bluetoothengine/headsetsimulator/core/inc/Tools/hstools.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 HSTOOLS_H
#define HSTOOLS_H
#include <btsdp.h> 
#include <btengconnman.h> 
#include <btengsettings.h>
#include <btengdiscovery.h>
#include <hsclientobservers.h>

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

/** Default bluetooth name */
_LIT(KHsDefaultName,"HS_Name");

/** Headset protocol name */
_LIT(KHsProtocolName,"BTLinkManager");

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

/**
 * @brief Observer to notify about found devices during inquiry.
 */
class MHsDeviceDiscovererObserver
{
public:
    /**
     * Informs that device was found during inquiry
     * 
     * @param aDevAddress 8-bit descriptor containing bluetooth address
     * @param aDevName  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 & aDevAddress,
            TDesC8 & aDevName, TUint16 aMajServClass, TUint aMajDeviceClass,
            TUint8 aMinDeviceClass ) = 0;

    /**
     * Informs that inquiry finished
     * 
     * @param aErr system-wide error code
     */
    virtual void HandleDeviceFindFailed( TInt aErr ) = 0;
};

/**
 * @brief Class asynchronously finds devices equipped with bluetooth.
 */
class CHsDeviceDiscoverer : public CActive
{
public:
    /**
     * Two-phase constructor
     * @param aDeviceObserver inquiry observer
     * @param aSocketServ session of socket server
     * @return instance of class
     */
    static CHsDeviceDiscoverer* NewL(
            MHsDeviceDiscovererObserver *aDeviceObserver,
            RSocketServ &aSocketServ );

    /**
     * Two-phase constructor
     * @param aDeviceObserver inquiry observer
     * @param aSocketServ session of socket server
     * @return instance of class
     */
    static CHsDeviceDiscoverer* NewLC(
            MHsDeviceDiscovererObserver *aDeviceObserver,
            RSocketServ &aSocketServ );

    /**
     * Destructor
     */
    ~CHsDeviceDiscoverer();

public:
    /**
     * Asynchronous method for searching devices.
     */
    void DeviceSearchL();

private:
    /**
     * Constructor for performing 1st stage construction
     * 
     * @param aDeviceObserver pointer to inquiry observer 
     * @param aSocketServ session of socket server 
     */
    CHsDeviceDiscoverer( MHsDeviceDiscovererObserver *aDeviceObserver,
            RSocketServ &aSocketServ );

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

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

private:
    /**
     * CActive method. 
     * When request of searching devices completes with success, notification
     * is sent to MHsDeviceDiscovererObserver::HandleDeviceFindSuccessL(), 
     * otherwise to MHsDeviceDiscovererObserver::HandleDeviceFindFailed().
     */
    void RunL();

    /**
     * CActive method. Cancels of an outstanding request of searching devices.
     */
    void DoCancel();

private:

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

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

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

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

    /**Provides methods to find devices*/
    RHostResolver iHostResolver;

    /** Pointer to observer to notify about founded devices. Not owned. */
    MHsDeviceDiscovererObserver *iDeviceObserver;
};

/**
 * @brief Provides handling of bluetooth. Manages searching for and connection with AG.
 */
class CHsBTManager : public CBase, public MBTEngSettingsObserver,
        public MBTEngConnObserver, public MHsDeviceDiscovererObserver,
        public MBTEngSdpResultReceiver
{

public:

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

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

    /**
     * Destructor.
     */
    ~CHsBTManager();

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

    /**
     * Turns on bluetooth on device.
     * @param aName new bluetooth name
     */
    void TurnBtOnL( const TDesC& aName = KHsDefaultName );

    /**
     * Turns off bluetooth on device.
     */
    void TurnBtOffL();

    /**
     * Connects with AG by bluetooth address
     * 
     * @pre SetService() should be called
     * 
     * @param aDevAddress bluetooth address
     */
    void FindDevL( const TDesC& aDevAddress );

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

    /**
     * Sets Headset Simulator's service
     * 
     * @param aService service Uuid
     */
    void SetService( const TUUID& aService );

    /**
     * Cancels ongoing attempt to connect with AG.
     */
    void CancelConnecting();

    /**
     * Sets bluetooth visibility mode.
     * 
     * @param aVisibilityMode mode
     * 
     * @return error value. KErrNotFound in case of failure, otherwise KErrNone
     */
    TInt SetVisible( TBTVisibilityMode aVisibilityMode );

public:
    // Methods derived from  MHsDeviceDiscovererObserver
    void HandleDeviceFindSuccessL( TDesC8& aDevAddress, TDesC8& aDevName,
            TUint16 aMajServClass, TUint aMajDeviceClass,
            TUint8 aMinDeviceClass );

    void HandleDeviceFindFailed( TInt aErr );

public:
    // Methods derived from MBTEngSettingsObserver
    void PowerStateChanged( TBTPowerStateValue aState );

    void VisibilityModeChanged( TBTVisibilityMode aState );

public:
    // 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 );

public:
    // 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:

    /**
     * Constructor for performing 1st stage construction
     */
    CHsBTManager( RSocketServ& aSocketServ,
            MHsBTManagerObserver* aBTManagerObserver );

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

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

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

    /**
     * Finds port on AG.
     * 
     * @param aAttr Sdp result array
     * @param aPort on return, port number
     * 
     * @return error value. KErrNotFound in case of failure, otherwise KErrNone
     */
    TInt FindPort( const RSdpResultArray& aAttr, TInt &aPort );

private:
    /** Searching type enum */
    enum TLookingFor
    {
        /** Search device by bluetooth name*/
        EName,
        /** Search device by bluetooth address*/
        EDevAddress
    };

    /** Searching type */
    TLookingFor iLookingFor;

    /** Service */
    TUUID iService;

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

    /** Bluetooth address */
    TBTDevAddr iDevAddr;

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

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

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

    /** Bluetooth name */
    RBuf iBTName;

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

    /** Bluetooth address to be searched */
    RBuf8 iDesiredDevAdrress;

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

    /** 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 */
    CHsDeviceDiscoverer* iDiscoverer;

    /** Pointer to observer */
    MHsBTManagerObserver* iBTManagerObserver;
};

#endif // HSTOOLS_H