|
1 /* |
|
2 * Copyright (c) 2004-2007 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 // EXTERNAL INCLUDES |
|
20 #include <s32file.h> |
|
21 |
|
22 #include "cbtdevicecontainer.h" |
|
23 #include "crsbtdevice.h" |
|
24 #include "rsutils.h" |
|
25 #include "mprotprintingdevice.h" |
|
26 #include "clog.h" |
|
27 |
|
28 // CONSTANTS |
|
29 namespace |
|
30 { |
|
31 /// File name where the BT devices will be stored, for future sessions. |
|
32 _LIT(KUsedDevicesFile,"btdevices.dat"); |
|
33 /// Maximum number of Bluetooth devices to be stored. |
|
34 const TInt KUsedDevicesMax = 150; |
|
35 /// Version of the file where the BT devices will be stored. |
|
36 const TInt KUsedDevicesVersion = 5; |
|
37 } |
|
38 |
|
39 |
|
40 |
|
41 |
|
42 ////////////////////////////////////////////////////////////////////// |
|
43 // Construction/Destruction |
|
44 ////////////////////////////////////////////////////////////////////// |
|
45 CBtDeviceContainer* CBtDeviceContainer::NewL() |
|
46 { |
|
47 CBtDeviceContainer* self = new (ELeave) CBtDeviceContainer(); |
|
48 CleanupStack::PushL(self); |
|
49 self->ConstructL(); |
|
50 CleanupStack::Pop(self); |
|
51 return self; |
|
52 } |
|
53 |
|
54 void CBtDeviceContainer::ConstructL() |
|
55 { |
|
56 User::LeaveIfError( iFs.Connect()); |
|
57 TInt err = RsUtils::CreateDataFolder( iFs, iDataFolder ); |
|
58 LOG1("[CBtDeviceContainer::ConstructL]\t err: %d", err ); |
|
59 iDataFolder = RsUtils::PathNotRom( iDataFolder ); |
|
60 iFs.SetSessionPath( iDataFolder ); |
|
61 LOG1("CBtDeviceContainer::ConstructL iDataFolder: %S", &iDataFolder ); |
|
62 } |
|
63 |
|
64 CBtDeviceContainer::CBtDeviceContainer() : iVersion(KUsedDevicesVersion) |
|
65 { |
|
66 } |
|
67 |
|
68 CBtDeviceContainer::~CBtDeviceContainer() |
|
69 { |
|
70 LOG("CBtDeviceContainer::~CBtDeviceContainer begin"); |
|
71 iDeviceArray.ResetAndDestroy(); |
|
72 iDeviceArray.Close(); |
|
73 iFs.Close(); |
|
74 LOG("CBtDeviceContainer::~CBtDeviceContainer end"); |
|
75 } |
|
76 |
|
77 //-------------------------------------------------------------------------------------------- |
|
78 // |
|
79 // CBtDeviceContainer::StoreL |
|
80 // |
|
81 //-------------------------------------------------------------------------------------------- |
|
82 void CBtDeviceContainer::StoreL() |
|
83 { |
|
84 LOG("[CBtDeviceContainer::StoreL]\t begin"); |
|
85 |
|
86 TFileName fileName( iDataFolder ); |
|
87 fileName.Append( KUsedDevicesFile ); |
|
88 LOG1("CBtDeviceContainer::StoreL fileName 1: %S", &fileName); |
|
89 |
|
90 RFileWriteStream outstream; |
|
91 TFileName tempFileName; |
|
92 |
|
93 User::LeaveIfError( outstream.Temp( iFs, iDataFolder, tempFileName, EFileWrite ) ); |
|
94 LOG1("[CBtDeviceContainer::StoreL]\t tempFileName: %S", &tempFileName); |
|
95 |
|
96 CleanupClosePushL( outstream ); |
|
97 ExternalizeL( outstream ); |
|
98 // makes sure all data is written from the buffer to file, before calling close. |
|
99 outstream.CommitL(); |
|
100 CleanupStack::PopAndDestroy(); // outstream |
|
101 |
|
102 if( RsUtils::FileExists( fileName, iFs ) ) |
|
103 User::LeaveIfError( iFs.Delete( fileName ) ); |
|
104 User::LeaveIfError( iFs.Rename( tempFileName, fileName ) ); |
|
105 |
|
106 LOG("[CBtDeviceContainer::StoreL]\t end"); |
|
107 } |
|
108 |
|
109 |
|
110 //-------------------------------------------------------------------------------------------- |
|
111 // |
|
112 // CBtDeviceContainer::RestoreL |
|
113 // |
|
114 //-------------------------------------------------------------------------------------------- |
|
115 void CBtDeviceContainer::RestoreL() |
|
116 { |
|
117 TFileName fileName( iDataFolder ); |
|
118 fileName.Append( KUsedDevicesFile ); |
|
119 |
|
120 RFileReadStream instream; |
|
121 CleanupClosePushL( instream ); |
|
122 |
|
123 User::LeaveIfError( instream.Open( iFs, fileName, EFileRead ) ); |
|
124 InternalizeL(instream); |
|
125 |
|
126 CleanupStack::PopAndDestroy(); // instream |
|
127 } |
|
128 |
|
129 //-------------------------------------------------------------------------------------------- |
|
130 // |
|
131 // CBtDeviceContainer::ExternalizeL |
|
132 // |
|
133 //-------------------------------------------------------------------------------------------- |
|
134 void CBtDeviceContainer::ExternalizeL(RWriteStream& aStream) const |
|
135 { |
|
136 TInt count = 0; |
|
137 for( TInt i = 0; i < iDeviceArray.Count(); i++ ) |
|
138 { |
|
139 if(iDeviceArray[i]->IsUsed()) |
|
140 { |
|
141 ++count; |
|
142 } |
|
143 } |
|
144 LOG1("[CBtDeviceContainer::ExternalizeL]\t count: %d", count); |
|
145 if(0 == count) |
|
146 { |
|
147 return; |
|
148 } |
|
149 |
|
150 aStream.WriteInt32L(iVersion); |
|
151 aStream.WriteInt32L(iCurrentId); |
|
152 aStream.WriteInt32L(count); |
|
153 for( TInt i = 0; i < iDeviceArray.Count(); i++ ) |
|
154 { |
|
155 if(iDeviceArray[i]->IsUsed()) |
|
156 { |
|
157 aStream << *(iDeviceArray[i]); |
|
158 } |
|
159 } |
|
160 LOG("CBtDeviceContainer::ExternalizeL end"); |
|
161 } |
|
162 |
|
163 //-------------------------------------------------------------------------------------------- |
|
164 // |
|
165 // CBtDeviceContainer::InternalizeL |
|
166 // |
|
167 //-------------------------------------------------------------------------------------------- |
|
168 void CBtDeviceContainer::InternalizeL(RReadStream& aStream) |
|
169 { |
|
170 TInt version = aStream.ReadInt32L(); |
|
171 if (version != KUsedDevicesVersion) |
|
172 { |
|
173 return; |
|
174 } |
|
175 iVersion = version; |
|
176 |
|
177 iCurrentId = aStream.ReadInt32L(); |
|
178 TInt count = aStream.ReadInt32L(); |
|
179 for (TInt i = 0; i < count; i++) |
|
180 { |
|
181 CRsBtDevice* tmpDevice = CRsBtDevice::NewLC(); |
|
182 |
|
183 tmpDevice->InternalizeL(aStream); |
|
184 |
|
185 tmpDevice->SetUsed(ETrue); |
|
186 tmpDevice->SetJustFound(EFalse); |
|
187 User::LeaveIfError( iDeviceArray.Append( tmpDevice ) ); |
|
188 CleanupStack::Pop(); // tmpDevice |
|
189 } |
|
190 } |
|
191 |
|
192 CRsBtDevice* CBtDeviceContainer::At(TInt aPosition) const |
|
193 { |
|
194 if( aPosition >= 0 && aPosition < iDeviceArray.Count() ) |
|
195 { |
|
196 return iDeviceArray[aPosition]; |
|
197 } |
|
198 else |
|
199 { |
|
200 return NULL; |
|
201 } |
|
202 } |
|
203 |
|
204 TInt CBtDeviceContainer::Count() const |
|
205 { |
|
206 return iDeviceArray.Count(); |
|
207 } |
|
208 |
|
209 //-------------------------------------------------------------------------------------------- |
|
210 // |
|
211 // CBtDeviceContainer::Find |
|
212 // |
|
213 //-------------------------------------------------------------------------------------------- |
|
214 TInt CBtDeviceContainer::Find( const CRsBtDevice &aDevice, TInt aInitIndex) const |
|
215 { |
|
216 TInt nCnt; |
|
217 CRsBtDevice *cmpDev; |
|
218 |
|
219 for (nCnt = aInitIndex+1; nCnt < iDeviceArray.Count(); nCnt++) |
|
220 { |
|
221 cmpDev = iDeviceArray[nCnt]; |
|
222 if (cmpDev->Equals(aDevice)) |
|
223 { |
|
224 return nCnt; |
|
225 } |
|
226 } |
|
227 |
|
228 return KErrNotFound; |
|
229 } |
|
230 |
|
231 //-------------------------------------------------------------------------------------------- |
|
232 // |
|
233 // CBtDeviceContainer::Find |
|
234 // |
|
235 //-------------------------------------------------------------------------------------------- |
|
236 TInt CBtDeviceContainer::Find( const TInt aDeviceId, TInt aInitIndex) const |
|
237 { |
|
238 for (TInt i = aInitIndex+1; i < iDeviceArray.Count(); i++) |
|
239 { |
|
240 if (iDeviceArray[i]->DeviceId() == aDeviceId) |
|
241 { |
|
242 return i; |
|
243 } |
|
244 } |
|
245 return KErrNotFound; |
|
246 } |
|
247 |
|
248 //-------------------------------------------------------------------------------------------- |
|
249 // |
|
250 // CBtDeviceContainer::Delete |
|
251 // |
|
252 //-------------------------------------------------------------------------------------------- |
|
253 TInt CBtDeviceContainer::Delete( const TInt aDeviceId ) |
|
254 { |
|
255 TInt pos = Find(aDeviceId); |
|
256 if(KErrNotFound != pos) |
|
257 { |
|
258 iDeviceArray[pos]->SetUsed(EFalse); |
|
259 } |
|
260 return pos; |
|
261 } |
|
262 |
|
263 //-------------------------------------------------------------------------------------------- |
|
264 // |
|
265 // CBtDeviceContainer::Delete |
|
266 // |
|
267 //-------------------------------------------------------------------------------------------- |
|
268 TInt CBtDeviceContainer::Delete( const CRsBtDevice &aDevice ) |
|
269 { |
|
270 TInt pos = Find(aDevice); |
|
271 if(KErrNotFound != pos) |
|
272 { |
|
273 iDeviceArray[pos]->SetUsed(EFalse); |
|
274 } |
|
275 return pos; |
|
276 } |
|
277 |
|
278 //-------------------------------------------------------------------------------------------- |
|
279 // |
|
280 // CBtDeviceContainer::AppendL |
|
281 // |
|
282 //-------------------------------------------------------------------------------------------- |
|
283 void CBtDeviceContainer::AppendL( CRsBtDevice* aDevice ) |
|
284 { |
|
285 LOG1("[CBtDeviceContainer::AppendL]\t %S", &(aDevice->FriendlyName())); |
|
286 |
|
287 if( iDeviceArray.Count() == KUsedDevicesMax ) |
|
288 { |
|
289 TInt nOldest = FindOldest(ENotUsed | ETimeLastUsed); |
|
290 CRsBtDevice* device = iDeviceArray[nOldest]; |
|
291 iDeviceArray.Remove(nOldest); |
|
292 delete device; |
|
293 } |
|
294 |
|
295 if( aDevice->DeviceId() <= 0 ) |
|
296 { |
|
297 aDevice->SetDeviceId( GetAvailableId( *aDevice ) ); |
|
298 } |
|
299 iDeviceArray.AppendL( aDevice ); |
|
300 } |
|
301 |
|
302 TInt CBtDeviceContainer::FindOldest(TUint aFlags) const |
|
303 { |
|
304 TInt oldest=-1; |
|
305 TTime timeOldest, timeCmp; |
|
306 CRsBtDevice *cmpDev; |
|
307 TBool bSavedCheck; |
|
308 |
|
309 timeOldest.UniversalTime(); |
|
310 for (TInt i=0; i < iDeviceArray.Count(); ++i) |
|
311 { |
|
312 cmpDev = iDeviceArray[i]; |
|
313 if ((aFlags & EUsed) && (aFlags & ENotUsed)) |
|
314 { |
|
315 bSavedCheck = ETrue; |
|
316 } |
|
317 else if (aFlags & ESaved) |
|
318 { |
|
319 bSavedCheck = cmpDev->IsUsed(); |
|
320 } |
|
321 else if (aFlags & ENotUsed) |
|
322 { |
|
323 bSavedCheck = !(cmpDev->IsUsed()); |
|
324 } |
|
325 else |
|
326 { |
|
327 bSavedCheck = ETrue; |
|
328 } |
|
329 |
|
330 if (bSavedCheck) |
|
331 { |
|
332 if (aFlags & ETimeLastUsed) |
|
333 { |
|
334 timeCmp = cmpDev->TimeLastUsed(); |
|
335 } |
|
336 else if (aFlags & ETimeDiscovered) |
|
337 { |
|
338 timeCmp = cmpDev->TimeDiscovered(); |
|
339 } |
|
340 else |
|
341 { |
|
342 // Error, but shouldn't get here if program does the right thing |
|
343 return -1; |
|
344 } |
|
345 |
|
346 if (timeCmp < timeOldest) |
|
347 { |
|
348 oldest = i; |
|
349 timeOldest = timeCmp; |
|
350 } |
|
351 } |
|
352 } |
|
353 |
|
354 return oldest; |
|
355 } |
|
356 |
|
357 TInt CBtDeviceContainer::GetAvailableId( const CRsBtDevice& aDevice ) |
|
358 { |
|
359 LOG("CBtDeviceContainer::GetAvailableId begin"); |
|
360 const TBTDevAddr& btAddr = aDevice.BDAddr(); |
|
361 TInt id( 0 ); |
|
362 TInt intSize = sizeof(TInt); |
|
363 LOG1("CBtDeviceContainer::GetAvailableId intSize: %d", intSize); |
|
364 for( TInt i = ( KBTDevAddrSize - 1 ), s = 0; i >= 0 && s < intSize; i--, s++ ) |
|
365 { |
|
366 const TUint8& element = btAddr[i]; |
|
367 TInt temp( element ); |
|
368 temp <<= ( s * 8 ); |
|
369 id |= temp; |
|
370 } |
|
371 |
|
372 id &= DEVICE_ID_FIELD_MASK; |
|
373 LOG1("[CBtDeviceContainer::GetAvailableId]\t id after masking: %d", id); |
|
374 |
|
375 while( !IsIdAvailable( id ) ) |
|
376 { |
|
377 id++; |
|
378 } |
|
379 |
|
380 iCurrentId = id; |
|
381 LOG1("[CBtDeviceContainer::GetAvailableId]\t return: %d", iCurrentId); |
|
382 return iCurrentId; |
|
383 } |
|
384 |
|
385 |
|
386 TBool CBtDeviceContainer::IsIdAvailable( TInt aId ) const |
|
387 { |
|
388 TBool result( ETrue ); |
|
389 for( TInt x = 0; x < iDeviceArray.Count() && result; ++x ) |
|
390 { |
|
391 if ( aId == iDeviceArray[x]->DeviceId() ) |
|
392 { |
|
393 result = EFalse; |
|
394 } |
|
395 } |
|
396 return result; |
|
397 } |
|
398 |
|
399 void CBtDeviceContainer::Reset() |
|
400 { |
|
401 iDeviceArray.ResetAndDestroy(); |
|
402 } |
|
403 |
|
404 // End of File |