Skip to content

Commit e548267

Browse files
committed
Clean up scanning and advertising logic
1 parent 58d409c commit e548267

File tree

4 files changed

+102
-488
lines changed

4 files changed

+102
-488
lines changed

include/esp32/esp32_bt_internal.h

-2
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,6 @@ bool esp32_bt_gatts_init(void);
3535

3636
void esp32_bt_set_is_advertising(bool is_advertising);
3737

38-
bool esp32_bt_wipe_config(void);
39-
4038
#ifdef __cplusplus
4139
}
4240
#endif

include/mgos_bt.h

+2
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,8 @@ void mgos_bt_uuid128_from_bytes(const uint8_t *bytes, bool reverse,
7676

7777
void mgos_event_trigger_schedule(int ev, void *ev_data, size_t data_len);
7878

79+
bool mgos_bt_get_device_address(struct mgos_bt_addr *addr);
80+
7981
#ifdef __cplusplus
8082
}
8183
#endif

src/esp32/esp32_bt.c

+41-41
Original file line numberDiff line numberDiff line change
@@ -93,22 +93,44 @@ void esp32_bt_uuid_to_mgos(const ble_uuid_any_t *in, struct mgos_bt_uuid *out) {
9393

9494
static void mgos_bt_net_ev(int ev, void *evd, void *arg) {
9595
if (ev != MGOS_NET_EV_IP_ACQUIRED) return;
96+
if (mgos_sys_config_get_bt_keep_enabled()) return;
9697
LOG(LL_INFO, ("Network is up, disabling Bluetooth"));
9798
mgos_sys_config_set_bt_enable(false);
98-
#if 0 // TODO
9999
char *msg = NULL;
100100
if (save_cfg(&mgos_sys_config, &msg)) {
101-
esp_bt_controller_disable();
102-
esp_bt_controller_deinit();
103-
esp_bt_controller_mem_release(ESP_BT_MODE_BLE);
101+
nimble_port_stop();
104102
}
105-
#endif
106103
(void) arg;
107104
}
108105

109-
bool esp32_bt_wipe_config(void) {
110-
// TODO
111-
return false;
106+
bool mgos_bt_get_device_address(struct mgos_bt_addr *addr) {
107+
int rc = ble_hs_id_infer_auto(mgos_sys_config_get_bt_random_address(),
108+
&own_addr_type);
109+
if (rc != 0) {
110+
LOG(LL_ERROR, ("error determining address type; rc=%d", rc));
111+
return false;
112+
}
113+
ble_addr_t baddr = {0};
114+
int is_nrpa = false;
115+
rc = ble_hs_id_copy_addr(own_addr_type, baddr.val, &is_nrpa);
116+
if (rc != 0) {
117+
LOG(LL_ERROR, ("BLE addr error %d", rc));
118+
return false;
119+
}
120+
switch (own_addr_type) {
121+
case BLE_OWN_ADDR_PUBLIC:
122+
baddr.type = BLE_ADDR_PUBLIC;
123+
break;
124+
case BLE_OWN_ADDR_RANDOM:
125+
case BLE_OWN_ADDR_RPA_RANDOM_DEFAULT:
126+
case BLE_OWN_ADDR_RPA_PUBLIC_DEFAULT:
127+
baddr.type = BLE_ADDR_RANDOM;
128+
break;
129+
default:
130+
return false;
131+
}
132+
esp32_bt_addr_to_mgos(&baddr, addr);
133+
return true;
112134
}
113135

114136
static void _on_reset(int reason) {
@@ -126,42 +148,25 @@ static void _on_sync(void) {
126148
return;
127149
}
128150

129-
rc = ble_hs_id_infer_auto(privacy, &own_addr_type);
130-
if (rc != 0) {
131-
LOG(LL_ERROR, ("error determining address type; rc=%d", rc));
132-
return;
151+
struct mgos_bt_addr addr;
152+
if (mgos_bt_get_device_address(&addr)) {
153+
char addr_str[MGOS_BT_ADDR_STR_LEN] = {0};
154+
LOG(LL_INFO,
155+
("BLE Device Address: %s",
156+
mgos_bt_addr_to_str(&addr, MGOS_BT_ADDR_STRINGIFY_TYPE, addr_str)));
133157
}
134158

135-
ble_addr_t addr = {0};
136-
int is_nrpa = false;
137-
rc = ble_hs_id_copy_addr(own_addr_type, addr.val, &is_nrpa);
138-
if (rc != 0) {
139-
LOG(LL_ERROR, ("BLE addr error %d", rc));
140-
return;
141-
}
142-
switch (own_addr_type) {
143-
case BLE_OWN_ADDR_PUBLIC:
144-
addr.type = BLE_ADDR_PUBLIC;
145-
break;
146-
case BLE_OWN_ADDR_RANDOM:
147-
case BLE_OWN_ADDR_RPA_RANDOM_DEFAULT:
148-
case BLE_OWN_ADDR_RPA_PUBLIC_DEFAULT:
149-
addr.type = BLE_ADDR_RANDOM;
150-
break;
159+
if (!esp32_bt_gap_init()) {
160+
LOG(LL_ERROR, ("GAP init failed"));
151161
}
152-
struct mgos_bt_addr maddr = {0};
153-
esp32_bt_addr_to_mgos(&addr, &maddr);
154-
char addr_str[18] = {0};
155-
LOG(LL_INFO,
156-
("BLE Device Address: %s",
157-
mgos_bt_addr_to_str(&maddr, MGOS_BT_ADDR_STRINGIFY_TYPE, addr_str)));
158-
159-
mgos_bt_gap_set_adv_enable(mgos_sys_config_get_bt_adv_enable());
160162
}
161163

162164
static void ble_host_task(void *param) {
165+
LOG(LL_INFO, ("BLE task starting"));
163166
nimble_port_run();
167+
LOG(LL_INFO, ("BLE task ending"));
164168
nimble_port_freertos_deinit();
169+
LOG(LL_INFO, ("BLE task exiting"));
165170
}
166171

167172
extern void ble_store_config_init(void);
@@ -200,11 +205,6 @@ bool mgos_bt_common_init(void) {
200205

201206
ble_att_set_preferred_mtu(mgos_sys_config_get_bt_gatt_mtu());
202207

203-
if (!esp32_bt_gap_init()) {
204-
LOG(LL_ERROR, ("GAP init failed"));
205-
ret = false;
206-
goto out;
207-
}
208208
#if 0
209209
if (!esp32_bt_gattc_init()) {
210210
LOG(LL_ERROR, ("GATTC init failed"));

0 commit comments

Comments
 (0)