123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257 |
- #ifndef ZEPHYR_INCLUDE_BLUETOOTH_ADDR_H_
- #define ZEPHYR_INCLUDE_BLUETOOTH_ADDR_H_
- #include <string.h>
- #include <sys/printk.h>
- #include <zephyr/types.h>
- #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
- typedef struct {
- uint8_t val[6];
- } bt_addr_t;
- typedef struct {
- uint8_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_ADDR_STR_LEN 18
- #define BT_ADDR_LE_STR_LEN 30
- static inline int bt_addr_to_str(const bt_addr_t *addr, char *str, size_t len)
- {
- return snprintk(str, len, "%02X:%02X:%02X:%02X:%02X:%02X",
- addr->val[5], addr->val[4], addr->val[3],
- addr->val[2], addr->val[1], addr->val[0]);
- }
- static inline int bt_addr_le_to_str(const bt_addr_le_t *addr, char *str,
- size_t len)
- {
- char type[10];
- switch (addr->type) {
- case BT_ADDR_LE_PUBLIC:
- strcpy(type, "public");
- break;
- case BT_ADDR_LE_RANDOM:
- strcpy(type, "random");
- break;
- case BT_ADDR_LE_PUBLIC_ID:
- strcpy(type, "public-id");
- break;
- case BT_ADDR_LE_RANDOM_ID:
- strcpy(type, "random-id");
- break;
- default:
- snprintk(type, sizeof(type), "0x%02x", addr->type);
- break;
- }
- return snprintk(str, len, "%02X:%02X:%02X:%02X:%02X:%02X (%s)",
- addr->a.val[5], addr->a.val[4], addr->a.val[3],
- addr->a.val[2], addr->a.val[1], addr->a.val[0], type);
- }
- int bt_addr_from_str(const char *str, bt_addr_t *addr);
- int bt_addr_le_from_str(const char *str, const char *type, bt_addr_le_t *addr);
- #ifdef __cplusplus
- }
- #endif
- #endif
|