feat(events): 添加事件提交函数到各个头文件中

为多个事件头文件添加了静态内联提交函数,包括:
- bat_state_event: 添加submit_bat_state_event函数
- ble_serial_rx_event: 添加submit_ble_serial_rx_event函数
- ble_serial_tx_event: 添加submit_ble_serial_tx_event函数
- cdc_proto_tx_event: 添加submit_cdc_proto_tx_event函数
- datetime_event: 添加submit_datetime_event函数
- encoder_event: 添加submit_encoder_event函数
- function_bitmap_update_event: 添加submit_function_bitmap_update_event函数
- hid_led_event: 添加submit_hid_led_event函数
- hid_report_sent_event: 添加submit_hid_report_sent_event函数
- hid_transport_state_event: 添加submit_hid_transport_state_event函数
- hid_tx_report_event: 添加submit_hid_tx_report_event函数
- key_function_event: 添加submit_key_function_event函数
- keyboard_hid_report_event: 添加submit_keyboard_hid_report_event函数
- led_strip_en_event: 添加submit_led_strip_en_event函数
- mode_switch_event: 添加submit_mode_switch_event函数
- set_protocol_event: 添加submit_set_protocol_event函数
- theme_rgb_update_event: 添加submit_theme_rgb_update_event函数
- time_sync_event: 添加submit_time_sync_event函数
- usb_cdc_rx_event: 添加submit_usb_cdc_rx_event函数
- usb_cdc_tx_event: 添加submit_usb_cdc_tx_event函数
- usb_device_state_event: 添加submit_usb_device_state_event函数
- usb_function_ready_event: 添加submit_usb_function_ready_event函数
- usb_prepare_event: 添加submit_usb_prepare_event函数

这些函数提供了一致的事件提交接口,简化了事件创建和提交过程。
This commit is contained in:
2026-04-14 16:42:04 +08:00
parent c342a8d3f0
commit 78a6dc212d
37 changed files with 485 additions and 624 deletions

View File

@@ -20,6 +20,16 @@ struct bat_state_event {
APP_EVENT_TYPE_DECLARE(bat_state_event); APP_EVENT_TYPE_DECLARE(bat_state_event);
static inline void submit_bat_state_event(uint8_t soc, bool charging, bool full)
{
struct bat_state_event *event = new_bat_state_event();
event->soc = soc;
event->charging = charging;
event->full = full;
APP_EVENT_SUBMIT(event);
}
#ifdef __cplusplus #ifdef __cplusplus
} }
#endif #endif

View File

@@ -1,6 +1,10 @@
#ifndef BLINKY_BLE_SERIAL_RX_EVENT_H_ #ifndef BLINKY_BLE_SERIAL_RX_EVENT_H_
#define BLINKY_BLE_SERIAL_RX_EVENT_H_ #define BLINKY_BLE_SERIAL_RX_EVENT_H_
#include <errno.h>
#include <stddef.h>
#include <string.h>
#include <app_event_manager.h> #include <app_event_manager.h>
#include <app_event_manager_profiler_tracer.h> #include <app_event_manager_profiler_tracer.h>
@@ -15,6 +19,23 @@ struct ble_serial_rx_event {
APP_EVENT_TYPE_DYNDATA_DECLARE(ble_serial_rx_event); APP_EVENT_TYPE_DYNDATA_DECLARE(ble_serial_rx_event);
static inline int submit_ble_serial_rx_event(const uint8_t *data, size_t len)
{
struct ble_serial_rx_event *event;
if ((data == NULL) && (len > 0U)) {
return -EINVAL;
}
event = new_ble_serial_rx_event(len);
if (len > 0U) {
memcpy(event->dyndata.data, data, len);
}
APP_EVENT_SUBMIT(event);
return 0;
}
#ifdef __cplusplus #ifdef __cplusplus
} }
#endif #endif

View File

@@ -1,6 +1,10 @@
#ifndef BLINKY_BLE_SERIAL_TX_EVENT_H_ #ifndef BLINKY_BLE_SERIAL_TX_EVENT_H_
#define BLINKY_BLE_SERIAL_TX_EVENT_H_ #define BLINKY_BLE_SERIAL_TX_EVENT_H_
#include <errno.h>
#include <stddef.h>
#include <string.h>
#include <app_event_manager.h> #include <app_event_manager.h>
#include <app_event_manager_profiler_tracer.h> #include <app_event_manager_profiler_tracer.h>
@@ -15,6 +19,23 @@ struct ble_serial_tx_event {
APP_EVENT_TYPE_DYNDATA_DECLARE(ble_serial_tx_event); APP_EVENT_TYPE_DYNDATA_DECLARE(ble_serial_tx_event);
static inline int submit_ble_serial_tx_event(const uint8_t *data, size_t len)
{
struct ble_serial_tx_event *event;
if ((data == NULL) && (len > 0U)) {
return -EINVAL;
}
event = new_ble_serial_tx_event(len);
if (len > 0U) {
memcpy(event->dyndata.data, data, len);
}
APP_EVENT_SUBMIT(event);
return 0;
}
#ifdef __cplusplus #ifdef __cplusplus
} }
#endif #endif

View File

@@ -1,6 +1,10 @@
#ifndef BLINKY_CDC_PROTO_TX_EVENT_H_ #ifndef BLINKY_CDC_PROTO_TX_EVENT_H_
#define BLINKY_CDC_PROTO_TX_EVENT_H_ #define BLINKY_CDC_PROTO_TX_EVENT_H_
#include <errno.h>
#include <stddef.h>
#include <string.h>
#include <app_event_manager.h> #include <app_event_manager.h>
#include <app_event_manager_profiler_tracer.h> #include <app_event_manager_profiler_tracer.h>
@@ -16,6 +20,25 @@ struct cdc_proto_tx_event {
APP_EVENT_TYPE_DYNDATA_DECLARE(cdc_proto_tx_event); APP_EVENT_TYPE_DYNDATA_DECLARE(cdc_proto_tx_event);
static inline int submit_cdc_proto_tx_event(uint8_t type, const uint8_t *payload,
size_t payload_len)
{
struct cdc_proto_tx_event *event;
if ((payload == NULL) && (payload_len > 0U)) {
return -EINVAL;
}
event = new_cdc_proto_tx_event(payload_len);
event->type = type;
if (payload_len > 0U) {
memcpy(event->dyndata.data, payload, payload_len);
}
APP_EVENT_SUBMIT(event);
return 0;
}
#ifdef __cplusplus #ifdef __cplusplus
} }
#endif #endif

View File

@@ -1,6 +1,9 @@
#ifndef BLINKY_DATETIME_EVENT_H_ #ifndef BLINKY_DATETIME_EVENT_H_
#define BLINKY_DATETIME_EVENT_H_ #define BLINKY_DATETIME_EVENT_H_
#include <errno.h>
#include <string.h>
#include <app_event_manager.h> #include <app_event_manager.h>
#include <app_event_manager_profiler_tracer.h> #include <app_event_manager_profiler_tracer.h>
@@ -19,6 +22,22 @@ struct datetime_event {
APP_EVENT_TYPE_DECLARE(datetime_event); APP_EVENT_TYPE_DECLARE(datetime_event);
static inline int submit_datetime_event(const char *date_text, const char *time_text)
{
struct datetime_event *event = new_datetime_event();
if ((date_text == NULL) || (time_text == NULL)) {
return -EINVAL;
}
strncpy(event->date_text, date_text, sizeof(event->date_text));
event->date_text[sizeof(event->date_text) - 1] = '\0';
strncpy(event->time_text, time_text, sizeof(event->time_text));
event->time_text[sizeof(event->time_text) - 1] = '\0';
APP_EVENT_SUBMIT(event);
return 0;
}
#ifdef __cplusplus #ifdef __cplusplus
} }
#endif #endif

View File

@@ -15,6 +15,14 @@ struct encoder_event {
APP_EVENT_TYPE_DECLARE(encoder_event); APP_EVENT_TYPE_DECLARE(encoder_event);
static inline void submit_encoder_event(int8_t detents)
{
struct encoder_event *event = new_encoder_event();
event->detents = detents;
APP_EVENT_SUBMIT(event);
}
#ifdef __cplusplus #ifdef __cplusplus
} }
#endif #endif

View File

@@ -1,6 +1,9 @@
#ifndef BLINKY_FUNCTION_BITMAP_UPDATE_EVENT_H_ #ifndef BLINKY_FUNCTION_BITMAP_UPDATE_EVENT_H_
#define BLINKY_FUNCTION_BITMAP_UPDATE_EVENT_H_ #define BLINKY_FUNCTION_BITMAP_UPDATE_EVENT_H_
#include <errno.h>
#include <string.h>
#include <app_event_manager.h> #include <app_event_manager.h>
#include <app_event_manager_profiler_tracer.h> #include <app_event_manager_profiler_tracer.h>
@@ -17,6 +20,20 @@ struct function_bitmap_update_event {
APP_EVENT_TYPE_DECLARE(function_bitmap_update_event); APP_EVENT_TYPE_DECLARE(function_bitmap_update_event);
static inline int submit_function_bitmap_update_event(const uint8_t *bitmap)
{
struct function_bitmap_update_event *event;
if (bitmap == NULL) {
return -EINVAL;
}
event = new_function_bitmap_update_event();
memcpy(event->bitmap, bitmap, sizeof(event->bitmap));
APP_EVENT_SUBMIT(event);
return 0;
}
#ifdef __cplusplus #ifdef __cplusplus
} }
#endif #endif

View File

@@ -18,6 +18,15 @@ struct hid_led_event {
APP_EVENT_TYPE_DECLARE(hid_led_event); APP_EVENT_TYPE_DECLARE(hid_led_event);
static inline void submit_hid_led_event(enum hid_transport transport, uint8_t led_bm)
{
struct hid_led_event *event = new_hid_led_event();
event->transport = transport;
event->led_bm = led_bm;
APP_EVENT_SUBMIT(event);
}
#ifdef __cplusplus #ifdef __cplusplus
} }
#endif #endif

View File

@@ -20,6 +20,19 @@ struct hid_report_sent_event {
APP_EVENT_TYPE_DECLARE(hid_report_sent_event); APP_EVENT_TYPE_DECLARE(hid_report_sent_event);
static inline void submit_hid_report_sent_event(enum hid_transport transport,
enum keyboard_report_type report_type,
uint16_t sequence, bool error)
{
struct hid_report_sent_event *event = new_hid_report_sent_event();
event->transport = transport;
event->report_type = report_type;
event->sequence = sequence;
event->error = error;
APP_EVENT_SUBMIT(event);
}
#ifdef __cplusplus #ifdef __cplusplus
} }
#endif #endif

View File

@@ -21,6 +21,20 @@ struct hid_transport_state_event {
APP_EVENT_TYPE_DECLARE(hid_transport_state_event); APP_EVENT_TYPE_DECLARE(hid_transport_state_event);
static inline void submit_hid_transport_state_event(
enum hid_transport transport, bool ready, bool keys_ready,
bool consumer_ready, enum keyboard_protocol_mode protocol_mode)
{
struct hid_transport_state_event *event = new_hid_transport_state_event();
event->transport = transport;
event->ready = ready;
event->keys_ready = keys_ready;
event->consumer_ready = consumer_ready;
event->protocol_mode = protocol_mode;
APP_EVENT_SUBMIT(event);
}
#ifdef __cplusplus #ifdef __cplusplus
} }
#endif #endif

View File

@@ -1,6 +1,10 @@
#ifndef BLINKY_HID_TX_REPORT_EVENT_H_ #ifndef BLINKY_HID_TX_REPORT_EVENT_H_
#define BLINKY_HID_TX_REPORT_EVENT_H_ #define BLINKY_HID_TX_REPORT_EVENT_H_
#include <errno.h>
#include <stddef.h>
#include <string.h>
#include <app_event_manager.h> #include <app_event_manager.h>
#include <app_event_manager_profiler_tracer.h> #include <app_event_manager_profiler_tracer.h>
@@ -21,6 +25,31 @@ struct hid_tx_report_event {
APP_EVENT_TYPE_DYNDATA_DECLARE(hid_tx_report_event); APP_EVENT_TYPE_DYNDATA_DECLARE(hid_tx_report_event);
static inline int submit_hid_tx_report_event(enum hid_transport transport,
enum keyboard_report_type report_type,
enum keyboard_protocol_mode protocol_mode,
uint16_t sequence,
const uint8_t *data, size_t size)
{
struct hid_tx_report_event *event;
if ((data == NULL) && (size > 0U)) {
return -EINVAL;
}
event = new_hid_tx_report_event(size);
event->transport = transport;
event->report_type = report_type;
event->protocol_mode = protocol_mode;
event->sequence = sequence;
if (size > 0U) {
memcpy(event->dyndata.data, data, size);
}
APP_EVENT_SUBMIT(event);
return 0;
}
#ifdef __cplusplus #ifdef __cplusplus
} }
#endif #endif

View File

@@ -21,6 +21,15 @@ struct key_function_event {
APP_EVENT_TYPE_DECLARE(key_function_event); APP_EVENT_TYPE_DECLARE(key_function_event);
static inline void submit_key_function_event(uint16_t usage, uint8_t action)
{
struct key_function_event *event = new_key_function_event();
event->usage = usage;
event->action = action;
APP_EVENT_SUBMIT(event);
}
#ifdef __cplusplus #ifdef __cplusplus
} }
#endif #endif

View File

@@ -1,6 +1,10 @@
#ifndef BLINKY_KEYBOARD_HID_REPORT_EVENT_H_ #ifndef BLINKY_KEYBOARD_HID_REPORT_EVENT_H_
#define BLINKY_KEYBOARD_HID_REPORT_EVENT_H_ #define BLINKY_KEYBOARD_HID_REPORT_EVENT_H_
#include <errno.h>
#include <stddef.h>
#include <string.h>
#include <app_event_manager.h> #include <app_event_manager.h>
#include <app_event_manager_profiler_tracer.h> #include <app_event_manager_profiler_tracer.h>
@@ -22,6 +26,30 @@ struct keyboard_hid_report_event {
APP_EVENT_TYPE_DYNDATA_DECLARE(keyboard_hid_report_event); APP_EVENT_TYPE_DYNDATA_DECLARE(keyboard_hid_report_event);
static inline int submit_keyboard_hid_report_event(
enum mode_switch_mode mode, enum keyboard_report_type report_type,
enum keyboard_protocol_mode protocol_mode,
enum hid_queue_policy queue_policy, const uint8_t *data, size_t size)
{
struct keyboard_hid_report_event *event;
if ((data == NULL) && (size > 0U)) {
return -EINVAL;
}
event = new_keyboard_hid_report_event(size);
event->mode = mode;
event->report_type = report_type;
event->protocol_mode = protocol_mode;
event->queue_policy = queue_policy;
if (size > 0U) {
memcpy(event->dyndata.data, data, size);
}
APP_EVENT_SUBMIT(event);
return 0;
}
#ifdef __cplusplus #ifdef __cplusplus
} }
#endif #endif

View File

@@ -17,6 +17,14 @@ struct led_strip_en_event {
APP_EVENT_TYPE_DECLARE(led_strip_en_event); APP_EVENT_TYPE_DECLARE(led_strip_en_event);
static inline void submit_led_strip_en_event(bool enabled)
{
struct led_strip_en_event *event = new_led_strip_en_event();
event->enabled = enabled;
APP_EVENT_SUBMIT(event);
}
#ifdef __cplusplus #ifdef __cplusplus
} }
#endif #endif

View File

@@ -22,6 +22,16 @@ struct mode_switch_event {
APP_EVENT_TYPE_DECLARE(mode_switch_event); APP_EVENT_TYPE_DECLARE(mode_switch_event);
static inline void submit_mode_switch_event(enum mode_switch_mode mode,
uint16_t voltage_mv)
{
struct mode_switch_event *event = new_mode_switch_event();
event->mode = mode;
event->voltage_mv = voltage_mv;
APP_EVENT_SUBMIT(event);
}
#ifdef __cplusplus #ifdef __cplusplus
} }
#endif #endif

View File

@@ -18,6 +18,16 @@ struct set_protocol_event {
APP_EVENT_TYPE_DECLARE(set_protocol_event); APP_EVENT_TYPE_DECLARE(set_protocol_event);
static inline void submit_set_protocol_event(enum hid_transport transport,
enum keyboard_protocol_mode protocol_mode)
{
struct set_protocol_event *event = new_set_protocol_event();
event->transport = transport;
event->protocol_mode = protocol_mode;
APP_EVENT_SUBMIT(event);
}
#ifdef __cplusplus #ifdef __cplusplus
} }
#endif #endif

View File

@@ -17,6 +17,14 @@ struct theme_rgb_update_event {
APP_EVENT_TYPE_DECLARE(theme_rgb_update_event); APP_EVENT_TYPE_DECLARE(theme_rgb_update_event);
static inline void submit_theme_rgb_update_event(struct theme_rgb theme)
{
struct theme_rgb_update_event *event = new_theme_rgb_update_event();
event->theme = theme;
APP_EVENT_SUBMIT(event);
}
#ifdef __cplusplus #ifdef __cplusplus
} }
#endif #endif

View File

@@ -19,6 +19,20 @@ struct time_sync_event {
APP_EVENT_TYPE_DECLARE(time_sync_event); APP_EVENT_TYPE_DECLARE(time_sync_event);
static inline void submit_time_sync_event(uint32_t version, uint32_t flags,
int32_t timezone_min, uint64_t utc_ms,
uint32_t accuracy_ms)
{
struct time_sync_event *event = new_time_sync_event();
event->version = version;
event->flags = flags;
event->timezone_min = timezone_min;
event->utc_ms = utc_ms;
event->accuracy_ms = accuracy_ms;
APP_EVENT_SUBMIT(event);
}
#ifdef __cplusplus #ifdef __cplusplus
} }
#endif #endif

View File

@@ -1,6 +1,10 @@
#ifndef BLINKY_USB_CDC_RX_EVENT_H_ #ifndef BLINKY_USB_CDC_RX_EVENT_H_
#define BLINKY_USB_CDC_RX_EVENT_H_ #define BLINKY_USB_CDC_RX_EVENT_H_
#include <errno.h>
#include <stddef.h>
#include <string.h>
#include <app_event_manager.h> #include <app_event_manager.h>
#include <app_event_manager_profiler_tracer.h> #include <app_event_manager_profiler_tracer.h>
@@ -15,6 +19,23 @@ struct usb_cdc_rx_event {
APP_EVENT_TYPE_DYNDATA_DECLARE(usb_cdc_rx_event); APP_EVENT_TYPE_DYNDATA_DECLARE(usb_cdc_rx_event);
static inline int submit_usb_cdc_rx_event(const uint8_t *data, size_t len)
{
struct usb_cdc_rx_event *event;
if ((data == NULL) && (len > 0U)) {
return -EINVAL;
}
event = new_usb_cdc_rx_event(len);
if (len > 0U) {
memcpy(event->dyndata.data, data, len);
}
APP_EVENT_SUBMIT(event);
return 0;
}
#ifdef __cplusplus #ifdef __cplusplus
} }
#endif #endif

View File

@@ -1,6 +1,10 @@
#ifndef BLINKY_USB_CDC_TX_EVENT_H_ #ifndef BLINKY_USB_CDC_TX_EVENT_H_
#define BLINKY_USB_CDC_TX_EVENT_H_ #define BLINKY_USB_CDC_TX_EVENT_H_
#include <errno.h>
#include <stddef.h>
#include <string.h>
#include <app_event_manager.h> #include <app_event_manager.h>
#include <app_event_manager_profiler_tracer.h> #include <app_event_manager_profiler_tracer.h>
@@ -15,6 +19,23 @@ struct usb_cdc_tx_event {
APP_EVENT_TYPE_DYNDATA_DECLARE(usb_cdc_tx_event); APP_EVENT_TYPE_DYNDATA_DECLARE(usb_cdc_tx_event);
static inline int submit_usb_cdc_tx_event(const uint8_t *data, size_t len)
{
struct usb_cdc_tx_event *event;
if ((data == NULL) && (len > 0U)) {
return -EINVAL;
}
event = new_usb_cdc_tx_event(len);
if (len > 0U) {
memcpy(event->dyndata.data, data, len);
}
APP_EVENT_SUBMIT(event);
return 0;
}
#ifdef __cplusplus #ifdef __cplusplus
} }
#endif #endif

View File

@@ -17,6 +17,14 @@ struct usb_device_state_event {
APP_EVENT_TYPE_DECLARE(usb_device_state_event); APP_EVENT_TYPE_DECLARE(usb_device_state_event);
static inline void submit_usb_device_state_event(enum usb_device_state state)
{
struct usb_device_state_event *event = new_usb_device_state_event();
event->state = state;
APP_EVENT_SUBMIT(event);
}
#ifdef __cplusplus #ifdef __cplusplus
} }
#endif #endif

View File

@@ -17,6 +17,14 @@ struct usb_function_ready_event {
APP_EVENT_TYPE_DECLARE(usb_function_ready_event); APP_EVENT_TYPE_DECLARE(usb_function_ready_event);
static inline void submit_usb_function_ready_event(uint8_t function_mask)
{
struct usb_function_ready_event *event = new_usb_function_ready_event();
event->function_mask = function_mask;
APP_EVENT_SUBMIT(event);
}
#ifdef __cplusplus #ifdef __cplusplus
} }
#endif #endif

View File

@@ -14,6 +14,11 @@ struct usb_prepare_event {
APP_EVENT_TYPE_DECLARE(usb_prepare_event); APP_EVENT_TYPE_DECLARE(usb_prepare_event);
static inline void submit_usb_prepare_event(void)
{
APP_EVENT_SUBMIT(new_usb_prepare_event());
}
#ifdef __cplusplus #ifdef __cplusplus
} }
#endif #endif

View File

@@ -81,30 +81,6 @@ static uint8_t battery_soc_from_mv(int voltage_mv)
return (uint8_t)(bucket * 10); return (uint8_t)(bucket * 10);
} }
static void submit_bat_state_event(uint8_t soc, bool charging, bool full)
{
struct bat_state_event *event;
if (last_bat_state.valid &&
(last_bat_state.soc == soc) &&
(last_bat_state.charging == charging) &&
(last_bat_state.full == full)) {
return;
}
last_bat_state.valid = true;
last_bat_state.soc = soc;
last_bat_state.charging = charging;
last_bat_state.full = full;
event = new_bat_state_event();
event->soc = soc;
event->charging = charging;
event->full = full;
APP_EVENT_SUBMIT(event);
}
static void battery_sample_fn(struct k_work *work) static void battery_sample_fn(struct k_work *work)
{ {
struct ip5306_status pmic_status; struct ip5306_status pmic_status;
@@ -137,9 +113,18 @@ static void battery_sample_fn(struct k_work *work)
} }
voltage_mv = sensor_value_to_mv(&voltage); voltage_mv = sensor_value_to_mv(&voltage);
submit_bat_state_event(battery_soc_from_mv(voltage_mv), uint8_t soc = battery_soc_from_mv(voltage_mv);
pmic_status.charging,
pmic_status.full); if (!last_bat_state.valid ||
(last_bat_state.soc != soc) ||
(last_bat_state.charging != pmic_status.charging) ||
(last_bat_state.full != pmic_status.full)) {
last_bat_state.valid = true;
last_bat_state.soc = soc;
last_bat_state.charging = pmic_status.charging;
last_bat_state.full = pmic_status.full;
submit_bat_state_event(soc, pmic_status.charging, pmic_status.full);
}
reschedule: reschedule:
if (running) { if (running) {

View File

@@ -98,54 +98,19 @@ static const uint8_t hid_report_desc[] = {
0xC0 /* End Collection */ 0xC0 /* End Collection */
}; };
static void submit_set_protocol_event(void) static void submit_ble_transport_state_event(void)
{ {
struct set_protocol_event *event = new_set_protocol_event();
event->transport = HID_TRANSPORT_BLE;
event->protocol_mode = protocol_mode;
APP_EVENT_SUBMIT(event);
}
static void submit_hid_led_event(uint8_t led_bm)
{
struct hid_led_event *event = new_hid_led_event();
event->transport = HID_TRANSPORT_BLE;
event->led_bm = led_bm;
APP_EVENT_SUBMIT(event);
}
static void submit_transport_state_event(void)
{
struct hid_transport_state_event *event = new_hid_transport_state_event();
bool ready = running && secured && (active_conn != NULL); bool ready = running && secured && (active_conn != NULL);
event->transport = HID_TRANSPORT_BLE; submit_hid_transport_state_event(
event->ready = ready; HID_TRANSPORT_BLE,
event->protocol_mode = protocol_mode; ready,
event->keys_ready = ready && ready && ((protocol_mode == KEYBOARD_PROTOCOL_MODE_BOOT) ?
((protocol_mode == KEYBOARD_PROTOCOL_MODE_BOOT) ? boot_keyboard_notify_enabled :
boot_keyboard_notify_enabled : keyboard_report_notify_enabled),
keyboard_report_notify_enabled); ready && (protocol_mode == KEYBOARD_PROTOCOL_MODE_REPORT) &&
event->consumer_ready = ready && consumer_report_notify_enabled,
(protocol_mode == KEYBOARD_PROTOCOL_MODE_REPORT) && protocol_mode);
consumer_report_notify_enabled;
APP_EVENT_SUBMIT(event);
}
static void submit_hid_report_sent_event(enum keyboard_report_type report_type,
uint16_t sequence, bool error)
{
struct hid_report_sent_event *event = new_hid_report_sent_event();
event->transport = HID_TRANSPORT_BLE;
event->report_type = report_type;
event->sequence = sequence;
event->error = error;
APP_EVENT_SUBMIT(event);
} }
static void input_report_notify_handler(uint8_t report_id, enum bt_hids_notify_evt evt) static void input_report_notify_handler(uint8_t report_id, enum bt_hids_notify_evt evt)
@@ -158,13 +123,13 @@ static void input_report_notify_handler(uint8_t report_id, enum bt_hids_notify_e
consumer_report_notify_enabled = enabled; consumer_report_notify_enabled = enabled;
} }
submit_transport_state_event(); submit_ble_transport_state_event();
} }
static void boot_keyboard_notify_handler(enum bt_hids_notify_evt evt) static void boot_keyboard_notify_handler(enum bt_hids_notify_evt evt)
{ {
boot_keyboard_notify_enabled = (evt == BT_HIDS_CCCD_EVT_NOTIFY_ENABLED); boot_keyboard_notify_enabled = (evt == BT_HIDS_CCCD_EVT_NOTIFY_ENABLED);
submit_transport_state_event(); submit_ble_transport_state_event();
} }
static void hid_report_complete_cb(struct bt_conn *conn, void *user_data) static void hid_report_complete_cb(struct bt_conn *conn, void *user_data)
@@ -176,7 +141,8 @@ static void hid_report_complete_cb(struct bt_conn *conn, void *user_data)
return; return;
} }
submit_hid_report_sent_event(in_flight.report_type, in_flight.sequence, false); submit_hid_report_sent_event(HID_TRANSPORT_BLE, in_flight.report_type,
in_flight.sequence, false);
in_flight.active = false; in_flight.active = false;
} }
@@ -186,7 +152,7 @@ static void keyboard_led_report_common(struct bt_hids_rep *rep, bool write)
return; return;
} }
submit_hid_led_event(rep->data[0]); submit_hid_led_event(HID_TRANSPORT_BLE, rep->data[0]);
} }
static void keyboard_led_report_handler(struct bt_hids_rep *rep, static void keyboard_led_report_handler(struct bt_hids_rep *rep,
@@ -224,8 +190,8 @@ static void pm_evt_handler(enum bt_hids_pm_evt evt, struct bt_conn *conn)
return; return;
} }
submit_set_protocol_event(); submit_set_protocol_event(HID_TRANSPORT_BLE, protocol_mode);
submit_transport_state_event(); submit_ble_transport_state_event();
} }
static int module_init(void) static int module_init(void)
@@ -272,7 +238,7 @@ static int module_start(void)
} }
running = true; running = true;
submit_transport_state_event(); submit_ble_transport_state_event();
return 0; return 0;
} }
@@ -285,7 +251,7 @@ static void module_pause(void)
in_flight.active = false; in_flight.active = false;
running = false; running = false;
submit_transport_state_event(); submit_ble_transport_state_event();
} }
static void reset_connection_state(void) static void reset_connection_state(void)
@@ -311,12 +277,12 @@ static bool handle_ble_peer_event(const struct ble_peer_event *event)
active_conn = event->id; active_conn = event->id;
protocol_mode = KEYBOARD_PROTOCOL_MODE_REPORT; protocol_mode = KEYBOARD_PROTOCOL_MODE_REPORT;
submit_set_protocol_event(); submit_set_protocol_event(HID_TRANSPORT_BLE, protocol_mode);
err = bt_hids_connected(&hids_obj, event->id); err = bt_hids_connected(&hids_obj, event->id);
if (err) { if (err) {
LOG_ERR("bt_hids_connected failed (%d)", err); LOG_ERR("bt_hids_connected failed (%d)", err);
} }
submit_transport_state_event(); submit_ble_transport_state_event();
return false; return false;
case PEER_STATE_SECURED: case PEER_STATE_SECURED:
@@ -325,7 +291,7 @@ static bool handle_ble_peer_event(const struct ble_peer_event *event)
} }
secured = true; secured = true;
submit_transport_state_event(); submit_ble_transport_state_event();
return false; return false;
case PEER_STATE_DISCONNECTED: case PEER_STATE_DISCONNECTED:
@@ -339,7 +305,7 @@ static bool handle_ble_peer_event(const struct ble_peer_event *event)
} }
reset_connection_state(); reset_connection_state();
submit_transport_state_event(); submit_ble_transport_state_event();
return false; return false;
default: default:
@@ -387,7 +353,8 @@ static bool handle_hid_tx_report_event(const struct hid_tx_report_event *event)
if (err) { if (err) {
in_flight.active = false; in_flight.active = false;
LOG_WRN("BLE keyboard report submit failed (%d)", err); LOG_WRN("BLE keyboard report submit failed (%d)", err);
submit_hid_report_sent_event(KEYBOARD_REPORT_TYPE_KEYS, submit_hid_report_sent_event(HID_TRANSPORT_BLE,
KEYBOARD_REPORT_TYPE_KEYS,
event->sequence, true); event->sequence, true);
} }
@@ -413,7 +380,8 @@ static bool handle_hid_tx_report_event(const struct hid_tx_report_event *event)
if (err) { if (err) {
in_flight.active = false; in_flight.active = false;
LOG_WRN("BLE consumer report submit failed (%d)", err); LOG_WRN("BLE consumer report submit failed (%d)", err);
submit_hid_report_sent_event(KEYBOARD_REPORT_TYPE_CONSUMER, submit_hid_report_sent_event(HID_TRANSPORT_BLE,
KEYBOARD_REPORT_TYPE_CONSUMER,
event->sequence, true); event->sequence, true);
} }
} }

View File

@@ -45,14 +45,6 @@ static const struct bt_uuid_128 ble_serial_rx_uuid =
static const struct bt_uuid_128 ble_serial_tx_uuid = static const struct bt_uuid_128 ble_serial_tx_uuid =
BT_UUID_INIT_128(BLE_SERIAL_TX_UUID_VAL); 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) static void tx_ccc_cfg_changed(const struct bt_gatt_attr *attr, uint16_t value)
{ {
ARG_UNUSED(attr); ARG_UNUSED(attr);
@@ -83,7 +75,7 @@ static ssize_t rx_write_handler(struct bt_conn *conn,
return BT_GATT_ERR(BT_ATT_ERR_INVALID_ATTRIBUTE_LEN); return BT_GATT_ERR(BT_ATT_ERR_INVALID_ATTRIBUTE_LEN);
} }
submit_ble_serial_rx_event(buf, len); (void)submit_ble_serial_rx_event(buf, len);
return len; return len;
} }

View File

@@ -69,18 +69,17 @@ static uint8_t frame_checksum(uint8_t len, uint8_t type,
static void submit_tx_frame(uint8_t type, const uint8_t *payload, size_t payload_len) 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; size_t frame_len = 2U + 1U + 1U + payload_len + 1U;
uint8_t frame_buf[CDC_WRAPPER_MAX_FRAME_LEN];
event = new_usb_cdc_tx_event(frame_len); frame_buf[0] = CDC_WRAPPER_HEAD1;
event->dyndata.data[0] = CDC_WRAPPER_HEAD1; frame_buf[1] = CDC_WRAPPER_HEAD2;
event->dyndata.data[1] = CDC_WRAPPER_HEAD2; frame_buf[2] = (uint8_t)payload_len;
event->dyndata.data[2] = (uint8_t)payload_len; frame_buf[3] = type;
event->dyndata.data[3] = type; memcpy(&frame_buf[4], payload, payload_len);
memcpy(&event->dyndata.data[4], payload, payload_len); frame_buf[4U + payload_len] =
event->dyndata.data[4U + payload_len] =
frame_checksum((uint8_t)payload_len, type, payload, payload_len); frame_checksum((uint8_t)payload_len, type, payload, payload_len);
APP_EVENT_SUBMIT(event); (void)submit_usb_cdc_tx_event(frame_buf, frame_len);
} }
static void process_complete_frame(void) static void process_complete_frame(void)

View File

@@ -39,23 +39,23 @@ static int64_t sensor_value_to_udeg(const struct sensor_value *value)
return ((int64_t)value->val1 * 1000000LL) + value->val2; return ((int64_t)value->val1 * 1000000LL) + value->val2;
} }
static void submit_detents(int32_t detents) static void submit_detents_batched(int32_t detents)
{ {
while (detents != 0) { while (detents != 0) {
struct encoder_event *event = new_encoder_event(); int8_t event_detents;
if (detents > INT8_MAX) { if (detents > INT8_MAX) {
event->detents = INT8_MAX; event_detents = INT8_MAX;
detents -= INT8_MAX; detents -= INT8_MAX;
} else if (detents < INT8_MIN) { } else if (detents < INT8_MIN) {
event->detents = INT8_MIN; event_detents = INT8_MIN;
detents -= INT8_MIN; detents -= INT8_MIN;
} else { } else {
event->detents = (int8_t)detents; event_detents = (int8_t)detents;
detents = 0; detents = 0;
} }
APP_EVENT_SUBMIT(event); submit_encoder_event(event_detents);
} }
} }
@@ -89,7 +89,7 @@ static void encoder_report_work_handler(struct k_work *work)
angle_remainder_udeg = total_udeg - ((int64_t)detents * ENCODER_DETENT_UDEG); angle_remainder_udeg = total_udeg - ((int64_t)detents * ENCODER_DETENT_UDEG);
if (detents != 0) { if (detents != 0) {
submit_detents(detents); submit_detents_batched(detents);
} }
} }

View File

@@ -153,27 +153,6 @@ static bool transport_can_send_report(enum keyboard_report_type report_type)
return state->consumer_ready; return state->consumer_ready;
} }
static void submit_hid_tx_report_event(enum hid_transport transport,
enum keyboard_report_type report_type,
enum keyboard_protocol_mode protocol_mode,
const uint8_t *data, size_t size)
{
struct hid_tx_report_event *event = new_hid_tx_report_event(size);
event->transport = transport;
event->report_type = report_type;
event->protocol_mode = protocol_mode;
event->sequence = next_sequence++;
memcpy(event->dyndata.data, data, size);
in_flight.active = true;
in_flight.transport = transport;
in_flight.report_type = report_type;
in_flight.sequence = event->sequence;
APP_EVENT_SUBMIT(event);
}
static void try_send_next(void) static void try_send_next(void)
{ {
struct queued_report queued; struct queued_report queued;
@@ -195,11 +174,14 @@ static void try_send_next(void)
LOG_WRN("Drop stale keys report after protocol change"); LOG_WRN("Drop stale keys report after protocol change");
pending_keys.valid = false; pending_keys.valid = false;
} else { } else {
submit_hid_tx_report_event(transport, in_flight.active = true;
pending_keys.report_type, in_flight.transport = transport;
pending_keys.protocol_mode, in_flight.report_type = pending_keys.report_type;
pending_keys.data, in_flight.sequence = next_sequence++;
pending_keys.size); (void)submit_hid_tx_report_event(
transport, pending_keys.report_type,
pending_keys.protocol_mode, in_flight.sequence,
pending_keys.data, pending_keys.size);
pending_keys.valid = false; pending_keys.valid = false;
return; return;
} }
@@ -211,11 +193,14 @@ static void try_send_next(void)
if (queued.protocol_mode != state->protocol_mode) { if (queued.protocol_mode != state->protocol_mode) {
LOG_WRN("Drop stale consumer report after protocol change"); LOG_WRN("Drop stale consumer report after protocol change");
} else { } else {
submit_hid_tx_report_event(transport, in_flight.active = true;
queued.report_type, in_flight.transport = transport;
queued.protocol_mode, in_flight.report_type = queued.report_type;
queued.data, in_flight.sequence = next_sequence++;
queued.size); (void)submit_hid_tx_report_event(
transport, queued.report_type,
queued.protocol_mode, in_flight.sequence,
queued.data, queued.size);
return; return;
} }
} }
@@ -226,11 +211,16 @@ static void try_send_next(void)
LOG_WRN("Drop stale latest consumer report after protocol change"); LOG_WRN("Drop stale latest consumer report after protocol change");
pending_consumer_latest.valid = false; pending_consumer_latest.valid = false;
} else { } else {
submit_hid_tx_report_event(transport, in_flight.active = true;
pending_consumer_latest.report_type, in_flight.transport = transport;
pending_consumer_latest.protocol_mode, in_flight.report_type = pending_consumer_latest.report_type;
pending_consumer_latest.data, in_flight.sequence = next_sequence++;
pending_consumer_latest.size); (void)submit_hid_tx_report_event(
transport, pending_consumer_latest.report_type,
pending_consumer_latest.protocol_mode,
in_flight.sequence,
pending_consumer_latest.data,
pending_consumer_latest.size);
pending_consumer_latest.valid = false; pending_consumer_latest.valid = false;
} }
} }

View File

@@ -274,32 +274,6 @@ static void build_consumer_report(uint8_t report[KEYBOARD_CONSUMER_REPORT_SIZE])
sys_put_le16(active_consumer_usage_get(), report); sys_put_le16(active_consumer_usage_get(), report);
} }
static void submit_keyboard_report_event(enum keyboard_report_type report_type,
enum hid_queue_policy queue_policy,
const uint8_t *data, size_t size)
{
struct keyboard_hid_report_event *event =
new_keyboard_hid_report_event(size);
enum keyboard_protocol_mode protocol_mode = active_protocol_mode_get();
event->mode = current_mode;
event->report_type = report_type;
event->protocol_mode = protocol_mode;
event->queue_policy = queue_policy;
memcpy(event->dyndata.data, data, size);
APP_EVENT_SUBMIT(event);
}
static void submit_key_function_event(uint16_t usage_id, uint8_t action)
{
struct key_function_event *event = new_key_function_event();
event->usage = usage_id;
event->action = action;
APP_EVENT_SUBMIT(event);
}
static void submit_consumer_fifo_frame(uint16_t usage_id) static void submit_consumer_fifo_frame(uint16_t usage_id)
{ {
uint8_t report_buf[KEYBOARD_CONSUMER_REPORT_SIZE]; uint8_t report_buf[KEYBOARD_CONSUMER_REPORT_SIZE];
@@ -311,10 +285,9 @@ static void submit_consumer_fifo_frame(uint16_t usage_id)
} }
sys_put_le16(usage_id, report_buf); sys_put_le16(usage_id, report_buf);
submit_keyboard_report_event(KEYBOARD_REPORT_TYPE_CONSUMER, (void)submit_keyboard_hid_report_event(
HID_QUEUE_POLICY_FIFO, current_mode, KEYBOARD_REPORT_TYPE_CONSUMER, protocol_mode,
report_buf, HID_QUEUE_POLICY_FIFO, report_buf, KEYBOARD_CONSUMER_REPORT_SIZE);
KEYBOARD_CONSUMER_REPORT_SIZE);
} }
static void submit_consumer_pulse_frames(enum keyboard_consumer_control control_id, static void submit_consumer_pulse_frames(enum keyboard_consumer_control control_id,
@@ -374,10 +347,9 @@ static void emit_keys_report(bool force)
memcpy(cache_buf, report_buf, report_size); memcpy(cache_buf, report_buf, report_size);
*cache_valid = true; *cache_valid = true;
submit_keyboard_report_event(KEYBOARD_REPORT_TYPE_KEYS, (void)submit_keyboard_hid_report_event(
HID_QUEUE_POLICY_LATEST, current_mode, KEYBOARD_REPORT_TYPE_KEYS, protocol_mode,
report_buf, HID_QUEUE_POLICY_LATEST, report_buf, report_size);
report_size);
} }
static void emit_consumer_report(bool force) static void emit_consumer_report(bool force)
@@ -399,10 +371,10 @@ static void emit_consumer_report(bool force)
memcpy(reports_cache.consumer_report, report_buf, KEYBOARD_CONSUMER_REPORT_SIZE); memcpy(reports_cache.consumer_report, report_buf, KEYBOARD_CONSUMER_REPORT_SIZE);
reports_cache.consumer_valid = true; reports_cache.consumer_valid = true;
submit_keyboard_report_event(KEYBOARD_REPORT_TYPE_CONSUMER, (void)submit_keyboard_hid_report_event(
HID_QUEUE_POLICY_LATEST, current_mode, KEYBOARD_REPORT_TYPE_CONSUMER, protocol_mode,
report_buf, HID_QUEUE_POLICY_LATEST, report_buf,
KEYBOARD_CONSUMER_REPORT_SIZE); KEYBOARD_CONSUMER_REPORT_SIZE);
} }
static void emit_all_reports(bool force) static void emit_all_reports(bool force)
@@ -427,7 +399,6 @@ static void emit_function_release_events(void)
static void emit_release_reports(enum mode_switch_mode mode) static void emit_release_reports(enum mode_switch_mode mode)
{ {
struct keyboard_hid_report_event *event;
uint8_t keys_report[KEYBOARD_NKRO_REPORT_SIZE] = { 0 }; uint8_t keys_report[KEYBOARD_NKRO_REPORT_SIZE] = { 0 };
uint8_t consumer_report[KEYBOARD_CONSUMER_REPORT_SIZE] = { 0 }; uint8_t consumer_report[KEYBOARD_CONSUMER_REPORT_SIZE] = { 0 };
enum keyboard_protocol_mode protocol_mode = active_protocol_mode_get(); enum keyboard_protocol_mode protocol_mode = active_protocol_mode_get();
@@ -435,21 +406,15 @@ static void emit_release_reports(enum mode_switch_mode mode)
(protocol_mode == KEYBOARD_PROTOCOL_MODE_BOOT) ? (protocol_mode == KEYBOARD_PROTOCOL_MODE_BOOT) ?
KEYBOARD_BOOT_REPORT_SIZE : KEYBOARD_NKRO_REPORT_SIZE; KEYBOARD_BOOT_REPORT_SIZE : KEYBOARD_NKRO_REPORT_SIZE;
event = new_keyboard_hid_report_event(keys_report_size); (void)submit_keyboard_hid_report_event(
event->mode = mode; mode, KEYBOARD_REPORT_TYPE_KEYS, protocol_mode,
event->report_type = KEYBOARD_REPORT_TYPE_KEYS; HID_QUEUE_POLICY_LATEST, keys_report, keys_report_size);
event->protocol_mode = protocol_mode;
memcpy(event->dyndata.data, keys_report, keys_report_size);
APP_EVENT_SUBMIT(event);
if (protocol_mode != KEYBOARD_PROTOCOL_MODE_BOOT) { if (protocol_mode != KEYBOARD_PROTOCOL_MODE_BOOT) {
event = new_keyboard_hid_report_event(KEYBOARD_CONSUMER_REPORT_SIZE); (void)submit_keyboard_hid_report_event(
event->mode = mode; mode, KEYBOARD_REPORT_TYPE_CONSUMER, protocol_mode,
event->report_type = KEYBOARD_REPORT_TYPE_CONSUMER; HID_QUEUE_POLICY_LATEST, consumer_report,
event->protocol_mode = protocol_mode; KEYBOARD_CONSUMER_REPORT_SIZE);
memcpy(event->dyndata.data, consumer_report,
KEYBOARD_CONSUMER_REPORT_SIZE);
APP_EVENT_SUBMIT(event);
} }
} }

View File

@@ -91,11 +91,7 @@ static void mode_switch_sample_fn(struct k_work *work)
enum mode_switch_mode mode = detect_mode(sample_mv); enum mode_switch_mode mode = detect_mode(sample_mv);
if (force_report || !mode_valid || (mode != last_mode)) { if (force_report || !mode_valid || (mode != last_mode)) {
struct mode_switch_event *event = new_mode_switch_event(); submit_mode_switch_event(mode, sample_mv);
event->mode = mode;
event->voltage_mv = sample_mv;
APP_EVENT_SUBMIT(event);
last_mode = mode; last_mode = mode;
mode_valid = true; mode_valid = true;

View File

@@ -172,81 +172,6 @@ static int encode_led_state(uint32_t led_mask, uint8_t *payload,
return encode_body(&body, payload, payload_buf_size, payload_len); return encode_body(&body, payload, payload_buf_size, payload_len);
} }
static int submit_cdc_proto_tx_event(uint8_t type, const uint8_t *payload,
size_t payload_len)
{
struct cdc_proto_tx_event *event;
if ((payload == NULL) && (payload_len > 0U)) {
return -EINVAL;
}
event = new_cdc_proto_tx_event(payload_len);
event->type = type;
if (payload_len > 0U) {
memcpy(event->dyndata.data, payload, payload_len);
}
APP_EVENT_SUBMIT(event);
return 0;
}
static int submit_function_bitmap_update_event(const Bitmap *bitmap)
{
struct function_bitmap_update_event *event;
if ((bitmap == NULL) ||
(bitmap->usage_bitmap.size != KEYBOARD_PROTOCOL_BITMAP_BYTES)) {
return -EINVAL;
}
event = new_function_bitmap_update_event();
memcpy(event->bitmap, bitmap->usage_bitmap.bytes,
KEYBOARD_PROTOCOL_BITMAP_BYTES);
APP_EVENT_SUBMIT(event);
return 0;
}
static int submit_theme_rgb_update_event(const ThemeRgb *theme_rgb)
{
struct theme_rgb_update_event *event;
if ((theme_rgb == NULL) ||
(theme_rgb->red > 255U) ||
(theme_rgb->green > 255U) ||
(theme_rgb->blue > 255U)) {
return -EINVAL;
}
event = new_theme_rgb_update_event();
event->theme.r = (uint8_t)theme_rgb->red;
event->theme.g = (uint8_t)theme_rgb->green;
event->theme.b = (uint8_t)theme_rgb->blue;
APP_EVENT_SUBMIT(event);
return 0;
}
static int submit_time_sync_event(const TimeSync *time_sync)
{
struct time_sync_event *event;
if (time_sync == NULL) {
return -EINVAL;
}
event = new_time_sync_event();
event->version = time_sync->version;
event->flags = time_sync->flags;
event->timezone_min = time_sync->timezone_min;
event->utc_ms = time_sync->utc_ms;
event->accuracy_ms = time_sync->accuracy_ms;
APP_EVENT_SUBMIT(event);
return 0;
}
static int encode_error_response(uint8_t req_type, ErrorCode error_code, static int encode_error_response(uint8_t req_type, ErrorCode error_code,
uint8_t *rsp_type, uint8_t *rsp_payload, uint8_t *rsp_type, uint8_t *rsp_payload,
size_t rsp_payload_buf_size, size_t rsp_payload_buf_size,
@@ -387,7 +312,8 @@ int protocol_module_process_cdc_packet(uint8_t req_type,
rsp_payload_len); rsp_payload_len);
} }
err = submit_function_bitmap_update_event(&body.body.bitmap); err = submit_function_bitmap_update_event(
body.body.bitmap.usage_bitmap.bytes);
if (err) { if (err) {
return encode_error_response(req_type, ErrorCode_ERROR_CODE_INVALID_PARAM, return encode_error_response(req_type, ErrorCode_ERROR_CODE_INVALID_PARAM,
rsp_type, rsp_payload, rsp_type, rsp_payload,
@@ -413,14 +339,11 @@ int protocol_module_process_cdc_packet(uint8_t req_type,
rsp_payload_len); rsp_payload_len);
} }
err = submit_time_sync_event(&body.body.time_sync); submit_time_sync_event(body.body.time_sync.version,
if (err) { body.body.time_sync.flags,
return encode_error_response(req_type, ErrorCode_ERROR_CODE_INVALID_PARAM, body.body.time_sync.timezone_min,
rsp_type, rsp_payload, body.body.time_sync.utc_ms,
rsp_payload_buf_size, body.body.time_sync.accuracy_ms);
rsp_payload_len);
}
return encode_ack_response(req_type, rsp_type, rsp_payload, return encode_ack_response(req_type, rsp_type, rsp_payload,
rsp_payload_buf_size, rsp_payload_len); rsp_payload_buf_size, rsp_payload_len);
@@ -432,14 +355,20 @@ int protocol_module_process_cdc_packet(uint8_t req_type,
rsp_payload_len); rsp_payload_len);
} }
err = submit_theme_rgb_update_event(&body.body.theme_rgb); if ((body.body.theme_rgb.red > 255U) ||
if (err) { (body.body.theme_rgb.green > 255U) ||
(body.body.theme_rgb.blue > 255U)) {
return encode_error_response(req_type, ErrorCode_ERROR_CODE_INVALID_PARAM, return encode_error_response(req_type, ErrorCode_ERROR_CODE_INVALID_PARAM,
rsp_type, rsp_payload, rsp_type, rsp_payload,
rsp_payload_buf_size, rsp_payload_buf_size,
rsp_payload_len); rsp_payload_len);
} }
submit_theme_rgb_update_event((struct theme_rgb) {
.r = (uint8_t)body.body.theme_rgb.red,
.g = (uint8_t)body.body.theme_rgb.green,
.b = (uint8_t)body.body.theme_rgb.blue,
});
return encode_ack_response(req_type, rsp_type, rsp_payload, return encode_ack_response(req_type, rsp_type, rsp_payload,
rsp_payload_buf_size, rsp_payload_len); rsp_payload_buf_size, rsp_payload_len);

View File

@@ -26,12 +26,13 @@ static int32_t timezone_min;
static uint64_t utc_ms_base; static uint64_t utc_ms_base;
static int64_t uptime_ms_base; static int64_t uptime_ms_base;
static void submit_datetime_event(void) static void publish_datetime_event(void)
{ {
struct datetime_event *event = new_datetime_event();
struct tm tm_buf; struct tm tm_buf;
time_t seconds; time_t seconds;
int64_t local_ms; int64_t local_ms;
char date_text[DATETIME_EVENT_DATE_TEXT_LEN];
char time_text[DATETIME_EVENT_TIME_TEXT_LEN];
local_ms = (int64_t)utc_ms_base + local_ms = (int64_t)utc_ms_base +
(k_uptime_get() - uptime_ms_base) + (k_uptime_get() - uptime_ms_base) +
@@ -42,22 +43,24 @@ static void submit_datetime_event(void)
seconds = (time_t)(local_ms / 1000LL); seconds = (time_t)(local_ms / 1000LL);
if (gmtime_r(&seconds, &tm_buf) == NULL) { if (gmtime_r(&seconds, &tm_buf) == NULL) {
strncpy(event->date_text, "1970/01/01", sizeof(event->date_text)); strncpy(date_text, "1970/01/01", sizeof(date_text));
strncpy(event->time_text, "00:00:00", sizeof(event->time_text)); date_text[sizeof(date_text) - 1] = '\0';
strncpy(time_text, "00:00:00", sizeof(time_text));
time_text[sizeof(time_text) - 1] = '\0';
} else { } else {
snprintk(event->date_text, sizeof(event->date_text), snprintk(date_text, sizeof(date_text),
"%04u/%02u/%02u", "%04u/%02u/%02u",
(unsigned int)(tm_buf.tm_year + 1900), (unsigned int)(tm_buf.tm_year + 1900),
(unsigned int)(tm_buf.tm_mon + 1), (unsigned int)(tm_buf.tm_mon + 1),
(unsigned int)tm_buf.tm_mday); (unsigned int)tm_buf.tm_mday);
snprintk(event->time_text, sizeof(event->time_text), snprintk(time_text, sizeof(time_text),
"%02u:%02u:%02u", "%02u:%02u:%02u",
(unsigned int)tm_buf.tm_hour, (unsigned int)tm_buf.tm_hour,
(unsigned int)tm_buf.tm_min, (unsigned int)tm_buf.tm_min,
(unsigned int)tm_buf.tm_sec); (unsigned int)tm_buf.tm_sec);
} }
APP_EVENT_SUBMIT(event); (void)submit_datetime_event(date_text, time_text);
} }
static void refresh_work_handler(struct k_work *work) static void refresh_work_handler(struct k_work *work)
@@ -68,7 +71,7 @@ static void refresh_work_handler(struct k_work *work)
return; return;
} }
submit_datetime_event(); publish_datetime_event();
k_work_reschedule(&refresh_work, TIME_SYNC_REFRESH_PERIOD); k_work_reschedule(&refresh_work, TIME_SYNC_REFRESH_PERIOD);
} }

View File

@@ -46,22 +46,6 @@ static bool usb_function_prepared;
static bool dtr_ready; static bool dtr_ready;
static bool rx_enabled; static bool rx_enabled;
static void submit_usb_cdc_rx_event(const uint8_t *data, size_t len)
{
struct usb_cdc_rx_event *event = new_usb_cdc_rx_event(len);
memcpy(event->dyndata.data, data, len);
APP_EVENT_SUBMIT(event);
}
static void submit_usb_function_ready_event(void)
{
struct usb_function_ready_event *event = new_usb_function_ready_event();
event->function_mask = USB_FUNCTION_CDC_ACM;
APP_EVENT_SUBMIT(event);
}
static void reset_ring_buffers(void) static void reset_ring_buffers(void)
{ {
unsigned int key = irq_lock(); unsigned int key = irq_lock();
@@ -145,7 +129,7 @@ static void rx_work_handler(struct k_work *work)
return; return;
} }
submit_usb_cdc_rx_event(buffer, len); (void)submit_usb_cdc_rx_event(buffer, len);
} }
} }
@@ -301,7 +285,7 @@ static bool handle_usb_prepare_event(const struct usb_prepare_event *event)
} }
usb_function_prepared = true; usb_function_prepared = true;
submit_usb_function_ready_event(); submit_usb_function_ready_event(USB_FUNCTION_CDC_ACM);
return false; return false;
} }

View File

@@ -1,230 +0,0 @@
#include <stdbool.h>
#include <string.h>
#include <app_event_manager.h>
#define MODULE usb_cdc_test_module
#include <caf/events/module_state_event.h>
#include <caf/events/power_event.h>
#include <caf/events/ble_common_event.h>
#include <zephyr/kernel.h>
#include <zephyr/logging/log.h>
#include "ble_serial_tx_event.h"
#include "usb_cdc_tx_event.h"
#include "usb_device_state_event.h"
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)
{
struct usb_cdc_tx_event *event =
new_usb_cdc_tx_event(sizeof(hello_message) - 1U);
memcpy(event->dyndata.data, hello_message, sizeof(hello_message) - 1U);
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) {
return;
}
if (usb_active) {
submit_hello_message();
}
if (ble_active) {
submit_ble_hello_message();
}
k_work_reschedule(&hello_work, USB_CDC_TEST_PERIOD);
}
static int module_init(void)
{
k_work_init_delayable(&hello_work, hello_work_handler);
return 0;
}
static int module_start(void)
{
if (running) {
return 0;
}
running = true;
if (usb_active || ble_active) {
k_work_reschedule(&hello_work, USB_CDC_TEST_PERIOD);
}
return 0;
}
static void module_pause(void)
{
if (!running) {
return;
}
k_work_cancel_delayable(&hello_work);
running = false;
}
static bool handle_usb_device_state_event(const struct usb_device_state_event *event)
{
bool new_usb_active = (event->state == USB_DEVICE_STATE_ACTIVE);
if (new_usb_active == usb_active) {
return false;
}
usb_active = new_usb_active;
if (!running) {
return false;
}
if (usb_active) {
k_work_reschedule(&hello_work, USB_CDC_TEST_PERIOD);
} else {
k_work_cancel_delayable(&hello_work);
}
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);
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, 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);

View File

@@ -48,21 +48,6 @@ static bool usb_enabled;
static uint8_t ready_function_mask; static uint8_t ready_function_mask;
static enum usb_device_state device_state = USB_DEVICE_STATE_DISCONNECTED; static enum usb_device_state device_state = USB_DEVICE_STATE_DISCONNECTED;
static void submit_usb_device_state_event(enum usb_device_state state)
{
struct usb_device_state_event *event = new_usb_device_state_event();
event->state = state;
APP_EVENT_SUBMIT(event);
}
static void submit_usb_prepare_event(void)
{
struct usb_prepare_event *event = new_usb_prepare_event();
APP_EVENT_SUBMIT(event);
}
static void update_usb_device_state(enum usb_device_state state) static void update_usb_device_state(enum usb_device_state state)
{ {
if (device_state == state) { if (device_state == state) {

View File

@@ -140,59 +140,16 @@ static void reset_usb_runtime_state(void)
consumer_report_in_flight = false; consumer_report_in_flight = false;
} }
static void submit_set_protocol_event(enum keyboard_protocol_mode protocol_mode) static void submit_usb_transport_state_event(void)
{ {
struct set_protocol_event *event = new_set_protocol_event();
event->transport = HID_TRANSPORT_USB;
event->protocol_mode = protocol_mode;
APP_EVENT_SUBMIT(event);
}
static void submit_hid_led_event(uint8_t led_bm)
{
struct hid_led_event *event = new_hid_led_event();
event->transport = HID_TRANSPORT_USB;
event->led_bm = led_bm;
APP_EVENT_SUBMIT(event);
}
static void submit_transport_state_event(void)
{
struct hid_transport_state_event *event = new_hid_transport_state_event();
bool ready = running && usb_active; bool ready = running && usb_active;
event->transport = HID_TRANSPORT_USB; submit_hid_transport_state_event(
event->ready = ready; HID_TRANSPORT_USB,
event->keys_ready = ready && ready,
hid_ifaces[USB_HID_INTERFACE_KEYBOARD].ready; ready && hid_ifaces[USB_HID_INTERFACE_KEYBOARD].ready,
event->consumer_ready = ready && ready && hid_ifaces[USB_HID_INTERFACE_CONSUMER].ready,
hid_ifaces[USB_HID_INTERFACE_CONSUMER].ready; keyboard_protocol_mode);
event->protocol_mode = keyboard_protocol_mode;
APP_EVENT_SUBMIT(event);
}
static void submit_hid_report_sent_event(enum keyboard_report_type report_type,
uint16_t sequence, bool error)
{
struct hid_report_sent_event *event = new_hid_report_sent_event();
event->transport = HID_TRANSPORT_USB;
event->report_type = report_type;
event->sequence = sequence;
event->error = error;
APP_EVENT_SUBMIT(event);
}
static void submit_usb_function_ready_event(void)
{
struct usb_function_ready_event *event = new_usb_function_ready_event();
event->function_mask = USB_FUNCTION_HID;
APP_EVENT_SUBMIT(event);
} }
static void keyboard_iface_ready(const struct device *dev, const bool ready) static void keyboard_iface_ready(const struct device *dev, const bool ready)
@@ -209,7 +166,7 @@ static void keyboard_iface_ready(const struct device *dev, const bool ready)
} }
LOG_INF("%s interface %s", dev->name, ready ? "ready" : "not ready"); LOG_INF("%s interface %s", dev->name, ready ? "ready" : "not ready");
submit_transport_state_event(); submit_usb_transport_state_event();
} }
static int keyboard_get_report(const struct device *dev, static int keyboard_get_report(const struct device *dev,
@@ -267,8 +224,8 @@ static void keyboard_set_protocol(const struct device *dev, const uint8_t proto)
keyboard_protocol_mode = new_mode; keyboard_protocol_mode = new_mode;
LOG_INF("USB keyboard protocol -> %s", LOG_INF("USB keyboard protocol -> %s",
(new_mode == KEYBOARD_PROTOCOL_MODE_BOOT) ? "boot" : "report"); (new_mode == KEYBOARD_PROTOCOL_MODE_BOOT) ? "boot" : "report");
submit_set_protocol_event(new_mode); submit_set_protocol_event(HID_TRANSPORT_USB, new_mode);
submit_transport_state_event(); submit_usb_transport_state_event();
} }
static void keyboard_input_report_done(const struct device *dev, static void keyboard_input_report_done(const struct device *dev,
@@ -278,7 +235,8 @@ static void keyboard_input_report_done(const struct device *dev,
keyboard_report_in_flight = false; keyboard_report_in_flight = false;
LOG_DBG("USB keyboard report sent by %s", dev->name); LOG_DBG("USB keyboard report sent by %s", dev->name);
submit_hid_report_sent_event(KEYBOARD_REPORT_TYPE_KEYS, submit_hid_report_sent_event(HID_TRANSPORT_USB,
KEYBOARD_REPORT_TYPE_KEYS,
keyboard_in_flight_sequence, false); keyboard_in_flight_sequence, false);
} }
@@ -293,7 +251,7 @@ static void keyboard_output_report(const struct device *dev,
return; return;
} }
submit_hid_led_event(buf[0]); submit_hid_led_event(HID_TRANSPORT_USB, buf[0]);
} }
static const struct hid_device_ops keyboard_ops = { static const struct hid_device_ops keyboard_ops = {
@@ -321,7 +279,7 @@ static void consumer_iface_ready(const struct device *dev, const bool ready)
} }
LOG_INF("%s interface %s", dev->name, ready ? "ready" : "not ready"); LOG_INF("%s interface %s", dev->name, ready ? "ready" : "not ready");
submit_transport_state_event(); submit_usb_transport_state_event();
} }
static int consumer_get_report(const struct device *dev, static int consumer_get_report(const struct device *dev,
@@ -379,7 +337,8 @@ static void consumer_input_report_done(const struct device *dev,
consumer_report_in_flight = false; consumer_report_in_flight = false;
LOG_DBG("USB consumer report sent by %s", dev->name); LOG_DBG("USB consumer report sent by %s", dev->name);
submit_hid_report_sent_event(KEYBOARD_REPORT_TYPE_CONSUMER, submit_hid_report_sent_event(HID_TRANSPORT_USB,
KEYBOARD_REPORT_TYPE_CONSUMER,
consumer_in_flight_sequence, false); consumer_in_flight_sequence, false);
} }
@@ -449,14 +408,14 @@ static int module_start(void)
} }
running = true; running = true;
submit_transport_state_event(); submit_usb_transport_state_event();
return 0; return 0;
} }
static void module_pause(void) static void module_pause(void)
{ {
running = false; running = false;
submit_transport_state_event(); submit_usb_transport_state_event();
} }
static bool handle_usb_prepare_event(const struct usb_prepare_event *event) static bool handle_usb_prepare_event(const struct usb_prepare_event *event)
@@ -477,7 +436,7 @@ static bool handle_usb_prepare_event(const struct usb_prepare_event *event)
} }
usb_function_prepared = true; usb_function_prepared = true;
submit_usb_function_ready_event(); submit_usb_function_ready_event(USB_FUNCTION_HID);
return false; return false;
} }
@@ -495,7 +454,7 @@ static bool handle_usb_device_state_event(const struct usb_device_state_event *e
usb_active = true; usb_active = true;
} }
submit_transport_state_event(); submit_usb_transport_state_event();
return false; return false;
} }
@@ -531,7 +490,8 @@ static bool handle_hid_tx_report_event(const struct hid_tx_report_event *event)
keyboard_tx_buf); keyboard_tx_buf);
if (err) { if (err) {
LOG_WRN("USB keyboard report submit failed (%d)", err); LOG_WRN("USB keyboard report submit failed (%d)", err);
submit_hid_report_sent_event(KEYBOARD_REPORT_TYPE_KEYS, submit_hid_report_sent_event(HID_TRANSPORT_USB,
KEYBOARD_REPORT_TYPE_KEYS,
event->sequence, true); event->sequence, true);
} else { } else {
keyboard_report_in_flight = true; keyboard_report_in_flight = true;
@@ -560,7 +520,8 @@ static bool handle_hid_tx_report_event(const struct hid_tx_report_event *event)
consumer_tx_buf); consumer_tx_buf);
if (err) { if (err) {
LOG_WRN("USB consumer report submit failed (%d)", err); LOG_WRN("USB consumer report submit failed (%d)", err);
submit_hid_report_sent_event(KEYBOARD_REPORT_TYPE_CONSUMER, submit_hid_report_sent_event(HID_TRANSPORT_USB,
KEYBOARD_REPORT_TYPE_CONSUMER,
event->sequence, true); event->sequence, true);
} else { } else {
consumer_report_in_flight = true; consumer_report_in_flight = true;