1 /******************************************************************************
2  *
3  *  Copyright 2003-2012 Broadcom Corporation
4  *
5  *  Licensed under the Apache License, Version 2.0 (the "License");
6  *  you may not use this file except in compliance with the License.
7  *  You may obtain a copy of the License at:
8  *
9  *  http://www.apache.org/licenses/LICENSE-2.0
10  *
11  *  Unless required by applicable law or agreed to in writing, software
12  *  distributed under the License is distributed on an "AS IS" BASIS,
13  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  *  See the License for the specific language governing permissions and
15  *  limitations under the License.
16  *
17  ******************************************************************************/
18 
19 /******************************************************************************
20  *
21  *  This file contains utility functions.
22  *
23  ******************************************************************************/
24 #include <cstdint>
25 
26 #include "bt_target.h"  // Must be first to define build configuration
27 
28 #include "bta/include/utl.h"
29 #include "stack/include/btm_api.h"
30 
31 /*******************************************************************************
32  *
33  * Function         utl_str2int
34  *
35  * Description      This utility function converts a character string to an
36  *                  integer.  Acceptable values in string are 0-9.  If invalid
37  *                  string or string value too large, -1 is returned.  Leading
38  *                  spaces are skipped.
39  *
40  *
41  * Returns          Integer value or -1 on error.
42  *
43  ******************************************************************************/
utl_str2int(const char * p_s)44 int16_t utl_str2int(const char* p_s) {
45   int32_t val = 0;
46 
47   for (; *p_s == ' ' && *p_s != 0; p_s++)
48     ;
49 
50   if (*p_s == 0) return -1;
51 
52   for (;;) {
53     if ((*p_s < '0') || (*p_s > '9')) return -1;
54 
55     val += (int32_t)(*p_s++ - '0');
56 
57     if (val > 32767) return -1;
58 
59     if (*p_s == 0) {
60       return (int16_t)val;
61     } else {
62       val *= 10;
63     }
64   }
65 }
66 
67 /*******************************************************************************
68  *
69  * Function         utl_strucmp
70  *
71  * Description      This utility function compares two strings in uppercase.
72  *                  String p_s must be uppercase.  String p_t is converted to
73  *                  uppercase if lowercase.  If p_s ends first, the substring
74  *                  match is counted as a match.
75  *
76  *
77  * Returns          0 if strings match, nonzero otherwise.
78  *
79  ******************************************************************************/
utl_strucmp(const char * p_s,const char * p_t)80 int utl_strucmp(const char* p_s, const char* p_t) {
81   char c;
82 
83   while (*p_s && *p_t) {
84     c = *p_t++;
85     if (c >= 'a' && c <= 'z') {
86       c -= 0x20;
87     }
88     if (*p_s++ != c) {
89       return -1;
90     }
91   }
92   /* if p_t hit null first, no match */
93   if (*p_t == 0 && *p_s != 0) {
94     return 1;
95   }
96   /* else p_s hit null first, count as match */
97   else {
98     return 0;
99   }
100 }
101 
102 /*******************************************************************************
103  *
104  * Function         utl_itoa
105  *
106  * Description      This utility function converts a uint16_t to a string.  The
107  *                  string is NULL-terminated.  The length of the string is
108  *                  returned;
109  *
110  *
111  * Returns          Length of string.
112  *
113  ******************************************************************************/
utl_itoa(uint16_t i,char * p_s)114 uint8_t utl_itoa(uint16_t i, char* p_s) {
115   uint16_t j, k;
116   char* p = p_s;
117   bool fill = false;
118 
119   if (i == 0) {
120     /* take care of zero case */
121     *p++ = '0';
122   } else {
123     for (j = 10000; j > 0; j /= 10) {
124       k = i / j;
125       i %= j;
126       if (k > 0 || fill) {
127         *p++ = k + '0';
128         fill = true;
129       }
130     }
131   }
132   *p = 0;
133   return (uint8_t)(p - p_s);
134 }
135 
136 /*******************************************************************************
137  *
138  * Function         utl_set_device_class
139  *
140  * Description      This function updates the local Device Class.
141  *
142  * Parameters:
143  *                  p_cod   - Pointer to the device class to set to
144  *
145  *                  cmd     - the fields of the device class to update.
146  *                            BTA_UTL_SET_COD_MAJOR_MINOR, - overwrite major,
147  *                                                           minor class
148  *                            BTA_UTL_SET_COD_SERVICE_CLASS - set the bits in
149  *                                                            the input
150  *                            BTA_UTL_CLR_COD_SERVICE_CLASS - clear the bits in
151  *                                                            the input
152  *                            BTA_UTL_SET_COD_ALL - overwrite major, minor, set
153  *                                                  the bits in service class
154  *                            BTA_UTL_INIT_COD - overwrite major, minor, and
155  *                                               service class
156  *
157  * Returns          true if successful, Otherwise false
158  *
159  ******************************************************************************/
utl_set_device_class(tBTA_UTL_COD * p_cod,uint8_t cmd)160 bool utl_set_device_class(tBTA_UTL_COD* p_cod, uint8_t cmd) {
161   uint8_t* dev;
162   uint16_t service;
163   uint8_t minor, major;
164   DEV_CLASS dev_class;
165 
166   dev = BTM_ReadDeviceClass();
167   BTM_COD_SERVICE_CLASS(service, dev);
168   BTM_COD_MINOR_CLASS(minor, dev);
169   BTM_COD_MAJOR_CLASS(major, dev);
170 
171   switch (cmd) {
172     case BTA_UTL_SET_COD_MAJOR_MINOR:
173       minor = p_cod->minor & BTM_COD_MINOR_CLASS_MASK;
174       major = p_cod->major & BTM_COD_MAJOR_CLASS_MASK;
175       break;
176 
177     case BTA_UTL_SET_COD_SERVICE_CLASS:
178       /* clear out the bits that is not SERVICE_CLASS bits */
179       p_cod->service &= BTM_COD_SERVICE_CLASS_MASK;
180       service = service | p_cod->service;
181       break;
182 
183     case BTA_UTL_CLR_COD_SERVICE_CLASS:
184       p_cod->service &= BTM_COD_SERVICE_CLASS_MASK;
185       service = service & (~p_cod->service);
186       break;
187 
188     case BTA_UTL_SET_COD_ALL:
189       minor = p_cod->minor & BTM_COD_MINOR_CLASS_MASK;
190       major = p_cod->major & BTM_COD_MAJOR_CLASS_MASK;
191       p_cod->service &= BTM_COD_SERVICE_CLASS_MASK;
192       service = service | p_cod->service;
193       break;
194 
195     case BTA_UTL_INIT_COD:
196       minor = p_cod->minor & BTM_COD_MINOR_CLASS_MASK;
197       major = p_cod->major & BTM_COD_MAJOR_CLASS_MASK;
198       service = p_cod->service & BTM_COD_SERVICE_CLASS_MASK;
199       break;
200 
201     default:
202       return false;
203   }
204 
205   /* convert the fields into the device class type */
206   FIELDS_TO_COD(dev_class, minor, major, service);
207 
208   if (BTM_SetDeviceClass(dev_class) == BTM_SUCCESS) return true;
209 
210   return false;
211 }
212 
213 /*******************************************************************************
214  *
215  * Function         utl_isintstr
216  *
217  * Description      This utility function checks if the given string is an
218  *                  integer string or not
219  *
220  *
221  * Returns          true if successful, Otherwise false
222  *
223  ******************************************************************************/
utl_isintstr(const char * p_s)224 bool utl_isintstr(const char* p_s) {
225   uint16_t i = 0;
226 
227   for (i = 0; p_s[i] != 0; i++) {
228     if (((p_s[i] < '0') || (p_s[i] > '9')) && (p_s[i] != ';')) return false;
229   }
230 
231   return true;
232 }
233 
234 /*******************************************************************************
235  *
236  * Function         utl_isdialchar
237  *
238  * Description      This utility function checks if the given character
239  *                  is an acceptable dial digit
240  *
241  * Returns          true if successful, Otherwise false
242  *
243  ******************************************************************************/
utl_isdialchar(const char d)244 bool utl_isdialchar(const char d) {
245   return (((d >= '0') && (d <= '9')) || (d == '*') || (d == '+') ||
246           (d == '#') || (d == ';') || (d == ',') ||
247           ((d >= 'A') && (d <= 'C')) ||
248           ((d == 'p') || (d == 'P') || (d == 'w') || (d == 'W')));
249 }
250 
251 /*******************************************************************************
252  *
253  * Function         utl_isdialstr
254  *
255  * Description      This utility function checks if the given string contains
256  *                  only dial digits or not
257  *
258  *
259  * Returns          true if successful, Otherwise false
260  *
261  ******************************************************************************/
utl_isdialstr(const char * p_s)262 bool utl_isdialstr(const char* p_s) {
263   for (uint16_t i = 0; p_s[i] != 0; i++) {
264     // include chars not in spec that work sent by some headsets.
265     if (!(utl_isdialchar(p_s[i]) || (p_s[i] == '-'))) return false;
266   }
267   return true;
268 }
269