|
1 /* |
|
2 * Copyright (c) 2006 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: Implements the actual connection management, and receives |
|
15 * notifications of Bluetooth connection events. |
|
16 * |
|
17 */ |
|
18 |
|
19 |
|
20 |
|
21 #include <e32base.h> |
|
22 |
|
23 #include "btengconnhandler.h" |
|
24 #include "btengconnman.h" |
|
25 #include "debug.h" |
|
26 |
|
27 /** ID to identify the outstanding asynchronous request. */ |
|
28 enum TRequestId |
|
29 { |
|
30 EConnectionEventId = 50, |
|
31 EPairDeviceId |
|
32 }; |
|
33 |
|
34 /** Max. number of addresses that can be passed to the server |
|
35 side for getting connected addresses */ |
|
36 const TInt KBTEngMaxAddrArraySize = 10; |
|
37 |
|
38 |
|
39 // ======== MEMBER FUNCTIONS ======== |
|
40 |
|
41 // --------------------------------------------------------------------------- |
|
42 // C++ default constructor |
|
43 // --------------------------------------------------------------------------- |
|
44 // |
|
45 CBTEngConnHandler::CBTEngConnHandler( MBTEngConnObserver* aObserver ) |
|
46 : iObserver( aObserver ) |
|
47 { |
|
48 } |
|
49 |
|
50 // --------------------------------------------------------------------------- |
|
51 // Symbian 2nd-phase constructor |
|
52 // --------------------------------------------------------------------------- |
|
53 // |
|
54 void CBTEngConnHandler::ConstructL() |
|
55 { |
|
56 TRACE_FUNC_ENTRY |
|
57 User::LeaveIfError( iBTEng.Connect() ); |
|
58 if( iObserver ) |
|
59 { |
|
60 NotifyConnectionEvents( iObserver ); |
|
61 } |
|
62 TRACE_FUNC_EXIT |
|
63 } |
|
64 |
|
65 // --------------------------------------------------------------------------- |
|
66 // NewL |
|
67 // --------------------------------------------------------------------------- |
|
68 // |
|
69 CBTEngConnHandler* CBTEngConnHandler::NewL( MBTEngConnObserver* aObserver ) |
|
70 { |
|
71 CBTEngConnHandler* self = new( ELeave ) CBTEngConnHandler( aObserver ); |
|
72 CleanupStack::PushL( self ); |
|
73 self->ConstructL(); |
|
74 CleanupStack::Pop( self ); |
|
75 return self; |
|
76 } |
|
77 |
|
78 // --------------------------------------------------------------------------- |
|
79 // Destructor |
|
80 // --------------------------------------------------------------------------- |
|
81 // |
|
82 CBTEngConnHandler::~CBTEngConnHandler() |
|
83 { |
|
84 TRACE_FUNC_ENTRY |
|
85 CancelNotifyConnectionEvents(); |
|
86 CancelPairing(); |
|
87 iBTEng.Close(); |
|
88 } |
|
89 |
|
90 // --------------------------------------------------------------------------- |
|
91 // Gets the connected device addresses; If a profile is defined, only |
|
92 // connections for that profile are returned. |
|
93 // --------------------------------------------------------------------------- |
|
94 // |
|
95 void CBTEngConnHandler::GetConnectedAddressesL( RBTDevAddrArray& aAddrArray, |
|
96 TBTProfile aConnectedProfile ) |
|
97 { |
|
98 aAddrArray.Reset(); |
|
99 TInt devAddrSize = sizeof( TBTDevAddr ); |
|
100 HBufC8* addrBuf = HBufC8::NewLC( KBTEngMaxAddrArraySize * devAddrSize ); |
|
101 TPtr8 ptr = addrBuf->Des(); |
|
102 TPckgBuf<TInt> profilePkg( aConnectedProfile ); |
|
103 User::LeaveIfError( iBTEng.GetConnectedAddresses( ptr, profilePkg ) ); |
|
104 while( ptr.Length() >= KBTDevAddrSize ) |
|
105 { |
|
106 TBTDevAddr addr( ptr.Mid( ptr.Length() - devAddrSize, KBTDevAddrSize ) ); |
|
107 ptr.SetLength( Max( ptr.Length() - devAddrSize, 0 ) ); |
|
108 aAddrArray.Append( addr ); |
|
109 } |
|
110 CleanupStack::PopAndDestroy( addrBuf ); |
|
111 } |
|
112 |
|
113 // --------------------------------------------------------------------------- |
|
114 // ?implementation_description |
|
115 // --------------------------------------------------------------------------- |
|
116 // |
|
117 TInt CBTEngConnHandler::NotifyConnectionEvents( MBTEngConnObserver* aObserver ) |
|
118 { |
|
119 TRACE_FUNC_ENTRY |
|
120 ASSERT( aObserver ); |
|
121 iObserver = aObserver; |
|
122 TInt err = KErrNone; |
|
123 if( iConnEventActive && iConnEventActive->IsActive() ) |
|
124 { |
|
125 CancelNotifyConnectionEvents(); |
|
126 } |
|
127 if( !iConnEventActive ) |
|
128 { |
|
129 // Use a higher prioritty than normal, because we want |
|
130 // to be notified fast (e.g. to update the UI). |
|
131 TRAP( err, iConnEventActive = CBTEngActive::NewL( *this, EConnectionEventId, |
|
132 CActive::EPriorityUserInput ) ); |
|
133 } |
|
134 if( !err && iConnEventActive->RequestStatus() != KErrServerTerminated ) |
|
135 { |
|
136 err = iBTEng.NotifyConnectionEvents( iEventPkg, iConnEventActive->RequestStatus() ); |
|
137 if( !err ) |
|
138 { |
|
139 iConnEventActive->GoActive(); |
|
140 } |
|
141 } |
|
142 return err; |
|
143 } |
|
144 |
|
145 // --------------------------------------------------------------------------- |
|
146 // ?implementation_description |
|
147 // --------------------------------------------------------------------------- |
|
148 // |
|
149 TInt CBTEngConnHandler::CancelNotifyConnectionEvents() |
|
150 { |
|
151 TRACE_FUNC_ENTRY |
|
152 TInt err = KErrNone; |
|
153 if( iConnEventActive && iConnEventActive->IsActive() ) |
|
154 { |
|
155 err = iBTEng.CancelNotifyConnectionEvents(); |
|
156 iConnEventActive->CancelRequest(); |
|
157 } |
|
158 delete iConnEventActive; |
|
159 iConnEventActive = NULL; |
|
160 return err; |
|
161 } |
|
162 |
|
163 // ----------------------------------------------------------------------------- |
|
164 // Request server side to activate/deactivate a pair observer |
|
165 // ----------------------------------------------------------------------------- |
|
166 // |
|
167 TInt CBTEngConnHandler::SetPairingObserver( const TBTDevAddr& aAddr, |
|
168 TBool aActivate ) |
|
169 { |
|
170 RBTEng bteng; |
|
171 TInt err = bteng.Connect(); |
|
172 if ( !err ) |
|
173 { |
|
174 err = bteng.SetPairingObserver( aAddr, aActivate ); |
|
175 } |
|
176 bteng.Close(); |
|
177 return err; |
|
178 } |
|
179 |
|
180 // --------------------------------------------------------------------------- |
|
181 // Request BTEng to pair the device |
|
182 // --------------------------------------------------------------------------- |
|
183 // |
|
184 TInt CBTEngConnHandler::StartPairing( const TBTDevAddr& aAddr, |
|
185 const TBTDeviceClass& aDeviceClass ) |
|
186 { |
|
187 TRACE_FUNC_ENTRY |
|
188 TInt err( KErrNone ); |
|
189 if( iPairActive && iPairActive->IsActive() ) |
|
190 { |
|
191 err = KErrServerBusy; |
|
192 } |
|
193 |
|
194 if( !iPairActive ) |
|
195 { |
|
196 // Use a higher prioritty than normal, because we want |
|
197 // to be notified fast (e.g. to update the UI). |
|
198 TRAP( err, iPairActive = CBTEngActive::NewL( *this, EPairDeviceId, |
|
199 CActive::EPriorityUserInput ) ); |
|
200 } |
|
201 if ( !err ) |
|
202 { |
|
203 iPairAddr() = aAddr; |
|
204 iPairDevCod = aDeviceClass.DeviceClass(); |
|
205 iBTEng.PairDevice( iPairAddr, iPairDevCod, iPairActive->RequestStatus() ); |
|
206 iPairActive->GoActive(); |
|
207 } |
|
208 TRACE_FUNC_EXIT |
|
209 return err; |
|
210 } |
|
211 |
|
212 // --------------------------------------------------------------------------- |
|
213 // Cancel any outstanding operation, free resources. |
|
214 // --------------------------------------------------------------------------- |
|
215 // |
|
216 void CBTEngConnHandler::CancelPairing() |
|
217 { |
|
218 TRACE_FUNC_ENTRY |
|
219 if( iPairActive && iPairActive->IsActive() ) |
|
220 { |
|
221 iBTEng.CancelPairDevice(); |
|
222 } |
|
223 delete iPairActive; |
|
224 iPairActive = NULL; |
|
225 TRACE_FUNC_EXIT |
|
226 } |
|
227 |
|
228 |
|
229 // --------------------------------------------------------------------------- |
|
230 // From class MBTEngActiveObserver. |
|
231 // Called by the active object when a change in connection status has occured. |
|
232 // --------------------------------------------------------------------------- |
|
233 // |
|
234 void CBTEngConnHandler::RequestCompletedL( CBTEngActive* aActive, TInt aId, |
|
235 TInt aStatus ) |
|
236 { |
|
237 TRACE_FUNC_ARG( ( _L( "ID: %d status: %d" ), aId, aStatus ) ) |
|
238 |
|
239 (void) aActive; |
|
240 switch ( aId ) |
|
241 { |
|
242 case EConnectionEventId: |
|
243 { |
|
244 HandleConnectionEvent( aStatus ); |
|
245 break; |
|
246 } |
|
247 case EPairDeviceId: |
|
248 { |
|
249 if ( iObserver ) |
|
250 { |
|
251 iObserver->PairingComplete( iPairAddr(), aStatus ); |
|
252 } |
|
253 } |
|
254 } |
|
255 |
|
256 TRACE_FUNC_EXIT |
|
257 } |
|
258 |
|
259 |
|
260 // --------------------------------------------------------------------------- |
|
261 // From class MBTEngActiveObserver. |
|
262 // Called when RequestCompletedL/RunL leaves. |
|
263 // --------------------------------------------------------------------------- |
|
264 // |
|
265 void CBTEngConnHandler::HandleError( CBTEngActive* aActive, TInt aId, TInt aError ) |
|
266 { |
|
267 TRACE_FUNC_ARG( ( _L( "error: %d" ), aError ) ) |
|
268 // Should any info be passed to the client?? |
|
269 (void) aActive; |
|
270 (void) aId; |
|
271 (void) aError; |
|
272 } |
|
273 |
|
274 void CBTEngConnHandler::HandleConnectionEvent( TInt aStatus ) |
|
275 { |
|
276 TBTDevAddr addr; |
|
277 RBTDevAddrArray conflictsArray; |
|
278 TBTEngConnectionStatus connStatus = EBTEngNotConnected; |
|
279 // Subscribe to the next event first. |
|
280 (void) NotifyConnectionEvents( iObserver ); |
|
281 addr = iEventPkg().iAddr; |
|
282 connStatus = iEventPkg().iConnEvent; |
|
283 if( iEventPkg().iConflictsBuf.Length() > 0 ) |
|
284 { |
|
285 // Parse conflicts array buffer |
|
286 TInt devAddrSize = sizeof( TBTDevAddr ); |
|
287 while( iEventPkg().iConflictsBuf.Length() >= KBTDevAddrSize ) |
|
288 { |
|
289 TInt len = iEventPkg().iConflictsBuf.Length(); |
|
290 TPtrC8 ptr = iEventPkg().iConflictsBuf.Mid( len - devAddrSize, |
|
291 KBTDevAddrSize ); |
|
292 TBTDevAddr addr( ptr ); |
|
293 conflictsArray.Append( addr ); |
|
294 iEventPkg().iConflictsBuf.SetLength( Max( len - devAddrSize, 0 ) ); |
|
295 } |
|
296 } |
|
297 |
|
298 switch( connStatus ) |
|
299 { |
|
300 // The enumeration allows for more information than can be |
|
301 // signalled to the client. Anyway allow for all cases. |
|
302 case EBTEngNotConnected: |
|
303 case EBTEngDisconnecting: |
|
304 { |
|
305 // Add device address logging here! |
|
306 TRACE_INFO( ( _L( "[BTENG]\t device X disconnected" ) ) ) |
|
307 iObserver->DisconnectComplete( addr, aStatus ); |
|
308 } |
|
309 break; |
|
310 case EBTEngConnecting: |
|
311 case EBTEngConnected: |
|
312 { |
|
313 // Note! Even though the status can say connected, it can |
|
314 // mean that the device is not connected because an error |
|
315 // occurred during connection establishment! |
|
316 |
|
317 // Add device address logging here! |
|
318 TRACE_INFO( ( _L( "[BTENG]\t device X connected" ) ) ) |
|
319 if ( conflictsArray.Count() ) |
|
320 { |
|
321 iObserver->ConnectComplete( addr, aStatus, &conflictsArray ); |
|
322 } |
|
323 else |
|
324 { |
|
325 iObserver->ConnectComplete( addr, aStatus, NULL ); |
|
326 } |
|
327 } |
|
328 break; |
|
329 default: |
|
330 { |
|
331 TRACE_INFO( ( _L( "[BTENG]\t Wrong connection status (%d)!!" ), |
|
332 connStatus ) ) |
|
333 } |
|
334 break; |
|
335 } |
|
336 conflictsArray.Close(); |
|
337 |
|
338 } |