Dawn Framework 1.0
Universal data acquisition framework for embedded systems
adv.cxx
1// dawn/src/proto/nimble/adv.cxx
2//
3// SPDX-License-Identifier: Apache-2.0
4//
5
6#include "dawn/proto/nimble/adv.hxx"
7
8#include <cstring>
9
10using namespace dawn;
11
14
15void CProtoNimbleAdv::startAdvertise()
16{
17 struct ble_gap_adv_params advp;
18 int ret;
19
20 printf("advertise\n");
21
22 CProtoNimbleAdv::updateAd();
23
24 std::memset(&advp, 0, sizeof advp);
25 advp.conn_mode = BLE_GAP_CONN_MODE_UND;
26 advp.disc_mode = BLE_GAP_DISC_MODE_GEN;
27 ret = ble_gap_adv_start(CProtoNimbleAdv::ownAddrType,
28 nullptr,
29 BLE_HS_FOREVER,
30 &advp,
31 CProtoNimbleAdv::gapEventCb,
32 nullptr);
33 if (ret != 0)
34 {
35 DAWNERR("ble_gap_adv_start failed: %d\n", ret);
36 }
37}
38
39void CProtoNimbleAdv::setGapName(const char *name, uint8_t len)
40{
41 size_t copy;
42
43 if (name == nullptr)
44 {
45 DAWNERR("NULL GAP name pointer\n");
46 return;
47 }
48
49 copy = (len > GAPNAME_MAX) ? GAPNAME_MAX : len;
50 std::memcpy(CProtoNimbleAdv::gapName, name, copy);
51 CProtoNimbleAdv::gapName[copy] = '\0';
52}
53
54void CProtoNimbleAdv::putAd(uint8_t type,
55 uint8_t ad_len,
56 const void *ad,
57 uint8_t *buf,
58 uint8_t *len)
59{
60 buf[(*len)++] = ad_len + 1;
61 buf[(*len)++] = type;
62
63 std::memcpy(&buf[*len], ad, ad_len);
64
65 *len += ad_len;
66}
67
68void CProtoNimbleAdv::updateAd()
69{
70 uint8_t ad_flags = BLE_HS_ADV_F_DISC_GEN | BLE_HS_ADV_F_BREDR_UNSUP;
71 uint8_t ad_len = 0;
72 uint8_t ad[BLE_HS_ADV_MAX_SZ];
73 size_t gap_len = strnlen(CProtoNimbleAdv::gapName, GAPNAME_MAX);
74
75 CProtoNimbleAdv::putAd(BLE_HS_ADV_TYPE_FLAGS, 1, &ad_flags, ad, &ad_len);
76 CProtoNimbleAdv::putAd(BLE_HS_ADV_TYPE_COMP_NAME, gap_len, CProtoNimbleAdv::gapName, ad, &ad_len);
77
78 ble_gap_adv_set_data(ad, ad_len);
79}
80
81int CProtoNimbleAdv::gapEventCb(struct ble_gap_event *event, void *arg)
82{
83 switch (event->type)
84 {
85 case BLE_GAP_EVENT_CONNECT:
86 {
87 if (event->connect.status)
88 {
89 CProtoNimbleAdv::startAdvertise();
90 }
91 break;
92 }
93
94 case BLE_GAP_EVENT_DISCONNECT:
95 {
96 DAWNINFO("disconected reason=%d\n", event->disconnect.reason);
97 CProtoNimbleAdv::startAdvertise();
98 break;
99 }
100 }
101
102 return 0;
103}
static size_t GAPNAME_MAX
Max GAP device name length (excluding null).
Definition adv.hxx:24
static char gapName[GAPNAME_MAX+1]
GAP device name visible during BLE discovery.
Definition adv.hxx:28
static uint8_t ownAddrType
BLE device address type (random/static/public).
Definition adv.hxx:27
Out-of-tree user-extension hooks for Dawn.
Definition bindable.hxx:13