diff --git a/include/esp32/esp32_bt.h b/include/esp32xx/esp32xx_bt.h similarity index 76% rename from include/esp32/esp32_bt.h rename to include/esp32xx/esp32xx_bt.h index d6c4e92..a9bee63 100644 --- a/include/esp32/esp32_bt.h +++ b/include/esp32xx/esp32xx_bt.h @@ -29,12 +29,12 @@ extern "C" { #define MGOS_BT_ADDR_LEN 6 void mgos_bt_addr_to_esp32(const struct mgos_bt_addr *in, ble_addr_t *out); -void esp32_bt_addr_to_mgos(const ble_addr_t *in, struct mgos_bt_addr *out); -const char *esp32_bt_addr_to_str(const ble_addr_t *addr, char *out); +void esp32xx_bt_addr_to_mgos(const ble_addr_t *in, struct mgos_bt_addr *out); +const char *esp32xx_bt_addr_to_str(const ble_addr_t *addr, char *out); void mgos_bt_uuid_to_esp32(const struct mgos_bt_uuid *in, ble_uuid_any_t *out); -void esp32_bt_uuid_to_mgos(const ble_uuid_t *in, struct mgos_bt_uuid *out); -const char *esp32_bt_uuid_to_str(const ble_uuid_t *uuid, char *out); +void esp32xx_bt_uuid_to_mgos(const ble_uuid_t *in, struct mgos_bt_uuid *out); +const char *esp32xx_bt_uuid_to_str(const ble_uuid_t *uuid, char *out); #ifdef __cplusplus } diff --git a/include/esp32/esp32_bt_gap.h b/include/esp32xx/esp32xx_bt_gap.h similarity index 97% rename from include/esp32/esp32_bt_gap.h rename to include/esp32xx/esp32xx_bt_gap.h index 95efca1..14058f1 100644 --- a/include/esp32/esp32_bt_gap.h +++ b/include/esp32xx/esp32xx_bt_gap.h @@ -19,7 +19,7 @@ #include "mgos_bt_gap.h" -#include "esp32_bt.h" +#include "esp32xx_bt.h" #ifdef __cplusplus extern "C" { diff --git a/include/esp32/esp32_bt_internal.h b/include/esp32xx/esp32xx_bt_internal.h similarity index 71% rename from include/esp32/esp32_bt_internal.h rename to include/esp32xx/esp32xx_bt_internal.h index 376b1ab..d6836b2 100644 --- a/include/esp32/esp32_bt_internal.h +++ b/include/esp32xx/esp32xx_bt_internal.h @@ -27,21 +27,21 @@ extern "C" { #endif -bool esp32_bt_gap_start_advertising(void); -bool esp32_bt_gatts_init(void); -bool esp32_bt_gatts_start(void); -void esp32_bt_restart(void); +bool esp32xx_bt_gap_start_advertising(void); +bool esp32xx_bt_gatts_init(void); +bool esp32xx_bt_gatts_start(void); +void esp32xx_bt_restart(void); struct ble_gap_event; -int esp32_bt_gatts_event(const struct ble_gap_event *event, void *arg); +int esp32xx_bt_gatts_event(const struct ble_gap_event *event, void *arg); extern uint8_t own_addr_type; -void esp32_bt_rlock(void); -void esp32_bt_runlock(void); +void esp32xx_bt_rlock(void); +void esp32xx_bt_runlock(void); struct os_mbuf; -struct mg_str esp32_bt_mbuf_to_flat(const struct os_mbuf *om); +struct mg_str esp32xx_bt_mbuf_to_flat(const struct os_mbuf *om); #ifdef __cplusplus } diff --git a/mos.yml b/mos.yml index b052d7b..310ab3b 100644 --- a/mos.yml +++ b/mos.yml @@ -1,9 +1,9 @@ author: mongoose-os description: Bluetooth support type: lib -version: 1.0 +version: 1.1.0 -platforms: [ esp32 ] +platforms: [ esp32, esp32c3 ] sources: - src @@ -31,6 +31,10 @@ config_schema: conds: - when: mos.platform == "esp32" apply: + sources: + - src/esp32xx + includes: + - include/esp32xx libs: - origin: https://github.com/mongoose-os-libs/mbedtls build_vars: @@ -53,6 +57,34 @@ conds: cdefs: MYNEWT_VAL_BLE_HS_AUTO_START: 0 + - when: mos.platform == "esp32c3" + apply: + sources: + - src/esp32xx + includes: + - include/esp32xx + libs: + - origin: https://github.com/mongoose-os-libs/mbedtls + build_vars: + ESP_IDF_EXTRA_COMPONENTS: "${build_vars.ESP_IDF_EXTRA_COMPONENTS} bt" + ESP_IDF_SDKCONFIG_OPTS: > + ${build_vars.ESP_IDF_SDKCONFIG_OPTS} + CONFIG_BT_ENABLED=y + CONFIG_BT_NIMBLE_ENABLED=y + CONFIG_BT_NIMBLE_ROLE_BROADCASTER=y + CONFIG_BT_NIMBLE_ROLE_OBSERVER=y + CONFIG_BT_NIMBLE_ROLE_CENTRAL=y + CONFIG_BT_NIMBLE_ROLE_PERIPHERAL=y + CONFIG_BT_NIMBLE_MAX_CONNECTIONS=7 + CONFIG_BT_NIMBLE_DEBUG=n + CONFIG_BT_NIMBLE_TASK_STACK_SIZE=6144 + CONFIG_BT_NIMBLE_L2CAP_COC_MAX_NUM=0 + CONFIG_BT_NIMBLE_GAP_DEVICE_NAME_MAX_LEN=50 + CONFIG_BT_NIMBLE_CRYPTO_STACK_MBEDTLS=y + CONFIG_BTDM_CTRL_HLI=n + cdefs: + MYNEWT_VAL_BLE_HS_AUTO_START: 0 + tags: - bt - bluetooth diff --git a/src/esp32/esp32_bt.c b/src/esp32xx/esp32xx_bt.c similarity index 87% rename from src/esp32/esp32_bt.c rename to src/esp32xx/esp32xx_bt.c index 6928499..90a58f0 100644 --- a/src/esp32/esp32_bt.c +++ b/src/esp32xx/esp32xx_bt.c @@ -15,9 +15,9 @@ * limitations under the License. */ -#include "esp32_bt.h" -#include "esp32_bt_gap.h" -#include "esp32_bt_internal.h" +#include "esp32xx_bt.h" +#include "esp32xx_bt_gap.h" +#include "esp32xx_bt_internal.h" #include #include @@ -44,13 +44,13 @@ static TaskHandle_t s_host_task_handle; static SemaphoreHandle_t s_sem = NULL; static struct mgos_rlock_type *s_lock = NULL; -enum esp32_bt_state { +enum esp32xx_bt_state { ESP32_BT_STOPPED = 0, ESP32_BT_STARTING = 1, ESP32_BT_STARTED = 2, ESP32_BT_STOPPING = 3, }; -static enum esp32_bt_state s_state = ESP32_BT_STOPPED; +static enum esp32xx_bt_state s_state = ESP32_BT_STOPPED; struct ble_hs_stop_listener s_stop_listener; void mgos_bt_addr_to_esp32(const struct mgos_bt_addr *in, ble_addr_t *out) { @@ -73,7 +73,7 @@ void mgos_bt_addr_to_esp32(const struct mgos_bt_addr *in, ble_addr_t *out) { out->val[5] = in->addr[0]; } -void esp32_bt_addr_to_mgos(const ble_addr_t *in, struct mgos_bt_addr *out) { +void esp32xx_bt_addr_to_mgos(const ble_addr_t *in, struct mgos_bt_addr *out) { out->type = MGOS_BT_ADDR_TYPE_NONE; switch (in->type) { case BLE_ADDR_PUBLIC: @@ -97,9 +97,9 @@ void esp32_bt_addr_to_mgos(const ble_addr_t *in, struct mgos_bt_addr *out) { out->addr[5] = in->val[0]; } -const char *esp32_bt_addr_to_str(const ble_addr_t *addr, char *out) { +const char *esp32xx_bt_addr_to_str(const ble_addr_t *addr, char *out) { struct mgos_bt_addr maddr; - esp32_bt_addr_to_mgos(addr, &maddr); + esp32xx_bt_addr_to_mgos(addr, &maddr); return mgos_bt_addr_to_str(&maddr, MGOS_BT_ADDR_STRINGIFY_TYPE, out); } @@ -120,7 +120,7 @@ void mgos_bt_uuid_to_esp32(const struct mgos_bt_uuid *in, ble_uuid_any_t *out) { } } -void esp32_bt_uuid_to_mgos(const ble_uuid_t *in, struct mgos_bt_uuid *out) { +void esp32xx_bt_uuid_to_mgos(const ble_uuid_t *in, struct mgos_bt_uuid *out) { out->len = in->type / 8; switch (in->type) { case BLE_UUID_TYPE_16: @@ -135,9 +135,9 @@ void esp32_bt_uuid_to_mgos(const ble_uuid_t *in, struct mgos_bt_uuid *out) { } } -const char *esp32_bt_uuid_to_str(const ble_uuid_t *uuid, char *out) { +const char *esp32xx_bt_uuid_to_str(const ble_uuid_t *uuid, char *out) { struct mgos_bt_uuid uuidm; - esp32_bt_uuid_to_mgos(uuid, &uuidm); + esp32xx_bt_uuid_to_mgos(uuid, &uuidm); return mgos_bt_uuid_to_str(&uuidm, out); } @@ -179,15 +179,15 @@ bool mgos_bt_get_device_address(struct mgos_bt_addr *addr) { default: return false; } - esp32_bt_addr_to_mgos(&baddr, addr); + esp32xx_bt_addr_to_mgos(&baddr, addr); return true; } -static void esp32_bt_reset(int reason) { +static void esp32xx_bt_reset(int reason) { LOG(LL_ERROR, ("Resetting state; reason=%d", reason)); } -static void esp32_bt_synced(void) { +static void esp32xx_bt_synced(void) { int rc; s_state = ESP32_BT_STARTED; @@ -213,12 +213,12 @@ static void esp32_bt_synced(void) { ("BLE Device Address: %s", mgos_bt_addr_to_str(&addr, MGOS_BT_ADDR_STRINGIFY_TYPE, addr_str))); } - esp32_bt_gap_start_advertising(); + esp32xx_bt_gap_start_advertising(); } // Handler callback that executes host events on the mgos task. // For efficiency, we process multiple events at a time. -static void esp32_bt_mgos_handler(void *arg) { +static void esp32xx_bt_mgos_handler(void *arg) { int nevs = 1; struct ble_npl_event *ev, **ep, *evs[20] = {arg}; struct ble_npl_eventq *q = nimble_port_get_dflt_eventq(); @@ -236,7 +236,7 @@ static void esp32_bt_mgos_handler(void *arg) { } } -static void esp32_bt_host_task(void *param) { +static void esp32xx_bt_host_task(void *param) { if (param == NULL) { // This is the dummy task. nimble_port_freertos_deinit(); return; @@ -246,24 +246,24 @@ static void esp32_bt_host_task(void *param) { struct ble_npl_eventq *q = nimble_port_get_dflt_eventq(); while (1) { ev = ble_npl_eventq_get(q, BLE_NPL_TIME_FOREVER); - while (!mgos_invoke_cb(esp32_bt_mgos_handler, ev, false /* from_isr */)) { + while (!mgos_invoke_cb(esp32xx_bt_mgos_handler, ev, false /* from_isr */)) { } // Wait for the mgos task callback to run and process the event. xSemaphoreTake(s_sem, portMAX_DELAY); } } -void esp32_bt_rlock(void) { +void esp32xx_bt_rlock(void) { mgos_rlock(s_lock); } -void esp32_bt_runlock(void) { +void esp32xx_bt_runlock(void) { mgos_runlock(s_lock); } extern void ble_store_config_init(void); -static bool esp32_bt_init(void) { +static bool esp32xx_bt_init(void) { if (s_inited) { return true; } @@ -287,8 +287,8 @@ static bool esp32_bt_init(void) { nimble_port_init(); - ble_hs_cfg.reset_cb = esp32_bt_reset; - ble_hs_cfg.sync_cb = esp32_bt_synced; + ble_hs_cfg.reset_cb = esp32xx_bt_reset; + ble_hs_cfg.sync_cb = esp32xx_bt_synced; ble_hs_cfg.store_status_cb = ble_store_util_status_rr; ble_hs_cfg.sm_sc = true; @@ -307,16 +307,16 @@ static bool esp32_bt_init(void) { // This creates the high-pri LL task and a host task. // We don't use the latter because its priority is too high. - nimble_port_freertos_init(esp32_bt_host_task); + nimble_port_freertos_init(esp32xx_bt_host_task); // Instead we create our own here with priority below the main mgos task. - xTaskCreatePinnedToCore(esp32_bt_host_task, "ble", 1024, (void *) 1, + xTaskCreatePinnedToCore(esp32xx_bt_host_task, "ble", 1024, (void *) 1, MGOS_TASK_PRIORITY - 1, &s_host_task_handle, NIMBLE_CORE); // Default INFO level log is too spammy. esp_log_level_set("NimBLE", ESP_LOG_WARN); - if (!esp32_bt_gatts_init()) { + if (!esp32xx_bt_gatts_init()) { LOG(LL_ERROR, ("%s init failed", "GATTS")); goto out; } @@ -345,10 +345,10 @@ static void ble_hs_stop_cb(int status, void *arg) { bool mgos_bt_start(void) { s_should_be_running = true; if (s_state != ESP32_BT_STOPPED) return true; - if (!esp32_bt_init()) return false; + if (!esp32xx_bt_init()) return false; s_state = ESP32_BT_STARTING; mgos_event_trigger_schedule(MGOS_BT_EV_STARTING, NULL, 0); - if (!esp32_bt_gatts_start()) { + if (!esp32xx_bt_gatts_start()) { LOG(LL_ERROR, ("%s start failed", "GATTS")); return false; } @@ -364,18 +364,18 @@ bool mgos_bt_stop(void) { return (ble_hs_stop(&s_stop_listener, ble_hs_stop_cb, NULL) == 0); } -void esp32_bt_restart(void) { +void esp32xx_bt_restart(void) { if (!s_should_be_running) return; mgos_bt_stop(); s_should_be_running = true; } -static void esp32_bt_start(void *arg) { +static void esp32xx_bt_start(void *arg) { mgos_bt_start(); (void) arg; } -struct mg_str esp32_bt_mbuf_to_flat(const struct os_mbuf *om) { +struct mg_str esp32xx_bt_mbuf_to_flat(const struct os_mbuf *om) { struct mg_str res = MG_NULL_STR; if (om == NULL) return res; char *data = NULL; @@ -418,8 +418,8 @@ bool mgos_bt_common_init(void) { LOG(LL_ERROR, ("Random addresses are not supported, using public")); mgos_sys_config_set_bt_random_address(false); } - esp32_bt_init(); + esp32xx_bt_init(); // Delay starting the stack until other libraries are initialized // and services registered to avoid unnecessary restarts. - return mgos_invoke_cb(esp32_bt_start, NULL, false /* from_isr */); + return mgos_invoke_cb(esp32xx_bt_start, NULL, false /* from_isr */); } diff --git a/src/esp32/esp32_bt_gap.c b/src/esp32xx/esp32xx_bt_gap.c similarity index 93% rename from src/esp32/esp32_bt_gap.c rename to src/esp32xx/esp32xx_bt_gap.c index fe5d447..9256400 100644 --- a/src/esp32/esp32_bt_gap.c +++ b/src/esp32xx/esp32xx_bt_gap.c @@ -15,14 +15,14 @@ * limitations under the License. */ -#include "esp32_bt_gap.h" +#include "esp32xx_bt_gap.h" #include "mgos.h" #include "host/ble_gap.h" #include "services/gap/ble_svc_gap.h" -#include "esp32_bt_internal.h" +#include "esp32xx_bt_internal.h" static bool s_adv_enable = false; static bool s_advertising = false; @@ -89,7 +89,7 @@ static int mgos_bt_scan_event_fn(struct ble_gap_event *ev, void *arg) { struct mgos_bt_gap_scan_result arg = { .rssi = ev->disc.rssi, }; - esp32_bt_addr_to_mgos(&ev->disc.addr, &arg.addr); + esp32xx_bt_addr_to_mgos(&ev->disc.addr, &arg.addr); struct mg_str data = mg_mk_str_n((char *) ev->disc.data, ev->disc.length_data); LOG(LL_DEBUG, @@ -175,27 +175,27 @@ bool mgos_bt_gap_scan(const struct mgos_bt_gap_scan_opts *opts) { } } -static int esp32_bt_gap_event(struct ble_gap_event *ev, void *arg) { +static int esp32xx_bt_gap_event(struct ble_gap_event *ev, void *arg) { int ret = 0; LOG(LL_DEBUG, ("GAP Event %d", ev->type)); - esp32_bt_rlock(); + esp32xx_bt_rlock(); switch (ev->type) { // Forward GATTS events to the GATTS handler. case BLE_GAP_EVENT_CONNECT: // Connect disables advertising. Resume, if it's enabled. s_advertising = false; - esp32_bt_gap_start_advertising(); + esp32xx_bt_gap_start_advertising(); // fallthrough case BLE_GAP_EVENT_DISCONNECT: case BLE_GAP_EVENT_ENC_CHANGE: case BLE_GAP_EVENT_SUBSCRIBE: case BLE_GAP_EVENT_NOTIFY_TX: case BLE_GAP_EVENT_MTU: - ret = esp32_bt_gatts_event(ev, arg); + ret = esp32xx_bt_gatts_event(ev, arg); break; case BLE_GAP_EVENT_ADV_COMPLETE: s_advertising = false; - esp32_bt_gap_start_advertising(); + esp32xx_bt_gap_start_advertising(); break; case BLE_GAP_EVENT_PASSKEY_ACTION: // TODO @@ -213,11 +213,11 @@ static int esp32_bt_gap_event(struct ble_gap_event *ev, void *arg) { ret = BLE_GAP_REPEAT_PAIRING_RETRY; } } - esp32_bt_runlock(); + esp32xx_bt_runlock(); return ret; } -bool esp32_bt_gap_start_advertising(void) { +bool esp32xx_bt_gap_start_advertising(void) { int rc; if (s_advertising) return true; @@ -288,7 +288,7 @@ bool esp32_bt_gap_start_advertising(void) { .channel_map = BLE_GAP_ADV_DFLT_CHANNEL_MAP, }; if ((rc = ble_gap_adv_start(own_addr_type, NULL, BLE_HS_FOREVER, &adv_params, - esp32_bt_gap_event, NULL)) != 0) { + esp32xx_bt_gap_event, NULL)) != 0) { LOG(LL_ERROR, ("ble_gap_adv_start: %d", rc)); return false; } @@ -303,14 +303,14 @@ bool esp32_bt_gap_start_advertising(void) { return true; } -static bool esp32_bt_gap_stop_advertising(void) { - esp32_bt_rlock(); +static bool esp32xx_bt_gap_stop_advertising(void) { + esp32xx_bt_rlock(); bool res = !s_advertising; if (res) goto out; res = (ble_gap_adv_stop() == 0); if (res) s_advertising = false; out: - esp32_bt_runlock(); + esp32xx_bt_runlock(); return res; } @@ -335,10 +335,10 @@ bool mgos_bt_gap_set_scan_rsp_data(struct mg_str scan_rsp_data) { } bool mgos_bt_gap_set_adv_enable(bool adv_enable) { - esp32_bt_rlock(); + esp32xx_bt_rlock(); s_adv_enable = adv_enable; - bool res = (s_adv_enable ? esp32_bt_gap_start_advertising() - : esp32_bt_gap_stop_advertising()); - esp32_bt_rlock(); + bool res = (s_adv_enable ? esp32xx_bt_gap_start_advertising() + : esp32xx_bt_gap_stop_advertising()); + esp32xx_bt_rlock(); return res; } diff --git a/src/esp32/esp32_bt_gattc.c b/src/esp32xx/esp32xx_bt_gattc.c similarity index 62% rename from src/esp32/esp32_bt_gattc.c rename to src/esp32xx/esp32xx_bt_gattc.c index c41ec38..f51bec7 100644 --- a/src/esp32/esp32_bt_gattc.c +++ b/src/esp32xx/esp32xx_bt_gattc.c @@ -26,75 +26,75 @@ #include "host/ble_gap.h" #include "host/ble_gatt.h" -#include "esp32_bt.h" -#include "esp32_bt_internal.h" +#include "esp32xx_bt.h" +#include "esp32xx_bt_internal.h" -struct esp32_bt_gattc_disc_result; -struct esp32_bt_gattc_pending_read; +struct esp32xx_bt_gattc_disc_result; +struct esp32xx_bt_gattc_pending_read; -struct esp32_bt_gattc_conn { +struct esp32xx_bt_gattc_conn { struct mgos_bt_gatt_conn gc; unsigned int connected : 1; unsigned int disc_done : 1; unsigned int disc_in_progress : 1; uint16_t last_read_handle; - SLIST_HEAD(disc_results, esp32_bt_gattc_disc_result) disc_results; - SLIST_HEAD(panding_reads, esp32_bt_gattc_pending_read) pending_reads; - SLIST_ENTRY(esp32_bt_gattc_conn) next; + SLIST_HEAD(disc_results, esp32xx_bt_gattc_disc_result) disc_results; + SLIST_HEAD(panding_reads, esp32xx_bt_gattc_pending_read) pending_reads; + SLIST_ENTRY(esp32xx_bt_gattc_conn) next; }; -enum esp32_bt_gattc_disc_result_type { +enum esp32xx_bt_gattc_disc_result_type { DISC_RESULT_SVC, DISC_RESULT_CHR, DISC_RESULT_DSC, }; -struct esp32_bt_gattc_disc_result { - enum esp32_bt_gattc_disc_result_type type; +struct esp32xx_bt_gattc_disc_result { + enum esp32xx_bt_gattc_disc_result_type type; union { struct ble_gatt_svc svc; struct ble_gatt_chr chr; struct ble_gatt_dsc dsc; }; - SLIST_ENTRY(esp32_bt_gattc_disc_result) next; + SLIST_ENTRY(esp32xx_bt_gattc_disc_result) next; }; -struct esp32_bt_gattc_pending_read { - struct esp32_bt_gattc_conn *conn; +struct esp32xx_bt_gattc_pending_read { + struct esp32xx_bt_gattc_conn *conn; struct mbuf data; uint16_t handle; - SLIST_ENTRY(esp32_bt_gattc_pending_read) next; + SLIST_ENTRY(esp32xx_bt_gattc_pending_read) next; }; -static SLIST_HEAD(s_conns, esp32_bt_gattc_conn) s_conns = +static SLIST_HEAD(s_conns, esp32xx_bt_gattc_conn) s_conns = SLIST_HEAD_INITIALIZER(s_conns); -static struct esp32_bt_gattc_conn *find_conn_by_id(uint16_t conn_id) { - struct esp32_bt_gattc_conn *conn; +static struct esp32xx_bt_gattc_conn *find_conn_by_id(uint16_t conn_id) { + struct esp32xx_bt_gattc_conn *conn; SLIST_FOREACH(conn, &s_conns, next) { if (conn->gc.conn_id == conn_id) return conn; } return NULL; } -static struct esp32_bt_gattc_conn *validate_conn(void *maybe_conn) { - struct esp32_bt_gattc_conn *conn; +static struct esp32xx_bt_gattc_conn *validate_conn(void *maybe_conn) { + struct esp32xx_bt_gattc_conn *conn; SLIST_FOREACH(conn, &s_conns, next) { if (conn == maybe_conn) return conn; } return NULL; } -static void esp32_bt_gattc_pending_read_free( - struct esp32_bt_gattc_pending_read *pr) { +static void esp32xx_bt_gattc_pending_read_free( + struct esp32xx_bt_gattc_pending_read *pr) { mbuf_free(&pr->data); free(pr); } -static int esp32_bt_gattc_mtu_event(uint16_t conn_id, - const struct ble_gatt_error *err, - uint16_t mtu, void *arg) { - struct esp32_bt_gattc_conn *conn = validate_conn(arg); +static int esp32xx_bt_gattc_mtu_event(uint16_t conn_id, + const struct ble_gatt_error *err, + uint16_t mtu, void *arg) { + struct esp32xx_bt_gattc_conn *conn = validate_conn(arg); if (conn == NULL) return BLE_ATT_ERR_UNLIKELY; LOG(LL_DEBUG, ("MTU_FN %d st %d mtu %d", conn_id, err->status, mtu)); if (err->status == 0) { @@ -109,8 +109,8 @@ static int esp32_bt_gattc_mtu_event(uint16_t conn_id, return 0; } -static uint16_t esp32_bt_gattc_get_disc_entry_handle( - const struct esp32_bt_gattc_disc_result *dre) { +static uint16_t esp32xx_bt_gattc_get_disc_entry_handle( + const struct esp32xx_bt_gattc_disc_result *dre) { switch (dre->type) { case DISC_RESULT_SVC: return dre->svc.start_handle; @@ -122,13 +122,13 @@ static uint16_t esp32_bt_gattc_get_disc_entry_handle( return 0xffff; } -static void esp32_bt_gattc_finish_discovery(struct esp32_bt_gattc_conn *conn, - bool ok) { +static void esp32xx_bt_gattc_finish_discovery( + struct esp32xx_bt_gattc_conn *conn, bool ok) { if (!conn->disc_in_progress) return; conn->disc_done = true; conn->disc_in_progress = false; if (ok) { - struct esp32_bt_gattc_disc_result *dre, *sdre = NULL; + struct esp32xx_bt_gattc_disc_result *dre, *sdre = NULL; SLIST_FOREACH(dre, &conn->disc_results, next) { switch (dre->type) { case DISC_RESULT_SVC: { @@ -140,8 +140,8 @@ static void esp32_bt_gattc_finish_discovery(struct esp32_bt_gattc_conn *conn, .conn = conn->gc, .handle = dre->chr.val_handle, }; - esp32_bt_uuid_to_mgos(&sdre->svc.uuid.u, &arg.svc); - esp32_bt_uuid_to_mgos(&dre->chr.uuid.u, &arg.chr); + esp32xx_bt_uuid_to_mgos(&sdre->svc.uuid.u, &arg.svc); + esp32xx_bt_uuid_to_mgos(&dre->chr.uuid.u, &arg.chr); uint8_t pp = dre->chr.properties; if ((pp & BLE_GATT_CHR_PROP_READ) != 0) { arg.prop |= MGOS_BT_GATT_PROP_READ; @@ -176,12 +176,12 @@ static void esp32_bt_gattc_finish_discovery(struct esp32_bt_gattc_conn *conn, sizeof(arg)); } -static int esp32_bt_gattc_event(struct ble_gap_event *ev, void *arg) { +static int esp32xx_bt_gattc_event(struct ble_gap_event *ev, void *arg) { char buf1[MGOS_BT_UUID_STR_LEN]; - esp32_bt_rlock(); - struct esp32_bt_gattc_conn *conn = validate_conn(arg); + esp32xx_bt_rlock(); + struct esp32xx_bt_gattc_conn *conn = validate_conn(arg); if (conn == NULL) { - esp32_bt_runlock(); + esp32xx_bt_runlock(); return BLE_ATT_ERR_UNLIKELY; } LOG(LL_DEBUG, ("GATTC EV %d", ev->type)); @@ -194,12 +194,12 @@ static int esp32_bt_gattc_event(struct ble_gap_event *ev, void *arg) { int8_t conn_rssi = 0; ble_gap_conn_rssi(conn_id, &conn_rssi); LOG(LL_INFO, ("CONNECT %s ch %d st %d rssi %d", - esp32_bt_addr_to_str(&cd.peer_ota_addr, buf1), conn_id, + esp32xx_bt_addr_to_str(&cd.peer_ota_addr, buf1), conn_id, ev->connect.status, -conn_rssi)); if (ev->connect.status == 0) { - ble_gattc_exchange_mtu(conn_id, esp32_bt_gattc_mtu_event, conn); + ble_gattc_exchange_mtu(conn_id, esp32xx_bt_gattc_mtu_event, conn); } else { - SLIST_REMOVE(&s_conns, conn, esp32_bt_gattc_conn, next); + SLIST_REMOVE(&s_conns, conn, esp32xx_bt_gattc_conn, next); struct mgos_bt_gattc_connect_arg carg = { .conn = conn->gc, .ok = false, @@ -217,18 +217,18 @@ static int esp32_bt_gattc_event(struct ble_gap_event *ev, void *arg) { const struct ble_gap_conn_desc *cd = &ev->disconnect.conn; uint16_t conn_id = cd->conn_handle; LOG(LL_INFO, ("DISCONNECT %s ch %d reason %d", - esp32_bt_addr_to_str(&cd->peer_ota_addr, buf1), conn_id, + esp32xx_bt_addr_to_str(&cd->peer_ota_addr, buf1), conn_id, ev->disconnect.reason)); - SLIST_REMOVE(&s_conns, conn, esp32_bt_gattc_conn, next); + SLIST_REMOVE(&s_conns, conn, esp32xx_bt_gattc_conn, next); if (conn->connected) { struct mgos_bt_gattc_disconnect_arg arg = { .conn = conn->gc, }; mgos_event_trigger_schedule(MGOS_BT_GATTC_EV_DISCONNECT, &arg, sizeof(arg)); - esp32_bt_gattc_finish_discovery(conn, false /* ok */); + esp32xx_bt_gattc_finish_discovery(conn, false /* ok */); } - struct esp32_bt_gattc_disc_result *dre, *dret; + struct esp32xx_bt_gattc_disc_result *dre, *dret; SLIST_FOREACH_SAFE(dre, &conn->disc_results, next, dret) { free(dre); } @@ -237,10 +237,10 @@ static int esp32_bt_gattc_event(struct ble_gap_event *ev, void *arg) { break; } case BLE_GAP_EVENT_NOTIFY_RX: { - struct esp32_bt_gattc_conn *conn = + struct esp32xx_bt_gattc_conn *conn = find_conn_by_id(ev->notify_rx.conn_handle); if (conn == NULL) return BLE_ATT_ERR_UNLIKELY; - struct mg_str data = esp32_bt_mbuf_to_flat(ev->notify_rx.om); + struct mg_str data = esp32xx_bt_mbuf_to_flat(ev->notify_rx.om); struct mgos_bt_gattc_notify_arg narg = { .conn = conn->gc, .handle = ev->notify_rx.attr_handle, @@ -252,21 +252,21 @@ static int esp32_bt_gattc_event(struct ble_gap_event *ev, void *arg) { break; } } - esp32_bt_runlock(); + esp32xx_bt_runlock(); return 0; } bool mgos_bt_gattc_connect(const struct mgos_bt_addr *addr) { ble_addr_t addr2; mgos_bt_addr_to_esp32(addr, &addr2); - struct esp32_bt_gattc_conn *conn = calloc(1, sizeof(*conn)); + struct esp32xx_bt_gattc_conn *conn = calloc(1, sizeof(*conn)); if (conn == NULL) return false; conn->gc.addr = *addr; conn->gc.conn_id = 0xffff; SLIST_INIT(&conn->disc_results); SLIST_INIT(&conn->pending_reads); int rc = ble_gap_connect(own_addr_type, &addr2, 1000 /* duration_ms */, - NULL /* params */, esp32_bt_gattc_event, conn); + NULL /* params */, esp32xx_bt_gattc_event, conn); if (rc != 0) { free(conn); return false; @@ -275,21 +275,21 @@ bool mgos_bt_gattc_connect(const struct mgos_bt_addr *addr) { return true; } -static int esp32_bt_gattc_read_cb(uint16_t conn_id, - const struct ble_gatt_error *err, - struct ble_gatt_attr *attr, void *arg) { - struct esp32_bt_gattc_pending_read *pr = arg; - struct esp32_bt_gattc_conn *conn = validate_conn(pr->conn); +static int esp32xx_bt_gattc_read_cb(uint16_t conn_id, + const struct ble_gatt_error *err, + struct ble_gatt_attr *attr, void *arg) { + struct esp32xx_bt_gattc_pending_read *pr = arg; + struct esp32xx_bt_gattc_conn *conn = validate_conn(pr->conn); if (conn == NULL) return BLE_ATT_ERR_UNLIKELY; if (err->status == 0) { - struct mg_str data = esp32_bt_mbuf_to_flat(attr->om); + struct mg_str data = esp32xx_bt_mbuf_to_flat(attr->om); LOG(LL_DEBUG, ("READ_PART c %d ah %d len %d", conn_id, pr->handle, data.len)); mbuf_append(&pr->data, data.p, data.len); mg_strfree(&data); return 0; } - SLIST_REMOVE(&conn->pending_reads, pr, esp32_bt_gattc_pending_read, next); + SLIST_REMOVE(&conn->pending_reads, pr, esp32xx_bt_gattc_pending_read, next); LOG(LL_DEBUG, ("READ_DONE c %d ah %d st %d len %d", conn_id, pr->handle, err->status, (int) pr->data.len)); struct mgos_bt_gattc_read_result_arg rarg = { @@ -299,7 +299,7 @@ static int esp32_bt_gattc_read_cb(uint16_t conn_id, .data = mg_mk_str_n(pr->data.buf, pr->data.len), }; mgos_event_trigger(MGOS_BT_GATTC_EV_READ_RESULT, &rarg); - esp32_bt_gattc_pending_read_free(pr); + esp32xx_bt_gattc_pending_read_free(pr); return 0; } @@ -307,10 +307,10 @@ bool mgos_bt_gattc_read(uint16_t conn_id, uint16_t handle) { LOG(LL_DEBUG, ("READ c %d ah %d", conn_id, handle)); int ret; bool res = false; - esp32_bt_rlock(); - struct esp32_bt_gattc_conn *conn = find_conn_by_id(conn_id); + esp32xx_bt_rlock(); + struct esp32xx_bt_gattc_conn *conn = find_conn_by_id(conn_id); if (conn == NULL) goto out; - struct esp32_bt_gattc_pending_read *pr = NULL; + struct esp32xx_bt_gattc_pending_read *pr = NULL; SLIST_FOREACH(pr, &conn->pending_reads, next) { if (pr->handle == handle) { // There's already a pending read for this attr, just wait. @@ -323,27 +323,27 @@ bool mgos_bt_gattc_read(uint16_t conn_id, uint16_t handle) { pr->conn = conn; pr->handle = handle; mbuf_init(&pr->data, 0); - ret = ble_gattc_read_long(conn_id, handle, 0, esp32_bt_gattc_read_cb, pr); + ret = ble_gattc_read_long(conn_id, handle, 0, esp32xx_bt_gattc_read_cb, pr); if (ret == 0) { SLIST_INSERT_HEAD(&conn->pending_reads, pr, next); res = true; } else { LOG(LL_ERROR, ("ret = %d", ret)); - esp32_bt_gattc_pending_read_free(pr); + esp32xx_bt_gattc_pending_read_free(pr); } out: - esp32_bt_runlock(); + esp32xx_bt_runlock(); return res; } -static int esp32_bt_gattc_write_cb(uint16_t conn_id, - const struct ble_gatt_error *err, - struct ble_gatt_attr *attr, void *arg) { +static int esp32xx_bt_gattc_write_cb(uint16_t conn_id, + const struct ble_gatt_error *err, + struct ble_gatt_attr *attr, void *arg) { bool resp_required = (bool) (uintptr_t) arg; LOG(LL_DEBUG, ("WRITE_CB c %d err %p st %d attr %p ah %d rr %d", conn_id, err, (err ? err->status : -1), attr, (attr ? attr->handle : 0), resp_required)); - struct esp32_bt_gattc_conn *conn = find_conn_by_id(conn_id); + struct esp32xx_bt_gattc_conn *conn = find_conn_by_id(conn_id); if (conn == NULL) return BLE_ATT_ERR_UNLIKELY; if (resp_required) { struct mgos_bt_gattc_write_result_arg rarg = { @@ -359,21 +359,22 @@ static int esp32_bt_gattc_write_cb(uint16_t conn_id, bool mgos_bt_gattc_write(uint16_t conn_id, uint16_t handle, struct mg_str data, bool resp_required) { LOG(LL_DEBUG, ("WRITE c %d ah %d rr %d", conn_id, handle, resp_required)); - struct esp32_bt_gattc_conn *conn = find_conn_by_id(conn_id); + struct esp32xx_bt_gattc_conn *conn = find_conn_by_id(conn_id); if (conn == NULL) return false; int ret; uint16_t mtu = ble_att_mtu(conn_id); if (data.len < mtu - 1) { if (resp_required) { ret = ble_gattc_write_flat(conn_id, handle, data.p, data.len, - esp32_bt_gattc_write_cb, (void *) 1); + esp32xx_bt_gattc_write_cb, (void *) 1); } else { ret = ble_gattc_write_no_rsp_flat(conn_id, handle, data.p, data.len); } } else { struct os_mbuf *om = ble_hs_mbuf_from_flat(data.p, data.len); - ret = ble_gattc_write_long(conn_id, handle, 0, om, esp32_bt_gattc_write_cb, - (void *) resp_required); + ret = + ble_gattc_write_long(conn_id, handle, 0, om, esp32xx_bt_gattc_write_cb, + (void *) resp_required); } if (ret != 0) { LOG(LL_ERROR, ("ret = %d", ret)); @@ -383,11 +384,11 @@ bool mgos_bt_gattc_write(uint16_t conn_id, uint16_t handle, struct mg_str data, bool mgos_bt_gattc_set_notify_mode_cccd(uint16_t conn_id, uint16_t cccd_handle, enum mgos_bt_gatt_notify_mode mode) { - struct esp32_bt_gattc_conn *conn = find_conn_by_id(conn_id); + struct esp32xx_bt_gattc_conn *conn = find_conn_by_id(conn_id); if (conn == NULL) return false; uint8_t data[2] = {mode, 0}; int ret = ble_gattc_write_flat(conn_id, cccd_handle, data, sizeof(data), - esp32_bt_gattc_write_cb, (void *) 0); + esp32xx_bt_gattc_write_cb, (void *) 0); if (ret != 0) { LOG(LL_ERROR, ("ret = %d", ret)); @@ -395,20 +396,20 @@ bool mgos_bt_gattc_set_notify_mode_cccd(uint16_t conn_id, uint16_t cccd_handle, return (ret == 0); } -static int esp32_bt_gattc_add_disc_result_entry( - struct esp32_bt_gattc_conn *conn, - const struct esp32_bt_gattc_disc_result *cdre) { - struct esp32_bt_gattc_disc_result *ndre = calloc(1, sizeof(*ndre)); +static int esp32xx_bt_gattc_add_disc_result_entry( + struct esp32xx_bt_gattc_conn *conn, + const struct esp32xx_bt_gattc_disc_result *cdre) { + struct esp32xx_bt_gattc_disc_result *ndre = calloc(1, sizeof(*ndre)); if (ndre == NULL) { - esp32_bt_gattc_finish_discovery(conn, false /* ok */); + esp32xx_bt_gattc_finish_discovery(conn, false /* ok */); return BLE_ATT_ERR_INSUFFICIENT_RES; } *ndre = *cdre; // Find insertion point: keep the list ordered by handle. - uint16_t ndre_h = esp32_bt_gattc_get_disc_entry_handle(ndre); - struct esp32_bt_gattc_disc_result *dre = NULL, *last_dre = NULL; + uint16_t ndre_h = esp32xx_bt_gattc_get_disc_entry_handle(ndre); + struct esp32xx_bt_gattc_disc_result *dre = NULL, *last_dre = NULL; SLIST_FOREACH(dre, &conn->disc_results, next) { - uint16_t dre_h = esp32_bt_gattc_get_disc_entry_handle(dre); + uint16_t dre_h = esp32xx_bt_gattc_get_disc_entry_handle(dre); if (dre_h == ndre_h) { // This happens when sweeping descriptors - char and svc handles are // returned as well, just ignore them. @@ -417,7 +418,7 @@ static int esp32_bt_gattc_add_disc_result_entry( free(ndre); return 0; } else { - esp32_bt_gattc_finish_discovery(conn, false /* ok */); + esp32xx_bt_gattc_finish_discovery(conn, false /* ok */); return BLE_ATT_ERR_UNLIKELY; } } else if (dre_h > ndre_h) { @@ -433,150 +434,151 @@ static int esp32_bt_gattc_add_disc_result_entry( return 0; } -static int esp32_bt_gattc_disc_dsc_ev(uint16_t conn_id, - const struct ble_gatt_error *err, - uint16_t chr_val_handle, - const struct ble_gatt_dsc *dsc, - void *arg) { +static int esp32xx_bt_gattc_disc_dsc_ev(uint16_t conn_id, + const struct ble_gatt_error *err, + uint16_t chr_val_handle, + const struct ble_gatt_dsc *dsc, + void *arg) { int ret = 0; char buf[MGOS_BT_UUID_STR_LEN]; - esp32_bt_rlock(); - struct esp32_bt_gattc_conn *conn = validate_conn(arg); + esp32xx_bt_rlock(); + struct esp32xx_bt_gattc_conn *conn = validate_conn(arg); if (conn == NULL) { - esp32_bt_runlock(); + esp32xx_bt_runlock(); return BLE_ATT_ERR_UNLIKELY; } switch (err->status) { case 0: LOG(LL_DEBUG, ("DISC_DSC ch %d uuid %s h %d", conn_id, - esp32_bt_uuid_to_str(&dsc->uuid.u, buf), dsc->handle)); - struct esp32_bt_gattc_disc_result dre = { + esp32xx_bt_uuid_to_str(&dsc->uuid.u, buf), dsc->handle)); + struct esp32xx_bt_gattc_disc_result dre = { .type = DISC_RESULT_DSC, .dsc = *dsc, }; - ret = esp32_bt_gattc_add_disc_result_entry(conn, &dre); + ret = esp32xx_bt_gattc_add_disc_result_entry(conn, &dre); break; case BLE_HS_EDONE: { - esp32_bt_gattc_finish_discovery(conn, true /* ok */); + esp32xx_bt_gattc_finish_discovery(conn, true /* ok */); break; } default: { - esp32_bt_gattc_finish_discovery(conn, false /* ok */); + esp32xx_bt_gattc_finish_discovery(conn, false /* ok */); } } - esp32_bt_runlock(); + esp32xx_bt_runlock(); return ret; } -static int esp32_bt_gattc_disc_chr_ev(uint16_t conn_id, - const struct ble_gatt_error *err, - const struct ble_gatt_chr *chr, - void *arg) { +static int esp32xx_bt_gattc_disc_chr_ev(uint16_t conn_id, + const struct ble_gatt_error *err, + const struct ble_gatt_chr *chr, + void *arg) { int ret = 0; char buf[MGOS_BT_UUID_STR_LEN]; - esp32_bt_rlock(); - struct esp32_bt_gattc_conn *conn = validate_conn(arg); + esp32xx_bt_rlock(); + struct esp32xx_bt_gattc_conn *conn = validate_conn(arg); if (conn == NULL) { - esp32_bt_runlock(); + esp32xx_bt_runlock(); return BLE_ATT_ERR_UNLIKELY; } switch (err->status) { case 0: LOG(LL_DEBUG, ("DISC_CHR ch %d uuid %s dh %d vh %d", conn_id, - esp32_bt_uuid_to_str(&chr->uuid.u, buf), chr->def_handle, + esp32xx_bt_uuid_to_str(&chr->uuid.u, buf), chr->def_handle, chr->val_handle)); - struct esp32_bt_gattc_disc_result dre = { + struct esp32xx_bt_gattc_disc_result dre = { .type = DISC_RESULT_CHR, .chr = *chr, }; - ret = esp32_bt_gattc_add_disc_result_entry(conn, &dre); + ret = esp32xx_bt_gattc_add_disc_result_entry(conn, &dre); break; case BLE_HS_EDONE: { uint16_t sh = SLIST_FIRST(&conn->disc_results)->svc.start_handle; if (ble_gattc_disc_all_dscs(conn_id, sh, 0xffff, - esp32_bt_gattc_disc_dsc_ev, conn) != 0) { - esp32_bt_gattc_finish_discovery(conn, false /* ok */); + esp32xx_bt_gattc_disc_dsc_ev, conn) != 0) { + esp32xx_bt_gattc_finish_discovery(conn, false /* ok */); } break; } default: { - esp32_bt_gattc_finish_discovery(conn, false /* ok */); + esp32xx_bt_gattc_finish_discovery(conn, false /* ok */); } } - esp32_bt_runlock(); + esp32xx_bt_runlock(); return ret; } -static int esp32_bt_gattc_disc_svc_ev(uint16_t conn_id, - const struct ble_gatt_error *err, - const struct ble_gatt_svc *svc, - void *arg) { +static int esp32xx_bt_gattc_disc_svc_ev(uint16_t conn_id, + const struct ble_gatt_error *err, + const struct ble_gatt_svc *svc, + void *arg) { int ret = 0; char buf[MGOS_BT_UUID_STR_LEN]; - esp32_bt_rlock(); - struct esp32_bt_gattc_conn *conn = validate_conn(arg); + esp32xx_bt_rlock(); + struct esp32xx_bt_gattc_conn *conn = validate_conn(arg); if (conn == NULL) { - esp32_bt_runlock(); + esp32xx_bt_runlock(); return BLE_ATT_ERR_UNLIKELY; } switch (err->status) { case 0: LOG(LL_DEBUG, ("DISC_SVC ch %d uuid %s sh %d eh %d", conn_id, - esp32_bt_uuid_to_str(&svc->uuid.u, buf), svc->start_handle, - svc->end_handle)); - struct esp32_bt_gattc_disc_result dre = { + esp32xx_bt_uuid_to_str(&svc->uuid.u, buf), + svc->start_handle, svc->end_handle)); + struct esp32xx_bt_gattc_disc_result dre = { .type = DISC_RESULT_SVC, .svc = *svc, }; - ret = esp32_bt_gattc_add_disc_result_entry(conn, &dre); + ret = esp32xx_bt_gattc_add_disc_result_entry(conn, &dre); break; case BLE_HS_EDONE: { if (SLIST_EMPTY(&conn->disc_results)) { // No services. - esp32_bt_gattc_finish_discovery(conn, true /* ok */); + esp32xx_bt_gattc_finish_discovery(conn, true /* ok */); } uint16_t sh = SLIST_FIRST(&conn->disc_results)->svc.start_handle; if (ble_gattc_disc_all_chrs(conn_id, sh, 0xffff, - esp32_bt_gattc_disc_chr_ev, conn) != 0) { - esp32_bt_gattc_finish_discovery(conn, false /* ok */); + esp32xx_bt_gattc_disc_chr_ev, conn) != 0) { + esp32xx_bt_gattc_finish_discovery(conn, false /* ok */); } break; } default: { - esp32_bt_gattc_finish_discovery(conn, false /* ok */); + esp32xx_bt_gattc_finish_discovery(conn, false /* ok */); } } - esp32_bt_runlock(); + esp32xx_bt_runlock(); return ret; } -static void esp32_bt_gattc_invoke_fd(void *arg) { - struct esp32_bt_gattc_conn *conn = validate_conn(arg); +static void esp32xx_bt_gattc_invoke_fd(void *arg) { + struct esp32xx_bt_gattc_conn *conn = validate_conn(arg); if (conn == NULL || !conn->disc_done) return; - esp32_bt_gattc_finish_discovery(conn, true /* ok */); + esp32xx_bt_gattc_finish_discovery(conn, true /* ok */); } bool mgos_bt_gattc_discover(uint16_t conn_id) { int ret = false; - esp32_bt_rlock(); - struct esp32_bt_gattc_conn *conn = find_conn_by_id(conn_id); + esp32xx_bt_rlock(); + struct esp32xx_bt_gattc_conn *conn = find_conn_by_id(conn_id); if (conn == NULL) goto out; if (!conn->connected || conn->disc_in_progress) goto out; if (conn->disc_done) { conn->disc_in_progress = true; - mgos_invoke_cb(esp32_bt_gattc_invoke_fd, conn, false /* from_isr */); + mgos_invoke_cb(esp32xx_bt_gattc_invoke_fd, conn, false /* from_isr */); ret = true; goto out; } conn->disc_in_progress = true; - if (ble_gattc_disc_all_svcs(conn_id, esp32_bt_gattc_disc_svc_ev, conn) != 0) { + if (ble_gattc_disc_all_svcs(conn_id, esp32xx_bt_gattc_disc_svc_ev, conn) != + 0) { conn->disc_in_progress = false; ret = false; } else { ret = true; } out: - esp32_bt_runlock(); + esp32xx_bt_runlock(); return ret; } diff --git a/src/esp32/esp32_bt_gatts.c b/src/esp32xx/esp32xx_bt_gatts.c similarity index 72% rename from src/esp32/esp32_bt_gatts.c rename to src/esp32xx/esp32xx_bt_gatts.c index 54ae668..3d3b824 100644 --- a/src/esp32/esp32_bt_gatts.c +++ b/src/esp32xx/esp32xx_bt_gatts.c @@ -27,19 +27,19 @@ #include "host/ble_gatt.h" #include "host/ble_hs.h" -#include "esp32_bt_gap.h" -#include "esp32_bt_internal.h" +#include "esp32xx_bt_gap.h" +#include "esp32xx_bt_internal.h" #ifndef MGOS_BT_GATTS_MAX_PREPARED_WRITE_LEN #define MGOS_BT_GATTS_MAX_PREPARED_WRITE_LEN 4096 #endif -struct esp32_bt_service_attr_info; +struct esp32xx_bt_service_attr_info; -struct esp32_bt_gatts_service_entry { +struct esp32xx_bt_gatts_service_entry { struct mgos_bt_uuid uuid; uint16_t svc_handle; - struct esp32_bt_service_attr_info *attrs; + struct esp32xx_bt_service_attr_info *attrs; uint16_t num_attrs; enum mgos_bt_gatt_sec_level sec_level; uint8_t deleting : 1; @@ -50,79 +50,80 @@ struct esp32_bt_gatts_service_entry { struct ble_gatt_chr_def *ble_chars; struct ble_gatt_dsc_def *ble_descrs; struct ble_gatt_svc_def ble_svc_def[2]; - SLIST_ENTRY(esp32_bt_gatts_service_entry) next; + SLIST_ENTRY(esp32xx_bt_gatts_service_entry) next; }; -struct esp32_bt_service_attr_info { +struct esp32xx_bt_service_attr_info { struct mgos_bt_gatts_char_def def; ble_uuid_any_t ble_uuid; uint16_t handle; }; -struct esp32_bt_gatts_pending_ind { +struct esp32xx_bt_gatts_pending_ind { uint16_t handle; bool is_ind; struct mg_str value; - STAILQ_ENTRY(esp32_bt_gatts_pending_ind) next; + STAILQ_ENTRY(esp32xx_bt_gatts_pending_ind) next; }; -struct esp32_bt_gatts_pending_read { +struct esp32xx_bt_gatts_pending_read { uint16_t handle; struct mbuf data; size_t read_offset; - SLIST_ENTRY(esp32_bt_gatts_pending_read) next; + SLIST_ENTRY(esp32xx_bt_gatts_pending_read) next; }; -struct esp32_bt_gatts_connection_entry; +struct esp32xx_bt_gatts_connection_entry; // A set of sessions is kept for each connection, one per service. -struct esp32_bt_gatts_session_entry { - struct esp32_bt_gatts_connection_entry *ce; +struct esp32xx_bt_gatts_session_entry { + struct esp32xx_bt_gatts_connection_entry *ce; struct mgos_bt_gatts_conn gsc; - struct esp32_bt_gatts_service_entry *se; - SLIST_ENTRY(esp32_bt_gatts_session_entry) next; + struct esp32xx_bt_gatts_service_entry *se; + SLIST_ENTRY(esp32xx_bt_gatts_session_entry) next; }; // This notification mode change is awaiting completion of the security // procedure. -struct esp32_bt_gatts_pending_nm { +struct esp32xx_bt_gatts_pending_nm { struct mgos_bt_gatts_notify_mode_arg nmarg; - SLIST_ENTRY(esp32_bt_gatts_pending_nm) next; + SLIST_ENTRY(esp32xx_bt_gatts_pending_nm) next; }; -struct esp32_bt_gatts_connection_entry { +struct esp32xx_bt_gatts_connection_entry { struct mgos_bt_gatt_conn gc; enum mgos_bt_gatt_sec_level sec_level; bool sec_in_flight; /* Notifications/indications are finicky, so we keep at most one in flight. */ int ind_queue_len; bool ind_in_flight; - SLIST_HEAD(sessions, esp32_bt_gatts_session_entry) sessions; // 1 per service - SLIST_HEAD(pending_nm, esp32_bt_gatts_pending_nm) pending_nm; - SLIST_HEAD(pending_reads, esp32_bt_gatts_pending_read) pending_reads; - STAILQ_HEAD(pending_inds, esp32_bt_gatts_pending_ind) pending_inds; - SLIST_ENTRY(esp32_bt_gatts_connection_entry) next; + SLIST_HEAD(sessions, esp32xx_bt_gatts_session_entry) + sessions; // 1 per service + SLIST_HEAD(pending_nm, esp32xx_bt_gatts_pending_nm) pending_nm; + SLIST_HEAD(pending_reads, esp32xx_bt_gatts_pending_read) pending_reads; + STAILQ_HEAD(pending_inds, esp32xx_bt_gatts_pending_ind) pending_inds; + SLIST_ENTRY(esp32xx_bt_gatts_connection_entry) next; }; -static SLIST_HEAD(s_svcs, esp32_bt_gatts_service_entry) s_svcs = +static SLIST_HEAD(s_svcs, esp32xx_bt_gatts_service_entry) s_svcs = SLIST_HEAD_INITIALIZER(s_svcs); -static SLIST_HEAD(s_conns, esp32_bt_gatts_connection_entry) s_conns = +static SLIST_HEAD(s_conns, esp32xx_bt_gatts_connection_entry) s_conns = SLIST_HEAD_INITIALIZER(s_conns); -static void esp32_bt_gatts_send_next_ind_locked( - struct esp32_bt_gatts_connection_entry *ce); -static void esp32_bt_gatts_create_sessions( - struct esp32_bt_gatts_connection_entry *ce); +static void esp32xx_bt_gatts_send_next_ind_locked( + struct esp32xx_bt_gatts_connection_entry *ce); +static void esp32xx_bt_gatts_create_sessions( + struct esp32xx_bt_gatts_connection_entry *ce); -static void esp32_bt_gatts_pending_ind_free( - struct esp32_bt_gatts_pending_ind *pi) { +static void esp32xx_bt_gatts_pending_ind_free( + struct esp32xx_bt_gatts_pending_ind *pi) { mg_strfree(&pi->value); memset(pi, 0, sizeof(*pi)); free(pi); } -static void esp32_bt_gatts_pending_read_free( - struct esp32_bt_gatts_pending_read *pr) { +static void esp32xx_bt_gatts_pending_read_free( + struct esp32xx_bt_gatts_pending_read *pr) { mbuf_free(&pr->data); free(pr); } @@ -172,26 +173,26 @@ static void esp32_gatts_register_cb(struct ble_gatt_register_ctxt *ctxt, switch (ctxt->op) { case BLE_GATT_REGISTER_OP_SVC: { LOG(LL_DEBUG, ("REGISTER_SVC %s sh %d", - esp32_bt_uuid_to_str(ctxt->svc.svc_def->uuid, buf), + esp32xx_bt_uuid_to_str(ctxt->svc.svc_def->uuid, buf), ctxt->svc.handle)); break; } case BLE_GATT_REGISTER_OP_CHR: { LOG(LL_DEBUG, ("REGISTER_CHR %s dh %d vh %d", - esp32_bt_uuid_to_str(ctxt->chr.chr_def->uuid, buf), + esp32xx_bt_uuid_to_str(ctxt->chr.chr_def->uuid, buf), ctxt->chr.def_handle, ctxt->chr.val_handle)); break; } case BLE_GATT_REGISTER_OP_DSC: { LOG(LL_DEBUG, ("REGISTER_DSC %s vh %d", - esp32_bt_uuid_to_str(ctxt->dsc.dsc_def->uuid, buf), + esp32xx_bt_uuid_to_str(ctxt->dsc.dsc_def->uuid, buf), ctxt->dsc.handle)); // Find the attr_info corresponding to this descriptor. // Descriptor's arg points at the characteristic and the descriptor // must be located after it. struct mgos_bt_uuid desc_uuid; - esp32_bt_uuid_to_mgos(ctxt->dsc.dsc_def->uuid, &desc_uuid); - struct esp32_bt_service_attr_info *ai = ctxt->dsc.dsc_def->arg; + esp32xx_bt_uuid_to_mgos(ctxt->dsc.dsc_def->uuid, &desc_uuid); + struct esp32xx_bt_service_attr_info *ai = ctxt->dsc.dsc_def->arg; for (ai++; ai->def.is_desc; ai++) { if (!mgos_bt_uuid_eq(&ai->def.uuid_bin, &desc_uuid)) continue; ai->handle = ctxt->dsc.handle; @@ -203,11 +204,11 @@ static void esp32_gatts_register_cb(struct ble_gatt_register_ctxt *ctxt, (void) arg; } -static struct esp32_bt_gatts_service_entry *find_service_by_attr_handle( - uint16_t attr_handle, struct esp32_bt_service_attr_info **aip) { - struct esp32_bt_gatts_service_entry *se; +static struct esp32xx_bt_gatts_service_entry *find_service_by_attr_handle( + uint16_t attr_handle, struct esp32xx_bt_service_attr_info **aip) { + struct esp32xx_bt_gatts_service_entry *se; SLIST_FOREACH(se, &s_svcs, next) { - struct esp32_bt_service_attr_info *ai = &se->attrs[0]; + struct esp32xx_bt_service_attr_info *ai = &se->attrs[0]; for (size_t i = 0; i < se->num_attrs; i++, ai++) { if (ai->handle != attr_handle) continue; if (aip != NULL) *aip = ai; @@ -217,46 +218,48 @@ static struct esp32_bt_gatts_service_entry *find_service_by_attr_handle( return NULL; } -static struct esp32_bt_gatts_connection_entry *find_connection( +static struct esp32xx_bt_gatts_connection_entry *find_connection( uint16_t conn_id) { - struct esp32_bt_gatts_connection_entry *ce = NULL; + struct esp32xx_bt_gatts_connection_entry *ce = NULL; SLIST_FOREACH(ce, &s_conns, next) { if (ce->gc.conn_id == conn_id) return ce; } return NULL; } -static struct esp32_bt_gatts_session_entry *find_session( - uint16_t conn_id, uint16_t handle, struct esp32_bt_service_attr_info **ai) { - struct esp32_bt_gatts_connection_entry *ce = find_connection(conn_id); +static struct esp32xx_bt_gatts_session_entry *find_session( + uint16_t conn_id, uint16_t handle, + struct esp32xx_bt_service_attr_info **ai) { + struct esp32xx_bt_gatts_connection_entry *ce = find_connection(conn_id); if (ce == NULL) return NULL; - struct esp32_bt_gatts_service_entry *se = + struct esp32xx_bt_gatts_service_entry *se = find_service_by_attr_handle(handle, ai); if (se == NULL) return NULL; - struct esp32_bt_gatts_session_entry *sse; + struct esp32xx_bt_gatts_session_entry *sse; SLIST_FOREACH(sse, &ce->sessions, next) { if (sse->se == se) return sse; } return NULL; } -static struct esp32_bt_gatts_session_entry *find_session_by_gsc( +static struct esp32xx_bt_gatts_session_entry *find_session_by_gsc( const struct mgos_bt_gatts_conn *gsc) { if (gsc == NULL) return NULL; - struct esp32_bt_gatts_connection_entry *ce = find_connection(gsc->gc.conn_id); + struct esp32xx_bt_gatts_connection_entry *ce = + find_connection(gsc->gc.conn_id); if (ce == NULL) return NULL; - struct esp32_bt_gatts_session_entry *sse; + struct esp32xx_bt_gatts_session_entry *sse; SLIST_FOREACH(sse, &ce->sessions, next) { if (&sse->gsc == gsc) return sse; } return NULL; } -static enum mgos_bt_gatt_status esp32_bt_gatts_call_handler( - struct esp32_bt_gatts_session_entry *sse, - struct esp32_bt_service_attr_info *ai, enum mgos_bt_gatts_ev ev, +static enum mgos_bt_gatt_status esp32xx_bt_gatts_call_handler( + struct esp32xx_bt_gatts_session_entry *sse, + struct esp32xx_bt_service_attr_info *ai, enum mgos_bt_gatts_ev ev, void *ev_arg) { - struct esp32_bt_gatts_service_entry *se = sse->se; + struct esp32xx_bt_gatts_service_entry *se = sse->se; /* Invoke attr handler if defined, fall back to service-wide handler. */ if (ai != NULL && ai->def.handler != NULL) { return ai->def.handler(&sse->gsc, ev, ev_arg, ai->def.handler_arg); @@ -264,31 +267,32 @@ static enum mgos_bt_gatt_status esp32_bt_gatts_call_handler( return se->handler(&sse->gsc, ev, ev_arg, se->handler_arg); } -void esp32_bt_gatts_close_session(struct esp32_bt_gatts_session_entry *sse) { - struct esp32_bt_gatts_pending_nm *pnm, *pnmt; +void esp32xx_bt_gatts_close_session( + struct esp32xx_bt_gatts_session_entry *sse) { + struct esp32xx_bt_gatts_pending_nm *pnm, *pnmt; SLIST_FOREACH_SAFE(pnm, &sse->ce->pending_nm, next, pnmt) { memset(pnm, 0, sizeof(*pnm)); free(pnm); } - SLIST_REMOVE(&sse->ce->sessions, sse, esp32_bt_gatts_session_entry, next); - esp32_bt_gatts_call_handler(sse, NULL, MGOS_BT_GATTS_EV_DISCONNECT, NULL); + SLIST_REMOVE(&sse->ce->sessions, sse, esp32xx_bt_gatts_session_entry, next); + esp32xx_bt_gatts_call_handler(sse, NULL, MGOS_BT_GATTS_EV_DISCONNECT, NULL); memset(sse, 0, sizeof(*sse)); free(sse); } -static void esp32_bt_gatts_create_sessions( - struct esp32_bt_gatts_connection_entry *ce) { +static void esp32xx_bt_gatts_create_sessions( + struct esp32xx_bt_gatts_connection_entry *ce) { /* Create a session for each of the currently registered services. */ - struct esp32_bt_gatts_service_entry *se; + struct esp32xx_bt_gatts_service_entry *se; SLIST_FOREACH(se, &s_svcs, next) { - struct esp32_bt_gatts_session_entry *sse = calloc(1, sizeof(*sse)); + struct esp32xx_bt_gatts_session_entry *sse = calloc(1, sizeof(*sse)); if (sse == NULL) break; sse->ce = ce; sse->se = se; sse->gsc.gc = ce->gc; sse->gsc.svc_uuid = se->uuid; - enum mgos_bt_gatt_status st = - esp32_bt_gatts_call_handler(sse, NULL, MGOS_BT_GATTS_EV_CONNECT, NULL); + enum mgos_bt_gatt_status st = esp32xx_bt_gatts_call_handler( + sse, NULL, MGOS_BT_GATTS_EV_CONNECT, NULL); if (st != MGOS_BT_GATT_STATUS_OK) { /* Service rejected the connection, do not create session for it. */ free(sse); @@ -300,33 +304,33 @@ static void esp32_bt_gatts_create_sessions( int mgos_bt_gatts_get_num_connections(void) { int num = 0; - esp32_bt_rlock(); - struct esp32_bt_gatts_connection_entry *ce; + esp32xx_bt_rlock(); + struct esp32xx_bt_gatts_connection_entry *ce; SLIST_FOREACH(ce, &s_conns, next) num++; - esp32_bt_runlock(); + esp32xx_bt_runlock(); return num; } bool mgos_bt_gatts_is_send_queue_empty(void) { bool res = true; - esp32_bt_rlock(); - struct esp32_bt_gatts_connection_entry *ce; + esp32xx_bt_rlock(); + struct esp32xx_bt_gatts_connection_entry *ce; SLIST_FOREACH(ce, &s_conns, next) { if (!STAILQ_EMPTY(&ce->pending_inds)) { res = false; break; } } - esp32_bt_runlock(); + esp32xx_bt_runlock(); return res; } -static void esp32_bt_gatts_send_next_ind_locked( - struct esp32_bt_gatts_connection_entry *ce) { +static void esp32xx_bt_gatts_send_next_ind_locked( + struct esp32xx_bt_gatts_connection_entry *ce) { int rc; if (ce->ind_in_flight) return; if (STAILQ_EMPTY(&ce->pending_inds)) return; - struct esp32_bt_gatts_pending_ind *pi = STAILQ_FIRST(&ce->pending_inds); + struct esp32xx_bt_gatts_pending_ind *pi = STAILQ_FIRST(&ce->pending_inds); struct os_mbuf *om = ble_hs_mbuf_from_flat(pi->value.p, pi->value.len); ce->ind_in_flight = true; if (pi->is_ind) { @@ -340,25 +344,25 @@ static void esp32_bt_gatts_send_next_ind_locked( } } -static int esp32_bt_gatts_mtu_event(uint16_t conn_id, - const struct ble_gatt_error *err, - uint16_t mtu, void *arg) { - struct esp32_bt_gatts_connection_entry *ce = arg; +static int esp32xx_bt_gatts_mtu_event(uint16_t conn_id, + const struct ble_gatt_error *err, + uint16_t mtu, void *arg) { + struct esp32xx_bt_gatts_connection_entry *ce = arg; LOG(LL_DEBUG, ("MTU_FN %d st %d mtu %d", conn_id, err->status, mtu)); // Peer may not support MTU exchange procedure, proceed anyway. if (err->status == 0) { ce->gc.mtu = mtu; } - esp32_bt_gatts_create_sessions(ce); + esp32xx_bt_gatts_create_sessions(ce); return 0; } -int esp32_bt_gatts_event(const struct ble_gap_event *ev, void *arg) { +int esp32xx_bt_gatts_event(const struct ble_gap_event *ev, void *arg) { int ret = 0; char buf1[MGOS_BT_UUID_STR_LEN], buf2[MGOS_BT_UUID_STR_LEN]; LOG(LL_DEBUG, ("GATTS EV %d hf %d", ev->type, (int) mgos_get_free_heap_size())); - esp32_bt_rlock(); + esp32xx_bt_rlock(); switch (ev->type) { case BLE_GAP_EVENT_CONNECT: { uint16_t conn_id = ev->connect.conn_handle; @@ -366,43 +370,43 @@ int esp32_bt_gatts_event(const struct ble_gap_event *ev, void *arg) { ble_gap_conn_find(conn_id, &cd); LOG(LL_INFO, ("CONNECT %s ch %d st %d AM %d M %d", - esp32_bt_addr_to_str(&cd.peer_ota_addr, buf1), conn_id, + esp32xx_bt_addr_to_str(&cd.peer_ota_addr, buf1), conn_id, ev->connect.status, ble_att_preferred_mtu(), ble_att_mtu(conn_id))); if (ev->connect.status != 0) break; - struct esp32_bt_gatts_connection_entry *ce = calloc(1, sizeof(*ce)); + struct esp32xx_bt_gatts_connection_entry *ce = calloc(1, sizeof(*ce)); if (ce == NULL) { ble_gap_terminate(conn_id, BLE_ERR_REM_USER_CONN_TERM); break; } ce->gc.conn_id = conn_id; ce->gc.mtu = ble_att_mtu(conn_id); - esp32_bt_addr_to_mgos(&cd.peer_ota_addr, &ce->gc.addr); + esp32xx_bt_addr_to_mgos(&cd.peer_ota_addr, &ce->gc.addr); SLIST_INIT(&ce->pending_nm); STAILQ_INIT(&ce->pending_inds); SLIST_INSERT_HEAD(&s_conns, ce, next); - ble_gattc_exchange_mtu(conn_id, esp32_bt_gatts_mtu_event, ce); + ble_gattc_exchange_mtu(conn_id, esp32xx_bt_gatts_mtu_event, ce); break; } case BLE_GAP_EVENT_DISCONNECT: { const struct ble_gap_conn_desc *cd = &ev->disconnect.conn; uint16_t conn_id = cd->conn_handle; LOG(LL_INFO, ("DISCONNECT %s ch %d reason %d", - esp32_bt_addr_to_str(&cd->peer_ota_addr, buf1), conn_id, + esp32xx_bt_addr_to_str(&cd->peer_ota_addr, buf1), conn_id, ev->disconnect.reason)); - struct esp32_bt_gatts_connection_entry *ce = find_connection(conn_id); + struct esp32xx_bt_gatts_connection_entry *ce = find_connection(conn_id); if (ce == NULL) break; - SLIST_REMOVE(&s_conns, ce, esp32_bt_gatts_connection_entry, next); + SLIST_REMOVE(&s_conns, ce, esp32xx_bt_gatts_connection_entry, next); while (!SLIST_EMPTY(&ce->sessions)) { - struct esp32_bt_gatts_session_entry *sse = SLIST_FIRST(&ce->sessions); - esp32_bt_gatts_close_session(sse); + struct esp32xx_bt_gatts_session_entry *sse = SLIST_FIRST(&ce->sessions); + esp32xx_bt_gatts_close_session(sse); } - struct esp32_bt_gatts_pending_ind *pi, *pit; + struct esp32xx_bt_gatts_pending_ind *pi, *pit; STAILQ_FOREACH_SAFE(pi, &ce->pending_inds, next, pit) { - esp32_bt_gatts_pending_ind_free(pi); + esp32xx_bt_gatts_pending_ind_free(pi); } - struct esp32_bt_gatts_pending_read *pr, *prt; + struct esp32xx_bt_gatts_pending_read *pr, *prt; SLIST_FOREACH_SAFE(pr, &ce->pending_reads, next, prt) { - esp32_bt_gatts_pending_read_free(pr); + esp32xx_bt_gatts_pending_read_free(pr); } free(ce); break; @@ -412,11 +416,11 @@ int esp32_bt_gatts_event(const struct ble_gap_event *ev, void *arg) { struct ble_gap_conn_desc cd = {0}; ble_gap_conn_find(ch, &cd); struct ble_gap_sec_state *ss = &cd.sec_state; - struct esp32_bt_gatts_connection_entry *ce = find_connection(ch); + struct esp32xx_bt_gatts_connection_entry *ce = find_connection(ch); if (ce == NULL) break; ce->sec_in_flight = false; LOG(LL_DEBUG, ("ENC_CHANGE %s ch %d st %d e %d a %d b %d ks %d", - esp32_bt_addr_to_str(&cd.peer_ota_addr, buf1), ch, + esp32xx_bt_addr_to_str(&cd.peer_ota_addr, buf1), ch, ev->enc_change.status, ss->encrypted, ss->authenticated, ss->bonded, ss->key_size)); if (ev->enc_change.status != 0) { @@ -424,18 +428,18 @@ int esp32_bt_gatts_event(const struct ble_gap_event *ev, void *arg) { break; } while (!SLIST_EMPTY(&ce->pending_nm)) { - struct esp32_bt_gatts_pending_nm *pnm = SLIST_FIRST(&ce->pending_nm); + struct esp32xx_bt_gatts_pending_nm *pnm = SLIST_FIRST(&ce->pending_nm); SLIST_REMOVE_HEAD(&ce->pending_nm, next); struct mgos_bt_gatts_notify_mode_arg *narg = &pnm->nmarg; uint16_t ah = narg->handle; - struct esp32_bt_service_attr_info *ai = NULL; - struct esp32_bt_gatts_session_entry *sse = find_session(ch, ah, &ai); + struct esp32xx_bt_service_attr_info *ai = NULL; + struct esp32xx_bt_gatts_session_entry *sse = find_session(ch, ah, &ai); LOG(LL_DEBUG, ("NOTIFY_MODE ch %d ah %d %s/%s %d", ch, ah, mgos_bt_uuid_to_str(&narg->svc_uuid, buf1), mgos_bt_uuid_to_str(&narg->char_uuid, buf2), narg->mode)); - esp32_bt_gatts_call_handler(sse, ai, MGOS_BT_GATTS_EV_NOTIFY_MODE, - narg); + esp32xx_bt_gatts_call_handler(sse, ai, MGOS_BT_GATTS_EV_NOTIFY_MODE, + narg); memset(pnm, 0, sizeof(*pnm)); free(pnm); } @@ -443,14 +447,14 @@ int esp32_bt_gatts_event(const struct ble_gap_event *ev, void *arg) { } case BLE_GAP_EVENT_MTU: { // Note: this event is not delivered in case of MTU exchange error, - // and in case of success it duplicates esp32_bt_gatts_mtu_event. + // and in case of success it duplicates esp32xx_bt_gatts_mtu_event. break; } case BLE_GAP_EVENT_SUBSCRIBE: { uint16_t ch = ev->subscribe.conn_handle; uint16_t ah = ev->subscribe.attr_handle; - struct esp32_bt_service_attr_info *ai = NULL; - struct esp32_bt_gatts_session_entry *sse = find_session(ch, ah, &ai); + struct esp32xx_bt_service_attr_info *ai = NULL; + struct esp32xx_bt_gatts_session_entry *sse = find_session(ch, ah, &ai); if (sse == NULL) break; struct mgos_bt_gatts_notify_mode_arg narg = { .svc_uuid = sse->se->uuid, @@ -470,7 +474,7 @@ int esp32_bt_gatts_event(const struct ble_gap_event *ev, void *arg) { ble_gap_conn_find(ch, &cd); struct ble_gap_sec_state *ss = &cd.sec_state; if (!ss->encrypted) { - struct esp32_bt_gatts_pending_nm *pnm = calloc(1, sizeof(*pnm)); + struct esp32xx_bt_gatts_pending_nm *pnm = calloc(1, sizeof(*pnm)); pnm->nmarg = narg; SLIST_INSERT_HEAD(&sse->ce->pending_nm, pnm, next); if (!sse->ce->sec_in_flight) { @@ -484,22 +488,23 @@ int esp32_bt_gatts_event(const struct ble_gap_event *ev, void *arg) { LOG(LL_DEBUG, ("NOTIFY_MODE ch %d ah %d %s/%s %d", ch, ah, mgos_bt_uuid_to_str(&narg.svc_uuid, buf1), mgos_bt_uuid_to_str(&narg.char_uuid, buf2), narg.mode)); - esp32_bt_gatts_call_handler(sse, ai, MGOS_BT_GATTS_EV_NOTIFY_MODE, &narg); + esp32xx_bt_gatts_call_handler(sse, ai, MGOS_BT_GATTS_EV_NOTIFY_MODE, + &narg); break; } case BLE_GAP_EVENT_NOTIFY_TX: { uint16_t ch = ev->notify_tx.conn_handle; uint16_t ah = ev->notify_tx.attr_handle; - struct esp32_bt_service_attr_info *ai = NULL; - struct esp32_bt_gatts_session_entry *sse = find_session(ch, ah, &ai); + struct esp32xx_bt_service_attr_info *ai = NULL; + struct esp32xx_bt_gatts_session_entry *sse = find_session(ch, ah, &ai); if (sse == NULL) break; - struct esp32_bt_gatts_connection_entry *ce = sse->ce; + struct esp32xx_bt_gatts_connection_entry *ce = sse->ce; ce->ind_in_flight = false; LOG(LL_DEBUG, ("NOTIFY_TX ch %d ah %d st %d", ch, ah, ev->notify_tx.status)); if (STAILQ_EMPTY(&ce->pending_inds)) break; // Shouldn't happen. bool remove = false; - struct esp32_bt_gatts_pending_ind *pi = STAILQ_FIRST(&ce->pending_inds); + struct esp32xx_bt_gatts_pending_ind *pi = STAILQ_FIRST(&ce->pending_inds); if (pi->is_ind) { // Indication raises this event twice: first with status 0 when // indication is sent, then again when it is acknowledged or times out. @@ -509,22 +514,22 @@ int esp32_bt_gatts_event(const struct ble_gap_event *ev, void *arg) { .handle = pi->handle, .ok = remove, }; - esp32_bt_gatts_call_handler(sse, ai, MGOS_BT_GATTS_EV_IND_CONFIRM, - &ic_arg); + esp32xx_bt_gatts_call_handler(sse, ai, MGOS_BT_GATTS_EV_IND_CONFIRM, + &ic_arg); } else { remove = (ev->notify_tx.status == 0); } if (remove) { STAILQ_REMOVE_HEAD(&ce->pending_inds, next); ce->ind_queue_len--; - esp32_bt_gatts_pending_ind_free(pi); + esp32xx_bt_gatts_pending_ind_free(pi); } // Send next one or retry sending this one that failed. - esp32_bt_gatts_send_next_ind_locked(ce); + esp32xx_bt_gatts_send_next_ind_locked(ce); break; } } - esp32_bt_runlock(); + esp32xx_bt_runlock(); return ret; } @@ -562,11 +567,11 @@ static int esp32_gatts_get_att_err(enum mgos_bt_gatt_status st) { } // Workaround for https://github.com/apache/mynewt-nimble/issues/1090 -bool esp32_bt_gatts_send_resp_data(struct ble_gatt_access_ctxt *ctxt, - struct esp32_bt_gatts_session_entry *sse, - uint16_t ch, uint16_t ah, bool quiet) { +bool esp32xx_bt_gatts_send_resp_data(struct ble_gatt_access_ctxt *ctxt, + struct esp32xx_bt_gatts_session_entry *sse, + uint16_t ch, uint16_t ah, bool quiet) { char buf[MGOS_BT_UUID_STR_LEN]; - struct esp32_bt_gatts_pending_read *pr; + struct esp32xx_bt_gatts_pending_read *pr; SLIST_FOREACH(pr, &sse->ce->pending_reads, next) { if (pr->handle == ah) break; } @@ -582,9 +587,9 @@ bool esp32_bt_gatts_send_resp_data(struct ble_gatt_access_ctxt *ctxt, os_mbuf_append(ctxt->om, pr->data.buf, rdl); pr->read_offset += (ble_att_mtu(ch) - 1); if (pr->read_offset >= rdl) { - SLIST_REMOVE(&sse->ce->pending_reads, pr, esp32_bt_gatts_pending_read, + SLIST_REMOVE(&sse->ce->pending_reads, pr, esp32xx_bt_gatts_pending_read, next); - esp32_bt_gatts_pending_read_free(pr); + esp32xx_bt_gatts_pending_read_free(pr); } return rdl; } @@ -595,13 +600,13 @@ static int esp32_gatts_attr_access_cb(uint16_t ch, uint16_t ah, int res = 0; char buf1[MGOS_BT_UUID_STR_LEN], buf2[MGOS_BT_UUID_STR_LEN], buf3[MGOS_BT_UUID_STR_LEN]; - struct esp32_bt_service_attr_info *ai = NULL; - struct esp32_bt_gatts_session_entry *sse = find_session(ch, ah, &ai); + struct esp32xx_bt_service_attr_info *ai = NULL; + struct esp32xx_bt_gatts_session_entry *sse = find_session(ch, ah, &ai); if (sse == NULL) return BLE_ATT_ERR_UNLIKELY; switch (ctxt->op) { case BLE_GATT_ACCESS_OP_READ_CHR: case BLE_GATT_ACCESS_OP_READ_DSC: { - if (esp32_bt_gatts_send_resp_data(ctxt, sse, ch, ah, false) > 0) { + if (esp32xx_bt_gatts_send_resp_data(ctxt, sse, ch, ah, false) > 0) { // Still sending previous response. break; } @@ -609,7 +614,7 @@ static int esp32_gatts_attr_access_cb(uint16_t ch, uint16_t ah, .svc_uuid = sse->se->uuid, .handle = ah, }; - struct esp32_bt_service_attr_info *cai = arg; + struct esp32xx_bt_service_attr_info *cai = arg; if (ctxt->op == BLE_GATT_ACCESS_OP_READ_CHR) { rarg.char_uuid = ai->def.uuid_bin; } else { @@ -617,11 +622,11 @@ static int esp32_gatts_attr_access_cb(uint16_t ch, uint16_t ah, rarg.desc_uuid = ai->def.uuid_bin; } enum mgos_bt_gatt_status st = - esp32_bt_gatts_call_handler(sse, ai, MGOS_BT_GATTS_EV_READ, &rarg); + esp32xx_bt_gatts_call_handler(sse, ai, MGOS_BT_GATTS_EV_READ, &rarg); res = esp32_gatts_get_att_err(st); size_t rdl = 0; if (res == 0) { - rdl = esp32_bt_gatts_send_resp_data(ctxt, sse, ch, ah, true); + rdl = esp32xx_bt_gatts_send_resp_data(ctxt, sse, ch, ah, true); } if (ctxt->op == BLE_GATT_ACCESS_OP_READ_CHR) { LOG(LL_DEBUG, @@ -641,13 +646,13 @@ static int esp32_gatts_attr_access_cb(uint16_t ch, uint16_t ah, } case BLE_GATT_ACCESS_OP_WRITE_CHR: case BLE_GATT_ACCESS_OP_WRITE_DSC: { - struct mg_str data = esp32_bt_mbuf_to_flat(ctxt->om); + struct mg_str data = esp32xx_bt_mbuf_to_flat(ctxt->om); struct mgos_bt_gatts_write_arg warg = { .svc_uuid = sse->se->uuid, .handle = ah, .data = data, }; - struct esp32_bt_service_attr_info *cai = arg; + struct esp32xx_bt_service_attr_info *cai = arg; if (ctxt->op == BLE_GATT_ACCESS_OP_WRITE_CHR) { warg.char_uuid = ai->def.uuid_bin; } else { @@ -655,7 +660,7 @@ static int esp32_gatts_attr_access_cb(uint16_t ch, uint16_t ah, warg.desc_uuid = ai->def.uuid_bin; } enum mgos_bt_gatt_status st = - esp32_bt_gatts_call_handler(sse, ai, MGOS_BT_GATTS_EV_WRITE, &warg); + esp32xx_bt_gatts_call_handler(sse, ai, MGOS_BT_GATTS_EV_WRITE, &warg); if (ctxt->op == BLE_GATT_ACCESS_OP_WRITE_CHR) { LOG(LL_DEBUG, ("WRITE_CHR %s ch %d ah %u (%s) len %d -> %d", mgos_bt_addr_to_str(&sse->gsc.gc.addr, 0, buf1), @@ -678,7 +683,8 @@ static int esp32_gatts_attr_access_cb(uint16_t ch, uint16_t ah, return res; } -static int esp32_bt_register_service(struct esp32_bt_gatts_service_entry *se) { +static int esp32xx_bt_register_service( + struct esp32xx_bt_gatts_service_entry *se) { int rc = 0; rc = ble_gatts_count_cfg(&se->ble_svc_def[0]); if (rc != 0) { @@ -693,16 +699,16 @@ static int esp32_bt_register_service(struct esp32_bt_gatts_service_entry *se) { return rc; } -static bool esp32_bt_register_services(void) { +static bool esp32xx_bt_register_services(void) { char buf[MGOS_BT_UUID_STR_LEN]; int rc = ble_gatts_reset(); if (rc != 0) { LOG(LL_ERROR, ("ble_gatts_reset() failed: %d", rc)); return false; } - struct esp32_bt_gatts_service_entry *se; + struct esp32xx_bt_gatts_service_entry *se; SLIST_FOREACH(se, &s_svcs, next) { - rc = esp32_bt_register_service(se); + rc = esp32xx_bt_register_service(se); if (rc != 0) { LOG(LL_ERROR, ("Failed to register BT service %s: %d", mgos_bt_uuid_to_str(&se->uuid, buf), rc)); @@ -718,7 +724,7 @@ bool mgos_bt_gatts_register_service(const char *svc_uuid, mgos_bt_gatts_ev_handler_t handler, void *handler_arg) { bool res = false; - struct esp32_bt_gatts_service_entry *se = calloc(1, sizeof(*se)); + struct esp32xx_bt_gatts_service_entry *se = calloc(1, sizeof(*se)); if (se == NULL) goto out; if (!mgos_bt_uuid_from_str(mg_mk_str(svc_uuid), &se->uuid)) { LOG(LL_ERROR, ("%s: Invalid svc UUID", svc_uuid)); @@ -754,7 +760,7 @@ bool mgos_bt_gatts_register_service(const char *svc_uuid, if (se->ble_descrs == NULL) goto out; } - struct esp32_bt_service_attr_info *ai = se->attrs, *last_chr = NULL; + struct esp32xx_bt_service_attr_info *ai = se->attrs, *last_chr = NULL; struct ble_gatt_chr_def *bchr = se->ble_chars; struct ble_gatt_dsc_def *bdsc = se->ble_descrs; for (cd = chars; cd->uuid != NULL; cd++, ai++) { @@ -805,10 +811,10 @@ bool mgos_bt_gatts_register_service(const char *svc_uuid, bsvc->characteristics = se->ble_chars; se->ble_svc_def[1].type = BLE_GATT_SVC_TYPE_END; - esp32_bt_rlock(); + esp32xx_bt_rlock(); SLIST_INSERT_HEAD(&s_svcs, se, next); - esp32_bt_runlock(); - esp32_bt_restart(); + esp32xx_bt_runlock(); + esp32xx_bt_restart(); res = true; out: if (!res && se != NULL) { @@ -821,7 +827,7 @@ bool mgos_bt_gatts_register_service(const char *svc_uuid, } bool mgos_bt_gatts_unregister_service(const char *uuid_str) { - struct esp32_bt_gatts_service_entry *se; + struct esp32xx_bt_gatts_service_entry *se; struct mgos_bt_uuid uuid; if (!mgos_bt_uuid_from_str(mg_mk_str(uuid_str), &uuid)) return false; SLIST_FOREACH(se, &s_svcs, next) { @@ -829,22 +835,22 @@ bool mgos_bt_gatts_unregister_service(const char *uuid_str) { } if (se == NULL) return false; // Remove from the list immediately in case it's re-registered. - SLIST_REMOVE(&s_svcs, se, esp32_bt_gatts_service_entry, next); + SLIST_REMOVE(&s_svcs, se, esp32xx_bt_gatts_service_entry, next); se->deleting = true; // Close the associated sessions; - struct esp32_bt_gatts_connection_entry *ce; + struct esp32xx_bt_gatts_connection_entry *ce; SLIST_FOREACH(ce, &s_conns, next) { - struct esp32_bt_gatts_session_entry *sse; + struct esp32xx_bt_gatts_session_entry *sse; SLIST_FOREACH(sse, &ce->sessions, next) { if (sse->se == se) break; } if (sse == NULL) continue; - esp32_bt_gatts_close_session(sse); + esp32xx_bt_gatts_close_session(sse); } free(se->attrs); free(se); // Need to restart the stack for this to take effect. - esp32_bt_restart(); + esp32xx_bt_restart(); return true; } @@ -852,10 +858,10 @@ void mgos_bt_gatts_send_resp_data(struct mgos_bt_gatts_conn *gsc, struct mgos_bt_gatts_read_arg *ra, struct mg_str data) { if (data.len == 0) return; - esp32_bt_rlock(); - struct esp32_bt_gatts_session_entry *sse = find_session_by_gsc(gsc); + esp32xx_bt_rlock(); + struct esp32xx_bt_gatts_session_entry *sse = find_session_by_gsc(gsc); if (sse == NULL) goto out; - struct esp32_bt_gatts_pending_read *pr = NULL; + struct esp32xx_bt_gatts_pending_read *pr = NULL; SLIST_FOREACH(pr, &sse->ce->pending_reads, next) { if (pr->handle == ra->handle) break; } @@ -868,36 +874,36 @@ void mgos_bt_gatts_send_resp_data(struct mgos_bt_gatts_conn *gsc, } mbuf_append(&pr->data, data.p, data.len); out: - esp32_bt_runlock(); + esp32xx_bt_runlock(); } void mgos_bt_gatts_notify(struct mgos_bt_gatts_conn *gsc, enum mgos_bt_gatt_notify_mode mode, uint16_t handle, struct mg_str data) { if (mode == MGOS_BT_GATT_NOTIFY_MODE_OFF) return; - struct esp32_bt_gatts_session_entry *sse = find_session_by_gsc(gsc); + struct esp32xx_bt_gatts_session_entry *sse = find_session_by_gsc(gsc); if (sse == NULL) return; - struct esp32_bt_gatts_pending_ind *pi = calloc(1, sizeof(*pi)); + struct esp32xx_bt_gatts_pending_ind *pi = calloc(1, sizeof(*pi)); if (pi == NULL) return; pi->handle = handle; pi->is_ind = (mode == MGOS_BT_GATT_NOTIFY_MODE_INDICATE); pi->value = mg_strdup(data); - esp32_bt_rlock(); + esp32xx_bt_rlock(); STAILQ_INSERT_TAIL(&sse->ce->pending_inds, pi, next); sse->ce->ind_queue_len++; - esp32_bt_gatts_send_next_ind_locked(sse->ce); - esp32_bt_runlock(); + esp32xx_bt_gatts_send_next_ind_locked(sse->ce); + esp32xx_bt_runlock(); } void mgos_bt_gatts_notify_uuid(struct mgos_bt_gatts_conn *gsc, const struct mgos_bt_uuid *char_uuid, enum mgos_bt_gatt_notify_mode mode, struct mg_str data) { - struct esp32_bt_gatts_session_entry *sse = find_session_by_gsc(gsc); + struct esp32xx_bt_gatts_session_entry *sse = find_session_by_gsc(gsc); if (sse == NULL) return; - struct esp32_bt_gatts_service_entry *se = sse->se; + struct esp32xx_bt_gatts_service_entry *se = sse->se; for (uint16_t i = 0; i < se->num_attrs; i++) { - struct esp32_bt_service_attr_info *ai = &se->attrs[i]; + struct esp32xx_bt_service_attr_info *ai = &se->attrs[i]; if (mgos_bt_uuid_eq(&ai->def.uuid_bin, char_uuid)) { mgos_bt_gatts_notify(gsc, mode, ai->handle, data); return; @@ -905,11 +911,11 @@ void mgos_bt_gatts_notify_uuid(struct mgos_bt_gatts_conn *gsc, } } -bool esp32_bt_gatts_start(void) { - return esp32_bt_register_services(); +bool esp32xx_bt_gatts_start(void) { + return esp32xx_bt_register_services(); } -bool esp32_bt_gatts_init(void) { +bool esp32xx_bt_gatts_init(void) { ble_hs_cfg.gatts_register_cb = esp32_gatts_register_cb; return true; }