From a57221785078515bcd64f93df1d85099d51c1814 Mon Sep 17 00:00:00 2001 From: morgan Date: Mon, 3 Aug 2026 14:47:13 -0700 Subject: [PATCH] migrate all barback BLE code to here, needs more work --- CMakeLists.txt | 1 + src/baros_ble.c | 387 +++++++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 380 insertions(+), 8 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 197316d..5ebcc70 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -2,6 +2,7 @@ set(REQUIRES app_update bootloader_support bt + configulator esp_ringbuf ) diff --git a/src/baros_ble.c b/src/baros_ble.c index 573c014..ad7e67e 100644 --- a/src/baros_ble.c +++ b/src/baros_ble.c @@ -3,29 +3,400 @@ #include "freertos/FreeRTOS.h" #include "freertos/semphr.h" +#include "host/ble_hs.h" + +#include "nimble/nimble_port.h" +#include "nimble/nimble_port_freertos.h" +#include "services/gap/ble_svc_gap.h" + #include "esp_bt.h" #include "esp_log.h" #include "esp_event_base.h" #include "baros.h" #include "baros_ble_ota.h" +#include "configulator.h" #define TAG "BOS_BLE" ESP_EVENT_DEFINE_BASE(EVENT_BAROS_BLE); -uint8_t baros_ble_init(baros_ble_cfg_t *cfg) { - ESP_LOGV(TAG, "init"); +static uint8_t blehr_addr_type; +static uint16_t conn_handle; +static uint16_t ble_svc_handle; +static uint16_t svc_handle_button; +static uint16_t svc_handle_name; +static const char *device_name = CONFIG_BARBACK_ID; - esp_bt_controller_config_t bt_cfg = BT_CONTROLLER_INIT_CONFIG_DEFAULT(); +typedef struct { + uint16_t conn_handle; +} barback_ble_t; - esp_err_t ret = esp_bt_controller_init(&bt_cfg); - ESP_ERROR_CHECK(ret); +static int ble_gap_event(struct ble_gap_event *event, void *arg); +static int svc_access_name(uint16_t conn_handle, uint16_t attr_handle, struct ble_gatt_access_ctxt *ctxt, void *arg); +static int svc_access_system(uint16_t conn_handle, uint16_t attr_handle, struct ble_gatt_access_ctxt *ctxt, void *arg); - ret = esp_bt_controller_enable(ESP_BT_MODE_BLE); - ESP_ERROR_CHECK(ret); +static int barback_ble_char_access(uint16_t conn_handle, uint16_t attr_handle, struct ble_gatt_access_ctxt *ctxt, void *arg); - baros_ble_ota_init(); +static const struct ble_gatt_svc_def service_defs[] = { + { + .type = BLE_GATT_SVC_TYPE_PRIMARY, + .uuid = BLE_UUID16_DECLARE(BAROS_BLE_SERVICE_BARBACK), + .characteristics = (struct ble_gatt_chr_def[]) { + { + .uuid = BLE_UUID16_DECLARE(BAROS_CHAR_POUR), + .access_cb = svc_access_system, + .val_handle = &ble_svc_handle, + .flags = BLE_GATT_CHR_F_WRITE | BLE_GATT_CHR_F_READ + }, { + .uuid = BLE_UUID16_DECLARE(BAROS_CHAR_BUTTON), + .access_cb = svc_access_system, + .val_handle = &svc_handle_button, + .flags = BLE_GATT_CHR_F_READ | BLE_GATT_CHR_F_NOTIFY + }, { + .uuid = BLE_UUID16_DECLARE(BAROS_CHAR_NAME), + .access_cb = svc_access_name, + .val_handle = &svc_handle_name, + .flags = BLE_GATT_CHR_F_READ | BLE_GATT_CHR_F_WRITE | BLE_GATT_CHR_F_NOTIFY + }, + { 0 } + } + }, + { 0 } // no more services +}; + +static int gatt_svr_chr_write(struct os_mbuf *om, uint16_t min_len, uint16_t max_len, void *dst, uint16_t *len) { + uint16_t om_len; + int rc; + + om_len = OS_MBUF_PKTLEN(om); + ESP_LOGI(TAG, "om_len: %d", om_len); + if (om_len < min_len || om_len > max_len) { + return BLE_ATT_ERR_INVALID_ATTR_VALUE_LEN; + } + + rc = ble_hs_mbuf_to_flat(om, dst, max_len, len); + if (rc != 0) { + return BLE_ATT_ERR_UNLIKELY; + } + + return 0; +} + + +static void ble_advertise(void) { + // check if already adverting + if(ble_gap_adv_active()) return; + + struct ble_gap_adv_params adv_params; + struct ble_hs_adv_fields fields; + int rc; + + /* + * Set the advertisement data included in our advertisements: + * o Flags (indicates advertisement type and other general info) + * o Advertising tx power + * o Device name + */ + memset(&fields, 0, sizeof(fields)); + + /* + * Advertise two flags: + * o Discoverability in forthcoming advertisement (general) + * o BLE-only (BR/EDR unsupported) + */ + fields.flags = BLE_HS_ADV_F_DISC_GEN | + BLE_HS_ADV_F_BREDR_UNSUP; + + /* + * Indicate that the TX power level field should be included; have the + * stack fill this value automatically. This is done by assigning the + * special value BLE_HS_ADV_TX_PWR_LVL_AUTO. + */ + fields.tx_pwr_lvl_is_present = 1; + fields.tx_pwr_lvl = BLE_HS_ADV_TX_PWR_LVL_AUTO; + + fields.name = (uint8_t *)device_name; + fields.name_len = strlen(device_name); + fields.name_is_complete = 1; + + rc = ble_gap_adv_set_fields(&fields); + if (rc != 0) { + ESP_LOGE(TAG, "error setting advertisement data; rc=%d\n", rc); + return; + } + + /* Begin advertising */ + memset(&adv_params, 0, sizeof(adv_params)); + adv_params.conn_mode = BLE_GAP_CONN_MODE_UND; + adv_params.disc_mode = BLE_GAP_DISC_MODE_GEN; + rc = ble_gap_adv_start(blehr_addr_type, NULL, BLE_HS_FOREVER, + &adv_params, ble_gap_event, NULL); + if (rc != 0) { + ESP_LOGE(TAG, "error enabling advertisement; rc=%d\n", rc); + return; + } +} + +static int svc_access_system(uint16_t conn_handle, uint16_t attr_handle, struct ble_gatt_access_ctxt *ctxt, void *arg) { + uint16_t uuid16 = ble_uuid_u16(ctxt->chr->uuid); + uint8_t value = 0; + uint16_t vlen = 0; + + ESP_LOGI(TAG, "0x%02X access: %d", uuid16, ctxt->op); + + if(ctxt->op == BLE_GATT_ACCESS_OP_READ_CHR) { + } else if(ctxt->op == BLE_GATT_ACCESS_OP_WRITE_CHR) { + } + + return 0; +} + +static int barback_ble_char_access(uint16_t conn_handle, uint16_t attr_handle, struct ble_gatt_access_ctxt *ctxt, void *arg) { + uint16_t chr_uuid = ble_uuid_u16(ctxt->chr->uuid); + + // match the UUID against 0b111 + uint8_t idx = (chr_uuid & 0x07) - 1; + uint8_t value = 0; + + ESP_LOGI(TAG, "conn_handle: %d attr_handle: %d char: 0x%02X op: %s", conn_handle, attr_handle, chr_uuid, (ctxt->op == 0 ? "read" : ctxt->op == 1 ? "write" : "unknown")); + + if(ctxt->op == BLE_GATT_ACCESS_OP_WRITE_CHR) { + gatt_svr_chr_write( + ctxt->om, + sizeof value, + sizeof value, + &value, NULL + ); + + struct ble_gap_conn_desc notifiee; + + // TODO + // this could needs to be verified, it might actually be needed + // NiBLE could be passing notifications itself on chr_handler changes + for (uint16_t i = 0; i < CONFIG_NIMBLE_MAX_CONNECTIONS; i++) { + if(ble_gap_conn_find(i, ¬ifiee) == ESP_OK) { + ble_gattc_notify_custom(i, attr_handle, ctxt->om); + } + } + } else if(ctxt->op == BLE_GATT_ACCESS_OP_READ_CHR) { + } + + return 0; +} + +static int svc_access_name(uint16_t conn_handle, uint16_t attr_handle, struct ble_gatt_access_ctxt *ctxt, void *arg) { + uint16_t uuid16 = ble_uuid_u16(ctxt->chr->uuid); + + char *name = malloc(32); + bzero(name, 32); + uint16_t len; + + if(ctxt->op == BLE_GATT_ACCESS_OP_READ_CHR) { + } else if(ctxt->op == BLE_GATT_ACCESS_OP_WRITE_CHR) { + esp_err_t err = gatt_svr_chr_write( + ctxt->om, + 0, + 32, + name, + NULL + ); + + err = ble_svc_gap_device_name_set(name); + + ESP_LOGI(TAG, "configulator: %p", configulator); + cfglr_element_t *name_handle = cfglr_get_element(configulator, "device_name"); + memcpy(name_handle->data, name, name_handle->datatype_size); + //name_handle->data[name_handle->datatype_size - 1] = '\0'; + + cfglr_commit(name_handle); + } + + //ESP_LOGI(TAG, "read: %d", len); + ESP_LOGI(TAG, "name: %s", name); + + return 0; +} + +static int ble_gap_event(struct ble_gap_event *event, void *arg) { + switch (event->type) { + case BLE_GAP_EVENT_DISC: { + struct ble_hs_adv_fields fields; + + ESP_ERROR_CHECK(ble_hs_adv_parse_fields(&fields, event->disc.data, event->disc.length_data)); + + if(fields.name != NULL) { + char *name = (char*)malloc(fields.name_len); + memcpy(name, (uint8_t*)fields.name, fields.name_len); + name[fields.name_len] = '\0'; + ESP_LOGI(TAG, "DISCOVERY: %s", name); + + if(strstr(name, "BARBACK") != NULL) { + ESP_LOGI(TAG, "Let's Party!"); + uint8_t addr_type; + ble_gap_disc_cancel(); + + ESP_ERROR_CHECK(ble_hs_id_infer_auto(0, &addr_type)); + ESP_ERROR_CHECK(ble_gap_connect(addr_type, &event->disc.addr, 30000, NULL, ble_gap_event, NULL)); + } + } + + return 0; + } + case BLE_GAP_EVENT_CONNECT: + /* A new connection was established or a connection attempt failed */ + conn_handle = event->connect.conn_handle; + + ESP_LOGI(TAG, "connection %s; conn_handle: %d status=%d\n", + event->connect.status == 0 ? "established" : "failed", + conn_handle, + event->connect.status + ); + + // always keep advertising + ble_advertise(); + + break; + + case BLE_GAP_EVENT_DISCONNECT: + ESP_LOGI(TAG, "disconnect; reason=%d\n", event->disconnect.reason); + + /* Connection terminated; resume advertising */ + ble_advertise(); + + break; + + case BLE_GAP_EVENT_ADV_COMPLETE: + ESP_LOGI(TAG, "adv complete\n"); + + ble_advertise(); + break; + + case BLE_GAP_EVENT_SUBSCRIBE: + ESP_LOGI(TAG, "subscribe event; cur_notify=%d\n value handle; " + "val_handle=%d\n", + event->subscribe.cur_notify, event->subscribe.attr_handle); + + if(event->subscribe.attr_handle == svc_handle_button) { + } + + break; + + case BLE_GAP_EVENT_CONN_UPDATE: + ESP_LOGI(TAG, "BLE_GAP_EVENT_CONN_UPDATE, conn_handle: %d status: %d", + event->mtu.conn_handle, + event->enc_change.status + ); + + break; + + + case BLE_GAP_EVENT_ENC_CHANGE: + ESP_LOGI(TAG, "BLE_GAP_EVENT_ENC_CHANGE"); + + break; + + case BLE_GAP_EVENT_MTU: + ESP_LOGI(TAG, "mtu update event; conn_handle=%d mtu=%d\n", + event->mtu.conn_handle, + event->mtu.value); + break; + + case BLE_GAP_EVENT_NOTIFY_RX: + ESP_LOGI(TAG, "BLE_GAP_EVENT_NOTIFY_RX; conn_handle=%d mtu=%d attr_handle=%d\n", + event->mtu.conn_handle, + event->mtu.value, + event->notify_rx.attr_handle); + + break; + + case BLE_GAP_EVENT_NOTIFY_TX: + ESP_LOGI(TAG, "BLE_GAP_EVENT_NOTIFY_TX; conn_handle=%d mtu=%d\n", + event->mtu.conn_handle, + event->mtu.value); + break; + + default: + ESP_LOGW(TAG, "unhandled ble_gap_event; conn_handle=%d type=%d\n", + event->mtu.conn_handle, + event->type + ); + } + + return 0; +} + +void ble_init_buddy_client() { + uint8_t addr_type; + struct ble_gap_disc_params disc_params = { + .filter_duplicates = 1, + .passive = 1, + .itvl = 0, + .window = 0, + .filter_policy = 0, + .limited = 0, + }; + + ESP_ERROR_CHECK(ble_hs_id_infer_auto(0, &addr_type)); + ESP_ERROR_CHECK(ble_gap_disc(addr_type, BLE_HS_FOREVER, &disc_params, ble_gap_event, &addr_type)); +} + + +static void on_sync() { + ESP_LOGI(TAG, "on_sync"); + + int err; + + err = ble_hs_id_infer_auto(0, &blehr_addr_type); + ESP_ERROR_CHECK(err); + + uint8_t addr_val[6] = {0}; + err = ble_hs_id_copy_addr(blehr_addr_type, addr_val, NULL); + ESP_ERROR_CHECK(err); + + ble_advertise(); + ble_init_buddy_client(); +} + +static void on_reset(int reason) { + ESP_LOGE(TAG, "on_reset, reason: %d", reason); +} + +int8_t ble_send_notification(void *buf, uint8_t size) { + struct os_mbuf *om; + om = ble_hs_mbuf_from_flat(buf, size); + + return ble_gattc_notify_custom(conn_handle, svc_handle_button, om); +} + +void nimble_host_task(void *param) { + nimble_port_run(); +} + +uint8_t baros_ble_init(baros_ble_cfg_t *cfg) { + ESP_LOGV(TAG, "init"); + + esp_err_t err; + + nimble_port_init(); + + ble_hs_cfg.sync_cb = on_sync; + ble_hs_cfg.reset_cb = on_reset; + + // initialize services + err = ble_gatts_count_cfg(service_defs); + ESP_ERROR_CHECK(err); + + err = ble_gatts_add_svcs(service_defs); + ESP_ERROR_CHECK(err); + + ESP_LOGI(TAG, "Setting device name: %s", cfg->name); + + device_name = cfg->name; + err = ble_svc_gap_device_name_set(cfg->name); + ESP_ERROR_CHECK(err); + + nimble_port_freertos_init(nimble_host_task); return 0; }