• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /******************************************************************************
2  *
3  *  Copyright (C) 2011-2012 Broadcom Corporation
4  *  Copyright 2018-2019 NXP.
5  *
6  *  Licensed under the Apache License, Version 2.0 (the "License");
7  *  you may not use this file except in compliance with the License.
8  *  You may obtain a copy of the License at:
9  *
10  *  http://www.apache.org/licenses/LICENSE-2.0
11  *
12  *  Unless required by applicable law or agreed to in writing, software
13  *  distributed under the License is distributed on an "AS IS" BASIS,
14  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  *  See the License for the specific language governing permissions and
16  *  limitations under the License.
17  *
18  ******************************************************************************/
19 
20 #include "phNxpConfig.h"
21 #include <stdio.h>
22 #include <string>
23 #include <vector>
24 #include <list>
25 #include <sys/stat.h>
26 #include <android-base/stringprintf.h>
27 #include <base/logging.h>
28 #include <cutils/properties.h>
29 #include  "phNxpLog.h"
30 
31 using android::base::StringPrintf;
32 
33 extern bool uwb_debug_enabled;
34 
35 #define nxp_config_name             "libuwb-nxp.conf"
36 #define uci_config_name             "libuwb-uci.conf"
37 
38 
39 const char alternative_config_path[] = "/vendor/etc/";
40 
41 #define extra_config_base "libuwb-"
42 #define extra_config_ext ".conf"
43 
44 #define extra_config_ext        ".conf"
45 #define     IsStringValue       0x80000000
46 
47 const char nxp_uci_config_path[] = "/vendor/etc/libuwb-uci.conf";
48 const char default_nxp_config_path[] = "/vendor/etc/";
49 
50 using namespace::std;
51 
52 class uwbParam : public string
53 {
54 public:
55     uwbParam();
56     uwbParam(const char* name, const string& value);
57     uwbParam(const char* name, unsigned long value);
58     virtual ~uwbParam();
numValue() const59     unsigned long numValue() const {return m_numValue;}
str_value() const60     const char*   str_value() const {return m_str_value.c_str();}
str_len() const61     size_t        str_len() const   {return m_str_value.length();}
62 private:
63     string          m_str_value;
64     unsigned long   m_numValue;
65 };
66 
67 class CUwbNxpConfig : public vector<const uwbParam*>
68 {
69 public:
70     virtual ~CUwbNxpConfig();
71     static CUwbNxpConfig& GetInstance();
72     friend void readOptionalConfig(const char* optional);
73 
74     bool    getValue(const char* name, char* pValue, size_t len) const;
75     bool    getValue(const char* name, unsigned long& rValue) const;
76     bool    getValue(const char* name, unsigned short & rValue) const;
77     bool    getValue(const char* name, char* pValue, long len,long* readlen) const;
78     const uwbParam*    find(const char* p_name) const;
79     void    readNxpConfig(const char* fileName) const;
80     void    clean();
81 private:
82     CUwbNxpConfig();
83     bool    readConfig(const char* name, bool bResetContent);
84     void    moveFromList();
85     void    moveToList();
86     void    add(const uwbParam* pParam);
87     void    dump();
88     bool    isAllowed(const char* name);
89     list<const uwbParam*> m_list;
90     bool    mValidFile;
91     string  mCurrentFile;
92 
93     unsigned long   state;
94 
Is(unsigned long f)95     inline bool Is(unsigned long f) {return (state & f) == f;}
Set(unsigned long f)96     inline void Set(unsigned long f) {state |= f;}
Reset(unsigned long f)97     inline void Reset(unsigned long f) {state &= ~f;}
98 };
99 
100 /*******************************************************************************
101 **
102 ** Function:    isPrintable()
103 **
104 ** Description: determine if 'c' is printable
105 **
106 ** Returns:     1, if printable, otherwise 0
107 **
108 *******************************************************************************/
isPrintable(char c)109 inline bool isPrintable(char c)
110 {
111     return  (c >= 'A' && c <= 'Z') ||
112             (c >= 'a' && c <= 'z') ||
113             (c >= '0' && c <= '9') ||
114             c == '/' || c == '_' || c == '-' || c == '.';
115 }
116 
117 /*******************************************************************************
118 **
119 ** Function:    isDigit()
120 **
121 ** Description: determine if 'c' is numeral digit
122 **
123 ** Returns:     true, if numerical digit
124 **
125 *******************************************************************************/
isDigit(char c,int base)126 inline bool isDigit(char c, int base)
127 {
128     if ('0' <= c && c <= '9')
129         return true;
130     if (base == 16)
131     {
132         if (('A' <= c && c <= 'F') ||
133             ('a' <= c && c <= 'f') )
134             return true;
135     }
136     return false;
137 }
138 
139 /*******************************************************************************
140 **
141 ** Function:    getDigitValue()
142 **
143 ** Description: return numerical value of a decimal or hex char
144 **
145 ** Returns:     numerical value if decimal or hex char, otherwise 0
146 **
147 *******************************************************************************/
getDigitValue(char c,int base)148 inline int getDigitValue(char c, int base)
149 {
150     if ('0' <= c && c <= '9')
151         return c - '0';
152     if (base == 16)
153     {
154         if ('A' <= c && c <= 'F')
155             return c - 'A' + 10;
156         else if ('a' <= c && c <= 'f')
157             return c - 'a' + 10;
158     }
159     return 0;
160 }
161 
162 /*******************************************************************************
163 **
164 ** Function:    CUwbNxpConfig::readConfig()
165 **
166 ** Description: read Config settings and parse them into a linked list
167 **              move the element from linked list to a array at the end
168 **
169 ** Returns:     1, if there are any config data, 0 otherwise
170 **
171 *******************************************************************************/
readConfig(const char * name,bool bResetContent)172 bool CUwbNxpConfig::readConfig(const char* name, bool bResetContent)
173 {
174     enum {
175         BEGIN_LINE = 1,
176         TOKEN,
177         STR_VALUE,
178         NUM_VALUE,
179         BEGIN_HEX,
180         BEGIN_QUOTE,
181         END_LINE
182     };
183 
184     FILE*   fd;
185     struct stat buf;
186     string  token;
187     string  strValue;
188     unsigned long    numValue = 0;
189     uwbParam* pParam = NULL;
190     int     i = 0;
191     int     base = 0;
192     char    c;
193     int     bflag = 0;
194     mCurrentFile = name;
195 
196     state = BEGIN_LINE;
197     /* open config file, read it into a buffer */
198     if ((fd = fopen(name, "rb")) == NULL)
199     {
200         DLOG_IF(INFO, uwb_debug_enabled) << StringPrintf("%s Cannot open config file %s\n", __func__, name);
201         if (bResetContent)
202         {
203             DLOG_IF(INFO, uwb_debug_enabled) << StringPrintf("%s Using default value for all settings\n", __func__);
204             mValidFile = false;
205         }
206         return false;
207     }
208     DLOG_IF(INFO, uwb_debug_enabled) << StringPrintf("%s Opened %s config %s\n", __func__, (bResetContent ? "base" : "optional"), name);
209     if(stat(name, &buf) < 0)
210     {
211         DLOG_IF(INFO, uwb_debug_enabled) << StringPrintf("Get File Information failed");
212         fclose(fd);
213         return false;
214     }
215 
216     mValidFile = true;
217     if (size() > 0)
218     {
219         if (bResetContent)
220         clean();
221         else
222             moveToList();
223     }
224 
225     for (;;)
226     {
227         if (feof(fd) || fread(&c, 1, 1, fd) != 1)
228         {
229             if (state == BEGIN_LINE)
230                 break;
231 
232             // got to the EOF but not in BEGIN_LINE state so the file
233             // probably does not end with a newline, so the parser has
234             // not processed current line, simulate a newline in the file
235             c = '\n';
236         }
237 
238         switch (state & 0xff)
239         {
240         case BEGIN_LINE:
241             if (c == '#')
242                 state = END_LINE;
243             else if (isPrintable(c))
244             {
245                 i = 0;
246                 token.erase();
247                 strValue.erase();
248                 state = TOKEN;
249                 token.push_back(c);
250             }
251             break;
252         case TOKEN:
253             if (c == '=')
254             {
255                 token.push_back('\0');
256                 state = BEGIN_QUOTE;
257             }
258             else if (isPrintable(c))
259                 token.push_back(c);
260             else
261                 state = END_LINE;
262             break;
263         case BEGIN_QUOTE:
264             if (c == '"')
265             {
266                 state = STR_VALUE;
267                 base = 0;
268             }
269             else if (c == '0')
270                 state = BEGIN_HEX;
271             else if (isDigit(c, 10))
272             {
273                 state = NUM_VALUE;
274                 base = 10;
275                 numValue = getDigitValue(c, base);
276                 i = 0;
277             }
278             else if (c == '{')
279             {
280                 state = NUM_VALUE;
281                 bflag = 1;
282                 base = 16;
283                 i = 0;
284                 Set(IsStringValue);
285             }
286             else
287                 state = END_LINE;
288             break;
289         case BEGIN_HEX:
290             if (c == 'x' || c == 'X')
291             {
292                 state = NUM_VALUE;
293                 base = 16;
294                 numValue = 0;
295                 i = 0;
296                 break;
297             }
298             else if (isDigit(c, 10))
299             {
300                 state = NUM_VALUE;
301                 base = 10;
302                 numValue = getDigitValue(c, base);
303                 break;
304             }
305             else if (c != '\n' && c != '\r')
306             {
307                 state = END_LINE;
308                 break;
309             }
310             // fall through to numValue to handle numValue
311             if (isDigit(c, base))
312             {
313                 numValue *= base;
314                 numValue += getDigitValue(c, base);
315                 ++i;
316             }
317             //else if(bflag == 1 && (c == ' ' || c == '\r' || c=='\n' || c=='\t'))
318             //{
319             //    break;
320             //}
321             //else if (base == 16 && (c== ','|| c == ':' || c == '-' || c == ' ' || c == '}'))
322             //{
323 
324             //    if( c=='}' )
325             //    {
326             //        bflag = 0;
327             //    }
328             //    if (i > 0)
329             //    {
330             //        int n = (i+1) / 2;
331             //        while (n-- > 0)
332             //        {
333             //            numValue = numValue >> (n * 8);
334             //            unsigned char c = (numValue)  & 0xFF;
335             //            strValue.push_back(c);
336             //        }
337             //    }
338 
339             //    Set(IsStringValue);
340             //    numValue = 0;
341             //    i = 0;
342             //}
343             else
344             {
345                 if (c == '\n' || c == '\r')
346                 {
347                     if(bflag == 0 )
348                     {
349                         state = BEGIN_LINE;
350                     }
351                 }
352             //    else
353             //    {
354             //        if( bflag == 0)
355             //        {
356             //            state = END_LINE;
357             //        }
358             //    }
359                 if (Is(IsStringValue) && base == 16 && i > 0)
360                 {
361                     int n = (i+1) / 2;
362                     while (n-- > 0)
363                         strValue.push_back(((numValue >> (n * 8))  & 0xFF));
364                 }
365                 if (strValue.length() > 0)
366                     pParam = new uwbParam(token.c_str(), strValue);
367                 else
368                     pParam = new uwbParam(token.c_str(), numValue);
369                 add(pParam);
370                 strValue.erase();
371                 numValue = 0;
372             }
373             break;
374 
375         case NUM_VALUE:
376             if (isDigit(c, base))
377             {
378                 numValue *= base;
379                 numValue += getDigitValue(c, base);
380                 ++i;
381             }
382             else if(bflag == 1 && (c == ' ' || c == '\r' || c=='\n' || c=='\t'))
383             {
384                 break;
385             }
386             else if (base == 16 && (c== ','|| c == ':' || c == '-' || c == ' ' || c == '}'))
387             {
388 
389                 if( c=='}' )
390                 {
391                     bflag = 0;
392                 }
393                 if (i > 0)
394                 {
395                     int n = (i+1) / 2;
396                     while (n-- > 0)
397                     {
398                         numValue = numValue >> (n * 8);
399                         unsigned char c = (numValue)  & 0xFF;
400                         strValue.push_back(c);
401                     }
402                 }
403 
404                 Set(IsStringValue);
405                 numValue = 0;
406                 i = 0;
407             }
408             else
409             {
410                 if (c == '\n' || c == '\r')
411                 {
412                     if(bflag == 0 )
413                     {
414                         state = BEGIN_LINE;
415                     }
416                 }
417                 else
418                 {
419                     if( bflag == 0)
420                     {
421                         state = END_LINE;
422                     }
423                 }
424                 if (Is(IsStringValue) && base == 16 && i > 0)
425                 {
426                     int n = (i+1) / 2;
427                     while (n-- > 0)
428                         strValue.push_back(((numValue >> (n * 8))  & 0xFF));
429                 }
430                 if (strValue.length() > 0)
431                     pParam = new uwbParam(token.c_str(), strValue);
432                 else
433                     pParam = new uwbParam(token.c_str(), numValue);
434                 add(pParam);
435                 strValue.erase();
436                 numValue = 0;
437             }
438             break;
439         case STR_VALUE:
440             if (c == '"')
441             {
442                 strValue.push_back('\0');
443                 state = END_LINE;
444                 pParam = new uwbParam(token.c_str(), strValue);
445                 add(pParam);
446             }
447             else if (isPrintable(c))
448                 strValue.push_back(c);
449             break;
450         case END_LINE:
451             if (c == '\n' || c == '\r')
452                 state = BEGIN_LINE;
453             break;
454         default:
455             break;
456         }
457     }
458 
459     fclose(fd);
460 
461     moveFromList();
462     return size() > 0;
463 }
464 
465 /*******************************************************************************
466 **
467 ** Function:    CUwbNxpConfig::CUwbNxpConfig()
468 **
469 ** Description: class constructor
470 **
471 ** Returns:     none
472 **
473 *******************************************************************************/
CUwbNxpConfig()474 CUwbNxpConfig::CUwbNxpConfig() :
475     mValidFile(true),
476     state(0)
477 {
478 }
479 
480 /*******************************************************************************
481 **
482 ** Function:    CUwbNxpConfig::~CUwbNxpConfig()
483 **
484 ** Description: class destructor
485 **
486 ** Returns:     none
487 **
488 *******************************************************************************/
~CUwbNxpConfig()489 CUwbNxpConfig::~CUwbNxpConfig()
490 {
491 }
492 
493 /*******************************************************************************
494 **
495 ** Function:    CUwbNxpConfig::GetInstance()
496 **
497 ** Description: get class singleton object
498 **
499 ** Returns:     none
500 **
501 *******************************************************************************/
GetInstance()502 CUwbNxpConfig& CUwbNxpConfig::GetInstance()
503 {
504   static CUwbNxpConfig theInstance;
505   if (theInstance.size() == 0 && theInstance.mValidFile) {
506     string strPath;
507     if (default_nxp_config_path[0] != '\0') {
508       strPath.assign(default_nxp_config_path);
509       strPath += nxp_config_name;
510       theInstance.readConfig(strPath.c_str(), true);
511       if (!theInstance.empty()) {
512         return theInstance;
513       }
514     }
515   }
516   return theInstance;
517 }
518 
519 /*******************************************************************************
520 **
521 ** Function:    CUwbNxpConfig::getValue()
522 **
523 ** Description: get a string value of a setting
524 **
525 ** Returns:     true if setting exists
526 **              false if setting does not exist
527 **
528 *******************************************************************************/
getValue(const char * name,char * pValue,size_t len) const529 bool CUwbNxpConfig::getValue(const char* name, char* pValue, size_t len) const
530 {
531     const uwbParam* pParam = find(name);
532     if (pParam == NULL)
533         return false;
534 
535     if (pParam->str_len() > 0)
536     {
537         memset(pValue, 0, len);
538         memcpy(pValue, pParam->str_value(), pParam->str_len());
539         return true;
540     }
541     return false;
542 }
543 
getValue(const char * name,char * pValue,long len,long * readlen) const544 bool CUwbNxpConfig::getValue(const char* name, char* pValue, long len,long* readlen) const
545 {
546     const uwbParam* pParam = find(name);
547     if (pParam == NULL)
548         return false;
549 
550     if (pParam->str_len() > 0)
551     {
552         if(pParam->str_len() <= (unsigned long)len)
553         {
554             memset(pValue, 0, len);
555             memcpy(pValue, pParam->str_value(), pParam->str_len());
556             *readlen = pParam->str_len();
557         }
558         else
559         {
560             *readlen = -1;
561         }
562 
563         return true;
564     }
565     return false;
566 }
567 
568 /*******************************************************************************
569 **
570 ** Function:    CUwbNxpConfig::getValue()
571 **
572 ** Description: get a long numerical value of a setting
573 **
574 ** Returns:     true if setting exists
575 **              false if setting does not exist
576 **
577 *******************************************************************************/
getValue(const char * name,unsigned long & rValue) const578 bool CUwbNxpConfig::getValue(const char* name, unsigned long& rValue) const
579 {
580     const uwbParam* pParam = find(name);
581     if (pParam == NULL)
582         return false;
583 
584     if (pParam->str_len() == 0)
585     {
586         rValue = static_cast<unsigned long>(pParam->numValue());
587         return true;
588     }
589     return false;
590 }
591 
592 /*******************************************************************************
593 **
594 ** Function:    CUwbNxpConfig::getValue()
595 **
596 ** Description: get a short numerical value of a setting
597 **
598 ** Returns:     true if setting exists
599 **              false if setting does not exist
600 **
601 *******************************************************************************/
getValue(const char * name,unsigned short & rValue) const602 bool CUwbNxpConfig::getValue(const char* name, unsigned short& rValue) const
603 {
604     const uwbParam* pParam = find(name);
605     if (pParam == NULL)
606         return false;
607 
608     if (pParam->str_len() == 0)
609     {
610         rValue = static_cast<unsigned short>(pParam->numValue());
611         return true;
612     }
613     return false;
614 }
615 
616 /*******************************************************************************
617 **
618 ** Function:    CUwbNxpConfig::find()
619 **
620 ** Description: search if a setting exist in the setting array
621 **
622 ** Returns:     pointer to the setting object
623 **
624 *******************************************************************************/
find(const char * p_name) const625 const uwbParam* CUwbNxpConfig::find(const char* p_name) const
626 {
627     if (size() == 0)
628         return NULL;
629 
630     for (const_iterator it = begin(), itEnd = end(); it != itEnd; ++it)
631     {
632         if (**it < p_name)
633         {
634             continue;
635         }
636         else if (**it == p_name)
637         {
638             if((*it)->str_len() > 0)
639             {
640                 DLOG_IF(INFO, uwb_debug_enabled) << StringPrintf("%s found %s=%s\n", __func__, p_name, (*it)->str_value());
641             }
642             else
643             {
644                 DLOG_IF(INFO, uwb_debug_enabled) << StringPrintf("%s found %s=(0x%lx)\n", __func__, p_name, (*it)->numValue());
645             }
646             return *it;
647         }
648         else
649             break;
650     }
651     return NULL;
652 }
653 /*******************************************************************************
654 **
655 ** Function:    CUwbNxpConfig::readNxpPHYConfig()
656 **
657 ** Description: read Config settings from RF conf file
658 **
659 ** Returns:     none
660 **
661 *******************************************************************************/
readNxpConfig(const char * fileName) const662 void CUwbNxpConfig::readNxpConfig(const char* fileName) const
663 {
664     DLOG_IF(INFO, uwb_debug_enabled) << StringPrintf("readNxpConfig-Enter..Reading");
665     CUwbNxpConfig::GetInstance().readConfig(fileName, false);
666 }
667 /*******************************************************************************
668 **
669 ** Function:    CUwbNxpConfig::clean()
670 **
671 ** Description: reset the setting array
672 **
673 ** Returns:     none
674 **
675 *******************************************************************************/
clean()676 void CUwbNxpConfig::clean()
677 {
678     if (size() == 0)
679         return;
680 
681     for (iterator it = begin(), itEnd = end(); it != itEnd; ++it)
682         delete *it;
683     clear();
684 }
685 
686 /*******************************************************************************
687 **
688 ** Function:    CUwbNxpConfig::Add()
689 **
690 ** Description: add a setting object to the list
691 **
692 ** Returns:     none
693 **
694 *******************************************************************************/
add(const uwbParam * pParam)695 void CUwbNxpConfig::add(const uwbParam* pParam)
696 {
697     if (m_list.size() == 0)
698     {
699         m_list.push_back(pParam);
700         return;
701     }
702     if((mCurrentFile.find("nxpPhy") != std::string::npos) && !isAllowed(pParam->c_str()))
703     {
704         DLOG_IF(INFO, uwb_debug_enabled) << StringPrintf("%s Token restricted. Returning", __func__);
705         return;
706     }
707     for (list<const uwbParam*>::iterator it = m_list.begin(), itEnd = m_list.end(); it != itEnd; ++it)
708     {
709         if (**it < pParam->c_str())
710             continue;
711         if (**it == pParam->c_str())
712             m_list.insert(m_list.erase(it), pParam);
713         else
714             m_list.insert(it, pParam);
715 
716         return;
717     }
718     m_list.push_back(pParam);
719 }
720 /*******************************************************************************
721 **
722 ** Function:    CUwbNxpConfig::dump()
723 **
724 ** Description: prints all elements in the list
725 **
726 ** Returns:     none
727 **
728 *******************************************************************************/
dump()729 void CUwbNxpConfig::dump()
730 {
731     DLOG_IF(INFO, uwb_debug_enabled) << StringPrintf("%s Enter", __func__);
732 
733     for (list<const uwbParam*>::iterator it = m_list.begin(), itEnd = m_list.end(); it != itEnd; ++it)
734     {
735         if((*it)->str_len()>0)
736             DLOG_IF(INFO, uwb_debug_enabled) << StringPrintf("%s %s \t= %s", __func__, (*it)->c_str(),(*it)->str_value());
737         else
738             DLOG_IF(INFO, uwb_debug_enabled) << StringPrintf("%s %s \t= (0x%0lX)\n", __func__,(*it)->c_str(),(*it)->numValue());
739     }
740 }
741 /*******************************************************************************
742 **
743 ** Function:    CUwbNxpConfig::isAllowed()
744 **
745 ** Description: checks if token update is allowed
746 **
747 ** Returns:     true if allowed else false
748 **
749 *******************************************************************************/
isAllowed(const char * name)750 bool CUwbNxpConfig::isAllowed(const char* name)
751 {
752     string token(name);
753     bool stat = false;
754     return stat;
755 }
756 /*******************************************************************************
757 **
758 ** Function:    CUwbNxpConfig::moveFromList()
759 **
760 ** Description: move the setting object from list to array
761 **
762 ** Returns:     none
763 **
764 *******************************************************************************/
moveFromList()765 void CUwbNxpConfig::moveFromList()
766 {
767     if (m_list.size() == 0)
768         return;
769 
770     for (list<const uwbParam*>::iterator it = m_list.begin(), itEnd = m_list.end(); it != itEnd; ++it)
771         push_back(*it);
772     m_list.clear();
773 }
774 
775 /*******************************************************************************
776 **
777 ** Function:    CUwbNxpConfig::moveToList()
778 **
779 ** Description: move the setting object from array to list
780 **
781 ** Returns:     none
782 **
783 *******************************************************************************/
moveToList()784 void CUwbNxpConfig::moveToList()
785 {
786     if (m_list.size() != 0)
787         m_list.clear();
788 
789     for (iterator it = begin(), itEnd = end(); it != itEnd; ++it)
790         m_list.push_back(*it);
791     clear();
792 }
793 
794 /*******************************************************************************
795 **
796 ** Function:    uwbParam::uwbParam()
797 **
798 ** Description: class constructor
799 **
800 ** Returns:     none
801 **
802 *******************************************************************************/
uwbParam()803 uwbParam::uwbParam() :
804     m_numValue(0)
805 {
806 }
807 
808 /*******************************************************************************
809 **
810 ** Function:    uwbParam::~uwbParam()
811 **
812 ** Description: class destructor
813 **
814 ** Returns:     none
815 **
816 *******************************************************************************/
~uwbParam()817 uwbParam::~uwbParam()
818 {
819 }
820 
821 /*******************************************************************************
822 **
823 ** Function:    uwbParam::uwbParam()
824 **
825 ** Description: class copy constructor
826 **
827 ** Returns:     none
828 **
829 *******************************************************************************/
uwbParam(const char * name,const string & value)830 uwbParam::uwbParam(const char* name,  const string& value) :
831     string(name),
832     m_str_value(value),
833     m_numValue(0)
834 {
835 }
836 
837 /*******************************************************************************
838 **
839 ** Function:    uwbParam::uwbParam()
840 **
841 ** Description: class copy constructor
842 **
843 ** Returns:     none
844 **
845 *******************************************************************************/
uwbParam(const char * name,unsigned long value)846 uwbParam::uwbParam(const char* name,  unsigned long value) :
847     string(name),
848     m_numValue(value)
849 {
850 }
851 
852 /*******************************************************************************
853 **
854 ** Function:    readOptionalConfig()
855 **
856 ** Description: read Config settings from an optional conf file
857 **
858 ** Returns:     none
859 **
860 *******************************************************************************/
readOptionalConfig(const char * extra)861 void readOptionalConfig(const char* extra)
862 {
863     string strPath;
864     if (alternative_config_path[0] != '\0')
865         strPath.assign(alternative_config_path);
866 
867     strPath += extra_config_base;
868     strPath += extra;
869     strPath += extra_config_ext;
870     CUwbNxpConfig::GetInstance().readConfig(strPath.c_str(), false);
871 }
872 
873 /*******************************************************************************
874 **
875 ** Function:    GetStrValue
876 **
877 ** Description: API function for getting a string value of a setting
878 **
879 ** Returns:     True if found, otherwise False.
880 **
881 *******************************************************************************/
GetNxpConfigStrValue(const char * name,char * pValue,unsigned long len)882 extern "C" int GetNxpConfigStrValue(const char* name, char* pValue, unsigned long len)
883 {
884     CUwbNxpConfig& rConfig = CUwbNxpConfig::GetInstance();
885 
886     return rConfig.getValue(name, pValue, len);
887 }
888 
889 /*******************************************************************************
890 **
891 ** Function:    GetNxpConfigCountryCodeByteArrayValue()
892 **
893 ** Description: Read byte array value from the config file.
894 **
895 ** Parameters:
896 **              name    - name of the config param to read.
897 **              fName   - name of the Country code.
898 **              pValue  - pointer to input buffer.
899 **              bufflen - input buffer length.
900 **              len     - out parameter to return the number of bytes read from config file,
901 **                        return -1 in case bufflen is not enough.
902 **
903 ** Returns:     TRUE[1] if config param name is found in the config file, else FALSE[0]
904 **
905 *******************************************************************************/
GetNxpConfigCountryCodeByteArrayValue(const char * name,const char * fName,char * pValue,long bufflen,long * len)906 extern "C" int GetNxpConfigCountryCodeByteArrayValue(const char* name,const char* fName, char* pValue,long bufflen, long *len)
907 {
908     DLOG_IF(INFO, true) << StringPrintf("GetNxpConfigCountryCodeByteArrayValue enter....");
909     CUwbNxpConfig& rConfig = CUwbNxpConfig::GetInstance();
910     readOptionalConfig(fName);
911     DLOG_IF(INFO, true) << StringPrintf("GetNxpConfigCountryCodeByteArrayValue exit....");
912     return rConfig.getValue(name, pValue, bufflen,len);
913 }
914 
915 /*******************************************************************************
916 **
917 ** Function:    GetNxpConfigUciByteArrayValue()
918 **
919 ** Description: Read byte array value from the config file.
920 **
921 ** Parameters:
922 **              name    - name of the config param to read.
923 **              pValue  - pointer to input buffer.
924 **              bufflen - input buffer length.
925 **              len     - out parameter to return the number of bytes read from config file,
926 **                        return -1 in case bufflen is not enough.
927 **
928 ** Returns:     TRUE[1] if config param name is found in the config file, else FALSE[0]
929 **
930 *******************************************************************************/
GetNxpConfigUciByteArrayValue(const char * name,char * pValue,long bufflen,long * len)931 extern "C" int GetNxpConfigUciByteArrayValue(const char* name, char* pValue,long bufflen, long *len)
932 {
933     DLOG_IF(INFO, uwb_debug_enabled) << StringPrintf("GetNxpConfigUciByteArrayValue enter....");
934     CUwbNxpConfig& rConfig = CUwbNxpConfig::GetInstance();
935     rConfig.readNxpConfig(nxp_uci_config_path);
936     DLOG_IF(INFO, uwb_debug_enabled) << StringPrintf("GetNxpConfigUciByteArrayValue exit....");
937     return rConfig.getValue(name, pValue, bufflen,len);
938 }
939 
940 /*******************************************************************************
941 **
942 ** Function:    GetNxpByteArrayValue()
943 **
944 ** Description: Read byte array value from the config file.
945 **
946 ** Parameters:
947 **              name    - name of the config param to read.
948 **              pValue  - pointer to input buffer.
949 **              bufflen - input buffer length.
950 **              len     - out parameter to return the number of bytes read from config file,
951 **                        return -1 in case bufflen is not enough.
952 **
953 ** Returns:     TRUE[1] if config param name is found in the config file, else FALSE[0]
954 **
955 *******************************************************************************/
GetNxpConfigByteArrayValue(const char * name,char * pValue,long bufflen,long * len)956 extern "C" int GetNxpConfigByteArrayValue(const char* name, char* pValue,long bufflen, long *len)
957 {
958     DLOG_IF(INFO, uwb_debug_enabled) << StringPrintf("GetNxpConfigByteArrayValue enter....");
959     CUwbNxpConfig& rConfig = CUwbNxpConfig::GetInstance();
960     rConfig.readNxpConfig(default_nxp_config_path);
961     DLOG_IF(INFO, uwb_debug_enabled) << StringPrintf("GetNxpConfigByteArrayValue1 enter....");
962     return rConfig.getValue(name, pValue, bufflen,len);
963 }
964 
965 /*******************************************************************************
966 **
967 ** Function:    GetNumValue
968 **
969 ** Description: API function for getting a numerical value of a setting
970 **
971 ** Returns:     true, if successful
972 **
973 *******************************************************************************/
GetNxpConfigNumValue(const char * name,void * pValue,unsigned long len)974 extern "C" int GetNxpConfigNumValue(const char* name, void* pValue, unsigned long len)
975 {
976     DLOG_IF(INFO, true) << StringPrintf("GetNxpConfigNumValue... enter....");
977     if (pValue == NULL){
978         return false;
979     }
980     CUwbNxpConfig& rConfig = CUwbNxpConfig::GetInstance();
981     const uwbParam* pParam = rConfig.find(name);
982 
983     if (pParam == NULL)
984         return false;
985     unsigned long v = pParam->numValue();
986     unsigned int strLen = (unsigned int)pParam->str_len();
987     if (v == 0 && strLen > 0 && strLen < 4)
988     {
989         const unsigned char* p = (const unsigned char*)pParam->str_value();
990         for (unsigned int i = 0 ; i < strLen; ++i)
991         {
992             v *= 256;
993             v += *p++;
994         }
995     }
996 
997     switch (len)
998     {
999     case sizeof(unsigned long):
1000         *(static_cast<unsigned long*>(pValue)) = (unsigned long)v;
1001         break;
1002     case sizeof(unsigned short):
1003         *(static_cast<unsigned short*>(pValue)) = (unsigned short)v;
1004         break;
1005     case sizeof(unsigned char):
1006         *(static_cast<unsigned char*> (pValue)) = (unsigned char)v;
1007         break;
1008     default:
1009     DLOG_IF(INFO, true) << StringPrintf("GetNxpConfigNumValue default");
1010         return false;
1011     }
1012     return true;
1013 }