Barback BLE DRY

This commit is contained in:
Morgan 'ARR\!' Allen 2021-09-10 21:38:19 -07:00
parent bd9feea036
commit a4253787a4
4 changed files with 52 additions and 69 deletions

14
include/barback.h Normal file
View file

@ -0,0 +1,14 @@
#ifndef __BARBACK_H__
#define __BARBACK_H__
typedef uint8_t (*barback_ble_access_read_t)(uint8_t idx);
typedef uint8_t (*barback_ble_access_write_t)(uint8_t idx, uint8_t value);
typedef struct {
uint16_t base_uuid;
barback_ble_access_read_t read;
barback_ble_access_write_t write;
} barback_ble_access_t;
#endif

View file

@ -13,8 +13,8 @@ void pumps_run();
void pumps_stop();
uint8_t pumps_init();
uint8_t pumps_set_time(uint8_t idx, uint8_t time);
uint8_t pumps_get_time(uint8_t idx);
uint8_t pumps_set_duration(uint8_t idx, uint8_t time);
uint8_t pumps_get_duration(uint8_t idx);
uint8_t pumps_set_enabled(uint8_t idx, uint8_t value);
uint8_t pumps_get_enabled(uint8_t idx);

View file

@ -15,6 +15,7 @@
#include "services/bas/ble_svc_bas.h"
#include "driver/gpio.h"
#include "barback.h"
#include "main.h"
#include "ble.h"
#include "pumps.h"
@ -44,10 +45,8 @@ 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 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);
static int svc_access_pump_duration(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);
static int barback_ble_char_access(uint16_t conn_handle, uint16_t attr_handle, struct ble_gatt_access_ctxt *ctxt, void *arg);
#define BLE_SERVICE_PUMP_ENABLED (0x4200)
#define BLE_SERVICE_PUMP_STATE (0x4300)
@ -57,23 +56,40 @@ static int svc_access_system(uint16_t conn_handle, uint16_t attr_handle, struct
#define CHAR_POUR (BLE_SERVICE_BARBACK + 1)
#define CHAR_BUTTON (BLE_SERVICE_BARBACK + 2)
static barback_ble_access_t barback_ble_pump_access_enabled = {
.read = pumps_get_enabled,
.write = pumps_set_enabled
};
#define BLE_CHAR_PUMP_ENABLED(idx) {\
.uuid = BLE_UUID16_DECLARE(BLE_SERVICE_PUMP_ENABLED + idx),\
.access_cb = svc_access_pump_enabled,\
.access_cb = barback_ble_char_access,\
.arg = &barback_ble_pump_access_enabled,\
.val_handle = &ble_svc_handle,\
.flags = BLE_GATT_CHR_F_NOTIFY | BLE_GATT_CHR_F_WRITE | BLE_GATT_CHR_F_READ\
}
static barback_ble_access_t barback_ble_pump_access_state = {
.read = pumps_get_state,
};
#define BLE_CHAR_PUMP_STATE(idx) {\
.uuid = BLE_UUID16_DECLARE(BLE_SERVICE_PUMP_STATE + idx),\
.access_cb = svc_access_pump_state,\
.access_cb = barback_ble_char_access,\
.arg = &barback_ble_pump_access_state,\
.val_handle = &ble_svc_handle,\
.flags = BLE_GATT_CHR_F_NOTIFY | BLE_GATT_CHR_F_READ\
}
static barback_ble_access_t barback_ble_pump_access_duration = {
.read = pumps_get_duration,
.write = pumps_set_duration
};
#define BLE_CHAR_PUMP_DURATION(idx) {\
.uuid = BLE_UUID16_DECLARE(BLE_SERVICE_PUMP_DURATION + idx),\
.access_cb = svc_access_pump_duration,\
.access_cb = barback_ble_char_access,\
.arg = &barback_ble_pump_access_duration,\
.val_handle = &pump_duration_svc_val_handles[idx - 1],\
.flags = BLE_GATT_CHR_F_WRITE | BLE_GATT_CHR_F_NOTIFY | BLE_GATT_CHR_F_READ\
}
@ -182,14 +198,15 @@ static int svc_access_system(uint16_t conn_handle, uint16_t attr_handle, struct
return 0;
}
static int svc_access_pump_enabled(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);
uint8_t idx = uuid16 - (BLE_SERVICE_PUMP_ENABLED + 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"));
if(ctxt->op == BLE_GATT_ACCESS_OP_WRITE_CHR) {
gatt_svr_chr_write(
ctxt->om,
@ -206,9 +223,15 @@ static int svc_access_pump_enabled(uint16_t conn_handle, uint16_t attr_handle, s
}
}
pumps_set_enabled(idx, value);
if(access->write != NULL)
access->write(idx, value);
} else if(ctxt->op == BLE_GATT_ACCESS_OP_READ_CHR) {
uint8_t enabled = pumps_get_enabled(idx);
if(access->read != NULL) {
ESP_LOGW(TAG, "Attempted read without defining access.read");
return 1;
}
uint8_t enabled = access->read(idx);
ESP_LOGI(TAG, "pumps_enabled[%d] = %d", idx, enabled);
os_mbuf_append(ctxt->om, &enabled, sizeof enabled);
@ -217,60 +240,6 @@ static int svc_access_pump_enabled(uint16_t conn_handle, uint16_t attr_handle, s
return 0;
}
static int svc_access_pump_duration(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_DURATION + 1);
uint8_t value = 0;
ESP_LOGI(TAG, "pump_duration: 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"));
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;
for (uint16_t i = 0; i < CONFIG_NIMBLE_MAX_CONNECTIONS; i++) {
if(ble_gap_conn_find(i, &notifiee) == ESP_OK) {
ble_gattc_notify_custom(i, attr_handle, ctxt->om);
}
}
pumps_set_time(idx, value);
} else if(ctxt->op == BLE_GATT_ACCESS_OP_READ_CHR) {
uint8_t duration = pumps_get_time(idx);
ESP_LOGI(TAG, "pumps_duration[%d] = %d", idx, duration);
os_mbuf_append(ctxt->om, &duration, sizeof duration);
}
return 0;
}
static int svc_access_pump_state(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);
ESP_LOGI(TAG, "pump_state: 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"));
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, "pumps_state[%d] = %d", idx, state);
os_mbuf_append(ctxt->om, &state, sizeof state);
}
return 0;
}
static void ble_advertise(void) {
// check if already adverting
if(ble_gap_adv_active()) return;

View file

@ -17,7 +17,7 @@ static uint8_t pumps_enabled[PUMPS] = {0};
static uint8_t pumps_state[PUMPS] = {0};
static uint8_t safety = 1;
uint8_t pumps_set_time(uint8_t idx, uint8_t time) {
uint8_t pumps_set_duration(uint8_t idx, uint8_t time) {
if(idx > PUMPS) return PUMPS_ERR_OUT_IDX;
ESP_LOGI(TAG, "duration update: %d = %d", idx, time);
@ -44,7 +44,7 @@ uint8_t pumps_get_enabled(uint8_t idx) {
return pumps_enabled[idx] ? 1 : 0;
}
uint8_t pumps_get_time(uint8_t idx) {
uint8_t pumps_get_duration(uint8_t idx) {
return pumps_duration[idx];
}