1*eb886013SMatthias Ringwald /* 2*eb886013SMatthias Ringwald * Copyright (C) 2014 BlueKitchen GmbH 3*eb886013SMatthias Ringwald * 4*eb886013SMatthias Ringwald * Redistribution and use in source and binary forms, with or without 5*eb886013SMatthias Ringwald * modification, are permitted provided that the following conditions 6*eb886013SMatthias Ringwald * are met: 7*eb886013SMatthias Ringwald * 8*eb886013SMatthias Ringwald * 1. Redistributions of source code must retain the above copyright 9*eb886013SMatthias Ringwald * notice, this list of conditions and the following disclaimer. 10*eb886013SMatthias Ringwald * 2. Redistributions in binary form must reproduce the above copyright 11*eb886013SMatthias Ringwald * notice, this list of conditions and the following disclaimer in the 12*eb886013SMatthias Ringwald * documentation and/or other materials provided with the distribution. 13*eb886013SMatthias Ringwald * 3. Neither the name of the copyright holders nor the names of 14*eb886013SMatthias Ringwald * contributors may be used to endorse or promote products derived 15*eb886013SMatthias Ringwald * from this software without specific prior written permission. 16*eb886013SMatthias Ringwald * 4. Any redistribution, use, or modification is done solely for 17*eb886013SMatthias Ringwald * personal benefit and not for any commercial purpose or for 18*eb886013SMatthias Ringwald * monetary gain. 19*eb886013SMatthias Ringwald * 20*eb886013SMatthias Ringwald * THIS SOFTWARE IS PROVIDED BY BLUEKITCHEN GMBH AND CONTRIBUTORS 21*eb886013SMatthias Ringwald * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 22*eb886013SMatthias Ringwald * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 23*eb886013SMatthias Ringwald * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL MATTHIAS 24*eb886013SMatthias Ringwald * RINGWALD OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 25*eb886013SMatthias Ringwald * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 26*eb886013SMatthias Ringwald * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS 27*eb886013SMatthias Ringwald * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 28*eb886013SMatthias Ringwald * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 29*eb886013SMatthias Ringwald * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF 30*eb886013SMatthias Ringwald * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31*eb886013SMatthias Ringwald * SUCH DAMAGE. 32*eb886013SMatthias Ringwald * 33*eb886013SMatthias Ringwald * Please inquire about commercial licensing options at 34*eb886013SMatthias Ringwald * [email protected] 35*eb886013SMatthias Ringwald * 36*eb886013SMatthias Ringwald */ 37*eb886013SMatthias Ringwald 38*eb886013SMatthias Ringwald /* 39*eb886013SMatthias Ringwald * btstack_util.c 40*eb886013SMatthias Ringwald * 41*eb886013SMatthias Ringwald * General utility functions 42*eb886013SMatthias Ringwald * 43*eb886013SMatthias Ringwald * Created by Matthias Ringwald on 7/23/09. 44*eb886013SMatthias Ringwald */ 45*eb886013SMatthias Ringwald 46*eb886013SMatthias Ringwald #include "btstack-config.h" 47*eb886013SMatthias Ringwald #include "btstack_util.h" 48*eb886013SMatthias Ringwald #include <stdio.h> 49*eb886013SMatthias Ringwald #include <string.h> 50*eb886013SMatthias Ringwald #include "btstack_debug.h" 51*eb886013SMatthias Ringwald 52*eb886013SMatthias Ringwald void bt_store_16(uint8_t *buffer, uint16_t pos, uint16_t value){ 53*eb886013SMatthias Ringwald buffer[pos++] = value; 54*eb886013SMatthias Ringwald buffer[pos++] = value >> 8; 55*eb886013SMatthias Ringwald } 56*eb886013SMatthias Ringwald 57*eb886013SMatthias Ringwald void bt_store_32(uint8_t *buffer, uint16_t pos, uint32_t value){ 58*eb886013SMatthias Ringwald buffer[pos++] = value; 59*eb886013SMatthias Ringwald buffer[pos++] = value >> 8; 60*eb886013SMatthias Ringwald buffer[pos++] = value >> 16; 61*eb886013SMatthias Ringwald buffer[pos++] = value >> 24; 62*eb886013SMatthias Ringwald } 63*eb886013SMatthias Ringwald 64*eb886013SMatthias Ringwald void net_store_16(uint8_t *buffer, uint16_t pos, uint16_t value){ 65*eb886013SMatthias Ringwald buffer[pos++] = value >> 8; 66*eb886013SMatthias Ringwald buffer[pos++] = value; 67*eb886013SMatthias Ringwald } 68*eb886013SMatthias Ringwald 69*eb886013SMatthias Ringwald void net_store_32(uint8_t *buffer, uint16_t pos, uint32_t value){ 70*eb886013SMatthias Ringwald buffer[pos++] = value >> 24; 71*eb886013SMatthias Ringwald buffer[pos++] = value >> 16; 72*eb886013SMatthias Ringwald buffer[pos++] = value >> 8; 73*eb886013SMatthias Ringwald buffer[pos++] = value; 74*eb886013SMatthias Ringwald } 75*eb886013SMatthias Ringwald 76*eb886013SMatthias Ringwald void bt_flip_addr(bd_addr_t dest, bd_addr_t src){ 77*eb886013SMatthias Ringwald dest[0] = src[5]; 78*eb886013SMatthias Ringwald dest[1] = src[4]; 79*eb886013SMatthias Ringwald dest[2] = src[3]; 80*eb886013SMatthias Ringwald dest[3] = src[2]; 81*eb886013SMatthias Ringwald dest[4] = src[1]; 82*eb886013SMatthias Ringwald dest[5] = src[0]; 83*eb886013SMatthias Ringwald } 84*eb886013SMatthias Ringwald 85*eb886013SMatthias Ringwald // general swap/endianess utils 86*eb886013SMatthias Ringwald void swapX(const uint8_t *src, uint8_t *dst, int len){ 87*eb886013SMatthias Ringwald int i; 88*eb886013SMatthias Ringwald for (i = 0; i < len; i++) 89*eb886013SMatthias Ringwald dst[len - 1 - i] = src[i]; 90*eb886013SMatthias Ringwald } 91*eb886013SMatthias Ringwald void swap24(const uint8_t src[3], uint8_t dst[3]){ 92*eb886013SMatthias Ringwald swapX(src, dst, 3); 93*eb886013SMatthias Ringwald } 94*eb886013SMatthias Ringwald void swap56(const uint8_t src[7], uint8_t dst[7]){ 95*eb886013SMatthias Ringwald swapX(src, dst, 7); 96*eb886013SMatthias Ringwald } 97*eb886013SMatthias Ringwald void swap64(const uint8_t src[8], uint8_t dst[8]){ 98*eb886013SMatthias Ringwald swapX(src, dst, 8); 99*eb886013SMatthias Ringwald } 100*eb886013SMatthias Ringwald void swap128(const uint8_t src[16], uint8_t dst[16]){ 101*eb886013SMatthias Ringwald swapX(src, dst, 16); 102*eb886013SMatthias Ringwald } 103*eb886013SMatthias Ringwald 104*eb886013SMatthias Ringwald char char_for_nibble(int nibble){ 105*eb886013SMatthias Ringwald if (nibble < 10) return '0' + nibble; 106*eb886013SMatthias Ringwald nibble -= 10; 107*eb886013SMatthias Ringwald if (nibble < 6) return 'A' + nibble; 108*eb886013SMatthias Ringwald return '?'; 109*eb886013SMatthias Ringwald } 110*eb886013SMatthias Ringwald 111*eb886013SMatthias Ringwald void printf_hexdump(const void *data, int size){ 112*eb886013SMatthias Ringwald if (size <= 0) return; 113*eb886013SMatthias Ringwald int i; 114*eb886013SMatthias Ringwald for (i=0; i<size;i++){ 115*eb886013SMatthias Ringwald printf("%02X ", ((uint8_t *)data)[i]); 116*eb886013SMatthias Ringwald } 117*eb886013SMatthias Ringwald printf("\n"); 118*eb886013SMatthias Ringwald } 119*eb886013SMatthias Ringwald 120*eb886013SMatthias Ringwald void hexdump(const void *data, int size){ 121*eb886013SMatthias Ringwald #ifdef ENABLE_LOG_INFO 122*eb886013SMatthias Ringwald char buffer[6*16+1]; 123*eb886013SMatthias Ringwald int i, j; 124*eb886013SMatthias Ringwald 125*eb886013SMatthias Ringwald uint8_t low = 0x0F; 126*eb886013SMatthias Ringwald uint8_t high = 0xF0; 127*eb886013SMatthias Ringwald j = 0; 128*eb886013SMatthias Ringwald for (i=0; i<size;i++){ 129*eb886013SMatthias Ringwald uint8_t byte = ((uint8_t *)data)[i]; 130*eb886013SMatthias Ringwald buffer[j++] = '0'; 131*eb886013SMatthias Ringwald buffer[j++] = 'x'; 132*eb886013SMatthias Ringwald buffer[j++] = char_for_nibble((byte & high) >> 4); 133*eb886013SMatthias Ringwald buffer[j++] = char_for_nibble(byte & low); 134*eb886013SMatthias Ringwald buffer[j++] = ','; 135*eb886013SMatthias Ringwald buffer[j++] = ' '; 136*eb886013SMatthias Ringwald if (j >= 6*16 ){ 137*eb886013SMatthias Ringwald buffer[j] = 0; 138*eb886013SMatthias Ringwald log_info("%s", buffer); 139*eb886013SMatthias Ringwald j = 0; 140*eb886013SMatthias Ringwald } 141*eb886013SMatthias Ringwald } 142*eb886013SMatthias Ringwald if (j != 0){ 143*eb886013SMatthias Ringwald buffer[j] = 0; 144*eb886013SMatthias Ringwald log_info("%s", buffer); 145*eb886013SMatthias Ringwald } 146*eb886013SMatthias Ringwald #endif 147*eb886013SMatthias Ringwald } 148*eb886013SMatthias Ringwald 149*eb886013SMatthias Ringwald void hexdumpf(const void *data, int size){ 150*eb886013SMatthias Ringwald char buffer[6*16+1]; 151*eb886013SMatthias Ringwald int i, j; 152*eb886013SMatthias Ringwald 153*eb886013SMatthias Ringwald uint8_t low = 0x0F; 154*eb886013SMatthias Ringwald uint8_t high = 0xF0; 155*eb886013SMatthias Ringwald j = 0; 156*eb886013SMatthias Ringwald for (i=0; i<size;i++){ 157*eb886013SMatthias Ringwald uint8_t byte = ((uint8_t *)data)[i]; 158*eb886013SMatthias Ringwald buffer[j++] = '0'; 159*eb886013SMatthias Ringwald buffer[j++] = 'x'; 160*eb886013SMatthias Ringwald buffer[j++] = char_for_nibble((byte & high) >> 4); 161*eb886013SMatthias Ringwald buffer[j++] = char_for_nibble(byte & low); 162*eb886013SMatthias Ringwald buffer[j++] = ','; 163*eb886013SMatthias Ringwald buffer[j++] = ' '; 164*eb886013SMatthias Ringwald if (j >= 6*16 ){ 165*eb886013SMatthias Ringwald buffer[j] = 0; 166*eb886013SMatthias Ringwald printf("%s\n", buffer); 167*eb886013SMatthias Ringwald j = 0; 168*eb886013SMatthias Ringwald } 169*eb886013SMatthias Ringwald } 170*eb886013SMatthias Ringwald if (j != 0){ 171*eb886013SMatthias Ringwald buffer[j] = 0; 172*eb886013SMatthias Ringwald printf("%s\n", buffer); 173*eb886013SMatthias Ringwald } 174*eb886013SMatthias Ringwald } 175*eb886013SMatthias Ringwald 176*eb886013SMatthias Ringwald void log_key(const char * name, sm_key_t key){ 177*eb886013SMatthias Ringwald log_info("%-6s ", name); 178*eb886013SMatthias Ringwald hexdump(key, 16); 179*eb886013SMatthias Ringwald } 180*eb886013SMatthias Ringwald 181*eb886013SMatthias Ringwald // Bluetooth Base UUID: 00000000-0000-1000-8000- 00805F9B34FB 182*eb886013SMatthias Ringwald const uint8_t sdp_bluetooth_base_uuid[] = { 0x00, 0x00, 0x00, 0x00, /* - */ 0x00, 0x00, /* - */ 0x10, 0x00, /* - */ 183*eb886013SMatthias Ringwald 0x80, 0x00, /* - */ 0x00, 0x80, 0x5F, 0x9B, 0x34, 0xFB }; 184*eb886013SMatthias Ringwald 185*eb886013SMatthias Ringwald void sdp_normalize_uuid(uint8_t *uuid, uint32_t shortUUID){ 186*eb886013SMatthias Ringwald memcpy(uuid, sdp_bluetooth_base_uuid, 16); 187*eb886013SMatthias Ringwald net_store_32(uuid, 0, shortUUID); 188*eb886013SMatthias Ringwald } 189*eb886013SMatthias Ringwald 190*eb886013SMatthias Ringwald int sdp_has_blueooth_base_uuid(uint8_t * uuid128){ 191*eb886013SMatthias Ringwald return memcmp(&uuid128[4], &sdp_bluetooth_base_uuid[4], 12) == 0; 192*eb886013SMatthias Ringwald } 193*eb886013SMatthias Ringwald 194*eb886013SMatthias Ringwald static char uuid128_to_str_buffer[32+4+1]; 195*eb886013SMatthias Ringwald char * uuid128_to_str(uint8_t * uuid){ 196*eb886013SMatthias Ringwald sprintf(uuid128_to_str_buffer, "%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x", 197*eb886013SMatthias Ringwald uuid[0], uuid[1], uuid[2], uuid[3], uuid[4], uuid[5], uuid[6], uuid[7], 198*eb886013SMatthias Ringwald uuid[8], uuid[9], uuid[10], uuid[11], uuid[12], uuid[13], uuid[14], uuid[15]); 199*eb886013SMatthias Ringwald return uuid128_to_str_buffer; 200*eb886013SMatthias Ringwald } 201*eb886013SMatthias Ringwald void printUUID128(uint8_t *uuid) { 202*eb886013SMatthias Ringwald printf("%s", uuid128_to_str(uuid)); 203*eb886013SMatthias Ringwald } 204*eb886013SMatthias Ringwald 205*eb886013SMatthias Ringwald static char bd_addr_to_str_buffer[6*3]; // 12:45:78:01:34:67\0 206*eb886013SMatthias Ringwald char * bd_addr_to_str(bd_addr_t addr){ 207*eb886013SMatthias Ringwald // orig code 208*eb886013SMatthias Ringwald // sprintf(bd_addr_to_str_buffer, "%02x:%02x:%02x:%02x:%02x:%02x", addr[0], addr[1], addr[2], addr[3], addr[4], addr[5]); 209*eb886013SMatthias Ringwald // sprintf-free code 210*eb886013SMatthias Ringwald char * p = bd_addr_to_str_buffer; 211*eb886013SMatthias Ringwald int i; 212*eb886013SMatthias Ringwald for (i = 0; i < 6 ; i++) { 213*eb886013SMatthias Ringwald *p++ = char_for_nibble((addr[i] >> 4) & 0x0F); 214*eb886013SMatthias Ringwald *p++ = char_for_nibble((addr[i] >> 0) & 0x0F); 215*eb886013SMatthias Ringwald *p++ = ':'; 216*eb886013SMatthias Ringwald } 217*eb886013SMatthias Ringwald *--p = 0; 218*eb886013SMatthias Ringwald return (char *) bd_addr_to_str_buffer; 219*eb886013SMatthias Ringwald } 220*eb886013SMatthias Ringwald 221*eb886013SMatthias Ringwald static char link_key_to_str_buffer[LINK_KEY_STR_LEN+1]; // 11223344556677889900112233445566\0 222*eb886013SMatthias Ringwald char *link_key_to_str(link_key_t link_key){ 223*eb886013SMatthias Ringwald char * p = link_key_to_str_buffer; 224*eb886013SMatthias Ringwald int i; 225*eb886013SMatthias Ringwald for (i = 0; i < LINK_KEY_LEN ; i++) { 226*eb886013SMatthias Ringwald *p++ = char_for_nibble((link_key[i] >> 4) & 0x0F); 227*eb886013SMatthias Ringwald *p++ = char_for_nibble((link_key[i] >> 0) & 0x0F); 228*eb886013SMatthias Ringwald } 229*eb886013SMatthias Ringwald *p = 0; 230*eb886013SMatthias Ringwald return (char *) link_key_to_str_buffer; 231*eb886013SMatthias Ringwald } 232*eb886013SMatthias Ringwald 233*eb886013SMatthias Ringwald static char link_key_type_to_str_buffer[2]; 234*eb886013SMatthias Ringwald char *link_key_type_to_str(link_key_type_t link_key){ 235*eb886013SMatthias Ringwald snprintf(link_key_type_to_str_buffer, sizeof(link_key_type_to_str_buffer), "%d", link_key); 236*eb886013SMatthias Ringwald return (char *) link_key_type_to_str_buffer; 237*eb886013SMatthias Ringwald } 238*eb886013SMatthias Ringwald 239*eb886013SMatthias Ringwald void print_bd_addr( bd_addr_t addr){ 240*eb886013SMatthias Ringwald log_info("%s", bd_addr_to_str(addr)); 241*eb886013SMatthias Ringwald } 242*eb886013SMatthias Ringwald 243*eb886013SMatthias Ringwald #ifndef EMBEDDED 244*eb886013SMatthias Ringwald int sscan_bd_addr(uint8_t * addr_string, bd_addr_t addr){ 245*eb886013SMatthias Ringwald unsigned int bd_addr_buffer[BD_ADDR_LEN]; //for sscanf, integer needed 246*eb886013SMatthias Ringwald // reset result buffer 247*eb886013SMatthias Ringwald memset(bd_addr_buffer, 0, sizeof(bd_addr_buffer)); 248*eb886013SMatthias Ringwald 249*eb886013SMatthias Ringwald // parse 250*eb886013SMatthias Ringwald int result = sscanf( (char *) addr_string, "%2x:%2x:%2x:%2x:%2x:%2x", &bd_addr_buffer[0], &bd_addr_buffer[1], &bd_addr_buffer[2], 251*eb886013SMatthias Ringwald &bd_addr_buffer[3], &bd_addr_buffer[4], &bd_addr_buffer[5]); 252*eb886013SMatthias Ringwald 253*eb886013SMatthias Ringwald if (result != BD_ADDR_LEN) return 0; 254*eb886013SMatthias Ringwald 255*eb886013SMatthias Ringwald // store 256*eb886013SMatthias Ringwald int i; 257*eb886013SMatthias Ringwald for (i = 0; i < BD_ADDR_LEN; i++) { 258*eb886013SMatthias Ringwald addr[i] = (uint8_t) bd_addr_buffer[i]; 259*eb886013SMatthias Ringwald } 260*eb886013SMatthias Ringwald return 1; 261*eb886013SMatthias Ringwald } 262*eb886013SMatthias Ringwald 263*eb886013SMatthias Ringwald int sscan_link_key(char * addr_string, link_key_t link_key){ 264*eb886013SMatthias Ringwald unsigned int buffer[LINK_KEY_LEN]; 265*eb886013SMatthias Ringwald 266*eb886013SMatthias Ringwald // reset result buffer 267*eb886013SMatthias Ringwald memset(&buffer, 0, sizeof(buffer)); 268*eb886013SMatthias Ringwald 269*eb886013SMatthias Ringwald // parse 270*eb886013SMatthias Ringwald int result = sscanf( (char *) addr_string, "%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x", 271*eb886013SMatthias Ringwald &buffer[0], &buffer[1], &buffer[2], &buffer[3], 272*eb886013SMatthias Ringwald &buffer[4], &buffer[5], &buffer[6], &buffer[7], 273*eb886013SMatthias Ringwald &buffer[8], &buffer[9], &buffer[10], &buffer[11], 274*eb886013SMatthias Ringwald &buffer[12], &buffer[13], &buffer[14], &buffer[15] ); 275*eb886013SMatthias Ringwald 276*eb886013SMatthias Ringwald if (result != LINK_KEY_LEN) return 0; 277*eb886013SMatthias Ringwald 278*eb886013SMatthias Ringwald // store 279*eb886013SMatthias Ringwald int i; 280*eb886013SMatthias Ringwald uint8_t *p = (uint8_t *) link_key; 281*eb886013SMatthias Ringwald for (i=0; i<LINK_KEY_LEN; i++ ) { 282*eb886013SMatthias Ringwald *p++ = (uint8_t) buffer[i]; 283*eb886013SMatthias Ringwald } 284*eb886013SMatthias Ringwald return 1; 285*eb886013SMatthias Ringwald } 286*eb886013SMatthias Ringwald 287*eb886013SMatthias Ringwald #endif 288*eb886013SMatthias Ringwald 289*eb886013SMatthias Ringwald 290*eb886013SMatthias Ringwald // treat standard pairing as Authenticated as it uses a PIN 291*eb886013SMatthias Ringwald int is_authenticated_link_key(link_key_type_t link_key_type){ 292*eb886013SMatthias Ringwald switch (link_key_type){ 293*eb886013SMatthias Ringwald case COMBINATION_KEY: 294*eb886013SMatthias Ringwald case AUTHENTICATED_COMBINATION_KEY_GENERATED_FROM_P192: 295*eb886013SMatthias Ringwald case AUTHENTICATED_COMBINATION_KEY_GENERATED_FROM_P256: 296*eb886013SMatthias Ringwald return 1; 297*eb886013SMatthias Ringwald default: 298*eb886013SMatthias Ringwald return 0; 299*eb886013SMatthias Ringwald } 300*eb886013SMatthias Ringwald } 301*eb886013SMatthias Ringwald 302*eb886013SMatthias Ringwald /* 303*eb886013SMatthias Ringwald * CRC (reversed crc) lookup table as calculated by the table generator in ETSI TS 101 369 V6.3.0. 304*eb886013SMatthias Ringwald */ 305*eb886013SMatthias Ringwald static const uint8_t crc8table[256] = { /* reversed, 8-bit, poly=0x07 */ 306*eb886013SMatthias Ringwald 0x00, 0x91, 0xE3, 0x72, 0x07, 0x96, 0xE4, 0x75, 0x0E, 0x9F, 0xED, 0x7C, 0x09, 0x98, 0xEA, 0x7B, 307*eb886013SMatthias Ringwald 0x1C, 0x8D, 0xFF, 0x6E, 0x1B, 0x8A, 0xF8, 0x69, 0x12, 0x83, 0xF1, 0x60, 0x15, 0x84, 0xF6, 0x67, 308*eb886013SMatthias Ringwald 0x38, 0xA9, 0xDB, 0x4A, 0x3F, 0xAE, 0xDC, 0x4D, 0x36, 0xA7, 0xD5, 0x44, 0x31, 0xA0, 0xD2, 0x43, 309*eb886013SMatthias Ringwald 0x24, 0xB5, 0xC7, 0x56, 0x23, 0xB2, 0xC0, 0x51, 0x2A, 0xBB, 0xC9, 0x58, 0x2D, 0xBC, 0xCE, 0x5F, 310*eb886013SMatthias Ringwald 0x70, 0xE1, 0x93, 0x02, 0x77, 0xE6, 0x94, 0x05, 0x7E, 0xEF, 0x9D, 0x0C, 0x79, 0xE8, 0x9A, 0x0B, 311*eb886013SMatthias Ringwald 0x6C, 0xFD, 0x8F, 0x1E, 0x6B, 0xFA, 0x88, 0x19, 0x62, 0xF3, 0x81, 0x10, 0x65, 0xF4, 0x86, 0x17, 312*eb886013SMatthias Ringwald 0x48, 0xD9, 0xAB, 0x3A, 0x4F, 0xDE, 0xAC, 0x3D, 0x46, 0xD7, 0xA5, 0x34, 0x41, 0xD0, 0xA2, 0x33, 313*eb886013SMatthias Ringwald 0x54, 0xC5, 0xB7, 0x26, 0x53, 0xC2, 0xB0, 0x21, 0x5A, 0xCB, 0xB9, 0x28, 0x5D, 0xCC, 0xBE, 0x2F, 314*eb886013SMatthias Ringwald 0xE0, 0x71, 0x03, 0x92, 0xE7, 0x76, 0x04, 0x95, 0xEE, 0x7F, 0x0D, 0x9C, 0xE9, 0x78, 0x0A, 0x9B, 315*eb886013SMatthias Ringwald 0xFC, 0x6D, 0x1F, 0x8E, 0xFB, 0x6A, 0x18, 0x89, 0xF2, 0x63, 0x11, 0x80, 0xF5, 0x64, 0x16, 0x87, 316*eb886013SMatthias Ringwald 0xD8, 0x49, 0x3B, 0xAA, 0xDF, 0x4E, 0x3C, 0xAD, 0xD6, 0x47, 0x35, 0xA4, 0xD1, 0x40, 0x32, 0xA3, 317*eb886013SMatthias Ringwald 0xC4, 0x55, 0x27, 0xB6, 0xC3, 0x52, 0x20, 0xB1, 0xCA, 0x5B, 0x29, 0xB8, 0xCD, 0x5C, 0x2E, 0xBF, 318*eb886013SMatthias Ringwald 0x90, 0x01, 0x73, 0xE2, 0x97, 0x06, 0x74, 0xE5, 0x9E, 0x0F, 0x7D, 0xEC, 0x99, 0x08, 0x7A, 0xEB, 319*eb886013SMatthias Ringwald 0x8C, 0x1D, 0x6F, 0xFE, 0x8B, 0x1A, 0x68, 0xF9, 0x82, 0x13, 0x61, 0xF0, 0x85, 0x14, 0x66, 0xF7, 320*eb886013SMatthias Ringwald 0xA8, 0x39, 0x4B, 0xDA, 0xAF, 0x3E, 0x4C, 0xDD, 0xA6, 0x37, 0x45, 0xD4, 0xA1, 0x30, 0x42, 0xD3, 321*eb886013SMatthias Ringwald 0xB4, 0x25, 0x57, 0xC6, 0xB3, 0x22, 0x50, 0xC1, 0xBA, 0x2B, 0x59, 0xC8, 0xBD, 0x2C, 0x5E, 0xCF 322*eb886013SMatthias Ringwald }; 323*eb886013SMatthias Ringwald 324*eb886013SMatthias Ringwald #define CRC8_INIT 0xFF // Initial FCS value 325*eb886013SMatthias Ringwald #define CRC8_OK 0xCF // Good final FCS value 326*eb886013SMatthias Ringwald /*-----------------------------------------------------------------------------------*/ 327*eb886013SMatthias Ringwald static uint8_t crc8(uint8_t *data, uint16_t len) 328*eb886013SMatthias Ringwald { 329*eb886013SMatthias Ringwald uint16_t count; 330*eb886013SMatthias Ringwald uint8_t crc = CRC8_INIT; 331*eb886013SMatthias Ringwald for (count = 0; count < len; count++) 332*eb886013SMatthias Ringwald crc = crc8table[crc ^ data[count]]; 333*eb886013SMatthias Ringwald return crc; 334*eb886013SMatthias Ringwald } 335*eb886013SMatthias Ringwald 336*eb886013SMatthias Ringwald /*-----------------------------------------------------------------------------------*/ 337*eb886013SMatthias Ringwald uint8_t crc8_check(uint8_t *data, uint16_t len, uint8_t check_sum) 338*eb886013SMatthias Ringwald { 339*eb886013SMatthias Ringwald uint8_t crc; 340*eb886013SMatthias Ringwald 341*eb886013SMatthias Ringwald crc = crc8(data, len); 342*eb886013SMatthias Ringwald 343*eb886013SMatthias Ringwald crc = crc8table[crc ^ check_sum]; 344*eb886013SMatthias Ringwald if (crc == CRC8_OK) 345*eb886013SMatthias Ringwald return 0; /* Valid */ 346*eb886013SMatthias Ringwald else 347*eb886013SMatthias Ringwald return 1; /* Failed */ 348*eb886013SMatthias Ringwald 349*eb886013SMatthias Ringwald } 350*eb886013SMatthias Ringwald 351*eb886013SMatthias Ringwald /*-----------------------------------------------------------------------------------*/ 352*eb886013SMatthias Ringwald uint8_t crc8_calc(uint8_t *data, uint16_t len) 353*eb886013SMatthias Ringwald { 354*eb886013SMatthias Ringwald /* Ones complement */ 355*eb886013SMatthias Ringwald return 0xFF - crc8(data, len); 356*eb886013SMatthias Ringwald } 357*eb886013SMatthias Ringwald 358