diff --git a/CMakeLists.txt b/CMakeLists.txt index 73112b0..2bc0a00 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -17,6 +17,7 @@ target_sources(app PRIVATE src/ble_adv_uuid16.c src/ble_bas_module.c src/ble_hid_module.c + src/ble_serial_module.c src/display_module.c src/encoder_module.c src/hid_flowctrl_module.c @@ -27,6 +28,8 @@ target_sources(app PRIVATE src/usb_device_module.c src/usb_hid_module.c src/events/bat_state_event.c + src/events/ble_serial_rx_event.c + src/events/ble_serial_tx_event.c src/events/encoder_event.c src/events/hid_led_event.c src/events/hid_report_sent_event.c diff --git a/inc/events/ble_serial_rx_event.h b/inc/events/ble_serial_rx_event.h new file mode 100644 index 0000000..b5d7817 --- /dev/null +++ b/inc/events/ble_serial_rx_event.h @@ -0,0 +1,22 @@ +#ifndef BLINKY_BLE_SERIAL_RX_EVENT_H_ +#define BLINKY_BLE_SERIAL_RX_EVENT_H_ + +#include +#include + +#ifdef __cplusplus +extern "C" { +#endif + +struct ble_serial_rx_event { + struct app_event_header header; + struct event_dyndata dyndata; +}; + +APP_EVENT_TYPE_DYNDATA_DECLARE(ble_serial_rx_event); + +#ifdef __cplusplus +} +#endif + +#endif /* BLINKY_BLE_SERIAL_RX_EVENT_H_ */ diff --git a/inc/events/ble_serial_tx_event.h b/inc/events/ble_serial_tx_event.h new file mode 100644 index 0000000..4deed35 --- /dev/null +++ b/inc/events/ble_serial_tx_event.h @@ -0,0 +1,22 @@ +#ifndef BLINKY_BLE_SERIAL_TX_EVENT_H_ +#define BLINKY_BLE_SERIAL_TX_EVENT_H_ + +#include +#include + +#ifdef __cplusplus +extern "C" { +#endif + +struct ble_serial_tx_event { + struct app_event_header header; + struct event_dyndata dyndata; +}; + +APP_EVENT_TYPE_DYNDATA_DECLARE(ble_serial_tx_event); + +#ifdef __cplusplus +} +#endif + +#endif /* BLINKY_BLE_SERIAL_TX_EVENT_H_ */ diff --git a/prj.conf b/prj.conf index 41ea7a1..f09e5ff 100644 --- a/prj.conf +++ b/prj.conf @@ -47,6 +47,9 @@ CONFIG_BT_SETTINGS=y CONFIG_BT_MAX_CONN=1 CONFIG_BT_MAX_PAIRED=1 CONFIG_BT_ATT_TX_COUNT=5 +CONFIG_BT_L2CAP_TX_MTU=65 +CONFIG_BT_BUF_ACL_RX_SIZE=69 +CONFIG_BT_BUF_ACL_TX_SIZE=69 CONFIG_BT_CONN_CTX=y CONFIG_BT_DEVICE_NAME="WH Mini Keyboard" CONFIG_BT_DEVICE_APPEARANCE=961 diff --git a/src/ble_serial_module.c b/src/ble_serial_module.c new file mode 100644 index 0000000..baf2259 --- /dev/null +++ b/src/ble_serial_module.c @@ -0,0 +1,308 @@ +#include +#include +#include +#include + +#include + +#define MODULE ble_serial_module +#include +#include + +#include +#include +#include +#include +#include +#include + +#include "ble_serial_rx_event.h" +#include "ble_serial_tx_event.h" + +LOG_MODULE_REGISTER(MODULE, LOG_LEVEL_INF); + +#define BLE_SERIAL_RX_MAX_LEN 64U +#define BLE_SERIAL_MIN_PAYLOAD_LEN 32U +#define BLE_SERIAL_NOTIFY_OVERHEAD 3U +#define BLE_SERIAL_SERVICE_UUID_VAL \ + BT_UUID_128_ENCODE(0x0b7f6000, 0x38d2, 0x4f62, 0x8f6f, 0x36c4fd73a110) +#define BLE_SERIAL_RX_UUID_VAL \ + BT_UUID_128_ENCODE(0x0b7f6001, 0x38d2, 0x4f62, 0x8f6f, 0x36c4fd73a110) +#define BLE_SERIAL_TX_UUID_VAL \ + BT_UUID_128_ENCODE(0x0b7f6002, 0x38d2, 0x4f62, 0x8f6f, 0x36c4fd73a110) + +static struct bt_conn *active_conn; +static bool initialized; +static bool running; +static bool ble_ready; +static bool secured; +static bool tx_notify_enabled; + +static const struct bt_uuid_128 ble_serial_service_uuid = + BT_UUID_INIT_128(BLE_SERIAL_SERVICE_UUID_VAL); +static const struct bt_uuid_128 ble_serial_rx_uuid = + BT_UUID_INIT_128(BLE_SERIAL_RX_UUID_VAL); +static const struct bt_uuid_128 ble_serial_tx_uuid = + BT_UUID_INIT_128(BLE_SERIAL_TX_UUID_VAL); + +static void submit_ble_serial_rx_event(const uint8_t *data, size_t len) +{ + struct ble_serial_rx_event *event = new_ble_serial_rx_event(len); + + memcpy(event->dyndata.data, data, len); + APP_EVENT_SUBMIT(event); +} + +static void tx_ccc_cfg_changed(const struct bt_gatt_attr *attr, uint16_t value) +{ + ARG_UNUSED(attr); + + tx_notify_enabled = (value == BT_GATT_CCC_NOTIFY); + LOG_INF("BLE serial TX notify %s", tx_notify_enabled ? "enabled" : "disabled"); +} + +static ssize_t rx_write_handler(struct bt_conn *conn, + const struct bt_gatt_attr *attr, + const void *buf, + uint16_t len, + uint16_t offset, + uint8_t flags) +{ + ARG_UNUSED(attr); + ARG_UNUSED(flags); + + if (!running || !ble_ready || !secured || (conn != active_conn)) { + return BT_GATT_ERR(BT_ATT_ERR_WRITE_REQ_REJECTED); + } + + if (offset != 0U) { + return BT_GATT_ERR(BT_ATT_ERR_INVALID_OFFSET); + } + + if ((buf == NULL) || (len == 0U) || (len > BLE_SERIAL_RX_MAX_LEN)) { + return BT_GATT_ERR(BT_ATT_ERR_INVALID_ATTRIBUTE_LEN); + } + + submit_ble_serial_rx_event(buf, len); + return len; +} + +BT_GATT_SERVICE_DEFINE(ble_serial_svc, + BT_GATT_PRIMARY_SERVICE(&ble_serial_service_uuid.uuid), + BT_GATT_CHARACTERISTIC(&ble_serial_rx_uuid.uuid, + BT_GATT_CHRC_WRITE | BT_GATT_CHRC_WRITE_WITHOUT_RESP, + BT_GATT_PERM_WRITE_ENCRYPT, + NULL, rx_write_handler, NULL), + BT_GATT_CHARACTERISTIC(&ble_serial_tx_uuid.uuid, + BT_GATT_CHRC_NOTIFY, + BT_GATT_PERM_NONE, + NULL, NULL, NULL), + BT_GATT_CCC(tx_ccc_cfg_changed, + BT_GATT_PERM_READ | BT_GATT_PERM_WRITE_ENCRYPT) +); + +static void mtu_updated(struct bt_conn *conn, uint16_t tx, uint16_t rx) +{ + if (conn != active_conn) { + return; + } + + LOG_INF("BLE serial MTU updated tx:%u rx:%u", tx, rx); +} + +static struct bt_gatt_cb gatt_callbacks = { + .att_mtu_updated = mtu_updated, +}; + +static size_t notify_payload_max(void) +{ + uint16_t mtu; + + if (active_conn == NULL) { + return 0U; + } + + mtu = bt_gatt_get_mtu(active_conn); + if (mtu <= BLE_SERIAL_NOTIFY_OVERHEAD) { + return 0U; + } + + return (size_t)(mtu - BLE_SERIAL_NOTIFY_OVERHEAD); +} + +static void reset_connection_state(void) +{ + active_conn = NULL; + secured = false; + tx_notify_enabled = false; +} + +static int module_init(void) +{ + bt_gatt_cb_register(&gatt_callbacks); + reset_connection_state(); + return 0; +} + +static int module_start(void) +{ + if (running) { + return 0; + } + + running = true; + return 0; +} + +static void module_pause(void) +{ + if (!running) { + return; + } + + running = false; + tx_notify_enabled = false; +} + +static bool handle_ble_peer_event(const struct ble_peer_event *event) +{ + switch (event->state) { + case PEER_STATE_CONNECTED: + if (active_conn != NULL) { + return false; + } + + active_conn = event->id; + secured = false; + tx_notify_enabled = false; + return false; + + case PEER_STATE_SECURED: + if (active_conn != event->id) { + return false; + } + + secured = true; + return false; + + case PEER_STATE_DISCONNECTED: + if (active_conn != event->id) { + return false; + } + + reset_connection_state(); + return false; + + default: + return false; + } +} + +static bool handle_ble_serial_tx_event(const struct ble_serial_tx_event *event) +{ + size_t payload_max; + int err; + + if (!running || !ble_ready || (active_conn == NULL) || !secured || !tx_notify_enabled) { + return false; + } + + payload_max = notify_payload_max(); + if (payload_max < BLE_SERIAL_MIN_PAYLOAD_LEN) { + LOG_WRN("BLE serial MTU payload too small:%u", (uint32_t)payload_max); + return false; + } + + if (event->dyndata.size > payload_max) { + LOG_WRN("BLE serial TX too large len:%u max:%u", + (uint32_t)event->dyndata.size, (uint32_t)payload_max); + return false; + } + + err = bt_gatt_notify(active_conn, &ble_serial_svc.attrs[4], + event->dyndata.data, + (uint16_t)event->dyndata.size); + if (err) { + LOG_WRN("bt_gatt_notify failed (%d)", err); + } + + return false; +} + +static bool app_event_handler(const struct app_event_header *aeh) +{ + if (is_ble_serial_tx_event(aeh)) { + return handle_ble_serial_tx_event(cast_ble_serial_tx_event(aeh)); + } + + if (is_ble_peer_event(aeh)) { + return handle_ble_peer_event(cast_ble_peer_event(aeh)); + } + + if (is_module_state_event(aeh)) { + const struct module_state_event *event = cast_module_state_event(aeh); + int err; + + if (check_state(event, MODULE_ID(main), MODULE_STATE_READY)) { + if (!initialized) { + err = module_init(); + if (err) { + module_set_state(MODULE_STATE_ERROR); + return false; + } + + initialized = true; + } + + err = module_start(); + if (err) { + module_set_state(MODULE_STATE_ERROR); + } + + return false; + } + + if (check_state(event, MODULE_ID(ble_state), MODULE_STATE_READY)) { + ble_ready = true; + if (running) { + module_set_state(MODULE_STATE_READY); + } + + return false; + } + + return false; + } + + if (is_power_down_event(aeh)) { + if (initialized) { + module_pause(); + module_set_state(MODULE_STATE_STANDBY); + } + + return false; + } + + if (is_wake_up_event(aeh)) { + if (initialized) { + int err = module_start(); + + if (err) { + module_set_state(MODULE_STATE_ERROR); + } else if (ble_ready) { + module_set_state(MODULE_STATE_READY); + } + } + + return false; + } + + return false; +} + +APP_EVENT_LISTENER(MODULE, app_event_handler); +APP_EVENT_SUBSCRIBE(MODULE, module_state_event); +APP_EVENT_SUBSCRIBE(MODULE, ble_serial_tx_event); +APP_EVENT_SUBSCRIBE_EARLY(MODULE, ble_peer_event); +APP_EVENT_SUBSCRIBE_EARLY(MODULE, power_down_event); +APP_EVENT_SUBSCRIBE(MODULE, wake_up_event); diff --git a/src/events/ble_serial_rx_event.c b/src/events/ble_serial_rx_event.c new file mode 100644 index 0000000..10a0b2d --- /dev/null +++ b/src/events/ble_serial_rx_event.c @@ -0,0 +1,73 @@ +#include +#include + +#include "ble_serial_rx_event.h" + +#define BLE_SERIAL_RX_EVENT_LOG_BUF_LEN 384 + +static void log_ble_serial_rx_event(const struct app_event_header *aeh) +{ + const struct ble_serial_rx_event *event = cast_ble_serial_rx_event(aeh); + char log_buf[BLE_SERIAL_RX_EVENT_LOG_BUF_LEN]; + int pos; + + pos = snprintf(log_buf, sizeof(log_buf), "len:%zu ascii:\"", + event->dyndata.size); + if ((pos < 0) || (pos >= sizeof(log_buf))) { + APP_EVENT_MANAGER_LOG(aeh, "log message preparation failure"); + return; + } + + for (size_t i = 0; i < event->dyndata.size; i++) { + int tmp = snprintf(&log_buf[pos], sizeof(log_buf) - pos, "%c", + isprint(event->dyndata.data[i]) ? + event->dyndata.data[i] : '.'); + + if ((tmp < 0) || ((pos + tmp) >= sizeof(log_buf))) { + APP_EVENT_MANAGER_LOG(aeh, "len:%zu ascii:\"...\"", + event->dyndata.size); + return; + } + + pos += tmp; + } + + pos += snprintf(&log_buf[pos], sizeof(log_buf) - pos, "\" hex:"); + if ((pos < 0) || (pos >= sizeof(log_buf))) { + APP_EVENT_MANAGER_LOG(aeh, "len:%zu ascii:\"...\"", + event->dyndata.size); + return; + } + + for (size_t i = 0; i < event->dyndata.size; i++) { + int tmp = snprintf(&log_buf[pos], sizeof(log_buf) - pos, " %02x", + event->dyndata.data[i]); + + if ((tmp < 0) || ((pos + tmp) >= sizeof(log_buf))) { + break; + } + + pos += tmp; + } + + APP_EVENT_MANAGER_LOG(aeh, "%s", log_buf); +} + +static void profile_ble_serial_rx_event(struct log_event_buf *buf, + const struct app_event_header *aeh) +{ + const struct ble_serial_rx_event *event = cast_ble_serial_rx_event(aeh); + + nrf_profiler_log_encode_uint8(buf, (uint8_t)event->dyndata.size); +} + +APP_EVENT_INFO_DEFINE(ble_serial_rx_event, + ENCODE(NRF_PROFILER_ARG_U8), + ENCODE("len"), + profile_ble_serial_rx_event); + +APP_EVENT_TYPE_DEFINE(ble_serial_rx_event, + log_ble_serial_rx_event, + &ble_serial_rx_event_info, + APP_EVENT_FLAGS_CREATE( + APP_EVENT_TYPE_FLAGS_INIT_LOG_ENABLE)); diff --git a/src/events/ble_serial_tx_event.c b/src/events/ble_serial_tx_event.c new file mode 100644 index 0000000..8e75ae6 --- /dev/null +++ b/src/events/ble_serial_tx_event.c @@ -0,0 +1,62 @@ +#include +#include + +#include "ble_serial_tx_event.h" + +#define BLE_SERIAL_TX_EVENT_LOG_BUF_LEN 256 + +static void log_ble_serial_tx_event(const struct app_event_header *aeh) +{ + const struct ble_serial_tx_event *event = cast_ble_serial_tx_event(aeh); + char log_buf[BLE_SERIAL_TX_EVENT_LOG_BUF_LEN]; + int pos; + + pos = snprintf(log_buf, sizeof(log_buf), "len:%zu ascii:\"", + event->dyndata.size); + if ((pos < 0) || (pos >= sizeof(log_buf))) { + APP_EVENT_MANAGER_LOG(aeh, "log message preparation failure"); + return; + } + + for (size_t i = 0; i < event->dyndata.size; i++) { + int tmp = snprintf(&log_buf[pos], sizeof(log_buf) - pos, "%c", + isprint(event->dyndata.data[i]) ? + event->dyndata.data[i] : '.'); + + if ((tmp < 0) || ((pos + tmp) >= sizeof(log_buf))) { + APP_EVENT_MANAGER_LOG(aeh, "len:%zu ascii:\"...\"", + event->dyndata.size); + return; + } + + pos += tmp; + } + + pos += snprintf(&log_buf[pos], sizeof(log_buf) - pos, "\""); + if ((pos < 0) || (pos >= sizeof(log_buf))) { + APP_EVENT_MANAGER_LOG(aeh, "len:%zu ascii:\"...\"", + event->dyndata.size); + return; + } + + APP_EVENT_MANAGER_LOG(aeh, "%s", log_buf); +} + +static void profile_ble_serial_tx_event(struct log_event_buf *buf, + const struct app_event_header *aeh) +{ + const struct ble_serial_tx_event *event = cast_ble_serial_tx_event(aeh); + + nrf_profiler_log_encode_uint8(buf, (uint8_t)event->dyndata.size); +} + +APP_EVENT_INFO_DEFINE(ble_serial_tx_event, + ENCODE(NRF_PROFILER_ARG_U8), + ENCODE("len"), + profile_ble_serial_tx_event); + +APP_EVENT_TYPE_DEFINE(ble_serial_tx_event, + log_ble_serial_tx_event, + &ble_serial_tx_event_info, + APP_EVENT_FLAGS_CREATE( + APP_EVENT_TYPE_FLAGS_INIT_LOG_ENABLE)); diff --git a/src/usb_cdc_test_module.c b/src/usb_cdc_test_module.c index 99b666a..4b4face 100644 --- a/src/usb_cdc_test_module.c +++ b/src/usb_cdc_test_module.c @@ -7,9 +7,12 @@ #include #include +#include + #include #include +#include "ble_serial_tx_event.h" #include "usb_cdc_tx_event.h" #include "usb_device_state_event.h" @@ -18,11 +21,13 @@ LOG_MODULE_REGISTER(MODULE, LOG_LEVEL_INF); #define USB_CDC_TEST_PERIOD K_SECONDS(1) static const uint8_t hello_message[] = "hello\r\n"; +static const uint8_t ble_hello_message[] = "ble_hello\r\n"; static struct k_work_delayable hello_work; static bool initialized; static bool running; static bool usb_active; +static bool ble_active; static void submit_hello_message(void) { @@ -33,15 +38,31 @@ static void submit_hello_message(void) APP_EVENT_SUBMIT(event); } +static void submit_ble_hello_message(void) +{ + struct ble_serial_tx_event *event = + new_ble_serial_tx_event(sizeof(ble_hello_message) - 1U); + + memcpy(event->dyndata.data, ble_hello_message, sizeof(ble_hello_message) - 1U); + APP_EVENT_SUBMIT(event); +} + static void hello_work_handler(struct k_work *work) { ARG_UNUSED(work); - if (!running || !usb_active) { + if (!running) { return; } - submit_hello_message(); + if (usb_active) { + submit_hello_message(); + } + + if (ble_active) { + submit_ble_hello_message(); + } + k_work_reschedule(&hello_work, USB_CDC_TEST_PERIOD); } @@ -58,7 +79,7 @@ static int module_start(void) } running = true; - if (usb_active) { + if (usb_active || ble_active) { k_work_reschedule(&hello_work, USB_CDC_TEST_PERIOD); } @@ -98,12 +119,56 @@ static bool handle_usb_device_state_event(const struct usb_device_state_event *e return false; } +static bool handle_ble_peer_event(const struct ble_peer_event *event) +{ + bool new_ble_active = ble_active; + + switch (event->state) { + case PEER_STATE_CONNECTED: + new_ble_active = false; + break; + + case PEER_STATE_SECURED: + new_ble_active = true; + break; + + case PEER_STATE_DISCONNECTED: + new_ble_active = false; + break; + + default: + return false; + } + + if (new_ble_active == ble_active) { + return false; + } + + ble_active = new_ble_active; + + if (!running) { + return false; + } + + if (usb_active || ble_active) { + k_work_reschedule(&hello_work, USB_CDC_TEST_PERIOD); + } else { + k_work_cancel_delayable(&hello_work); + } + + return false; +} + static bool app_event_handler(const struct app_event_header *aeh) { if (is_usb_device_state_event(aeh)) { return handle_usb_device_state_event(cast_usb_device_state_event(aeh)); } + if (is_ble_peer_event(aeh)) { + return handle_ble_peer_event(cast_ble_peer_event(aeh)); + } + if (is_module_state_event(aeh)) { const struct module_state_event *event = cast_module_state_event(aeh); @@ -159,6 +224,7 @@ static bool app_event_handler(const struct app_event_header *aeh) APP_EVENT_LISTENER(MODULE, app_event_handler); APP_EVENT_SUBSCRIBE(MODULE, module_state_event); +APP_EVENT_SUBSCRIBE(MODULE, ble_peer_event); APP_EVENT_SUBSCRIBE(MODULE, usb_device_state_event); APP_EVENT_SUBSCRIBE_EARLY(MODULE, power_down_event); APP_EVENT_SUBSCRIBE(MODULE, wake_up_event);