diff --git a/example/daemon/l2cap_throughput.c b/example/daemon/l2cap_throughput.c index f91b7fce4..2e65a112e 100644 --- a/example/daemon/l2cap_throughput.c +++ b/example/daemon/l2cap_throughput.c @@ -195,7 +195,7 @@ void packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint int main (int argc, const char * argv[]){ // handle remote addr if (argc > 1){ - if (sscan_bd_addr((uint8_t *)argv[1], addr)){ + if (sscanf_bd_addr((uint8_t *)argv[1], addr)){ serverMode = 0; prepare_packet(); } diff --git a/example/daemon/rfcomm_cat.c b/example/daemon/rfcomm_cat.c index 946682431..00bff82c9 100644 --- a/example/daemon/rfcomm_cat.c +++ b/example/daemon/rfcomm_cat.c @@ -138,7 +138,7 @@ int main (int argc, const char * argv[]){ while (arg < argc) { if(!strcmp(argv[arg], "-a") || !strcmp(argv[arg], "--address")){ arg++; - if(arg >= argc || !sscan_bd_addr((uint8_t *)argv[arg], addr)){ + if(arg >= argc || !sscanf_bd_addr((uint8_t *)argv[arg], addr)){ usage(argv[0]); return 1; } diff --git a/example/embedded/gatt_battery_query.c b/example/embedded/gatt_battery_query.c index a21469516..1c6bcac9b 100644 --- a/example/embedded/gatt_battery_query.c +++ b/example/embedded/gatt_battery_query.c @@ -297,7 +297,7 @@ int btstack_main(int argc, const char * argv[]){ while (arg < argc) { if(!strcmp(argv[arg], "-a") || !strcmp(argv[arg], "--address")){ arg++; - cmdline_addr_found = sscan_bd_addr((uint8_t *)argv[arg], cmdline_addr); + cmdline_addr_found = sscanf_bd_addr((uint8_t *)argv[arg], cmdline_addr); arg++; continue; } diff --git a/example/embedded/gatt_browser.c b/example/embedded/gatt_browser.c index d1f978215..c42e2fdac 100644 --- a/example/embedded/gatt_browser.c +++ b/example/embedded/gatt_browser.c @@ -315,7 +315,7 @@ int btstack_main(int argc, const char * argv[]){ while (arg < argc) { if(!strcmp(argv[arg], "-a") || !strcmp(argv[arg], "--address")){ arg++; - cmdline_addr_found = sscan_bd_addr((uint8_t *)argv[arg], cmdline_addr); + cmdline_addr_found = sscanf_bd_addr((uint8_t *)argv[arg], cmdline_addr); arg++; continue; } diff --git a/port/wiced/main.c b/port/wiced/main.c index 85e07f259..efce54eb4 100644 --- a/port/wiced/main.c +++ b/port/wiced/main.c @@ -58,7 +58,7 @@ static const hci_transport_config_uart_t hci_transport_config_uart = { extern int btstack_main(void); -static int main_sscan_bd_addr(const char * addr_string, bd_addr_t addr){ +static int main_sscanf_bd_addr(const char * addr_string, bd_addr_t addr){ unsigned int bd_addr_buffer[BD_ADDR_LEN]; //for sscanf, integer needed // reset result buffer memset(bd_addr_buffer, 0, sizeof(bd_addr_buffer)); @@ -99,7 +99,7 @@ void application_start(void){ // use WIFI Mac address + 1 for Bluetooth bd_addr_t dummy = { 1,2,3,4,5,6}; - main_sscan_bd_addr(&wifi_mac_address[8], dummy); + main_sscanf_bd_addr(&wifi_mac_address[8], dummy); dummy[5]++; hci_set_bd_addr(dummy); diff --git a/src/btstack_util.c b/src/btstack_util.c index 869dd080b..ac06ec8b1 100644 --- a/src/btstack_util.c +++ b/src/btstack_util.c @@ -49,6 +49,36 @@ #include #include "btstack_debug.h" + +/** + * @brief Compare two Bluetooth addresses + * @param a + * @param b + * @return true if equal + */ +int bd_addr_cmp(bd_addr_t a, bd_addr_t b){ + return memcmp(a,b, BD_ADDR_LEN); +} + +/** + * @brief Copy Bluetooth address + * @param dest + * @param src + */ +void bd_addr_copy(bd_addr_t dest, bd_addr_t src){ + memcpy(dest,src,BD_ADDR_LEN); +} + +uint16_t little_endian_read_16(const uint8_t * buffer, int pos){ + return ((uint16_t) buffer[pos]) | (((uint16_t)buffer[(pos)+1]) << 8); +} +uint32_t little_endian_read_24(const uint8_t * buffer, int pos){ + return ((uint32_t) buffer[pos]) | (((uint32_t)buffer[(pos)+1]) << 8) | (((uint32_t)buffer[(pos)+2]) << 16); +} +uint32_t little_endian_read_32(const uint8_t * buffer, int pos){ + return ((uint32_t) buffer[pos]) | (((uint32_t)buffer[(pos)+1]) << 8) | (((uint32_t)buffer[(pos)+2]) << 16) | (((uint32_t) buffer[(pos)+3]) << 24); +} + void little_endian_store_16(uint8_t *buffer, uint16_t pos, uint16_t value){ buffer[pos++] = value; buffer[pos++] = value >> 8; @@ -61,6 +91,14 @@ void little_endian_store_32(uint8_t *buffer, uint16_t pos, uint32_t value){ buffer[pos++] = value >> 24; } +uint32_t big_endian_read_16( const uint8_t * buffer, int pos) { + return ((uint16_t) buffer[(pos)+1]) | (((uint16_t)buffer[ pos ]) << 8); +} + +uint32_t big_endian_read_32( const uint8_t * buffer, int pos) { + return ((uint32_t) buffer[(pos)+3]) | (((uint32_t)buffer[(pos)+2]) << 8) | (((uint32_t)buffer[(pos)+1]) << 16) | (((uint32_t) buffer[pos]) << 24); +} + void big_endian_store_16(uint8_t *buffer, uint16_t pos, uint16_t value){ buffer[pos++] = value >> 8; buffer[pos++] = value; @@ -73,6 +111,7 @@ void big_endian_store_32(uint8_t *buffer, uint16_t pos, uint32_t value){ buffer[pos++] = value; } + void bt_flip_addr(bd_addr_t dest, bd_addr_t src){ dest[0] = src[5]; dest[1] = src[4]; @@ -222,7 +261,7 @@ char * bd_addr_to_str(bd_addr_t addr){ return (char *) bd_addr_to_str_buffer; } -int sscan_bd_addr(uint8_t * addr_string, bd_addr_t addr){ +int sscanf_bd_addr(uint8_t * addr_string, bd_addr_t addr){ unsigned int bd_addr_buffer[BD_ADDR_LEN]; //for sscanf, integer needed // reset result buffer memset(bd_addr_buffer, 0, sizeof(bd_addr_buffer)); diff --git a/src/btstack_util.h b/src/btstack_util.h index b37162bb2..c19b97eb7 100644 --- a/src/btstack_util.h +++ b/src/btstack_util.h @@ -58,6 +58,11 @@ extern "C" { #include "btstack_defines.h" #include "btstack_linked_list.h" +// hack: compilation with the android ndk causes an error as there's a swap64 macro +#ifdef swap64 +#undef swap64 +#endif + // will be moved to daemon/btstack_device_name_db.h /** @@ -65,37 +70,46 @@ extern "C" { */ #define DEVICE_NAME_LEN 248 typedef uint8_t device_name_t[DEVICE_NAME_LEN+1]; + + -// helper for little endian format -static inline uint16_t little_endian_read_16(const uint8_t * buffer, int pos){ - return ((uint16_t) buffer[pos]) | (((uint16_t)buffer[(pos)+1]) << 8); -} -static inline uint32_t little_endian_read_24(const uint8_t * buffer, int pos){ - return ((uint32_t) buffer[pos]) | (((uint32_t)buffer[(pos)+1]) << 8) | (((uint32_t)buffer[(pos)+2]) << 16); -} -static inline uint32_t little_endian_read_32(const uint8_t * buffer, int pos){ - return ((uint32_t) buffer[pos]) | (((uint32_t)buffer[(pos)+1]) << 8) | (((uint32_t)buffer[(pos)+2]) << 16) | (((uint32_t) buffer[(pos)+3]) << 24); -} -void little_endian_store_16(uint8_t *buffer, uint16_t pos, uint16_t value); -void little_endian_store_32(uint8_t *buffer, uint16_t pos, uint32_t value); +/** + * @brief Read 16/24/32 bit little endian value from buffer + * @param buffer + * @param position in buffer + * @return value + */ +uint16_t little_endian_read_16(const uint8_t * buffer, int position); +uint32_t little_endian_read_24(const uint8_t * buffer, int position); +uint32_t little_endian_read_32(const uint8_t * buffer, int position); -// helper for big endian format -static inline uint32_t big_endian_read_16( const uint8_t * buffer, int pos) { - return ((uint16_t) buffer[(pos)+1]) | (((uint16_t)buffer[ pos ]) << 8); -} +/** + * @brief Write 16/32 bit little endian value into buffer + * @param buffer + * @param position in buffer + * @param value + */ +void little_endian_store_16(uint8_t *buffer, uint16_t position, uint16_t value); +void little_endian_store_32(uint8_t *buffer, uint16_t position, uint32_t value); -static inline uint32_t big_endian_read_32( const uint8_t * buffer, int pos) { - return ((uint32_t) buffer[(pos)+3]) | (((uint32_t)buffer[(pos)+2]) << 8) | (((uint32_t)buffer[(pos)+1]) << 16) | (((uint32_t) buffer[pos]) << 24); -} +/** + * @brief Read 16/24/32 bit big endian value from buffer + * @param buffer + * @param position in buffer + * @return value + */ +uint32_t big_endian_read_16( const uint8_t * buffer, int pos); +uint32_t big_endian_read_32( const uint8_t * buffer, int pos); +/** + * @brief Write 16/32 bit big endian value into buffer + * @param buffer + * @param position in buffer + * @param value + */ void big_endian_store_16(uint8_t *buffer, uint16_t pos, uint16_t value); void big_endian_store_32(uint8_t *buffer, uint16_t pos, uint32_t value); -// hack: compilation with the android ndk causes an error as there's a swap64 macro -#ifdef swap64 -#undef swap64 -#endif - /** * @brief Copy from source to destination and reverse byte order */ @@ -105,7 +119,6 @@ void swap48 (const uint8_t *src, uint8_t * dst); void swap56 (const uint8_t *src, uint8_t * dst); void swap64 (const uint8_t *src, uint8_t * dst); void swap128(const uint8_t *src, uint8_t * dst); - void bt_flip_addr(bd_addr_t dest, bd_addr_t src); /** @@ -120,18 +133,14 @@ char char_for_nibble(int nibble); * @param b * @return true if equal */ -static inline int bd_addr_cmp(bd_addr_t a, bd_addr_t b){ - return memcmp(a,b, BD_ADDR_LEN); -} +int bd_addr_cmp(bd_addr_t a, bd_addr_t b); /** * @brief Copy Bluetooth address -s * @param dest + * @param dest * @param src */ -static inline void bd_addr_copy(bd_addr_t dest, bd_addr_t src){ - memcpy(dest,src,BD_ADDR_LEN); -} +void bd_addr_copy(bd_addr_t dest, bd_addr_t src); /** * @brief Use printf to write hexdump as single line of data @@ -166,10 +175,20 @@ char * bd_addr_to_str(bd_addr_t addr); * @param buffer for parsed address * @return 1 if string was parsed successfully */ -int sscan_bd_addr(uint8_t * addr_string, bd_addr_t addr); +int sscanf_bd_addr(uint8_t * addr_string, bd_addr_t addr); +/** + * @brief Constructs UUID128 from 16 or 32 bit UUID using Bluetooth base UUID + * @param uuid128 output buffer + * @param short_uuid + */ +void uuid_add_bluetooth_prefix(uint8_t * uuid128, uint32_t short_uuid); -void uuid_add_bluetooth_prefix(uint8_t *uuid, uint32_t shortUUID); +/** + * @brief Checks if UUID128 has Bluetooth base UUID prefix + * @param uui128 to test + * @return 1 if it can be expressed as UUID32 + */ int uuid_has_bluetooth_prefix(uint8_t * uuid128); #if defined __cplusplus diff --git a/src/classic/bnep.c b/src/classic/bnep.c index c0d23adde..5124dfd52 100644 --- a/src/classic/bnep.c +++ b/src/classic/bnep.c @@ -684,7 +684,7 @@ static bnep_channel_t * bnep_channel_create_for_addr(bd_addr_t addr) channel->state = BNEP_CHANNEL_STATE_CLOSED; channel->max_frame_size = bnep_max_frame_size_for_l2cap_mtu(l2cap_max_mtu()); - bd_addr_copy(&channel->remote_addr, addr); + bd_addr_copy(channel->remote_addr, addr); hci_local_bd_addr(channel->local_addr); channel->net_filter_count = 0; diff --git a/src/classic/rfcomm.c b/src/classic/rfcomm.c index 970a37158..8d13c9167 100644 --- a/src/classic/rfcomm.c +++ b/src/classic/rfcomm.c @@ -253,7 +253,7 @@ static rfcomm_multiplexer_t * rfcomm_multiplexer_create_for_addr(bd_addr_t addr) // fill in rfcomm_multiplexer_initialize(multiplexer); - bd_addr_copy(&multiplexer->remote_addr, addr); + bd_addr_copy(multiplexer->remote_addr, addr); // add to services list btstack_linked_list_add(&rfcomm_multiplexers, (btstack_linked_item_t *) multiplexer);