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 
9 #pragma once
10 
29 #include <stdint.h>
30 #include <stddef.h>
31 #include <string.h>
32 
33 #include "net/ble.h"
34 
35 #ifdef __cplusplus
36 extern "C" {
37 #endif
38 
42 #define BLUETIL_AD_INIT(b,p,s) { .buf = b, .pos = p, .size = s }
43 
47 #define BLUETIL_AD_FLAGS_DEFAULT (BLE_GAP_DISCOVERABLE | \
48  BLE_GAP_FLAG_BREDR_NOTSUP)
49 
53 enum {
57 };
58 
62 typedef struct {
63  uint8_t *data;
64  size_t len;
66 
70 typedef struct {
71  uint8_t *buf;
72  size_t pos;
73  size_t size;
74 } bluetil_ad_t;
75 
84 void bluetil_ad_init(bluetil_ad_t *ad, void *buf, size_t pos, size_t size);
85 
97  uint8_t type, bluetil_ad_data_t *data);
98 
110 int bluetil_ad_find_and_cmp(const bluetil_ad_t *ad, uint8_t type,
111  const void *val, size_t val_len);
112 
129 int bluetil_ad_find_str(const bluetil_ad_t *ad, uint8_t type,
130  char *str, size_t str_len);
131 
143 int bluetil_ad_add(bluetil_ad_t *ad, uint8_t type,
144  const void *data, size_t data_len);
145 
155 static inline int bluetil_ad_add_flags(bluetil_ad_t *ad, uint8_t flags)
156 {
157  return bluetil_ad_add(ad, BLE_GAP_AD_FLAGS, &flags, 1);
158 }
159 
172 static inline int bluetil_ad_add_name(bluetil_ad_t *ad, const char *name)
173 {
174  return bluetil_ad_add(ad, BLE_GAP_AD_NAME, name, strlen(name));
175 }
176 
194  void *buf, size_t buf_len,
195  uint8_t flags)
196 {
197  bluetil_ad_init(ad, buf, 0, buf_len);
198  return bluetil_ad_add_flags(ad, flags);
199 }
200 
201 /* add more convenience functions on demand... */
202 
203 #ifdef __cplusplus
204 }
205 #endif
206 
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.
static int bluetil_ad_add_flags(bluetil_ad_t *ad, uint8_t flags)
Convenience function to add the "flags" field.
Definition: ad.h:155
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.
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.
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.
void bluetil_ad_init(bluetil_ad_t *ad, void *buf, size_t pos, size_t size)
Initialize the given advertising data descriptor.
static int bluetil_ad_add_name(bluetil_ad_t *ad, const char *name)
Convenience function to add the "full name" field.
Definition: ad.h:172
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:193
@ BLUETIL_AD_NOTFOUND
entry not found
Definition: ad.h:55
@ BLUETIL_AD_NOMEM
insufficient memory to write field
Definition: ad.h:56
@ BLUETIL_AD_OK
everything went as expected
Definition: ad.h:54
Struct used for returning the contents of a selected field.
Definition: ad.h:62
uint8_t * data
pointer a field's payload
Definition: ad.h:63
size_t len
length of the payload
Definition: ad.h:64
Descriptor for a buffer containing advertising data.
Definition: ad.h:70
size_t pos
current write position in the buffer
Definition: ad.h:72
size_t size
overall length of the buffer
Definition: ad.h:73
uint8_t * buf
buffer containing the advertising data
Definition: ad.h:71
General BLE values as defined by the BT standard.