Zephyr Project API 4.4.99
A Scalable Open Source RTOS
Loading...
Searching...
No Matches
addr.h
Go to the documentation of this file.
1
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 <stdint.h>
14#include <string.h>
15
16#include <zephyr/sys/printk.h>
17
18#ifdef __cplusplus
19extern "C" {
20#endif
21
30
31#define BT_ADDR_LE_PUBLIC 0x00
32#define BT_ADDR_LE_RANDOM 0x01
33#define BT_ADDR_LE_PUBLIC_ID 0x02
34#define BT_ADDR_LE_RANDOM_ID 0x03
35#define BT_ADDR_LE_UNRESOLVED 0xFE /* Resolvable Private Address
36 * (Controller unable to resolve)
37 */
38#define BT_ADDR_LE_ANONYMOUS 0xFF /* No address provided
39 * (anonymous advertisement)
40 */
41
42/** Length in bytes of a standard Bluetooth address */
43#define BT_ADDR_SIZE 6
44
45
46typedef struct {
49/**/
50
51/** Length in bytes of an LE Bluetooth address. Not packed, so no sizeof() */
52#define BT_ADDR_LE_SIZE 7
55typedef struct {
59
60/* Global Bluetooth address constants defined in bluetooth/common/addr.c */
61extern const bt_addr_t bt_addr_any;
62extern const bt_addr_t bt_addr_none;
64extern const bt_addr_le_t bt_addr_le_none;
67#define BT_ADDR_ANY (&bt_addr_any)
69#define BT_ADDR_NONE (&bt_addr_none)
71#define BT_ADDR_LE_ANY (&bt_addr_le_any)
73#define BT_ADDR_LE_NONE (&bt_addr_le_none)
74
78 * @param b Second Bluetooth device address to compare
79 *
80 * @return negative value if @a a < @a b, 0 if @a a == @a b, else positive
81 */
82static inline int bt_addr_cmp(const bt_addr_t *a, const bt_addr_t *b)
83{
84 return memcmp(a, b, sizeof(*a));
85}
86
88 *
89 * @retval true if the two addresses are equal
90 * @retval false otherwise
91 */
92static inline bool bt_addr_eq(const bt_addr_t *a, const bt_addr_t *b)
93{
94 return bt_addr_cmp(a, b) == 0;
95}
96
102 * @return negative value if @a a < @a b, 0 if @a a == @a b, else positive
103 *
104 * @sa bt_addr_le_eq
105 */
106static inline int bt_addr_le_cmp(const bt_addr_le_t *a, const bt_addr_le_t *b)
107{
108 return memcmp(a, b, sizeof(*a));
109}
110
116 * @retval true if the two addresses are equal
117 * @retval false otherwise
118 */
119static inline bool bt_addr_le_eq(const bt_addr_le_t *a, const bt_addr_le_t *b)
120{
121 return bt_addr_le_cmp(a, b) == 0;
122}
123
126 * @param dst Bluetooth device address destination buffer.
127 * @param src Bluetooth device address source buffer.
128 */
129static inline void bt_addr_copy(bt_addr_t *dst, const bt_addr_t *src)
130{
131 memcpy(dst, src, sizeof(*dst));
132}
133
136 * @param dst Bluetooth LE device address destination buffer.
137 * @param src Bluetooth LE device address source buffer.
138 */
139static inline void bt_addr_le_copy(bt_addr_le_t *dst, const bt_addr_le_t *src)
140{
141 memcpy(dst, src, sizeof(*dst));
142}
143
146 * @param dst Bluetooth LE device address destination buffer.
147 * @param src Bluetooth device address source buffer.
148 * @param src_type Bluetooth device address source type.
149 */
150static inline void bt_addr_le_copy_addr(bt_addr_le_t *dst, const bt_addr_t *src, uint8_t src_type)
151{
152 bt_addr_copy(&dst->a, src);
153 dst->type = src_type;
154}
155
157#define BT_ADDR_IS_RPA(a) (((a)->val[5] & 0xc0) == 0x40)
160#define BT_ADDR_IS_NRPA(a) (((a)->val[5] & 0xc0) == 0x00)
162#define BT_ADDR_IS_STATIC(a) (((a)->val[5] & 0xc0) == 0xc0)
165#define BT_ADDR_SET_RPA(a) ((a)->val[5] = (((a)->val[5] & 0x3f) | 0x40))
167#define BT_ADDR_SET_NRPA(a) ((a)->val[5] &= 0x3f)
169#define BT_ADDR_SET_STATIC(a) ((a)->val[5] |= 0xc0)
170
173
176
180 * @param addr Bluetooth LE device address.
181 *
182 * @return true if address is a random private resolvable address.
183 */
184static inline bool bt_addr_le_is_rpa(const bt_addr_le_t *addr)
185{
186 if (addr->type != BT_ADDR_LE_RANDOM) {
187 return false;
188 }
189
190 return BT_ADDR_IS_RPA(&addr->a);
191}
192
198 * @param addr Bluetooth LE device address.
199 *
200 * @return true if address is a valid identity address.
201 */
202static inline bool bt_addr_le_is_identity(const bt_addr_le_t *addr)
203{
204 if (addr->type == BT_ADDR_LE_PUBLIC) {
205 return true;
206 }
207
208 return BT_ADDR_IS_STATIC(&addr->a);
209}
210
218#define BT_ADDR_STR_LEN 18
219
227#define BT_ADDR_LE_STR_LEN 30
228
235 * BT_ADDR_STR_LEN about recommended value.
236 *
237 * @return Number of successfully formatted bytes from binary address.
238 */
239static inline int bt_addr_to_str(const bt_addr_t *addr, char *str, size_t len)
240{
241 return snprintk(str, len, "%02X:%02X:%02X:%02X:%02X:%02X",
242 addr->val[5], addr->val[4], addr->val[3],
243 addr->val[2], addr->val[1], addr->val[0]);
244}
245
252 * BT_ADDR_LE_STR_LEN about recommended value.
253 *
254 * @return Number of successfully formatted bytes from binary address.
255 */
256static inline int bt_addr_le_to_str(const bt_addr_le_t *addr, char *str,
257 size_t len)
258{
259 char type[10];
260
261 switch (addr->type) {
263 strcpy(type, "public");
264 break;
266 strcpy(type, "random");
267 break;
269 strcpy(type, "public-id");
270 break;
272 strcpy(type, "random-id");
273 break;
274 default:
275 snprintk(type, sizeof(type), "0x%02x", addr->type);
276 break;
277 }
278
279 return snprintk(str, len, "%02X:%02X:%02X:%02X:%02X:%02X (%s)",
280 addr->a.val[5], addr->a.val[4], addr->a.val[3],
281 addr->a.val[2], addr->a.val[1], addr->a.val[0], type);
282}
283
285struct bt_addr_tmp_str {
286 char str[BT_ADDR_STR_LEN];
287};
288
289struct bt_addr_tmp_str bt_addr_tmp_str(const bt_addr_t *addr);
290
291struct bt_addr_le_tmp_str {
292 char str[BT_ADDR_LE_STR_LEN];
293};
294
295struct bt_addr_le_tmp_str bt_addr_le_tmp_str(const bt_addr_le_t *addr);
297
308#define bt_addr_str(_addr) bt_addr_tmp_str(_addr).str
309
320#define bt_addr_le_str(_addr) bt_addr_le_tmp_str(_addr).str
321
330int bt_addr_from_str(const char *str, bt_addr_t *addr);
331
341int bt_addr_le_from_str(const char *str, const char *type, bt_addr_le_t *addr);
342
366int bt_addr_le_rpa_resolve(uint8_t id, const bt_addr_le_t *addr, bt_addr_le_t *resolved_addr);
367
371
372#ifdef __cplusplus
373}
374#endif
375
376#endif /* ZEPHYR_INCLUDE_BLUETOOTH_ADDR_H_ */
const bt_addr_t bt_addr_none
#define BT_ADDR_IS_STATIC(a)
Check if a Bluetooth LE random address is a static address.
Definition addr.h:158
static int bt_addr_to_str(const bt_addr_t *addr, char *str, size_t len)
Converts binary Bluetooth address to string.
Definition addr.h:235
#define BT_ADDR_LE_PUBLIC
Definition addr.h:31
#define BT_ADDR_LE_PUBLIC_ID
Definition addr.h:33
int bt_addr_le_from_str(const char *str, const char *type, bt_addr_le_t *addr)
Convert LE Bluetooth address from string to binary.
int bt_addr_le_rpa_resolve(uint8_t id, const bt_addr_le_t *addr, bt_addr_le_t *resolved_addr)
Resolve a Bluetooth LE Resolvable Private Address (RPA) to its identity address.
#define BT_ADDR_IS_RPA(a)
Check if a Bluetooth LE random address is resolvable private address.
Definition addr.h:153
const bt_addr_le_t bt_addr_le_any
static int bt_addr_cmp(const bt_addr_t *a, const bt_addr_t *b)
Compare Bluetooth device addresses.
Definition addr.h:78
static bool bt_addr_le_is_rpa(const bt_addr_le_t *addr)
Check if a Bluetooth LE address is a random private resolvable address.
Definition addr.h:180
static int bt_addr_le_cmp(const bt_addr_le_t *a, const bt_addr_le_t *b)
Compare Bluetooth LE device addresses.
Definition addr.h:102
static void bt_addr_copy(bt_addr_t *dst, const bt_addr_t *src)
Copy Bluetooth device address.
Definition addr.h:125
#define BT_ADDR_SIZE
Length in bytes of a standard Bluetooth address.
Definition addr.h:39
#define BT_ADDR_LE_RANDOM
Definition addr.h:32
static int bt_addr_le_to_str(const bt_addr_le_t *addr, char *str, size_t len)
Converts binary LE Bluetooth address to string.
Definition addr.h:252
static void bt_addr_le_copy_addr(bt_addr_le_t *dst, const bt_addr_t *src, uint8_t src_type)
Copy Bluetooth LE device address from Bluetooth device address and type.
Definition addr.h:146
static bool bt_addr_le_is_identity(const bt_addr_le_t *addr)
Check if a Bluetooth LE address is valid identity address.
Definition addr.h:198
#define BT_ADDR_STR_LEN
Recommended length of user string buffer for Bluetooth address.
Definition addr.h:214
static bool bt_addr_le_eq(const bt_addr_le_t *a, const bt_addr_le_t *b)
Determine equality of two Bluetooth LE device addresses.
Definition addr.h:115
static void bt_addr_le_copy(bt_addr_le_t *dst, const bt_addr_le_t *src)
Copy Bluetooth LE device address.
Definition addr.h:135
int bt_addr_le_create_static(bt_addr_le_t *addr)
Create a Bluetooth LE random static address.
int bt_addr_from_str(const char *str, bt_addr_t *addr)
Convert Bluetooth address from string to binary.
const bt_addr_t bt_addr_any
static bool bt_addr_eq(const bt_addr_t *a, const bt_addr_t *b)
Determine equality of two Bluetooth device addresses.
Definition addr.h:88
#define BT_ADDR_LE_STR_LEN
Recommended length of user string buffer for Bluetooth LE address.
Definition addr.h:223
const bt_addr_le_t bt_addr_le_none
int bt_addr_le_create_nrpa(bt_addr_le_t *addr)
Create a Bluetooth LE random non-resolvable private address.
#define BT_ADDR_LE_RANDOM_ID
Definition addr.h:34
int snprintk(char *str, size_t size, const char *fmt,...)
__UINT8_TYPE__ uint8_t
Definition stdint.h:88
char * strcpy(char *ZRESTRICT d, const char *ZRESTRICT s)
int memcmp(const void *m1, const void *m2, size_t n)
void * memcpy(void *ZRESTRICT d, const void *ZRESTRICT s, size_t n)
Bluetooth LE Device Address.
Definition addr.h:51
uint8_t type
Definition addr.h:52
bt_addr_t a
Definition addr.h:53
Bluetooth Device Address.
Definition addr.h:42
uint8_t val[6]
Definition addr.h:43