all the BLE changes
This commit is contained in:
parent
584d6dc04a
commit
ad9ffa46c1
1 changed files with 140 additions and 107 deletions
247
main/ble.c
247
main/ble.c
|
@ -26,10 +26,20 @@
|
||||||
static const char *device_name = CONFIG_BARBACK_ID;
|
static const char *device_name = CONFIG_BARBACK_ID;
|
||||||
|
|
||||||
static uint8_t blehr_addr_type;
|
static uint8_t blehr_addr_type;
|
||||||
static uint8_t callback_handler_count = 0;
|
|
||||||
static uint16_t conn_handle;
|
static uint16_t conn_handle;
|
||||||
static uint16_t ble_svc_handle;
|
static uint16_t ble_svc_handle;
|
||||||
static uint16_t svc_handle_button;
|
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 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_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 CHAR_BUTTON (BLE_SERVICE_BARBACK + 2)
|
||||||
|
|
||||||
#define BLE_CHAR_PUMP_ENABLED(idx) {\
|
#define BLE_CHAR_PUMP_ENABLED(idx) {\
|
||||||
.uuid = BLE_UUID16_DECLARE(BLE_SERVICE_PUMP_ENABLED + idx),\
|
.uuid = BLE_UUID16_DECLARE(BLE_SERVICE_PUMP_ENABLED + idx),\
|
||||||
.access_cb = svc_access_pump_enabled,\
|
.access_cb = svc_access_pump_enabled,\
|
||||||
.val_handle = &ble_svc_handle,\
|
.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) {\
|
#define BLE_CHAR_PUMP_STATE(idx) {\
|
||||||
.uuid = BLE_UUID16_DECLARE(BLE_SERVICE_PUMP_STATE + idx),\
|
.uuid = BLE_UUID16_DECLARE(BLE_SERVICE_PUMP_STATE + idx),\
|
||||||
.access_cb = svc_access_pump_state,\
|
.access_cb = svc_access_pump_state,\
|
||||||
.val_handle = &ble_svc_handle,\
|
.val_handle = &ble_svc_handle,\
|
||||||
.flags = BLE_GATT_CHR_F_NOTIFY | BLE_GATT_CHR_F_READ\
|
.flags = BLE_GATT_CHR_F_NOTIFY | BLE_GATT_CHR_F_READ\
|
||||||
}
|
}
|
||||||
|
|
||||||
#define BLE_CHAR_PUMP_DURATION(idx) {\
|
#define BLE_CHAR_PUMP_DURATION(idx) {\
|
||||||
.uuid = BLE_UUID16_DECLARE(BLE_SERVICE_PUMP_DURATION + idx),\
|
.uuid = BLE_UUID16_DECLARE(BLE_SERVICE_PUMP_DURATION + idx),\
|
||||||
.access_cb = svc_access_pump_duration,\
|
.access_cb = svc_access_pump_duration,\
|
||||||
.val_handle = &ble_svc_handle,\
|
.val_handle = &pump_duration_svc_val_handles[idx - 1],\
|
||||||
.flags = BLE_GATT_CHR_F_WRITE | BLE_GATT_CHR_F_READ\
|
.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[] = {
|
static const struct ble_gatt_svc_def service_defs[] = {
|
||||||
{
|
{
|
||||||
.type = BLE_GATT_SVC_TYPE_PRIMARY,
|
.type = BLE_GATT_SVC_TYPE_PRIMARY,
|
||||||
.uuid = BLE_UUID16_DECLARE(BLE_SERVICE_PUMP_ENABLED),
|
.uuid = BLE_UUID16_DECLARE(BLE_SERVICE_PUMP_ENABLED),
|
||||||
|
|
||||||
.characteristics = (struct ble_gatt_chr_def[]) {
|
.characteristics = (struct ble_gatt_chr_def[]) {
|
||||||
BLE_CHAR_PUMP_ENABLED(1),
|
BLE_CHAR_PUMP_ENABLED(1),
|
||||||
BLE_CHAR_PUMP_ENABLED(2),
|
BLE_CHAR_PUMP_ENABLED(2),
|
||||||
BLE_CHAR_PUMP_ENABLED(3),
|
BLE_CHAR_PUMP_ENABLED(3),
|
||||||
BLE_CHAR_PUMP_ENABLED(4),
|
BLE_CHAR_PUMP_ENABLED(4),
|
||||||
{ 0 }
|
{ 0 }
|
||||||
},
|
},
|
||||||
}, {
|
}, {
|
||||||
.type = BLE_GATT_SVC_TYPE_PRIMARY,
|
.type = BLE_GATT_SVC_TYPE_PRIMARY,
|
||||||
.uuid = BLE_UUID16_DECLARE(BLE_SERVICE_PUMP_STATE),
|
.uuid = BLE_UUID16_DECLARE(BLE_SERVICE_PUMP_STATE),
|
||||||
.characteristics = (struct ble_gatt_chr_def[]) {
|
.characteristics = (struct ble_gatt_chr_def[]) {
|
||||||
BLE_CHAR_PUMP_STATE(1),
|
BLE_CHAR_PUMP_STATE(1),
|
||||||
BLE_CHAR_PUMP_STATE(2),
|
BLE_CHAR_PUMP_STATE(2),
|
||||||
BLE_CHAR_PUMP_STATE(3),
|
BLE_CHAR_PUMP_STATE(3),
|
||||||
BLE_CHAR_PUMP_STATE(4),
|
BLE_CHAR_PUMP_STATE(4),
|
||||||
{ 0 }
|
{ 0 }
|
||||||
},
|
},
|
||||||
}, {
|
}, {
|
||||||
.type = BLE_GATT_SVC_TYPE_PRIMARY,
|
.type = BLE_GATT_SVC_TYPE_PRIMARY,
|
||||||
.uuid = BLE_UUID16_DECLARE(BLE_SERVICE_PUMP_DURATION),
|
.uuid = BLE_UUID16_DECLARE(BLE_SERVICE_PUMP_DURATION),
|
||||||
.characteristics = (struct ble_gatt_chr_def[]) {
|
.characteristics = (struct ble_gatt_chr_def[]) {
|
||||||
BLE_CHAR_PUMP_DURATION(1),
|
BLE_CHAR_PUMP_DURATION(1),
|
||||||
BLE_CHAR_PUMP_DURATION(2),
|
BLE_CHAR_PUMP_DURATION(2),
|
||||||
BLE_CHAR_PUMP_DURATION(3),
|
BLE_CHAR_PUMP_DURATION(3),
|
||||||
BLE_CHAR_PUMP_DURATION(4),
|
BLE_CHAR_PUMP_DURATION(4),
|
||||||
{ 0 }
|
{ 0 }
|
||||||
},
|
},
|
||||||
}, {
|
}, {
|
||||||
.type = BLE_GATT_SVC_TYPE_PRIMARY,
|
.type = BLE_GATT_SVC_TYPE_PRIMARY,
|
||||||
.uuid = BLE_UUID16_DECLARE(BLE_SERVICE_BARBACK),
|
.uuid = BLE_UUID16_DECLARE(BLE_SERVICE_BARBACK),
|
||||||
.characteristics = (struct ble_gatt_chr_def[]) {
|
.characteristics = (struct ble_gatt_chr_def[]) {
|
||||||
{
|
{
|
||||||
.uuid = BLE_UUID16_DECLARE(CHAR_POUR),
|
.uuid = BLE_UUID16_DECLARE(CHAR_POUR),
|
||||||
.access_cb = svc_access_system,
|
.access_cb = svc_access_system,
|
||||||
.val_handle = &ble_svc_handle,
|
.val_handle = &ble_svc_handle,
|
||||||
.flags = BLE_GATT_CHR_F_WRITE | BLE_GATT_CHR_F_READ
|
.flags = BLE_GATT_CHR_F_WRITE | BLE_GATT_CHR_F_READ
|
||||||
}, {
|
}, {
|
||||||
.uuid = BLE_UUID16_DECLARE(CHAR_BUTTON),
|
.uuid = BLE_UUID16_DECLARE(CHAR_BUTTON),
|
||||||
.access_cb = svc_access_system,
|
.access_cb = svc_access_system,
|
||||||
.val_handle = &svc_handle_button,
|
.val_handle = &svc_handle_button,
|
||||||
.flags = BLE_GATT_CHR_F_READ | BLE_GATT_CHR_F_NOTIFY
|
.flags = BLE_GATT_CHR_F_READ | BLE_GATT_CHR_F_NOTIFY
|
||||||
},
|
},
|
||||||
{ 0 }
|
{ 0 }
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
{ 0 } // no more services
|
{ 0 } // no more services
|
||||||
};
|
};
|
||||||
|
|
||||||
static int gatt_svr_chr_write(struct os_mbuf *om, uint16_t min_len, uint16_t max_len,
|
static int gatt_svr_chr_write(struct os_mbuf *om, uint16_t min_len, uint16_t max_len,
|
||||||
void *dst, uint16_t *len) {
|
void *dst, uint16_t *len) {
|
||||||
uint16_t om_len;
|
uint16_t om_len;
|
||||||
int rc;
|
int rc;
|
||||||
|
|
||||||
om_len = OS_MBUF_PKTLEN(om);
|
om_len = OS_MBUF_PKTLEN(om);
|
||||||
if (om_len < min_len || om_len > max_len) {
|
if (om_len < min_len || om_len > max_len) {
|
||||||
return BLE_ATT_ERR_INVALID_ATTR_VALUE_LEN;
|
return BLE_ATT_ERR_INVALID_ATTR_VALUE_LEN;
|
||||||
}
|
}
|
||||||
|
|
||||||
rc = ble_hs_mbuf_to_flat(om, dst, max_len, len);
|
rc = ble_hs_mbuf_to_flat(om, dst, max_len, len);
|
||||||
if (rc != 0) {
|
if (rc != 0) {
|
||||||
return BLE_ATT_ERR_UNLIKELY;
|
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) {
|
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,
|
||||||
sizeof value,
|
sizeof value,
|
||||||
&value, NULL
|
&value, NULL
|
||||||
);
|
);
|
||||||
|
|
||||||
switch(uuid16) {
|
switch(uuid16) {
|
||||||
case CHAR_POUR:
|
case CHAR_POUR:
|
||||||
|
@ -162,10 +172,10 @@ static int svc_access_system(uint16_t conn_handle, uint16_t attr_handle, struct
|
||||||
} else {
|
} else {
|
||||||
pumps_stop();
|
pumps_stop();
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case CHAR_BUTTON:
|
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);
|
uint16_t uuid16 = ble_uuid_u16(ctxt->chr->uuid);
|
||||||
uint8_t value = 0;
|
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);
|
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(
|
gatt_svr_chr_write(
|
||||||
ctxt->om,
|
ctxt->om,
|
||||||
sizeof value,
|
sizeof value,
|
||||||
sizeof value,
|
sizeof value,
|
||||||
&value, NULL
|
&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, ¬ifiee) == ESP_OK) {
|
||||||
|
ble_gattc_notify_custom(i, attr_handle, ctxt->om);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
pumps_set_enabled(idx, value);
|
pumps_set_enabled(idx, value);
|
||||||
} else if(ctxt->op == BLE_GATT_ACCESS_OP_READ_CHR) {
|
} 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 idx = uuid16 - (BLE_SERVICE_PUMP_DURATION + 1);
|
||||||
uint8_t value = 0;
|
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(
|
gatt_svr_chr_write(
|
||||||
ctxt->om,
|
ctxt->om,
|
||||||
sizeof value,
|
sizeof value,
|
||||||
sizeof value,
|
sizeof value,
|
||||||
&value, NULL
|
&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, ¬ifiee) == ESP_OK) {
|
||||||
|
ble_gattc_notify_custom(i, attr_handle, ctxt->om);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
pumps_set_time(idx, value);
|
pumps_set_time(idx, value);
|
||||||
} else if(ctxt->op == BLE_GATT_ACCESS_OP_READ_CHR) {
|
} 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);
|
uint16_t uuid16 = ble_uuid_u16(ctxt->chr->uuid);
|
||||||
uint8_t idx = uuid16 - (BLE_SERVICE_PUMP_ENABLED + 1);
|
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) {
|
if(ctxt->op == BLE_GATT_ACCESS_OP_WRITE_CHR) {
|
||||||
ESP_LOGW(TAG, "attempting non-read op on read only characteristics");
|
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) {
|
} else if(ctxt->op == BLE_GATT_ACCESS_OP_READ_CHR) {
|
||||||
uint8_t state = pumps_get_state(idx);
|
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);
|
os_mbuf_append(ctxt->om, &state, sizeof state);
|
||||||
}
|
}
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
static void ble_advertise(void) {
|
static void ble_advertise(void) {
|
||||||
|
// check if already adverting
|
||||||
|
if(ble_gap_adv_active()) return;
|
||||||
|
|
||||||
struct ble_gap_adv_params adv_params;
|
struct ble_gap_adv_params adv_params;
|
||||||
struct ble_hs_adv_fields fields;
|
struct ble_hs_adv_fields fields;
|
||||||
int rc;
|
int rc;
|
||||||
|
@ -303,15 +332,13 @@ static int ble_gap_event(struct ble_gap_event *event, void *arg) {
|
||||||
conn_handle = event->connect.conn_handle;
|
conn_handle = event->connect.conn_handle;
|
||||||
|
|
||||||
ESP_LOGI(TAG, "connection %s; conn_handle: %d status=%d\n",
|
ESP_LOGI(TAG, "connection %s; conn_handle: %d status=%d\n",
|
||||||
event->connect.status == 0 ? "established" : "failed",
|
event->connect.status == 0 ? "established" : "failed",
|
||||||
conn_handle,
|
conn_handle,
|
||||||
event->connect.status
|
event->connect.status
|
||||||
);
|
);
|
||||||
|
|
||||||
if (event->connect.status != 0) {
|
// always keep advertising
|
||||||
/* Connection failed; resume advertising */
|
ble_advertise();
|
||||||
ble_advertise();
|
|
||||||
}
|
|
||||||
|
|
||||||
break;
|
break;
|
||||||
|
|
||||||
|
@ -331,8 +358,8 @@ static int ble_gap_event(struct ble_gap_event *event, void *arg) {
|
||||||
|
|
||||||
case BLE_GAP_EVENT_SUBSCRIBE:
|
case BLE_GAP_EVENT_SUBSCRIBE:
|
||||||
ESP_LOGI(TAG, "subscribe event; cur_notify=%d\n value handle; "
|
ESP_LOGI(TAG, "subscribe event; cur_notify=%d\n value handle; "
|
||||||
"val_handle=%d\n",
|
"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) {
|
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:
|
case BLE_GAP_EVENT_CONN_UPDATE:
|
||||||
ESP_LOGI(TAG, "BLE_GAP_EVENT_CONN_UPDATE, conn_handle: %d status: %d",
|
ESP_LOGI(TAG, "BLE_GAP_EVENT_CONN_UPDATE, conn_handle: %d status: %d",
|
||||||
event->mtu.conn_handle,
|
event->mtu.conn_handle,
|
||||||
event->enc_change.status
|
event->enc_change.status
|
||||||
);
|
);
|
||||||
|
|
||||||
break;
|
break;
|
||||||
|
|
||||||
|
@ -359,11 +386,17 @@ static int ble_gap_event(struct ble_gap_event *event, void *arg) {
|
||||||
event->mtu.value);
|
event->mtu.value);
|
||||||
break;
|
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:
|
default:
|
||||||
ESP_LOGW(TAG, "unhandled ble_gap_event; conn_handle=%d type=%d\n",
|
ESP_LOGW(TAG, "unhandled ble_gap_event; conn_handle=%d type=%d\n",
|
||||||
event->mtu.conn_handle,
|
event->mtu.conn_handle,
|
||||||
event->type
|
event->type
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
|
|
Loading…
Reference in a new issue