1 /* 2 * Copyright (C) 2014 BlueKitchen GmbH 3 * 4 * Redistribution and use in source and binary forms, with or without 5 * modification, are permitted provided that the following conditions 6 * are met: 7 * 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 3. Neither the name of the copyright holders nor the names of 14 * contributors may be used to endorse or promote products derived 15 * from this software without specific prior written permission. 16 * 4. Any redistribution, use, or modification is done solely for 17 * personal benefit and not for any commercial purpose or for 18 * monetary gain. 19 * 20 * THIS SOFTWARE IS PROVIDED BY BLUEKITCHEN GMBH AND CONTRIBUTORS 21 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 22 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 23 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL MATTHIAS 24 * RINGWALD OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 25 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 26 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS 27 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 28 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 29 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF 30 * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31 * SUCH DAMAGE. 32 * 33 * Please inquire about commercial licensing options at 34 * [email protected] 35 * 36 */ 37 38 /* 39 * btstack_util.c 40 * 41 * General utility functions 42 * 43 * Created by Matthias Ringwald on 7/23/09. 44 */ 45 46 #include "btstack_config.h" 47 #include "btstack_debug.h" 48 #include "btstack_util.h" 49 50 #include <stdio.h> 51 #include <string.h> 52 53 /** 54 * @brief Compare two Bluetooth addresses 55 * @param a 56 * @param b 57 * @return 0 if equal 58 */ 59 int bd_addr_cmp(bd_addr_t a, bd_addr_t b){ 60 return memcmp(a,b, BD_ADDR_LEN); 61 } 62 63 /** 64 * @brief Copy Bluetooth address 65 * @param dest 66 * @param src 67 */ 68 void bd_addr_copy(bd_addr_t dest, bd_addr_t src){ 69 memcpy(dest,src,BD_ADDR_LEN); 70 } 71 72 uint16_t little_endian_read_16(const uint8_t * buffer, int pos){ 73 return ((uint16_t) buffer[pos]) | (((uint16_t)buffer[(pos)+1]) << 8); 74 } 75 uint32_t little_endian_read_24(const uint8_t * buffer, int pos){ 76 return ((uint32_t) buffer[pos]) | (((uint32_t)buffer[(pos)+1]) << 8) | (((uint32_t)buffer[(pos)+2]) << 16); 77 } 78 uint32_t little_endian_read_32(const uint8_t * buffer, int pos){ 79 return ((uint32_t) buffer[pos]) | (((uint32_t)buffer[(pos)+1]) << 8) | (((uint32_t)buffer[(pos)+2]) << 16) | (((uint32_t) buffer[(pos)+3]) << 24); 80 } 81 82 void little_endian_store_16(uint8_t *buffer, uint16_t pos, uint16_t value){ 83 buffer[pos++] = value; 84 buffer[pos++] = value >> 8; 85 } 86 87 void little_endian_store_32(uint8_t *buffer, uint16_t pos, uint32_t value){ 88 buffer[pos++] = value; 89 buffer[pos++] = value >> 8; 90 buffer[pos++] = value >> 16; 91 buffer[pos++] = value >> 24; 92 } 93 94 uint32_t big_endian_read_16( const uint8_t * buffer, int pos) { 95 return ((uint16_t) buffer[(pos)+1]) | (((uint16_t)buffer[ pos ]) << 8); 96 } 97 98 uint32_t big_endian_read_24( const uint8_t * buffer, int pos) { 99 return ( ((uint32_t)buffer[(pos)+2]) | (((uint32_t)buffer[(pos)+1]) << 8) | (((uint32_t) buffer[pos]) << 16)); 100 } 101 102 uint32_t big_endian_read_32( const uint8_t * buffer, int pos) { 103 return ((uint32_t) buffer[(pos)+3]) | (((uint32_t)buffer[(pos)+2]) << 8) | (((uint32_t)buffer[(pos)+1]) << 16) | (((uint32_t) buffer[pos]) << 24); 104 } 105 106 void big_endian_store_16(uint8_t *buffer, uint16_t pos, uint16_t value){ 107 buffer[pos++] = value >> 8; 108 buffer[pos++] = value; 109 } 110 111 void big_endian_store_24(uint8_t *buffer, uint16_t pos, uint32_t value){ 112 buffer[pos++] = value >> 16; 113 buffer[pos++] = value >> 8; 114 buffer[pos++] = value; 115 } 116 117 void big_endian_store_32(uint8_t *buffer, uint16_t pos, uint32_t value){ 118 buffer[pos++] = value >> 24; 119 buffer[pos++] = value >> 16; 120 buffer[pos++] = value >> 8; 121 buffer[pos++] = value; 122 } 123 124 // general swap/endianess utils 125 void reverse_bytes(const uint8_t *src, uint8_t *dst, int len){ 126 int i; 127 for (i = 0; i < len; i++) 128 dst[len - 1 - i] = src[i]; 129 } 130 void reverse_24(const uint8_t * src, uint8_t * dst){ 131 reverse_bytes(src, dst, 3); 132 } 133 void reverse_48(const uint8_t * src, uint8_t * dst){ 134 reverse_bytes(src, dst, 6); 135 } 136 void reverse_56(const uint8_t * src, uint8_t * dst){ 137 reverse_bytes(src, dst, 7); 138 } 139 void reverse_64(const uint8_t * src, uint8_t * dst){ 140 reverse_bytes(src, dst, 8); 141 } 142 void reverse_128(const uint8_t * src, uint8_t * dst){ 143 reverse_bytes(src, dst, 16); 144 } 145 void reverse_256(const uint8_t * src, uint8_t * dst){ 146 reverse_bytes(src, dst, 32); 147 } 148 149 void reverse_bd_addr(const bd_addr_t src, bd_addr_t dest){ 150 reverse_bytes(src, dest, 6); 151 } 152 153 uint32_t btstack_min(uint32_t a, uint32_t b){ 154 return a < b ? a : b; 155 } 156 157 uint32_t btstack_max(uint32_t a, uint32_t b){ 158 return a > b ? a : b; 159 } 160 161 char char_for_nibble(int nibble){ 162 if (nibble < 10) return '0' + nibble; 163 nibble -= 10; 164 if (nibble < 6) return 'A' + nibble; 165 return '?'; 166 } 167 168 static inline char char_for_high_nibble(int value){ 169 return char_for_nibble((value >> 4) & 0x0f); 170 } 171 172 static inline char char_for_low_nibble(int value){ 173 return char_for_nibble(value & 0x0f); 174 } 175 176 int nibble_for_char(char c){ 177 if (c >= '0' && c <= '9') return c - '0'; 178 if (c >= 'a' && c <= 'f') return c - 'a' + 10; 179 if (c >= 'A' && c <= 'F') return c - 'A' + 10; 180 return -1; 181 } 182 183 void printf_hexdump(const void *data, int size){ 184 if (size <= 0) return; 185 int i; 186 for (i=0; i<size;i++){ 187 printf("%02X ", ((uint8_t *)data)[i]); 188 } 189 printf("\n"); 190 } 191 192 void log_info_hexdump(const void *data, int size){ 193 #ifdef ENABLE_LOG_INFO 194 195 #define ITEMS_PER_LINE 16 196 // template '0x12, ' 197 #define BYTES_PER_BYTE 6 198 199 char buffer[BYTES_PER_BYTE*ITEMS_PER_LINE+1]; 200 int i, j; 201 j = 0; 202 for (i=0; i<size;i++){ 203 204 // help static analyzer proof that j stays within bounds 205 if (j > BYTES_PER_BYTE * (ITEMS_PER_LINE-1)){ 206 j = 0; 207 } 208 209 uint8_t byte = ((uint8_t *)data)[i]; 210 buffer[j++] = '0'; 211 buffer[j++] = 'x'; 212 buffer[j++] = char_for_high_nibble(byte); 213 buffer[j++] = char_for_low_nibble(byte); 214 buffer[j++] = ','; 215 buffer[j++] = ' '; 216 217 if (j >= BYTES_PER_BYTE * ITEMS_PER_LINE ){ 218 buffer[j] = 0; 219 log_info("%s", buffer); 220 j = 0; 221 } 222 } 223 if (j != 0){ 224 buffer[j] = 0; 225 log_info("%s", buffer); 226 } 227 #else 228 UNUSED(data); 229 UNUSED(size); 230 #endif 231 } 232 233 void log_info_key(const char * name, sm_key_t key){ 234 #ifdef ENABLE_LOG_INFO 235 char buffer[16*2+1]; 236 int i; 237 int j = 0; 238 for (i=0; i<16;i++){ 239 uint8_t byte = key[i]; 240 buffer[j++] = char_for_high_nibble(byte); 241 buffer[j++] = char_for_low_nibble(byte); 242 } 243 buffer[j] = 0; 244 log_info("%-6s %s", name, buffer); 245 #else 246 UNUSED(name); 247 UNUSED(key); 248 #endif 249 } 250 251 // UUIDs are stored in big endian, similar to bd_addr_t 252 253 // Bluetooth Base UUID: 00000000-0000-1000-8000- 00805F9B34FB 254 const uint8_t bluetooth_base_uuid[] = { 0x00, 0x00, 0x00, 0x00, /* - */ 0x00, 0x00, /* - */ 0x10, 0x00, /* - */ 255 0x80, 0x00, /* - */ 0x00, 0x80, 0x5F, 0x9B, 0x34, 0xFB }; 256 257 void uuid_add_bluetooth_prefix(uint8_t *uuid, uint32_t shortUUID){ 258 memcpy(uuid, bluetooth_base_uuid, 16); 259 big_endian_store_32(uuid, 0, shortUUID); 260 } 261 262 int uuid_has_bluetooth_prefix(uint8_t * uuid128){ 263 return memcmp(&uuid128[4], &bluetooth_base_uuid[4], 12) == 0; 264 } 265 266 static char uuid128_to_str_buffer[32+4+1]; 267 char * uuid128_to_str(uint8_t * uuid){ 268 int i; 269 int j = 0; 270 // after 4, 6, 8, and 10 bytes = XYXYXYXY-XYXY-XYXY-XYXY-XYXYXYXYXYXY, there's a dash 271 const int dash_locations = (1<<3) | (1<<5) | (1<<7) | (1<<9); 272 for (i=0;i<16;i++){ 273 uint8_t byte = uuid[i]; 274 uuid128_to_str_buffer[j++] = char_for_high_nibble(byte); 275 uuid128_to_str_buffer[j++] = char_for_low_nibble(byte); 276 if (dash_locations & (1<<i)){ 277 uuid128_to_str_buffer[j++] = '-'; 278 } 279 } 280 return uuid128_to_str_buffer; 281 } 282 283 static char bd_addr_to_str_buffer[6*3]; // 12:45:78:01:34:67\0 284 char * bd_addr_to_str(bd_addr_t addr){ 285 // orig code 286 // sprintf(bd_addr_to_str_buffer, "%02x:%02x:%02x:%02x:%02x:%02x", addr[0], addr[1], addr[2], addr[3], addr[4], addr[5]); 287 // sprintf-free code 288 char * p = bd_addr_to_str_buffer; 289 int i; 290 for (i = 0; i < 6 ; i++) { 291 uint8_t byte = addr[i]; 292 *p++ = char_for_high_nibble(byte); 293 *p++ = char_for_low_nibble(byte); 294 *p++ = ':'; 295 } 296 *--p = 0; 297 return (char *) bd_addr_to_str_buffer; 298 } 299 300 static int scan_hex_byte(const char * byte_string){ 301 int upper_nibble = nibble_for_char(*byte_string++); 302 if (upper_nibble < 0) return -1; 303 int lower_nibble = nibble_for_char(*byte_string); 304 if (lower_nibble < 0) return -1; 305 return (upper_nibble << 4) | lower_nibble; 306 } 307 308 int sscanf_bd_addr(const char * addr_string, bd_addr_t addr){ 309 uint8_t buffer[BD_ADDR_LEN]; 310 int result = 0; 311 int i; 312 for (i = 0; i < BD_ADDR_LEN; i++) { 313 int single_byte = scan_hex_byte(addr_string); 314 if (single_byte < 0) break; 315 addr_string += 2; 316 buffer[i] = single_byte; 317 // don't check seperator after last byte 318 if (i == BD_ADDR_LEN - 1) { 319 result = 1; 320 break; 321 } 322 char separator = *addr_string++; 323 if (separator != ':' && separator != '-' && separator != ' ') break; 324 } 325 326 if (result){ 327 bd_addr_copy(addr, buffer); 328 } 329 return result; 330 } 331 332 uint32_t btstack_atoi(const char *str){ 333 uint32_t val = 0; 334 while (1){ 335 char chr = *str; 336 if (!chr || chr < '0' || chr > '9') 337 return val; 338 val = (val * 10) + (chr - '0'); 339 str++; 340 } 341 }