|
1 // Copyright (c) 2006-2009 Nokia Corporation and/or its subsidiary(-ies). |
|
2 // All rights reserved. |
|
3 // This component and the accompanying materials are made available |
|
4 // under the terms of "Eclipse Public License v1.0" |
|
5 // which accompanies this distribution, and is available |
|
6 // at the URL "http://www.eclipse.org/legal/epl-v10.html". |
|
7 // |
|
8 // Initial Contributors: |
|
9 // Nokia Corporation - initial contribution. |
|
10 // |
|
11 // Contributors: |
|
12 // |
|
13 // Description: |
|
14 // |
|
15 |
|
16 /** |
|
17 @file |
|
18 @publishedPartner |
|
19 */ |
|
20 |
|
21 #include <ecom/ecomresolverparams.h> |
|
22 #include <ecom/implementationinformation.h> |
|
23 #include <ecom/ecom.h> |
|
24 |
|
25 #include <bluetooth/hci/hciutil.h> |
|
26 #include <bluetooth/hci/hciinifile.h> |
|
27 #include <bluetooth/logger.h> |
|
28 |
|
29 #ifdef __FLOG_ACTIVE |
|
30 _LIT8(KLogComponent, LOG_COMPONENT_HCIUTIL); |
|
31 #endif |
|
32 |
|
33 const TInt KTokenSize=256; |
|
34 |
|
35 /** |
|
36 Creates an instance of CHciUtil for working with a HCI config |
|
37 file for a particular component. |
|
38 @param aComponentName The name of the component using the instance. |
|
39 @return A new instance of CHciUtil. |
|
40 */ |
|
41 EXPORT_C CHciUtil* CHciUtil::NewL(const TDesC& aComponentName) |
|
42 { |
|
43 LOG_STATIC_FUNC |
|
44 LOG1(_L("aComponentName = %S"), &aComponentName); |
|
45 |
|
46 CHciUtil* self = new (ELeave) CHciUtil(); |
|
47 CleanupStack::PushL(self); |
|
48 self->ConstructL(aComponentName); |
|
49 CleanupStack::Pop(self); |
|
50 return self; |
|
51 } |
|
52 |
|
53 void CHciUtil::ConstructL(const TDesC& aComponentName) |
|
54 { |
|
55 LOG_FUNC |
|
56 |
|
57 if (aComponentName.Length() > KMaxHciUtilComponentName) |
|
58 { |
|
59 LEAVEIFERRORL(KErrArgument); |
|
60 } |
|
61 |
|
62 iCompName = aComponentName; |
|
63 } |
|
64 |
|
65 CHciUtil::CHciUtil() |
|
66 { |
|
67 LOG_FUNC |
|
68 } |
|
69 |
|
70 EXPORT_C CHciUtil::~CHciUtil() |
|
71 { |
|
72 LOG_FUNC |
|
73 |
|
74 // ensure any ini file is closed |
|
75 CloseIniFile(); |
|
76 } |
|
77 |
|
78 /** |
|
79 Opens an ini file based on the name of the component (provided when the CHciUtil |
|
80 instance was created.) It looks for the file in the default path. |
|
81 @panic KHciUtilPanic EAlreadyAnInFileOpen If the CHciUtil instance already has an ini file opened. |
|
82 */ |
|
83 EXPORT_C void CHciUtil::OpenIniFileL() |
|
84 { |
|
85 LOG_FUNC |
|
86 |
|
87 __ASSERT_ALWAYS(!iIniFile, PANIC(KHciUtilPanic, EAlreadyAnIniFileOpen)); |
|
88 |
|
89 TName name(KDefaultIniFilePath()); |
|
90 name.Append(iCompName); |
|
91 name.Append(_L(".ini")); |
|
92 |
|
93 iIniFile = CIniFileData::NewL(name); |
|
94 } |
|
95 |
|
96 /** |
|
97 Opens an ini file with a particular name in a given path location. |
|
98 @param aPath The path the ini file is located. |
|
99 @param aFile The name of the ini file to be opened. |
|
100 @panic KHciUtilPanic EAlreadyAnIniFileOpen If the CHciUtil instance already has an ini file opened. |
|
101 */ |
|
102 EXPORT_C void CHciUtil::OpenIniFileL(const TDesC& aPath, const TDesC& aFile) |
|
103 { |
|
104 LOG_FUNC |
|
105 |
|
106 __ASSERT_ALWAYS(!iIniFile, PANIC(KHciUtilPanic, EAlreadyAnIniFileOpen)); |
|
107 |
|
108 TName name = aPath; |
|
109 name.Append(aFile); |
|
110 |
|
111 iIniFile = CIniFileData::NewL(name); |
|
112 } |
|
113 |
|
114 /** |
|
115 Closes any ini file currently open. |
|
116 */ |
|
117 EXPORT_C void CHciUtil::CloseIniFile() |
|
118 { |
|
119 LOG_FUNC |
|
120 |
|
121 delete iIniFile; |
|
122 iIniFile = NULL; |
|
123 } |
|
124 |
|
125 TUid CHciUtil::FormatUidL(TPtrC& aDes) |
|
126 { |
|
127 LOG_FUNC |
|
128 |
|
129 aDes.Set(aDes.Right(aDes.Length() - 2)); // skip '0x' |
|
130 TLex uidLex(aDes.Left(8)); |
|
131 TUint tmp = 0; |
|
132 |
|
133 if (uidLex.Val(tmp, EHex) != KErrNone) |
|
134 { |
|
135 LEAVEIFERRORL(KErrCorrupt); |
|
136 } |
|
137 |
|
138 TUid uid; |
|
139 uid.iUid = tmp; |
|
140 return uid; |
|
141 } |
|
142 |
|
143 TUint CHciUtil::FormatValueL(TPtrC& aDes) |
|
144 { |
|
145 LOG_FUNC |
|
146 |
|
147 TLex uidLex(aDes); |
|
148 TUint ret = 0; |
|
149 |
|
150 if (uidLex.Val(ret) != KErrNone) |
|
151 { |
|
152 LEAVEIFERRORL(KErrCorrupt); |
|
153 } |
|
154 |
|
155 return ret; |
|
156 } |
|
157 |
|
158 void CHciUtil::GetFromFileL(const TDesC& aSection, const TDesC& aTag, TPtrC& aRetText) |
|
159 { |
|
160 LOG_FUNC |
|
161 if (!iIniFile) |
|
162 { |
|
163 // File isn't open - so we aren't ready to get values. |
|
164 LEAVEIFERRORL(KErrNotReady); |
|
165 } |
|
166 |
|
167 if (!iIniFile->FindVar(aSection, aTag, aRetText)) |
|
168 { |
|
169 LEAVEIFERRORL(KErrNotFound); |
|
170 } |
|
171 } |
|
172 |
|
173 /** |
|
174 Gets a TUid from an opened ini file. |
|
175 @param aSection The section name under which the tag for the UID is to be found. |
|
176 @param aTag The tag which marks up the UID that is required. |
|
177 @return The UID found in the ini file under given section and tag. |
|
178 */ |
|
179 EXPORT_C TUid CHciUtil::GetUidFromFileL(const TDesC& aSection, const TDesC& aTag) |
|
180 { |
|
181 LOG_FUNC |
|
182 |
|
183 TPtrC uidText; |
|
184 |
|
185 // Get the uid from the ini file |
|
186 GetFromFileL(aSection, aTag, uidText); |
|
187 |
|
188 // uid found |
|
189 return (FormatUidL(uidText)); |
|
190 } |
|
191 |
|
192 /** |
|
193 Gets a numeric value from an opened ini file. |
|
194 @param aSection The section name under which the tag for the UID is to be found. |
|
195 @param aTag The tag which marks up the UID that is required. |
|
196 @return The value found in the ini file under given section and tag. |
|
197 */ |
|
198 EXPORT_C TUint CHciUtil::GetValueFromFileL(const TDesC& aSection, const TDesC& aTag) |
|
199 { |
|
200 LOG_FUNC |
|
201 |
|
202 TPtrC valText; |
|
203 |
|
204 // Get the value from the ini file |
|
205 GetFromFileL(aSection, aTag, valText); |
|
206 |
|
207 // value found |
|
208 return (FormatValueL(valText)); |
|
209 } |
|
210 |
|
211 void ResetAndDestroy(TAny* aPtr) |
|
212 { |
|
213 reinterpret_cast<RImplInfoPtrArray*>(aPtr)->ResetAndDestroy(); |
|
214 } |
|
215 |
|
216 void CleanupResetAndDestroyPushL(RImplInfoPtrArray& aArray) |
|
217 { |
|
218 TCleanupItem item(ResetAndDestroy, &aArray); |
|
219 CleanupStack::PushL(item); |
|
220 } |
|
221 |
|
222 /** |
|
223 Utility for loading HCI plugins. |
|
224 Attempts to create a plugin instance with the appropriate interface. |
|
225 @param aIfUid The ECom interface UID to create an instance of. |
|
226 @param aKeyOffset The key offset. |
|
227 @return A newly created instance of the given ECom interface UID. |
|
228 @leave KErrNotFound If no ECom plugins supporting the supplied interface UID are available. |
|
229 @leave KErrArgument If more than one ECom plugin implementations are providing the specified interface. |
|
230 */ |
|
231 EXPORT_C TAny* CHciUtil::LoadImplementationL(TUid aIfUid, TInt aKeyOffset) |
|
232 { |
|
233 LOG_STATIC_FUNC |
|
234 |
|
235 RImplInfoPtrArray implInfoArray; |
|
236 CleanupResetAndDestroyPushL(implInfoArray); |
|
237 |
|
238 REComSession::ListImplementationsL(aIfUid, implInfoArray); |
|
239 TUid implUid = TUid::Null(); |
|
240 |
|
241 if (implInfoArray.Count() == 1) |
|
242 { |
|
243 // Return the UID of the one and only implementation |
|
244 implUid = implInfoArray[0]->ImplementationUid(); |
|
245 } |
|
246 else |
|
247 { |
|
248 // Not a single implementation |
|
249 if (implInfoArray.Count() == 0) |
|
250 { |
|
251 // No implementations found |
|
252 LEAVEIFERRORL(KErrNotFound); |
|
253 } |
|
254 else |
|
255 { |
|
256 // More than one implementation found |
|
257 LEAVEIFERRORL(KErrArgument); |
|
258 } |
|
259 } |
|
260 |
|
261 // Load the implementation |
|
262 TAny* ret = REComSession::CreateImplementationL(implUid, aKeyOffset); |
|
263 CleanupStack::PopAndDestroy(); // cleanup implInfoArray |
|
264 return ret; |
|
265 } |
|
266 |
|
267 // Used by Plugin base classes to load an implementation |
|
268 EXPORT_C TAny* CHciUtil::LoadImplementationL(TUid aIfUid, TUid aImplUid, TInt aKeyOffset) |
|
269 { |
|
270 LOG_STATIC_FUNC |
|
271 |
|
272 RImplInfoPtrArray implInfoArray; |
|
273 CleanupResetAndDestroyPushL(implInfoArray); |
|
274 |
|
275 REComSession::ListImplementationsL(aIfUid, implInfoArray); |
|
276 |
|
277 // Check if the specified implementation is for the correct interface |
|
278 for (TInt implIndex = implInfoArray.Count() - 1; implIndex >= 0; implIndex--) |
|
279 { |
|
280 if (implInfoArray[implIndex]->ImplementationUid() == aImplUid) |
|
281 { |
|
282 // Found the implmentation for the interface so load it |
|
283 TAny* ret = REComSession::CreateImplementationL(aImplUid, aKeyOffset); |
|
284 CleanupStack::PopAndDestroy(); // cleanup implInfoArray |
|
285 return ret; |
|
286 } |
|
287 } |
|
288 |
|
289 // Implementation not found for the interface |
|
290 LEAVEIFERRORL(KErrNotFound); |
|
291 |
|
292 // Not Reached |
|
293 return NULL; |
|
294 } |
|
295 |
|
296 /** |
|
297 Indicates that a specific ECom instance should be destroyed. |
|
298 */ |
|
299 EXPORT_C void CHciUtil::DestroyedImplementation(TUid aKey) |
|
300 { |
|
301 LOG_STATIC_FUNC |
|
302 |
|
303 REComSession::DestroyedImplementation(aKey); |
|
304 } |
|
305 |
|
306 /** |
|
307 A Method that converts a HCI error code (as defined by the Bluetooth specification) |
|
308 into a Symbian error code. |
|
309 @param aErrorCode The HCI error code to be converted |
|
310 @return The HCI error code provided as a value in the Symbian error code space. |
|
311 */ |
|
312 EXPORT_C /*static*/ TInt CHciUtil::SymbianErrorCode(THCIErrorCode aErrorCode) |
|
313 { |
|
314 LOG_STATIC_FUNC |
|
315 |
|
316 TInt rerr = KErrNone; |
|
317 if(aErrorCode != EOK) |
|
318 { |
|
319 rerr = KHCIErrorBase - aErrorCode; |
|
320 } |
|
321 return rerr; |
|
322 } |
|
323 |
|
324 |
|
325 // |
|
326 // |
|
327 // Find a key's value given a section name and a key name |
|
328 // |
|
329 |
|
330 |
|
331 CIniFileData::CIniFileData() |
|
332 : iPtr(NULL,0) |
|
333 /** Constructor |
|
334 |
|
335 */ |
|
336 { |
|
337 __DECLARE_NAME(_S("CIniFileData")); |
|
338 } |
|
339 |
|
340 |
|
341 CIniFileData::~CIniFileData() |
|
342 /** Destructor. |
|
343 |
|
344 Frees the resources located in second-phase constructor |
|
345 |
|
346 */ |
|
347 { |
|
348 |
|
349 delete (TText*)iPtr.Ptr(); |
|
350 delete iToken; |
|
351 delete iName; |
|
352 } |
|
353 |
|
354 |
|
355 CIniFileData* CIniFileData::NewL(const TDesC& aName) |
|
356 /** |
|
357 Creates, and returns a pointer to CIniFileData object, leave on failure |
|
358 |
|
359 @return A pointer to the CIniFileData object |
|
360 */ |
|
361 { |
|
362 CIniFileData* p=new(ELeave) CIniFileData; |
|
363 CleanupStack::PushL(p); |
|
364 p->ConstructL(aName); |
|
365 CleanupStack::Pop(); |
|
366 return p; |
|
367 } |
|
368 |
|
369 |
|
370 void CIniFileData::ConstructL(const TDesC& aName) |
|
371 /** |
|
372 Second-phase constructor. |
|
373 |
|
374 The function attempts to allocate a buffer and Read file's contents into iPtr |
|
375 |
|
376 @param aName the name of the file which contains the ini data |
|
377 |
|
378 @leave One of the system-wide error codes |
|
379 */ |
|
380 { |
|
381 // Allocate space for token |
|
382 iToken=HBufC::NewL(KTokenSize+2); // 2 extra chars for [tokenName] |
|
383 |
|
384 // Connect to file server |
|
385 TAutoClose<RFs> fs; |
|
386 User::LeaveIfError(fs.iObj.Connect()); |
|
387 fs.PushL(); |
|
388 |
|
389 // Find file, given name |
|
390 TFindFile ff(fs.iObj); |
|
391 User::LeaveIfError(ff.FindByDir(aName, KDefaultIniFilePath)); |
|
392 iName=ff.File().AllocL(); |
|
393 |
|
394 // Open file |
|
395 TAutoClose<RFile> file; |
|
396 TInt size; |
|
397 User::LeaveIfError(file.iObj.Open(fs.iObj,*iName,EFileStreamText|EFileShareReadersOrWriters)); |
|
398 file.PushL(); |
|
399 |
|
400 // Get file size and read in |
|
401 User::LeaveIfError(file.iObj.Size(size)); |
|
402 TText* data=(TText*)User::AllocL(size); |
|
403 iPtr.Set(data, size/sizeof(TText), size/sizeof(TText)); |
|
404 TPtr8 dest((TUint8*)data, 0, size); |
|
405 User::LeaveIfError(file.iObj.Read(dest)); |
|
406 TUint8* ptr = (TUint8*)data; |
|
407 |
|
408 // |
|
409 // This is orderred as FEFF assuming the processor is Little Endian |
|
410 // The data in the file is FFFE. PRR 28/9/98 |
|
411 // |
|
412 if(size>=(TInt)sizeof(TText) && iPtr[0]==0xFEFF) |
|
413 { |
|
414 // UNICODE Text file so lose the FFFE |
|
415 Mem::Copy(ptr, ptr+sizeof(TText), size-sizeof(TText)); |
|
416 iPtr.Set(data, size/sizeof(TText)-1, size/sizeof(TText)-1); |
|
417 } |
|
418 else if(size) |
|
419 { |
|
420 // NON-UNICODE so convert to UNICODE |
|
421 TText* newdata = (TText*)User::AllocL(size*sizeof(TText)); |
|
422 iPtr.Set(newdata, size, size); |
|
423 TInt i; |
|
424 for(i=0 ; i<size ; ++i) |
|
425 iPtr[i]=ptr[i]; |
|
426 delete data; |
|
427 } |
|
428 |
|
429 file.Pop(); |
|
430 fs.Pop(); |
|
431 } |
|
432 |
|
433 |
|
434 // |
|
435 // |
|
436 // Find a key's value given a section name and a key name |
|
437 // |
|
438 TBool CIniFileData::FindVar(const TDesC &aSectName,const TDesC &aKeyName,TPtrC &aResult) |
|
439 /** Find a text value from given aKeyName and aSecName in the ini data file |
|
440 |
|
441 @param aSectName Section containing key |
|
442 @param aKeyName Key being searched for in aSectName |
|
443 @param aResult On return, contains the text result |
|
444 |
|
445 @return ETrue if found, otherwise EFalse |
|
446 */ |
|
447 { |
|
448 __ASSERT_DEBUG(aSectName.Length()<=KTokenSize,PANIC(KHciUtilPanic, ESectionNameTooBig)); |
|
449 __ASSERT_DEBUG(aKeyName.Length()<=KTokenSize,PANIC(KHciUtilPanic, EKeyNameTooBig)); |
|
450 |
|
451 TInt posStartOfSection(0); |
|
452 TInt posEndOfSection(iPtr.Length()); // Default to the entire length of the ini data |
|
453 TPtrC SearchBuf; |
|
454 |
|
455 // If we have a section, set pos to section start |
|
456 TInt posI(0); // Position in internal data Buffer |
|
457 if( aSectName.Length() > 0 ) |
|
458 { |
|
459 TBool FoundSection(false); |
|
460 while ( ! FoundSection ) |
|
461 { |
|
462 // Move search buffer to next area of interest |
|
463 SearchBuf.Set(iPtr.Mid(posI)); |
|
464 |
|
465 // Make up token "[SECTIONNAME]", to search for |
|
466 TPtr sectionToken=iToken->Des(); |
|
467 _LIT(sectionTokenFmtString,"[%S]"); |
|
468 sectionToken.Format(sectionTokenFmtString,&aSectName); |
|
469 |
|
470 // Search for next occurrence of aSectName |
|
471 TInt posSB = SearchBuf.Find(sectionToken); |
|
472 |
|
473 if (posSB==KErrNotFound) |
|
474 return(EFalse); |
|
475 |
|
476 // Check this is at beginning of line (ie. non-commented) |
|
477 // ie. Check preceding char was LF |
|
478 if(posSB>0) |
|
479 { |
|
480 // Create a Buffer, starting one char before found subBuf |
|
481 TPtrC CharBefore(SearchBuf.Right(SearchBuf.Length()-posSB+1)); |
|
482 // Check first char is end of prev |
|
483 if(CharBefore[0] == '\n') |
|
484 { |
|
485 FoundSection = ETrue; // found |
|
486 posI = posI + posSB; |
|
487 } |
|
488 else |
|
489 { |
|
490 posI = posI + posSB + 1; // try again |
|
491 } |
|
492 } |
|
493 else |
|
494 { |
|
495 FoundSection = ETrue; |
|
496 } |
|
497 |
|
498 } // while ( ! FoundSection ) |
|
499 |
|
500 // Set start of section, after section name, (incl '[' and ']') |
|
501 posStartOfSection = posI + aSectName.Length() + 2; |
|
502 |
|
503 // Set end of section, by finding begin of next section or end |
|
504 SearchBuf.Set(iPtr.Mid(posI)); |
|
505 _LIT(nextSectionBuf,"\n["); |
|
506 TInt posSB = SearchBuf.Find(nextSectionBuf); |
|
507 if(posSB != KErrNotFound) |
|
508 { |
|
509 posEndOfSection = posI + posSB; |
|
510 } |
|
511 else |
|
512 { |
|
513 posEndOfSection = iPtr.Length(); |
|
514 } |
|
515 |
|
516 } // if( aSectName.Length() > 0 ) |
|
517 |
|
518 // Look for key in ini file data Buffer |
|
519 posI = posStartOfSection; |
|
520 TBool FoundKey(false); |
|
521 while ( ! FoundKey ) |
|
522 { |
|
523 // Search for next occurrence of aKeyName |
|
524 SearchBuf.Set(iPtr.Mid(posI,posEndOfSection-posI)); |
|
525 TInt posSB = SearchBuf.Find(aKeyName); |
|
526 |
|
527 // If not found, return |
|
528 if (posSB==KErrNotFound) |
|
529 return(EFalse); |
|
530 |
|
531 // Check this is at beginning of line (ie. non-commented) |
|
532 // ie. Check preceding char was CR or LF |
|
533 if(posSB>0) |
|
534 { |
|
535 // Create a Buffer, starting one char before found subBuf |
|
536 TPtrC CharBefore(SearchBuf.Right(SearchBuf.Length()-posSB+1)); |
|
537 // Check if the first char is end of prev and also check |
|
538 // if the token found is not a substring of another string |
|
539 TBool beginningOK = ((CharBefore[0] == '\n') || (CharBefore[0] == ' ') || (CharBefore[0] == '\t')); |
|
540 TBool endingOK = ((CharBefore[aKeyName.Length()+1] == '=') || (CharBefore[aKeyName.Length()+1] == ' ') || (CharBefore[aKeyName.Length()+1] == '\t')); |
|
541 if (beginningOK && endingOK) |
|
542 { |
|
543 FoundKey = ETrue; |
|
544 posI = posI + posSB; |
|
545 } |
|
546 else |
|
547 { |
|
548 posI = posI + posSB + 1; |
|
549 } |
|
550 } |
|
551 else |
|
552 { |
|
553 FoundKey = ETrue; |
|
554 } |
|
555 } // while ( ! FoundKey ) |
|
556 |
|
557 // Set pos, to just after '=' sign |
|
558 SearchBuf.Set(iPtr.Mid(posI)); |
|
559 TInt posSB = SearchBuf.Locate('='); |
|
560 if(posSB==KErrNotFound) // Illegal format, should flag this... |
|
561 return(EFalse); |
|
562 |
|
563 // Identify start and end of data (EOL or EOF) |
|
564 posI = posI + posSB + 1; // 1 char after '=' |
|
565 TInt posValStart = posI; |
|
566 TInt posValEnd; |
|
567 SearchBuf.Set(iPtr.Mid(posI)); |
|
568 posSB = SearchBuf.Locate('\r'); |
|
569 if(posSB!=KErrNotFound) |
|
570 { |
|
571 posValEnd = posI + posSB; |
|
572 } |
|
573 else |
|
574 { |
|
575 posValEnd = iPtr.Length(); |
|
576 } |
|
577 |
|
578 // Check we are still in the section requested |
|
579 if( aSectName.Length() > 0 ) |
|
580 { |
|
581 if( posValEnd > posEndOfSection ) |
|
582 { |
|
583 return(EFalse); |
|
584 } |
|
585 } |
|
586 // Parse Buffer from posn of key |
|
587 // Start one space after '=' |
|
588 TLex lex(iPtr.Mid(posValStart, posValEnd-posValStart)); |
|
589 lex.SkipSpaceAndMark(); // Should be at the start of the data |
|
590 aResult.Set(lex.MarkedToken().Ptr(),posValEnd-posValStart - lex.Offset() ); |
|
591 return(ETrue); |
|
592 } |
|
593 |
|
594 |
|
595 |
|
596 |
|
597 |
|
598 |