blob: 61b8d437aa1399e38f22201b14c6512856d7e4cc [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>
The Android Open Source Project5738f832012-12-12 16:00:35 -080025#include "utl.h"
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"
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*******************************************************************************/
Marie Janssene9e58ce2016-06-17 14:12:17 -070042int16_t utl_str2int(const char *p_s)
The Android Open Source Project5738f832012-12-12 16:00:35 -080043{
Marie Janssene9e58ce2016-06-17 14:12:17 -070044 int32_t val = 0;
The Android Open Source Project5738f832012-12-12 16:00:35 -080045
46 for (;*p_s == ' ' && *p_s != 0; p_s++);
47
48 if (*p_s == 0) return -1;
49
50 for (;;)
51 {
52 if ((*p_s < '0') || (*p_s > '9')) return -1;
53
Marie Janssene9e58ce2016-06-17 14:12:17 -070054 val += (int32_t) (*p_s++ - '0');
The Android Open Source Project5738f832012-12-12 16:00:35 -080055
56 if (val > 32767) return -1;
57
58 if (*p_s == 0)
59 {
Marie Janssene9e58ce2016-06-17 14:12:17 -070060 return (int16_t) val;
The Android Open Source Project5738f832012-12-12 16:00:35 -080061 }
62 else
63 {
64 val *= 10;
65 }
66 }
67}
68
69/*******************************************************************************
70**
71** Function utl_strucmp
72**
73** Description This utility function compares two strings in uppercase.
74** String p_s must be uppercase. String p_t is converted to
75** uppercase if lowercase. If p_s ends first, the substring
76** match is counted as a match.
77**
78**
79** Returns 0 if strings match, nonzero otherwise.
80**
81*******************************************************************************/
82int utl_strucmp(const char *p_s, const char *p_t)
83{
84 char c;
85
86 while (*p_s && *p_t)
87 {
88 c = *p_t++;
89 if (c >= 'a' && c <= 'z')
90 {
91 c -= 0x20;
92 }
93 if (*p_s++ != c)
94 {
95 return -1;
96 }
97 }
98 /* if p_t hit null first, no match */
99 if (*p_t == 0 && *p_s != 0)
100 {
101 return 1;
102 }
103 /* else p_s hit null first, count as match */
104 else
105 {
106 return 0;
107 }
108}
109
110/*******************************************************************************
111**
112** Function utl_itoa
113**
Marie Janssene9e58ce2016-06-17 14:12:17 -0700114** Description This utility function converts a uint16_t to a string. The
The Android Open Source Project5738f832012-12-12 16:00:35 -0800115** string is NULL-terminated. The length of the string is
116** returned;
117**
118**
119** Returns Length of string.
120**
121*******************************************************************************/
Marie Janssene9e58ce2016-06-17 14:12:17 -0700122uint8_t utl_itoa(uint16_t i, char *p_s)
The Android Open Source Project5738f832012-12-12 16:00:35 -0800123{
Marie Janssene9e58ce2016-06-17 14:12:17 -0700124 uint16_t j, k;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800125 char *p = p_s;
Marie Janssene9e58ce2016-06-17 14:12:17 -0700126 bool fill = false;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800127
128 if (i == 0)
129 {
130 /* take care of zero case */
131 *p++ = '0';
132 }
133 else
134 {
135 for(j = 10000; j > 0; j /= 10)
136 {
137 k = i / j;
138 i %= j;
139 if (k > 0 || fill)
140 {
141 *p++ = k + '0';
Marie Janssene9e58ce2016-06-17 14:12:17 -0700142 fill = true;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800143 }
144 }
145 }
146 *p = 0;
Marie Janssene9e58ce2016-06-17 14:12:17 -0700147 return (uint8_t) (p - p_s);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800148}
149
150/*******************************************************************************
151**
The Android Open Source Project5738f832012-12-12 16:00:35 -0800152** Function utl_set_device_class
153**
154** Description This function updates the local Device Class.
155**
156** Parameters:
157** p_cod - Pointer to the device class to set to
158**
159** cmd - the fields of the device class to update.
160** BTA_UTL_SET_COD_MAJOR_MINOR, - overwrite major, minor class
161** BTA_UTL_SET_COD_SERVICE_CLASS - set the bits in the input
162** BTA_UTL_CLR_COD_SERVICE_CLASS - clear the bits in the input
163** BTA_UTL_SET_COD_ALL - overwrite major, minor, set the bits in service class
164** BTA_UTL_INIT_COD - overwrite major, minor, and service class
165**
Marie Janssene9e58ce2016-06-17 14:12:17 -0700166** Returns true if successful, Otherwise false
The Android Open Source Project5738f832012-12-12 16:00:35 -0800167**
168*******************************************************************************/
Marie Janssene9e58ce2016-06-17 14:12:17 -0700169bool utl_set_device_class(tBTA_UTL_COD *p_cod, uint8_t cmd)
The Android Open Source Project5738f832012-12-12 16:00:35 -0800170{
Marie Janssene9e58ce2016-06-17 14:12:17 -0700171 uint8_t *dev;
172 uint16_t service;
173 uint8_t minor, major;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800174 DEV_CLASS dev_class;
175
176 dev = BTM_ReadDeviceClass();
177 BTM_COD_SERVICE_CLASS( service, dev );
178 BTM_COD_MINOR_CLASS(minor, dev );
179 BTM_COD_MAJOR_CLASS(major, dev );
180
181 switch(cmd)
182 {
183 case BTA_UTL_SET_COD_MAJOR_MINOR:
184 minor = p_cod->minor & BTM_COD_MINOR_CLASS_MASK;
185 major = p_cod->major & BTM_COD_MAJOR_CLASS_MASK;
186 break;
187
188 case BTA_UTL_SET_COD_SERVICE_CLASS:
189 /* clear out the bits that is not SERVICE_CLASS bits */
190 p_cod->service &= BTM_COD_SERVICE_CLASS_MASK;
191 service = service | p_cod->service;
192 break;
193
194 case BTA_UTL_CLR_COD_SERVICE_CLASS:
195 p_cod->service &= BTM_COD_SERVICE_CLASS_MASK;
196 service = service & (~p_cod->service);
197 break;
198
199 case BTA_UTL_SET_COD_ALL:
200 minor = p_cod->minor & BTM_COD_MINOR_CLASS_MASK;
201 major = p_cod->major & BTM_COD_MAJOR_CLASS_MASK;
202 p_cod->service &= BTM_COD_SERVICE_CLASS_MASK;
203 service = service | p_cod->service;
204 break;
205
206 case BTA_UTL_INIT_COD:
207 minor = p_cod->minor & BTM_COD_MINOR_CLASS_MASK;
208 major = p_cod->major & BTM_COD_MAJOR_CLASS_MASK;
209 service = p_cod->service & BTM_COD_SERVICE_CLASS_MASK;
210 break;
211
212 default:
Marie Janssene9e58ce2016-06-17 14:12:17 -0700213 return false;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800214 }
215
216 /* convert the fields into the device class type */
217 FIELDS_TO_COD(dev_class, minor, major, service);
218
219 if (BTM_SetDeviceClass(dev_class) == BTM_SUCCESS)
Marie Janssene9e58ce2016-06-17 14:12:17 -0700220 return true;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800221
Marie Janssene9e58ce2016-06-17 14:12:17 -0700222 return false;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800223}
224
225/*******************************************************************************
226**
227** Function utl_isintstr
228**
229** Description This utility function checks if the given string is an
230** integer string or not
231**
232**
Marie Janssene9e58ce2016-06-17 14:12:17 -0700233** Returns true if successful, Otherwise false
The Android Open Source Project5738f832012-12-12 16:00:35 -0800234**
235*******************************************************************************/
Marie Janssene9e58ce2016-06-17 14:12:17 -0700236bool utl_isintstr(const char *p_s)
The Android Open Source Project5738f832012-12-12 16:00:35 -0800237{
Marie Janssene9e58ce2016-06-17 14:12:17 -0700238 uint16_t i = 0;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800239
240 for(i=0; p_s[i] != 0; i++)
241 {
242 if(((p_s[i] < '0') || (p_s[i] > '9')) && (p_s[i] != ';'))
Marie Janssene9e58ce2016-06-17 14:12:17 -0700243 return false;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800244 }
245
Marie Janssene9e58ce2016-06-17 14:12:17 -0700246 return true;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800247}
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**
Marie Janssene9e58ce2016-06-17 14:12:17 -0700257** Returns true if successful, Otherwise false
The Android Open Source Project5738f832012-12-12 16:00:35 -0800258**
259*******************************************************************************/
Marie Janssene9e58ce2016-06-17 14:12:17 -0700260bool utl_isdialstr(const char *p_s)
The Android Open Source Project5738f832012-12-12 16:00:35 -0800261{
Marie Janssene9e58ce2016-06-17 14:12:17 -0700262 uint16_t i = 0;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800263
264 for(i=0; p_s[i] != 0; i++)
265 {
266 if(!(((p_s[i] >= '0') && (p_s[i] <= '9'))
267 || (p_s[i] == '*') || (p_s[i] == '+') || (p_s[i] == '#') || (p_s[i] == ';')
Mallikarjuna GBf9a00142015-05-21 19:17:06 +0530268 || ((p_s[i] >= 'A') && (p_s[i] <= 'C'))
269 || ((p_s[i] == 'p') || (p_s[i] == 'P')
270 || (p_s[i] == 'w') || (p_s[i] == 'W'))))
Marie Janssene9e58ce2016-06-17 14:12:17 -0700271 return false;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800272 }
273
Marie Janssene9e58ce2016-06-17 14:12:17 -0700274 return true;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800275}
276
277