Compare commits
4 Commits
33fb416cfa
...
d86f0d6b78
| Author | SHA1 | Date | |
|---|---|---|---|
| d86f0d6b78 | |||
| 15307dfde5 | |||
| 227158870a | |||
| 39c6a1fe84 |
@@ -4,12 +4,19 @@ cmake_minimum_required(VERSION 3.20.0)
|
|||||||
find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE})
|
find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE})
|
||||||
project(blinky)
|
project(blinky)
|
||||||
|
|
||||||
|
list(APPEND CMAKE_MODULE_PATH ${ZEPHYR_BASE}/modules/nanopb)
|
||||||
|
include(nanopb)
|
||||||
|
|
||||||
zephyr_include_directories(
|
zephyr_include_directories(
|
||||||
inc
|
inc
|
||||||
inc/events
|
inc/events
|
||||||
)
|
)
|
||||||
add_subdirectory(drivers)
|
add_subdirectory(drivers)
|
||||||
|
|
||||||
|
zephyr_nanopb_sources(app
|
||||||
|
proto/device_comm.proto
|
||||||
|
)
|
||||||
|
|
||||||
target_sources(app PRIVATE
|
target_sources(app PRIVATE
|
||||||
src/main.c
|
src/main.c
|
||||||
src/battery_module.c
|
src/battery_module.c
|
||||||
@@ -17,16 +24,20 @@ target_sources(app PRIVATE
|
|||||||
src/ble_adv_uuid16.c
|
src/ble_adv_uuid16.c
|
||||||
src/ble_bas_module.c
|
src/ble_bas_module.c
|
||||||
src/ble_hid_module.c
|
src/ble_hid_module.c
|
||||||
|
src/ble_serial_module.c
|
||||||
src/display_module.c
|
src/display_module.c
|
||||||
src/encoder_module.c
|
src/encoder_module.c
|
||||||
src/hid_flowctrl_module.c
|
src/hid_flowctrl_module.c
|
||||||
src/keyboard_core_module.c
|
src/keyboard_core_module.c
|
||||||
src/ui/ui_main.c
|
src/ui/ui_main.c
|
||||||
|
src/cdc_wrapper_module.c
|
||||||
|
src/protocol_module.c
|
||||||
src/usb_cdc_module.c
|
src/usb_cdc_module.c
|
||||||
src/usb_cdc_test_module.c
|
|
||||||
src/usb_device_module.c
|
src/usb_device_module.c
|
||||||
src/usb_hid_module.c
|
src/usb_hid_module.c
|
||||||
src/events/bat_state_event.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/encoder_event.c
|
||||||
src/events/hid_led_event.c
|
src/events/hid_led_event.c
|
||||||
src/events/hid_report_sent_event.c
|
src/events/hid_report_sent_event.c
|
||||||
|
|||||||
12
inc/cdc_wrapper_module.h
Normal file
12
inc/cdc_wrapper_module.h
Normal file
@@ -0,0 +1,12 @@
|
|||||||
|
#ifndef BLINKY_CDC_WRAPPER_MODULE_H_
|
||||||
|
#define BLINKY_CDC_WRAPPER_MODULE_H_
|
||||||
|
|
||||||
|
#ifdef __cplusplus
|
||||||
|
extern "C" {
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#ifdef __cplusplus
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#endif /* BLINKY_CDC_WRAPPER_MODULE_H_ */
|
||||||
22
inc/events/ble_serial_rx_event.h
Normal file
22
inc/events/ble_serial_rx_event.h
Normal file
@@ -0,0 +1,22 @@
|
|||||||
|
#ifndef BLINKY_BLE_SERIAL_RX_EVENT_H_
|
||||||
|
#define BLINKY_BLE_SERIAL_RX_EVENT_H_
|
||||||
|
|
||||||
|
#include <app_event_manager.h>
|
||||||
|
#include <app_event_manager_profiler_tracer.h>
|
||||||
|
|
||||||
|
#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_ */
|
||||||
22
inc/events/ble_serial_tx_event.h
Normal file
22
inc/events/ble_serial_tx_event.h
Normal file
@@ -0,0 +1,22 @@
|
|||||||
|
#ifndef BLINKY_BLE_SERIAL_TX_EVENT_H_
|
||||||
|
#define BLINKY_BLE_SERIAL_TX_EVENT_H_
|
||||||
|
|
||||||
|
#include <app_event_manager.h>
|
||||||
|
#include <app_event_manager_profiler_tracer.h>
|
||||||
|
|
||||||
|
#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_ */
|
||||||
26
inc/protocol_module.h
Normal file
26
inc/protocol_module.h
Normal file
@@ -0,0 +1,26 @@
|
|||||||
|
#ifndef BLINKY_PROTOCOL_MODULE_H_
|
||||||
|
#define BLINKY_PROTOCOL_MODULE_H_
|
||||||
|
|
||||||
|
#include <stddef.h>
|
||||||
|
#include <stdint.h>
|
||||||
|
|
||||||
|
#ifdef __cplusplus
|
||||||
|
extern "C" {
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#define CDC_PROTO_TYPE_HELLO_REQ 0x01U
|
||||||
|
#define CDC_PROTO_TYPE_HELLO_RSP 0x02U
|
||||||
|
|
||||||
|
int protocol_module_process_cdc_packet(uint8_t req_type,
|
||||||
|
const uint8_t *req_payload,
|
||||||
|
size_t req_payload_len,
|
||||||
|
uint8_t *rsp_type,
|
||||||
|
uint8_t *rsp_payload,
|
||||||
|
size_t rsp_payload_buf_size,
|
||||||
|
size_t *rsp_payload_len);
|
||||||
|
|
||||||
|
#ifdef __cplusplus
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#endif /* BLINKY_PROTOCOL_MODULE_H_ */
|
||||||
4
prj.conf
4
prj.conf
@@ -34,6 +34,7 @@ CONFIG_SERIAL=y
|
|||||||
CONFIG_UART_INTERRUPT_DRIVEN=y
|
CONFIG_UART_INTERRUPT_DRIVEN=y
|
||||||
CONFIG_UART_LINE_CTRL=y
|
CONFIG_UART_LINE_CTRL=y
|
||||||
CONFIG_UART_USE_RUNTIME_CONFIGURE=y
|
CONFIG_UART_USE_RUNTIME_CONFIGURE=y
|
||||||
|
CONFIG_NANOPB=y
|
||||||
CONFIG_USBD_HID_SUPPORT=y
|
CONFIG_USBD_HID_SUPPORT=y
|
||||||
CONFIG_USBD_CDC_ACM_CLASS=y
|
CONFIG_USBD_CDC_ACM_CLASS=y
|
||||||
CONFIG_CDC_ACM_SERIAL_INITIALIZE_AT_BOOT=n
|
CONFIG_CDC_ACM_SERIAL_INITIALIZE_AT_BOOT=n
|
||||||
@@ -47,6 +48,9 @@ CONFIG_BT_SETTINGS=y
|
|||||||
CONFIG_BT_MAX_CONN=1
|
CONFIG_BT_MAX_CONN=1
|
||||||
CONFIG_BT_MAX_PAIRED=1
|
CONFIG_BT_MAX_PAIRED=1
|
||||||
CONFIG_BT_ATT_TX_COUNT=5
|
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_CONN_CTX=y
|
||||||
CONFIG_BT_DEVICE_NAME="WH Mini Keyboard"
|
CONFIG_BT_DEVICE_NAME="WH Mini Keyboard"
|
||||||
CONFIG_BT_DEVICE_APPEARANCE=961
|
CONFIG_BT_DEVICE_APPEARANCE=961
|
||||||
|
|||||||
21
proto/device_comm.proto
Normal file
21
proto/device_comm.proto
Normal file
@@ -0,0 +1,21 @@
|
|||||||
|
syntax = "proto3";
|
||||||
|
|
||||||
|
message HelloReq {
|
||||||
|
uint32 protocol_version = 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
message HelloRsp {
|
||||||
|
uint32 protocol_version = 1;
|
||||||
|
uint32 vendor_id = 2;
|
||||||
|
uint32 product_id = 3;
|
||||||
|
uint32 firmware_major = 4;
|
||||||
|
uint32 firmware_minor = 5;
|
||||||
|
uint32 capability_flags = 6;
|
||||||
|
}
|
||||||
|
|
||||||
|
message CdcPacketBody {
|
||||||
|
oneof body {
|
||||||
|
HelloReq hello_req = 1;
|
||||||
|
HelloRsp hello_rsp = 2;
|
||||||
|
}
|
||||||
|
}
|
||||||
308
src/ble_serial_module.c
Normal file
308
src/ble_serial_module.c
Normal file
@@ -0,0 +1,308 @@
|
|||||||
|
#include <errno.h>
|
||||||
|
#include <stdbool.h>
|
||||||
|
#include <stdint.h>
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
|
#include <app_event_manager.h>
|
||||||
|
|
||||||
|
#define MODULE ble_serial_module
|
||||||
|
#include <caf/events/module_state_event.h>
|
||||||
|
#include <caf/events/power_event.h>
|
||||||
|
|
||||||
|
#include <caf/events/ble_common_event.h>
|
||||||
|
#include <zephyr/bluetooth/att.h>
|
||||||
|
#include <zephyr/bluetooth/conn.h>
|
||||||
|
#include <zephyr/bluetooth/gatt.h>
|
||||||
|
#include <zephyr/bluetooth/uuid.h>
|
||||||
|
#include <zephyr/logging/log.h>
|
||||||
|
|
||||||
|
#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);
|
||||||
275
src/cdc_wrapper_module.c
Normal file
275
src/cdc_wrapper_module.c
Normal file
@@ -0,0 +1,275 @@
|
|||||||
|
#include <errno.h>
|
||||||
|
#include <stdbool.h>
|
||||||
|
#include <stddef.h>
|
||||||
|
#include <stdint.h>
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
|
#include <app_event_manager.h>
|
||||||
|
|
||||||
|
#define MODULE cdc_wrapper_module
|
||||||
|
#include <caf/events/module_state_event.h>
|
||||||
|
#include <caf/events/power_event.h>
|
||||||
|
|
||||||
|
#include <zephyr/logging/log.h>
|
||||||
|
|
||||||
|
#include "protocol_module.h"
|
||||||
|
#include "usb_cdc_rx_event.h"
|
||||||
|
#include "usb_cdc_tx_event.h"
|
||||||
|
|
||||||
|
LOG_MODULE_REGISTER(MODULE, LOG_LEVEL_INF);
|
||||||
|
|
||||||
|
#define CDC_WRAPPER_HEAD1 0xAAU
|
||||||
|
#define CDC_WRAPPER_HEAD2 0x55U
|
||||||
|
#define CDC_WRAPPER_MAX_PAYLOAD_LEN 64U
|
||||||
|
#define CDC_WRAPPER_MAX_FRAME_LEN (2U + 1U + 1U + CDC_WRAPPER_MAX_PAYLOAD_LEN + 1U)
|
||||||
|
|
||||||
|
enum frame_parse_state {
|
||||||
|
FRAME_PARSE_HEAD1,
|
||||||
|
FRAME_PARSE_HEAD2,
|
||||||
|
FRAME_PARSE_LEN,
|
||||||
|
FRAME_PARSE_TYPE,
|
||||||
|
FRAME_PARSE_PAYLOAD,
|
||||||
|
FRAME_PARSE_CHECKSUM,
|
||||||
|
};
|
||||||
|
|
||||||
|
struct cdc_frame_parser {
|
||||||
|
enum frame_parse_state state;
|
||||||
|
uint8_t len;
|
||||||
|
uint8_t type;
|
||||||
|
uint8_t checksum;
|
||||||
|
uint8_t payload[CDC_WRAPPER_MAX_PAYLOAD_LEN];
|
||||||
|
size_t payload_pos;
|
||||||
|
};
|
||||||
|
|
||||||
|
static bool initialized;
|
||||||
|
static bool running;
|
||||||
|
static struct cdc_frame_parser parser;
|
||||||
|
|
||||||
|
static void parser_reset(void)
|
||||||
|
{
|
||||||
|
parser.state = FRAME_PARSE_HEAD1;
|
||||||
|
parser.len = 0U;
|
||||||
|
parser.type = 0U;
|
||||||
|
parser.checksum = 0U;
|
||||||
|
parser.payload_pos = 0U;
|
||||||
|
}
|
||||||
|
|
||||||
|
static uint8_t frame_checksum(uint8_t len, uint8_t type,
|
||||||
|
const uint8_t *payload, size_t payload_len)
|
||||||
|
{
|
||||||
|
uint8_t checksum = CDC_WRAPPER_HEAD1 ^ CDC_WRAPPER_HEAD2 ^ len ^ type;
|
||||||
|
|
||||||
|
for (size_t i = 0; i < payload_len; i++) {
|
||||||
|
checksum ^= payload[i];
|
||||||
|
}
|
||||||
|
|
||||||
|
return checksum;
|
||||||
|
}
|
||||||
|
|
||||||
|
static void submit_tx_frame(uint8_t type, const uint8_t *payload, size_t payload_len)
|
||||||
|
{
|
||||||
|
struct usb_cdc_tx_event *event;
|
||||||
|
size_t frame_len = 2U + 1U + 1U + payload_len + 1U;
|
||||||
|
|
||||||
|
event = new_usb_cdc_tx_event(frame_len);
|
||||||
|
event->dyndata.data[0] = CDC_WRAPPER_HEAD1;
|
||||||
|
event->dyndata.data[1] = CDC_WRAPPER_HEAD2;
|
||||||
|
event->dyndata.data[2] = (uint8_t)payload_len;
|
||||||
|
event->dyndata.data[3] = type;
|
||||||
|
memcpy(&event->dyndata.data[4], payload, payload_len);
|
||||||
|
event->dyndata.data[4U + payload_len] =
|
||||||
|
frame_checksum((uint8_t)payload_len, type, payload, payload_len);
|
||||||
|
APP_EVENT_SUBMIT(event);
|
||||||
|
}
|
||||||
|
|
||||||
|
static void process_complete_frame(void)
|
||||||
|
{
|
||||||
|
uint8_t rsp_type;
|
||||||
|
uint8_t rsp_payload[CDC_WRAPPER_MAX_PAYLOAD_LEN];
|
||||||
|
size_t rsp_payload_len = 0U;
|
||||||
|
int err;
|
||||||
|
|
||||||
|
err = protocol_module_process_cdc_packet(parser.type,
|
||||||
|
parser.payload,
|
||||||
|
parser.payload_pos,
|
||||||
|
&rsp_type,
|
||||||
|
rsp_payload,
|
||||||
|
sizeof(rsp_payload),
|
||||||
|
&rsp_payload_len);
|
||||||
|
if (err == -ENOTSUP) {
|
||||||
|
LOG_WRN("Ignore unsupported CDC packet type:0x%02x", parser.type);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (err) {
|
||||||
|
LOG_WRN("Protocol processing failed (%d)", err);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
LOG_INF("CDC HelloRsp encoded len:%u", (uint32_t)rsp_payload_len);
|
||||||
|
submit_tx_frame(rsp_type, rsp_payload, rsp_payload_len);
|
||||||
|
}
|
||||||
|
|
||||||
|
static void consume_byte(uint8_t byte)
|
||||||
|
{
|
||||||
|
switch (parser.state) {
|
||||||
|
case FRAME_PARSE_HEAD1:
|
||||||
|
if (byte == CDC_WRAPPER_HEAD1) {
|
||||||
|
parser.state = FRAME_PARSE_HEAD2;
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
|
||||||
|
case FRAME_PARSE_HEAD2:
|
||||||
|
if (byte == CDC_WRAPPER_HEAD2) {
|
||||||
|
parser.state = FRAME_PARSE_LEN;
|
||||||
|
} else if (byte != CDC_WRAPPER_HEAD1) {
|
||||||
|
parser.state = FRAME_PARSE_HEAD1;
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
|
||||||
|
case FRAME_PARSE_LEN:
|
||||||
|
if (byte > CDC_WRAPPER_MAX_PAYLOAD_LEN) {
|
||||||
|
LOG_WRN("Drop CDC frame with invalid len:%u", byte);
|
||||||
|
parser_reset();
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
parser.len = byte;
|
||||||
|
parser.payload_pos = 0U;
|
||||||
|
parser.state = FRAME_PARSE_TYPE;
|
||||||
|
break;
|
||||||
|
|
||||||
|
case FRAME_PARSE_TYPE:
|
||||||
|
parser.type = byte;
|
||||||
|
parser.state = (parser.len == 0U) ? FRAME_PARSE_CHECKSUM :
|
||||||
|
FRAME_PARSE_PAYLOAD;
|
||||||
|
break;
|
||||||
|
|
||||||
|
case FRAME_PARSE_PAYLOAD:
|
||||||
|
parser.payload[parser.payload_pos++] = byte;
|
||||||
|
if (parser.payload_pos >= parser.len) {
|
||||||
|
parser.state = FRAME_PARSE_CHECKSUM;
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
|
||||||
|
case FRAME_PARSE_CHECKSUM:
|
||||||
|
if (byte != frame_checksum(parser.len, parser.type,
|
||||||
|
parser.payload, parser.payload_pos)) {
|
||||||
|
LOG_WRN("Drop CDC frame with invalid checksum");
|
||||||
|
parser_reset();
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
process_complete_frame();
|
||||||
|
parser_reset();
|
||||||
|
break;
|
||||||
|
|
||||||
|
default:
|
||||||
|
parser_reset();
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static bool handle_usb_cdc_rx_event(const struct usb_cdc_rx_event *event)
|
||||||
|
{
|
||||||
|
if (!running) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
for (size_t i = 0; i < event->dyndata.size; i++) {
|
||||||
|
consume_byte(event->dyndata.data[i]);
|
||||||
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
static int module_init(void)
|
||||||
|
{
|
||||||
|
parser_reset();
|
||||||
|
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;
|
||||||
|
parser_reset();
|
||||||
|
}
|
||||||
|
|
||||||
|
static bool app_event_handler(const struct app_event_header *aeh)
|
||||||
|
{
|
||||||
|
if (is_usb_cdc_rx_event(aeh)) {
|
||||||
|
return handle_usb_cdc_rx_event(cast_usb_cdc_rx_event(aeh));
|
||||||
|
}
|
||||||
|
|
||||||
|
if (is_module_state_event(aeh)) {
|
||||||
|
const struct module_state_event *event = cast_module_state_event(aeh);
|
||||||
|
|
||||||
|
if (check_state(event, MODULE_ID(main), MODULE_STATE_READY)) {
|
||||||
|
int err;
|
||||||
|
|
||||||
|
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);
|
||||||
|
} else {
|
||||||
|
module_set_state(MODULE_STATE_READY);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
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 {
|
||||||
|
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, usb_cdc_rx_event);
|
||||||
|
APP_EVENT_SUBSCRIBE_EARLY(MODULE, power_down_event);
|
||||||
|
APP_EVENT_SUBSCRIBE(MODULE, wake_up_event);
|
||||||
73
src/events/ble_serial_rx_event.c
Normal file
73
src/events/ble_serial_rx_event.c
Normal file
@@ -0,0 +1,73 @@
|
|||||||
|
#include <ctype.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
|
||||||
|
#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));
|
||||||
62
src/events/ble_serial_tx_event.c
Normal file
62
src/events/ble_serial_tx_event.c
Normal file
@@ -0,0 +1,62 @@
|
|||||||
|
#include <ctype.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
|
||||||
|
#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));
|
||||||
133
src/protocol_module.c
Normal file
133
src/protocol_module.c
Normal file
@@ -0,0 +1,133 @@
|
|||||||
|
#include <errno.h>
|
||||||
|
#include <stdbool.h>
|
||||||
|
#include <stddef.h>
|
||||||
|
#include <stdint.h>
|
||||||
|
|
||||||
|
#include <zephyr/logging/log.h>
|
||||||
|
|
||||||
|
#include <pb_decode.h>
|
||||||
|
#include <pb_encode.h>
|
||||||
|
|
||||||
|
#include <proto/device_comm.pb.h>
|
||||||
|
|
||||||
|
#include "protocol_module.h"
|
||||||
|
|
||||||
|
LOG_MODULE_REGISTER(protocol_module, LOG_LEVEL_INF);
|
||||||
|
|
||||||
|
#define PROTOCOL_VERSION 1U
|
||||||
|
#define PROTOCOL_VENDOR_ID 0x1915U
|
||||||
|
#define PROTOCOL_PRODUCT_ID 0x52F0U
|
||||||
|
#define PROTOCOL_FIRMWARE_MAJOR 0U
|
||||||
|
#define PROTOCOL_FIRMWARE_MINOR 0U
|
||||||
|
#define PROTOCOL_CAPABILITY_FLAGS 0U
|
||||||
|
|
||||||
|
static bool type_matches_body(uint8_t type, const CdcPacketBody *body)
|
||||||
|
{
|
||||||
|
switch (type) {
|
||||||
|
case CDC_PROTO_TYPE_HELLO_REQ:
|
||||||
|
return body->which_body == CdcPacketBody_hello_req_tag;
|
||||||
|
case CDC_PROTO_TYPE_HELLO_RSP:
|
||||||
|
return body->which_body == CdcPacketBody_hello_rsp_tag;
|
||||||
|
default:
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static int decode_body(const uint8_t *payload, size_t payload_len,
|
||||||
|
CdcPacketBody *body)
|
||||||
|
{
|
||||||
|
pb_istream_t stream;
|
||||||
|
|
||||||
|
if ((payload == NULL) || (body == NULL)) {
|
||||||
|
return -EINVAL;
|
||||||
|
}
|
||||||
|
|
||||||
|
*body = (CdcPacketBody)CdcPacketBody_init_zero;
|
||||||
|
stream = pb_istream_from_buffer(payload, payload_len);
|
||||||
|
|
||||||
|
if (!pb_decode(&stream, CdcPacketBody_fields, body)) {
|
||||||
|
LOG_WRN("pb_decode failed: %s", PB_GET_ERROR(&stream));
|
||||||
|
return -EBADMSG;
|
||||||
|
}
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
static int encode_hello_rsp(uint8_t *rsp_payload, size_t rsp_payload_buf_size,
|
||||||
|
size_t *rsp_payload_len)
|
||||||
|
{
|
||||||
|
CdcPacketBody body = CdcPacketBody_init_zero;
|
||||||
|
pb_ostream_t stream;
|
||||||
|
|
||||||
|
if ((rsp_payload == NULL) || (rsp_payload_len == NULL)) {
|
||||||
|
return -EINVAL;
|
||||||
|
}
|
||||||
|
|
||||||
|
body.which_body = CdcPacketBody_hello_rsp_tag;
|
||||||
|
body.body.hello_rsp.protocol_version = PROTOCOL_VERSION;
|
||||||
|
body.body.hello_rsp.vendor_id = PROTOCOL_VENDOR_ID;
|
||||||
|
body.body.hello_rsp.product_id = PROTOCOL_PRODUCT_ID;
|
||||||
|
body.body.hello_rsp.firmware_major = PROTOCOL_FIRMWARE_MAJOR;
|
||||||
|
body.body.hello_rsp.firmware_minor = PROTOCOL_FIRMWARE_MINOR;
|
||||||
|
body.body.hello_rsp.capability_flags = PROTOCOL_CAPABILITY_FLAGS;
|
||||||
|
|
||||||
|
stream = pb_ostream_from_buffer(rsp_payload, rsp_payload_buf_size);
|
||||||
|
|
||||||
|
if (!pb_encode(&stream, CdcPacketBody_fields, &body)) {
|
||||||
|
LOG_WRN("pb_encode failed: %s", PB_GET_ERROR(&stream));
|
||||||
|
return -EIO;
|
||||||
|
}
|
||||||
|
|
||||||
|
*rsp_payload_len = stream.bytes_written;
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
int protocol_module_process_cdc_packet(uint8_t req_type,
|
||||||
|
const uint8_t *req_payload,
|
||||||
|
size_t req_payload_len,
|
||||||
|
uint8_t *rsp_type,
|
||||||
|
uint8_t *rsp_payload,
|
||||||
|
size_t rsp_payload_buf_size,
|
||||||
|
size_t *rsp_payload_len)
|
||||||
|
{
|
||||||
|
CdcPacketBody body;
|
||||||
|
int err;
|
||||||
|
|
||||||
|
if ((rsp_type == NULL) || (rsp_payload == NULL) || (rsp_payload_len == NULL)) {
|
||||||
|
return -EINVAL;
|
||||||
|
}
|
||||||
|
|
||||||
|
err = decode_body(req_payload, req_payload_len, &body);
|
||||||
|
if (err) {
|
||||||
|
return err;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!type_matches_body(req_type, &body)) {
|
||||||
|
LOG_WRN("CDC type/body mismatch type:0x%02x body_case:%d",
|
||||||
|
req_type, body.which_body);
|
||||||
|
return -EBADMSG;
|
||||||
|
}
|
||||||
|
|
||||||
|
switch (req_type) {
|
||||||
|
case CDC_PROTO_TYPE_HELLO_REQ:
|
||||||
|
LOG_INF("HelloReq protocol_version:%u",
|
||||||
|
body.body.hello_req.protocol_version);
|
||||||
|
|
||||||
|
if (body.body.hello_req.protocol_version != PROTOCOL_VERSION) {
|
||||||
|
LOG_WRN("Unexpected protocol version:%u",
|
||||||
|
body.body.hello_req.protocol_version);
|
||||||
|
}
|
||||||
|
|
||||||
|
err = encode_hello_rsp(rsp_payload, rsp_payload_buf_size, rsp_payload_len);
|
||||||
|
if (err) {
|
||||||
|
return err;
|
||||||
|
}
|
||||||
|
|
||||||
|
*rsp_type = CDC_PROTO_TYPE_HELLO_RSP;
|
||||||
|
return 0;
|
||||||
|
|
||||||
|
default:
|
||||||
|
LOG_WRN("Unsupported CDC protocol type:0x%02x", req_type);
|
||||||
|
return -ENOTSUP;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -28,7 +28,6 @@ LOG_MODULE_REGISTER(MODULE, LOG_LEVEL_INF);
|
|||||||
#define USB_CDC_RX_RING_BUF_SIZE 256
|
#define USB_CDC_RX_RING_BUF_SIZE 256
|
||||||
#define USB_CDC_TX_RING_BUF_SIZE 256
|
#define USB_CDC_TX_RING_BUF_SIZE 256
|
||||||
#define USB_CDC_RX_CHUNK_SIZE 32
|
#define USB_CDC_RX_CHUNK_SIZE 32
|
||||||
#define USB_CDC_MAX_LINE_SIZE 96
|
|
||||||
#define USB_CDC_CONTROL_POLL_INTERVAL K_MSEC(100)
|
#define USB_CDC_CONTROL_POLL_INTERVAL K_MSEC(100)
|
||||||
#define USB_CDC_EXPECTED_BAUDRATE 115200U
|
#define USB_CDC_EXPECTED_BAUDRATE 115200U
|
||||||
|
|
||||||
@@ -46,9 +45,6 @@ static bool usb_active;
|
|||||||
static bool usb_function_prepared;
|
static bool usb_function_prepared;
|
||||||
static bool dtr_ready;
|
static bool dtr_ready;
|
||||||
static bool rx_enabled;
|
static bool rx_enabled;
|
||||||
static bool rx_line_overflow;
|
|
||||||
static uint8_t rx_line_buf[USB_CDC_MAX_LINE_SIZE];
|
|
||||||
static size_t rx_line_len;
|
|
||||||
|
|
||||||
static void submit_usb_cdc_rx_event(const uint8_t *data, size_t len)
|
static void submit_usb_cdc_rx_event(const uint8_t *data, size_t len)
|
||||||
{
|
{
|
||||||
@@ -72,8 +68,6 @@ static void reset_ring_buffers(void)
|
|||||||
|
|
||||||
ring_buf_init(&rx_ringbuf, sizeof(rx_ring_buffer), rx_ring_buffer);
|
ring_buf_init(&rx_ringbuf, sizeof(rx_ring_buffer), rx_ring_buffer);
|
||||||
ring_buf_init(&tx_ringbuf, sizeof(tx_ring_buffer), tx_ring_buffer);
|
ring_buf_init(&tx_ringbuf, sizeof(tx_ring_buffer), tx_ring_buffer);
|
||||||
rx_line_len = 0U;
|
|
||||||
rx_line_overflow = false;
|
|
||||||
|
|
||||||
irq_unlock(key);
|
irq_unlock(key);
|
||||||
}
|
}
|
||||||
@@ -134,36 +128,6 @@ static void validate_line_coding(void)
|
|||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
static void process_rx_byte(uint8_t byte)
|
|
||||||
{
|
|
||||||
if ((byte == '\r') || (byte == '\n')) {
|
|
||||||
if (rx_line_overflow) {
|
|
||||||
LOG_WRN("Drop oversized CDC line");
|
|
||||||
rx_line_overflow = false;
|
|
||||||
rx_line_len = 0U;
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (rx_line_len > 0U) {
|
|
||||||
submit_usb_cdc_rx_event(rx_line_buf, rx_line_len);
|
|
||||||
rx_line_len = 0U;
|
|
||||||
}
|
|
||||||
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (rx_line_overflow) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (rx_line_len >= sizeof(rx_line_buf)) {
|
|
||||||
rx_line_overflow = true;
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
rx_line_buf[rx_line_len++] = byte;
|
|
||||||
}
|
|
||||||
|
|
||||||
static void rx_work_handler(struct k_work *work)
|
static void rx_work_handler(struct k_work *work)
|
||||||
{
|
{
|
||||||
uint8_t buffer[USB_CDC_RX_CHUNK_SIZE];
|
uint8_t buffer[USB_CDC_RX_CHUNK_SIZE];
|
||||||
@@ -181,9 +145,7 @@ static void rx_work_handler(struct k_work *work)
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
for (uint32_t i = 0; i < len; i++) {
|
submit_usb_cdc_rx_event(buffer, len);
|
||||||
process_rx_byte(buffer[i]);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -7,9 +7,12 @@
|
|||||||
#include <caf/events/module_state_event.h>
|
#include <caf/events/module_state_event.h>
|
||||||
#include <caf/events/power_event.h>
|
#include <caf/events/power_event.h>
|
||||||
|
|
||||||
|
#include <caf/events/ble_common_event.h>
|
||||||
|
|
||||||
#include <zephyr/kernel.h>
|
#include <zephyr/kernel.h>
|
||||||
#include <zephyr/logging/log.h>
|
#include <zephyr/logging/log.h>
|
||||||
|
|
||||||
|
#include "ble_serial_tx_event.h"
|
||||||
#include "usb_cdc_tx_event.h"
|
#include "usb_cdc_tx_event.h"
|
||||||
#include "usb_device_state_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)
|
#define USB_CDC_TEST_PERIOD K_SECONDS(1)
|
||||||
|
|
||||||
static const uint8_t hello_message[] = "hello\r\n";
|
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 struct k_work_delayable hello_work;
|
||||||
static bool initialized;
|
static bool initialized;
|
||||||
static bool running;
|
static bool running;
|
||||||
static bool usb_active;
|
static bool usb_active;
|
||||||
|
static bool ble_active;
|
||||||
|
|
||||||
static void submit_hello_message(void)
|
static void submit_hello_message(void)
|
||||||
{
|
{
|
||||||
@@ -33,15 +38,31 @@ static void submit_hello_message(void)
|
|||||||
APP_EVENT_SUBMIT(event);
|
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)
|
static void hello_work_handler(struct k_work *work)
|
||||||
{
|
{
|
||||||
ARG_UNUSED(work);
|
ARG_UNUSED(work);
|
||||||
|
|
||||||
if (!running || !usb_active) {
|
if (!running) {
|
||||||
return;
|
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);
|
k_work_reschedule(&hello_work, USB_CDC_TEST_PERIOD);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -58,7 +79,7 @@ static int module_start(void)
|
|||||||
}
|
}
|
||||||
|
|
||||||
running = true;
|
running = true;
|
||||||
if (usb_active) {
|
if (usb_active || ble_active) {
|
||||||
k_work_reschedule(&hello_work, USB_CDC_TEST_PERIOD);
|
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;
|
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)
|
static bool app_event_handler(const struct app_event_header *aeh)
|
||||||
{
|
{
|
||||||
if (is_usb_device_state_event(aeh)) {
|
if (is_usb_device_state_event(aeh)) {
|
||||||
return handle_usb_device_state_event(cast_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)) {
|
if (is_module_state_event(aeh)) {
|
||||||
const struct module_state_event *event = cast_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_LISTENER(MODULE, app_event_handler);
|
||||||
APP_EVENT_SUBSCRIBE(MODULE, module_state_event);
|
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(MODULE, usb_device_state_event);
|
||||||
APP_EVENT_SUBSCRIBE_EARLY(MODULE, power_down_event);
|
APP_EVENT_SUBSCRIBE_EARLY(MODULE, power_down_event);
|
||||||
APP_EVENT_SUBSCRIBE(MODULE, wake_up_event);
|
APP_EVENT_SUBSCRIBE(MODULE, wake_up_event);
|
||||||
|
|||||||
@@ -11,6 +11,8 @@
|
|||||||
#include <zephyr/logging/log.h>
|
#include <zephyr/logging/log.h>
|
||||||
#include <zephyr/usb/usbd.h>
|
#include <zephyr/usb/usbd.h>
|
||||||
|
|
||||||
|
#include <caf/events/power_manager_event.h>
|
||||||
|
|
||||||
#include "usb_device_module.h"
|
#include "usb_device_module.h"
|
||||||
#include "usb_device_state_event.h"
|
#include "usb_device_state_event.h"
|
||||||
#include "usb_function_ready_event.h"
|
#include "usb_function_ready_event.h"
|
||||||
@@ -71,6 +73,13 @@ static void update_usb_device_state(enum usb_device_state state)
|
|||||||
submit_usb_device_state_event(state);
|
submit_usb_device_state_event(state);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static void update_power_manager_restriction(bool vbus_present)
|
||||||
|
{
|
||||||
|
power_manager_restrict(MODULE_IDX(MODULE),
|
||||||
|
vbus_present ? POWER_MANAGER_LEVEL_ALIVE :
|
||||||
|
POWER_MANAGER_LEVEL_SUSPENDED);
|
||||||
|
}
|
||||||
|
|
||||||
static int usb_descriptors_init(void)
|
static int usb_descriptors_init(void)
|
||||||
{
|
{
|
||||||
int err;
|
int err;
|
||||||
@@ -112,6 +121,7 @@ static void usbd_msg_cb(struct usbd_context *const usbd_ctx,
|
|||||||
|
|
||||||
switch (msg->type) {
|
switch (msg->type) {
|
||||||
case USBD_MSG_VBUS_READY:
|
case USBD_MSG_VBUS_READY:
|
||||||
|
update_power_manager_restriction(true);
|
||||||
if (!usb_enabled) {
|
if (!usb_enabled) {
|
||||||
int err = usbd_enable(&blinky_usbd);
|
int err = usbd_enable(&blinky_usbd);
|
||||||
|
|
||||||
@@ -125,6 +135,7 @@ static void usbd_msg_cb(struct usbd_context *const usbd_ctx,
|
|||||||
break;
|
break;
|
||||||
|
|
||||||
case USBD_MSG_VBUS_REMOVED:
|
case USBD_MSG_VBUS_REMOVED:
|
||||||
|
update_power_manager_restriction(false);
|
||||||
if (usb_enabled) {
|
if (usb_enabled) {
|
||||||
int err = usbd_disable(&blinky_usbd);
|
int err = usbd_disable(&blinky_usbd);
|
||||||
|
|
||||||
@@ -207,6 +218,7 @@ static int module_init(void)
|
|||||||
ready_function_mask = 0U;
|
ready_function_mask = 0U;
|
||||||
prepare_broadcasted = false;
|
prepare_broadcasted = false;
|
||||||
usbd_initialized = false;
|
usbd_initialized = false;
|
||||||
|
update_power_manager_restriction(false);
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user