|
1 /* |
|
2 * Copyright (c) 2008 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: |
|
15 * |
|
16 */ |
|
17 |
|
18 |
|
19 #ifndef BLUETOOTHCLIENTCONNECTION_H |
|
20 #define BLUETOOTHCLIENTCONNECTION_H |
|
21 |
|
22 #include <e32base.h> |
|
23 #include <bt_sock.h> |
|
24 #include <btsdp.h> |
|
25 |
|
26 #include "monitor.h" |
|
27 #include "bluetoothfunctionserver.h" |
|
28 #include "bluetoothconsts.h" |
|
29 |
|
30 #include <sys/time.h> |
|
31 |
|
32 //#define LAUNCH_TYPE_PUSH 3 |
|
33 //#define LAUNCH_TYPE_JSR 4 |
|
34 |
|
35 #define DEFAULT_MTU 672 |
|
36 namespace java |
|
37 { |
|
38 namespace bluetooth |
|
39 { |
|
40 |
|
41 const TInt KReceivedBufferSize = 512; |
|
42 |
|
43 /** |
|
44 * BluetoothClientConnection manages all Bluetooth Client operations. |
|
45 * Responsible for send/receive data. |
|
46 * |
|
47 * |
|
48 */ |
|
49 class BluetoothClientConnection: public MBluetoothSocketNotifier |
|
50 { |
|
51 public: |
|
52 OS_IMPORT BluetoothClientConnection(BluetoothFunctionServer* server); |
|
53 |
|
54 /** |
|
55 * Constructor to be used by Server Side connections. |
|
56 * @param[in] aSocket: Socket got as a result of acceptAndOpen call. |
|
57 * @param[in] aServer: Handle to the Function Server in the context of which |
|
58 * aSocket has to be used. Since RObjects are not sharable |
|
59 * across threads, we need to do this. |
|
60 * @param[in] aProtocol: Protocol to which the Socket belongs to. |
|
61 * PROTOCOL_L2CAP or PROTOCOL_RFCOMM |
|
62 * |
|
63 * @see Call void init() after construction of the object. |
|
64 */ |
|
65 OS_IMPORT BluetoothClientConnection(CBluetoothSocket* aSocket, |
|
66 BluetoothFunctionServer* aServer); |
|
67 |
|
68 OS_IMPORT ~BluetoothClientConnection(); |
|
69 |
|
70 /** |
|
71 * To be used after creating a ClientConnection object from the Server |
|
72 * implementation. Since the RSocket passed is already initialized during |
|
73 * connection open, all we do here is to add the object to Active Scheduler. |
|
74 */ |
|
75 OS_IMPORT |
|
76 void BluetoothClientConnection::initialize(int protocol, |
|
77 TInt64 aRemoteAddr, int aReceiveMtu, int aTransmitMtu); |
|
78 |
|
79 /** |
|
80 * Used to Initialize the protocol specific options of the Bluetooth Socket. |
|
81 * This needs to be called before a call to Connect is made. |
|
82 * @param[in] integer value representing the Protocol. |
|
83 * Defined in BluetoothConsts: PROTOCOL_L2CAP and PROTOCOL_RFCOMM |
|
84 * @return error code. |
|
85 */ |
|
86 OS_IMPORT int init(int protocol); |
|
87 |
|
88 /** |
|
89 * Used to Connect to a remote Bluetooth device. |
|
90 * The same call serves for both L2CAP and RFCOMM. Just need to make sure we |
|
91 * call init before making call to connect. With protocol specified. |
|
92 * |
|
93 * @param[in] btAddress: Address of the remote device to connect to. |
|
94 * @param[in] channel: The port at which the remote device is waiting. |
|
95 * @param[in] authenticate: Indicates if authenticate has to be switched on. |
|
96 * @param[in] encrypt: Indicates if encryption has to be enabled on socket. |
|
97 * @param[in] receiveMTU: In case of L2CAP connections, indicates the |
|
98 * Receive MTU value to be set. For RFCOMM Connections, |
|
99 * nothing needs to be passed. |
|
100 * @param[in] transmitMTU: In case of L2CAP connections, indicates the |
|
101 * Transmit MTU value to be set. For RFCOMM Connections, |
|
102 * nothing needs to be passed. |
|
103 * |
|
104 * @return error code. |
|
105 */ |
|
106 OS_IMPORT |
|
107 int connect(long long btAddress, int channel, bool authenticate, |
|
108 bool encrypt, int receiveMTU = 0, int transmitMTU = 0); |
|
109 |
|
110 |
|
111 /** |
|
112 * Receive is used to read data from Bluetooth Socket |
|
113 * Returns immediately when some data is available in the socket. |
|
114 * @param[out] inBuf: Pointer to a byte array containing data received from |
|
115 * the socket. Memory needed will be allocated within |
|
116 * receive based on the amount of data received. |
|
117 * @return Number of bytes received. 0 if EOF and -<error> in case of error. |
|
118 */ |
|
119 OS_IMPORT int receive(char* &inBuf); |
|
120 |
|
121 /** |
|
122 * Send data to Bluetooth Socket |
|
123 * Returns immediately when data is sent through the socket. |
|
124 * @param[in] data: Data that has to be sent through the socket. |
|
125 * @param[in] length: Length of data that needs to be sent over. |
|
126 * @return Number of bytes successfully sent. -<error> in case of error. |
|
127 */ |
|
128 OS_IMPORT int send(const char* data, int length); |
|
129 |
|
130 /** |
|
131 * Closes the current connection with remote device. |
|
132 */ |
|
133 OS_IMPORT void close(); |
|
134 /** |
|
135 * Retrieves the remote address of the device the currently connected to. |
|
136 * @return address of the remote device. |
|
137 */ |
|
138 OS_IMPORT long long getRemoteAddress(); |
|
139 |
|
140 /** |
|
141 * Retreives the Transmit MTU of the Connection. This is valid only in case |
|
142 * of L2CAP Connections. |
|
143 * @return transmit MTU value. |
|
144 */ |
|
145 OS_IMPORT int getTransmitMTU(); |
|
146 |
|
147 /** |
|
148 * Retreives the Receive MTU of the Connection. This is valid only in case |
|
149 * of L2CAP Connections. |
|
150 * @return Receive MTU value. |
|
151 */ |
|
152 OS_IMPORT int getReceiveMTU(); |
|
153 |
|
154 /** |
|
155 * returns available data. |
|
156 */ |
|
157 |
|
158 OS_IMPORT long available(); |
|
159 |
|
160 OS_IMPORT void registerCallback(JNIEnv* ajni, jobject peer); |
|
161 |
|
162 public: |
|
163 // Methods from MBluetoothSocketNotifier |
|
164 //Notification of an accept complete event |
|
165 void HandleAcceptCompleteL(TInt err); |
|
166 |
|
167 // Notification of a baseband event |
|
168 void HandleActivateBasebandEventNotifierCompleteL(TInt aErr, |
|
169 TBTBasebandEventNotification &aEventNotification); |
|
170 |
|
171 //Notification of a connection complete event |
|
172 void HandleConnectCompleteL(TInt err); |
|
173 |
|
174 //Notification of a ioctl complete event |
|
175 void HandleIoctlCompleteL(TInt err); |
|
176 |
|
177 //Notification of a receive complete event |
|
178 void HandleReceiveCompleteL(TInt err); |
|
179 |
|
180 //Notification of a send complete event |
|
181 void HandleSendCompleteL(TInt err); |
|
182 |
|
183 //Notification of a shutdown complete event |
|
184 void HandleShutdownCompleteL(TInt err); |
|
185 |
|
186 private: |
|
187 BluetoothClientConnection(); |
|
188 |
|
189 //Used in case of L2CAP Connection |
|
190 TInt Connect(TInt64 btAddress, TInt channel, TInt authenticate, |
|
191 TInt encrypt, TInt receiveMTU, TInt transmitMTU); |
|
192 |
|
193 TInt ReceiveData(); |
|
194 TInt SendData(const TDesC8& aData); |
|
195 void InitL(TInt protocol); |
|
196 void CloseFs(); |
|
197 |
|
198 void makeReadCompleteCallback(); |
|
199 |
|
200 private: |
|
201 // Function Server handle within which the Socket operations have to be |
|
202 // performed. |
|
203 BluetoothFunctionServer* mServer; |
|
204 |
|
205 // Client socket |
|
206 CBluetoothSocket *mSocket; |
|
207 |
|
208 // Monitor used to Notify on completion of Shutdown. |
|
209 java::util::Monitor* mShutdownNotifyMonitor; |
|
210 |
|
211 // Monitor used to Notify on completion of read. |
|
212 java::util::Monitor* mReadNotifyMonitor; |
|
213 |
|
214 //Holds the read status value. Needed in case of errors |
|
215 int mReadStatus; |
|
216 |
|
217 // Buffer holding received data. |
|
218 TPtr8 mBuffer; |
|
219 |
|
220 // Monitor used to Notify on completion of send. |
|
221 java::util::Monitor* mSendNotifyMonitor; |
|
222 |
|
223 TInt mRequestedRMtu; |
|
224 TInt mRequestedTMtu; |
|
225 //Holds the read status value |
|
226 int mWriteStatus; |
|
227 |
|
228 // Knows which protocol the object relates to |
|
229 int mProtocol; |
|
230 |
|
231 private: |
|
232 int mNegotiatedReceiveMtu; |
|
233 |
|
234 int mNegotiatedTransmitMtu; |
|
235 |
|
236 TInt64 mRemoteBTAddr; |
|
237 |
|
238 // Length of received data |
|
239 TSockXfrLength mLen; |
|
240 |
|
241 // Socket server handle. |
|
242 RSocketServ mSocketServ; |
|
243 |
|
244 TSockXfrLength mSockXfrLength; |
|
245 |
|
246 // Monitor used to notify completion of async operations to BlueCoveS60. |
|
247 java::util::Monitor* mConnectNotifyMonitor; |
|
248 |
|
249 TInt mConnectError; |
|
250 TBool mMakeJavaCallbackOnRead; |
|
251 |
|
252 TBool mReadPending; |
|
253 |
|
254 TBool mBufferInitialized; |
|
255 jmethodID mReadCompleteCallback; |
|
256 }; |
|
257 |
|
258 } //end namespace bluetooth |
|
259 } //end namespace java |
|
260 #endif // BLUETOOTHCLIENTCONNECTION_H |