Compare commits
12 Commits
dev
...
0a905d280d
| Author | SHA1 | Date | |
|---|---|---|---|
| 0a905d280d | |||
| 79af0eb025 | |||
| bc42a4dd63 | |||
| c4b205b8a1 | |||
| 78a6dc212d | |||
| c342a8d3f0 | |||
| 23e23f63a7 | |||
| ff8f0d81d7 | |||
| 968fb626bb | |||
| 30bc314698 | |||
| d86f0d6b78 | |||
| 15307dfde5 |
@@ -10,6 +10,7 @@ include(nanopb)
|
|||||||
zephyr_include_directories(
|
zephyr_include_directories(
|
||||||
inc
|
inc
|
||||||
inc/events
|
inc/events
|
||||||
|
src
|
||||||
)
|
)
|
||||||
add_subdirectory(drivers)
|
add_subdirectory(drivers)
|
||||||
|
|
||||||
@@ -24,29 +25,37 @@ target_sources(app PRIVATE
|
|||||||
src/ble_adv_uuid16.c
|
src/ble_adv_uuid16.c
|
||||||
src/ble_bas_module.c
|
src/ble_bas_module.c
|
||||||
src/ble_hid_module.c
|
src/ble_hid_module.c
|
||||||
|
src/ble_nus_module.c
|
||||||
src/display_module.c
|
src/display_module.c
|
||||||
src/encoder_module.c
|
src/encoder_module.c
|
||||||
src/hid_flowctrl_module.c
|
src/hid_flowctrl_module.c
|
||||||
src/keyboard_core_module.c
|
src/keyboard_core_module.c
|
||||||
|
src/led_effect/led_effect_registry.c
|
||||||
|
src/led_effect/effects/led_effect_key_fade.c
|
||||||
|
src/led_strip_module.c
|
||||||
|
src/time_sync_module.c
|
||||||
src/ui/ui_main.c
|
src/ui/ui_main.c
|
||||||
src/cdc_wrapper_module.c
|
|
||||||
src/protocol_module.c
|
src/protocol_module.c
|
||||||
src/usb_cdc_module.c
|
src/usb_cdc_module.c
|
||||||
src/usb_device_module.c
|
src/usb_device_module.c
|
||||||
src/usb_hid_module.c
|
src/usb_hid_module.c
|
||||||
src/events/bat_state_event.c
|
src/events/bat_state_event.c
|
||||||
|
src/events/datetime_event.c
|
||||||
src/events/encoder_event.c
|
src/events/encoder_event.c
|
||||||
|
src/events/function_bitmap_state_event.c
|
||||||
|
src/events/function_bitmap_update_event.c
|
||||||
src/events/hid_led_event.c
|
src/events/hid_led_event.c
|
||||||
src/events/hid_report_sent_event.c
|
src/events/hid_report_sent_event.c
|
||||||
src/events/hid_transport_state_event.c
|
src/events/hid_transport_state_event.c
|
||||||
src/events/hid_tx_report_event.c
|
src/events/hid_tx_report_event.c
|
||||||
|
src/events/led_strip_en_event.c
|
||||||
src/mode_switch_module.c
|
src/mode_switch_module.c
|
||||||
src/events/keyboard_hid_report_event.c
|
src/events/keyboard_hid_report_event.c
|
||||||
src/events/mode_switch_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/set_protocol_event.c
|
||||||
src/events/usb_cdc_rx_event.c
|
src/events/theme_rgb_update_event.c
|
||||||
src/events/usb_cdc_tx_event.c
|
src/events/time_sync_event.c
|
||||||
src/events/usb_device_state_event.c
|
src/events/usb_state_event.c
|
||||||
src/events/usb_function_ready_event.c
|
|
||||||
src/events/usb_prepare_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 {
|
pwm0_default: pwm0_default {
|
||||||
group1 {
|
group1 {
|
||||||
psels = <NRF_PSEL(PWM_OUT0, 1, 11)>;
|
psels = <NRF_PSEL(PWM_OUT0, 1, 11)>;
|
||||||
|
|||||||
@@ -2,6 +2,7 @@
|
|||||||
#include <nordic/nrf52840_qiaa.dtsi>
|
#include <nordic/nrf52840_qiaa.dtsi>
|
||||||
#include "mini_keyboard-pinctrl.dtsi"
|
#include "mini_keyboard-pinctrl.dtsi"
|
||||||
#include <zephyr/dt-bindings/adc/adc.h>
|
#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/mipi_dbi/mipi_dbi.h>
|
||||||
#include <zephyr/dt-bindings/pwm/pwm.h>
|
#include <zephyr/dt-bindings/pwm/pwm.h>
|
||||||
|
|
||||||
@@ -14,10 +15,12 @@
|
|||||||
zephyr,flash = &flash0;
|
zephyr,flash = &flash0;
|
||||||
zephyr,code-partition = &slot0_partition;
|
zephyr,code-partition = &slot0_partition;
|
||||||
zephyr,display = &screen_lcd;
|
zephyr,display = &screen_lcd;
|
||||||
|
zephyr,led-strip = &led_strip;
|
||||||
};
|
};
|
||||||
|
|
||||||
aliases {
|
aliases {
|
||||||
led0 = &myled0;
|
led0 = &myled0;
|
||||||
|
led-strip = &led_strip;
|
||||||
qdec0 = &qdec;
|
qdec0 = &qdec;
|
||||||
backlight = &backlight;
|
backlight = &backlight;
|
||||||
};
|
};
|
||||||
@@ -206,6 +209,28 @@
|
|||||||
cs-gpios = <&gpio0 2 GPIO_ACTIVE_LOW>;
|
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 {
|
&pwm0 {
|
||||||
status = "okay";
|
status = "okay";
|
||||||
pinctrl-0 = <&pwm0_default>;
|
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);
|
APP_EVENT_TYPE_DECLARE(bat_state_event);
|
||||||
|
|
||||||
|
static inline void submit_bat_state_event(uint8_t soc, bool charging, bool full)
|
||||||
|
{
|
||||||
|
struct bat_state_event *event = new_bat_state_event();
|
||||||
|
|
||||||
|
event->soc = soc;
|
||||||
|
event->charging = charging;
|
||||||
|
event->full = full;
|
||||||
|
APP_EVENT_SUBMIT(event);
|
||||||
|
}
|
||||||
|
|
||||||
#ifdef __cplusplus
|
#ifdef __cplusplus
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
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);
|
APP_EVENT_TYPE_DECLARE(encoder_event);
|
||||||
|
|
||||||
|
static inline void submit_encoder_event(int8_t detents)
|
||||||
|
{
|
||||||
|
struct encoder_event *event = new_encoder_event();
|
||||||
|
|
||||||
|
event->detents = detents;
|
||||||
|
APP_EVENT_SUBMIT(event);
|
||||||
|
}
|
||||||
|
|
||||||
#ifdef __cplusplus
|
#ifdef __cplusplus
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
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_ */
|
||||||
@@ -18,6 +18,15 @@ struct hid_led_event {
|
|||||||
|
|
||||||
APP_EVENT_TYPE_DECLARE(hid_led_event);
|
APP_EVENT_TYPE_DECLARE(hid_led_event);
|
||||||
|
|
||||||
|
static inline void submit_hid_led_event(enum hid_transport transport, uint8_t led_bm)
|
||||||
|
{
|
||||||
|
struct hid_led_event *event = new_hid_led_event();
|
||||||
|
|
||||||
|
event->transport = transport;
|
||||||
|
event->led_bm = led_bm;
|
||||||
|
APP_EVENT_SUBMIT(event);
|
||||||
|
}
|
||||||
|
|
||||||
#ifdef __cplusplus
|
#ifdef __cplusplus
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
@@ -20,6 +20,19 @@ struct hid_report_sent_event {
|
|||||||
|
|
||||||
APP_EVENT_TYPE_DECLARE(hid_report_sent_event);
|
APP_EVENT_TYPE_DECLARE(hid_report_sent_event);
|
||||||
|
|
||||||
|
static inline void submit_hid_report_sent_event(enum hid_transport transport,
|
||||||
|
enum keyboard_report_type report_type,
|
||||||
|
uint16_t sequence, bool error)
|
||||||
|
{
|
||||||
|
struct hid_report_sent_event *event = new_hid_report_sent_event();
|
||||||
|
|
||||||
|
event->transport = transport;
|
||||||
|
event->report_type = report_type;
|
||||||
|
event->sequence = sequence;
|
||||||
|
event->error = error;
|
||||||
|
APP_EVENT_SUBMIT(event);
|
||||||
|
}
|
||||||
|
|
||||||
#ifdef __cplusplus
|
#ifdef __cplusplus
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
@@ -21,6 +21,20 @@ struct hid_transport_state_event {
|
|||||||
|
|
||||||
APP_EVENT_TYPE_DECLARE(hid_transport_state_event);
|
APP_EVENT_TYPE_DECLARE(hid_transport_state_event);
|
||||||
|
|
||||||
|
static inline void submit_hid_transport_state_event(
|
||||||
|
enum hid_transport transport, bool ready, bool keys_ready,
|
||||||
|
bool consumer_ready, enum keyboard_protocol_mode protocol_mode)
|
||||||
|
{
|
||||||
|
struct hid_transport_state_event *event = new_hid_transport_state_event();
|
||||||
|
|
||||||
|
event->transport = transport;
|
||||||
|
event->ready = ready;
|
||||||
|
event->keys_ready = keys_ready;
|
||||||
|
event->consumer_ready = consumer_ready;
|
||||||
|
event->protocol_mode = protocol_mode;
|
||||||
|
APP_EVENT_SUBMIT(event);
|
||||||
|
}
|
||||||
|
|
||||||
#ifdef __cplusplus
|
#ifdef __cplusplus
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
@@ -1,6 +1,10 @@
|
|||||||
#ifndef BLINKY_HID_TX_REPORT_EVENT_H_
|
#ifndef BLINKY_HID_TX_REPORT_EVENT_H_
|
||||||
#define BLINKY_HID_TX_REPORT_EVENT_H_
|
#define BLINKY_HID_TX_REPORT_EVENT_H_
|
||||||
|
|
||||||
|
#include <errno.h>
|
||||||
|
#include <stddef.h>
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
#include <app_event_manager.h>
|
#include <app_event_manager.h>
|
||||||
#include <app_event_manager_profiler_tracer.h>
|
#include <app_event_manager_profiler_tracer.h>
|
||||||
|
|
||||||
@@ -21,6 +25,31 @@ struct hid_tx_report_event {
|
|||||||
|
|
||||||
APP_EVENT_TYPE_DYNDATA_DECLARE(hid_tx_report_event);
|
APP_EVENT_TYPE_DYNDATA_DECLARE(hid_tx_report_event);
|
||||||
|
|
||||||
|
static inline int submit_hid_tx_report_event(enum hid_transport transport,
|
||||||
|
enum keyboard_report_type report_type,
|
||||||
|
enum keyboard_protocol_mode protocol_mode,
|
||||||
|
uint16_t sequence,
|
||||||
|
const uint8_t *data, size_t size)
|
||||||
|
{
|
||||||
|
struct hid_tx_report_event *event;
|
||||||
|
|
||||||
|
if ((data == NULL) && (size > 0U)) {
|
||||||
|
return -EINVAL;
|
||||||
|
}
|
||||||
|
|
||||||
|
event = new_hid_tx_report_event(size);
|
||||||
|
event->transport = transport;
|
||||||
|
event->report_type = report_type;
|
||||||
|
event->protocol_mode = protocol_mode;
|
||||||
|
event->sequence = sequence;
|
||||||
|
if (size > 0U) {
|
||||||
|
memcpy(event->dyndata.data, data, size);
|
||||||
|
}
|
||||||
|
|
||||||
|
APP_EVENT_SUBMIT(event);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
#ifdef __cplusplus
|
#ifdef __cplusplus
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
@@ -1,6 +1,10 @@
|
|||||||
#ifndef BLINKY_KEYBOARD_HID_REPORT_EVENT_H_
|
#ifndef BLINKY_KEYBOARD_HID_REPORT_EVENT_H_
|
||||||
#define BLINKY_KEYBOARD_HID_REPORT_EVENT_H_
|
#define BLINKY_KEYBOARD_HID_REPORT_EVENT_H_
|
||||||
|
|
||||||
|
#include <errno.h>
|
||||||
|
#include <stddef.h>
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
#include <app_event_manager.h>
|
#include <app_event_manager.h>
|
||||||
#include <app_event_manager_profiler_tracer.h>
|
#include <app_event_manager_profiler_tracer.h>
|
||||||
|
|
||||||
@@ -22,6 +26,30 @@ struct keyboard_hid_report_event {
|
|||||||
|
|
||||||
APP_EVENT_TYPE_DYNDATA_DECLARE(keyboard_hid_report_event);
|
APP_EVENT_TYPE_DYNDATA_DECLARE(keyboard_hid_report_event);
|
||||||
|
|
||||||
|
static inline int submit_keyboard_hid_report_event(
|
||||||
|
enum mode_switch_mode mode, enum keyboard_report_type report_type,
|
||||||
|
enum keyboard_protocol_mode protocol_mode,
|
||||||
|
enum hid_queue_policy queue_policy, const uint8_t *data, size_t size)
|
||||||
|
{
|
||||||
|
struct keyboard_hid_report_event *event;
|
||||||
|
|
||||||
|
if ((data == NULL) && (size > 0U)) {
|
||||||
|
return -EINVAL;
|
||||||
|
}
|
||||||
|
|
||||||
|
event = new_keyboard_hid_report_event(size);
|
||||||
|
event->mode = mode;
|
||||||
|
event->report_type = report_type;
|
||||||
|
event->protocol_mode = protocol_mode;
|
||||||
|
event->queue_policy = queue_policy;
|
||||||
|
if (size > 0U) {
|
||||||
|
memcpy(event->dyndata.data, data, size);
|
||||||
|
}
|
||||||
|
|
||||||
|
APP_EVENT_SUBMIT(event);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
#ifdef __cplusplus
|
#ifdef __cplusplus
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
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);
|
APP_EVENT_TYPE_DECLARE(mode_switch_event);
|
||||||
|
|
||||||
|
static inline void submit_mode_switch_event(enum mode_switch_mode mode,
|
||||||
|
uint16_t voltage_mv)
|
||||||
|
{
|
||||||
|
struct mode_switch_event *event = new_mode_switch_event();
|
||||||
|
|
||||||
|
event->mode = mode;
|
||||||
|
event->voltage_mv = voltage_mv;
|
||||||
|
APP_EVENT_SUBMIT(event);
|
||||||
|
}
|
||||||
|
|
||||||
#ifdef __cplusplus
|
#ifdef __cplusplus
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
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);
|
APP_EVENT_TYPE_DECLARE(set_protocol_event);
|
||||||
|
|
||||||
|
static inline void submit_set_protocol_event(enum hid_transport transport,
|
||||||
|
enum keyboard_protocol_mode protocol_mode)
|
||||||
|
{
|
||||||
|
struct set_protocol_event *event = new_set_protocol_event();
|
||||||
|
|
||||||
|
event->transport = transport;
|
||||||
|
event->protocol_mode = protocol_mode;
|
||||||
|
APP_EVENT_SUBMIT(event);
|
||||||
|
}
|
||||||
|
|
||||||
#ifdef __cplusplus
|
#ifdef __cplusplus
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
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_ */
|
|
||||||
82
inc/events/usb_state_event.h
Normal file
82
inc/events/usb_state_event.h
Normal file
@@ -0,0 +1,82 @@
|
|||||||
|
#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_event_op {
|
||||||
|
USB_STATE_EVENT_OP_SET_BITS,
|
||||||
|
USB_STATE_EVENT_OP_CLEAR_BITS,
|
||||||
|
USB_STATE_EVENT_OP_SNAPSHOT,
|
||||||
|
};
|
||||||
|
|
||||||
|
enum usb_state_flag {
|
||||||
|
USB_STATEF_PREPARE = BIT(0),
|
||||||
|
USB_STATEF_HID_READY = BIT(1),
|
||||||
|
USB_STATEF_CDC_READY = BIT(2),
|
||||||
|
USB_STATEF_STACK_READY = BIT(3),
|
||||||
|
USB_STATEF_POWERED = BIT(4),
|
||||||
|
USB_STATEF_ACTIVE = BIT(5),
|
||||||
|
USB_STATEF_SUSPENDED = BIT(6),
|
||||||
|
USB_STATEF_STACK_RUNNING = BIT(31),
|
||||||
|
};
|
||||||
|
|
||||||
|
struct usb_state_event {
|
||||||
|
struct app_event_header header;
|
||||||
|
const void *src_module_id;
|
||||||
|
const void *sink_module_id;
|
||||||
|
uint8_t op;
|
||||||
|
uint32_t flags;
|
||||||
|
};
|
||||||
|
|
||||||
|
APP_EVENT_TYPE_DECLARE(usb_state_event);
|
||||||
|
|
||||||
|
static inline void submit_usb_state_event(const void *src_module_id,
|
||||||
|
const void *sink_module_id,
|
||||||
|
uint8_t op,
|
||||||
|
uint32_t flags)
|
||||||
|
{
|
||||||
|
struct usb_state_event *event = new_usb_state_event();
|
||||||
|
|
||||||
|
event->src_module_id = src_module_id;
|
||||||
|
event->sink_module_id = sink_module_id;
|
||||||
|
event->op = op;
|
||||||
|
event->flags = flags;
|
||||||
|
APP_EVENT_SUBMIT(event);
|
||||||
|
}
|
||||||
|
|
||||||
|
static inline void submit_usb_state_set(const void *src_module_id,
|
||||||
|
const void *sink_module_id,
|
||||||
|
uint32_t flags)
|
||||||
|
{
|
||||||
|
submit_usb_state_event(src_module_id, sink_module_id,
|
||||||
|
USB_STATE_EVENT_OP_SET_BITS, flags);
|
||||||
|
}
|
||||||
|
|
||||||
|
static inline void submit_usb_state_clear(const void *src_module_id,
|
||||||
|
const void *sink_module_id,
|
||||||
|
uint32_t flags)
|
||||||
|
{
|
||||||
|
submit_usb_state_event(src_module_id, sink_module_id,
|
||||||
|
USB_STATE_EVENT_OP_CLEAR_BITS, flags);
|
||||||
|
}
|
||||||
|
|
||||||
|
static inline void submit_usb_state_snapshot(const void *src_module_id,
|
||||||
|
uint32_t flags)
|
||||||
|
{
|
||||||
|
submit_usb_state_event(src_module_id, NULL,
|
||||||
|
USB_STATE_EVENT_OP_SNAPSHOT, flags);
|
||||||
|
}
|
||||||
|
|
||||||
|
#ifdef __cplusplus
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#endif /* BLINKY_USB_STATE_EVENT_H_ */
|
||||||
@@ -8,6 +8,8 @@ extern "C" {
|
|||||||
#endif
|
#endif
|
||||||
|
|
||||||
#define KEYBOARD_BOOT_REPORT_SIZE 8U
|
#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_USAGE_MAX 0xDFU
|
||||||
#define KEYBOARD_NKRO_BITMAP_BYTES ((KEYBOARD_NKRO_USAGE_MAX + 8U) / 8U)
|
#define KEYBOARD_NKRO_BITMAP_BYTES ((KEYBOARD_NKRO_USAGE_MAX + 8U) / 8U)
|
||||||
#define KEYBOARD_NKRO_REPORT_SIZE (1U + KEYBOARD_NKRO_BITMAP_BYTES)
|
#define KEYBOARD_NKRO_REPORT_SIZE (1U + KEYBOARD_NKRO_BITMAP_BYTES)
|
||||||
|
|||||||
@@ -1,24 +1,25 @@
|
|||||||
#ifndef BLINKY_PROTOCOL_MODULE_H_
|
#ifndef BLINKY_PROTOCOL_MODULE_H_
|
||||||
#define BLINKY_PROTOCOL_MODULE_H_
|
#define BLINKY_PROTOCOL_MODULE_H_
|
||||||
|
|
||||||
|
#include <stdbool.h>
|
||||||
#include <stddef.h>
|
#include <stddef.h>
|
||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
|
|
||||||
|
#include "proto_common.h"
|
||||||
|
|
||||||
#ifdef __cplusplus
|
#ifdef __cplusplus
|
||||||
extern "C" {
|
extern "C" {
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#define CDC_PROTO_TYPE_HELLO_REQ 0x01U
|
int protocol_module_process_message(enum proto_transport transport,
|
||||||
#define CDC_PROTO_TYPE_HELLO_RSP 0x02U
|
|
||||||
|
|
||||||
int protocol_module_process_cdc_packet(uint8_t req_type,
|
|
||||||
const uint8_t *req_payload,
|
const uint8_t *req_payload,
|
||||||
size_t req_payload_len,
|
size_t req_payload_len,
|
||||||
uint8_t *rsp_type,
|
|
||||||
uint8_t *rsp_payload,
|
uint8_t *rsp_payload,
|
||||||
size_t rsp_payload_buf_size,
|
size_t rsp_payload_buf_size,
|
||||||
size_t *rsp_payload_len);
|
size_t *rsp_payload_len);
|
||||||
|
|
||||||
|
void protocol_module_reset_transport_state(enum proto_transport transport);
|
||||||
|
|
||||||
#ifdef __cplusplus
|
#ifdef __cplusplus
|
||||||
}
|
}
|
||||||
#endif
|
#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_ */
|
|
||||||
7
prj.conf
7
prj.conf
@@ -27,6 +27,9 @@ CONFIG_NVS=y
|
|||||||
CONFIG_HEAP_MEM_POOL_SIZE=4096
|
CONFIG_HEAP_MEM_POOL_SIZE=4096
|
||||||
CONFIG_LOG=y
|
CONFIG_LOG=y
|
||||||
CONFIG_ASSERT=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
|
# USB HID next stack
|
||||||
CONFIG_USB_DEVICE_STACK_NEXT=y
|
CONFIG_USB_DEVICE_STACK_NEXT=y
|
||||||
@@ -42,12 +45,16 @@ CONFIG_CDC_ACM_SERIAL_INITIALIZE_AT_BOOT=n
|
|||||||
# BLE
|
# BLE
|
||||||
CONFIG_BT=y
|
CONFIG_BT=y
|
||||||
CONFIG_BT_PERIPHERAL=y
|
CONFIG_BT_PERIPHERAL=y
|
||||||
|
CONFIG_BT_ZEPHYR_NUS=y
|
||||||
CONFIG_BT_SMP=y
|
CONFIG_BT_SMP=y
|
||||||
CONFIG_BT_BONDABLE=y
|
CONFIG_BT_BONDABLE=y
|
||||||
CONFIG_BT_SETTINGS=y
|
CONFIG_BT_SETTINGS=y
|
||||||
CONFIG_BT_MAX_CONN=1
|
CONFIG_BT_MAX_CONN=1
|
||||||
CONFIG_BT_MAX_PAIRED=1
|
CONFIG_BT_MAX_PAIRED=1
|
||||||
CONFIG_BT_ATT_TX_COUNT=5
|
CONFIG_BT_ATT_TX_COUNT=5
|
||||||
|
CONFIG_BT_L2CAP_TX_MTU=65
|
||||||
|
CONFIG_BT_BUF_ACL_RX_SIZE=69
|
||||||
|
CONFIG_BT_BUF_ACL_TX_SIZE=69
|
||||||
CONFIG_BT_CONN_CTX=y
|
CONFIG_BT_CONN_CTX=y
|
||||||
CONFIG_BT_DEVICE_NAME="WH Mini Keyboard"
|
CONFIG_BT_DEVICE_NAME="WH Mini Keyboard"
|
||||||
CONFIG_BT_DEVICE_APPEARANCE=961
|
CONFIG_BT_DEVICE_APPEARANCE=961
|
||||||
|
|||||||
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;
|
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 {
|
message CdcPacketBody {
|
||||||
oneof body {
|
oneof body {
|
||||||
HelloReq hello_req = 1;
|
HelloReq hello_req = 1;
|
||||||
HelloRsp hello_rsp = 2;
|
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);
|
return (uint8_t)(bucket * 10);
|
||||||
}
|
}
|
||||||
|
|
||||||
static void submit_bat_state_event(uint8_t soc, bool charging, bool full)
|
|
||||||
{
|
|
||||||
struct bat_state_event *event;
|
|
||||||
|
|
||||||
if (last_bat_state.valid &&
|
|
||||||
(last_bat_state.soc == soc) &&
|
|
||||||
(last_bat_state.charging == charging) &&
|
|
||||||
(last_bat_state.full == full)) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
last_bat_state.valid = true;
|
|
||||||
last_bat_state.soc = soc;
|
|
||||||
last_bat_state.charging = charging;
|
|
||||||
last_bat_state.full = full;
|
|
||||||
|
|
||||||
event = new_bat_state_event();
|
|
||||||
event->soc = soc;
|
|
||||||
event->charging = charging;
|
|
||||||
event->full = full;
|
|
||||||
|
|
||||||
APP_EVENT_SUBMIT(event);
|
|
||||||
}
|
|
||||||
|
|
||||||
static void battery_sample_fn(struct k_work *work)
|
static void battery_sample_fn(struct k_work *work)
|
||||||
{
|
{
|
||||||
struct ip5306_status pmic_status;
|
struct ip5306_status pmic_status;
|
||||||
@@ -137,9 +113,18 @@ static void battery_sample_fn(struct k_work *work)
|
|||||||
}
|
}
|
||||||
|
|
||||||
voltage_mv = sensor_value_to_mv(&voltage);
|
voltage_mv = sensor_value_to_mv(&voltage);
|
||||||
submit_bat_state_event(battery_soc_from_mv(voltage_mv),
|
uint8_t soc = battery_soc_from_mv(voltage_mv);
|
||||||
pmic_status.charging,
|
|
||||||
pmic_status.full);
|
if (!last_bat_state.valid ||
|
||||||
|
(last_bat_state.soc != soc) ||
|
||||||
|
(last_bat_state.charging != pmic_status.charging) ||
|
||||||
|
(last_bat_state.full != pmic_status.full)) {
|
||||||
|
last_bat_state.valid = true;
|
||||||
|
last_bat_state.soc = soc;
|
||||||
|
last_bat_state.charging = pmic_status.charging;
|
||||||
|
last_bat_state.full = pmic_status.full;
|
||||||
|
submit_bat_state_event(soc, pmic_status.charging, pmic_status.full);
|
||||||
|
}
|
||||||
|
|
||||||
reschedule:
|
reschedule:
|
||||||
if (running) {
|
if (running) {
|
||||||
|
|||||||
@@ -98,54 +98,19 @@ static const uint8_t hid_report_desc[] = {
|
|||||||
0xC0 /* End Collection */
|
0xC0 /* End Collection */
|
||||||
};
|
};
|
||||||
|
|
||||||
static void submit_set_protocol_event(void)
|
static void submit_ble_transport_state_event(void)
|
||||||
{
|
{
|
||||||
struct set_protocol_event *event = new_set_protocol_event();
|
|
||||||
|
|
||||||
event->transport = HID_TRANSPORT_BLE;
|
|
||||||
event->protocol_mode = protocol_mode;
|
|
||||||
APP_EVENT_SUBMIT(event);
|
|
||||||
}
|
|
||||||
|
|
||||||
static void submit_hid_led_event(uint8_t led_bm)
|
|
||||||
{
|
|
||||||
struct hid_led_event *event = new_hid_led_event();
|
|
||||||
|
|
||||||
event->transport = HID_TRANSPORT_BLE;
|
|
||||||
event->led_bm = led_bm;
|
|
||||||
APP_EVENT_SUBMIT(event);
|
|
||||||
}
|
|
||||||
|
|
||||||
static void submit_transport_state_event(void)
|
|
||||||
{
|
|
||||||
struct hid_transport_state_event *event = new_hid_transport_state_event();
|
|
||||||
bool ready = running && secured && (active_conn != NULL);
|
bool ready = running && secured && (active_conn != NULL);
|
||||||
|
|
||||||
event->transport = HID_TRANSPORT_BLE;
|
submit_hid_transport_state_event(
|
||||||
event->ready = ready;
|
HID_TRANSPORT_BLE,
|
||||||
event->protocol_mode = protocol_mode;
|
ready,
|
||||||
event->keys_ready = ready &&
|
ready && ((protocol_mode == KEYBOARD_PROTOCOL_MODE_BOOT) ?
|
||||||
((protocol_mode == KEYBOARD_PROTOCOL_MODE_BOOT) ?
|
|
||||||
boot_keyboard_notify_enabled :
|
boot_keyboard_notify_enabled :
|
||||||
keyboard_report_notify_enabled);
|
keyboard_report_notify_enabled),
|
||||||
event->consumer_ready = ready &&
|
ready && (protocol_mode == KEYBOARD_PROTOCOL_MODE_REPORT) &&
|
||||||
(protocol_mode == KEYBOARD_PROTOCOL_MODE_REPORT) &&
|
consumer_report_notify_enabled,
|
||||||
consumer_report_notify_enabled;
|
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_BLE;
|
|
||||||
event->report_type = report_type;
|
|
||||||
event->sequence = sequence;
|
|
||||||
event->error = error;
|
|
||||||
|
|
||||||
APP_EVENT_SUBMIT(event);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static void input_report_notify_handler(uint8_t report_id, enum bt_hids_notify_evt evt)
|
static void input_report_notify_handler(uint8_t report_id, enum bt_hids_notify_evt evt)
|
||||||
@@ -158,13 +123,13 @@ static void input_report_notify_handler(uint8_t report_id, enum bt_hids_notify_e
|
|||||||
consumer_report_notify_enabled = enabled;
|
consumer_report_notify_enabled = enabled;
|
||||||
}
|
}
|
||||||
|
|
||||||
submit_transport_state_event();
|
submit_ble_transport_state_event();
|
||||||
}
|
}
|
||||||
|
|
||||||
static void boot_keyboard_notify_handler(enum bt_hids_notify_evt evt)
|
static void boot_keyboard_notify_handler(enum bt_hids_notify_evt evt)
|
||||||
{
|
{
|
||||||
boot_keyboard_notify_enabled = (evt == BT_HIDS_CCCD_EVT_NOTIFY_ENABLED);
|
boot_keyboard_notify_enabled = (evt == BT_HIDS_CCCD_EVT_NOTIFY_ENABLED);
|
||||||
submit_transport_state_event();
|
submit_ble_transport_state_event();
|
||||||
}
|
}
|
||||||
|
|
||||||
static void hid_report_complete_cb(struct bt_conn *conn, void *user_data)
|
static void hid_report_complete_cb(struct bt_conn *conn, void *user_data)
|
||||||
@@ -176,7 +141,8 @@ static void hid_report_complete_cb(struct bt_conn *conn, void *user_data)
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
submit_hid_report_sent_event(in_flight.report_type, in_flight.sequence, false);
|
submit_hid_report_sent_event(HID_TRANSPORT_BLE, in_flight.report_type,
|
||||||
|
in_flight.sequence, false);
|
||||||
in_flight.active = false;
|
in_flight.active = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -186,7 +152,7 @@ static void keyboard_led_report_common(struct bt_hids_rep *rep, bool write)
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
submit_hid_led_event(rep->data[0]);
|
submit_hid_led_event(HID_TRANSPORT_BLE, rep->data[0]);
|
||||||
}
|
}
|
||||||
|
|
||||||
static void keyboard_led_report_handler(struct bt_hids_rep *rep,
|
static void keyboard_led_report_handler(struct bt_hids_rep *rep,
|
||||||
@@ -224,8 +190,8 @@ static void pm_evt_handler(enum bt_hids_pm_evt evt, struct bt_conn *conn)
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
submit_set_protocol_event();
|
submit_set_protocol_event(HID_TRANSPORT_BLE, protocol_mode);
|
||||||
submit_transport_state_event();
|
submit_ble_transport_state_event();
|
||||||
}
|
}
|
||||||
|
|
||||||
static int module_init(void)
|
static int module_init(void)
|
||||||
@@ -272,7 +238,7 @@ static int module_start(void)
|
|||||||
}
|
}
|
||||||
|
|
||||||
running = true;
|
running = true;
|
||||||
submit_transport_state_event();
|
submit_ble_transport_state_event();
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
@@ -285,7 +251,7 @@ static void module_pause(void)
|
|||||||
|
|
||||||
in_flight.active = false;
|
in_flight.active = false;
|
||||||
running = false;
|
running = false;
|
||||||
submit_transport_state_event();
|
submit_ble_transport_state_event();
|
||||||
}
|
}
|
||||||
|
|
||||||
static void reset_connection_state(void)
|
static void reset_connection_state(void)
|
||||||
@@ -311,12 +277,12 @@ static bool handle_ble_peer_event(const struct ble_peer_event *event)
|
|||||||
|
|
||||||
active_conn = event->id;
|
active_conn = event->id;
|
||||||
protocol_mode = KEYBOARD_PROTOCOL_MODE_REPORT;
|
protocol_mode = KEYBOARD_PROTOCOL_MODE_REPORT;
|
||||||
submit_set_protocol_event();
|
submit_set_protocol_event(HID_TRANSPORT_BLE, protocol_mode);
|
||||||
err = bt_hids_connected(&hids_obj, event->id);
|
err = bt_hids_connected(&hids_obj, event->id);
|
||||||
if (err) {
|
if (err) {
|
||||||
LOG_ERR("bt_hids_connected failed (%d)", err);
|
LOG_ERR("bt_hids_connected failed (%d)", err);
|
||||||
}
|
}
|
||||||
submit_transport_state_event();
|
submit_ble_transport_state_event();
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
case PEER_STATE_SECURED:
|
case PEER_STATE_SECURED:
|
||||||
@@ -325,7 +291,7 @@ static bool handle_ble_peer_event(const struct ble_peer_event *event)
|
|||||||
}
|
}
|
||||||
|
|
||||||
secured = true;
|
secured = true;
|
||||||
submit_transport_state_event();
|
submit_ble_transport_state_event();
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
case PEER_STATE_DISCONNECTED:
|
case PEER_STATE_DISCONNECTED:
|
||||||
@@ -339,7 +305,7 @@ static bool handle_ble_peer_event(const struct ble_peer_event *event)
|
|||||||
}
|
}
|
||||||
|
|
||||||
reset_connection_state();
|
reset_connection_state();
|
||||||
submit_transport_state_event();
|
submit_ble_transport_state_event();
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
default:
|
default:
|
||||||
@@ -387,7 +353,8 @@ static bool handle_hid_tx_report_event(const struct hid_tx_report_event *event)
|
|||||||
if (err) {
|
if (err) {
|
||||||
in_flight.active = false;
|
in_flight.active = false;
|
||||||
LOG_WRN("BLE keyboard report submit failed (%d)", err);
|
LOG_WRN("BLE keyboard report submit failed (%d)", err);
|
||||||
submit_hid_report_sent_event(KEYBOARD_REPORT_TYPE_KEYS,
|
submit_hid_report_sent_event(HID_TRANSPORT_BLE,
|
||||||
|
KEYBOARD_REPORT_TYPE_KEYS,
|
||||||
event->sequence, true);
|
event->sequence, true);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -413,7 +380,8 @@ static bool handle_hid_tx_report_event(const struct hid_tx_report_event *event)
|
|||||||
if (err) {
|
if (err) {
|
||||||
in_flight.active = false;
|
in_flight.active = false;
|
||||||
LOG_WRN("BLE consumer report submit failed (%d)", err);
|
LOG_WRN("BLE consumer report submit failed (%d)", err);
|
||||||
submit_hid_report_sent_event(KEYBOARD_REPORT_TYPE_CONSUMER,
|
submit_hid_report_sent_event(HID_TRANSPORT_BLE,
|
||||||
|
KEYBOARD_REPORT_TYPE_CONSUMER,
|
||||||
event->sequence, true);
|
event->sequence, true);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
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 <zephyr/logging/log.h>
|
||||||
|
|
||||||
#include "bat_state_event.h"
|
#include "bat_state_event.h"
|
||||||
|
#include "datetime_event.h"
|
||||||
#include "hid_led_event.h"
|
#include "hid_led_event.h"
|
||||||
#include "mode_switch_event.h"
|
#include "mode_switch_event.h"
|
||||||
|
#include "theme_rgb_update_event.h"
|
||||||
|
#include "theme_color.h"
|
||||||
#include "ui/ui_main.h"
|
#include "ui/ui_main.h"
|
||||||
|
|
||||||
LOG_MODULE_REGISTER(MODULE, LOG_LEVEL_INF);
|
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)));
|
DEVICE_DT_GET(DT_PARENT(DT_ALIAS(backlight)));
|
||||||
static const uint32_t backlight_idx = DT_NODE_CHILD_IDX(DT_ALIAS(backlight));
|
static const uint32_t backlight_idx = DT_NODE_CHILD_IDX(DT_ALIAS(backlight));
|
||||||
static struct ui_main_model ui_model = {
|
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),
|
.inactive_border_color = LV_COLOR_MAKE(0x3A, 0x44, 0x52),
|
||||||
.mode = MODE_SWITCH_BLE,
|
.mode = MODE_SWITCH_BLE,
|
||||||
};
|
};
|
||||||
static bool initialized;
|
static bool initialized;
|
||||||
static bool running;
|
static bool running;
|
||||||
static bool lvgl_initialized;
|
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)
|
static int backlight_set(bool on)
|
||||||
{
|
{
|
||||||
@@ -90,7 +97,7 @@ static int module_start(void)
|
|||||||
lvgl_initialized = true;
|
lvgl_initialized = true;
|
||||||
|
|
||||||
lvgl_lock();
|
lvgl_lock();
|
||||||
ui_main_init(&ui_model, "WH Mini", "Hello World");
|
ui_main_init(&ui_model, date_text, time_text);
|
||||||
lvgl_unlock();
|
lvgl_unlock();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -132,7 +139,7 @@ static void refresh_ui(void)
|
|||||||
}
|
}
|
||||||
|
|
||||||
lvgl_lock();
|
lvgl_lock();
|
||||||
ui_main_refresh_all(&ui_model, "WH Mini", "Hello World");
|
ui_main_refresh_all(&ui_model, date_text, time_text);
|
||||||
lvgl_unlock();
|
lvgl_unlock();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -164,6 +171,28 @@ static bool app_event_handler(const struct app_event_header *aeh)
|
|||||||
return false;
|
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)) {
|
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);
|
||||||
int err;
|
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_LISTENER(MODULE, app_event_handler);
|
||||||
APP_EVENT_SUBSCRIBE(MODULE, bat_state_event);
|
APP_EVENT_SUBSCRIBE(MODULE, bat_state_event);
|
||||||
|
APP_EVENT_SUBSCRIBE(MODULE, datetime_event);
|
||||||
APP_EVENT_SUBSCRIBE(MODULE, hid_led_event);
|
APP_EVENT_SUBSCRIBE(MODULE, hid_led_event);
|
||||||
APP_EVENT_SUBSCRIBE(MODULE, module_state_event);
|
APP_EVENT_SUBSCRIBE(MODULE, module_state_event);
|
||||||
APP_EVENT_SUBSCRIBE(MODULE, mode_switch_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_EARLY(MODULE, power_down_event);
|
||||||
APP_EVENT_SUBSCRIBE(MODULE, wake_up_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;
|
return ((int64_t)value->val1 * 1000000LL) + value->val2;
|
||||||
}
|
}
|
||||||
|
|
||||||
static void submit_detents(int32_t detents)
|
static void submit_detents_batched(int32_t detents)
|
||||||
{
|
{
|
||||||
while (detents != 0) {
|
while (detents != 0) {
|
||||||
struct encoder_event *event = new_encoder_event();
|
int8_t event_detents;
|
||||||
|
|
||||||
if (detents > INT8_MAX) {
|
if (detents > INT8_MAX) {
|
||||||
event->detents = INT8_MAX;
|
event_detents = INT8_MAX;
|
||||||
detents -= INT8_MAX;
|
detents -= INT8_MAX;
|
||||||
} else if (detents < INT8_MIN) {
|
} else if (detents < INT8_MIN) {
|
||||||
event->detents = INT8_MIN;
|
event_detents = INT8_MIN;
|
||||||
detents -= INT8_MIN;
|
detents -= INT8_MIN;
|
||||||
} else {
|
} else {
|
||||||
event->detents = (int8_t)detents;
|
event_detents = (int8_t)detents;
|
||||||
detents = 0;
|
detents = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
APP_EVENT_SUBMIT(event);
|
submit_encoder_event(event_detents);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -89,7 +89,7 @@ static void encoder_report_work_handler(struct k_work *work)
|
|||||||
angle_remainder_udeg = total_udeg - ((int64_t)detents * ENCODER_DETENT_UDEG);
|
angle_remainder_udeg = total_udeg - ((int64_t)detents * ENCODER_DETENT_UDEG);
|
||||||
|
|
||||||
if (detents != 0) {
|
if (detents != 0) {
|
||||||
submit_detents(detents);
|
submit_detents_batched(detents);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
27
src/events/datetime_event.c
Normal file
27
src/events/datetime_event.c
Normal file
@@ -0,0 +1,27 @@
|
|||||||
|
#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(
|
||||||
|
APP_EVENT_TYPE_FLAGS_INIT_LOG_ENABLE));
|
||||||
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));
|
||||||
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));
|
|
||||||
54
src/events/usb_state_event.c
Normal file
54
src/events/usb_state_event.c
Normal file
@@ -0,0 +1,54 @@
|
|||||||
|
#include <caf/events/module_state_event.h>
|
||||||
|
|
||||||
|
#include "usb_state_event.h"
|
||||||
|
|
||||||
|
static const char *usb_state_event_op_name(uint8_t op)
|
||||||
|
{
|
||||||
|
switch ((enum usb_state_event_op)op) {
|
||||||
|
case USB_STATE_EVENT_OP_SET_BITS:
|
||||||
|
return "set_bits";
|
||||||
|
case USB_STATE_EVENT_OP_CLEAR_BITS:
|
||||||
|
return "clear_bits";
|
||||||
|
case USB_STATE_EVENT_OP_SNAPSHOT:
|
||||||
|
return "snapshot";
|
||||||
|
default:
|
||||||
|
return "?";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static const char *module_name_or_any(const void *module_id)
|
||||||
|
{
|
||||||
|
return (module_id != NULL) ? module_name_get(module_id) : "*";
|
||||||
|
}
|
||||||
|
|
||||||
|
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,
|
||||||
|
"src:%s sink:%s op:%s flags:0x%08x",
|
||||||
|
module_name_or_any(event->src_module_id),
|
||||||
|
module_name_or_any(event->sink_module_id),
|
||||||
|
usb_state_event_op_name(event->op),
|
||||||
|
event->flags);
|
||||||
|
}
|
||||||
|
|
||||||
|
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->op);
|
||||||
|
nrf_profiler_log_encode_uint32(buf, event->flags);
|
||||||
|
}
|
||||||
|
|
||||||
|
APP_EVENT_INFO_DEFINE(usb_state_event,
|
||||||
|
ENCODE(NRF_PROFILER_ARG_U8, NRF_PROFILER_ARG_U32),
|
||||||
|
ENCODE("op", "flags"),
|
||||||
|
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));
|
||||||
@@ -153,27 +153,6 @@ static bool transport_can_send_report(enum keyboard_report_type report_type)
|
|||||||
return state->consumer_ready;
|
return state->consumer_ready;
|
||||||
}
|
}
|
||||||
|
|
||||||
static void submit_hid_tx_report_event(enum hid_transport transport,
|
|
||||||
enum keyboard_report_type report_type,
|
|
||||||
enum keyboard_protocol_mode protocol_mode,
|
|
||||||
const uint8_t *data, size_t size)
|
|
||||||
{
|
|
||||||
struct hid_tx_report_event *event = new_hid_tx_report_event(size);
|
|
||||||
|
|
||||||
event->transport = transport;
|
|
||||||
event->report_type = report_type;
|
|
||||||
event->protocol_mode = protocol_mode;
|
|
||||||
event->sequence = next_sequence++;
|
|
||||||
memcpy(event->dyndata.data, data, size);
|
|
||||||
|
|
||||||
in_flight.active = true;
|
|
||||||
in_flight.transport = transport;
|
|
||||||
in_flight.report_type = report_type;
|
|
||||||
in_flight.sequence = event->sequence;
|
|
||||||
|
|
||||||
APP_EVENT_SUBMIT(event);
|
|
||||||
}
|
|
||||||
|
|
||||||
static void try_send_next(void)
|
static void try_send_next(void)
|
||||||
{
|
{
|
||||||
struct queued_report queued;
|
struct queued_report queued;
|
||||||
@@ -195,11 +174,14 @@ static void try_send_next(void)
|
|||||||
LOG_WRN("Drop stale keys report after protocol change");
|
LOG_WRN("Drop stale keys report after protocol change");
|
||||||
pending_keys.valid = false;
|
pending_keys.valid = false;
|
||||||
} else {
|
} else {
|
||||||
submit_hid_tx_report_event(transport,
|
in_flight.active = true;
|
||||||
pending_keys.report_type,
|
in_flight.transport = transport;
|
||||||
pending_keys.protocol_mode,
|
in_flight.report_type = pending_keys.report_type;
|
||||||
pending_keys.data,
|
in_flight.sequence = next_sequence++;
|
||||||
pending_keys.size);
|
(void)submit_hid_tx_report_event(
|
||||||
|
transport, pending_keys.report_type,
|
||||||
|
pending_keys.protocol_mode, in_flight.sequence,
|
||||||
|
pending_keys.data, pending_keys.size);
|
||||||
pending_keys.valid = false;
|
pending_keys.valid = false;
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@@ -211,11 +193,14 @@ static void try_send_next(void)
|
|||||||
if (queued.protocol_mode != state->protocol_mode) {
|
if (queued.protocol_mode != state->protocol_mode) {
|
||||||
LOG_WRN("Drop stale consumer report after protocol change");
|
LOG_WRN("Drop stale consumer report after protocol change");
|
||||||
} else {
|
} else {
|
||||||
submit_hid_tx_report_event(transport,
|
in_flight.active = true;
|
||||||
queued.report_type,
|
in_flight.transport = transport;
|
||||||
queued.protocol_mode,
|
in_flight.report_type = queued.report_type;
|
||||||
queued.data,
|
in_flight.sequence = next_sequence++;
|
||||||
queued.size);
|
(void)submit_hid_tx_report_event(
|
||||||
|
transport, queued.report_type,
|
||||||
|
queued.protocol_mode, in_flight.sequence,
|
||||||
|
queued.data, queued.size);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -226,9 +211,14 @@ static void try_send_next(void)
|
|||||||
LOG_WRN("Drop stale latest consumer report after protocol change");
|
LOG_WRN("Drop stale latest consumer report after protocol change");
|
||||||
pending_consumer_latest.valid = false;
|
pending_consumer_latest.valid = false;
|
||||||
} else {
|
} else {
|
||||||
submit_hid_tx_report_event(transport,
|
in_flight.active = true;
|
||||||
pending_consumer_latest.report_type,
|
in_flight.transport = transport;
|
||||||
|
in_flight.report_type = pending_consumer_latest.report_type;
|
||||||
|
in_flight.sequence = next_sequence++;
|
||||||
|
(void)submit_hid_tx_report_event(
|
||||||
|
transport, pending_consumer_latest.report_type,
|
||||||
pending_consumer_latest.protocol_mode,
|
pending_consumer_latest.protocol_mode,
|
||||||
|
in_flight.sequence,
|
||||||
pending_consumer_latest.data,
|
pending_consumer_latest.data,
|
||||||
pending_consumer_latest.size);
|
pending_consumer_latest.size);
|
||||||
pending_consumer_latest.valid = false;
|
pending_consumer_latest.valid = false;
|
||||||
|
|||||||
@@ -15,6 +15,8 @@
|
|||||||
#include <zephyr/sys/util.h>
|
#include <zephyr/sys/util.h>
|
||||||
|
|
||||||
#include "encoder_event.h"
|
#include "encoder_event.h"
|
||||||
|
#include "function_bitmap_state_event.h"
|
||||||
|
#include "function_bitmap_update_event.h"
|
||||||
#include "keyboard_core.h"
|
#include "keyboard_core.h"
|
||||||
#include "keyboard_hid_report_event.h"
|
#include "keyboard_hid_report_event.h"
|
||||||
#include "mode_switch_event.h"
|
#include "mode_switch_event.h"
|
||||||
@@ -39,8 +41,8 @@ struct keymap_entry {
|
|||||||
};
|
};
|
||||||
|
|
||||||
struct keyboard_state {
|
struct keyboard_state {
|
||||||
uint8_t modifiers;
|
uint8_t pressed_usage_bitmap[KEYBOARD_PROTOCOL_BITMAP_BYTES];
|
||||||
uint8_t keys_bitmap[KEYBOARD_NKRO_BITMAP_BYTES];
|
uint8_t function_pressed_bitmap[KEYBOARD_PROTOCOL_BITMAP_BYTES];
|
||||||
uint32_t consumer_bits;
|
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_state keyboard_state;
|
||||||
static struct keyboard_reports_cache reports_cache;
|
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] = {
|
static enum keyboard_protocol_mode transport_protocol_modes[HID_TRANSPORT_COUNT] = {
|
||||||
[HID_TRANSPORT_USB] = KEYBOARD_PROTOCOL_MODE_REPORT,
|
[HID_TRANSPORT_USB] = KEYBOARD_PROTOCOL_MODE_REPORT,
|
||||||
[HID_TRANSPORT_BLE] = 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;
|
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,
|
if ((byte_idx == NULL) || (bit_idx == NULL)) {
|
||||||
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) {
|
|
||||||
return false;
|
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;
|
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);
|
LOG_WRN("Unsupported keyboard usage 0x%04x", usage_id);
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
uint8_t byte_idx = usage_id / 8U;
|
was_pressed = (bitmap[byte_idx] & BIT(bit_idx)) != 0U;
|
||||||
uint8_t bit_idx = usage_id % 8U;
|
|
||||||
bool was_pressed = (keyboard_state.keys_bitmap[byte_idx] & BIT(bit_idx)) != 0U;
|
|
||||||
|
|
||||||
if (was_pressed == pressed) {
|
if (was_pressed == pressed) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
WRITE_BIT(keyboard_state.keys_bitmap[byte_idx], bit_idx, pressed);
|
WRITE_BIT(bitmap[byte_idx], bit_idx, pressed);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -202,6 +226,11 @@ static void keyboard_state_clear(void)
|
|||||||
memset(&keyboard_state, 0, sizeof(keyboard_state));
|
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)
|
static void reports_cache_invalidate(void)
|
||||||
{
|
{
|
||||||
reports_cache.boot_valid = false;
|
reports_cache.boot_valid = false;
|
||||||
@@ -209,19 +238,31 @@ static void reports_cache_invalidate(void)
|
|||||||
reports_cache.consumer_valid = false;
|
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])
|
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;
|
size_t key_count = 0;
|
||||||
|
|
||||||
|
build_effective_hid_bitmap(effective_hid_bitmap);
|
||||||
memset(report, 0, KEYBOARD_BOOT_REPORT_SIZE);
|
memset(report, 0, KEYBOARD_BOOT_REPORT_SIZE);
|
||||||
report[0] = keyboard_state.modifiers;
|
report[0] = effective_hid_bitmap[0];
|
||||||
report[1] = KEYBOARD_BOOT_RESERVED_BYTE;
|
report[1] = KEYBOARD_BOOT_RESERVED_BYTE;
|
||||||
|
|
||||||
for (uint16_t usage_id = 0; usage_id <= KEYBOARD_NKRO_USAGE_MAX; usage_id++) {
|
for (uint16_t usage_id = 0; usage_id <= KEYBOARD_NKRO_USAGE_MAX; usage_id++) {
|
||||||
uint8_t byte_idx = usage_id / 8U;
|
uint8_t byte_idx;
|
||||||
uint8_t bit_idx = usage_id % 8U;
|
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;
|
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])
|
static void build_nkro_report(uint8_t report[KEYBOARD_NKRO_REPORT_SIZE])
|
||||||
{
|
{
|
||||||
report[0] = keyboard_state.modifiers;
|
uint8_t effective_hid_bitmap[KEYBOARD_PROTOCOL_BITMAP_BYTES];
|
||||||
memcpy(&report[1], keyboard_state.keys_bitmap, KEYBOARD_NKRO_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)
|
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);
|
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)
|
static void submit_consumer_fifo_frame(uint16_t usage_id)
|
||||||
{
|
{
|
||||||
uint8_t report_buf[KEYBOARD_CONSUMER_REPORT_SIZE];
|
uint8_t report_buf[KEYBOARD_CONSUMER_REPORT_SIZE];
|
||||||
@@ -286,10 +313,9 @@ static void submit_consumer_fifo_frame(uint16_t usage_id)
|
|||||||
}
|
}
|
||||||
|
|
||||||
sys_put_le16(usage_id, report_buf);
|
sys_put_le16(usage_id, report_buf);
|
||||||
submit_keyboard_report_event(KEYBOARD_REPORT_TYPE_CONSUMER,
|
(void)submit_keyboard_hid_report_event(
|
||||||
HID_QUEUE_POLICY_FIFO,
|
current_mode, KEYBOARD_REPORT_TYPE_CONSUMER, protocol_mode,
|
||||||
report_buf,
|
HID_QUEUE_POLICY_FIFO, report_buf, KEYBOARD_CONSUMER_REPORT_SIZE);
|
||||||
KEYBOARD_CONSUMER_REPORT_SIZE);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static void submit_consumer_pulse_frames(enum keyboard_consumer_control control_id,
|
static void submit_consumer_pulse_frames(enum keyboard_consumer_control control_id,
|
||||||
@@ -349,10 +375,9 @@ static void emit_keys_report(bool force)
|
|||||||
memcpy(cache_buf, report_buf, report_size);
|
memcpy(cache_buf, report_buf, report_size);
|
||||||
*cache_valid = true;
|
*cache_valid = true;
|
||||||
|
|
||||||
submit_keyboard_report_event(KEYBOARD_REPORT_TYPE_KEYS,
|
(void)submit_keyboard_hid_report_event(
|
||||||
HID_QUEUE_POLICY_LATEST,
|
current_mode, KEYBOARD_REPORT_TYPE_KEYS, protocol_mode,
|
||||||
report_buf,
|
HID_QUEUE_POLICY_LATEST, report_buf, report_size);
|
||||||
report_size);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static void emit_consumer_report(bool force)
|
static void emit_consumer_report(bool force)
|
||||||
@@ -374,9 +399,9 @@ static void emit_consumer_report(bool force)
|
|||||||
memcpy(reports_cache.consumer_report, report_buf, KEYBOARD_CONSUMER_REPORT_SIZE);
|
memcpy(reports_cache.consumer_report, report_buf, KEYBOARD_CONSUMER_REPORT_SIZE);
|
||||||
reports_cache.consumer_valid = true;
|
reports_cache.consumer_valid = true;
|
||||||
|
|
||||||
submit_keyboard_report_event(KEYBOARD_REPORT_TYPE_CONSUMER,
|
(void)submit_keyboard_hid_report_event(
|
||||||
HID_QUEUE_POLICY_LATEST,
|
current_mode, KEYBOARD_REPORT_TYPE_CONSUMER, protocol_mode,
|
||||||
report_buf,
|
HID_QUEUE_POLICY_LATEST, report_buf,
|
||||||
KEYBOARD_CONSUMER_REPORT_SIZE);
|
KEYBOARD_CONSUMER_REPORT_SIZE);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -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)
|
static void emit_release_reports(enum mode_switch_mode mode)
|
||||||
{
|
{
|
||||||
struct keyboard_hid_report_event *event;
|
|
||||||
uint8_t keys_report[KEYBOARD_NKRO_REPORT_SIZE] = { 0 };
|
uint8_t keys_report[KEYBOARD_NKRO_REPORT_SIZE] = { 0 };
|
||||||
uint8_t consumer_report[KEYBOARD_CONSUMER_REPORT_SIZE] = { 0 };
|
uint8_t consumer_report[KEYBOARD_CONSUMER_REPORT_SIZE] = { 0 };
|
||||||
enum keyboard_protocol_mode protocol_mode = active_protocol_mode_get();
|
enum keyboard_protocol_mode protocol_mode = active_protocol_mode_get();
|
||||||
@@ -399,21 +428,15 @@ static void emit_release_reports(enum mode_switch_mode mode)
|
|||||||
(protocol_mode == KEYBOARD_PROTOCOL_MODE_BOOT) ?
|
(protocol_mode == KEYBOARD_PROTOCOL_MODE_BOOT) ?
|
||||||
KEYBOARD_BOOT_REPORT_SIZE : KEYBOARD_NKRO_REPORT_SIZE;
|
KEYBOARD_BOOT_REPORT_SIZE : KEYBOARD_NKRO_REPORT_SIZE;
|
||||||
|
|
||||||
event = new_keyboard_hid_report_event(keys_report_size);
|
(void)submit_keyboard_hid_report_event(
|
||||||
event->mode = mode;
|
mode, KEYBOARD_REPORT_TYPE_KEYS, protocol_mode,
|
||||||
event->report_type = KEYBOARD_REPORT_TYPE_KEYS;
|
HID_QUEUE_POLICY_LATEST, keys_report, keys_report_size);
|
||||||
event->protocol_mode = protocol_mode;
|
|
||||||
memcpy(event->dyndata.data, keys_report, keys_report_size);
|
|
||||||
APP_EVENT_SUBMIT(event);
|
|
||||||
|
|
||||||
if (protocol_mode != KEYBOARD_PROTOCOL_MODE_BOOT) {
|
if (protocol_mode != KEYBOARD_PROTOCOL_MODE_BOOT) {
|
||||||
event = new_keyboard_hid_report_event(KEYBOARD_CONSUMER_REPORT_SIZE);
|
(void)submit_keyboard_hid_report_event(
|
||||||
event->mode = mode;
|
mode, KEYBOARD_REPORT_TYPE_CONSUMER, protocol_mode,
|
||||||
event->report_type = KEYBOARD_REPORT_TYPE_CONSUMER;
|
HID_QUEUE_POLICY_LATEST, consumer_report,
|
||||||
event->protocol_mode = protocol_mode;
|
|
||||||
memcpy(event->dyndata.data, consumer_report,
|
|
||||||
KEYBOARD_CONSUMER_REPORT_SIZE);
|
KEYBOARD_CONSUMER_REPORT_SIZE);
|
||||||
APP_EVENT_SUBMIT(event);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -421,6 +444,7 @@ static int module_init(void)
|
|||||||
{
|
{
|
||||||
keyboard_state_clear();
|
keyboard_state_clear();
|
||||||
reports_cache_invalidate();
|
reports_cache_invalidate();
|
||||||
|
function_usage_mask_clear();
|
||||||
mode_valid = false;
|
mode_valid = false;
|
||||||
transport_protocol_modes[HID_TRANSPORT_USB] =
|
transport_protocol_modes[HID_TRANSPORT_USB] =
|
||||||
KEYBOARD_PROTOCOL_MODE_REPORT;
|
KEYBOARD_PROTOCOL_MODE_REPORT;
|
||||||
@@ -449,6 +473,7 @@ static void module_pause(void)
|
|||||||
if (mode_valid) {
|
if (mode_valid) {
|
||||||
emit_release_reports(current_mode);
|
emit_release_reports(current_mode);
|
||||||
}
|
}
|
||||||
|
emit_function_state_event();
|
||||||
|
|
||||||
keyboard_state_clear();
|
keyboard_state_clear();
|
||||||
reports_cache_invalidate();
|
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) {
|
if (entry->usage_type == KEY_USAGE_TYPE_KEYBOARD) {
|
||||||
changed = keyboard_key_update(entry->usage_id, event->pressed);
|
bool routed_to_function;
|
||||||
if (changed) {
|
|
||||||
|
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);
|
emit_keys_report(false);
|
||||||
}
|
}
|
||||||
} else {
|
} 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);
|
mode_changed = mode_valid && (current_mode != event->mode);
|
||||||
if (mode_changed) {
|
if (mode_changed) {
|
||||||
emit_release_reports(current_mode);
|
emit_release_reports(current_mode);
|
||||||
|
emit_function_state_event();
|
||||||
keyboard_state_clear();
|
keyboard_state_clear();
|
||||||
reports_cache_invalidate();
|
reports_cache_invalidate();
|
||||||
}
|
}
|
||||||
@@ -526,6 +574,13 @@ static bool handle_encoder_event(const struct encoder_event *event)
|
|||||||
return false;
|
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)
|
static bool app_event_handler(const struct app_event_header *aeh)
|
||||||
{
|
{
|
||||||
if (is_button_event(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));
|
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)) {
|
if (is_set_protocol_event(aeh)) {
|
||||||
const struct set_protocol_event *event = cast_set_protocol_event(aeh);
|
const struct set_protocol_event *event = cast_set_protocol_event(aeh);
|
||||||
enum hid_transport active_transport;
|
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_LISTENER(MODULE, app_event_handler);
|
||||||
APP_EVENT_SUBSCRIBE(MODULE, button_event);
|
APP_EVENT_SUBSCRIBE(MODULE, button_event);
|
||||||
APP_EVENT_SUBSCRIBE(MODULE, encoder_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, set_protocol_event);
|
||||||
APP_EVENT_SUBSCRIBE(MODULE, mode_switch_event);
|
APP_EVENT_SUBSCRIBE(MODULE, mode_switch_event);
|
||||||
APP_EVENT_SUBSCRIBE(MODULE, module_state_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);
|
||||||
@@ -91,11 +91,7 @@ static void mode_switch_sample_fn(struct k_work *work)
|
|||||||
enum mode_switch_mode mode = detect_mode(sample_mv);
|
enum mode_switch_mode mode = detect_mode(sample_mv);
|
||||||
|
|
||||||
if (force_report || !mode_valid || (mode != last_mode)) {
|
if (force_report || !mode_valid || (mode != last_mode)) {
|
||||||
struct mode_switch_event *event = new_mode_switch_event();
|
submit_mode_switch_event(mode, sample_mv);
|
||||||
|
|
||||||
event->mode = mode;
|
|
||||||
event->voltage_mv = sample_mv;
|
|
||||||
APP_EVENT_SUBMIT(event);
|
|
||||||
|
|
||||||
last_mode = mode;
|
last_mode = mode;
|
||||||
mode_valid = true;
|
mode_valid = true;
|
||||||
|
|||||||
@@ -2,36 +2,44 @@
|
|||||||
#include <stdbool.h>
|
#include <stdbool.h>
|
||||||
#include <stddef.h>
|
#include <stddef.h>
|
||||||
#include <stdint.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/logging/log.h>
|
||||||
|
#include <zephyr/sys/util.h>
|
||||||
|
|
||||||
#include <pb_decode.h>
|
#include <pb_decode.h>
|
||||||
#include <pb_encode.h>
|
#include <pb_encode.h>
|
||||||
|
|
||||||
#include <proto/device_comm.pb.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 "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_VERSION 1U
|
||||||
#define PROTOCOL_VENDOR_ID 0x1915U
|
#define PROTOCOL_VENDOR_ID 0x1915U
|
||||||
#define PROTOCOL_PRODUCT_ID 0x52F0U
|
#define PROTOCOL_PRODUCT_ID 0x52F0U
|
||||||
#define PROTOCOL_FIRMWARE_MAJOR 0U
|
#define PROTOCOL_FIRMWARE_MAJOR 0U
|
||||||
#define PROTOCOL_FIRMWARE_MINOR 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)
|
static bool initialized;
|
||||||
{
|
static bool running;
|
||||||
switch (type) {
|
static bool hello_done[PROTO_TRANSPORT_COUNT];
|
||||||
case CDC_PROTO_TYPE_HELLO_REQ:
|
|
||||||
return body->which_body == CdcPacketBody_hello_req_tag;
|
|
||||||
case CDC_PROTO_TYPE_HELLO_RSP:
|
|
||||||
return body->which_body == CdcPacketBody_hello_rsp_tag;
|
|
||||||
default:
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
static int decode_body(const uint8_t *payload, size_t payload_len,
|
static int decode_body(const uint8_t *payload, size_t payload_len,
|
||||||
CdcPacketBody *body)
|
CdcPacketBody *body)
|
||||||
@@ -53,15 +61,29 @@ static int decode_body(const uint8_t *payload, size_t payload_len,
|
|||||||
return 0;
|
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,
|
static int encode_hello_rsp(uint8_t *rsp_payload, size_t rsp_payload_buf_size,
|
||||||
size_t *rsp_payload_len)
|
size_t *rsp_payload_len)
|
||||||
{
|
{
|
||||||
CdcPacketBody body = CdcPacketBody_init_zero;
|
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.which_body = CdcPacketBody_hello_rsp_tag;
|
||||||
body.body.hello_rsp.protocol_version = PROTOCOL_VERSION;
|
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.firmware_minor = PROTOCOL_FIRMWARE_MINOR;
|
||||||
body.body.hello_rsp.capability_flags = PROTOCOL_CAPABILITY_FLAGS;
|
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)) {
|
static int encode_ack(uint8_t acked_type, uint8_t *rsp_payload,
|
||||||
LOG_WRN("pb_encode failed: %s", PB_GET_ERROR(&stream));
|
size_t rsp_payload_buf_size, size_t *rsp_payload_len)
|
||||||
return -EIO;
|
{
|
||||||
|
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;
|
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,
|
const uint8_t *req_payload,
|
||||||
size_t req_payload_len,
|
size_t req_payload_len,
|
||||||
uint8_t *rsp_type,
|
|
||||||
uint8_t *rsp_payload,
|
uint8_t *rsp_payload,
|
||||||
size_t rsp_payload_buf_size,
|
size_t rsp_payload_buf_size,
|
||||||
size_t *rsp_payload_len)
|
size_t *rsp_payload_len)
|
||||||
@@ -93,41 +200,285 @@ int protocol_module_process_cdc_packet(uint8_t req_type,
|
|||||||
CdcPacketBody body;
|
CdcPacketBody body;
|
||||||
int err;
|
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;
|
return -EINVAL;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (!running) {
|
||||||
|
return -EAGAIN;
|
||||||
|
}
|
||||||
|
|
||||||
err = decode_body(req_payload, req_payload_len, &body);
|
err = decode_body(req_payload, req_payload_len, &body);
|
||||||
if (err) {
|
if (err) {
|
||||||
return err;
|
return err;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!type_matches_body(req_type, &body)) {
|
switch (body.which_body) {
|
||||||
LOG_WRN("CDC type/body mismatch type:0x%02x body_case:%d",
|
case CdcPacketBody_hello_req_tag:
|
||||||
req_type, body.which_body);
|
LOG_INF("HelloReq transport:%u protocol_version:%u",
|
||||||
return -EBADMSG;
|
transport, body.body.hello_req.protocol_version);
|
||||||
}
|
|
||||||
|
|
||||||
switch (req_type) {
|
|
||||||
case CDC_PROTO_TYPE_HELLO_REQ:
|
|
||||||
LOG_INF("HelloReq protocol_version:%u",
|
|
||||||
body.body.hello_req.protocol_version);
|
|
||||||
|
|
||||||
if (body.body.hello_req.protocol_version != PROTOCOL_VERSION) {
|
if (body.body.hello_req.protocol_version != PROTOCOL_VERSION) {
|
||||||
LOG_WRN("Unexpected protocol version:%u",
|
LOG_WRN("Unexpected protocol version:%u",
|
||||||
body.body.hello_req.protocol_version);
|
body.body.hello_req.protocol_version);
|
||||||
}
|
}
|
||||||
|
|
||||||
err = encode_hello_rsp(rsp_payload, rsp_payload_buf_size, rsp_payload_len);
|
hello_done[transport] = true;
|
||||||
if (err) {
|
return encode_hello_rsp(rsp_payload, rsp_payload_buf_size, rsp_payload_len);
|
||||||
return err;
|
|
||||||
|
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;
|
if (body.body.bitmap.usage_bitmap.size != KEYBOARD_PROTOCOL_BITMAP_BYTES) {
|
||||||
return 0;
|
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:
|
default:
|
||||||
LOG_WRN("Unsupported CDC protocol type:0x%02x", req_type);
|
LOG_WRN("Unsupported protobuf body case %d", body.which_body);
|
||||||
return -ENOTSUP;
|
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_clean(screen);
|
||||||
lv_obj_set_style_bg_color(screen, lv_color_hex(0x0F1115), 0);
|
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_bg_opa(screen, LV_OPA_COVER, 0);
|
||||||
lv_obj_set_style_text_color(screen, lv_color_white(), 0);
|
lv_obj_set_style_text_color(screen, lv_color_white(), 0);
|
||||||
lv_obj_set_style_pad_all(screen, 0, 0);
|
lv_obj_set_style_pad_all(screen, 0, 0);
|
||||||
|
|||||||
@@ -16,19 +16,19 @@
|
|||||||
#include <zephyr/sys/ring_buffer.h>
|
#include <zephyr/sys/ring_buffer.h>
|
||||||
#include <zephyr/sys/util.h>
|
#include <zephyr/sys/util.h>
|
||||||
|
|
||||||
#include "usb_cdc_rx_event.h"
|
#include "proto_rx_event.h"
|
||||||
#include "usb_cdc_tx_event.h"
|
#include "proto_tx_event.h"
|
||||||
#include "usb_function_ready_event.h"
|
#include "protocol_module.h"
|
||||||
#include "usb_prepare_event.h"
|
#include "usb_state_event.h"
|
||||||
#include "usb_device_module.h"
|
|
||||||
#include "usb_device_state_event.h"
|
|
||||||
|
|
||||||
LOG_MODULE_REGISTER(MODULE, LOG_LEVEL_INF);
|
LOG_MODULE_REGISTER(MODULE, LOG_LEVEL_INF);
|
||||||
|
|
||||||
#define USB_CDC_RX_RING_BUF_SIZE 256
|
#define USB_CDC_RX_RING_BUF_SIZE 256
|
||||||
#define USB_CDC_TX_RING_BUF_SIZE 256
|
#define USB_CDC_TX_RING_BUF_SIZE 256
|
||||||
#define USB_CDC_RX_CHUNK_SIZE 32
|
#define USB_CDC_RX_CHUNK_SIZE 32
|
||||||
|
#define USB_CDC_PROTO_RX_BUF_SIZE 128
|
||||||
#define USB_CDC_CONTROL_POLL_INTERVAL K_MSEC(100)
|
#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
|
#define USB_CDC_EXPECTED_BAUDRATE 115200U
|
||||||
|
|
||||||
static const struct device *const cdc_dev = DEVICE_DT_GET_ONE(zephyr_cdc_acm_uart);
|
static const struct device *const cdc_dev = DEVICE_DT_GET_ONE(zephyr_cdc_acm_uart);
|
||||||
@@ -39,27 +39,21 @@ static struct ring_buf rx_ringbuf;
|
|||||||
static struct ring_buf tx_ringbuf;
|
static struct ring_buf tx_ringbuf;
|
||||||
static struct k_work rx_work;
|
static struct k_work rx_work;
|
||||||
static struct k_work_delayable control_work;
|
static struct k_work_delayable control_work;
|
||||||
|
static struct k_work_delayable rx_flush_work;
|
||||||
static bool initialized;
|
static bool initialized;
|
||||||
static bool running;
|
static bool running;
|
||||||
static bool usb_active;
|
static bool usb_active;
|
||||||
static bool usb_function_prepared;
|
static bool usb_function_prepared;
|
||||||
static bool dtr_ready;
|
static bool dtr_ready;
|
||||||
static bool rx_enabled;
|
static bool rx_enabled;
|
||||||
|
static uint8_t proto_rx_buf[USB_CDC_PROTO_RX_BUF_SIZE];
|
||||||
|
static size_t proto_rx_len;
|
||||||
|
|
||||||
static void submit_usb_cdc_rx_event(const uint8_t *data, size_t len)
|
static bool is_usb_owner_snapshot(const struct usb_state_event *event)
|
||||||
{
|
{
|
||||||
struct usb_cdc_rx_event *event = new_usb_cdc_rx_event(len);
|
return (event->op == USB_STATE_EVENT_OP_SNAPSHOT) &&
|
||||||
|
(event->src_module_id == MODULE_ID(usb_device_module)) &&
|
||||||
memcpy(event->dyndata.data, data, len);
|
(event->sink_module_id == NULL);
|
||||||
APP_EVENT_SUBMIT(event);
|
|
||||||
}
|
|
||||||
|
|
||||||
static void submit_usb_function_ready_event(void)
|
|
||||||
{
|
|
||||||
struct usb_function_ready_event *event = new_usb_function_ready_event();
|
|
||||||
|
|
||||||
event->function_mask = USB_FUNCTION_CDC_ACM;
|
|
||||||
APP_EVENT_SUBMIT(event);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static void reset_ring_buffers(void)
|
static void reset_ring_buffers(void)
|
||||||
@@ -78,6 +72,8 @@ static void disable_uart_io(void)
|
|||||||
uart_irq_tx_disable(cdc_dev);
|
uart_irq_tx_disable(cdc_dev);
|
||||||
rx_enabled = false;
|
rx_enabled = false;
|
||||||
dtr_ready = false;
|
dtr_ready = false;
|
||||||
|
proto_rx_len = 0U;
|
||||||
|
k_work_cancel_delayable(&rx_flush_work);
|
||||||
reset_ring_buffers();
|
reset_ring_buffers();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -145,8 +141,30 @@ static void rx_work_handler(struct k_work *work)
|
|||||||
return;
|
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)
|
static void control_work_handler(struct k_work *work)
|
||||||
@@ -262,8 +280,10 @@ static int module_init(void)
|
|||||||
reset_ring_buffers();
|
reset_ring_buffers();
|
||||||
k_work_init(&rx_work, rx_work_handler);
|
k_work_init(&rx_work, rx_work_handler);
|
||||||
k_work_init_delayable(&control_work, control_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);
|
uart_irq_callback_set(cdc_dev, cdc_interrupt_handler);
|
||||||
usb_function_prepared = false;
|
usb_function_prepared = false;
|
||||||
|
proto_rx_len = 0U;
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -288,26 +308,26 @@ static void module_pause(void)
|
|||||||
}
|
}
|
||||||
|
|
||||||
k_work_cancel_delayable(&control_work);
|
k_work_cancel_delayable(&control_work);
|
||||||
|
k_work_cancel_delayable(&rx_flush_work);
|
||||||
disable_uart_io();
|
disable_uart_io();
|
||||||
running = false;
|
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 (!is_usb_owner_snapshot(event)) {
|
||||||
|
|
||||||
if (!running || usb_function_prepared) {
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (running && !usb_function_prepared &&
|
||||||
|
((event->flags & USB_STATEF_PREPARE) != 0U)) {
|
||||||
usb_function_prepared = true;
|
usb_function_prepared = true;
|
||||||
submit_usb_function_ready_event();
|
submit_usb_state_set(MODULE_ID(MODULE),
|
||||||
return false;
|
MODULE_ID(usb_device_module),
|
||||||
}
|
USB_STATEF_CDC_READY);
|
||||||
|
}
|
||||||
|
|
||||||
static bool handle_usb_device_state_event(const struct usb_device_state_event *event)
|
bool new_usb_active = (event->flags & USB_STATEF_ACTIVE) != 0U;
|
||||||
{
|
|
||||||
bool new_usb_active = (event->state == USB_DEVICE_STATE_ACTIVE);
|
|
||||||
|
|
||||||
if (new_usb_active == usb_active) {
|
if (new_usb_active == usb_active) {
|
||||||
return false;
|
return false;
|
||||||
@@ -317,6 +337,8 @@ static bool handle_usb_device_state_event(const struct usb_device_state_event *e
|
|||||||
|
|
||||||
if (!usb_active) {
|
if (!usb_active) {
|
||||||
k_work_cancel_delayable(&control_work);
|
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();
|
disable_uart_io();
|
||||||
} else if (running) {
|
} else if (running) {
|
||||||
k_work_reschedule(&control_work, K_NO_WAIT);
|
k_work_reschedule(&control_work, K_NO_WAIT);
|
||||||
@@ -325,11 +347,15 @@ static bool handle_usb_device_state_event(const struct usb_device_state_event *e
|
|||||||
return false;
|
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;
|
uint32_t written;
|
||||||
unsigned int key;
|
unsigned int key;
|
||||||
|
|
||||||
|
if (event->transport != PROTO_TRANSPORT_USB_CDC) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
if (!running || !usb_active || !dtr_ready) {
|
if (!running || !usb_active || !dtr_ready) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
@@ -352,16 +378,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)
|
static bool app_event_handler(const struct app_event_header *aeh)
|
||||||
{
|
{
|
||||||
if (is_usb_device_state_event(aeh)) {
|
if (is_usb_state_event(aeh)) {
|
||||||
return handle_usb_device_state_event(cast_usb_device_state_event(aeh));
|
return handle_usb_state_event(cast_usb_state_event(aeh));
|
||||||
}
|
}
|
||||||
|
|
||||||
if (is_usb_prepare_event(aeh)) {
|
if (is_proto_tx_event(aeh)) {
|
||||||
return handle_usb_prepare_event(cast_usb_prepare_event(aeh));
|
return handle_proto_tx_event(cast_proto_tx_event(aeh));
|
||||||
}
|
|
||||||
|
|
||||||
if (is_usb_cdc_tx_event(aeh)) {
|
|
||||||
return handle_usb_cdc_tx_event(cast_usb_cdc_tx_event(aeh));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (is_module_state_event(aeh)) {
|
if (is_module_state_event(aeh)) {
|
||||||
@@ -419,8 +441,7 @@ static bool app_event_handler(const struct app_event_header *aeh)
|
|||||||
|
|
||||||
APP_EVENT_LISTENER(MODULE, app_event_handler);
|
APP_EVENT_LISTENER(MODULE, app_event_handler);
|
||||||
APP_EVENT_SUBSCRIBE(MODULE, module_state_event);
|
APP_EVENT_SUBSCRIBE(MODULE, module_state_event);
|
||||||
APP_EVENT_SUBSCRIBE(MODULE, usb_prepare_event);
|
APP_EVENT_SUBSCRIBE(MODULE, proto_tx_event);
|
||||||
APP_EVENT_SUBSCRIBE(MODULE, usb_device_state_event);
|
APP_EVENT_SUBSCRIBE(MODULE, usb_state_event);
|
||||||
APP_EVENT_SUBSCRIBE(MODULE, usb_cdc_tx_event);
|
|
||||||
APP_EVENT_SUBSCRIBE_EARLY(MODULE, power_down_event);
|
APP_EVENT_SUBSCRIBE_EARLY(MODULE, power_down_event);
|
||||||
APP_EVENT_SUBSCRIBE(MODULE, wake_up_event);
|
APP_EVENT_SUBSCRIBE(MODULE, wake_up_event);
|
||||||
|
|||||||
@@ -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);
|
|
||||||
@@ -13,10 +13,7 @@
|
|||||||
|
|
||||||
#include <caf/events/power_manager_event.h>
|
#include <caf/events/power_manager_event.h>
|
||||||
|
|
||||||
#include "usb_device_module.h"
|
#include "usb_state_event.h"
|
||||||
#include "usb_device_state_event.h"
|
|
||||||
#include "usb_function_ready_event.h"
|
|
||||||
#include "usb_prepare_event.h"
|
|
||||||
|
|
||||||
LOG_MODULE_REGISTER(MODULE, LOG_LEVEL_INF);
|
LOG_MODULE_REGISTER(MODULE, LOG_LEVEL_INF);
|
||||||
|
|
||||||
@@ -24,7 +21,7 @@ LOG_MODULE_REGISTER(MODULE, LOG_LEVEL_INF);
|
|||||||
#define USB_DEVICE_PID 0x52F0
|
#define USB_DEVICE_PID 0x52F0
|
||||||
#define USB_DEVICE_MANUFACTURER "Atguigu"
|
#define USB_DEVICE_MANUFACTURER "Atguigu"
|
||||||
#define USB_DEVICE_PRODUCT "WH Mini Keyboard"
|
#define USB_DEVICE_PRODUCT "WH Mini Keyboard"
|
||||||
#define USB_REQUIRED_FUNCTION_MASK (USB_FUNCTION_HID | USB_FUNCTION_CDC_ACM)
|
#define USB_REQUIRED_READY_MASK (USB_STATEF_HID_READY | USB_STATEF_CDC_READY)
|
||||||
|
|
||||||
USBD_DEVICE_DEFINE(blinky_usbd, DEVICE_DT_GET(DT_NODELABEL(usbd)),
|
USBD_DEVICE_DEFINE(blinky_usbd, DEVICE_DT_GET(DT_NODELABEL(usbd)),
|
||||||
USB_DEVICE_VID, USB_DEVICE_PID);
|
USB_DEVICE_VID, USB_DEVICE_PID);
|
||||||
@@ -42,35 +39,69 @@ static const char *const class_blocklist[] = {
|
|||||||
|
|
||||||
static bool initialized;
|
static bool initialized;
|
||||||
static bool running;
|
static bool running;
|
||||||
static bool prepare_broadcasted;
|
|
||||||
static bool usbd_initialized;
|
static bool usbd_initialized;
|
||||||
static bool usb_enabled;
|
static bool usb_enabled;
|
||||||
static uint8_t ready_function_mask;
|
static uint32_t usb_state_flags;
|
||||||
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_runtime_state {
|
||||||
|
USB_RUNTIME_STATE_DISCONNECTED,
|
||||||
|
USB_RUNTIME_STATE_POWERED,
|
||||||
|
USB_RUNTIME_STATE_ACTIVE,
|
||||||
|
USB_RUNTIME_STATE_SUSPENDED,
|
||||||
|
};
|
||||||
|
|
||||||
|
static enum usb_runtime_state device_state = USB_RUNTIME_STATE_DISCONNECTED;
|
||||||
|
|
||||||
|
static void broadcast_usb_state(void)
|
||||||
{
|
{
|
||||||
struct usb_device_state_event *event = new_usb_device_state_event();
|
submit_usb_state_snapshot(MODULE_ID(MODULE), usb_state_flags);
|
||||||
|
|
||||||
event->state = state;
|
|
||||||
APP_EVENT_SUBMIT(event);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static void submit_usb_prepare_event(void)
|
static void set_usb_state_flags(uint32_t mask)
|
||||||
{
|
{
|
||||||
struct usb_prepare_event *event = new_usb_prepare_event();
|
usb_state_flags |= mask;
|
||||||
|
|
||||||
APP_EVENT_SUBMIT(event);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static void update_usb_device_state(enum usb_device_state state)
|
static void clear_usb_state_flags(uint32_t mask)
|
||||||
|
{
|
||||||
|
usb_state_flags &= ~mask;
|
||||||
|
}
|
||||||
|
|
||||||
|
static void update_usb_runtime_state(enum usb_runtime_state state)
|
||||||
{
|
{
|
||||||
if (device_state == state) {
|
if (device_state == state) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
device_state = state;
|
device_state = state;
|
||||||
submit_usb_device_state_event(state);
|
|
||||||
|
switch (state) {
|
||||||
|
case USB_RUNTIME_STATE_DISCONNECTED:
|
||||||
|
clear_usb_state_flags(USB_STATEF_POWERED |
|
||||||
|
USB_STATEF_ACTIVE |
|
||||||
|
USB_STATEF_SUSPENDED);
|
||||||
|
break;
|
||||||
|
|
||||||
|
case USB_RUNTIME_STATE_POWERED:
|
||||||
|
set_usb_state_flags(USB_STATEF_POWERED);
|
||||||
|
clear_usb_state_flags(USB_STATEF_ACTIVE | USB_STATEF_SUSPENDED);
|
||||||
|
break;
|
||||||
|
|
||||||
|
case USB_RUNTIME_STATE_ACTIVE:
|
||||||
|
set_usb_state_flags(USB_STATEF_POWERED | USB_STATEF_ACTIVE);
|
||||||
|
clear_usb_state_flags(USB_STATEF_SUSPENDED);
|
||||||
|
break;
|
||||||
|
|
||||||
|
case USB_RUNTIME_STATE_SUSPENDED:
|
||||||
|
set_usb_state_flags(USB_STATEF_POWERED | USB_STATEF_SUSPENDED);
|
||||||
|
clear_usb_state_flags(USB_STATEF_ACTIVE);
|
||||||
|
break;
|
||||||
|
|
||||||
|
default:
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
broadcast_usb_state();
|
||||||
}
|
}
|
||||||
|
|
||||||
static void update_power_manager_restriction(bool vbus_present)
|
static void update_power_manager_restriction(bool vbus_present)
|
||||||
@@ -129,7 +160,7 @@ static void usbd_msg_cb(struct usbd_context *const usbd_ctx,
|
|||||||
LOG_ERR("usbd_enable failed (%d)", err);
|
LOG_ERR("usbd_enable failed (%d)", err);
|
||||||
} else {
|
} else {
|
||||||
usb_enabled = true;
|
usb_enabled = true;
|
||||||
update_usb_device_state(USB_DEVICE_STATE_POWERED);
|
update_usb_runtime_state(USB_RUNTIME_STATE_POWERED);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
@@ -145,26 +176,26 @@ static void usbd_msg_cb(struct usbd_context *const usbd_ctx,
|
|||||||
}
|
}
|
||||||
|
|
||||||
usb_enabled = false;
|
usb_enabled = false;
|
||||||
update_usb_device_state(USB_DEVICE_STATE_DISCONNECTED);
|
update_usb_runtime_state(USB_RUNTIME_STATE_DISCONNECTED);
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case USBD_MSG_CONFIGURATION:
|
case USBD_MSG_CONFIGURATION:
|
||||||
if (msg->status) {
|
if (msg->status) {
|
||||||
update_usb_device_state(USB_DEVICE_STATE_ACTIVE);
|
update_usb_runtime_state(USB_RUNTIME_STATE_ACTIVE);
|
||||||
} else if (usb_enabled) {
|
} else if (usb_enabled) {
|
||||||
update_usb_device_state(USB_DEVICE_STATE_POWERED);
|
update_usb_runtime_state(USB_RUNTIME_STATE_POWERED);
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case USBD_MSG_SUSPEND:
|
case USBD_MSG_SUSPEND:
|
||||||
if (usb_enabled) {
|
if (usb_enabled) {
|
||||||
update_usb_device_state(USB_DEVICE_STATE_SUSPENDED);
|
update_usb_runtime_state(USB_RUNTIME_STATE_SUSPENDED);
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case USBD_MSG_RESUME:
|
case USBD_MSG_RESUME:
|
||||||
if (usb_enabled) {
|
if (usb_enabled) {
|
||||||
update_usb_device_state(USB_DEVICE_STATE_ACTIVE);
|
update_usb_runtime_state(USB_RUNTIME_STATE_ACTIVE);
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
|
|
||||||
@@ -196,6 +227,7 @@ static int usb_stack_init(void)
|
|||||||
}
|
}
|
||||||
|
|
||||||
usbd_initialized = true;
|
usbd_initialized = true;
|
||||||
|
set_usb_state_flags(USB_STATEF_STACK_READY);
|
||||||
|
|
||||||
if (!usbd_can_detect_vbus(&blinky_usbd)) {
|
if (!usbd_can_detect_vbus(&blinky_usbd)) {
|
||||||
err = usbd_enable(&blinky_usbd);
|
err = usbd_enable(&blinky_usbd);
|
||||||
@@ -205,19 +237,33 @@ static int usb_stack_init(void)
|
|||||||
}
|
}
|
||||||
|
|
||||||
usb_enabled = true;
|
usb_enabled = true;
|
||||||
update_usb_device_state(USB_DEVICE_STATE_POWERED);
|
update_usb_runtime_state(USB_RUNTIME_STATE_POWERED);
|
||||||
}
|
}
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static int try_start_usb_stack(void)
|
||||||
|
{
|
||||||
|
if (usbd_initialized) {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
if ((usb_state_flags & USB_REQUIRED_READY_MASK) != USB_REQUIRED_READY_MASK) {
|
||||||
|
set_usb_state_flags(USB_STATEF_PREPARE);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
clear_usb_state_flags(USB_STATEF_PREPARE);
|
||||||
|
return usb_stack_init();
|
||||||
|
}
|
||||||
|
|
||||||
static int module_init(void)
|
static int module_init(void)
|
||||||
{
|
{
|
||||||
device_state = USB_DEVICE_STATE_DISCONNECTED;
|
device_state = USB_RUNTIME_STATE_DISCONNECTED;
|
||||||
usb_enabled = false;
|
usb_enabled = false;
|
||||||
ready_function_mask = 0U;
|
|
||||||
prepare_broadcasted = false;
|
|
||||||
usbd_initialized = false;
|
usbd_initialized = false;
|
||||||
|
usb_state_flags = 0U;
|
||||||
update_power_manager_restriction(false);
|
update_power_manager_restriction(false);
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
@@ -230,16 +276,12 @@ static int module_start(void)
|
|||||||
}
|
}
|
||||||
|
|
||||||
running = true;
|
running = true;
|
||||||
|
set_usb_state_flags(USB_STATEF_STACK_RUNNING);
|
||||||
|
|
||||||
if (usbd_initialized || prepare_broadcasted) {
|
int err = try_start_usb_stack();
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
ready_function_mask = 0U;
|
broadcast_usb_state();
|
||||||
prepare_broadcasted = true;
|
return err;
|
||||||
submit_usb_prepare_event();
|
|
||||||
|
|
||||||
return 0;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static void module_pause(void)
|
static void module_pause(void)
|
||||||
@@ -249,6 +291,8 @@ static void module_pause(void)
|
|||||||
}
|
}
|
||||||
|
|
||||||
running = false;
|
running = false;
|
||||||
|
clear_usb_state_flags(USB_STATEF_STACK_RUNNING);
|
||||||
|
broadcast_usb_state();
|
||||||
}
|
}
|
||||||
|
|
||||||
static bool handle_module_state_event(const struct module_state_event *event)
|
static bool handle_module_state_event(const struct module_state_event *event)
|
||||||
@@ -281,28 +325,32 @@ static bool handle_module_state_event(const struct module_state_event *event)
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
static bool handle_usb_function_ready_event(const struct usb_function_ready_event *event)
|
static bool handle_usb_state_event(const struct usb_state_event *event)
|
||||||
{
|
{
|
||||||
int err;
|
if ((event->op == USB_STATE_EVENT_OP_SNAPSHOT) ||
|
||||||
|
(event->sink_module_id != MODULE_ID(MODULE))) {
|
||||||
if (!running || !prepare_broadcasted || usbd_initialized) {
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
ready_function_mask |= event->function_mask;
|
if (event->op == USB_STATE_EVENT_OP_SET_BITS) {
|
||||||
|
usb_state_flags |= event->flags;
|
||||||
if ((ready_function_mask & USB_REQUIRED_FUNCTION_MASK) !=
|
} else if (event->op == USB_STATE_EVENT_OP_CLEAR_BITS) {
|
||||||
USB_REQUIRED_FUNCTION_MASK) {
|
usb_state_flags &= ~event->flags;
|
||||||
|
} else {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
err = usb_stack_init();
|
int err = running ? try_start_usb_stack() : 0;
|
||||||
if (err) {
|
if (err) {
|
||||||
module_set_state(MODULE_STATE_ERROR);
|
module_set_state(MODULE_STATE_ERROR);
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
broadcast_usb_state();
|
||||||
|
|
||||||
|
if (usbd_initialized) {
|
||||||
module_set_state(MODULE_STATE_READY);
|
module_set_state(MODULE_STATE_READY);
|
||||||
|
}
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -312,8 +360,8 @@ static bool app_event_handler(const struct app_event_header *aeh)
|
|||||||
return handle_module_state_event(cast_module_state_event(aeh));
|
return handle_module_state_event(cast_module_state_event(aeh));
|
||||||
}
|
}
|
||||||
|
|
||||||
if (is_usb_function_ready_event(aeh)) {
|
if (is_usb_state_event(aeh)) {
|
||||||
return handle_usb_function_ready_event(cast_usb_function_ready_event(aeh));
|
return handle_usb_state_event(cast_usb_state_event(aeh));
|
||||||
}
|
}
|
||||||
|
|
||||||
if (is_power_down_event(aeh)) {
|
if (is_power_down_event(aeh)) {
|
||||||
@@ -344,6 +392,6 @@ static bool app_event_handler(const struct app_event_header *aeh)
|
|||||||
|
|
||||||
APP_EVENT_LISTENER(MODULE, app_event_handler);
|
APP_EVENT_LISTENER(MODULE, app_event_handler);
|
||||||
APP_EVENT_SUBSCRIBE(MODULE, module_state_event);
|
APP_EVENT_SUBSCRIBE(MODULE, module_state_event);
|
||||||
APP_EVENT_SUBSCRIBE(MODULE, usb_function_ready_event);
|
APP_EVENT_SUBSCRIBE(MODULE, usb_state_event);
|
||||||
APP_EVENT_SUBSCRIBE_EARLY(MODULE, power_down_event);
|
APP_EVENT_SUBSCRIBE_EARLY(MODULE, power_down_event);
|
||||||
APP_EVENT_SUBSCRIBE(MODULE, wake_up_event);
|
APP_EVENT_SUBSCRIBE(MODULE, wake_up_event);
|
||||||
|
|||||||
@@ -21,10 +21,7 @@
|
|||||||
#include "hid_tx_report_event.h"
|
#include "hid_tx_report_event.h"
|
||||||
#include "keyboard_core.h"
|
#include "keyboard_core.h"
|
||||||
#include "set_protocol_event.h"
|
#include "set_protocol_event.h"
|
||||||
#include "usb_function_ready_event.h"
|
#include "usb_state_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);
|
LOG_MODULE_REGISTER(MODULE, LOG_LEVEL_INF);
|
||||||
|
|
||||||
@@ -140,59 +137,23 @@ static void reset_usb_runtime_state(void)
|
|||||||
consumer_report_in_flight = false;
|
consumer_report_in_flight = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
static void submit_set_protocol_event(enum keyboard_protocol_mode protocol_mode)
|
static void submit_usb_transport_state_event(void)
|
||||||
{
|
{
|
||||||
struct set_protocol_event *event = new_set_protocol_event();
|
|
||||||
|
|
||||||
event->transport = HID_TRANSPORT_USB;
|
|
||||||
event->protocol_mode = protocol_mode;
|
|
||||||
APP_EVENT_SUBMIT(event);
|
|
||||||
}
|
|
||||||
|
|
||||||
static void submit_hid_led_event(uint8_t led_bm)
|
|
||||||
{
|
|
||||||
struct hid_led_event *event = new_hid_led_event();
|
|
||||||
|
|
||||||
event->transport = HID_TRANSPORT_USB;
|
|
||||||
event->led_bm = led_bm;
|
|
||||||
APP_EVENT_SUBMIT(event);
|
|
||||||
}
|
|
||||||
|
|
||||||
static void submit_transport_state_event(void)
|
|
||||||
{
|
|
||||||
struct hid_transport_state_event *event = new_hid_transport_state_event();
|
|
||||||
bool ready = running && usb_active;
|
bool ready = running && usb_active;
|
||||||
|
|
||||||
event->transport = HID_TRANSPORT_USB;
|
submit_hid_transport_state_event(
|
||||||
event->ready = ready;
|
HID_TRANSPORT_USB,
|
||||||
event->keys_ready = ready &&
|
ready,
|
||||||
hid_ifaces[USB_HID_INTERFACE_KEYBOARD].ready;
|
ready && hid_ifaces[USB_HID_INTERFACE_KEYBOARD].ready,
|
||||||
event->consumer_ready = ready &&
|
ready && hid_ifaces[USB_HID_INTERFACE_CONSUMER].ready,
|
||||||
hid_ifaces[USB_HID_INTERFACE_CONSUMER].ready;
|
keyboard_protocol_mode);
|
||||||
event->protocol_mode = keyboard_protocol_mode;
|
|
||||||
|
|
||||||
APP_EVENT_SUBMIT(event);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static void submit_hid_report_sent_event(enum keyboard_report_type report_type,
|
static bool is_usb_owner_snapshot(const struct usb_state_event *event)
|
||||||
uint16_t sequence, bool error)
|
|
||||||
{
|
{
|
||||||
struct hid_report_sent_event *event = new_hid_report_sent_event();
|
return (event->op == USB_STATE_EVENT_OP_SNAPSHOT) &&
|
||||||
|
(event->src_module_id == MODULE_ID(usb_device_module)) &&
|
||||||
event->transport = HID_TRANSPORT_USB;
|
(event->sink_module_id == NULL);
|
||||||
event->report_type = report_type;
|
|
||||||
event->sequence = sequence;
|
|
||||||
event->error = error;
|
|
||||||
|
|
||||||
APP_EVENT_SUBMIT(event);
|
|
||||||
}
|
|
||||||
|
|
||||||
static void submit_usb_function_ready_event(void)
|
|
||||||
{
|
|
||||||
struct usb_function_ready_event *event = new_usb_function_ready_event();
|
|
||||||
|
|
||||||
event->function_mask = USB_FUNCTION_HID;
|
|
||||||
APP_EVENT_SUBMIT(event);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static void keyboard_iface_ready(const struct device *dev, const bool ready)
|
static void keyboard_iface_ready(const struct device *dev, const bool ready)
|
||||||
@@ -209,7 +170,7 @@ static void keyboard_iface_ready(const struct device *dev, const bool ready)
|
|||||||
}
|
}
|
||||||
|
|
||||||
LOG_INF("%s interface %s", dev->name, ready ? "ready" : "not ready");
|
LOG_INF("%s interface %s", dev->name, ready ? "ready" : "not ready");
|
||||||
submit_transport_state_event();
|
submit_usb_transport_state_event();
|
||||||
}
|
}
|
||||||
|
|
||||||
static int keyboard_get_report(const struct device *dev,
|
static int keyboard_get_report(const struct device *dev,
|
||||||
@@ -267,8 +228,8 @@ static void keyboard_set_protocol(const struct device *dev, const uint8_t proto)
|
|||||||
keyboard_protocol_mode = new_mode;
|
keyboard_protocol_mode = new_mode;
|
||||||
LOG_INF("USB keyboard protocol -> %s",
|
LOG_INF("USB keyboard protocol -> %s",
|
||||||
(new_mode == KEYBOARD_PROTOCOL_MODE_BOOT) ? "boot" : "report");
|
(new_mode == KEYBOARD_PROTOCOL_MODE_BOOT) ? "boot" : "report");
|
||||||
submit_set_protocol_event(new_mode);
|
submit_set_protocol_event(HID_TRANSPORT_USB, new_mode);
|
||||||
submit_transport_state_event();
|
submit_usb_transport_state_event();
|
||||||
}
|
}
|
||||||
|
|
||||||
static void keyboard_input_report_done(const struct device *dev,
|
static void keyboard_input_report_done(const struct device *dev,
|
||||||
@@ -278,7 +239,8 @@ static void keyboard_input_report_done(const struct device *dev,
|
|||||||
|
|
||||||
keyboard_report_in_flight = false;
|
keyboard_report_in_flight = false;
|
||||||
LOG_DBG("USB keyboard report sent by %s", dev->name);
|
LOG_DBG("USB keyboard report sent by %s", dev->name);
|
||||||
submit_hid_report_sent_event(KEYBOARD_REPORT_TYPE_KEYS,
|
submit_hid_report_sent_event(HID_TRANSPORT_USB,
|
||||||
|
KEYBOARD_REPORT_TYPE_KEYS,
|
||||||
keyboard_in_flight_sequence, false);
|
keyboard_in_flight_sequence, false);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -293,7 +255,7 @@ static void keyboard_output_report(const struct device *dev,
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
submit_hid_led_event(buf[0]);
|
submit_hid_led_event(HID_TRANSPORT_USB, buf[0]);
|
||||||
}
|
}
|
||||||
|
|
||||||
static const struct hid_device_ops keyboard_ops = {
|
static const struct hid_device_ops keyboard_ops = {
|
||||||
@@ -321,7 +283,7 @@ static void consumer_iface_ready(const struct device *dev, const bool ready)
|
|||||||
}
|
}
|
||||||
|
|
||||||
LOG_INF("%s interface %s", dev->name, ready ? "ready" : "not ready");
|
LOG_INF("%s interface %s", dev->name, ready ? "ready" : "not ready");
|
||||||
submit_transport_state_event();
|
submit_usb_transport_state_event();
|
||||||
}
|
}
|
||||||
|
|
||||||
static int consumer_get_report(const struct device *dev,
|
static int consumer_get_report(const struct device *dev,
|
||||||
@@ -379,7 +341,8 @@ static void consumer_input_report_done(const struct device *dev,
|
|||||||
|
|
||||||
consumer_report_in_flight = false;
|
consumer_report_in_flight = false;
|
||||||
LOG_DBG("USB consumer report sent by %s", dev->name);
|
LOG_DBG("USB consumer report sent by %s", dev->name);
|
||||||
submit_hid_report_sent_event(KEYBOARD_REPORT_TYPE_CONSUMER,
|
submit_hid_report_sent_event(HID_TRANSPORT_USB,
|
||||||
|
KEYBOARD_REPORT_TYPE_CONSUMER,
|
||||||
consumer_in_flight_sequence, false);
|
consumer_in_flight_sequence, false);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -449,27 +412,26 @@ static int module_start(void)
|
|||||||
}
|
}
|
||||||
|
|
||||||
running = true;
|
running = true;
|
||||||
submit_transport_state_event();
|
submit_usb_transport_state_event();
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
static void module_pause(void)
|
static void module_pause(void)
|
||||||
{
|
{
|
||||||
running = false;
|
running = false;
|
||||||
submit_transport_state_event();
|
submit_usb_transport_state_event();
|
||||||
}
|
}
|
||||||
|
|
||||||
static bool handle_usb_prepare_event(const struct usb_prepare_event *event)
|
static bool handle_usb_state_event(const struct usb_state_event *event)
|
||||||
{
|
{
|
||||||
int err;
|
if (!is_usb_owner_snapshot(event)) {
|
||||||
|
|
||||||
ARG_UNUSED(event);
|
|
||||||
|
|
||||||
if (!running || usb_function_prepared) {
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
err = usb_hid_register_devices();
|
if (running && !usb_function_prepared &&
|
||||||
|
((event->flags & USB_STATEF_PREPARE) != 0U)) {
|
||||||
|
int err = usb_hid_register_devices();
|
||||||
|
|
||||||
if (err) {
|
if (err) {
|
||||||
LOG_ERR("hid_device_register failed (%d)", err);
|
LOG_ERR("hid_device_register failed (%d)", err);
|
||||||
module_set_state(MODULE_STATE_ERROR);
|
module_set_state(MODULE_STATE_ERROR);
|
||||||
@@ -477,13 +439,12 @@ static bool handle_usb_prepare_event(const struct usb_prepare_event *event)
|
|||||||
}
|
}
|
||||||
|
|
||||||
usb_function_prepared = true;
|
usb_function_prepared = true;
|
||||||
submit_usb_function_ready_event();
|
submit_usb_state_set(MODULE_ID(MODULE),
|
||||||
return false;
|
MODULE_ID(usb_device_module),
|
||||||
}
|
USB_STATEF_HID_READY);
|
||||||
|
}
|
||||||
|
|
||||||
static bool handle_usb_device_state_event(const struct usb_device_state_event *event)
|
bool new_usb_active = (event->flags & USB_STATEF_ACTIVE) != 0U;
|
||||||
{
|
|
||||||
bool new_usb_active = (event->state == USB_DEVICE_STATE_ACTIVE);
|
|
||||||
|
|
||||||
if (new_usb_active == usb_active) {
|
if (new_usb_active == usb_active) {
|
||||||
return false;
|
return false;
|
||||||
@@ -495,7 +456,7 @@ static bool handle_usb_device_state_event(const struct usb_device_state_event *e
|
|||||||
usb_active = true;
|
usb_active = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
submit_transport_state_event();
|
submit_usb_transport_state_event();
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -531,7 +492,8 @@ static bool handle_hid_tx_report_event(const struct hid_tx_report_event *event)
|
|||||||
keyboard_tx_buf);
|
keyboard_tx_buf);
|
||||||
if (err) {
|
if (err) {
|
||||||
LOG_WRN("USB keyboard report submit failed (%d)", err);
|
LOG_WRN("USB keyboard report submit failed (%d)", err);
|
||||||
submit_hid_report_sent_event(KEYBOARD_REPORT_TYPE_KEYS,
|
submit_hid_report_sent_event(HID_TRANSPORT_USB,
|
||||||
|
KEYBOARD_REPORT_TYPE_KEYS,
|
||||||
event->sequence, true);
|
event->sequence, true);
|
||||||
} else {
|
} else {
|
||||||
keyboard_report_in_flight = true;
|
keyboard_report_in_flight = true;
|
||||||
@@ -560,7 +522,8 @@ static bool handle_hid_tx_report_event(const struct hid_tx_report_event *event)
|
|||||||
consumer_tx_buf);
|
consumer_tx_buf);
|
||||||
if (err) {
|
if (err) {
|
||||||
LOG_WRN("USB consumer report submit failed (%d)", err);
|
LOG_WRN("USB consumer report submit failed (%d)", err);
|
||||||
submit_hid_report_sent_event(KEYBOARD_REPORT_TYPE_CONSUMER,
|
submit_hid_report_sent_event(HID_TRANSPORT_USB,
|
||||||
|
KEYBOARD_REPORT_TYPE_CONSUMER,
|
||||||
event->sequence, true);
|
event->sequence, true);
|
||||||
} else {
|
} else {
|
||||||
consumer_report_in_flight = true;
|
consumer_report_in_flight = true;
|
||||||
@@ -577,12 +540,8 @@ static bool app_event_handler(const struct app_event_header *aeh)
|
|||||||
return handle_hid_tx_report_event(cast_hid_tx_report_event(aeh));
|
return handle_hid_tx_report_event(cast_hid_tx_report_event(aeh));
|
||||||
}
|
}
|
||||||
|
|
||||||
if (is_usb_device_state_event(aeh)) {
|
if (is_usb_state_event(aeh)) {
|
||||||
return handle_usb_device_state_event(cast_usb_device_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_module_state_event(aeh)) {
|
if (is_module_state_event(aeh)) {
|
||||||
@@ -641,7 +600,6 @@ static bool app_event_handler(const struct app_event_header *aeh)
|
|||||||
APP_EVENT_LISTENER(MODULE, app_event_handler);
|
APP_EVENT_LISTENER(MODULE, app_event_handler);
|
||||||
APP_EVENT_SUBSCRIBE(MODULE, module_state_event);
|
APP_EVENT_SUBSCRIBE(MODULE, module_state_event);
|
||||||
APP_EVENT_SUBSCRIBE(MODULE, hid_tx_report_event);
|
APP_EVENT_SUBSCRIBE(MODULE, hid_tx_report_event);
|
||||||
APP_EVENT_SUBSCRIBE(MODULE, usb_prepare_event);
|
APP_EVENT_SUBSCRIBE(MODULE, usb_state_event);
|
||||||
APP_EVENT_SUBSCRIBE(MODULE, usb_device_state_event);
|
|
||||||
APP_EVENT_SUBSCRIBE_EARLY(MODULE, power_down_event);
|
APP_EVENT_SUBSCRIBE_EARLY(MODULE, power_down_event);
|
||||||
APP_EVENT_SUBSCRIBE(MODULE, wake_up_event);
|
APP_EVENT_SUBSCRIBE(MODULE, wake_up_event);
|
||||||
|
|||||||
Reference in New Issue
Block a user