htiui/HtiCommPlugins/HtiBtCommPlugin/BtEngine/inc/BtSerialClient.h
changeset 0 d6fe6244b863
child 3 2703485a934c
equal deleted inserted replaced
-1:000000000000 0:d6fe6244b863
       
     1 /*
       
     2 * Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies).
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description:  Bluetooth serial client.
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 #ifndef __BTSERIALCLIENT_H__
       
    20 #define __BTSERIALCLIENT_H__
       
    21 
       
    22 // INCLUDES
       
    23 #include <e32base.h>
       
    24 #include <es_sock.h>
       
    25 #include <bt_sock.h>
       
    26 #include <BTextNotifiers.h>
       
    27 #include <BtSdp.h>
       
    28 
       
    29 #include "SocketObserver.h"
       
    30 
       
    31 // FORWARD DECLARATIONS
       
    32 class CMessageServiceSearcher;
       
    33 class CSocketsReader;
       
    34 class CSocketsWriter;
       
    35 
       
    36 
       
    37 class MBtSerialClientObserver
       
    38     {
       
    39 public:
       
    40     virtual void ConnectedToServer(TInt aError) = 0;
       
    41     virtual void DisconnectedFromServer() = 0;
       
    42     virtual void DataFromServer(const TDesC8& aData) = 0;
       
    43     virtual void AllBufferedDataSent() = 0;
       
    44     };
       
    45 
       
    46 /**
       
    47 * CBtSerialClient
       
    48 * Connects and sends messages to a remote machine using bluetooth
       
    49 */
       
    50 class CBtSerialClient : public CActive, public MSocketObserver
       
    51     {
       
    52     public: // Constructors and destructor
       
    53 
       
    54         /**
       
    55         * NewL()
       
    56         * Construct a CBtSerialClient
       
    57         * @param aObserver the observer for this BT serial client
       
    58         * @return a pointer to the created instance of CBtSerialClient
       
    59         */
       
    60         IMPORT_C static CBtSerialClient* NewL( MBtSerialClientObserver& aObserver );
       
    61 
       
    62         /**
       
    63         * ~CBtSerialClient()
       
    64         * Destroy the object and release all memory objects.
       
    65         * Close any open sockets.
       
    66         */
       
    67         IMPORT_C virtual ~CBtSerialClient();
       
    68 
       
    69     public:     // New functions
       
    70 
       
    71         /**
       
    72         * ConnectL()
       
    73         * Connect to remote device. Query user for a device.
       
    74         */
       
    75         IMPORT_C void ConnectL();
       
    76 
       
    77         /**
       
    78         * ConnectL()
       
    79         * Connect to remote device.
       
    80         */
       
    81         IMPORT_C void ConnectL(const TBTDevAddr aBTServerDevice, const TInt aPort );
       
    82 
       
    83         /**
       
    84         * ConnectL()
       
    85         * Connect to remote device by device name.
       
    86         */
       
    87         IMPORT_C void ConnectL( const TDesC& aBTServerDeviceName, const TInt aPort );
       
    88 
       
    89         /**
       
    90         * DisconnectL()
       
    91         * Disconnect from remote machine
       
    92         */
       
    93         IMPORT_C void Disconnect();
       
    94 
       
    95         /**
       
    96         * IsConnecting()
       
    97         * @return ETrue if the client is establishing a
       
    98         * connection to the server.
       
    99         */
       
   100         IMPORT_C TBool Connecting();
       
   101 
       
   102         /**
       
   103         * Connected()
       
   104         * @return ETrue if the client is fully connected to the server.
       
   105         */
       
   106         IMPORT_C TBool Connected();
       
   107 
       
   108         /**
       
   109         * ServerAddressL()
       
   110         * @return address of connected server. Leaves with KErrNotReady, if
       
   111         * not connected.
       
   112         */
       
   113         TBTDevAddr ServerAddressL();
       
   114 
       
   115         /**
       
   116         * Add data to outgoing buffer and start sending it to client.
       
   117         *
       
   118         * Leaves with KErrOverflow, if the outgoing buffer cannot
       
   119         * be added all of the aData.
       
   120         * Leaves with KErrNotReady, if client is not connected.
       
   121         * When all data in internal buffer has been sent, observer
       
   122         * is notified (AllBufferedDataSent)
       
   123         */
       
   124         IMPORT_C void SendL(const TDesC8& aData);
       
   125 
       
   126         /**
       
   127         * Issue read operation. Will complete asyncronously.
       
   128         *
       
   129         * Leaves with KErrNotReady, if client is not connected.
       
   130         * Notifies observer, when some data has been read. Caller
       
   131         * is responsible for calling this method again to receive
       
   132         * more data.
       
   133         */
       
   134         IMPORT_C void ReadAsyncL();
       
   135 
       
   136         /**
       
   137         * Query free size of outgoing buffer.
       
   138         */
       
   139         IMPORT_C TInt FreeSpaceInSendBuffer();
       
   140 
       
   141         /**
       
   142         * Query max size of outgoing buffer.
       
   143         */
       
   144         IMPORT_C TInt SendBufferMaxSize();
       
   145 
       
   146         /**
       
   147         * Query the port of the service we are connected with
       
   148         * @return Port number or KErrDisconnected if not connected to a service
       
   149         */
       
   150         IMPORT_C TInt ServicePort();
       
   151 
       
   152     protected:    // from CActive
       
   153 
       
   154         /**
       
   155         * DoCancel()
       
   156         * Cancel any outstanding requests
       
   157         */
       
   158         void DoCancel();
       
   159 
       
   160         /**
       
   161         * RunL()
       
   162         * Respond to an event
       
   163         */
       
   164         void RunL();
       
   165 
       
   166     protected: // from MSocketObserver
       
   167 
       
   168         void ReportError( TErrorType aErrorType, TInt aErrorCode );
       
   169         void NewData( const TDesC8& aData );
       
   170         void AllBufferedDataSent();
       
   171 
       
   172     private:
       
   173 
       
   174         /**
       
   175         * ConnectToServerL
       
   176         * Connects to the service
       
   177         */
       
   178         void ConnectToServerL();
       
   179 
       
   180         /**
       
   181         * DisconnectFromServer()
       
   182         * Disconnects from the service
       
   183         */
       
   184         void DisconnectFromServer();
       
   185 
       
   186         /**
       
   187         * CBtSerialClient()
       
   188         * Constructs this object
       
   189         */
       
   190         CBtSerialClient( MBtSerialClientObserver& aObserver );
       
   191 
       
   192         /**
       
   193         * ConstructL()
       
   194         * Performs second phase construction of this object
       
   195         */
       
   196         void ConstructL();
       
   197 
       
   198     private:    // data
       
   199 
       
   200         /**
       
   201         * TState
       
   202         * The state of the active object, determines behaviour within
       
   203         * the RunL method.
       
   204         * EWaitingToGetDevice waiting for the user to select a device
       
   205         * EGettingDevice searching for a device
       
   206         * EGettingService searching for a service
       
   207         * EGettingConnection connecting to a service on a remote machine
       
   208         * EConnected connected to a service on a remote machine
       
   209         */
       
   210         enum TState
       
   211             {
       
   212             EWaitingToGetDevice,      // phase 1 of Connect
       
   213             EGettingDevice,           // phase 2 of Connect
       
   214             EGettingService,          // phase 3 of Connect
       
   215             EGettingConnection,       // phase 4 of Connect
       
   216             EConnected, // sending and receiving data
       
   217             EDisconnecting
       
   218             };
       
   219 
       
   220         MBtSerialClientObserver& iObserver;
       
   221 
       
   222         /** iState the current state of the client */
       
   223         TState iState;
       
   224 
       
   225         /**
       
   226         * iServiceSearcher searches for service this
       
   227         * client can connect to.
       
   228         * Owned by CBtSerialClient
       
   229         */
       
   230         CMessageServiceSearcher* iServiceSearcher;
       
   231 
       
   232         /**
       
   233         * iCurrentServiceIndex the index number of the service we are
       
   234         * currently connecting/connected to
       
   235         */
       
   236         TInt iCurrentServiceIndex;
       
   237 
       
   238         /** iSocketServer a connection to the socket server */
       
   239         RSocketServ iSocketServer;
       
   240 
       
   241         /** iSocket a socket to connect with */
       
   242         RSocket iSocket;
       
   243 
       
   244         /** iServiceClass the service class UUID to search for */
       
   245         TUUID iServiceClass;
       
   246 
       
   247         TBTDevAddr iBTServerDevice;
       
   248 
       
   249         CSocketsReader* iSocketReader;
       
   250         CSocketsWriter* iSocketWriter;
       
   251     };
       
   252 
       
   253 #endif // __BTSERIALCLIENT_H__
       
   254 
       
   255 // End of File