bluetoothengine/bthid/bthidserver/src/bthidserver.cpp
changeset 24 8930e1d1d530
parent 1 6a1fe72036e3
child 39 5a89845f78e2
equal deleted inserted replaced
23:988cddd6adbd 24:8930e1d1d530
    36 #include "debug.h"
    36 #include "debug.h"
    37 #include "debugconfig.h"
    37 #include "debugconfig.h"
    38 #include "hidgeneric.h"
    38 #include "hidgeneric.h"
    39 #include "hidlayoutids.h"
    39 #include "hidlayoutids.h"
    40 #include "bthidPsKey.h"
    40 #include "bthidPsKey.h"
       
    41 #include "hidsdpclient.h"
    41 
    42 
    42 
    43 
    43 #ifndef DBG
    44 #ifndef DBG
    44 #ifdef _DEBUG
    45 #ifdef _DEBUG
    45 #define DBG(a) a
    46 #define DBG(a) a
    96 
    97 
    97     iReqs.ResetAndDestroy();
    98     iReqs.ResetAndDestroy();
    98     
    99     
    99     delete iGenHID;
   100     delete iGenHID;
   100     
   101     
       
   102     delete iHidSdpClient;
       
   103     
   101     RProperty::Delete( KPSUidBthidSrv, KBTMouseCursorState );
   104     RProperty::Delete( KPSUidBthidSrv, KBTMouseCursorState );
   102     }
   105     }
   103 
   106 
   104 CBTHidServer* CBTHidServer::NewL()
   107 CBTHidServer* CBTHidServer::NewL()
   105     {
   108     {
   239 
   242 
   240 void CBTHidServer::InformClientsOfStatusChange(
   243 void CBTHidServer::InformClientsOfStatusChange(
   241         const CBTHidDevice& aDeviceDetails, TBTHidConnState aState)
   244         const CBTHidDevice& aDeviceDetails, TBTHidConnState aState)
   242     {
   245     {
   243         TRACE_INFO( (_L("[BTHID]\tCBTHidServer::InformClientsOfStatusChange, state=%d"),aState) );
   246         TRACE_INFO( (_L("[BTHID]\tCBTHidServer::InformClientsOfStatusChange, state=%d"),aState) );
   244     if (aState == EBTDeviceConnected || aState == EBTDeviceLinkRestored
   247         if (aState == EBTDeviceConnected || 
   245             || aState == EBTDeviceLinkLost || aState == EBTDeviceDisconnected)
   248 	     aState == EBTDeviceLinkRestored || 
       
   249 		 aState == EBTDeviceLinkLost     || 
       
   250 		 aState == EBTDeviceDisconnected || 
       
   251 		 aState == EBTDeviceConnectedFromRemote)
   246         {
   252         {
   247         iLastUsedAddr = aDeviceDetails.iAddress;
   253         iLastUsedAddr = aDeviceDetails.iAddress;
   248         iActiveState = ETrue;
   254         iActiveState = ETrue;
   249         }
   255         }
   250     else
   256     else
   284         TBTHidConnState aState)
   290         TBTHidConnState aState)
   285     {
   291     {
   286     switch (aState)
   292     switch (aState)
   287         {
   293         {
   288         case EBTDeviceLinkRestored:
   294         case EBTDeviceLinkRestored:
       
   295         case EBTDeviceConnectedFromRemote:
   289             {
   296             {
   290             HandleAsyncRequest(aDeviceAddr, EBTConnected);
   297             HandleAsyncRequest(aDeviceAddr, EBTConnected);
   291             break;
   298             break;
   292             }
   299             }
   293 
   300 
   482             if (devDetails.iAddress == aAddress)
   489             if (devDetails.iAddress == aAddress)
   483                 {
   490                 {
   484                 foundItem = ETrue;
   491                 foundItem = ETrue;
   485                 TBTConnectionState HidConnectionStatus =
   492                 TBTConnectionState HidConnectionStatus =
   486                         connection->ConnectStatus();
   493                         connection->ConnectStatus();
   487                 if (EFirstConnection == HidConnectionStatus || EConnecting
   494                 if ( (EFirstConnection   == HidConnectionStatus) || 
   488                         == HidConnectionStatus || EHIDReconnecting
   495 				     (EConnecting        == HidConnectionStatus) || 
   489                         == HidConnectionStatus || EHostReconnecting
   496                      (EHIDReconnecting   == HidConnectionStatus) || 
   490                         == HidConnectionStatus)
   497                      (EHostReconnecting  == HidConnectionStatus) || 
       
   498                      (EHIDInitConnecting == HidConnectionStatus) )
   491                     {
   499                     {
   492                     retVal = EBTEngConnecting;
   500                     retVal = EBTEngConnecting;
   493                     }
   501                     }
   494                 if (EConnected == HidConnectionStatus)
   502                 if (EConnected == HidConnectionStatus)
   495                     {
   503                     {
   502         }
   510         }
   503 
   511 
   504     return retVal;
   512     return retVal;
   505     }
   513     }
   506 
   514 
       
   515 TBool CBTHidServer::DeviceExistInContainer(const TBTDevAddr& aAddress)
       
   516     {
       
   517     TInt i = 0;
       
   518     TBool foundItem = EFalse;
       
   519     TInt BTConnectionObjCount = iBTConnContainer->Count();
       
   520 
       
   521     TRACE_INFO(_L("[BTHID]\tCBTHidServer::DeviceExistInContainer()"));
       
   522     while ((i < BTConnectionObjCount) && (!foundItem))
       
   523         {
       
   524         CBTHidConnection *connection =
       
   525                 static_cast<CBTHidConnection*> ((*iBTConnContainer)[i]);
       
   526 
       
   527         if (connection)
       
   528             {
       
   529             CBTHidDevice& devDetails = connection->DeviceDetails();
       
   530 
       
   531             if (devDetails.iAddress == aAddress)
       
   532                 {
       
   533                 foundItem = ETrue;                
       
   534                 }
       
   535             }
       
   536         i++;
       
   537         }
       
   538 
       
   539     return foundItem;
       
   540     }
       
   541 
   507 TBool CBTHidServer::GetConnectionAddress(TDes8& aAddressBuf)
   542 TBool CBTHidServer::GetConnectionAddress(TDes8& aAddressBuf)
   508     {
   543     {
   509     TInt i = 0;
   544     TInt i = 0;
   510     TBool retVal = EFalse;
   545     TBool retVal = EFalse;
   511     TInt BTConnectionObjCount = iBTConnContainer->Count();
   546     TInt BTConnectionObjCount = iBTConnContainer->Count();
   547         if (connection)
   582         if (connection)
   548             {
   583             {
   549             CBTHidDevice& devDetails = connection->DeviceDetails();
   584             CBTHidDevice& devDetails = connection->DeviceDetails();
   550             TBTConnectionState HidConnectionStatus =
   585             TBTConnectionState HidConnectionStatus =
   551                     connection->ConnectStatus();
   586                     connection->ConnectStatus();
   552             if (connection->IsConnected() || HidConnectionStatus
   587             if (connection->IsConnected() || 
   553                     == EHIDReconnecting)
   588 			      HidConnectionStatus == EHIDReconnecting || 
       
   589 				  HidConnectionStatus == EHIDInitConnecting)
   554                 {
   590                 {
   555                 if ((IsKeyboard(aDeviceSubClass) && IsKeyboard(
   591                 if ((IsKeyboard(aDeviceSubClass) && IsKeyboard(
   556                         devDetails.iDeviceSubClass)) || (IsPointer(
   592                         devDetails.iDeviceSubClass)) || (IsPointer(
   557                         aDeviceSubClass) && IsPointer(
   593                         aDeviceSubClass) && IsPointer(
   558                         devDetails.iDeviceSubClass)))
   594                         devDetails.iDeviceSubClass)))
   595     TInt i = 0;
   631     TInt i = 0;
   596     TBool retVal = ETrue;
   632     TBool retVal = ETrue;
   597     TInt BTConnectionObjCount = iBTConnContainer->Count();
   633     TInt BTConnectionObjCount = iBTConnContainer->Count();
   598 
   634 
   599     TUint deviceSubClass = GetDeviceSubClass(aDevAddr);
   635     TUint deviceSubClass = GetDeviceSubClass(aDevAddr);
   600         TRACE_INFO(_L("[BTHID]\tCBTHidServer::IsAllowToConnectFromClientSide()"));
   636     TRACE_INFO( (_L("[BTHID]\tCBTHidServer::IsAllowToConnectFromClientSide() BTConnectionObjCount = %d"), BTConnectionObjCount) );
   601     while ((i < BTConnectionObjCount) && retVal)
   637     while ((i < BTConnectionObjCount) && retVal)
   602         {
   638         {
   603         CBTHidConnection *connection =
   639         CBTHidConnection *connection =
   604                 static_cast<CBTHidConnection*> ((*iBTConnContainer)[i]);
   640                 static_cast<CBTHidConnection*> ((*iBTConnContainer)[i]);
   605 
   641 
   606         if (connection)
   642         if (connection)
   607             {
   643             {
   608             CBTHidDevice& devDetails = connection->DeviceDetails();
   644             CBTHidDevice& devDetails = connection->DeviceDetails();
   609             TBTConnectionState HidConnectionStatus =
   645             TBTConnectionState HidConnectionStatus =
   610                     connection->ConnectStatus();
   646                     connection->ConnectStatus();
   611             if (connection->IsConnected() || HidConnectionStatus
   647             if (connection->IsConnected() || 
   612                     == EConnecting)
   648 			       HidConnectionStatus == EConnecting || 
       
   649 				   HidConnectionStatus == EHIDInitConnecting)
   613                 {
   650                 {
   614                 if (devDetails.iAddress != aDevAddr)
   651                 if (devDetails.iAddress != aDevAddr)
   615                     {
   652                     {                    
   616                     if ((IsKeyboard(deviceSubClass) && IsKeyboard(
   653                     if ((IsKeyboard(deviceSubClass) && IsKeyboard(
   617                             devDetails.iDeviceSubClass)) || (IsPointer(
   654                             devDetails.iDeviceSubClass)) || (IsPointer(
   618                             deviceSubClass) && IsPointer(
   655                             deviceSubClass) && IsPointer(
   619                             devDetails.iDeviceSubClass)))
   656                             devDetails.iDeviceSubClass)))
   620                         {
   657                         {
       
   658                         TRACE_INFO(_L("[BTHID]\tCBTHidServer::() NO connection allowed, connection exist already!"));
   621                         retVal = EFalse;
   659                         retVal = EFalse;
   622                         iConflictAddr = devDetails.iAddress;
   660                         iConflictAddr = devDetails.iAddress;
   623                         }
   661                         }
   624                     }
   662                     }
   625                 }
   663                 }
   697         }
   735         }
   698 
   736 
   699     return;
   737     return;
   700     } 
   738     } 
   701 
   739 
   702 TInt CBTHidServer::NewConnectionL()
   740 TInt CBTHidServer::NewConnectionL(TBTConnectionState aConnectionStatus)
   703     {
   741     {
   704         TRACE_INFO(_L("[BTHID]\tCBTHidServer::NewConnectionL"));
   742         TRACE_INFO(_L("[BTHID]\tCBTHidServer::NewConnectionL"));
       
   743      __ASSERT_DEBUG( aConnectionStatus == EConnecting || aConnectionStatus == EHIDInitConnecting ,
       
   744                                CBTHidServer::PanicServer(EBadState));
       
   745      
   705     CBTHidConnection *connection = CBTHidConnection::NewLC(iSocketServ,
   746     CBTHidConnection *connection = CBTHidConnection::NewLC(iSocketServ,
   706             *this, EConnecting);
   747             *this, aConnectionStatus);
   707     // Add to the connection container object.
   748     // Add to the connection container object.
   708     iBTConnContainer->AddL(connection);
   749     iBTConnContainer->AddL(connection);
   709     CleanupStack::Pop(); // connection
   750     CleanupStack::Pop(); // connection
   710 
   751 
   711     // Now add the object to the index to get an id.
   752     // Now add the object to the index to get an id.
  1010 
  1051 
  1011         session->InformConnectionResult(aConnID, error);
  1052         session->InformConnectionResult(aConnID, error);
  1012         }
  1053         }
  1013     }
  1054     }
  1014 
  1055 
       
  1056 void CBTHidServer::FirstTimeConnectionCompleteFromRemote(TInt aConnID, TInt aStatus)
       
  1057     {
       
  1058         TRACE_INFO( (_L("[BTHID]\tCBTHidServer::FirstTimeConnectionCompleteFromRemote(%d)"), aStatus));
       
  1059     TInt error = aStatus;
       
  1060 
       
  1061     CBTHidConnection* connection =
       
  1062             static_cast<CBTHidConnection*> (iBTConnIndex->At(aConnID));
       
  1063     __ASSERT_ALWAYS(connection, PanicServer(EInvalidHandle));
       
  1064 
       
  1065     if (error == KErrNone)
       
  1066         {
       
  1067         TBool genHidConnected = EFalse;
       
  1068 
       
  1069             TRAP( error,
       
  1070                     // Inform the Generic HID of the Connection
       
  1071                     GenericHIDConnectL(connection, ETrue);
       
  1072 
       
  1073                     // Record that we got as far as informing the Generic HID.
       
  1074                     genHidConnected = ETrue;
       
  1075 
       
  1076                     // Try to start monitoring the channels.
       
  1077                     connection->StartMonitoringChannelsL();
       
  1078             )
       
  1079 
       
  1080         if (error != KErrNone)
       
  1081             {
       
  1082             // If we informed the Generic HID of the connection, then
       
  1083             // we must also disconnect.
       
  1084             if (genHidConnected)
       
  1085                 {
       
  1086                 iGenHID->Disconnected(aConnID);
       
  1087                 }
       
  1088 
       
  1089             // Delete the connection object.
       
  1090             //Quietly refuse the remote initialized connection in case of error. 
       
  1091             //No need to bother user.
       
  1092             iBTConnIndex->Remove(aConnID);
       
  1093             }
       
  1094         else
       
  1095             {
       
  1096             // Update the stored devices, as we could have power off
       
  1097             // and no clean shutdown.
       
  1098             // Use the non-leaving version.
       
  1099             CleanOldConnection(aConnID);
       
  1100             StoreVirtuallyCabledDevices(KFileStore);
       
  1101             InformClientsOfStatusChange(connection->DeviceDetails(),
       
  1102                     EBTDeviceConnectedFromRemote);
       
  1103             }
       
  1104         }
       
  1105     else
       
  1106         {
       
  1107         //Quietly refuse the remote initialized connection in case of error.
       
  1108         //No need to bother user.
       
  1109         iBTConnIndex->Remove(aConnID);
       
  1110         }
       
  1111     }
       
  1112 
       
  1113 void CBTHidServer::StartSDPSearch(TInt aConnID)
       
  1114     {
       
  1115     iConnID = aConnID;
       
  1116     TRACE_INFO( (_L("[BTHID]\tCBTHidServer::StartSDPSearch aConnID= (%d)"), aConnID));
       
  1117     CBTHidConnection* connection =
       
  1118             static_cast<CBTHidConnection*> (iBTConnIndex->At(iConnID));
       
  1119     __ASSERT_ALWAYS(connection, PanicServer(EInvalidHandle));
       
  1120     
       
  1121     TRAPD( res,
       
  1122                 // Retrieve the hid device object for this new connection
       
  1123                 CBTHidDevice &devDetails =
       
  1124                 ConnectionDetailsL(iConnID);
       
  1125 
       
  1126                 // Create a new HID Sdp Client
       
  1127                 // Its only used here so it doesn't matter if we leave.
       
  1128                 delete iHidSdpClient;
       
  1129                 iHidSdpClient = 0;
       
  1130                 //Create a new hid sdp client using the hid device object.
       
  1131                 iHidSdpClient = CHidSdpClient::NewL(devDetails, *this);
       
  1132 
       
  1133                 // Start the hid sdp client
       
  1134                 iHidSdpClient->StartL();
       
  1135         )
       
  1136     
       
  1137     if (res != KErrNone)
       
  1138         {
       
  1139         // Get the server to delete the new connection object
       
  1140         DeleteNewConnection(iConnID);
       
  1141         }
       
  1142     }
       
  1143 
       
  1144 void CBTHidServer::HidSdpSearchComplete(TInt aResult)
       
  1145     {
       
  1146     TRACE_FUNC(_L("[BTHID]\tCBTHidServer::HidSdpSearchComplete"));
       
  1147     // This is a callback from the Hid SDP client so we can't delete it here
       
  1148     // Get it to destroy itself when its convenient.
       
  1149     iHidSdpClient->Kill();
       
  1150     // Deleted outside destructor.
       
  1151     iHidSdpClient = 0;
       
  1152 
       
  1153     // If the SDP search was a success
       
  1154     if (aResult == KErrNone)
       
  1155         {       
       
  1156         // Try to connect to the device as a HID
       
  1157         CBTHidConnection* connection =
       
  1158                 static_cast<CBTHidConnection*> (iBTConnIndex->At(iConnID));
       
  1159         __ASSERT_ALWAYS(connection, PanicServer(EInvalidHandle));
       
  1160         if (connection)
       
  1161             {
       
  1162             CBTHidDevice& devDetails = connection->DeviceDetails();
       
  1163             
       
  1164             //Only after SDP search complete, do we know the CoD which is needed
       
  1165             //to tell if the incoming connection is allowed or not.
       
  1166             //ETrue , establish the connection.
       
  1167             //EFalse, refuse the remote connecion sliently
       
  1168             if (IsAllowToConnectFromClientSide(devDetails.iAddress))
       
  1169                 {
       
  1170                 FirstTimeConnectionCompleteFromRemote(iConnID, aResult);
       
  1171                 }
       
  1172             else
       
  1173                 {
       
  1174                 FirstTimeConnectionCompleteFromRemote(iConnID, KErrAlreadyExists);
       
  1175                 }
       
  1176             }
       
  1177         
       
  1178         }
       
  1179     }
       
  1180 
       
  1181 
  1015 void CBTHidServer::LinkLost(TInt aConnID)
  1182 void CBTHidServer::LinkLost(TInt aConnID)
  1016     {
  1183     {
  1017         TRACE_INFO( (_L("[BTHID]\tCBTHidServer::LinkLost(%d)"), aConnID));
  1184         TRACE_INFO( (_L("[BTHID]\tCBTHidServer::LinkLost(%d)"), aConnID));
  1018     // Stop the driver.
  1185     // Stop the driver.
  1019     iGenHID->DriverActive(aConnID, CHidTransport::ESuspend);
  1186     iGenHID->DriverActive(aConnID, CHidTransport::ESuspend);
  1124         ShutdownListeners(aErrorCode);
  1291         ShutdownListeners(aErrorCode);
  1125         }
  1292         }
  1126     else
  1293     else
  1127         {
  1294         {
  1128         TInt i = 0;
  1295         TInt i = 0;
  1129         TInt count = iBTConnContainer->Count();
  1296         
  1130         TInt err = KErrNone;
  1297         TInt err = KErrNone;
  1131 
  1298 
       
  1299         TInt connectingID = 0;
  1132         // Check which port has accepted a connection
  1300         // Check which port has accepted a connection
  1133         switch (aPort)
  1301         switch (aPort)
  1134             {
  1302             {
  1135             // The HID Control Channel
  1303             // The HID Control Channel
  1136             case KL2CAPHidControl:
  1304             case KL2CAPHidControl:
  1137                 // Get the BT address of the device that has connected
  1305                 // Get the BT address of the device that has connected
  1138                 iTempControl->RemoteName(sockAddr);
  1306                 iTempControl->RemoteName(sockAddr);
  1139                 devAddr = sockAddr.BTAddr();
  1307                 devAddr = sockAddr.BTAddr();
       
  1308                 // incoming HID connection is allowed
       
  1309                 if (!DeviceExistInContainer(devAddr))
       
  1310                     {                    
       
  1311                     TRAPD( res,                            
       
  1312                            // to be created as New if device not yet listed in container
       
  1313                            connectingID = NewConnectionL(EHIDInitConnecting);
       
  1314                     
       
  1315                            // Retrieve the hid device object for this new connection
       
  1316                            CBTHidDevice &devDetails =
       
  1317                            ConnectionDetailsL(connectingID);
       
  1318 
       
  1319                            // Fill in the information we got from the client
       
  1320                            devDetails.iAddress = devAddr;
       
  1321                            devDetails.iUseSecurity = ETrue;
       
  1322                            )
       
  1323 
       
  1324                      if (res != KErrNone && connectingID != 0)
       
  1325                          {
       
  1326                          // Get the server to delete the new connection object
       
  1327                          DeleteNewConnection(connectingID);
       
  1328                          }
       
  1329                      }
       
  1330 
  1140                 if (IsAllowToConnectFromClientSide(devAddr))
  1331                 if (IsAllowToConnectFromClientSide(devAddr))
  1141                     {
  1332                     {
       
  1333                     TInt count = iBTConnContainer->Count();
  1142                     while ((i < count) && (iTempControl))
  1334                     while ((i < count) && (iTempControl))
  1143                         {
  1335                         {
  1144                         CBTHidConnection
  1336                         CBTHidConnection
  1145                                 * connection =
  1337                                 * connection =
  1146                                         static_cast<CBTHidConnection*> ((*iBTConnContainer)[i]);
  1338                                         static_cast<CBTHidConnection*> ((*iBTConnContainer)[i]);
  1182                 if (err != KErrNone)
  1374                 if (err != KErrNone)
  1183                     {
  1375                     {
  1184                         TRACE_INFO(_L("[BTHID]\tCBTHidServer::SocketAccepted, control channel failed, shutdown listener"));
  1376                         TRACE_INFO(_L("[BTHID]\tCBTHidServer::SocketAccepted, control channel failed, shutdown listener"));
  1185                     ShutdownListeners(err);
  1377                     ShutdownListeners(err);
  1186                     }
  1378                     }
  1187 
  1379                 
  1188                 break;
  1380                 break;
  1189 
  1381 
  1190                 // The HID Interrupt Channel
  1382                 // The HID Interrupt Channel
  1191             case KL2CAPHidInterrupt:
  1383             case KL2CAPHidInterrupt:
  1192                 // Get the BT address of the device that has connected
  1384                 // Get the BT address of the device that has connected
  1193                 iTempInterrupt->RemoteName(sockAddr);
  1385                 iTempInterrupt->RemoteName(sockAddr);
  1194                 devAddr = sockAddr.BTAddr();
  1386                 devAddr = sockAddr.BTAddr();
  1195                 if (IsAllowToConnectFromClientSide(devAddr))
  1387                 if (IsAllowToConnectFromClientSide(devAddr))
  1196                     {
  1388                     {
       
  1389                     TInt count = iBTConnContainer->Count();
  1197                     while ((i < count) && (iTempInterrupt))
  1390                     while ((i < count) && (iTempInterrupt))
  1198                         {
  1391                         {
  1199                         CBTHidConnection
  1392                         CBTHidConnection
  1200                                 *connection =
  1393                                 *connection =
  1201                                         static_cast<CBTHidConnection*> ((*iBTConnContainer)[i]);
  1394                                         static_cast<CBTHidConnection*> ((*iBTConnContainer)[i]);