Zephyr Project
A Scalable Open Source RTOS for IoT Embedded Devices
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 <sys/printk.h>
15 #include <zephyr/types.h>
16 
17 #ifdef __cplusplus
18 extern "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 
34 typedef struct {
35  uint8_t val[6];
36 } bt_addr_t;
37 
39 typedef struct {
42 } bt_addr_le_t;
43 
45 #define BT_ADDR_ANY ((bt_addr_t[]) { { { 0, 0, 0, 0, 0, 0 } } })
46 
47 #define BT_ADDR_NONE ((bt_addr_t[]) { { \
48  { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff } } })
49 
50 #define BT_ADDR_LE_ANY ((bt_addr_le_t[]) { { 0, { { 0, 0, 0, 0, 0, 0 } } } })
51 
52 #define BT_ADDR_LE_NONE ((bt_addr_le_t[]) { { 0, \
53  { { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff } } } })
54 
62 static inline int bt_addr_cmp(const bt_addr_t *a, const bt_addr_t *b)
63 {
64  return memcmp(a, b, sizeof(*a));
65 }
66 
74 static inline int bt_addr_le_cmp(const bt_addr_le_t *a, const bt_addr_le_t *b)
75 {
76  return memcmp(a, b, sizeof(*a));
77 }
78 
84 static inline void bt_addr_copy(bt_addr_t *dst, const bt_addr_t *src)
85 {
86  memcpy(dst, src, sizeof(*dst));
87 }
88 
94 static inline void bt_addr_le_copy(bt_addr_le_t *dst, const bt_addr_le_t *src)
95 {
96  memcpy(dst, src, sizeof(*dst));
97 }
98 
100 #define BT_ADDR_IS_RPA(a) (((a)->val[5] & 0xc0) == 0x40)
101 
103 #define BT_ADDR_IS_NRPA(a) (((a)->val[5] & 0xc0) == 0x00)
104 
105 #define BT_ADDR_IS_STATIC(a) (((a)->val[5] & 0xc0) == 0xc0)
106 
108 #define BT_ADDR_SET_RPA(a) ((a)->val[5] = (((a)->val[5] & 0x3f) | 0x40))
109 
110 #define BT_ADDR_SET_NRPA(a) ((a)->val[5] &= 0x3f)
111 
112 #define BT_ADDR_SET_STATIC(a) ((a)->val[5] |= 0xc0)
113 
116 
119 
127 static inline bool bt_addr_le_is_rpa(const bt_addr_le_t *addr)
128 {
129  if (addr->type != BT_ADDR_LE_RANDOM) {
130  return false;
131  }
132 
133  return BT_ADDR_IS_RPA(&addr->a);
134 }
135 
145 static inline bool bt_addr_le_is_identity(const bt_addr_le_t *addr)
146 {
147  if (addr->type == BT_ADDR_LE_PUBLIC) {
148  return true;
149  }
150 
151  return BT_ADDR_IS_STATIC(&addr->a);
152 }
153 
162 #define BT_ADDR_STR_LEN 18
163 
172 #define BT_ADDR_LE_STR_LEN 30
173 
184 static inline int bt_addr_to_str(const bt_addr_t *addr, char *str, size_t len)
185 {
186  return snprintk(str, len, "%02X:%02X:%02X:%02X:%02X:%02X",
187  addr->val[5], addr->val[4], addr->val[3],
188  addr->val[2], addr->val[1], addr->val[0]);
189 }
190 
201 static inline int bt_addr_le_to_str(const bt_addr_le_t *addr, char *str,
202  size_t len)
203 {
204  char type[10];
205 
206  switch (addr->type) {
207  case BT_ADDR_LE_PUBLIC:
208  strcpy(type, "public");
209  break;
210  case BT_ADDR_LE_RANDOM:
211  strcpy(type, "random");
212  break;
214  strcpy(type, "public-id");
215  break;
217  strcpy(type, "random-id");
218  break;
219  default:
220  snprintk(type, sizeof(type), "0x%02x", addr->type);
221  break;
222  }
223 
224  return snprintk(str, len, "%02X:%02X:%02X:%02X:%02X:%02X (%s)",
225  addr->a.val[5], addr->a.val[4], addr->a.val[3],
226  addr->a.val[2], addr->a.val[1], addr->a.val[0], type);
227 }
228 
236 int bt_addr_from_str(const char *str, bt_addr_t *addr);
237 
247 int bt_addr_le_from_str(const char *str, const char *type, bt_addr_le_t *addr);
248 
253 #ifdef __cplusplus
254 }
255 #endif
256 
257 #endif /* ZEPHYR_INCLUDE_BLUETOOTH_ADDR_H_ */
bt_addr_le_to_str
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:201
bt_addr_le_create_nrpa
int bt_addr_le_create_nrpa(bt_addr_le_t *addr)
Create a Bluetooth LE random non-resolvable private address.
string.h
bt_addr_t
Definition: addr.h:34
BT_ADDR_LE_RANDOM
#define BT_ADDR_LE_RANDOM
Definition: addr.h:29
bt_addr_le_t::type
uint8_t type
Definition: addr.h:40
BT_ADDR_IS_STATIC
#define BT_ADDR_IS_STATIC(a)
Definition: addr.h:105
uint8_t
__UINT8_TYPE__ uint8_t
Definition: stdint.h:58
strcpy
char * strcpy(char *restrict d, const char *restrict s)
bt_addr_cmp
static int bt_addr_cmp(const bt_addr_t *a, const bt_addr_t *b)
Compare Bluetooth device addresses.
Definition: addr.h:62
BT_ADDR_IS_RPA
#define BT_ADDR_IS_RPA(a)
Definition: addr.h:100
types.h
bt_addr_le_is_identity
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:145
bt_addr_from_str
int bt_addr_from_str(const char *str, bt_addr_t *addr)
Convert Bluetooth address from string to binary.
memcmp
int memcmp(const void *m1, const void *m2, size_t n)
snprintk
int snprintk(char *str, size_t size, const char *fmt,...)
bt_addr_le_create_static
int bt_addr_le_create_static(bt_addr_le_t *addr)
Create a Bluetooth LE random static address.
bt_addr_le_from_str
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.
bt_addr_le_copy
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:94
bt_addr_le_is_rpa
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:127
bt_addr_le_t
Definition: addr.h:39
bt_addr_t::val
uint8_t val[6]
Definition: addr.h:35
memcpy
void * memcpy(void *restrict d, const void *restrict s, size_t n)
printk.h
BT_ADDR_LE_PUBLIC_ID
#define BT_ADDR_LE_PUBLIC_ID
Definition: addr.h:30
BT_ADDR_LE_RANDOM_ID
#define BT_ADDR_LE_RANDOM_ID
Definition: addr.h:31
bt_addr_le_cmp
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:74
BT_ADDR_LE_PUBLIC
#define BT_ADDR_LE_PUBLIC
Definition: addr.h:28
bt_addr_le_t::a
bt_addr_t a
Definition: addr.h:41
bt_addr_copy
static void bt_addr_copy(bt_addr_t *dst, const bt_addr_t *src)
Copy Bluetooth device address.
Definition: addr.h:84
bt_addr_to_str
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:184