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: CSconBtEngine implementation |
|
15 * |
|
16 */ |
|
17 |
|
18 |
|
19 |
|
20 #include <centralrepository.h> |
|
21 #include <btengdomaincrkeys.h> |
|
22 |
|
23 #include <e32property.h> |
|
24 #include <bttypes.h> |
|
25 #include <bt_subscribe.h> |
|
26 |
|
27 #include "sconbtengine.h" |
|
28 #include "debug.h" |
|
29 |
|
30 CSconBtEngine* CSconBtEngine::NewL() |
|
31 { |
|
32 TRACE_FUNC; |
|
33 CSconBtEngine* self = new (ELeave) CSconBtEngine(); |
|
34 CleanupStack::PushL( self ); |
|
35 self->ConstructL(); |
|
36 CleanupStack::Pop( self ); |
|
37 return self; |
|
38 } |
|
39 |
|
40 CSconBtEngine::~CSconBtEngine() |
|
41 { |
|
42 TRACE_FUNC; |
|
43 if ( iBtEng ) |
|
44 { |
|
45 iBtEng->Cancel(); |
|
46 delete iBtEng; |
|
47 } |
|
48 |
|
49 delete iBtSettings; |
|
50 } |
|
51 |
|
52 CSconBtEngine::CSconBtEngine() |
|
53 { |
|
54 } |
|
55 |
|
56 void CSconBtEngine::ConstructL() |
|
57 { |
|
58 iBtEng = CBTEngDevMan::NewL( this ); |
|
59 iBtSettings = CBTEngSettings::NewL( this ); |
|
60 iState = EIdle; |
|
61 } |
|
62 |
|
63 void CSconBtEngine::ReadBTInfoL( RBufWriteStream& aStream ) |
|
64 { |
|
65 TRACE_FUNC_ENTRY; |
|
66 if ( iState != EIdle ) |
|
67 { |
|
68 User::Leave( KErrInUse ); |
|
69 } |
|
70 iStream = &aStream; |
|
71 iOperationError = KErrNone; |
|
72 |
|
73 // Write local name (8 bit name length + unicode name) |
|
74 TBuf<100> localName; |
|
75 TInt err = iBtSettings->GetLocalName( localName ); |
|
76 User::LeaveIfError( err ); |
|
77 LOGGER_WRITE_1("localName: %S", &localName); |
|
78 iStream->WriteUint8L( localName.Length() ); |
|
79 iStream->WriteL( localName, localName.Length() ); |
|
80 |
|
81 if ( iBtAddress.Length() == 0 ) |
|
82 { |
|
83 LOGGER_WRITE("Try to read bt address"); |
|
84 |
|
85 CRepository* cenRep = NULL; |
|
86 // Use TRAP to catch a leave if the CenRep key does not exist. |
|
87 TRAP( err, cenRep = CRepository::NewL( KCRUidBluetoothLocalDeviceAddress ) ); |
|
88 CleanupStack::PushL( cenRep ); |
|
89 if( !err ) |
|
90 { |
|
91 err = cenRep->Get( KBTLocalDeviceAddress, iBtAddress ); |
|
92 LOGGER_WRITE_1("cenRep->Get err: %d", err); |
|
93 } |
|
94 CleanupStack::PopAndDestroy( cenRep ); |
|
95 |
|
96 if ( err ) |
|
97 { |
|
98 iBtAddress.Zero(); |
|
99 } |
|
100 } |
|
101 |
|
102 // Write bt address (8 bit addr length + unicode address) |
|
103 if ( iBtAddress.Length() > 0 ) |
|
104 { |
|
105 LOGGER_WRITE_1("btAddr: %S", &iBtAddress); |
|
106 iStream->WriteUint8L( iBtAddress.Length() ); |
|
107 iStream->WriteL( iBtAddress, iBtAddress.Length() ); |
|
108 } |
|
109 else |
|
110 { |
|
111 iStream->WriteUint8L( 0 ); |
|
112 } |
|
113 |
|
114 // Write BT Power State (8-bit, 0=Off, 1=On) |
|
115 TBTPowerStateValue state; |
|
116 err = iBtSettings->GetPowerState( state ); |
|
117 User::LeaveIfError( err ); |
|
118 LOGGER_WRITE_1("PowerState: %d", state); |
|
119 iStream->WriteUint8L( state ); |
|
120 |
|
121 // Get device info |
|
122 TBTRegistrySearch crit; |
|
123 crit.FindAll(); |
|
124 if ( !iBtDevArray ) |
|
125 { |
|
126 LOGGER_WRITE("create arr"); |
|
127 iBtDevArray = new (ELeave) CBTDeviceArray(5); |
|
128 LOGGER_WRITE("create arr -ok"); |
|
129 } |
|
130 else |
|
131 { |
|
132 iBtDevArray->ResetAndDestroy(); |
|
133 } |
|
134 err = iBtEng->GetDevices( crit, iBtDevArray ); |
|
135 LOGGER_WRITE_1("GetDevices err: %d", err); |
|
136 User::LeaveIfError( err ); |
|
137 iState = ERequestBtInfo; |
|
138 |
|
139 // Wait until completed |
|
140 iWait.Start(); |
|
141 iState = EIdle; |
|
142 if ( iOperationError ) |
|
143 { |
|
144 LOGGER_WRITE_1("Operation failed, leave %d", iOperationError); |
|
145 User::Leave( iOperationError ); |
|
146 } |
|
147 TRACE_FUNC_EXIT; |
|
148 } |
|
149 |
|
150 void CSconBtEngine::SetBtAuthorizedL( const TDesC& aBtDevAddress, TBool aAuthorized ) |
|
151 { |
|
152 TRACE_FUNC_ENTRY; |
|
153 if ( iState != EIdle ) |
|
154 { |
|
155 User::Leave( KErrInUse ); |
|
156 } |
|
157 |
|
158 TBTRegistrySearch crit; |
|
159 TBTDevAddr addr; |
|
160 addr.SetReadable( aBtDevAddress ); |
|
161 crit.FindAddress( addr ); |
|
162 |
|
163 if ( !iBtDevArray ) |
|
164 { |
|
165 LOGGER_WRITE("create arr"); |
|
166 iBtDevArray = new (ELeave) CBTDeviceArray(5); |
|
167 LOGGER_WRITE("create arr -ok"); |
|
168 } |
|
169 else |
|
170 { |
|
171 iBtDevArray->ResetAndDestroy(); |
|
172 } |
|
173 TInt err = iBtEng->GetDevices( crit, iBtDevArray ); |
|
174 LOGGER_WRITE_1("GetDevices err: %d", err); |
|
175 User::LeaveIfError( err ); |
|
176 iState = EChangeAuthorized; |
|
177 iOperationError = KErrNone; |
|
178 iAuthorizedValue = aAuthorized; |
|
179 |
|
180 // Wait until completed |
|
181 iWait.Start(); |
|
182 iState = EIdle; |
|
183 if ( iOperationError ) |
|
184 { |
|
185 LOGGER_WRITE_1("Operation failed, leave %d", iOperationError); |
|
186 User::Leave( iOperationError ); |
|
187 |
|
188 } |
|
189 |
|
190 TRACE_FUNC_EXIT; |
|
191 } |
|
192 |
|
193 TInt CSconBtEngine::SetBtPowerState( TBool aPower ) |
|
194 { |
|
195 TRACE_FUNC_ENTRY; |
|
196 if ( iState != EIdle ) |
|
197 { |
|
198 return KErrInUse; |
|
199 } |
|
200 |
|
201 TBTPowerStateValue state; |
|
202 iOperationError = iBtSettings->GetPowerState( state ); |
|
203 if ( iOperationError ) |
|
204 { |
|
205 return iOperationError; |
|
206 } |
|
207 else if ( ( aPower && state == EBTPowerOn ) |
|
208 || ( !aPower && state == EBTPowerOff ) ) |
|
209 { |
|
210 // No need to change state |
|
211 return KErrNone; |
|
212 } |
|
213 |
|
214 state = EBTPowerOff; |
|
215 if ( aPower ) |
|
216 { |
|
217 state = EBTPowerOn; |
|
218 } |
|
219 iOperationError = iBtSettings->SetPowerState( state ); |
|
220 if ( iOperationError ) |
|
221 { |
|
222 return iOperationError; |
|
223 } |
|
224 iState = EChangeBtState; |
|
225 |
|
226 // Wait until completed |
|
227 iWait.Start(); |
|
228 |
|
229 iState = EIdle; |
|
230 TRACE_FUNC_RET( iOperationError ); |
|
231 return iOperationError; |
|
232 } |
|
233 |
|
234 TInt CSconBtEngine::SetBtName( const TDesC& aBtName ) |
|
235 { |
|
236 LOGGER_WRITE_1("SetBtName aBtName: %S", &aBtName); |
|
237 TInt err = iBtSettings->SetLocalName( static_cast<const TDes&>( aBtName ) ); |
|
238 TRACE_FUNC_RET( err ); |
|
239 return err; |
|
240 } |
|
241 |
|
242 // From MBTEngDevManObserver |
|
243 void CSconBtEngine::HandleDevManComplete( TInt aErr ) |
|
244 { |
|
245 TRACE_FUNC; |
|
246 LOGGER_WRITE_1("aErr: %d", aErr ); |
|
247 if ( iState == EChangeAuthorized ) |
|
248 { |
|
249 if ( iWait.IsStarted() ) |
|
250 { |
|
251 iOperationError = aErr; |
|
252 iWait.AsyncStop(); |
|
253 } |
|
254 } |
|
255 |
|
256 } |
|
257 |
|
258 void CSconBtEngine::HandleGetDevicesComplete( TInt aErr, |
|
259 CBTDeviceArray* aDeviceArray ) |
|
260 { |
|
261 TRACE_FUNC_ENTRY; |
|
262 LOGGER_WRITE_1("aErr: %d", aErr); |
|
263 //iBtReqOngoing = EFalse; |
|
264 if ( iState == ERequestBtInfo ) |
|
265 { |
|
266 // Continue to write devices info |
|
267 TRAP( iOperationError, PrintDevicesL() ); |
|
268 LOGGER_WRITE_1("PrintDevicesL err: %d", iOperationError); |
|
269 |
|
270 if ( iWait.IsStarted() ) |
|
271 { |
|
272 iWait.AsyncStop(); |
|
273 } |
|
274 } |
|
275 else if ( iState == EChangeAuthorized ) |
|
276 { |
|
277 iOperationError = aErr; |
|
278 if ( !aErr && aDeviceArray->Count() == 1 ) |
|
279 { |
|
280 CBTDevice* btRef = aDeviceArray->At(0); |
|
281 TBTDeviceSecurity devSec = btRef->GlobalSecurity(); |
|
282 if ( btRef->IsPaired() && !devSec.Banned() ) |
|
283 { |
|
284 LOGGER_WRITE8_1("btAdrr: %S", &btRef->BDAddr().Des() ); |
|
285 CBTDevice* btDevice = CBTDevice::NewLC( btRef->BDAddr() ); |
|
286 |
|
287 btDevice->SetPaired( ETrue ); |
|
288 |
|
289 btDevice->SetDeviceNameL( btRef->DeviceName() ); |
|
290 btDevice->SetFriendlyNameL( btRef->FriendlyName() ); |
|
291 LOGGER_WRITE8_1("name: %S", &btRef->DeviceName()); |
|
292 LOGGER_WRITE8_1("FriendlyName: %S", &btRef->FriendlyName()); |
|
293 |
|
294 LOGGER_WRITE_1("iAuthorizedValue: %d", iAuthorizedValue); |
|
295 // Set as authorized and store the device |
|
296 TBTDeviceSecurity security; |
|
297 security.SetNoAuthenticate( EFalse ); |
|
298 security.SetNoAuthorise( iAuthorizedValue ); |
|
299 security.SetBanned( EFalse ); |
|
300 |
|
301 btDevice->SetGlobalSecurity (security ); |
|
302 |
|
303 //iOperationError = iBtEng->ModifyDevice(*aDeviceArray->At(0)); |
|
304 iOperationError = iBtEng->ModifyDevice( *btDevice ); |
|
305 LOGGER_WRITE_1( "ModifyDevice err: %d", iOperationError ); |
|
306 CleanupStack::PopAndDestroy( btDevice ); |
|
307 } |
|
308 else |
|
309 { |
|
310 LOGGER_WRITE("Device was not paired or it's banned") |
|
311 iOperationError = KErrAccessDenied; |
|
312 } |
|
313 } |
|
314 |
|
315 // If error occured stop waiting, otherwise continue waiting until |
|
316 // HandleDevManComplete has been called |
|
317 if ( iOperationError && iWait.IsStarted() ) |
|
318 { |
|
319 iWait.AsyncStop(); |
|
320 } |
|
321 } |
|
322 TRACE_FUNC_EXIT; |
|
323 } |
|
324 |
|
325 void CSconBtEngine::PrintDevicesL() |
|
326 { |
|
327 TRACE_FUNC_ENTRY; |
|
328 // calculate devices count |
|
329 TInt devCount(0); |
|
330 |
|
331 for ( TInt i=0 ; i<iBtDevArray->Count(); i++ ) |
|
332 { |
|
333 CBTDevice* device = iBtDevArray->At(i); |
|
334 // show only paired and banned devices |
|
335 if ( device->IsPaired() || device->GlobalSecurity().Banned() ) |
|
336 { |
|
337 devCount++; |
|
338 } |
|
339 } |
|
340 |
|
341 // Print info |
|
342 iStream->WriteUint8L( devCount ); |
|
343 |
|
344 for ( TInt i=0 ; i<iBtDevArray->Count(); i++ ) |
|
345 { |
|
346 CBTDevice* device = iBtDevArray->At(i); |
|
347 |
|
348 const TBTDeviceSecurity btSec = device->GlobalSecurity(); |
|
349 |
|
350 // show only paired and banned devices |
|
351 if ( device->IsPaired() || btSec.Banned() ) |
|
352 { |
|
353 LOGGER_WRITE_1("IsValidDeviceName: %d", device->IsValidDeviceName()); |
|
354 const TDesC8& deviceName = device->DeviceName(); |
|
355 LOGGER_WRITE8_1("DeviceName: %S", &deviceName ); |
|
356 iStream->WriteUint8L( deviceName.Length() ); |
|
357 iStream->WriteL( deviceName, deviceName.Length() ); |
|
358 |
|
359 LOGGER_WRITE_1("IsValidFriendlyName: %d", device->IsValidFriendlyName()); |
|
360 LOGGER_WRITE_1("FriendlyName: %S", &device->FriendlyName() ); |
|
361 |
|
362 const TBTDevAddr& addr = device->BDAddr(); |
|
363 |
|
364 TBuf<100> readable; |
|
365 addr.GetReadable( readable ); |
|
366 LOGGER_WRITE_1("readable addr: %S", &readable); |
|
367 iStream->WriteUint8L( readable.Length() ); |
|
368 iStream->WriteL( readable, readable.Length() ); |
|
369 |
|
370 LOGGER_WRITE_1("paired: %d", device->IsPaired() ); |
|
371 LOGGER_WRITE_1("IsValidPaired: %d", device->IsValidPaired() ); |
|
372 TUint8 paired(0); |
|
373 if ( device->IsPaired() && device->IsValidPaired() ) |
|
374 { |
|
375 paired = 1; |
|
376 } |
|
377 iStream->WriteUint8L( paired ); |
|
378 |
|
379 TUint8 noAuthorise(0); |
|
380 if ( btSec.NoAuthorise() ) |
|
381 { |
|
382 noAuthorise = 1; |
|
383 } |
|
384 iStream->WriteUint8L( noAuthorise ); |
|
385 |
|
386 TUint8 banned(0); |
|
387 if ( btSec.Banned() ) |
|
388 { |
|
389 banned = 1; |
|
390 } |
|
391 iStream->WriteUint8L( banned ); |
|
392 |
|
393 LOGGER_WRITE(""); |
|
394 } |
|
395 } |
|
396 iStream->CommitL(); |
|
397 iStream = NULL; //delete reference only |
|
398 TRACE_FUNC_EXIT; |
|
399 } |
|
400 |
|
401 // From MBTEngSettingsObserver |
|
402 void CSconBtEngine::PowerStateChanged( TBTPowerStateValue /*aState*/ ) |
|
403 { |
|
404 TRACE_FUNC; |
|
405 if ( iState == EChangeBtState && iWait.IsStarted() ) |
|
406 { |
|
407 iWait.AsyncStop(); |
|
408 } |
|
409 |
|
410 } |
|
411 |
|
412 void CSconBtEngine::VisibilityModeChanged( TBTVisibilityMode /*aState*/ ) |
|
413 { |
|
414 TRACE_FUNC; |
|
415 } |
|