2026-04-11 18:21:18 +08:00
|
|
|
#include <errno.h>
|
|
|
|
|
#include <stdbool.h>
|
|
|
|
|
#include <stddef.h>
|
|
|
|
|
#include <stdint.h>
|
2026-04-13 11:55:59 +08:00
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
|
|
#include <app_event_manager.h>
|
|
|
|
|
|
|
|
|
|
#define MODULE protocol_module
|
|
|
|
|
#include <caf/events/module_state_event.h>
|
|
|
|
|
#include <caf/events/power_event.h>
|
2026-04-11 18:21:18 +08:00
|
|
|
|
|
|
|
|
#include <zephyr/logging/log.h>
|
2026-04-13 11:55:59 +08:00
|
|
|
#include <zephyr/sys/util.h>
|
2026-04-11 18:21:18 +08:00
|
|
|
|
|
|
|
|
#include <pb_decode.h>
|
|
|
|
|
#include <pb_encode.h>
|
|
|
|
|
|
|
|
|
|
#include <proto/device_comm.pb.h>
|
|
|
|
|
|
2026-04-15 10:23:12 +08:00
|
|
|
#include "proto_rx_event.h"
|
|
|
|
|
#include "proto_tx_event.h"
|
2026-04-11 18:21:18 +08:00
|
|
|
#include "protocol_module.h"
|
|
|
|
|
|
2026-04-13 11:55:59 +08:00
|
|
|
LOG_MODULE_REGISTER(MODULE, LOG_LEVEL_INF);
|
2026-04-11 18:21:18 +08:00
|
|
|
|
|
|
|
|
#define PROTOCOL_VERSION 1U
|
|
|
|
|
#define PROTOCOL_VENDOR_ID 0x1915U
|
|
|
|
|
#define PROTOCOL_PRODUCT_ID 0x52F0U
|
|
|
|
|
#define PROTOCOL_FIRMWARE_MAJOR 0U
|
|
|
|
|
#define PROTOCOL_FIRMWARE_MINOR 0U
|
2026-04-15 10:23:12 +08:00
|
|
|
#define PROTOCOL_CAPABILITY_FLAGS 0U
|
|
|
|
|
#define PROTOCOL_MAX_MSG_LEN 128U
|
2026-04-13 11:55:59 +08:00
|
|
|
|
|
|
|
|
static bool initialized;
|
|
|
|
|
static bool running;
|
2026-04-15 10:23:12 +08:00
|
|
|
static bool hello_done[PROTO_TRANSPORT_COUNT];
|
2026-04-11 18:21:18 +08:00
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-13 11:55:59 +08:00
|
|
|
static int encode_body(const CdcPacketBody *body, uint8_t *payload,
|
|
|
|
|
size_t payload_buf_size, size_t *payload_len)
|
2026-04-11 18:21:18 +08:00
|
|
|
{
|
|
|
|
|
pb_ostream_t stream;
|
|
|
|
|
|
2026-04-13 11:55:59 +08:00
|
|
|
if ((body == NULL) || (payload == NULL) || (payload_len == NULL)) {
|
2026-04-11 18:21:18 +08:00
|
|
|
return -EINVAL;
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-13 11:55:59 +08:00
|
|
|
stream = pb_ostream_from_buffer(payload, payload_buf_size);
|
|
|
|
|
if (!pb_encode(&stream, CdcPacketBody_fields, body)) {
|
|
|
|
|
LOG_WRN("pb_encode failed: %s", PB_GET_ERROR(&stream));
|
|
|
|
|
return -EIO;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
*payload_len = stream.bytes_written;
|
|
|
|
|
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;
|
|
|
|
|
|
2026-04-11 18:21:18 +08:00
|
|
|
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;
|
|
|
|
|
|
2026-04-13 11:55:59 +08:00
|
|
|
return encode_body(&body, rsp_payload, rsp_payload_buf_size, rsp_payload_len);
|
|
|
|
|
}
|
2026-04-11 18:21:18 +08:00
|
|
|
|
2026-04-15 10:23:12 +08:00
|
|
|
static int module_init(void)
|
2026-04-13 11:55:59 +08:00
|
|
|
{
|
2026-04-15 10:23:12 +08:00
|
|
|
for (size_t i = 0; i < ARRAY_SIZE(hello_done); i++) {
|
|
|
|
|
hello_done[i] = false;
|
2026-04-13 11:55:59 +08:00
|
|
|
}
|
|
|
|
|
|
2026-04-11 18:21:18 +08:00
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-13 11:55:59 +08:00
|
|
|
static int module_start(void)
|
|
|
|
|
{
|
|
|
|
|
if (running) {
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
running = true;
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void module_pause(void)
|
|
|
|
|
{
|
|
|
|
|
if (!running) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-15 10:23:12 +08:00
|
|
|
for (size_t i = 0; i < ARRAY_SIZE(hello_done); i++) {
|
|
|
|
|
hello_done[i] = false;
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-13 11:55:59 +08:00
|
|
|
running = false;
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-15 10:23:12 +08:00
|
|
|
void protocol_module_reset_transport_state(enum proto_transport transport)
|
|
|
|
|
{
|
|
|
|
|
if (transport >= PROTO_TRANSPORT_COUNT) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
hello_done[transport] = false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int protocol_module_process_message(enum proto_transport transport,
|
|
|
|
|
const uint8_t *req_payload,
|
|
|
|
|
size_t req_payload_len,
|
|
|
|
|
uint8_t *rsp_payload,
|
|
|
|
|
size_t rsp_payload_buf_size,
|
|
|
|
|
size_t *rsp_payload_len)
|
2026-04-11 18:21:18 +08:00
|
|
|
{
|
|
|
|
|
CdcPacketBody body;
|
|
|
|
|
int err;
|
|
|
|
|
|
2026-04-15 10:23:12 +08:00
|
|
|
if ((transport >= PROTO_TRANSPORT_COUNT) ||
|
|
|
|
|
(rsp_payload == NULL) || (rsp_payload_len == NULL)) {
|
2026-04-11 18:21:18 +08:00
|
|
|
return -EINVAL;
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-13 11:55:59 +08:00
|
|
|
if (!running) {
|
|
|
|
|
return -EAGAIN;
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-11 18:21:18 +08:00
|
|
|
err = decode_body(req_payload, req_payload_len, &body);
|
|
|
|
|
if (err) {
|
2026-04-15 10:23:12 +08:00
|
|
|
return err;
|
2026-04-11 18:21:18 +08:00
|
|
|
}
|
|
|
|
|
|
2026-04-15 10:23:12 +08:00
|
|
|
if (body.which_body != CdcPacketBody_hello_req_tag) {
|
|
|
|
|
LOG_WRN("Unsupported protobuf body case %d", body.which_body);
|
|
|
|
|
return -ENOTSUP;
|
2026-04-13 11:55:59 +08:00
|
|
|
}
|
|
|
|
|
|
2026-04-15 10:23:12 +08:00
|
|
|
LOG_INF("HelloReq transport:%u protocol_version:%u",
|
|
|
|
|
transport, body.body.hello_req.protocol_version);
|
2026-04-13 11:55:59 +08:00
|
|
|
|
2026-04-15 10:23:12 +08:00
|
|
|
if (body.body.hello_req.protocol_version != PROTOCOL_VERSION) {
|
|
|
|
|
LOG_WRN("Unexpected protocol version:%u",
|
|
|
|
|
body.body.hello_req.protocol_version);
|
2026-04-13 11:55:59 +08:00
|
|
|
}
|
|
|
|
|
|
2026-04-15 10:23:12 +08:00
|
|
|
hello_done[transport] = true;
|
|
|
|
|
return encode_hello_rsp(rsp_payload, rsp_payload_buf_size, rsp_payload_len);
|
2026-04-13 11:55:59 +08:00
|
|
|
}
|
|
|
|
|
|
2026-04-15 10:23:12 +08:00
|
|
|
static bool handle_proto_rx_event(const struct proto_rx_event *event)
|
2026-04-13 16:43:17 +08:00
|
|
|
{
|
2026-04-15 10:23:12 +08:00
|
|
|
uint8_t rsp_payload[PROTOCOL_MAX_MSG_LEN];
|
|
|
|
|
size_t rsp_payload_len = 0U;
|
2026-04-13 16:43:17 +08:00
|
|
|
int err;
|
|
|
|
|
|
2026-04-15 10:23:12 +08:00
|
|
|
if (!running) {
|
2026-04-13 16:43:17 +08:00
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-15 10:23:12 +08:00
|
|
|
err = protocol_module_process_message(event->transport,
|
|
|
|
|
event->dyndata.data,
|
|
|
|
|
event->dyndata.size,
|
|
|
|
|
rsp_payload,
|
|
|
|
|
sizeof(rsp_payload),
|
|
|
|
|
&rsp_payload_len);
|
2026-04-13 16:43:17 +08:00
|
|
|
if (err) {
|
2026-04-15 10:23:12 +08:00
|
|
|
if (err != -ENOTSUP) {
|
|
|
|
|
LOG_WRN("Protocol processing failed (%d)", err);
|
|
|
|
|
}
|
2026-04-13 16:43:17 +08:00
|
|
|
|
2026-04-15 09:30:40 +08:00
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-15 10:23:12 +08:00
|
|
|
err = submit_proto_tx_event(event->transport, rsp_payload, rsp_payload_len);
|
|
|
|
|
if (err) {
|
|
|
|
|
LOG_WRN("Proto TX submit failed (%d)", err);
|
2026-04-13 11:55:59 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static bool app_event_handler(const struct app_event_header *aeh)
|
|
|
|
|
{
|
2026-04-15 10:23:12 +08:00
|
|
|
if (is_proto_rx_event(aeh)) {
|
|
|
|
|
return handle_proto_rx_event(cast_proto_rx_event(aeh));
|
2026-04-13 11:55:59 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
} else {
|
|
|
|
|
module_set_state(MODULE_STATE_READY);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return false;
|
2026-04-11 18:21:18 +08:00
|
|
|
}
|
2026-04-13 11:55:59 +08:00
|
|
|
|
|
|
|
|
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;
|
2026-04-11 18:21:18 +08:00
|
|
|
}
|
2026-04-13 11:55:59 +08:00
|
|
|
|
|
|
|
|
APP_EVENT_LISTENER(MODULE, app_event_handler);
|
|
|
|
|
APP_EVENT_SUBSCRIBE(MODULE, module_state_event);
|
2026-04-15 10:23:12 +08:00
|
|
|
APP_EVENT_SUBSCRIBE(MODULE, proto_rx_event);
|
2026-04-13 11:55:59 +08:00
|
|
|
APP_EVENT_SUBSCRIBE_EARLY(MODULE, power_down_event);
|
|
|
|
|
APP_EVENT_SUBSCRIBE(MODULE, wake_up_event);
|