robustify and genericify BLE access
Some checks reported errors
continuous-integration/drone/push Build is passing
continuous-integration/drone Build was killed

This commit is contained in:
Morgan 'ARR\!' Allen 2021-09-23 23:00:03 -07:00
parent 88f4c96247
commit e9eb537eee

View file

@ -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) { 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); uint16_t chr_uuid = ble_uuid_u16(ctxt->chr->uuid);
uint8_t idx = uuid16 - (BLE_SERVICE_PUMP_ENABLED + 1);
// match the UUID against 0b111
uint8_t idx = (chr_uuid & 0x07) - 1;
uint8_t value = 0; uint8_t value = 0;
barback_ble_access_t *access = (barback_ble_access_t *)arg; 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) { if(ctxt->op == BLE_GATT_ACCESS_OP_WRITE_CHR) {
gatt_svr_chr_write( 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; 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++) { for (uint16_t i = 0; i < CONFIG_NIMBLE_MAX_CONNECTIONS; i++) {
if(ble_gap_conn_find(i, &notifiee) == ESP_OK) { if(ble_gap_conn_find(i, &notifiee) == ESP_OK) {
ble_gattc_notify_custom(i, attr_handle, ctxt->om); 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); 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) { if(access->read == NULL) {
ESP_LOGW(TAG, "Attempted read without defining access.read"); ESP_LOGW(TAG, "Attempted read without defining access.read");
return 1; return 1;
} }
uint8_t enabled = access->read(idx); uint8_t value = access->read(idx);
ESP_LOGI(TAG, "pumps_enabled[%d] = %d", idx, enabled); ESP_LOGI(TAG, "returning read idx: %d value: %d", idx, value);
os_mbuf_append(ctxt->om, &enabled, sizeof enabled); os_mbuf_append(ctxt->om, &value, sizeof value);
} }
return 0; return 0;