Zephyr Project API  3.3.0
A Scalable Open Source RTOS
addr.h
Go to the documentation of this file.
1
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 <zephyr/sys/printk.h>
15#include <zephyr/types.h>
16
17#ifdef __cplusplus
18extern "C" {
19#endif
20
28#define BT_ADDR_LE_PUBLIC 0x00
29#define BT_ADDR_LE_RANDOM 0x01
30#define BT_ADDR_LE_PUBLIC_ID 0x02
31#define BT_ADDR_LE_RANDOM_ID 0x03
32#define BT_ADDR_LE_UNRESOLVED 0xFE /* Resolvable Private Address
33 * (Controller unable to resolve)
34 */
35#define BT_ADDR_LE_ANONYMOUS 0xFF /* No address provided
36 * (anonymous advertisement)
37 */
38
40#define BT_ADDR_SIZE 6
41
43typedef struct {
46
47
49#define BT_ADDR_LE_SIZE 7
52typedef struct {
53 uint8_t type;
54 bt_addr_t a;
56
57/* Global Bluetooth address constants defined in bluetooth/common/addr.c */
58extern const bt_addr_t bt_addr_any;
59extern const bt_addr_t bt_addr_none;
61extern const bt_addr_le_t bt_addr_le_none;
64#define BT_ADDR_ANY (&bt_addr_any)
66#define BT_ADDR_NONE (&bt_addr_none)
68#define BT_ADDR_LE_ANY (&bt_addr_le_any)
70#define BT_ADDR_LE_NONE (&bt_addr_le_none)
71
79static inline int bt_addr_cmp(const bt_addr_t *a, const bt_addr_t *b)
80{
81 return memcmp(a, b, sizeof(*a));
82}
83
93static inline int bt_addr_le_cmp(const bt_addr_le_t *a, const bt_addr_le_t *b)
94{
95 return memcmp(a, b, sizeof(*a));
96}
97
106static inline bool bt_addr_le_eq(const bt_addr_le_t *a, const bt_addr_le_t *b)
107{
108 return bt_addr_le_cmp(a, b) == 0;
109}
110
116static inline void bt_addr_copy(bt_addr_t *dst, const bt_addr_t *src)
117{
118 memcpy(dst, src, sizeof(*dst));
119}
120
126static inline void bt_addr_le_copy(bt_addr_le_t *dst, const bt_addr_le_t *src)
127{
128 memcpy(dst, src, sizeof(*dst));
129}
130
132#define BT_ADDR_IS_RPA(a) (((a)->val[5] & 0xc0) == 0x40)
135#define BT_ADDR_IS_NRPA(a) (((a)->val[5] & 0xc0) == 0x00)
137#define BT_ADDR_IS_STATIC(a) (((a)->val[5] & 0xc0) == 0xc0)
140#define BT_ADDR_SET_RPA(a) ((a)->val[5] = (((a)->val[5] & 0x3f) | 0x40))
142#define BT_ADDR_SET_NRPA(a) ((a)->val[5] &= 0x3f)
144#define BT_ADDR_SET_STATIC(a) ((a)->val[5] |= 0xc0)
145
148
151
159static inline bool bt_addr_le_is_rpa(const bt_addr_le_t *addr)
160{
161 if (addr->type != BT_ADDR_LE_RANDOM) {
162 return false;
163 }
164
165 return BT_ADDR_IS_RPA(&addr->a);
166}
167
177static inline bool bt_addr_le_is_identity(const bt_addr_le_t *addr)
178{
179 if (addr->type == BT_ADDR_LE_PUBLIC) {
180 return true;
181 }
182
183 return BT_ADDR_IS_STATIC(&addr->a);
184}
185
193#define BT_ADDR_STR_LEN 18
194
202#define BT_ADDR_LE_STR_LEN 30
203
214static inline int bt_addr_to_str(const bt_addr_t *addr, char *str, size_t len)
215{
216 return snprintk(str, len, "%02X:%02X:%02X:%02X:%02X:%02X",
217 addr->val[5], addr->val[4], addr->val[3],
218 addr->val[2], addr->val[1], addr->val[0]);
219}
220
231static inline int bt_addr_le_to_str(const bt_addr_le_t *addr, char *str,
232 size_t len)
233{
234 char type[10];
235
236 switch (addr->type) {
238 strcpy(type, "public");
239 break;
241 strcpy(type, "random");
242 break;
244 strcpy(type, "public-id");
245 break;
247 strcpy(type, "random-id");
248 break;
249 default:
250 snprintk(type, sizeof(type), "0x%02x", addr->type);
251 break;
252 }
253
254 return snprintk(str, len, "%02X:%02X:%02X:%02X:%02X:%02X (%s)",
255 addr->a.val[5], addr->a.val[4], addr->a.val[3],
256 addr->a.val[2], addr->a.val[1], addr->a.val[0], type);
257}
258
266int bt_addr_from_str(const char *str, bt_addr_t *addr);
267
277int bt_addr_le_from_str(const char *str, const char *type, bt_addr_le_t *addr);
278
283#ifdef __cplusplus
284}
285#endif
286
287#endif /* ZEPHYR_INCLUDE_BLUETOOTH_ADDR_H_ */
const bt_addr_t bt_addr_none
#define BT_ADDR_IS_STATIC(a)
Definition: addr.h:133
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:210
#define BT_ADDR_LE_PUBLIC
Definition: addr.h:28
#define BT_ADDR_LE_PUBLIC_ID
Definition: addr.h:30
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)
Definition: addr.h:128
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:75
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:155
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:89
static void bt_addr_copy(bt_addr_t *dst, const bt_addr_t *src)
Copy Bluetooth device address.
Definition: addr.h:112
#define BT_ADDR_SIZE
Definition: addr.h:36
#define BT_ADDR_LE_RANDOM
Definition: addr.h:29
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:227
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:173
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:102
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:122
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
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:31
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)
Definition: addr.h:48
uint8_t type
Definition: addr.h:49
bt_addr_t a
Definition: addr.h:50
Definition: addr.h:39
uint8_t val[6]
Definition: addr.h:40