6#include "dawn/proto/nimble/adv.hxx"
10#include "nimble/nimble_port.h"
17static struct ble_npl_callout g_adv_retry_callout;
18static bool g_adv_retry_inited =
false;
20void CProtoNimbleAdv::advRetryCb(
struct ble_npl_event *ev)
24 if (ble_gap_adv_active())
29 CProtoNimbleAdv::startAdvertise();
32void CProtoNimbleAdv::scheduleAdvRetry()
34 if (!g_adv_retry_inited)
37 &g_adv_retry_callout, nimble_port_get_dflt_eventq(), CProtoNimbleAdv::advRetryCb,
nullptr);
38 g_adv_retry_inited =
true;
41 ble_npl_callout_reset(&g_adv_retry_callout, ble_npl_time_ms_to_ticks32(ADV_RETRY_MS));
44void CProtoNimbleAdv::startAdvertise()
46 struct ble_gap_adv_params advp;
49 printf(
"advertise\n");
51 CProtoNimbleAdv::updateAd();
53 std::memset(&advp, 0,
sizeof advp);
54 advp.conn_mode = BLE_GAP_CONN_MODE_UND;
55 advp.disc_mode = BLE_GAP_DISC_MODE_GEN;
60 CProtoNimbleAdv::gapEventCb,
69 "ble_gap_adv_start failed: %d, retry in %u ms\n", ret,
static_cast<unsigned>(ADV_RETRY_MS));
70 CProtoNimbleAdv::scheduleAdvRetry();
74void CProtoNimbleAdv::setGapName(
const char *name, uint8_t len)
80 DAWNERR(
"NULL GAP name pointer\n");
89void CProtoNimbleAdv::putAd(uint8_t type,
95 buf[(*len)++] = ad_len + 1;
98 std::memcpy(&buf[*len], ad, ad_len);
103void CProtoNimbleAdv::updateAd()
105 uint8_t ad_flags = BLE_HS_ADV_F_DISC_GEN | BLE_HS_ADV_F_BREDR_UNSUP;
107 uint8_t ad[BLE_HS_ADV_MAX_SZ];
110 CProtoNimbleAdv::putAd(BLE_HS_ADV_TYPE_FLAGS, 1, &ad_flags, ad, &ad_len);
113 ble_gap_adv_set_data(ad, ad_len);
116void CProtoNimbleAdv::requestConnParams(uint16_t conn_handle)
124 struct ble_gap_upd_params params;
125 std::memset(¶ms, 0,
sizeof params);
129 params.supervision_timeout = 400;
131 int ret = ble_gap_update_params(conn_handle, ¶ms);
134 DAWNERR(
"ble_gap_update_params failed: %d\n", ret);
138int CProtoNimbleAdv::gapEventCb(
struct ble_gap_event *event,
void *arg)
142 case BLE_GAP_EVENT_CONNECT:
144 if (event->connect.status)
146 CProtoNimbleAdv::startAdvertise();
150 CProtoNimbleAdv::requestConnParams(event->connect.conn_handle);
155 case BLE_GAP_EVENT_DISCONNECT:
157 DAWNINFO(
"disconected reason=%d\n", event->disconnect.reason);
158 CProtoNimbleAdv::startAdvertise();
static uint16_t CONN_ITVL_MIN
Preferred connection interval (units of 1.25ms); 0 leaves it untouched.
static size_t GAPNAME_MAX
Max GAP device name length (excluding null).
static char gapName[GAPNAME_MAX+1]
GAP device name visible during BLE discovery.
static uint8_t ownAddrType
BLE device address type (random/static/public).
Out-of-tree user-extension hooks for Dawn.