The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 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 | ******************************************************************************/ |
Scott James Remnant | 933926c | 2015-04-02 15:22:14 -0700 | [diff] [blame] | 24 | #include <stddef.h> |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 25 | #include "utl.h" |
Pavlin Radoslavov | 258c253 | 2015-09-27 20:59:05 -0700 | [diff] [blame] | 26 | #include "bt_common.h" |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 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 | ** |
Sumit Bajpai | 840cf9b | 2015-01-09 14:49:00 +0530 | [diff] [blame] | 71 | ** Function utl_str2int32 |
| 72 | ** |
| 73 | ** Description This utility function converts a character string to an |
| 74 | ** int32. Acceptable values in string are 0-9. If invalid |
| 75 | ** string or string value too large, -1 is returned. Leading |
| 76 | ** spaces are skipped. |
| 77 | ** |
| 78 | ** |
| 79 | ** Returns int32_t value or -1 on error. |
| 80 | ** |
| 81 | *******************************************************************************/ |
| 82 | INT32 utl_str2int32(const char *p_s) |
| 83 | { |
| 84 | INT32 val = 0; |
| 85 | |
| 86 | for (;*p_s == ' ' && *p_s != 0; p_s++); |
| 87 | |
| 88 | if (*p_s == 0) return -1; |
| 89 | |
| 90 | for (;;) |
| 91 | { |
| 92 | if ((*p_s < '0') || (*p_s > '9')) return -1; |
| 93 | |
| 94 | val += (INT32) (*p_s++ - '0'); |
| 95 | |
| 96 | if (val > 65535) return -1; |
| 97 | |
| 98 | if (*p_s == 0) |
| 99 | { |
| 100 | return val; |
| 101 | } |
| 102 | else |
| 103 | { |
| 104 | val *= 10; |
| 105 | } |
| 106 | } |
| 107 | } |
| 108 | |
| 109 | /******************************************************************************* |
| 110 | ** |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 111 | ** Function utl_strucmp |
| 112 | ** |
| 113 | ** Description This utility function compares two strings in uppercase. |
| 114 | ** String p_s must be uppercase. String p_t is converted to |
| 115 | ** uppercase if lowercase. If p_s ends first, the substring |
| 116 | ** match is counted as a match. |
| 117 | ** |
| 118 | ** |
| 119 | ** Returns 0 if strings match, nonzero otherwise. |
| 120 | ** |
| 121 | *******************************************************************************/ |
| 122 | int utl_strucmp(const char *p_s, const char *p_t) |
| 123 | { |
| 124 | char c; |
| 125 | |
| 126 | while (*p_s && *p_t) |
| 127 | { |
| 128 | c = *p_t++; |
| 129 | if (c >= 'a' && c <= 'z') |
| 130 | { |
| 131 | c -= 0x20; |
| 132 | } |
| 133 | if (*p_s++ != c) |
| 134 | { |
| 135 | return -1; |
| 136 | } |
| 137 | } |
| 138 | /* if p_t hit null first, no match */ |
| 139 | if (*p_t == 0 && *p_s != 0) |
| 140 | { |
| 141 | return 1; |
| 142 | } |
| 143 | /* else p_s hit null first, count as match */ |
| 144 | else |
| 145 | { |
| 146 | return 0; |
| 147 | } |
| 148 | } |
| 149 | |
| 150 | /******************************************************************************* |
| 151 | ** |
| 152 | ** Function utl_itoa |
| 153 | ** |
| 154 | ** Description This utility function converts a UINT16 to a string. The |
| 155 | ** string is NULL-terminated. The length of the string is |
| 156 | ** returned; |
| 157 | ** |
| 158 | ** |
| 159 | ** Returns Length of string. |
| 160 | ** |
| 161 | *******************************************************************************/ |
| 162 | UINT8 utl_itoa(UINT16 i, char *p_s) |
| 163 | { |
| 164 | UINT16 j, k; |
| 165 | char *p = p_s; |
| 166 | BOOLEAN fill = FALSE; |
| 167 | |
| 168 | if (i == 0) |
| 169 | { |
| 170 | /* take care of zero case */ |
| 171 | *p++ = '0'; |
| 172 | } |
| 173 | else |
| 174 | { |
| 175 | for(j = 10000; j > 0; j /= 10) |
| 176 | { |
| 177 | k = i / j; |
| 178 | i %= j; |
| 179 | if (k > 0 || fill) |
| 180 | { |
| 181 | *p++ = k + '0'; |
| 182 | fill = TRUE; |
| 183 | } |
| 184 | } |
| 185 | } |
| 186 | *p = 0; |
| 187 | return (UINT8) (p - p_s); |
| 188 | } |
| 189 | |
| 190 | /******************************************************************************* |
| 191 | ** |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 192 | ** Function utl_set_device_class |
| 193 | ** |
| 194 | ** Description This function updates the local Device Class. |
| 195 | ** |
| 196 | ** Parameters: |
| 197 | ** p_cod - Pointer to the device class to set to |
| 198 | ** |
| 199 | ** cmd - the fields of the device class to update. |
| 200 | ** BTA_UTL_SET_COD_MAJOR_MINOR, - overwrite major, minor class |
| 201 | ** BTA_UTL_SET_COD_SERVICE_CLASS - set the bits in the input |
| 202 | ** BTA_UTL_CLR_COD_SERVICE_CLASS - clear the bits in the input |
| 203 | ** BTA_UTL_SET_COD_ALL - overwrite major, minor, set the bits in service class |
| 204 | ** BTA_UTL_INIT_COD - overwrite major, minor, and service class |
| 205 | ** |
| 206 | ** Returns TRUE if successful, Otherwise FALSE |
| 207 | ** |
| 208 | *******************************************************************************/ |
| 209 | BOOLEAN utl_set_device_class(tBTA_UTL_COD *p_cod, UINT8 cmd) |
| 210 | { |
| 211 | UINT8 *dev; |
| 212 | UINT16 service; |
| 213 | UINT8 minor, major; |
| 214 | DEV_CLASS dev_class; |
| 215 | |
| 216 | dev = BTM_ReadDeviceClass(); |
| 217 | BTM_COD_SERVICE_CLASS( service, dev ); |
| 218 | BTM_COD_MINOR_CLASS(minor, dev ); |
| 219 | BTM_COD_MAJOR_CLASS(major, dev ); |
| 220 | |
| 221 | switch(cmd) |
| 222 | { |
| 223 | case BTA_UTL_SET_COD_MAJOR_MINOR: |
| 224 | minor = p_cod->minor & BTM_COD_MINOR_CLASS_MASK; |
| 225 | major = p_cod->major & BTM_COD_MAJOR_CLASS_MASK; |
| 226 | break; |
| 227 | |
| 228 | case BTA_UTL_SET_COD_SERVICE_CLASS: |
| 229 | /* clear out the bits that is not SERVICE_CLASS bits */ |
| 230 | p_cod->service &= BTM_COD_SERVICE_CLASS_MASK; |
| 231 | service = service | p_cod->service; |
| 232 | break; |
| 233 | |
| 234 | case BTA_UTL_CLR_COD_SERVICE_CLASS: |
| 235 | p_cod->service &= BTM_COD_SERVICE_CLASS_MASK; |
| 236 | service = service & (~p_cod->service); |
| 237 | break; |
| 238 | |
| 239 | case BTA_UTL_SET_COD_ALL: |
| 240 | minor = p_cod->minor & BTM_COD_MINOR_CLASS_MASK; |
| 241 | major = p_cod->major & BTM_COD_MAJOR_CLASS_MASK; |
| 242 | p_cod->service &= BTM_COD_SERVICE_CLASS_MASK; |
| 243 | service = service | p_cod->service; |
| 244 | break; |
| 245 | |
| 246 | case BTA_UTL_INIT_COD: |
| 247 | minor = p_cod->minor & BTM_COD_MINOR_CLASS_MASK; |
| 248 | major = p_cod->major & BTM_COD_MAJOR_CLASS_MASK; |
| 249 | service = p_cod->service & BTM_COD_SERVICE_CLASS_MASK; |
| 250 | break; |
| 251 | |
| 252 | default: |
| 253 | return FALSE; |
| 254 | } |
| 255 | |
| 256 | /* convert the fields into the device class type */ |
| 257 | FIELDS_TO_COD(dev_class, minor, major, service); |
| 258 | |
| 259 | if (BTM_SetDeviceClass(dev_class) == BTM_SUCCESS) |
| 260 | return TRUE; |
| 261 | |
| 262 | return FALSE; |
| 263 | } |
| 264 | |
| 265 | /******************************************************************************* |
| 266 | ** |
| 267 | ** Function utl_isintstr |
| 268 | ** |
| 269 | ** Description This utility function checks if the given string is an |
| 270 | ** integer string or not |
| 271 | ** |
| 272 | ** |
| 273 | ** Returns TRUE if successful, Otherwise FALSE |
| 274 | ** |
| 275 | *******************************************************************************/ |
| 276 | BOOLEAN utl_isintstr(const char *p_s) |
| 277 | { |
| 278 | UINT16 i = 0; |
| 279 | |
| 280 | for(i=0; p_s[i] != 0; i++) |
| 281 | { |
| 282 | if(((p_s[i] < '0') || (p_s[i] > '9')) && (p_s[i] != ';')) |
| 283 | return FALSE; |
| 284 | } |
| 285 | |
| 286 | return TRUE; |
| 287 | } |
| 288 | |
| 289 | /******************************************************************************* |
| 290 | ** |
| 291 | ** Function utl_isdialstr |
| 292 | ** |
| 293 | ** Description This utility function checks if the given string contains |
| 294 | ** only dial digits or not |
| 295 | ** |
| 296 | ** |
| 297 | ** Returns TRUE if successful, Otherwise FALSE |
| 298 | ** |
| 299 | *******************************************************************************/ |
| 300 | BOOLEAN utl_isdialstr(const char *p_s) |
| 301 | { |
| 302 | UINT16 i = 0; |
| 303 | |
| 304 | for(i=0; p_s[i] != 0; i++) |
| 305 | { |
| 306 | if(!(((p_s[i] >= '0') && (p_s[i] <= '9')) |
| 307 | || (p_s[i] == '*') || (p_s[i] == '+') || (p_s[i] == '#') || (p_s[i] == ';') |
Satheesh Kumar Pallemoni | 7d378b6 | 2016-06-21 10:27:13 +0530 | [diff] [blame] | 308 | || (p_s[i] == '-') |
Mallikarjuna GB | f9a0014 | 2015-05-21 19:17:06 +0530 | [diff] [blame] | 309 | || ((p_s[i] >= 'A') && (p_s[i] <= 'C')) |
| 310 | || ((p_s[i] == 'p') || (p_s[i] == 'P') |
| 311 | || (p_s[i] == 'w') || (p_s[i] == 'W')))) |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 312 | return FALSE; |
| 313 | } |
| 314 | |
| 315 | return TRUE; |
| 316 | } |
| 317 | |
| 318 | |