blob: 7feda3371153f6e7eca1e7e67ac1b5619657b75e [file] [log] [blame]
The Android Open Source Project5738f832012-12-12 16:00:35 -08001/******************************************************************************
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 ******************************************************************************/
Scott James Remnant933926c2015-04-02 15:22:14 -070024#include <stddef.h>
Myles Watsonf355ef52016-11-09 13:04:33 -080025
Pavlin Radoslavov258c2532015-09-27 20:59:05 -070026#include "bt_common.h"
The Android Open Source Project5738f832012-12-12 16:00:35 -080027#include "btm_api.h"
Myles Watsoncd1fd072016-11-09 13:17:43 -080028#include "utl.h"
The Android Open Source Project5738f832012-12-12 16:00:35 -080029
30/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -080031 *
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 ******************************************************************************/
Myles Watsoncd1fd072016-11-09 13:17:43 -080043int16_t utl_str2int(const char* p_s) {
44 int32_t val = 0;
The Android Open Source Project5738f832012-12-12 16:00:35 -080045
Myles Watsoncd1fd072016-11-09 13:17:43 -080046 for (; *p_s == ' ' && *p_s != 0; p_s++)
47 ;
The Android Open Source Project5738f832012-12-12 16:00:35 -080048
Myles Watsoncd1fd072016-11-09 13:17:43 -080049 if (*p_s == 0) return -1;
The Android Open Source Project5738f832012-12-12 16:00:35 -080050
Myles Watsoncd1fd072016-11-09 13:17:43 -080051 for (;;) {
52 if ((*p_s < '0') || (*p_s > '9')) return -1;
The Android Open Source Project5738f832012-12-12 16:00:35 -080053
Myles Watsoncd1fd072016-11-09 13:17:43 -080054 val += (int32_t)(*p_s++ - '0');
The Android Open Source Project5738f832012-12-12 16:00:35 -080055
Myles Watsoncd1fd072016-11-09 13:17:43 -080056 if (val > 32767) return -1;
The Android Open Source Project5738f832012-12-12 16:00:35 -080057
Myles Watsoncd1fd072016-11-09 13:17:43 -080058 if (*p_s == 0) {
59 return (int16_t)val;
60 } else {
61 val *= 10;
The Android Open Source Project5738f832012-12-12 16:00:35 -080062 }
Myles Watsoncd1fd072016-11-09 13:17:43 -080063 }
The Android Open Source Project5738f832012-12-12 16:00:35 -080064}
65
66/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -080067 *
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 ******************************************************************************/
Myles Watsoncd1fd072016-11-09 13:17:43 -080079int utl_strucmp(const char* p_s, const char* p_t) {
80 char c;
The Android Open Source Project5738f832012-12-12 16:00:35 -080081
Myles Watsoncd1fd072016-11-09 13:17:43 -080082 while (*p_s && *p_t) {
83 c = *p_t++;
84 if (c >= 'a' && c <= 'z') {
85 c -= 0x20;
The Android Open Source Project5738f832012-12-12 16:00:35 -080086 }
Myles Watsoncd1fd072016-11-09 13:17:43 -080087 if (*p_s++ != c) {
88 return -1;
The Android Open Source Project5738f832012-12-12 16:00:35 -080089 }
Myles Watsoncd1fd072016-11-09 13:17:43 -080090 }
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 }
The Android Open Source Project5738f832012-12-12 16:00:35 -080099}
100
101/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -0800102 *
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 ******************************************************************************/
Myles Watsoncd1fd072016-11-09 13:17:43 -0800113uint8_t utl_itoa(uint16_t i, char* p_s) {
114 uint16_t j, k;
115 char* p = p_s;
116 bool fill = false;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800117
Myles Watsoncd1fd072016-11-09 13:17:43 -0800118 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 }
The Android Open Source Project5738f832012-12-12 16:00:35 -0800129 }
Myles Watsoncd1fd072016-11-09 13:17:43 -0800130 }
131 *p = 0;
132 return (uint8_t)(p - p_s);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800133}
134
135/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -0800136 *
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.
Myles Watsoncd1fd072016-11-09 13:17:43 -0800145 * BTA_UTL_SET_COD_MAJOR_MINOR, - overwrite major,
Myles Watson1baaae32016-11-09 14:25:23 -0800146 * minor class
Myles Watsoncd1fd072016-11-09 13:17:43 -0800147 * BTA_UTL_SET_COD_SERVICE_CLASS - set the bits in
Myles Watson1baaae32016-11-09 14:25:23 -0800148 * the input
Myles Watsoncd1fd072016-11-09 13:17:43 -0800149 * BTA_UTL_CLR_COD_SERVICE_CLASS - clear the bits in
Myles Watson1baaae32016-11-09 14:25:23 -0800150 * the input
Myles Watsoncd1fd072016-11-09 13:17:43 -0800151 * BTA_UTL_SET_COD_ALL - overwrite major, minor, set
Myles Watson1baaae32016-11-09 14:25:23 -0800152 * the bits in service class
Myles Watsoncd1fd072016-11-09 13:17:43 -0800153 * BTA_UTL_INIT_COD - overwrite major, minor, and
Myles Watson1baaae32016-11-09 14:25:23 -0800154 * service class
Myles Watson8af480e2016-11-09 10:40:23 -0800155 *
156 * Returns true if successful, Otherwise false
157 *
158 ******************************************************************************/
Myles Watsoncd1fd072016-11-09 13:17:43 -0800159bool 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;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800164
Myles Watsoncd1fd072016-11-09 13:17:43 -0800165 dev = BTM_ReadDeviceClass();
166 BTM_COD_SERVICE_CLASS(service, dev);
167 BTM_COD_MINOR_CLASS(minor, dev);
168 BTM_COD_MAJOR_CLASS(major, dev);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800169
Myles Watsoncd1fd072016-11-09 13:17:43 -0800170 switch (cmd) {
The Android Open Source Project5738f832012-12-12 16:00:35 -0800171 case BTA_UTL_SET_COD_MAJOR_MINOR:
Myles Watsoncd1fd072016-11-09 13:17:43 -0800172 minor = p_cod->minor & BTM_COD_MINOR_CLASS_MASK;
173 major = p_cod->major & BTM_COD_MAJOR_CLASS_MASK;
174 break;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800175
176 case BTA_UTL_SET_COD_SERVICE_CLASS:
Myles Watsoncd1fd072016-11-09 13:17:43 -0800177 /* 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;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800181
182 case BTA_UTL_CLR_COD_SERVICE_CLASS:
Myles Watsoncd1fd072016-11-09 13:17:43 -0800183 p_cod->service &= BTM_COD_SERVICE_CLASS_MASK;
184 service = service & (~p_cod->service);
185 break;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800186
187 case BTA_UTL_SET_COD_ALL:
Myles Watsoncd1fd072016-11-09 13:17:43 -0800188 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;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800193
194 case BTA_UTL_INIT_COD:
Myles Watsoncd1fd072016-11-09 13:17:43 -0800195 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;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800199
200 default:
Myles Watsoncd1fd072016-11-09 13:17:43 -0800201 return false;
202 }
The Android Open Source Project5738f832012-12-12 16:00:35 -0800203
Myles Watsoncd1fd072016-11-09 13:17:43 -0800204 /* convert the fields into the device class type */
205 FIELDS_TO_COD(dev_class, minor, major, service);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800206
Myles Watsoncd1fd072016-11-09 13:17:43 -0800207 if (BTM_SetDeviceClass(dev_class) == BTM_SUCCESS) return true;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800208
Myles Watsoncd1fd072016-11-09 13:17:43 -0800209 return false;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800210}
211
212/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -0800213 *
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 ******************************************************************************/
Myles Watsoncd1fd072016-11-09 13:17:43 -0800223bool utl_isintstr(const char* p_s) {
224 uint16_t i = 0;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800225
Myles Watsoncd1fd072016-11-09 13:17:43 -0800226 for (i = 0; p_s[i] != 0; i++) {
227 if (((p_s[i] < '0') || (p_s[i] > '9')) && (p_s[i] != ';')) return false;
228 }
The Android Open Source Project5738f832012-12-12 16:00:35 -0800229
Myles Watsoncd1fd072016-11-09 13:17:43 -0800230 return true;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800231}
232
233/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -0800234 *
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 ******************************************************************************/
Myles Watsoncd1fd072016-11-09 13:17:43 -0800243bool 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')));
Satish Kodishalac75a71d2016-06-22 14:22:41 +0530247}
248
249/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -0800250 *
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 ******************************************************************************/
Myles Watsoncd1fd072016-11-09 13:17:43 -0800260bool 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;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800266}