1 /** @file
2 * @brief Bluetooth device address definitions and utilities.
3 */
4
5 /*
6 * Copyright (c) 2019 Nordic Semiconductor ASA
7 *
8 * SPDX-License-Identifier: Apache-2.0
9 */
10 #ifndef ZEPHYR_INCLUDE_BLUETOOTH_ADDR_H_
11 #define ZEPHYR_INCLUDE_BLUETOOTH_ADDR_H_
12
13 #include <string.h>
14 #include <ble_types/types.h>
15
16 #ifdef __cplusplus
17 extern "C" {
18 #endif
19
20 /**
21 * @brief Bluetooth device address definitions and utilities.
22 * @defgroup bt_addr Device Address
23 * @ingroup bluetooth
24 * @{
25 */
26
27 #define BT_ADDR_LE_PUBLIC 0x00
28 #define BT_ADDR_LE_RANDOM 0x01
29 #define BT_ADDR_LE_PUBLIC_ID 0x02
30 #define BT_ADDR_LE_RANDOM_ID 0x03
31
32 /** Bluetooth Device Address */
33 typedef struct {
34 u8_t val[6];
35 } bt_addr_t;
36
37 /** Bluetooth LE Device Address */
38 typedef struct {
39 u8_t type;
40 bt_addr_t a;
41 } bt_addr_le_t;
42
43 #define BT_ADDR_ANY ((bt_addr_t[]) { { { 0, 0, 0, 0, 0, 0 } } })
44 #define BT_ADDR_NONE ((bt_addr_t[]) { { \
45 { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff } } })
46 #define BT_ADDR_LE_ANY ((bt_addr_le_t[]) { { 0, { { 0, 0, 0, 0, 0, 0 } } } })
47 #define BT_ADDR_LE_NONE ((bt_addr_le_t[]) { { 0, \
48 { { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff } } } })
49
bt_addr_cmp(const bt_addr_t * a,const bt_addr_t * b)50 static inline int bt_addr_cmp(const bt_addr_t *a, const bt_addr_t *b)
51 {
52 return memcmp(a, b, sizeof(*a));
53 }
54
bt_addr_le_cmp(const bt_addr_le_t * a,const bt_addr_le_t * b)55 static inline int bt_addr_le_cmp(const bt_addr_le_t *a, const bt_addr_le_t *b)
56 {
57 return memcmp(a, b, sizeof(*a));
58 }
59
bt_addr_copy(bt_addr_t * dst,const bt_addr_t * src)60 static inline void bt_addr_copy(bt_addr_t *dst, const bt_addr_t *src)
61 {
62 memcpy(dst, src, sizeof(*dst));
63 }
64
bt_addr_le_copy(bt_addr_le_t * dst,const bt_addr_le_t * src)65 static inline void bt_addr_le_copy(bt_addr_le_t *dst, const bt_addr_le_t *src)
66 {
67 memcpy(dst, src, sizeof(*dst));
68 }
69
70 #define BT_ADDR_IS_RPA(a) (((a)->val[5] & 0xc0) == 0x40)
71 #define BT_ADDR_IS_NRPA(a) (((a)->val[5] & 0xc0) == 0x00)
72 #define BT_ADDR_IS_STATIC(a) (((a)->val[5] & 0xc0) == 0xc0)
73
74 #define BT_ADDR_SET_RPA(a) ((a)->val[5] = (((a)->val[5] & 0x3f) | 0x40))
75 #define BT_ADDR_SET_NRPA(a) ((a)->val[5] &= 0x3f)
76 #define BT_ADDR_SET_STATIC(a) ((a)->val[5] |= 0xc0)
77
78 int bt_addr_le_create_nrpa(bt_addr_le_t *addr);
79 int bt_addr_le_create_static(bt_addr_le_t *addr);
80
bt_addr_le_is_rpa(const bt_addr_le_t * addr)81 static inline bool bt_addr_le_is_rpa(const bt_addr_le_t *addr)
82 {
83 if (addr->type != BT_ADDR_LE_RANDOM) {
84 return false;
85 }
86
87 return BT_ADDR_IS_RPA(&addr->a);
88 }
89
bt_addr_le_is_identity(const bt_addr_le_t * addr)90 static inline bool bt_addr_le_is_identity(const bt_addr_le_t *addr)
91 {
92 if (addr->type == BT_ADDR_LE_PUBLIC) {
93 return true;
94 }
95
96 return BT_ADDR_IS_STATIC(&addr->a);
97 }
98
99 /**
100 * @}
101 */
102
103 #ifdef __cplusplus
104 }
105 #endif
106
107 #endif /* ZEPHYR_INCLUDE_BLUETOOTH_ADDR_H_ */
108