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