• 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 #include "utl.h"
26 #include "bt_common.h"
27 #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 *******************************************************************************/
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) return -1;
49 
50     for (;;)
51     {
52         if ((*p_s < '0') || (*p_s > '9')) return -1;
53 
54         val += (INT32) (*p_s++ - '0');
55 
56         if (val > 32767) return -1;
57 
58         if (*p_s == 0)
59         {
60             return (INT16) val;
61         }
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 *******************************************************************************/
utl_strucmp(const char * p_s,const char * p_t)82 int 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 **
114 ** Description      This utility function converts a UINT16 to a string.  The
115 **                  string is NULL-terminated.  The length of the string is
116 **                  returned;
117 **
118 **
119 ** Returns          Length of string.
120 **
121 *******************************************************************************/
utl_itoa(UINT16 i,char * p_s)122 UINT8 utl_itoa(UINT16 i, char *p_s)
123 {
124     UINT16  j, k;
125     char    *p = p_s;
126     BOOLEAN fill = FALSE;
127 
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';
142               fill = TRUE;
143             }
144         }
145     }
146     *p = 0;
147     return (UINT8) (p - p_s);
148 }
149 
150 /*******************************************************************************
151 **
152 ** 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 **
166 ** Returns          TRUE if successful, Otherwise FALSE
167 **
168 *******************************************************************************/
utl_set_device_class(tBTA_UTL_COD * p_cod,UINT8 cmd)169 BOOLEAN utl_set_device_class(tBTA_UTL_COD *p_cod, UINT8 cmd)
170 {
171     UINT8 *dev;
172     UINT16 service;
173     UINT8  minor, major;
174     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:
213         return FALSE;
214     }
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)
220         return TRUE;
221 
222     return FALSE;
223 }
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 **
233 ** Returns          TRUE if successful, Otherwise FALSE
234 **
235 *******************************************************************************/
utl_isintstr(const char * p_s)236 BOOLEAN utl_isintstr(const char *p_s)
237 {
238     UINT16 i = 0;
239 
240     for(i=0; p_s[i] != 0; i++)
241     {
242         if(((p_s[i] < '0') || (p_s[i] > '9')) && (p_s[i] != ';'))
243             return FALSE;
244     }
245 
246     return TRUE;
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 BOOLEAN utl_isdialstr(const char *p_s)
261 {
262     UINT16 i = 0;
263 
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] == ';')
268             || ((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'))))
271             return FALSE;
272     }
273 
274     return TRUE;
275 }
276 
277 
278