bluetoothengine/headsetsimulator/remotecontroller/inc/bttools/hsrcbttools.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 HSRCBTTOOLS_H
       
    17 #define HSRCBTTOOLS_H
       
    18 
       
    19 #include <btsdp.h>
       
    20 #include <bt_sock.h>
       
    21 #include <btengconnman.h>
       
    22 #include <btengsettings.h>
       
    23 #include <btengdiscovery.h>
       
    24 
       
    25 class CBTEngSettings;
       
    26 class CBTEngConnMan;
       
    27 class CBTEngDiscovery;
       
    28 class RSocketServ;
       
    29 class MHsRCBTManagerObserver;
       
    30 
       
    31 /** Length of device bluetooth address */
       
    32 const TInt KDevAddrLength = KBTDevAddrSize * 2;
       
    33 
       
    34 /** default bluetooth name */
       
    35 _LIT(KHsRCDefaultName,"HS_RemoteController");
       
    36 
       
    37 /**
       
    38  * @brief Observes devices found during inquiry 
       
    39  */
       
    40 class MHsRCDeviceDiscovererObserver
       
    41 {
       
    42 public:
       
    43     /**
       
    44      * Informs that device was found during inquiry
       
    45      * 
       
    46      * @param aBTAddress 8-bit descriptor containing bluetooth address
       
    47      * @param aBTName  8-bit descriptor containing bluetooth name
       
    48      * @param aMajServClass major service class - part of Class of Device
       
    49      * @param aMajDeviceClass major device class - part of Class of Device
       
    50      * @param aMinDeviceClass minor device class - part of Class of Device
       
    51      */
       
    52     virtual void HandleDeviceFindSuccessL( TDesC8 &aBTAddress, TDesC8 &aBTName,
       
    53             TUint16 aMajServClass, TUint aMajDeviceClass,
       
    54             TUint8 aMinDeviceClass )=0;
       
    55 
       
    56     /**
       
    57      * Informs that inquiry finished 
       
    58      * 
       
    59      * @param aErr error value. If KErrEof - proper finish, otherwise error 
       
    60      *        occured 
       
    61      */
       
    62     virtual void HandleDeviceFindFailed( TInt aErr )=0;
       
    63 };
       
    64 
       
    65 /**
       
    66  * @brief Class asynchronously finds devices equipped with bluetooth
       
    67  */
       
    68 class CHsRCDeviceDiscoverer : public CActive
       
    69 {
       
    70 public:
       
    71     /**
       
    72      * Two-phase constructor
       
    73      * @param aDeviceObserver inquiry observer
       
    74      * @param aSServ session of application
       
    75      * 
       
    76      * @return instance of class
       
    77      */
       
    78     static CHsRCDeviceDiscoverer
       
    79             * NewL( MHsRCDeviceDiscovererObserver *aDeviceObserver,
       
    80                     RSocketServ &aSServ );
       
    81 
       
    82     /**
       
    83      * Two-phase constructor
       
    84      * @param aDeviceObserver inquiry observer
       
    85      * @param aSServ session of application
       
    86      * 
       
    87      * @return instance of class
       
    88      */
       
    89     static CHsRCDeviceDiscoverer
       
    90     * NewLC( MHsRCDeviceDiscovererObserver *aDeviceObserver,
       
    91             RSocketServ &aSServ );
       
    92     /**
       
    93      * Destructor
       
    94      */
       
    95     ~CHsRCDeviceDiscoverer();
       
    96 
       
    97 public:
       
    98 
       
    99     /**
       
   100      * Async method for searching devices
       
   101      */
       
   102     void DeviceSearchL();
       
   103 
       
   104     /**
       
   105      * Gets device's bluetooth address
       
   106      * 
       
   107      * @return TBTDevAddr address of device
       
   108      */
       
   109     TBTDevAddr GetBTDevAddress();
       
   110 
       
   111 private:
       
   112     /**
       
   113      * Constructor for performing 1st stage construction
       
   114      * 
       
   115      * @param aDeviceObserver pointer to inquiry observer 
       
   116      * @param aSServ subsession of socket server 
       
   117      */
       
   118     CHsRCDeviceDiscoverer( MHsRCDeviceDiscovererObserver* aDeviceObserver,
       
   119             RSocketServ& aSServ );
       
   120 
       
   121     /**
       
   122      * Constructor for performing 2nd stage construction
       
   123      */
       
   124     void ConstructL();
       
   125 
       
   126 private:
       
   127 
       
   128     /**
       
   129      * Initializes and connects host resolver
       
   130      */
       
   131     void ConnectHostResolverL();
       
   132 
       
   133 private:
       
   134     /**
       
   135      * CActive method
       
   136      */
       
   137     void RunL();
       
   138 
       
   139     /**
       
   140      * CActive method
       
   141      */
       
   142     void DoCancel();
       
   143 
       
   144 private:
       
   145 
       
   146     /** Session of socket server */
       
   147     RSocketServ& iSServ;
       
   148 
       
   149     /** Provides methods to find device*/
       
   150     RHostResolver iHRes;
       
   151 
       
   152     /** Bluetooth device's name */
       
   153     TBuf <KMaxBluetoothNameLen> iName;
       
   154 
       
   155     /** Socket address used for inquiry */
       
   156     TInquirySockAddr iSockAddr;
       
   157 
       
   158     /** Bluetooth address */
       
   159     TBTDevAddr iDevAddr;
       
   160 
       
   161     /** Wrapps bluetooth device"*/
       
   162     TNameEntry iEntry;
       
   163 
       
   164     /** Denotes if searching is in progress*/
       
   165     TBool iSearching;
       
   166 
       
   167     /** 
       
   168      * Pointer to observer 
       
   169      * not owned
       
   170      */
       
   171     MHsRCDeviceDiscovererObserver* iDeviceObserver;
       
   172 };
       
   173 
       
   174 /**
       
   175  * @brief Provides handling of bluetooth. Manages Headset Simulator searching 
       
   176  *        and connection
       
   177  */
       
   178 class CHsRCBTManager : public CBase, public MBTEngSettingsObserver,
       
   179         public MBTEngConnObserver, public MBTEngSdpResultReceiver,
       
   180         public MHsRCDeviceDiscovererObserver
       
   181 {
       
   182 
       
   183 public:
       
   184 
       
   185     /**
       
   186      * Two-phased constructor
       
   187      * 
       
   188      * @param aSServ socket server subsession
       
   189      * @param aBTManagerObserver pointer to observer
       
   190      * 
       
   191      * @return instance of class
       
   192      */
       
   193     static CHsRCBTManager* NewL( RSocketServ& aSServ,
       
   194             MHsRCBTManagerObserver* aBTManagerObserver );
       
   195 
       
   196     /**
       
   197      * Two-phased constructor.
       
   198      * 
       
   199      * @param aSServ socket server subsession
       
   200      * @param aBTManagerObserver pointer to observer
       
   201      * 
       
   202      * @return instance of class
       
   203      */
       
   204     static CHsRCBTManager* NewLC( RSocketServ& aSServ,
       
   205             MHsRCBTManagerObserver* aBTManagerObserver );
       
   206 
       
   207     /**
       
   208      * Destructor
       
   209      */
       
   210     ~CHsRCBTManager();
       
   211 
       
   212 public:
       
   213 
       
   214     /**
       
   215      * Connects with Headset Simulator by bluetooth address
       
   216      * 
       
   217      * @pre SetService() should be called
       
   218      * 
       
   219      * @param aDevAddr bluetooth address
       
   220      */
       
   221     void ConnectDevAddrL( const TBTDevAddr& aDevAddr );
       
   222 
       
   223     /**
       
   224      * Connects with Headset Simulator by bluetooth name
       
   225      * 
       
   226      * @pre SetService() should be called
       
   227      * 
       
   228      * @param aDeviceName bluetooth name
       
   229      */
       
   230     void ConnectDevNameL( const TDesC& aDeviceName );
       
   231 
       
   232     /**
       
   233      * Cancels ongoing attempt to connect with Headset Simulator
       
   234      */
       
   235     void CancelConnecting();
       
   236 
       
   237     /**
       
   238      * Sets Headset Simulator's remote control service
       
   239      */
       
   240     void SetService( const TUUID& aService );
       
   241 
       
   242 private:
       
   243 
       
   244     /**
       
   245      * Constructor for performing 1st stage construction
       
   246      */
       
   247     CHsRCBTManager( RSocketServ& aSServ,
       
   248             MHsRCBTManagerObserver* aBTManagerObserver );
       
   249 
       
   250     /**
       
   251      * Constructor for performing 2nd stage construction
       
   252      */
       
   253     void ConstructL();
       
   254 
       
   255 private:
       
   256     /** Methods derived from MBTEngSettingsObserver */
       
   257 
       
   258     void PowerStateChanged( TBTPowerStateValue aState );
       
   259 
       
   260     void VisibilityModeChanged( TBTVisibilityMode aState );
       
   261 
       
   262 private:
       
   263     /** Methods derived from MBTEngConnObserver */
       
   264 
       
   265     void ConnectComplete( TBTDevAddr& aAddr, TInt aErr,
       
   266             RBTDevAddrArray* aConflicts = NULL );
       
   267 
       
   268     void DisconnectComplete( TBTDevAddr& aAddr, TInt aErr );
       
   269 
       
   270     void PairingComplete( TBTDevAddr& aAddr, TInt aErr );
       
   271 
       
   272 private:
       
   273     /** Methods derived from MBTEngSdpResultReceiver */
       
   274 
       
   275     void ServiceSearchComplete( const RSdpRecHandleArray& aResult,
       
   276             TUint aTotalRecordsCount, TInt aErr );
       
   277 
       
   278     void AttributeSearchComplete( TSdpServRecordHandle aHandle,
       
   279             const RSdpResultArray& aAttr, TInt aErr );
       
   280 
       
   281     void ServiceAttributeSearchComplete( TSdpServRecordHandle aHandle,
       
   282             const RSdpResultArray& aAttr, TInt aErr );
       
   283 
       
   284     void DeviceSearchComplete( CBTDevice* aDevice, TInt aErr );
       
   285 
       
   286 private:
       
   287     /** Methods derived from  MDeviceDiscovererObserver */
       
   288 
       
   289     void HandleDeviceFindSuccessL( TDesC8 & address, TDesC8 & name,
       
   290             TUint16 aMajServClass, TUint aMajDeviceClass,
       
   291             TUint8 aMinDeviceClass );
       
   292 
       
   293     void HandleDeviceFindFailed( TInt aErr );
       
   294 
       
   295 private:
       
   296 
       
   297     /**
       
   298      * Finds port on Audio Gateway
       
   299      * 
       
   300      * @return error value. KErrNotFound in case of failure, otherwise KErrNone
       
   301      */
       
   302 
       
   303     TInt FindPort( const RSdpResultArray& aAttr, TInt& aPort );
       
   304 
       
   305     /**
       
   306      * Restores local name and power state to initial values 
       
   307      */
       
   308     void RestoreInitialSettingsL();
       
   309 
       
   310     /**
       
   311      * Sets local bluetooth name
       
   312      * @param aBTName new local name
       
   313      */
       
   314     void SetBTNameL( const TDesC& aBTName );
       
   315 
       
   316     /*
       
   317      * Turns on bluetooth with specified (optional) name
       
   318      * 
       
   319      * @param aName bluetooth name
       
   320      */
       
   321     void TurnOnBTL( const TDesC& aName = KHsRCDefaultName );
       
   322 
       
   323     /*
       
   324      * Turns off bluetooth on device
       
   325      */
       
   326     void TurnOffBTL();
       
   327 
       
   328     /*
       
   329      * Finds service on a specified device
       
   330      * 
       
   331      * @param aDevAddr  bluetooth address
       
   332      * @param aService  service UUID
       
   333      */
       
   334     void FindServiceL( const TBTDevAddr& aDevAddr, const TUUID& aService );
       
   335 
       
   336 private:
       
   337     /** Searching type enum */
       
   338     enum THsRCSearchType
       
   339     {
       
   340         /** Search device by bluetooth name*/
       
   341         EDevName,
       
   342         /** Search device by bluetooth address*/
       
   343         EDevAddr
       
   344     };
       
   345 
       
   346     /** Searching type */
       
   347     THsRCSearchType iSearchType;
       
   348 
       
   349     /** Service */
       
   350     TUUID iService;
       
   351 
       
   352     /** Needed for restoring bluetooth name */
       
   353     RBuf iBTInitialName;
       
   354 
       
   355     /** Needed for restoring bluetooth power */
       
   356     TBTPowerStateValue iBTInitialPowerState;
       
   357 
       
   358     /** Needed for restoring bluetooth visibility */
       
   359     TBTVisibilityMode iBTInitialVisibilityMode;
       
   360 
       
   361     /** Bluetooth name */
       
   362     RBuf iBTName;
       
   363 
       
   364     /** Bluetooth address */
       
   365     TBTDevAddr iDevAddr;
       
   366 
       
   367     /** Bluetooth address to be searched */
       
   368     RBuf iDesiredMAC;
       
   369 
       
   370     /** Bluetooth name to be searched */
       
   371     RBuf iDesiredName;
       
   372 
       
   373     /** Session of socket server */
       
   374     RSocketServ& iSServ;
       
   375 
       
   376     /** Pointer to bluetooth settings (power state, local name). Owned */
       
   377     CBTEngSettings* iBTSettings;
       
   378 
       
   379     /** Pointer to bluetooth connection manager. Owned */
       
   380     CBTEngConnMan* iBTConnection;
       
   381 
       
   382     /** Pointer to class responsible SDP queries */
       
   383     CBTEngDiscovery* iBTDiscovery;
       
   384 
       
   385     /** Pointer to class responsible for device discovery */
       
   386     CHsRCDeviceDiscoverer* iDiscoverer;
       
   387 
       
   388     /** Pointer to observer */
       
   389     MHsRCBTManagerObserver* iBTManagerObserver;
       
   390 
       
   391     /** Denotes if service is set */
       
   392     TBool iServiceSet;
       
   393 };
       
   394 
       
   395 #endif //HSRCBTTOOLS_H