barback32/main/ble.c

493 lines
13 KiB
C
Raw Normal View History

2020-04-23 01:23:02 -04:00
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "freertos/event_groups.h"
#include "esp_system.h"
#include "esp_nimble_hci.h"
#include "nimble/nimble_port.h"
#include "nimble/nimble_port_freertos.h"
#include "host/ble_hs.h"
#include "host/util/util.h"
#include "services/gap/ble_svc_gap.h"
#include "services/bas/ble_svc_bas.h"
2020-10-13 00:35:25 -04:00
#include "driver/gpio.h"
2020-04-23 01:23:02 -04:00
2021-09-11 00:38:19 -04:00
#include "barback.h"
2020-04-23 01:23:02 -04:00
#include "main.h"
#include "ble.h"
2020-10-25 22:49:14 -04:00
#include "pumps.h"
2020-04-23 01:23:02 -04:00
#define TAG "BLE"
#define CONFIG_CACO_MAX_SERVICES (10)
#define CACO_BLE_ADV_PERIOD (4000)
static const char *device_name = CONFIG_BARBACK_ID;
2020-04-23 01:23:02 -04:00
static uint8_t blehr_addr_type;
static uint16_t conn_handle;
2020-10-26 22:12:25 -04:00
static uint16_t ble_svc_handle;
static uint16_t svc_handle_button;
2021-09-10 00:32:23 -04:00
// ^^^OLD^^^
// |||NEW|||
typedef struct {
uint16_t conn_handle;
} barback_ble_t;
static uint16_t pump_duration_svc_val_handles[4];
2020-04-23 01:23:02 -04:00
static event_callback_handle_t callback_handlers[CONFIG_CACO_MAX_SERVICES] = { 0 };
static int ble_gap_event(struct ble_gap_event *event, void *arg);
2021-09-11 00:41:08 -04:00
static int svc_access_system(uint16_t conn_handle, uint16_t attr_handle, struct ble_gatt_access_ctxt *ctxt, void *arg);
2021-09-11 00:38:19 -04:00
static int barback_ble_char_access(uint16_t conn_handle, uint16_t attr_handle, struct ble_gatt_access_ctxt *ctxt, void *arg);
2020-10-13 00:35:25 -04:00
#define BLE_SERVICE_PUMP_ENABLED (0x4200)
#define BLE_SERVICE_PUMP_STATE (0x4300)
2020-10-25 22:49:14 -04:00
#define BLE_SERVICE_PUMP_DURATION (0x4350)
#define BLE_SERVICE_BARBACK (0x4400)
#define CHAR_POUR (BLE_SERVICE_BARBACK + 1)
#define CHAR_BUTTON (BLE_SERVICE_BARBACK + 2)
2020-10-13 00:35:25 -04:00
2021-09-11 00:38:19 -04:00
static barback_ble_access_t barback_ble_pump_access_enabled = {
.read = pumps_get_enabled,
.write = pumps_set_enabled
};
2020-10-13 00:35:25 -04:00
#define BLE_CHAR_PUMP_ENABLED(idx) {\
2021-09-10 00:32:23 -04:00
.uuid = BLE_UUID16_DECLARE(BLE_SERVICE_PUMP_ENABLED + idx),\
2021-09-11 00:38:19 -04:00
.access_cb = barback_ble_char_access,\
.arg = &barback_ble_pump_access_enabled,\
2021-09-10 00:32:23 -04:00
.val_handle = &ble_svc_handle,\
.flags = BLE_GATT_CHR_F_NOTIFY | BLE_GATT_CHR_F_WRITE | BLE_GATT_CHR_F_READ\
}
2020-10-13 00:35:25 -04:00
2021-09-11 00:38:19 -04:00
static barback_ble_access_t barback_ble_pump_access_state = {
.read = pumps_get_state,
};
2020-10-13 00:35:25 -04:00
#define BLE_CHAR_PUMP_STATE(idx) {\
2021-09-10 00:32:23 -04:00
.uuid = BLE_UUID16_DECLARE(BLE_SERVICE_PUMP_STATE + idx),\
2021-09-11 00:38:19 -04:00
.access_cb = barback_ble_char_access,\
.arg = &barback_ble_pump_access_state,\
2021-09-10 00:32:23 -04:00
.val_handle = &ble_svc_handle,\
.flags = BLE_GATT_CHR_F_NOTIFY | BLE_GATT_CHR_F_READ\
}
2020-10-13 00:35:25 -04:00
2021-09-11 00:38:19 -04:00
static barback_ble_access_t barback_ble_pump_access_duration = {
.read = pumps_get_duration,
.write = pumps_set_duration
};
2020-10-25 22:49:14 -04:00
#define BLE_CHAR_PUMP_DURATION(idx) {\
2021-09-10 00:32:23 -04:00
.uuid = BLE_UUID16_DECLARE(BLE_SERVICE_PUMP_DURATION + idx),\
2021-09-11 00:38:19 -04:00
.access_cb = barback_ble_char_access,\
.arg = &barback_ble_pump_access_duration,\
2021-09-10 00:32:23 -04:00
.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\
}
2020-10-25 22:49:14 -04:00
2020-10-13 00:35:25 -04:00
static const struct ble_gatt_svc_def service_defs[] = {
2021-09-10 00:32:23 -04:00
{
.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
2020-04-23 01:23:02 -04:00
};
2020-10-13 00:35:25 -04:00
static int gatt_svr_chr_write(struct os_mbuf *om, uint16_t min_len, uint16_t max_len,
2021-09-10 00:32:23 -04:00
void *dst, uint16_t *len) {
uint16_t om_len;
int rc;
2020-04-23 01:23:02 -04:00
2021-09-10 00:32:23 -04:00
om_len = OS_MBUF_PKTLEN(om);
if (om_len < min_len || om_len > max_len) {
return BLE_ATT_ERR_INVALID_ATTR_VALUE_LEN;
}
2020-10-13 00:35:25 -04:00
2021-09-10 00:32:23 -04:00
rc = ble_hs_mbuf_to_flat(om, dst, max_len, len);
if (rc != 0) {
return BLE_ATT_ERR_UNLIKELY;
}
2020-10-13 00:35:25 -04:00
2021-09-10 00:32:23 -04:00
return 0;
2020-10-13 00:35:25 -04:00
}
2020-10-25 22:49:14 -04:00
static int svc_access_system(uint16_t conn_handle, uint16_t attr_handle, struct ble_gatt_access_ctxt *ctxt, void *arg) {
2020-04-23 01:23:02 -04:00
uint16_t uuid16 = ble_uuid_u16(ctxt->chr->uuid);
2020-10-25 22:49:14 -04:00
uint8_t value = 0;
ESP_LOGI(TAG, "0x%02X access: %d", uuid16, ctxt->op);
if(ctxt->op == BLE_GATT_ACCESS_OP_READ_CHR) {
} else if(ctxt->op == BLE_GATT_ACCESS_OP_WRITE_CHR) {
gatt_svr_chr_write(
ctxt->om,
sizeof value,
sizeof value,
&value, NULL
2021-09-10 00:32:23 -04:00
);
2020-10-25 22:49:14 -04:00
switch(uuid16) {
case CHAR_POUR:
if(value == 1) {
2021-09-22 21:54:49 -04:00
ESP_LOGI(TAG, "starting pumps");
2020-10-25 22:49:14 -04:00
pumps_run();
} else {
2021-09-22 21:54:49 -04:00
ESP_LOGI(TAG, "stoping pumps");
2020-10-25 22:49:14 -04:00
pumps_stop();
}
2021-09-10 00:32:23 -04:00
break;
2020-10-25 22:49:14 -04:00
case CHAR_BUTTON:
2021-09-10 00:32:23 -04:00
break;
2020-10-25 22:49:14 -04:00
}
}
2020-04-23 01:23:02 -04:00
2020-10-25 22:49:14 -04:00
return 0;
}
2020-10-13 00:35:25 -04:00
2021-09-11 00:38:19 -04:00
static int barback_ble_char_access(uint16_t conn_handle, uint16_t attr_handle, struct ble_gatt_access_ctxt *ctxt, void *arg) {
2021-09-24 02:00:03 -04:00
uint16_t chr_uuid = ble_uuid_u16(ctxt->chr->uuid);
// match the UUID against 0b111
uint8_t idx = (chr_uuid & 0x07) - 1;
2020-10-13 00:35:25 -04:00
uint8_t value = 0;
2021-09-11 00:38:19 -04:00
barback_ble_access_t *access = (barback_ble_access_t *)arg;
2020-10-13 00:35:25 -04:00
2021-09-24 02:00:03 -04:00
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"));
2020-10-13 00:35:25 -04:00
2021-09-10 00:32:23 -04:00
if(ctxt->op == BLE_GATT_ACCESS_OP_WRITE_CHR) {
2020-10-25 22:49:14 -04:00
gatt_svr_chr_write(
ctxt->om,
sizeof value,
sizeof value,
&value, NULL
2021-09-10 00:32:23 -04:00
);
struct ble_gap_conn_desc notifiee;
2021-09-24 02:00:03 -04:00
// TODO
// this could needs to be verified, it might actually be needed
// NiBLE could be passing notifications itself on chr_handler changes
2021-09-10 00:32:23 -04:00
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);
}
}
2020-10-13 00:35:25 -04:00
2021-09-24 02:00:03 -04:00
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);
2021-09-11 00:38:19 -04:00
access->write(idx, value);
2021-09-24 02:00:03 -04:00
}
} else if(access != NULL && ctxt->op == BLE_GATT_ACCESS_OP_READ_CHR) {
if(access->read == NULL) {
2021-09-11 00:38:19 -04:00
ESP_LOGW(TAG, "Attempted read without defining access.read");
return 1;
2021-09-10 00:32:23 -04:00
}
2020-04-23 01:23:02 -04:00
2021-09-24 02:00:03 -04:00
uint8_t value = access->read(idx);
2020-10-25 22:49:14 -04:00
2021-09-24 02:00:03 -04:00
ESP_LOGI(TAG, "returning read idx: %d value: %d", idx, value);
os_mbuf_append(ctxt->om, &value, sizeof value);
2020-10-26 22:12:25 -04:00
}
2020-04-23 01:23:02 -04:00
2021-09-10 00:32:23 -04:00
return 0;
2020-04-23 01:23:02 -04:00
}
static void ble_advertise(void) {
2021-09-10 00:32:23 -04:00
// check if already adverting
if(ble_gap_adv_active()) return;
2020-04-23 01:23:02 -04:00
struct ble_gap_adv_params adv_params;
struct ble_hs_adv_fields fields;
int rc;
/*
* Set the advertisement data included in our advertisements:
* o Flags (indicates advertisement type and other general info)
* o Advertising tx power
* o Device name
*/
memset(&fields, 0, sizeof(fields));
/*
* Advertise two flags:
* o Discoverability in forthcoming advertisement (general)
* o BLE-only (BR/EDR unsupported)
*/
fields.flags = BLE_HS_ADV_F_DISC_GEN |
BLE_HS_ADV_F_BREDR_UNSUP;
/*
* Indicate that the TX power level field should be included; have the
* stack fill this value automatically. This is done by assigning the
* special value BLE_HS_ADV_TX_PWR_LVL_AUTO.
*/
fields.tx_pwr_lvl_is_present = 1;
fields.tx_pwr_lvl = BLE_HS_ADV_TX_PWR_LVL_AUTO;
fields.name = (uint8_t *)device_name;
fields.name_len = strlen(device_name);
fields.name_is_complete = 1;
rc = ble_gap_adv_set_fields(&fields);
if (rc != 0) {
2020-10-13 00:35:25 -04:00
ESP_LOGE(TAG, "error setting advertisement data; rc=%d\n", rc);
2020-04-23 01:23:02 -04:00
return;
}
/* Begin advertising */
memset(&adv_params, 0, sizeof(adv_params));
adv_params.conn_mode = BLE_GAP_CONN_MODE_UND;
adv_params.disc_mode = BLE_GAP_DISC_MODE_GEN;
rc = ble_gap_adv_start(blehr_addr_type, NULL, BLE_HS_FOREVER,
&adv_params, ble_gap_event, NULL);
if (rc != 0) {
2020-10-13 00:35:25 -04:00
ESP_LOGE(TAG, "error enabling advertisement; rc=%d\n", rc);
2020-04-23 01:23:02 -04:00
return;
}
}
static int ble_gap_event(struct ble_gap_event *event, void *arg) {
switch (event->type) {
case BLE_GAP_EVENT_DISC: {
struct ble_hs_adv_fields fields;
ESP_ERROR_CHECK(ble_hs_adv_parse_fields(&fields, event->disc.data, event->disc.length_data));
if(fields.name != NULL) {
char *name = (char*)malloc(fields.name_len);
memcpy(name, (uint8_t*)fields.name, fields.name_len);
name[fields.name_len] = '\0';
ESP_LOGI(TAG, "DISCOVERY: %s", name);
if(strstr(name, "BARBACK") != NULL) {
ESP_LOGI(TAG, "Let's Party!");
uint8_t addr_type;
ble_gap_disc_cancel();
ESP_ERROR_CHECK(ble_hs_id_infer_auto(0, &addr_type));
ESP_ERROR_CHECK(ble_gap_connect(addr_type, &event->disc.addr, 30000, NULL, ble_gap_event, NULL));
}
}
return 0;
}
2020-04-23 01:23:02 -04:00
case BLE_GAP_EVENT_CONNECT:
/* A new connection was established or a connection attempt failed */
2020-10-13 00:35:25 -04:00
conn_handle = event->connect.conn_handle;
ESP_LOGI(TAG, "connection %s; conn_handle: %d status=%d\n",
2021-09-10 00:32:23 -04:00
event->connect.status == 0 ? "established" : "failed",
conn_handle,
event->connect.status
);
// always keep advertising
ble_advertise();
2020-10-13 00:35:25 -04:00
2020-04-23 01:23:02 -04:00
break;
case BLE_GAP_EVENT_DISCONNECT:
ESP_LOGI(TAG, "disconnect; reason=%d\n", event->disconnect.reason);
/* Connection terminated; resume advertising */
ble_advertise();
2020-10-13 00:35:25 -04:00
2020-04-23 01:23:02 -04:00
break;
case BLE_GAP_EVENT_ADV_COMPLETE:
ESP_LOGI(TAG, "adv complete\n");
ble_advertise();
break;
case BLE_GAP_EVENT_SUBSCRIBE:
2020-10-26 22:12:25 -04:00
ESP_LOGI(TAG, "subscribe event; cur_notify=%d\n value handle; "
2021-09-10 00:32:23 -04:00
"val_handle=%d\n",
event->subscribe.cur_notify, event->subscribe.attr_handle);
2020-04-23 01:23:02 -04:00
2020-10-26 22:12:25 -04:00
if(event->subscribe.attr_handle == svc_handle_button) {
2020-04-23 01:23:02 -04:00
}
2020-10-26 22:12:25 -04:00
2020-04-23 01:23:02 -04:00
break;
case BLE_GAP_EVENT_CONN_UPDATE:
2020-10-13 00:35:25 -04:00
ESP_LOGI(TAG, "BLE_GAP_EVENT_CONN_UPDATE, conn_handle: %d status: %d",
2021-09-10 00:32:23 -04:00
event->mtu.conn_handle,
event->enc_change.status
);
2020-10-13 00:35:25 -04:00
2020-04-23 01:23:02 -04:00
break;
case BLE_GAP_EVENT_ENC_CHANGE:
ESP_LOGI(TAG, "BLE_GAP_EVENT_ENC_CHANGE");
2020-10-13 00:35:25 -04:00
2020-04-23 01:23:02 -04:00
break;
case BLE_GAP_EVENT_MTU:
ESP_LOGI(TAG, "mtu update event; conn_handle=%d mtu=%d\n",
event->mtu.conn_handle,
event->mtu.value);
break;
2020-10-13 00:35:25 -04:00
2021-09-22 21:55:26 -04:00
case BLE_GAP_EVENT_NOTIFY_RX:
ESP_LOGI(TAG, "BLE_GAP_EVENT_NOTIFY_RX; conn_handle=%d mtu=%d attr_handle=%d\n",
event->mtu.conn_handle,
event->mtu.value,
event->notify_rx.attr_handle);
break;
2021-09-10 00:32:23 -04:00
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;
2020-10-13 00:35:25 -04:00
default:
2020-10-26 22:12:25 -04:00
ESP_LOGW(TAG, "unhandled ble_gap_event; conn_handle=%d type=%d\n",
2021-09-10 00:32:23 -04:00
event->mtu.conn_handle,
event->type
);
2020-04-23 01:23:02 -04:00
}
return 0;
}
void ble_init_buddy_client() {
uint8_t addr_type;
struct ble_gap_disc_params disc_params = {
.filter_duplicates = 1,
.passive = 1,
.itvl = 0,
.window = 0,
.filter_policy = 0,
.limited = 0,
};
ESP_ERROR_CHECK(ble_hs_id_infer_auto(0, &addr_type));
ESP_ERROR_CHECK(ble_gap_disc(addr_type, BLE_HS_FOREVER, &disc_params, ble_gap_event, &addr_type));
}
2020-04-23 01:23:02 -04:00
static void on_sync() {
ESP_LOGI(TAG, "on_sync");
int err;
err = ble_hs_id_infer_auto(0, &blehr_addr_type);
ESP_ERROR_CHECK(err);
uint8_t addr_val[6] = {0};
err = ble_hs_id_copy_addr(blehr_addr_type, addr_val, NULL);
ESP_ERROR_CHECK(err);
ble_advertise();
ble_init_buddy_client();
2020-04-23 01:23:02 -04:00
}
static void on_reset(int reason) {
ESP_LOGE(TAG, "on_reset, reason: %d", reason);
2020-04-23 01:23:02 -04:00
}
void nimble_host_task(void *param) {
nimble_port_run();
}
2020-10-26 22:12:25 -04:00
int8_t ble_send_notification(void *buf, uint8_t size) {
struct os_mbuf *om;
om = ble_hs_mbuf_from_flat(buf, size);
return ble_gattc_notify_custom(conn_handle, svc_handle_button, om);
}
2021-09-08 23:41:24 -04:00
void ble_init(char *name) {
2020-04-23 01:23:02 -04:00
esp_err_t err;
memset(&callback_handlers, 0, sizeof(event_callback_handle_t) * CONFIG_CACO_MAX_SERVICES);
ESP_ERROR_CHECK(esp_nimble_hci_and_controller_init());
nimble_port_init();
ble_hs_cfg.sync_cb = on_sync;
ble_hs_cfg.reset_cb = on_reset;
// initialize services
err = ble_gatts_count_cfg(service_defs);
ESP_ERROR_CHECK(err);
err = ble_gatts_add_svcs(service_defs);
ESP_ERROR_CHECK(err);
2021-09-08 23:41:24 -04:00
ESP_LOGI(TAG, "Setting device name: %s", name);
2020-10-28 20:08:55 -04:00
2021-09-08 23:41:24 -04:00
device_name = name;
err = ble_svc_gap_device_name_set(name);
2020-04-23 01:23:02 -04:00
ESP_ERROR_CHECK(err);
2020-10-28 20:59:49 -04:00
vTaskDelay(500 / portTICK_PERIOD_MS);
2020-04-23 01:23:02 -04:00
nimble_port_freertos_init(nimble_host_task);
}