The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 1 | /****************************************************************************** |
| 2 | * |
Jakub Pawlowski | 5b790fe | 2017-09-18 09:00:20 -0700 | [diff] [blame] | 3 | * Copyright 2003-2012 Broadcom Corporation |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 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> |
Myles Watson | f355ef5 | 2016-11-09 13:04:33 -0800 | [diff] [blame] | 25 | |
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" |
Myles Watson | cd1fd07 | 2016-11-09 13:17:43 -0800 | [diff] [blame] | 28 | #include "utl.h" |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 29 | |
| 30 | /******************************************************************************* |
Myles Watson | 8af480e | 2016-11-09 10:40:23 -0800 | [diff] [blame] | 31 | * |
| 32 | * Function utl_str2int |
| 33 | * |
| 34 | * Description This utility function converts a character string to an |
| 35 | * integer. Acceptable values in string are 0-9. If invalid |
| 36 | * string or string value too large, -1 is returned. Leading |
| 37 | * spaces are skipped. |
| 38 | * |
| 39 | * |
| 40 | * Returns Integer value or -1 on error. |
| 41 | * |
| 42 | ******************************************************************************/ |
Myles Watson | cd1fd07 | 2016-11-09 13:17:43 -0800 | [diff] [blame] | 43 | int16_t utl_str2int(const char* p_s) { |
| 44 | int32_t val = 0; |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 45 | |
Myles Watson | cd1fd07 | 2016-11-09 13:17:43 -0800 | [diff] [blame] | 46 | for (; *p_s == ' ' && *p_s != 0; p_s++) |
| 47 | ; |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 48 | |
Myles Watson | cd1fd07 | 2016-11-09 13:17:43 -0800 | [diff] [blame] | 49 | if (*p_s == 0) return -1; |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 50 | |
Myles Watson | cd1fd07 | 2016-11-09 13:17:43 -0800 | [diff] [blame] | 51 | for (;;) { |
| 52 | if ((*p_s < '0') || (*p_s > '9')) return -1; |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 53 | |
Myles Watson | cd1fd07 | 2016-11-09 13:17:43 -0800 | [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 | |
Myles Watson | cd1fd07 | 2016-11-09 13:17:43 -0800 | [diff] [blame] | 56 | if (val > 32767) return -1; |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 57 | |
Myles Watson | cd1fd07 | 2016-11-09 13:17:43 -0800 | [diff] [blame] | 58 | if (*p_s == 0) { |
| 59 | return (int16_t)val; |
| 60 | } else { |
| 61 | val *= 10; |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 62 | } |
Myles Watson | cd1fd07 | 2016-11-09 13:17:43 -0800 | [diff] [blame] | 63 | } |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 64 | } |
| 65 | |
| 66 | /******************************************************************************* |
Myles Watson | 8af480e | 2016-11-09 10:40:23 -0800 | [diff] [blame] | 67 | * |
| 68 | * Function utl_strucmp |
| 69 | * |
| 70 | * Description This utility function compares two strings in uppercase. |
| 71 | * String p_s must be uppercase. String p_t is converted to |
| 72 | * uppercase if lowercase. If p_s ends first, the substring |
| 73 | * match is counted as a match. |
| 74 | * |
| 75 | * |
| 76 | * Returns 0 if strings match, nonzero otherwise. |
| 77 | * |
| 78 | ******************************************************************************/ |
Myles Watson | cd1fd07 | 2016-11-09 13:17:43 -0800 | [diff] [blame] | 79 | int utl_strucmp(const char* p_s, const char* p_t) { |
| 80 | char c; |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 81 | |
Myles Watson | cd1fd07 | 2016-11-09 13:17:43 -0800 | [diff] [blame] | 82 | while (*p_s && *p_t) { |
| 83 | c = *p_t++; |
| 84 | if (c >= 'a' && c <= 'z') { |
| 85 | c -= 0x20; |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 86 | } |
Myles Watson | cd1fd07 | 2016-11-09 13:17:43 -0800 | [diff] [blame] | 87 | if (*p_s++ != c) { |
| 88 | return -1; |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 89 | } |
Myles Watson | cd1fd07 | 2016-11-09 13:17:43 -0800 | [diff] [blame] | 90 | } |
| 91 | /* if p_t hit null first, no match */ |
| 92 | if (*p_t == 0 && *p_s != 0) { |
| 93 | return 1; |
| 94 | } |
| 95 | /* else p_s hit null first, count as match */ |
| 96 | else { |
| 97 | return 0; |
| 98 | } |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 99 | } |
| 100 | |
| 101 | /******************************************************************************* |
Myles Watson | 8af480e | 2016-11-09 10:40:23 -0800 | [diff] [blame] | 102 | * |
| 103 | * Function utl_itoa |
| 104 | * |
| 105 | * Description This utility function converts a uint16_t to a string. The |
| 106 | * string is NULL-terminated. The length of the string is |
| 107 | * returned; |
| 108 | * |
| 109 | * |
| 110 | * Returns Length of string. |
| 111 | * |
| 112 | ******************************************************************************/ |
Myles Watson | cd1fd07 | 2016-11-09 13:17:43 -0800 | [diff] [blame] | 113 | uint8_t utl_itoa(uint16_t i, char* p_s) { |
| 114 | uint16_t j, k; |
| 115 | char* p = p_s; |
| 116 | bool fill = false; |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 117 | |
Myles Watson | cd1fd07 | 2016-11-09 13:17:43 -0800 | [diff] [blame] | 118 | if (i == 0) { |
| 119 | /* take care of zero case */ |
| 120 | *p++ = '0'; |
| 121 | } else { |
| 122 | for (j = 10000; j > 0; j /= 10) { |
| 123 | k = i / j; |
| 124 | i %= j; |
| 125 | if (k > 0 || fill) { |
| 126 | *p++ = k + '0'; |
| 127 | fill = true; |
| 128 | } |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 129 | } |
Myles Watson | cd1fd07 | 2016-11-09 13:17:43 -0800 | [diff] [blame] | 130 | } |
| 131 | *p = 0; |
| 132 | return (uint8_t)(p - p_s); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 133 | } |
| 134 | |
| 135 | /******************************************************************************* |
Myles Watson | 8af480e | 2016-11-09 10:40:23 -0800 | [diff] [blame] | 136 | * |
| 137 | * Function utl_set_device_class |
| 138 | * |
| 139 | * Description This function updates the local Device Class. |
| 140 | * |
| 141 | * Parameters: |
| 142 | * p_cod - Pointer to the device class to set to |
| 143 | * |
| 144 | * cmd - the fields of the device class to update. |
Myles Watson | cd1fd07 | 2016-11-09 13:17:43 -0800 | [diff] [blame] | 145 | * BTA_UTL_SET_COD_MAJOR_MINOR, - overwrite major, |
Myles Watson | 1baaae3 | 2016-11-09 14:25:23 -0800 | [diff] [blame] | 146 | * minor class |
Myles Watson | cd1fd07 | 2016-11-09 13:17:43 -0800 | [diff] [blame] | 147 | * BTA_UTL_SET_COD_SERVICE_CLASS - set the bits in |
Myles Watson | 1baaae3 | 2016-11-09 14:25:23 -0800 | [diff] [blame] | 148 | * the input |
Myles Watson | cd1fd07 | 2016-11-09 13:17:43 -0800 | [diff] [blame] | 149 | * BTA_UTL_CLR_COD_SERVICE_CLASS - clear the bits in |
Myles Watson | 1baaae3 | 2016-11-09 14:25:23 -0800 | [diff] [blame] | 150 | * the input |
Myles Watson | cd1fd07 | 2016-11-09 13:17:43 -0800 | [diff] [blame] | 151 | * BTA_UTL_SET_COD_ALL - overwrite major, minor, set |
Myles Watson | 1baaae3 | 2016-11-09 14:25:23 -0800 | [diff] [blame] | 152 | * the bits in service class |
Myles Watson | cd1fd07 | 2016-11-09 13:17:43 -0800 | [diff] [blame] | 153 | * BTA_UTL_INIT_COD - overwrite major, minor, and |
Myles Watson | 1baaae3 | 2016-11-09 14:25:23 -0800 | [diff] [blame] | 154 | * service class |
Myles Watson | 8af480e | 2016-11-09 10:40:23 -0800 | [diff] [blame] | 155 | * |
| 156 | * Returns true if successful, Otherwise false |
| 157 | * |
| 158 | ******************************************************************************/ |
Myles Watson | cd1fd07 | 2016-11-09 13:17:43 -0800 | [diff] [blame] | 159 | bool utl_set_device_class(tBTA_UTL_COD* p_cod, uint8_t cmd) { |
| 160 | uint8_t* dev; |
| 161 | uint16_t service; |
| 162 | uint8_t minor, major; |
| 163 | DEV_CLASS dev_class; |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 164 | |
Myles Watson | cd1fd07 | 2016-11-09 13:17:43 -0800 | [diff] [blame] | 165 | dev = BTM_ReadDeviceClass(); |
| 166 | BTM_COD_SERVICE_CLASS(service, dev); |
| 167 | BTM_COD_MINOR_CLASS(minor, dev); |
| 168 | BTM_COD_MAJOR_CLASS(major, dev); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 169 | |
Myles Watson | cd1fd07 | 2016-11-09 13:17:43 -0800 | [diff] [blame] | 170 | switch (cmd) { |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 171 | case BTA_UTL_SET_COD_MAJOR_MINOR: |
Myles Watson | cd1fd07 | 2016-11-09 13:17:43 -0800 | [diff] [blame] | 172 | minor = p_cod->minor & BTM_COD_MINOR_CLASS_MASK; |
| 173 | major = p_cod->major & BTM_COD_MAJOR_CLASS_MASK; |
| 174 | break; |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 175 | |
| 176 | case BTA_UTL_SET_COD_SERVICE_CLASS: |
Myles Watson | cd1fd07 | 2016-11-09 13:17:43 -0800 | [diff] [blame] | 177 | /* clear out the bits that is not SERVICE_CLASS bits */ |
| 178 | p_cod->service &= BTM_COD_SERVICE_CLASS_MASK; |
| 179 | service = service | p_cod->service; |
| 180 | break; |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 181 | |
| 182 | case BTA_UTL_CLR_COD_SERVICE_CLASS: |
Myles Watson | cd1fd07 | 2016-11-09 13:17:43 -0800 | [diff] [blame] | 183 | p_cod->service &= BTM_COD_SERVICE_CLASS_MASK; |
| 184 | service = service & (~p_cod->service); |
| 185 | break; |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 186 | |
| 187 | case BTA_UTL_SET_COD_ALL: |
Myles Watson | cd1fd07 | 2016-11-09 13:17:43 -0800 | [diff] [blame] | 188 | minor = p_cod->minor & BTM_COD_MINOR_CLASS_MASK; |
| 189 | major = p_cod->major & BTM_COD_MAJOR_CLASS_MASK; |
| 190 | p_cod->service &= BTM_COD_SERVICE_CLASS_MASK; |
| 191 | service = service | p_cod->service; |
| 192 | break; |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 193 | |
| 194 | case BTA_UTL_INIT_COD: |
Myles Watson | cd1fd07 | 2016-11-09 13:17:43 -0800 | [diff] [blame] | 195 | minor = p_cod->minor & BTM_COD_MINOR_CLASS_MASK; |
| 196 | major = p_cod->major & BTM_COD_MAJOR_CLASS_MASK; |
| 197 | service = p_cod->service & BTM_COD_SERVICE_CLASS_MASK; |
| 198 | break; |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 199 | |
| 200 | default: |
Myles Watson | cd1fd07 | 2016-11-09 13:17:43 -0800 | [diff] [blame] | 201 | return false; |
| 202 | } |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 203 | |
Myles Watson | cd1fd07 | 2016-11-09 13:17:43 -0800 | [diff] [blame] | 204 | /* convert the fields into the device class type */ |
| 205 | FIELDS_TO_COD(dev_class, minor, major, service); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 206 | |
Myles Watson | cd1fd07 | 2016-11-09 13:17:43 -0800 | [diff] [blame] | 207 | if (BTM_SetDeviceClass(dev_class) == BTM_SUCCESS) return true; |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 208 | |
Myles Watson | cd1fd07 | 2016-11-09 13:17:43 -0800 | [diff] [blame] | 209 | return false; |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 210 | } |
| 211 | |
| 212 | /******************************************************************************* |
Myles Watson | 8af480e | 2016-11-09 10:40:23 -0800 | [diff] [blame] | 213 | * |
| 214 | * Function utl_isintstr |
| 215 | * |
| 216 | * Description This utility function checks if the given string is an |
| 217 | * integer string or not |
| 218 | * |
| 219 | * |
| 220 | * Returns true if successful, Otherwise false |
| 221 | * |
| 222 | ******************************************************************************/ |
Myles Watson | cd1fd07 | 2016-11-09 13:17:43 -0800 | [diff] [blame] | 223 | bool utl_isintstr(const char* p_s) { |
| 224 | uint16_t i = 0; |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 225 | |
Myles Watson | cd1fd07 | 2016-11-09 13:17:43 -0800 | [diff] [blame] | 226 | for (i = 0; p_s[i] != 0; i++) { |
| 227 | if (((p_s[i] < '0') || (p_s[i] > '9')) && (p_s[i] != ';')) return false; |
| 228 | } |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 229 | |
Myles Watson | cd1fd07 | 2016-11-09 13:17:43 -0800 | [diff] [blame] | 230 | return true; |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 231 | } |
| 232 | |
| 233 | /******************************************************************************* |
Myles Watson | 8af480e | 2016-11-09 10:40:23 -0800 | [diff] [blame] | 234 | * |
| 235 | * Function utl_isdialchar |
| 236 | * |
| 237 | * Description This utility function checks if the given character |
| 238 | * is an acceptable dial digit |
| 239 | * |
| 240 | * Returns true if successful, Otherwise false |
| 241 | * |
| 242 | ******************************************************************************/ |
Myles Watson | cd1fd07 | 2016-11-09 13:17:43 -0800 | [diff] [blame] | 243 | bool utl_isdialchar(const char d) { |
| 244 | return (((d >= '0') && (d <= '9')) || (d == '*') || (d == '+') || |
jonerlin | 39943dfe | 2018-10-30 14:09:41 +0800 | [diff] [blame] | 245 | (d == '#') || (d == ';') || (d == ',') || |
| 246 | ((d >= 'A') && (d <= 'C')) || |
Myles Watson | cd1fd07 | 2016-11-09 13:17:43 -0800 | [diff] [blame] | 247 | ((d == 'p') || (d == 'P') || (d == 'w') || (d == 'W'))); |
Satish Kodishala | c75a71d | 2016-06-22 14:22:41 +0530 | [diff] [blame] | 248 | } |
| 249 | |
| 250 | /******************************************************************************* |
Myles Watson | 8af480e | 2016-11-09 10:40:23 -0800 | [diff] [blame] | 251 | * |
| 252 | * Function utl_isdialstr |
| 253 | * |
| 254 | * Description This utility function checks if the given string contains |
| 255 | * only dial digits or not |
| 256 | * |
| 257 | * |
| 258 | * Returns true if successful, Otherwise false |
| 259 | * |
| 260 | ******************************************************************************/ |
Myles Watson | cd1fd07 | 2016-11-09 13:17:43 -0800 | [diff] [blame] | 261 | bool utl_isdialstr(const char* p_s) { |
| 262 | for (uint16_t i = 0; p_s[i] != 0; i++) { |
| 263 | // include chars not in spec that work sent by some headsets. |
| 264 | if (!(utl_isdialchar(p_s[i]) || (p_s[i] == '-'))) return false; |
| 265 | } |
| 266 | return true; |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 267 | } |