|
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: This is the implementation of application class |
|
15 * |
|
16 */ |
|
17 |
|
18 |
|
19 #include <e32svr.h> |
|
20 #include <bttypes.h> |
|
21 #include "bthidsession.h" |
|
22 #include "bthidserver.h" |
|
23 #include "bthidclientsrv.h" |
|
24 #include "hidsdpclient.h" |
|
25 #include "bthiddevice.h" |
|
26 #include "bthidserver.pan" |
|
27 #include "debug.h" |
|
28 |
|
29 CBTHidServerSession::CBTHidServerSession(CBTHidServer& aServer) : |
|
30 iServer(aServer) |
|
31 { |
|
32 // Implementation not required |
|
33 } |
|
34 |
|
35 CBTHidServerSession::~CBTHidServerSession() |
|
36 { |
|
37 iServer.DecrementSessions(); |
|
38 delete iHidSdpClient; |
|
39 } |
|
40 |
|
41 void CBTHidServerSession::ServiceL(const RMessage2& aMessage) |
|
42 { |
|
43 TRAPD( err, DispatchMessageL( aMessage ) ); |
|
44 |
|
45 if (aMessage.Function() == EBTHIDGetConnections && !aMessage.IsNull()) |
|
46 { |
|
47 // Handle the error code, what to to? |
|
48 err = err; |
|
49 } |
|
50 } |
|
51 |
|
52 void CBTHidServerSession::DispatchMessageL(const RMessage2& aMessage) |
|
53 { |
|
54 switch (aMessage.Function()) |
|
55 { |
|
56 case EBTHIDServConnectDevice: |
|
57 RequestConnectionL(aMessage); |
|
58 break; |
|
59 |
|
60 case EBTHIDServCancelConnect: |
|
61 if (iConnectionMessage.Handle()) |
|
62 { |
|
63 delete iHidSdpClient; |
|
64 iHidSdpClient = 0; |
|
65 |
|
66 iServer.DeleteNewConnection(iConnectingID); |
|
67 |
|
68 iConnectionMessage.Complete(KErrCancel); |
|
69 } |
|
70 aMessage.Complete(KErrNone); |
|
71 break; |
|
72 |
|
73 case EBTHIDServNotifyConnectionChange: |
|
74 if (iUpdateMessage.Handle()) |
|
75 { |
|
76 // We're already busy |
|
77 CBTHidServer::PanicClient(aMessage, EReqAlreadyPending); |
|
78 } |
|
79 else |
|
80 { |
|
81 iUpdateMessage = aMessage; |
|
82 } |
|
83 break; |
|
84 |
|
85 case EBTHIDServCancelNotify: |
|
86 if (iUpdateMessage.Handle()) |
|
87 { |
|
88 iUpdateMessage.Complete(KErrCancel); |
|
89 } |
|
90 aMessage.Complete(KErrNone); |
|
91 break; |
|
92 |
|
93 case EBTHIDServDisconnectDevice: |
|
94 RequestDisconnectionL(aMessage); |
|
95 break; |
|
96 |
|
97 case EBTHIDServIsConnected: |
|
98 IsConnectedL(aMessage); |
|
99 break; |
|
100 |
|
101 case EBTHidSrvDisconnectAllGracefully: |
|
102 if (iDisconnectMsg.Handle()) |
|
103 { |
|
104 aMessage.Complete(KErrServerBusy); |
|
105 } |
|
106 else |
|
107 { |
|
108 iDisconnectMsg = aMessage; |
|
109 iServer.DisconnectAllDeviceL(); |
|
110 iDisconnectMsg.Complete(KErrNone); |
|
111 } |
|
112 break; |
|
113 |
|
114 case EBTHIDGetConnections: |
|
115 GetConnections(aMessage); |
|
116 break; |
|
117 |
|
118 default: |
|
119 { |
|
120 TRACE_INFO( ( _L( "[BTHID]\t DispatchMessageL: bad request (%d)" ), |
|
121 aMessage.Function() ) ) |
|
122 CBTHidServer::PanicClient(aMessage, EBadRequest); |
|
123 } |
|
124 break; |
|
125 } |
|
126 } |
|
127 |
|
128 void CBTHidServerSession::HidSdpSearchComplete(TInt aResult) |
|
129 { |
|
130 TRACE_FUNC |
|
131 (_L("[BTHID]\tCBTHidServerSession::HidSdpSearchComplete")); |
|
132 // This is a callback for the Hid SDP client so we can't delete it here |
|
133 // Get it to destroy itself when its convenient. |
|
134 iHidSdpClient->Kill(); |
|
135 // Deleted outside destructor. |
|
136 iHidSdpClient = 0; |
|
137 |
|
138 TInt err = aResult; |
|
139 |
|
140 // If the SDP search was a success |
|
141 if (err == KErrNone) |
|
142 { |
|
143 // Try to connect to the device as a HID |
|
144 TRAP( err, iServer.DoFirstConnectionL(iConnectingID); ) |
|
145 |
|
146 if (err == KErrAlreadyExists) |
|
147 { |
|
148 //there is already an active connection with the same CoD |
|
149 TBuf8<KBTDevAddrSize> buf; |
|
150 TBTDevAddr dummyAddr = iServer.ConflictAddress(); |
|
151 buf.Append(dummyAddr.Des()); |
|
152 if (buf.Length()) |
|
153 { |
|
154 iConnectionMessage.Write(1, buf); |
|
155 } |
|
156 |
|
157 //delete the connection object in the server |
|
158 iServer.DeleteNewConnection(iConnectingID); |
|
159 iConnectionMessage.Complete(err); |
|
160 return; |
|
161 } |
|
162 } |
|
163 |
|
164 // Any error causes clean up, |
|
165 if (err != KErrNone) |
|
166 { |
|
167 //delete the connection object in the server |
|
168 iServer.DeleteNewConnection(iConnectingID); |
|
169 |
|
170 //Inform the client of the result |
|
171 iConnectionMessage.Complete(aResult); |
|
172 } |
|
173 } |
|
174 |
|
175 void CBTHidServerSession::RequestConnectionL(const RMessage2& aMessage) |
|
176 { |
|
177 TRACE_FUNC |
|
178 (_L("[BTHID]\tCBTHidServerSession::RequestConnectionL")); |
|
179 |
|
180 if (iConnectionMessage.Handle()) |
|
181 { |
|
182 // We're already busy |
|
183 CBTHidServer::PanicClient(aMessage, EReqAlreadyPending); |
|
184 } |
|
185 else |
|
186 { |
|
187 TRACE_INFO(_L("[BTHID]\tCBTHidServerSession::RequestConnectionL continue to connect")); |
|
188 // Keep a copy of message - for use later |
|
189 iConnectionMessage = aMessage; |
|
190 |
|
191 // Read the connection parameters |
|
192 TBTDevAddr addr; |
|
193 TPckg<TBTDevAddr> addrPack(addr); |
|
194 |
|
195 iConnectionMessage.ReadL(0, addrPack); |
|
196 // Ask the server to create a new connection object |
|
197 |
|
198 |
|
199 iConnectingID = iServer.NewConnectionL(); |
|
200 TRAPD( res, |
|
201 // Retrieve the hid device object for this new connection |
|
202 CBTHidDevice &devDetails = |
|
203 iServer.ConnectionDetailsL(iConnectingID); |
|
204 |
|
205 // Fill in the information we got from the client |
|
206 devDetails.iAddress = addr; |
|
207 devDetails.iUseSecurity = ETrue; |
|
208 |
|
209 // Create a new HID Sdp Client |
|
210 // Its only used here so it doesn't matter if we leave. |
|
211 delete iHidSdpClient; |
|
212 iHidSdpClient = 0; |
|
213 //Create a new hid sdp client using the hid device object. |
|
214 iHidSdpClient = CHidSdpClient::NewL(devDetails, *this); |
|
215 |
|
216 // Start the hid sdp client |
|
217 iHidSdpClient->StartL(); |
|
218 ) |
|
219 |
|
220 // If there was any error, inform the client and finish |
|
221 if (res != KErrNone) |
|
222 { |
|
223 // Get the server to delete the new connection object |
|
224 iServer.DeleteNewConnection(iConnectingID); |
|
225 aMessage.Complete(res); |
|
226 } |
|
227 } |
|
228 |
|
229 } |
|
230 |
|
231 void CBTHidServerSession::RequestDisconnectionL(const RMessage2& aMessage) |
|
232 { |
|
233 TBTDevAddr addr; |
|
234 TPckg<TBTDevAddr> addrPack(addr); |
|
235 |
|
236 // Read the address from the client thread. |
|
237 aMessage.ReadL(0, addrPack); |
|
238 |
|
239 // Since the user can select this option using the BT Keyboard |
|
240 // and the keydown event triggers it, depending on how long they |
|
241 // hold down the key, the connection could be restored to send |
|
242 // the key up event. |
|
243 // Add a small delay, sensible enough for a press of the enter |
|
244 // key before disconnection. |
|
245 // Currently 3/4 of a second. |
|
246 User::After(750000); |
|
247 |
|
248 //Keep the connection alive, initial reconnect from HID device available... |
|
249 iServer.CloseBluetoothConnection(addr); |
|
250 |
|
251 aMessage.Complete(KErrNone); |
|
252 } |
|
253 |
|
254 void CBTHidServerSession::InformStatusChange( |
|
255 const THIDStateUpdateBuf& aUpdateParams) |
|
256 { |
|
257 TRACE_FUNC |
|
258 (_L("[BTHID]\tCBTHidServerSession::InformStatusChange")); |
|
259 |
|
260 THIDStateUpdate update = aUpdateParams(); |
|
261 TBTHidConnState state = update.iState; |
|
262 |
|
263 TBuf8<KBTDevAddrSize> buf; |
|
264 if (state == EBTDeviceAnotherExist) |
|
265 { |
|
266 TBTDevAddr iDumyAddr = iServer.ConflictAddress(); |
|
267 buf.Append(iDumyAddr.Des()); |
|
268 } |
|
269 |
|
270 TRACE_INFO( (_L("[BTHID]\tCBTHidServerSession::InformStatusChange, state=%d"), state) ); |
|
271 |
|
272 if (iUpdateMessage.Handle()) |
|
273 { |
|
274 TRACE_FUNC |
|
275 (_L("[BTHID]\tCBTHidServerSession::InformStatusChange, OK - send message")); |
|
276 // Trap any error |
|
277 TRAPD( res, |
|
278 iUpdateMessage.WriteL(0,aUpdateParams); |
|
279 if (buf.Length()) |
|
280 { |
|
281 iUpdateMessage.WriteL(1,buf); |
|
282 } |
|
283 ) |
|
284 |
|
285 if (state == EBTDeviceAnotherExist) |
|
286 { |
|
287 iUpdateMessage.Complete(KErrAlreadyExists); |
|
288 } |
|
289 else |
|
290 { |
|
291 // Complete the message, with the error code from the trap. |
|
292 iUpdateMessage.Complete(res); |
|
293 } |
|
294 } |
|
295 } |
|
296 |
|
297 void CBTHidServerSession::InformConnectionResult(TInt aConnID, TInt aResult) |
|
298 { |
|
299 TRACE_INFO( (_L("[BTHID new]\tCBTHidServerSession::InformConnectionResult, Ids: %d==%d aResult = %d"), |
|
300 iConnectingID, aConnID, aResult) ); |
|
301 if ((iConnectionMessage.Handle()) && (iConnectingID == aConnID)) |
|
302 { |
|
303 iConnectionMessage.Complete(aResult); |
|
304 } |
|
305 } |
|
306 |
|
307 void CBTHidServerSession::IsConnectedL(const RMessage2& aMessage) |
|
308 { |
|
309 TBTDevAddr addr; |
|
310 TPckg<TBTDevAddr> addrPck(addr); |
|
311 aMessage.ReadL(0, addrPck); |
|
312 TBTEngConnectionStatus ret = iServer.ConnectStatus(addr); |
|
313 |
|
314 aMessage.Complete(ret); |
|
315 } |
|
316 |
|
317 void CBTHidServerSession::GetConnections(const RMessage2& aMessage) |
|
318 { |
|
319 TRACE_FUNC |
|
320 TBuf8<KBTDevAddrSize * 2> buf; |
|
321 buf.Zero(); |
|
322 iServer.GetConnectionAddress(buf); |
|
323 if (buf.Length()) |
|
324 { |
|
325 aMessage.Write(0, buf); |
|
326 } |
|
327 |
|
328 aMessage.Complete(KErrNone); |
|
329 } |
|
330 |
|
331 CBTHidServerSession* CBTHidServerSession::NewL(CBTHidServer& aServer) |
|
332 { |
|
333 CBTHidServerSession* self = CBTHidServerSession::NewLC(aServer); |
|
334 CleanupStack::Pop(self); |
|
335 return self; |
|
336 } |
|
337 |
|
338 CBTHidServerSession* CBTHidServerSession::NewLC(CBTHidServer& aServer) |
|
339 { |
|
340 CBTHidServerSession* self = new (ELeave) CBTHidServerSession(aServer); |
|
341 |
|
342 CleanupStack::PushL(self); |
|
343 self->ConstructL(); |
|
344 return self; |
|
345 } |
|
346 |
|
347 void CBTHidServerSession::ConstructL() |
|
348 { |
|
349 iServer.IncrementSessions(); |
|
350 } |