From becaf02ab63a67d584fcaa64fe649b69bb7a931f Mon Sep 17 00:00:00 2001 From: Jef Driesen Date: Thu, 15 Feb 2018 08:25:28 +0100 Subject: [PATCH] Add functions for converting bluetooth addresses --- src/bluetooth.c | 49 +++++++++++++++++++++++++++++++++++++++++++++++++ src/bluetooth.h | 34 ++++++++++++++++++++++++++++++++++ 2 files changed, 83 insertions(+) diff --git a/src/bluetooth.c b/src/bluetooth.c index e6dc09d..08347d5 100644 --- a/src/bluetooth.c +++ b/src/bluetooth.c @@ -24,6 +24,7 @@ #endif #include // malloc, free +#include #include "socket.h" @@ -223,6 +224,54 @@ error: #endif #endif +char * +dc_bluetooth_addr2str(dc_bluetooth_address_t address, char *str, size_t size) +{ + if (str == NULL || size < DC_BLUETOOTH_SIZE) + return NULL; + + int n = snprintf(str, size, "%02X:%02X:%02X:%02X:%02X:%02X", + (unsigned char)((address >> 40) & 0xFF), + (unsigned char)((address >> 32) & 0xFF), + (unsigned char)((address >> 24) & 0xFF), + (unsigned char)((address >> 16) & 0xFF), + (unsigned char)((address >> 8) & 0xFF), + (unsigned char)((address >> 0) & 0xFF)); + if (n < 0 || (size_t) n >= size) + return NULL; + + return str; +} + +dc_bluetooth_address_t +dc_bluetooth_str2addr(const char *str) +{ + dc_bluetooth_address_t address = 0; + + if (str == NULL) + return 0; + + unsigned char c = 0; + while ((c = *str++) != '\0') { + if (c == ':') { + continue; + } else if (c >= '0' && c <= '9') { + c -= '0'; + } else if (c >= 'A' && c <= 'F') { + c -= 'A' - 10; + } else if (c >= 'a' && c <= 'f') { + c -= 'a' - 10; + } else { + return 0; /* Invalid character! */ + } + + address <<= 4; + address |= c; + } + + return address; +} + dc_bluetooth_address_t dc_bluetooth_device_get_address (dc_bluetooth_device_t *device) { diff --git a/src/bluetooth.h b/src/bluetooth.h index bc5bb2a..5b443a4 100644 --- a/src/bluetooth.h +++ b/src/bluetooth.h @@ -32,6 +32,12 @@ extern "C" { #endif /* __cplusplus */ +/** + * The minimum number of bytes (including the terminating null byte) for + * formatting a bluetooth address as a string. + */ +#define DC_BLUETOOTH_SIZE 18 + /** * Bluetooth address (48 bits). */ @@ -41,6 +47,34 @@ typedef unsigned __int64 dc_bluetooth_address_t; typedef unsigned long long dc_bluetooth_address_t; #endif +/** + * Convert a bluetooth address to a string. + * + * The bluetooth address is formatted as XX:XX:XX:XX:XX:XX, where each + * XX is a hexadecimal number specifying an octet of the 48-bit address. + * The minimum size for the buffer is #DC_BLUETOOTH_SIZE bytes. + * + * @param[in] address A bluetooth address. + * @param[in] str The memory buffer to store the result. + * @param[in] size The size of the memory buffer. + * @returns The null-terminated string on success, or NULL on failure. + */ +char * +dc_bluetooth_addr2str(dc_bluetooth_address_t address, char *str, size_t size); + +/** + * Convert a string to a bluetooth address. + * + * The string is expected to be in the format XX:XX:XX:XX:XX:XX, where + * each XX is a hexadecimal number specifying an octet of the 48-bit + * address. + * + * @param[in] address A null-terminated string. + * @returns The bluetooth address on success, or zero on failure. + */ +dc_bluetooth_address_t +dc_bluetooth_str2addr(const char *address); + /** * Opaque object representing a bluetooth device. */