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 | *******************************************************************************/ |
Marie Janssen | e9e58ce | 2016-06-17 14:12:17 -0700 | [diff] [blame^] | 42 | int16_t utl_str2int(const char *p_s) |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 43 | { |
Marie Janssen | e9e58ce | 2016-06-17 14:12:17 -0700 | [diff] [blame^] | 44 | int32_t val = 0; |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 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 | |
Marie Janssen | e9e58ce | 2016-06-17 14:12:17 -0700 | [diff] [blame^] | 54 | val += (int32_t) (*p_s++ - '0'); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 55 | |
| 56 | if (val > 32767) return -1; |
| 57 | |
| 58 | if (*p_s == 0) |
| 59 | { |
Marie Janssen | e9e58ce | 2016-06-17 14:12:17 -0700 | [diff] [blame^] | 60 | return (int16_t) val; |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 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 | ** |
Marie Janssen | e9e58ce | 2016-06-17 14:12:17 -0700 | [diff] [blame^] | 114 | ** Description This utility function converts a uint16_t to a string. The |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 115 | ** string is NULL-terminated. The length of the string is |
| 116 | ** returned; |
| 117 | ** |
| 118 | ** |
| 119 | ** Returns Length of string. |
| 120 | ** |
| 121 | *******************************************************************************/ |
Marie Janssen | e9e58ce | 2016-06-17 14:12:17 -0700 | [diff] [blame^] | 122 | uint8_t utl_itoa(uint16_t i, char *p_s) |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 123 | { |
Marie Janssen | e9e58ce | 2016-06-17 14:12:17 -0700 | [diff] [blame^] | 124 | uint16_t j, k; |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 125 | char *p = p_s; |
Marie Janssen | e9e58ce | 2016-06-17 14:12:17 -0700 | [diff] [blame^] | 126 | bool fill = false; |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 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'; |
Marie Janssen | e9e58ce | 2016-06-17 14:12:17 -0700 | [diff] [blame^] | 142 | fill = true; |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 143 | } |
| 144 | } |
| 145 | } |
| 146 | *p = 0; |
Marie Janssen | e9e58ce | 2016-06-17 14:12:17 -0700 | [diff] [blame^] | 147 | return (uint8_t) (p - p_s); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 148 | } |
| 149 | |
| 150 | /******************************************************************************* |
| 151 | ** |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 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 | ** |
Marie Janssen | e9e58ce | 2016-06-17 14:12:17 -0700 | [diff] [blame^] | 166 | ** Returns true if successful, Otherwise false |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 167 | ** |
| 168 | *******************************************************************************/ |
Marie Janssen | e9e58ce | 2016-06-17 14:12:17 -0700 | [diff] [blame^] | 169 | bool utl_set_device_class(tBTA_UTL_COD *p_cod, uint8_t cmd) |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 170 | { |
Marie Janssen | e9e58ce | 2016-06-17 14:12:17 -0700 | [diff] [blame^] | 171 | uint8_t *dev; |
| 172 | uint16_t service; |
| 173 | uint8_t minor, major; |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 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: |
Marie Janssen | e9e58ce | 2016-06-17 14:12:17 -0700 | [diff] [blame^] | 213 | return false; |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 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) |
Marie Janssen | e9e58ce | 2016-06-17 14:12:17 -0700 | [diff] [blame^] | 220 | return true; |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 221 | |
Marie Janssen | e9e58ce | 2016-06-17 14:12:17 -0700 | [diff] [blame^] | 222 | return false; |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 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 | ** |
Marie Janssen | e9e58ce | 2016-06-17 14:12:17 -0700 | [diff] [blame^] | 233 | ** Returns true if successful, Otherwise false |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 234 | ** |
| 235 | *******************************************************************************/ |
Marie Janssen | e9e58ce | 2016-06-17 14:12:17 -0700 | [diff] [blame^] | 236 | bool utl_isintstr(const char *p_s) |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 237 | { |
Marie Janssen | e9e58ce | 2016-06-17 14:12:17 -0700 | [diff] [blame^] | 238 | uint16_t i = 0; |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 239 | |
| 240 | for(i=0; p_s[i] != 0; i++) |
| 241 | { |
| 242 | if(((p_s[i] < '0') || (p_s[i] > '9')) && (p_s[i] != ';')) |
Marie Janssen | e9e58ce | 2016-06-17 14:12:17 -0700 | [diff] [blame^] | 243 | return false; |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 244 | } |
| 245 | |
Marie Janssen | e9e58ce | 2016-06-17 14:12:17 -0700 | [diff] [blame^] | 246 | return true; |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 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 | ** |
Marie Janssen | e9e58ce | 2016-06-17 14:12:17 -0700 | [diff] [blame^] | 257 | ** Returns true if successful, Otherwise false |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 258 | ** |
| 259 | *******************************************************************************/ |
Marie Janssen | e9e58ce | 2016-06-17 14:12:17 -0700 | [diff] [blame^] | 260 | bool utl_isdialstr(const char *p_s) |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 261 | { |
Marie Janssen | e9e58ce | 2016-06-17 14:12:17 -0700 | [diff] [blame^] | 262 | uint16_t i = 0; |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 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] == ';') |
Mallikarjuna GB | f9a0014 | 2015-05-21 19:17:06 +0530 | [diff] [blame] | 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')))) |
Marie Janssen | e9e58ce | 2016-06-17 14:12:17 -0700 | [diff] [blame^] | 271 | return false; |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 272 | } |
| 273 | |
Marie Janssen | e9e58ce | 2016-06-17 14:12:17 -0700 | [diff] [blame^] | 274 | return true; |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 275 | } |
| 276 | |
| 277 | |