From 35fa84a3ffd81bc95228353358d68b6c3c8c9e82 Mon Sep 17 00:00:00 2001 From: "Morgan 'ARR\\!' Allen" Date: Mon, 26 Oct 2020 19:12:25 -0700 Subject: [PATCH] working towards notifications --- include/ble.h | 1 + include/pumps.h | 2 ++ main/ble.c | 47 ++++++++++++++++++++++++---------------------- main/pumps.c | 6 +++++- main/user_button.c | 15 ++++++++++++--- 5 files changed, 45 insertions(+), 26 deletions(-) diff --git a/include/ble.h b/include/ble.h index 8a63e9a..3056b7e 100644 --- a/include/ble.h +++ b/include/ble.h @@ -8,6 +8,7 @@ typedef struct { event_callback_t callback; } event_callback_handle_t; +int8_t ble_send_notification(void *buf, uint8_t size); void ble_init(); #endif diff --git a/include/pumps.h b/include/pumps.h index c5a2573..63c6b83 100644 --- a/include/pumps.h +++ b/include/pumps.h @@ -19,4 +19,6 @@ uint8_t pumps_get_time(uint8_t idx); uint8_t pumps_set_enabled(uint8_t idx, uint8_t value); uint8_t pumps_get_enabled(uint8_t idx); +uint8_t pumps_get_state(uint8_t idx); + #endif diff --git a/main/ble.c b/main/ble.c index d7b25ed..84099cf 100644 --- a/main/ble.c +++ b/main/ble.c @@ -28,10 +28,11 @@ static const char *device_name = "Barback"; static uint8_t blehr_addr_type; static uint8_t callback_handler_count = 0; static uint16_t conn_handle; +static uint16_t ble_svc_handle; +static uint16_t svc_handle_button; static event_callback_handle_t callback_handlers[CONFIG_CACO_MAX_SERVICES] = { 0 }; static int ble_gap_event(struct ble_gap_event *event, void *arg); -static uint16_t ble_svc_handle; static int ble_svc_access(uint16_t conn_handle, uint16_t attr_handle, struct ble_gatt_access_ctxt *ctxt, void *arg); static int svc_access_pump_state(uint16_t conn_handle, uint16_t attr_handle, struct ble_gatt_access_ctxt *ctxt, void *arg); static int svc_access_pump_enabled(uint16_t conn_handle, uint16_t attr_handle, struct ble_gatt_access_ctxt *ctxt, void *arg); @@ -119,7 +120,7 @@ static const struct ble_gatt_svc_def service_defs[] = { }, { .uuid = BLE_UUID16_DECLARE(CHAR_BUTTON), .access_cb = svc_access_system, - .val_handle = &ble_svc_handle, + .val_handle = &svc_handle_button, .flags = BLE_GATT_CHR_F_READ | BLE_GATT_CHR_F_NOTIFY }, { 0 } @@ -236,13 +237,18 @@ static int svc_access_pump_state(uint16_t conn_handle, uint16_t attr_handle, str uint16_t uuid16 = ble_uuid_u16(ctxt->chr->uuid); uint8_t idx = uuid16 - (BLE_SERVICE_PUMP_ENABLED + 1); - if(ctxt->op != BLE_GATT_ACCESS_OP_READ_CHR) { + ESP_LOGI(TAG, "0x%02X access: %d", uuid16, ctxt->op); + + if(ctxt->op == BLE_GATT_ACCESS_OP_WRITE_CHR) { ESP_LOGW(TAG, "attempting non-read op on read only characteristics"); return 1; - } + } else if(ctxt->op == BLE_GATT_ACCESS_OP_READ_CHR) { + uint8_t state = pumps_get_state(idx); - ESP_LOGI(TAG, "0x%02X access: %d", uuid16, ctxt->op); + ESP_LOGI(TAG, "pumps_state[%d] = %d", idx, state); + os_mbuf_append(ctxt->om, &state, sizeof state); + } return 0; } @@ -299,8 +305,6 @@ static void ble_advertise(void) { } static int ble_gap_event(struct ble_gap_event *event, void *arg) { - uint8_t i = 0; - switch (event->type) { case BLE_GAP_EVENT_CONNECT: /* A new connection was established or a connection attempt failed */ @@ -334,22 +338,14 @@ static int ble_gap_event(struct ble_gap_event *event, void *arg) { break; case BLE_GAP_EVENT_SUBSCRIBE: - /* - ESP_LOGI(TAG, "handling BLE_GAP_EVENT_SUBSCRIBE"); - ESP_LOGI(TAG, "attr_handle: %d", event->subscribe.attr_handle); - ESP_LOGI(TAG, "callback_handler_count: %d", callback_handler_count); + ESP_LOGI(TAG, "subscribe event; cur_notify=%d\n value handle; " + "val_handle=%d\n", + event->subscribe.cur_notify, svc_handle_button); - for(i = 0; (i < CONFIG_CACO_MAX_SERVICES); i++) { - if(callback_handlers[i].handle != NULL) continue; - - ESP_LOGI(TAG, "callback_handler: %d", (uint32_t)(callback_handlers[i].handle)); - - if (event->subscribe.attr_handle == (uint32_t)(*callback_handlers[i].handle)) { - callback_handlers[i].callback(event, arg); - } + 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", @@ -372,9 +368,9 @@ static int ble_gap_event(struct ble_gap_event *event, void *arg) { break; default: - ESP_LOGW(TAG, "unhandled ble_gap_event; conn_handle=%d mtu=%d\n", + ESP_LOGW(TAG, "unhandled ble_gap_event; conn_handle=%d type=%d\n", event->mtu.conn_handle, - event->mtu.value + event->type ); } @@ -404,6 +400,13 @@ void nimble_host_task(void *param) { nimble_port_run(); } +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 ble_init() { esp_err_t err; diff --git a/main/pumps.c b/main/pumps.c index 0fe77bf..650d082 100644 --- a/main/pumps.c +++ b/main/pumps.c @@ -14,7 +14,7 @@ uint8_t running = 0; TimerHandle_t pump_timers[PUMPS]; static uint8_t pumps_duration[PUMPS] = {0}; static uint8_t pumps_enabled[PUMPS] = {0}; -static uint8_t pumps_state[PUMPS]; +static uint8_t pumps_state[PUMPS] = {0}; static uint8_t safety = 1; uint8_t pumps_set_time(uint8_t idx, uint8_t time) { @@ -48,6 +48,10 @@ uint8_t pumps_get_time(uint8_t idx) { return pumps_duration[idx]; } +uint8_t pumps_get_state(uint8_t idx) { + return pumps_state[idx]; +} + uint8_t pump_enable(int8_t i) { return gpio_set_level(pump_gpio_map[i], 0); } diff --git a/main/user_button.c b/main/user_button.c index b0cf5ec..098df5e 100644 --- a/main/user_button.c +++ b/main/user_button.c @@ -5,6 +5,7 @@ #include "main.h" #include "pumps.h" +#include "ble.h" #define GPIO_USER_BUTTON (19) @@ -14,16 +15,24 @@ static void IRAM_ATTR gpio_isr_handler(void* arg) { xQueueSendFromISR(gpio_evt_queue, &gpio_num, NULL); } -static void gpio_task_example(void* arg) { +static void gpio_task(void* arg) { uint32_t io_num; + uint8_t on = 1; + uint8_t off = 0; for(;;) { if(xQueueReceive(gpio_evt_queue, &io_num, portMAX_DELAY)) { - printf("GPIO[%d] intr, val: %d\n", io_num, gpio_get_level(io_num)); + uint8_t level = gpio_get_level(io_num); + printf("GPIO[%d] intr, val: %d\n", io_num, level); pumps_run(); + + ble_send_notification((void*)&on, 1); + // just a wee debounce vTaskDelay(500 / portTICK_PERIOD_MS); + + ble_send_notification((void*)&off, 1); } } } @@ -40,7 +49,7 @@ void user_button_init() { gpio_config(&io_conf); gpio_evt_queue = xQueueCreate(10, sizeof(uint32_t)); - xTaskCreate(gpio_task_example, "gpio_task_example", 4096, NULL, 10, NULL); + xTaskCreate(gpio_task, "gpio_task", 4096, NULL, 10, NULL); gpio_install_isr_service(0); gpio_isr_handler_add(GPIO_USER_BUTTON, gpio_isr_handler, (void*) 0);