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_MAJOR_MINOR, - overwrite major, minor class
174 ** BTA_UTL_SET_COD_SERVICE_CLASS - set the bits in the input
175 ** BTA_UTL_CLR_COD_SERVICE_CLASS - clear the bits in the input
176 ** BTA_UTL_SET_COD_ALL - overwrite major, minor, set the bits in service class
177 ** BTA_UTL_INIT_COD - overwrite major, minor, and service class
178 **
179 ** Returns TRUE if successful, Otherwise FALSE
180 **
181 *******************************************************************************/
utl_set_device_class(tBTA_UTL_COD * p_cod,UINT8 cmd)182 BOOLEAN utl_set_device_class(tBTA_UTL_COD *p_cod, UINT8 cmd)
183 {
184 UINT8 *dev;
185 UINT16 service;
186 UINT8 minor, major;
187 DEV_CLASS dev_class;
188
189 dev = BTM_ReadDeviceClass();
190 BTM_COD_SERVICE_CLASS( service, dev );
191 BTM_COD_MINOR_CLASS(minor, dev );
192 BTM_COD_MAJOR_CLASS(major, dev );
193
194 switch (cmd) {
195 case BTA_UTL_SET_COD_MAJOR_MINOR:
196 minor = p_cod->minor & BTM_COD_MINOR_CLASS_MASK;
197 major = p_cod->major & BTM_COD_MAJOR_CLASS_MASK;
198 break;
199
200 case BTA_UTL_SET_COD_SERVICE_CLASS:
201 /* clear out the bits that is not SERVICE_CLASS bits */
202 p_cod->service &= BTM_COD_SERVICE_CLASS_MASK;
203 service = service | p_cod->service;
204 break;
205
206 case BTA_UTL_CLR_COD_SERVICE_CLASS:
207 p_cod->service &= BTM_COD_SERVICE_CLASS_MASK;
208 service = service & (~p_cod->service);
209 break;
210
211 case BTA_UTL_SET_COD_ALL:
212 minor = p_cod->minor & BTM_COD_MINOR_CLASS_MASK;
213 major = p_cod->major & BTM_COD_MAJOR_CLASS_MASK;
214 p_cod->service &= BTM_COD_SERVICE_CLASS_MASK;
215 service = service | p_cod->service;
216 break;
217
218 case BTA_UTL_INIT_COD:
219 minor = p_cod->minor & BTM_COD_MINOR_CLASS_MASK;
220 major = p_cod->major & BTM_COD_MAJOR_CLASS_MASK;
221 service = p_cod->service & BTM_COD_SERVICE_CLASS_MASK;
222 break;
223
224 default:
225 return FALSE;
226 }
227
228 /* convert the fields into the device class type */
229 FIELDS_TO_COD(dev_class, minor, major, service);
230
231 if (BTM_SetDeviceClass(dev_class) == BTM_SUCCESS) {
232 return TRUE;
233 }
234
235 return FALSE;
236 }
237
238 /*******************************************************************************
239 **
240 ** Function utl_get_device_class
241 **
242 ** Description This function get the local Device Class.
243 **
244 ** Parameters:
245 ** p_cod - Pointer to the device class to get to
246 **
247 **
248 ** Returns TRUE if successful, Otherwise FALSE
249 **
250 *******************************************************************************/
utl_get_device_class(tBTA_UTL_COD * p_cod)251 BOOLEAN utl_get_device_class(tBTA_UTL_COD *p_cod)
252 {
253 UINT8 *dev;
254 UINT16 service;
255 UINT8 minor, major;
256
257 dev = BTM_ReadDeviceClass();
258 BTM_COD_SERVICE_CLASS( service, dev );
259 BTM_COD_MINOR_CLASS(minor, dev );
260 BTM_COD_MAJOR_CLASS(major, dev );
261
262 p_cod->minor = minor;
263 p_cod->major = major;
264 p_cod->service = service;
265
266 return TRUE;
267 }
268
269 /*******************************************************************************
270 **
271 ** Function utl_isintstr
272 **
273 ** Description This utility function checks if the given string is an
274 ** integer string or not
275 **
276 **
277 ** Returns TRUE if successful, Otherwise FALSE
278 **
279 *******************************************************************************/
utl_isintstr(const char * p_s)280 BOOLEAN utl_isintstr(const char *p_s)
281 {
282 UINT16 i = 0;
283
284 for (i = 0; p_s[i] != 0; i++) {
285 if (((p_s[i] < '0') || (p_s[i] > '9')) && (p_s[i] != ';')) {
286 return FALSE;
287 }
288 }
289
290 return TRUE;
291 }
292
293 /*******************************************************************************
294 **
295 ** Function utl_isdialstr
296 **
297 ** Description This utility function checks if the given string contains
298 ** only dial digits or not
299 **
300 **
301 ** Returns TRUE if successful, Otherwise FALSE
302 **
303 *******************************************************************************/
utl_isdialstr(const char * p_s)304 BOOLEAN utl_isdialstr(const char *p_s)
305 {
306 UINT16 i = 0;
307
308 for (i = 0; p_s[i] != 0; i++) {
309 if (!(((p_s[i] >= '0') && (p_s[i] <= '9'))
310 || (p_s[i] == '*') || (p_s[i] == '+') || (p_s[i] == '#') || (p_s[i] == ';')
311 || ((p_s[i] >= 'A') && (p_s[i] <= 'C'))
312 || ((p_s[i] == 'p') || (p_s[i] == 'P')
313 || (p_s[i] == 'w') || (p_s[i] == 'W')))) {
314 return FALSE;
315 }
316 }
317
318 return TRUE;
319 }
320