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