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