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);
@ -48,95 +58,95 @@ static int svc_access_system(uint16_t conn_handle, uint16_t attr_handle, struct
#define CHAR_BUTTON (BLE_SERVICE_BARBACK + 2)
#define BLE_CHAR_PUMP_ENABLED(idx) {\
.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\
}
.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_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\
}
.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\
}
.uuid = BLE_UUID16_DECLARE(BLE_SERVICE_PUMP_DURATION + idx),\
.access_cb = svc_access_pump_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\
}
static const struct ble_gatt_svc_def service_defs[] = {
{
.type = BLE_GATT_SVC_TYPE_PRIMARY,
.uuid = BLE_UUID16_DECLARE(BLE_SERVICE_PUMP_ENABLED),
{
.type = BLE_GATT_SVC_TYPE_PRIMARY,
.uuid = BLE_UUID16_DECLARE(BLE_SERVICE_PUMP_ENABLED),
.characteristics = (struct ble_gatt_chr_def[]) {
BLE_CHAR_PUMP_ENABLED(1),
BLE_CHAR_PUMP_ENABLED(2),
BLE_CHAR_PUMP_ENABLED(3),
BLE_CHAR_PUMP_ENABLED(4),
{ 0 }
},
}, {
.type = BLE_GATT_SVC_TYPE_PRIMARY,
.uuid = BLE_UUID16_DECLARE(BLE_SERVICE_PUMP_STATE),
.characteristics = (struct ble_gatt_chr_def[]) {
BLE_CHAR_PUMP_STATE(1),
BLE_CHAR_PUMP_STATE(2),
BLE_CHAR_PUMP_STATE(3),
BLE_CHAR_PUMP_STATE(4),
{ 0 }
},
}, {
.type = BLE_GATT_SVC_TYPE_PRIMARY,
.uuid = BLE_UUID16_DECLARE(BLE_SERVICE_PUMP_DURATION),
.characteristics = (struct ble_gatt_chr_def[]) {
BLE_CHAR_PUMP_DURATION(1),
BLE_CHAR_PUMP_DURATION(2),
BLE_CHAR_PUMP_DURATION(3),
BLE_CHAR_PUMP_DURATION(4),
{ 0 }
},
}, {
.type = BLE_GATT_SVC_TYPE_PRIMARY,
.uuid = BLE_UUID16_DECLARE(BLE_SERVICE_BARBACK),
.characteristics = (struct ble_gatt_chr_def[]) {
{
.uuid = BLE_UUID16_DECLARE(CHAR_POUR),
.access_cb = svc_access_system,
.val_handle = &ble_svc_handle,
.flags = BLE_GATT_CHR_F_WRITE | BLE_GATT_CHR_F_READ
}, {
.uuid = BLE_UUID16_DECLARE(CHAR_BUTTON),
.access_cb = svc_access_system,
.val_handle = &svc_handle_button,
.flags = BLE_GATT_CHR_F_READ | BLE_GATT_CHR_F_NOTIFY
},
{ 0 }
}
},
{ 0 } // no more services
.characteristics = (struct ble_gatt_chr_def[]) {
BLE_CHAR_PUMP_ENABLED(1),
BLE_CHAR_PUMP_ENABLED(2),
BLE_CHAR_PUMP_ENABLED(3),
BLE_CHAR_PUMP_ENABLED(4),
{ 0 }
},
}, {
.type = BLE_GATT_SVC_TYPE_PRIMARY,
.uuid = BLE_UUID16_DECLARE(BLE_SERVICE_PUMP_STATE),
.characteristics = (struct ble_gatt_chr_def[]) {
BLE_CHAR_PUMP_STATE(1),
BLE_CHAR_PUMP_STATE(2),
BLE_CHAR_PUMP_STATE(3),
BLE_CHAR_PUMP_STATE(4),
{ 0 }
},
}, {
.type = BLE_GATT_SVC_TYPE_PRIMARY,
.uuid = BLE_UUID16_DECLARE(BLE_SERVICE_PUMP_DURATION),
.characteristics = (struct ble_gatt_chr_def[]) {
BLE_CHAR_PUMP_DURATION(1),
BLE_CHAR_PUMP_DURATION(2),
BLE_CHAR_PUMP_DURATION(3),
BLE_CHAR_PUMP_DURATION(4),
{ 0 }
},
}, {
.type = BLE_GATT_SVC_TYPE_PRIMARY,
.uuid = BLE_UUID16_DECLARE(BLE_SERVICE_BARBACK),
.characteristics = (struct ble_gatt_chr_def[]) {
{
.uuid = BLE_UUID16_DECLARE(CHAR_POUR),
.access_cb = svc_access_system,
.val_handle = &ble_svc_handle,
.flags = BLE_GATT_CHR_F_WRITE | BLE_GATT_CHR_F_READ
}, {
.uuid = BLE_UUID16_DECLARE(CHAR_BUTTON),
.access_cb = svc_access_system,
.val_handle = &svc_handle_button,
.flags = BLE_GATT_CHR_F_READ | BLE_GATT_CHR_F_NOTIFY
},
{ 0 }
}
},
{ 0 } // no more services
};
static int gatt_svr_chr_write(struct os_mbuf *om, uint16_t min_len, uint16_t max_len,
void *dst, uint16_t *len) {
uint16_t om_len;
int rc;
void *dst, uint16_t *len) {
uint16_t om_len;
int rc;
om_len = OS_MBUF_PKTLEN(om);
if (om_len < min_len || om_len > max_len) {
return BLE_ATT_ERR_INVALID_ATTR_VALUE_LEN;
}
om_len = OS_MBUF_PKTLEN(om);
if (om_len < min_len || om_len > max_len) {
return BLE_ATT_ERR_INVALID_ATTR_VALUE_LEN;
}
rc = ble_hs_mbuf_to_flat(om, dst, max_len, len);
if (rc != 0) {
return BLE_ATT_ERR_UNLIKELY;
}
rc = ble_hs_mbuf_to_flat(om, dst, max_len, len);
if (rc != 0) {
return BLE_ATT_ERR_UNLIKELY;
}
return 0;
return 0;
}
static int svc_access_system(uint16_t conn_handle, uint16_t attr_handle, struct ble_gatt_access_ctxt *ctxt, void *arg) {
@ -152,7 +162,7 @@ static int svc_access_system(uint16_t conn_handle, uint16_t attr_handle, struct
sizeof value,
sizeof value,
&value, NULL
);
);
switch(uuid16) {
case CHAR_POUR:
@ -162,10 +172,10 @@ static int svc_access_system(uint16_t conn_handle, uint16_t attr_handle, struct
} else {
pumps_stop();
}
break;
break;
case CHAR_BUTTON:
break;
break;
}
}
@ -176,17 +186,25 @@ 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);
if(ctxt->op == BLE_GATT_ACCESS_OP_WRITE_CHR) {
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_enabled(idx, value);
} else if(ctxt->op == BLE_GATT_ACCESS_OP_READ_CHR) {
@ -204,15 +222,23 @@ 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) {
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) {
@ -229,12 +255,12 @@ 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");
if(ctxt->op == BLE_GATT_ACCESS_OP_WRITE_CHR) {
ESP_LOGW(TAG, "attempting non-read op on read only characteristics");
return 1;
return 1;
} else if(ctxt->op == BLE_GATT_ACCESS_OP_READ_CHR) {
uint8_t state = pumps_get_state(idx);
@ -242,10 +268,13 @@ static int svc_access_pump_state(uint16_t conn_handle, uint16_t attr_handle, str
os_mbuf_append(ctxt->om, &state, sizeof state);
}
return 0;
return 0;
}
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;
@ -303,15 +332,13 @@ static int ble_gap_event(struct ble_gap_event *event, void *arg) {
conn_handle = event->connect.conn_handle;
ESP_LOGI(TAG, "connection %s; conn_handle: %d status=%d\n",
event->connect.status == 0 ? "established" : "failed",
conn_handle,
event->connect.status
);
event->connect.status == 0 ? "established" : "failed",
conn_handle,
event->connect.status
);
if (event->connect.status != 0) {
/* Connection failed; resume advertising */
ble_advertise();
}
// always keep advertising
ble_advertise();
break;
@ -331,8 +358,8 @@ 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);
"val_handle=%d\n",
event->subscribe.cur_notify, event->subscribe.attr_handle);
if(event->subscribe.attr_handle == svc_handle_button) {
}
@ -341,9 +368,9 @@ static int ble_gap_event(struct ble_gap_event *event, void *arg) {
case BLE_GAP_EVENT_CONN_UPDATE:
ESP_LOGI(TAG, "BLE_GAP_EVENT_CONN_UPDATE, conn_handle: %d status: %d",
event->mtu.conn_handle,
event->enc_change.status
);
event->mtu.conn_handle,
event->enc_change.status
);
break;
@ -359,11 +386,17 @@ 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,
event->type
);
event->mtu.conn_handle,
event->type
);
}
return 0;