diff --git a/main/ble.c b/main/ble.c index eba7a83..a440a2b 100644 --- a/main/ble.c +++ b/main/ble.c @@ -198,13 +198,15 @@ static int svc_access_system(uint16_t conn_handle, uint16_t attr_handle, struct } static int barback_ble_char_access(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 idx = uuid16 - (BLE_SERVICE_PUMP_ENABLED + 1); + 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; barback_ble_access_t *access = (barback_ble_access_t *)arg; - ESP_LOGI(TAG, "conn_handle: %d attr_handle: %d char: 0x%02X op: %s", conn_handle, attr_handle, uuid16, (ctxt->op == 0 ? "read" : ctxt->op == 1 ? "write" : "unknown")); + 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( @@ -216,24 +218,35 @@ static int barback_ble_char_access(uint16_t conn_handle, uint16_t attr_handle, s 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); } } - if(access->write != NULL) + if(access == NULL) { + ESP_LOGI(TAG, "no read/write functions defined"); + return 0; + } + + if(access->write != NULL) { + ESP_LOGI(TAG, "calling BLE write function: %d %d", idx, value); + access->write(idx, value); - } else if(ctxt->op == BLE_GATT_ACCESS_OP_READ_CHR) { + } + } else if(access != NULL && ctxt->op == BLE_GATT_ACCESS_OP_READ_CHR) { if(access->read == NULL) { ESP_LOGW(TAG, "Attempted read without defining access.read"); return 1; } - uint8_t enabled = access->read(idx); + uint8_t value = access->read(idx); - ESP_LOGI(TAG, "pumps_enabled[%d] = %d", idx, enabled); - os_mbuf_append(ctxt->om, &enabled, sizeof enabled); + ESP_LOGI(TAG, "returning read idx: %d value: %d", idx, value); + os_mbuf_append(ctxt->om, &value, sizeof value); } return 0;