all the BLE changes

This commit is contained in:
Morgan 'ARR\!' Allen 2021-09-09 21:32:23 -07:00
parent 584d6dc04a
commit ad9ffa46c1

View file

@ -26,10 +26,20 @@
static const char *device_name = CONFIG_BARBACK_ID;
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;
// ^^^OLD^^^
// |||NEW|||
typedef struct {
uint16_t conn_handle;
} barback_ble_t;
static barback_ble_t conn_handles[CONFIG_NIMBLE_MAX_CONNECTIONS];
static uint16_t pump_duration_svc_val_handles[4];
static event_callback_handle_t callback_handlers[CONFIG_CACO_MAX_SERVICES] = { 0 };
static int ble_gap_event(struct ble_gap_event *event, void *arg);
@ -51,22 +61,22 @@ static int svc_access_system(uint16_t conn_handle, uint16_t attr_handle, struct
.uuid = BLE_UUID16_DECLARE(BLE_SERVICE_PUMP_ENABLED + idx),\
.access_cb = svc_access_pump_enabled,\
.val_handle = &ble_svc_handle,\
.flags = BLE_GATT_CHR_F_WRITE | BLE_GATT_CHR_F_READ\
}
.flags = BLE_GATT_CHR_F_NOTIFY | BLE_GATT_CHR_F_WRITE | BLE_GATT_CHR_F_READ\
}
#define BLE_CHAR_PUMP_STATE(idx) {\
.uuid = BLE_UUID16_DECLARE(BLE_SERVICE_PUMP_STATE + idx),\
.access_cb = svc_access_pump_state,\
.val_handle = &ble_svc_handle,\
.flags = BLE_GATT_CHR_F_NOTIFY | BLE_GATT_CHR_F_READ\
}
}
#define BLE_CHAR_PUMP_DURATION(idx) {\
.uuid = BLE_UUID16_DECLARE(BLE_SERVICE_PUMP_DURATION + idx),\
.access_cb = svc_access_pump_duration,\
.val_handle = &ble_svc_handle,\
.flags = BLE_GATT_CHR_F_WRITE | BLE_GATT_CHR_F_READ\
}
.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\
}
static const struct ble_gatt_svc_def service_defs[] = {
{
@ -176,7 +186,7 @@ static int svc_access_pump_enabled(uint16_t conn_handle, uint16_t attr_handle, s
uint16_t uuid16 = ble_uuid_u16(ctxt->chr->uuid);
uint8_t value = 0;
ESP_LOGI(TAG, "0x%02X access: %d", uuid16, ctxt->op);
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"));
uint8_t idx = uuid16 - (BLE_SERVICE_PUMP_ENABLED + 1);
@ -188,6 +198,14 @@ static int svc_access_pump_enabled(uint16_t conn_handle, uint16_t attr_handle, s
&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_enabled(idx, value);
} else if(ctxt->op == BLE_GATT_ACCESS_OP_READ_CHR) {
uint8_t enabled = pumps_get_enabled(idx);
@ -204,7 +222,7 @@ static int svc_access_pump_duration(uint16_t conn_handle, uint16_t attr_handle,
uint8_t idx = uuid16 - (BLE_SERVICE_PUMP_DURATION + 1);
uint8_t value = 0;
ESP_LOGI(TAG, "0x%02X access: %d", uuid16, ctxt->op);
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(
@ -214,6 +232,14 @@ static int svc_access_pump_duration(uint16_t conn_handle, uint16_t attr_handle,
&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);
@ -229,7 +255,7 @@ 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);
ESP_LOGI(TAG, "0x%02X access: %d", uuid16, ctxt->op);
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");
@ -246,6 +272,9 @@ static int svc_access_pump_state(uint16_t conn_handle, uint16_t attr_handle, str
}
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;
@ -308,10 +337,8 @@ static int ble_gap_event(struct ble_gap_event *event, void *arg) {
event->connect.status
);
if (event->connect.status != 0) {
/* Connection failed; resume advertising */
// always keep advertising
ble_advertise();
}
break;
@ -332,7 +359,7 @@ static int ble_gap_event(struct ble_gap_event *event, void *arg) {
case BLE_GAP_EVENT_SUBSCRIBE:
ESP_LOGI(TAG, "subscribe event; cur_notify=%d\n value handle; "
"val_handle=%d\n",
event->subscribe.cur_notify, svc_handle_button);
event->subscribe.cur_notify, event->subscribe.attr_handle);
if(event->subscribe.attr_handle == svc_handle_button) {
}
@ -359,6 +386,12 @@ static int ble_gap_event(struct ble_gap_event *event, void *arg) {
event->mtu.value);
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,