Add BLE host transport selectable via DIP8 alongside UART.
Master can speak the same framed protobuf protocol over NimBLE or UART; goTool gains -ble, ble-rtt, and ble-stream timing checks for ESP-NOW coexistence. Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
@@ -12,6 +12,9 @@ idf_component_register(
|
||||
"powerpod.c"
|
||||
"led_ring.c"
|
||||
"uart.c"
|
||||
"host_transport.c"
|
||||
"ble_host.c"
|
||||
"ble_stream.c"
|
||||
"uart_proto.c"
|
||||
"uart_cmd.c"
|
||||
"cmd/cmd_handler.c"
|
||||
@@ -59,6 +62,7 @@ idf_component_register(
|
||||
esp_netif
|
||||
esp_event
|
||||
nvs_flash
|
||||
bt
|
||||
PRIV_REQUIRES
|
||||
esp_driver_gpio
|
||||
esp_driver_uart
|
||||
|
||||
@@ -9,6 +9,8 @@
|
||||
typedef struct {
|
||||
bool master;
|
||||
uint8_t network;
|
||||
/** false = UART host, true = BLE host (DIP8; mutual exclusive, boot-only). */
|
||||
bool host_ble;
|
||||
char running_partition[APP_RUNNING_PARTITION_LABEL_MAX];
|
||||
} app_config_t;
|
||||
|
||||
|
||||
+417
@@ -0,0 +1,417 @@
|
||||
#include "ble_host.h"
|
||||
#include "esp_log.h"
|
||||
#include "esp_timer.h"
|
||||
#include "host_transport.h"
|
||||
#include "nvs_flash.h"
|
||||
#include "freertos/FreeRTOS.h"
|
||||
#include "freertos/task.h"
|
||||
|
||||
#include "nimble/nimble_port.h"
|
||||
#include "nimble/nimble_port_freertos.h"
|
||||
#include "host/ble_att.h"
|
||||
#include "host/ble_hs.h"
|
||||
#include "host/util/util.h"
|
||||
#include "services/gap/ble_svc_gap.h"
|
||||
#include "services/gatt/ble_svc_gatt.h"
|
||||
|
||||
#include <string.h>
|
||||
|
||||
static const char *TAG = "[BLE_HOST]";
|
||||
static const char *DEVICE_NAME = "Powerpod";
|
||||
|
||||
#define BLE_CONN_ITVL_MIN_UNITS 6 /* 7.5 ms */
|
||||
#define BLE_CONN_ITVL_MAX_UNITS 6
|
||||
#define BLE_CONN_TUNE_MAX_TRIES 6
|
||||
|
||||
/*
|
||||
* Nordic UART style:
|
||||
* Service: 6e400001-b5a3-f393-e0a9-e50e24dcca9e
|
||||
* Characteristic: 6e400002-b5a3-f393-e0a9-e50e24dcca9e
|
||||
* Write = host→ESP (framed), Notify = ESP→host (framed).
|
||||
*/
|
||||
static const ble_uuid128_t ble_svc_uuid =
|
||||
BLE_UUID128_INIT(0x9e, 0xca, 0xdc, 0x24, 0x0e, 0xe5, 0xa9, 0xe0, 0x93, 0xf3,
|
||||
0xa3, 0xb5, 0x01, 0x00, 0x40, 0x6e);
|
||||
static const ble_uuid128_t ble_chr_uuid =
|
||||
BLE_UUID128_INIT(0x9e, 0xca, 0xdc, 0x24, 0x0e, 0xe5, 0xa9, 0xe0, 0x93, 0xf3,
|
||||
0xa3, 0xb5, 0x02, 0x00, 0x40, 0x6e);
|
||||
|
||||
static uint8_t own_addr_type;
|
||||
static uint16_t conn_handle = BLE_HS_CONN_HANDLE_NONE;
|
||||
static uint16_t ble_chr_val_handle;
|
||||
static bool notify_enabled;
|
||||
static uint16_t att_mtu = 23;
|
||||
static esp_timer_handle_t conn_tune_timer;
|
||||
static uint8_t conn_tune_tries;
|
||||
|
||||
static int ble_gap_event(struct ble_gap_event *event, void *arg);
|
||||
static void ble_request_fast_conn_params(uint16_t handle);
|
||||
static void ble_optimize_link(uint16_t handle);
|
||||
|
||||
bool ble_host_ready(void) {
|
||||
return notify_enabled && conn_handle != BLE_HS_CONN_HANDLE_NONE;
|
||||
}
|
||||
|
||||
uint16_t ble_host_att_mtu(void) { return att_mtu; }
|
||||
|
||||
uint16_t ble_host_max_payload(void) {
|
||||
if (att_mtu < 4) {
|
||||
return 20;
|
||||
}
|
||||
return (uint16_t)(att_mtu - 3);
|
||||
}
|
||||
|
||||
static esp_err_t ble_host_tx(const uint8_t *frame, size_t frame_len) {
|
||||
struct os_mbuf *om;
|
||||
int rc;
|
||||
|
||||
if (!ble_host_ready()) {
|
||||
return ESP_ERR_INVALID_STATE;
|
||||
}
|
||||
if (frame_len == 0 || frame_len > ble_host_max_payload()) {
|
||||
return ESP_ERR_INVALID_SIZE;
|
||||
}
|
||||
|
||||
om = ble_hs_mbuf_from_flat(frame, (uint16_t)frame_len);
|
||||
if (om == NULL) {
|
||||
return ESP_ERR_NO_MEM;
|
||||
}
|
||||
|
||||
rc = ble_gatts_notify_custom(conn_handle, ble_chr_val_handle, om);
|
||||
if (rc != 0) {
|
||||
os_mbuf_free_chain(om);
|
||||
return ESP_FAIL;
|
||||
}
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
static void ble_conn_tune_timer_cb(void *arg) {
|
||||
struct ble_gap_conn_desc desc;
|
||||
|
||||
(void)arg;
|
||||
if (conn_handle == BLE_HS_CONN_HANDLE_NONE) {
|
||||
return;
|
||||
}
|
||||
if (ble_gap_conn_find(conn_handle, &desc) != 0) {
|
||||
return;
|
||||
}
|
||||
if (desc.conn_itvl <= BLE_CONN_ITVL_MAX_UNITS) {
|
||||
ESP_LOGI(TAG, "conn already at interval_us=%lu",
|
||||
(unsigned long)desc.conn_itvl * 1250UL);
|
||||
return;
|
||||
}
|
||||
if (conn_tune_tries >= BLE_CONN_TUNE_MAX_TRIES) {
|
||||
ESP_LOGW(TAG, "central kept interval_us=%lu (ESP requested 7500)",
|
||||
(unsigned long)desc.conn_itvl * 1250UL);
|
||||
return;
|
||||
}
|
||||
|
||||
conn_tune_tries++;
|
||||
ESP_LOGI(TAG, "retry fast conn params (%u/%u); current interval_us=%lu",
|
||||
conn_tune_tries, BLE_CONN_TUNE_MAX_TRIES,
|
||||
(unsigned long)desc.conn_itvl * 1250UL);
|
||||
ble_request_fast_conn_params(conn_handle);
|
||||
esp_timer_start_once(conn_tune_timer, 750 * 1000);
|
||||
}
|
||||
|
||||
static void ble_schedule_conn_tune(void) {
|
||||
conn_tune_tries = 0;
|
||||
esp_timer_stop(conn_tune_timer);
|
||||
esp_timer_start_once(conn_tune_timer, 300 * 1000);
|
||||
}
|
||||
|
||||
static void ble_request_fast_conn_params(uint16_t handle) {
|
||||
struct ble_gap_upd_params params = {
|
||||
.itvl_min = BLE_CONN_ITVL_MIN_UNITS,
|
||||
.itvl_max = BLE_CONN_ITVL_MAX_UNITS,
|
||||
.latency = 0,
|
||||
.supervision_timeout = 400,
|
||||
.min_ce_len = 0,
|
||||
.max_ce_len = 24,
|
||||
};
|
||||
int rc = ble_gap_update_params(handle, ¶ms);
|
||||
if (rc != 0) {
|
||||
ESP_LOGW(TAG, "conn param update request failed: %d", rc);
|
||||
} else {
|
||||
ESP_LOGI(TAG, "requested conn interval 7.5 ms");
|
||||
}
|
||||
}
|
||||
|
||||
static void ble_optimize_link(uint16_t handle) {
|
||||
int rc;
|
||||
|
||||
ble_request_fast_conn_params(handle);
|
||||
|
||||
rc = ble_gap_set_prefered_le_phy(handle, BLE_GAP_LE_PHY_2M_MASK,
|
||||
BLE_GAP_LE_PHY_2M_MASK,
|
||||
BLE_GAP_LE_PHY_CODED_ANY);
|
||||
if (rc != 0) {
|
||||
ESP_LOGW(TAG, "2M PHY request failed: %d", rc);
|
||||
}
|
||||
|
||||
rc = ble_gap_set_data_len(handle, 251, 2120);
|
||||
if (rc != 0) {
|
||||
ESP_LOGW(TAG, "data len request failed: %d", rc);
|
||||
}
|
||||
|
||||
ble_schedule_conn_tune();
|
||||
}
|
||||
|
||||
static void ble_advertise(void) {
|
||||
struct ble_gap_adv_params adv_params;
|
||||
struct ble_hs_adv_fields fields;
|
||||
int rc;
|
||||
|
||||
memset(&fields, 0, sizeof(fields));
|
||||
fields.flags = BLE_HS_ADV_F_DISC_GEN | BLE_HS_ADV_F_BREDR_UNSUP;
|
||||
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) {
|
||||
ESP_LOGE(TAG, "adv set fields failed: %d", rc);
|
||||
return;
|
||||
}
|
||||
|
||||
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;
|
||||
adv_params.itvl_min = 0x20;
|
||||
adv_params.itvl_max = 0x30;
|
||||
|
||||
rc = ble_gap_adv_start(own_addr_type, NULL, BLE_HS_FOREVER, &adv_params,
|
||||
ble_gap_event, NULL);
|
||||
if (rc != 0) {
|
||||
ESP_LOGE(TAG, "adv start failed: %d", rc);
|
||||
return;
|
||||
}
|
||||
|
||||
ESP_LOGI(TAG, "advertising as \"%s\"", DEVICE_NAME);
|
||||
}
|
||||
|
||||
static int ble_chr_access(uint16_t conn_handle_arg, uint16_t attr_handle,
|
||||
struct ble_gatt_access_ctxt *ctxt, void *arg) {
|
||||
(void)conn_handle_arg;
|
||||
(void)attr_handle;
|
||||
(void)arg;
|
||||
|
||||
switch (ctxt->op) {
|
||||
case BLE_GATT_ACCESS_OP_READ_CHR:
|
||||
return 0;
|
||||
|
||||
case BLE_GATT_ACCESS_OP_WRITE_CHR: {
|
||||
uint16_t om_len = OS_MBUF_PKTLEN(ctxt->om);
|
||||
uint8_t buf[512];
|
||||
uint16_t copied = 0;
|
||||
int rc;
|
||||
|
||||
if (om_len == 0 || om_len > sizeof(buf)) {
|
||||
return BLE_ATT_ERR_INVALID_ATTR_VALUE_LEN;
|
||||
}
|
||||
|
||||
rc = ble_hs_mbuf_to_flat(ctxt->om, buf, sizeof(buf), &copied);
|
||||
if (rc != 0) {
|
||||
return BLE_ATT_ERR_UNLIKELY;
|
||||
}
|
||||
|
||||
host_rx_bytes(buf, copied);
|
||||
return 0;
|
||||
}
|
||||
|
||||
default:
|
||||
return BLE_ATT_ERR_UNLIKELY;
|
||||
}
|
||||
}
|
||||
|
||||
static const struct ble_gatt_svc_def gatt_svcs[] = {
|
||||
{
|
||||
.type = BLE_GATT_SVC_TYPE_PRIMARY,
|
||||
.uuid = &ble_svc_uuid.u,
|
||||
.characteristics =
|
||||
(struct ble_gatt_chr_def[]){
|
||||
{
|
||||
.uuid = &ble_chr_uuid.u,
|
||||
.access_cb = ble_chr_access,
|
||||
.val_handle = &ble_chr_val_handle,
|
||||
.flags = BLE_GATT_CHR_F_READ | BLE_GATT_CHR_F_WRITE |
|
||||
BLE_GATT_CHR_F_WRITE_NO_RSP | BLE_GATT_CHR_F_NOTIFY,
|
||||
},
|
||||
{0},
|
||||
},
|
||||
},
|
||||
{0},
|
||||
};
|
||||
|
||||
static int gatt_svr_init(void) {
|
||||
int rc;
|
||||
|
||||
ble_svc_gap_init();
|
||||
ble_svc_gatt_init();
|
||||
|
||||
rc = ble_gatts_count_cfg(gatt_svcs);
|
||||
if (rc != 0) {
|
||||
return rc;
|
||||
}
|
||||
|
||||
return ble_gatts_add_svcs(gatt_svcs);
|
||||
}
|
||||
|
||||
static int ble_gap_event(struct ble_gap_event *event, void *arg) {
|
||||
(void)arg;
|
||||
|
||||
switch (event->type) {
|
||||
case BLE_GAP_EVENT_CONNECT:
|
||||
ESP_LOGI(TAG, "connection %s; status=%d handle=%d",
|
||||
event->connect.status == 0 ? "established" : "failed",
|
||||
event->connect.status, event->connect.conn_handle);
|
||||
if (event->connect.status != 0) {
|
||||
ble_advertise();
|
||||
} else {
|
||||
conn_handle = event->connect.conn_handle;
|
||||
ble_optimize_link(conn_handle);
|
||||
}
|
||||
return 0;
|
||||
|
||||
case BLE_GAP_EVENT_DISCONNECT:
|
||||
ESP_LOGI(TAG, "disconnect; reason=%d", event->disconnect.reason);
|
||||
esp_timer_stop(conn_tune_timer);
|
||||
conn_tune_tries = 0;
|
||||
conn_handle = BLE_HS_CONN_HANDLE_NONE;
|
||||
notify_enabled = false;
|
||||
att_mtu = 23;
|
||||
ble_advertise();
|
||||
return 0;
|
||||
|
||||
case BLE_GAP_EVENT_CONN_UPDATE: {
|
||||
struct ble_gap_conn_desc desc;
|
||||
if (ble_gap_conn_find(event->conn_update.conn_handle, &desc) == 0) {
|
||||
ESP_LOGI(TAG, "conn updated; status=%d interval_us=%lu latency=%u",
|
||||
event->conn_update.status,
|
||||
(unsigned long)desc.conn_itvl * 1250UL, desc.conn_latency);
|
||||
if (event->conn_update.status == 0 &&
|
||||
desc.conn_itvl > BLE_CONN_ITVL_MAX_UNITS &&
|
||||
conn_tune_tries < BLE_CONN_TUNE_MAX_TRIES) {
|
||||
esp_timer_stop(conn_tune_timer);
|
||||
esp_timer_start_once(conn_tune_timer, 500 * 1000);
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
case BLE_GAP_EVENT_PHY_UPDATE_COMPLETE:
|
||||
ESP_LOGI(TAG, "phy update; status=%d tx=%u rx=%u",
|
||||
event->phy_updated.status, event->phy_updated.tx_phy,
|
||||
event->phy_updated.rx_phy);
|
||||
return 0;
|
||||
|
||||
case BLE_GAP_EVENT_ADV_COMPLETE:
|
||||
ESP_LOGI(TAG, "advertise complete; reason=%d", event->adv_complete.reason);
|
||||
ble_advertise();
|
||||
return 0;
|
||||
|
||||
case BLE_GAP_EVENT_SUBSCRIBE:
|
||||
if (event->subscribe.attr_handle == ble_chr_val_handle) {
|
||||
notify_enabled = event->subscribe.cur_notify;
|
||||
ESP_LOGI(TAG, "notify %s", notify_enabled ? "enabled" : "disabled");
|
||||
if (notify_enabled && conn_handle != BLE_HS_CONN_HANDLE_NONE) {
|
||||
ble_request_fast_conn_params(conn_handle);
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
|
||||
case BLE_GAP_EVENT_MTU:
|
||||
att_mtu = event->mtu.value;
|
||||
ESP_LOGI(TAG, "mtu update; handle=%d mtu=%d", event->mtu.conn_handle,
|
||||
event->mtu.value);
|
||||
return 0;
|
||||
|
||||
default:
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
static void ble_on_sync(void) {
|
||||
int rc;
|
||||
|
||||
rc = ble_hs_id_infer_auto(0, &own_addr_type);
|
||||
if (rc != 0) {
|
||||
ESP_LOGE(TAG, "ble_hs_id_infer_auto failed: %d", rc);
|
||||
return;
|
||||
}
|
||||
|
||||
uint8_t addr_val[6] = {0};
|
||||
ble_hs_id_copy_addr(own_addr_type, addr_val, NULL);
|
||||
ESP_LOGI(TAG, "device address: %02x:%02x:%02x:%02x:%02x:%02x", addr_val[5],
|
||||
addr_val[4], addr_val[3], addr_val[2], addr_val[1], addr_val[0]);
|
||||
|
||||
ble_advertise();
|
||||
}
|
||||
|
||||
static void ble_on_reset(int reason) {
|
||||
ESP_LOGE(TAG, "nimble reset; reason=%d", reason);
|
||||
}
|
||||
|
||||
static void ble_host_task(void *param) {
|
||||
(void)param;
|
||||
ESP_LOGI(TAG, "nimble host task started");
|
||||
nimble_port_run();
|
||||
nimble_port_freertos_deinit();
|
||||
}
|
||||
|
||||
esp_err_t init_ble_host(QueueHandle_t cmd_queue) {
|
||||
esp_err_t ret;
|
||||
|
||||
host_transport_set_cmd_queue(cmd_queue);
|
||||
host_transport_set_tx(ble_host_tx);
|
||||
|
||||
/* NVS may already be init via pod_settings; ignore ALREADY_INIT. */
|
||||
ret = nvs_flash_init();
|
||||
if (ret == ESP_ERR_NVS_NO_FREE_PAGES ||
|
||||
ret == ESP_ERR_NVS_NEW_VERSION_FOUND) {
|
||||
ESP_ERROR_CHECK(nvs_flash_erase());
|
||||
ret = nvs_flash_init();
|
||||
}
|
||||
if (ret != ESP_OK && ret != ESP_ERR_INVALID_STATE) {
|
||||
ESP_LOGE(TAG, "nvs_flash_init failed: %s", esp_err_to_name(ret));
|
||||
return ret;
|
||||
}
|
||||
|
||||
const esp_timer_create_args_t tune_args = {
|
||||
.callback = &ble_conn_tune_timer_cb,
|
||||
.name = "ble_conn_tune",
|
||||
};
|
||||
ESP_ERROR_CHECK(esp_timer_create(&tune_args, &conn_tune_timer));
|
||||
|
||||
ret = nimble_port_init();
|
||||
if (ret != ESP_OK) {
|
||||
ESP_LOGE(TAG, "nimble_port_init failed: %s", esp_err_to_name(ret));
|
||||
return ret;
|
||||
}
|
||||
|
||||
ble_hs_cfg.sync_cb = ble_on_sync;
|
||||
ble_hs_cfg.reset_cb = ble_on_reset;
|
||||
|
||||
int rc = gatt_svr_init();
|
||||
if (rc != 0) {
|
||||
ESP_LOGE(TAG, "gatt_svr_init failed: %d", rc);
|
||||
return ESP_FAIL;
|
||||
}
|
||||
|
||||
rc = ble_svc_gap_device_name_set(DEVICE_NAME);
|
||||
if (rc != 0) {
|
||||
ESP_LOGE(TAG, "device name set failed: %d", rc);
|
||||
return ESP_FAIL;
|
||||
}
|
||||
|
||||
rc = ble_att_set_preferred_mtu(247);
|
||||
if (rc != 0) {
|
||||
ESP_LOGW(TAG, "preferred MTU set failed: %d", rc);
|
||||
}
|
||||
|
||||
nimble_port_freertos_init(ble_host_task);
|
||||
ESP_LOGI(TAG, "BLE host transport started");
|
||||
return ESP_OK;
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
#ifndef BLE_HOST_H
|
||||
#define BLE_HOST_H
|
||||
|
||||
#include "esp_err.h"
|
||||
#include "freertos/FreeRTOS.h"
|
||||
#include "freertos/queue.h"
|
||||
#include <stddef.h>
|
||||
#include <stdint.h>
|
||||
|
||||
/** Start NimBLE host transport (Master only). Uses same framing as UART. */
|
||||
esp_err_t init_ble_host(QueueHandle_t cmd_queue);
|
||||
|
||||
/** True when a central is connected and CCCD notify is enabled. */
|
||||
bool ble_host_ready(void);
|
||||
|
||||
/** Negotiated ATT MTU (default 23 until exchange). */
|
||||
uint16_t ble_host_att_mtu(void);
|
||||
|
||||
/** Max notify/write payload bytes (ATT MTU - 3). */
|
||||
uint16_t ble_host_max_payload(void);
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,201 @@
|
||||
#include "ble_stream.h"
|
||||
#include "app_config.h"
|
||||
#include "ble_host.h"
|
||||
#include "client_registry.h"
|
||||
#include "esp_log.h"
|
||||
#include "esp_timer.h"
|
||||
#include "freertos/FreeRTOS.h"
|
||||
#include "freertos/task.h"
|
||||
#include "pb_encode.h"
|
||||
#include "uart.h"
|
||||
#include "uart_cmd.h"
|
||||
#include "uart_messages.pb.h"
|
||||
#include <string.h>
|
||||
|
||||
static const char *TAG = "[BLE_STREAM]";
|
||||
|
||||
#define BLE_STREAM_PERIOD_MIN_MS 8u
|
||||
#define BLE_STREAM_PERIOD_MAX_MS 16u
|
||||
#define BLE_STREAM_PERIOD_DEFAULT_MS 16u
|
||||
|
||||
static uint32_t s_period_ms = BLE_STREAM_PERIOD_DEFAULT_MS;
|
||||
static TaskHandle_t s_task;
|
||||
static volatile bool s_running;
|
||||
|
||||
static bool tap_notify_any(const client_info_t *client) {
|
||||
return client != NULL &&
|
||||
(client->tap_notify_single || client->tap_notify_double ||
|
||||
client->tap_notify_triple);
|
||||
}
|
||||
|
||||
static alox_TapKind tap_kind_from_registry(uint32_t kind) {
|
||||
switch (kind) {
|
||||
case 1:
|
||||
return alox_TapKind_TAP_SINGLE;
|
||||
case 2:
|
||||
return alox_TapKind_TAP_DOUBLE;
|
||||
case 3:
|
||||
return alox_TapKind_TAP_TRIPLE;
|
||||
default:
|
||||
return alox_TapKind_TAP_NONE;
|
||||
}
|
||||
}
|
||||
|
||||
static void fill_clients(alox_BleStreamTick *tick) {
|
||||
tick->clients_count = 0;
|
||||
size_t count = client_registry_count();
|
||||
for (size_t i = 0; i < count; i++) {
|
||||
const client_info_t *client = client_registry_at(i);
|
||||
if (client == NULL) {
|
||||
continue;
|
||||
}
|
||||
const bool want_accel = client->accel_stream_enabled;
|
||||
const bool want_tap = tap_notify_any(client);
|
||||
if (!want_accel && !want_tap) {
|
||||
continue;
|
||||
}
|
||||
if (tick->clients_count >=
|
||||
sizeof(tick->clients) / sizeof(tick->clients[0])) {
|
||||
break;
|
||||
}
|
||||
|
||||
alox_CacheClientStatus *entry = &tick->clients[tick->clients_count++];
|
||||
entry->client_id = client->id;
|
||||
entry->has_accel = false;
|
||||
entry->has_tap = false;
|
||||
|
||||
if (want_accel) {
|
||||
entry->has_accel = true;
|
||||
entry->accel.valid = client->accel_valid;
|
||||
if (client->accel_valid) {
|
||||
entry->accel.x = client->accel_x;
|
||||
entry->accel.y = client->accel_y;
|
||||
entry->accel.z = client->accel_z;
|
||||
entry->accel.age_ms =
|
||||
client_registry_ms_since(client->accel_updated_at);
|
||||
}
|
||||
}
|
||||
|
||||
if (want_tap) {
|
||||
uint32_t kind = 0;
|
||||
uint32_t age_ms = 0;
|
||||
if (client_registry_take_tap(client->id, &kind, &age_ms)) {
|
||||
entry->has_tap = true;
|
||||
entry->tap.kind = tap_kind_from_registry(kind);
|
||||
entry->tap.age_ms = age_ms;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static bool any_accel_stream(void) {
|
||||
size_t count = client_registry_count();
|
||||
for (size_t i = 0; i < count; i++) {
|
||||
const client_info_t *client = client_registry_at(i);
|
||||
if (client != NULL && client->accel_stream_enabled) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
static esp_err_t send_tick_truncated(alox_UartMessage *msg) {
|
||||
uint8_t pb_buf[MAX_PAYLOAD_SIZE];
|
||||
pb_ostream_t stream = pb_ostream_from_buffer(pb_buf, sizeof(pb_buf));
|
||||
|
||||
while (true) {
|
||||
stream = pb_ostream_from_buffer(pb_buf, sizeof(pb_buf));
|
||||
if (!pb_encode(&stream, alox_UartMessage_fields, msg)) {
|
||||
return ESP_FAIL;
|
||||
}
|
||||
|
||||
size_t payload_len = stream.bytes_written + 1;
|
||||
uint16_t max_pay = ble_host_max_payload();
|
||||
/* Framed size = payload + 4; notify must fit ATT MTU-3. */
|
||||
size_t frame_len = payload_len + 4;
|
||||
if (payload_len <= MAX_PAYLOAD_SIZE && frame_len <= max_pay) {
|
||||
uint8_t payload[MAX_PAYLOAD_SIZE];
|
||||
payload[0] = (uint8_t)msg->type;
|
||||
memcpy(&payload[1], pb_buf, stream.bytes_written);
|
||||
return uart_send_framed(payload, payload_len);
|
||||
}
|
||||
|
||||
if (msg->payload.ble_stream_tick.clients_count == 0) {
|
||||
ESP_LOGW(TAG, "tick too large even with 0 clients (need %u max %u)",
|
||||
(unsigned)frame_len, (unsigned)max_pay);
|
||||
return ESP_ERR_NO_MEM;
|
||||
}
|
||||
|
||||
msg->payload.ble_stream_tick.clients_count--;
|
||||
ESP_LOGW(TAG, "truncating tick clients to %u for MTU",
|
||||
(unsigned)msg->payload.ble_stream_tick.clients_count);
|
||||
}
|
||||
}
|
||||
|
||||
static void ble_stream_task(void *param) {
|
||||
(void)param;
|
||||
ESP_LOGI(TAG, "BLE stream task started period_ms=%lu",
|
||||
(unsigned long)s_period_ms);
|
||||
|
||||
while (s_running) {
|
||||
uint32_t period = s_period_ms;
|
||||
if (period < BLE_STREAM_PERIOD_MIN_MS) {
|
||||
period = BLE_STREAM_PERIOD_MIN_MS;
|
||||
}
|
||||
if (period > BLE_STREAM_PERIOD_MAX_MS) {
|
||||
period = BLE_STREAM_PERIOD_MAX_MS;
|
||||
}
|
||||
|
||||
if (ble_host_ready() && any_accel_stream()) {
|
||||
alox_UartMessage msg = alox_UartMessage_init_zero;
|
||||
uart_cmd_init_response(&msg, alox_MessageType_BLE_STREAM_TICK,
|
||||
alox_UartMessage_ble_stream_tick_tag);
|
||||
msg.payload.ble_stream_tick.esp_timestamp_us =
|
||||
(uint64_t)esp_timer_get_time();
|
||||
msg.payload.ble_stream_tick.period_ms = period;
|
||||
fill_clients(&msg.payload.ble_stream_tick);
|
||||
|
||||
esp_err_t err = send_tick_truncated(&msg);
|
||||
if (err != ESP_OK) {
|
||||
ESP_LOGW(TAG, "tick send failed: %s", esp_err_to_name(err));
|
||||
}
|
||||
}
|
||||
|
||||
vTaskDelay(pdMS_TO_TICKS(period));
|
||||
}
|
||||
|
||||
s_task = NULL;
|
||||
vTaskDelete(NULL);
|
||||
}
|
||||
|
||||
void ble_stream_set_period_ms(uint32_t period_ms) {
|
||||
if (period_ms == 0) {
|
||||
period_ms = BLE_STREAM_PERIOD_DEFAULT_MS;
|
||||
}
|
||||
if (period_ms < BLE_STREAM_PERIOD_MIN_MS) {
|
||||
period_ms = BLE_STREAM_PERIOD_MIN_MS;
|
||||
}
|
||||
if (period_ms > BLE_STREAM_PERIOD_MAX_MS) {
|
||||
period_ms = BLE_STREAM_PERIOD_MAX_MS;
|
||||
}
|
||||
s_period_ms = period_ms;
|
||||
}
|
||||
|
||||
uint32_t ble_stream_period_ms(void) { return s_period_ms; }
|
||||
|
||||
void ble_stream_update(void) {
|
||||
const app_config_t *cfg = app_config_get();
|
||||
bool want = cfg != NULL && cfg->host_ble && any_accel_stream();
|
||||
|
||||
if (want && !s_running) {
|
||||
s_running = true;
|
||||
if (xTaskCreate(ble_stream_task, "ble_stream", 4096, NULL, 5, &s_task) !=
|
||||
pdPASS) {
|
||||
s_running = false;
|
||||
s_task = NULL;
|
||||
ESP_LOGE(TAG, "failed to create ble_stream task");
|
||||
}
|
||||
} else if (!want && s_running) {
|
||||
s_running = false;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
#ifndef BLE_STREAM_H
|
||||
#define BLE_STREAM_H
|
||||
|
||||
#include <stdbool.h>
|
||||
#include <stdint.h>
|
||||
|
||||
/** Clamp and store BLE push period (8–16 ms). */
|
||||
void ble_stream_set_period_ms(uint32_t period_ms);
|
||||
|
||||
uint32_t ble_stream_period_ms(void);
|
||||
|
||||
/**
|
||||
* Re-evaluate whether the BLE push task should run (host_ble + any accel
|
||||
* stream enabled). Safe to call from accel-stream handlers.
|
||||
*/
|
||||
void ble_stream_update(void);
|
||||
|
||||
#endif
|
||||
@@ -1,5 +1,6 @@
|
||||
#include "client_registry.h"
|
||||
#include "cmd_accel_stream.h"
|
||||
#include "ble_stream.h"
|
||||
#include "esp_log.h"
|
||||
#include "esp_now_comm.h"
|
||||
#include "uart_cmd.h"
|
||||
@@ -50,6 +51,10 @@ static void handle_accel_stream(const uint8_t *data, size_t len) {
|
||||
}
|
||||
|
||||
if (req.write) {
|
||||
if (req.period_ms != 0) {
|
||||
ble_stream_set_period_ms(req.period_ms);
|
||||
}
|
||||
|
||||
if (req.all_clients) {
|
||||
size_t n = client_registry_set_accel_stream_all(req.enable);
|
||||
uint32_t sent = 0;
|
||||
@@ -65,8 +70,10 @@ static void handle_accel_stream(const uint8_t *data, size_t len) {
|
||||
}
|
||||
}
|
||||
|
||||
ESP_LOGI(TAG, "accel stream %s for %u/%u slaves",
|
||||
req.enable ? "on" : "off", (unsigned)sent, (unsigned)n);
|
||||
ESP_LOGI(TAG, "accel stream %s for %u/%u slaves period_ms=%lu",
|
||||
req.enable ? "on" : "off", (unsigned)sent, (unsigned)n,
|
||||
(unsigned long)ble_stream_period_ms());
|
||||
ble_stream_update();
|
||||
reply(req.enable, 0, sent > 0, sent);
|
||||
return;
|
||||
}
|
||||
@@ -85,6 +92,7 @@ static void handle_accel_stream(const uint8_t *data, size_t len) {
|
||||
}
|
||||
|
||||
esp_err_t err = push_stream_to_slave(client, req.enable);
|
||||
ble_stream_update();
|
||||
reply(req.enable, req.client_id, err == ESP_OK, err == ESP_OK ? 1u : 0u);
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
#include "cmd_ota.h"
|
||||
#include "app_config.h"
|
||||
#include "led_ring.h"
|
||||
#include "ota_espnow.h"
|
||||
#include "ota_uart.h"
|
||||
@@ -103,10 +104,24 @@ static void ota_prepare_task(void *param) {
|
||||
vTaskDelete(NULL);
|
||||
}
|
||||
|
||||
static bool ota_host_is_uart(void) {
|
||||
const app_config_t *cfg = app_config_get();
|
||||
if (cfg != NULL && cfg->host_ble) {
|
||||
ESP_LOGW(TAG, "OTA requires UART host (DIP8=0); BLE host active");
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
static void handle_ota_start(const uint8_t *data, size_t len) {
|
||||
alox_UartMessage uart_msg;
|
||||
alox_OtaStartPayload req = alox_OtaStartPayload_init_zero;
|
||||
|
||||
if (!ota_host_is_uart()) {
|
||||
send_ota_failed(20);
|
||||
return;
|
||||
}
|
||||
|
||||
if (uart_cmd_decode(data, len, &uart_msg) != ESP_OK) {
|
||||
send_ota_failed( 2);
|
||||
return;
|
||||
@@ -334,6 +349,10 @@ static void ota_start_espnow_task(void *param) {
|
||||
}
|
||||
|
||||
static void handle_ota_start_espnow(const uint8_t *data, size_t len) {
|
||||
if (!ota_host_is_uart()) {
|
||||
send_ota_failed(20);
|
||||
return;
|
||||
}
|
||||
(void)data;
|
||||
(void)len;
|
||||
|
||||
|
||||
@@ -0,0 +1,97 @@
|
||||
#include "host_transport.h"
|
||||
#include "cmd_handler.h"
|
||||
#include "esp_log.h"
|
||||
#include "uart.h"
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
static const char *TAG = "[HOST]";
|
||||
|
||||
static host_tx_fn_t s_tx_fn;
|
||||
static QueueHandle_t s_cmd_queue;
|
||||
static uart_packet_t s_rx_packet = {.state = STATE_START};
|
||||
static TickType_t s_last_byte_ticks;
|
||||
static const TickType_t s_rx_timeout_ticks = pdMS_TO_TICKS(50);
|
||||
|
||||
void host_transport_set_tx(host_tx_fn_t tx_fn) { s_tx_fn = tx_fn; }
|
||||
|
||||
void host_transport_set_cmd_queue(QueueHandle_t cmd_queue) {
|
||||
s_cmd_queue = cmd_queue;
|
||||
}
|
||||
|
||||
QueueHandle_t host_transport_cmd_queue(void) { return s_cmd_queue; }
|
||||
|
||||
bool host_enqueue_packet(const uint8_t *payload, uint8_t len) {
|
||||
if (s_cmd_queue == NULL || payload == NULL || len == 0) {
|
||||
return false;
|
||||
}
|
||||
|
||||
generic_msg_t msg = {
|
||||
.msg_id = payload[0],
|
||||
.len = len > 1 ? (size_t)(len - 1) : 0,
|
||||
.payload = NULL,
|
||||
};
|
||||
|
||||
if (msg.len > 0) {
|
||||
msg.payload = malloc(msg.len);
|
||||
if (msg.payload == NULL) {
|
||||
ESP_LOGE(TAG, "failed to allocate command payload");
|
||||
return false;
|
||||
}
|
||||
memcpy(msg.payload, &payload[1], msg.len);
|
||||
}
|
||||
|
||||
if (xQueueSend(s_cmd_queue, &msg, pdMS_TO_TICKS(500)) != pdPASS) {
|
||||
free(msg.payload);
|
||||
ESP_LOGW(TAG, "command queue full (cmd 0x%02x)", (unsigned)msg.msg_id);
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
esp_err_t host_send_framed(const uint8_t *payload, size_t len) {
|
||||
if (payload == NULL || len == 0 || len > MAX_PAYLOAD_SIZE) {
|
||||
return ESP_ERR_INVALID_ARG;
|
||||
}
|
||||
if (s_tx_fn == NULL) {
|
||||
return ESP_ERR_INVALID_STATE;
|
||||
}
|
||||
|
||||
uint8_t checksum = 0;
|
||||
for (size_t i = 0; i < len; i++) {
|
||||
checksum ^= payload[i];
|
||||
}
|
||||
|
||||
uint8_t frame[4 + MAX_PAYLOAD_SIZE];
|
||||
size_t pos = 0;
|
||||
frame[pos++] = START_MARKER;
|
||||
frame[pos++] = (uint8_t)len;
|
||||
memcpy(&frame[pos], payload, len);
|
||||
pos += len;
|
||||
frame[pos++] = checksum;
|
||||
frame[pos++] = STOP_MARKER;
|
||||
|
||||
return s_tx_fn(frame, pos);
|
||||
}
|
||||
|
||||
void host_rx_bytes(const uint8_t *data, size_t len) {
|
||||
if (data == NULL || len == 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
TickType_t now = xTaskGetTickCount();
|
||||
if (s_rx_packet.state != STATE_START &&
|
||||
(now - s_last_byte_ticks) > s_rx_timeout_ticks) {
|
||||
s_rx_packet.state = STATE_START;
|
||||
}
|
||||
|
||||
for (size_t i = 0; i < len; i++) {
|
||||
if (parse_uart_byte(data[i], &s_rx_packet)) {
|
||||
ESP_LOGI(TAG, "received message cmd=0x%02x len=%u",
|
||||
s_rx_packet.len > 0 ? s_rx_packet.payload[0] : 0,
|
||||
(unsigned)s_rx_packet.len);
|
||||
host_enqueue_packet(s_rx_packet.payload, s_rx_packet.len);
|
||||
}
|
||||
}
|
||||
s_last_byte_ticks = now;
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
#ifndef HOST_TRANSPORT_H
|
||||
#define HOST_TRANSPORT_H
|
||||
|
||||
#include "esp_err.h"
|
||||
#include "freertos/FreeRTOS.h"
|
||||
#include "freertos/queue.h"
|
||||
#include <stddef.h>
|
||||
#include <stdint.h>
|
||||
|
||||
/** TX callback: send a complete framed packet (0xAA … 0xCC). */
|
||||
typedef esp_err_t (*host_tx_fn_t)(const uint8_t *frame, size_t frame_len);
|
||||
|
||||
/** Set the active host TX backend (UART write or BLE notify). */
|
||||
void host_transport_set_tx(host_tx_fn_t tx_fn);
|
||||
|
||||
/** Bind the shared cmd_queue used by uart_enqueue_packet / host RX. */
|
||||
void host_transport_set_cmd_queue(QueueHandle_t cmd_queue);
|
||||
|
||||
QueueHandle_t host_transport_cmd_queue(void);
|
||||
|
||||
/**
|
||||
* Build AA|len|payload|xor|CC and send via the registered TX backend.
|
||||
* Same semantics as uart_send_framed.
|
||||
*/
|
||||
esp_err_t host_send_framed(const uint8_t *payload, size_t len);
|
||||
|
||||
/**
|
||||
* Feed raw host RX bytes into the shared frame parser and enqueue complete
|
||||
* packets onto the cmd_queue.
|
||||
*/
|
||||
void host_rx_bytes(const uint8_t *data, size_t len);
|
||||
|
||||
/** Enqueue a completed uart_packet_t (payload[0]=msg type). */
|
||||
bool host_enqueue_packet(const uint8_t *payload, uint8_t len);
|
||||
|
||||
#endif
|
||||
+28
-7
@@ -32,8 +32,10 @@
|
||||
#include "led_ring.h"
|
||||
#include "pod_settings.h"
|
||||
#include "uart.h"
|
||||
#include "ble_host.h"
|
||||
#include "battery_uv.h"
|
||||
#include <stdint.h>
|
||||
#include <string.h>
|
||||
|
||||
enum MASTER_STATES {
|
||||
MSTATE_INIT,
|
||||
@@ -94,7 +96,8 @@ void app_main(void) {
|
||||
gpio_reset_pin(DIP_MASTER);
|
||||
gpio_set_direction(DIP_MASTER, GPIO_MODE_INPUT);
|
||||
uint8_t master = gpio_get_level(DIP_MASTER) ? 0 : 1;
|
||||
uint8_t network = 0;
|
||||
uint8_t network = 1;
|
||||
bool host_ble = false;
|
||||
|
||||
esp_err_t err;
|
||||
|
||||
@@ -137,12 +140,20 @@ void app_main(void) {
|
||||
-1);
|
||||
if (err != ESP_OK) {
|
||||
ESP_LOGI(TAG, "Could not read IO Expander input %d", err);
|
||||
} else {
|
||||
/* High nibble physically reversed on PCB; DIP1..8 after reverse+invert. */
|
||||
uint8_t bits = (uint8_t)(~reverse_high_nibble_lut(read_val));
|
||||
/* DIP8 (bit7) = host transport: 1 = BLE, 0 = UART */
|
||||
host_ble = (bits & 0x80u) != 0;
|
||||
uint8_t net_bits = (uint8_t)(bits & 0x7Fu);
|
||||
if (net_bits == 0 || (net_bits & (net_bits - 1u)) != 0) {
|
||||
ESP_LOGW(TAG, "invalid network DIP one-hot=0x%02x; fallback network=1",
|
||||
net_bits);
|
||||
network = 1;
|
||||
} else {
|
||||
network = (uint8_t)(__builtin_ctz(net_bits) + 1); /* 1..7 */
|
||||
}
|
||||
}
|
||||
// Build NetworkNumber because Pin 5-8 are reversed
|
||||
network = reverse_high_nibble_lut(read_val);
|
||||
network = ~network; // reverse bits
|
||||
network = __builtin_ctz(network) +
|
||||
1; // map network to 0-8, 0 is not a valid network
|
||||
}
|
||||
|
||||
if (bus_handle != NULL) {
|
||||
@@ -167,6 +178,7 @@ void app_main(void) {
|
||||
|
||||
app_config.master = (master != 0);
|
||||
app_config.network = network;
|
||||
app_config.host_ble = host_ble;
|
||||
if (running != NULL) {
|
||||
memcpy(app_config.running_partition, running->label,
|
||||
sizeof(app_config.running_partition));
|
||||
@@ -177,6 +189,8 @@ void app_main(void) {
|
||||
ESP_LOGI(TAG, "RUNNING CONFIG:");
|
||||
ESP_LOGI(TAG, "Master: %d", app_config.master);
|
||||
ESP_LOGI(TAG, "Network: %d", app_config.network);
|
||||
ESP_LOGI(TAG, "Host: %s (DIP8; reboot to switch)",
|
||||
app_config.host_ble ? "BLE" : "UART");
|
||||
ESP_LOGI(TAG, "Running Partition: %s (OTA slot %d)",
|
||||
app_config.running_partition, ota_slot);
|
||||
|
||||
@@ -193,7 +207,14 @@ void app_main(void) {
|
||||
if (app_config.master) {
|
||||
cmd_queue = xQueueCreate(64, sizeof(generic_msg_t));
|
||||
init_cmdHandler(cmd_queue);
|
||||
init_uart(cmd_queue);
|
||||
if (app_config.host_ble) {
|
||||
err = init_ble_host(cmd_queue);
|
||||
if (err != ESP_OK) {
|
||||
ESP_LOGE(TAG, "BLE host init failed: %s", esp_err_to_name(err));
|
||||
}
|
||||
} else {
|
||||
init_uart(cmd_queue);
|
||||
}
|
||||
cmd_version_register();
|
||||
cmd_client_info_register();
|
||||
cmd_accel_deadzone_register();
|
||||
|
||||
@@ -81,6 +81,9 @@ PB_BIND(alox_CacheClientStatus, alox_CacheClientStatus, AUTO)
|
||||
PB_BIND(alox_CacheStatusResponse, alox_CacheStatusResponse, 2)
|
||||
|
||||
|
||||
PB_BIND(alox_BleStreamTick, alox_BleStreamTick, 2)
|
||||
|
||||
|
||||
PB_BIND(alox_EspNowUnicastTestRequest, alox_EspNowUnicastTestRequest, AUTO)
|
||||
|
||||
|
||||
|
||||
@@ -38,7 +38,9 @@ typedef enum _alox_MessageType {
|
||||
/* * Host → master: get/set ESP-IDF log level for tag "*" (global). */
|
||||
alox_MessageType_SET_LOG_LEVEL = 31,
|
||||
/* * Host → master: get/set BMA456H multitap detection parameters. */
|
||||
alox_MessageType_TAP_CONFIG = 32
|
||||
alox_MessageType_TAP_CONFIG = 32,
|
||||
/* * Master → host (BLE only): periodic pushed cache snapshot with ESP timestamp. */
|
||||
alox_MessageType_BLE_STREAM_TICK = 33
|
||||
} alox_MessageType;
|
||||
|
||||
typedef enum _alox_TapKind {
|
||||
@@ -113,12 +115,14 @@ typedef struct _alox_AccelDeadzoneResponse {
|
||||
} alox_AccelDeadzoneResponse;
|
||||
|
||||
/* Host → master: enable/disable slave accel ESP-NOW stream (~16 ms per slave).
|
||||
write=false: read; write=true: apply. client_id 0 invalid for write (use >0 or all_clients). */
|
||||
write=false: read; write=true: apply. client_id 0 invalid for write (use >0 or all_clients).
|
||||
period_ms (8–16): BLE host push interval for BleStreamTick; ignored on UART (default 16). */
|
||||
typedef struct _alox_AccelStreamRequest {
|
||||
bool write;
|
||||
bool enable;
|
||||
uint32_t client_id;
|
||||
bool all_clients;
|
||||
uint32_t period_ms;
|
||||
} alox_AccelStreamRequest;
|
||||
|
||||
typedef struct _alox_AccelStreamResponse {
|
||||
@@ -231,6 +235,14 @@ typedef struct _alox_CacheStatusResponse {
|
||||
alox_CacheClientStatus clients[16];
|
||||
} alox_CacheStatusResponse;
|
||||
|
||||
/* * Master → host over BLE notify: aggregated cache + ESP timer timestamp. */
|
||||
typedef struct _alox_BleStreamTick {
|
||||
uint64_t esp_timestamp_us;
|
||||
uint32_t period_ms;
|
||||
pb_size_t clients_count;
|
||||
alox_CacheClientStatus clients[16];
|
||||
} alox_BleStreamTick;
|
||||
|
||||
typedef struct _alox_EspNowUnicastTestRequest {
|
||||
uint32_t client_id;
|
||||
uint32_t seq;
|
||||
@@ -443,6 +455,7 @@ typedef struct _alox_UartMessage {
|
||||
alox_SetLogLevelResponse set_log_level_response;
|
||||
alox_TapConfigRequest tap_config_request;
|
||||
alox_TapConfigResponse tap_config_response;
|
||||
alox_BleStreamTick ble_stream_tick;
|
||||
} payload;
|
||||
} alox_UartMessage;
|
||||
|
||||
@@ -453,8 +466,8 @@ extern "C" {
|
||||
|
||||
/* Helper constants for enums */
|
||||
#define _alox_MessageType_MIN alox_MessageType_UNKNOWN
|
||||
#define _alox_MessageType_MAX alox_MessageType_TAP_CONFIG
|
||||
#define _alox_MessageType_ARRAYSIZE ((alox_MessageType)(alox_MessageType_TAP_CONFIG+1))
|
||||
#define _alox_MessageType_MAX alox_MessageType_BLE_STREAM_TICK
|
||||
#define _alox_MessageType_ARRAYSIZE ((alox_MessageType)(alox_MessageType_BLE_STREAM_TICK+1))
|
||||
|
||||
#define _alox_TapKind_MIN alox_TapKind_TAP_NONE
|
||||
#define _alox_TapKind_MAX alox_TapKind_TAP_TRIPLE
|
||||
@@ -508,6 +521,7 @@ extern "C" {
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
/* Initializer values for message structs */
|
||||
@@ -521,7 +535,7 @@ extern "C" {
|
||||
#define alox_ClientInputResponse_init_default {{{NULL}, NULL}}
|
||||
#define alox_AccelDeadzoneRequest_init_default {0, 0, 0, 0}
|
||||
#define alox_AccelDeadzoneResponse_init_default {0, 0, 0, 0}
|
||||
#define alox_AccelStreamRequest_init_default {0, 0, 0, 0}
|
||||
#define alox_AccelStreamRequest_init_default {0, 0, 0, 0, 0}
|
||||
#define alox_AccelStreamResponse_init_default {0, 0, 0, 0}
|
||||
#define alox_BatteryStatusRequest_init_default {0, 0}
|
||||
#define alox_LipoReading_init_default {0, 0}
|
||||
@@ -536,6 +550,7 @@ extern "C" {
|
||||
#define alox_CacheClientTap_init_default {_alox_TapKind_MIN, 0}
|
||||
#define alox_CacheClientStatus_init_default {0, false, alox_CacheClientAccel_init_default, false, alox_CacheClientTap_init_default}
|
||||
#define alox_CacheStatusResponse_init_default {0, {alox_CacheClientStatus_init_default, alox_CacheClientStatus_init_default, alox_CacheClientStatus_init_default, alox_CacheClientStatus_init_default, alox_CacheClientStatus_init_default, alox_CacheClientStatus_init_default, alox_CacheClientStatus_init_default, alox_CacheClientStatus_init_default, alox_CacheClientStatus_init_default, alox_CacheClientStatus_init_default, alox_CacheClientStatus_init_default, alox_CacheClientStatus_init_default, alox_CacheClientStatus_init_default, alox_CacheClientStatus_init_default, alox_CacheClientStatus_init_default, alox_CacheClientStatus_init_default}}
|
||||
#define alox_BleStreamTick_init_default {0, 0, 0, {alox_CacheClientStatus_init_default, alox_CacheClientStatus_init_default, alox_CacheClientStatus_init_default, alox_CacheClientStatus_init_default, alox_CacheClientStatus_init_default, alox_CacheClientStatus_init_default, alox_CacheClientStatus_init_default, alox_CacheClientStatus_init_default, alox_CacheClientStatus_init_default, alox_CacheClientStatus_init_default, alox_CacheClientStatus_init_default, alox_CacheClientStatus_init_default, alox_CacheClientStatus_init_default, alox_CacheClientStatus_init_default, alox_CacheClientStatus_init_default, alox_CacheClientStatus_init_default}}
|
||||
#define alox_EspNowUnicastTestRequest_init_default {0, 0}
|
||||
#define alox_EspNowUnicastTestResponse_init_default {0, 0}
|
||||
#define alox_EspNowEchoPingRequest_init_default {0, 0}
|
||||
@@ -567,7 +582,7 @@ extern "C" {
|
||||
#define alox_ClientInputResponse_init_zero {{{NULL}, NULL}}
|
||||
#define alox_AccelDeadzoneRequest_init_zero {0, 0, 0, 0}
|
||||
#define alox_AccelDeadzoneResponse_init_zero {0, 0, 0, 0}
|
||||
#define alox_AccelStreamRequest_init_zero {0, 0, 0, 0}
|
||||
#define alox_AccelStreamRequest_init_zero {0, 0, 0, 0, 0}
|
||||
#define alox_AccelStreamResponse_init_zero {0, 0, 0, 0}
|
||||
#define alox_BatteryStatusRequest_init_zero {0, 0}
|
||||
#define alox_LipoReading_init_zero {0, 0}
|
||||
@@ -582,6 +597,7 @@ extern "C" {
|
||||
#define alox_CacheClientTap_init_zero {_alox_TapKind_MIN, 0}
|
||||
#define alox_CacheClientStatus_init_zero {0, false, alox_CacheClientAccel_init_zero, false, alox_CacheClientTap_init_zero}
|
||||
#define alox_CacheStatusResponse_init_zero {0, {alox_CacheClientStatus_init_zero, alox_CacheClientStatus_init_zero, alox_CacheClientStatus_init_zero, alox_CacheClientStatus_init_zero, alox_CacheClientStatus_init_zero, alox_CacheClientStatus_init_zero, alox_CacheClientStatus_init_zero, alox_CacheClientStatus_init_zero, alox_CacheClientStatus_init_zero, alox_CacheClientStatus_init_zero, alox_CacheClientStatus_init_zero, alox_CacheClientStatus_init_zero, alox_CacheClientStatus_init_zero, alox_CacheClientStatus_init_zero, alox_CacheClientStatus_init_zero, alox_CacheClientStatus_init_zero}}
|
||||
#define alox_BleStreamTick_init_zero {0, 0, 0, {alox_CacheClientStatus_init_zero, alox_CacheClientStatus_init_zero, alox_CacheClientStatus_init_zero, alox_CacheClientStatus_init_zero, alox_CacheClientStatus_init_zero, alox_CacheClientStatus_init_zero, alox_CacheClientStatus_init_zero, alox_CacheClientStatus_init_zero, alox_CacheClientStatus_init_zero, alox_CacheClientStatus_init_zero, alox_CacheClientStatus_init_zero, alox_CacheClientStatus_init_zero, alox_CacheClientStatus_init_zero, alox_CacheClientStatus_init_zero, alox_CacheClientStatus_init_zero, alox_CacheClientStatus_init_zero}}
|
||||
#define alox_EspNowUnicastTestRequest_init_zero {0, 0}
|
||||
#define alox_EspNowUnicastTestResponse_init_zero {0, 0}
|
||||
#define alox_EspNowEchoPingRequest_init_zero {0, 0}
|
||||
@@ -638,6 +654,7 @@ extern "C" {
|
||||
#define alox_AccelStreamRequest_enable_tag 2
|
||||
#define alox_AccelStreamRequest_client_id_tag 3
|
||||
#define alox_AccelStreamRequest_all_clients_tag 4
|
||||
#define alox_AccelStreamRequest_period_ms_tag 5
|
||||
#define alox_AccelStreamResponse_enabled_tag 1
|
||||
#define alox_AccelStreamResponse_client_id_tag 2
|
||||
#define alox_AccelStreamResponse_success_tag 3
|
||||
@@ -685,6 +702,9 @@ extern "C" {
|
||||
#define alox_CacheClientStatus_accel_tag 2
|
||||
#define alox_CacheClientStatus_tap_tag 3
|
||||
#define alox_CacheStatusResponse_clients_tag 1
|
||||
#define alox_BleStreamTick_esp_timestamp_us_tag 1
|
||||
#define alox_BleStreamTick_period_ms_tag 2
|
||||
#define alox_BleStreamTick_clients_tag 3
|
||||
#define alox_EspNowUnicastTestRequest_client_id_tag 1
|
||||
#define alox_EspNowUnicastTestRequest_seq_tag 2
|
||||
#define alox_EspNowUnicastTestResponse_success_tag 1
|
||||
@@ -801,6 +821,7 @@ extern "C" {
|
||||
#define alox_UartMessage_set_log_level_response_tag 38
|
||||
#define alox_UartMessage_tap_config_request_tag 39
|
||||
#define alox_UartMessage_tap_config_response_tag 40
|
||||
#define alox_UartMessage_ble_stream_tick_tag 41
|
||||
|
||||
/* Struct field encoding specification for nanopb */
|
||||
#define alox_UartMessage_FIELDLIST(X, a) \
|
||||
@@ -839,7 +860,8 @@ X(a, STATIC, ONEOF, MESSAGE, (payload,espnow_echo_ping_response,payload.es
|
||||
X(a, STATIC, ONEOF, MESSAGE, (payload,set_log_level_request,payload.set_log_level_request), 37) \
|
||||
X(a, STATIC, ONEOF, MESSAGE, (payload,set_log_level_response,payload.set_log_level_response), 38) \
|
||||
X(a, STATIC, ONEOF, MESSAGE, (payload,tap_config_request,payload.tap_config_request), 39) \
|
||||
X(a, STATIC, ONEOF, MESSAGE, (payload,tap_config_response,payload.tap_config_response), 40)
|
||||
X(a, STATIC, ONEOF, MESSAGE, (payload,tap_config_response,payload.tap_config_response), 40) \
|
||||
X(a, STATIC, ONEOF, MESSAGE, (payload,ble_stream_tick,payload.ble_stream_tick), 41)
|
||||
#define alox_UartMessage_CALLBACK NULL
|
||||
#define alox_UartMessage_DEFAULT NULL
|
||||
#define alox_UartMessage_payload_ack_payload_MSGTYPE alox_Ack
|
||||
@@ -877,6 +899,7 @@ X(a, STATIC, ONEOF, MESSAGE, (payload,tap_config_response,payload.tap_conf
|
||||
#define alox_UartMessage_payload_set_log_level_response_MSGTYPE alox_SetLogLevelResponse
|
||||
#define alox_UartMessage_payload_tap_config_request_MSGTYPE alox_TapConfigRequest
|
||||
#define alox_UartMessage_payload_tap_config_response_MSGTYPE alox_TapConfigResponse
|
||||
#define alox_UartMessage_payload_ble_stream_tick_MSGTYPE alox_BleStreamTick
|
||||
|
||||
#define alox_Ack_FIELDLIST(X, a) \
|
||||
|
||||
@@ -950,7 +973,8 @@ X(a, STATIC, SINGULAR, UINT32, slaves_updated, 4)
|
||||
X(a, STATIC, SINGULAR, BOOL, write, 1) \
|
||||
X(a, STATIC, SINGULAR, BOOL, enable, 2) \
|
||||
X(a, STATIC, SINGULAR, UINT32, client_id, 3) \
|
||||
X(a, STATIC, SINGULAR, BOOL, all_clients, 4)
|
||||
X(a, STATIC, SINGULAR, BOOL, all_clients, 4) \
|
||||
X(a, STATIC, SINGULAR, UINT32, period_ms, 5)
|
||||
#define alox_AccelStreamRequest_CALLBACK NULL
|
||||
#define alox_AccelStreamRequest_DEFAULT NULL
|
||||
|
||||
@@ -1064,6 +1088,14 @@ X(a, STATIC, REPEATED, MESSAGE, clients, 1)
|
||||
#define alox_CacheStatusResponse_DEFAULT NULL
|
||||
#define alox_CacheStatusResponse_clients_MSGTYPE alox_CacheClientStatus
|
||||
|
||||
#define alox_BleStreamTick_FIELDLIST(X, a) \
|
||||
X(a, STATIC, SINGULAR, UINT64, esp_timestamp_us, 1) \
|
||||
X(a, STATIC, SINGULAR, UINT32, period_ms, 2) \
|
||||
X(a, STATIC, REPEATED, MESSAGE, clients, 3)
|
||||
#define alox_BleStreamTick_CALLBACK NULL
|
||||
#define alox_BleStreamTick_DEFAULT NULL
|
||||
#define alox_BleStreamTick_clients_MSGTYPE alox_CacheClientStatus
|
||||
|
||||
#define alox_EspNowUnicastTestRequest_FIELDLIST(X, a) \
|
||||
X(a, STATIC, SINGULAR, UINT32, client_id, 1) \
|
||||
X(a, STATIC, SINGULAR, UINT32, seq, 2)
|
||||
@@ -1255,6 +1287,7 @@ extern const pb_msgdesc_t alox_CacheClientAccel_msg;
|
||||
extern const pb_msgdesc_t alox_CacheClientTap_msg;
|
||||
extern const pb_msgdesc_t alox_CacheClientStatus_msg;
|
||||
extern const pb_msgdesc_t alox_CacheStatusResponse_msg;
|
||||
extern const pb_msgdesc_t alox_BleStreamTick_msg;
|
||||
extern const pb_msgdesc_t alox_EspNowUnicastTestRequest_msg;
|
||||
extern const pb_msgdesc_t alox_EspNowUnicastTestResponse_msg;
|
||||
extern const pb_msgdesc_t alox_EspNowEchoPingRequest_msg;
|
||||
@@ -1303,6 +1336,7 @@ extern const pb_msgdesc_t alox_OtaSlaveProgressResponse_msg;
|
||||
#define alox_CacheClientTap_fields &alox_CacheClientTap_msg
|
||||
#define alox_CacheClientStatus_fields &alox_CacheClientStatus_msg
|
||||
#define alox_CacheStatusResponse_fields &alox_CacheStatusResponse_msg
|
||||
#define alox_BleStreamTick_fields &alox_BleStreamTick_msg
|
||||
#define alox_EspNowUnicastTestRequest_fields &alox_EspNowUnicastTestRequest_msg
|
||||
#define alox_EspNowUnicastTestResponse_fields &alox_EspNowUnicastTestResponse_msg
|
||||
#define alox_EspNowEchoPingRequest_fields &alox_EspNowEchoPingRequest_msg
|
||||
@@ -1332,16 +1366,17 @@ extern const pb_msgdesc_t alox_OtaSlaveProgressResponse_msg;
|
||||
/* alox_ClientInfo_size depends on runtime parameters */
|
||||
/* alox_ClientInfoResponse_size depends on runtime parameters */
|
||||
/* alox_ClientInputResponse_size depends on runtime parameters */
|
||||
#define ALOX_UART_MESSAGES_PB_H_MAX_SIZE alox_CacheStatusResponse_size
|
||||
#define ALOX_UART_MESSAGES_PB_H_MAX_SIZE alox_BleStreamTick_size
|
||||
#define alox_AccelDeadzoneRequest_size 16
|
||||
#define alox_AccelDeadzoneResponse_size 20
|
||||
#define alox_AccelSample_size 32
|
||||
#define alox_AccelStreamRequest_size 12
|
||||
#define alox_AccelStreamRequest_size 18
|
||||
#define alox_AccelStreamResponse_size 16
|
||||
#define alox_Ack_size 0
|
||||
#define alox_BatterySample_size 32
|
||||
#define alox_BatteryStatusRequest_size 8
|
||||
#define alox_BatteryStatusResponse_size 580
|
||||
#define alox_BleStreamTick_size 753
|
||||
#define alox_CacheClientAccel_size 26
|
||||
#define alox_CacheClientStatus_size 44
|
||||
#define alox_CacheClientTap_size 8
|
||||
|
||||
@@ -35,6 +35,8 @@ enum MessageType {
|
||||
SET_LOG_LEVEL = 31;
|
||||
/** Host → master: get/set BMA456H multitap detection parameters. */
|
||||
TAP_CONFIG = 32;
|
||||
/** Master → host (BLE only): periodic pushed cache snapshot with ESP timestamp. */
|
||||
BLE_STREAM_TICK = 33;
|
||||
}
|
||||
|
||||
message UartMessage {
|
||||
@@ -75,6 +77,7 @@ message UartMessage {
|
||||
SetLogLevelResponse set_log_level_response = 38;
|
||||
TapConfigRequest tap_config_request = 39;
|
||||
TapConfigResponse tap_config_response = 40;
|
||||
BleStreamTick ble_stream_tick = 41;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -141,11 +144,13 @@ message AccelDeadzoneResponse {
|
||||
|
||||
// Host → master: enable/disable slave accel ESP-NOW stream (~16 ms per slave).
|
||||
// write=false: read; write=true: apply. client_id 0 invalid for write (use >0 or all_clients).
|
||||
// period_ms (8–16): BLE host push interval for BleStreamTick; ignored on UART (default 16).
|
||||
message AccelStreamRequest {
|
||||
bool write = 1;
|
||||
bool enable = 2;
|
||||
uint32 client_id = 3;
|
||||
bool all_clients = 4;
|
||||
uint32 period_ms = 5;
|
||||
}
|
||||
|
||||
message AccelStreamResponse {
|
||||
@@ -257,6 +262,13 @@ message CacheStatusResponse {
|
||||
repeated CacheClientStatus clients = 1 [(nanopb).max_count = 16];
|
||||
}
|
||||
|
||||
/** Master → host over BLE notify: aggregated cache + ESP timer timestamp. */
|
||||
message BleStreamTick {
|
||||
uint64 esp_timestamp_us = 1;
|
||||
uint32 period_ms = 2;
|
||||
repeated CacheClientStatus clients = 3 [(nanopb).max_count = 16];
|
||||
}
|
||||
|
||||
message EspNowUnicastTestRequest {
|
||||
uint32 client_id = 1;
|
||||
uint32 seq = 2;
|
||||
|
||||
+12
-69
@@ -4,46 +4,28 @@
|
||||
#include "esp_log.h"
|
||||
#include "freertos/idf_additions.h"
|
||||
#include "hal/uart_types.h"
|
||||
#include "host_transport.h"
|
||||
#include "uart.h"
|
||||
#include <stdbool.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
static const char *TAG = "[UART]";
|
||||
static QueueHandle_t uart_cmd_queue;
|
||||
|
||||
static bool uart_enqueue_packet(const uart_packet_t *packet) {
|
||||
if (packet->len == 0) {
|
||||
return false;
|
||||
static esp_err_t uart_host_tx(const uint8_t *frame, size_t frame_len) {
|
||||
int written = uart_write_bytes(UART_NUM, frame, frame_len);
|
||||
if (written < 0 || (size_t)written != frame_len) {
|
||||
return ESP_FAIL;
|
||||
}
|
||||
|
||||
generic_msg_t msg = {
|
||||
.msg_id = packet->payload[0],
|
||||
.len = packet->len > 1 ? packet->len - 1 : 0,
|
||||
.payload = NULL,
|
||||
};
|
||||
|
||||
if (msg.len > 0) {
|
||||
msg.payload = malloc(msg.len);
|
||||
if (msg.payload == NULL) {
|
||||
ESP_LOGE(TAG, "failed to allocate command payload");
|
||||
return false;
|
||||
}
|
||||
memcpy(msg.payload, &packet->payload[1], msg.len);
|
||||
}
|
||||
|
||||
if (xQueueSend(uart_cmd_queue, &msg, pdMS_TO_TICKS(500)) != pdPASS) {
|
||||
free(msg.payload);
|
||||
ESP_LOGW(TAG, "command queue full (cmd 0x%02x)", (unsigned)msg.msg_id);
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
void init_uart(QueueHandle_t cmd_queue) {
|
||||
esp_err_t err;
|
||||
|
||||
uart_cmd_queue = cmd_queue;
|
||||
host_transport_set_cmd_queue(cmd_queue);
|
||||
host_transport_set_tx(uart_host_tx);
|
||||
|
||||
uart_config_t uart_config = {
|
||||
.baud_rate = UART_BAUD_RATE,
|
||||
.data_bits = UART_DATA_8_BITS,
|
||||
@@ -85,27 +67,10 @@ void uart_read_task(void *param) {
|
||||
return;
|
||||
}
|
||||
|
||||
int len = 0;
|
||||
uart_packet_t packet = {.state = STATE_START};
|
||||
TickType_t last_byte_time = xTaskGetTickCount();
|
||||
const TickType_t timeout_ticks = pdMS_TO_TICKS(50);
|
||||
|
||||
while (1) {
|
||||
len = uart_read_bytes(UART_NUM, data, UART_BUF_SIZE, pdMS_TO_TICKS(20));
|
||||
int len = uart_read_bytes(UART_NUM, data, UART_BUF_SIZE, pdMS_TO_TICKS(20));
|
||||
if (len > 0) {
|
||||
for (int i = 0; i < len; ++i) {
|
||||
if (parse_uart_byte(data[i], &packet)) {
|
||||
ESP_LOGI(TAG, "received message cmd=0x%02x len=%u",
|
||||
packet.len > 0 ? packet.payload[0] : 0, (unsigned)packet.len);
|
||||
uart_enqueue_packet(&packet);
|
||||
}
|
||||
}
|
||||
last_byte_time = xTaskGetTickCount();
|
||||
} else if (packet.state != STATE_START) {
|
||||
TickType_t now = xTaskGetTickCount();
|
||||
if ((now - last_byte_time) > timeout_ticks) {
|
||||
packet.state = STATE_START;
|
||||
}
|
||||
host_rx_bytes(data, (size_t)len);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -156,27 +121,5 @@ bool parse_uart_byte(uint8_t byte, uart_packet_t *p) {
|
||||
}
|
||||
|
||||
esp_err_t uart_send_framed(const uint8_t *payload, size_t len) {
|
||||
if (payload == NULL || len == 0 || len > MAX_PAYLOAD_SIZE) {
|
||||
return ESP_ERR_INVALID_ARG;
|
||||
}
|
||||
|
||||
uint8_t checksum = 0;
|
||||
for (size_t i = 0; i < len; i++) {
|
||||
checksum ^= payload[i];
|
||||
}
|
||||
|
||||
uint8_t frame[4 + MAX_PAYLOAD_SIZE];
|
||||
size_t pos = 0;
|
||||
frame[pos++] = START_MARKER;
|
||||
frame[pos++] = (uint8_t)len;
|
||||
memcpy(&frame[pos], payload, len);
|
||||
pos += len;
|
||||
frame[pos++] = checksum;
|
||||
frame[pos++] = STOP_MARKER;
|
||||
|
||||
int written = uart_write_bytes(UART_NUM, frame, pos);
|
||||
if (written < 0 || (size_t)written != pos) {
|
||||
return ESP_FAIL;
|
||||
}
|
||||
return ESP_OK;
|
||||
return host_send_framed(payload, len);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user