barbackification

This commit is contained in:
Morgan 'ARR\!' Allen 2020-10-12 21:35:25 -07:00
parent 3347521afa
commit 5e777c5df2

View file

@ -13,16 +13,16 @@
#include "host/util/util.h" #include "host/util/util.h"
#include "services/gap/ble_svc_gap.h" #include "services/gap/ble_svc_gap.h"
#include "services/bas/ble_svc_bas.h" #include "services/bas/ble_svc_bas.h"
#include "driver/gpio.h"
#include "main.h" #include "main.h"
#include "ble.h" #include "ble.h"
#include "esp32-lora.h"
#define TAG "BLE" #define TAG "BLE"
#define CONFIG_CACO_MAX_SERVICES (10) #define CONFIG_CACO_MAX_SERVICES (10)
#define CACO_BLE_ADV_PERIOD (4000) #define CACO_BLE_ADV_PERIOD (4000)
static const char *device_name = "LoRaComm"; static const char *device_name = "Barback";
static uint8_t blehr_addr_type; static uint8_t blehr_addr_type;
static uint8_t callback_handler_count = 0; static uint8_t callback_handler_count = 0;
@ -32,62 +32,112 @@ 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);
static uint16_t ble_svc_handle; static uint16_t ble_svc_handle;
static int ble_svc_access(uint16_t conn_handle, uint16_t attr_handle, struct ble_gatt_access_ctxt *ctxt, 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_enabled(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);
#define BLE_SERVICE_LORA (0x4200) #define BLE_SERVICE_PUMP_ENABLED (0x4200)
#define BLE_SERVICE_LORA_BW (0x4201) #define BLE_SERVICE_PUMP_STATE (0x4300)
#define BLE_SERVICE_LORA_CR (0x4202)
#define BLE_SERVICE_LORA_SF (0x4203)
#define BLE_SERVICE_LORA_PRE (0x4204)
#define BLE_SERVICE_LORA_TX (0x4205)
#define BLE_SERVICE_LORA_RX (0x4206)
#define BLE_SERVICE_LORA_FREQ (0x4207)
static const struct ble_gatt_svc_def service_defs[] = {{ #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\
}
#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\
}
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_LORA), .uuid = BLE_UUID16_DECLARE(BLE_SERVICE_PUMP_ENABLED),
.characteristics = (struct ble_gatt_chr_def[]) { {
.uuid = BLE_UUID16_DECLARE(BLE_SERVICE_LORA_BW),
.access_cb = ble_svc_access,
.val_handle = &ble_svc_handle,
.flags = BLE_GATT_CHR_F_WRITE | BLE_GATT_CHR_F_READ
}, {
.uuid = BLE_UUID16_DECLARE(BLE_SERVICE_LORA_CR),
.access_cb = ble_svc_access,
.val_handle = &ble_svc_handle,
.flags = BLE_GATT_CHR_F_WRITE | BLE_GATT_CHR_F_READ
}, {
.uuid = BLE_UUID16_DECLARE(BLE_SERVICE_LORA_SF),
.access_cb = ble_svc_access,
.val_handle = &ble_svc_handle,
.flags = BLE_GATT_CHR_F_WRITE | BLE_GATT_CHR_F_READ
}, {
.uuid = BLE_UUID16_DECLARE(BLE_SERVICE_LORA_PRE),
.access_cb = ble_svc_access,
.val_handle = &ble_svc_handle,
.flags = BLE_GATT_CHR_F_WRITE | BLE_GATT_CHR_F_READ
}, {
.uuid = BLE_UUID16_DECLARE(BLE_SERVICE_LORA_TX),
.access_cb = ble_svc_access,
.val_handle = &ble_svc_handle,
.flags = BLE_GATT_CHR_F_WRITE
}, {
.uuid = BLE_UUID16_DECLARE(BLE_SERVICE_LORA_RX),
.access_cb = ble_svc_access,
.val_handle = &ble_svc_handle,
.flags = BLE_GATT_CHR_F_NOTIFY | BLE_GATT_CHR_F_READ
}, {
.uuid = BLE_UUID16_DECLARE(BLE_SERVICE_LORA_FREQ),
.access_cb = ble_svc_access,
.val_handle = &ble_svc_handle,
.flags = BLE_GATT_CHR_F_WRITE | BLE_GATT_CHR_F_READ
}, {
0, /* No more characteristics in this service. */
} },
},
{ 0 } .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, // No more characteristics in this service.
}
},
}, {
.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, // No more characteristics in this service.
}
},
},
{ 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;
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;
}
return 0;
}
static int svc_access_pump_enabled(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);
ESP_LOGI(TAG, "0x%02X (%d) access: %d", uuid16, uuid16, ctxt->op);
uint8_t value = 0;
gatt_svr_chr_write(
ctxt->om,
sizeof value,
sizeof value,
&value, NULL
);
uint8_t io = pump_gpio_map[uuid16 - 0x4201];
ESP_LOGI(TAG, "gpio: %d value: %d", io, value);
ESP_LOGI(TAG, "idx: %d", uuid16 - 4201);
gpio_set_level(io, value);
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);
if(ctxt->op != BLE_GATT_ACCESS_OP_READ_CHR) {
ESP_LOGW(TAG, "attempting non-read op on write only characteristics");
return 1;
}
ESP_LOGI(TAG, "0x%02X access: %d", uuid16, ctxt->op);
return 0;
}
static int ble_svc_access(uint16_t conn_handle, uint16_t attr_handle, struct ble_gatt_access_ctxt *ctxt, void *arg) { static int ble_svc_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); uint16_t uuid16 = ble_uuid_u16(ctxt->chr->uuid);
@ -97,22 +147,6 @@ static int ble_svc_access(uint16_t conn_handle, uint16_t attr_handle, struct ble
ESP_LOGI(TAG, "0x%02X access: %d", uuid16, ctxt->op); ESP_LOGI(TAG, "0x%02X access: %d", uuid16, ctxt->op);
switch(uuid16) { switch(uuid16) {
case BLE_SERVICE_LORA_BW:
os_mbuf_append(ctxt->om, (void*)&lora.bandwidth, 1);
break;
case BLE_SERVICE_LORA_CR:
os_mbuf_append(ctxt->om, (void*)&lora.codingRate, 1);
break;
case BLE_SERVICE_LORA_SF:
os_mbuf_append(ctxt->om, (void*)&lora.spreadingFactor, 1);
break;
case BLE_SERVICE_LORA_FREQ:
os_mbuf_append(ctxt->om, (void*)&lora.frequency, 1);
break;
case 0x2901: case 0x2901:
os_mbuf_append(ctxt->om, "hello", 5); os_mbuf_append(ctxt->om, "hello", 5);
break; break;
@ -156,7 +190,7 @@ static void ble_advertise(void) {
rc = ble_gap_adv_set_fields(&fields); rc = ble_gap_adv_set_fields(&fields);
if (rc != 0) { if (rc != 0) {
MODLOG_DFLT(ERROR, "error setting advertisement data; rc=%d\n", rc); ESP_LOGE(TAG, "error setting advertisement data; rc=%d\n", rc);
return; return;
} }
@ -167,7 +201,7 @@ static void ble_advertise(void) {
rc = ble_gap_adv_start(blehr_addr_type, NULL, BLE_HS_FOREVER, rc = ble_gap_adv_start(blehr_addr_type, NULL, BLE_HS_FOREVER,
&adv_params, ble_gap_event, NULL); &adv_params, ble_gap_event, NULL);
if (rc != 0) { if (rc != 0) {
MODLOG_DFLT(ERROR, "error enabling advertisement; rc=%d\n", rc); ESP_LOGE(TAG, "error enabling advertisement; rc=%d\n", rc);
return; return;
} }
} }
@ -178,15 +212,19 @@ static int ble_gap_event(struct ble_gap_event *event, void *arg) {
switch (event->type) { switch (event->type) {
case BLE_GAP_EVENT_CONNECT: case BLE_GAP_EVENT_CONNECT:
/* A new connection was established or a connection attempt failed */ /* A new connection was established or a connection attempt failed */
MODLOG_DFLT(INFO, "connection %s; status=%d\n", conn_handle = event->connect.conn_handle;
ESP_LOGI(TAG, "connection %s; conn_handle: %d status=%d\n",
event->connect.status == 0 ? "established" : "failed", event->connect.status == 0 ? "established" : "failed",
event->connect.status); conn_handle,
event->connect.status
);
if (event->connect.status != 0) { if (event->connect.status != 0) {
/* Connection failed; resume advertising */ /* Connection failed; resume advertising */
ble_advertise(); ble_advertise();
} }
conn_handle = event->connect.conn_handle;
break; break;
case BLE_GAP_EVENT_DISCONNECT: case BLE_GAP_EVENT_DISCONNECT:
@ -194,6 +232,7 @@ static int ble_gap_event(struct ble_gap_event *event, void *arg) {
/* Connection terminated; resume advertising */ /* Connection terminated; resume advertising */
ble_advertise(); ble_advertise();
break; break;
case BLE_GAP_EVENT_ADV_COMPLETE: case BLE_GAP_EVENT_ADV_COMPLETE:
@ -219,12 +258,17 @@ static int ble_gap_event(struct ble_gap_event *event, void *arg) {
break; break;
case BLE_GAP_EVENT_CONN_UPDATE: case BLE_GAP_EVENT_CONN_UPDATE:
ESP_LOGI(TAG, "BLE_GAP_EVENT_CONN_UPDATE, status: %d", event->enc_change.status); ESP_LOGI(TAG, "BLE_GAP_EVENT_CONN_UPDATE, conn_handle: %d status: %d",
event->mtu.conn_handle,
event->enc_change.status
);
break; break;
case BLE_GAP_EVENT_ENC_CHANGE: case BLE_GAP_EVENT_ENC_CHANGE:
ESP_LOGI(TAG, "BLE_GAP_EVENT_ENC_CHANGE"); ESP_LOGI(TAG, "BLE_GAP_EVENT_ENC_CHANGE");
break; break;
case BLE_GAP_EVENT_MTU: case BLE_GAP_EVENT_MTU:
@ -232,6 +276,12 @@ static int ble_gap_event(struct ble_gap_event *event, void *arg) {
event->mtu.conn_handle, event->mtu.conn_handle,
event->mtu.value); event->mtu.value);
break; break;
default:
ESP_LOGW(TAG, "unhandled ble_gap_event; conn_handle=%d mtu=%d\n",
event->mtu.conn_handle,
event->mtu.value
);
} }
return 0; return 0;