bluetoothengine/headsetsimulator/core/inc/hscore.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 HSCORE_H
#define HSCORE_H

#include <es_sock.h>
#include <btdevice.h>
#include <bttypes.h>
#include <hsobserver.h>
#include <hsclientobservers.h>

class CHsProfileManager;
class CHsControlServer;
class CHsServer;
class CHsClientManager;
class CHsBTManager;
class CHsAudioServer;

/** Index of AG to which data should be send */
const TInt KHsClientSendNumber = 0;

/** Length of plugin identificator data */
const TInt KHsIdentificationFieldBufSize = 128;

/** Length of command data */
const TInt32 KHsBufferLength = 0x800;

/** Headset property Uid */
const TUid KHSProperty =
    {
    0x1000008d
    };

/** Length of last-connected device bluetooth address */
const TInt KLastDevAddrLength = KBTDevAddrSize * 2;

/** Headset property key enum */
enum THSPropertyKey
{
    /** Last-connected AG*/
    ELastDevice
};

/** Bluetooth friendly name of device */
typedef TBuf <KMaxFriendlyNameLen> TBTFriendlyName;

/** Data buffer from profile to be send to client */
typedef TBuf8 <KHsBufferLength> TBufferFromProfileToClient;

/**
 * @brief Class responsible for main functionality of Headset Simulator.
 */
class CHsCore : public CBase, public MHsClientStateNotifier,
        public MHsAcceptNewClientObserver, public MHsBTManagerObserver,
        public MRemoteControllerConnectionObserver
{
public:
    /**
     * Two-phase constructor
     * @param aService service UUID
     * @return instance of class
     */
    IMPORT_C static CHsCore* NewL( const TUUID &aService );

    /**
     * Two-phase constructor
     * @param aService service UUID
     * @return instance of class
     */
    IMPORT_C static CHsCore* NewLC( const TUUID &aService );

    /**
     * Destructor
     */
    IMPORT_C ~CHsCore();

public:
    /**
     * Starts simulation of Headset.
     * Turns bluetooth on and sets local name, starts Headset server, loads 
     * CoD, Sdp and profile plugins.
     * 
     * @param aCoDUid Class of Device Uid
     * @param aSdpUid Sdp Uid
     * @param aProfileUid profile Uid
     * @param aBTName bluetooth name
     */
    IMPORT_C void StartSimulationL( const TUid &aCoDUid, const TUid &aSdpUid,
            const TUid &aProfileUid, const TDesC &aBTName = KNullDesC );

    /**
     * Starts simulation of Headset.
     * Turns bluetooth on and sets local name, starts Headset server, loads 
     * CoD, Sdp and profile plugins.
     * 
     * @param aCoDUid Class of Device Uid as descriptor
     * @param aSdpUid Sdp Uid as descriptor
     * @param aProfileUid profile Uid as descriptor
     * @param aBTName bluetooth name
     */
    IMPORT_C void StartSimulationL( const TDesC8 &aCoDUid,
            const TDesC8 &aSdpUid, const TDesC8 &aProfileUid,
            const TDesC &aBTName = KNullDesC );

    /**
     * Stops simulation of Headset.
     * Disconnects from AGs, Remote Controller, restores initial bluetooth 
     * settings.
     */
    IMPORT_C void StopSimulation();

    /**
     * Connects AG by its bluetooth name.
     * 
     * When request completes with success notification is sent to 
     * MHsObserver::HandleConnectedToClient(), otherwise to 
     * MHsObserver::HandleUnableToConnectClient().
     * 
     * @param aName AG's bluetooth name
     */
    IMPORT_C void ConnectWithName( const TDesC &aName );

    /**
     * Connects AG by its bluetooth address.
     * 
     * When request completes with success notification is sent to 
     * MHsObserver::HandleConnectedToClient(), otherwise to 
     * MHsObserver::HandleUnableToConnectClient().
     * 
     * @param aDevAddress AG's bluetooth address
     */
    IMPORT_C void ConnectWithDevAddress( const TDesC &aDevAddress );

    /**
     * Connects last-connected AG.
     * 
     * When request completes with success notification is sent to 
     * MHsObserver::HandleConnectedToClient(), otherwise to 
     * MHsObserver::HandleUnableToConnectClient().
     * 
     * @pre Invocation of this method must be after at least one call of 
     * ConnectWithName or ConnectWithDevAddress which saves connection 
     * parameters.
     */
    IMPORT_C void ConnectWithLastConnectedL();

    /**
     * Cancels attempt of connection with AG.
     */
    IMPORT_C void CancelConnecting();

    /**
     * Disconnects from AGs.
     *
     * When request completes notification is sent to 
     * MHsObserver::HandleDisconnectedFromClient().
     */
    IMPORT_C void DisconnectClients();

    /**
     * Sends command to AG. Data is handled by profile manager before.
     * 
     * @param aData data to be handled and response send
     * @param aNumber specifies to which AG response command should be send
     * @return KErrNone if successful, otherwise one of the system-wide 
     *          error codes
     */
    IMPORT_C TInt Send( const TDesC8 &aData, 
            TInt aNumber = KHsClientSendNumber );

    /**
     * Sets observer of Headset actions.
     * 
     * @param aObserver reference to MHsObserver
     */
    IMPORT_C void SetObserver( MHsObserver &aObserver );

    /**
     * Accepts incoming call. If call is not pending, nothing happens.
     * 
     * @return error value
     */
    IMPORT_C TInt AcceptIncomingCall();

    /**
     * Releases ongoing call. If call does not exist, nothing happens.
     * 
     * @return error value
     */
    IMPORT_C TInt ReleaseOngoingCall();

private:
    /**
     * Constructor for performing 1st stage construction
     * 
     * @param aService service UUID
     */
    CHsCore( const TUUID &aService );

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

private:

    /**
     * Simulation initialization. Starts bluetooth with optionally 
     * specified name and server
     * 
     * @param aBTName bluetooth name 
     */
    void InitializeSimulationL( const TDesC &aBTName = KNullDesC );

    /**
     * Registers Headset property.
     */
    void RegisterProperty();

    /**
     * Creates Headset components.
     * 
     * @leave system-wide error codes if any component's creation fails.
     */
    void CreateComponentsL();

    /**
     * Destroys Headset components.
     */
    void DestroyComponents();

    /**
     * Creates Headset server.
     * 
     * @return KErrNone if successful, otherwise one of the system-wide 
     *         error codes
     */
    TInt CreateHSServer();

    /**
     * Starts Headset server.
     * 
     * @return KErrNone if successful, otherwise one of the system-wide 
     *         error codes
     */
    TInt StartHSServer();

    /**
     * Stops Headset server.
     */
    void StopHSServer();

    /**
     * Destroys Headset server.
     */
    void DestroyHSServer();

    /**
     * Creates audio server.
     * 
     * @return KErrNone if successful, otherwise one of the system-wide 
     *         error code
     */
    TInt CreateAudioServer( TBool aPullAudio = EFalse );

    /**
     * Starts audio server.
     * 
     * @param aPullAudio denotes if SCO should be accepted
     * @return KErrNone if successful, otherwise one of the system-wide 
     *         error code
     * 
     */
    TInt StartAudioServer( TBool aPullAudio = EFalse );

    /**
     * Stops audio server.
     */
    void StopAudioServer();

    /**
     * Destroys audio server.
     */
    void DestroyAudioServer();

    /**
     * Creates AG clients manager.
     * 
     * @return KErrNone if successful, otherwise one of the system-wide
     *         error codes
     */
    TInt CreateClientManager();

    /**
     * Destroys AG clients manager.
     */
    void DestroyClientManager();

    /**
     * Creates plugin manager.
     * 
     * @return KErrNone if successful, otherwise one of the system-wide 
     *         error codes
     */
    TInt CreatePluginManager();

    /**
     * Destroys plugin manager.
     */
    void DestroyPluginManager();

    /**
     * Creates bluetooth manager.
     * 
     * @return KErrNone if successful, otherwise one of the system-wide 
     *         error codes
     */
    TInt CreateBTManager();

    /**
     * Destroys bluetooth manager.
     */
    void DestroyBTManager();

    /**
     * Creates Remote Control server.
     * 
     * @return KErrNone if successful, otherwise one of the system-wide 
     *         error codes
     */
    TInt CreateControlServer();

    /**
     * Destroys Remote Control server.
     */
    void DestroyControlServer();

    /**
     * Sets Headset bluetooth friendly name.
     * 
     * @param aBTName bluetooth name
     */
    void SetBTName( const TBTFriendlyName &aBTName );

    /**
     * Gets last-connected AG's bluetooth address.
     * 
     * @param aBTDevAddr on return, the value of bluetooth address 
     * @return KErrNone if successful, otherwise one of the system-wide 
     *         error codes
     */
    TInt GetLastConnected( TBTDevAddr &aBTDevAddr );

    /**
     * Saves connection parameter - bluetooth address of last-connected AG.
     * 
     * @param aSocket socket which AG is connected
     * @return KErrNone if successful, otherwise one of the system-wide 
     *         error codes
     */
    TInt SaveClient( CBluetoothSocket* aSocket );

    /**
     * Sets listening Headset server port to be used in settings of Sdp record.
     */
    void SetSdpRecordServicePortFromServer();

    /**
     * Loads profile.
     * 
     * @param aProfileName profile name
     * @pre CoD and Sdp plugins loaded.
     */
    void LoadProfileL( const TDesC8 &aProfileName );

    /**
     * Loads profile.
     * 
     * @param aProfileUid profile Uid
     * @pre CoD and Sdp plugins loaded.
     */
    void LoadProfileL( const TUid &aProfileUid );

    /**
     * Loads Sdp.
     * 
     * @param aSdpName Sdp name
     */
    void LoadSdpL( const TDesC8 &aSdpName );

    /**
     * Loads Sdp.
     * 
     * @param aSdpUid Sdp Uid
     */
    void LoadSdpL( const TUid &aSdpUid );

    /**
     * Loads CoD plugin
     * 
     * @param aCodName CoD name
     */
    void LoadCodL( const TDesC8 &aCoDName );

    /**
     * Loads CoD plugin
     * 
     * @param aCodUid CoD Uid
     */
    void LoadCodL( const TUid &aCoDUid );

private:
    //Methods inherited from MHsClientStateNotifier
    void HandleClientConnectL( TInt aErr );
    
    void HandleClientDisconnectL( TInt aErr );
    
    void HandleClientReceiveCompleteL( const TDesC8 &aData, TInt aErr );
    
    void HandleClientSendCompleteL( TInt aErr );
    
    void HandleClientOtherProblemL( TInt aErr );

private:
    //Methods inherited from MHsAcceptNewClientObserver
    void HandleNewClientL( CBluetoothSocket *aClientSocket, TInt aErr );

private:
    //Methods inherited from MHsBTManagerObserver
    void HandleDesiredDeviceFound( const TDesC &aDevName,
            const TBTDevAddr &aDevAddr, const TInt aPort );
    
    void HandleDesiredDeviceNotFound( TInt aErr );
    
    void HandleSdpQueryError( TInt aErr );
    
    void HandleServiceNotFound( TInt aErr );
    
    void HandlePortNotFound( TInt aErr );

private:
    //Methods inherited from MRemoteControllerConnectionObserver
    void HandleRemoteControllerConnected( TInt aErr );
    
    void HandleRemoteControllerDisconnected( TInt aErr );

private:

    /** Plugin types */
    enum TPluginType
    {
        /** CoD plugin */
        ECoDPlugin,
        /** Sdp plugin */
        ESdpPlugin,
        /** Profile plugin */
        EProfilePlugin,
        /** Number of plugins */
        EEnd,
    };

    /** Service UUID */
    TUUID iService;

    /** Bluetooth friendly name of device */
    TBTFriendlyName iBTFriendlyName;

    /** Data buffer from profile to be send to AG */
    TBufferFromProfileToClient iBufferFromProfileToClient;

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

    /** Pointer to CHsServer. Owned */
    CHsServer* iHSServer;

    /** Pointer to CHsAudioServer. Owned */
    CHsAudioServer* iAudioServer;

    /** Pointer to CHsControlServer. Owned */
    CHsControlServer *iHSControlServer;

    /** Pointer to CHsClientManager. Owned */
    CHsClientManager *iHSClientManager;

    /** Pointer to CHsProfileManager. Owned */
    CHsProfileManager *iHSProfileManager;

    /** Pointer to CHsBTManager. Owned */
    CHsBTManager *iHsBTManager;

    /** Pointer to MHsObserver. Not owned */
    MHsObserver *iObserver;

};

#endif  // HSCORE_H