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