ad.h
Go to the documentation of this file.
1 /*
2  * SPDX-FileCopyrightText: 2018-2019 Freie Universität Berlin
3  * SPDX-License-Identifier: LGPL-2.1-only
4  */
5 
6 #pragma once
7 
26 #include <stdint.h>
27 #include <stddef.h>
28 #include <string.h>
29 
30 #include "net/ble.h"
31 
32 #ifdef __cplusplus
33 extern "C" {
34 #endif
35 
39 #define BLUETIL_AD_INIT(b,p,s) { .buf = b, .pos = p, .size = s }
40 
44 #define BLUETIL_AD_FLAGS_DEFAULT (BLE_GAP_DISCOVERABLE | \
45  BLE_GAP_FLAG_BREDR_NOTSUP)
46 
50 enum {
54 };
55 
59 typedef struct {
60  uint8_t *data;
61  size_t len;
63 
67 typedef struct {
68  uint8_t *buf;
69  size_t pos;
70  size_t size;
71 } bluetil_ad_t;
72 
81 void bluetil_ad_init(bluetil_ad_t *ad, void *buf, size_t pos, size_t size);
82 
94  uint8_t type, bluetil_ad_data_t *data);
95 
107 int bluetil_ad_find_and_cmp(const bluetil_ad_t *ad, uint8_t type,
108  const void *val, size_t val_len);
109 
126 int bluetil_ad_find_str(const bluetil_ad_t *ad, uint8_t type,
127  char *str, size_t str_len);
128 
140 int bluetil_ad_add(bluetil_ad_t *ad, uint8_t type,
141  const void *data, size_t data_len);
142 
152 static inline int bluetil_ad_add_flags(bluetil_ad_t *ad, uint8_t flags)
153 {
154  return bluetil_ad_add(ad, BLE_GAP_AD_FLAGS, &flags, 1);
155 }
156 
169 static inline int bluetil_ad_add_name(bluetil_ad_t *ad, const char *name)
170 {
171  return bluetil_ad_add(ad, BLE_GAP_AD_NAME, name, strlen(name));
172 }
173 
191  void *buf, size_t buf_len,
192  uint8_t flags)
193 {
194  bluetil_ad_init(ad, buf, 0, buf_len);
195  return bluetil_ad_add_flags(ad, flags);
196 }
197 
198 /* add more convenience functions on demand... */
199 
200 #ifdef __cplusplus
201 }
202 #endif
203 
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:152
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:169
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:190
@ BLUETIL_AD_NOTFOUND
entry not found
Definition: ad.h:52
@ BLUETIL_AD_NOMEM
insufficient memory to write field
Definition: ad.h:53
@ BLUETIL_AD_OK
everything went as expected
Definition: ad.h:51
Struct used for returning the contents of a selected field.
Definition: ad.h:59
uint8_t * data
pointer a field's payload
Definition: ad.h:60
size_t len
length of the payload
Definition: ad.h:61
Descriptor for a buffer containing advertising data.
Definition: ad.h:67
size_t pos
current write position in the buffer
Definition: ad.h:69
size_t size
overall length of the buffer
Definition: ad.h:70
uint8_t * buf
buffer containing the advertising data
Definition: ad.h:68
General BLE values as defined by the BT standard.