bluetoothengine/headsetsimulator/core/inc/Tools/hstools.h
branchheadsetsimulator
changeset 60 90dbfc0435e3
equal deleted inserted replaced
59:02103bf20ee5 60:90dbfc0435e3
       
     1 /*
       
     2  * Component Name: Headset Simulator
       
     3  * Author: Comarch S.A.
       
     4  * Version: 1.0
       
     5  * Copyright (c) 2010 Comarch S.A.
       
     6  *  
       
     7  * This Software is submitted by Comarch S.A. to Symbian Foundation Limited on 
       
     8  * the basis of the Member Contribution Agreement entered between Comarch S.A. 
       
     9  * and Symbian Foundation Limited on 5th June 2009 (“Agreement”) and may be 
       
    10  * used only in accordance with the terms and conditions of the Agreement. 
       
    11  * Any other usage, duplication or redistribution of this Software is not 
       
    12  * allowed without written permission of Comarch S.A.
       
    13  * 
       
    14  */
       
    15 
       
    16 #ifndef HSTOOLS_H
       
    17 #define HSTOOLS_H
       
    18 #include <btsdp.h> 
       
    19 #include <btengconnman.h> 
       
    20 #include <btengsettings.h>
       
    21 #include <btengdiscovery.h>
       
    22 #include <hsclientobservers.h>
       
    23 
       
    24 class CBTEngSettings;
       
    25 class CBTEngConnMan;
       
    26 class CBTEngDiscovery;
       
    27 class RSocketServ;
       
    28 
       
    29 /** Default bluetooth name */
       
    30 _LIT(KHsDefaultName,"HS_Name");
       
    31 
       
    32 /** Headset protocol name */
       
    33 _LIT(KHsProtocolName,"BTLinkManager");
       
    34 
       
    35 /** Length of device bluetooth address */
       
    36 const TInt KDevAddrLength = KBTDevAddrSize * 2;
       
    37 
       
    38 /**
       
    39  * @brief Observer to notify about found devices during inquiry.
       
    40  */
       
    41 class MHsDeviceDiscovererObserver
       
    42 {
       
    43 public:
       
    44     /**
       
    45      * Informs that device was found during inquiry
       
    46      * 
       
    47      * @param aDevAddress 8-bit descriptor containing bluetooth address
       
    48      * @param aDevName  8-bit descriptor containing bluetooth name
       
    49      * @param aMajServClass major service class - part of Class of Device
       
    50      * @param aMajDeviceClass major device class - part of Class of Device
       
    51      * @param aMinDeviceClass minor device class - part of Class of Device
       
    52      */
       
    53     virtual void HandleDeviceFindSuccessL( TDesC8 & aDevAddress,
       
    54             TDesC8 & aDevName, TUint16 aMajServClass, TUint aMajDeviceClass,
       
    55             TUint8 aMinDeviceClass ) = 0;
       
    56 
       
    57     /**
       
    58      * Informs that inquiry finished
       
    59      * 
       
    60      * @param aErr system-wide error code
       
    61      */
       
    62     virtual void HandleDeviceFindFailed( TInt aErr ) = 0;
       
    63 };
       
    64 
       
    65 /**
       
    66  * @brief Class asynchronously finds devices equipped with bluetooth.
       
    67  */
       
    68 class CHsDeviceDiscoverer : public CActive
       
    69 {
       
    70 public:
       
    71     /**
       
    72      * Two-phase constructor
       
    73      * @param aDeviceObserver inquiry observer
       
    74      * @param aSocketServ session of socket server
       
    75      * @return instance of class
       
    76      */
       
    77     static CHsDeviceDiscoverer* NewL(
       
    78             MHsDeviceDiscovererObserver *aDeviceObserver,
       
    79             RSocketServ &aSocketServ );
       
    80 
       
    81     /**
       
    82      * Two-phase constructor
       
    83      * @param aDeviceObserver inquiry observer
       
    84      * @param aSocketServ session of socket server
       
    85      * @return instance of class
       
    86      */
       
    87     static CHsDeviceDiscoverer* NewLC(
       
    88             MHsDeviceDiscovererObserver *aDeviceObserver,
       
    89             RSocketServ &aSocketServ );
       
    90 
       
    91     /**
       
    92      * Destructor
       
    93      */
       
    94     ~CHsDeviceDiscoverer();
       
    95 
       
    96 public:
       
    97     /**
       
    98      * Asynchronous method for searching devices.
       
    99      */
       
   100     void DeviceSearchL();
       
   101 
       
   102 private:
       
   103     /**
       
   104      * Constructor for performing 1st stage construction
       
   105      * 
       
   106      * @param aDeviceObserver pointer to inquiry observer 
       
   107      * @param aSocketServ session of socket server 
       
   108      */
       
   109     CHsDeviceDiscoverer( MHsDeviceDiscovererObserver *aDeviceObserver,
       
   110             RSocketServ &aSocketServ );
       
   111 
       
   112     /**
       
   113      * Constructor for performing 2nd stage construction
       
   114      */
       
   115     void ConstructL();
       
   116 
       
   117 private:
       
   118     /**
       
   119      * Initializes and connects host resolver.
       
   120      */
       
   121     void ConnectHostResolverL();
       
   122 
       
   123 private:
       
   124     /**
       
   125      * CActive method. 
       
   126      * When request of searching devices completes with success, notification
       
   127      * is sent to MHsDeviceDiscovererObserver::HandleDeviceFindSuccessL(), 
       
   128      * otherwise to MHsDeviceDiscovererObserver::HandleDeviceFindFailed().
       
   129      */
       
   130     void RunL();
       
   131 
       
   132     /**
       
   133      * CActive method. Cancels of an outstanding request of searching devices.
       
   134      */
       
   135     void DoCancel();
       
   136 
       
   137 private:
       
   138 
       
   139     /** Denotes if searching is in progress*/
       
   140     TBool iSearching;
       
   141 
       
   142     /** Socket address used for inquiry */
       
   143     TInquirySockAddr iSockAddr;
       
   144 
       
   145     /** Wrapps bluetooth device name */
       
   146     TNameEntry iEntry;
       
   147 
       
   148     /** Session of socket server */
       
   149     RSocketServ &iSocketServ;
       
   150 
       
   151     /**Provides methods to find devices*/
       
   152     RHostResolver iHostResolver;
       
   153 
       
   154     /** Pointer to observer to notify about founded devices. Not owned. */
       
   155     MHsDeviceDiscovererObserver *iDeviceObserver;
       
   156 };
       
   157 
       
   158 /**
       
   159  * @brief Provides handling of bluetooth. Manages searching for and connection with AG.
       
   160  */
       
   161 class CHsBTManager : public CBase, public MBTEngSettingsObserver,
       
   162         public MBTEngConnObserver, public MHsDeviceDiscovererObserver,
       
   163         public MBTEngSdpResultReceiver
       
   164 {
       
   165 
       
   166 public:
       
   167 
       
   168     /**
       
   169      * Two-phased constructor
       
   170      * 
       
   171      * @param aSocketServ socket server subsession
       
   172      * @param aBTManagerObserver pointer to observer
       
   173      * 
       
   174      * @return instance of class
       
   175      */
       
   176     static CHsBTManager* NewL( RSocketServ& aSocketServ,
       
   177             MHsBTManagerObserver* aBTManagerObserver );
       
   178 
       
   179     /**
       
   180      * Two-phased constructor.
       
   181      * 
       
   182      * @param aSocketServ socket server subsession
       
   183      * @param aBTManagerObserver pointer to observer
       
   184      * 
       
   185      * @return instance of class
       
   186      */
       
   187     static CHsBTManager* NewLC( RSocketServ& aSocketServ,
       
   188             MHsBTManagerObserver* aBTManagerObserver );
       
   189 
       
   190     /**
       
   191      * Destructor.
       
   192      */
       
   193     ~CHsBTManager();
       
   194 
       
   195 public:
       
   196     /*
       
   197      * Sets bluetooth name
       
   198      * @param aBTName new bluetooth name
       
   199      */
       
   200     void SetBTNameL( const TDesC& aBTName );
       
   201 
       
   202     /**
       
   203      * Turns on bluetooth on device.
       
   204      * @param aName new bluetooth name
       
   205      */
       
   206     void TurnBtOnL( const TDesC& aName = KHsDefaultName );
       
   207 
       
   208     /**
       
   209      * Turns off bluetooth on device.
       
   210      */
       
   211     void TurnBtOffL();
       
   212 
       
   213     /**
       
   214      * Connects with AG by bluetooth address
       
   215      * 
       
   216      * @pre SetService() should be called
       
   217      * 
       
   218      * @param aDevAddress bluetooth address
       
   219      */
       
   220     void FindDevL( const TDesC& aDevAddress );
       
   221 
       
   222     /**
       
   223      * Connects with AG by bluetooth name
       
   224      * 
       
   225      * @pre SetService() should be called
       
   226      * 
       
   227      * @param aDeviceName bluetooth name
       
   228      */
       
   229     void FindNameL( const TDesC& aDeviceName );
       
   230 
       
   231     /**
       
   232      * Sets Headset Simulator's service
       
   233      * 
       
   234      * @param aService service Uuid
       
   235      */
       
   236     void SetService( const TUUID& aService );
       
   237 
       
   238     /**
       
   239      * Cancels ongoing attempt to connect with AG.
       
   240      */
       
   241     void CancelConnecting();
       
   242 
       
   243     /**
       
   244      * Sets bluetooth visibility mode.
       
   245      * 
       
   246      * @param aVisibilityMode mode
       
   247      * 
       
   248      * @return error value. KErrNotFound in case of failure, otherwise KErrNone
       
   249      */
       
   250     TInt SetVisible( TBTVisibilityMode aVisibilityMode );
       
   251 
       
   252 public:
       
   253     // Methods derived from  MHsDeviceDiscovererObserver
       
   254     void HandleDeviceFindSuccessL( TDesC8& aDevAddress, TDesC8& aDevName,
       
   255             TUint16 aMajServClass, TUint aMajDeviceClass,
       
   256             TUint8 aMinDeviceClass );
       
   257 
       
   258     void HandleDeviceFindFailed( TInt aErr );
       
   259 
       
   260 public:
       
   261     // Methods derived from MBTEngSettingsObserver
       
   262     void PowerStateChanged( TBTPowerStateValue aState );
       
   263 
       
   264     void VisibilityModeChanged( TBTVisibilityMode aState );
       
   265 
       
   266 public:
       
   267     // Methods derived from MBTEngConnObserver
       
   268 
       
   269     void ConnectComplete( TBTDevAddr& aAddr, TInt aErr,
       
   270             RBTDevAddrArray* aConflicts = NULL );
       
   271 
       
   272     void DisconnectComplete( TBTDevAddr& aAddr, TInt aErr );
       
   273 
       
   274     void PairingComplete( TBTDevAddr& aAddr, TInt aErr );
       
   275 
       
   276 public:
       
   277     // Methods derived from MBTEngSdpResultReceiver
       
   278     void ServiceSearchComplete( const RSdpRecHandleArray& aResult,
       
   279             TUint aTotalRecordsCount, TInt aErr );
       
   280 
       
   281     void AttributeSearchComplete( TSdpServRecordHandle aHandle,
       
   282             const RSdpResultArray& aAttr, TInt aErr );
       
   283 
       
   284     void ServiceAttributeSearchComplete( TSdpServRecordHandle aHandle,
       
   285             const RSdpResultArray& aAttr, TInt aErr );
       
   286 
       
   287     void DeviceSearchComplete( CBTDevice* aDevice, TInt aErr );
       
   288 
       
   289 private:
       
   290 
       
   291     /**
       
   292      * Constructor for performing 1st stage construction
       
   293      */
       
   294     CHsBTManager( RSocketServ& aSocketServ,
       
   295             MHsBTManagerObserver* aBTManagerObserver );
       
   296 
       
   297     /**
       
   298      * Constructor for performing 2nd stage construction
       
   299      */
       
   300     void ConstructL();
       
   301 
       
   302     /*
       
   303      * Restores local name and power state to initial values.
       
   304      */
       
   305     void RestoreInitialSettings();
       
   306 
       
   307     /*
       
   308      * Finds service on a specified device.
       
   309      * 
       
   310      * @param aDevAddr  bluetooth address
       
   311      * @param aService  service UUID
       
   312      */
       
   313     void FindServiceL( const TBTDevAddr& aDevAddr, const TUUID& aService );
       
   314 
       
   315     /**
       
   316      * Finds port on AG.
       
   317      * 
       
   318      * @param aAttr Sdp result array
       
   319      * @param aPort on return, port number
       
   320      * 
       
   321      * @return error value. KErrNotFound in case of failure, otherwise KErrNone
       
   322      */
       
   323     TInt FindPort( const RSdpResultArray& aAttr, TInt &aPort );
       
   324 
       
   325 private:
       
   326     /** Searching type enum */
       
   327     enum TLookingFor
       
   328     {
       
   329         /** Search device by bluetooth name*/
       
   330         EName,
       
   331         /** Search device by bluetooth address*/
       
   332         EDevAddress
       
   333     };
       
   334 
       
   335     /** Searching type */
       
   336     TLookingFor iLookingFor;
       
   337 
       
   338     /** Service */
       
   339     TUUID iService;
       
   340 
       
   341     /** Denotes if service is set */
       
   342     TBool iServiceSet;
       
   343 
       
   344     /** Bluetooth address */
       
   345     TBTDevAddr iDevAddr;
       
   346 
       
   347     /** Needed for restoring bluetooth power state */
       
   348     TBTPowerStateValue iBTInitialPowerState;
       
   349 
       
   350     /** Needed for restoring bluetooth visibility mode */
       
   351     TBTVisibilityMode iBTInitialVisibilityMode;
       
   352 
       
   353     /** Needed for restoring bluetooth name */
       
   354     RBuf iBTInitialName;
       
   355 
       
   356     /** Bluetooth name */
       
   357     RBuf iBTName;
       
   358 
       
   359     /** Bluetooth name to be searched */
       
   360     RBuf iDesiredName;
       
   361 
       
   362     /** Bluetooth address to be searched */
       
   363     RBuf8 iDesiredDevAdrress;
       
   364 
       
   365     /** Session of socket server */
       
   366     RSocketServ& iSocketServ;
       
   367 
       
   368     /** Pointer to bluetooth settings (power state, local name). Owned */
       
   369     CBTEngSettings* iBTSettings;
       
   370 
       
   371     /** Pointer to bluetooth connection manager. Owned */
       
   372     CBTEngConnMan* iBTConnection;
       
   373 
       
   374     /** Pointer to class responsible SDP queries */
       
   375     CBTEngDiscovery* iBTDiscovery;
       
   376 
       
   377     /** Pointer to class responsible for device discovery */
       
   378     CHsDeviceDiscoverer* iDiscoverer;
       
   379 
       
   380     /** Pointer to observer */
       
   381     MHsBTManagerObserver* iBTManagerObserver;
       
   382 };
       
   383 
       
   384 #endif // HSTOOLS_H