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 <bluetooth/btpowercontrol.h> |
|
26 #include <bt_sock.h> |
|
27 #include <btextnotifiers.h> |
|
28 #include <btsdp.h> |
|
29 |
|
30 #include "SocketObserver.h" |
|
31 |
|
32 // FORWARD DECLARATIONS |
|
33 class CMessageServiceSearcher; |
|
34 class CSocketsReader; |
|
35 class CSocketsWriter; |
|
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, |
|
51 public MSocketObserver |
|
52 { |
|
53 public: // Constructors and destructor |
|
54 |
|
55 /** |
|
56 * NewL() |
|
57 * Construct a CBtSerialClient |
|
58 * @param aObserver the observer for this BT serial client |
|
59 * @return a pointer to the created instance of CBtSerialClient |
|
60 */ |
|
61 IMPORT_C static CBtSerialClient* NewL( MBtSerialClientObserver& aObserver ); |
|
62 |
|
63 /** |
|
64 * ~CBtSerialClient() |
|
65 * Destroy the object and release all memory objects. |
|
66 * Close any open sockets. |
|
67 */ |
|
68 IMPORT_C virtual ~CBtSerialClient(); |
|
69 |
|
70 public: // New functions |
|
71 |
|
72 /** |
|
73 * ConnectL() |
|
74 * Connect to remote device. Query user for a device. |
|
75 */ |
|
76 IMPORT_C void ConnectL(); |
|
77 |
|
78 /** |
|
79 * ConnectL() |
|
80 * Connect to remote device. |
|
81 */ |
|
82 IMPORT_C void ConnectL(const TBTDevAddr aBTServerDevice, const TInt aPort ); |
|
83 |
|
84 /** |
|
85 * ConnectL() |
|
86 * Connect to remote device by device name. |
|
87 */ |
|
88 IMPORT_C void ConnectL( const TDesC& aBTServerDeviceName, const TInt aPort ); |
|
89 |
|
90 /** |
|
91 * DisconnectL() |
|
92 * Disconnect from remote machine |
|
93 */ |
|
94 IMPORT_C void Disconnect(); |
|
95 |
|
96 /** |
|
97 * IsConnecting() |
|
98 * @return ETrue if the client is establishing a |
|
99 * connection to the server. |
|
100 */ |
|
101 IMPORT_C TBool Connecting(); |
|
102 |
|
103 /** |
|
104 * Connected() |
|
105 * @return ETrue if the client is fully connected to the server. |
|
106 */ |
|
107 IMPORT_C TBool Connected(); |
|
108 |
|
109 /** |
|
110 * ServerAddressL() |
|
111 * @return address of connected server. Leaves with KErrNotReady, if |
|
112 * not connected. |
|
113 */ |
|
114 TBTDevAddr ServerAddressL(); |
|
115 |
|
116 /** |
|
117 * Add data to outgoing buffer and start sending it to client. |
|
118 * |
|
119 * Leaves with KErrOverflow, if the outgoing buffer cannot |
|
120 * be added all of the aData. |
|
121 * Leaves with KErrNotReady, if client is not connected. |
|
122 * When all data in internal buffer has been sent, observer |
|
123 * is notified (AllBufferedDataSent) |
|
124 */ |
|
125 IMPORT_C void SendL(const TDesC8& aData); |
|
126 |
|
127 /** |
|
128 * Issue read operation. Will complete asyncronously. |
|
129 * |
|
130 * Leaves with KErrNotReady, if client is not connected. |
|
131 * Notifies observer, when some data has been read. Caller |
|
132 * is responsible for calling this method again to receive |
|
133 * more data. |
|
134 */ |
|
135 IMPORT_C void ReadAsyncL(); |
|
136 |
|
137 /** |
|
138 * Query free size of outgoing buffer. |
|
139 */ |
|
140 IMPORT_C TInt FreeSpaceInSendBuffer(); |
|
141 |
|
142 /** |
|
143 * Query max size of outgoing buffer. |
|
144 */ |
|
145 IMPORT_C TInt SendBufferMaxSize(); |
|
146 |
|
147 /** |
|
148 * Query the port of the service we are connected with |
|
149 * @return Port number or KErrDisconnected if not connected to a service |
|
150 */ |
|
151 IMPORT_C TInt ServicePort(); |
|
152 |
|
153 protected: // from CActive |
|
154 |
|
155 /** |
|
156 * DoCancel() |
|
157 * Cancel any outstanding requests |
|
158 */ |
|
159 void DoCancel(); |
|
160 |
|
161 /** |
|
162 * RunL() |
|
163 * Respond to an event |
|
164 */ |
|
165 void RunL(); |
|
166 |
|
167 protected: // from MSocketObserver |
|
168 |
|
169 void ReportError( TErrorType aErrorType, TInt aErrorCode ); |
|
170 void NewData( const TDesC8& aData ); |
|
171 void AllBufferedDataSent(); |
|
172 |
|
173 private: |
|
174 |
|
175 /** |
|
176 * ConnectToServerL |
|
177 * Connects to the service |
|
178 */ |
|
179 void ConnectToServerL(); |
|
180 |
|
181 /** |
|
182 * DisconnectFromServer() |
|
183 * Disconnects from the service |
|
184 */ |
|
185 void DisconnectFromServer(); |
|
186 |
|
187 /** |
|
188 * CBtSerialClient() |
|
189 * Constructs this object |
|
190 */ |
|
191 CBtSerialClient( MBtSerialClientObserver& aObserver ); |
|
192 |
|
193 /** |
|
194 * ConstructL() |
|
195 * Performs second phase construction of this object |
|
196 */ |
|
197 void ConstructL(); |
|
198 |
|
199 private: // data |
|
200 |
|
201 /** |
|
202 * TState |
|
203 * The state of the active object, determines behaviour within |
|
204 * the RunL method. |
|
205 * EWaitingToGetDevice waiting for the user to select a device |
|
206 * EGettingDevice searching for a device |
|
207 * EGettingService searching for a service |
|
208 * EGettingConnection connecting to a service on a remote machine |
|
209 * EConnected connected to a service on a remote machine |
|
210 */ |
|
211 enum TState |
|
212 { |
|
213 EWaitingToGetDevice, // phase 1 of Connect |
|
214 EGettingDevice, // phase 2 of Connect |
|
215 EGettingService, // phase 3 of Connect |
|
216 EGettingConnection, // phase 4 of Connect |
|
217 EConnected, // sending and receiving data |
|
218 EDisconnecting |
|
219 }; |
|
220 |
|
221 MBtSerialClientObserver& iObserver; |
|
222 |
|
223 /** iState the current state of the client */ |
|
224 TState iState; |
|
225 |
|
226 /** |
|
227 * iServiceSearcher searches for service this |
|
228 * client can connect to. |
|
229 * Owned by CBtSerialClient |
|
230 */ |
|
231 CMessageServiceSearcher* iServiceSearcher; |
|
232 |
|
233 /** |
|
234 * iCurrentServiceIndex the index number of the service we are |
|
235 * currently connecting/connected to |
|
236 */ |
|
237 TInt iCurrentServiceIndex; |
|
238 |
|
239 /** iSocketServer a connection to the socket server */ |
|
240 RSocketServ iSocketServer; |
|
241 |
|
242 /** iSocket a socket to connect with */ |
|
243 RSocket iSocket; |
|
244 |
|
245 /** iServiceClass the service class UUID to search for */ |
|
246 TUUID iServiceClass; |
|
247 |
|
248 TBTDevAddr iBTServerDevice; |
|
249 |
|
250 CSocketsReader* iSocketReader; |
|
251 CSocketsWriter* iSocketWriter; |
|
252 |
|
253 RBTPowerControl iPowerControl; |
|
254 }; |
|
255 |
|
256 #endif // __BTSERIALCLIENT_H__ |
|
257 |
|
258 // End of File |
|