bluetoothengine/bthid/bthidserver/src/bthidconnection.cpp
changeset 0 f63038272f30
child 11 a42ed326b458
equal deleted inserted replaced
-1:000000000000 0:f63038272f30
       
     1 /*
       
     2 * Copyright (c) 2008 Nokia Corporation and/or its subsidiary(-ies).
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description:  This is the implementation of application class
       
    15  *
       
    16 */
       
    17 
       
    18 
       
    19 #include <e32debug.h>
       
    20 #include "hiddebug.h"
       
    21 #include "bthidconnection.h"
       
    22 #include "btconnectionobserver.h"
       
    23 #include "sockets.pan"
       
    24 #include "socketinitiator.h"
       
    25 #include "socketreader.h"
       
    26 #include "socketwriter.h"
       
    27 #include "datasegmenter.h"
       
    28 #include "timeouttimer.h"
       
    29 #include "bthidtypes.h"
       
    30 #include "bthiddevice.h"
       
    31 #include "hidinterfaces.h"
       
    32 #include "debug.h"
       
    33 
       
    34 CBTHidConnection* CBTHidConnection::NewL(RSocketServ& aSocketServ,
       
    35         MBTConnectionObserver& aObserver, TBTConnectionState aConnectionState)
       
    36     {
       
    37     CBTHidConnection* self = CBTHidConnection::NewLC(aSocketServ, aObserver,
       
    38             aConnectionState);
       
    39     CleanupStack::Pop(self);
       
    40     return self;
       
    41     }
       
    42 
       
    43 CBTHidConnection* CBTHidConnection::NewLC(RSocketServ& aSocketServ,
       
    44         MBTConnectionObserver& aObserver, TBTConnectionState aConnectionState)
       
    45     {
       
    46     CBTHidConnection* self = new (ELeave) CBTHidConnection(aSocketServ,
       
    47             aObserver, aConnectionState);
       
    48     // This is a CObject derived class so must be closed to destroy it.
       
    49     CleanupClosePushL(*self);
       
    50     self->ConstructL();
       
    51     return self;
       
    52     }
       
    53 
       
    54 CBTHidConnection::CBTHidConnection(RSocketServ& aSocketServ,
       
    55         MBTConnectionObserver& aObserver, TBTConnectionState aConnectionState) :
       
    56     iSocketServ(aSocketServ), iObserver(aObserver)
       
    57     {
       
    58     ChangeState(aConnectionState);
       
    59     }
       
    60 
       
    61 CBTHidConnection::~CBTHidConnection()
       
    62     {
       
    63     delete iControlSocketReader;
       
    64 
       
    65     delete iControlSocketWriter;
       
    66 
       
    67     delete iInterruptSocketReader;
       
    68 
       
    69     delete iInterruptSocketWriter;
       
    70 
       
    71     delete iSocketInitiator;
       
    72 
       
    73     delete iCommandSegmenter;
       
    74 
       
    75     delete iControlDataBuffer;
       
    76 
       
    77     delete iInterruptDataBuffer;
       
    78 
       
    79     delete iInactivityTimer;
       
    80 
       
    81     delete iDevice;
       
    82 
       
    83     // Close connections in the reverse order to when they were opened
       
    84     if (iInterruptSocket)
       
    85         {
       
    86         iInterruptSocket->Close();
       
    87         delete iInterruptSocket;
       
    88         }
       
    89 
       
    90     if (iControlSocket)
       
    91         {
       
    92         iControlSocket->Close();
       
    93         delete iControlSocket;
       
    94         }
       
    95     }
       
    96 
       
    97 TInt CBTHidConnection::ConnID()
       
    98     {
       
    99     return iConnID;
       
   100     }
       
   101 
       
   102 void CBTHidConnection::SetConnID(TInt aConnID)
       
   103     {
       
   104     // We shouldn't be trying to set the id for this connection
       
   105     // after it is connected.
       
   106     __ASSERT_DEBUG(iConnectionState == ENotConnected || iConnectionState == EConnecting,
       
   107             User::Panic(KPanicBTConnection, ESocketsBadState));
       
   108 
       
   109     iConnID = aConnID;
       
   110     }
       
   111 
       
   112 void CBTHidConnection::ConstructL()
       
   113     {
       
   114         TRACE_FUNC
       
   115     (_L("[BTHID]\tCBTHidConnection::ConstructL"));
       
   116     //ChangeState(ENotConnected);
       
   117 
       
   118     // A new hid device information object.
       
   119     iDevice = CBTHidDevice::NewL();
       
   120 
       
   121     // Socket readers and writer objects to perform asynchronous requests
       
   122     iControlSocketReader = CSocketReader::NewL(EControlSocketID, *this,
       
   123             KL2CAPDefaultMTU);
       
   124 
       
   125     iControlSocketWriter = CSocketWriter::NewL(EControlSocketID, *this);
       
   126 
       
   127     iInterruptSocketReader = CSocketReader::NewL(EInterruptSocketID, *this,
       
   128             KL2CAPDefaultMTU);
       
   129 
       
   130     iInterruptSocketWriter = CSocketWriter::NewL(EInterruptSocketID, *this);
       
   131 
       
   132     // Socket initiator to perform socket connections
       
   133     iSocketInitiator = CSocketInitiator::NewL(iSocketServ, *this);
       
   134 
       
   135     // Create a timeout timer
       
   136     iInactivityTimer = CTimeOutTimer::NewL(EPriorityHigh, *this);
       
   137     }
       
   138 
       
   139 void CBTHidConnection::ConnectL()
       
   140     {
       
   141         TRACE_FUNC
       
   142     (_L("[BTHID]\tCBTHidConnection::ConnectL"));
       
   143     // If we aren't in the correct stiDeviceate for an initial connection Panic.
       
   144     __ASSERT_DEBUG(iConnectionState == ENotConnected || iConnectionState == EConnecting,
       
   145             User::Panic(KPanicBTConnection, ESocketsBadState));
       
   146 
       
   147     // Create the two sockets.
       
   148 
       
   149     delete iControlSocket;
       
   150     iControlSocket = 0;
       
   151     iControlSocket = new (ELeave) RSocket;
       
   152 
       
   153     // If we leave after this iControlSocket will not be NULL.
       
   154     // This is ok since any place where it can be set will delete it first
       
   155     // or it will be finally deleted in the destructor
       
   156     delete iInterruptSocket;
       
   157     iInterruptSocket = 0;
       
   158     iInterruptSocket = new (ELeave) RSocket;
       
   159 
       
   160     // If we leave after this iControlSocket & iInterruptSocket will not
       
   161     // be NULL.
       
   162     // This is ok since any place where they can be set will delete first
       
   163     // or they will be finally deleted in the destructor
       
   164 
       
   165     // Ask the socket initiator to connect these sockets,
       
   166     // giving it the bluetooth address of the device and a security flag.
       
   167     iSocketInitiator->ConnectSocketsL(iDevice->iAddress,
       
   168             iDevice->iUseSecurity, iControlSocket, iInterruptSocket);
       
   169     ChangeState(EFirstConnection);
       
   170     }
       
   171 
       
   172 void CBTHidConnection::ReconnectL()
       
   173     {
       
   174         TRACE_FUNC
       
   175     (_L("[BTHID]\tCBTHidConnection::ReconnectL"));
       
   176     // If we aren't in the correct state for an reconnection Panic.
       
   177     __ASSERT_DEBUG(iConnectionState == ENotConnected,
       
   178             User::Panic(KPanicBTConnection, ESocketsBadState));
       
   179 
       
   180     // Expect the device to reconnect for now
       
   181     ChangeState(ELinkLost);
       
   182     }
       
   183 
       
   184 void CBTHidConnection::Disconnect() //Virtual Cable Unplug. (this is not Bluetooth disconnect)
       
   185     {
       
   186         TRACE_FUNC
       
   187     (_L("[BTHID]\tCBTHidConnection::Disconnect (Virtual Cable Unplug)"));
       
   188     if (iConnectionState == EConnected)
       
   189         {
       
   190         //First Send VCUnplug
       
   191         iCommandBuffer.Zero();
       
   192         iCommandBuffer.Append(EHIDControlVCUnplug);
       
   193 
       
   194         TRequestStatus status;
       
   195         iControlSocket->Write(iCommandBuffer, status);
       
   196         User::WaitForRequest(status);
       
   197 
       
   198         //Then wait for a reply from Su-8W on L2CAP channel(s). If not waited, Su-8W will go mad.
       
   199         User::After(500000); //0.5 sec
       
   200 
       
   201         //but we never handle that acknowledgement (because it is not required by BT HID
       
   202         //specification, but Su-8W needs to send something.)
       
   203         iControlSocketWriter->Cancel();
       
   204         iControlSocketReader->Cancel();
       
   205         iInterruptSocketReader->Cancel();
       
   206         iInterruptSocketWriter->Cancel();
       
   207         }
       
   208     }
       
   209 
       
   210 void CBTHidConnection::OfferControlSocket(const TBTDevAddr& aAddress,
       
   211         RSocket*& aSocket)
       
   212     {
       
   213         TRACE_FUNC
       
   214     (_L("[BTHID]\tCBTHidConnection::OfferControlSocket"));
       
   215     if (aAddress == iDevice->iAddress)
       
   216         {
       
   217         __ASSERT_DEBUG((iConnectionState == ELinkLost) ||
       
   218                 (iConnectionState == EHIDReconnecting),
       
   219                 User::Panic(KPanicBTConnection, ESocketsBadState));
       
   220 
       
   221         // Take ownership of this socket
       
   222         delete iControlSocket;
       
   223         iControlSocket = aSocket;
       
   224         aSocket = 0;
       
   225 
       
   226         // Mark that the HID Device is reconnecting to us.
       
   227         ChangeState(EHIDReconnecting);
       
   228         }
       
   229     }
       
   230 
       
   231 void CBTHidConnection::OfferInterruptSocket(const TBTDevAddr& aAddress,
       
   232         RSocket*& aSocket)
       
   233     {
       
   234         TRACE_FUNC
       
   235     (_L("[BTHID]\tCBTHidConnection::OfferInterruptSocket"));
       
   236 
       
   237     if (aAddress == iDevice->iAddress)
       
   238         {
       
   239         __ASSERT_DEBUG((iConnectionState == EHIDReconnecting), //||(iConnectionState == ELinkLost) ,
       
   240                 User::Panic(KPanicBTConnection, ESocketsBadState));
       
   241 
       
   242         // Take ownership of this socket
       
   243         delete iInterruptSocket;
       
   244         iInterruptSocket = aSocket;
       
   245         aSocket = 0;
       
   246         TRAPD(error, PrepareSocketsL());
       
   247 
       
   248         if (KErrNone == error)
       
   249             {
       
   250             // Mark that we are now reconnected.
       
   251 
       
   252             ChangeState(EConnected);
       
   253 
       
   254             // Inform the observer that the connection has been restored.
       
   255             iObserver.LinkRestored(iConnID);
       
   256 
       
   257             }
       
   258         else
       
   259             {
       
   260             // Close the sockets as they can't be used
       
   261             CloseChannels();
       
   262             ChangeState(ELinkLost);
       
   263             }
       
   264         }
       
   265     }
       
   266 
       
   267 CBTHidDevice& CBTHidConnection::DeviceDetails()
       
   268     {
       
   269     return (*iDevice);
       
   270     }
       
   271 
       
   272 TBool CBTHidConnection::IsConnected() const
       
   273     {
       
   274         TRACE_INFO( (_L("[BTHID]\tCBTHidConnection::IsConnected: connection state = %d"), iConnectionState) );
       
   275     return (EConnected == iConnectionState);
       
   276     }
       
   277 
       
   278 TBTConnectionState CBTHidConnection::ConnectStatus() const
       
   279     {
       
   280         TRACE_INFO( (_L("[BTHID]\tCBTHidConnection::ConnectStatus: connection state = %d"), iConnectionState) );
       
   281     return (iConnectionState);
       
   282     }
       
   283 
       
   284 void CBTHidConnection::StartMonitoringChannelsL()
       
   285     {
       
   286         TRACE_FUNC
       
   287     (_L("[BTHID]\tCBTHidConnection::StartMonitoringChannelsL"));
       
   288     __ASSERT_DEBUG(iConnectionState == EConnected,
       
   289             User::Panic(KPanicBTConnection, ESocketsBadState));
       
   290 
       
   291     iControlSocketReader->StartReadingL(iControlSocket, iControlInMTU);
       
   292     iInterruptSocketReader->StartReadingL(iInterruptSocket, iInterruptInMTU);
       
   293 
       
   294     // If the device will reconnect to us we are ok to drop the link
       
   295     // after a period of inactivity.
       
   296     if (iDevice->iReconnectInit)
       
   297         {
       
   298         iInactivityTimer->Cancel();
       
   299         iInactivityTimer->After(KInactivityTimeout);
       
   300         }
       
   301     }
       
   302 
       
   303 void CBTHidConnection::DropConnection()
       
   304     {
       
   305         TRACE_FUNC
       
   306     (_L("[BTHID]\tCBTHidConnection::DropConnection"));
       
   307     // Close the Bluetooth Channels.
       
   308     CloseChannels();
       
   309 
       
   310     // Update the connection state.
       
   311     ChangeState(ELinkLost);
       
   312 
       
   313     // If a command is outstanding
       
   314     if (iCommandIssued)
       
   315         {
       
   316         // Generate an error to the parent.
       
   317         iObserver.HandleCommandAck(iConnID, KErrNotReady);
       
   318         // Reset the command flag.
       
   319         iCommandIssued = EFalse;
       
   320 
       
   321         // Reset this, so we don't leave it in a bad state.
       
   322         if (iCommandSegmenter)
       
   323             {
       
   324             iCommandSegmenter->Reset();
       
   325             }
       
   326         }
       
   327     }
       
   328 
       
   329 // from MSocketObserver
       
   330 void CBTHidConnection::HandleSocketError(TUint /*aSocketID*/,
       
   331         TBool aConnectionLost, TInt aErrorCode)
       
   332     {
       
   333         TRACE_INFO( (_L("[BTHID]\tCBTHidConnection::HandleSocketError: connlost %d, error code %d"), aConnectionLost, aErrorCode) );
       
   334     if (aConnectionLost)
       
   335         {
       
   336         ConnectionLost();
       
   337         }
       
   338     else
       
   339         {
       
   340         // If we failed to write to the device then inform Generic HID
       
   341         // of the failure
       
   342         if (iCommandIssued)
       
   343             {
       
   344             iCommandIssued = EFalse;
       
   345 
       
   346             // Reset this, so we don't leave it in a bad state.
       
   347             if (iCommandSegmenter)
       
   348                 {
       
   349                 iCommandSegmenter->Reset();
       
   350                 }
       
   351 
       
   352             iObserver.HandleCommandAck(iConnID, aErrorCode);
       
   353             }
       
   354         }
       
   355     }
       
   356 
       
   357 // from MSocketObserver
       
   358 TBool CBTHidConnection::HandleDataReceived(TUint aSocketID,
       
   359         const TDesC8& aBuffer)
       
   360     {
       
   361         TRACE_FUNC
       
   362     (_L("[BTHID]\tCBTHidConnection::HandledataReceived"));
       
   363     TBool result = ETrue;
       
   364 
       
   365     // Cancel the inactivity timer.
       
   366     iInactivityTimer->Cancel();
       
   367 
       
   368     switch (aSocketID)
       
   369         {
       
   370         case EControlSocketID:
       
   371             result = ProcessControlData(aBuffer);
       
   372             break;
       
   373         case EInterruptSocketID:
       
   374             result = ProcessInterruptData(aBuffer);
       
   375             break;
       
   376         default:
       
   377             // Shouldn't have any other socket id
       
   378             User::Panic(KPanicBTConnection, ESocketsUnknownID);
       
   379             break;
       
   380         }
       
   381 
       
   382     // If the device will reconnect to us we are ok to drop the link
       
   383     // after a period of inactivity.
       
   384     if ((result) && (iDevice->iReconnectInit))
       
   385         {
       
   386         iInactivityTimer->After(KInactivityTimeout);
       
   387         }
       
   388 
       
   389     return result;
       
   390     }
       
   391 
       
   392 // from MSocketObserver
       
   393 void CBTHidConnection::HandleWriteComplete(TUint aSocketID)
       
   394     {
       
   395         //to handle DATA Output in interrupt channel (Host to Device DATA)
       
   396         // Check the ID of the socket
       
   397         TRACE_FUNC
       
   398     (_L("[BTHID]\tCBTHidConnection::HandleWriteComplete"));
       
   399     if (aSocketID == EControlSocketID || aSocketID == EInterruptSocketID)
       
   400         {
       
   401         // Send any additional packets on the control channel
       
   402         if (iCommandSegmenter)
       
   403             {
       
   404             const HBufC8* data = iCommandSegmenter->NextPacket();
       
   405             if (data)
       
   406                 {
       
   407                 if (aSocketID == EControlSocketID)
       
   408                     {
       
   409                     TRAPD(err, iControlSocketWriter->IssueWriteL(*data));
       
   410                     if (KErrNone != err)
       
   411                         {
       
   412                         // Reset this, so we don't leave it in a bad state.
       
   413                         iCommandSegmenter->Reset();
       
   414                         iCommandIssued = EFalse;
       
   415 
       
   416                         iObserver.HandleCommandAck(iConnID, err);
       
   417                         }
       
   418                     }
       
   419                 else //aSocketID == EInterruptSocketID
       
   420                     {
       
   421                     TRAPD(err, iInterruptSocketWriter->IssueWriteL(*data));
       
   422                     if (KErrNone != err)
       
   423                         {
       
   424                         // Reset this, so we don't leave it in a bad state.
       
   425                         iCommandSegmenter->Reset();
       
   426                         iCommandIssued = EFalse;
       
   427 
       
   428                         iObserver.HandleCommandAck(iConnID, err);
       
   429                         }
       
   430                     }
       
   431                 }
       
   432             if (!data && aSocketID == EInterruptSocketID && iCommandIssued)
       
   433                 { //We don't expect response from HID Device, However we'll notify GenericHID
       
   434                 //that async write operation has been finished.
       
   435                 iObserver.HandleCommandAck(iConnID, KErrNone); //Socket write complete!
       
   436                 iCommandIssued = EFalse;
       
   437                 }
       
   438             }
       
   439         }
       
   440 
       
   441     }
       
   442 
       
   443 void CBTHidConnection::SocketsConnected()
       
   444     {
       
   445         TRACE_FUNC
       
   446     (_L("[BTHID]\tCBTHidConnection::SocketsConnected"));
       
   447     TRAPD(error, PrepareSocketsL());
       
   448 
       
   449     if (KErrNone == error)
       
   450         {
       
   451         switch (iConnectionState)
       
   452             {
       
   453             case EFirstConnection:
       
   454                 // We are now connected
       
   455 
       
   456                 ChangeState(EConnected);
       
   457 
       
   458                 // If this was an initial connection inform the observer
       
   459                 iObserver.FirstTimeConnectionComplete(iConnID, KErrNone);
       
   460                 break;
       
   461 
       
   462             case EHostReconnecting:
       
   463                 // We are now connected
       
   464                 ChangeState(EConnected);
       
   465 
       
   466                 iObserver.LinkRestored(iConnID);
       
   467                 break;
       
   468 
       
   469             default:
       
   470                 User::Panic(KPanicBTConnection, ESocketsBadState);
       
   471                 break;
       
   472             }
       
   473         }
       
   474     else
       
   475         {
       
   476         SocketsConnFailed(error);
       
   477         }
       
   478     }
       
   479 
       
   480 void CBTHidConnection::SocketsConnFailed(TInt aStatus)
       
   481     {
       
   482         TRACE_INFO( (_L("[BTHID]\tCBTHidConnection::SocketsConnFailed(%d)"),aStatus) );
       
   483     switch (iConnectionState)
       
   484         {
       
   485         case EFirstConnection:
       
   486             // We are not connected
       
   487             ChangeState(ENotConnected);
       
   488             // If this was an initial connection inform the observer
       
   489             iObserver.FirstTimeConnectionComplete(iConnID, aStatus);
       
   490             break;
       
   491 
       
   492         case EHostReconnecting:
       
   493             ChangeState(ELinkLost);
       
   494             break;
       
   495 
       
   496         default:
       
   497             // we don't expect to be connecting in any other state.
       
   498             User::Panic(KPanicBTConnection, ESocketsBadState);
       
   499             break;
       
   500         }
       
   501     }
       
   502 
       
   503 void CBTHidConnection::TimerExpired()
       
   504     {
       
   505     // Inactivity timer has expired.
       
   506     // Drop the connection.
       
   507     DropConnection();
       
   508     // Inform the observer of the state change.
       
   509     iObserver.Disconnected(iConnID);
       
   510     }
       
   511 
       
   512 void CBTHidConnection::GetProtocolL()
       
   513     {
       
   514     LeaveIfCommandNotReadyL();
       
   515 
       
   516     iCommandBuffer.Zero();
       
   517     iCommandBuffer.Append(EGetProtocol);
       
   518     iControlSocketWriter->IssueWriteL(iCommandBuffer);
       
   519     iCommandIssued = ETrue;
       
   520     }
       
   521 
       
   522 void CBTHidConnection::SetProtocolL(TUint16 aValue)
       
   523     {
       
   524     LeaveIfCommandNotReadyL();
       
   525 
       
   526     iCommandBuffer.Zero();
       
   527     // Value should be
       
   528     // 0 for boot
       
   529     // 1 for report
       
   530     // ESetProtocol is set for Boot mode. Add aValue to get the correct
       
   531     // command
       
   532     iCommandBuffer.Append(ESetProtocol + aValue);
       
   533     iControlSocketWriter->IssueWriteL(iCommandBuffer);
       
   534     iCommandIssued = ETrue;
       
   535     }
       
   536 
       
   537 void CBTHidConnection::GetReportL(TUint8 aReportType, TUint8 aReportID,
       
   538         TUint16 aLength)
       
   539     {
       
   540     LeaveIfCommandNotReadyL();
       
   541 
       
   542     iCommandBuffer.Zero();
       
   543 
       
   544     // Add the Transaction header byte
       
   545     // Report Type 0 = Reserved
       
   546     // Report Type 1 = Input
       
   547     // Report Type 2 = Output
       
   548     // Report Type 3 = Feature
       
   549     iCommandBuffer.Append(EGetReportFullBufReserved + aReportType);
       
   550 
       
   551     // If Report ID's are declared in the report descriptor then we
       
   552     // add this field
       
   553     if (aReportID != 0)
       
   554         {
       
   555         iCommandBuffer.Append(aReportID);
       
   556         }
       
   557 
       
   558     // We need to check we have a buffer large enough to hold the control
       
   559     // data we get back from the device
       
   560     if (!iControlDataBuffer)
       
   561         {
       
   562         // Allocate the buffer if it didn't exist
       
   563         iControlDataBuffer = HBufC8::NewL(aLength);
       
   564         }
       
   565     else
       
   566         {
       
   567         // Get a modifiable pointer to the buffer.
       
   568         TPtr8 dataPtr = iControlDataBuffer->Des();
       
   569 
       
   570         if (dataPtr.MaxLength() < aLength)
       
   571             {
       
   572             // Reallocate the buffer if its too small.
       
   573             delete iControlDataBuffer;
       
   574             iControlDataBuffer = 0;
       
   575             iControlDataBuffer = HBufC8::NewL(aLength);
       
   576             }
       
   577         else
       
   578             {
       
   579             // Existing buffer is large enough, just zero it.
       
   580             dataPtr.Zero();
       
   581             }
       
   582         }
       
   583 
       
   584     iControlSocketWriter->IssueWriteL(iCommandBuffer);
       
   585     iCommandIssued = ETrue;
       
   586     }
       
   587 
       
   588 void CBTHidConnection::SetReportL(TUint8 aReportType, TUint8 aReportID,
       
   589         const TDesC8& aReport)
       
   590     {
       
   591     LeaveIfCommandNotReadyL();
       
   592 
       
   593     if (!iCommandSegmenter)
       
   594         {
       
   595         iCommandSegmenter = CDataSegmenter::NewL();
       
   596         }
       
   597 
       
   598     iCommandBuffer.Zero();
       
   599 
       
   600     // Add the Transaction header byte
       
   601     // Report Type 0 = Reserved
       
   602     // Report Type 1 = Input
       
   603     // Report Type 2 = Output
       
   604     // Report Type 3 = Feature
       
   605     iCommandBuffer.Append(ESetReportReserved + aReportType);
       
   606 
       
   607     // If Report ID's are declared in the report descriptor then we
       
   608     // add this field
       
   609     if (aReportID != 0)
       
   610         {
       
   611         iCommandBuffer.Append(aReportID);
       
   612         }
       
   613 
       
   614     iCommandSegmenter->SegmentDataL(iCommandBuffer, aReport, EDATCOutput,
       
   615             iControlOutMTU);
       
   616 
       
   617     iControlSocketWriter->IssueWriteL(*(iCommandSegmenter->FirstPacketL()));
       
   618 
       
   619     iCommandIssued = ETrue;
       
   620     }
       
   621 
       
   622 void CBTHidConnection::DataOutL(TUint8 aReportID, const TDesC8& aReport)
       
   623     {
       
   624     //This implementation is similar to SetReportL, but instead of Control channel,
       
   625     //sends the data in Interrupt channel.
       
   626 
       
   627     LeaveIfCommandNotReadyL();
       
   628     //We'll use Command buffer instead of Data buffer to send.
       
   629     //could they happen at the same time?
       
   630     if (!iCommandSegmenter)
       
   631         {
       
   632         iCommandSegmenter = CDataSegmenter::NewL();
       
   633         }
       
   634 
       
   635     iCommandBuffer.Zero();
       
   636 
       
   637     // Add the Transaction header byte
       
   638     // Report Type 0 = Reserved
       
   639     // Report Type 1 = Input
       
   640     // Report Type 2 = Output
       
   641     // Report Type 3 = Feature
       
   642     iCommandBuffer.Append(EDATAOutput);
       
   643 
       
   644     // If Report ID's are declared in the report descriptor then we
       
   645     // add this field
       
   646     if (aReportID != 0)
       
   647         {
       
   648         iCommandBuffer.Append(aReportID);
       
   649         }
       
   650 
       
   651     iCommandSegmenter->SegmentDataL(iCommandBuffer, aReport, EDATCOutput,
       
   652             iInterruptOutMTU);
       
   653 
       
   654     iInterruptSocketWriter->IssueWriteL(*(iCommandSegmenter->FirstPacketL()));
       
   655 
       
   656     iCommandIssued = ETrue; //We have sent the data but we don't expect
       
   657     //HANDSHAKE SUCCESSFULL for DATA OUT from HID device.
       
   658     //We will notify Generic HID when data has been sent.
       
   659     }
       
   660 
       
   661 void CBTHidConnection::GetIdleL()
       
   662     {
       
   663     LeaveIfCommandNotReadyL();
       
   664 
       
   665     iCommandBuffer.Zero();
       
   666     iCommandBuffer.Append(EGetIdle);
       
   667     iControlSocketWriter->IssueWriteL(iCommandBuffer);
       
   668     iCommandIssued = ETrue;
       
   669     }
       
   670 
       
   671 void CBTHidConnection::SetIdleL(TUint8 aDuration)
       
   672     {
       
   673     LeaveIfCommandNotReadyL();
       
   674 
       
   675     iCommandBuffer.Zero();
       
   676     iCommandBuffer.Append(ESetIdle);
       
   677     iCommandBuffer.Append(aDuration);
       
   678     iControlSocketWriter->IssueWriteL(iCommandBuffer);
       
   679     iCommandIssued = ETrue;
       
   680     }
       
   681 
       
   682 void CBTHidConnection::PrepareSocketsL()
       
   683     {
       
   684         TRACE_FUNC
       
   685     (_L("[BTHID]\tCBTHidConnection::PrepareSockets"));
       
   686     // Retrieve the MTU values from the sockets
       
   687     User::LeaveIfError(iControlSocket->GetOpt(KL2CAPGetOutboundMTU,
       
   688             KSolBtL2CAP, iControlOutMTU));
       
   689     User::LeaveIfError(iControlSocket->GetOpt(KL2CAPInboundMTU, KSolBtL2CAP,
       
   690             iControlInMTU));
       
   691     User::LeaveIfError(iInterruptSocket->GetOpt(KL2CAPInboundMTU,
       
   692             KSolBtL2CAP, iInterruptInMTU));
       
   693     User::LeaveIfError(iInterruptSocket->GetOpt(KL2CAPGetOutboundMTU,
       
   694             KSolBtL2CAP, iInterruptOutMTU));
       
   695 
       
   696     // Initialise the control socket writer with the new socket.
       
   697     iControlSocketWriter->Initialise(iControlSocket);
       
   698 
       
   699     // Initialise the interrupt socket writer with the new socket.
       
   700     iInterruptSocketWriter->Initialise(iInterruptSocket);
       
   701 
       
   702     }
       
   703 
       
   704 void CBTHidConnection::ChangeState(TBTConnectionState aNewStatus)
       
   705     {
       
   706     iConnectionState = aNewStatus;
       
   707     }
       
   708 
       
   709 void CBTHidConnection::ConnectionLost()
       
   710     {
       
   711         TRACE_FUNC
       
   712     (_L("[BTHID]\tCBTHidConnection::ConnectionLost"));
       
   713     CloseChannels();
       
   714 
       
   715     // First go into link loss state
       
   716     ChangeState(ELinkLost);
       
   717 
       
   718     // If a command is outstanding
       
   719     if (iCommandIssued)
       
   720         {
       
   721             TRACE_INFO(_L("[BTHID]\tCBTHidConnection::ConnectionLost, command outstanding"));
       
   722         // Generate an error to the parent.
       
   723         iObserver.HandleCommandAck(iConnID, KErrNotReady);
       
   724         // Reset the command flag.
       
   725         iCommandIssued = EFalse;
       
   726 
       
   727         // Reset this, so we don't leave it in a bad state.
       
   728         if (iCommandSegmenter)
       
   729             {
       
   730             iCommandSegmenter->Reset();
       
   731             }
       
   732         }
       
   733 
       
   734     // Check if the device will reconnect to us.
       
   735     if (iDevice->iReconnectInit)
       
   736         {
       
   737             TRACE_INFO(_L("[BTHID]\tCBTHidConnection::ConnectionLost, device inits reconnect"));
       
   738         // Inform the parent of the link loss and the fact we are not
       
   739         // reconnecting
       
   740         iObserver.LinkLost(iConnID);
       
   741         }
       
   742     else
       
   743         {
       
   744             TRACE_INFO(_L("[BTHID]\tCBTHidConnection::ConnectionLost, host inits reconnect"));
       
   745         // Device won't reconnect, check if we are able to.
       
   746         if (iDevice->iNormallyConnectable)
       
   747             {
       
   748                 TRACE_INFO(_L("[BTHID]\tCBTHidConnection::ConnectionLost, device is normally connectable"));
       
   749             // Attempt to initiate reconnection to the device.
       
   750             TRAPD(res, iSocketInitiator->ConnectSocketsL(iDevice->iAddress,
       
   751                             iDevice->iUseSecurity,
       
   752                             iControlSocket,
       
   753                             iInterruptSocket);)
       
   754             if (res == KErrNone)
       
   755                 {
       
   756                 // Reconnection is in progress, so record this and inform
       
   757                 // the parent.
       
   758                 ChangeState(EHostReconnecting);
       
   759                 iObserver.LinkLost(iConnID);
       
   760                 }
       
   761             else
       
   762                 {
       
   763                 // Inform the parent of the link loss and the fact we are not
       
   764                 // reconnecting
       
   765                 iObserver.LinkLost(iConnID);
       
   766                 }
       
   767             }
       
   768         else
       
   769             {
       
   770                 TRACE_INFO(_L("[BTHID]\tCBTHidConnection::ConnectionLost, device does not allow reconnection"));
       
   771             // Device won't allow reconnection, so this connection is
       
   772             // effectively dead.
       
   773             // Change to not-connected and inform the parent of the
       
   774             // disconnection.
       
   775             ChangeState(ENotConnected);
       
   776             iObserver.Unplugged(iConnID);
       
   777             }
       
   778         }
       
   779     }
       
   780 
       
   781 void CBTHidConnection::ReceivedVirtualCableUnplug()
       
   782     {
       
   783         TRACE_FUNC
       
   784     (_L("[BTHID]\tCBTHidConnection::ReceivedVirtualCableUnplug"));
       
   785     CloseChannels();
       
   786     ChangeState(ENotConnected);
       
   787 
       
   788     iObserver.Unplugged(iConnID);
       
   789     }
       
   790 
       
   791 void CBTHidConnection::LeaveIfCommandNotReadyL() const
       
   792     {
       
   793     if (iConnectionState != EConnected)
       
   794         {
       
   795         User::Leave(KErrNotReady);
       
   796         }
       
   797 
       
   798     if (iCommandIssued)
       
   799         {
       
   800         User::Leave(KErrInUse);
       
   801         }
       
   802     }
       
   803 
       
   804 void CBTHidConnection::CloseChannels()
       
   805     {
       
   806         TRACE_FUNC
       
   807     (_L("[BTHID]\tCBTHidConnection::CloseChannels"));
       
   808     // Cancel the inactivity timer.
       
   809     iInactivityTimer->Cancel();
       
   810 
       
   811     iControlSocketReader->Cancel();
       
   812     iControlSocketWriter->Cancel();
       
   813     iInterruptSocketReader->Cancel();
       
   814     iInterruptSocketWriter->Cancel();
       
   815 
       
   816     // BT HID Spec. says the channels should be closed in the reverse
       
   817     // order to when they were opened
       
   818     if (iInterruptSocket)
       
   819         {
       
   820         if (iConnectionState == EConnected)
       
   821             {
       
   822             // Immediate shutdown
       
   823             TRequestStatus status;
       
   824             iInterruptSocket->Shutdown(RSocket::EImmediate, status);
       
   825             User::WaitForRequest(status);
       
   826             }
       
   827 
       
   828         iInterruptSocket->Close();
       
   829         }
       
   830 
       
   831     if (iControlSocket)
       
   832         {
       
   833         if (iConnectionState == EConnected)
       
   834             {
       
   835             // Immediate shutdown
       
   836             TRequestStatus status;
       
   837             iControlSocket->Shutdown(RSocket::EImmediate, status);
       
   838             User::WaitForRequest(status);
       
   839             }
       
   840 
       
   841         iControlSocket->Close();
       
   842         }
       
   843 
       
   844     }
       
   845 
       
   846 TBool CBTHidConnection::ProcessControlData(const TDesC8& aBuffer)
       
   847     {
       
   848     TBool result = ETrue;
       
   849 
       
   850         TRACE_FUNC
       
   851     (_L("[BTHID]\tCBTHidConnection::ProcessControlData"));
       
   852     // Reset this, so we don't leave it in a bad state;
       
   853     if (iCommandSegmenter)
       
   854         {
       
   855         iCommandSegmenter->Reset();
       
   856         }
       
   857 
       
   858     TInt dataLength = aBuffer.Length();
       
   859 
       
   860     if (dataLength > 0)
       
   861         {
       
   862         TBTTransaction transaction = static_cast<TBTTransaction> (aBuffer[0]);
       
   863 
       
   864         // The only unsolicited response should be a Virtual Cable Unplugged
       
   865         // notification.
       
   866         // All other responses should only be handled, if we have an
       
   867         // outstanding command
       
   868         if (transaction == EHIDControlVCUnplug)
       
   869             {
       
   870             ReceivedVirtualCableUnplug();
       
   871             // Don't want to read any more
       
   872             result = EFalse;
       
   873             }
       
   874         else if (iCommandIssued)
       
   875             {
       
   876             // Reset the command issued flag.
       
   877             iCommandIssued = EFalse;
       
   878 
       
   879                 TRACE_INFO( (_L("[BTHID]\tCBTHidConnection::ProcessControlData: transaction %d"), transaction) );
       
   880             switch (transaction)
       
   881                 {
       
   882                 case EHandshakeSuccess:
       
   883                     iObserver.HandleCommandAck(iConnID, KErrNone);
       
   884                     break;
       
   885                 case EHandshakeNotReady:
       
   886                     iObserver.HandleCommandAck(iConnID, KErrNotReady);
       
   887                     break;
       
   888                 case EHandshakeInvalidRepID:
       
   889                     iObserver.HandleCommandAck(iConnID,
       
   890                             KErrAckInvalidReportID);
       
   891                     break;
       
   892                 case EHandshakeUnsupported:
       
   893                     iObserver.HandleCommandAck(iConnID, KErrNotSupported);
       
   894                     break;
       
   895                 case EHandshakeInvalidParam:
       
   896                     iObserver.HandleCommandAck(iConnID,
       
   897                             KErrAckInvalidParameter);
       
   898                     break;
       
   899                 case EHandshakeUnknown:
       
   900                     iObserver.HandleCommandAck(iConnID, KErrAckUnknown);
       
   901                     break;
       
   902                 case EHandshakeFatal:
       
   903                     iObserver.HandleCommandAck(iConnID, KErrAckFatal);
       
   904                     break;
       
   905 
       
   906                 case EDATAInput:
       
   907                     // Fall through.
       
   908                 case EDATAFeature:
       
   909                     // If this packet was smaller than the maximum transmission
       
   910                     // unit this is the only data we will get.
       
   911                     if (dataLength < iControlInMTU)
       
   912                         {
       
   913                         // Pass to the observer
       
   914                         iObserver.HandleControlData(iConnID, aBuffer.Mid(1));
       
   915                         }
       
   916                     else
       
   917                         {
       
   918                         // Data was the size of the MTU, so we will expect
       
   919                         // further DATC packets.
       
   920 
       
   921                         // Mark that the command hasn't finished
       
   922                         iCommandIssued = ETrue;
       
   923 
       
   924                         // Store as much data as we can in the buffer allocated
       
   925                         if (iControlDataBuffer)
       
   926                             {
       
   927                             TPtr8 dataPtr = iControlDataBuffer->Des();
       
   928                             dataPtr.Zero();
       
   929                             AppendData(dataPtr, aBuffer.Mid(1));
       
   930                             }
       
   931                         }
       
   932                     break;
       
   933 
       
   934                 case EDATCInput:
       
   935                     // Fall through.
       
   936                 case EDATCFeature:
       
   937                     // If this packet was smaller than the maximum transmission
       
   938                     // this is the final packet.
       
   939                     HandleEDATCFeature(aBuffer);
       
   940                     break;
       
   941 
       
   942                 case EDATAOther:
       
   943                     iObserver.HandleControlData(iConnID, aBuffer.Mid(1));
       
   944                     break;
       
   945 
       
   946                 default:
       
   947                     // This transaction was unknown or unexpected, but
       
   948                     // we must report the error.
       
   949                     iObserver.HandleCommandAck(iConnID, KErrAckUnknown);
       
   950                     break;
       
   951                 }
       
   952             }
       
   953         }
       
   954 
       
   955     return result;
       
   956     }
       
   957 
       
   958 void CBTHidConnection::HandleEDATCFeature(const TDesC8& aBuffer)
       
   959     {
       
   960     TInt dataLength = aBuffer.Length();
       
   961     if (dataLength < iControlInMTU)
       
   962         {
       
   963         // If there is a data buffer.
       
   964         if (iControlDataBuffer)
       
   965             {
       
   966             // Get a modifiable pointer to the data
       
   967             TPtr8 dataPtr = iControlDataBuffer->Des();
       
   968 
       
   969             // If there is already some data then we can
       
   970             // append.
       
   971             if (dataPtr.Length() > 0)
       
   972                 {
       
   973                 // Append as much as we can.
       
   974                 AppendData(dataPtr, aBuffer.Mid(1));
       
   975 
       
   976                 // Pass up what data we have.
       
   977                 iObserver.HandleControlData(iConnID, *iControlDataBuffer);
       
   978                 }
       
   979             else
       
   980                 {
       
   981                 // No initial data was stored, so report an
       
   982                 // error.
       
   983                 iObserver.HandleCommandAck(iConnID, KErrAckUnknown);
       
   984                 }
       
   985             }
       
   986         else
       
   987             {
       
   988             // There is no buffer to handle the data so just
       
   989             // report an error.
       
   990             iObserver.HandleCommandAck(iConnID, KErrNoMemory);
       
   991             }
       
   992         }
       
   993     else
       
   994         {
       
   995         // This is an intermediate packet.
       
   996 
       
   997         // Mark that the command hasn't finished
       
   998         iCommandIssued = ETrue;
       
   999 
       
  1000         if (iControlDataBuffer)
       
  1001             {
       
  1002             // Get a modifiable pointer to the data.
       
  1003             TPtr8 dataPtr = iControlDataBuffer->Des();
       
  1004 
       
  1005             // If there is already some data then we can
       
  1006             // append.
       
  1007             if (dataPtr.Length() > 0)
       
  1008                 {
       
  1009                 // Append as much as we can.
       
  1010                 AppendData(dataPtr, aBuffer.Mid(1));
       
  1011                 }
       
  1012             }
       
  1013         }
       
  1014 
       
  1015     }
       
  1016 
       
  1017 void CBTHidConnection::AppendData(TDes8& aDest, const TDesC8& aSource)
       
  1018     {
       
  1019     TInt remainingSpace = aDest.MaxLength() - aDest.Length();
       
  1020 
       
  1021     // If the control data buffer can handle this new packet
       
  1022     if (aSource.Length() <= remainingSpace)
       
  1023         {
       
  1024         // Append the data
       
  1025         aDest.Append(aSource);
       
  1026         }
       
  1027     }
       
  1028 
       
  1029 TBool CBTHidConnection::ProcessInterruptData(const TDesC8& aBuffer)
       
  1030     {
       
  1031         TRACE_FUNC
       
  1032     (_L("[BTHID]\tCBTHidConnection::ProcessInterruptData"));
       
  1033     TInt dataLength = aBuffer.Length();
       
  1034 
       
  1035     if (dataLength > 0)
       
  1036         {
       
  1037         TBTTransaction transaction = static_cast<TBTTransaction> (aBuffer[0]);
       
  1038 
       
  1039         switch (transaction)
       
  1040             {
       
  1041             case EDATAInput:
       
  1042                 // If this packet was smaller than the maximum transmission
       
  1043                 // unit this is the only data we will get
       
  1044                 if (dataLength < iInterruptInMTU)
       
  1045                     {
       
  1046                     // Pass to the observer
       
  1047                     iObserver.HandleInterruptData(iConnID, aBuffer.Mid(1));
       
  1048                     }
       
  1049                 else
       
  1050                     {
       
  1051                     // Data was the size of the MTU, so we will expect further
       
  1052                     // DATC packets.
       
  1053                     // Try to handle the start of this sequence
       
  1054                     TRAPD(res, StartSegmentedInterruptDataL(aBuffer.Mid(1));)
       
  1055                     if (res != KErrNone)
       
  1056                         {
       
  1057                         // First segment wasn't handled, so just pass up
       
  1058                         // what we have
       
  1059                         iObserver.HandleInterruptData(iConnID, aBuffer.Mid(1));
       
  1060                         }
       
  1061                     }
       
  1062                 break;
       
  1063 
       
  1064             case EDATCInput:
       
  1065                 // Handle the next segmented packet
       
  1066                 // If this packet was smaller than the maximum transmission
       
  1067                 // unit this is the last packet in the sequence
       
  1068                 if (dataLength < iInterruptInMTU)
       
  1069                     {
       
  1070                     ContinueSegmentedInterruptData(aBuffer.Mid(1), ETrue);
       
  1071                     }
       
  1072                 else
       
  1073                     {
       
  1074                     ContinueSegmentedInterruptData(aBuffer.Mid(1), EFalse);
       
  1075                     }
       
  1076                 break;
       
  1077 
       
  1078             default:
       
  1079                 // Don't expect anything more here
       
  1080                 break;
       
  1081             }
       
  1082         }
       
  1083 
       
  1084     return ETrue;
       
  1085     }
       
  1086 
       
  1087 void CBTHidConnection::StartSegmentedInterruptDataL(const TDesC8& aBuffer)
       
  1088     {
       
  1089         TRACE_FUNC
       
  1090     (_L("[BTHID]\tCBTHidConnection::StartSegmentedInterruptDataL"));
       
  1091     // First check to see if we already have a data buffer
       
  1092     if (!iInterruptDataBuffer)
       
  1093         {
       
  1094         // Allocate a buffer based on the size of the data and copy it.
       
  1095         iInterruptDataBuffer = aBuffer.AllocL();
       
  1096         }
       
  1097     else
       
  1098         {
       
  1099         // Get a modifiable pointer to the buffer
       
  1100         TPtr8 dataPtr = iInterruptDataBuffer->Des();
       
  1101 
       
  1102         if (dataPtr.MaxLength() < aBuffer.Length())
       
  1103             {
       
  1104             // If the buffer isn't large enough recreate it
       
  1105             delete iInterruptDataBuffer;
       
  1106             iInterruptDataBuffer = 0;
       
  1107             iInterruptDataBuffer = aBuffer.AllocL();
       
  1108             }
       
  1109         else
       
  1110             {
       
  1111             // We have a large enough buffer so copy the data
       
  1112             dataPtr.Copy(aBuffer);
       
  1113             }
       
  1114         }
       
  1115     }
       
  1116 
       
  1117 void CBTHidConnection::ContinueSegmentedInterruptData(const TDesC8& aBuffer,
       
  1118         TBool aFinalSegment)
       
  1119     {
       
  1120         TRACE_FUNC
       
  1121     (_L("[BTHID]\tCBTHidConnection::ContinueSegmentedInterruptDataL"));
       
  1122     // If there is no buffer, then we haven't received a start packet, so
       
  1123     // just ignore this data
       
  1124     if (iInterruptDataBuffer)
       
  1125         {
       
  1126         // Get a modifiable pointer to the buffer
       
  1127         TPtr8 dataPtr = iInterruptDataBuffer->Des();
       
  1128 
       
  1129         // If there is no data already, then we can't handle this continuation
       
  1130         // packet
       
  1131         if (dataPtr.Length() > 0)
       
  1132             {
       
  1133             // Append the new data to the end of the buffer
       
  1134             TBool dataWasAppended = ETrue;
       
  1135             TInt newSize = (dataPtr.Length() + aBuffer.Length());
       
  1136 
       
  1137             if (dataPtr.MaxLength() < newSize)
       
  1138                 {
       
  1139                 // Reallocate the buffer if it isn't large enough
       
  1140                 HBufC8* newBuffer = iInterruptDataBuffer->ReAlloc(newSize);
       
  1141 
       
  1142                 if (newBuffer)
       
  1143                     {
       
  1144                     iInterruptDataBuffer = newBuffer;
       
  1145                     dataPtr.Set(iInterruptDataBuffer->Des());
       
  1146                     dataPtr.Append(aBuffer);
       
  1147                     }
       
  1148                 else
       
  1149                     {
       
  1150                     // We couldn't append the data, so reset this
       
  1151                     dataWasAppended = EFalse;
       
  1152                     }
       
  1153                 }
       
  1154             else
       
  1155                 {
       
  1156                 dataPtr.Append(aBuffer);
       
  1157                 }
       
  1158 
       
  1159             // If this is the final segment, or we couldn't append this
       
  1160             // segment, pass up what we have.
       
  1161             if ((aFinalSegment) || (!dataWasAppended))
       
  1162                 {
       
  1163                 iObserver.HandleInterruptData(iConnID, *iInterruptDataBuffer);
       
  1164 
       
  1165                 // Zero the buffer so any more continuation segments will
       
  1166                 // be ignored
       
  1167                 dataPtr.Zero();
       
  1168                 }
       
  1169             }
       
  1170         }
       
  1171     }
       
  1172 
       
  1173 //End of file