diff --git a/include/bluetooth/addr.h b/include/bluetooth/addr.h new file mode 100644 index 00000000000..201c3a5df67 --- /dev/null +++ b/include/bluetooth/addr.h @@ -0,0 +1,95 @@ +/** @file + * @brief Bluetooth device address definitions and utilities. + */ + +/* + * Copyright (c) 2019 Nordic Semiconductor ASA + * + * SPDX-License-Identifier: Apache-2.0 + */ +#ifndef ZEPHYR_INCLUDE_BLUETOOTH_ADDR_H_ +#define ZEPHYR_INCLUDE_BLUETOOTH_ADDR_H_ + +#include + +#ifdef __cplusplus +extern "C" { +#endif + +#define BT_ADDR_LE_PUBLIC 0x00 +#define BT_ADDR_LE_RANDOM 0x01 +#define BT_ADDR_LE_PUBLIC_ID 0x02 +#define BT_ADDR_LE_RANDOM_ID 0x03 + +/** Bluetooth Device Address */ +typedef struct { + u8_t val[6]; +} bt_addr_t; + +/** Bluetooth LE Device Address */ +typedef struct { + u8_t type; + bt_addr_t a; +} bt_addr_le_t; + +#define BT_ADDR_ANY (&(bt_addr_t) { { 0, 0, 0, 0, 0, 0 } }) +#define BT_ADDR_NONE (&(bt_addr_t) { \ + { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff } }) +#define BT_ADDR_LE_ANY (&(bt_addr_le_t) { 0, { { 0, 0, 0, 0, 0, 0 } } }) +#define BT_ADDR_LE_NONE (&(bt_addr_le_t) { 0, \ + { { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff } } }) + +static inline int bt_addr_cmp(const bt_addr_t *a, const bt_addr_t *b) +{ + return memcmp(a, b, sizeof(*a)); +} + +static inline int bt_addr_le_cmp(const bt_addr_le_t *a, const bt_addr_le_t *b) +{ + return memcmp(a, b, sizeof(*a)); +} + +static inline void bt_addr_copy(bt_addr_t *dst, const bt_addr_t *src) +{ + memcpy(dst, src, sizeof(*dst)); +} + +static inline void bt_addr_le_copy(bt_addr_le_t *dst, const bt_addr_le_t *src) +{ + memcpy(dst, src, sizeof(*dst)); +} + +#define BT_ADDR_IS_RPA(a) (((a)->val[5] & 0xc0) == 0x40) +#define BT_ADDR_IS_NRPA(a) (((a)->val[5] & 0xc0) == 0x00) +#define BT_ADDR_IS_STATIC(a) (((a)->val[5] & 0xc0) == 0xc0) + +#define BT_ADDR_SET_RPA(a) ((a)->val[5] = (((a)->val[5] & 0x3f) | 0x40)) +#define BT_ADDR_SET_NRPA(a) ((a)->val[5] &= 0x3f) +#define BT_ADDR_SET_STATIC(a) ((a)->val[5] |= 0xc0) + +int bt_addr_le_create_nrpa(bt_addr_le_t *addr); +int bt_addr_le_create_static(bt_addr_le_t *addr); + +static inline bool bt_addr_le_is_rpa(const bt_addr_le_t *addr) +{ + if (addr->type != BT_ADDR_LE_RANDOM) { + return false; + } + + return BT_ADDR_IS_RPA(&addr->a); +} + +static inline bool bt_addr_le_is_identity(const bt_addr_le_t *addr) +{ + if (addr->type == BT_ADDR_LE_PUBLIC) { + return true; + } + + return BT_ADDR_IS_STATIC(&addr->a); +} + +#ifdef __cplusplus +} +#endif + +#endif /* ZEPHYR_INCLUDE_BLUETOOTH_ADDR_H_ */ diff --git a/include/bluetooth/bluetooth.h b/include/bluetooth/bluetooth.h index 57a401d9ed3..ef1c03e9d5b 100644 --- a/include/bluetooth/bluetooth.h +++ b/include/bluetooth/bluetooth.h @@ -23,6 +23,7 @@ #include #include #include +#include #include #ifdef __cplusplus diff --git a/include/bluetooth/conn.h b/include/bluetooth/conn.h index 6629046add9..691521ff703 100644 --- a/include/bluetooth/conn.h +++ b/include/bluetooth/conn.h @@ -21,6 +21,7 @@ #include #include +#include #ifdef __cplusplus extern "C" { diff --git a/include/bluetooth/hci.h b/include/bluetooth/hci.h index bdcab27e32c..c77f4a43287 100644 --- a/include/bluetooth/hci.h +++ b/include/bluetooth/hci.h @@ -14,88 +14,17 @@ #include #include #include +#include #ifdef __cplusplus extern "C" { #endif -#define BT_ADDR_LE_PUBLIC 0x00 -#define BT_ADDR_LE_RANDOM 0x01 -#define BT_ADDR_LE_PUBLIC_ID 0x02 -#define BT_ADDR_LE_RANDOM_ID 0x03 - /* Special own address types for LL privacy (used in adv & scan parameters) */ #define BT_HCI_OWN_ADDR_RPA_OR_PUBLIC 0x02 #define BT_HCI_OWN_ADDR_RPA_OR_RANDOM 0x03 #define BT_HCI_OWN_ADDR_RPA_MASK 0x02 -/** Bluetooth Device Address */ -typedef struct { - u8_t val[6]; -} bt_addr_t; - -/** Bluetooth LE Device Address */ -typedef struct { - u8_t type; - bt_addr_t a; -} bt_addr_le_t; - -#define BT_ADDR_ANY (&(bt_addr_t) { { 0, 0, 0, 0, 0, 0 } }) -#define BT_ADDR_NONE (&(bt_addr_t) { \ - { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff } }) -#define BT_ADDR_LE_ANY (&(bt_addr_le_t) { 0, { { 0, 0, 0, 0, 0, 0 } } }) -#define BT_ADDR_LE_NONE (&(bt_addr_le_t) { 0, \ - { { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff } } }) - -static inline int bt_addr_cmp(const bt_addr_t *a, const bt_addr_t *b) -{ - return memcmp(a, b, sizeof(*a)); -} - -static inline int bt_addr_le_cmp(const bt_addr_le_t *a, const bt_addr_le_t *b) -{ - return memcmp(a, b, sizeof(*a)); -} - -static inline void bt_addr_copy(bt_addr_t *dst, const bt_addr_t *src) -{ - memcpy(dst, src, sizeof(*dst)); -} - -static inline void bt_addr_le_copy(bt_addr_le_t *dst, const bt_addr_le_t *src) -{ - memcpy(dst, src, sizeof(*dst)); -} - -#define BT_ADDR_IS_RPA(a) (((a)->val[5] & 0xc0) == 0x40) -#define BT_ADDR_IS_NRPA(a) (((a)->val[5] & 0xc0) == 0x00) -#define BT_ADDR_IS_STATIC(a) (((a)->val[5] & 0xc0) == 0xc0) - -#define BT_ADDR_SET_RPA(a) ((a)->val[5] = (((a)->val[5] & 0x3f) | 0x40)) -#define BT_ADDR_SET_NRPA(a) ((a)->val[5] &= 0x3f) -#define BT_ADDR_SET_STATIC(a) ((a)->val[5] |= 0xc0) - -int bt_addr_le_create_nrpa(bt_addr_le_t *addr); -int bt_addr_le_create_static(bt_addr_le_t *addr); - -static inline bool bt_addr_le_is_rpa(const bt_addr_le_t *addr) -{ - if (addr->type != BT_ADDR_LE_RANDOM) { - return false; - } - - return BT_ADDR_IS_RPA(&addr->a); -} - -static inline bool bt_addr_le_is_identity(const bt_addr_le_t *addr) -{ - if (addr->type == BT_ADDR_LE_PUBLIC) { - return true; - } - - return BT_ADDR_IS_STATIC(&addr->a); -} - #define BT_ENC_KEY_SIZE_MIN 0x07 #define BT_ENC_KEY_SIZE_MAX 0x10