Compare commits
15 Commits
dev
...
2ca02325c1
| Author | SHA1 | Date | |
|---|---|---|---|
| 2ca02325c1 | |||
| bd57b00080 | |||
| 6125f04102 | |||
| 0a905d280d | |||
| 79af0eb025 | |||
| bc42a4dd63 | |||
| c4b205b8a1 | |||
| 78a6dc212d | |||
| c342a8d3f0 | |||
| 23e23f63a7 | |||
| ff8f0d81d7 | |||
| 968fb626bb | |||
| 30bc314698 | |||
| d86f0d6b78 | |||
| 15307dfde5 |
@@ -6,10 +6,12 @@ project(blinky)
|
||||
|
||||
list(APPEND CMAKE_MODULE_PATH ${ZEPHYR_BASE}/modules/nanopb)
|
||||
include(nanopb)
|
||||
zephyr_linker_sources(SECTIONS src/usb_function_hook.ld)
|
||||
|
||||
zephyr_include_directories(
|
||||
inc
|
||||
inc/events
|
||||
src
|
||||
)
|
||||
add_subdirectory(drivers)
|
||||
|
||||
@@ -20,33 +22,42 @@ zephyr_nanopb_sources(app
|
||||
target_sources(app PRIVATE
|
||||
src/main.c
|
||||
src/battery_module.c
|
||||
src/ble_adv_ctrl_module.c
|
||||
src/ble_adv_uuid16.c
|
||||
src/ble_bas_module.c
|
||||
src/ble_hid_module.c
|
||||
src/ble_nus_module.c
|
||||
src/display_module.c
|
||||
src/encoder_module.c
|
||||
src/hid_flowctrl_module.c
|
||||
src/keyboard_core_module.c
|
||||
src/led_effect/led_effect_registry.c
|
||||
src/led_effect/effects/led_effect_key_fade.c
|
||||
src/led_strip_module.c
|
||||
src/mode_policy_module.c
|
||||
src/time_sync_module.c
|
||||
src/ui/ui_main.c
|
||||
src/cdc_wrapper_module.c
|
||||
src/protocol_module.c
|
||||
src/usb_cdc_module.c
|
||||
src/usb_device_module.c
|
||||
src/usb_hid_module.c
|
||||
src/usb_hid_consumer_module.c
|
||||
src/usb_hid_keyboard_module.c
|
||||
src/events/bat_state_event.c
|
||||
src/events/datetime_event.c
|
||||
src/events/encoder_event.c
|
||||
src/events/function_bitmap_state_event.c
|
||||
src/events/function_bitmap_update_event.c
|
||||
src/events/hid_channel_state_event.c
|
||||
src/events/hid_led_event.c
|
||||
src/events/hid_report_sent_event.c
|
||||
src/events/hid_transport_state_event.c
|
||||
src/events/hid_tx_report_event.c
|
||||
src/events/led_strip_en_event.c
|
||||
src/mode_switch_module.c
|
||||
src/events/keyboard_hid_report_event.c
|
||||
src/events/mode_switch_event.c
|
||||
src/events/proto_rx_event.c
|
||||
src/events/proto_tx_event.c
|
||||
src/events/set_protocol_event.c
|
||||
src/events/usb_cdc_rx_event.c
|
||||
src/events/usb_cdc_tx_event.c
|
||||
src/events/usb_device_state_event.c
|
||||
src/events/usb_function_ready_event.c
|
||||
src/events/usb_prepare_event.c
|
||||
src/events/theme_rgb_update_event.c
|
||||
src/events/time_sync_event.c
|
||||
src/events/usb_state_event.c
|
||||
)
|
||||
|
||||
@@ -46,6 +46,21 @@
|
||||
};
|
||||
};
|
||||
|
||||
spi1_ws2812_default: spi1_ws2812_default {
|
||||
group1 {
|
||||
psels = <NRF_PSEL(SPIM_SCK, 1, 4)>,
|
||||
<NRF_PSEL(SPIM_MOSI, 0, 20)>;
|
||||
};
|
||||
};
|
||||
|
||||
spi1_ws2812_sleep: spi1_ws2812_sleep {
|
||||
group1 {
|
||||
psels = <NRF_PSEL(SPIM_SCK, 1, 4)>,
|
||||
<NRF_PSEL(SPIM_MOSI, 0, 20)>;
|
||||
low-power-enable;
|
||||
};
|
||||
};
|
||||
|
||||
pwm0_default: pwm0_default {
|
||||
group1 {
|
||||
psels = <NRF_PSEL(PWM_OUT0, 1, 11)>;
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
#include <nordic/nrf52840_qiaa.dtsi>
|
||||
#include "mini_keyboard-pinctrl.dtsi"
|
||||
#include <zephyr/dt-bindings/adc/adc.h>
|
||||
#include <zephyr/dt-bindings/led/led.h>
|
||||
#include <zephyr/dt-bindings/mipi_dbi/mipi_dbi.h>
|
||||
#include <zephyr/dt-bindings/pwm/pwm.h>
|
||||
|
||||
@@ -14,10 +15,12 @@
|
||||
zephyr,flash = &flash0;
|
||||
zephyr,code-partition = &slot0_partition;
|
||||
zephyr,display = &screen_lcd;
|
||||
zephyr,led-strip = &led_strip;
|
||||
};
|
||||
|
||||
aliases {
|
||||
led0 = &myled0;
|
||||
led-strip = &led_strip;
|
||||
qdec0 = &qdec;
|
||||
backlight = &backlight;
|
||||
};
|
||||
@@ -27,7 +30,7 @@
|
||||
label = "HID_KBD";
|
||||
protocol-code = "keyboard";
|
||||
in-report-size = <29>;
|
||||
out-report-size = <1>;
|
||||
out-report-size = <29>;
|
||||
in-polling-period-us = <1000>;
|
||||
out-polling-period-us = <1000>;
|
||||
};
|
||||
@@ -206,6 +209,28 @@
|
||||
cs-gpios = <&gpio0 2 GPIO_ACTIVE_LOW>;
|
||||
};
|
||||
|
||||
&spi1 {
|
||||
status = "okay";
|
||||
pinctrl-0 = <&spi1_ws2812_default>;
|
||||
pinctrl-1 = <&spi1_ws2812_sleep>;
|
||||
pinctrl-names = "default", "sleep";
|
||||
|
||||
led_strip: ws2812@0 {
|
||||
compatible = "worldsemi,ws2812-spi";
|
||||
reg = <0>;
|
||||
spi-max-frequency = <6400000>;
|
||||
chain-length = <17>;
|
||||
color-mapping = <LED_COLOR_ID_GREEN
|
||||
LED_COLOR_ID_RED
|
||||
LED_COLOR_ID_BLUE>;
|
||||
spi-one-frame = <0x70>;
|
||||
spi-zero-frame = <0x40>;
|
||||
bits-per-symbol = <8>;
|
||||
reset-delay = <8>;
|
||||
supply-gpios = <&gpio0 13 GPIO_ACTIVE_HIGH>;
|
||||
};
|
||||
};
|
||||
|
||||
&pwm0 {
|
||||
status = "okay";
|
||||
pinctrl-0 = <&pwm0_default>;
|
||||
|
||||
@@ -1,12 +0,0 @@
|
||||
#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_ */
|
||||
@@ -20,6 +20,16 @@ struct 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
|
||||
}
|
||||
#endif
|
||||
|
||||
45
inc/events/datetime_event.h
Normal file
45
inc/events/datetime_event.h
Normal file
@@ -0,0 +1,45 @@
|
||||
#ifndef BLINKY_DATETIME_EVENT_H_
|
||||
#define BLINKY_DATETIME_EVENT_H_
|
||||
|
||||
#include <errno.h>
|
||||
#include <string.h>
|
||||
|
||||
#include <app_event_manager.h>
|
||||
#include <app_event_manager_profiler_tracer.h>
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
#define DATETIME_EVENT_DATE_TEXT_LEN 16
|
||||
#define DATETIME_EVENT_TIME_TEXT_LEN 16
|
||||
|
||||
struct datetime_event {
|
||||
struct app_event_header header;
|
||||
char date_text[DATETIME_EVENT_DATE_TEXT_LEN];
|
||||
char time_text[DATETIME_EVENT_TIME_TEXT_LEN];
|
||||
};
|
||||
|
||||
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
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* BLINKY_DATETIME_EVENT_H_ */
|
||||
@@ -15,6 +15,14 @@ struct 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
|
||||
}
|
||||
#endif
|
||||
|
||||
41
inc/events/function_bitmap_state_event.h
Normal file
41
inc/events/function_bitmap_state_event.h
Normal file
@@ -0,0 +1,41 @@
|
||||
#ifndef BLINKY_FUNCTION_BITMAP_STATE_EVENT_H_
|
||||
#define BLINKY_FUNCTION_BITMAP_STATE_EVENT_H_
|
||||
|
||||
#include <errno.h>
|
||||
#include <string.h>
|
||||
|
||||
#include <app_event_manager.h>
|
||||
#include <app_event_manager_profiler_tracer.h>
|
||||
|
||||
#include "keyboard_core.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
struct function_bitmap_state_event {
|
||||
struct app_event_header header;
|
||||
uint8_t bitmap[KEYBOARD_PROTOCOL_BITMAP_BYTES];
|
||||
};
|
||||
|
||||
APP_EVENT_TYPE_DECLARE(function_bitmap_state_event);
|
||||
|
||||
static inline int submit_function_bitmap_state_event(const uint8_t *bitmap)
|
||||
{
|
||||
struct function_bitmap_state_event *event;
|
||||
|
||||
if (bitmap == NULL) {
|
||||
return -EINVAL;
|
||||
}
|
||||
|
||||
event = new_function_bitmap_state_event();
|
||||
memcpy(event->bitmap, bitmap, sizeof(event->bitmap));
|
||||
APP_EVENT_SUBMIT(event);
|
||||
return 0;
|
||||
}
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* BLINKY_FUNCTION_BITMAP_STATE_EVENT_H_ */
|
||||
41
inc/events/function_bitmap_update_event.h
Normal file
41
inc/events/function_bitmap_update_event.h
Normal file
@@ -0,0 +1,41 @@
|
||||
#ifndef 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_profiler_tracer.h>
|
||||
|
||||
#include "keyboard_core.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
struct function_bitmap_update_event {
|
||||
struct app_event_header header;
|
||||
uint8_t bitmap[KEYBOARD_PROTOCOL_BITMAP_BYTES];
|
||||
};
|
||||
|
||||
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
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* BLINKY_FUNCTION_BITMAP_UPDATE_EVENT_H_ */
|
||||
38
inc/events/hid_channel_state_event.h
Normal file
38
inc/events/hid_channel_state_event.h
Normal file
@@ -0,0 +1,38 @@
|
||||
#ifndef BLINKY_HID_CHANNEL_STATE_EVENT_H_
|
||||
#define BLINKY_HID_CHANNEL_STATE_EVENT_H_
|
||||
|
||||
#include <app_event_manager.h>
|
||||
#include <app_event_manager_profiler_tracer.h>
|
||||
|
||||
#include "keyboard_core.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
struct hid_channel_state_event {
|
||||
struct app_event_header header;
|
||||
enum hid_send_channel channel;
|
||||
uint8_t report_ready_bm;
|
||||
enum keyboard_protocol_mode protocol_mode;
|
||||
};
|
||||
|
||||
APP_EVENT_TYPE_DECLARE(hid_channel_state_event);
|
||||
|
||||
static inline void submit_hid_channel_state_event(
|
||||
enum hid_send_channel channel, uint8_t report_ready_bm,
|
||||
enum keyboard_protocol_mode protocol_mode)
|
||||
{
|
||||
struct hid_channel_state_event *event = new_hid_channel_state_event();
|
||||
|
||||
event->channel = channel;
|
||||
event->report_ready_bm = report_ready_bm;
|
||||
event->protocol_mode = protocol_mode;
|
||||
APP_EVENT_SUBMIT(event);
|
||||
}
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* BLINKY_HID_CHANNEL_STATE_EVENT_H_ */
|
||||
@@ -18,6 +18,15 @@ struct 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
|
||||
}
|
||||
#endif
|
||||
|
||||
@@ -12,7 +12,7 @@ extern "C" {
|
||||
|
||||
struct hid_report_sent_event {
|
||||
struct app_event_header header;
|
||||
enum hid_transport transport;
|
||||
enum hid_send_channel channel;
|
||||
enum keyboard_report_type report_type;
|
||||
uint16_t sequence;
|
||||
bool error;
|
||||
@@ -20,6 +20,19 @@ struct hid_report_sent_event {
|
||||
|
||||
APP_EVENT_TYPE_DECLARE(hid_report_sent_event);
|
||||
|
||||
static inline void submit_hid_report_sent_event(enum hid_send_channel channel,
|
||||
enum keyboard_report_type report_type,
|
||||
uint16_t sequence, bool error)
|
||||
{
|
||||
struct hid_report_sent_event *event = new_hid_report_sent_event();
|
||||
|
||||
event->channel = channel;
|
||||
event->report_type = report_type;
|
||||
event->sequence = sequence;
|
||||
event->error = error;
|
||||
APP_EVENT_SUBMIT(event);
|
||||
}
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
@@ -1,28 +0,0 @@
|
||||
#ifndef BLINKY_HID_TRANSPORT_STATE_EVENT_H_
|
||||
#define BLINKY_HID_TRANSPORT_STATE_EVENT_H_
|
||||
|
||||
#include <app_event_manager.h>
|
||||
#include <app_event_manager_profiler_tracer.h>
|
||||
|
||||
#include "keyboard_core.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
struct hid_transport_state_event {
|
||||
struct app_event_header header;
|
||||
enum hid_transport transport;
|
||||
bool ready;
|
||||
bool keys_ready;
|
||||
bool consumer_ready;
|
||||
enum keyboard_protocol_mode protocol_mode;
|
||||
};
|
||||
|
||||
APP_EVENT_TYPE_DECLARE(hid_transport_state_event);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* BLINKY_HID_TRANSPORT_STATE_EVENT_H_ */
|
||||
@@ -1,6 +1,10 @@
|
||||
#ifndef 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_profiler_tracer.h>
|
||||
|
||||
@@ -12,7 +16,7 @@ extern "C" {
|
||||
|
||||
struct hid_tx_report_event {
|
||||
struct app_event_header header;
|
||||
enum hid_transport transport;
|
||||
enum hid_send_channel channel;
|
||||
enum keyboard_report_type report_type;
|
||||
enum keyboard_protocol_mode protocol_mode;
|
||||
uint16_t sequence;
|
||||
@@ -21,6 +25,31 @@ struct hid_tx_report_event {
|
||||
|
||||
APP_EVENT_TYPE_DYNDATA_DECLARE(hid_tx_report_event);
|
||||
|
||||
static inline int submit_hid_tx_report_event(enum hid_send_channel channel,
|
||||
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->channel = channel;
|
||||
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
|
||||
}
|
||||
#endif
|
||||
|
||||
@@ -1,6 +1,10 @@
|
||||
#ifndef 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_profiler_tracer.h>
|
||||
|
||||
@@ -22,6 +26,30 @@ struct 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
|
||||
}
|
||||
#endif
|
||||
|
||||
32
inc/events/led_strip_en_event.h
Normal file
32
inc/events/led_strip_en_event.h
Normal file
@@ -0,0 +1,32 @@
|
||||
#ifndef BLINKY_LED_STRIP_EN_EVENT_H_
|
||||
#define BLINKY_LED_STRIP_EN_EVENT_H_
|
||||
|
||||
#include <stdbool.h>
|
||||
|
||||
#include <app_event_manager.h>
|
||||
#include <app_event_manager_profiler_tracer.h>
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
struct led_strip_en_event {
|
||||
struct app_event_header header;
|
||||
bool enabled;
|
||||
};
|
||||
|
||||
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
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* BLINKY_LED_STRIP_EN_EVENT_H_ */
|
||||
@@ -22,6 +22,16 @@ struct 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
|
||||
}
|
||||
#endif
|
||||
|
||||
18
inc/events/proto_common.h
Normal file
18
inc/events/proto_common.h
Normal file
@@ -0,0 +1,18 @@
|
||||
#ifndef BLINKY_PROTO_COMMON_H_
|
||||
#define BLINKY_PROTO_COMMON_H_
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
enum proto_transport {
|
||||
PROTO_TRANSPORT_USB_CDC = 0,
|
||||
PROTO_TRANSPORT_BLE_NUS,
|
||||
PROTO_TRANSPORT_COUNT,
|
||||
};
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* BLINKY_PROTO_COMMON_H_ */
|
||||
50
inc/events/proto_rx_event.h
Normal file
50
inc/events/proto_rx_event.h
Normal file
@@ -0,0 +1,50 @@
|
||||
#ifndef BLINKY_PROTO_RX_EVENT_H_
|
||||
#define BLINKY_PROTO_RX_EVENT_H_
|
||||
|
||||
#include <errno.h>
|
||||
#include <stddef.h>
|
||||
#include <string.h>
|
||||
|
||||
#include <app_event_manager.h>
|
||||
#include <app_event_manager_profiler_tracer.h>
|
||||
|
||||
#include "proto_common.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
struct proto_rx_event {
|
||||
struct app_event_header header;
|
||||
enum proto_transport transport;
|
||||
struct event_dyndata dyndata;
|
||||
};
|
||||
|
||||
APP_EVENT_TYPE_DYNDATA_DECLARE(proto_rx_event);
|
||||
|
||||
static inline int submit_proto_rx_event(enum proto_transport transport,
|
||||
const uint8_t *data, size_t len)
|
||||
{
|
||||
struct proto_rx_event *event;
|
||||
|
||||
if ((transport >= PROTO_TRANSPORT_COUNT) ||
|
||||
((data == NULL) && (len > 0U))) {
|
||||
return -EINVAL;
|
||||
}
|
||||
|
||||
event = new_proto_rx_event(len);
|
||||
event->transport = transport;
|
||||
|
||||
if (len > 0U) {
|
||||
memcpy(event->dyndata.data, data, len);
|
||||
}
|
||||
|
||||
APP_EVENT_SUBMIT(event);
|
||||
return 0;
|
||||
}
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* BLINKY_PROTO_RX_EVENT_H_ */
|
||||
50
inc/events/proto_tx_event.h
Normal file
50
inc/events/proto_tx_event.h
Normal file
@@ -0,0 +1,50 @@
|
||||
#ifndef BLINKY_PROTO_TX_EVENT_H_
|
||||
#define BLINKY_PROTO_TX_EVENT_H_
|
||||
|
||||
#include <errno.h>
|
||||
#include <stddef.h>
|
||||
#include <string.h>
|
||||
|
||||
#include <app_event_manager.h>
|
||||
#include <app_event_manager_profiler_tracer.h>
|
||||
|
||||
#include "proto_common.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
struct proto_tx_event {
|
||||
struct app_event_header header;
|
||||
enum proto_transport transport;
|
||||
struct event_dyndata dyndata;
|
||||
};
|
||||
|
||||
APP_EVENT_TYPE_DYNDATA_DECLARE(proto_tx_event);
|
||||
|
||||
static inline int submit_proto_tx_event(enum proto_transport transport,
|
||||
const uint8_t *data, size_t len)
|
||||
{
|
||||
struct proto_tx_event *event;
|
||||
|
||||
if ((transport >= PROTO_TRANSPORT_COUNT) ||
|
||||
((data == NULL) && (len > 0U))) {
|
||||
return -EINVAL;
|
||||
}
|
||||
|
||||
event = new_proto_tx_event(len);
|
||||
event->transport = transport;
|
||||
|
||||
if (len > 0U) {
|
||||
memcpy(event->dyndata.data, data, len);
|
||||
}
|
||||
|
||||
APP_EVENT_SUBMIT(event);
|
||||
return 0;
|
||||
}
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* BLINKY_PROTO_TX_EVENT_H_ */
|
||||
@@ -18,6 +18,16 @@ struct 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
|
||||
}
|
||||
#endif
|
||||
|
||||
32
inc/events/theme_rgb_update_event.h
Normal file
32
inc/events/theme_rgb_update_event.h
Normal file
@@ -0,0 +1,32 @@
|
||||
#ifndef BLINKY_THEME_RGB_UPDATE_EVENT_H_
|
||||
#define BLINKY_THEME_RGB_UPDATE_EVENT_H_
|
||||
|
||||
#include <app_event_manager.h>
|
||||
#include <app_event_manager_profiler_tracer.h>
|
||||
|
||||
#include "theme_color.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
struct theme_rgb_update_event {
|
||||
struct app_event_header header;
|
||||
struct theme_rgb theme;
|
||||
};
|
||||
|
||||
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
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* BLINKY_THEME_RGB_UPDATE_EVENT_H_ */
|
||||
40
inc/events/time_sync_event.h
Normal file
40
inc/events/time_sync_event.h
Normal file
@@ -0,0 +1,40 @@
|
||||
#ifndef BLINKY_TIME_SYNC_EVENT_H_
|
||||
#define BLINKY_TIME_SYNC_EVENT_H_
|
||||
|
||||
#include <app_event_manager.h>
|
||||
#include <app_event_manager_profiler_tracer.h>
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
struct time_sync_event {
|
||||
struct app_event_header header;
|
||||
uint32_t version;
|
||||
uint32_t flags;
|
||||
int32_t timezone_min;
|
||||
uint64_t utc_ms;
|
||||
uint32_t accuracy_ms;
|
||||
};
|
||||
|
||||
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
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* BLINKY_TIME_SYNC_EVENT_H_ */
|
||||
@@ -1,22 +0,0 @@
|
||||
#ifndef BLINKY_USB_CDC_RX_EVENT_H_
|
||||
#define BLINKY_USB_CDC_RX_EVENT_H_
|
||||
|
||||
#include <app_event_manager.h>
|
||||
#include <app_event_manager_profiler_tracer.h>
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
struct usb_cdc_rx_event {
|
||||
struct app_event_header header;
|
||||
struct event_dyndata dyndata;
|
||||
};
|
||||
|
||||
APP_EVENT_TYPE_DYNDATA_DECLARE(usb_cdc_rx_event);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* BLINKY_USB_CDC_RX_EVENT_H_ */
|
||||
@@ -1,22 +0,0 @@
|
||||
#ifndef BLINKY_USB_CDC_TX_EVENT_H_
|
||||
#define BLINKY_USB_CDC_TX_EVENT_H_
|
||||
|
||||
#include <app_event_manager.h>
|
||||
#include <app_event_manager_profiler_tracer.h>
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
struct usb_cdc_tx_event {
|
||||
struct app_event_header header;
|
||||
struct event_dyndata dyndata;
|
||||
};
|
||||
|
||||
APP_EVENT_TYPE_DYNDATA_DECLARE(usb_cdc_tx_event);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* BLINKY_USB_CDC_TX_EVENT_H_ */
|
||||
@@ -1,24 +0,0 @@
|
||||
#ifndef BLINKY_USB_DEVICE_STATE_EVENT_H_
|
||||
#define BLINKY_USB_DEVICE_STATE_EVENT_H_
|
||||
|
||||
#include <app_event_manager.h>
|
||||
#include <app_event_manager_profiler_tracer.h>
|
||||
|
||||
#include "usb_device_module.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
struct usb_device_state_event {
|
||||
struct app_event_header header;
|
||||
enum usb_device_state state;
|
||||
};
|
||||
|
||||
APP_EVENT_TYPE_DECLARE(usb_device_state_event);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* BLINKY_USB_DEVICE_STATE_EVENT_H_ */
|
||||
@@ -1,24 +0,0 @@
|
||||
#ifndef BLINKY_USB_FUNCTION_READY_EVENT_H_
|
||||
#define BLINKY_USB_FUNCTION_READY_EVENT_H_
|
||||
|
||||
#include <app_event_manager.h>
|
||||
#include <app_event_manager_profiler_tracer.h>
|
||||
|
||||
#include "usb_device_module.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
struct usb_function_ready_event {
|
||||
struct app_event_header header;
|
||||
uint8_t function_mask;
|
||||
};
|
||||
|
||||
APP_EVENT_TYPE_DECLARE(usb_function_ready_event);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* BLINKY_USB_FUNCTION_READY_EVENT_H_ */
|
||||
@@ -1,21 +0,0 @@
|
||||
#ifndef BLINKY_USB_PREPARE_EVENT_H_
|
||||
#define BLINKY_USB_PREPARE_EVENT_H_
|
||||
|
||||
#include <app_event_manager.h>
|
||||
#include <app_event_manager_profiler_tracer.h>
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
struct usb_prepare_event {
|
||||
struct app_event_header header;
|
||||
};
|
||||
|
||||
APP_EVENT_TYPE_DECLARE(usb_prepare_event);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* BLINKY_USB_PREPARE_EVENT_H_ */
|
||||
41
inc/events/usb_state_event.h
Normal file
41
inc/events/usb_state_event.h
Normal file
@@ -0,0 +1,41 @@
|
||||
#ifndef BLINKY_USB_STATE_EVENT_H_
|
||||
#define BLINKY_USB_STATE_EVENT_H_
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
#include <app_event_manager.h>
|
||||
#include <app_event_manager_profiler_tracer.h>
|
||||
#include <zephyr/sys/util.h>
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
enum usb_state {
|
||||
USB_STATE_DISABLED = 0,
|
||||
USB_STATE_DISCONNECTED,
|
||||
USB_STATE_POWERED,
|
||||
USB_STATE_ACTIVE,
|
||||
USB_STATE_SUSPENDED,
|
||||
};
|
||||
|
||||
struct usb_state_event {
|
||||
struct app_event_header header;
|
||||
enum usb_state state;
|
||||
};
|
||||
|
||||
APP_EVENT_TYPE_DECLARE(usb_state_event);
|
||||
|
||||
static inline void submit_usb_state(enum usb_state state)
|
||||
{
|
||||
struct usb_state_event *event = new_usb_state_event();
|
||||
|
||||
event->state = state;
|
||||
APP_EVENT_SUBMIT(event);
|
||||
}
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* BLINKY_USB_STATE_EVENT_H_ */
|
||||
@@ -8,6 +8,8 @@ extern "C" {
|
||||
#endif
|
||||
|
||||
#define KEYBOARD_BOOT_REPORT_SIZE 8U
|
||||
#define KEYBOARD_PROTOCOL_USAGE_MAX 0xE7U
|
||||
#define KEYBOARD_PROTOCOL_BITMAP_BYTES ((KEYBOARD_PROTOCOL_USAGE_MAX + 8U) / 8U)
|
||||
#define KEYBOARD_NKRO_USAGE_MAX 0xDFU
|
||||
#define KEYBOARD_NKRO_BITMAP_BYTES ((KEYBOARD_NKRO_USAGE_MAX + 8U) / 8U)
|
||||
#define KEYBOARD_NKRO_REPORT_SIZE (1U + KEYBOARD_NKRO_BITMAP_BYTES)
|
||||
@@ -34,6 +36,13 @@ enum hid_transport {
|
||||
HID_TRANSPORT_COUNT,
|
||||
};
|
||||
|
||||
enum hid_send_channel {
|
||||
HID_SEND_CH_USB_KEYS,
|
||||
HID_SEND_CH_USB_CONSUMER,
|
||||
HID_SEND_CH_BLE_SHARED,
|
||||
HID_SEND_CH_COUNT,
|
||||
};
|
||||
|
||||
enum keyboard_consumer_control {
|
||||
KEYBOARD_CONSUMER_CTRL_MUTE,
|
||||
KEYBOARD_CONSUMER_CTRL_VOLUME_UP,
|
||||
|
||||
@@ -1,24 +1,25 @@
|
||||
#ifndef BLINKY_PROTOCOL_MODULE_H_
|
||||
#define BLINKY_PROTOCOL_MODULE_H_
|
||||
|
||||
#include <stdbool.h>
|
||||
#include <stddef.h>
|
||||
#include <stdint.h>
|
||||
|
||||
#include "proto_common.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,
|
||||
int protocol_module_process_message(enum proto_transport transport,
|
||||
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);
|
||||
|
||||
void protocol_module_reset_transport_state(enum proto_transport transport);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
24
inc/theme_color.h
Normal file
24
inc/theme_color.h
Normal file
@@ -0,0 +1,24 @@
|
||||
#ifndef BLINKY_THEME_COLOR_H_
|
||||
#define BLINKY_THEME_COLOR_H_
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
#define BLINKY_THEME_DEFAULT_R 0x4CU
|
||||
#define BLINKY_THEME_DEFAULT_G 0x9EU
|
||||
#define BLINKY_THEME_DEFAULT_B 0xF5U
|
||||
|
||||
struct theme_rgb {
|
||||
uint8_t r;
|
||||
uint8_t g;
|
||||
uint8_t b;
|
||||
};
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* BLINKY_THEME_COLOR_H_ */
|
||||
@@ -1,26 +0,0 @@
|
||||
#ifndef BLINKY_USB_DEVICE_MODULE_H_
|
||||
#define BLINKY_USB_DEVICE_MODULE_H_
|
||||
|
||||
#include <stdbool.h>
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
enum usb_function {
|
||||
USB_FUNCTION_HID = 0x01,
|
||||
USB_FUNCTION_CDC_ACM = 0x02,
|
||||
};
|
||||
|
||||
enum usb_device_state {
|
||||
USB_DEVICE_STATE_DISCONNECTED,
|
||||
USB_DEVICE_STATE_POWERED,
|
||||
USB_DEVICE_STATE_ACTIVE,
|
||||
USB_DEVICE_STATE_SUSPENDED,
|
||||
};
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* BLINKY_USB_DEVICE_MODULE_H_ */
|
||||
25
inc/usb_function_hook.h
Normal file
25
inc/usb_function_hook.h
Normal file
@@ -0,0 +1,25 @@
|
||||
#ifndef BLINKY_USB_FUNCTION_HOOK_H_
|
||||
#define BLINKY_USB_FUNCTION_HOOK_H_
|
||||
|
||||
#include <zephyr/sys/iterable_sections.h>
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
struct usb_function_hook {
|
||||
const char *name;
|
||||
int (*pre_stack_init)(void);
|
||||
};
|
||||
|
||||
#define USB_FUNCTION_HOOK_DEFINE(_name, _pre_stack_init) \
|
||||
const STRUCT_SECTION_ITERABLE(usb_function_hook, _name) = { \
|
||||
.name = STRINGIFY(_name), \
|
||||
.pre_stack_init = (_pre_stack_init), \
|
||||
}
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* BLINKY_USB_FUNCTION_HOOK_H_ */
|
||||
7
prj.conf
7
prj.conf
@@ -27,6 +27,9 @@ CONFIG_NVS=y
|
||||
CONFIG_HEAP_MEM_POOL_SIZE=4096
|
||||
CONFIG_LOG=y
|
||||
CONFIG_ASSERT=y
|
||||
CONFIG_APP_EVENT_MANAGER_MAX_EVENT_CNT=64
|
||||
CONFIG_LED_STRIP=y
|
||||
CONFIG_WS2812_STRIP_SPI=y
|
||||
|
||||
# USB HID next stack
|
||||
CONFIG_USB_DEVICE_STACK_NEXT=y
|
||||
@@ -42,12 +45,16 @@ CONFIG_CDC_ACM_SERIAL_INITIALIZE_AT_BOOT=n
|
||||
# BLE
|
||||
CONFIG_BT=y
|
||||
CONFIG_BT_PERIPHERAL=y
|
||||
CONFIG_BT_ZEPHYR_NUS=y
|
||||
CONFIG_BT_SMP=y
|
||||
CONFIG_BT_BONDABLE=y
|
||||
CONFIG_BT_SETTINGS=y
|
||||
CONFIG_BT_MAX_CONN=1
|
||||
CONFIG_BT_MAX_PAIRED=1
|
||||
CONFIG_BT_ATT_TX_COUNT=5
|
||||
CONFIG_BT_L2CAP_TX_MTU=65
|
||||
CONFIG_BT_BUF_ACL_RX_SIZE=69
|
||||
CONFIG_BT_BUF_ACL_TX_SIZE=69
|
||||
CONFIG_BT_CONN_CTX=y
|
||||
CONFIG_BT_DEVICE_NAME="WH Mini Keyboard"
|
||||
CONFIG_BT_DEVICE_APPEARANCE=961
|
||||
|
||||
2
proto/device_comm.options
Normal file
2
proto/device_comm.options
Normal file
@@ -0,0 +1,2 @@
|
||||
Bitmap.usage_bitmap max_size:29
|
||||
FunctionKeyEvent.usage_bitmap max_size:29
|
||||
@@ -13,9 +13,59 @@ message HelloRsp {
|
||||
uint32 capability_flags = 6;
|
||||
}
|
||||
|
||||
message Bitmap {
|
||||
bytes usage_bitmap = 1;
|
||||
}
|
||||
|
||||
message FunctionKeyEvent {
|
||||
bytes usage_bitmap = 1;
|
||||
}
|
||||
|
||||
message LedState {
|
||||
uint32 led_mask = 1;
|
||||
}
|
||||
|
||||
message TimeSync {
|
||||
uint32 version = 1;
|
||||
uint32 flags = 2;
|
||||
sint32 timezone_min = 3;
|
||||
fixed64 utc_ms = 4;
|
||||
fixed32 accuracy_ms = 5;
|
||||
}
|
||||
|
||||
message ThemeRgb {
|
||||
uint32 red = 1;
|
||||
uint32 green = 2;
|
||||
uint32 blue = 3;
|
||||
}
|
||||
|
||||
message Ack {
|
||||
uint32 acked_type = 1;
|
||||
}
|
||||
|
||||
enum ErrorCode {
|
||||
ERROR_CODE_NONE = 0;
|
||||
ERROR_CODE_UNKNOWN_TYPE = 1;
|
||||
ERROR_CODE_INVALID_LENGTH = 2;
|
||||
ERROR_CODE_INVALID_PARAM = 3;
|
||||
ERROR_CODE_NOT_READY = 4;
|
||||
}
|
||||
|
||||
message Error {
|
||||
uint32 error_type = 1;
|
||||
ErrorCode error_code = 2;
|
||||
}
|
||||
|
||||
message CdcPacketBody {
|
||||
oneof body {
|
||||
HelloReq hello_req = 1;
|
||||
HelloRsp hello_rsp = 2;
|
||||
Bitmap bitmap = 3;
|
||||
FunctionKeyEvent function_key_event = 4;
|
||||
LedState led_state = 5;
|
||||
TimeSync time_sync = 6;
|
||||
ThemeRgb theme_rgb = 7;
|
||||
Ack ack = 8;
|
||||
Error error = 9;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -81,30 +81,6 @@ static uint8_t battery_soc_from_mv(int voltage_mv)
|
||||
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)
|
||||
{
|
||||
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);
|
||||
submit_bat_state_event(battery_soc_from_mv(voltage_mv),
|
||||
pmic_status.charging,
|
||||
pmic_status.full);
|
||||
uint8_t soc = battery_soc_from_mv(voltage_mv);
|
||||
|
||||
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:
|
||||
if (running) {
|
||||
|
||||
@@ -14,8 +14,8 @@
|
||||
#include <zephyr/logging/log.h>
|
||||
|
||||
#include "hid_led_event.h"
|
||||
#include "hid_channel_state_event.h"
|
||||
#include "hid_report_sent_event.h"
|
||||
#include "hid_transport_state_event.h"
|
||||
#include "hid_tx_report_event.h"
|
||||
#include "keyboard_core.h"
|
||||
#include "set_protocol_event.h"
|
||||
@@ -98,54 +98,26 @@ static const uint8_t hid_report_desc[] = {
|
||||
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);
|
||||
uint8_t report_ready_bm = 0U;
|
||||
|
||||
event->transport = HID_TRANSPORT_BLE;
|
||||
event->ready = ready;
|
||||
event->protocol_mode = protocol_mode;
|
||||
event->keys_ready = ready &&
|
||||
((protocol_mode == KEYBOARD_PROTOCOL_MODE_BOOT) ?
|
||||
if (ready && ((protocol_mode == KEYBOARD_PROTOCOL_MODE_BOOT) ?
|
||||
boot_keyboard_notify_enabled :
|
||||
keyboard_report_notify_enabled);
|
||||
event->consumer_ready = ready &&
|
||||
(protocol_mode == KEYBOARD_PROTOCOL_MODE_REPORT) &&
|
||||
consumer_report_notify_enabled;
|
||||
keyboard_report_notify_enabled)) {
|
||||
report_ready_bm |= BIT(KEYBOARD_REPORT_TYPE_KEYS);
|
||||
}
|
||||
|
||||
APP_EVENT_SUBMIT(event);
|
||||
}
|
||||
if (ready && (protocol_mode == KEYBOARD_PROTOCOL_MODE_REPORT) &&
|
||||
consumer_report_notify_enabled) {
|
||||
report_ready_bm |= BIT(KEYBOARD_REPORT_TYPE_CONSUMER);
|
||||
}
|
||||
|
||||
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);
|
||||
submit_hid_channel_state_event(
|
||||
HID_SEND_CH_BLE_SHARED,
|
||||
report_ready_bm,
|
||||
protocol_mode);
|
||||
}
|
||||
|
||||
static void input_report_notify_handler(uint8_t report_id, enum bt_hids_notify_evt evt)
|
||||
@@ -158,13 +130,13 @@ static void input_report_notify_handler(uint8_t report_id, enum bt_hids_notify_e
|
||||
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)
|
||||
{
|
||||
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)
|
||||
@@ -176,7 +148,9 @@ static void hid_report_complete_cb(struct bt_conn *conn, void *user_data)
|
||||
return;
|
||||
}
|
||||
|
||||
submit_hid_report_sent_event(in_flight.report_type, in_flight.sequence, false);
|
||||
submit_hid_report_sent_event(HID_SEND_CH_BLE_SHARED,
|
||||
in_flight.report_type,
|
||||
in_flight.sequence, false);
|
||||
in_flight.active = false;
|
||||
}
|
||||
|
||||
@@ -186,7 +160,7 @@ static void keyboard_led_report_common(struct bt_hids_rep *rep, bool write)
|
||||
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,
|
||||
@@ -224,8 +198,8 @@ static void pm_evt_handler(enum bt_hids_pm_evt evt, struct bt_conn *conn)
|
||||
return;
|
||||
}
|
||||
|
||||
submit_set_protocol_event();
|
||||
submit_transport_state_event();
|
||||
submit_set_protocol_event(HID_TRANSPORT_BLE, protocol_mode);
|
||||
submit_ble_transport_state_event();
|
||||
}
|
||||
|
||||
static int module_init(void)
|
||||
@@ -272,7 +246,7 @@ static int module_start(void)
|
||||
}
|
||||
|
||||
running = true;
|
||||
submit_transport_state_event();
|
||||
submit_ble_transport_state_event();
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -285,7 +259,7 @@ static void module_pause(void)
|
||||
|
||||
in_flight.active = false;
|
||||
running = false;
|
||||
submit_transport_state_event();
|
||||
submit_ble_transport_state_event();
|
||||
}
|
||||
|
||||
static void reset_connection_state(void)
|
||||
@@ -311,12 +285,12 @@ static bool handle_ble_peer_event(const struct ble_peer_event *event)
|
||||
|
||||
active_conn = event->id;
|
||||
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);
|
||||
if (err) {
|
||||
LOG_ERR("bt_hids_connected failed (%d)", err);
|
||||
}
|
||||
submit_transport_state_event();
|
||||
submit_ble_transport_state_event();
|
||||
return false;
|
||||
|
||||
case PEER_STATE_SECURED:
|
||||
@@ -325,7 +299,7 @@ static bool handle_ble_peer_event(const struct ble_peer_event *event)
|
||||
}
|
||||
|
||||
secured = true;
|
||||
submit_transport_state_event();
|
||||
submit_ble_transport_state_event();
|
||||
return false;
|
||||
|
||||
case PEER_STATE_DISCONNECTED:
|
||||
@@ -339,7 +313,7 @@ static bool handle_ble_peer_event(const struct ble_peer_event *event)
|
||||
}
|
||||
|
||||
reset_connection_state();
|
||||
submit_transport_state_event();
|
||||
submit_ble_transport_state_event();
|
||||
return false;
|
||||
|
||||
default:
|
||||
@@ -351,7 +325,8 @@ static bool handle_hid_tx_report_event(const struct hid_tx_report_event *event)
|
||||
{
|
||||
int err;
|
||||
|
||||
if (!running || (event->transport != HID_TRANSPORT_BLE) || in_flight.active) {
|
||||
if (!running || (event->channel != HID_SEND_CH_BLE_SHARED) ||
|
||||
in_flight.active) {
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -387,7 +362,8 @@ static bool handle_hid_tx_report_event(const struct hid_tx_report_event *event)
|
||||
if (err) {
|
||||
in_flight.active = false;
|
||||
LOG_WRN("BLE keyboard report submit failed (%d)", err);
|
||||
submit_hid_report_sent_event(KEYBOARD_REPORT_TYPE_KEYS,
|
||||
submit_hid_report_sent_event(HID_SEND_CH_BLE_SHARED,
|
||||
KEYBOARD_REPORT_TYPE_KEYS,
|
||||
event->sequence, true);
|
||||
}
|
||||
|
||||
@@ -413,7 +389,8 @@ static bool handle_hid_tx_report_event(const struct hid_tx_report_event *event)
|
||||
if (err) {
|
||||
in_flight.active = false;
|
||||
LOG_WRN("BLE consumer report submit failed (%d)", err);
|
||||
submit_hid_report_sent_event(KEYBOARD_REPORT_TYPE_CONSUMER,
|
||||
submit_hid_report_sent_event(HID_SEND_CH_BLE_SHARED,
|
||||
KEYBOARD_REPORT_TYPE_CONSUMER,
|
||||
event->sequence, true);
|
||||
}
|
||||
}
|
||||
|
||||
215
src/ble_nus_module.c
Normal file
215
src/ble_nus_module.c
Normal file
@@ -0,0 +1,215 @@
|
||||
#include <errno.h>
|
||||
#include <stdbool.h>
|
||||
#include <stdint.h>
|
||||
|
||||
#include <app_event_manager.h>
|
||||
|
||||
#define MODULE ble_nus_module
|
||||
#include <caf/events/module_state_event.h>
|
||||
#include <caf/events/power_event.h>
|
||||
|
||||
#include <caf/events/ble_common_event.h>
|
||||
#include <zephyr/bluetooth/conn.h>
|
||||
#include <zephyr/bluetooth/services/nus.h>
|
||||
#include <zephyr/logging/log.h>
|
||||
|
||||
#include "proto_rx_event.h"
|
||||
#include "proto_tx_event.h"
|
||||
#include "protocol_module.h"
|
||||
|
||||
LOG_MODULE_REGISTER(MODULE, LOG_LEVEL_INF);
|
||||
|
||||
static struct bt_conn *active_conn;
|
||||
static bool initialized;
|
||||
static bool running;
|
||||
static bool ble_ready;
|
||||
static bool tx_notify_enabled;
|
||||
|
||||
static void notif_enabled(bool enabled, void *ctx)
|
||||
{
|
||||
ARG_UNUSED(ctx);
|
||||
|
||||
tx_notify_enabled = enabled;
|
||||
LOG_INF("BLE NUS TX notify %s", enabled ? "enabled" : "disabled");
|
||||
}
|
||||
|
||||
static void received(struct bt_conn *conn, const void *data, uint16_t len, void *ctx)
|
||||
{
|
||||
ARG_UNUSED(ctx);
|
||||
|
||||
if (!running || !ble_ready || (conn != active_conn)) {
|
||||
return;
|
||||
}
|
||||
|
||||
(void)submit_proto_rx_event(PROTO_TRANSPORT_BLE_NUS, data, len);
|
||||
}
|
||||
|
||||
static struct bt_nus_cb nus_listener = {
|
||||
.notif_enabled = notif_enabled,
|
||||
.received = received,
|
||||
};
|
||||
|
||||
static void reset_connection_state(void)
|
||||
{
|
||||
active_conn = NULL;
|
||||
tx_notify_enabled = false;
|
||||
protocol_module_reset_transport_state(PROTO_TRANSPORT_BLE_NUS);
|
||||
}
|
||||
|
||||
static int module_init(void)
|
||||
{
|
||||
int err;
|
||||
|
||||
err = bt_nus_cb_register(&nus_listener, NULL);
|
||||
if (err) {
|
||||
LOG_ERR("bt_nus_cb_register failed (%d)", err);
|
||||
return err;
|
||||
}
|
||||
|
||||
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;
|
||||
tx_notify_enabled = false;
|
||||
protocol_module_reset_transport_state(PROTO_TRANSPORT_BLE_NUS);
|
||||
return false;
|
||||
|
||||
case PEER_STATE_DISCONNECTED:
|
||||
if (active_conn != event->id) {
|
||||
return false;
|
||||
}
|
||||
|
||||
reset_connection_state();
|
||||
return false;
|
||||
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
static bool handle_proto_tx_event(const struct proto_tx_event *event)
|
||||
{
|
||||
int err;
|
||||
|
||||
if (event->transport != PROTO_TRANSPORT_BLE_NUS) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!running || !ble_ready || (active_conn == NULL) || !tx_notify_enabled) {
|
||||
return false;
|
||||
}
|
||||
|
||||
err = bt_nus_send(active_conn, event->dyndata.data, (uint16_t)event->dyndata.size);
|
||||
if (err) {
|
||||
LOG_WRN("bt_nus_send failed (%d)", err);
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
static bool app_event_handler(const struct app_event_header *aeh)
|
||||
{
|
||||
if (is_proto_tx_event(aeh)) {
|
||||
return handle_proto_tx_event(cast_proto_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, proto_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);
|
||||
@@ -1,275 +0,0 @@
|
||||
#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);
|
||||
@@ -14,8 +14,11 @@
|
||||
#include <zephyr/logging/log.h>
|
||||
|
||||
#include "bat_state_event.h"
|
||||
#include "datetime_event.h"
|
||||
#include "hid_led_event.h"
|
||||
#include "mode_switch_event.h"
|
||||
#include "theme_rgb_update_event.h"
|
||||
#include "theme_color.h"
|
||||
#include "ui/ui_main.h"
|
||||
|
||||
LOG_MODULE_REGISTER(MODULE, LOG_LEVEL_INF);
|
||||
@@ -30,13 +33,17 @@ static const struct device *const backlight_dev =
|
||||
DEVICE_DT_GET(DT_PARENT(DT_ALIAS(backlight)));
|
||||
static const uint32_t backlight_idx = DT_NODE_CHILD_IDX(DT_ALIAS(backlight));
|
||||
static struct ui_main_model ui_model = {
|
||||
.theme_color = LV_COLOR_MAKE(0x4C, 0x9E, 0xF5),
|
||||
.theme_color = LV_COLOR_MAKE(BLINKY_THEME_DEFAULT_R,
|
||||
BLINKY_THEME_DEFAULT_G,
|
||||
BLINKY_THEME_DEFAULT_B),
|
||||
.inactive_border_color = LV_COLOR_MAKE(0x3A, 0x44, 0x52),
|
||||
.mode = MODE_SWITCH_BLE,
|
||||
};
|
||||
static bool initialized;
|
||||
static bool running;
|
||||
static bool lvgl_initialized;
|
||||
static char date_text[DATETIME_EVENT_DATE_TEXT_LEN] = "1970/01/01";
|
||||
static char time_text[DATETIME_EVENT_TIME_TEXT_LEN] = "00:00:00";
|
||||
|
||||
static int backlight_set(bool on)
|
||||
{
|
||||
@@ -90,7 +97,7 @@ static int module_start(void)
|
||||
lvgl_initialized = true;
|
||||
|
||||
lvgl_lock();
|
||||
ui_main_init(&ui_model, "WH Mini", "Hello World");
|
||||
ui_main_init(&ui_model, date_text, time_text);
|
||||
lvgl_unlock();
|
||||
}
|
||||
|
||||
@@ -132,7 +139,7 @@ static void refresh_ui(void)
|
||||
}
|
||||
|
||||
lvgl_lock();
|
||||
ui_main_refresh_all(&ui_model, "WH Mini", "Hello World");
|
||||
ui_main_refresh_all(&ui_model, date_text, time_text);
|
||||
lvgl_unlock();
|
||||
}
|
||||
|
||||
@@ -164,6 +171,28 @@ static bool app_event_handler(const struct app_event_header *aeh)
|
||||
return false;
|
||||
}
|
||||
|
||||
if (is_theme_rgb_update_event(aeh)) {
|
||||
const struct theme_rgb_update_event *event =
|
||||
cast_theme_rgb_update_event(aeh);
|
||||
|
||||
ui_model.theme_color = (lv_color_t)LV_COLOR_MAKE(event->theme.r,
|
||||
event->theme.g,
|
||||
event->theme.b);
|
||||
refresh_ui();
|
||||
return false;
|
||||
}
|
||||
|
||||
if (is_datetime_event(aeh)) {
|
||||
const struct datetime_event *event = cast_datetime_event(aeh);
|
||||
|
||||
strncpy(date_text, event->date_text, sizeof(date_text));
|
||||
date_text[sizeof(date_text) - 1] = '\0';
|
||||
strncpy(time_text, event->time_text, sizeof(time_text));
|
||||
time_text[sizeof(time_text) - 1] = '\0';
|
||||
refresh_ui();
|
||||
return false;
|
||||
}
|
||||
|
||||
if (is_module_state_event(aeh)) {
|
||||
const struct module_state_event *event = cast_module_state_event(aeh);
|
||||
int err;
|
||||
@@ -218,8 +247,10 @@ static bool app_event_handler(const struct app_event_header *aeh)
|
||||
|
||||
APP_EVENT_LISTENER(MODULE, app_event_handler);
|
||||
APP_EVENT_SUBSCRIBE(MODULE, bat_state_event);
|
||||
APP_EVENT_SUBSCRIBE(MODULE, datetime_event);
|
||||
APP_EVENT_SUBSCRIBE(MODULE, hid_led_event);
|
||||
APP_EVENT_SUBSCRIBE(MODULE, module_state_event);
|
||||
APP_EVENT_SUBSCRIBE(MODULE, mode_switch_event);
|
||||
APP_EVENT_SUBSCRIBE(MODULE, theme_rgb_update_event);
|
||||
APP_EVENT_SUBSCRIBE_EARLY(MODULE, power_down_event);
|
||||
APP_EVENT_SUBSCRIBE(MODULE, wake_up_event);
|
||||
|
||||
@@ -39,23 +39,23 @@ static int64_t sensor_value_to_udeg(const struct sensor_value *value)
|
||||
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) {
|
||||
struct encoder_event *event = new_encoder_event();
|
||||
int8_t event_detents;
|
||||
|
||||
if (detents > INT8_MAX) {
|
||||
event->detents = INT8_MAX;
|
||||
event_detents = INT8_MAX;
|
||||
detents -= INT8_MAX;
|
||||
} else if (detents < INT8_MIN) {
|
||||
event->detents = INT8_MIN;
|
||||
event_detents = INT8_MIN;
|
||||
detents -= INT8_MIN;
|
||||
} else {
|
||||
event->detents = (int8_t)detents;
|
||||
event_detents = (int8_t)detents;
|
||||
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);
|
||||
|
||||
if (detents != 0) {
|
||||
submit_detents(detents);
|
||||
submit_detents_batched(detents);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
26
src/events/datetime_event.c
Normal file
26
src/events/datetime_event.c
Normal file
@@ -0,0 +1,26 @@
|
||||
#include "datetime_event.h"
|
||||
|
||||
static void log_datetime_event(const struct app_event_header *aeh)
|
||||
{
|
||||
const struct datetime_event *event = cast_datetime_event(aeh);
|
||||
|
||||
APP_EVENT_MANAGER_LOG(aeh, "date:%s time:%s",
|
||||
event->date_text, event->time_text);
|
||||
}
|
||||
|
||||
static void profile_datetime_event(struct log_event_buf *buf,
|
||||
const struct app_event_header *aeh)
|
||||
{
|
||||
ARG_UNUSED(buf);
|
||||
ARG_UNUSED(aeh);
|
||||
}
|
||||
|
||||
APP_EVENT_INFO_DEFINE(datetime_event,
|
||||
ENCODE(),
|
||||
ENCODE(),
|
||||
profile_datetime_event);
|
||||
|
||||
APP_EVENT_TYPE_DEFINE(datetime_event,
|
||||
log_datetime_event,
|
||||
&datetime_event_info,
|
||||
APP_EVENT_FLAGS_CREATE());
|
||||
27
src/events/function_bitmap_state_event.c
Normal file
27
src/events/function_bitmap_state_event.c
Normal file
@@ -0,0 +1,27 @@
|
||||
#include "function_bitmap_state_event.h"
|
||||
|
||||
static void log_function_bitmap_state_event(const struct app_event_header *aeh)
|
||||
{
|
||||
const struct function_bitmap_state_event *event =
|
||||
cast_function_bitmap_state_event(aeh);
|
||||
|
||||
APP_EVENT_MANAGER_LOG(aeh, "bitmap_len:%zu", sizeof(event->bitmap));
|
||||
}
|
||||
|
||||
static void profile_function_bitmap_state_event(struct log_event_buf *buf,
|
||||
const struct app_event_header *aeh)
|
||||
{
|
||||
ARG_UNUSED(buf);
|
||||
ARG_UNUSED(aeh);
|
||||
}
|
||||
|
||||
APP_EVENT_INFO_DEFINE(function_bitmap_state_event,
|
||||
ENCODE(),
|
||||
ENCODE(),
|
||||
profile_function_bitmap_state_event);
|
||||
|
||||
APP_EVENT_TYPE_DEFINE(function_bitmap_state_event,
|
||||
log_function_bitmap_state_event,
|
||||
&function_bitmap_state_event_info,
|
||||
APP_EVENT_FLAGS_CREATE(
|
||||
APP_EVENT_TYPE_FLAGS_INIT_LOG_ENABLE));
|
||||
24
src/events/function_bitmap_update_event.c
Normal file
24
src/events/function_bitmap_update_event.c
Normal file
@@ -0,0 +1,24 @@
|
||||
#include "function_bitmap_update_event.h"
|
||||
|
||||
static void log_function_bitmap_update_event(const struct app_event_header *aeh)
|
||||
{
|
||||
APP_EVENT_MANAGER_LOG(aeh, "bitmap updated");
|
||||
}
|
||||
|
||||
static void profile_function_bitmap_update_event(struct log_event_buf *buf,
|
||||
const struct app_event_header *aeh)
|
||||
{
|
||||
ARG_UNUSED(buf);
|
||||
ARG_UNUSED(aeh);
|
||||
}
|
||||
|
||||
APP_EVENT_INFO_DEFINE(function_bitmap_update_event,
|
||||
ENCODE(),
|
||||
ENCODE(),
|
||||
profile_function_bitmap_update_event);
|
||||
|
||||
APP_EVENT_TYPE_DEFINE(function_bitmap_update_event,
|
||||
log_function_bitmap_update_event,
|
||||
&function_bitmap_update_event_info,
|
||||
APP_EVENT_FLAGS_CREATE(
|
||||
APP_EVENT_TYPE_FLAGS_INIT_LOG_ENABLE));
|
||||
51
src/events/hid_channel_state_event.c
Normal file
51
src/events/hid_channel_state_event.c
Normal file
@@ -0,0 +1,51 @@
|
||||
#include "hid_channel_state_event.h"
|
||||
|
||||
static const char *channel_name(enum hid_send_channel channel)
|
||||
{
|
||||
switch (channel) {
|
||||
case HID_SEND_CH_USB_KEYS:
|
||||
return "usb_keys";
|
||||
case HID_SEND_CH_USB_CONSUMER:
|
||||
return "usb_consumer";
|
||||
case HID_SEND_CH_BLE_SHARED:
|
||||
return "ble_shared";
|
||||
default:
|
||||
return "?";
|
||||
}
|
||||
}
|
||||
|
||||
static void log_hid_channel_state_event(const struct app_event_header *aeh)
|
||||
{
|
||||
const struct hid_channel_state_event *event =
|
||||
cast_hid_channel_state_event(aeh);
|
||||
|
||||
APP_EVENT_MANAGER_LOG(aeh,
|
||||
"channel:%s ready_bm:0x%02x protocol:%d",
|
||||
channel_name(event->channel),
|
||||
event->report_ready_bm,
|
||||
event->protocol_mode);
|
||||
}
|
||||
|
||||
static void profile_hid_channel_state_event(struct log_event_buf *buf,
|
||||
const struct app_event_header *aeh)
|
||||
{
|
||||
const struct hid_channel_state_event *event =
|
||||
cast_hid_channel_state_event(aeh);
|
||||
|
||||
nrf_profiler_log_encode_uint8(buf, event->channel);
|
||||
nrf_profiler_log_encode_uint8(buf, event->report_ready_bm);
|
||||
nrf_profiler_log_encode_uint8(buf, event->protocol_mode);
|
||||
}
|
||||
|
||||
APP_EVENT_INFO_DEFINE(hid_channel_state_event,
|
||||
ENCODE(NRF_PROFILER_ARG_U8,
|
||||
NRF_PROFILER_ARG_U8,
|
||||
NRF_PROFILER_ARG_U8),
|
||||
ENCODE("channel", "ready_bm", "protocol_mode"),
|
||||
profile_hid_channel_state_event);
|
||||
|
||||
APP_EVENT_TYPE_DEFINE(hid_channel_state_event,
|
||||
log_hid_channel_state_event,
|
||||
&hid_channel_state_event_info,
|
||||
APP_EVENT_FLAGS_CREATE(
|
||||
APP_EVENT_TYPE_FLAGS_INIT_LOG_ENABLE));
|
||||
@@ -1,12 +1,14 @@
|
||||
#include "hid_report_sent_event.h"
|
||||
|
||||
static const char *transport_name(enum hid_transport transport)
|
||||
static const char *channel_name(enum hid_send_channel channel)
|
||||
{
|
||||
switch (transport) {
|
||||
case HID_TRANSPORT_USB:
|
||||
return "USB";
|
||||
case HID_TRANSPORT_BLE:
|
||||
return "BLE";
|
||||
switch (channel) {
|
||||
case HID_SEND_CH_USB_KEYS:
|
||||
return "usb_keys";
|
||||
case HID_SEND_CH_USB_CONSUMER:
|
||||
return "usb_consumer";
|
||||
case HID_SEND_CH_BLE_SHARED:
|
||||
return "ble_shared";
|
||||
default:
|
||||
return "?";
|
||||
}
|
||||
@@ -28,8 +30,8 @@ static void log_hid_report_sent_event(const struct app_event_header *aeh)
|
||||
{
|
||||
const struct hid_report_sent_event *event = cast_hid_report_sent_event(aeh);
|
||||
|
||||
APP_EVENT_MANAGER_LOG(aeh, "transport:%s type:%s seq:%u error:%u",
|
||||
transport_name(event->transport),
|
||||
APP_EVENT_MANAGER_LOG(aeh, "channel:%s type:%s seq:%u error:%u",
|
||||
channel_name(event->channel),
|
||||
report_type_name(event->report_type),
|
||||
event->sequence,
|
||||
event->error);
|
||||
@@ -40,7 +42,7 @@ static void profile_hid_report_sent_event(struct log_event_buf *buf,
|
||||
{
|
||||
const struct hid_report_sent_event *event = cast_hid_report_sent_event(aeh);
|
||||
|
||||
nrf_profiler_log_encode_uint8(buf, event->transport);
|
||||
nrf_profiler_log_encode_uint8(buf, event->channel);
|
||||
nrf_profiler_log_encode_uint8(buf, event->report_type);
|
||||
nrf_profiler_log_encode_uint16(buf, event->sequence);
|
||||
nrf_profiler_log_encode_uint8(buf, event->error);
|
||||
@@ -51,7 +53,7 @@ APP_EVENT_INFO_DEFINE(hid_report_sent_event,
|
||||
NRF_PROFILER_ARG_U8,
|
||||
NRF_PROFILER_ARG_U16,
|
||||
NRF_PROFILER_ARG_U8),
|
||||
ENCODE("transport", "report_type", "sequence", "error"),
|
||||
ENCODE("channel", "report_type", "sequence", "error"),
|
||||
profile_hid_report_sent_event);
|
||||
|
||||
APP_EVENT_TYPE_DEFINE(hid_report_sent_event,
|
||||
|
||||
@@ -1,68 +0,0 @@
|
||||
#include "hid_transport_state_event.h"
|
||||
|
||||
static const char *transport_name(enum hid_transport transport)
|
||||
{
|
||||
switch (transport) {
|
||||
case HID_TRANSPORT_USB:
|
||||
return "USB";
|
||||
case HID_TRANSPORT_BLE:
|
||||
return "BLE";
|
||||
default:
|
||||
return "?";
|
||||
}
|
||||
}
|
||||
|
||||
static const char *protocol_mode_name(enum keyboard_protocol_mode protocol_mode)
|
||||
{
|
||||
switch (protocol_mode) {
|
||||
case KEYBOARD_PROTOCOL_MODE_BOOT:
|
||||
return "boot";
|
||||
case KEYBOARD_PROTOCOL_MODE_REPORT:
|
||||
return "report";
|
||||
default:
|
||||
return "?";
|
||||
}
|
||||
}
|
||||
|
||||
static void log_hid_transport_state_event(const struct app_event_header *aeh)
|
||||
{
|
||||
const struct hid_transport_state_event *event =
|
||||
cast_hid_transport_state_event(aeh);
|
||||
|
||||
APP_EVENT_MANAGER_LOG(aeh,
|
||||
"transport:%s ready:%u keys_ready:%u consumer_ready:%u protocol:%s",
|
||||
transport_name(event->transport),
|
||||
event->ready,
|
||||
event->keys_ready,
|
||||
event->consumer_ready,
|
||||
protocol_mode_name(event->protocol_mode));
|
||||
}
|
||||
|
||||
static void profile_hid_transport_state_event(struct log_event_buf *buf,
|
||||
const struct app_event_header *aeh)
|
||||
{
|
||||
const struct hid_transport_state_event *event =
|
||||
cast_hid_transport_state_event(aeh);
|
||||
|
||||
nrf_profiler_log_encode_uint8(buf, event->transport);
|
||||
nrf_profiler_log_encode_uint8(buf, event->ready);
|
||||
nrf_profiler_log_encode_uint8(buf, event->keys_ready);
|
||||
nrf_profiler_log_encode_uint8(buf, event->consumer_ready);
|
||||
nrf_profiler_log_encode_uint8(buf, event->protocol_mode);
|
||||
}
|
||||
|
||||
APP_EVENT_INFO_DEFINE(hid_transport_state_event,
|
||||
ENCODE(NRF_PROFILER_ARG_U8,
|
||||
NRF_PROFILER_ARG_U8,
|
||||
NRF_PROFILER_ARG_U8,
|
||||
NRF_PROFILER_ARG_U8,
|
||||
NRF_PROFILER_ARG_U8),
|
||||
ENCODE("transport", "ready", "keys_ready", "consumer_ready",
|
||||
"protocol_mode"),
|
||||
profile_hid_transport_state_event);
|
||||
|
||||
APP_EVENT_TYPE_DEFINE(hid_transport_state_event,
|
||||
log_hid_transport_state_event,
|
||||
&hid_transport_state_event_info,
|
||||
APP_EVENT_FLAGS_CREATE(
|
||||
APP_EVENT_TYPE_FLAGS_INIT_LOG_ENABLE));
|
||||
@@ -4,13 +4,15 @@
|
||||
|
||||
#define HID_TX_REPORT_EVENT_LOG_BUF_LEN 192
|
||||
|
||||
static const char *transport_name(enum hid_transport transport)
|
||||
static const char *channel_name(enum hid_send_channel channel)
|
||||
{
|
||||
switch (transport) {
|
||||
case HID_TRANSPORT_USB:
|
||||
return "USB";
|
||||
case HID_TRANSPORT_BLE:
|
||||
return "BLE";
|
||||
switch (channel) {
|
||||
case HID_SEND_CH_USB_KEYS:
|
||||
return "usb_keys";
|
||||
case HID_SEND_CH_USB_CONSUMER:
|
||||
return "usb_consumer";
|
||||
case HID_SEND_CH_BLE_SHARED:
|
||||
return "ble_shared";
|
||||
default:
|
||||
return "?";
|
||||
}
|
||||
@@ -47,8 +49,8 @@ static void log_hid_tx_report_event(const struct app_event_header *aeh)
|
||||
int pos;
|
||||
|
||||
pos = snprintf(log_buf, sizeof(log_buf),
|
||||
"transport:%s type:%s protocol:%s seq:%u len:%zu",
|
||||
transport_name(event->transport),
|
||||
"channel:%s type:%s protocol:%s seq:%u len:%zu",
|
||||
channel_name(event->channel),
|
||||
report_type_name(event->report_type),
|
||||
protocol_mode_name(event->protocol_mode),
|
||||
event->sequence,
|
||||
@@ -84,7 +86,7 @@ static void profile_hid_tx_report_event(struct log_event_buf *buf,
|
||||
{
|
||||
const struct hid_tx_report_event *event = cast_hid_tx_report_event(aeh);
|
||||
|
||||
nrf_profiler_log_encode_uint8(buf, event->transport);
|
||||
nrf_profiler_log_encode_uint8(buf, event->channel);
|
||||
nrf_profiler_log_encode_uint8(buf, event->report_type);
|
||||
nrf_profiler_log_encode_uint8(buf, event->protocol_mode);
|
||||
nrf_profiler_log_encode_uint16(buf, event->sequence);
|
||||
@@ -97,7 +99,7 @@ APP_EVENT_INFO_DEFINE(hid_tx_report_event,
|
||||
NRF_PROFILER_ARG_U8,
|
||||
NRF_PROFILER_ARG_U16,
|
||||
NRF_PROFILER_ARG_U8),
|
||||
ENCODE("transport", "report_type", "protocol_mode", "sequence", "len"),
|
||||
ENCODE("channel", "report_type", "protocol_mode", "sequence", "len"),
|
||||
profile_hid_tx_report_event);
|
||||
|
||||
APP_EVENT_TYPE_DEFINE(hid_tx_report_event,
|
||||
|
||||
27
src/events/led_strip_en_event.c
Normal file
27
src/events/led_strip_en_event.c
Normal file
@@ -0,0 +1,27 @@
|
||||
#include "led_strip_en_event.h"
|
||||
|
||||
static void log_led_strip_en_event(const struct app_event_header *aeh)
|
||||
{
|
||||
const struct led_strip_en_event *event = cast_led_strip_en_event(aeh);
|
||||
|
||||
APP_EVENT_MANAGER_LOG(aeh, "enabled:%u", event->enabled);
|
||||
}
|
||||
|
||||
static void profile_led_strip_en_event(struct log_event_buf *buf,
|
||||
const struct app_event_header *aeh)
|
||||
{
|
||||
const struct led_strip_en_event *event = cast_led_strip_en_event(aeh);
|
||||
|
||||
nrf_profiler_log_encode_uint8(buf, event->enabled);
|
||||
}
|
||||
|
||||
APP_EVENT_INFO_DEFINE(led_strip_en_event,
|
||||
ENCODE(NRF_PROFILER_ARG_U8),
|
||||
ENCODE("enabled"),
|
||||
profile_led_strip_en_event);
|
||||
|
||||
APP_EVENT_TYPE_DEFINE(led_strip_en_event,
|
||||
log_led_strip_en_event,
|
||||
&led_strip_en_event_info,
|
||||
APP_EVENT_FLAGS_CREATE(
|
||||
APP_EVENT_TYPE_FLAGS_INIT_LOG_ENABLE));
|
||||
42
src/events/proto_rx_event.c
Normal file
42
src/events/proto_rx_event.c
Normal file
@@ -0,0 +1,42 @@
|
||||
#include "proto_rx_event.h"
|
||||
|
||||
static const char *transport_name(enum proto_transport transport)
|
||||
{
|
||||
switch (transport) {
|
||||
case PROTO_TRANSPORT_USB_CDC:
|
||||
return "usb_cdc";
|
||||
case PROTO_TRANSPORT_BLE_NUS:
|
||||
return "ble_nus";
|
||||
default:
|
||||
return "?";
|
||||
}
|
||||
}
|
||||
|
||||
static void log_proto_rx_event(const struct app_event_header *aeh)
|
||||
{
|
||||
const struct proto_rx_event *event = cast_proto_rx_event(aeh);
|
||||
|
||||
APP_EVENT_MANAGER_LOG(aeh, "transport:%s len:%zu",
|
||||
transport_name(event->transport),
|
||||
event->dyndata.size);
|
||||
}
|
||||
|
||||
static void profile_proto_rx_event(struct log_event_buf *buf,
|
||||
const struct app_event_header *aeh)
|
||||
{
|
||||
const struct proto_rx_event *event = cast_proto_rx_event(aeh);
|
||||
|
||||
nrf_profiler_log_encode_uint8(buf, event->transport);
|
||||
nrf_profiler_log_encode_uint16(buf, (uint16_t)event->dyndata.size);
|
||||
}
|
||||
|
||||
APP_EVENT_INFO_DEFINE(proto_rx_event,
|
||||
ENCODE(NRF_PROFILER_ARG_U8, NRF_PROFILER_ARG_U16),
|
||||
ENCODE("transport", "len"),
|
||||
profile_proto_rx_event);
|
||||
|
||||
APP_EVENT_TYPE_DEFINE(proto_rx_event,
|
||||
log_proto_rx_event,
|
||||
&proto_rx_event_info,
|
||||
APP_EVENT_FLAGS_CREATE(
|
||||
APP_EVENT_TYPE_FLAGS_INIT_LOG_ENABLE));
|
||||
42
src/events/proto_tx_event.c
Normal file
42
src/events/proto_tx_event.c
Normal file
@@ -0,0 +1,42 @@
|
||||
#include "proto_tx_event.h"
|
||||
|
||||
static const char *transport_name(enum proto_transport transport)
|
||||
{
|
||||
switch (transport) {
|
||||
case PROTO_TRANSPORT_USB_CDC:
|
||||
return "usb_cdc";
|
||||
case PROTO_TRANSPORT_BLE_NUS:
|
||||
return "ble_nus";
|
||||
default:
|
||||
return "?";
|
||||
}
|
||||
}
|
||||
|
||||
static void log_proto_tx_event(const struct app_event_header *aeh)
|
||||
{
|
||||
const struct proto_tx_event *event = cast_proto_tx_event(aeh);
|
||||
|
||||
APP_EVENT_MANAGER_LOG(aeh, "transport:%s len:%zu",
|
||||
transport_name(event->transport),
|
||||
event->dyndata.size);
|
||||
}
|
||||
|
||||
static void profile_proto_tx_event(struct log_event_buf *buf,
|
||||
const struct app_event_header *aeh)
|
||||
{
|
||||
const struct proto_tx_event *event = cast_proto_tx_event(aeh);
|
||||
|
||||
nrf_profiler_log_encode_uint8(buf, event->transport);
|
||||
nrf_profiler_log_encode_uint16(buf, (uint16_t)event->dyndata.size);
|
||||
}
|
||||
|
||||
APP_EVENT_INFO_DEFINE(proto_tx_event,
|
||||
ENCODE(NRF_PROFILER_ARG_U8, NRF_PROFILER_ARG_U16),
|
||||
ENCODE("transport", "len"),
|
||||
profile_proto_tx_event);
|
||||
|
||||
APP_EVENT_TYPE_DEFINE(proto_tx_event,
|
||||
log_proto_tx_event,
|
||||
&proto_tx_event_info,
|
||||
APP_EVENT_FLAGS_CREATE(
|
||||
APP_EVENT_TYPE_FLAGS_INIT_LOG_ENABLE));
|
||||
34
src/events/theme_rgb_update_event.c
Normal file
34
src/events/theme_rgb_update_event.c
Normal file
@@ -0,0 +1,34 @@
|
||||
#include "theme_rgb_update_event.h"
|
||||
|
||||
static void log_theme_rgb_update_event(const struct app_event_header *aeh)
|
||||
{
|
||||
const struct theme_rgb_update_event *event =
|
||||
cast_theme_rgb_update_event(aeh);
|
||||
|
||||
APP_EVENT_MANAGER_LOG(aeh, "r:%u g:%u b:%u",
|
||||
event->theme.r, event->theme.g, event->theme.b);
|
||||
}
|
||||
|
||||
static void profile_theme_rgb_update_event(struct log_event_buf *buf,
|
||||
const struct app_event_header *aeh)
|
||||
{
|
||||
const struct theme_rgb_update_event *event =
|
||||
cast_theme_rgb_update_event(aeh);
|
||||
|
||||
nrf_profiler_log_encode_uint8(buf, event->theme.r);
|
||||
nrf_profiler_log_encode_uint8(buf, event->theme.g);
|
||||
nrf_profiler_log_encode_uint8(buf, event->theme.b);
|
||||
}
|
||||
|
||||
APP_EVENT_INFO_DEFINE(theme_rgb_update_event,
|
||||
ENCODE(NRF_PROFILER_ARG_U8,
|
||||
NRF_PROFILER_ARG_U8,
|
||||
NRF_PROFILER_ARG_U8),
|
||||
ENCODE("r", "g", "b"),
|
||||
profile_theme_rgb_update_event);
|
||||
|
||||
APP_EVENT_TYPE_DEFINE(theme_rgb_update_event,
|
||||
log_theme_rgb_update_event,
|
||||
&theme_rgb_update_event_info,
|
||||
APP_EVENT_FLAGS_CREATE(
|
||||
APP_EVENT_TYPE_FLAGS_INIT_LOG_ENABLE));
|
||||
41
src/events/time_sync_event.c
Normal file
41
src/events/time_sync_event.c
Normal file
@@ -0,0 +1,41 @@
|
||||
#include "time_sync_event.h"
|
||||
|
||||
static void log_time_sync_event(const struct app_event_header *aeh)
|
||||
{
|
||||
const struct time_sync_event *event = cast_time_sync_event(aeh);
|
||||
|
||||
APP_EVENT_MANAGER_LOG(aeh,
|
||||
"ver:%u flags:0x%08x tz:%d utc_ms:%llu acc:%u",
|
||||
event->version,
|
||||
event->flags,
|
||||
event->timezone_min,
|
||||
event->utc_ms,
|
||||
event->accuracy_ms);
|
||||
}
|
||||
|
||||
static void profile_time_sync_event(struct log_event_buf *buf,
|
||||
const struct app_event_header *aeh)
|
||||
{
|
||||
const struct time_sync_event *event = cast_time_sync_event(aeh);
|
||||
|
||||
nrf_profiler_log_encode_uint32(buf, event->version);
|
||||
nrf_profiler_log_encode_uint32(buf, event->flags);
|
||||
nrf_profiler_log_encode_int32(buf, event->timezone_min);
|
||||
nrf_profiler_log_encode_uint32(buf, (uint32_t)(event->utc_ms & 0xFFFFFFFFULL));
|
||||
nrf_profiler_log_encode_uint32(buf, event->accuracy_ms);
|
||||
}
|
||||
|
||||
APP_EVENT_INFO_DEFINE(time_sync_event,
|
||||
ENCODE(NRF_PROFILER_ARG_U32,
|
||||
NRF_PROFILER_ARG_U32,
|
||||
NRF_PROFILER_ARG_S32,
|
||||
NRF_PROFILER_ARG_U32,
|
||||
NRF_PROFILER_ARG_U32),
|
||||
ENCODE("version", "flags", "timezone_min", "utc_ms_lo", "accuracy_ms"),
|
||||
profile_time_sync_event);
|
||||
|
||||
APP_EVENT_TYPE_DEFINE(time_sync_event,
|
||||
log_time_sync_event,
|
||||
&time_sync_event_info,
|
||||
APP_EVENT_FLAGS_CREATE(
|
||||
APP_EVENT_TYPE_FLAGS_INIT_LOG_ENABLE));
|
||||
@@ -1,73 +0,0 @@
|
||||
#include <ctype.h>
|
||||
#include <stdio.h>
|
||||
|
||||
#include "usb_cdc_rx_event.h"
|
||||
|
||||
#define USB_CDC_RX_EVENT_LOG_BUF_LEN 384
|
||||
|
||||
static void log_usb_cdc_rx_event(const struct app_event_header *aeh)
|
||||
{
|
||||
const struct usb_cdc_rx_event *event = cast_usb_cdc_rx_event(aeh);
|
||||
char log_buf[USB_CDC_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_usb_cdc_rx_event(struct log_event_buf *buf,
|
||||
const struct app_event_header *aeh)
|
||||
{
|
||||
const struct usb_cdc_rx_event *event = cast_usb_cdc_rx_event(aeh);
|
||||
|
||||
nrf_profiler_log_encode_uint8(buf, (uint8_t)event->dyndata.size);
|
||||
}
|
||||
|
||||
APP_EVENT_INFO_DEFINE(usb_cdc_rx_event,
|
||||
ENCODE(NRF_PROFILER_ARG_U8),
|
||||
ENCODE("len"),
|
||||
profile_usb_cdc_rx_event);
|
||||
|
||||
APP_EVENT_TYPE_DEFINE(usb_cdc_rx_event,
|
||||
log_usb_cdc_rx_event,
|
||||
&usb_cdc_rx_event_info,
|
||||
APP_EVENT_FLAGS_CREATE(
|
||||
APP_EVENT_TYPE_FLAGS_INIT_LOG_ENABLE));
|
||||
@@ -1,62 +0,0 @@
|
||||
#include <ctype.h>
|
||||
#include <stdio.h>
|
||||
|
||||
#include "usb_cdc_tx_event.h"
|
||||
|
||||
#define USB_CDC_TX_EVENT_LOG_BUF_LEN 256
|
||||
|
||||
static void log_usb_cdc_tx_event(const struct app_event_header *aeh)
|
||||
{
|
||||
const struct usb_cdc_tx_event *event = cast_usb_cdc_tx_event(aeh);
|
||||
char log_buf[USB_CDC_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_usb_cdc_tx_event(struct log_event_buf *buf,
|
||||
const struct app_event_header *aeh)
|
||||
{
|
||||
const struct usb_cdc_tx_event *event = cast_usb_cdc_tx_event(aeh);
|
||||
|
||||
nrf_profiler_log_encode_uint8(buf, (uint8_t)event->dyndata.size);
|
||||
}
|
||||
|
||||
APP_EVENT_INFO_DEFINE(usb_cdc_tx_event,
|
||||
ENCODE(NRF_PROFILER_ARG_U8),
|
||||
ENCODE("len"),
|
||||
profile_usb_cdc_tx_event);
|
||||
|
||||
APP_EVENT_TYPE_DEFINE(usb_cdc_tx_event,
|
||||
log_usb_cdc_tx_event,
|
||||
&usb_cdc_tx_event_info,
|
||||
APP_EVENT_FLAGS_CREATE(
|
||||
APP_EVENT_TYPE_FLAGS_INIT_LOG_ENABLE));
|
||||
@@ -1,46 +0,0 @@
|
||||
#include "usb_device_state_event.h"
|
||||
|
||||
static const char *usb_device_state_name(enum usb_device_state state)
|
||||
{
|
||||
switch (state) {
|
||||
case USB_DEVICE_STATE_DISCONNECTED:
|
||||
return "disconnected";
|
||||
case USB_DEVICE_STATE_POWERED:
|
||||
return "powered";
|
||||
case USB_DEVICE_STATE_ACTIVE:
|
||||
return "active";
|
||||
case USB_DEVICE_STATE_SUSPENDED:
|
||||
return "suspended";
|
||||
default:
|
||||
return "?";
|
||||
}
|
||||
}
|
||||
|
||||
static void log_usb_device_state_event(const struct app_event_header *aeh)
|
||||
{
|
||||
const struct usb_device_state_event *event =
|
||||
cast_usb_device_state_event(aeh);
|
||||
|
||||
APP_EVENT_MANAGER_LOG(aeh, "state:%s",
|
||||
usb_device_state_name(event->state));
|
||||
}
|
||||
|
||||
static void profile_usb_device_state_event(struct log_event_buf *buf,
|
||||
const struct app_event_header *aeh)
|
||||
{
|
||||
const struct usb_device_state_event *event =
|
||||
cast_usb_device_state_event(aeh);
|
||||
|
||||
nrf_profiler_log_encode_uint8(buf, event->state);
|
||||
}
|
||||
|
||||
APP_EVENT_INFO_DEFINE(usb_device_state_event,
|
||||
ENCODE(NRF_PROFILER_ARG_U8),
|
||||
ENCODE("state"),
|
||||
profile_usb_device_state_event);
|
||||
|
||||
APP_EVENT_TYPE_DEFINE(usb_device_state_event,
|
||||
log_usb_device_state_event,
|
||||
&usb_device_state_event_info,
|
||||
APP_EVENT_FLAGS_CREATE(
|
||||
APP_EVENT_TYPE_FLAGS_INIT_LOG_ENABLE));
|
||||
@@ -1,42 +0,0 @@
|
||||
#include "usb_function_ready_event.h"
|
||||
|
||||
static const char *usb_function_name(uint8_t function_mask)
|
||||
{
|
||||
switch (function_mask) {
|
||||
case USB_FUNCTION_HID:
|
||||
return "hid";
|
||||
case USB_FUNCTION_CDC_ACM:
|
||||
return "cdc_acm";
|
||||
default:
|
||||
return "?";
|
||||
}
|
||||
}
|
||||
|
||||
static void log_usb_function_ready_event(const struct app_event_header *aeh)
|
||||
{
|
||||
const struct usb_function_ready_event *event =
|
||||
cast_usb_function_ready_event(aeh);
|
||||
|
||||
APP_EVENT_MANAGER_LOG(aeh, "function:%s",
|
||||
usb_function_name(event->function_mask));
|
||||
}
|
||||
|
||||
static void profile_usb_function_ready_event(struct log_event_buf *buf,
|
||||
const struct app_event_header *aeh)
|
||||
{
|
||||
const struct usb_function_ready_event *event =
|
||||
cast_usb_function_ready_event(aeh);
|
||||
|
||||
nrf_profiler_log_encode_uint8(buf, event->function_mask);
|
||||
}
|
||||
|
||||
APP_EVENT_INFO_DEFINE(usb_function_ready_event,
|
||||
ENCODE(NRF_PROFILER_ARG_U8),
|
||||
ENCODE("function_mask"),
|
||||
profile_usb_function_ready_event);
|
||||
|
||||
APP_EVENT_TYPE_DEFINE(usb_function_ready_event,
|
||||
log_usb_function_ready_event,
|
||||
&usb_function_ready_event_info,
|
||||
APP_EVENT_FLAGS_CREATE(
|
||||
APP_EVENT_TYPE_FLAGS_INIT_LOG_ENABLE));
|
||||
@@ -1,24 +0,0 @@
|
||||
#include "usb_prepare_event.h"
|
||||
|
||||
static void log_usb_prepare_event(const struct app_event_header *aeh)
|
||||
{
|
||||
APP_EVENT_MANAGER_LOG(aeh, "prepare");
|
||||
}
|
||||
|
||||
static void profile_usb_prepare_event(struct log_event_buf *buf,
|
||||
const struct app_event_header *aeh)
|
||||
{
|
||||
ARG_UNUSED(buf);
|
||||
ARG_UNUSED(aeh);
|
||||
}
|
||||
|
||||
APP_EVENT_INFO_DEFINE(usb_prepare_event,
|
||||
ENCODE(),
|
||||
ENCODE(),
|
||||
profile_usb_prepare_event);
|
||||
|
||||
APP_EVENT_TYPE_DEFINE(usb_prepare_event,
|
||||
log_usb_prepare_event,
|
||||
&usb_prepare_event_info,
|
||||
APP_EVENT_FLAGS_CREATE(
|
||||
APP_EVENT_TYPE_FLAGS_INIT_LOG_ENABLE));
|
||||
47
src/events/usb_state_event.c
Normal file
47
src/events/usb_state_event.c
Normal file
@@ -0,0 +1,47 @@
|
||||
#include <caf/events/module_state_event.h>
|
||||
|
||||
#include "usb_state_event.h"
|
||||
|
||||
static const char *usb_state_name(enum usb_state state)
|
||||
{
|
||||
switch (state) {
|
||||
case USB_STATE_DISABLED:
|
||||
return "disabled";
|
||||
case USB_STATE_DISCONNECTED:
|
||||
return "disconnected";
|
||||
case USB_STATE_POWERED:
|
||||
return "powered";
|
||||
case USB_STATE_ACTIVE:
|
||||
return "active";
|
||||
case USB_STATE_SUSPENDED:
|
||||
return "suspended";
|
||||
default:
|
||||
return "?";
|
||||
}
|
||||
}
|
||||
|
||||
static void log_usb_state_event(const struct app_event_header *aeh)
|
||||
{
|
||||
const struct usb_state_event *event = cast_usb_state_event(aeh);
|
||||
|
||||
APP_EVENT_MANAGER_LOG(aeh, "state:%s", usb_state_name(event->state));
|
||||
}
|
||||
|
||||
static void profile_usb_state_event(struct log_event_buf *buf,
|
||||
const struct app_event_header *aeh)
|
||||
{
|
||||
const struct usb_state_event *event = cast_usb_state_event(aeh);
|
||||
|
||||
nrf_profiler_log_encode_uint8(buf, event->state);
|
||||
}
|
||||
|
||||
APP_EVENT_INFO_DEFINE(usb_state_event,
|
||||
ENCODE(NRF_PROFILER_ARG_U8),
|
||||
ENCODE("state"),
|
||||
profile_usb_state_event);
|
||||
|
||||
APP_EVENT_TYPE_DEFINE(usb_state_event,
|
||||
log_usb_state_event,
|
||||
&usb_state_event_info,
|
||||
APP_EVENT_FLAGS_CREATE(
|
||||
APP_EVENT_TYPE_FLAGS_INIT_LOG_ENABLE));
|
||||
@@ -10,8 +10,8 @@
|
||||
|
||||
#include <zephyr/logging/log.h>
|
||||
|
||||
#include "hid_channel_state_event.h"
|
||||
#include "hid_report_sent_event.h"
|
||||
#include "hid_transport_state_event.h"
|
||||
#include "hid_tx_report_event.h"
|
||||
#include "keyboard_core.h"
|
||||
#include "keyboard_hid_report_event.h"
|
||||
@@ -37,25 +37,26 @@ struct queued_report {
|
||||
uint8_t data[HID_FLOWCTRL_REPORT_DATA_MAX];
|
||||
};
|
||||
|
||||
struct hid_transport_state_data {
|
||||
bool ready;
|
||||
bool keys_ready;
|
||||
bool consumer_ready;
|
||||
struct hid_channel_state_data {
|
||||
uint8_t report_ready_bm;
|
||||
enum keyboard_protocol_mode protocol_mode;
|
||||
};
|
||||
|
||||
struct in_flight_report {
|
||||
bool active;
|
||||
enum hid_transport transport;
|
||||
enum hid_send_channel channel;
|
||||
enum keyboard_report_type report_type;
|
||||
uint16_t sequence;
|
||||
};
|
||||
|
||||
static struct hid_transport_state_data transport_state[HID_TRANSPORT_COUNT] = {
|
||||
[HID_TRANSPORT_USB] = {
|
||||
static struct hid_channel_state_data channel_state[HID_SEND_CH_COUNT] = {
|
||||
[HID_SEND_CH_USB_KEYS] = {
|
||||
.protocol_mode = KEYBOARD_PROTOCOL_MODE_REPORT,
|
||||
},
|
||||
[HID_TRANSPORT_BLE] = {
|
||||
[HID_SEND_CH_USB_CONSUMER] = {
|
||||
.protocol_mode = KEYBOARD_PROTOCOL_MODE_REPORT,
|
||||
},
|
||||
[HID_SEND_CH_BLE_SHARED] = {
|
||||
.protocol_mode = KEYBOARD_PROTOCOL_MODE_REPORT,
|
||||
},
|
||||
};
|
||||
@@ -65,21 +66,28 @@ static struct queued_report consumer_fifo[HID_FLOWCTRL_FIFO_DEPTH];
|
||||
static uint8_t consumer_fifo_head;
|
||||
static uint8_t consumer_fifo_tail;
|
||||
static uint8_t consumer_fifo_count;
|
||||
static struct in_flight_report in_flight;
|
||||
static struct in_flight_report in_flight[HID_SEND_CH_COUNT];
|
||||
static enum mode_switch_mode current_mode;
|
||||
static uint16_t next_sequence;
|
||||
static bool initialized;
|
||||
static bool running;
|
||||
|
||||
static bool mode_to_transport(enum mode_switch_mode mode, enum hid_transport *transport)
|
||||
static bool current_mode_to_channel(enum keyboard_report_type report_type,
|
||||
enum hid_send_channel *channel)
|
||||
{
|
||||
switch (mode) {
|
||||
if (channel == NULL) {
|
||||
return false;
|
||||
}
|
||||
|
||||
switch (current_mode) {
|
||||
case MODE_SWITCH_USB:
|
||||
*transport = HID_TRANSPORT_USB;
|
||||
*channel = (report_type == KEYBOARD_REPORT_TYPE_KEYS) ?
|
||||
HID_SEND_CH_USB_KEYS :
|
||||
HID_SEND_CH_USB_CONSUMER;
|
||||
return true;
|
||||
|
||||
case MODE_SWITCH_BLE:
|
||||
*transport = HID_TRANSPORT_BLE;
|
||||
*channel = HID_SEND_CH_BLE_SHARED;
|
||||
return true;
|
||||
|
||||
default:
|
||||
@@ -131,112 +139,137 @@ static bool consumer_fifo_pop(struct queued_report *entry)
|
||||
return true;
|
||||
}
|
||||
|
||||
static bool transport_can_send_report(enum keyboard_report_type report_type)
|
||||
static bool channel_can_send_report(enum hid_send_channel channel,
|
||||
enum keyboard_report_type report_type,
|
||||
enum keyboard_protocol_mode protocol_mode)
|
||||
{
|
||||
enum hid_transport transport;
|
||||
struct hid_transport_state_data *state;
|
||||
const struct hid_channel_state_data *state = &channel_state[channel];
|
||||
|
||||
if (!mode_to_transport(current_mode, &transport) || in_flight.active) {
|
||||
return false;
|
||||
}
|
||||
|
||||
state = &transport_state[transport];
|
||||
|
||||
if (!state->ready) {
|
||||
if (in_flight[channel].active) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (report_type == KEYBOARD_REPORT_TYPE_KEYS) {
|
||||
return state->keys_ready;
|
||||
return (state->report_ready_bm & BIT(KEYBOARD_REPORT_TYPE_KEYS)) &&
|
||||
(state->protocol_mode == protocol_mode);
|
||||
}
|
||||
|
||||
return state->consumer_ready;
|
||||
if (channel == HID_SEND_CH_BLE_SHARED) {
|
||||
return (state->report_ready_bm & BIT(KEYBOARD_REPORT_TYPE_CONSUMER)) &&
|
||||
(state->protocol_mode == KEYBOARD_PROTOCOL_MODE_REPORT);
|
||||
}
|
||||
|
||||
return (state->report_ready_bm & BIT(KEYBOARD_REPORT_TYPE_CONSUMER)) != 0U;
|
||||
}
|
||||
|
||||
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)
|
||||
static void try_send_keys(void)
|
||||
{
|
||||
struct hid_tx_report_event *event = new_hid_tx_report_event(size);
|
||||
enum hid_send_channel channel;
|
||||
|
||||
event->transport = transport;
|
||||
event->report_type = report_type;
|
||||
event->protocol_mode = protocol_mode;
|
||||
event->sequence = next_sequence++;
|
||||
memcpy(event->dyndata.data, data, size);
|
||||
if (!pending_keys.valid) {
|
||||
return;
|
||||
}
|
||||
|
||||
in_flight.active = true;
|
||||
in_flight.transport = transport;
|
||||
in_flight.report_type = report_type;
|
||||
in_flight.sequence = event->sequence;
|
||||
if (!current_mode_to_channel(KEYBOARD_REPORT_TYPE_KEYS, &channel)) {
|
||||
return;
|
||||
}
|
||||
|
||||
APP_EVENT_SUBMIT(event);
|
||||
if (!channel_can_send_report(channel, pending_keys.report_type,
|
||||
pending_keys.protocol_mode)) {
|
||||
return;
|
||||
}
|
||||
|
||||
in_flight[channel].active = true;
|
||||
in_flight[channel].channel = channel;
|
||||
in_flight[channel].report_type = pending_keys.report_type;
|
||||
in_flight[channel].sequence = next_sequence++;
|
||||
(void)submit_hid_tx_report_event(channel, pending_keys.report_type,
|
||||
pending_keys.protocol_mode,
|
||||
in_flight[channel].sequence,
|
||||
pending_keys.data, pending_keys.size);
|
||||
pending_keys.valid = false;
|
||||
}
|
||||
|
||||
static void try_send_consumer_fifo(void)
|
||||
{
|
||||
struct queued_report queued;
|
||||
enum hid_send_channel channel;
|
||||
|
||||
if (consumer_fifo_count == 0U) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (!current_mode_to_channel(KEYBOARD_REPORT_TYPE_CONSUMER, &channel)) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (!consumer_fifo_pop(&queued)) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (!channel_can_send_report(channel, queued.report_type,
|
||||
queued.protocol_mode)) {
|
||||
if (queued.protocol_mode == channel_state[channel].protocol_mode) {
|
||||
consumer_fifo_push(queued.report_type, queued.protocol_mode,
|
||||
queued.data, queued.size);
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
in_flight[channel].active = true;
|
||||
in_flight[channel].channel = channel;
|
||||
in_flight[channel].report_type = queued.report_type;
|
||||
in_flight[channel].sequence = next_sequence++;
|
||||
(void)submit_hid_tx_report_event(channel, queued.report_type,
|
||||
queued.protocol_mode,
|
||||
in_flight[channel].sequence,
|
||||
queued.data, queued.size);
|
||||
}
|
||||
|
||||
static void try_send_consumer_latest(void)
|
||||
{
|
||||
enum hid_send_channel channel;
|
||||
|
||||
if (!pending_consumer_latest.valid) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (!current_mode_to_channel(KEYBOARD_REPORT_TYPE_CONSUMER, &channel)) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (!channel_can_send_report(channel,
|
||||
pending_consumer_latest.report_type,
|
||||
pending_consumer_latest.protocol_mode)) {
|
||||
return;
|
||||
}
|
||||
|
||||
in_flight[channel].active = true;
|
||||
in_flight[channel].channel = channel;
|
||||
in_flight[channel].report_type = pending_consumer_latest.report_type;
|
||||
in_flight[channel].sequence = next_sequence++;
|
||||
(void)submit_hid_tx_report_event(channel,
|
||||
pending_consumer_latest.report_type,
|
||||
pending_consumer_latest.protocol_mode,
|
||||
in_flight[channel].sequence,
|
||||
pending_consumer_latest.data,
|
||||
pending_consumer_latest.size);
|
||||
pending_consumer_latest.valid = false;
|
||||
}
|
||||
|
||||
static void try_send_next(void)
|
||||
{
|
||||
struct queued_report queued;
|
||||
enum hid_transport transport;
|
||||
struct hid_transport_state_data *state;
|
||||
|
||||
if (!running || in_flight.active || !mode_to_transport(current_mode, &transport)) {
|
||||
if (!running) {
|
||||
return;
|
||||
}
|
||||
|
||||
state = &transport_state[transport];
|
||||
|
||||
if (!state->ready) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (pending_keys.valid && transport_can_send_report(KEYBOARD_REPORT_TYPE_KEYS)) {
|
||||
if (pending_keys.protocol_mode != state->protocol_mode) {
|
||||
LOG_WRN("Drop stale keys report after protocol change");
|
||||
pending_keys.valid = false;
|
||||
} else {
|
||||
submit_hid_tx_report_event(transport,
|
||||
pending_keys.report_type,
|
||||
pending_keys.protocol_mode,
|
||||
pending_keys.data,
|
||||
pending_keys.size);
|
||||
pending_keys.valid = false;
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
if ((consumer_fifo_count > 0U) &&
|
||||
transport_can_send_report(KEYBOARD_REPORT_TYPE_CONSUMER) &&
|
||||
consumer_fifo_pop(&queued)) {
|
||||
if (queued.protocol_mode != state->protocol_mode) {
|
||||
LOG_WRN("Drop stale consumer report after protocol change");
|
||||
} else {
|
||||
submit_hid_tx_report_event(transport,
|
||||
queued.report_type,
|
||||
queued.protocol_mode,
|
||||
queued.data,
|
||||
queued.size);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
if (pending_consumer_latest.valid &&
|
||||
transport_can_send_report(KEYBOARD_REPORT_TYPE_CONSUMER)) {
|
||||
if (pending_consumer_latest.protocol_mode != state->protocol_mode) {
|
||||
LOG_WRN("Drop stale latest consumer report after protocol change");
|
||||
pending_consumer_latest.valid = false;
|
||||
} else {
|
||||
submit_hid_tx_report_event(transport,
|
||||
pending_consumer_latest.report_type,
|
||||
pending_consumer_latest.protocol_mode,
|
||||
pending_consumer_latest.data,
|
||||
pending_consumer_latest.size);
|
||||
pending_consumer_latest.valid = false;
|
||||
}
|
||||
}
|
||||
try_send_keys();
|
||||
try_send_consumer_fifo();
|
||||
try_send_consumer_latest();
|
||||
}
|
||||
|
||||
static bool handle_keyboard_hid_report_event(const struct keyboard_hid_report_event *event)
|
||||
static bool handle_keyboard_hid_report_event(
|
||||
const struct keyboard_hid_report_event *event)
|
||||
{
|
||||
if (!running ||
|
||||
((event->mode != MODE_SWITCH_USB) && (event->mode != MODE_SWITCH_BLE))) {
|
||||
@@ -267,41 +300,18 @@ static bool handle_keyboard_hid_report_event(const struct keyboard_hid_report_ev
|
||||
return false;
|
||||
}
|
||||
|
||||
static bool handle_hid_transport_state_event(const struct hid_transport_state_event *event)
|
||||
static bool handle_hid_channel_state_event(
|
||||
const struct hid_channel_state_event *event)
|
||||
{
|
||||
enum hid_transport active_transport;
|
||||
struct hid_transport_state_data *state;
|
||||
|
||||
if (event->transport >= HID_TRANSPORT_COUNT) {
|
||||
if (event->channel >= HID_SEND_CH_COUNT) {
|
||||
return false;
|
||||
}
|
||||
|
||||
state = &transport_state[event->transport];
|
||||
channel_state[event->channel].report_ready_bm = event->report_ready_bm;
|
||||
channel_state[event->channel].protocol_mode = event->protocol_mode;
|
||||
|
||||
state->ready = event->ready;
|
||||
state->keys_ready = event->keys_ready;
|
||||
state->consumer_ready = event->consumer_ready;
|
||||
|
||||
if (state->protocol_mode != event->protocol_mode) {
|
||||
state->protocol_mode = event->protocol_mode;
|
||||
|
||||
if (mode_to_transport(current_mode, &active_transport) &&
|
||||
(active_transport == event->transport)) {
|
||||
pending_keys.valid = false;
|
||||
pending_consumer_latest.valid = false;
|
||||
consumer_fifo_head = 0U;
|
||||
consumer_fifo_tail = 0U;
|
||||
consumer_fifo_count = 0U;
|
||||
}
|
||||
}
|
||||
|
||||
if (!state->ready &&
|
||||
mode_to_transport(current_mode, &active_transport) &&
|
||||
(active_transport == event->transport)) {
|
||||
consumer_fifo_head = 0U;
|
||||
consumer_fifo_tail = 0U;
|
||||
consumer_fifo_count = 0U;
|
||||
in_flight.active = false;
|
||||
if (event->report_ready_bm == 0U) {
|
||||
in_flight[event->channel].active = false;
|
||||
}
|
||||
|
||||
try_send_next();
|
||||
@@ -310,17 +320,21 @@ static bool handle_hid_transport_state_event(const struct hid_transport_state_ev
|
||||
|
||||
static bool handle_hid_report_sent_event(const struct hid_report_sent_event *event)
|
||||
{
|
||||
if (!in_flight.active || (event->transport != in_flight.transport)) {
|
||||
if (event->channel >= HID_SEND_CH_COUNT) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (event->sequence != in_flight.sequence) {
|
||||
if (!in_flight[event->channel].active) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (event->sequence != in_flight[event->channel].sequence) {
|
||||
LOG_WRN("Unexpected HID sent sequence %u (expected %u)",
|
||||
event->sequence, in_flight.sequence);
|
||||
event->sequence, in_flight[event->channel].sequence);
|
||||
return false;
|
||||
}
|
||||
|
||||
in_flight.active = false;
|
||||
in_flight[event->channel].active = false;
|
||||
|
||||
if (event->error) {
|
||||
LOG_WRN("HID report send failed for seq %u", event->sequence);
|
||||
@@ -349,18 +363,14 @@ static int module_init(void)
|
||||
{
|
||||
clear_pending_reports();
|
||||
current_mode = MODE_SWITCH_USB;
|
||||
transport_state[HID_TRANSPORT_USB].ready = false;
|
||||
transport_state[HID_TRANSPORT_USB].keys_ready = false;
|
||||
transport_state[HID_TRANSPORT_USB].consumer_ready = false;
|
||||
transport_state[HID_TRANSPORT_USB].protocol_mode =
|
||||
memset(channel_state, 0, sizeof(channel_state));
|
||||
channel_state[HID_SEND_CH_USB_KEYS].protocol_mode =
|
||||
KEYBOARD_PROTOCOL_MODE_REPORT;
|
||||
transport_state[HID_TRANSPORT_BLE].ready = false;
|
||||
transport_state[HID_TRANSPORT_BLE].keys_ready = false;
|
||||
transport_state[HID_TRANSPORT_BLE].consumer_ready = false;
|
||||
transport_state[HID_TRANSPORT_BLE].protocol_mode =
|
||||
channel_state[HID_SEND_CH_USB_CONSUMER].protocol_mode =
|
||||
KEYBOARD_PROTOCOL_MODE_REPORT;
|
||||
channel_state[HID_SEND_CH_BLE_SHARED].protocol_mode =
|
||||
KEYBOARD_PROTOCOL_MODE_REPORT;
|
||||
next_sequence = 1U;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -372,7 +382,6 @@ static int module_start(void)
|
||||
|
||||
running = true;
|
||||
try_send_next();
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -389,15 +398,18 @@ static void module_pause(void)
|
||||
static bool app_event_handler(const struct app_event_header *aeh)
|
||||
{
|
||||
if (is_keyboard_hid_report_event(aeh)) {
|
||||
return handle_keyboard_hid_report_event(cast_keyboard_hid_report_event(aeh));
|
||||
return handle_keyboard_hid_report_event(
|
||||
cast_keyboard_hid_report_event(aeh));
|
||||
}
|
||||
|
||||
if (is_hid_transport_state_event(aeh)) {
|
||||
return handle_hid_transport_state_event(cast_hid_transport_state_event(aeh));
|
||||
if (is_hid_channel_state_event(aeh)) {
|
||||
return handle_hid_channel_state_event(
|
||||
cast_hid_channel_state_event(aeh));
|
||||
}
|
||||
|
||||
if (is_hid_report_sent_event(aeh)) {
|
||||
return handle_hid_report_sent_event(cast_hid_report_sent_event(aeh));
|
||||
return handle_hid_report_sent_event(
|
||||
cast_hid_report_sent_event(aeh));
|
||||
}
|
||||
|
||||
if (is_mode_switch_event(aeh)) {
|
||||
@@ -405,7 +417,8 @@ static bool app_event_handler(const struct app_event_header *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);
|
||||
|
||||
if (check_state(event, MODULE_ID(main), MODULE_STATE_READY)) {
|
||||
int err;
|
||||
@@ -460,7 +473,7 @@ static bool app_event_handler(const struct app_event_header *aeh)
|
||||
|
||||
APP_EVENT_LISTENER(MODULE, app_event_handler);
|
||||
APP_EVENT_SUBSCRIBE(MODULE, keyboard_hid_report_event);
|
||||
APP_EVENT_SUBSCRIBE(MODULE, hid_transport_state_event);
|
||||
APP_EVENT_SUBSCRIBE(MODULE, hid_channel_state_event);
|
||||
APP_EVENT_SUBSCRIBE(MODULE, hid_report_sent_event);
|
||||
APP_EVENT_SUBSCRIBE(MODULE, mode_switch_event);
|
||||
APP_EVENT_SUBSCRIBE(MODULE, module_state_event);
|
||||
|
||||
@@ -15,6 +15,8 @@
|
||||
#include <zephyr/sys/util.h>
|
||||
|
||||
#include "encoder_event.h"
|
||||
#include "function_bitmap_state_event.h"
|
||||
#include "function_bitmap_update_event.h"
|
||||
#include "keyboard_core.h"
|
||||
#include "keyboard_hid_report_event.h"
|
||||
#include "mode_switch_event.h"
|
||||
@@ -39,8 +41,8 @@ struct keymap_entry {
|
||||
};
|
||||
|
||||
struct keyboard_state {
|
||||
uint8_t modifiers;
|
||||
uint8_t keys_bitmap[KEYBOARD_NKRO_BITMAP_BYTES];
|
||||
uint8_t pressed_usage_bitmap[KEYBOARD_PROTOCOL_BITMAP_BYTES];
|
||||
uint8_t function_pressed_bitmap[KEYBOARD_PROTOCOL_BITMAP_BYTES];
|
||||
uint32_t consumer_bits;
|
||||
};
|
||||
|
||||
@@ -85,6 +87,7 @@ static const uint16_t consumer_usage_map[KEYBOARD_CONSUMER_CTRL_COUNT] = {
|
||||
|
||||
static struct keyboard_state keyboard_state;
|
||||
static struct keyboard_reports_cache reports_cache;
|
||||
static uint8_t function_usage_mask[KEYBOARD_PROTOCOL_BITMAP_BYTES];
|
||||
static enum keyboard_protocol_mode transport_protocol_modes[HID_TRANSPORT_COUNT] = {
|
||||
[HID_TRANSPORT_USB] = KEYBOARD_PROTOCOL_MODE_REPORT,
|
||||
[HID_TRANSPORT_BLE] = KEYBOARD_PROTOCOL_MODE_REPORT,
|
||||
@@ -143,40 +146,61 @@ static const struct keymap_entry *keymap_get(uint16_t key_id)
|
||||
return NULL;
|
||||
}
|
||||
|
||||
static bool usage_is_modifier(uint16_t usage_id)
|
||||
static bool usage_to_bitmap_pos(uint16_t usage_id, uint8_t *byte_idx,
|
||||
uint8_t *bit_idx)
|
||||
{
|
||||
return IN_RANGE(usage_id, KEYBOARD_USAGE_FIRST_MODIFIER,
|
||||
KEYBOARD_USAGE_LAST_MODIFIER);
|
||||
}
|
||||
|
||||
static bool keyboard_key_update(uint16_t usage_id, bool pressed)
|
||||
{
|
||||
if (usage_is_modifier(usage_id)) {
|
||||
uint8_t new_modifiers = keyboard_state.modifiers;
|
||||
|
||||
WRITE_BIT(new_modifiers, usage_id - KEYBOARD_USAGE_FIRST_MODIFIER, pressed);
|
||||
if (new_modifiers == keyboard_state.modifiers) {
|
||||
if ((byte_idx == NULL) || (bit_idx == NULL)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
keyboard_state.modifiers = new_modifiers;
|
||||
if ((usage_id >= KEYBOARD_USAGE_FIRST_MODIFIER) &&
|
||||
(usage_id <= KEYBOARD_USAGE_LAST_MODIFIER)) {
|
||||
*byte_idx = 0U;
|
||||
*bit_idx = (uint8_t)(usage_id - KEYBOARD_USAGE_FIRST_MODIFIER);
|
||||
return true;
|
||||
}
|
||||
|
||||
if (usage_id > KEYBOARD_NKRO_USAGE_MAX) {
|
||||
if (usage_id <= KEYBOARD_NKRO_USAGE_MAX) {
|
||||
*byte_idx = (uint8_t)(1U + (usage_id / 8U));
|
||||
*bit_idx = (uint8_t)(usage_id % 8U);
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
static bool usage_bitmap_test(const uint8_t *bitmap, uint16_t usage_id)
|
||||
{
|
||||
uint8_t byte_idx;
|
||||
uint8_t bit_idx;
|
||||
|
||||
if ((bitmap == NULL) ||
|
||||
!usage_to_bitmap_pos(usage_id, &byte_idx, &bit_idx)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return (bitmap[byte_idx] & BIT(bit_idx)) != 0U;
|
||||
}
|
||||
|
||||
static bool usage_bitmap_write(uint8_t *bitmap, uint16_t usage_id, bool pressed)
|
||||
{
|
||||
uint8_t byte_idx;
|
||||
uint8_t bit_idx;
|
||||
bool was_pressed;
|
||||
|
||||
if ((bitmap == NULL) ||
|
||||
!usage_to_bitmap_pos(usage_id, &byte_idx, &bit_idx)) {
|
||||
LOG_WRN("Unsupported keyboard usage 0x%04x", usage_id);
|
||||
return false;
|
||||
}
|
||||
|
||||
uint8_t byte_idx = usage_id / 8U;
|
||||
uint8_t bit_idx = usage_id % 8U;
|
||||
bool was_pressed = (keyboard_state.keys_bitmap[byte_idx] & BIT(bit_idx)) != 0U;
|
||||
was_pressed = (bitmap[byte_idx] & BIT(bit_idx)) != 0U;
|
||||
|
||||
if (was_pressed == pressed) {
|
||||
return false;
|
||||
}
|
||||
|
||||
WRITE_BIT(keyboard_state.keys_bitmap[byte_idx], bit_idx, pressed);
|
||||
WRITE_BIT(bitmap[byte_idx], bit_idx, pressed);
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -202,6 +226,11 @@ static void keyboard_state_clear(void)
|
||||
memset(&keyboard_state, 0, sizeof(keyboard_state));
|
||||
}
|
||||
|
||||
static void function_usage_mask_clear(void)
|
||||
{
|
||||
memset(function_usage_mask, 0, sizeof(function_usage_mask));
|
||||
}
|
||||
|
||||
static void reports_cache_invalidate(void)
|
||||
{
|
||||
reports_cache.boot_valid = false;
|
||||
@@ -209,19 +238,31 @@ static void reports_cache_invalidate(void)
|
||||
reports_cache.consumer_valid = false;
|
||||
}
|
||||
|
||||
static void build_effective_hid_bitmap(uint8_t bitmap[KEYBOARD_PROTOCOL_BITMAP_BYTES])
|
||||
{
|
||||
for (size_t i = 0; i < KEYBOARD_PROTOCOL_BITMAP_BYTES; i++) {
|
||||
bitmap[i] = keyboard_state.pressed_usage_bitmap[i] &
|
||||
(uint8_t)~keyboard_state.function_pressed_bitmap[i];
|
||||
}
|
||||
}
|
||||
|
||||
static void build_boot_report(uint8_t report[KEYBOARD_BOOT_REPORT_SIZE])
|
||||
{
|
||||
uint8_t effective_hid_bitmap[KEYBOARD_PROTOCOL_BITMAP_BYTES];
|
||||
size_t key_count = 0;
|
||||
|
||||
build_effective_hid_bitmap(effective_hid_bitmap);
|
||||
memset(report, 0, KEYBOARD_BOOT_REPORT_SIZE);
|
||||
report[0] = keyboard_state.modifiers;
|
||||
report[0] = effective_hid_bitmap[0];
|
||||
report[1] = KEYBOARD_BOOT_RESERVED_BYTE;
|
||||
|
||||
for (uint16_t usage_id = 0; usage_id <= KEYBOARD_NKRO_USAGE_MAX; usage_id++) {
|
||||
uint8_t byte_idx = usage_id / 8U;
|
||||
uint8_t bit_idx = usage_id % 8U;
|
||||
uint8_t byte_idx;
|
||||
uint8_t bit_idx;
|
||||
|
||||
if ((keyboard_state.keys_bitmap[byte_idx] & BIT(bit_idx)) == 0U) {
|
||||
(void)usage_to_bitmap_pos(usage_id, &byte_idx, &bit_idx);
|
||||
|
||||
if ((effective_hid_bitmap[byte_idx] & BIT(bit_idx)) == 0U) {
|
||||
continue;
|
||||
}
|
||||
|
||||
@@ -238,8 +279,11 @@ static void build_boot_report(uint8_t report[KEYBOARD_BOOT_REPORT_SIZE])
|
||||
|
||||
static void build_nkro_report(uint8_t report[KEYBOARD_NKRO_REPORT_SIZE])
|
||||
{
|
||||
report[0] = keyboard_state.modifiers;
|
||||
memcpy(&report[1], keyboard_state.keys_bitmap, KEYBOARD_NKRO_BITMAP_BYTES);
|
||||
uint8_t effective_hid_bitmap[KEYBOARD_PROTOCOL_BITMAP_BYTES];
|
||||
|
||||
build_effective_hid_bitmap(effective_hid_bitmap);
|
||||
report[0] = effective_hid_bitmap[0];
|
||||
memcpy(&report[1], &effective_hid_bitmap[1], KEYBOARD_NKRO_BITMAP_BYTES);
|
||||
}
|
||||
|
||||
static uint16_t active_consumer_usage_get(void)
|
||||
@@ -258,23 +302,6 @@ static void build_consumer_report(uint8_t report[KEYBOARD_CONSUMER_REPORT_SIZE])
|
||||
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_consumer_fifo_frame(uint16_t usage_id)
|
||||
{
|
||||
uint8_t report_buf[KEYBOARD_CONSUMER_REPORT_SIZE];
|
||||
@@ -286,10 +313,9 @@ static void submit_consumer_fifo_frame(uint16_t usage_id)
|
||||
}
|
||||
|
||||
sys_put_le16(usage_id, report_buf);
|
||||
submit_keyboard_report_event(KEYBOARD_REPORT_TYPE_CONSUMER,
|
||||
HID_QUEUE_POLICY_FIFO,
|
||||
report_buf,
|
||||
KEYBOARD_CONSUMER_REPORT_SIZE);
|
||||
(void)submit_keyboard_hid_report_event(
|
||||
current_mode, KEYBOARD_REPORT_TYPE_CONSUMER, protocol_mode,
|
||||
HID_QUEUE_POLICY_FIFO, report_buf, KEYBOARD_CONSUMER_REPORT_SIZE);
|
||||
}
|
||||
|
||||
static void submit_consumer_pulse_frames(enum keyboard_consumer_control control_id,
|
||||
@@ -349,10 +375,9 @@ static void emit_keys_report(bool force)
|
||||
memcpy(cache_buf, report_buf, report_size);
|
||||
*cache_valid = true;
|
||||
|
||||
submit_keyboard_report_event(KEYBOARD_REPORT_TYPE_KEYS,
|
||||
HID_QUEUE_POLICY_LATEST,
|
||||
report_buf,
|
||||
report_size);
|
||||
(void)submit_keyboard_hid_report_event(
|
||||
current_mode, KEYBOARD_REPORT_TYPE_KEYS, protocol_mode,
|
||||
HID_QUEUE_POLICY_LATEST, report_buf, report_size);
|
||||
}
|
||||
|
||||
static void emit_consumer_report(bool force)
|
||||
@@ -374,9 +399,9 @@ static void emit_consumer_report(bool force)
|
||||
memcpy(reports_cache.consumer_report, report_buf, KEYBOARD_CONSUMER_REPORT_SIZE);
|
||||
reports_cache.consumer_valid = true;
|
||||
|
||||
submit_keyboard_report_event(KEYBOARD_REPORT_TYPE_CONSUMER,
|
||||
HID_QUEUE_POLICY_LATEST,
|
||||
report_buf,
|
||||
(void)submit_keyboard_hid_report_event(
|
||||
current_mode, KEYBOARD_REPORT_TYPE_CONSUMER, protocol_mode,
|
||||
HID_QUEUE_POLICY_LATEST, report_buf,
|
||||
KEYBOARD_CONSUMER_REPORT_SIZE);
|
||||
}
|
||||
|
||||
@@ -389,9 +414,13 @@ static void emit_all_reports(bool force)
|
||||
}
|
||||
}
|
||||
|
||||
static void emit_function_state_event(void)
|
||||
{
|
||||
(void)submit_function_bitmap_state_event(keyboard_state.pressed_usage_bitmap);
|
||||
}
|
||||
|
||||
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 consumer_report[KEYBOARD_CONSUMER_REPORT_SIZE] = { 0 };
|
||||
enum keyboard_protocol_mode protocol_mode = active_protocol_mode_get();
|
||||
@@ -399,21 +428,15 @@ static void emit_release_reports(enum mode_switch_mode mode)
|
||||
(protocol_mode == KEYBOARD_PROTOCOL_MODE_BOOT) ?
|
||||
KEYBOARD_BOOT_REPORT_SIZE : KEYBOARD_NKRO_REPORT_SIZE;
|
||||
|
||||
event = new_keyboard_hid_report_event(keys_report_size);
|
||||
event->mode = mode;
|
||||
event->report_type = KEYBOARD_REPORT_TYPE_KEYS;
|
||||
event->protocol_mode = protocol_mode;
|
||||
memcpy(event->dyndata.data, keys_report, keys_report_size);
|
||||
APP_EVENT_SUBMIT(event);
|
||||
(void)submit_keyboard_hid_report_event(
|
||||
mode, KEYBOARD_REPORT_TYPE_KEYS, protocol_mode,
|
||||
HID_QUEUE_POLICY_LATEST, keys_report, keys_report_size);
|
||||
|
||||
if (protocol_mode != KEYBOARD_PROTOCOL_MODE_BOOT) {
|
||||
event = new_keyboard_hid_report_event(KEYBOARD_CONSUMER_REPORT_SIZE);
|
||||
event->mode = mode;
|
||||
event->report_type = KEYBOARD_REPORT_TYPE_CONSUMER;
|
||||
event->protocol_mode = protocol_mode;
|
||||
memcpy(event->dyndata.data, consumer_report,
|
||||
(void)submit_keyboard_hid_report_event(
|
||||
mode, KEYBOARD_REPORT_TYPE_CONSUMER, protocol_mode,
|
||||
HID_QUEUE_POLICY_LATEST, consumer_report,
|
||||
KEYBOARD_CONSUMER_REPORT_SIZE);
|
||||
APP_EVENT_SUBMIT(event);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -421,6 +444,7 @@ static int module_init(void)
|
||||
{
|
||||
keyboard_state_clear();
|
||||
reports_cache_invalidate();
|
||||
function_usage_mask_clear();
|
||||
mode_valid = false;
|
||||
transport_protocol_modes[HID_TRANSPORT_USB] =
|
||||
KEYBOARD_PROTOCOL_MODE_REPORT;
|
||||
@@ -449,6 +473,7 @@ static void module_pause(void)
|
||||
if (mode_valid) {
|
||||
emit_release_reports(current_mode);
|
||||
}
|
||||
emit_function_state_event();
|
||||
|
||||
keyboard_state_clear();
|
||||
reports_cache_invalidate();
|
||||
@@ -472,8 +497,30 @@ static bool handle_button_event(const struct button_event *event)
|
||||
}
|
||||
|
||||
if (entry->usage_type == KEY_USAGE_TYPE_KEYBOARD) {
|
||||
changed = keyboard_key_update(entry->usage_id, event->pressed);
|
||||
if (changed) {
|
||||
bool routed_to_function;
|
||||
|
||||
changed = usage_bitmap_write(keyboard_state.pressed_usage_bitmap,
|
||||
entry->usage_id, event->pressed);
|
||||
if (!changed) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (event->pressed) {
|
||||
routed_to_function =
|
||||
usage_bitmap_test(function_usage_mask, entry->usage_id);
|
||||
(void)usage_bitmap_write(keyboard_state.function_pressed_bitmap,
|
||||
entry->usage_id, routed_to_function);
|
||||
} else {
|
||||
routed_to_function =
|
||||
usage_bitmap_test(keyboard_state.function_pressed_bitmap,
|
||||
entry->usage_id);
|
||||
(void)usage_bitmap_write(keyboard_state.function_pressed_bitmap,
|
||||
entry->usage_id, false);
|
||||
}
|
||||
|
||||
if (routed_to_function) {
|
||||
emit_function_state_event();
|
||||
} else {
|
||||
emit_keys_report(false);
|
||||
}
|
||||
} else {
|
||||
@@ -498,6 +545,7 @@ static bool handle_mode_switch_event(const struct mode_switch_event *event)
|
||||
mode_changed = mode_valid && (current_mode != event->mode);
|
||||
if (mode_changed) {
|
||||
emit_release_reports(current_mode);
|
||||
emit_function_state_event();
|
||||
keyboard_state_clear();
|
||||
reports_cache_invalidate();
|
||||
}
|
||||
@@ -526,6 +574,13 @@ static bool handle_encoder_event(const struct encoder_event *event)
|
||||
return false;
|
||||
}
|
||||
|
||||
static bool handle_function_bitmap_update_event(
|
||||
const struct function_bitmap_update_event *event)
|
||||
{
|
||||
memcpy(function_usage_mask, event->bitmap, sizeof(function_usage_mask));
|
||||
return false;
|
||||
}
|
||||
|
||||
static bool app_event_handler(const struct app_event_header *aeh)
|
||||
{
|
||||
if (is_button_event(aeh)) {
|
||||
@@ -536,6 +591,11 @@ static bool app_event_handler(const struct app_event_header *aeh)
|
||||
return handle_encoder_event(cast_encoder_event(aeh));
|
||||
}
|
||||
|
||||
if (is_function_bitmap_update_event(aeh)) {
|
||||
return handle_function_bitmap_update_event(
|
||||
cast_function_bitmap_update_event(aeh));
|
||||
}
|
||||
|
||||
if (is_set_protocol_event(aeh)) {
|
||||
const struct set_protocol_event *event = cast_set_protocol_event(aeh);
|
||||
enum hid_transport active_transport;
|
||||
@@ -623,6 +683,7 @@ static bool app_event_handler(const struct app_event_header *aeh)
|
||||
APP_EVENT_LISTENER(MODULE, app_event_handler);
|
||||
APP_EVENT_SUBSCRIBE(MODULE, button_event);
|
||||
APP_EVENT_SUBSCRIBE(MODULE, encoder_event);
|
||||
APP_EVENT_SUBSCRIBE(MODULE, function_bitmap_update_event);
|
||||
APP_EVENT_SUBSCRIBE(MODULE, set_protocol_event);
|
||||
APP_EVENT_SUBSCRIBE(MODULE, mode_switch_event);
|
||||
APP_EVENT_SUBSCRIBE(MODULE, module_state_event);
|
||||
|
||||
155
src/led_effect/effects/led_effect_key_fade.c
Normal file
155
src/led_effect/effects/led_effect_key_fade.c
Normal file
@@ -0,0 +1,155 @@
|
||||
#include <stdbool.h>
|
||||
#include <stddef.h>
|
||||
#include <stdint.h>
|
||||
#include <string.h>
|
||||
|
||||
#include <zephyr/sys/util.h>
|
||||
|
||||
#include "led_effect/led_effect.h"
|
||||
|
||||
#define KEY_FADE_LEVEL_MAX 255U
|
||||
#define KEY_FADE_STEP_PER_TICK 12U
|
||||
|
||||
struct led_effect_key_fade_state {
|
||||
const struct led_key_map *key_map;
|
||||
size_t key_map_len;
|
||||
size_t pixel_count;
|
||||
uint8_t default_brightness;
|
||||
uint8_t level[17];
|
||||
struct theme_rgb theme;
|
||||
};
|
||||
|
||||
static struct led_effect_key_fade_state key_fade_state;
|
||||
|
||||
static const struct led_key_map *find_key(const struct led_effect *effect,
|
||||
uint16_t key_id)
|
||||
{
|
||||
const struct led_effect_key_fade_state *state = effect->state;
|
||||
|
||||
for (size_t i = 0; i < state->key_map_len; i++) {
|
||||
if (state->key_map[i].key_id == key_id) {
|
||||
return &state->key_map[i];
|
||||
}
|
||||
}
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
static int led_effect_key_fade_init(struct led_effect *effect,
|
||||
const struct led_effect_config *cfg)
|
||||
{
|
||||
struct led_effect_key_fade_state *state = effect->state;
|
||||
|
||||
if ((effect == NULL) || (cfg == NULL) || (cfg->key_map == NULL) ||
|
||||
(cfg->pixel_count > ARRAY_SIZE(state->level))) {
|
||||
return -EINVAL;
|
||||
}
|
||||
|
||||
memset(state, 0, sizeof(*state));
|
||||
state->key_map = cfg->key_map;
|
||||
state->key_map_len = cfg->key_map_len;
|
||||
state->pixel_count = cfg->pixel_count;
|
||||
state->default_brightness = cfg->default_brightness;
|
||||
state->theme.r = BLINKY_THEME_DEFAULT_R;
|
||||
state->theme.g = BLINKY_THEME_DEFAULT_G;
|
||||
state->theme.b = BLINKY_THEME_DEFAULT_B;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static void led_effect_key_fade_reset(struct led_effect *effect)
|
||||
{
|
||||
struct led_effect_key_fade_state *state = effect->state;
|
||||
|
||||
memset(state->level, 0, sizeof(state->level));
|
||||
}
|
||||
|
||||
static void led_effect_key_fade_set_theme(struct led_effect *effect,
|
||||
const struct theme_rgb *theme)
|
||||
{
|
||||
struct led_effect_key_fade_state *state = effect->state;
|
||||
|
||||
if (theme == NULL) {
|
||||
return;
|
||||
}
|
||||
|
||||
state->theme = *theme;
|
||||
}
|
||||
|
||||
static void led_effect_key_fade_on_key_press(struct led_effect *effect,
|
||||
uint16_t key_id)
|
||||
{
|
||||
struct led_effect_key_fade_state *state = effect->state;
|
||||
const struct led_key_map *map = find_key(effect, key_id);
|
||||
|
||||
if ((map == NULL) || (map->led_idx >= state->pixel_count)) {
|
||||
return;
|
||||
}
|
||||
|
||||
state->level[map->led_idx] =
|
||||
(state->default_brightness == 0U) ? KEY_FADE_LEVEL_MAX :
|
||||
state->default_brightness;
|
||||
}
|
||||
|
||||
static bool led_effect_key_fade_tick(struct led_effect *effect, uint32_t dt_ms,
|
||||
struct led_rgb *pixels, size_t pixel_count)
|
||||
{
|
||||
struct led_effect_key_fade_state *state = effect->state;
|
||||
bool active = false;
|
||||
bool visible = false;
|
||||
uint8_t decay;
|
||||
|
||||
ARG_UNUSED(dt_ms);
|
||||
|
||||
if ((pixels == NULL) || (pixel_count < state->pixel_count)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
memset(pixels, 0, sizeof(*pixels) * pixel_count);
|
||||
decay = KEY_FADE_STEP_PER_TICK;
|
||||
|
||||
for (size_t i = 0; i < state->pixel_count; i++) {
|
||||
uint8_t level = state->level[i];
|
||||
|
||||
if (level == 0U) {
|
||||
continue;
|
||||
}
|
||||
|
||||
visible = true;
|
||||
pixels[i].r = (uint8_t)(((uint16_t)state->theme.r * level) / 255U);
|
||||
pixels[i].g = (uint8_t)(((uint16_t)state->theme.g * level) / 255U);
|
||||
pixels[i].b = (uint8_t)(((uint16_t)state->theme.b * level) / 255U);
|
||||
|
||||
state->level[i] = (level > decay) ? (uint8_t)(level - decay) : 0U;
|
||||
active = active || (state->level[i] > 0U);
|
||||
}
|
||||
|
||||
return visible || active;
|
||||
}
|
||||
|
||||
static bool led_effect_key_fade_is_active(const struct led_effect *effect)
|
||||
{
|
||||
const struct led_effect_key_fade_state *state = effect->state;
|
||||
|
||||
for (size_t i = 0; i < state->pixel_count; i++) {
|
||||
if (state->level[i] > 0U) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
static const struct led_effect_ops key_fade_ops = {
|
||||
.init = led_effect_key_fade_init,
|
||||
.reset = led_effect_key_fade_reset,
|
||||
.set_theme = led_effect_key_fade_set_theme,
|
||||
.on_key_press = led_effect_key_fade_on_key_press,
|
||||
.tick = led_effect_key_fade_tick,
|
||||
.is_active = led_effect_key_fade_is_active,
|
||||
};
|
||||
|
||||
struct led_effect led_effect_key_fade = {
|
||||
.ops = &key_fade_ops,
|
||||
.state = &key_fade_state,
|
||||
};
|
||||
50
src/led_effect/led_effect.h
Normal file
50
src/led_effect/led_effect.h
Normal file
@@ -0,0 +1,50 @@
|
||||
#ifndef BLINKY_LED_EFFECT_H_
|
||||
#define BLINKY_LED_EFFECT_H_
|
||||
|
||||
#include <stdbool.h>
|
||||
#include <stddef.h>
|
||||
#include <stdint.h>
|
||||
|
||||
#include <zephyr/drivers/led_strip.h>
|
||||
|
||||
#include "led_effect_types.h"
|
||||
#include "theme_color.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
struct led_effect;
|
||||
|
||||
struct led_effect_config {
|
||||
const struct led_key_map *key_map;
|
||||
size_t key_map_len;
|
||||
size_t pixel_count;
|
||||
uint8_t default_brightness;
|
||||
};
|
||||
|
||||
struct led_effect_ops {
|
||||
int (*init)(struct led_effect *effect,
|
||||
const struct led_effect_config *cfg);
|
||||
void (*reset)(struct led_effect *effect);
|
||||
void (*set_theme)(struct led_effect *effect,
|
||||
const struct theme_rgb *theme);
|
||||
void (*on_key_press)(struct led_effect *effect, uint16_t key_id);
|
||||
bool (*tick)(struct led_effect *effect, uint32_t dt_ms,
|
||||
struct led_rgb *pixels, size_t pixel_count);
|
||||
bool (*is_active)(const struct led_effect *effect);
|
||||
};
|
||||
|
||||
struct led_effect {
|
||||
const struct led_effect_ops *ops;
|
||||
void *state;
|
||||
};
|
||||
|
||||
const struct led_effect *led_effect_get(enum led_effect_id id);
|
||||
struct led_effect *led_effect_get_mutable(enum led_effect_id id);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* BLINKY_LED_EFFECT_H_ */
|
||||
27
src/led_effect/led_effect_registry.c
Normal file
27
src/led_effect/led_effect_registry.c
Normal file
@@ -0,0 +1,27 @@
|
||||
#include <stddef.h>
|
||||
|
||||
#include "led_effect.h"
|
||||
|
||||
extern struct led_effect led_effect_key_fade;
|
||||
|
||||
static struct led_effect *const effect_registry[LED_EFFECT_ID_COUNT] = {
|
||||
[LED_EFFECT_ID_KEY_FADE] = &led_effect_key_fade,
|
||||
};
|
||||
|
||||
const struct led_effect *led_effect_get(enum led_effect_id id)
|
||||
{
|
||||
if (id >= LED_EFFECT_ID_COUNT) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
return effect_registry[id];
|
||||
}
|
||||
|
||||
struct led_effect *led_effect_get_mutable(enum led_effect_id id)
|
||||
{
|
||||
if (id >= LED_EFFECT_ID_COUNT) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
return effect_registry[id];
|
||||
}
|
||||
24
src/led_effect/led_effect_types.h
Normal file
24
src/led_effect/led_effect_types.h
Normal file
@@ -0,0 +1,24 @@
|
||||
#ifndef BLINKY_LED_EFFECT_TYPES_H_
|
||||
#define BLINKY_LED_EFFECT_TYPES_H_
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
struct led_key_map {
|
||||
uint16_t key_id;
|
||||
uint8_t led_idx;
|
||||
};
|
||||
|
||||
enum led_effect_id {
|
||||
LED_EFFECT_ID_KEY_FADE = 0,
|
||||
LED_EFFECT_ID_COUNT,
|
||||
};
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* BLINKY_LED_EFFECT_TYPES_H_ */
|
||||
368
src/led_strip_module.c
Normal file
368
src/led_strip_module.c
Normal file
@@ -0,0 +1,368 @@
|
||||
#include <errno.h>
|
||||
#include <stdbool.h>
|
||||
#include <string.h>
|
||||
|
||||
#include <app_event_manager.h>
|
||||
|
||||
#define MODULE led_strip_module
|
||||
#include <caf/events/button_event.h>
|
||||
#include <caf/events/module_state_event.h>
|
||||
#include <caf/events/power_event.h>
|
||||
#include <caf/key_id.h>
|
||||
|
||||
#include <zephyr/device.h>
|
||||
#include <zephyr/drivers/gpio.h>
|
||||
#include <zephyr/drivers/led_strip.h>
|
||||
#include <zephyr/kernel.h>
|
||||
#include <zephyr/logging/log.h>
|
||||
|
||||
#include "led_effect/led_effect.h"
|
||||
#include "led_strip_en_event.h"
|
||||
#include "theme_rgb_update_event.h"
|
||||
#include "theme_color.h"
|
||||
|
||||
LOG_MODULE_REGISTER(MODULE, LOG_LEVEL_INF);
|
||||
|
||||
#define LED_STRIP_NODE DT_CHOSEN(zephyr_led_strip)
|
||||
#define LED_STRIP_NUM_PIXELS DT_PROP(LED_STRIP_NODE, chain_length)
|
||||
#define LED_STRIP_TICK_INTERVAL K_MSEC(20)
|
||||
|
||||
BUILD_ASSERT(DT_NODE_HAS_STATUS(LED_STRIP_NODE, okay),
|
||||
"Missing zephyr,led-strip chosen node");
|
||||
BUILD_ASSERT(DT_NODE_HAS_PROP(LED_STRIP_NODE, supply_gpios),
|
||||
"Missing supply-gpios on zephyr,led-strip node");
|
||||
BUILD_ASSERT(LED_STRIP_NUM_PIXELS == 17U,
|
||||
"LED strip key map expects 17 pixels");
|
||||
|
||||
static const struct device *const strip = DEVICE_DT_GET(LED_STRIP_NODE);
|
||||
static const struct gpio_dt_spec strip_en =
|
||||
GPIO_DT_SPEC_GET(LED_STRIP_NODE, supply_gpios);
|
||||
|
||||
static const struct led_key_map led_key_map[] = {
|
||||
{ KEY_ID(0, 1), 0U },
|
||||
{ KEY_ID(1, 1), 1U },
|
||||
{ KEY_ID(2, 1), 2U },
|
||||
{ KEY_ID(3, 1), 3U },
|
||||
{ KEY_ID(0, 2), 4U },
|
||||
{ KEY_ID(1, 2), 5U },
|
||||
{ KEY_ID(2, 2), 6U },
|
||||
{ KEY_ID(0, 3), 7U },
|
||||
{ KEY_ID(1, 3), 8U },
|
||||
{ KEY_ID(2, 3), 9U },
|
||||
{ KEY_ID(3, 3), 10U },
|
||||
{ KEY_ID(0, 4), 11U },
|
||||
{ KEY_ID(1, 4), 12U },
|
||||
{ KEY_ID(2, 4), 13U },
|
||||
{ KEY_ID(0, 5), 14U },
|
||||
{ KEY_ID(1, 5), 15U },
|
||||
{ KEY_ID(3, 5), 16U },
|
||||
};
|
||||
|
||||
static const struct led_effect_config effect_cfg = {
|
||||
.key_map = led_key_map,
|
||||
.key_map_len = ARRAY_SIZE(led_key_map),
|
||||
.pixel_count = LED_STRIP_NUM_PIXELS,
|
||||
.default_brightness = 0xFFU,
|
||||
};
|
||||
|
||||
static struct led_rgb pixels[LED_STRIP_NUM_PIXELS];
|
||||
static struct k_work_delayable effect_work;
|
||||
static struct led_effect *effect;
|
||||
static struct theme_rgb current_theme = {
|
||||
.r = BLINKY_THEME_DEFAULT_R,
|
||||
.g = BLINKY_THEME_DEFAULT_G,
|
||||
.b = BLINKY_THEME_DEFAULT_B,
|
||||
};
|
||||
static bool initialized;
|
||||
static bool running;
|
||||
static bool enabled = true;
|
||||
|
||||
static void clear_pixels(void)
|
||||
{
|
||||
memset(pixels, 0, sizeof(pixels));
|
||||
}
|
||||
|
||||
static int submit_frame(void)
|
||||
{
|
||||
int err;
|
||||
|
||||
err = led_strip_update_rgb(strip, pixels, ARRAY_SIZE(pixels));
|
||||
if (err) {
|
||||
LOG_WRN("led_strip_update_rgb failed (%d)", err);
|
||||
}
|
||||
|
||||
return err;
|
||||
}
|
||||
|
||||
static int set_strip_power(bool on)
|
||||
{
|
||||
int err;
|
||||
|
||||
err = gpio_pin_set_dt(&strip_en, on ? 1 : 0);
|
||||
if (err) {
|
||||
LOG_WRN("LED strip EN set failed (%d)", err);
|
||||
}
|
||||
|
||||
return err;
|
||||
}
|
||||
|
||||
static int refresh_idle_frame(void)
|
||||
{
|
||||
clear_pixels();
|
||||
return submit_frame();
|
||||
}
|
||||
|
||||
static void stop_effect_work(void)
|
||||
{
|
||||
k_work_cancel_delayable(&effect_work);
|
||||
}
|
||||
|
||||
static void schedule_effect_tick(k_timeout_t delay)
|
||||
{
|
||||
k_work_reschedule(&effect_work, delay);
|
||||
}
|
||||
|
||||
static int enable_strip_output(void)
|
||||
{
|
||||
int err;
|
||||
|
||||
err = set_strip_power(true);
|
||||
if (err) {
|
||||
return err;
|
||||
}
|
||||
|
||||
return refresh_idle_frame();
|
||||
}
|
||||
|
||||
static void disable_strip_output(void)
|
||||
{
|
||||
stop_effect_work();
|
||||
clear_pixels();
|
||||
(void)submit_frame();
|
||||
(void)set_strip_power(false);
|
||||
}
|
||||
|
||||
static void effect_work_handler(struct k_work *work)
|
||||
{
|
||||
bool keep_running;
|
||||
|
||||
ARG_UNUSED(work);
|
||||
|
||||
if (!running || !enabled || (effect == NULL)) {
|
||||
return;
|
||||
}
|
||||
|
||||
keep_running = effect->ops->tick(effect, k_ticks_to_ms_floor32(
|
||||
LED_STRIP_TICK_INTERVAL.ticks),
|
||||
pixels, ARRAY_SIZE(pixels));
|
||||
(void)submit_frame();
|
||||
|
||||
if (keep_running) {
|
||||
schedule_effect_tick(LED_STRIP_TICK_INTERVAL);
|
||||
}
|
||||
}
|
||||
|
||||
static int module_init(void)
|
||||
{
|
||||
int err;
|
||||
|
||||
if (!device_is_ready(strip)) {
|
||||
LOG_ERR("LED strip device %s not ready", strip->name);
|
||||
return -ENODEV;
|
||||
}
|
||||
|
||||
if (!gpio_is_ready_dt(&strip_en)) {
|
||||
LOG_ERR("LED strip supply GPIO not ready");
|
||||
return -ENODEV;
|
||||
}
|
||||
|
||||
err = gpio_pin_configure_dt(&strip_en, GPIO_OUTPUT_INACTIVE);
|
||||
if (err) {
|
||||
LOG_ERR("Failed to configure LED strip supply GPIO (%d)", err);
|
||||
return err;
|
||||
}
|
||||
|
||||
k_work_init_delayable(&effect_work, effect_work_handler);
|
||||
clear_pixels();
|
||||
|
||||
effect = led_effect_get_mutable(LED_EFFECT_ID_KEY_FADE);
|
||||
if ((effect == NULL) || (effect->ops == NULL)) {
|
||||
LOG_ERR("LED effect not available");
|
||||
return -ENOENT;
|
||||
}
|
||||
|
||||
err = effect->ops->init(effect, &effect_cfg);
|
||||
if (err) {
|
||||
LOG_ERR("LED effect init failed (%d)", err);
|
||||
return err;
|
||||
}
|
||||
|
||||
effect->ops->set_theme(effect, ¤t_theme);
|
||||
|
||||
LOG_INF("LED strip ready len:%u", (uint32_t)led_strip_length(strip));
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int module_start(void)
|
||||
{
|
||||
int err;
|
||||
|
||||
if (running) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
running = true;
|
||||
|
||||
if (!enabled) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
err = enable_strip_output();
|
||||
if (err) {
|
||||
running = false;
|
||||
}
|
||||
|
||||
return err;
|
||||
}
|
||||
|
||||
static void module_pause(void)
|
||||
{
|
||||
if (!running) {
|
||||
return;
|
||||
}
|
||||
|
||||
if ((effect != NULL) && (effect->ops != NULL)) {
|
||||
effect->ops->reset(effect);
|
||||
}
|
||||
|
||||
disable_strip_output();
|
||||
running = false;
|
||||
}
|
||||
|
||||
static bool handle_button_event(const struct button_event *event)
|
||||
{
|
||||
if (!running || !enabled || (effect == NULL) || !event->pressed) {
|
||||
return false;
|
||||
}
|
||||
|
||||
effect->ops->on_key_press(effect, event->key_id);
|
||||
schedule_effect_tick(K_NO_WAIT);
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
static bool handle_led_strip_en_event(const struct led_strip_en_event *event)
|
||||
{
|
||||
enabled = event->enabled;
|
||||
|
||||
if (!running) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (enabled) {
|
||||
int err = enable_strip_output();
|
||||
|
||||
if (err) {
|
||||
module_set_state(MODULE_STATE_ERROR);
|
||||
return false;
|
||||
}
|
||||
} else {
|
||||
if ((effect != NULL) && (effect->ops != NULL)) {
|
||||
effect->ops->reset(effect);
|
||||
}
|
||||
|
||||
disable_strip_output();
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
static bool handle_theme_rgb_update_event(const struct theme_rgb_update_event *event)
|
||||
{
|
||||
current_theme = event->theme;
|
||||
|
||||
if ((effect != NULL) && (effect->ops != NULL)) {
|
||||
effect->ops->set_theme(effect, ¤t_theme);
|
||||
}
|
||||
|
||||
if (running && enabled && (effect != NULL) && effect->ops->is_active(effect)) {
|
||||
schedule_effect_tick(K_NO_WAIT);
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
static bool app_event_handler(const struct app_event_header *aeh)
|
||||
{
|
||||
if (is_button_event(aeh)) {
|
||||
return handle_button_event(cast_button_event(aeh));
|
||||
}
|
||||
|
||||
if (is_led_strip_en_event(aeh)) {
|
||||
return handle_led_strip_en_event(cast_led_strip_en_event(aeh));
|
||||
}
|
||||
|
||||
if (is_theme_rgb_update_event(aeh)) {
|
||||
return handle_theme_rgb_update_event(cast_theme_rgb_update_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_EARLY(MODULE, button_event);
|
||||
APP_EVENT_SUBSCRIBE(MODULE, led_strip_en_event);
|
||||
APP_EVENT_SUBSCRIBE(MODULE, module_state_event);
|
||||
APP_EVENT_SUBSCRIBE(MODULE, theme_rgb_update_event);
|
||||
APP_EVENT_SUBSCRIBE_EARLY(MODULE, power_down_event);
|
||||
APP_EVENT_SUBSCRIBE(MODULE, wake_up_event);
|
||||
@@ -2,9 +2,10 @@
|
||||
|
||||
#include <app_event_manager.h>
|
||||
|
||||
#define MODULE ble_adv_ctrl_module
|
||||
#define MODULE mode_policy_module
|
||||
#include <caf/events/module_state_event.h>
|
||||
#include <caf/events/module_suspend_event.h>
|
||||
#include <caf/events/power_event.h>
|
||||
|
||||
#include <zephyr/logging/log.h>
|
||||
|
||||
@@ -15,6 +16,7 @@ LOG_MODULE_REGISTER(MODULE, LOG_LEVEL_INF);
|
||||
static bool initialized;
|
||||
static bool running;
|
||||
static bool ble_adv_suspended = true;
|
||||
static bool usb_enabled;
|
||||
|
||||
static void broadcast_ble_adv_req(bool suspend)
|
||||
{
|
||||
@@ -33,10 +35,40 @@ static void broadcast_ble_adv_req(bool suspend)
|
||||
}
|
||||
}
|
||||
|
||||
static void apply_mode_policy(enum mode_switch_mode mode)
|
||||
{
|
||||
bool should_suspend_ble_adv = (mode != MODE_SWITCH_BLE);
|
||||
bool should_enable_usb = (mode == MODE_SWITCH_USB);
|
||||
|
||||
if (should_suspend_ble_adv != ble_adv_suspended) {
|
||||
ble_adv_suspended = should_suspend_ble_adv;
|
||||
broadcast_ble_adv_req(should_suspend_ble_adv);
|
||||
}
|
||||
|
||||
if (should_enable_usb != usb_enabled) {
|
||||
usb_enabled = should_enable_usb;
|
||||
if (usb_enabled) {
|
||||
struct module_resume_req_event *event =
|
||||
new_module_resume_req_event();
|
||||
|
||||
event->sink_module_id = MODULE_ID(usb_device_module);
|
||||
event->src_module_id = MODULE_ID(MODULE);
|
||||
APP_EVENT_SUBMIT(event);
|
||||
} else {
|
||||
struct module_suspend_req_event *event =
|
||||
new_module_suspend_req_event();
|
||||
|
||||
event->sink_module_id = MODULE_ID(usb_device_module);
|
||||
event->src_module_id = MODULE_ID(MODULE);
|
||||
APP_EVENT_SUBMIT(event);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static int module_init(void)
|
||||
{
|
||||
ble_adv_suspended = true;
|
||||
|
||||
usb_enabled = false;
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -47,7 +79,6 @@ static int module_start(void)
|
||||
}
|
||||
|
||||
running = true;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -56,28 +87,16 @@ static void module_pause(void)
|
||||
running = false;
|
||||
}
|
||||
|
||||
static bool handle_mode_switch_event(const struct mode_switch_event *event)
|
||||
{
|
||||
bool should_suspend;
|
||||
|
||||
if (!running) {
|
||||
return false;
|
||||
}
|
||||
|
||||
should_suspend = (event->mode != MODE_SWITCH_BLE);
|
||||
|
||||
if (should_suspend != ble_adv_suspended) {
|
||||
ble_adv_suspended = should_suspend;
|
||||
broadcast_ble_adv_req(should_suspend);
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
static bool app_event_handler(const struct app_event_header *aeh)
|
||||
{
|
||||
if (is_mode_switch_event(aeh)) {
|
||||
return handle_mode_switch_event(cast_mode_switch_event(aeh));
|
||||
const struct mode_switch_event *event = cast_mode_switch_event(aeh);
|
||||
|
||||
if (running) {
|
||||
apply_mode_policy(event->mode);
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
if (is_module_state_event(aeh)) {
|
||||
@@ -107,9 +126,34 @@ static bool app_event_handler(const struct app_event_header *aeh)
|
||||
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, mode_switch_event);
|
||||
APP_EVENT_SUBSCRIBE(MODULE, module_state_event);
|
||||
APP_EVENT_SUBSCRIBE_EARLY(MODULE, power_down_event);
|
||||
APP_EVENT_SUBSCRIBE(MODULE, wake_up_event);
|
||||
@@ -91,11 +91,7 @@ static void mode_switch_sample_fn(struct k_work *work)
|
||||
enum mode_switch_mode mode = detect_mode(sample_mv);
|
||||
|
||||
if (force_report || !mode_valid || (mode != last_mode)) {
|
||||
struct mode_switch_event *event = new_mode_switch_event();
|
||||
|
||||
event->mode = mode;
|
||||
event->voltage_mv = sample_mv;
|
||||
APP_EVENT_SUBMIT(event);
|
||||
submit_mode_switch_event(mode, sample_mv);
|
||||
|
||||
last_mode = mode;
|
||||
mode_valid = true;
|
||||
|
||||
@@ -2,36 +2,44 @@
|
||||
#include <stdbool.h>
|
||||
#include <stddef.h>
|
||||
#include <stdint.h>
|
||||
#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>
|
||||
|
||||
#include <zephyr/logging/log.h>
|
||||
#include <zephyr/sys/util.h>
|
||||
|
||||
#include <pb_decode.h>
|
||||
#include <pb_encode.h>
|
||||
|
||||
#include <proto/device_comm.pb.h>
|
||||
|
||||
#include "function_bitmap_state_event.h"
|
||||
#include "function_bitmap_update_event.h"
|
||||
#include "hid_led_event.h"
|
||||
#include "proto_rx_event.h"
|
||||
#include "proto_tx_event.h"
|
||||
#include "protocol_module.h"
|
||||
#include "theme_rgb_update_event.h"
|
||||
#include "time_sync_event.h"
|
||||
|
||||
LOG_MODULE_REGISTER(protocol_module, LOG_LEVEL_INF);
|
||||
LOG_MODULE_REGISTER(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
|
||||
#define PROTOCOL_CAPABILITY_FLAGS (BIT(0) | BIT(1) | BIT(2) | BIT(3) | BIT(4))
|
||||
#define PROTOCOL_MAX_MSG_LEN 128U
|
||||
|
||||
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 bool initialized;
|
||||
static bool running;
|
||||
static bool hello_done[PROTO_TRANSPORT_COUNT];
|
||||
|
||||
static int decode_body(const uint8_t *payload, size_t payload_len,
|
||||
CdcPacketBody *body)
|
||||
@@ -53,15 +61,29 @@ static int decode_body(const uint8_t *payload, size_t payload_len,
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int encode_body(const CdcPacketBody *body, uint8_t *payload,
|
||||
size_t payload_buf_size, size_t *payload_len)
|
||||
{
|
||||
pb_ostream_t stream;
|
||||
|
||||
if ((body == NULL) || (payload == NULL) || (payload_len == NULL)) {
|
||||
return -EINVAL;
|
||||
}
|
||||
|
||||
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;
|
||||
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;
|
||||
@@ -71,21 +93,106 @@ static int encode_hello_rsp(uint8_t *rsp_payload, size_t rsp_payload_buf_size,
|
||||
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);
|
||||
return encode_body(&body, rsp_payload, rsp_payload_buf_size, rsp_payload_len);
|
||||
}
|
||||
|
||||
if (!pb_encode(&stream, CdcPacketBody_fields, &body)) {
|
||||
LOG_WRN("pb_encode failed: %s", PB_GET_ERROR(&stream));
|
||||
return -EIO;
|
||||
static int encode_ack(uint8_t acked_type, uint8_t *rsp_payload,
|
||||
size_t rsp_payload_buf_size, size_t *rsp_payload_len)
|
||||
{
|
||||
CdcPacketBody body = CdcPacketBody_init_zero;
|
||||
|
||||
body.which_body = CdcPacketBody_ack_tag;
|
||||
body.body.ack.acked_type = acked_type;
|
||||
|
||||
return encode_body(&body, rsp_payload, rsp_payload_buf_size, rsp_payload_len);
|
||||
}
|
||||
|
||||
static int encode_error(uint8_t error_type, ErrorCode error_code,
|
||||
uint8_t *rsp_payload, size_t rsp_payload_buf_size,
|
||||
size_t *rsp_payload_len)
|
||||
{
|
||||
CdcPacketBody body = CdcPacketBody_init_zero;
|
||||
|
||||
body.which_body = CdcPacketBody_error_tag;
|
||||
body.body.error.error_type = error_type;
|
||||
body.body.error.error_code = error_code;
|
||||
|
||||
return encode_body(&body, rsp_payload, rsp_payload_buf_size, rsp_payload_len);
|
||||
}
|
||||
|
||||
static int encode_led_state(uint32_t led_mask, uint8_t *payload,
|
||||
size_t payload_buf_size, size_t *payload_len)
|
||||
{
|
||||
CdcPacketBody body = CdcPacketBody_init_zero;
|
||||
|
||||
body.which_body = CdcPacketBody_led_state_tag;
|
||||
body.body.led_state.led_mask = led_mask;
|
||||
|
||||
return encode_body(&body, payload, payload_buf_size, payload_len);
|
||||
}
|
||||
|
||||
static int encode_function_bitmap_state(const uint8_t *bitmap, uint8_t *payload,
|
||||
size_t payload_buf_size,
|
||||
size_t *payload_len)
|
||||
{
|
||||
CdcPacketBody body = CdcPacketBody_init_zero;
|
||||
|
||||
if (bitmap == NULL) {
|
||||
return -EINVAL;
|
||||
}
|
||||
|
||||
body.which_body = CdcPacketBody_function_key_event_tag;
|
||||
body.body.function_key_event.usage_bitmap.size = KEYBOARD_PROTOCOL_BITMAP_BYTES;
|
||||
memcpy(body.body.function_key_event.usage_bitmap.bytes, bitmap,
|
||||
KEYBOARD_PROTOCOL_BITMAP_BYTES);
|
||||
|
||||
return encode_body(&body, payload, payload_buf_size, payload_len);
|
||||
}
|
||||
|
||||
static int module_init(void)
|
||||
{
|
||||
for (size_t i = 0; i < ARRAY_SIZE(hello_done); i++) {
|
||||
hello_done[i] = false;
|
||||
}
|
||||
|
||||
*rsp_payload_len = stream.bytes_written;
|
||||
return 0;
|
||||
}
|
||||
|
||||
int protocol_module_process_cdc_packet(uint8_t req_type,
|
||||
static int module_start(void)
|
||||
{
|
||||
if (running) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
running = true;
|
||||
return 0;
|
||||
}
|
||||
|
||||
static void module_pause(void)
|
||||
{
|
||||
if (!running) {
|
||||
return;
|
||||
}
|
||||
|
||||
for (size_t i = 0; i < ARRAY_SIZE(hello_done); i++) {
|
||||
hello_done[i] = false;
|
||||
}
|
||||
|
||||
running = false;
|
||||
}
|
||||
|
||||
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_type,
|
||||
uint8_t *rsp_payload,
|
||||
size_t rsp_payload_buf_size,
|
||||
size_t *rsp_payload_len)
|
||||
@@ -93,41 +200,285 @@ int protocol_module_process_cdc_packet(uint8_t req_type,
|
||||
CdcPacketBody body;
|
||||
int err;
|
||||
|
||||
if ((rsp_type == NULL) || (rsp_payload == NULL) || (rsp_payload_len == NULL)) {
|
||||
if ((transport >= PROTO_TRANSPORT_COUNT) ||
|
||||
(rsp_payload == NULL) || (rsp_payload_len == NULL)) {
|
||||
return -EINVAL;
|
||||
}
|
||||
|
||||
if (!running) {
|
||||
return -EAGAIN;
|
||||
}
|
||||
|
||||
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);
|
||||
switch (body.which_body) {
|
||||
case CdcPacketBody_hello_req_tag:
|
||||
LOG_INF("HelloReq transport:%u protocol_version:%u",
|
||||
transport, 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;
|
||||
hello_done[transport] = true;
|
||||
return encode_hello_rsp(rsp_payload, rsp_payload_buf_size, rsp_payload_len);
|
||||
|
||||
case CdcPacketBody_bitmap_tag:
|
||||
if (!hello_done[transport]) {
|
||||
return encode_error(CdcPacketBody_bitmap_tag,
|
||||
ErrorCode_ERROR_CODE_NOT_READY,
|
||||
rsp_payload, rsp_payload_buf_size,
|
||||
rsp_payload_len);
|
||||
}
|
||||
|
||||
*rsp_type = CDC_PROTO_TYPE_HELLO_RSP;
|
||||
return 0;
|
||||
if (body.body.bitmap.usage_bitmap.size != KEYBOARD_PROTOCOL_BITMAP_BYTES) {
|
||||
return encode_error(CdcPacketBody_bitmap_tag,
|
||||
ErrorCode_ERROR_CODE_INVALID_LENGTH,
|
||||
rsp_payload, rsp_payload_buf_size,
|
||||
rsp_payload_len);
|
||||
}
|
||||
|
||||
err = submit_function_bitmap_update_event(
|
||||
body.body.bitmap.usage_bitmap.bytes);
|
||||
if (err) {
|
||||
return encode_error(CdcPacketBody_bitmap_tag,
|
||||
ErrorCode_ERROR_CODE_INVALID_PARAM,
|
||||
rsp_payload, rsp_payload_buf_size,
|
||||
rsp_payload_len);
|
||||
}
|
||||
|
||||
return encode_ack(CdcPacketBody_bitmap_tag, rsp_payload,
|
||||
rsp_payload_buf_size, rsp_payload_len);
|
||||
|
||||
case CdcPacketBody_time_sync_tag:
|
||||
if (!hello_done[transport]) {
|
||||
return encode_error(CdcPacketBody_time_sync_tag,
|
||||
ErrorCode_ERROR_CODE_NOT_READY,
|
||||
rsp_payload, rsp_payload_buf_size,
|
||||
rsp_payload_len);
|
||||
}
|
||||
|
||||
if (body.body.time_sync.version != 1U) {
|
||||
return encode_error(CdcPacketBody_time_sync_tag,
|
||||
ErrorCode_ERROR_CODE_INVALID_PARAM,
|
||||
rsp_payload, rsp_payload_buf_size,
|
||||
rsp_payload_len);
|
||||
}
|
||||
|
||||
submit_time_sync_event(body.body.time_sync.version,
|
||||
body.body.time_sync.flags,
|
||||
body.body.time_sync.timezone_min,
|
||||
body.body.time_sync.utc_ms,
|
||||
body.body.time_sync.accuracy_ms);
|
||||
return encode_ack(CdcPacketBody_time_sync_tag, rsp_payload,
|
||||
rsp_payload_buf_size, rsp_payload_len);
|
||||
|
||||
case CdcPacketBody_theme_rgb_tag:
|
||||
if (!hello_done[transport]) {
|
||||
return encode_error(CdcPacketBody_theme_rgb_tag,
|
||||
ErrorCode_ERROR_CODE_NOT_READY,
|
||||
rsp_payload, rsp_payload_buf_size,
|
||||
rsp_payload_len);
|
||||
}
|
||||
|
||||
if ((body.body.theme_rgb.red > 255U) ||
|
||||
(body.body.theme_rgb.green > 255U) ||
|
||||
(body.body.theme_rgb.blue > 255U)) {
|
||||
return encode_error(CdcPacketBody_theme_rgb_tag,
|
||||
ErrorCode_ERROR_CODE_INVALID_PARAM,
|
||||
rsp_payload, rsp_payload_buf_size,
|
||||
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(CdcPacketBody_theme_rgb_tag, rsp_payload,
|
||||
rsp_payload_buf_size, rsp_payload_len);
|
||||
|
||||
default:
|
||||
LOG_WRN("Unsupported CDC protocol type:0x%02x", req_type);
|
||||
LOG_WRN("Unsupported protobuf body case %d", body.which_body);
|
||||
return -ENOTSUP;
|
||||
}
|
||||
}
|
||||
|
||||
static bool handle_proto_rx_event(const struct proto_rx_event *event)
|
||||
{
|
||||
uint8_t rsp_payload[PROTOCOL_MAX_MSG_LEN];
|
||||
size_t rsp_payload_len = 0U;
|
||||
int err;
|
||||
|
||||
if (!running) {
|
||||
return false;
|
||||
}
|
||||
|
||||
err = protocol_module_process_message(event->transport,
|
||||
event->dyndata.data,
|
||||
event->dyndata.size,
|
||||
rsp_payload,
|
||||
sizeof(rsp_payload),
|
||||
&rsp_payload_len);
|
||||
if (err) {
|
||||
if (err != -ENOTSUP) {
|
||||
LOG_WRN("Protocol processing failed (%d)", err);
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
err = submit_proto_tx_event(event->transport, rsp_payload, rsp_payload_len);
|
||||
if (err) {
|
||||
LOG_WRN("Proto TX submit failed (%d)", err);
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
static bool handle_function_bitmap_state_event(
|
||||
const struct function_bitmap_state_event *event)
|
||||
{
|
||||
uint8_t payload[PROTOCOL_MAX_MSG_LEN];
|
||||
size_t payload_len;
|
||||
int err;
|
||||
|
||||
if (!running) {
|
||||
return false;
|
||||
}
|
||||
|
||||
for (enum proto_transport transport = 0; transport < PROTO_TRANSPORT_COUNT;
|
||||
transport++) {
|
||||
if (!hello_done[transport]) {
|
||||
continue;
|
||||
}
|
||||
|
||||
err = encode_function_bitmap_state(event->bitmap, payload,
|
||||
sizeof(payload), &payload_len);
|
||||
if (err) {
|
||||
LOG_WRN("FunctionKeyEvent encode failed (%d)", err);
|
||||
return false;
|
||||
}
|
||||
|
||||
err = submit_proto_tx_event(transport, payload, payload_len);
|
||||
if (err) {
|
||||
LOG_WRN("FunctionKeyEvent submit failed (%d)", err);
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
static bool handle_hid_led_event(const struct hid_led_event *event)
|
||||
{
|
||||
uint8_t payload[PROTOCOL_MAX_MSG_LEN];
|
||||
size_t payload_len;
|
||||
int err;
|
||||
enum proto_transport transport;
|
||||
|
||||
if (!running) {
|
||||
return false;
|
||||
}
|
||||
|
||||
transport = (event->transport == HID_TRANSPORT_USB) ?
|
||||
PROTO_TRANSPORT_USB_CDC :
|
||||
PROTO_TRANSPORT_BLE_NUS;
|
||||
|
||||
if (!hello_done[transport]) {
|
||||
return false;
|
||||
}
|
||||
|
||||
err = encode_led_state(event->led_bm, payload, sizeof(payload), &payload_len);
|
||||
if (err) {
|
||||
LOG_WRN("LedState encode failed (%d)", err);
|
||||
return false;
|
||||
}
|
||||
|
||||
err = submit_proto_tx_event(transport, payload, payload_len);
|
||||
if (err) {
|
||||
LOG_WRN("LedState submit failed (%d)", err);
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
static bool app_event_handler(const struct app_event_header *aeh)
|
||||
{
|
||||
if (is_proto_rx_event(aeh)) {
|
||||
return handle_proto_rx_event(cast_proto_rx_event(aeh));
|
||||
}
|
||||
|
||||
if (is_function_bitmap_state_event(aeh)) {
|
||||
return handle_function_bitmap_state_event(
|
||||
cast_function_bitmap_state_event(aeh));
|
||||
}
|
||||
|
||||
if (is_hid_led_event(aeh)) {
|
||||
return handle_hid_led_event(cast_hid_led_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);
|
||||
} else {
|
||||
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 {
|
||||
module_set_state(MODULE_STATE_READY);
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
APP_EVENT_LISTENER(MODULE, app_event_handler);
|
||||
APP_EVENT_SUBSCRIBE(MODULE, function_bitmap_state_event);
|
||||
APP_EVENT_SUBSCRIBE(MODULE, hid_led_event);
|
||||
APP_EVENT_SUBSCRIBE(MODULE, module_state_event);
|
||||
APP_EVENT_SUBSCRIBE(MODULE, proto_rx_event);
|
||||
APP_EVENT_SUBSCRIBE_EARLY(MODULE, power_down_event);
|
||||
APP_EVENT_SUBSCRIBE(MODULE, wake_up_event);
|
||||
|
||||
189
src/time_sync_module.c
Normal file
189
src/time_sync_module.c
Normal file
@@ -0,0 +1,189 @@
|
||||
#include <stdbool.h>
|
||||
#include <string.h>
|
||||
#include <time.h>
|
||||
|
||||
#include <app_event_manager.h>
|
||||
|
||||
#define MODULE time_sync_module
|
||||
#include <caf/events/module_state_event.h>
|
||||
#include <caf/events/power_event.h>
|
||||
|
||||
#include <zephyr/kernel.h>
|
||||
#include <zephyr/logging/log.h>
|
||||
#include <zephyr/sys/printk.h>
|
||||
|
||||
#include "datetime_event.h"
|
||||
#include "time_sync_event.h"
|
||||
|
||||
LOG_MODULE_REGISTER(MODULE, LOG_LEVEL_INF);
|
||||
|
||||
#define TIME_SYNC_REFRESH_PERIOD K_SECONDS(1)
|
||||
|
||||
static struct k_work_delayable refresh_work;
|
||||
static bool initialized;
|
||||
static bool running;
|
||||
static int32_t timezone_min;
|
||||
static uint64_t utc_ms_base;
|
||||
static int64_t uptime_ms_base;
|
||||
|
||||
static void publish_datetime_event(void)
|
||||
{
|
||||
struct tm tm_buf;
|
||||
time_t seconds;
|
||||
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 +
|
||||
(k_uptime_get() - uptime_ms_base) +
|
||||
((int64_t)timezone_min * 60LL * 1000LL);
|
||||
if (local_ms < 0) {
|
||||
local_ms = 0;
|
||||
}
|
||||
|
||||
seconds = (time_t)(local_ms / 1000LL);
|
||||
if (gmtime_r(&seconds, &tm_buf) == NULL) {
|
||||
strncpy(date_text, "1970/01/01", sizeof(date_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 {
|
||||
snprintk(date_text, sizeof(date_text),
|
||||
"%04u/%02u/%02u",
|
||||
(unsigned int)(tm_buf.tm_year + 1900),
|
||||
(unsigned int)(tm_buf.tm_mon + 1),
|
||||
(unsigned int)tm_buf.tm_mday);
|
||||
snprintk(time_text, sizeof(time_text),
|
||||
"%02u:%02u:%02u",
|
||||
(unsigned int)tm_buf.tm_hour,
|
||||
(unsigned int)tm_buf.tm_min,
|
||||
(unsigned int)tm_buf.tm_sec);
|
||||
}
|
||||
|
||||
(void)submit_datetime_event(date_text, time_text);
|
||||
}
|
||||
|
||||
static void refresh_work_handler(struct k_work *work)
|
||||
{
|
||||
ARG_UNUSED(work);
|
||||
|
||||
if (!running) {
|
||||
return;
|
||||
}
|
||||
|
||||
publish_datetime_event();
|
||||
k_work_reschedule(&refresh_work, TIME_SYNC_REFRESH_PERIOD);
|
||||
}
|
||||
|
||||
static int module_init(void)
|
||||
{
|
||||
timezone_min = 0;
|
||||
utc_ms_base = 0U;
|
||||
uptime_ms_base = k_uptime_get();
|
||||
k_work_init_delayable(&refresh_work, refresh_work_handler);
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int module_start(void)
|
||||
{
|
||||
if (running) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
running = true;
|
||||
k_work_reschedule(&refresh_work, K_NO_WAIT);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static void module_pause(void)
|
||||
{
|
||||
if (!running) {
|
||||
return;
|
||||
}
|
||||
|
||||
k_work_cancel_delayable(&refresh_work);
|
||||
running = false;
|
||||
}
|
||||
|
||||
static bool handle_time_sync_event(const struct time_sync_event *event)
|
||||
{
|
||||
if (event->version != 1U) {
|
||||
LOG_WRN("Unexpected TimeSync version:%u", event->version);
|
||||
return false;
|
||||
}
|
||||
|
||||
timezone_min = event->timezone_min;
|
||||
utc_ms_base = event->utc_ms;
|
||||
uptime_ms_base = k_uptime_get();
|
||||
|
||||
if (running) {
|
||||
k_work_reschedule(&refresh_work, K_NO_WAIT);
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
static bool app_event_handler(const struct app_event_header *aeh)
|
||||
{
|
||||
if (is_time_sync_event(aeh)) {
|
||||
return handle_time_sync_event(cast_time_sync_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);
|
||||
} 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, time_sync_event);
|
||||
APP_EVENT_SUBSCRIBE_EARLY(MODULE, power_down_event);
|
||||
APP_EVENT_SUBSCRIBE(MODULE, wake_up_event);
|
||||
@@ -207,8 +207,6 @@ void ui_main_init(const struct ui_main_model *model,
|
||||
|
||||
lv_obj_clean(screen);
|
||||
lv_obj_set_style_bg_color(screen, lv_color_hex(0x0F1115), 0);
|
||||
lv_obj_set_style_bg_grad_color(screen, lv_color_hex(0x1A1F29), 0);
|
||||
lv_obj_set_style_bg_grad_dir(screen, LV_GRAD_DIR_VER, 0);
|
||||
lv_obj_set_style_bg_opa(screen, LV_OPA_COVER, 0);
|
||||
lv_obj_set_style_text_color(screen, lv_color_white(), 0);
|
||||
lv_obj_set_style_pad_all(screen, 0, 0);
|
||||
|
||||
@@ -16,19 +16,19 @@
|
||||
#include <zephyr/sys/ring_buffer.h>
|
||||
#include <zephyr/sys/util.h>
|
||||
|
||||
#include "usb_cdc_rx_event.h"
|
||||
#include "usb_cdc_tx_event.h"
|
||||
#include "usb_function_ready_event.h"
|
||||
#include "usb_prepare_event.h"
|
||||
#include "usb_device_module.h"
|
||||
#include "usb_device_state_event.h"
|
||||
#include "proto_rx_event.h"
|
||||
#include "proto_tx_event.h"
|
||||
#include "protocol_module.h"
|
||||
#include "usb_state_event.h"
|
||||
|
||||
LOG_MODULE_REGISTER(MODULE, LOG_LEVEL_INF);
|
||||
|
||||
#define USB_CDC_RX_RING_BUF_SIZE 256
|
||||
#define USB_CDC_TX_RING_BUF_SIZE 256
|
||||
#define USB_CDC_RX_CHUNK_SIZE 32
|
||||
#define USB_CDC_PROTO_RX_BUF_SIZE 128
|
||||
#define USB_CDC_CONTROL_POLL_INTERVAL K_MSEC(100)
|
||||
#define USB_CDC_PROTO_RX_FLUSH_DELAY K_MSEC(3)
|
||||
#define USB_CDC_EXPECTED_BAUDRATE 115200U
|
||||
|
||||
static const struct device *const cdc_dev = DEVICE_DT_GET_ONE(zephyr_cdc_acm_uart);
|
||||
@@ -39,28 +39,14 @@ static struct ring_buf rx_ringbuf;
|
||||
static struct ring_buf tx_ringbuf;
|
||||
static struct k_work rx_work;
|
||||
static struct k_work_delayable control_work;
|
||||
static struct k_work_delayable rx_flush_work;
|
||||
static bool initialized;
|
||||
static bool running;
|
||||
static bool usb_active;
|
||||
static bool usb_function_prepared;
|
||||
static bool dtr_ready;
|
||||
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 uint8_t proto_rx_buf[USB_CDC_PROTO_RX_BUF_SIZE];
|
||||
static size_t proto_rx_len;
|
||||
|
||||
static void reset_ring_buffers(void)
|
||||
{
|
||||
@@ -78,6 +64,8 @@ static void disable_uart_io(void)
|
||||
uart_irq_tx_disable(cdc_dev);
|
||||
rx_enabled = false;
|
||||
dtr_ready = false;
|
||||
proto_rx_len = 0U;
|
||||
k_work_cancel_delayable(&rx_flush_work);
|
||||
reset_ring_buffers();
|
||||
}
|
||||
|
||||
@@ -145,8 +133,30 @@ static void rx_work_handler(struct k_work *work)
|
||||
return;
|
||||
}
|
||||
|
||||
submit_usb_cdc_rx_event(buffer, len);
|
||||
if ((proto_rx_len + len) > sizeof(proto_rx_buf)) {
|
||||
LOG_WRN("Drop oversized CDC protobuf message len:%u",
|
||||
(uint32_t)(proto_rx_len + len));
|
||||
proto_rx_len = 0U;
|
||||
}
|
||||
|
||||
if (len > 0U) {
|
||||
memcpy(&proto_rx_buf[proto_rx_len], buffer, len);
|
||||
proto_rx_len += len;
|
||||
k_work_reschedule(&rx_flush_work, USB_CDC_PROTO_RX_FLUSH_DELAY);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static void rx_flush_work_handler(struct k_work *work)
|
||||
{
|
||||
ARG_UNUSED(work);
|
||||
|
||||
if (!running || !usb_active || !dtr_ready || (proto_rx_len == 0U)) {
|
||||
return;
|
||||
}
|
||||
|
||||
(void)submit_proto_rx_event(PROTO_TRANSPORT_USB_CDC, proto_rx_buf, proto_rx_len);
|
||||
proto_rx_len = 0U;
|
||||
}
|
||||
|
||||
static void control_work_handler(struct k_work *work)
|
||||
@@ -262,8 +272,9 @@ static int module_init(void)
|
||||
reset_ring_buffers();
|
||||
k_work_init(&rx_work, rx_work_handler);
|
||||
k_work_init_delayable(&control_work, control_work_handler);
|
||||
k_work_init_delayable(&rx_flush_work, rx_flush_work_handler);
|
||||
uart_irq_callback_set(cdc_dev, cdc_interrupt_handler);
|
||||
usb_function_prepared = false;
|
||||
proto_rx_len = 0U;
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -288,26 +299,14 @@ static void module_pause(void)
|
||||
}
|
||||
|
||||
k_work_cancel_delayable(&control_work);
|
||||
k_work_cancel_delayable(&rx_flush_work);
|
||||
disable_uart_io();
|
||||
running = false;
|
||||
}
|
||||
|
||||
static bool handle_usb_prepare_event(const struct usb_prepare_event *event)
|
||||
static bool handle_usb_state_event(const struct usb_state_event *event)
|
||||
{
|
||||
ARG_UNUSED(event);
|
||||
|
||||
if (!running || usb_function_prepared) {
|
||||
return false;
|
||||
}
|
||||
|
||||
usb_function_prepared = true;
|
||||
submit_usb_function_ready_event();
|
||||
return 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);
|
||||
bool new_usb_active = (event->state == USB_STATE_ACTIVE);
|
||||
|
||||
if (new_usb_active == usb_active) {
|
||||
return false;
|
||||
@@ -317,6 +316,8 @@ static bool handle_usb_device_state_event(const struct usb_device_state_event *e
|
||||
|
||||
if (!usb_active) {
|
||||
k_work_cancel_delayable(&control_work);
|
||||
k_work_cancel_delayable(&rx_flush_work);
|
||||
protocol_module_reset_transport_state(PROTO_TRANSPORT_USB_CDC);
|
||||
disable_uart_io();
|
||||
} else if (running) {
|
||||
k_work_reschedule(&control_work, K_NO_WAIT);
|
||||
@@ -325,11 +326,15 @@ static bool handle_usb_device_state_event(const struct usb_device_state_event *e
|
||||
return false;
|
||||
}
|
||||
|
||||
static bool handle_usb_cdc_tx_event(const struct usb_cdc_tx_event *event)
|
||||
static bool handle_proto_tx_event(const struct proto_tx_event *event)
|
||||
{
|
||||
uint32_t written;
|
||||
unsigned int key;
|
||||
|
||||
if (event->transport != PROTO_TRANSPORT_USB_CDC) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!running || !usb_active || !dtr_ready) {
|
||||
return false;
|
||||
}
|
||||
@@ -352,16 +357,12 @@ static bool handle_usb_cdc_tx_event(const struct usb_cdc_tx_event *event)
|
||||
|
||||
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_usb_state_event(aeh)) {
|
||||
return handle_usb_state_event(cast_usb_state_event(aeh));
|
||||
}
|
||||
|
||||
if (is_usb_prepare_event(aeh)) {
|
||||
return handle_usb_prepare_event(cast_usb_prepare_event(aeh));
|
||||
}
|
||||
|
||||
if (is_usb_cdc_tx_event(aeh)) {
|
||||
return handle_usb_cdc_tx_event(cast_usb_cdc_tx_event(aeh));
|
||||
if (is_proto_tx_event(aeh)) {
|
||||
return handle_proto_tx_event(cast_proto_tx_event(aeh));
|
||||
}
|
||||
|
||||
if (is_module_state_event(aeh)) {
|
||||
@@ -419,8 +420,7 @@ static bool app_event_handler(const struct app_event_header *aeh)
|
||||
|
||||
APP_EVENT_LISTENER(MODULE, app_event_handler);
|
||||
APP_EVENT_SUBSCRIBE(MODULE, module_state_event);
|
||||
APP_EVENT_SUBSCRIBE(MODULE, usb_prepare_event);
|
||||
APP_EVENT_SUBSCRIBE(MODULE, usb_device_state_event);
|
||||
APP_EVENT_SUBSCRIBE(MODULE, usb_cdc_tx_event);
|
||||
APP_EVENT_SUBSCRIBE(MODULE, proto_tx_event);
|
||||
APP_EVENT_SUBSCRIBE(MODULE, usb_state_event);
|
||||
APP_EVENT_SUBSCRIBE_EARLY(MODULE, power_down_event);
|
||||
APP_EVENT_SUBSCRIBE(MODULE, wake_up_event);
|
||||
|
||||
@@ -1,164 +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 <zephyr/kernel.h>
|
||||
#include <zephyr/logging/log.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 struct k_work_delayable hello_work;
|
||||
static bool initialized;
|
||||
static bool running;
|
||||
static bool usb_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 hello_work_handler(struct k_work *work)
|
||||
{
|
||||
ARG_UNUSED(work);
|
||||
|
||||
if (!running || !usb_active) {
|
||||
return;
|
||||
}
|
||||
|
||||
submit_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) {
|
||||
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 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_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_device_state_event);
|
||||
APP_EVENT_SUBSCRIBE_EARLY(MODULE, power_down_event);
|
||||
APP_EVENT_SUBSCRIBE(MODULE, wake_up_event);
|
||||
@@ -6,6 +6,7 @@
|
||||
|
||||
#define MODULE usb_device_module
|
||||
#include <caf/events/module_state_event.h>
|
||||
#include <caf/events/module_suspend_event.h>
|
||||
#include <caf/events/power_event.h>
|
||||
|
||||
#include <zephyr/logging/log.h>
|
||||
@@ -13,10 +14,8 @@
|
||||
|
||||
#include <caf/events/power_manager_event.h>
|
||||
|
||||
#include "usb_device_module.h"
|
||||
#include "usb_device_state_event.h"
|
||||
#include "usb_function_ready_event.h"
|
||||
#include "usb_prepare_event.h"
|
||||
#include "usb_function_hook.h"
|
||||
#include "usb_state_event.h"
|
||||
|
||||
LOG_MODULE_REGISTER(MODULE, LOG_LEVEL_INF);
|
||||
|
||||
@@ -24,7 +23,6 @@ LOG_MODULE_REGISTER(MODULE, LOG_LEVEL_INF);
|
||||
#define USB_DEVICE_PID 0x52F0
|
||||
#define USB_DEVICE_MANUFACTURER "Atguigu"
|
||||
#define USB_DEVICE_PRODUCT "WH Mini Keyboard"
|
||||
#define USB_REQUIRED_FUNCTION_MASK (USB_FUNCTION_HID | USB_FUNCTION_CDC_ACM)
|
||||
|
||||
USBD_DEVICE_DEFINE(blinky_usbd, DEVICE_DT_GET(DT_NODELABEL(usbd)),
|
||||
USB_DEVICE_VID, USB_DEVICE_PID);
|
||||
@@ -42,35 +40,65 @@ static const char *const class_blocklist[] = {
|
||||
|
||||
static bool initialized;
|
||||
static bool running;
|
||||
static bool prepare_broadcasted;
|
||||
static bool usbd_initialized;
|
||||
static bool usb_enabled;
|
||||
static uint8_t ready_function_mask;
|
||||
static enum usb_device_state device_state = USB_DEVICE_STATE_DISCONNECTED;
|
||||
|
||||
static void submit_usb_device_state_event(enum usb_device_state state)
|
||||
enum usb_stack_state {
|
||||
USB_STACK_UNINITIALIZED = 0,
|
||||
USB_STACK_DISABLED,
|
||||
USB_STACK_READY,
|
||||
USB_STACK_ENABLED,
|
||||
};
|
||||
|
||||
enum usb_bus_state {
|
||||
USB_BUS_DISCONNECTED = 0,
|
||||
USB_BUS_POWERED,
|
||||
USB_BUS_ACTIVE,
|
||||
USB_BUS_SUSPENDED,
|
||||
};
|
||||
|
||||
struct usb_owner_ctx {
|
||||
enum usb_stack_state stack;
|
||||
enum usb_bus_state bus;
|
||||
};
|
||||
|
||||
static struct usb_owner_ctx usb_ctx = {
|
||||
.stack = USB_STACK_UNINITIALIZED,
|
||||
.bus = USB_BUS_DISCONNECTED,
|
||||
};
|
||||
|
||||
static inline enum usb_state usb_public_state_get(void)
|
||||
{
|
||||
struct usb_device_state_event *event = new_usb_device_state_event();
|
||||
if ((usb_ctx.stack == USB_STACK_UNINITIALIZED) ||
|
||||
(usb_ctx.stack == USB_STACK_DISABLED)) {
|
||||
return USB_STATE_DISABLED;
|
||||
}
|
||||
|
||||
event->state = state;
|
||||
APP_EVENT_SUBMIT(event);
|
||||
switch (usb_ctx.bus) {
|
||||
case USB_BUS_DISCONNECTED:
|
||||
return USB_STATE_DISCONNECTED;
|
||||
case USB_BUS_POWERED:
|
||||
return USB_STATE_POWERED;
|
||||
case USB_BUS_ACTIVE:
|
||||
return USB_STATE_ACTIVE;
|
||||
case USB_BUS_SUSPENDED:
|
||||
return USB_STATE_SUSPENDED;
|
||||
default:
|
||||
return USB_STATE_DISABLED;
|
||||
}
|
||||
}
|
||||
|
||||
static void submit_usb_prepare_event(void)
|
||||
static inline bool usb_stack_was_initialized(void)
|
||||
{
|
||||
struct usb_prepare_event *event = new_usb_prepare_event();
|
||||
|
||||
APP_EVENT_SUBMIT(event);
|
||||
return (usb_ctx.stack != USB_STACK_UNINITIALIZED);
|
||||
}
|
||||
|
||||
static void update_usb_device_state(enum usb_device_state state)
|
||||
static inline void usb_bus_set(enum usb_bus_state state)
|
||||
{
|
||||
if (device_state == state) {
|
||||
if (usb_ctx.bus == state) {
|
||||
return;
|
||||
}
|
||||
|
||||
device_state = state;
|
||||
submit_usb_device_state_event(state);
|
||||
usb_ctx.bus = state;
|
||||
submit_usb_state(usb_public_state_get());
|
||||
}
|
||||
|
||||
static void update_power_manager_restriction(bool vbus_present)
|
||||
@@ -122,21 +150,23 @@ static void usbd_msg_cb(struct usbd_context *const usbd_ctx,
|
||||
switch (msg->type) {
|
||||
case USBD_MSG_VBUS_READY:
|
||||
update_power_manager_restriction(true);
|
||||
if (!usb_enabled) {
|
||||
|
||||
if (usb_ctx.stack == USB_STACK_READY) {
|
||||
int err = usbd_enable(&blinky_usbd);
|
||||
|
||||
if (err) {
|
||||
LOG_ERR("usbd_enable failed (%d)", err);
|
||||
} else {
|
||||
usb_enabled = true;
|
||||
update_usb_device_state(USB_DEVICE_STATE_POWERED);
|
||||
usb_ctx.stack = USB_STACK_ENABLED;
|
||||
usb_bus_set(USB_BUS_POWERED);
|
||||
}
|
||||
}
|
||||
break;
|
||||
|
||||
case USBD_MSG_VBUS_REMOVED:
|
||||
update_power_manager_restriction(false);
|
||||
if (usb_enabled) {
|
||||
|
||||
if (usb_ctx.stack == USB_STACK_ENABLED) {
|
||||
int err = usbd_disable(&blinky_usbd);
|
||||
|
||||
if (err) {
|
||||
@@ -144,27 +174,30 @@ static void usbd_msg_cb(struct usbd_context *const usbd_ctx,
|
||||
}
|
||||
}
|
||||
|
||||
usb_enabled = false;
|
||||
update_usb_device_state(USB_DEVICE_STATE_DISCONNECTED);
|
||||
if (usb_ctx.stack != USB_STACK_DISABLED) {
|
||||
usb_ctx.stack = USB_STACK_READY;
|
||||
}
|
||||
|
||||
usb_bus_set(USB_BUS_DISCONNECTED);
|
||||
break;
|
||||
|
||||
case USBD_MSG_CONFIGURATION:
|
||||
if (msg->status) {
|
||||
update_usb_device_state(USB_DEVICE_STATE_ACTIVE);
|
||||
} else if (usb_enabled) {
|
||||
update_usb_device_state(USB_DEVICE_STATE_POWERED);
|
||||
usb_bus_set(USB_BUS_ACTIVE);
|
||||
} else if (usb_ctx.stack == USB_STACK_ENABLED) {
|
||||
usb_bus_set(USB_BUS_POWERED);
|
||||
}
|
||||
break;
|
||||
|
||||
case USBD_MSG_SUSPEND:
|
||||
if (usb_enabled) {
|
||||
update_usb_device_state(USB_DEVICE_STATE_SUSPENDED);
|
||||
if (usb_ctx.stack == USB_STACK_ENABLED) {
|
||||
usb_bus_set(USB_BUS_SUSPENDED);
|
||||
}
|
||||
break;
|
||||
|
||||
case USBD_MSG_RESUME:
|
||||
if (usb_enabled) {
|
||||
update_usb_device_state(USB_DEVICE_STATE_ACTIVE);
|
||||
if (usb_ctx.stack == USB_STACK_ENABLED) {
|
||||
usb_bus_set(USB_BUS_ACTIVE);
|
||||
}
|
||||
break;
|
||||
|
||||
@@ -173,10 +206,29 @@ static void usbd_msg_cb(struct usbd_context *const usbd_ctx,
|
||||
}
|
||||
}
|
||||
|
||||
static int usb_stack_init(void)
|
||||
static int usb_stack_init_once(void)
|
||||
{
|
||||
int err;
|
||||
|
||||
if (usb_stack_was_initialized()) {
|
||||
if (usb_ctx.stack == USB_STACK_DISABLED) {
|
||||
usb_ctx.stack = USB_STACK_READY;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
STRUCT_SECTION_FOREACH(usb_function_hook, hook) {
|
||||
if (hook->pre_stack_init == NULL) {
|
||||
continue;
|
||||
}
|
||||
|
||||
err = hook->pre_stack_init();
|
||||
if (err) {
|
||||
LOG_ERR("USB function hook %s failed (%d)", hook->name, err);
|
||||
return err;
|
||||
}
|
||||
}
|
||||
|
||||
err = usbd_msg_register_cb(&blinky_usbd, usbd_msg_cb);
|
||||
if (err) {
|
||||
LOG_ERR("usbd_msg_register_cb failed (%d)", err);
|
||||
@@ -195,7 +247,7 @@ static int usb_stack_init(void)
|
||||
return err;
|
||||
}
|
||||
|
||||
usbd_initialized = true;
|
||||
usb_ctx.stack = USB_STACK_READY;
|
||||
|
||||
if (!usbd_can_detect_vbus(&blinky_usbd)) {
|
||||
err = usbd_enable(&blinky_usbd);
|
||||
@@ -204,20 +256,67 @@ static int usb_stack_init(void)
|
||||
return err;
|
||||
}
|
||||
|
||||
usb_enabled = true;
|
||||
update_usb_device_state(USB_DEVICE_STATE_POWERED);
|
||||
usb_ctx.stack = USB_STACK_ENABLED;
|
||||
usb_bus_set(USB_BUS_POWERED);
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int usb_stack_enable_if_needed(void)
|
||||
{
|
||||
int err;
|
||||
|
||||
err = usb_stack_init_once();
|
||||
if (err) {
|
||||
return err;
|
||||
}
|
||||
|
||||
if ((usb_ctx.stack == USB_STACK_READY) &&
|
||||
(!usbd_can_detect_vbus(&blinky_usbd) ||
|
||||
(usb_ctx.bus != USB_BUS_DISCONNECTED))) {
|
||||
err = usbd_enable(&blinky_usbd);
|
||||
if (err) {
|
||||
LOG_ERR("usbd_enable failed (%d)", err);
|
||||
return err;
|
||||
}
|
||||
|
||||
usb_ctx.stack = USB_STACK_ENABLED;
|
||||
|
||||
if (usb_ctx.bus == USB_BUS_DISCONNECTED) {
|
||||
usb_bus_set(USB_BUS_POWERED);
|
||||
} else {
|
||||
submit_usb_state(usb_public_state_get());
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
submit_usb_state(usb_public_state_get());
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int usb_stack_disable_if_needed(void)
|
||||
{
|
||||
if (usb_ctx.stack == USB_STACK_ENABLED) {
|
||||
int err = usbd_disable(&blinky_usbd);
|
||||
|
||||
if (err) {
|
||||
LOG_ERR("usbd_disable failed (%d)", err);
|
||||
return err;
|
||||
}
|
||||
}
|
||||
|
||||
usb_ctx.stack = USB_STACK_DISABLED;
|
||||
update_power_manager_restriction(false);
|
||||
submit_usb_state(usb_public_state_get());
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int module_init(void)
|
||||
{
|
||||
device_state = USB_DEVICE_STATE_DISCONNECTED;
|
||||
usb_enabled = false;
|
||||
ready_function_mask = 0U;
|
||||
prepare_broadcasted = false;
|
||||
usbd_initialized = false;
|
||||
usb_ctx.stack = USB_STACK_UNINITIALIZED;
|
||||
usb_ctx.bus = USB_BUS_DISCONNECTED;
|
||||
update_power_manager_restriction(false);
|
||||
|
||||
return 0;
|
||||
@@ -230,15 +329,7 @@ static int module_start(void)
|
||||
}
|
||||
|
||||
running = true;
|
||||
|
||||
if (usbd_initialized || prepare_broadcasted) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
ready_function_mask = 0U;
|
||||
prepare_broadcasted = true;
|
||||
submit_usb_prepare_event();
|
||||
|
||||
submit_usb_state(usb_public_state_get());
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -249,6 +340,7 @@ static void module_pause(void)
|
||||
}
|
||||
|
||||
running = false;
|
||||
submit_usb_state(usb_public_state_get());
|
||||
}
|
||||
|
||||
static bool handle_module_state_event(const struct module_state_event *event)
|
||||
@@ -273,7 +365,7 @@ static bool handle_module_state_event(const struct module_state_event *event)
|
||||
|
||||
if (err) {
|
||||
module_set_state(MODULE_STATE_ERROR);
|
||||
} else if (usbd_initialized) {
|
||||
} else if (usb_stack_was_initialized()) {
|
||||
module_set_state(MODULE_STATE_READY);
|
||||
}
|
||||
}
|
||||
@@ -281,22 +373,15 @@ static bool handle_module_state_event(const struct module_state_event *event)
|
||||
return false;
|
||||
}
|
||||
|
||||
static bool handle_usb_function_ready_event(const struct usb_function_ready_event *event)
|
||||
static bool handle_module_suspend_req_event(
|
||||
const struct module_suspend_req_event *event)
|
||||
{
|
||||
int err;
|
||||
|
||||
if (!running || !prepare_broadcasted || usbd_initialized) {
|
||||
if ((event->sink_module_id != MODULE_ID(MODULE)) || !running) {
|
||||
return false;
|
||||
}
|
||||
|
||||
ready_function_mask |= event->function_mask;
|
||||
int err = usb_stack_disable_if_needed();
|
||||
|
||||
if ((ready_function_mask & USB_REQUIRED_FUNCTION_MASK) !=
|
||||
USB_REQUIRED_FUNCTION_MASK) {
|
||||
return false;
|
||||
}
|
||||
|
||||
err = usb_stack_init();
|
||||
if (err) {
|
||||
module_set_state(MODULE_STATE_ERROR);
|
||||
return false;
|
||||
@@ -306,14 +391,41 @@ static bool handle_usb_function_ready_event(const struct usb_function_ready_even
|
||||
return false;
|
||||
}
|
||||
|
||||
static bool handle_module_resume_req_event(
|
||||
const struct module_resume_req_event *event)
|
||||
{
|
||||
if ((event->sink_module_id != MODULE_ID(MODULE)) || !running) {
|
||||
return false;
|
||||
}
|
||||
|
||||
int err = usb_stack_enable_if_needed();
|
||||
|
||||
if (err) {
|
||||
module_set_state(MODULE_STATE_ERROR);
|
||||
return false;
|
||||
}
|
||||
|
||||
if (usb_stack_was_initialized()) {
|
||||
module_set_state(MODULE_STATE_READY);
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
static bool app_event_handler(const struct app_event_header *aeh)
|
||||
{
|
||||
if (is_module_state_event(aeh)) {
|
||||
return handle_module_state_event(cast_module_state_event(aeh));
|
||||
}
|
||||
|
||||
if (is_usb_function_ready_event(aeh)) {
|
||||
return handle_usb_function_ready_event(cast_usb_function_ready_event(aeh));
|
||||
if (is_module_suspend_req_event(aeh)) {
|
||||
return handle_module_suspend_req_event(
|
||||
cast_module_suspend_req_event(aeh));
|
||||
}
|
||||
|
||||
if (is_module_resume_req_event(aeh)) {
|
||||
return handle_module_resume_req_event(
|
||||
cast_module_resume_req_event(aeh));
|
||||
}
|
||||
|
||||
if (is_power_down_event(aeh)) {
|
||||
@@ -331,7 +443,7 @@ static bool app_event_handler(const struct app_event_header *aeh)
|
||||
|
||||
if (err) {
|
||||
module_set_state(MODULE_STATE_ERROR);
|
||||
} else if (usbd_initialized) {
|
||||
} else if (usb_stack_was_initialized()) {
|
||||
module_set_state(MODULE_STATE_READY);
|
||||
}
|
||||
}
|
||||
@@ -344,6 +456,7 @@ static bool app_event_handler(const struct app_event_header *aeh)
|
||||
|
||||
APP_EVENT_LISTENER(MODULE, app_event_handler);
|
||||
APP_EVENT_SUBSCRIBE(MODULE, module_state_event);
|
||||
APP_EVENT_SUBSCRIBE(MODULE, usb_function_ready_event);
|
||||
APP_EVENT_SUBSCRIBE(MODULE, module_suspend_req_event);
|
||||
APP_EVENT_SUBSCRIBE(MODULE, module_resume_req_event);
|
||||
APP_EVENT_SUBSCRIBE_EARLY(MODULE, power_down_event);
|
||||
APP_EVENT_SUBSCRIBE(MODULE, wake_up_event);
|
||||
|
||||
1
src/usb_function_hook.ld
Normal file
1
src/usb_function_hook.ld
Normal file
@@ -0,0 +1 @@
|
||||
ITERABLE_SECTION_ROM(usb_function_hook, Z_LINK_ITERABLE_SUBALIGN)
|
||||
312
src/usb_hid_consumer_module.c
Normal file
312
src/usb_hid_consumer_module.c
Normal file
@@ -0,0 +1,312 @@
|
||||
#include <errno.h>
|
||||
#include <stdbool.h>
|
||||
#include <stdint.h>
|
||||
#include <string.h>
|
||||
|
||||
#include <app_event_manager.h>
|
||||
|
||||
#define MODULE usb_hid_consumer_module
|
||||
#include <caf/events/module_state_event.h>
|
||||
#include <caf/events/power_event.h>
|
||||
|
||||
#include <zephyr/device.h>
|
||||
#include <zephyr/drivers/usb/udc_buf.h>
|
||||
#include <zephyr/logging/log.h>
|
||||
#include <zephyr/usb/class/usbd_hid.h>
|
||||
|
||||
#include "hid_channel_state_event.h"
|
||||
#include "hid_report_sent_event.h"
|
||||
#include "hid_tx_report_event.h"
|
||||
#include "keyboard_core.h"
|
||||
#include "usb_function_hook.h"
|
||||
#include "usb_state_event.h"
|
||||
|
||||
LOG_MODULE_REGISTER(MODULE, LOG_LEVEL_INF);
|
||||
|
||||
static const struct device *const hid_dev =
|
||||
DEVICE_DT_GET(DT_NODELABEL(hid_consumer));
|
||||
|
||||
static const uint8_t consumer_report_desc[] = {
|
||||
0x05, 0x0C, 0x09, 0x01, 0xA1, 0x01, 0x15, 0x00,
|
||||
0x26, 0xFF, 0x03, 0x19, 0x00, 0x2A, 0xFF, 0x03,
|
||||
0x75, 0x10, 0x95, 0x01, 0x81, 0x00, 0xC0
|
||||
};
|
||||
|
||||
static bool initialized;
|
||||
static bool running;
|
||||
static bool usb_active;
|
||||
static bool iface_ready;
|
||||
static bool report_in_flight;
|
||||
static uint16_t in_flight_sequence;
|
||||
|
||||
UDC_STATIC_BUF_DEFINE(consumer_tx_buf, KEYBOARD_CONSUMER_REPORT_SIZE);
|
||||
|
||||
static void publish_consumer_state(void)
|
||||
{
|
||||
bool ready = running && usb_active && iface_ready;
|
||||
|
||||
submit_hid_channel_state_event(HID_SEND_CH_USB_CONSUMER,
|
||||
ready ? BIT(KEYBOARD_REPORT_TYPE_CONSUMER) : 0U,
|
||||
KEYBOARD_PROTOCOL_MODE_REPORT);
|
||||
}
|
||||
|
||||
static void consumer_iface_ready(const struct device *dev, const bool ready)
|
||||
{
|
||||
ARG_UNUSED(dev);
|
||||
|
||||
iface_ready = ready;
|
||||
if (!ready) {
|
||||
report_in_flight = false;
|
||||
}
|
||||
|
||||
LOG_INF("%s interface %s", hid_dev->name, ready ? "ready" : "not ready");
|
||||
publish_consumer_state();
|
||||
}
|
||||
|
||||
static int consumer_get_report(const struct device *dev,
|
||||
const uint8_t type, const uint8_t id,
|
||||
const uint16_t len, uint8_t *const buf)
|
||||
{
|
||||
ARG_UNUSED(dev);
|
||||
ARG_UNUSED(type);
|
||||
ARG_UNUSED(id);
|
||||
ARG_UNUSED(len);
|
||||
ARG_UNUSED(buf);
|
||||
|
||||
return -ENOTSUP;
|
||||
}
|
||||
|
||||
static int consumer_set_report(const struct device *dev,
|
||||
const uint8_t type, const uint8_t id,
|
||||
const uint16_t len, const uint8_t *const buf)
|
||||
{
|
||||
ARG_UNUSED(dev);
|
||||
ARG_UNUSED(type);
|
||||
ARG_UNUSED(id);
|
||||
ARG_UNUSED(len);
|
||||
ARG_UNUSED(buf);
|
||||
|
||||
return -ENOTSUP;
|
||||
}
|
||||
|
||||
static void consumer_set_idle(const struct device *dev,
|
||||
const uint8_t id, const uint32_t duration)
|
||||
{
|
||||
ARG_UNUSED(dev);
|
||||
ARG_UNUSED(id);
|
||||
ARG_UNUSED(duration);
|
||||
}
|
||||
|
||||
static uint32_t consumer_get_idle(const struct device *dev, const uint8_t id)
|
||||
{
|
||||
ARG_UNUSED(dev);
|
||||
ARG_UNUSED(id);
|
||||
|
||||
return 0U;
|
||||
}
|
||||
|
||||
static void consumer_set_protocol(const struct device *dev, const uint8_t proto)
|
||||
{
|
||||
ARG_UNUSED(dev);
|
||||
ARG_UNUSED(proto);
|
||||
}
|
||||
|
||||
static void consumer_input_report_done(const struct device *dev,
|
||||
const uint8_t *const report)
|
||||
{
|
||||
ARG_UNUSED(dev);
|
||||
ARG_UNUSED(report);
|
||||
|
||||
report_in_flight = false;
|
||||
submit_hid_report_sent_event(HID_SEND_CH_USB_CONSUMER,
|
||||
KEYBOARD_REPORT_TYPE_CONSUMER,
|
||||
in_flight_sequence, false);
|
||||
}
|
||||
|
||||
static void consumer_output_report(const struct device *dev,
|
||||
const uint16_t len,
|
||||
const uint8_t *const buf)
|
||||
{
|
||||
ARG_UNUSED(dev);
|
||||
ARG_UNUSED(len);
|
||||
ARG_UNUSED(buf);
|
||||
}
|
||||
|
||||
static const struct hid_device_ops consumer_ops = {
|
||||
.iface_ready = consumer_iface_ready,
|
||||
.get_report = consumer_get_report,
|
||||
.set_report = consumer_set_report,
|
||||
.set_idle = consumer_set_idle,
|
||||
.get_idle = consumer_get_idle,
|
||||
.set_protocol = consumer_set_protocol,
|
||||
.input_report_done = consumer_input_report_done,
|
||||
.output_report = consumer_output_report,
|
||||
};
|
||||
|
||||
static int usb_hid_consumer_register_device(void)
|
||||
{
|
||||
if (!device_is_ready(hid_dev)) {
|
||||
LOG_ERR("HID device %s not ready", hid_dev->name);
|
||||
return -ENODEV;
|
||||
}
|
||||
|
||||
return hid_device_register(hid_dev, consumer_report_desc,
|
||||
sizeof(consumer_report_desc), &consumer_ops);
|
||||
}
|
||||
|
||||
USB_FUNCTION_HOOK_DEFINE(usb_hid_consumer_hook, usb_hid_consumer_register_device);
|
||||
|
||||
static int module_init(void)
|
||||
{
|
||||
usb_active = false;
|
||||
iface_ready = false;
|
||||
report_in_flight = false;
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int module_start(void)
|
||||
{
|
||||
if (running) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
running = true;
|
||||
publish_consumer_state();
|
||||
return 0;
|
||||
}
|
||||
|
||||
static void module_pause(void)
|
||||
{
|
||||
if (!running) {
|
||||
return;
|
||||
}
|
||||
|
||||
running = false;
|
||||
report_in_flight = false;
|
||||
publish_consumer_state();
|
||||
}
|
||||
|
||||
static bool handle_usb_state_event(const struct usb_state_event *event)
|
||||
{
|
||||
bool new_usb_active = (event->state == USB_STATE_ACTIVE);
|
||||
|
||||
if (new_usb_active == usb_active) {
|
||||
return false;
|
||||
}
|
||||
|
||||
usb_active = new_usb_active;
|
||||
if (!usb_active) {
|
||||
iface_ready = false;
|
||||
report_in_flight = false;
|
||||
}
|
||||
|
||||
publish_consumer_state();
|
||||
return false;
|
||||
}
|
||||
|
||||
static bool handle_hid_tx_report_event(const struct hid_tx_report_event *event)
|
||||
{
|
||||
int err;
|
||||
|
||||
if (!running || !usb_active ||
|
||||
(event->channel != HID_SEND_CH_USB_CONSUMER)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (event->report_type != KEYBOARD_REPORT_TYPE_CONSUMER) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!iface_ready) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (report_in_flight) {
|
||||
LOG_WRN("Drop USB consumer report while previous report is in flight");
|
||||
return false;
|
||||
}
|
||||
|
||||
memcpy(consumer_tx_buf, event->dyndata.data, event->dyndata.size);
|
||||
err = hid_device_submit_report(hid_dev, (uint16_t)event->dyndata.size,
|
||||
consumer_tx_buf);
|
||||
if (err) {
|
||||
LOG_WRN("USB consumer report submit failed (%d)", err);
|
||||
submit_hid_report_sent_event(HID_SEND_CH_USB_CONSUMER,
|
||||
KEYBOARD_REPORT_TYPE_CONSUMER,
|
||||
event->sequence, true);
|
||||
} else {
|
||||
report_in_flight = true;
|
||||
in_flight_sequence = event->sequence;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
static bool app_event_handler(const struct app_event_header *aeh)
|
||||
{
|
||||
if (is_hid_tx_report_event(aeh)) {
|
||||
return handle_hid_tx_report_event(cast_hid_tx_report_event(aeh));
|
||||
}
|
||||
|
||||
if (is_usb_state_event(aeh)) {
|
||||
return handle_usb_state_event(cast_usb_state_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);
|
||||
} 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, hid_tx_report_event);
|
||||
APP_EVENT_SUBSCRIBE(MODULE, usb_state_event);
|
||||
APP_EVENT_SUBSCRIBE_EARLY(MODULE, power_down_event);
|
||||
APP_EVENT_SUBSCRIBE(MODULE, wake_up_event);
|
||||
384
src/usb_hid_keyboard_module.c
Normal file
384
src/usb_hid_keyboard_module.c
Normal file
@@ -0,0 +1,384 @@
|
||||
#include <errno.h>
|
||||
#include <stdbool.h>
|
||||
#include <stdint.h>
|
||||
#include <string.h>
|
||||
|
||||
#include <app_event_manager.h>
|
||||
|
||||
#define MODULE usb_hid_keyboard_module
|
||||
#include <caf/events/module_state_event.h>
|
||||
#include <caf/events/power_event.h>
|
||||
|
||||
#include <zephyr/device.h>
|
||||
#include <zephyr/drivers/usb/udc_buf.h>
|
||||
#include <zephyr/logging/log.h>
|
||||
#include <zephyr/usb/class/usbd_hid.h>
|
||||
|
||||
#include "hid_channel_state_event.h"
|
||||
#include "hid_led_event.h"
|
||||
#include "hid_report_sent_event.h"
|
||||
#include "hid_tx_report_event.h"
|
||||
#include "keyboard_core.h"
|
||||
#include "set_protocol_event.h"
|
||||
#include "usb_function_hook.h"
|
||||
#include "usb_state_event.h"
|
||||
|
||||
LOG_MODULE_REGISTER(MODULE, LOG_LEVEL_INF);
|
||||
|
||||
#define KBD_LED_REPORT_SIZE 1U
|
||||
#define KBD_LED_REPORT_WITH_ID_SIZE (KBD_LED_REPORT_SIZE + 1U)
|
||||
|
||||
static const struct device *const hid_dev = DEVICE_DT_GET(DT_NODELABEL(hid_kbd));
|
||||
|
||||
static const uint8_t keyboard_report_desc[] = {
|
||||
0x05, 0x01, 0x09, 0x06, 0xA1, 0x01, 0x05, 0x07,
|
||||
0x19, 0xE0, 0x29, 0xE7, 0x15, 0x00, 0x25, 0x01,
|
||||
0x75, 0x01, 0x95, 0x08, 0x81, 0x02, 0x05, 0x07,
|
||||
0x19, 0x00, 0x2A, 0xDF, 0x00, 0x15, 0x00, 0x25,
|
||||
0x01, 0x75, 0x01, 0x96, 0xE0, 0x00, 0x81, 0x02,
|
||||
0x05, 0x08, 0x19, 0x01, 0x29, 0x05, 0x15, 0x00,
|
||||
0x25, 0x01, 0x75, 0x01, 0x95, 0x05, 0x91, 0x02,
|
||||
0x75, 0x03, 0x95, 0x01, 0x91, 0x01, 0xC0
|
||||
};
|
||||
|
||||
static enum keyboard_protocol_mode keyboard_protocol_mode =
|
||||
KEYBOARD_PROTOCOL_MODE_REPORT;
|
||||
static bool initialized;
|
||||
static bool running;
|
||||
static bool usb_active;
|
||||
static bool iface_ready;
|
||||
static bool report_in_flight;
|
||||
static uint16_t in_flight_sequence;
|
||||
|
||||
UDC_STATIC_BUF_DEFINE(keyboard_tx_buf, KEYBOARD_NKRO_REPORT_SIZE);
|
||||
|
||||
static void publish_keyboard_state(void)
|
||||
{
|
||||
bool ready = running && usb_active && iface_ready;
|
||||
|
||||
submit_hid_channel_state_event(HID_SEND_CH_USB_KEYS,
|
||||
ready ? BIT(KEYBOARD_REPORT_TYPE_KEYS) : 0U,
|
||||
keyboard_protocol_mode);
|
||||
}
|
||||
|
||||
static void keyboard_iface_ready(const struct device *dev, const bool ready)
|
||||
{
|
||||
ARG_UNUSED(dev);
|
||||
|
||||
iface_ready = ready;
|
||||
if (!ready) {
|
||||
report_in_flight = false;
|
||||
}
|
||||
|
||||
LOG_INF("%s interface %s", hid_dev->name, ready ? "ready" : "not ready");
|
||||
publish_keyboard_state();
|
||||
}
|
||||
|
||||
static int keyboard_handle_led_report(const char *source, uint8_t type, uint8_t id,
|
||||
uint16_t len, const uint8_t *buf)
|
||||
{
|
||||
uint8_t led_bm;
|
||||
|
||||
if (buf == NULL) {
|
||||
LOG_WRN("%s invalid keyboard report", source);
|
||||
return -EINVAL;
|
||||
}
|
||||
|
||||
|
||||
if (len == KBD_LED_REPORT_SIZE) {
|
||||
led_bm = buf[0];
|
||||
} else if (len == KBD_LED_REPORT_WITH_ID_SIZE) {
|
||||
led_bm = buf[1];
|
||||
} else {
|
||||
LOG_WRN("%s unexpected len %u type %u id %u",
|
||||
source, len, type, id);
|
||||
if (len >= KBD_LED_REPORT_WITH_ID_SIZE) {
|
||||
LOG_WRN("%s possible report_id=0x%02x led_bm=0x%02x",
|
||||
source, buf[0], buf[1]);
|
||||
}
|
||||
return -EMSGSIZE;
|
||||
}
|
||||
|
||||
submit_hid_led_event(HID_TRANSPORT_USB, led_bm);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int keyboard_get_report(const struct device *dev,
|
||||
const uint8_t type, const uint8_t id,
|
||||
const uint16_t len, uint8_t *const buf)
|
||||
{
|
||||
ARG_UNUSED(dev);
|
||||
ARG_UNUSED(type);
|
||||
ARG_UNUSED(id);
|
||||
ARG_UNUSED(len);
|
||||
ARG_UNUSED(buf);
|
||||
|
||||
return -ENOTSUP;
|
||||
}
|
||||
|
||||
static int keyboard_set_report(const struct device *dev,
|
||||
const uint8_t type, const uint8_t id,
|
||||
const uint16_t len, const uint8_t *const buf)
|
||||
{
|
||||
ARG_UNUSED(dev);
|
||||
|
||||
if (type != HID_REPORT_TYPE_OUTPUT) {
|
||||
LOG_WRN("USB keyboard set_report unsupported type %u id %u len %u",
|
||||
type, id, len);
|
||||
if (buf != NULL) {
|
||||
LOG_HEXDUMP_INF(buf, len, "USB keyboard set_report raw");
|
||||
}
|
||||
return -ENOTSUP;
|
||||
}
|
||||
|
||||
return keyboard_handle_led_report("USB keyboard set_report", type, id,
|
||||
len, buf);
|
||||
}
|
||||
|
||||
static void keyboard_set_idle(const struct device *dev,
|
||||
const uint8_t id, const uint32_t duration)
|
||||
{
|
||||
ARG_UNUSED(dev);
|
||||
ARG_UNUSED(id);
|
||||
ARG_UNUSED(duration);
|
||||
}
|
||||
|
||||
static uint32_t keyboard_get_idle(const struct device *dev, const uint8_t id)
|
||||
{
|
||||
ARG_UNUSED(dev);
|
||||
ARG_UNUSED(id);
|
||||
|
||||
return 0U;
|
||||
}
|
||||
|
||||
static enum keyboard_protocol_mode usb_proto_to_keyboard_proto(uint8_t proto)
|
||||
{
|
||||
return (proto == HID_PROTOCOL_BOOT) ? KEYBOARD_PROTOCOL_MODE_BOOT
|
||||
: KEYBOARD_PROTOCOL_MODE_REPORT;
|
||||
}
|
||||
|
||||
static void keyboard_set_protocol(const struct device *dev, const uint8_t proto)
|
||||
{
|
||||
ARG_UNUSED(dev);
|
||||
|
||||
enum keyboard_protocol_mode new_mode = usb_proto_to_keyboard_proto(proto);
|
||||
|
||||
if (keyboard_protocol_mode == new_mode) {
|
||||
return;
|
||||
}
|
||||
|
||||
keyboard_protocol_mode = new_mode;
|
||||
LOG_INF("USB keyboard protocol -> %s",
|
||||
(new_mode == KEYBOARD_PROTOCOL_MODE_BOOT) ? "boot" : "report");
|
||||
submit_set_protocol_event(HID_TRANSPORT_USB, new_mode);
|
||||
publish_keyboard_state();
|
||||
}
|
||||
|
||||
static void keyboard_input_report_done(const struct device *dev,
|
||||
const uint8_t *const report)
|
||||
{
|
||||
ARG_UNUSED(dev);
|
||||
ARG_UNUSED(report);
|
||||
|
||||
report_in_flight = false;
|
||||
submit_hid_report_sent_event(HID_SEND_CH_USB_KEYS,
|
||||
KEYBOARD_REPORT_TYPE_KEYS,
|
||||
in_flight_sequence, false);
|
||||
}
|
||||
|
||||
static void keyboard_output_report(const struct device *dev,
|
||||
const uint16_t len,
|
||||
const uint8_t *const buf)
|
||||
{
|
||||
ARG_UNUSED(dev);
|
||||
|
||||
(void)keyboard_handle_led_report("USB keyboard output report",
|
||||
HID_REPORT_TYPE_OUTPUT, 0U,
|
||||
len, buf);
|
||||
}
|
||||
|
||||
static const struct hid_device_ops keyboard_ops = {
|
||||
.iface_ready = keyboard_iface_ready,
|
||||
.get_report = keyboard_get_report,
|
||||
.set_report = keyboard_set_report,
|
||||
.set_idle = keyboard_set_idle,
|
||||
.get_idle = keyboard_get_idle,
|
||||
.set_protocol = keyboard_set_protocol,
|
||||
.input_report_done = keyboard_input_report_done,
|
||||
.output_report = keyboard_output_report,
|
||||
};
|
||||
|
||||
static int usb_hid_keyboard_register_device(void)
|
||||
{
|
||||
if (!device_is_ready(hid_dev)) {
|
||||
LOG_ERR("HID device %s not ready", hid_dev->name);
|
||||
return -ENODEV;
|
||||
}
|
||||
|
||||
return hid_device_register(hid_dev, keyboard_report_desc,
|
||||
sizeof(keyboard_report_desc), &keyboard_ops);
|
||||
}
|
||||
|
||||
USB_FUNCTION_HOOK_DEFINE(usb_hid_keyboard_hook, usb_hid_keyboard_register_device);
|
||||
|
||||
static int module_init(void)
|
||||
{
|
||||
keyboard_protocol_mode = KEYBOARD_PROTOCOL_MODE_REPORT;
|
||||
usb_active = false;
|
||||
iface_ready = false;
|
||||
report_in_flight = false;
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int module_start(void)
|
||||
{
|
||||
if (running) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
running = true;
|
||||
publish_keyboard_state();
|
||||
return 0;
|
||||
}
|
||||
|
||||
static void module_pause(void)
|
||||
{
|
||||
if (!running) {
|
||||
return;
|
||||
}
|
||||
|
||||
running = false;
|
||||
report_in_flight = false;
|
||||
publish_keyboard_state();
|
||||
}
|
||||
|
||||
static bool handle_usb_state_event(const struct usb_state_event *event)
|
||||
{
|
||||
bool new_usb_active = (event->state == USB_STATE_ACTIVE);
|
||||
|
||||
if (new_usb_active == usb_active) {
|
||||
return false;
|
||||
}
|
||||
|
||||
usb_active = new_usb_active;
|
||||
if (!usb_active) {
|
||||
iface_ready = false;
|
||||
report_in_flight = false;
|
||||
}
|
||||
|
||||
publish_keyboard_state();
|
||||
return false;
|
||||
}
|
||||
|
||||
static bool handle_hid_tx_report_event(const struct hid_tx_report_event *event)
|
||||
{
|
||||
int err;
|
||||
|
||||
if (!running || !usb_active ||
|
||||
(event->channel != HID_SEND_CH_USB_KEYS)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (event->report_type != KEYBOARD_REPORT_TYPE_KEYS) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (event->protocol_mode != keyboard_protocol_mode) {
|
||||
LOG_WRN("Drop USB keys report due to protocol mismatch");
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!iface_ready) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (report_in_flight) {
|
||||
LOG_WRN("Drop USB keyboard report while previous report is in flight");
|
||||
return false;
|
||||
}
|
||||
|
||||
memcpy(keyboard_tx_buf, event->dyndata.data, event->dyndata.size);
|
||||
err = hid_device_submit_report(hid_dev, (uint16_t)event->dyndata.size,
|
||||
keyboard_tx_buf);
|
||||
if (err) {
|
||||
LOG_WRN("USB keyboard report submit failed (%d)", err);
|
||||
submit_hid_report_sent_event(HID_SEND_CH_USB_KEYS,
|
||||
KEYBOARD_REPORT_TYPE_KEYS,
|
||||
event->sequence, true);
|
||||
} else {
|
||||
report_in_flight = true;
|
||||
in_flight_sequence = event->sequence;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
static bool app_event_handler(const struct app_event_header *aeh)
|
||||
{
|
||||
if (is_hid_tx_report_event(aeh)) {
|
||||
return handle_hid_tx_report_event(cast_hid_tx_report_event(aeh));
|
||||
}
|
||||
|
||||
if (is_usb_state_event(aeh)) {
|
||||
return handle_usb_state_event(cast_usb_state_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);
|
||||
} 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, hid_tx_report_event);
|
||||
APP_EVENT_SUBSCRIBE(MODULE, usb_state_event);
|
||||
APP_EVENT_SUBSCRIBE_EARLY(MODULE, power_down_event);
|
||||
APP_EVENT_SUBSCRIBE(MODULE, wake_up_event);
|
||||
@@ -1,647 +0,0 @@
|
||||
#include <errno.h>
|
||||
#include <stdbool.h>
|
||||
#include <stdint.h>
|
||||
#include <string.h>
|
||||
|
||||
#include <app_event_manager.h>
|
||||
|
||||
#define MODULE usb_hid_module
|
||||
#include <caf/events/module_state_event.h>
|
||||
#include <caf/events/power_event.h>
|
||||
|
||||
#include <zephyr/device.h>
|
||||
#include <zephyr/drivers/usb/udc_buf.h>
|
||||
#include <zephyr/logging/log.h>
|
||||
#include <zephyr/sys/util.h>
|
||||
#include <zephyr/usb/class/usbd_hid.h>
|
||||
|
||||
#include "hid_led_event.h"
|
||||
#include "hid_report_sent_event.h"
|
||||
#include "hid_transport_state_event.h"
|
||||
#include "hid_tx_report_event.h"
|
||||
#include "keyboard_core.h"
|
||||
#include "set_protocol_event.h"
|
||||
#include "usb_function_ready_event.h"
|
||||
#include "usb_prepare_event.h"
|
||||
#include "usb_device_module.h"
|
||||
#include "usb_device_state_event.h"
|
||||
|
||||
LOG_MODULE_REGISTER(MODULE, LOG_LEVEL_INF);
|
||||
|
||||
#define KBD_LED_REPORT_SIZE 1U
|
||||
|
||||
enum usb_hid_interface {
|
||||
USB_HID_INTERFACE_KEYBOARD,
|
||||
USB_HID_INTERFACE_CONSUMER,
|
||||
USB_HID_INTERFACE_COUNT,
|
||||
};
|
||||
|
||||
struct usb_hid_interface_state {
|
||||
const struct device *dev;
|
||||
bool ready;
|
||||
};
|
||||
|
||||
static const uint8_t keyboard_report_desc[] = {
|
||||
0x05, 0x01, /* Usage Page (Generic Desktop) */
|
||||
0x09, 0x06, /* Usage (Keyboard) */
|
||||
0xA1, 0x01, /* Collection (Application) */
|
||||
0x05, 0x07, /* Usage Page (Keyboard/Keypad) */
|
||||
0x19, 0xE0, /* Usage Minimum (0xE0) */
|
||||
0x29, 0xE7, /* Usage Maximum (0xE7) */
|
||||
0x15, 0x00, /* Logical Minimum (0) */
|
||||
0x25, 0x01, /* Logical Maximum (1) */
|
||||
0x75, 0x01, /* Report Size (1) */
|
||||
0x95, 0x08, /* Report Count (8) */
|
||||
0x81, 0x02, /* Input (Data,Var,Abs) */
|
||||
0x05, 0x07, /* Usage Page (Keyboard/Keypad) */
|
||||
0x19, 0x00, /* Usage Minimum (0x00) */
|
||||
0x2A, 0xDF, 0x00, /* Usage Maximum (0x00DF) */
|
||||
0x15, 0x00, /* Logical Minimum (0) */
|
||||
0x25, 0x01, /* Logical Maximum (1) */
|
||||
0x75, 0x01, /* Report Size (1) */
|
||||
0x96, 0xE0, 0x00, /* Report Count (224) */
|
||||
0x81, 0x02, /* Input (Data,Var,Abs) */
|
||||
0x05, 0x08, /* Usage Page (LEDs) */
|
||||
0x19, 0x01, /* Usage Minimum (1) */
|
||||
0x29, 0x05, /* Usage Maximum (5) */
|
||||
0x15, 0x00, /* Logical Minimum (0) */
|
||||
0x25, 0x01, /* Logical Maximum (1) */
|
||||
0x75, 0x01, /* Report Size (1) */
|
||||
0x95, 0x05, /* Report Count (5) */
|
||||
0x91, 0x02, /* Output (Data,Var,Abs) */
|
||||
0x75, 0x03, /* Report Size (3) */
|
||||
0x95, 0x01, /* Report Count (1) */
|
||||
0x91, 0x01, /* Output (Const,Array,Abs) */
|
||||
0xC0 /* End Collection */
|
||||
};
|
||||
|
||||
static const uint8_t consumer_report_desc[] = {
|
||||
0x05, 0x0C, /* Usage Page (Consumer) */
|
||||
0x09, 0x01, /* Usage (Consumer Control) */
|
||||
0xA1, 0x01, /* Collection (Application) */
|
||||
0x15, 0x00, /* Logical Minimum (0) */
|
||||
0x26, 0xFF, 0x03, /* Logical Maximum (1023) */
|
||||
0x19, 0x00, /* Usage Minimum (0) */
|
||||
0x2A, 0xFF, 0x03, /* Usage Maximum (1023) */
|
||||
0x75, 0x10, /* Report Size (16) */
|
||||
0x95, 0x01, /* Report Count (1) */
|
||||
0x81, 0x00, /* Input (Data,Array,Abs) */
|
||||
0xC0 /* End Collection */
|
||||
};
|
||||
|
||||
static struct usb_hid_interface_state hid_ifaces[USB_HID_INTERFACE_COUNT] = {
|
||||
[USB_HID_INTERFACE_KEYBOARD] = {
|
||||
.dev = DEVICE_DT_GET(DT_NODELABEL(hid_kbd)),
|
||||
},
|
||||
[USB_HID_INTERFACE_CONSUMER] = {
|
||||
.dev = DEVICE_DT_GET(DT_NODELABEL(hid_consumer)),
|
||||
},
|
||||
};
|
||||
|
||||
static enum keyboard_protocol_mode keyboard_protocol_mode =
|
||||
KEYBOARD_PROTOCOL_MODE_REPORT;
|
||||
static bool initialized;
|
||||
static bool running;
|
||||
static bool usb_active;
|
||||
static bool usb_function_prepared;
|
||||
static bool keyboard_report_in_flight;
|
||||
static bool consumer_report_in_flight;
|
||||
static uint16_t keyboard_in_flight_sequence;
|
||||
static uint16_t consumer_in_flight_sequence;
|
||||
|
||||
UDC_STATIC_BUF_DEFINE(keyboard_tx_buf, KEYBOARD_NKRO_REPORT_SIZE);
|
||||
UDC_STATIC_BUF_DEFINE(consumer_tx_buf, KEYBOARD_CONSUMER_REPORT_SIZE);
|
||||
|
||||
static struct usb_hid_interface_state *iface_from_dev(const struct device *dev)
|
||||
{
|
||||
for (size_t i = 0; i < ARRAY_SIZE(hid_ifaces); i++) {
|
||||
if (hid_ifaces[i].dev == dev) {
|
||||
return &hid_ifaces[i];
|
||||
}
|
||||
}
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
static enum keyboard_protocol_mode usb_proto_to_keyboard_proto(uint8_t proto)
|
||||
{
|
||||
return (proto == HID_PROTOCOL_BOOT) ? KEYBOARD_PROTOCOL_MODE_BOOT
|
||||
: KEYBOARD_PROTOCOL_MODE_REPORT;
|
||||
}
|
||||
|
||||
static void reset_usb_runtime_state(void)
|
||||
{
|
||||
for (size_t i = 0; i < ARRAY_SIZE(hid_ifaces); i++) {
|
||||
hid_ifaces[i].ready = false;
|
||||
}
|
||||
|
||||
usb_active = false;
|
||||
keyboard_report_in_flight = false;
|
||||
consumer_report_in_flight = false;
|
||||
}
|
||||
|
||||
static void submit_set_protocol_event(enum keyboard_protocol_mode protocol_mode)
|
||||
{
|
||||
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;
|
||||
|
||||
event->transport = HID_TRANSPORT_USB;
|
||||
event->ready = ready;
|
||||
event->keys_ready = ready &&
|
||||
hid_ifaces[USB_HID_INTERFACE_KEYBOARD].ready;
|
||||
event->consumer_ready = ready &&
|
||||
hid_ifaces[USB_HID_INTERFACE_CONSUMER].ready;
|
||||
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)
|
||||
{
|
||||
struct usb_hid_interface_state *iface = iface_from_dev(dev);
|
||||
|
||||
if (!iface) {
|
||||
return;
|
||||
}
|
||||
|
||||
iface->ready = ready;
|
||||
if (!ready) {
|
||||
keyboard_report_in_flight = false;
|
||||
}
|
||||
|
||||
LOG_INF("%s interface %s", dev->name, ready ? "ready" : "not ready");
|
||||
submit_transport_state_event();
|
||||
}
|
||||
|
||||
static int keyboard_get_report(const struct device *dev,
|
||||
const uint8_t type, const uint8_t id,
|
||||
const uint16_t len, uint8_t *const buf)
|
||||
{
|
||||
ARG_UNUSED(dev);
|
||||
ARG_UNUSED(type);
|
||||
ARG_UNUSED(id);
|
||||
ARG_UNUSED(len);
|
||||
ARG_UNUSED(buf);
|
||||
|
||||
return -ENOTSUP;
|
||||
}
|
||||
|
||||
static int keyboard_set_report(const struct device *dev,
|
||||
const uint8_t type, const uint8_t id,
|
||||
const uint16_t len, const uint8_t *const buf)
|
||||
{
|
||||
ARG_UNUSED(dev);
|
||||
ARG_UNUSED(type);
|
||||
ARG_UNUSED(id);
|
||||
ARG_UNUSED(len);
|
||||
ARG_UNUSED(buf);
|
||||
|
||||
return -ENOTSUP;
|
||||
}
|
||||
|
||||
static void keyboard_set_idle(const struct device *dev,
|
||||
const uint8_t id, const uint32_t duration)
|
||||
{
|
||||
ARG_UNUSED(dev);
|
||||
ARG_UNUSED(id);
|
||||
ARG_UNUSED(duration);
|
||||
}
|
||||
|
||||
static uint32_t keyboard_get_idle(const struct device *dev, const uint8_t id)
|
||||
{
|
||||
ARG_UNUSED(dev);
|
||||
ARG_UNUSED(id);
|
||||
|
||||
return 0U;
|
||||
}
|
||||
|
||||
static void keyboard_set_protocol(const struct device *dev, const uint8_t proto)
|
||||
{
|
||||
ARG_UNUSED(dev);
|
||||
|
||||
enum keyboard_protocol_mode new_mode = usb_proto_to_keyboard_proto(proto);
|
||||
|
||||
if (keyboard_protocol_mode == new_mode) {
|
||||
return;
|
||||
}
|
||||
|
||||
keyboard_protocol_mode = new_mode;
|
||||
LOG_INF("USB keyboard protocol -> %s",
|
||||
(new_mode == KEYBOARD_PROTOCOL_MODE_BOOT) ? "boot" : "report");
|
||||
submit_set_protocol_event(new_mode);
|
||||
submit_transport_state_event();
|
||||
}
|
||||
|
||||
static void keyboard_input_report_done(const struct device *dev,
|
||||
const uint8_t *const report)
|
||||
{
|
||||
ARG_UNUSED(report);
|
||||
|
||||
keyboard_report_in_flight = false;
|
||||
LOG_DBG("USB keyboard report sent by %s", dev->name);
|
||||
submit_hid_report_sent_event(KEYBOARD_REPORT_TYPE_KEYS,
|
||||
keyboard_in_flight_sequence, false);
|
||||
}
|
||||
|
||||
static void keyboard_output_report(const struct device *dev,
|
||||
const uint16_t len,
|
||||
const uint8_t *const buf)
|
||||
{
|
||||
ARG_UNUSED(dev);
|
||||
|
||||
if ((len < KBD_LED_REPORT_SIZE) || (buf == NULL)) {
|
||||
LOG_WRN("Invalid keyboard output report");
|
||||
return;
|
||||
}
|
||||
|
||||
submit_hid_led_event(buf[0]);
|
||||
}
|
||||
|
||||
static const struct hid_device_ops keyboard_ops = {
|
||||
.iface_ready = keyboard_iface_ready,
|
||||
.get_report = keyboard_get_report,
|
||||
.set_report = keyboard_set_report,
|
||||
.set_idle = keyboard_set_idle,
|
||||
.get_idle = keyboard_get_idle,
|
||||
.set_protocol = keyboard_set_protocol,
|
||||
.input_report_done = keyboard_input_report_done,
|
||||
.output_report = keyboard_output_report,
|
||||
};
|
||||
|
||||
static void consumer_iface_ready(const struct device *dev, const bool ready)
|
||||
{
|
||||
struct usb_hid_interface_state *iface = iface_from_dev(dev);
|
||||
|
||||
if (!iface) {
|
||||
return;
|
||||
}
|
||||
|
||||
iface->ready = ready;
|
||||
if (!ready) {
|
||||
consumer_report_in_flight = false;
|
||||
}
|
||||
|
||||
LOG_INF("%s interface %s", dev->name, ready ? "ready" : "not ready");
|
||||
submit_transport_state_event();
|
||||
}
|
||||
|
||||
static int consumer_get_report(const struct device *dev,
|
||||
const uint8_t type, const uint8_t id,
|
||||
const uint16_t len, uint8_t *const buf)
|
||||
{
|
||||
ARG_UNUSED(dev);
|
||||
ARG_UNUSED(type);
|
||||
ARG_UNUSED(id);
|
||||
ARG_UNUSED(len);
|
||||
ARG_UNUSED(buf);
|
||||
|
||||
return -ENOTSUP;
|
||||
}
|
||||
|
||||
static int consumer_set_report(const struct device *dev,
|
||||
const uint8_t type, const uint8_t id,
|
||||
const uint16_t len, const uint8_t *const buf)
|
||||
{
|
||||
ARG_UNUSED(dev);
|
||||
ARG_UNUSED(type);
|
||||
ARG_UNUSED(id);
|
||||
ARG_UNUSED(len);
|
||||
ARG_UNUSED(buf);
|
||||
|
||||
return -ENOTSUP;
|
||||
}
|
||||
|
||||
static void consumer_set_idle(const struct device *dev,
|
||||
const uint8_t id, const uint32_t duration)
|
||||
{
|
||||
ARG_UNUSED(dev);
|
||||
ARG_UNUSED(id);
|
||||
ARG_UNUSED(duration);
|
||||
}
|
||||
|
||||
static uint32_t consumer_get_idle(const struct device *dev, const uint8_t id)
|
||||
{
|
||||
ARG_UNUSED(dev);
|
||||
ARG_UNUSED(id);
|
||||
|
||||
return 0U;
|
||||
}
|
||||
|
||||
static void consumer_set_protocol(const struct device *dev, const uint8_t proto)
|
||||
{
|
||||
ARG_UNUSED(dev);
|
||||
ARG_UNUSED(proto);
|
||||
}
|
||||
|
||||
static void consumer_input_report_done(const struct device *dev,
|
||||
const uint8_t *const report)
|
||||
{
|
||||
ARG_UNUSED(report);
|
||||
|
||||
consumer_report_in_flight = false;
|
||||
LOG_DBG("USB consumer report sent by %s", dev->name);
|
||||
submit_hid_report_sent_event(KEYBOARD_REPORT_TYPE_CONSUMER,
|
||||
consumer_in_flight_sequence, false);
|
||||
}
|
||||
|
||||
static void consumer_output_report(const struct device *dev,
|
||||
const uint16_t len,
|
||||
const uint8_t *const buf)
|
||||
{
|
||||
ARG_UNUSED(dev);
|
||||
ARG_UNUSED(len);
|
||||
ARG_UNUSED(buf);
|
||||
}
|
||||
|
||||
static const struct hid_device_ops consumer_ops = {
|
||||
.iface_ready = consumer_iface_ready,
|
||||
.get_report = consumer_get_report,
|
||||
.set_report = consumer_set_report,
|
||||
.set_idle = consumer_set_idle,
|
||||
.get_idle = consumer_get_idle,
|
||||
.set_protocol = consumer_set_protocol,
|
||||
.input_report_done = consumer_input_report_done,
|
||||
.output_report = consumer_output_report,
|
||||
};
|
||||
|
||||
static int usb_hid_register_devices(void)
|
||||
{
|
||||
int err;
|
||||
|
||||
for (size_t i = 0; i < ARRAY_SIZE(hid_ifaces); i++) {
|
||||
if (!device_is_ready(hid_ifaces[i].dev)) {
|
||||
LOG_ERR("HID device %s not ready", hid_ifaces[i].dev->name);
|
||||
return -ENODEV;
|
||||
}
|
||||
}
|
||||
|
||||
err = hid_device_register(hid_ifaces[USB_HID_INTERFACE_KEYBOARD].dev,
|
||||
keyboard_report_desc,
|
||||
sizeof(keyboard_report_desc),
|
||||
&keyboard_ops);
|
||||
if (err) {
|
||||
return err;
|
||||
}
|
||||
|
||||
err = hid_device_register(hid_ifaces[USB_HID_INTERFACE_CONSUMER].dev,
|
||||
consumer_report_desc,
|
||||
sizeof(consumer_report_desc),
|
||||
&consumer_ops);
|
||||
if (err) {
|
||||
return err;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int module_init(void)
|
||||
{
|
||||
reset_usb_runtime_state();
|
||||
keyboard_protocol_mode = KEYBOARD_PROTOCOL_MODE_REPORT;
|
||||
usb_function_prepared = false;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int module_start(void)
|
||||
{
|
||||
if (running) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
running = true;
|
||||
submit_transport_state_event();
|
||||
return 0;
|
||||
}
|
||||
|
||||
static void module_pause(void)
|
||||
{
|
||||
running = false;
|
||||
submit_transport_state_event();
|
||||
}
|
||||
|
||||
static bool handle_usb_prepare_event(const struct usb_prepare_event *event)
|
||||
{
|
||||
int err;
|
||||
|
||||
ARG_UNUSED(event);
|
||||
|
||||
if (!running || usb_function_prepared) {
|
||||
return false;
|
||||
}
|
||||
|
||||
err = usb_hid_register_devices();
|
||||
if (err) {
|
||||
LOG_ERR("hid_device_register failed (%d)", err);
|
||||
module_set_state(MODULE_STATE_ERROR);
|
||||
return false;
|
||||
}
|
||||
|
||||
usb_function_prepared = true;
|
||||
submit_usb_function_ready_event();
|
||||
return 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;
|
||||
}
|
||||
|
||||
if (!new_usb_active) {
|
||||
reset_usb_runtime_state();
|
||||
} else {
|
||||
usb_active = true;
|
||||
}
|
||||
|
||||
submit_transport_state_event();
|
||||
return false;
|
||||
}
|
||||
|
||||
static bool handle_hid_tx_report_event(const struct hid_tx_report_event *event)
|
||||
{
|
||||
int err;
|
||||
|
||||
if (!running || !usb_active || (event->transport != HID_TRANSPORT_USB)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (event->report_type == KEYBOARD_REPORT_TYPE_KEYS) {
|
||||
if (event->protocol_mode != keyboard_protocol_mode) {
|
||||
LOG_WRN("Drop USB keys report due to protocol mismatch");
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!hid_ifaces[USB_HID_INTERFACE_KEYBOARD].ready) {
|
||||
LOG_DBG("USB keyboard interface not ready");
|
||||
return false;
|
||||
}
|
||||
|
||||
if (keyboard_report_in_flight) {
|
||||
LOG_WRN("Drop USB keyboard report while previous report is in flight");
|
||||
return false;
|
||||
}
|
||||
|
||||
memcpy(keyboard_tx_buf, event->dyndata.data, event->dyndata.size);
|
||||
|
||||
err = hid_device_submit_report(
|
||||
hid_ifaces[USB_HID_INTERFACE_KEYBOARD].dev,
|
||||
(uint16_t)event->dyndata.size,
|
||||
keyboard_tx_buf);
|
||||
if (err) {
|
||||
LOG_WRN("USB keyboard report submit failed (%d)", err);
|
||||
submit_hid_report_sent_event(KEYBOARD_REPORT_TYPE_KEYS,
|
||||
event->sequence, true);
|
||||
} else {
|
||||
keyboard_report_in_flight = true;
|
||||
keyboard_in_flight_sequence = event->sequence;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
if (event->report_type == KEYBOARD_REPORT_TYPE_CONSUMER) {
|
||||
if (!hid_ifaces[USB_HID_INTERFACE_CONSUMER].ready) {
|
||||
LOG_DBG("USB consumer interface not ready");
|
||||
return false;
|
||||
}
|
||||
|
||||
if (consumer_report_in_flight) {
|
||||
LOG_WRN("Drop USB consumer report while previous report is in flight");
|
||||
return false;
|
||||
}
|
||||
|
||||
memcpy(consumer_tx_buf, event->dyndata.data, event->dyndata.size);
|
||||
|
||||
err = hid_device_submit_report(
|
||||
hid_ifaces[USB_HID_INTERFACE_CONSUMER].dev,
|
||||
(uint16_t)event->dyndata.size,
|
||||
consumer_tx_buf);
|
||||
if (err) {
|
||||
LOG_WRN("USB consumer report submit failed (%d)", err);
|
||||
submit_hid_report_sent_event(KEYBOARD_REPORT_TYPE_CONSUMER,
|
||||
event->sequence, true);
|
||||
} else {
|
||||
consumer_report_in_flight = true;
|
||||
consumer_in_flight_sequence = event->sequence;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
static bool app_event_handler(const struct app_event_header *aeh)
|
||||
{
|
||||
if (is_hid_tx_report_event(aeh)) {
|
||||
return handle_hid_tx_report_event(cast_hid_tx_report_event(aeh));
|
||||
}
|
||||
|
||||
if (is_usb_device_state_event(aeh)) {
|
||||
return handle_usb_device_state_event(cast_usb_device_state_event(aeh));
|
||||
}
|
||||
|
||||
if (is_usb_prepare_event(aeh)) {
|
||||
return handle_usb_prepare_event(cast_usb_prepare_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, hid_tx_report_event);
|
||||
APP_EVENT_SUBSCRIBE(MODULE, usb_prepare_event);
|
||||
APP_EVENT_SUBSCRIBE(MODULE, usb_device_state_event);
|
||||
APP_EVENT_SUBSCRIBE_EARLY(MODULE, power_down_event);
|
||||
APP_EVENT_SUBSCRIBE(MODULE, wake_up_event);
|
||||
Reference in New Issue
Block a user