ad.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2018,2019 Freie Universität Berlin
3  *
4  * This file is subject to the terms and conditions of the GNU Lesser
5  * General Public License v2.1. See the file LICENSE in the top level
6  * directory for more details.
7  */
8 
27 #ifndef NET_BLUETIL_AD_H
28 #define NET_BLUETIL_AD_H
29 
30 #include <stdint.h>
31 #include <stddef.h>
32 #include <string.h>
33 
34 #include "net/ble.h"
35 
36 #ifdef __cplusplus
37 extern "C" {
38 #endif
39 
43 #define BLUETIL_AD_INIT(b,p,s) { .buf = b, .pos = p, .size = s }
44 
48 #define BLUETIL_AD_FLAGS_DEFAULT (BLE_GAP_DISCOVERABLE | \
49  BLE_GAP_FLAG_BREDR_NOTSUP)
50 
54 enum {
58 };
59 
63 typedef struct {
64  uint8_t *data;
65  size_t len;
67 
71 typedef struct {
72  uint8_t *buf;
73  size_t pos;
74  size_t size;
75 } bluetil_ad_t;
76 
85 void bluetil_ad_init(bluetil_ad_t *ad, void *buf, size_t pos, size_t size);
86 
97 int bluetil_ad_find(const bluetil_ad_t *ad,
98  uint8_t type, bluetil_ad_data_t *data);
99 
111 int bluetil_ad_find_and_cmp(const bluetil_ad_t *ad, uint8_t type,
112  const void *val, size_t val_len);
113 
130 int bluetil_ad_find_str(const bluetil_ad_t *ad, uint8_t type,
131  char *str, size_t str_len);
132 
144 int bluetil_ad_add(bluetil_ad_t *ad, uint8_t type,
145  const void *data, size_t data_len);
146 
156 static inline int bluetil_ad_add_flags(bluetil_ad_t *ad, uint8_t flags)
157 {
158  return bluetil_ad_add(ad, BLE_GAP_AD_FLAGS, &flags, 1);
159 }
160 
173 static inline int bluetil_ad_add_name(bluetil_ad_t *ad, const char *name)
174 {
175  return bluetil_ad_add(ad, BLE_GAP_AD_NAME, name, strlen(name));
176 }
177 
195  void *buf, size_t buf_len,
196  uint8_t flags)
197 {
198  bluetil_ad_init(ad, buf, 0, buf_len);
199  return bluetil_ad_add_flags(ad, flags);
200 }
201 
202 /* add more convenience functions on demand... */
203 
204 #ifdef __cplusplus
205 }
206 #endif
207 
208 #endif /* NET_BLUETIL_AD_H */
209 
bluetil_ad_add_name
static int bluetil_ad_add_name(bluetil_ad_t *ad, const char *name)
Convenience function to add the "full name" field.
Definition: ad.h:173
BLUETIL_AD_NOMEM
@ BLUETIL_AD_NOMEM
insufficient memory to write field
Definition: ad.h:57
BLUETIL_AD_NOTFOUND
@ BLUETIL_AD_NOTFOUND
entry not found
Definition: ad.h:56
bluetil_ad_find
int bluetil_ad_find(const bluetil_ad_t *ad, uint8_t type, bluetil_ad_data_t *data)
Find a specific field in the given advertising data.
bluetil_ad_add_flags
static int bluetil_ad_add_flags(bluetil_ad_t *ad, uint8_t flags)
Convenience function to add the "flags" field.
Definition: ad.h:156
ble.h
General BLE values as defined by the BT standard.
bluetil_ad_data_t::len
size_t len
length of the payload
Definition: ad.h:65
bluetil_ad_find_str
int bluetil_ad_find_str(const bluetil_ad_t *ad, uint8_t type, char *str, size_t str_len)
Find the given field and copy its payload into a string.
bluetil_ad_t::buf
uint8_t * buf
buffer containing the advertising data
Definition: ad.h:72
BLUETIL_AD_OK
@ BLUETIL_AD_OK
everything went as expected
Definition: ad.h:55
bluetil_ad_find_and_cmp
int bluetil_ad_find_and_cmp(const bluetil_ad_t *ad, uint8_t type, const void *val, size_t val_len)
Find a specific field and compare its value against the given data.
bluetil_ad_init
void bluetil_ad_init(bluetil_ad_t *ad, void *buf, size_t pos, size_t size)
Initialize the given advertising data descriptor.
bluetil_ad_t::pos
size_t pos
current write position in the buffer
Definition: ad.h:73
bluetil_ad_add
int bluetil_ad_add(bluetil_ad_t *ad, uint8_t type, const void *data, size_t data_len)
Add a new field to the given advertising data.
bluetil_ad_t
Descriptor for a buffer containing advertising data.
Definition: ad.h:71
bluetil_ad_data_t::data
uint8_t * data
pointer a field's payload
Definition: ad.h:64
bluetil_ad_t::size
size_t size
overall length of the buffer
Definition: ad.h:74
bluetil_ad_init_with_flags
static int bluetil_ad_init_with_flags(bluetil_ad_t *ad, void *buf, size_t buf_len, uint8_t flags)
Convenience function for initializing the advertising data descriptor and directly adding the flags f...
Definition: ad.h:194
bluetil_ad_data_t
Struct used for returning the contents of a selected field.
Definition: ad.h:63