bluetoothcommsprofiles/btpan/panhelpersvr/panhelperdevicediscoverer.cpp
changeset 0 29b1cd4cb562
equal deleted inserted replaced
-1:000000000000 0:29b1cd4cb562
       
     1 // Copyright (c) 2004-2009 Nokia Corporation and/or its subsidiary(-ies).
       
     2 // All rights reserved.
       
     3 // This component and the accompanying materials are made available
       
     4 // under the terms of "Eclipse Public License v1.0"
       
     5 // which accompanies this distribution, and is available
       
     6 // at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     7 //
       
     8 // Initial Contributors:
       
     9 // Nokia Corporation - initial contribution.
       
    10 //
       
    11 // Contributors:
       
    12 //
       
    13 // Description:
       
    14 //
       
    15 
       
    16 #include "panhelperdevicediscoverer.h"
       
    17 using namespace PanHelper;
       
    18 
       
    19 CPanHelperDeviceDiscoverer* CPanHelperDeviceDiscoverer::NewL()
       
    20 /**
       
    21 Return a newly constructed device discoverer
       
    22 */
       
    23 	{
       
    24 	CPanHelperDeviceDiscoverer* self = new (ELeave) CPanHelperDeviceDiscoverer;
       
    25 	CleanupStack::PushL(self);
       
    26 	self->ConstructL();
       
    27 	CleanupStack::Pop(self);
       
    28 	return self;
       
    29 	}
       
    30 
       
    31 CPanHelperDeviceDiscoverer::~CPanHelperDeviceDiscoverer()
       
    32 /**
       
    33 Clean up
       
    34 */
       
    35 	{
       
    36 	iDeviceAddresses.Close();
       
    37 	iHostResolver.Close();
       
    38 	iSockServ.Close();
       
    39 	}
       
    40 	
       
    41 void CPanHelperDeviceDiscoverer::RunL()
       
    42 /**
       
    43 Found a new device
       
    44 */
       
    45 	{
       
    46 	if(iStatus==KErrNone)
       
    47 		{
       
    48 		TBTDevAddr newDevAddr(static_cast<TInquirySockAddr>(iNameEntry().iAddr).BTAddr());
       
    49 		TInt index = iDeviceAddresses.Find(newDevAddr);
       
    50 		if(index != KErrNotFound)
       
    51 			{
       
    52 			TPckg<TBTDevAddr> devAddrBuf(iDeviceAddresses[index]);
       
    53 			iRemDevAddrBuf->Copy(devAddrBuf);
       
    54 			User::RequestComplete(iClientStatus, KErrNone);	
       
    55 			Cancel();
       
    56 			}
       
    57 		else 
       
    58 			{
       
    59 			iHostResolver.Next(iNameEntry, iStatus);
       
    60 			SetActive();
       
    61 			}
       
    62 		}
       
    63 	else
       
    64 		{
       
    65 		User::RequestComplete(iClientStatus, iStatus.Int());
       
    66 		Cancel();	
       
    67 		}
       
    68 	}
       
    69 	
       
    70 void CPanHelperDeviceDiscoverer::DoCancel()
       
    71 /**
       
    72 Cancel HostResolver
       
    73 */
       
    74 	{
       
    75 	iHostResolver.Cancel();
       
    76 	}
       
    77 	
       
    78 void CPanHelperDeviceDiscoverer::FindDevice(TDesC& aDeviceAddrList, TDes8& aDeviceAddr, TRequestStatus& aStatus)
       
    79 /**
       
    80 Start a generic inquiry
       
    81 */
       
    82 	{
       
    83 	iRemDevAddrBuf = (TPckgBuf<TBTDevAddr>*) &aDeviceAddr;
       
    84 	iClientStatus = &aStatus;
       
    85 	ConvertListToArray(aDeviceAddrList);
       
    86 	iHostResolver.GetByAddress(iInquiryAddr, iNameEntry, iStatus);
       
    87 	SetActive();
       
    88 	}
       
    89 	
       
    90 void CPanHelperDeviceDiscoverer::CancelFindDevice()
       
    91 /**
       
    92 Cancel
       
    93 */
       
    94 	{
       
    95 	Cancel();
       
    96 	User::RequestComplete(iClientStatus, KErrCancel);
       
    97 	}
       
    98 	
       
    99 CPanHelperDeviceDiscoverer::CPanHelperDeviceDiscoverer() :
       
   100 	CActive(EPriorityStandard)
       
   101 /**
       
   102 Constructor
       
   103 */
       
   104 	{
       
   105 	CActiveScheduler::Add(this);
       
   106 	iInquiryAddr.SetIAC(KGIAC);
       
   107 	iInquiryAddr.SetAction(KHostResInquiry); // don't need device names here
       
   108 	}
       
   109 	
       
   110 void CPanHelperDeviceDiscoverer::ConstructL()
       
   111 /**
       
   112 Initialise
       
   113 */
       
   114 	{
       
   115 	User::LeaveIfError(iSockServ.Connect());
       
   116 	User::LeaveIfError(iHostResolver.Open(iSockServ, KBTAddrFamily, KBTLinkManager));
       
   117 	}
       
   118 	
       
   119 void CPanHelperDeviceDiscoverer::ConvertListToArray(TDesC& aDeviceAddrList)
       
   120 /**
       
   121 Put the comma-separated list of readable BT device addresses into an array
       
   122 */
       
   123 	{
       
   124 	TBTDevAddr devAddr;
       
   125 	TLex lex(aDeviceAddrList);
       
   126 
       
   127 	TChar charComma(KCharComma);	// this weird construct is needed to prevent us needing static data
       
   128 	while(lex.Peek()!=charComma && !lex.Eos())
       
   129 	{
       
   130 		lex.Mark();
       
   131 		while((lex.Get()!=charComma) && !lex.Eos())
       
   132 			{
       
   133 			
       
   134 			}
       
   135 		devAddr.SetReadable(lex.MarkedToken());
       
   136 
       
   137 		// This is a best efforst service.  If we can't add the address
       
   138 		// to our search list we'll just continue with the ones we could.
       
   139 		(void)iDeviceAddresses.Append(devAddr);
       
   140 		}
       
   141 	}