bluetoothengine/headsetsimulator/remotecontroller/inc/bttools/hsrcbttools.h
author michal.sulewski
Wed, 15 Sep 2010 15:59:44 +0200
branchheadsetsimulator
changeset 60 90dbfc0435e3
permissions -rw-r--r--
source code commit

/*
 * Component Name: Headset Simulator
 * Author: Comarch S.A.
 * Version: 1.0
 * Copyright (c) 2010 Comarch S.A.
 *  
 * This Software is submitted by Comarch S.A. to Symbian Foundation Limited on 
 * the basis of the Member Contribution Agreement entered between Comarch S.A. 
 * and Symbian Foundation Limited on 5th June 2009 (“Agreement”) and may be 
 * used only in accordance with the terms and conditions of the Agreement. 
 * Any other usage, duplication or redistribution of this Software is not 
 * allowed without written permission of Comarch S.A.
 * 
 */

#ifndef HSRCBTTOOLS_H
#define HSRCBTTOOLS_H

#include <btsdp.h>
#include <bt_sock.h>
#include <btengconnman.h>
#include <btengsettings.h>
#include <btengdiscovery.h>

class CBTEngSettings;
class CBTEngConnMan;
class CBTEngDiscovery;
class RSocketServ;
class MHsRCBTManagerObserver;

/** Length of device bluetooth address */
const TInt KDevAddrLength = KBTDevAddrSize * 2;

/** default bluetooth name */
_LIT(KHsRCDefaultName,"HS_RemoteController");

/**
 * @brief Observes devices found during inquiry 
 */
class MHsRCDeviceDiscovererObserver
{
public:
    /**
     * Informs that device was found during inquiry
     * 
     * @param aBTAddress 8-bit descriptor containing bluetooth address
     * @param aBTName  8-bit descriptor containing bluetooth name
     * @param aMajServClass major service class - part of Class of Device
     * @param aMajDeviceClass major device class - part of Class of Device
     * @param aMinDeviceClass minor device class - part of Class of Device
     */
    virtual void HandleDeviceFindSuccessL( TDesC8 &aBTAddress, TDesC8 &aBTName,
            TUint16 aMajServClass, TUint aMajDeviceClass,
            TUint8 aMinDeviceClass )=0;

    /**
     * Informs that inquiry finished 
     * 
     * @param aErr error value. If KErrEof - proper finish, otherwise error 
     *        occured 
     */
    virtual void HandleDeviceFindFailed( TInt aErr )=0;
};

/**
 * @brief Class asynchronously finds devices equipped with bluetooth
 */
class CHsRCDeviceDiscoverer : public CActive
{
public:
    /**
     * Two-phase constructor
     * @param aDeviceObserver inquiry observer
     * @param aSServ session of application
     * 
     * @return instance of class
     */
    static CHsRCDeviceDiscoverer
            * NewL( MHsRCDeviceDiscovererObserver *aDeviceObserver,
                    RSocketServ &aSServ );

    /**
     * Two-phase constructor
     * @param aDeviceObserver inquiry observer
     * @param aSServ session of application
     * 
     * @return instance of class
     */
    static CHsRCDeviceDiscoverer
    * NewLC( MHsRCDeviceDiscovererObserver *aDeviceObserver,
            RSocketServ &aSServ );
    /**
     * Destructor
     */
    ~CHsRCDeviceDiscoverer();

public:

    /**
     * Async method for searching devices
     */
    void DeviceSearchL();

    /**
     * Gets device's bluetooth address
     * 
     * @return TBTDevAddr address of device
     */
    TBTDevAddr GetBTDevAddress();

private:
    /**
     * Constructor for performing 1st stage construction
     * 
     * @param aDeviceObserver pointer to inquiry observer 
     * @param aSServ subsession of socket server 
     */
    CHsRCDeviceDiscoverer( MHsRCDeviceDiscovererObserver* aDeviceObserver,
            RSocketServ& aSServ );

    /**
     * Constructor for performing 2nd stage construction
     */
    void ConstructL();

private:

    /**
     * Initializes and connects host resolver
     */
    void ConnectHostResolverL();

private:
    /**
     * CActive method
     */
    void RunL();

    /**
     * CActive method
     */
    void DoCancel();

private:

    /** Session of socket server */
    RSocketServ& iSServ;

    /** Provides methods to find device*/
    RHostResolver iHRes;

    /** Bluetooth device's name */
    TBuf <KMaxBluetoothNameLen> iName;

    /** Socket address used for inquiry */
    TInquirySockAddr iSockAddr;

    /** Bluetooth address */
    TBTDevAddr iDevAddr;

    /** Wrapps bluetooth device"*/
    TNameEntry iEntry;

    /** Denotes if searching is in progress*/
    TBool iSearching;

    /** 
     * Pointer to observer 
     * not owned
     */
    MHsRCDeviceDiscovererObserver* iDeviceObserver;
};

/**
 * @brief Provides handling of bluetooth. Manages Headset Simulator searching 
 *        and connection
 */
class CHsRCBTManager : public CBase, public MBTEngSettingsObserver,
        public MBTEngConnObserver, public MBTEngSdpResultReceiver,
        public MHsRCDeviceDiscovererObserver
{

public:

    /**
     * Two-phased constructor
     * 
     * @param aSServ socket server subsession
     * @param aBTManagerObserver pointer to observer
     * 
     * @return instance of class
     */
    static CHsRCBTManager* NewL( RSocketServ& aSServ,
            MHsRCBTManagerObserver* aBTManagerObserver );

    /**
     * Two-phased constructor.
     * 
     * @param aSServ socket server subsession
     * @param aBTManagerObserver pointer to observer
     * 
     * @return instance of class
     */
    static CHsRCBTManager* NewLC( RSocketServ& aSServ,
            MHsRCBTManagerObserver* aBTManagerObserver );

    /**
     * Destructor
     */
    ~CHsRCBTManager();

public:

    /**
     * Connects with Headset Simulator by bluetooth address
     * 
     * @pre SetService() should be called
     * 
     * @param aDevAddr bluetooth address
     */
    void ConnectDevAddrL( const TBTDevAddr& aDevAddr );

    /**
     * Connects with Headset Simulator by bluetooth name
     * 
     * @pre SetService() should be called
     * 
     * @param aDeviceName bluetooth name
     */
    void ConnectDevNameL( const TDesC& aDeviceName );

    /**
     * Cancels ongoing attempt to connect with Headset Simulator
     */
    void CancelConnecting();

    /**
     * Sets Headset Simulator's remote control service
     */
    void SetService( const TUUID& aService );

private:

    /**
     * Constructor for performing 1st stage construction
     */
    CHsRCBTManager( RSocketServ& aSServ,
            MHsRCBTManagerObserver* aBTManagerObserver );

    /**
     * Constructor for performing 2nd stage construction
     */
    void ConstructL();

private:
    /** Methods derived from MBTEngSettingsObserver */

    void PowerStateChanged( TBTPowerStateValue aState );

    void VisibilityModeChanged( TBTVisibilityMode aState );

private:
    /** Methods derived from MBTEngConnObserver */

    void ConnectComplete( TBTDevAddr& aAddr, TInt aErr,
            RBTDevAddrArray* aConflicts = NULL );

    void DisconnectComplete( TBTDevAddr& aAddr, TInt aErr );

    void PairingComplete( TBTDevAddr& aAddr, TInt aErr );

private:
    /** Methods derived from MBTEngSdpResultReceiver */

    void ServiceSearchComplete( const RSdpRecHandleArray& aResult,
            TUint aTotalRecordsCount, TInt aErr );

    void AttributeSearchComplete( TSdpServRecordHandle aHandle,
            const RSdpResultArray& aAttr, TInt aErr );

    void ServiceAttributeSearchComplete( TSdpServRecordHandle aHandle,
            const RSdpResultArray& aAttr, TInt aErr );

    void DeviceSearchComplete( CBTDevice* aDevice, TInt aErr );

private:
    /** Methods derived from  MDeviceDiscovererObserver */

    void HandleDeviceFindSuccessL( TDesC8 & address, TDesC8 & name,
            TUint16 aMajServClass, TUint aMajDeviceClass,
            TUint8 aMinDeviceClass );

    void HandleDeviceFindFailed( TInt aErr );

private:

    /**
     * Finds port on Audio Gateway
     * 
     * @return error value. KErrNotFound in case of failure, otherwise KErrNone
     */

    TInt FindPort( const RSdpResultArray& aAttr, TInt& aPort );

    /**
     * Restores local name and power state to initial values 
     */
    void RestoreInitialSettingsL();

    /**
     * Sets local bluetooth name
     * @param aBTName new local name
     */
    void SetBTNameL( const TDesC& aBTName );

    /*
     * Turns on bluetooth with specified (optional) name
     * 
     * @param aName bluetooth name
     */
    void TurnOnBTL( const TDesC& aName = KHsRCDefaultName );

    /*
     * Turns off bluetooth on device
     */
    void TurnOffBTL();

    /*
     * Finds service on a specified device
     * 
     * @param aDevAddr  bluetooth address
     * @param aService  service UUID
     */
    void FindServiceL( const TBTDevAddr& aDevAddr, const TUUID& aService );

private:
    /** Searching type enum */
    enum THsRCSearchType
    {
        /** Search device by bluetooth name*/
        EDevName,
        /** Search device by bluetooth address*/
        EDevAddr
    };

    /** Searching type */
    THsRCSearchType iSearchType;

    /** Service */
    TUUID iService;

    /** Needed for restoring bluetooth name */
    RBuf iBTInitialName;

    /** Needed for restoring bluetooth power */
    TBTPowerStateValue iBTInitialPowerState;

    /** Needed for restoring bluetooth visibility */
    TBTVisibilityMode iBTInitialVisibilityMode;

    /** Bluetooth name */
    RBuf iBTName;

    /** Bluetooth address */
    TBTDevAddr iDevAddr;

    /** Bluetooth address to be searched */
    RBuf iDesiredMAC;

    /** Bluetooth name to be searched */
    RBuf iDesiredName;

    /** Session of socket server */
    RSocketServ& iSServ;

    /** Pointer to bluetooth settings (power state, local name). Owned */
    CBTEngSettings* iBTSettings;

    /** Pointer to bluetooth connection manager. Owned */
    CBTEngConnMan* iBTConnection;

    /** Pointer to class responsible SDP queries */
    CBTEngDiscovery* iBTDiscovery;

    /** Pointer to class responsible for device discovery */
    CHsRCDeviceDiscoverer* iDiscoverer;

    /** Pointer to observer */
    MHsRCBTManagerObserver* iBTManagerObserver;

    /** Denotes if service is set */
    TBool iServiceSet;
};

#endif //HSRCBTTOOLS_H