bluetoothengine/headsetsimulator/core/inc/RemoteControl/hsremotecontrolserver.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 HSREMOTECONTROLSERVER_H
#define HSREMOTECONTROLSERVER_H

#include <bt_sock.h> 
#include <e32base.h>
#include <es_sock.h>
#include <btsdp.h>
#include <hsclientobservers.h>

class CHsCore;
class CHsRemoteControlDataHandler;

/**  
 * The id used for advertising and discovering the remote control service. 
 */
const TUint32 KHsRemoteControlServiceID = 0xfabc234f;

/** Avalaible service state */
const TInt KServicAvailable = 0xFF;

/** Unavalaible service state */
const TInt KServiceUnavailable = 0x00;

/** Buffer size */
const TInt KHsRemoteControlBufferSize = 256;

/** The number of connections allowed in the queue */
const TInt KHsSizeOfListenQueue = 5;

/** Useful constants */
_LIT(KHsRFComm,"RFCOMM");
_LIT(KServiceName, "HSControlServer");
_LIT(KServiceDesc, "HSControlServer");

/** Buffer for remote control */
typedef TBuf8 <KHsRemoteControlBufferSize> THsRemoteControlBuffer;

/**
 * @brief Server for remote control requests
 */
class CHsControlServer : public CBase, public MBluetoothSocketNotifier
{

public:

    /**
     * Two-phased constructor.
     * 
     * @param aSocketServ sesion to socket server
     * @param aHsCore pointer to CHsCore
     * @return class instance
     */
    static CHsControlServer* NewL( RSocketServ& aSocketServ, CHsCore* aHsCore );

    /**
     * Two-phased constructor.
     * 
     * @param aSocketServ session to socket server
     * @param aHsCore pointer to CHsCore
     * @return class instance
     */
    static CHsControlServer* NewLC( RSocketServ& aSocketServ, 
            CHsCore* aHsCore );

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

public:

    /**
     * Turns on server for remote control
     */
    void StartRemoteControlServerL();

    /**
     * Stops remote control server. Cancels receiving and sending data, destroys
     * all CBluetoothSockets
     */
    void CancelListen();

    /**
     * Setter for observer
     * 
     * @param aConnectionObserver observer
     */
    void SetConnectionObserver(
            MRemoteControllerConnectionObserver &aConnectionObserver );

private:

    /**
     * Constructor for performing 1st stage construction
     * 
     * @param aSocketServ sesion to socket server
     * @param aHsCore pointer to CHsCore
     */
    CHsControlServer( RSocketServ& aSocketServ, CHsCore* aHsCore );

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

private:
    /**
     * Sends data to client.
     * 
     * @param aData message to be send
     *  
     * @return error code
     */
    TInt Send( const TDesC8& aData );

    /**
     * Receives data from listening socket.
     */
    void Receive();

    /**
     * Registers service in SDP database
     *  
     * @param aChannel port number
     * @return error code. KErrNone if success
     */
    void RegisterSdpL( TInt aChannel );

    /**
     * Updates availability of service registered in SDP database
     * 
     * @param aAvailable service availability
     */
    void UpdateAvailabilityL( TBool aAvailable );

    /**
     * Deletes service record from SDP database
     */
    void DeleteRecordSdp();

    /**
     * Handles incoming remote control request
     * 
     * @param aRequest 8-bit desciptor containing request
     */
    void HandleRequestL( TDes8& aRequest );

    /**
     * Retrieves 'HsTurnOn' command's data needed for HS's initialization
     * 
     * @param aParams command's data
     * @param aPluginCod Cod plugin's name
     * @param aPluginSdp Sdp plugin's name
     * @param aPluginName profile plugin's name
     */
    void RetrieveStartupParamsL( const TDesC8& aParams, RBuf8& aPluginCod,
            RBuf8& aPluginSdp, RBuf8& aPluginProfile );

private:
    //Methods inherited from MBluetoothSocketNotifier

    void HandleConnectCompleteL( TInt aErr );

    void HandleAcceptCompleteL( TInt aErr );

    void HandleShutdownCompleteL( TInt aErr );

    void HandleSendCompleteL( TInt aErr );

    void HandleReceiveCompleteL( TInt aErr );

    void HandleIoctlCompleteL( TInt aErr );

    void HandleActivateBasebandEventNotifierCompleteL( TInt aErr,
            TBTBasebandEventNotification& aEventNotification );

private:

    /** States of the Remote control server */
    enum THsRemoteControlState
    {
        /** Not initialized */
        ENone = 1,
        /** Connection attempt ongoing */
        EConnecting,
        /** Ready for clients */
        EWaiting
    };

    /** Current state */
    THsRemoteControlState iState;

    /** Listening status */
    TBool iInitialized;

    /** Server port */
    TInt iServerPort;

    /** Service record state */
    TInt iRecordState;

    /** Data length */
    TSockXfrLength iDataLength;

    /** Holds received data */
    THsRemoteControlBuffer iReceiveDataBuffer;

    /** Used for buffering data */
    THsRemoteControlBuffer iTempDataBuffer;

    /** Holds data to be send */
    THsRemoteControlBuffer iSendDataBuffer;

    /** Service record */
    TSdpServRecordHandle iSdpRecordHandle;

    /** Service discovery database session */
    RSdp iSdpServer;

    /** Service discovery database subsession */
    RSdpDatabase iSdpDB;

    /** Creates subsession to socket server */
    RSocketServ &iSocketServ;

    /** Socket for listening for new connections */
    CBluetoothSocket *iListenSocket;

    /** Socket */
    CBluetoothSocket *iSocket;

    /** Pointer to CHsCore */
    CHsCore *iHsCore;

    /** Pointer to CHsRemoteControlDataHandler */
    CHsRemoteControlDataHandler* iDataHandler;

    /** Pointer to observer */
    MRemoteControllerConnectionObserver *iConnectionObserver;

};

#endif // HSREMOTECONTROLSERVER_H