Zephyr API Documentation 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
28
29#define BT_ADDR_LE_PUBLIC 0x00
30#define BT_ADDR_LE_RANDOM 0x01
31#define BT_ADDR_LE_PUBLIC_ID 0x02
32#define BT_ADDR_LE_RANDOM_ID 0x03
33#define BT_ADDR_LE_UNRESOLVED 0xFE /* Resolvable Private Address
34 * (Controller unable to resolve)
35 */
36#define BT_ADDR_LE_ANONYMOUS 0xFF /* No address provided
37 * (anonymous advertisement)
38 */
39
40/** Length in bytes of a standard Bluetooth address */
41#define BT_ADDR_SIZE 6
42
43
44typedef struct {
47/**/
48
49/** Length in bytes of an LE Bluetooth address. Not packed, so no sizeof() */
50#define BT_ADDR_LE_SIZE 7
53typedef struct {
57
58/* Global Bluetooth address constants defined in bluetooth/common/addr.c */
59extern const bt_addr_t bt_addr_any;
60extern const bt_addr_t bt_addr_none;
62extern const bt_addr_le_t bt_addr_le_none;
65#define BT_ADDR_ANY (&bt_addr_any)
67#define BT_ADDR_NONE (&bt_addr_none)
69#define BT_ADDR_LE_ANY (&bt_addr_le_any)
71#define BT_ADDR_LE_NONE (&bt_addr_le_none)
72
76 * @param b Second Bluetooth device address to compare
77 *
78 * @return negative value if @a a < @a b, 0 if @a a == @a b, else positive
79 */
80static inline int bt_addr_cmp(const bt_addr_t *a, const bt_addr_t *b)
81{
82 return memcmp(a, b, sizeof(*a));
83}
84
86 *
87 * @retval true if the two addresses are equal
88 * @retval false otherwise
89 */
90static inline bool bt_addr_eq(const bt_addr_t *a, const bt_addr_t *b)
91{
92 return bt_addr_cmp(a, b) == 0;
93}
94
100 * @return negative value if @a a < @a b, 0 if @a a == @a b, else positive
101 *
102 * @sa bt_addr_le_eq
103 */
104static inline int bt_addr_le_cmp(const bt_addr_le_t *a, const bt_addr_le_t *b)
105{
106 return memcmp(a, b, sizeof(*a));
107}
108
114 * @retval true if the two addresses are equal
115 * @retval false otherwise
116 */
117static inline bool bt_addr_le_eq(const bt_addr_le_t *a, const bt_addr_le_t *b)
118{
119 return bt_addr_le_cmp(a, b) == 0;
120}
121
124 * @param dst Bluetooth device address destination buffer.
125 * @param src Bluetooth device address source buffer.
126 */
127static inline void bt_addr_copy(bt_addr_t *dst, const bt_addr_t *src)
128{
129 memcpy(dst, src, sizeof(*dst));
130}
131
134 * @param dst Bluetooth LE device address destination buffer.
135 * @param src Bluetooth LE device address source buffer.
136 */
137static inline void bt_addr_le_copy(bt_addr_le_t *dst, const bt_addr_le_t *src)
138{
139 memcpy(dst, src, sizeof(*dst));
140}
141
144 * @param dst Bluetooth LE device address destination buffer.
145 * @param src Bluetooth device address source buffer.
146 * @param src_type Bluetooth device address source type.
147 */
148static inline void bt_addr_le_copy_addr(bt_addr_le_t *dst, const bt_addr_t *src, uint8_t src_type)
149{
150 bt_addr_copy(&dst->a, src);
151 dst->type = src_type;
152}
153
155#define BT_ADDR_IS_RPA(a) (((a)->val[5] & 0xc0) == 0x40)
158#define BT_ADDR_IS_NRPA(a) (((a)->val[5] & 0xc0) == 0x00)
160#define BT_ADDR_IS_STATIC(a) (((a)->val[5] & 0xc0) == 0xc0)
163#define BT_ADDR_SET_RPA(a) ((a)->val[5] = (((a)->val[5] & 0x3f) | 0x40))
165#define BT_ADDR_SET_NRPA(a) ((a)->val[5] &= 0x3f)
167#define BT_ADDR_SET_STATIC(a) ((a)->val[5] |= 0xc0)
168
171
174
178 * @param addr Bluetooth LE device address.
179 *
180 * @return true if address is a random private resolvable address.
181 */
182static inline bool bt_addr_le_is_rpa(const bt_addr_le_t *addr)
183{
184 if (addr->type != BT_ADDR_LE_RANDOM) {
185 return false;
186 }
187
188 return BT_ADDR_IS_RPA(&addr->a);
189}
190
196 * @param addr Bluetooth LE device address.
197 *
198 * @return true if address is a valid identity address.
199 */
200static inline bool bt_addr_le_is_identity(const bt_addr_le_t *addr)
201{
202 if (addr->type == BT_ADDR_LE_PUBLIC) {
203 return true;
204 }
205
206 return BT_ADDR_IS_STATIC(&addr->a);
207}
208
216#define BT_ADDR_STR_LEN 18
217
225#define BT_ADDR_LE_STR_LEN 30
226
233 * BT_ADDR_STR_LEN about recommended value.
234 *
235 * @return Number of successfully formatted bytes from binary address.
236 */
237static inline int bt_addr_to_str(const bt_addr_t *addr, char *str, size_t len)
238{
239 return snprintk(str, len, "%02X:%02X:%02X:%02X:%02X:%02X",
240 addr->val[5], addr->val[4], addr->val[3],
241 addr->val[2], addr->val[1], addr->val[0]);
242}
243
250 * BT_ADDR_LE_STR_LEN about recommended value.
251 *
252 * @return Number of successfully formatted bytes from binary address.
253 */
254static inline int bt_addr_le_to_str(const bt_addr_le_t *addr, char *str,
255 size_t len)
256{
257 char type[10];
258
259 switch (addr->type) {
261 strcpy(type, "public");
262 break;
264 strcpy(type, "random");
265 break;
267 strcpy(type, "public-id");
268 break;
270 strcpy(type, "random-id");
271 break;
272 default:
273 snprintk(type, sizeof(type), "0x%02x", addr->type);
274 break;
275 }
276
277 return snprintk(str, len, "%02X:%02X:%02X:%02X:%02X:%02X (%s)",
278 addr->a.val[5], addr->a.val[4], addr->a.val[3],
279 addr->a.val[2], addr->a.val[1], addr->a.val[0], type);
280}
281
283struct bt_addr_tmp_str {
284 char str[BT_ADDR_STR_LEN];
285};
286
287struct bt_addr_tmp_str bt_addr_tmp_str(const bt_addr_t *addr);
288
289struct bt_addr_le_tmp_str {
290 char str[BT_ADDR_LE_STR_LEN];
291};
292
293struct bt_addr_le_tmp_str bt_addr_le_tmp_str(const bt_addr_le_t *addr);
295
306#define bt_addr_str(_addr) bt_addr_tmp_str(_addr).str
307
318#define bt_addr_le_str(_addr) bt_addr_le_tmp_str(_addr).str
319
328int bt_addr_from_str(const char *str, bt_addr_t *addr);
329
339int bt_addr_le_from_str(const char *str, const char *type, bt_addr_le_t *addr);
340
344
345#ifdef __cplusplus
346}
347#endif
348
349#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:156
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:233
#define BT_ADDR_LE_PUBLIC
Definition addr.h:29
#define BT_ADDR_LE_PUBLIC_ID
Definition addr.h:31
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.
#define BT_ADDR_IS_RPA(a)
Check if a Bluetooth LE random address is resolvable private address.
Definition addr.h:151
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:76
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:178
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:100
static void bt_addr_copy(bt_addr_t *dst, const bt_addr_t *src)
Copy Bluetooth device address.
Definition addr.h:123
#define BT_ADDR_SIZE
Length in bytes of a standard Bluetooth address.
Definition addr.h:37
#define BT_ADDR_LE_RANDOM
Definition addr.h:30
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:250
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:144
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:196
#define BT_ADDR_STR_LEN
Recommended length of user string buffer for Bluetooth address.
Definition addr.h:212
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:113
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:133
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:86
#define BT_ADDR_LE_STR_LEN
Recommended length of user string buffer for Bluetooth LE address.
Definition addr.h:221
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:32
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:49
uint8_t type
Definition addr.h:50
bt_addr_t a
Definition addr.h:51
Bluetooth Device Address.
Definition addr.h:40
uint8_t val[6]
Definition addr.h:41