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