bthci/bthci2/hciserverclient/src/hciserverclient.cpp
changeset 0 29b1cd4cb562
equal deleted inserted replaced
-1:000000000000 0:29b1cd4cb562
       
     1 // Copyright (c) 2006-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 /**
       
    17  @file
       
    18  @internalComponent
       
    19 */
       
    20 
       
    21 #include <bluetooth/hciserverclient.h>
       
    22 
       
    23 #include <bt_sock.h>
       
    24 #include <bluetooth/hci/hciipc.h>
       
    25 #include <bluetooth/logger.h>
       
    26 
       
    27 #ifdef __FLOG_ACTIVE
       
    28 _LIT8(KLogComponent, LOG_COMPONENT_HCISERVERCLIENT);
       
    29 #endif
       
    30 
       
    31 // Don't allocate memory for dedicated message slots, just use the ones from
       
    32 // the global pool.
       
    33 const TInt KAsyncMessageSlots = -1;
       
    34 
       
    35 EXPORT_C RHCIServerSession::RHCIServerSession() 
       
    36 	{
       
    37 	}
       
    38 
       
    39 /**
       
    40 Connect the handle to the server.
       
    41 Must be called before all other methods (except Version and Close).
       
    42 @return KErrNone if successful, a system-wide error code otherwise.
       
    43 */
       
    44 EXPORT_C TInt RHCIServerSession::Open(TInt aInterfaceUid)
       
    45 	{
       
    46 	TRAPD(err, OpenL(aInterfaceUid));
       
    47 	return err;
       
    48 	}
       
    49 
       
    50 void RHCIServerSession::OpenL(TInt aInterfaceUid)
       
    51 	{
       
    52 	// Attempt to connect to the latest version number
       
    53 	TVersion hciSvrVersion2(KHCISrvVersion2MajorVersionNumber, 
       
    54 							KHCISrvVersion2MinorVersionNumber, 
       
    55 							KHCISrvVersion2BuildNumber);
       
    56 
       
    57 	TInt err = CreateSession(KHCIServerName, hciSvrVersion2, KAsyncMessageSlots);
       
    58 	
       
    59 	if (err != KErrNone)
       
    60 		{
       
    61 		// The connection failed for some reason, this could be due to the version number not
       
    62 		// being supported, the server not being loaded or another reason. First attempt
       
    63 		// to load the Bluetooth stack and in turn the server.
       
    64 		LEAVEIFERRORL(iSocketServer.Connect());
       
    65 		CleanupClosePushL(iSocketServer);
       
    66 
       
    67 		TRequestStatus status;
       
    68 		iSocketServer.StartProtocol(KBTAddrFamily,KSockSeqPacket,KBTLinkManager,status);
       
    69 		User::WaitForRequest(status);
       
    70 		TInt err = status.Int();
       
    71 		if (err != KErrNone && err != KErrAlreadyExists)
       
    72 			{
       
    73 			LEAVEIFERRORL(err);
       
    74 			}
       
    75 
       
    76 		// Now attempt the connection again.
       
    77 		err = CreateSession(KHCIServerName, hciSvrVersion2, KAsyncMessageSlots);
       
    78 
       
    79 		if (err != KErrNone)
       
    80 			{
       
    81 			// Finally try reverting to the old version number.
       
    82 			TVersion hciSvrVersion1(KHCISrvMajorVersionNumber, 
       
    83 									KHCISrvMinorVersionNumber, 
       
    84 									KHCISrvBuildNumber);
       
    85 			
       
    86 			LEAVEIFERRORL(CreateSession(KHCIServerName, hciSvrVersion1, KAsyncMessageSlots));
       
    87 			}
       
    88 		
       
    89 		// Now we have a server side session setup, so now we will cleanup the socketserver
       
    90 		// on Close
       
    91 		CleanupStack::Pop();
       
    92 		}
       
    93 
       
    94 	// Now try and connect to the requested service.
       
    95 	CleanupClosePushL(*this);
       
    96 	LEAVEIFERRORL(SendReceive(EConnectToService, TIpcArgs(aInterfaceUid)));
       
    97 	CleanupStack::Pop();
       
    98 	}
       
    99 
       
   100 /**
       
   101 Close the session.
       
   102 */
       
   103 EXPORT_C void RHCIServerSession::Close()
       
   104 	{
       
   105 	if (iSocketServer.Handle())
       
   106 		{
       
   107 		// Release handle on the Bluetooth stack if we loaded it in OpenL
       
   108 		TRequestStatus status;
       
   109 		iSocketServer.StopProtocol(KBTAddrFamily,KSockSeqPacket,KBTLinkManager,status);
       
   110 		User::WaitForRequest(status);
       
   111 		
       
   112 		iSocketServer.Close();
       
   113 		}
       
   114 
       
   115 	// Close the session
       
   116 	RSessionBase::Close();
       
   117 	}
       
   118 
       
   119 /**
       
   120 Synchronous request to be sent to HCI.
       
   121 
       
   122 @param aInterfaceUid The type of request, for example power control, or direct access
       
   123 @param aCommand The actual request, for example SetPower
       
   124 @return KErrNone if successful, a system-wide error code otherwise.
       
   125 */
       
   126 EXPORT_C TInt RHCIServerSession::SendSync(TUint aCommand)
       
   127 	{
       
   128 	return SendSync(aCommand, NULL, NULL, 0);
       
   129 	}
       
   130 
       
   131 /**
       
   132 Asynchronous request to be sent to HCI.
       
   133 	
       
   134 @param aInterfaceUid The type of request, for example power control, or direct access
       
   135 @param aCommand The actual request, for example SetPower
       
   136 @param aStatus Used to alert user of completion of request.
       
   137 */
       
   138 EXPORT_C void RHCIServerSession::SendAsync(TUint aCommand, TRequestStatus &aStatus)
       
   139 	{
       
   140 	SendAsync(aCommand, NULL, NULL, 0, aStatus);
       
   141 	}
       
   142 
       
   143 /**
       
   144 Synchronous request to be sent to HCI.
       
   145 	
       
   146 @param aInterfaceUid The type of request, for example power control, or direct access
       
   147 @param aCommand The actual request, for example SetPower
       
   148 @param aDescIn The descriptor carries input information
       
   149 @param aDescOut The descriptor carries output information
       
   150 @param aValue A value for use with the command.
       
   151 @return KErrNone if successful, a system-wide error code otherwise.
       
   152 	*/
       
   153 EXPORT_C TInt RHCIServerSession::SendSync(TUint aCommand, TDes8* aDescIn, TDes8* aDescOut, TUint aValue)
       
   154 	{
       
   155 	return SendReceive(EServiceSpecificRequest,TIpcArgs(aCommand,aDescIn,aDescOut,aValue));
       
   156 	}
       
   157 
       
   158 /**
       
   159 Asynchronous request to be sent to HCI.
       
   160 	
       
   161 @param aInterfaceUid The type of request, for example power control, or direct access
       
   162 @param aCommand The actual request, for example SetPower
       
   163 @param aDescIn The descriptor carries input information
       
   164 @param aDescOut The descriptor carries output information
       
   165 @param aValue A value for use with the command.
       
   166 @param aStatus Used to alert user of completion of request.
       
   167 */
       
   168 EXPORT_C void RHCIServerSession::SendAsync(TUint aCommand, TDes8* aDescIn, TDes8* aDescOut, TUint aValue, TRequestStatus& aStatus)
       
   169 	{
       
   170 	SendReceive(EServiceSpecificRequest,TIpcArgs(aCommand,aDescIn,aDescOut,aValue),aStatus);
       
   171 	}