bluetoothengine/bthid/bthidserver/inc/bthidconnection.h
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:  Declares main application class.
       
    15  *
       
    16 */
       
    17 
       
    18 
       
    19 #ifndef __BTCONNECTION_H__
       
    20 #define __BTCONNECTION_H__
       
    21 
       
    22 #include <es_sock.h>
       
    23 #include <bt_sock.h>
       
    24 #include "socketobserver.h"
       
    25 #include "sockinitnotifier.h"
       
    26 #include "timeoutnotifier.h"
       
    27 
       
    28 const TUint KCommandBufferSize = 2;
       
    29 
       
    30 enum
       
    31     {
       
    32     EControlSocketID, EInterruptSocketID
       
    33     };
       
    34 
       
    35 const TUint KL2CAPDefaultMTU = 672;
       
    36 
       
    37 /*! 5 Minute -10 sec Inactivity Timeout (-10 sec due to Nokia Su8W*/
       
    38 const TUint KInactivityTimeout = (5 * 60 * 1000000) - 10000000;
       
    39 
       
    40 class CSocketReader;
       
    41 class CSocketWriter;
       
    42 class CSocketInitiator;
       
    43 class CDataSegmenter;
       
    44 class CTimeOutTimer;
       
    45 class CBTHidDevice;
       
    46 class MBTConnectionObserver;
       
    47 
       
    48 /*!
       
    49  Tracks the current state of the connection
       
    50  */
       
    51 enum TBTConnectionState
       
    52     {
       
    53     ENotConnected, /*!< No connection has been established. */
       
    54     EFirstConnection, /*!< A first-time connection is in progress. */
       
    55     ELinkLost, /*!< The connection is in link loss. */
       
    56     EConnected, /*!< The connection is active */
       
    57     EHostReconnecting, /*!< Host is reconnecting to the device */
       
    58     EHIDReconnecting, /*!< The device is reconnection to the host */
       
    59     EDisconnecting, /*!< The connection is being closed */
       
    60     EConnecting
       
    61     };
       
    62 
       
    63 /*!
       
    64  This class represents a single connection to a bluetooth HID device.
       
    65  It creates instances of separate active objects to perform reading from,
       
    66  and writing to, the sockets.
       
    67  */
       
    68 class CBTHidConnection : public CObject,
       
    69         public MSocketObserver,
       
    70         public MSockInitNotifier,
       
    71         public MTimeOutNotifier
       
    72     {
       
    73 public:
       
    74     /*!
       
    75      Create a CBTHidConnection object.
       
    76      @param aSocketServ The socket server session.
       
    77      @param aObserver An observer of this object.
       
    78      @result A pointer to the created instance of CBTHidConnection.
       
    79      */
       
    80     static CBTHidConnection* NewL(RSocketServ& aSocketServ,
       
    81             MBTConnectionObserver& aObserver,
       
    82             TBTConnectionState aConnectionState);
       
    83 
       
    84     /*!
       
    85      Create a CBTHidConnection object.
       
    86      @param aSocketServ The socket server session.
       
    87      @param aObserver An observer of this object.
       
    88      @result A pointer to the created instance of CBTHidConnection.
       
    89      */
       
    90     static CBTHidConnection* NewLC(RSocketServ& aSocketServ,
       
    91             MBTConnectionObserver& aObserver,
       
    92             TBTConnectionState aConnectionState);
       
    93 
       
    94     /*!
       
    95      Destroy the object and release all memory objects.
       
    96      */
       
    97     ~CBTHidConnection();
       
    98 
       
    99     /*!
       
   100      Gets the id for this connection.
       
   101      @return A connection ID.
       
   102      */
       
   103     TInt ConnID();
       
   104 
       
   105     /*!
       
   106      Sets the id for this connection.
       
   107      @param aConnID The new id for this connection.
       
   108      */
       
   109     void SetConnID(TInt aConnID);
       
   110 
       
   111     /*!
       
   112      Starts a first time connection to the HID device.
       
   113      */
       
   114     void ConnectL();
       
   115 
       
   116     /*!
       
   117      The connection should restart. either by connecting to the device or waiting
       
   118      for a connection.
       
   119      */
       
   120     void ReconnectL();
       
   121 
       
   122     /*!
       
   123      Perform a deliberate disconnection from the device. 
       
   124      Includes sending the Virtual-Cable-Unplug message if needed.
       
   125      */
       
   126     void Disconnect();
       
   127 
       
   128     /*!
       
   129      Drops the Bluetooth connection to a device
       
   130      */
       
   131     void DropConnection();
       
   132 
       
   133     /*!
       
   134      Starts the reading process on the sockets.
       
   135      */
       
   136     void StartMonitoringChannelsL();
       
   137 
       
   138     /*!
       
   139      Get access to the connections device details object.
       
   140      @result A reference to a device details object.
       
   141      */
       
   142     CBTHidDevice& DeviceDetails();
       
   143 
       
   144     /*!
       
   145      Current status of a connection after initial connection has been performed.
       
   146      @result ETrue device is connected.
       
   147      @result EFalse device is in link loss.
       
   148      */
       
   149     TBool IsConnected() const;
       
   150 
       
   151     TBTConnectionState ConnectStatus() const;
       
   152 
       
   153     /*!
       
   154      Asks to accept an incoming connection on the control channel.
       
   155      @param aAddress address of the connecting device.
       
   156      @param aSocket the socket for the connection. This will be set to NULL
       
   157      if the connection accepts the socket.
       
   158      */
       
   159     void OfferControlSocket(const TBTDevAddr& aAddress, RSocket*& aSocket);
       
   160 
       
   161     /*!
       
   162      Asks to accept an incoming connection on the interrupt channel.
       
   163      @param aAddress address of the connecting device.
       
   164      @param aSocket the socket for the connection. This will be set to NULL
       
   165      if the connection accepts the socket.
       
   166      */
       
   167     void OfferInterruptSocket(const TBTDevAddr& aAddress, RSocket*& aSocket);
       
   168 
       
   169     /*!
       
   170      Issues the Get Protocol Command over the control channel..
       
   171      */
       
   172     void GetProtocolL();
       
   173 
       
   174     /*!
       
   175      Issues the Set Protocol Command over the control channel.
       
   176      @param aValue the protocol setting. 0 = Boot, 1 = Report.
       
   177      */
       
   178     void SetProtocolL(TUint16 aValue);
       
   179 
       
   180     /*!
       
   181      Issues the Get Report Command over the control channel.
       
   182      @param aReportType the type of the report.
       
   183      @param aReportID the report ID.
       
   184      @param aLength the length of the report we are asking for.
       
   185      */
       
   186     void GetReportL(TUint8 aReportType, TUint8 aReportID, TUint16 aLength);
       
   187 
       
   188     /*!
       
   189      Issues the Set Report Command over the control channel.
       
   190      @param aReportType the type of the report.
       
   191      @param aReportID the report ID.
       
   192      @param aReport the report.
       
   193      */
       
   194     void SetReportL(TUint8 aReportType, TUint8 aReportID,
       
   195             const TDesC8& aReport);
       
   196 
       
   197     /*!
       
   198      Issues the DATA Out command over the Interrupt channel. This is similar to
       
   199      SetReport which uses ReportType Output. Therefore you may use SetReport
       
   200      to do data transfers from Host to device as well. 
       
   201      @param aReportID
       
   202      @param aReport reference to the report buffer
       
   203      */
       
   204     void DataOutL(TUint8 aReportID, const TDesC8& aReport);
       
   205 
       
   206     /*!
       
   207      Issues the Get Idle Command over the control channel.
       
   208      */
       
   209     void GetIdleL();
       
   210 
       
   211     /*!
       
   212      Issues the Set Idle Command over the control channel.
       
   213      @param aDuration the idle duration.
       
   214      */
       
   215     void SetIdleL(TUint8 aDuration);
       
   216 
       
   217 public:
       
   218     // from MSocketObserver
       
   219 
       
   220     void HandleSocketError(TUint aSocketID, TBool aConnectionLost,
       
   221             TInt aErrorCode);
       
   222 
       
   223     TBool HandleDataReceived(TUint aSocketID, const TDesC8& aBuffer);
       
   224 
       
   225     void HandleWriteComplete(TUint aSocketID);
       
   226 
       
   227 public:
       
   228     // from MSockInitNotifier
       
   229 
       
   230     void SocketsConnected();
       
   231 
       
   232     void SocketsConnFailed(TInt aStatus);
       
   233 
       
   234 public:
       
   235     // from MTimeOutNotifier
       
   236     void TimerExpired();
       
   237 
       
   238 private:
       
   239     /*!
       
   240      Perform the first phase of two phase construction. 
       
   241      @param aSocketServ the socket server session.
       
   242      @param aObserver an observer of this object.
       
   243      */
       
   244     CBTHidConnection(RSocketServ& aSocketServ,
       
   245             MBTConnectionObserver& aObserver,
       
   246             TBTConnectionState aConnectionState);
       
   247 
       
   248     /*!
       
   249      Perform the second phase construction of a CBTHidConnection object.
       
   250      */
       
   251     void ConstructL();
       
   252 
       
   253     /*!
       
   254      The BT HID Transaction Header based upon Bluetooth HID Spec Sec. 7.3
       
   255      */
       
   256     enum TBTTransaction
       
   257         {
       
   258         // Handshake commands
       
   259         EHandshakeSuccess = 0,
       
   260         EHandshakeNotReady = 1,
       
   261         EHandshakeInvalidRepID = 2,
       
   262         EHandshakeUnsupported = 3,
       
   263         EHandshakeInvalidParam = 4,
       
   264         EHandshakeUnknown = 14,
       
   265         EHandshakeFatal = 15,
       
   266         // Control Commands
       
   267         EHIDControlNOP = 16,
       
   268         EHIDControlHardReset = 17,
       
   269         EHIDControlSoftReset = 18,
       
   270         EHIDControlSuspend = 19,
       
   271         EHIDControlExitSuspend = 20,
       
   272         EHIDControlVCUnplug = 21,
       
   273         // Get Report Commands
       
   274         EGetReportFullBufReserved = 64,
       
   275         // Set Report Commands
       
   276         ESetReportReserved = 80,
       
   277         // Get/Set Protocol Commands
       
   278         EGetProtocol = 96,
       
   279         ESetProtocol = 112,
       
   280         // Get/Set Idle Commands
       
   281         EGetIdle = 128,
       
   282         ESetIdle = 144,
       
   283         // DATA Commands
       
   284         EDATAOther = 160,
       
   285         EDATAInput = 161,
       
   286         EDATAOutput = 162,
       
   287         EDATAFeature = 163,
       
   288         // DATC Commands
       
   289         EDATCOther = 176,
       
   290         EDATCInput = 177,
       
   291         EDATCOutput = 178,
       
   292         EDATCFeature = 179
       
   293         };
       
   294 
       
   295     /**
       
   296      *  Prepare the newly-connected sockets for reading and writing.
       
   297      */
       
   298     void PrepareSocketsL();
       
   299 
       
   300     /*!
       
   301      Perform a change in the connection status
       
   302      @param aNewStatus new connection status
       
   303      */
       
   304     void ChangeState(TBTConnectionState aNewStatus);
       
   305 
       
   306     /*!
       
   307      Handle the situation when the link to a device is lost.
       
   308      */
       
   309     void ConnectionLost();
       
   310 
       
   311     /*!
       
   312      The HID device has sent the virtual cable unplug command.
       
   313      */
       
   314     void ReceivedVirtualCableUnplug();
       
   315 
       
   316     /*!
       
   317      Checks to see if the connections is in a state able to service a set/get
       
   318      command.
       
   319      */
       
   320     void LeaveIfCommandNotReadyL() const;
       
   321 
       
   322     /*!
       
   323      Closes the L2CAP Control and Interrupt channels
       
   324      */
       
   325     void CloseChannels();
       
   326 
       
   327     /*!
       
   328      Handle data received on the control channel.
       
   329      @param aBuffer the data buffer.
       
   330      @result ETrue to continue reading.
       
   331      @result EFalse to stop reading.
       
   332      */
       
   333     TBool ProcessControlData(const TDesC8& aBuffer);
       
   334 
       
   335     /*!
       
   336      Append as much data to a buffer as can be handled.
       
   337      @param aDest the destination buffer.
       
   338      @param aSource the source buffer.
       
   339      */
       
   340     void AppendData(TDes8& aDest, const TDesC8& aSource);
       
   341 
       
   342     /*!
       
   343      Handle data received on the interrupt channel.
       
   344      @param aBuffer the data buffer.
       
   345      @result ETrue to continue reading.
       
   346      @result EFalse to stop reading.
       
   347      */
       
   348     TBool ProcessInterruptData(const TDesC8& aBuffer);
       
   349 
       
   350     void HandleEDATCFeature(const TDesC8& aBuffer);
       
   351 
       
   352     /*!
       
   353      Begin handling a new multiple interrupt packet transaction
       
   354      @param aBuffer the data buffer
       
   355      */
       
   356     void StartSegmentedInterruptDataL(const TDesC8& aBuffer);
       
   357 
       
   358     /*!
       
   359      Handle a Continuation data packet on the interrupt channel
       
   360      @param aBuffer the data buffer
       
   361      @param aFinalSegment ETrue if this is the final segment, EFalse otherwise
       
   362      */
       
   363     void ContinueSegmentedInterruptData(const TDesC8& aBuffer,
       
   364             TBool aFinalSegment);
       
   365 
       
   366 private:
       
   367 
       
   368     /*! The socket server */
       
   369     RSocketServ& iSocketServ;
       
   370 
       
   371     /*! An observer for status reporting */
       
   372     MBTConnectionObserver& iObserver;
       
   373 
       
   374     /*! The ID given to this connection */
       
   375     TInt iConnID;
       
   376 
       
   377     /*! The HID device details */
       
   378     CBTHidDevice* iDevice;
       
   379 
       
   380     /*! Current connection status */
       
   381     TBTConnectionState iConnectionState;
       
   382 
       
   383     /*! Control channel socket */
       
   384     RSocket* iControlSocket;
       
   385 
       
   386     /*! Interrupt channel socket */
       
   387     RSocket* iInterruptSocket;
       
   388 
       
   389     /*! Active object to control connecting sockets */
       
   390     CSocketInitiator* iSocketInitiator;
       
   391 
       
   392     /*! Active object to control reads from the socket */
       
   393     CSocketReader* iControlSocketReader;
       
   394 
       
   395     /*! Active object to control writes to the socket */
       
   396     CSocketWriter* iControlSocketWriter;
       
   397 
       
   398     /*! Active object to control reads from the socket */
       
   399     CSocketReader* iInterruptSocketReader;
       
   400 
       
   401     /*! Active object to control writes to the socket */
       
   402     CSocketWriter* iInterruptSocketWriter;
       
   403 
       
   404     /*! Set/Get command is outstanding with the device */
       
   405     TBool iCommandIssued;
       
   406 
       
   407     /*! Fixed buffer used for all Set/Get commands except SetReport*/
       
   408     TBuf8<KCommandBufferSize> iCommandBuffer;
       
   409 
       
   410     /*! Utility object to provide individual packets for large data */
       
   411     CDataSegmenter* iCommandSegmenter;
       
   412 
       
   413     /*! Dynamic buffer used for control data larger than the incoming MTU */
       
   414     HBufC8* iControlDataBuffer;
       
   415 
       
   416     /*! Dynamic buffer used for interrupt data larger than the incoming MTU */
       
   417     HBufC8* iInterruptDataBuffer;
       
   418 
       
   419     /*! Size of data host can send in a packet on the control channel*/
       
   420     TInt iControlOutMTU;
       
   421 
       
   422     /*! Size of data the device sends in a packet on the control channel*/
       
   423     TInt iControlInMTU;
       
   424 
       
   425     /*! Size of data host can send in a packet on the interrupt channel*/
       
   426     TInt iInterruptOutMTU;
       
   427 
       
   428     /*! Size of data the device sends in a packet on the interrupt channel*/
       
   429     TInt iInterruptInMTU;
       
   430 
       
   431     /*! A timer to track inactivity on the Bluetooth Connection */
       
   432     CTimeOutTimer* iInactivityTimer;
       
   433     };
       
   434 
       
   435 #endif // __BTCONNECTION_H__