Compare commits
9 Commits
e97bd47e36
...
dev
| Author | SHA1 | Date | |
|---|---|---|---|
| 227158870a | |||
| 39c6a1fe84 | |||
| 33fb416cfa | |||
| c40fc709d5 | |||
| 2f6126da96 | |||
| 76adb3584c | |||
| 39d2962258 | |||
| b9b7d342f5 | |||
| 70381192d9 |
@@ -4,22 +4,49 @@ cmake_minimum_required(VERSION 3.20.0)
|
|||||||
find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE})
|
find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE})
|
||||||
project(blinky)
|
project(blinky)
|
||||||
|
|
||||||
|
list(APPEND CMAKE_MODULE_PATH ${ZEPHYR_BASE}/modules/nanopb)
|
||||||
|
include(nanopb)
|
||||||
|
|
||||||
zephyr_include_directories(
|
zephyr_include_directories(
|
||||||
inc
|
inc
|
||||||
inc/events
|
inc/events
|
||||||
)
|
)
|
||||||
add_subdirectory(drivers)
|
add_subdirectory(drivers)
|
||||||
|
|
||||||
|
zephyr_nanopb_sources(app
|
||||||
|
proto/device_comm.proto
|
||||||
|
)
|
||||||
|
|
||||||
target_sources(app PRIVATE
|
target_sources(app PRIVATE
|
||||||
src/main.c
|
src/main.c
|
||||||
src/battery_module.c
|
src/battery_module.c
|
||||||
|
src/ble_adv_ctrl_module.c
|
||||||
|
src/ble_adv_uuid16.c
|
||||||
|
src/ble_bas_module.c
|
||||||
|
src/ble_hid_module.c
|
||||||
|
src/display_module.c
|
||||||
src/encoder_module.c
|
src/encoder_module.c
|
||||||
|
src/hid_flowctrl_module.c
|
||||||
src/keyboard_core_module.c
|
src/keyboard_core_module.c
|
||||||
|
src/ui/ui_main.c
|
||||||
|
src/cdc_wrapper_module.c
|
||||||
|
src/protocol_module.c
|
||||||
|
src/usb_cdc_module.c
|
||||||
|
src/usb_device_module.c
|
||||||
src/usb_hid_module.c
|
src/usb_hid_module.c
|
||||||
|
src/events/bat_state_event.c
|
||||||
src/events/encoder_event.c
|
src/events/encoder_event.c
|
||||||
src/events/hid_led_event.c
|
src/events/hid_led_event.c
|
||||||
|
src/events/hid_report_sent_event.c
|
||||||
|
src/events/hid_transport_state_event.c
|
||||||
|
src/events/hid_tx_report_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/set_protocol_event.c
|
src/events/set_protocol_event.c
|
||||||
|
src/events/usb_cdc_rx_event.c
|
||||||
|
src/events/usb_cdc_tx_event.c
|
||||||
|
src/events/usb_device_state_event.c
|
||||||
|
src/events/usb_function_ready_event.c
|
||||||
|
src/events/usb_prepare_event.c
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -30,4 +30,32 @@
|
|||||||
bias-pull-up;
|
bias-pull-up;
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
|
spi3_default: spi3_default {
|
||||||
|
group1 {
|
||||||
|
psels = <NRF_PSEL(SPIM_SCK, 1, 13)>,
|
||||||
|
<NRF_PSEL(SPIM_MOSI, 0, 28)>;
|
||||||
|
};
|
||||||
|
};
|
||||||
|
|
||||||
|
spi3_sleep: spi3_sleep {
|
||||||
|
group1 {
|
||||||
|
psels = <NRF_PSEL(SPIM_SCK, 1, 13)>,
|
||||||
|
<NRF_PSEL(SPIM_MOSI, 0, 28)>;
|
||||||
|
low-power-enable;
|
||||||
|
};
|
||||||
|
};
|
||||||
|
|
||||||
|
pwm0_default: pwm0_default {
|
||||||
|
group1 {
|
||||||
|
psels = <NRF_PSEL(PWM_OUT0, 1, 11)>;
|
||||||
|
};
|
||||||
|
};
|
||||||
|
|
||||||
|
pwm0_sleep: pwm0_sleep {
|
||||||
|
group1 {
|
||||||
|
psels = <NRF_PSEL(PWM_OUT0, 1, 11)>;
|
||||||
|
low-power-enable;
|
||||||
|
};
|
||||||
|
};
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -2,6 +2,8 @@
|
|||||||
#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/mipi_dbi/mipi_dbi.h>
|
||||||
|
#include <zephyr/dt-bindings/pwm/pwm.h>
|
||||||
|
|
||||||
/ {
|
/ {
|
||||||
model = "Mini keyboard";
|
model = "Mini keyboard";
|
||||||
@@ -11,11 +13,13 @@
|
|||||||
zephyr,sram = &sram0;
|
zephyr,sram = &sram0;
|
||||||
zephyr,flash = &flash0;
|
zephyr,flash = &flash0;
|
||||||
zephyr,code-partition = &slot0_partition;
|
zephyr,code-partition = &slot0_partition;
|
||||||
|
zephyr,display = &screen_lcd;
|
||||||
};
|
};
|
||||||
|
|
||||||
aliases {
|
aliases {
|
||||||
led0 = &myled0;
|
led0 = &myled0;
|
||||||
qdec0 = &qdec;
|
qdec0 = &qdec;
|
||||||
|
backlight = &backlight;
|
||||||
};
|
};
|
||||||
|
|
||||||
hid_kbd: hid_kbd {
|
hid_kbd: hid_kbd {
|
||||||
@@ -43,6 +47,51 @@
|
|||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
|
pwm_leds {
|
||||||
|
compatible = "pwm-leds";
|
||||||
|
status = "okay";
|
||||||
|
|
||||||
|
backlight: pwm_led_0 {
|
||||||
|
pwms = <&pwm0 0 PWM_MSEC(10) PWM_POLARITY_INVERTED>;
|
||||||
|
};
|
||||||
|
};
|
||||||
|
|
||||||
|
mipi_dbi_screen: mipi_dbi_screen {
|
||||||
|
compatible = "zephyr,mipi-dbi-spi";
|
||||||
|
spi-dev = <&spi3>;
|
||||||
|
dc-gpios = <&gpio0 3 GPIO_ACTIVE_HIGH>;
|
||||||
|
reset-gpios = <&gpio1 10 GPIO_ACTIVE_LOW>;
|
||||||
|
write-only;
|
||||||
|
#address-cells = <1>;
|
||||||
|
#size-cells = <0>;
|
||||||
|
|
||||||
|
screen_lcd: st7789v@0 {
|
||||||
|
compatible = "sitronix,st7789v";
|
||||||
|
status = "okay";
|
||||||
|
reg = <0>;
|
||||||
|
mipi-max-frequency = <32000000>;
|
||||||
|
width = <320>;
|
||||||
|
height = <172>;
|
||||||
|
x-offset = <0>;
|
||||||
|
y-offset = <34>;
|
||||||
|
vcom = <0x34>;
|
||||||
|
gctrl = <0x00>;
|
||||||
|
mdac = <0xA0>;
|
||||||
|
gamma = <0x01>;
|
||||||
|
colmod = <0x05>;
|
||||||
|
lcm = <0x2c>;
|
||||||
|
porch-param = [ 0c 0c 00 33 33 ];
|
||||||
|
cmd2en-param = [ 5a 69 02 01 ];
|
||||||
|
pwctrl1-param = [ a4 a1 ];
|
||||||
|
pvgam-param = [ f0 04 08 0a 0a 05 25 33 3c 24 0e 0f 27 2f ];
|
||||||
|
nvgam-param = [ f0 02 06 06 04 22 25 32 3b 3a 15 17 2d 37 ];
|
||||||
|
ram-param = [ 00 f0 ];
|
||||||
|
rgb-param = [ cd 08 14 ];
|
||||||
|
ready-time-ms = <120>;
|
||||||
|
mipi-mode = "MIPI_DBI_MODE_SPI_4WIRE";
|
||||||
|
};
|
||||||
|
};
|
||||||
|
|
||||||
vbatt: vbatt {
|
vbatt: vbatt {
|
||||||
compatible = "voltage-divider";
|
compatible = "voltage-divider";
|
||||||
io-channels = <&adc 7>;
|
io-channels = <&adc 7>;
|
||||||
@@ -90,6 +139,7 @@
|
|||||||
|
|
||||||
&uicr {
|
&uicr {
|
||||||
nfct-pins-as-gpios;
|
nfct-pins-as-gpios;
|
||||||
|
gpio-as-nreset;
|
||||||
};
|
};
|
||||||
|
|
||||||
&adc {
|
&adc {
|
||||||
@@ -148,6 +198,21 @@
|
|||||||
status = "okay";
|
status = "okay";
|
||||||
};
|
};
|
||||||
|
|
||||||
|
&spi3 {
|
||||||
|
status = "okay";
|
||||||
|
pinctrl-0 = <&spi3_default>;
|
||||||
|
pinctrl-1 = <&spi3_sleep>;
|
||||||
|
pinctrl-names = "default", "sleep";
|
||||||
|
cs-gpios = <&gpio0 2 GPIO_ACTIVE_LOW>;
|
||||||
|
};
|
||||||
|
|
||||||
|
&pwm0 {
|
||||||
|
status = "okay";
|
||||||
|
pinctrl-0 = <&pwm0_default>;
|
||||||
|
pinctrl-1 = <&pwm0_sleep>;
|
||||||
|
pinctrl-names = "default", "sleep";
|
||||||
|
};
|
||||||
|
|
||||||
&qdec {
|
&qdec {
|
||||||
status = "okay";
|
status = "okay";
|
||||||
pinctrl-0 = <&encoder_default>;
|
pinctrl-0 = <&encoder_default>;
|
||||||
@@ -160,9 +225,14 @@
|
|||||||
|
|
||||||
&usbd {
|
&usbd {
|
||||||
status = "okay";
|
status = "okay";
|
||||||
num-bidir-endpoints = <0>;
|
num-bidir-endpoints = <1>;
|
||||||
num-in-endpoints = <2>;
|
num-in-endpoints = <7>;
|
||||||
num-out-endpoints = <1>;
|
num-out-endpoints = <7>;
|
||||||
num-isoin-endpoints = <0>;
|
num-isoin-endpoints = <0>;
|
||||||
num-isoout-endpoints = <0>;
|
num-isoout-endpoints = <0>;
|
||||||
|
|
||||||
|
cdc_acm_uart0: cdc_acm_uart0 {
|
||||||
|
compatible = "zephyr,cdc-acm-uart";
|
||||||
|
label = "CDC_ACM_0";
|
||||||
|
};
|
||||||
};
|
};
|
||||||
|
|||||||
12
inc/cdc_wrapper_module.h
Normal file
12
inc/cdc_wrapper_module.h
Normal file
@@ -0,0 +1,12 @@
|
|||||||
|
#ifndef BLINKY_CDC_WRAPPER_MODULE_H_
|
||||||
|
#define BLINKY_CDC_WRAPPER_MODULE_H_
|
||||||
|
|
||||||
|
#ifdef __cplusplus
|
||||||
|
extern "C" {
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#ifdef __cplusplus
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#endif /* BLINKY_CDC_WRAPPER_MODULE_H_ */
|
||||||
15
inc/click_detector_def.h
Normal file
15
inc/click_detector_def.h
Normal file
@@ -0,0 +1,15 @@
|
|||||||
|
/*
|
||||||
|
* This configuration file is included only once from the CAF click detector
|
||||||
|
* module and defines the keys that should produce click events.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include <caf/click_detector.h>
|
||||||
|
|
||||||
|
const struct {} click_detector_def_include_once;
|
||||||
|
|
||||||
|
static const struct click_detector_config click_detector_config[] = {
|
||||||
|
{
|
||||||
|
.key_id = 0x180,
|
||||||
|
.consume_button_event = true,
|
||||||
|
},
|
||||||
|
};
|
||||||
27
inc/events/bat_state_event.h
Normal file
27
inc/events/bat_state_event.h
Normal file
@@ -0,0 +1,27 @@
|
|||||||
|
#ifndef BLINKY_BAT_STATE_EVENT_H_
|
||||||
|
#define BLINKY_BAT_STATE_EVENT_H_
|
||||||
|
|
||||||
|
#include <stdbool.h>
|
||||||
|
#include <stdint.h>
|
||||||
|
|
||||||
|
#include <app_event_manager.h>
|
||||||
|
#include <app_event_manager_profiler_tracer.h>
|
||||||
|
|
||||||
|
#ifdef __cplusplus
|
||||||
|
extern "C" {
|
||||||
|
#endif
|
||||||
|
|
||||||
|
struct bat_state_event {
|
||||||
|
struct app_event_header header;
|
||||||
|
uint8_t soc;
|
||||||
|
bool charging;
|
||||||
|
bool full;
|
||||||
|
};
|
||||||
|
|
||||||
|
APP_EVENT_TYPE_DECLARE(bat_state_event);
|
||||||
|
|
||||||
|
#ifdef __cplusplus
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#endif /* BLINKY_BAT_STATE_EVENT_H_ */
|
||||||
@@ -4,12 +4,15 @@
|
|||||||
#include <app_event_manager.h>
|
#include <app_event_manager.h>
|
||||||
#include <app_event_manager_profiler_tracer.h>
|
#include <app_event_manager_profiler_tracer.h>
|
||||||
|
|
||||||
|
#include "keyboard_core.h"
|
||||||
|
|
||||||
#ifdef __cplusplus
|
#ifdef __cplusplus
|
||||||
extern "C" {
|
extern "C" {
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
struct hid_led_event {
|
struct hid_led_event {
|
||||||
struct app_event_header header;
|
struct app_event_header header;
|
||||||
|
enum hid_transport transport;
|
||||||
uint8_t led_bm;
|
uint8_t led_bm;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
27
inc/events/hid_report_sent_event.h
Normal file
27
inc/events/hid_report_sent_event.h
Normal file
@@ -0,0 +1,27 @@
|
|||||||
|
#ifndef BLINKY_HID_REPORT_SENT_EVENT_H_
|
||||||
|
#define BLINKY_HID_REPORT_SENT_EVENT_H_
|
||||||
|
|
||||||
|
#include <app_event_manager.h>
|
||||||
|
#include <app_event_manager_profiler_tracer.h>
|
||||||
|
|
||||||
|
#include "keyboard_core.h"
|
||||||
|
|
||||||
|
#ifdef __cplusplus
|
||||||
|
extern "C" {
|
||||||
|
#endif
|
||||||
|
|
||||||
|
struct hid_report_sent_event {
|
||||||
|
struct app_event_header header;
|
||||||
|
enum hid_transport transport;
|
||||||
|
enum keyboard_report_type report_type;
|
||||||
|
uint16_t sequence;
|
||||||
|
bool error;
|
||||||
|
};
|
||||||
|
|
||||||
|
APP_EVENT_TYPE_DECLARE(hid_report_sent_event);
|
||||||
|
|
||||||
|
#ifdef __cplusplus
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#endif /* BLINKY_HID_REPORT_SENT_EVENT_H_ */
|
||||||
28
inc/events/hid_transport_state_event.h
Normal file
28
inc/events/hid_transport_state_event.h
Normal file
@@ -0,0 +1,28 @@
|
|||||||
|
#ifndef BLINKY_HID_TRANSPORT_STATE_EVENT_H_
|
||||||
|
#define BLINKY_HID_TRANSPORT_STATE_EVENT_H_
|
||||||
|
|
||||||
|
#include <app_event_manager.h>
|
||||||
|
#include <app_event_manager_profiler_tracer.h>
|
||||||
|
|
||||||
|
#include "keyboard_core.h"
|
||||||
|
|
||||||
|
#ifdef __cplusplus
|
||||||
|
extern "C" {
|
||||||
|
#endif
|
||||||
|
|
||||||
|
struct hid_transport_state_event {
|
||||||
|
struct app_event_header header;
|
||||||
|
enum hid_transport transport;
|
||||||
|
bool ready;
|
||||||
|
bool keys_ready;
|
||||||
|
bool consumer_ready;
|
||||||
|
enum keyboard_protocol_mode protocol_mode;
|
||||||
|
};
|
||||||
|
|
||||||
|
APP_EVENT_TYPE_DECLARE(hid_transport_state_event);
|
||||||
|
|
||||||
|
#ifdef __cplusplus
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#endif /* BLINKY_HID_TRANSPORT_STATE_EVENT_H_ */
|
||||||
28
inc/events/hid_tx_report_event.h
Normal file
28
inc/events/hid_tx_report_event.h
Normal file
@@ -0,0 +1,28 @@
|
|||||||
|
#ifndef BLINKY_HID_TX_REPORT_EVENT_H_
|
||||||
|
#define BLINKY_HID_TX_REPORT_EVENT_H_
|
||||||
|
|
||||||
|
#include <app_event_manager.h>
|
||||||
|
#include <app_event_manager_profiler_tracer.h>
|
||||||
|
|
||||||
|
#include "keyboard_core.h"
|
||||||
|
|
||||||
|
#ifdef __cplusplus
|
||||||
|
extern "C" {
|
||||||
|
#endif
|
||||||
|
|
||||||
|
struct hid_tx_report_event {
|
||||||
|
struct app_event_header header;
|
||||||
|
enum hid_transport transport;
|
||||||
|
enum keyboard_report_type report_type;
|
||||||
|
enum keyboard_protocol_mode protocol_mode;
|
||||||
|
uint16_t sequence;
|
||||||
|
struct event_dyndata dyndata;
|
||||||
|
};
|
||||||
|
|
||||||
|
APP_EVENT_TYPE_DYNDATA_DECLARE(hid_tx_report_event);
|
||||||
|
|
||||||
|
#ifdef __cplusplus
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#endif /* BLINKY_HID_TX_REPORT_EVENT_H_ */
|
||||||
@@ -16,6 +16,7 @@ struct keyboard_hid_report_event {
|
|||||||
enum mode_switch_mode mode;
|
enum mode_switch_mode mode;
|
||||||
enum keyboard_report_type report_type;
|
enum keyboard_report_type report_type;
|
||||||
enum keyboard_protocol_mode protocol_mode;
|
enum keyboard_protocol_mode protocol_mode;
|
||||||
|
enum hid_queue_policy queue_policy;
|
||||||
struct event_dyndata dyndata;
|
struct event_dyndata dyndata;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@@ -12,6 +12,7 @@ extern "C" {
|
|||||||
|
|
||||||
struct set_protocol_event {
|
struct set_protocol_event {
|
||||||
struct app_event_header header;
|
struct app_event_header header;
|
||||||
|
enum hid_transport transport;
|
||||||
enum keyboard_protocol_mode protocol_mode;
|
enum keyboard_protocol_mode protocol_mode;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
22
inc/events/usb_cdc_rx_event.h
Normal file
22
inc/events/usb_cdc_rx_event.h
Normal file
@@ -0,0 +1,22 @@
|
|||||||
|
#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_ */
|
||||||
22
inc/events/usb_cdc_tx_event.h
Normal file
22
inc/events/usb_cdc_tx_event.h
Normal file
@@ -0,0 +1,22 @@
|
|||||||
|
#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_ */
|
||||||
24
inc/events/usb_device_state_event.h
Normal file
24
inc/events/usb_device_state_event.h
Normal file
@@ -0,0 +1,24 @@
|
|||||||
|
#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_ */
|
||||||
24
inc/events/usb_function_ready_event.h
Normal file
24
inc/events/usb_function_ready_event.h
Normal file
@@ -0,0 +1,24 @@
|
|||||||
|
#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_ */
|
||||||
21
inc/events/usb_prepare_event.h
Normal file
21
inc/events/usb_prepare_event.h
Normal file
@@ -0,0 +1,21 @@
|
|||||||
|
#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_ */
|
||||||
@@ -23,6 +23,17 @@ enum keyboard_report_type {
|
|||||||
KEYBOARD_REPORT_TYPE_CONSUMER,
|
KEYBOARD_REPORT_TYPE_CONSUMER,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
enum hid_queue_policy {
|
||||||
|
HID_QUEUE_POLICY_LATEST,
|
||||||
|
HID_QUEUE_POLICY_FIFO,
|
||||||
|
};
|
||||||
|
|
||||||
|
enum hid_transport {
|
||||||
|
HID_TRANSPORT_USB,
|
||||||
|
HID_TRANSPORT_BLE,
|
||||||
|
HID_TRANSPORT_COUNT,
|
||||||
|
};
|
||||||
|
|
||||||
enum keyboard_consumer_control {
|
enum keyboard_consumer_control {
|
||||||
KEYBOARD_CONSUMER_CTRL_MUTE,
|
KEYBOARD_CONSUMER_CTRL_MUTE,
|
||||||
KEYBOARD_CONSUMER_CTRL_VOLUME_UP,
|
KEYBOARD_CONSUMER_CTRL_VOLUME_UP,
|
||||||
|
|||||||
26
inc/protocol_module.h
Normal file
26
inc/protocol_module.h
Normal file
@@ -0,0 +1,26 @@
|
|||||||
|
#ifndef BLINKY_PROTOCOL_MODULE_H_
|
||||||
|
#define BLINKY_PROTOCOL_MODULE_H_
|
||||||
|
|
||||||
|
#include <stddef.h>
|
||||||
|
#include <stdint.h>
|
||||||
|
|
||||||
|
#ifdef __cplusplus
|
||||||
|
extern "C" {
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#define CDC_PROTO_TYPE_HELLO_REQ 0x01U
|
||||||
|
#define CDC_PROTO_TYPE_HELLO_RSP 0x02U
|
||||||
|
|
||||||
|
int protocol_module_process_cdc_packet(uint8_t req_type,
|
||||||
|
const uint8_t *req_payload,
|
||||||
|
size_t req_payload_len,
|
||||||
|
uint8_t *rsp_type,
|
||||||
|
uint8_t *rsp_payload,
|
||||||
|
size_t rsp_payload_buf_size,
|
||||||
|
size_t *rsp_payload_len);
|
||||||
|
|
||||||
|
#ifdef __cplusplus
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#endif /* BLINKY_PROTOCOL_MODULE_H_ */
|
||||||
15
inc/settings_loader_def.h
Normal file
15
inc/settings_loader_def.h
Normal file
@@ -0,0 +1,15 @@
|
|||||||
|
/*
|
||||||
|
* Settings must be loaded after HIDS has registered its dynamic GATT
|
||||||
|
* service and after BLE state is initialized.
|
||||||
|
*/
|
||||||
|
|
||||||
|
const struct {} settings_loader_def_include_once;
|
||||||
|
|
||||||
|
#include <caf/events/module_state_event.h>
|
||||||
|
|
||||||
|
static inline void get_req_modules(struct module_flags *mf)
|
||||||
|
{
|
||||||
|
module_flags_set_bit(mf, MODULE_IDX(main));
|
||||||
|
module_flags_set_bit(mf, MODULE_IDX(ble_hid_module));
|
||||||
|
module_flags_set_bit(mf, MODULE_IDX(ble_state));
|
||||||
|
}
|
||||||
26
inc/usb_device_module.h
Normal file
26
inc/usb_device_module.h
Normal file
@@ -0,0 +1,26 @@
|
|||||||
|
#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_ */
|
||||||
30
pm_static.yml
Normal file
30
pm_static.yml
Normal file
@@ -0,0 +1,30 @@
|
|||||||
|
mcuboot:
|
||||||
|
address: 0x0
|
||||||
|
size: 0x20000
|
||||||
|
|
||||||
|
mcuboot_pad:
|
||||||
|
address: 0x20000
|
||||||
|
size: 0x200
|
||||||
|
|
||||||
|
app:
|
||||||
|
address: 0x20200
|
||||||
|
size: 0xBFE00
|
||||||
|
|
||||||
|
mcuboot_primary:
|
||||||
|
orig_span: &id001
|
||||||
|
- mcuboot_pad
|
||||||
|
- app
|
||||||
|
span: *id001
|
||||||
|
address: 0x20000
|
||||||
|
size: 0xC0000
|
||||||
|
|
||||||
|
mcuboot_primary_app:
|
||||||
|
orig_span: &id002
|
||||||
|
- app
|
||||||
|
span: *id002
|
||||||
|
address: 0x20200
|
||||||
|
size: 0xBFE00
|
||||||
|
|
||||||
|
settings_storage:
|
||||||
|
address: 0xE0000
|
||||||
|
size: 0x20000
|
||||||
99
prj.conf
99
prj.conf
@@ -1,8 +1,13 @@
|
|||||||
CONFIG_CAF=y
|
CONFIG_CAF=y
|
||||||
CONFIG_CAF_BUTTONS=y
|
CONFIG_CAF_BUTTONS=y
|
||||||
CONFIG_CAF_BUTTONS_DEF_PATH="buttons_def.h"
|
CONFIG_CAF_BUTTONS_DEF_PATH="buttons_def.h"
|
||||||
|
CONFIG_CAF_CLICK_DETECTOR=y
|
||||||
|
CONFIG_CAF_CLICK_DETECTOR_DEF_PATH="click_detector_def.h"
|
||||||
CONFIG_GPIO=y
|
CONFIG_GPIO=y
|
||||||
CONFIG_I2C=y
|
CONFIG_I2C=y
|
||||||
|
CONFIG_LED=y
|
||||||
|
CONFIG_PWM=y
|
||||||
|
CONFIG_SPI=y
|
||||||
CONFIG_NRFX_RTC2=y
|
CONFIG_NRFX_RTC2=y
|
||||||
CONFIG_NRFX_GPPI=y
|
CONFIG_NRFX_GPPI=y
|
||||||
CONFIG_NRFX_QDEC=y
|
CONFIG_NRFX_QDEC=y
|
||||||
@@ -10,15 +15,107 @@ CONFIG_PINCTRL_DYNAMIC=y
|
|||||||
CONFIG_REBOOT=y
|
CONFIG_REBOOT=y
|
||||||
CONFIG_SENSOR=y
|
CONFIG_SENSOR=y
|
||||||
CONFIG_ADC=y
|
CONFIG_ADC=y
|
||||||
CONFIG_HEAP_MEM_POOL_SIZE=2048
|
CONFIG_DISPLAY=y
|
||||||
|
CONFIG_DISPLAY_LOG_LEVEL_ERR=y
|
||||||
|
CONFIG_MIPI_DBI_LOG_LEVEL_ERR=y
|
||||||
|
CONFIG_SETTINGS=y
|
||||||
|
CONFIG_SETTINGS_NVS=y
|
||||||
|
CONFIG_FLASH=y
|
||||||
|
CONFIG_FLASH_PAGE_LAYOUT=y
|
||||||
|
CONFIG_FLASH_MAP=y
|
||||||
|
CONFIG_NVS=y
|
||||||
|
CONFIG_HEAP_MEM_POOL_SIZE=4096
|
||||||
CONFIG_LOG=y
|
CONFIG_LOG=y
|
||||||
CONFIG_ASSERT=y
|
CONFIG_ASSERT=y
|
||||||
|
|
||||||
# USB HID next stack
|
# USB HID next stack
|
||||||
CONFIG_USB_DEVICE_STACK_NEXT=y
|
CONFIG_USB_DEVICE_STACK_NEXT=y
|
||||||
|
CONFIG_SERIAL=y
|
||||||
|
CONFIG_UART_INTERRUPT_DRIVEN=y
|
||||||
|
CONFIG_UART_LINE_CTRL=y
|
||||||
|
CONFIG_UART_USE_RUNTIME_CONFIGURE=y
|
||||||
|
CONFIG_NANOPB=y
|
||||||
CONFIG_USBD_HID_SUPPORT=y
|
CONFIG_USBD_HID_SUPPORT=y
|
||||||
|
CONFIG_USBD_CDC_ACM_CLASS=y
|
||||||
|
CONFIG_CDC_ACM_SERIAL_INITIALIZE_AT_BOOT=n
|
||||||
|
|
||||||
|
# BLE
|
||||||
|
CONFIG_BT=y
|
||||||
|
CONFIG_BT_PERIPHERAL=y
|
||||||
|
CONFIG_BT_SMP=y
|
||||||
|
CONFIG_BT_BONDABLE=y
|
||||||
|
CONFIG_BT_SETTINGS=y
|
||||||
|
CONFIG_BT_MAX_CONN=1
|
||||||
|
CONFIG_BT_MAX_PAIRED=1
|
||||||
|
CONFIG_BT_ATT_TX_COUNT=5
|
||||||
|
CONFIG_BT_CONN_CTX=y
|
||||||
|
CONFIG_BT_DEVICE_NAME="WH Mini Keyboard"
|
||||||
|
CONFIG_BT_DEVICE_APPEARANCE=961
|
||||||
|
|
||||||
|
CONFIG_BT_BAS=y
|
||||||
|
CONFIG_BT_HIDS=y
|
||||||
|
CONFIG_BT_HIDS_MAX_CLIENT_COUNT=1
|
||||||
|
CONFIG_BT_HIDS_DEFAULT_PERM_RW_ENCRYPT=y
|
||||||
|
CONFIG_BT_HIDS_INPUT_REP_MAX=2
|
||||||
|
CONFIG_BT_HIDS_OUTPUT_REP_MAX=1
|
||||||
|
CONFIG_BT_HIDS_FEATURE_REP_MAX=0
|
||||||
|
CONFIG_BT_GATT_UUID16_POOL_SIZE=40
|
||||||
|
CONFIG_BT_GATT_CHRC_POOL_SIZE=20
|
||||||
|
|
||||||
|
CONFIG_BT_DIS=y
|
||||||
|
CONFIG_BT_DIS_MANUF_NAME=y
|
||||||
|
CONFIG_BT_DIS_MANUF_NAME_STR="Atguigu"
|
||||||
|
CONFIG_BT_DIS_MODEL_NUMBER=y
|
||||||
|
CONFIG_BT_DIS_MODEL_NUMBER_STR="WH Mini Keyboard"
|
||||||
|
CONFIG_BT_DIS_PNP=y
|
||||||
|
CONFIG_BT_DIS_PNP_VID_SRC=2
|
||||||
|
CONFIG_BT_DIS_PNP_VID=0x1915
|
||||||
|
CONFIG_BT_DIS_PNP_PID=0x52F0
|
||||||
|
CONFIG_BT_DIS_PNP_VER=0x0100
|
||||||
|
|
||||||
# Power manager
|
# Power manager
|
||||||
CONFIG_CAF_POWER_MANAGER=y
|
CONFIG_CAF_POWER_MANAGER=y
|
||||||
CONFIG_CAF_POWER_MANAGER_TIMEOUT=120
|
CONFIG_CAF_POWER_MANAGER_TIMEOUT=120
|
||||||
# CONFIG_CAF_POWER_MANAGER_STAY_ON=y
|
# CONFIG_CAF_POWER_MANAGER_STAY_ON=y
|
||||||
|
|
||||||
|
# CAF BLE
|
||||||
|
CONFIG_CAF_SETTINGS_LOADER=y
|
||||||
|
CONFIG_CAF_SETTINGS_LOADER_DEF_PATH="settings_loader_def.h"
|
||||||
|
CONFIG_CAF_SETTINGS_LOADER_USE_THREAD=y
|
||||||
|
CONFIG_CAF_SETTINGS_LOADER_THREAD_STACK_SIZE=1792
|
||||||
|
CONFIG_CAF_BLE_STATE=y
|
||||||
|
CONFIG_CAF_BLE_STATE_SECURITY_REQ=y
|
||||||
|
CONFIG_CAF_BLE_STATE_PM=y
|
||||||
|
CONFIG_CAF_BLE_STATE_MAX_LOCAL_ID_BONDS=1
|
||||||
|
CONFIG_CAF_BLE_ADV=y
|
||||||
|
CONFIG_CAF_BLE_ADV_SUSPEND_ON_READY=y
|
||||||
|
CONFIG_CAF_BLE_ADV_FAST_ADV=y
|
||||||
|
CONFIG_CAF_BLE_ADV_FILTER_ACCEPT_LIST=y
|
||||||
|
CONFIG_CAF_BLE_ADV_MODULE_SUSPEND_EVENTS=y
|
||||||
|
CONFIG_CAF_BLE_BOND=y
|
||||||
|
CONFIG_CAF_BLE_BOND_PEER_ERASE_CLICK=y
|
||||||
|
CONFIG_CAF_BLE_BOND_PEER_ERASE_CLICK_KEY_ID=0x180
|
||||||
|
CONFIG_CAF_BLE_BOND_PEER_ERASE_CLICK_LONG=y
|
||||||
|
CONFIG_CAF_BLE_BOND_PEER_ERASE_CLICK_TIMEOUT=-1
|
||||||
|
CONFIG_CAF_MODULE_SUSPEND_EVENTS=y
|
||||||
|
CONFIG_BT_ADV_PROV_FLAGS=y
|
||||||
|
CONFIG_BT_ADV_PROV_GAP_APPEARANCE=y
|
||||||
|
CONFIG_BT_ADV_PROV_DEVICE_NAME=y
|
||||||
|
CONFIG_BT_ADV_PROV_DEVICE_NAME_SD=y
|
||||||
|
|
||||||
|
# LVGL
|
||||||
|
CONFIG_LVGL=y
|
||||||
|
CONFIG_LV_Z_AUTO_INIT=n
|
||||||
|
CONFIG_LV_Z_RUN_LVGL_ON_WORKQUEUE=y
|
||||||
|
CONFIG_LV_Z_LVGL_WORKQUEUE_STACK_SIZE=16384
|
||||||
|
CONFIG_LV_Z_LVGL_MUTEX=y
|
||||||
|
CONFIG_LV_COLOR_DEPTH_16=y
|
||||||
|
CONFIG_LV_COLOR_16_SWAP=y
|
||||||
|
CONFIG_LV_Z_BITS_PER_PIXEL=16
|
||||||
|
CONFIG_LV_Z_VDB_SIZE=25
|
||||||
|
CONFIG_LV_Z_DOUBLE_VDB=y
|
||||||
|
CONFIG_LV_Z_MEM_POOL_SIZE=16384
|
||||||
|
CONFIG_LV_USE_LABEL=y
|
||||||
|
CONFIG_LV_FONT_MONTSERRAT_14=y
|
||||||
|
CONFIG_LV_FONT_MONTSERRAT_32=y
|
||||||
|
CONFIG_MAIN_STACK_SIZE=4096
|
||||||
|
|||||||
21
proto/device_comm.proto
Normal file
21
proto/device_comm.proto
Normal file
@@ -0,0 +1,21 @@
|
|||||||
|
syntax = "proto3";
|
||||||
|
|
||||||
|
message HelloReq {
|
||||||
|
uint32 protocol_version = 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
message HelloRsp {
|
||||||
|
uint32 protocol_version = 1;
|
||||||
|
uint32 vendor_id = 2;
|
||||||
|
uint32 product_id = 3;
|
||||||
|
uint32 firmware_major = 4;
|
||||||
|
uint32 firmware_minor = 5;
|
||||||
|
uint32 capability_flags = 6;
|
||||||
|
}
|
||||||
|
|
||||||
|
message CdcPacketBody {
|
||||||
|
oneof body {
|
||||||
|
HelloReq hello_req = 1;
|
||||||
|
HelloRsp hello_rsp = 2;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,5 +1,6 @@
|
|||||||
#include <stdbool.h>
|
#include <stdbool.h>
|
||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
#include <app_event_manager.h>
|
#include <app_event_manager.h>
|
||||||
|
|
||||||
@@ -16,11 +17,15 @@
|
|||||||
#include <zephyr/logging/log.h>
|
#include <zephyr/logging/log.h>
|
||||||
#include <zephyr/pm/device.h>
|
#include <zephyr/pm/device.h>
|
||||||
|
|
||||||
|
#include "bat_state_event.h"
|
||||||
|
|
||||||
LOG_MODULE_REGISTER(MODULE, LOG_LEVEL_INF);
|
LOG_MODULE_REGISTER(MODULE, LOG_LEVEL_INF);
|
||||||
|
|
||||||
#define VBATT_NODE DT_PATH(vbatt)
|
#define VBATT_NODE DT_PATH(vbatt)
|
||||||
#define IP5306_NODE DT_NODELABEL(ip5306)
|
#define IP5306_NODE DT_NODELABEL(ip5306)
|
||||||
#define BATTERY_SAMPLE_INTERVAL K_SECONDS(1)
|
#define BATTERY_SAMPLE_INTERVAL K_SECONDS(1)
|
||||||
|
#define BATTERY_SOC_MIN_MV 3300
|
||||||
|
#define BATTERY_SOC_MAX_MV 4200
|
||||||
|
|
||||||
BUILD_ASSERT(DT_NODE_HAS_STATUS(VBATT_NODE, okay),
|
BUILD_ASSERT(DT_NODE_HAS_STATUS(VBATT_NODE, okay),
|
||||||
"Missing /vbatt voltage-divider node in devicetree");
|
"Missing /vbatt voltage-divider node in devicetree");
|
||||||
@@ -30,6 +35,12 @@ BUILD_ASSERT(DT_NODE_HAS_STATUS(IP5306_NODE, okay),
|
|||||||
static const struct device *const vbatt_dev = DEVICE_DT_GET(VBATT_NODE);
|
static const struct device *const vbatt_dev = DEVICE_DT_GET(VBATT_NODE);
|
||||||
static const struct device *const ip5306_dev = DEVICE_DT_GET(IP5306_NODE);
|
static const struct device *const ip5306_dev = DEVICE_DT_GET(IP5306_NODE);
|
||||||
static struct k_work_delayable battery_sample_work;
|
static struct k_work_delayable battery_sample_work;
|
||||||
|
static struct {
|
||||||
|
bool valid;
|
||||||
|
uint8_t soc;
|
||||||
|
bool charging;
|
||||||
|
bool full;
|
||||||
|
} last_bat_state;
|
||||||
static bool initialized;
|
static bool initialized;
|
||||||
static bool running;
|
static bool running;
|
||||||
|
|
||||||
@@ -52,6 +63,48 @@ static int measurement_enable(bool enable)
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static uint8_t battery_soc_from_mv(int voltage_mv)
|
||||||
|
{
|
||||||
|
const int span_mv = BATTERY_SOC_MAX_MV - BATTERY_SOC_MIN_MV;
|
||||||
|
int bucket;
|
||||||
|
|
||||||
|
if (voltage_mv <= BATTERY_SOC_MIN_MV) {
|
||||||
|
return 0U;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (voltage_mv >= BATTERY_SOC_MAX_MV) {
|
||||||
|
return 100U;
|
||||||
|
}
|
||||||
|
|
||||||
|
bucket = ((voltage_mv - BATTERY_SOC_MIN_MV) * 10 + (span_mv / 2)) / span_mv;
|
||||||
|
|
||||||
|
return (uint8_t)(bucket * 10);
|
||||||
|
}
|
||||||
|
|
||||||
|
static void submit_bat_state_event(uint8_t soc, bool charging, bool full)
|
||||||
|
{
|
||||||
|
struct bat_state_event *event;
|
||||||
|
|
||||||
|
if (last_bat_state.valid &&
|
||||||
|
(last_bat_state.soc == soc) &&
|
||||||
|
(last_bat_state.charging == charging) &&
|
||||||
|
(last_bat_state.full == full)) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
last_bat_state.valid = true;
|
||||||
|
last_bat_state.soc = soc;
|
||||||
|
last_bat_state.charging = charging;
|
||||||
|
last_bat_state.full = full;
|
||||||
|
|
||||||
|
event = new_bat_state_event();
|
||||||
|
event->soc = soc;
|
||||||
|
event->charging = charging;
|
||||||
|
event->full = full;
|
||||||
|
|
||||||
|
APP_EVENT_SUBMIT(event);
|
||||||
|
}
|
||||||
|
|
||||||
static void battery_sample_fn(struct k_work *work)
|
static void battery_sample_fn(struct k_work *work)
|
||||||
{
|
{
|
||||||
struct ip5306_status pmic_status;
|
struct ip5306_status pmic_status;
|
||||||
@@ -84,8 +137,9 @@ static void battery_sample_fn(struct k_work *work)
|
|||||||
}
|
}
|
||||||
|
|
||||||
voltage_mv = sensor_value_to_mv(&voltage);
|
voltage_mv = sensor_value_to_mv(&voltage);
|
||||||
// LOG_INF("Battery: %d mV, charging=%d, full=%d",
|
submit_bat_state_event(battery_soc_from_mv(voltage_mv),
|
||||||
// voltage_mv, pmic_status.charging, pmic_status.full);
|
pmic_status.charging,
|
||||||
|
pmic_status.full);
|
||||||
|
|
||||||
reschedule:
|
reschedule:
|
||||||
if (running) {
|
if (running) {
|
||||||
@@ -112,6 +166,7 @@ static int module_init(void)
|
|||||||
}
|
}
|
||||||
|
|
||||||
k_work_init_delayable(&battery_sample_work, battery_sample_fn);
|
k_work_init_delayable(&battery_sample_work, battery_sample_fn);
|
||||||
|
memset(&last_bat_state, 0, sizeof(last_bat_state));
|
||||||
power_manager_restrict(MODULE_IDX(MODULE), POWER_MANAGER_LEVEL_SUSPENDED);
|
power_manager_restrict(MODULE_IDX(MODULE), POWER_MANAGER_LEVEL_SUSPENDED);
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
|
|||||||
115
src/ble_adv_ctrl_module.c
Normal file
115
src/ble_adv_ctrl_module.c
Normal file
@@ -0,0 +1,115 @@
|
|||||||
|
#include <stdbool.h>
|
||||||
|
|
||||||
|
#include <app_event_manager.h>
|
||||||
|
|
||||||
|
#define MODULE ble_adv_ctrl_module
|
||||||
|
#include <caf/events/module_state_event.h>
|
||||||
|
#include <caf/events/module_suspend_event.h>
|
||||||
|
|
||||||
|
#include <zephyr/logging/log.h>
|
||||||
|
|
||||||
|
#include "mode_switch_event.h"
|
||||||
|
|
||||||
|
LOG_MODULE_REGISTER(MODULE, LOG_LEVEL_INF);
|
||||||
|
|
||||||
|
static bool initialized;
|
||||||
|
static bool running;
|
||||||
|
static bool ble_adv_suspended = true;
|
||||||
|
|
||||||
|
static void broadcast_ble_adv_req(bool suspend)
|
||||||
|
{
|
||||||
|
if (suspend) {
|
||||||
|
struct module_suspend_req_event *event = new_module_suspend_req_event();
|
||||||
|
|
||||||
|
event->sink_module_id = MODULE_ID(ble_adv);
|
||||||
|
event->src_module_id = MODULE_ID(MODULE);
|
||||||
|
APP_EVENT_SUBMIT(event);
|
||||||
|
} else {
|
||||||
|
struct module_resume_req_event *event = new_module_resume_req_event();
|
||||||
|
|
||||||
|
event->sink_module_id = MODULE_ID(ble_adv);
|
||||||
|
event->src_module_id = MODULE_ID(MODULE);
|
||||||
|
APP_EVENT_SUBMIT(event);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static int module_init(void)
|
||||||
|
{
|
||||||
|
ble_adv_suspended = true;
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
static int module_start(void)
|
||||||
|
{
|
||||||
|
if (running) {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
running = true;
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
static void module_pause(void)
|
||||||
|
{
|
||||||
|
running = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
static bool handle_mode_switch_event(const struct mode_switch_event *event)
|
||||||
|
{
|
||||||
|
bool should_suspend;
|
||||||
|
|
||||||
|
if (!running) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
should_suspend = (event->mode != MODE_SWITCH_BLE);
|
||||||
|
|
||||||
|
if (should_suspend != ble_adv_suspended) {
|
||||||
|
ble_adv_suspended = should_suspend;
|
||||||
|
broadcast_ble_adv_req(should_suspend);
|
||||||
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
static bool app_event_handler(const struct app_event_header *aeh)
|
||||||
|
{
|
||||||
|
if (is_mode_switch_event(aeh)) {
|
||||||
|
return handle_mode_switch_event(cast_mode_switch_event(aeh));
|
||||||
|
}
|
||||||
|
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
APP_EVENT_LISTENER(MODULE, app_event_handler);
|
||||||
|
APP_EVENT_SUBSCRIBE(MODULE, mode_switch_event);
|
||||||
|
APP_EVENT_SUBSCRIBE(MODULE, module_state_event);
|
||||||
23
src/ble_adv_uuid16.c
Normal file
23
src/ble_adv_uuid16.c
Normal file
@@ -0,0 +1,23 @@
|
|||||||
|
#include <zephyr/bluetooth/uuid.h>
|
||||||
|
|
||||||
|
#include <bluetooth/adv_prov.h>
|
||||||
|
|
||||||
|
static int get_data(struct bt_data *sd, const struct bt_le_adv_prov_adv_state *state,
|
||||||
|
struct bt_le_adv_prov_feedback *fb)
|
||||||
|
{
|
||||||
|
static const uint8_t data[] = {
|
||||||
|
BT_UUID_16_ENCODE(BT_UUID_HIDS_VAL),
|
||||||
|
BT_UUID_16_ENCODE(BT_UUID_BAS_VAL),
|
||||||
|
};
|
||||||
|
|
||||||
|
ARG_UNUSED(state);
|
||||||
|
ARG_UNUSED(fb);
|
||||||
|
|
||||||
|
sd->type = BT_DATA_UUID16_ALL;
|
||||||
|
sd->data_len = sizeof(data);
|
||||||
|
sd->data = data;
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
BT_LE_ADV_PROV_SD_PROVIDER_REGISTER(uuid16_all, get_data);
|
||||||
121
src/ble_bas_module.c
Normal file
121
src/ble_bas_module.c
Normal file
@@ -0,0 +1,121 @@
|
|||||||
|
#include <stdbool.h>
|
||||||
|
#include <stdint.h>
|
||||||
|
|
||||||
|
#include <app_event_manager.h>
|
||||||
|
|
||||||
|
#define MODULE ble_bas_module
|
||||||
|
#include <caf/events/module_state_event.h>
|
||||||
|
|
||||||
|
#include <caf/events/ble_common_event.h>
|
||||||
|
#include <zephyr/bluetooth/services/bas.h>
|
||||||
|
#include <zephyr/logging/log.h>
|
||||||
|
|
||||||
|
#include "bat_state_event.h"
|
||||||
|
|
||||||
|
LOG_MODULE_REGISTER(MODULE, LOG_LEVEL_INF);
|
||||||
|
|
||||||
|
static uint8_t current_soc = 100U;
|
||||||
|
static bool initialized;
|
||||||
|
static bool running;
|
||||||
|
static bool ble_ready;
|
||||||
|
|
||||||
|
static int module_init(void)
|
||||||
|
{
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
static int module_start(void)
|
||||||
|
{
|
||||||
|
int err;
|
||||||
|
|
||||||
|
if (running) {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
running = true;
|
||||||
|
|
||||||
|
if (!ble_ready) {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
err = bt_bas_set_battery_level(current_soc);
|
||||||
|
if (err) {
|
||||||
|
LOG_WRN("bt_bas_set_battery_level failed (%d)", err);
|
||||||
|
return err;
|
||||||
|
}
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
static void module_pause(void)
|
||||||
|
{
|
||||||
|
running = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
static bool handle_bat_state_event(const struct bat_state_event *event)
|
||||||
|
{
|
||||||
|
current_soc = event->soc;
|
||||||
|
|
||||||
|
if (running && ble_ready) {
|
||||||
|
int err = bt_bas_set_battery_level(current_soc);
|
||||||
|
|
||||||
|
if (err) {
|
||||||
|
LOG_WRN("bt_bas_set_battery_level failed (%d)", err);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
static bool app_event_handler(const struct app_event_header *aeh)
|
||||||
|
{
|
||||||
|
if (is_bat_state_event(aeh)) {
|
||||||
|
return handle_bat_state_event(cast_bat_state_event(aeh));
|
||||||
|
}
|
||||||
|
|
||||||
|
if (is_module_state_event(aeh)) {
|
||||||
|
const struct module_state_event *event = cast_module_state_event(aeh);
|
||||||
|
int err;
|
||||||
|
|
||||||
|
if (check_state(event, MODULE_ID(main), MODULE_STATE_READY)) {
|
||||||
|
if (!initialized) {
|
||||||
|
err = module_init();
|
||||||
|
if (err) {
|
||||||
|
module_set_state(MODULE_STATE_ERROR);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
initialized = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
err = module_start();
|
||||||
|
if (err) {
|
||||||
|
module_set_state(MODULE_STATE_ERROR);
|
||||||
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (check_state(event, MODULE_ID(ble_state), MODULE_STATE_READY)) {
|
||||||
|
ble_ready = true;
|
||||||
|
|
||||||
|
if (running) {
|
||||||
|
err = bt_bas_set_battery_level(current_soc);
|
||||||
|
if (err) {
|
||||||
|
LOG_WRN("bt_bas_set_battery_level failed (%d)", err);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
module_set_state(MODULE_STATE_READY);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
APP_EVENT_LISTENER(MODULE, app_event_handler);
|
||||||
|
APP_EVENT_SUBSCRIBE(MODULE, bat_state_event);
|
||||||
|
APP_EVENT_SUBSCRIBE(MODULE, module_state_event);
|
||||||
491
src/ble_hid_module.c
Normal file
491
src/ble_hid_module.c
Normal file
@@ -0,0 +1,491 @@
|
|||||||
|
#include <errno.h>
|
||||||
|
#include <stdbool.h>
|
||||||
|
#include <stdint.h>
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
|
#include <app_event_manager.h>
|
||||||
|
|
||||||
|
#define MODULE ble_hid_module
|
||||||
|
#include <caf/events/module_state_event.h>
|
||||||
|
#include <caf/events/power_event.h>
|
||||||
|
|
||||||
|
#include <caf/events/ble_common_event.h>
|
||||||
|
#include <bluetooth/services/hids.h>
|
||||||
|
#include <zephyr/logging/log.h>
|
||||||
|
|
||||||
|
#include "hid_led_event.h"
|
||||||
|
#include "hid_report_sent_event.h"
|
||||||
|
#include "hid_transport_state_event.h"
|
||||||
|
#include "hid_tx_report_event.h"
|
||||||
|
#include "keyboard_core.h"
|
||||||
|
#include "set_protocol_event.h"
|
||||||
|
|
||||||
|
LOG_MODULE_REGISTER(MODULE, LOG_LEVEL_INF);
|
||||||
|
|
||||||
|
#define BLE_HID_KEYS_REPORT_ID 0x01
|
||||||
|
#define BLE_HID_CONSUMER_REPORT_ID 0x02
|
||||||
|
#define BLE_HID_KEYS_REPORT_IDX 0
|
||||||
|
#define BLE_HID_CONSUMER_REPORT_IDX 1
|
||||||
|
#define BLE_HID_KEYS_LED_REPORT_SIZE 1U
|
||||||
|
#define BASE_USB_HID_SPEC_VERSION 0x0101
|
||||||
|
|
||||||
|
struct in_flight_report {
|
||||||
|
bool active;
|
||||||
|
enum keyboard_report_type report_type;
|
||||||
|
uint16_t sequence;
|
||||||
|
};
|
||||||
|
|
||||||
|
BT_HIDS_DEF(hids_obj,
|
||||||
|
KEYBOARD_NKRO_REPORT_SIZE,
|
||||||
|
KEYBOARD_CONSUMER_REPORT_SIZE,
|
||||||
|
BLE_HID_KEYS_LED_REPORT_SIZE);
|
||||||
|
|
||||||
|
static struct bt_conn *active_conn;
|
||||||
|
static struct in_flight_report in_flight;
|
||||||
|
static enum keyboard_protocol_mode protocol_mode = KEYBOARD_PROTOCOL_MODE_REPORT;
|
||||||
|
static bool initialized;
|
||||||
|
static bool running;
|
||||||
|
static bool secured;
|
||||||
|
static bool keyboard_report_notify_enabled;
|
||||||
|
static bool consumer_report_notify_enabled;
|
||||||
|
static bool boot_keyboard_notify_enabled;
|
||||||
|
|
||||||
|
static const uint8_t hid_report_desc[] = {
|
||||||
|
0x05, 0x01, /* Usage Page (Generic Desktop) */
|
||||||
|
0x09, 0x06, /* Usage (Keyboard) */
|
||||||
|
0xA1, 0x01, /* Collection (Application) */
|
||||||
|
0x85, BLE_HID_KEYS_REPORT_ID,
|
||||||
|
0x05, 0x07, /* Usage Page (Keyboard/Keypad) */
|
||||||
|
0x19, 0xE0, /* Usage Minimum (0xE0) */
|
||||||
|
0x29, 0xE7, /* Usage Maximum (0xE7) */
|
||||||
|
0x15, 0x00, /* Logical Minimum (0) */
|
||||||
|
0x25, 0x01, /* Logical Maximum (1) */
|
||||||
|
0x75, 0x01, /* Report Size (1) */
|
||||||
|
0x95, 0x08, /* Report Count (8) */
|
||||||
|
0x81, 0x02, /* Input (Data,Var,Abs) */
|
||||||
|
0x05, 0x07, /* Usage Page (Keyboard/Keypad) */
|
||||||
|
0x19, 0x00, /* Usage Minimum (0x00) */
|
||||||
|
0x2A, 0xDF, 0x00, /* Usage Maximum (0x00DF) */
|
||||||
|
0x15, 0x00, /* Logical Minimum (0) */
|
||||||
|
0x25, 0x01, /* Logical Maximum (1) */
|
||||||
|
0x75, 0x01, /* Report Size (1) */
|
||||||
|
0x96, 0xE0, 0x00, /* Report Count (224) */
|
||||||
|
0x81, 0x02, /* Input (Data,Var,Abs) */
|
||||||
|
0x85, BLE_HID_KEYS_REPORT_ID,
|
||||||
|
0x05, 0x08, /* Usage Page (LEDs) */
|
||||||
|
0x19, 0x01, /* Usage Minimum (1) */
|
||||||
|
0x29, 0x05, /* Usage Maximum (5) */
|
||||||
|
0x15, 0x00, /* Logical Minimum (0) */
|
||||||
|
0x25, 0x01, /* Logical Maximum (1) */
|
||||||
|
0x75, 0x01, /* Report Size (1) */
|
||||||
|
0x95, 0x05, /* Report Count (5) */
|
||||||
|
0x91, 0x02, /* Output (Data,Var,Abs) */
|
||||||
|
0x75, 0x03, /* Report Size (3) */
|
||||||
|
0x95, 0x01, /* Report Count (1) */
|
||||||
|
0x91, 0x01, /* Output (Const,Array,Abs) */
|
||||||
|
0xC0, /* End Collection */
|
||||||
|
0x05, 0x0C, /* Usage Page (Consumer) */
|
||||||
|
0x09, 0x01, /* Usage (Consumer Control) */
|
||||||
|
0xA1, 0x01, /* Collection (Application) */
|
||||||
|
0x85, BLE_HID_CONSUMER_REPORT_ID,
|
||||||
|
0x15, 0x00, /* Logical Minimum (0) */
|
||||||
|
0x26, 0xFF, 0x03, /* Logical Maximum (1023) */
|
||||||
|
0x19, 0x00, /* Usage Minimum (0) */
|
||||||
|
0x2A, 0xFF, 0x03, /* Usage Maximum (1023) */
|
||||||
|
0x75, 0x10, /* Report Size (16) */
|
||||||
|
0x95, 0x01, /* Report Count (1) */
|
||||||
|
0x81, 0x00, /* Input (Data,Array,Abs) */
|
||||||
|
0xC0 /* End Collection */
|
||||||
|
};
|
||||||
|
|
||||||
|
static void submit_set_protocol_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);
|
||||||
|
|
||||||
|
event->transport = HID_TRANSPORT_BLE;
|
||||||
|
event->ready = ready;
|
||||||
|
event->protocol_mode = protocol_mode;
|
||||||
|
event->keys_ready = ready &&
|
||||||
|
((protocol_mode == KEYBOARD_PROTOCOL_MODE_BOOT) ?
|
||||||
|
boot_keyboard_notify_enabled :
|
||||||
|
keyboard_report_notify_enabled);
|
||||||
|
event->consumer_ready = ready &&
|
||||||
|
(protocol_mode == KEYBOARD_PROTOCOL_MODE_REPORT) &&
|
||||||
|
consumer_report_notify_enabled;
|
||||||
|
|
||||||
|
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)
|
||||||
|
{
|
||||||
|
bool enabled = (evt == BT_HIDS_CCCD_EVT_NOTIFY_ENABLED);
|
||||||
|
|
||||||
|
if (report_id == BLE_HID_KEYS_REPORT_ID) {
|
||||||
|
keyboard_report_notify_enabled = enabled;
|
||||||
|
} else if (report_id == BLE_HID_CONSUMER_REPORT_ID) {
|
||||||
|
consumer_report_notify_enabled = enabled;
|
||||||
|
}
|
||||||
|
|
||||||
|
submit_transport_state_event();
|
||||||
|
}
|
||||||
|
|
||||||
|
static void boot_keyboard_notify_handler(enum bt_hids_notify_evt evt)
|
||||||
|
{
|
||||||
|
boot_keyboard_notify_enabled = (evt == BT_HIDS_CCCD_EVT_NOTIFY_ENABLED);
|
||||||
|
submit_transport_state_event();
|
||||||
|
}
|
||||||
|
|
||||||
|
static void hid_report_complete_cb(struct bt_conn *conn, void *user_data)
|
||||||
|
{
|
||||||
|
ARG_UNUSED(conn);
|
||||||
|
ARG_UNUSED(user_data);
|
||||||
|
|
||||||
|
if (!in_flight.active) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
submit_hid_report_sent_event(in_flight.report_type, in_flight.sequence, false);
|
||||||
|
in_flight.active = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
static void keyboard_led_report_common(struct bt_hids_rep *rep, bool write)
|
||||||
|
{
|
||||||
|
if (!write || (rep->data == NULL) || (rep->size < BLE_HID_KEYS_LED_REPORT_SIZE)) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
submit_hid_led_event(rep->data[0]);
|
||||||
|
}
|
||||||
|
|
||||||
|
static void keyboard_led_report_handler(struct bt_hids_rep *rep,
|
||||||
|
struct bt_conn *conn,
|
||||||
|
bool write)
|
||||||
|
{
|
||||||
|
ARG_UNUSED(conn);
|
||||||
|
|
||||||
|
keyboard_led_report_common(rep, write);
|
||||||
|
}
|
||||||
|
|
||||||
|
static void boot_keyboard_led_report_handler(struct bt_hids_rep *rep,
|
||||||
|
struct bt_conn *conn,
|
||||||
|
bool write)
|
||||||
|
{
|
||||||
|
ARG_UNUSED(conn);
|
||||||
|
|
||||||
|
keyboard_led_report_common(rep, write);
|
||||||
|
}
|
||||||
|
|
||||||
|
static void pm_evt_handler(enum bt_hids_pm_evt evt, struct bt_conn *conn)
|
||||||
|
{
|
||||||
|
ARG_UNUSED(conn);
|
||||||
|
|
||||||
|
switch (evt) {
|
||||||
|
case BT_HIDS_PM_EVT_BOOT_MODE_ENTERED:
|
||||||
|
protocol_mode = KEYBOARD_PROTOCOL_MODE_BOOT;
|
||||||
|
break;
|
||||||
|
|
||||||
|
case BT_HIDS_PM_EVT_REPORT_MODE_ENTERED:
|
||||||
|
protocol_mode = KEYBOARD_PROTOCOL_MODE_REPORT;
|
||||||
|
break;
|
||||||
|
|
||||||
|
default:
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
submit_set_protocol_event();
|
||||||
|
submit_transport_state_event();
|
||||||
|
}
|
||||||
|
|
||||||
|
static int module_init(void)
|
||||||
|
{
|
||||||
|
struct bt_hids_init_param hids_init_param = { 0 };
|
||||||
|
struct bt_hids_inp_rep *input_report;
|
||||||
|
struct bt_hids_outp_feat_rep *output_report;
|
||||||
|
|
||||||
|
hids_init_param.info.bcd_hid = BASE_USB_HID_SPEC_VERSION;
|
||||||
|
hids_init_param.info.b_country_code = 0x00;
|
||||||
|
hids_init_param.info.flags = BT_HIDS_REMOTE_WAKE | BT_HIDS_NORMALLY_CONNECTABLE;
|
||||||
|
hids_init_param.rep_map.data = hid_report_desc;
|
||||||
|
hids_init_param.rep_map.size = sizeof(hid_report_desc);
|
||||||
|
hids_init_param.pm_evt_handler = pm_evt_handler;
|
||||||
|
hids_init_param.is_kb = true;
|
||||||
|
hids_init_param.boot_kb_notif_handler = boot_keyboard_notify_handler;
|
||||||
|
hids_init_param.boot_kb_outp_rep_handler = boot_keyboard_led_report_handler;
|
||||||
|
|
||||||
|
input_report = &hids_init_param.inp_rep_group_init.reports[BLE_HID_KEYS_REPORT_IDX];
|
||||||
|
input_report->id = BLE_HID_KEYS_REPORT_ID;
|
||||||
|
input_report->size = KEYBOARD_NKRO_REPORT_SIZE;
|
||||||
|
input_report->handler_ext = input_report_notify_handler;
|
||||||
|
hids_init_param.inp_rep_group_init.cnt++;
|
||||||
|
|
||||||
|
input_report = &hids_init_param.inp_rep_group_init.reports[BLE_HID_CONSUMER_REPORT_IDX];
|
||||||
|
input_report->id = BLE_HID_CONSUMER_REPORT_ID;
|
||||||
|
input_report->size = KEYBOARD_CONSUMER_REPORT_SIZE;
|
||||||
|
input_report->handler_ext = input_report_notify_handler;
|
||||||
|
hids_init_param.inp_rep_group_init.cnt++;
|
||||||
|
|
||||||
|
output_report = &hids_init_param.outp_rep_group_init.reports[0];
|
||||||
|
output_report->id = BLE_HID_KEYS_REPORT_ID;
|
||||||
|
output_report->size = BLE_HID_KEYS_LED_REPORT_SIZE;
|
||||||
|
output_report->handler = keyboard_led_report_handler;
|
||||||
|
hids_init_param.outp_rep_group_init.cnt = 1U;
|
||||||
|
|
||||||
|
return bt_hids_init(&hids_obj, &hids_init_param);
|
||||||
|
}
|
||||||
|
|
||||||
|
static int module_start(void)
|
||||||
|
{
|
||||||
|
if (running) {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
running = true;
|
||||||
|
submit_transport_state_event();
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
static void module_pause(void)
|
||||||
|
{
|
||||||
|
if (!running) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
in_flight.active = false;
|
||||||
|
running = false;
|
||||||
|
submit_transport_state_event();
|
||||||
|
}
|
||||||
|
|
||||||
|
static void reset_connection_state(void)
|
||||||
|
{
|
||||||
|
active_conn = NULL;
|
||||||
|
secured = false;
|
||||||
|
keyboard_report_notify_enabled = false;
|
||||||
|
consumer_report_notify_enabled = false;
|
||||||
|
boot_keyboard_notify_enabled = false;
|
||||||
|
protocol_mode = KEYBOARD_PROTOCOL_MODE_REPORT;
|
||||||
|
in_flight.active = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
static bool handle_ble_peer_event(const struct ble_peer_event *event)
|
||||||
|
{
|
||||||
|
int err;
|
||||||
|
|
||||||
|
switch (event->state) {
|
||||||
|
case PEER_STATE_CONNECTED:
|
||||||
|
if (active_conn != NULL) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
active_conn = event->id;
|
||||||
|
protocol_mode = KEYBOARD_PROTOCOL_MODE_REPORT;
|
||||||
|
submit_set_protocol_event();
|
||||||
|
err = bt_hids_connected(&hids_obj, event->id);
|
||||||
|
if (err) {
|
||||||
|
LOG_ERR("bt_hids_connected failed (%d)", err);
|
||||||
|
}
|
||||||
|
submit_transport_state_event();
|
||||||
|
return false;
|
||||||
|
|
||||||
|
case PEER_STATE_SECURED:
|
||||||
|
if (active_conn != event->id) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
secured = true;
|
||||||
|
submit_transport_state_event();
|
||||||
|
return false;
|
||||||
|
|
||||||
|
case PEER_STATE_DISCONNECTED:
|
||||||
|
if (active_conn != event->id) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
err = bt_hids_disconnected(&hids_obj, event->id);
|
||||||
|
if (err) {
|
||||||
|
LOG_WRN("bt_hids_disconnected failed (%d)", err);
|
||||||
|
}
|
||||||
|
|
||||||
|
reset_connection_state();
|
||||||
|
submit_transport_state_event();
|
||||||
|
return false;
|
||||||
|
|
||||||
|
default:
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static bool handle_hid_tx_report_event(const struct hid_tx_report_event *event)
|
||||||
|
{
|
||||||
|
int err;
|
||||||
|
|
||||||
|
if (!running || (event->transport != HID_TRANSPORT_BLE) || in_flight.active) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
if ((active_conn == NULL) || !secured) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (event->report_type == KEYBOARD_REPORT_TYPE_KEYS) {
|
||||||
|
if (event->protocol_mode != protocol_mode) {
|
||||||
|
LOG_WRN("Drop BLE keys report due to protocol mismatch");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
in_flight.active = true;
|
||||||
|
in_flight.report_type = event->report_type;
|
||||||
|
in_flight.sequence = event->sequence;
|
||||||
|
|
||||||
|
if (protocol_mode == KEYBOARD_PROTOCOL_MODE_BOOT) {
|
||||||
|
err = bt_hids_boot_kb_inp_rep_send(&hids_obj,
|
||||||
|
active_conn,
|
||||||
|
event->dyndata.data,
|
||||||
|
(uint8_t)event->dyndata.size,
|
||||||
|
hid_report_complete_cb);
|
||||||
|
} else {
|
||||||
|
err = bt_hids_inp_rep_send(&hids_obj,
|
||||||
|
active_conn,
|
||||||
|
BLE_HID_KEYS_REPORT_IDX,
|
||||||
|
event->dyndata.data,
|
||||||
|
(uint8_t)event->dyndata.size,
|
||||||
|
hid_report_complete_cb);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (err) {
|
||||||
|
in_flight.active = false;
|
||||||
|
LOG_WRN("BLE keyboard report submit failed (%d)", err);
|
||||||
|
submit_hid_report_sent_event(KEYBOARD_REPORT_TYPE_KEYS,
|
||||||
|
event->sequence, true);
|
||||||
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (event->report_type == KEYBOARD_REPORT_TYPE_CONSUMER) {
|
||||||
|
if (protocol_mode != KEYBOARD_PROTOCOL_MODE_REPORT) {
|
||||||
|
LOG_WRN("Drop BLE consumer report in boot mode");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
in_flight.active = true;
|
||||||
|
in_flight.report_type = event->report_type;
|
||||||
|
in_flight.sequence = event->sequence;
|
||||||
|
|
||||||
|
err = bt_hids_inp_rep_send(&hids_obj,
|
||||||
|
active_conn,
|
||||||
|
BLE_HID_CONSUMER_REPORT_IDX,
|
||||||
|
event->dyndata.data,
|
||||||
|
(uint8_t)event->dyndata.size,
|
||||||
|
hid_report_complete_cb);
|
||||||
|
if (err) {
|
||||||
|
in_flight.active = false;
|
||||||
|
LOG_WRN("BLE consumer report submit failed (%d)", err);
|
||||||
|
submit_hid_report_sent_event(KEYBOARD_REPORT_TYPE_CONSUMER,
|
||||||
|
event->sequence, true);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
static bool app_event_handler(const struct app_event_header *aeh)
|
||||||
|
{
|
||||||
|
if (is_hid_tx_report_event(aeh)) {
|
||||||
|
return handle_hid_tx_report_event(cast_hid_tx_report_event(aeh));
|
||||||
|
}
|
||||||
|
|
||||||
|
if (is_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);
|
||||||
|
} 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, module_state_event);
|
||||||
|
APP_EVENT_SUBSCRIBE(MODULE, hid_tx_report_event);
|
||||||
|
APP_EVENT_SUBSCRIBE_EARLY(MODULE, ble_peer_event);
|
||||||
|
APP_EVENT_SUBSCRIBE_EARLY(MODULE, power_down_event);
|
||||||
|
APP_EVENT_SUBSCRIBE(MODULE, wake_up_event);
|
||||||
275
src/cdc_wrapper_module.c
Normal file
275
src/cdc_wrapper_module.c
Normal file
@@ -0,0 +1,275 @@
|
|||||||
|
#include <errno.h>
|
||||||
|
#include <stdbool.h>
|
||||||
|
#include <stddef.h>
|
||||||
|
#include <stdint.h>
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
|
#include <app_event_manager.h>
|
||||||
|
|
||||||
|
#define MODULE cdc_wrapper_module
|
||||||
|
#include <caf/events/module_state_event.h>
|
||||||
|
#include <caf/events/power_event.h>
|
||||||
|
|
||||||
|
#include <zephyr/logging/log.h>
|
||||||
|
|
||||||
|
#include "protocol_module.h"
|
||||||
|
#include "usb_cdc_rx_event.h"
|
||||||
|
#include "usb_cdc_tx_event.h"
|
||||||
|
|
||||||
|
LOG_MODULE_REGISTER(MODULE, LOG_LEVEL_INF);
|
||||||
|
|
||||||
|
#define CDC_WRAPPER_HEAD1 0xAAU
|
||||||
|
#define CDC_WRAPPER_HEAD2 0x55U
|
||||||
|
#define CDC_WRAPPER_MAX_PAYLOAD_LEN 64U
|
||||||
|
#define CDC_WRAPPER_MAX_FRAME_LEN (2U + 1U + 1U + CDC_WRAPPER_MAX_PAYLOAD_LEN + 1U)
|
||||||
|
|
||||||
|
enum frame_parse_state {
|
||||||
|
FRAME_PARSE_HEAD1,
|
||||||
|
FRAME_PARSE_HEAD2,
|
||||||
|
FRAME_PARSE_LEN,
|
||||||
|
FRAME_PARSE_TYPE,
|
||||||
|
FRAME_PARSE_PAYLOAD,
|
||||||
|
FRAME_PARSE_CHECKSUM,
|
||||||
|
};
|
||||||
|
|
||||||
|
struct cdc_frame_parser {
|
||||||
|
enum frame_parse_state state;
|
||||||
|
uint8_t len;
|
||||||
|
uint8_t type;
|
||||||
|
uint8_t checksum;
|
||||||
|
uint8_t payload[CDC_WRAPPER_MAX_PAYLOAD_LEN];
|
||||||
|
size_t payload_pos;
|
||||||
|
};
|
||||||
|
|
||||||
|
static bool initialized;
|
||||||
|
static bool running;
|
||||||
|
static struct cdc_frame_parser parser;
|
||||||
|
|
||||||
|
static void parser_reset(void)
|
||||||
|
{
|
||||||
|
parser.state = FRAME_PARSE_HEAD1;
|
||||||
|
parser.len = 0U;
|
||||||
|
parser.type = 0U;
|
||||||
|
parser.checksum = 0U;
|
||||||
|
parser.payload_pos = 0U;
|
||||||
|
}
|
||||||
|
|
||||||
|
static uint8_t frame_checksum(uint8_t len, uint8_t type,
|
||||||
|
const uint8_t *payload, size_t payload_len)
|
||||||
|
{
|
||||||
|
uint8_t checksum = CDC_WRAPPER_HEAD1 ^ CDC_WRAPPER_HEAD2 ^ len ^ type;
|
||||||
|
|
||||||
|
for (size_t i = 0; i < payload_len; i++) {
|
||||||
|
checksum ^= payload[i];
|
||||||
|
}
|
||||||
|
|
||||||
|
return checksum;
|
||||||
|
}
|
||||||
|
|
||||||
|
static void submit_tx_frame(uint8_t type, const uint8_t *payload, size_t payload_len)
|
||||||
|
{
|
||||||
|
struct usb_cdc_tx_event *event;
|
||||||
|
size_t frame_len = 2U + 1U + 1U + payload_len + 1U;
|
||||||
|
|
||||||
|
event = new_usb_cdc_tx_event(frame_len);
|
||||||
|
event->dyndata.data[0] = CDC_WRAPPER_HEAD1;
|
||||||
|
event->dyndata.data[1] = CDC_WRAPPER_HEAD2;
|
||||||
|
event->dyndata.data[2] = (uint8_t)payload_len;
|
||||||
|
event->dyndata.data[3] = type;
|
||||||
|
memcpy(&event->dyndata.data[4], payload, payload_len);
|
||||||
|
event->dyndata.data[4U + payload_len] =
|
||||||
|
frame_checksum((uint8_t)payload_len, type, payload, payload_len);
|
||||||
|
APP_EVENT_SUBMIT(event);
|
||||||
|
}
|
||||||
|
|
||||||
|
static void process_complete_frame(void)
|
||||||
|
{
|
||||||
|
uint8_t rsp_type;
|
||||||
|
uint8_t rsp_payload[CDC_WRAPPER_MAX_PAYLOAD_LEN];
|
||||||
|
size_t rsp_payload_len = 0U;
|
||||||
|
int err;
|
||||||
|
|
||||||
|
err = protocol_module_process_cdc_packet(parser.type,
|
||||||
|
parser.payload,
|
||||||
|
parser.payload_pos,
|
||||||
|
&rsp_type,
|
||||||
|
rsp_payload,
|
||||||
|
sizeof(rsp_payload),
|
||||||
|
&rsp_payload_len);
|
||||||
|
if (err == -ENOTSUP) {
|
||||||
|
LOG_WRN("Ignore unsupported CDC packet type:0x%02x", parser.type);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (err) {
|
||||||
|
LOG_WRN("Protocol processing failed (%d)", err);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
LOG_INF("CDC HelloRsp encoded len:%u", (uint32_t)rsp_payload_len);
|
||||||
|
submit_tx_frame(rsp_type, rsp_payload, rsp_payload_len);
|
||||||
|
}
|
||||||
|
|
||||||
|
static void consume_byte(uint8_t byte)
|
||||||
|
{
|
||||||
|
switch (parser.state) {
|
||||||
|
case FRAME_PARSE_HEAD1:
|
||||||
|
if (byte == CDC_WRAPPER_HEAD1) {
|
||||||
|
parser.state = FRAME_PARSE_HEAD2;
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
|
||||||
|
case FRAME_PARSE_HEAD2:
|
||||||
|
if (byte == CDC_WRAPPER_HEAD2) {
|
||||||
|
parser.state = FRAME_PARSE_LEN;
|
||||||
|
} else if (byte != CDC_WRAPPER_HEAD1) {
|
||||||
|
parser.state = FRAME_PARSE_HEAD1;
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
|
||||||
|
case FRAME_PARSE_LEN:
|
||||||
|
if (byte > CDC_WRAPPER_MAX_PAYLOAD_LEN) {
|
||||||
|
LOG_WRN("Drop CDC frame with invalid len:%u", byte);
|
||||||
|
parser_reset();
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
parser.len = byte;
|
||||||
|
parser.payload_pos = 0U;
|
||||||
|
parser.state = FRAME_PARSE_TYPE;
|
||||||
|
break;
|
||||||
|
|
||||||
|
case FRAME_PARSE_TYPE:
|
||||||
|
parser.type = byte;
|
||||||
|
parser.state = (parser.len == 0U) ? FRAME_PARSE_CHECKSUM :
|
||||||
|
FRAME_PARSE_PAYLOAD;
|
||||||
|
break;
|
||||||
|
|
||||||
|
case FRAME_PARSE_PAYLOAD:
|
||||||
|
parser.payload[parser.payload_pos++] = byte;
|
||||||
|
if (parser.payload_pos >= parser.len) {
|
||||||
|
parser.state = FRAME_PARSE_CHECKSUM;
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
|
||||||
|
case FRAME_PARSE_CHECKSUM:
|
||||||
|
if (byte != frame_checksum(parser.len, parser.type,
|
||||||
|
parser.payload, parser.payload_pos)) {
|
||||||
|
LOG_WRN("Drop CDC frame with invalid checksum");
|
||||||
|
parser_reset();
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
process_complete_frame();
|
||||||
|
parser_reset();
|
||||||
|
break;
|
||||||
|
|
||||||
|
default:
|
||||||
|
parser_reset();
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static bool handle_usb_cdc_rx_event(const struct usb_cdc_rx_event *event)
|
||||||
|
{
|
||||||
|
if (!running) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
for (size_t i = 0; i < event->dyndata.size; i++) {
|
||||||
|
consume_byte(event->dyndata.data[i]);
|
||||||
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
static int module_init(void)
|
||||||
|
{
|
||||||
|
parser_reset();
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
static int module_start(void)
|
||||||
|
{
|
||||||
|
if (running) {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
running = true;
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
static void module_pause(void)
|
||||||
|
{
|
||||||
|
if (!running) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
running = false;
|
||||||
|
parser_reset();
|
||||||
|
}
|
||||||
|
|
||||||
|
static bool app_event_handler(const struct app_event_header *aeh)
|
||||||
|
{
|
||||||
|
if (is_usb_cdc_rx_event(aeh)) {
|
||||||
|
return handle_usb_cdc_rx_event(cast_usb_cdc_rx_event(aeh));
|
||||||
|
}
|
||||||
|
|
||||||
|
if (is_module_state_event(aeh)) {
|
||||||
|
const struct module_state_event *event = cast_module_state_event(aeh);
|
||||||
|
|
||||||
|
if (check_state(event, MODULE_ID(main), MODULE_STATE_READY)) {
|
||||||
|
int err;
|
||||||
|
|
||||||
|
if (!initialized) {
|
||||||
|
err = module_init();
|
||||||
|
if (err) {
|
||||||
|
module_set_state(MODULE_STATE_ERROR);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
initialized = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
err = module_start();
|
||||||
|
if (err) {
|
||||||
|
module_set_state(MODULE_STATE_ERROR);
|
||||||
|
} else {
|
||||||
|
module_set_state(MODULE_STATE_READY);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (is_power_down_event(aeh)) {
|
||||||
|
if (initialized) {
|
||||||
|
module_pause();
|
||||||
|
module_set_state(MODULE_STATE_STANDBY);
|
||||||
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (is_wake_up_event(aeh)) {
|
||||||
|
if (initialized) {
|
||||||
|
int err = module_start();
|
||||||
|
|
||||||
|
if (err) {
|
||||||
|
module_set_state(MODULE_STATE_ERROR);
|
||||||
|
} else {
|
||||||
|
module_set_state(MODULE_STATE_READY);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
APP_EVENT_LISTENER(MODULE, app_event_handler);
|
||||||
|
APP_EVENT_SUBSCRIBE(MODULE, module_state_event);
|
||||||
|
APP_EVENT_SUBSCRIBE(MODULE, usb_cdc_rx_event);
|
||||||
|
APP_EVENT_SUBSCRIBE_EARLY(MODULE, power_down_event);
|
||||||
|
APP_EVENT_SUBSCRIBE(MODULE, wake_up_event);
|
||||||
225
src/display_module.c
Normal file
225
src/display_module.c
Normal file
@@ -0,0 +1,225 @@
|
|||||||
|
#include <errno.h>
|
||||||
|
#include <stdbool.h>
|
||||||
|
|
||||||
|
#include <app_event_manager.h>
|
||||||
|
|
||||||
|
#define MODULE display_module
|
||||||
|
#include <caf/events/module_state_event.h>
|
||||||
|
#include <caf/events/power_event.h>
|
||||||
|
|
||||||
|
#include <lvgl_zephyr.h>
|
||||||
|
#include <zephyr/device.h>
|
||||||
|
#include <zephyr/drivers/display.h>
|
||||||
|
#include <zephyr/drivers/led.h>
|
||||||
|
#include <zephyr/logging/log.h>
|
||||||
|
|
||||||
|
#include "bat_state_event.h"
|
||||||
|
#include "hid_led_event.h"
|
||||||
|
#include "mode_switch_event.h"
|
||||||
|
#include "ui/ui_main.h"
|
||||||
|
|
||||||
|
LOG_MODULE_REGISTER(MODULE, LOG_LEVEL_INF);
|
||||||
|
|
||||||
|
BUILD_ASSERT(DT_HAS_CHOSEN(zephyr_display), "Missing zephyr,display chosen node");
|
||||||
|
BUILD_ASSERT(DT_NODE_HAS_STATUS(DT_ALIAS(backlight), okay),
|
||||||
|
"Missing backlight alias");
|
||||||
|
|
||||||
|
static const struct device *const display_dev =
|
||||||
|
DEVICE_DT_GET(DT_CHOSEN(zephyr_display));
|
||||||
|
static const struct device *const backlight_dev =
|
||||||
|
DEVICE_DT_GET(DT_PARENT(DT_ALIAS(backlight)));
|
||||||
|
static const uint32_t backlight_idx = DT_NODE_CHILD_IDX(DT_ALIAS(backlight));
|
||||||
|
static struct ui_main_model ui_model = {
|
||||||
|
.theme_color = LV_COLOR_MAKE(0x4C, 0x9E, 0xF5),
|
||||||
|
.inactive_border_color = LV_COLOR_MAKE(0x3A, 0x44, 0x52),
|
||||||
|
.mode = MODE_SWITCH_BLE,
|
||||||
|
};
|
||||||
|
static bool initialized;
|
||||||
|
static bool running;
|
||||||
|
static bool lvgl_initialized;
|
||||||
|
|
||||||
|
static int backlight_set(bool on)
|
||||||
|
{
|
||||||
|
if (on) {
|
||||||
|
return led_on(backlight_dev, backlight_idx);
|
||||||
|
}
|
||||||
|
|
||||||
|
return led_off(backlight_dev, backlight_idx);
|
||||||
|
}
|
||||||
|
|
||||||
|
static int module_init(void)
|
||||||
|
{
|
||||||
|
int err;
|
||||||
|
|
||||||
|
LOG_INF("Display init on %s", display_dev->name);
|
||||||
|
|
||||||
|
if (!device_is_ready(display_dev)) {
|
||||||
|
LOG_ERR("Display device %s not ready", display_dev->name);
|
||||||
|
return -ENODEV;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!device_is_ready(backlight_dev)) {
|
||||||
|
LOG_ERR("Backlight device %s not ready", backlight_dev->name);
|
||||||
|
return -ENODEV;
|
||||||
|
}
|
||||||
|
|
||||||
|
err = backlight_set(false);
|
||||||
|
if (err) {
|
||||||
|
LOG_ERR("Backlight off failed (%d)", err);
|
||||||
|
return err;
|
||||||
|
}
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
static int module_start(void)
|
||||||
|
{
|
||||||
|
int err;
|
||||||
|
|
||||||
|
if (running) {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!lvgl_initialized) {
|
||||||
|
err = lvgl_init();
|
||||||
|
if (err) {
|
||||||
|
LOG_ERR("lvgl_init failed (%d)", err);
|
||||||
|
return err;
|
||||||
|
}
|
||||||
|
|
||||||
|
lvgl_initialized = true;
|
||||||
|
|
||||||
|
lvgl_lock();
|
||||||
|
ui_main_init(&ui_model, "WH Mini", "Hello World");
|
||||||
|
lvgl_unlock();
|
||||||
|
}
|
||||||
|
|
||||||
|
err = backlight_set(true);
|
||||||
|
if (err) {
|
||||||
|
LOG_ERR("Backlight enable failed (%d)", err);
|
||||||
|
return err;
|
||||||
|
}
|
||||||
|
|
||||||
|
err = display_blanking_off(display_dev);
|
||||||
|
if (err) {
|
||||||
|
LOG_ERR("display_blanking_off failed (%d)", err);
|
||||||
|
(void)backlight_set(false);
|
||||||
|
return err;
|
||||||
|
}
|
||||||
|
|
||||||
|
running = true;
|
||||||
|
LOG_INF("LVGL display started");
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
static void module_pause(void)
|
||||||
|
{
|
||||||
|
if (!running) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
(void)display_blanking_on(display_dev);
|
||||||
|
(void)backlight_set(false);
|
||||||
|
running = false;
|
||||||
|
LOG_INF("LVGL display paused");
|
||||||
|
}
|
||||||
|
|
||||||
|
static void refresh_ui(void)
|
||||||
|
{
|
||||||
|
if (!lvgl_initialized) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
lvgl_lock();
|
||||||
|
ui_main_refresh_all(&ui_model, "WH Mini", "Hello World");
|
||||||
|
lvgl_unlock();
|
||||||
|
}
|
||||||
|
|
||||||
|
static bool app_event_handler(const struct app_event_header *aeh)
|
||||||
|
{
|
||||||
|
if (is_bat_state_event(aeh)) {
|
||||||
|
const struct bat_state_event *event = cast_bat_state_event(aeh);
|
||||||
|
|
||||||
|
ui_model.battery_level = event->soc;
|
||||||
|
ui_model.charging = event->charging;
|
||||||
|
ui_model.full = event->full;
|
||||||
|
refresh_ui();
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (is_mode_switch_event(aeh)) {
|
||||||
|
const struct mode_switch_event *event = cast_mode_switch_event(aeh);
|
||||||
|
|
||||||
|
ui_model.mode = event->mode;
|
||||||
|
refresh_ui();
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (is_hid_led_event(aeh)) {
|
||||||
|
const struct hid_led_event *event = cast_hid_led_event(aeh);
|
||||||
|
|
||||||
|
ui_model.led_mask = event->led_bm;
|
||||||
|
refresh_ui();
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
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, bat_state_event);
|
||||||
|
APP_EVENT_SUBSCRIBE(MODULE, hid_led_event);
|
||||||
|
APP_EVENT_SUBSCRIBE(MODULE, module_state_event);
|
||||||
|
APP_EVENT_SUBSCRIBE(MODULE, mode_switch_event);
|
||||||
|
APP_EVENT_SUBSCRIBE_EARLY(MODULE, power_down_event);
|
||||||
|
APP_EVENT_SUBSCRIBE(MODULE, wake_up_event);
|
||||||
32
src/events/bat_state_event.c
Normal file
32
src/events/bat_state_event.c
Normal file
@@ -0,0 +1,32 @@
|
|||||||
|
#include "bat_state_event.h"
|
||||||
|
|
||||||
|
static void log_bat_state_event(const struct app_event_header *aeh)
|
||||||
|
{
|
||||||
|
const struct bat_state_event *event = cast_bat_state_event(aeh);
|
||||||
|
|
||||||
|
APP_EVENT_MANAGER_LOG(aeh, "soc:%u charging:%u full:%u",
|
||||||
|
event->soc, event->charging, event->full);
|
||||||
|
}
|
||||||
|
|
||||||
|
static void profile_bat_state_event(struct log_event_buf *buf,
|
||||||
|
const struct app_event_header *aeh)
|
||||||
|
{
|
||||||
|
const struct bat_state_event *event = cast_bat_state_event(aeh);
|
||||||
|
|
||||||
|
nrf_profiler_log_encode_uint8(buf, event->soc);
|
||||||
|
nrf_profiler_log_encode_uint8(buf, event->charging);
|
||||||
|
nrf_profiler_log_encode_uint8(buf, event->full);
|
||||||
|
}
|
||||||
|
|
||||||
|
APP_EVENT_INFO_DEFINE(bat_state_event,
|
||||||
|
ENCODE(NRF_PROFILER_ARG_U8,
|
||||||
|
NRF_PROFILER_ARG_U8,
|
||||||
|
NRF_PROFILER_ARG_U8),
|
||||||
|
ENCODE("soc", "charging", "full"),
|
||||||
|
profile_bat_state_event);
|
||||||
|
|
||||||
|
APP_EVENT_TYPE_DEFINE(bat_state_event,
|
||||||
|
log_bat_state_event,
|
||||||
|
&bat_state_event_info,
|
||||||
|
APP_EVENT_FLAGS_CREATE(
|
||||||
|
APP_EVENT_TYPE_FLAGS_INIT_LOG_ENABLE));
|
||||||
@@ -1,10 +1,23 @@
|
|||||||
#include "hid_led_event.h"
|
#include "hid_led_event.h"
|
||||||
|
|
||||||
|
static const char *transport_name(enum hid_transport transport)
|
||||||
|
{
|
||||||
|
switch (transport) {
|
||||||
|
case HID_TRANSPORT_USB:
|
||||||
|
return "USB";
|
||||||
|
case HID_TRANSPORT_BLE:
|
||||||
|
return "BLE";
|
||||||
|
default:
|
||||||
|
return "?";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
static void log_hid_led_event(const struct app_event_header *aeh)
|
static void log_hid_led_event(const struct app_event_header *aeh)
|
||||||
{
|
{
|
||||||
const struct hid_led_event *event = cast_hid_led_event(aeh);
|
const struct hid_led_event *event = cast_hid_led_event(aeh);
|
||||||
|
|
||||||
APP_EVENT_MANAGER_LOG(aeh, "led_bm:0x%02x", event->led_bm);
|
APP_EVENT_MANAGER_LOG(aeh, "transport:%s led_bm:0x%02x",
|
||||||
|
transport_name(event->transport), event->led_bm);
|
||||||
}
|
}
|
||||||
|
|
||||||
static void profile_hid_led_event(struct log_event_buf *buf,
|
static void profile_hid_led_event(struct log_event_buf *buf,
|
||||||
@@ -12,12 +25,13 @@ static void profile_hid_led_event(struct log_event_buf *buf,
|
|||||||
{
|
{
|
||||||
const struct hid_led_event *event = cast_hid_led_event(aeh);
|
const struct hid_led_event *event = cast_hid_led_event(aeh);
|
||||||
|
|
||||||
|
nrf_profiler_log_encode_uint8(buf, event->transport);
|
||||||
nrf_profiler_log_encode_uint8(buf, event->led_bm);
|
nrf_profiler_log_encode_uint8(buf, event->led_bm);
|
||||||
}
|
}
|
||||||
|
|
||||||
APP_EVENT_INFO_DEFINE(hid_led_event,
|
APP_EVENT_INFO_DEFINE(hid_led_event,
|
||||||
ENCODE(NRF_PROFILER_ARG_U8),
|
ENCODE(NRF_PROFILER_ARG_U8, NRF_PROFILER_ARG_U8),
|
||||||
ENCODE("led_bm"),
|
ENCODE("transport", "led_bm"),
|
||||||
profile_hid_led_event);
|
profile_hid_led_event);
|
||||||
|
|
||||||
APP_EVENT_TYPE_DEFINE(hid_led_event,
|
APP_EVENT_TYPE_DEFINE(hid_led_event,
|
||||||
|
|||||||
61
src/events/hid_report_sent_event.c
Normal file
61
src/events/hid_report_sent_event.c
Normal file
@@ -0,0 +1,61 @@
|
|||||||
|
#include "hid_report_sent_event.h"
|
||||||
|
|
||||||
|
static const char *transport_name(enum hid_transport transport)
|
||||||
|
{
|
||||||
|
switch (transport) {
|
||||||
|
case HID_TRANSPORT_USB:
|
||||||
|
return "USB";
|
||||||
|
case HID_TRANSPORT_BLE:
|
||||||
|
return "BLE";
|
||||||
|
default:
|
||||||
|
return "?";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static const char *report_type_name(enum keyboard_report_type report_type)
|
||||||
|
{
|
||||||
|
switch (report_type) {
|
||||||
|
case KEYBOARD_REPORT_TYPE_KEYS:
|
||||||
|
return "keys";
|
||||||
|
case KEYBOARD_REPORT_TYPE_CONSUMER:
|
||||||
|
return "consumer";
|
||||||
|
default:
|
||||||
|
return "?";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static void log_hid_report_sent_event(const struct app_event_header *aeh)
|
||||||
|
{
|
||||||
|
const struct hid_report_sent_event *event = cast_hid_report_sent_event(aeh);
|
||||||
|
|
||||||
|
APP_EVENT_MANAGER_LOG(aeh, "transport:%s type:%s seq:%u error:%u",
|
||||||
|
transport_name(event->transport),
|
||||||
|
report_type_name(event->report_type),
|
||||||
|
event->sequence,
|
||||||
|
event->error);
|
||||||
|
}
|
||||||
|
|
||||||
|
static void profile_hid_report_sent_event(struct log_event_buf *buf,
|
||||||
|
const struct app_event_header *aeh)
|
||||||
|
{
|
||||||
|
const struct hid_report_sent_event *event = cast_hid_report_sent_event(aeh);
|
||||||
|
|
||||||
|
nrf_profiler_log_encode_uint8(buf, event->transport);
|
||||||
|
nrf_profiler_log_encode_uint8(buf, event->report_type);
|
||||||
|
nrf_profiler_log_encode_uint16(buf, event->sequence);
|
||||||
|
nrf_profiler_log_encode_uint8(buf, event->error);
|
||||||
|
}
|
||||||
|
|
||||||
|
APP_EVENT_INFO_DEFINE(hid_report_sent_event,
|
||||||
|
ENCODE(NRF_PROFILER_ARG_U8,
|
||||||
|
NRF_PROFILER_ARG_U8,
|
||||||
|
NRF_PROFILER_ARG_U16,
|
||||||
|
NRF_PROFILER_ARG_U8),
|
||||||
|
ENCODE("transport", "report_type", "sequence", "error"),
|
||||||
|
profile_hid_report_sent_event);
|
||||||
|
|
||||||
|
APP_EVENT_TYPE_DEFINE(hid_report_sent_event,
|
||||||
|
log_hid_report_sent_event,
|
||||||
|
&hid_report_sent_event_info,
|
||||||
|
APP_EVENT_FLAGS_CREATE(
|
||||||
|
APP_EVENT_TYPE_FLAGS_INIT_LOG_ENABLE));
|
||||||
68
src/events/hid_transport_state_event.c
Normal file
68
src/events/hid_transport_state_event.c
Normal file
@@ -0,0 +1,68 @@
|
|||||||
|
#include "hid_transport_state_event.h"
|
||||||
|
|
||||||
|
static const char *transport_name(enum hid_transport transport)
|
||||||
|
{
|
||||||
|
switch (transport) {
|
||||||
|
case HID_TRANSPORT_USB:
|
||||||
|
return "USB";
|
||||||
|
case HID_TRANSPORT_BLE:
|
||||||
|
return "BLE";
|
||||||
|
default:
|
||||||
|
return "?";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static const char *protocol_mode_name(enum keyboard_protocol_mode protocol_mode)
|
||||||
|
{
|
||||||
|
switch (protocol_mode) {
|
||||||
|
case KEYBOARD_PROTOCOL_MODE_BOOT:
|
||||||
|
return "boot";
|
||||||
|
case KEYBOARD_PROTOCOL_MODE_REPORT:
|
||||||
|
return "report";
|
||||||
|
default:
|
||||||
|
return "?";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static void log_hid_transport_state_event(const struct app_event_header *aeh)
|
||||||
|
{
|
||||||
|
const struct hid_transport_state_event *event =
|
||||||
|
cast_hid_transport_state_event(aeh);
|
||||||
|
|
||||||
|
APP_EVENT_MANAGER_LOG(aeh,
|
||||||
|
"transport:%s ready:%u keys_ready:%u consumer_ready:%u protocol:%s",
|
||||||
|
transport_name(event->transport),
|
||||||
|
event->ready,
|
||||||
|
event->keys_ready,
|
||||||
|
event->consumer_ready,
|
||||||
|
protocol_mode_name(event->protocol_mode));
|
||||||
|
}
|
||||||
|
|
||||||
|
static void profile_hid_transport_state_event(struct log_event_buf *buf,
|
||||||
|
const struct app_event_header *aeh)
|
||||||
|
{
|
||||||
|
const struct hid_transport_state_event *event =
|
||||||
|
cast_hid_transport_state_event(aeh);
|
||||||
|
|
||||||
|
nrf_profiler_log_encode_uint8(buf, event->transport);
|
||||||
|
nrf_profiler_log_encode_uint8(buf, event->ready);
|
||||||
|
nrf_profiler_log_encode_uint8(buf, event->keys_ready);
|
||||||
|
nrf_profiler_log_encode_uint8(buf, event->consumer_ready);
|
||||||
|
nrf_profiler_log_encode_uint8(buf, event->protocol_mode);
|
||||||
|
}
|
||||||
|
|
||||||
|
APP_EVENT_INFO_DEFINE(hid_transport_state_event,
|
||||||
|
ENCODE(NRF_PROFILER_ARG_U8,
|
||||||
|
NRF_PROFILER_ARG_U8,
|
||||||
|
NRF_PROFILER_ARG_U8,
|
||||||
|
NRF_PROFILER_ARG_U8,
|
||||||
|
NRF_PROFILER_ARG_U8),
|
||||||
|
ENCODE("transport", "ready", "keys_ready", "consumer_ready",
|
||||||
|
"protocol_mode"),
|
||||||
|
profile_hid_transport_state_event);
|
||||||
|
|
||||||
|
APP_EVENT_TYPE_DEFINE(hid_transport_state_event,
|
||||||
|
log_hid_transport_state_event,
|
||||||
|
&hid_transport_state_event_info,
|
||||||
|
APP_EVENT_FLAGS_CREATE(
|
||||||
|
APP_EVENT_TYPE_FLAGS_INIT_LOG_ENABLE));
|
||||||
107
src/events/hid_tx_report_event.c
Normal file
107
src/events/hid_tx_report_event.c
Normal file
@@ -0,0 +1,107 @@
|
|||||||
|
#include <stdio.h>
|
||||||
|
|
||||||
|
#include "hid_tx_report_event.h"
|
||||||
|
|
||||||
|
#define HID_TX_REPORT_EVENT_LOG_BUF_LEN 192
|
||||||
|
|
||||||
|
static const char *transport_name(enum hid_transport transport)
|
||||||
|
{
|
||||||
|
switch (transport) {
|
||||||
|
case HID_TRANSPORT_USB:
|
||||||
|
return "USB";
|
||||||
|
case HID_TRANSPORT_BLE:
|
||||||
|
return "BLE";
|
||||||
|
default:
|
||||||
|
return "?";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static const char *report_type_name(enum keyboard_report_type report_type)
|
||||||
|
{
|
||||||
|
switch (report_type) {
|
||||||
|
case KEYBOARD_REPORT_TYPE_KEYS:
|
||||||
|
return "keys";
|
||||||
|
case KEYBOARD_REPORT_TYPE_CONSUMER:
|
||||||
|
return "consumer";
|
||||||
|
default:
|
||||||
|
return "?";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static const char *protocol_mode_name(enum keyboard_protocol_mode protocol_mode)
|
||||||
|
{
|
||||||
|
switch (protocol_mode) {
|
||||||
|
case KEYBOARD_PROTOCOL_MODE_BOOT:
|
||||||
|
return "boot";
|
||||||
|
case KEYBOARD_PROTOCOL_MODE_REPORT:
|
||||||
|
return "report";
|
||||||
|
default:
|
||||||
|
return "?";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static void log_hid_tx_report_event(const struct app_event_header *aeh)
|
||||||
|
{
|
||||||
|
const struct hid_tx_report_event *event = cast_hid_tx_report_event(aeh);
|
||||||
|
char log_buf[HID_TX_REPORT_EVENT_LOG_BUF_LEN];
|
||||||
|
int pos;
|
||||||
|
|
||||||
|
pos = snprintf(log_buf, sizeof(log_buf),
|
||||||
|
"transport:%s type:%s protocol:%s seq:%u len:%zu",
|
||||||
|
transport_name(event->transport),
|
||||||
|
report_type_name(event->report_type),
|
||||||
|
protocol_mode_name(event->protocol_mode),
|
||||||
|
event->sequence,
|
||||||
|
event->dyndata.size);
|
||||||
|
if ((pos > 0) && (pos < sizeof(log_buf))) {
|
||||||
|
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) {
|
||||||
|
log_buf[sizeof(log_buf) - 2] = '~';
|
||||||
|
pos = tmp;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
pos += tmp;
|
||||||
|
if (pos >= sizeof(log_buf)) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (pos < 0) {
|
||||||
|
APP_EVENT_MANAGER_LOG(aeh, "log message preparation failure");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
APP_EVENT_MANAGER_LOG(aeh, "%s", log_buf);
|
||||||
|
}
|
||||||
|
|
||||||
|
static void profile_hid_tx_report_event(struct log_event_buf *buf,
|
||||||
|
const struct app_event_header *aeh)
|
||||||
|
{
|
||||||
|
const struct hid_tx_report_event *event = cast_hid_tx_report_event(aeh);
|
||||||
|
|
||||||
|
nrf_profiler_log_encode_uint8(buf, event->transport);
|
||||||
|
nrf_profiler_log_encode_uint8(buf, event->report_type);
|
||||||
|
nrf_profiler_log_encode_uint8(buf, event->protocol_mode);
|
||||||
|
nrf_profiler_log_encode_uint16(buf, event->sequence);
|
||||||
|
nrf_profiler_log_encode_uint8(buf, (uint8_t)event->dyndata.size);
|
||||||
|
}
|
||||||
|
|
||||||
|
APP_EVENT_INFO_DEFINE(hid_tx_report_event,
|
||||||
|
ENCODE(NRF_PROFILER_ARG_U8,
|
||||||
|
NRF_PROFILER_ARG_U8,
|
||||||
|
NRF_PROFILER_ARG_U8,
|
||||||
|
NRF_PROFILER_ARG_U16,
|
||||||
|
NRF_PROFILER_ARG_U8),
|
||||||
|
ENCODE("transport", "report_type", "protocol_mode", "sequence", "len"),
|
||||||
|
profile_hid_tx_report_event);
|
||||||
|
|
||||||
|
APP_EVENT_TYPE_DEFINE(hid_tx_report_event,
|
||||||
|
log_hid_tx_report_event,
|
||||||
|
&hid_tx_report_event_info,
|
||||||
|
APP_EVENT_FLAGS_CREATE(
|
||||||
|
APP_EVENT_TYPE_FLAGS_INIT_LOG_ENABLE));
|
||||||
@@ -42,6 +42,18 @@ static const char *protocol_mode_name(enum keyboard_protocol_mode protocol_mode)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static const char *queue_policy_name(enum hid_queue_policy queue_policy)
|
||||||
|
{
|
||||||
|
switch (queue_policy) {
|
||||||
|
case HID_QUEUE_POLICY_LATEST:
|
||||||
|
return "latest";
|
||||||
|
case HID_QUEUE_POLICY_FIFO:
|
||||||
|
return "fifo";
|
||||||
|
default:
|
||||||
|
return "?";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
static void log_keyboard_hid_report_event(const struct app_event_header *aeh)
|
static void log_keyboard_hid_report_event(const struct app_event_header *aeh)
|
||||||
{
|
{
|
||||||
const struct keyboard_hid_report_event *event =
|
const struct keyboard_hid_report_event *event =
|
||||||
@@ -49,10 +61,12 @@ static void log_keyboard_hid_report_event(const struct app_event_header *aeh)
|
|||||||
char log_buf[KEYBOARD_HID_REPORT_EVENT_LOG_BUF_LEN];
|
char log_buf[KEYBOARD_HID_REPORT_EVENT_LOG_BUF_LEN];
|
||||||
int pos;
|
int pos;
|
||||||
|
|
||||||
pos = snprintf(log_buf, sizeof(log_buf), "mode:%s type:%s protocol:%s len:%zu",
|
pos = snprintf(log_buf, sizeof(log_buf),
|
||||||
|
"mode:%s type:%s protocol:%s queue:%s len:%zu",
|
||||||
mode_name(event->mode),
|
mode_name(event->mode),
|
||||||
report_type_name(event->report_type),
|
report_type_name(event->report_type),
|
||||||
protocol_mode_name(event->protocol_mode),
|
protocol_mode_name(event->protocol_mode),
|
||||||
|
queue_policy_name(event->queue_policy),
|
||||||
event->dyndata.size);
|
event->dyndata.size);
|
||||||
if ((pos > 0) && (pos < sizeof(log_buf))) {
|
if ((pos > 0) && (pos < sizeof(log_buf))) {
|
||||||
for (size_t i = 0; i < event->dyndata.size; i++) {
|
for (size_t i = 0; i < event->dyndata.size; i++) {
|
||||||
@@ -89,15 +103,17 @@ static void profile_keyboard_hid_report_event(struct log_event_buf *buf,
|
|||||||
nrf_profiler_log_encode_uint8(buf, event->mode);
|
nrf_profiler_log_encode_uint8(buf, event->mode);
|
||||||
nrf_profiler_log_encode_uint8(buf, event->report_type);
|
nrf_profiler_log_encode_uint8(buf, event->report_type);
|
||||||
nrf_profiler_log_encode_uint8(buf, event->protocol_mode);
|
nrf_profiler_log_encode_uint8(buf, event->protocol_mode);
|
||||||
|
nrf_profiler_log_encode_uint8(buf, event->queue_policy);
|
||||||
nrf_profiler_log_encode_uint8(buf, (uint8_t)event->dyndata.size);
|
nrf_profiler_log_encode_uint8(buf, (uint8_t)event->dyndata.size);
|
||||||
}
|
}
|
||||||
|
|
||||||
APP_EVENT_INFO_DEFINE(keyboard_hid_report_event,
|
APP_EVENT_INFO_DEFINE(keyboard_hid_report_event,
|
||||||
ENCODE(NRF_PROFILER_ARG_U8,
|
ENCODE(NRF_PROFILER_ARG_U8,
|
||||||
|
NRF_PROFILER_ARG_U8,
|
||||||
NRF_PROFILER_ARG_U8,
|
NRF_PROFILER_ARG_U8,
|
||||||
NRF_PROFILER_ARG_U8,
|
NRF_PROFILER_ARG_U8,
|
||||||
NRF_PROFILER_ARG_U8),
|
NRF_PROFILER_ARG_U8),
|
||||||
ENCODE("mode", "report_type", "protocol_mode", "len"),
|
ENCODE("mode", "report_type", "protocol_mode", "queue_policy", "len"),
|
||||||
profile_keyboard_hid_report_event);
|
profile_keyboard_hid_report_event);
|
||||||
|
|
||||||
APP_EVENT_TYPE_DEFINE(keyboard_hid_report_event,
|
APP_EVENT_TYPE_DEFINE(keyboard_hid_report_event,
|
||||||
|
|||||||
@@ -1,5 +1,17 @@
|
|||||||
#include "set_protocol_event.h"
|
#include "set_protocol_event.h"
|
||||||
|
|
||||||
|
static const char *transport_name(enum hid_transport transport)
|
||||||
|
{
|
||||||
|
switch (transport) {
|
||||||
|
case HID_TRANSPORT_USB:
|
||||||
|
return "USB";
|
||||||
|
case HID_TRANSPORT_BLE:
|
||||||
|
return "BLE";
|
||||||
|
default:
|
||||||
|
return "?";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
static const char *protocol_mode_name(enum keyboard_protocol_mode protocol_mode)
|
static const char *protocol_mode_name(enum keyboard_protocol_mode protocol_mode)
|
||||||
{
|
{
|
||||||
switch (protocol_mode) {
|
switch (protocol_mode) {
|
||||||
@@ -16,7 +28,8 @@ static void log_set_protocol_event(const struct app_event_header *aeh)
|
|||||||
{
|
{
|
||||||
const struct set_protocol_event *event = cast_set_protocol_event(aeh);
|
const struct set_protocol_event *event = cast_set_protocol_event(aeh);
|
||||||
|
|
||||||
APP_EVENT_MANAGER_LOG(aeh, "protocol:%s",
|
APP_EVENT_MANAGER_LOG(aeh, "transport:%s protocol:%s",
|
||||||
|
transport_name(event->transport),
|
||||||
protocol_mode_name(event->protocol_mode));
|
protocol_mode_name(event->protocol_mode));
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -25,12 +38,13 @@ static void profile_set_protocol_event(struct log_event_buf *buf,
|
|||||||
{
|
{
|
||||||
const struct set_protocol_event *event = cast_set_protocol_event(aeh);
|
const struct set_protocol_event *event = cast_set_protocol_event(aeh);
|
||||||
|
|
||||||
|
nrf_profiler_log_encode_uint8(buf, event->transport);
|
||||||
nrf_profiler_log_encode_uint8(buf, event->protocol_mode);
|
nrf_profiler_log_encode_uint8(buf, event->protocol_mode);
|
||||||
}
|
}
|
||||||
|
|
||||||
APP_EVENT_INFO_DEFINE(set_protocol_event,
|
APP_EVENT_INFO_DEFINE(set_protocol_event,
|
||||||
ENCODE(NRF_PROFILER_ARG_U8),
|
ENCODE(NRF_PROFILER_ARG_U8, NRF_PROFILER_ARG_U8),
|
||||||
ENCODE("protocol_mode"),
|
ENCODE("transport", "protocol_mode"),
|
||||||
profile_set_protocol_event);
|
profile_set_protocol_event);
|
||||||
|
|
||||||
APP_EVENT_TYPE_DEFINE(set_protocol_event,
|
APP_EVENT_TYPE_DEFINE(set_protocol_event,
|
||||||
|
|||||||
73
src/events/usb_cdc_rx_event.c
Normal file
73
src/events/usb_cdc_rx_event.c
Normal file
@@ -0,0 +1,73 @@
|
|||||||
|
#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));
|
||||||
62
src/events/usb_cdc_tx_event.c
Normal file
62
src/events/usb_cdc_tx_event.c
Normal file
@@ -0,0 +1,62 @@
|
|||||||
|
#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));
|
||||||
46
src/events/usb_device_state_event.c
Normal file
46
src/events/usb_device_state_event.c
Normal file
@@ -0,0 +1,46 @@
|
|||||||
|
#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));
|
||||||
42
src/events/usb_function_ready_event.c
Normal file
42
src/events/usb_function_ready_event.c
Normal file
@@ -0,0 +1,42 @@
|
|||||||
|
#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));
|
||||||
24
src/events/usb_prepare_event.c
Normal file
24
src/events/usb_prepare_event.c
Normal file
@@ -0,0 +1,24 @@
|
|||||||
|
#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));
|
||||||
468
src/hid_flowctrl_module.c
Normal file
468
src/hid_flowctrl_module.c
Normal file
@@ -0,0 +1,468 @@
|
|||||||
|
#include <stdbool.h>
|
||||||
|
#include <stdint.h>
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
|
#include <app_event_manager.h>
|
||||||
|
|
||||||
|
#define MODULE hid_flowctrl_module
|
||||||
|
#include <caf/events/module_state_event.h>
|
||||||
|
#include <caf/events/power_event.h>
|
||||||
|
|
||||||
|
#include <zephyr/logging/log.h>
|
||||||
|
|
||||||
|
#include "hid_report_sent_event.h"
|
||||||
|
#include "hid_transport_state_event.h"
|
||||||
|
#include "hid_tx_report_event.h"
|
||||||
|
#include "keyboard_core.h"
|
||||||
|
#include "keyboard_hid_report_event.h"
|
||||||
|
#include "mode_switch_event.h"
|
||||||
|
|
||||||
|
LOG_MODULE_REGISTER(MODULE, LOG_LEVEL_INF);
|
||||||
|
|
||||||
|
#define HID_FLOWCTRL_FIFO_DEPTH 32U
|
||||||
|
#define HID_FLOWCTRL_REPORT_DATA_MAX KEYBOARD_NKRO_REPORT_SIZE
|
||||||
|
|
||||||
|
struct pending_report {
|
||||||
|
bool valid;
|
||||||
|
enum keyboard_report_type report_type;
|
||||||
|
enum keyboard_protocol_mode protocol_mode;
|
||||||
|
size_t size;
|
||||||
|
uint8_t data[HID_FLOWCTRL_REPORT_DATA_MAX];
|
||||||
|
};
|
||||||
|
|
||||||
|
struct queued_report {
|
||||||
|
enum keyboard_report_type report_type;
|
||||||
|
enum keyboard_protocol_mode protocol_mode;
|
||||||
|
size_t size;
|
||||||
|
uint8_t data[HID_FLOWCTRL_REPORT_DATA_MAX];
|
||||||
|
};
|
||||||
|
|
||||||
|
struct hid_transport_state_data {
|
||||||
|
bool ready;
|
||||||
|
bool keys_ready;
|
||||||
|
bool consumer_ready;
|
||||||
|
enum keyboard_protocol_mode protocol_mode;
|
||||||
|
};
|
||||||
|
|
||||||
|
struct in_flight_report {
|
||||||
|
bool active;
|
||||||
|
enum hid_transport transport;
|
||||||
|
enum keyboard_report_type report_type;
|
||||||
|
uint16_t sequence;
|
||||||
|
};
|
||||||
|
|
||||||
|
static struct hid_transport_state_data transport_state[HID_TRANSPORT_COUNT] = {
|
||||||
|
[HID_TRANSPORT_USB] = {
|
||||||
|
.protocol_mode = KEYBOARD_PROTOCOL_MODE_REPORT,
|
||||||
|
},
|
||||||
|
[HID_TRANSPORT_BLE] = {
|
||||||
|
.protocol_mode = KEYBOARD_PROTOCOL_MODE_REPORT,
|
||||||
|
},
|
||||||
|
};
|
||||||
|
static struct pending_report pending_keys;
|
||||||
|
static struct pending_report pending_consumer_latest;
|
||||||
|
static struct queued_report consumer_fifo[HID_FLOWCTRL_FIFO_DEPTH];
|
||||||
|
static uint8_t consumer_fifo_head;
|
||||||
|
static uint8_t consumer_fifo_tail;
|
||||||
|
static uint8_t consumer_fifo_count;
|
||||||
|
static struct in_flight_report in_flight;
|
||||||
|
static enum mode_switch_mode current_mode;
|
||||||
|
static uint16_t next_sequence;
|
||||||
|
static bool initialized;
|
||||||
|
static bool running;
|
||||||
|
|
||||||
|
static bool mode_to_transport(enum mode_switch_mode mode, enum hid_transport *transport)
|
||||||
|
{
|
||||||
|
switch (mode) {
|
||||||
|
case MODE_SWITCH_USB:
|
||||||
|
*transport = HID_TRANSPORT_USB;
|
||||||
|
return true;
|
||||||
|
|
||||||
|
case MODE_SWITCH_BLE:
|
||||||
|
*transport = HID_TRANSPORT_BLE;
|
||||||
|
return true;
|
||||||
|
|
||||||
|
default:
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static void clear_pending_reports(void)
|
||||||
|
{
|
||||||
|
memset(&pending_keys, 0, sizeof(pending_keys));
|
||||||
|
memset(&pending_consumer_latest, 0, sizeof(pending_consumer_latest));
|
||||||
|
consumer_fifo_head = 0U;
|
||||||
|
consumer_fifo_tail = 0U;
|
||||||
|
consumer_fifo_count = 0U;
|
||||||
|
memset(&in_flight, 0, sizeof(in_flight));
|
||||||
|
}
|
||||||
|
|
||||||
|
static void consumer_fifo_push(enum keyboard_report_type report_type,
|
||||||
|
enum keyboard_protocol_mode protocol_mode,
|
||||||
|
const uint8_t *data, size_t size)
|
||||||
|
{
|
||||||
|
if (consumer_fifo_count == HID_FLOWCTRL_FIFO_DEPTH) {
|
||||||
|
LOG_WRN("Consumer FIFO full, dropping oldest pulse");
|
||||||
|
consumer_fifo_head = (consumer_fifo_head + 1U) % HID_FLOWCTRL_FIFO_DEPTH;
|
||||||
|
consumer_fifo_count--;
|
||||||
|
}
|
||||||
|
|
||||||
|
struct queued_report *entry = &consumer_fifo[consumer_fifo_tail];
|
||||||
|
|
||||||
|
entry->report_type = report_type;
|
||||||
|
entry->protocol_mode = protocol_mode;
|
||||||
|
entry->size = size;
|
||||||
|
memcpy(entry->data, data, size);
|
||||||
|
|
||||||
|
consumer_fifo_tail = (consumer_fifo_tail + 1U) % HID_FLOWCTRL_FIFO_DEPTH;
|
||||||
|
consumer_fifo_count++;
|
||||||
|
}
|
||||||
|
|
||||||
|
static bool consumer_fifo_pop(struct queued_report *entry)
|
||||||
|
{
|
||||||
|
if (consumer_fifo_count == 0U) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
*entry = consumer_fifo[consumer_fifo_head];
|
||||||
|
consumer_fifo_head = (consumer_fifo_head + 1U) % HID_FLOWCTRL_FIFO_DEPTH;
|
||||||
|
consumer_fifo_count--;
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
static bool transport_can_send_report(enum keyboard_report_type report_type)
|
||||||
|
{
|
||||||
|
enum hid_transport transport;
|
||||||
|
struct hid_transport_state_data *state;
|
||||||
|
|
||||||
|
if (!mode_to_transport(current_mode, &transport) || in_flight.active) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
state = &transport_state[transport];
|
||||||
|
|
||||||
|
if (!state->ready) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (report_type == KEYBOARD_REPORT_TYPE_KEYS) {
|
||||||
|
return state->keys_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)
|
||||||
|
{
|
||||||
|
struct queued_report queued;
|
||||||
|
enum hid_transport transport;
|
||||||
|
struct hid_transport_state_data *state;
|
||||||
|
|
||||||
|
if (!running || in_flight.active || !mode_to_transport(current_mode, &transport)) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
state = &transport_state[transport];
|
||||||
|
|
||||||
|
if (!state->ready) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (pending_keys.valid && transport_can_send_report(KEYBOARD_REPORT_TYPE_KEYS)) {
|
||||||
|
if (pending_keys.protocol_mode != state->protocol_mode) {
|
||||||
|
LOG_WRN("Drop stale keys report after protocol change");
|
||||||
|
pending_keys.valid = false;
|
||||||
|
} else {
|
||||||
|
submit_hid_tx_report_event(transport,
|
||||||
|
pending_keys.report_type,
|
||||||
|
pending_keys.protocol_mode,
|
||||||
|
pending_keys.data,
|
||||||
|
pending_keys.size);
|
||||||
|
pending_keys.valid = false;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if ((consumer_fifo_count > 0U) &&
|
||||||
|
transport_can_send_report(KEYBOARD_REPORT_TYPE_CONSUMER) &&
|
||||||
|
consumer_fifo_pop(&queued)) {
|
||||||
|
if (queued.protocol_mode != state->protocol_mode) {
|
||||||
|
LOG_WRN("Drop stale consumer report after protocol change");
|
||||||
|
} else {
|
||||||
|
submit_hid_tx_report_event(transport,
|
||||||
|
queued.report_type,
|
||||||
|
queued.protocol_mode,
|
||||||
|
queued.data,
|
||||||
|
queued.size);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (pending_consumer_latest.valid &&
|
||||||
|
transport_can_send_report(KEYBOARD_REPORT_TYPE_CONSUMER)) {
|
||||||
|
if (pending_consumer_latest.protocol_mode != state->protocol_mode) {
|
||||||
|
LOG_WRN("Drop stale latest consumer report after protocol change");
|
||||||
|
pending_consumer_latest.valid = false;
|
||||||
|
} else {
|
||||||
|
submit_hid_tx_report_event(transport,
|
||||||
|
pending_consumer_latest.report_type,
|
||||||
|
pending_consumer_latest.protocol_mode,
|
||||||
|
pending_consumer_latest.data,
|
||||||
|
pending_consumer_latest.size);
|
||||||
|
pending_consumer_latest.valid = false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static bool handle_keyboard_hid_report_event(const struct keyboard_hid_report_event *event)
|
||||||
|
{
|
||||||
|
if (!running ||
|
||||||
|
((event->mode != MODE_SWITCH_USB) && (event->mode != MODE_SWITCH_BLE))) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (event->queue_policy == HID_QUEUE_POLICY_FIFO) {
|
||||||
|
consumer_fifo_push(event->report_type,
|
||||||
|
event->protocol_mode,
|
||||||
|
event->dyndata.data,
|
||||||
|
event->dyndata.size);
|
||||||
|
} else if (event->report_type == KEYBOARD_REPORT_TYPE_KEYS) {
|
||||||
|
pending_keys.valid = true;
|
||||||
|
pending_keys.report_type = event->report_type;
|
||||||
|
pending_keys.protocol_mode = event->protocol_mode;
|
||||||
|
pending_keys.size = event->dyndata.size;
|
||||||
|
memcpy(pending_keys.data, event->dyndata.data, event->dyndata.size);
|
||||||
|
} else {
|
||||||
|
pending_consumer_latest.valid = true;
|
||||||
|
pending_consumer_latest.report_type = event->report_type;
|
||||||
|
pending_consumer_latest.protocol_mode = event->protocol_mode;
|
||||||
|
pending_consumer_latest.size = event->dyndata.size;
|
||||||
|
memcpy(pending_consumer_latest.data, event->dyndata.data,
|
||||||
|
event->dyndata.size);
|
||||||
|
}
|
||||||
|
|
||||||
|
try_send_next();
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
static bool handle_hid_transport_state_event(const struct hid_transport_state_event *event)
|
||||||
|
{
|
||||||
|
enum hid_transport active_transport;
|
||||||
|
struct hid_transport_state_data *state;
|
||||||
|
|
||||||
|
if (event->transport >= HID_TRANSPORT_COUNT) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
state = &transport_state[event->transport];
|
||||||
|
|
||||||
|
state->ready = event->ready;
|
||||||
|
state->keys_ready = event->keys_ready;
|
||||||
|
state->consumer_ready = event->consumer_ready;
|
||||||
|
|
||||||
|
if (state->protocol_mode != event->protocol_mode) {
|
||||||
|
state->protocol_mode = event->protocol_mode;
|
||||||
|
|
||||||
|
if (mode_to_transport(current_mode, &active_transport) &&
|
||||||
|
(active_transport == event->transport)) {
|
||||||
|
pending_keys.valid = false;
|
||||||
|
pending_consumer_latest.valid = false;
|
||||||
|
consumer_fifo_head = 0U;
|
||||||
|
consumer_fifo_tail = 0U;
|
||||||
|
consumer_fifo_count = 0U;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!state->ready &&
|
||||||
|
mode_to_transport(current_mode, &active_transport) &&
|
||||||
|
(active_transport == event->transport)) {
|
||||||
|
consumer_fifo_head = 0U;
|
||||||
|
consumer_fifo_tail = 0U;
|
||||||
|
consumer_fifo_count = 0U;
|
||||||
|
in_flight.active = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
try_send_next();
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
static bool handle_hid_report_sent_event(const struct hid_report_sent_event *event)
|
||||||
|
{
|
||||||
|
if (!in_flight.active || (event->transport != in_flight.transport)) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (event->sequence != in_flight.sequence) {
|
||||||
|
LOG_WRN("Unexpected HID sent sequence %u (expected %u)",
|
||||||
|
event->sequence, in_flight.sequence);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
in_flight.active = false;
|
||||||
|
|
||||||
|
if (event->error) {
|
||||||
|
LOG_WRN("HID report send failed for seq %u", event->sequence);
|
||||||
|
}
|
||||||
|
|
||||||
|
try_send_next();
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
static bool handle_mode_switch_event(const struct mode_switch_event *event)
|
||||||
|
{
|
||||||
|
bool mode_changed = (current_mode != event->mode);
|
||||||
|
|
||||||
|
current_mode = event->mode;
|
||||||
|
|
||||||
|
if (mode_changed || ((current_mode != MODE_SWITCH_USB) &&
|
||||||
|
(current_mode != MODE_SWITCH_BLE))) {
|
||||||
|
clear_pending_reports();
|
||||||
|
}
|
||||||
|
|
||||||
|
try_send_next();
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
static int module_init(void)
|
||||||
|
{
|
||||||
|
clear_pending_reports();
|
||||||
|
current_mode = MODE_SWITCH_USB;
|
||||||
|
transport_state[HID_TRANSPORT_USB].ready = false;
|
||||||
|
transport_state[HID_TRANSPORT_USB].keys_ready = false;
|
||||||
|
transport_state[HID_TRANSPORT_USB].consumer_ready = false;
|
||||||
|
transport_state[HID_TRANSPORT_USB].protocol_mode =
|
||||||
|
KEYBOARD_PROTOCOL_MODE_REPORT;
|
||||||
|
transport_state[HID_TRANSPORT_BLE].ready = false;
|
||||||
|
transport_state[HID_TRANSPORT_BLE].keys_ready = false;
|
||||||
|
transport_state[HID_TRANSPORT_BLE].consumer_ready = false;
|
||||||
|
transport_state[HID_TRANSPORT_BLE].protocol_mode =
|
||||||
|
KEYBOARD_PROTOCOL_MODE_REPORT;
|
||||||
|
next_sequence = 1U;
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
static int module_start(void)
|
||||||
|
{
|
||||||
|
if (running) {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
running = true;
|
||||||
|
try_send_next();
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
static void module_pause(void)
|
||||||
|
{
|
||||||
|
if (!running) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
clear_pending_reports();
|
||||||
|
running = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
static bool app_event_handler(const struct app_event_header *aeh)
|
||||||
|
{
|
||||||
|
if (is_keyboard_hid_report_event(aeh)) {
|
||||||
|
return handle_keyboard_hid_report_event(cast_keyboard_hid_report_event(aeh));
|
||||||
|
}
|
||||||
|
|
||||||
|
if (is_hid_transport_state_event(aeh)) {
|
||||||
|
return handle_hid_transport_state_event(cast_hid_transport_state_event(aeh));
|
||||||
|
}
|
||||||
|
|
||||||
|
if (is_hid_report_sent_event(aeh)) {
|
||||||
|
return handle_hid_report_sent_event(cast_hid_report_sent_event(aeh));
|
||||||
|
}
|
||||||
|
|
||||||
|
if (is_mode_switch_event(aeh)) {
|
||||||
|
return handle_mode_switch_event(cast_mode_switch_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;
|
||||||
|
}
|
||||||
|
|
||||||
|
__ASSERT_NO_MSG(false);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
APP_EVENT_LISTENER(MODULE, app_event_handler);
|
||||||
|
APP_EVENT_SUBSCRIBE(MODULE, keyboard_hid_report_event);
|
||||||
|
APP_EVENT_SUBSCRIBE(MODULE, hid_transport_state_event);
|
||||||
|
APP_EVENT_SUBSCRIBE(MODULE, hid_report_sent_event);
|
||||||
|
APP_EVENT_SUBSCRIBE(MODULE, mode_switch_event);
|
||||||
|
APP_EVENT_SUBSCRIBE(MODULE, module_state_event);
|
||||||
|
APP_EVENT_SUBSCRIBE_EARLY(MODULE, power_down_event);
|
||||||
|
APP_EVENT_SUBSCRIBE(MODULE, wake_up_event);
|
||||||
@@ -14,6 +14,7 @@
|
|||||||
#include <zephyr/sys/byteorder.h>
|
#include <zephyr/sys/byteorder.h>
|
||||||
#include <zephyr/sys/util.h>
|
#include <zephyr/sys/util.h>
|
||||||
|
|
||||||
|
#include "encoder_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"
|
||||||
@@ -84,12 +85,42 @@ 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 enum keyboard_protocol_mode protocol_mode = KEYBOARD_PROTOCOL_MODE_REPORT;
|
static enum keyboard_protocol_mode transport_protocol_modes[HID_TRANSPORT_COUNT] = {
|
||||||
|
[HID_TRANSPORT_USB] = KEYBOARD_PROTOCOL_MODE_REPORT,
|
||||||
|
[HID_TRANSPORT_BLE] = KEYBOARD_PROTOCOL_MODE_REPORT,
|
||||||
|
};
|
||||||
static enum mode_switch_mode current_mode;
|
static enum mode_switch_mode current_mode;
|
||||||
static bool initialized;
|
static bool initialized;
|
||||||
static bool running;
|
static bool running;
|
||||||
static bool mode_valid;
|
static bool mode_valid;
|
||||||
|
|
||||||
|
static bool mode_to_transport(enum mode_switch_mode mode, enum hid_transport *transport)
|
||||||
|
{
|
||||||
|
switch (mode) {
|
||||||
|
case MODE_SWITCH_USB:
|
||||||
|
*transport = HID_TRANSPORT_USB;
|
||||||
|
return true;
|
||||||
|
|
||||||
|
case MODE_SWITCH_BLE:
|
||||||
|
*transport = HID_TRANSPORT_BLE;
|
||||||
|
return true;
|
||||||
|
|
||||||
|
default:
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static enum keyboard_protocol_mode active_protocol_mode_get(void)
|
||||||
|
{
|
||||||
|
enum hid_transport transport;
|
||||||
|
|
||||||
|
if (mode_valid && mode_to_transport(current_mode, &transport)) {
|
||||||
|
return transport_protocol_modes[transport];
|
||||||
|
}
|
||||||
|
|
||||||
|
return KEYBOARD_PROTOCOL_MODE_REPORT;
|
||||||
|
}
|
||||||
|
|
||||||
static const struct keymap_entry *keymap_get(uint16_t key_id)
|
static const struct keymap_entry *keymap_get(uint16_t key_id)
|
||||||
{
|
{
|
||||||
size_t left = 0;
|
size_t left = 0;
|
||||||
@@ -228,25 +259,72 @@ static void build_consumer_report(uint8_t report[KEYBOARD_CONSUMER_REPORT_SIZE])
|
|||||||
}
|
}
|
||||||
|
|
||||||
static void submit_keyboard_report_event(enum keyboard_report_type report_type,
|
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)
|
const uint8_t *data, size_t size)
|
||||||
{
|
{
|
||||||
struct keyboard_hid_report_event *event =
|
struct keyboard_hid_report_event *event =
|
||||||
new_keyboard_hid_report_event(size);
|
new_keyboard_hid_report_event(size);
|
||||||
|
enum keyboard_protocol_mode protocol_mode = active_protocol_mode_get();
|
||||||
|
|
||||||
event->mode = current_mode;
|
event->mode = current_mode;
|
||||||
event->report_type = report_type;
|
event->report_type = report_type;
|
||||||
event->protocol_mode = protocol_mode;
|
event->protocol_mode = protocol_mode;
|
||||||
|
event->queue_policy = queue_policy;
|
||||||
memcpy(event->dyndata.data, data, size);
|
memcpy(event->dyndata.data, data, size);
|
||||||
|
|
||||||
APP_EVENT_SUBMIT(event);
|
APP_EVENT_SUBMIT(event);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static void submit_consumer_fifo_frame(uint16_t usage_id)
|
||||||
|
{
|
||||||
|
uint8_t report_buf[KEYBOARD_CONSUMER_REPORT_SIZE];
|
||||||
|
enum keyboard_protocol_mode protocol_mode = active_protocol_mode_get();
|
||||||
|
|
||||||
|
if (!running || !mode_valid ||
|
||||||
|
(protocol_mode == KEYBOARD_PROTOCOL_MODE_BOOT)) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
sys_put_le16(usage_id, report_buf);
|
||||||
|
submit_keyboard_report_event(KEYBOARD_REPORT_TYPE_CONSUMER,
|
||||||
|
HID_QUEUE_POLICY_FIFO,
|
||||||
|
report_buf,
|
||||||
|
KEYBOARD_CONSUMER_REPORT_SIZE);
|
||||||
|
}
|
||||||
|
|
||||||
|
static void submit_consumer_pulse_frames(enum keyboard_consumer_control control_id,
|
||||||
|
uint8_t pulse_count)
|
||||||
|
{
|
||||||
|
uint16_t usage_id;
|
||||||
|
|
||||||
|
if (active_protocol_mode_get() == KEYBOARD_PROTOCOL_MODE_BOOT) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (control_id >= KEYBOARD_CONSUMER_CTRL_COUNT) {
|
||||||
|
LOG_WRN("Unsupported consumer control id %u", control_id);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
usage_id = consumer_usage_map[control_id];
|
||||||
|
if (usage_id == 0U) {
|
||||||
|
LOG_WRN("Unmapped consumer control id %u", control_id);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
for (uint8_t i = 0; i < pulse_count; i++) {
|
||||||
|
submit_consumer_fifo_frame(usage_id);
|
||||||
|
submit_consumer_fifo_frame(0U);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
static void emit_keys_report(bool force)
|
static void emit_keys_report(bool force)
|
||||||
{
|
{
|
||||||
uint8_t report_buf[KEYBOARD_NKRO_REPORT_SIZE];
|
uint8_t report_buf[KEYBOARD_NKRO_REPORT_SIZE];
|
||||||
uint8_t report_size;
|
uint8_t report_size;
|
||||||
uint8_t *cache_buf;
|
uint8_t *cache_buf;
|
||||||
bool *cache_valid;
|
bool *cache_valid;
|
||||||
|
enum keyboard_protocol_mode protocol_mode = active_protocol_mode_get();
|
||||||
|
|
||||||
if (!mode_valid) {
|
if (!mode_valid) {
|
||||||
return;
|
return;
|
||||||
@@ -271,14 +349,18 @@ 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, report_buf, report_size);
|
submit_keyboard_report_event(KEYBOARD_REPORT_TYPE_KEYS,
|
||||||
|
HID_QUEUE_POLICY_LATEST,
|
||||||
|
report_buf,
|
||||||
|
report_size);
|
||||||
}
|
}
|
||||||
|
|
||||||
static void emit_consumer_report(bool force)
|
static void emit_consumer_report(bool force)
|
||||||
{
|
{
|
||||||
uint8_t report_buf[KEYBOARD_CONSUMER_REPORT_SIZE];
|
uint8_t report_buf[KEYBOARD_CONSUMER_REPORT_SIZE];
|
||||||
|
enum keyboard_protocol_mode protocol_mode = active_protocol_mode_get();
|
||||||
|
|
||||||
if (!mode_valid) {
|
if (!mode_valid || (protocol_mode == KEYBOARD_PROTOCOL_MODE_BOOT)) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -293,6 +375,7 @@ static void emit_consumer_report(bool force)
|
|||||||
reports_cache.consumer_valid = true;
|
reports_cache.consumer_valid = true;
|
||||||
|
|
||||||
submit_keyboard_report_event(KEYBOARD_REPORT_TYPE_CONSUMER,
|
submit_keyboard_report_event(KEYBOARD_REPORT_TYPE_CONSUMER,
|
||||||
|
HID_QUEUE_POLICY_LATEST,
|
||||||
report_buf,
|
report_buf,
|
||||||
KEYBOARD_CONSUMER_REPORT_SIZE);
|
KEYBOARD_CONSUMER_REPORT_SIZE);
|
||||||
}
|
}
|
||||||
@@ -300,7 +383,10 @@ static void emit_consumer_report(bool force)
|
|||||||
static void emit_all_reports(bool force)
|
static void emit_all_reports(bool force)
|
||||||
{
|
{
|
||||||
emit_keys_report(force);
|
emit_keys_report(force);
|
||||||
|
|
||||||
|
if (active_protocol_mode_get() != KEYBOARD_PROTOCOL_MODE_BOOT) {
|
||||||
emit_consumer_report(force);
|
emit_consumer_report(force);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static void emit_release_reports(enum mode_switch_mode mode)
|
static void emit_release_reports(enum mode_switch_mode mode)
|
||||||
@@ -308,6 +394,7 @@ static void emit_release_reports(enum mode_switch_mode mode)
|
|||||||
struct keyboard_hid_report_event *event;
|
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();
|
||||||
size_t keys_report_size =
|
size_t keys_report_size =
|
||||||
(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;
|
||||||
@@ -319,12 +406,15 @@ static void emit_release_reports(enum mode_switch_mode mode)
|
|||||||
memcpy(event->dyndata.data, keys_report, keys_report_size);
|
memcpy(event->dyndata.data, keys_report, keys_report_size);
|
||||||
APP_EVENT_SUBMIT(event);
|
APP_EVENT_SUBMIT(event);
|
||||||
|
|
||||||
|
if (protocol_mode != KEYBOARD_PROTOCOL_MODE_BOOT) {
|
||||||
event = new_keyboard_hid_report_event(KEYBOARD_CONSUMER_REPORT_SIZE);
|
event = new_keyboard_hid_report_event(KEYBOARD_CONSUMER_REPORT_SIZE);
|
||||||
event->mode = mode;
|
event->mode = mode;
|
||||||
event->report_type = KEYBOARD_REPORT_TYPE_CONSUMER;
|
event->report_type = KEYBOARD_REPORT_TYPE_CONSUMER;
|
||||||
event->protocol_mode = protocol_mode;
|
event->protocol_mode = protocol_mode;
|
||||||
memcpy(event->dyndata.data, consumer_report, KEYBOARD_CONSUMER_REPORT_SIZE);
|
memcpy(event->dyndata.data, consumer_report,
|
||||||
|
KEYBOARD_CONSUMER_REPORT_SIZE);
|
||||||
APP_EVENT_SUBMIT(event);
|
APP_EVENT_SUBMIT(event);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static int module_init(void)
|
static int module_init(void)
|
||||||
@@ -332,7 +422,10 @@ static int module_init(void)
|
|||||||
keyboard_state_clear();
|
keyboard_state_clear();
|
||||||
reports_cache_invalidate();
|
reports_cache_invalidate();
|
||||||
mode_valid = false;
|
mode_valid = false;
|
||||||
protocol_mode = KEYBOARD_PROTOCOL_MODE_REPORT;
|
transport_protocol_modes[HID_TRANSPORT_USB] =
|
||||||
|
KEYBOARD_PROTOCOL_MODE_REPORT;
|
||||||
|
transport_protocol_modes[HID_TRANSPORT_BLE] =
|
||||||
|
KEYBOARD_PROTOCOL_MODE_REPORT;
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
@@ -416,20 +509,53 @@ static bool handle_mode_switch_event(const struct mode_switch_event *event)
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static bool handle_encoder_event(const struct encoder_event *event)
|
||||||
|
{
|
||||||
|
if (!running || !mode_valid) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (event->detents > 0) {
|
||||||
|
submit_consumer_pulse_frames(KEYBOARD_CONSUMER_CTRL_VOLUME_UP,
|
||||||
|
(uint8_t)event->detents);
|
||||||
|
} else if (event->detents < 0) {
|
||||||
|
submit_consumer_pulse_frames(KEYBOARD_CONSUMER_CTRL_VOLUME_DOWN,
|
||||||
|
(uint8_t)(-event->detents));
|
||||||
|
}
|
||||||
|
|
||||||
|
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)) {
|
||||||
return handle_button_event(cast_button_event(aeh));
|
return handle_button_event(cast_button_event(aeh));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (is_encoder_event(aeh)) {
|
||||||
|
return handle_encoder_event(cast_encoder_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;
|
||||||
|
|
||||||
if (protocol_mode != event->protocol_mode) {
|
if (event->transport >= HID_TRANSPORT_COUNT) {
|
||||||
protocol_mode = event->protocol_mode;
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
if (running && mode_valid && (current_mode == MODE_SWITCH_USB)) {
|
if (transport_protocol_modes[event->transport] != event->protocol_mode) {
|
||||||
|
transport_protocol_modes[event->transport] = event->protocol_mode;
|
||||||
|
|
||||||
|
if (running && mode_valid &&
|
||||||
|
mode_to_transport(current_mode, &active_transport) &&
|
||||||
|
(active_transport == event->transport)) {
|
||||||
|
reports_cache_invalidate();
|
||||||
emit_keys_report(true);
|
emit_keys_report(true);
|
||||||
|
|
||||||
|
if (event->protocol_mode != KEYBOARD_PROTOCOL_MODE_BOOT) {
|
||||||
|
emit_consumer_report(true);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -496,6 +622,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, 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);
|
||||||
|
|||||||
133
src/protocol_module.c
Normal file
133
src/protocol_module.c
Normal file
@@ -0,0 +1,133 @@
|
|||||||
|
#include <errno.h>
|
||||||
|
#include <stdbool.h>
|
||||||
|
#include <stddef.h>
|
||||||
|
#include <stdint.h>
|
||||||
|
|
||||||
|
#include <zephyr/logging/log.h>
|
||||||
|
|
||||||
|
#include <pb_decode.h>
|
||||||
|
#include <pb_encode.h>
|
||||||
|
|
||||||
|
#include <proto/device_comm.pb.h>
|
||||||
|
|
||||||
|
#include "protocol_module.h"
|
||||||
|
|
||||||
|
LOG_MODULE_REGISTER(protocol_module, LOG_LEVEL_INF);
|
||||||
|
|
||||||
|
#define PROTOCOL_VERSION 1U
|
||||||
|
#define PROTOCOL_VENDOR_ID 0x1915U
|
||||||
|
#define PROTOCOL_PRODUCT_ID 0x52F0U
|
||||||
|
#define PROTOCOL_FIRMWARE_MAJOR 0U
|
||||||
|
#define PROTOCOL_FIRMWARE_MINOR 0U
|
||||||
|
#define PROTOCOL_CAPABILITY_FLAGS 0U
|
||||||
|
|
||||||
|
static bool type_matches_body(uint8_t type, const CdcPacketBody *body)
|
||||||
|
{
|
||||||
|
switch (type) {
|
||||||
|
case CDC_PROTO_TYPE_HELLO_REQ:
|
||||||
|
return body->which_body == CdcPacketBody_hello_req_tag;
|
||||||
|
case CDC_PROTO_TYPE_HELLO_RSP:
|
||||||
|
return body->which_body == CdcPacketBody_hello_rsp_tag;
|
||||||
|
default:
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static int decode_body(const uint8_t *payload, size_t payload_len,
|
||||||
|
CdcPacketBody *body)
|
||||||
|
{
|
||||||
|
pb_istream_t stream;
|
||||||
|
|
||||||
|
if ((payload == NULL) || (body == NULL)) {
|
||||||
|
return -EINVAL;
|
||||||
|
}
|
||||||
|
|
||||||
|
*body = (CdcPacketBody)CdcPacketBody_init_zero;
|
||||||
|
stream = pb_istream_from_buffer(payload, payload_len);
|
||||||
|
|
||||||
|
if (!pb_decode(&stream, CdcPacketBody_fields, body)) {
|
||||||
|
LOG_WRN("pb_decode failed: %s", PB_GET_ERROR(&stream));
|
||||||
|
return -EBADMSG;
|
||||||
|
}
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
static int encode_hello_rsp(uint8_t *rsp_payload, size_t rsp_payload_buf_size,
|
||||||
|
size_t *rsp_payload_len)
|
||||||
|
{
|
||||||
|
CdcPacketBody body = CdcPacketBody_init_zero;
|
||||||
|
pb_ostream_t stream;
|
||||||
|
|
||||||
|
if ((rsp_payload == NULL) || (rsp_payload_len == NULL)) {
|
||||||
|
return -EINVAL;
|
||||||
|
}
|
||||||
|
|
||||||
|
body.which_body = CdcPacketBody_hello_rsp_tag;
|
||||||
|
body.body.hello_rsp.protocol_version = PROTOCOL_VERSION;
|
||||||
|
body.body.hello_rsp.vendor_id = PROTOCOL_VENDOR_ID;
|
||||||
|
body.body.hello_rsp.product_id = PROTOCOL_PRODUCT_ID;
|
||||||
|
body.body.hello_rsp.firmware_major = PROTOCOL_FIRMWARE_MAJOR;
|
||||||
|
body.body.hello_rsp.firmware_minor = PROTOCOL_FIRMWARE_MINOR;
|
||||||
|
body.body.hello_rsp.capability_flags = PROTOCOL_CAPABILITY_FLAGS;
|
||||||
|
|
||||||
|
stream = pb_ostream_from_buffer(rsp_payload, rsp_payload_buf_size);
|
||||||
|
|
||||||
|
if (!pb_encode(&stream, CdcPacketBody_fields, &body)) {
|
||||||
|
LOG_WRN("pb_encode failed: %s", PB_GET_ERROR(&stream));
|
||||||
|
return -EIO;
|
||||||
|
}
|
||||||
|
|
||||||
|
*rsp_payload_len = stream.bytes_written;
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
int protocol_module_process_cdc_packet(uint8_t req_type,
|
||||||
|
const uint8_t *req_payload,
|
||||||
|
size_t req_payload_len,
|
||||||
|
uint8_t *rsp_type,
|
||||||
|
uint8_t *rsp_payload,
|
||||||
|
size_t rsp_payload_buf_size,
|
||||||
|
size_t *rsp_payload_len)
|
||||||
|
{
|
||||||
|
CdcPacketBody body;
|
||||||
|
int err;
|
||||||
|
|
||||||
|
if ((rsp_type == NULL) || (rsp_payload == NULL) || (rsp_payload_len == NULL)) {
|
||||||
|
return -EINVAL;
|
||||||
|
}
|
||||||
|
|
||||||
|
err = decode_body(req_payload, req_payload_len, &body);
|
||||||
|
if (err) {
|
||||||
|
return err;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!type_matches_body(req_type, &body)) {
|
||||||
|
LOG_WRN("CDC type/body mismatch type:0x%02x body_case:%d",
|
||||||
|
req_type, body.which_body);
|
||||||
|
return -EBADMSG;
|
||||||
|
}
|
||||||
|
|
||||||
|
switch (req_type) {
|
||||||
|
case CDC_PROTO_TYPE_HELLO_REQ:
|
||||||
|
LOG_INF("HelloReq protocol_version:%u",
|
||||||
|
body.body.hello_req.protocol_version);
|
||||||
|
|
||||||
|
if (body.body.hello_req.protocol_version != PROTOCOL_VERSION) {
|
||||||
|
LOG_WRN("Unexpected protocol version:%u",
|
||||||
|
body.body.hello_req.protocol_version);
|
||||||
|
}
|
||||||
|
|
||||||
|
err = encode_hello_rsp(rsp_payload, rsp_payload_buf_size, rsp_payload_len);
|
||||||
|
if (err) {
|
||||||
|
return err;
|
||||||
|
}
|
||||||
|
|
||||||
|
*rsp_type = CDC_PROTO_TYPE_HELLO_RSP;
|
||||||
|
return 0;
|
||||||
|
|
||||||
|
default:
|
||||||
|
LOG_WRN("Unsupported CDC protocol type:0x%02x", req_type);
|
||||||
|
return -ENOTSUP;
|
||||||
|
}
|
||||||
|
}
|
||||||
291
src/ui/ui_main.c
Normal file
291
src/ui/ui_main.c
Normal file
@@ -0,0 +1,291 @@
|
|||||||
|
#include <string.h>
|
||||||
|
|
||||||
|
#include <lvgl.h>
|
||||||
|
#include <zephyr/sys/printk.h>
|
||||||
|
|
||||||
|
#include "ui_main.h"
|
||||||
|
|
||||||
|
enum ui_status_id {
|
||||||
|
UI_STATUS_USB = 0,
|
||||||
|
UI_STATUS_BLE,
|
||||||
|
UI_STATUS_NUMLOCK,
|
||||||
|
UI_STATUS_CAPSLOCK,
|
||||||
|
UI_STATUS_COUNT,
|
||||||
|
};
|
||||||
|
|
||||||
|
enum {
|
||||||
|
UI_LED_MASK_NUM_LOCK = BIT(0),
|
||||||
|
UI_LED_MASK_CAPS_LOCK = BIT(1),
|
||||||
|
};
|
||||||
|
|
||||||
|
struct ui_main_ctx {
|
||||||
|
lv_obj_t *status_badges[UI_STATUS_COUNT];
|
||||||
|
lv_obj_t *status_labels[UI_STATUS_COUNT];
|
||||||
|
lv_obj_t *battery_icon;
|
||||||
|
lv_obj_t *battery_label;
|
||||||
|
lv_obj_t *battery_state_label;
|
||||||
|
lv_obj_t *date_label;
|
||||||
|
lv_obj_t *time_label;
|
||||||
|
};
|
||||||
|
|
||||||
|
static struct ui_main_ctx g_ui;
|
||||||
|
static bool ui_initialized;
|
||||||
|
|
||||||
|
static const char *const status_texts[UI_STATUS_COUNT] = {
|
||||||
|
LV_SYMBOL_USB,
|
||||||
|
LV_SYMBOL_BLUETOOTH,
|
||||||
|
"1",
|
||||||
|
"A",
|
||||||
|
};
|
||||||
|
|
||||||
|
static lv_color_t ui_main_get_battery_color(uint8_t battery_level)
|
||||||
|
{
|
||||||
|
if (battery_level > 70U) {
|
||||||
|
return lv_color_hex(0x8BD450);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (battery_level >= 20U) {
|
||||||
|
return lv_color_hex(0xF4D35E);
|
||||||
|
}
|
||||||
|
|
||||||
|
return lv_color_hex(0xE63946);
|
||||||
|
}
|
||||||
|
|
||||||
|
static const char *ui_main_get_battery_symbol(uint8_t battery_level)
|
||||||
|
{
|
||||||
|
if (battery_level > 85U) {
|
||||||
|
return LV_SYMBOL_BATTERY_FULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (battery_level > 60U) {
|
||||||
|
return LV_SYMBOL_BATTERY_3;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (battery_level > 35U) {
|
||||||
|
return LV_SYMBOL_BATTERY_2;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (battery_level >= 20U) {
|
||||||
|
return LV_SYMBOL_BATTERY_1;
|
||||||
|
}
|
||||||
|
|
||||||
|
return LV_SYMBOL_BATTERY_EMPTY;
|
||||||
|
}
|
||||||
|
|
||||||
|
static bool ui_main_status_is_active(enum ui_status_id id,
|
||||||
|
const struct ui_main_model *model)
|
||||||
|
{
|
||||||
|
switch (id) {
|
||||||
|
case UI_STATUS_USB:
|
||||||
|
return model->mode == MODE_SWITCH_USB;
|
||||||
|
|
||||||
|
case UI_STATUS_BLE:
|
||||||
|
return model->mode == MODE_SWITCH_BLE;
|
||||||
|
|
||||||
|
case UI_STATUS_NUMLOCK:
|
||||||
|
return (model->led_mask & UI_LED_MASK_NUM_LOCK) != 0U;
|
||||||
|
|
||||||
|
case UI_STATUS_CAPSLOCK:
|
||||||
|
return (model->led_mask & UI_LED_MASK_CAPS_LOCK) != 0U;
|
||||||
|
|
||||||
|
default:
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static void ui_main_create_status_chip(lv_obj_t *parent, enum ui_status_id id)
|
||||||
|
{
|
||||||
|
lv_obj_t *badge = lv_obj_create(parent);
|
||||||
|
lv_obj_t *label = lv_label_create(badge);
|
||||||
|
|
||||||
|
lv_obj_remove_style_all(badge);
|
||||||
|
lv_obj_set_size(badge, 50, 32);
|
||||||
|
lv_obj_set_style_radius(badge, 10, 0);
|
||||||
|
lv_obj_set_style_bg_opa(badge, LV_OPA_COVER, 0);
|
||||||
|
lv_obj_set_style_pad_all(badge, 0, 0);
|
||||||
|
|
||||||
|
lv_label_set_text(label, status_texts[id]);
|
||||||
|
lv_obj_set_width(label, LV_PCT(100));
|
||||||
|
lv_obj_set_style_text_font(label, &lv_font_montserrat_14, 0);
|
||||||
|
lv_obj_set_style_text_align(label, LV_TEXT_ALIGN_CENTER, 0);
|
||||||
|
lv_obj_center(label);
|
||||||
|
|
||||||
|
g_ui.status_badges[id] = badge;
|
||||||
|
g_ui.status_labels[id] = label;
|
||||||
|
}
|
||||||
|
|
||||||
|
void ui_main_refresh_status_bar(const struct ui_main_model *model)
|
||||||
|
{
|
||||||
|
for (uint32_t i = 0; i < UI_STATUS_COUNT; i++) {
|
||||||
|
lv_obj_t *badge = g_ui.status_badges[i];
|
||||||
|
lv_obj_t *label = g_ui.status_labels[i];
|
||||||
|
bool active = ui_main_status_is_active((enum ui_status_id)i, model);
|
||||||
|
|
||||||
|
if ((badge == NULL) || (label == NULL)) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
lv_obj_set_style_border_width(badge, 3, 0);
|
||||||
|
lv_obj_set_style_border_color(
|
||||||
|
badge,
|
||||||
|
active ? model->theme_color : model->inactive_border_color, 0);
|
||||||
|
lv_obj_set_style_bg_color(
|
||||||
|
badge,
|
||||||
|
active ? lv_color_hex(0x1D2735) : lv_color_hex(0x161A20), 0);
|
||||||
|
lv_obj_set_style_text_color(
|
||||||
|
label,
|
||||||
|
active ? lv_color_white() : lv_color_hex(0x7C8798), 0);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void ui_main_refresh_battery(const struct ui_main_model *model)
|
||||||
|
{
|
||||||
|
char battery_text[8];
|
||||||
|
const char *state_symbol = "";
|
||||||
|
lv_color_t battery_color;
|
||||||
|
lv_color_t state_color = lv_color_white();
|
||||||
|
|
||||||
|
if ((g_ui.battery_icon == NULL) || (g_ui.battery_label == NULL) ||
|
||||||
|
(g_ui.battery_state_label == NULL)) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
battery_color = ui_main_get_battery_color(model->battery_level);
|
||||||
|
snprintk(battery_text, sizeof(battery_text), "%u%%", model->battery_level);
|
||||||
|
|
||||||
|
if (model->full) {
|
||||||
|
state_symbol = LV_SYMBOL_USB;
|
||||||
|
state_color = lv_color_hex(0x4C9EF5);
|
||||||
|
} else if (model->charging) {
|
||||||
|
state_symbol = LV_SYMBOL_CHARGE;
|
||||||
|
state_color = lv_color_hex(0xF4D35E);
|
||||||
|
}
|
||||||
|
|
||||||
|
lv_label_set_text(g_ui.battery_icon,
|
||||||
|
ui_main_get_battery_symbol(model->battery_level));
|
||||||
|
lv_obj_set_style_text_color(g_ui.battery_icon, battery_color, 0);
|
||||||
|
lv_label_set_text(g_ui.battery_label, battery_text);
|
||||||
|
lv_label_set_text(g_ui.battery_state_label, state_symbol);
|
||||||
|
lv_obj_set_style_text_color(g_ui.battery_state_label, state_color, 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
void ui_main_refresh_datetime(const char *date_text, const char *time_text)
|
||||||
|
{
|
||||||
|
if ((g_ui.date_label == NULL) || (g_ui.time_label == NULL)) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
lv_label_set_text(g_ui.date_label, date_text);
|
||||||
|
lv_label_set_text(g_ui.time_label, time_text);
|
||||||
|
}
|
||||||
|
|
||||||
|
void ui_main_refresh_all(const struct ui_main_model *model,
|
||||||
|
const char *date_text,
|
||||||
|
const char *time_text)
|
||||||
|
{
|
||||||
|
ui_main_refresh_status_bar(model);
|
||||||
|
ui_main_refresh_battery(model);
|
||||||
|
ui_main_refresh_datetime(date_text, time_text);
|
||||||
|
}
|
||||||
|
|
||||||
|
void ui_main_init(const struct ui_main_model *model,
|
||||||
|
const char *date_text,
|
||||||
|
const char *time_text)
|
||||||
|
{
|
||||||
|
lv_obj_t *screen = lv_screen_active();
|
||||||
|
lv_obj_t *content;
|
||||||
|
lv_obj_t *top_row;
|
||||||
|
lv_obj_t *battery_wrap;
|
||||||
|
lv_obj_t *middle_row;
|
||||||
|
lv_obj_t *bottom_row;
|
||||||
|
|
||||||
|
if (ui_initialized) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
memset(&g_ui, 0, sizeof(g_ui));
|
||||||
|
|
||||||
|
lv_obj_clean(screen);
|
||||||
|
lv_obj_set_style_bg_color(screen, lv_color_hex(0x0F1115), 0);
|
||||||
|
lv_obj_set_style_bg_grad_color(screen, lv_color_hex(0x1A1F29), 0);
|
||||||
|
lv_obj_set_style_bg_grad_dir(screen, LV_GRAD_DIR_VER, 0);
|
||||||
|
lv_obj_set_style_bg_opa(screen, LV_OPA_COVER, 0);
|
||||||
|
lv_obj_set_style_text_color(screen, lv_color_white(), 0);
|
||||||
|
lv_obj_set_style_pad_all(screen, 0, 0);
|
||||||
|
lv_obj_set_scrollbar_mode(screen, LV_SCROLLBAR_MODE_OFF);
|
||||||
|
|
||||||
|
content = lv_obj_create(screen);
|
||||||
|
lv_obj_remove_style_all(content);
|
||||||
|
lv_obj_set_size(content, LV_PCT(100), LV_PCT(100));
|
||||||
|
lv_obj_set_style_bg_opa(content, LV_OPA_TRANSP, 0);
|
||||||
|
lv_obj_set_style_pad_left(content, 14, 0);
|
||||||
|
lv_obj_set_style_pad_right(content, 14, 0);
|
||||||
|
lv_obj_set_style_pad_top(content, 8, 0);
|
||||||
|
lv_obj_set_style_pad_bottom(content, 8, 0);
|
||||||
|
lv_obj_set_layout(content, LV_LAYOUT_FLEX);
|
||||||
|
lv_obj_set_flex_flow(content, LV_FLEX_FLOW_COLUMN);
|
||||||
|
lv_obj_set_flex_align(content, LV_FLEX_ALIGN_START, LV_FLEX_ALIGN_CENTER,
|
||||||
|
LV_FLEX_ALIGN_CENTER);
|
||||||
|
|
||||||
|
top_row = lv_obj_create(content);
|
||||||
|
lv_obj_remove_style_all(top_row);
|
||||||
|
lv_obj_set_width(top_row, LV_PCT(100));
|
||||||
|
lv_obj_set_flex_grow(top_row, 1);
|
||||||
|
lv_obj_set_style_bg_opa(top_row, LV_OPA_TRANSP, 0);
|
||||||
|
lv_obj_set_layout(top_row, LV_LAYOUT_FLEX);
|
||||||
|
lv_obj_set_flex_flow(top_row, LV_FLEX_FLOW_ROW);
|
||||||
|
lv_obj_set_flex_align(top_row, LV_FLEX_ALIGN_SPACE_BETWEEN,
|
||||||
|
LV_FLEX_ALIGN_CENTER, LV_FLEX_ALIGN_CENTER);
|
||||||
|
|
||||||
|
g_ui.date_label = lv_label_create(top_row);
|
||||||
|
lv_obj_set_style_text_font(g_ui.date_label, &lv_font_montserrat_14, 0);
|
||||||
|
lv_obj_set_style_text_color(g_ui.date_label, lv_color_hex(0xD8DEE9), 0);
|
||||||
|
|
||||||
|
battery_wrap = lv_obj_create(top_row);
|
||||||
|
lv_obj_remove_style_all(battery_wrap);
|
||||||
|
lv_obj_set_width(battery_wrap, LV_SIZE_CONTENT);
|
||||||
|
lv_obj_set_layout(battery_wrap, LV_LAYOUT_FLEX);
|
||||||
|
lv_obj_set_flex_flow(battery_wrap, LV_FLEX_FLOW_ROW);
|
||||||
|
lv_obj_set_flex_align(battery_wrap, LV_FLEX_ALIGN_CENTER,
|
||||||
|
LV_FLEX_ALIGN_CENTER, LV_FLEX_ALIGN_CENTER);
|
||||||
|
lv_obj_set_style_pad_column(battery_wrap, 4, 0);
|
||||||
|
|
||||||
|
g_ui.battery_icon = lv_label_create(battery_wrap);
|
||||||
|
lv_obj_set_style_text_font(g_ui.battery_icon, &lv_font_montserrat_14, 0);
|
||||||
|
|
||||||
|
g_ui.battery_label = lv_label_create(battery_wrap);
|
||||||
|
lv_obj_set_style_text_font(g_ui.battery_label, &lv_font_montserrat_14, 0);
|
||||||
|
lv_obj_set_style_text_color(g_ui.battery_label, lv_color_hex(0xD8DEE9), 0);
|
||||||
|
|
||||||
|
g_ui.battery_state_label = lv_label_create(battery_wrap);
|
||||||
|
lv_obj_set_style_text_font(g_ui.battery_state_label, &lv_font_montserrat_14, 0);
|
||||||
|
|
||||||
|
middle_row = lv_obj_create(content);
|
||||||
|
lv_obj_remove_style_all(middle_row);
|
||||||
|
lv_obj_set_width(middle_row, LV_PCT(100));
|
||||||
|
lv_obj_set_flex_grow(middle_row, 2);
|
||||||
|
lv_obj_set_style_bg_opa(middle_row, LV_OPA_TRANSP, 0);
|
||||||
|
|
||||||
|
g_ui.time_label = lv_label_create(middle_row);
|
||||||
|
lv_obj_set_style_text_font(g_ui.time_label, &lv_font_montserrat_32, 0);
|
||||||
|
lv_obj_set_style_text_color(g_ui.time_label, lv_color_white(), 0);
|
||||||
|
lv_obj_center(g_ui.time_label);
|
||||||
|
|
||||||
|
bottom_row = lv_obj_create(content);
|
||||||
|
lv_obj_remove_style_all(bottom_row);
|
||||||
|
lv_obj_set_width(bottom_row, LV_PCT(100));
|
||||||
|
lv_obj_set_flex_grow(bottom_row, 1);
|
||||||
|
lv_obj_set_style_bg_opa(bottom_row, LV_OPA_TRANSP, 0);
|
||||||
|
lv_obj_set_layout(bottom_row, LV_LAYOUT_FLEX);
|
||||||
|
lv_obj_set_flex_flow(bottom_row, LV_FLEX_FLOW_ROW);
|
||||||
|
lv_obj_set_flex_align(bottom_row, LV_FLEX_ALIGN_CENTER,
|
||||||
|
LV_FLEX_ALIGN_CENTER, LV_FLEX_ALIGN_CENTER);
|
||||||
|
lv_obj_set_style_pad_column(bottom_row, 6, 0);
|
||||||
|
|
||||||
|
for (uint32_t i = 0; i < UI_STATUS_COUNT; i++) {
|
||||||
|
ui_main_create_status_chip(bottom_row, (enum ui_status_id)i);
|
||||||
|
}
|
||||||
|
|
||||||
|
ui_main_refresh_all(model, date_text, time_text);
|
||||||
|
ui_initialized = true;
|
||||||
|
}
|
||||||
39
src/ui/ui_main.h
Normal file
39
src/ui/ui_main.h
Normal file
@@ -0,0 +1,39 @@
|
|||||||
|
#ifndef BLINKY_UI_MAIN_H_
|
||||||
|
#define BLINKY_UI_MAIN_H_
|
||||||
|
|
||||||
|
#include <stdbool.h>
|
||||||
|
#include <stdint.h>
|
||||||
|
|
||||||
|
#include <lvgl.h>
|
||||||
|
|
||||||
|
#include "mode_switch_event.h"
|
||||||
|
|
||||||
|
#ifdef __cplusplus
|
||||||
|
extern "C" {
|
||||||
|
#endif
|
||||||
|
|
||||||
|
struct ui_main_model {
|
||||||
|
lv_color_t theme_color;
|
||||||
|
lv_color_t inactive_border_color;
|
||||||
|
uint8_t battery_level;
|
||||||
|
enum mode_switch_mode mode;
|
||||||
|
uint8_t led_mask;
|
||||||
|
bool charging;
|
||||||
|
bool full;
|
||||||
|
};
|
||||||
|
|
||||||
|
void ui_main_init(const struct ui_main_model *model,
|
||||||
|
const char *date_text,
|
||||||
|
const char *time_text);
|
||||||
|
void ui_main_refresh_all(const struct ui_main_model *model,
|
||||||
|
const char *date_text,
|
||||||
|
const char *time_text);
|
||||||
|
void ui_main_refresh_status_bar(const struct ui_main_model *model);
|
||||||
|
void ui_main_refresh_battery(const struct ui_main_model *model);
|
||||||
|
void ui_main_refresh_datetime(const char *date_text, const char *time_text);
|
||||||
|
|
||||||
|
#ifdef __cplusplus
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#endif /* BLINKY_UI_MAIN_H_ */
|
||||||
426
src/usb_cdc_module.c
Normal file
426
src/usb_cdc_module.c
Normal file
@@ -0,0 +1,426 @@
|
|||||||
|
#include <errno.h>
|
||||||
|
#include <stdbool.h>
|
||||||
|
#include <stdint.h>
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
|
#include <app_event_manager.h>
|
||||||
|
|
||||||
|
#define MODULE usb_cdc_module
|
||||||
|
#include <caf/events/module_state_event.h>
|
||||||
|
#include <caf/events/power_event.h>
|
||||||
|
|
||||||
|
#include <zephyr/device.h>
|
||||||
|
#include <zephyr/drivers/uart.h>
|
||||||
|
#include <zephyr/kernel.h>
|
||||||
|
#include <zephyr/logging/log.h>
|
||||||
|
#include <zephyr/sys/ring_buffer.h>
|
||||||
|
#include <zephyr/sys/util.h>
|
||||||
|
|
||||||
|
#include "usb_cdc_rx_event.h"
|
||||||
|
#include "usb_cdc_tx_event.h"
|
||||||
|
#include "usb_function_ready_event.h"
|
||||||
|
#include "usb_prepare_event.h"
|
||||||
|
#include "usb_device_module.h"
|
||||||
|
#include "usb_device_state_event.h"
|
||||||
|
|
||||||
|
LOG_MODULE_REGISTER(MODULE, LOG_LEVEL_INF);
|
||||||
|
|
||||||
|
#define USB_CDC_RX_RING_BUF_SIZE 256
|
||||||
|
#define USB_CDC_TX_RING_BUF_SIZE 256
|
||||||
|
#define USB_CDC_RX_CHUNK_SIZE 32
|
||||||
|
#define USB_CDC_CONTROL_POLL_INTERVAL K_MSEC(100)
|
||||||
|
#define USB_CDC_EXPECTED_BAUDRATE 115200U
|
||||||
|
|
||||||
|
static const struct device *const cdc_dev = DEVICE_DT_GET_ONE(zephyr_cdc_acm_uart);
|
||||||
|
|
||||||
|
static uint8_t rx_ring_buffer[USB_CDC_RX_RING_BUF_SIZE];
|
||||||
|
static uint8_t tx_ring_buffer[USB_CDC_TX_RING_BUF_SIZE];
|
||||||
|
static struct ring_buf rx_ringbuf;
|
||||||
|
static struct ring_buf tx_ringbuf;
|
||||||
|
static struct k_work rx_work;
|
||||||
|
static struct k_work_delayable control_work;
|
||||||
|
static bool initialized;
|
||||||
|
static bool running;
|
||||||
|
static bool usb_active;
|
||||||
|
static bool usb_function_prepared;
|
||||||
|
static bool dtr_ready;
|
||||||
|
static bool rx_enabled;
|
||||||
|
|
||||||
|
static void submit_usb_cdc_rx_event(const uint8_t *data, size_t len)
|
||||||
|
{
|
||||||
|
struct usb_cdc_rx_event *event = new_usb_cdc_rx_event(len);
|
||||||
|
|
||||||
|
memcpy(event->dyndata.data, data, len);
|
||||||
|
APP_EVENT_SUBMIT(event);
|
||||||
|
}
|
||||||
|
|
||||||
|
static void submit_usb_function_ready_event(void)
|
||||||
|
{
|
||||||
|
struct usb_function_ready_event *event = new_usb_function_ready_event();
|
||||||
|
|
||||||
|
event->function_mask = USB_FUNCTION_CDC_ACM;
|
||||||
|
APP_EVENT_SUBMIT(event);
|
||||||
|
}
|
||||||
|
|
||||||
|
static void reset_ring_buffers(void)
|
||||||
|
{
|
||||||
|
unsigned int key = irq_lock();
|
||||||
|
|
||||||
|
ring_buf_init(&rx_ringbuf, sizeof(rx_ring_buffer), rx_ring_buffer);
|
||||||
|
ring_buf_init(&tx_ringbuf, sizeof(tx_ring_buffer), tx_ring_buffer);
|
||||||
|
|
||||||
|
irq_unlock(key);
|
||||||
|
}
|
||||||
|
|
||||||
|
static void disable_uart_io(void)
|
||||||
|
{
|
||||||
|
uart_irq_rx_disable(cdc_dev);
|
||||||
|
uart_irq_tx_disable(cdc_dev);
|
||||||
|
rx_enabled = false;
|
||||||
|
dtr_ready = false;
|
||||||
|
reset_ring_buffers();
|
||||||
|
}
|
||||||
|
|
||||||
|
static void kick_tx(void)
|
||||||
|
{
|
||||||
|
if (!running || !usb_active || !dtr_ready) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
uart_irq_tx_enable(cdc_dev);
|
||||||
|
}
|
||||||
|
|
||||||
|
static void validate_line_coding(void)
|
||||||
|
{
|
||||||
|
uint32_t baudrate = 0U;
|
||||||
|
int err;
|
||||||
|
|
||||||
|
err = uart_line_ctrl_get(cdc_dev, UART_LINE_CTRL_BAUD_RATE, &baudrate);
|
||||||
|
if (err) {
|
||||||
|
LOG_WRN("Failed to get CDC baudrate (%d)", err);
|
||||||
|
} else {
|
||||||
|
LOG_INF("CDC baudrate %u", baudrate);
|
||||||
|
if (baudrate != USB_CDC_EXPECTED_BAUDRATE) {
|
||||||
|
LOG_WRN("Expected CDC baudrate %u, got %u",
|
||||||
|
USB_CDC_EXPECTED_BAUDRATE, baudrate);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#ifdef CONFIG_UART_USE_RUNTIME_CONFIGURE
|
||||||
|
{
|
||||||
|
struct uart_config cfg;
|
||||||
|
|
||||||
|
err = uart_config_get(cdc_dev, &cfg);
|
||||||
|
if (err) {
|
||||||
|
LOG_WRN("uart_config_get failed (%d)", err);
|
||||||
|
} else {
|
||||||
|
LOG_INF("CDC line coding data:%u stop:%u parity:%u flow:%u",
|
||||||
|
cfg.data_bits, cfg.stop_bits, cfg.parity,
|
||||||
|
cfg.flow_ctrl);
|
||||||
|
if ((cfg.data_bits != UART_CFG_DATA_BITS_8) ||
|
||||||
|
(cfg.stop_bits != UART_CFG_STOP_BITS_1) ||
|
||||||
|
(cfg.parity != UART_CFG_PARITY_NONE) ||
|
||||||
|
(cfg.flow_ctrl != UART_CFG_FLOW_CTRL_NONE)) {
|
||||||
|
LOG_WRN("Expected CDC line coding 115200 8N1 no flow control");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
|
||||||
|
static void rx_work_handler(struct k_work *work)
|
||||||
|
{
|
||||||
|
uint8_t buffer[USB_CDC_RX_CHUNK_SIZE];
|
||||||
|
|
||||||
|
ARG_UNUSED(work);
|
||||||
|
|
||||||
|
while (true) {
|
||||||
|
uint32_t len;
|
||||||
|
unsigned int key = irq_lock();
|
||||||
|
|
||||||
|
len = ring_buf_get(&rx_ringbuf, buffer, sizeof(buffer));
|
||||||
|
irq_unlock(key);
|
||||||
|
|
||||||
|
if (len == 0U) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
submit_usb_cdc_rx_event(buffer, len);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static void control_work_handler(struct k_work *work)
|
||||||
|
{
|
||||||
|
uint32_t dtr = 0U;
|
||||||
|
int err;
|
||||||
|
|
||||||
|
ARG_UNUSED(work);
|
||||||
|
|
||||||
|
if (!running || !usb_active) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
err = uart_line_ctrl_get(cdc_dev, UART_LINE_CTRL_DTR, &dtr);
|
||||||
|
if (err) {
|
||||||
|
LOG_WRN("Failed to get CDC DTR (%d)", err);
|
||||||
|
goto reschedule;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (dtr && !dtr_ready) {
|
||||||
|
dtr_ready = true;
|
||||||
|
LOG_INF("CDC DTR set");
|
||||||
|
validate_line_coding();
|
||||||
|
|
||||||
|
err = uart_line_ctrl_set(cdc_dev, UART_LINE_CTRL_DCD, 1);
|
||||||
|
if (err) {
|
||||||
|
LOG_WRN("Failed to set DCD (%d)", err);
|
||||||
|
}
|
||||||
|
|
||||||
|
err = uart_line_ctrl_set(cdc_dev, UART_LINE_CTRL_DSR, 1);
|
||||||
|
if (err) {
|
||||||
|
LOG_WRN("Failed to set DSR (%d)", err);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!rx_enabled) {
|
||||||
|
uart_irq_rx_enable(cdc_dev);
|
||||||
|
rx_enabled = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
kick_tx();
|
||||||
|
} else if (!dtr && dtr_ready) {
|
||||||
|
LOG_INF("CDC DTR cleared");
|
||||||
|
disable_uart_io();
|
||||||
|
}
|
||||||
|
|
||||||
|
reschedule:
|
||||||
|
k_work_reschedule(&control_work, USB_CDC_CONTROL_POLL_INTERVAL);
|
||||||
|
}
|
||||||
|
|
||||||
|
static void cdc_interrupt_handler(const struct device *dev, void *user_data)
|
||||||
|
{
|
||||||
|
ARG_UNUSED(user_data);
|
||||||
|
|
||||||
|
while (uart_irq_update(dev) && uart_irq_is_pending(dev)) {
|
||||||
|
if (uart_irq_rx_ready(dev)) {
|
||||||
|
uint8_t buffer[USB_CDC_RX_CHUNK_SIZE];
|
||||||
|
int recv_len = uart_fifo_read(dev, buffer, sizeof(buffer));
|
||||||
|
|
||||||
|
if (recv_len < 0) {
|
||||||
|
LOG_ERR("Failed to read CDC RX FIFO");
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (recv_len > 0) {
|
||||||
|
uint32_t written;
|
||||||
|
unsigned int key = irq_lock();
|
||||||
|
|
||||||
|
written = ring_buf_put(&rx_ringbuf, buffer,
|
||||||
|
(uint32_t)recv_len);
|
||||||
|
irq_unlock(key);
|
||||||
|
|
||||||
|
if (written < (uint32_t)recv_len) {
|
||||||
|
LOG_WRN("Drop %d CDC RX bytes", recv_len - (int)written);
|
||||||
|
}
|
||||||
|
|
||||||
|
k_work_submit(&rx_work);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (uart_irq_tx_ready(dev)) {
|
||||||
|
uint8_t buffer[USB_CDC_RX_CHUNK_SIZE];
|
||||||
|
uint32_t len;
|
||||||
|
int sent_len;
|
||||||
|
unsigned int key = irq_lock();
|
||||||
|
|
||||||
|
len = ring_buf_get(&tx_ringbuf, buffer, sizeof(buffer));
|
||||||
|
irq_unlock(key);
|
||||||
|
|
||||||
|
if (len == 0U) {
|
||||||
|
uart_irq_tx_disable(dev);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
sent_len = uart_fifo_fill(dev, buffer, len);
|
||||||
|
if (sent_len < 0) {
|
||||||
|
LOG_ERR("Failed to write CDC TX FIFO");
|
||||||
|
uart_irq_tx_disable(dev);
|
||||||
|
} else if ((uint32_t)sent_len < len) {
|
||||||
|
LOG_WRN("Drop %u CDC TX bytes",
|
||||||
|
(unsigned int)(len - (uint32_t)sent_len));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static int module_init(void)
|
||||||
|
{
|
||||||
|
if (!device_is_ready(cdc_dev)) {
|
||||||
|
LOG_ERR("CDC ACM device not ready");
|
||||||
|
return -ENODEV;
|
||||||
|
}
|
||||||
|
|
||||||
|
reset_ring_buffers();
|
||||||
|
k_work_init(&rx_work, rx_work_handler);
|
||||||
|
k_work_init_delayable(&control_work, control_work_handler);
|
||||||
|
uart_irq_callback_set(cdc_dev, cdc_interrupt_handler);
|
||||||
|
usb_function_prepared = false;
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
static int module_start(void)
|
||||||
|
{
|
||||||
|
if (running) {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
running = true;
|
||||||
|
if (usb_active) {
|
||||||
|
k_work_reschedule(&control_work, K_NO_WAIT);
|
||||||
|
}
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
static void module_pause(void)
|
||||||
|
{
|
||||||
|
if (!running) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
k_work_cancel_delayable(&control_work);
|
||||||
|
disable_uart_io();
|
||||||
|
running = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
static bool handle_usb_prepare_event(const struct usb_prepare_event *event)
|
||||||
|
{
|
||||||
|
ARG_UNUSED(event);
|
||||||
|
|
||||||
|
if (!running || usb_function_prepared) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
usb_function_prepared = true;
|
||||||
|
submit_usb_function_ready_event();
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
static bool handle_usb_device_state_event(const struct usb_device_state_event *event)
|
||||||
|
{
|
||||||
|
bool new_usb_active = (event->state == USB_DEVICE_STATE_ACTIVE);
|
||||||
|
|
||||||
|
if (new_usb_active == usb_active) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
usb_active = new_usb_active;
|
||||||
|
|
||||||
|
if (!usb_active) {
|
||||||
|
k_work_cancel_delayable(&control_work);
|
||||||
|
disable_uart_io();
|
||||||
|
} else if (running) {
|
||||||
|
k_work_reschedule(&control_work, K_NO_WAIT);
|
||||||
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
static bool handle_usb_cdc_tx_event(const struct usb_cdc_tx_event *event)
|
||||||
|
{
|
||||||
|
uint32_t written;
|
||||||
|
unsigned int key;
|
||||||
|
|
||||||
|
if (!running || !usb_active || !dtr_ready) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
key = irq_lock();
|
||||||
|
written = ring_buf_put(&tx_ringbuf, event->dyndata.data,
|
||||||
|
(uint32_t)event->dyndata.size);
|
||||||
|
irq_unlock(key);
|
||||||
|
|
||||||
|
if (written < event->dyndata.size) {
|
||||||
|
LOG_WRN("Drop %zu CDC TX bytes", event->dyndata.size - written);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (written > 0U) {
|
||||||
|
kick_tx();
|
||||||
|
}
|
||||||
|
|
||||||
|
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_usb_prepare_event(aeh)) {
|
||||||
|
return handle_usb_prepare_event(cast_usb_prepare_event(aeh));
|
||||||
|
}
|
||||||
|
|
||||||
|
if (is_usb_cdc_tx_event(aeh)) {
|
||||||
|
return handle_usb_cdc_tx_event(cast_usb_cdc_tx_event(aeh));
|
||||||
|
}
|
||||||
|
|
||||||
|
if (is_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_prepare_event);
|
||||||
|
APP_EVENT_SUBSCRIBE(MODULE, usb_device_state_event);
|
||||||
|
APP_EVENT_SUBSCRIBE(MODULE, usb_cdc_tx_event);
|
||||||
|
APP_EVENT_SUBSCRIBE_EARLY(MODULE, power_down_event);
|
||||||
|
APP_EVENT_SUBSCRIBE(MODULE, wake_up_event);
|
||||||
164
src/usb_cdc_test_module.c
Normal file
164
src/usb_cdc_test_module.c
Normal file
@@ -0,0 +1,164 @@
|
|||||||
|
#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);
|
||||||
349
src/usb_device_module.c
Normal file
349
src/usb_device_module.c
Normal file
@@ -0,0 +1,349 @@
|
|||||||
|
#include <errno.h>
|
||||||
|
#include <stdbool.h>
|
||||||
|
#include <stdint.h>
|
||||||
|
|
||||||
|
#include <app_event_manager.h>
|
||||||
|
|
||||||
|
#define MODULE usb_device_module
|
||||||
|
#include <caf/events/module_state_event.h>
|
||||||
|
#include <caf/events/power_event.h>
|
||||||
|
|
||||||
|
#include <zephyr/logging/log.h>
|
||||||
|
#include <zephyr/usb/usbd.h>
|
||||||
|
|
||||||
|
#include <caf/events/power_manager_event.h>
|
||||||
|
|
||||||
|
#include "usb_device_module.h"
|
||||||
|
#include "usb_device_state_event.h"
|
||||||
|
#include "usb_function_ready_event.h"
|
||||||
|
#include "usb_prepare_event.h"
|
||||||
|
|
||||||
|
LOG_MODULE_REGISTER(MODULE, LOG_LEVEL_INF);
|
||||||
|
|
||||||
|
#define USB_DEVICE_VID 0x1915
|
||||||
|
#define USB_DEVICE_PID 0x52F0
|
||||||
|
#define USB_DEVICE_MANUFACTURER "Atguigu"
|
||||||
|
#define USB_DEVICE_PRODUCT "WH Mini Keyboard"
|
||||||
|
#define USB_REQUIRED_FUNCTION_MASK (USB_FUNCTION_HID | USB_FUNCTION_CDC_ACM)
|
||||||
|
|
||||||
|
USBD_DEVICE_DEFINE(blinky_usbd, DEVICE_DT_GET(DT_NODELABEL(usbd)),
|
||||||
|
USB_DEVICE_VID, USB_DEVICE_PID);
|
||||||
|
|
||||||
|
USBD_DESC_LANG_DEFINE(blinky_lang);
|
||||||
|
USBD_DESC_MANUFACTURER_DEFINE(blinky_mfr, USB_DEVICE_MANUFACTURER);
|
||||||
|
USBD_DESC_PRODUCT_DEFINE(blinky_product, USB_DEVICE_PRODUCT);
|
||||||
|
|
||||||
|
USBD_DESC_CONFIG_DEFINE(blinky_fs_cfg_desc, "FS Configuration");
|
||||||
|
USBD_CONFIGURATION_DEFINE(blinky_fs_config, 0, 250, &blinky_fs_cfg_desc);
|
||||||
|
|
||||||
|
static const char *const class_blocklist[] = {
|
||||||
|
NULL,
|
||||||
|
};
|
||||||
|
|
||||||
|
static bool initialized;
|
||||||
|
static bool running;
|
||||||
|
static bool prepare_broadcasted;
|
||||||
|
static bool usbd_initialized;
|
||||||
|
static bool usb_enabled;
|
||||||
|
static uint8_t ready_function_mask;
|
||||||
|
static enum usb_device_state device_state = USB_DEVICE_STATE_DISCONNECTED;
|
||||||
|
|
||||||
|
static void submit_usb_device_state_event(enum usb_device_state state)
|
||||||
|
{
|
||||||
|
struct usb_device_state_event *event = new_usb_device_state_event();
|
||||||
|
|
||||||
|
event->state = state;
|
||||||
|
APP_EVENT_SUBMIT(event);
|
||||||
|
}
|
||||||
|
|
||||||
|
static void submit_usb_prepare_event(void)
|
||||||
|
{
|
||||||
|
struct usb_prepare_event *event = new_usb_prepare_event();
|
||||||
|
|
||||||
|
APP_EVENT_SUBMIT(event);
|
||||||
|
}
|
||||||
|
|
||||||
|
static void update_usb_device_state(enum usb_device_state state)
|
||||||
|
{
|
||||||
|
if (device_state == state) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
device_state = state;
|
||||||
|
submit_usb_device_state_event(state);
|
||||||
|
}
|
||||||
|
|
||||||
|
static void update_power_manager_restriction(bool vbus_present)
|
||||||
|
{
|
||||||
|
power_manager_restrict(MODULE_IDX(MODULE),
|
||||||
|
vbus_present ? POWER_MANAGER_LEVEL_ALIVE :
|
||||||
|
POWER_MANAGER_LEVEL_SUSPENDED);
|
||||||
|
}
|
||||||
|
|
||||||
|
static int usb_descriptors_init(void)
|
||||||
|
{
|
||||||
|
int err;
|
||||||
|
|
||||||
|
err = usbd_add_descriptor(&blinky_usbd, &blinky_lang);
|
||||||
|
if (err) {
|
||||||
|
return err;
|
||||||
|
}
|
||||||
|
|
||||||
|
err = usbd_add_descriptor(&blinky_usbd, &blinky_mfr);
|
||||||
|
if (err) {
|
||||||
|
return err;
|
||||||
|
}
|
||||||
|
|
||||||
|
err = usbd_add_descriptor(&blinky_usbd, &blinky_product);
|
||||||
|
if (err) {
|
||||||
|
return err;
|
||||||
|
}
|
||||||
|
|
||||||
|
err = usbd_add_configuration(&blinky_usbd, USBD_SPEED_FS, &blinky_fs_config);
|
||||||
|
if (err) {
|
||||||
|
return err;
|
||||||
|
}
|
||||||
|
|
||||||
|
err = usbd_register_all_classes(&blinky_usbd, USBD_SPEED_FS, 1, class_blocklist);
|
||||||
|
if (err) {
|
||||||
|
return err;
|
||||||
|
}
|
||||||
|
|
||||||
|
usbd_device_set_code_triple(&blinky_usbd, USBD_SPEED_FS, 0, 0, 0);
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
static void usbd_msg_cb(struct usbd_context *const usbd_ctx,
|
||||||
|
const struct usbd_msg *const msg)
|
||||||
|
{
|
||||||
|
ARG_UNUSED(usbd_ctx);
|
||||||
|
|
||||||
|
switch (msg->type) {
|
||||||
|
case USBD_MSG_VBUS_READY:
|
||||||
|
update_power_manager_restriction(true);
|
||||||
|
if (!usb_enabled) {
|
||||||
|
int err = usbd_enable(&blinky_usbd);
|
||||||
|
|
||||||
|
if (err) {
|
||||||
|
LOG_ERR("usbd_enable failed (%d)", err);
|
||||||
|
} else {
|
||||||
|
usb_enabled = true;
|
||||||
|
update_usb_device_state(USB_DEVICE_STATE_POWERED);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
|
||||||
|
case USBD_MSG_VBUS_REMOVED:
|
||||||
|
update_power_manager_restriction(false);
|
||||||
|
if (usb_enabled) {
|
||||||
|
int err = usbd_disable(&blinky_usbd);
|
||||||
|
|
||||||
|
if (err) {
|
||||||
|
LOG_ERR("usbd_disable failed (%d)", err);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
usb_enabled = false;
|
||||||
|
update_usb_device_state(USB_DEVICE_STATE_DISCONNECTED);
|
||||||
|
break;
|
||||||
|
|
||||||
|
case USBD_MSG_CONFIGURATION:
|
||||||
|
if (msg->status) {
|
||||||
|
update_usb_device_state(USB_DEVICE_STATE_ACTIVE);
|
||||||
|
} else if (usb_enabled) {
|
||||||
|
update_usb_device_state(USB_DEVICE_STATE_POWERED);
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
|
||||||
|
case USBD_MSG_SUSPEND:
|
||||||
|
if (usb_enabled) {
|
||||||
|
update_usb_device_state(USB_DEVICE_STATE_SUSPENDED);
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
|
||||||
|
case USBD_MSG_RESUME:
|
||||||
|
if (usb_enabled) {
|
||||||
|
update_usb_device_state(USB_DEVICE_STATE_ACTIVE);
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
|
||||||
|
default:
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static int usb_stack_init(void)
|
||||||
|
{
|
||||||
|
int err;
|
||||||
|
|
||||||
|
err = usbd_msg_register_cb(&blinky_usbd, usbd_msg_cb);
|
||||||
|
if (err) {
|
||||||
|
LOG_ERR("usbd_msg_register_cb failed (%d)", err);
|
||||||
|
return err;
|
||||||
|
}
|
||||||
|
|
||||||
|
err = usb_descriptors_init();
|
||||||
|
if (err) {
|
||||||
|
LOG_ERR("usb descriptor init failed (%d)", err);
|
||||||
|
return err;
|
||||||
|
}
|
||||||
|
|
||||||
|
err = usbd_init(&blinky_usbd);
|
||||||
|
if (err) {
|
||||||
|
LOG_ERR("usbd_init failed (%d)", err);
|
||||||
|
return err;
|
||||||
|
}
|
||||||
|
|
||||||
|
usbd_initialized = true;
|
||||||
|
|
||||||
|
if (!usbd_can_detect_vbus(&blinky_usbd)) {
|
||||||
|
err = usbd_enable(&blinky_usbd);
|
||||||
|
if (err) {
|
||||||
|
LOG_ERR("usbd_enable failed (%d)", err);
|
||||||
|
return err;
|
||||||
|
}
|
||||||
|
|
||||||
|
usb_enabled = true;
|
||||||
|
update_usb_device_state(USB_DEVICE_STATE_POWERED);
|
||||||
|
}
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
static int module_init(void)
|
||||||
|
{
|
||||||
|
device_state = USB_DEVICE_STATE_DISCONNECTED;
|
||||||
|
usb_enabled = false;
|
||||||
|
ready_function_mask = 0U;
|
||||||
|
prepare_broadcasted = false;
|
||||||
|
usbd_initialized = false;
|
||||||
|
update_power_manager_restriction(false);
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
static int module_start(void)
|
||||||
|
{
|
||||||
|
if (running) {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
running = true;
|
||||||
|
|
||||||
|
if (usbd_initialized || prepare_broadcasted) {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
ready_function_mask = 0U;
|
||||||
|
prepare_broadcasted = true;
|
||||||
|
submit_usb_prepare_event();
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
static void module_pause(void)
|
||||||
|
{
|
||||||
|
if (!running) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
running = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
static bool handle_module_state_event(const struct module_state_event *event)
|
||||||
|
{
|
||||||
|
if (!check_state(event, MODULE_ID(main), MODULE_STATE_READY)) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!initialized) {
|
||||||
|
int err = module_init();
|
||||||
|
|
||||||
|
if (err) {
|
||||||
|
module_set_state(MODULE_STATE_ERROR);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
initialized = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!running) {
|
||||||
|
int err = module_start();
|
||||||
|
|
||||||
|
if (err) {
|
||||||
|
module_set_state(MODULE_STATE_ERROR);
|
||||||
|
} else if (usbd_initialized) {
|
||||||
|
module_set_state(MODULE_STATE_READY);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
static bool handle_usb_function_ready_event(const struct usb_function_ready_event *event)
|
||||||
|
{
|
||||||
|
int err;
|
||||||
|
|
||||||
|
if (!running || !prepare_broadcasted || usbd_initialized) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
ready_function_mask |= event->function_mask;
|
||||||
|
|
||||||
|
if ((ready_function_mask & USB_REQUIRED_FUNCTION_MASK) !=
|
||||||
|
USB_REQUIRED_FUNCTION_MASK) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
err = usb_stack_init();
|
||||||
|
if (err) {
|
||||||
|
module_set_state(MODULE_STATE_ERROR);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
module_set_state(MODULE_STATE_READY);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
static bool app_event_handler(const struct app_event_header *aeh)
|
||||||
|
{
|
||||||
|
if (is_module_state_event(aeh)) {
|
||||||
|
return handle_module_state_event(cast_module_state_event(aeh));
|
||||||
|
}
|
||||||
|
|
||||||
|
if (is_usb_function_ready_event(aeh)) {
|
||||||
|
return handle_usb_function_ready_event(cast_usb_function_ready_event(aeh));
|
||||||
|
}
|
||||||
|
|
||||||
|
if (is_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 (usbd_initialized) {
|
||||||
|
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_function_ready_event);
|
||||||
|
APP_EVENT_SUBSCRIBE_EARLY(MODULE, power_down_event);
|
||||||
|
APP_EVENT_SUBSCRIBE(MODULE, wake_up_event);
|
||||||
@@ -12,24 +12,22 @@
|
|||||||
#include <zephyr/device.h>
|
#include <zephyr/device.h>
|
||||||
#include <zephyr/drivers/usb/udc_buf.h>
|
#include <zephyr/drivers/usb/udc_buf.h>
|
||||||
#include <zephyr/logging/log.h>
|
#include <zephyr/logging/log.h>
|
||||||
#include <zephyr/sys/byteorder.h>
|
|
||||||
#include <zephyr/sys/util.h>
|
#include <zephyr/sys/util.h>
|
||||||
#include <zephyr/usb/class/usbd_hid.h>
|
#include <zephyr/usb/class/usbd_hid.h>
|
||||||
#include <zephyr/usb/usbd.h>
|
|
||||||
|
|
||||||
#include "hid_led_event.h"
|
#include "hid_led_event.h"
|
||||||
|
#include "hid_report_sent_event.h"
|
||||||
|
#include "hid_transport_state_event.h"
|
||||||
|
#include "hid_tx_report_event.h"
|
||||||
#include "keyboard_core.h"
|
#include "keyboard_core.h"
|
||||||
#include "keyboard_hid_report_event.h"
|
|
||||||
#include "set_protocol_event.h"
|
#include "set_protocol_event.h"
|
||||||
|
#include "usb_function_ready_event.h"
|
||||||
|
#include "usb_prepare_event.h"
|
||||||
|
#include "usb_device_module.h"
|
||||||
|
#include "usb_device_state_event.h"
|
||||||
|
|
||||||
LOG_MODULE_REGISTER(MODULE, LOG_LEVEL_INF);
|
LOG_MODULE_REGISTER(MODULE, LOG_LEVEL_INF);
|
||||||
|
|
||||||
#define USB_HID_VID 0x1915
|
|
||||||
#define USB_HID_PID 0x52F0
|
|
||||||
#define USB_HID_MANUFACTURER "blinky"
|
|
||||||
#define USB_HID_PRODUCT "Mini Keyboard"
|
|
||||||
#define USB_HID_POLLING_US 1000U
|
|
||||||
|
|
||||||
#define KBD_LED_REPORT_SIZE 1U
|
#define KBD_LED_REPORT_SIZE 1U
|
||||||
|
|
||||||
enum usb_hid_interface {
|
enum usb_hid_interface {
|
||||||
@@ -104,27 +102,16 @@ static enum keyboard_protocol_mode keyboard_protocol_mode =
|
|||||||
KEYBOARD_PROTOCOL_MODE_REPORT;
|
KEYBOARD_PROTOCOL_MODE_REPORT;
|
||||||
static bool initialized;
|
static bool initialized;
|
||||||
static bool running;
|
static bool running;
|
||||||
static bool usb_enabled;
|
static bool usb_active;
|
||||||
|
static bool usb_function_prepared;
|
||||||
static bool keyboard_report_in_flight;
|
static bool keyboard_report_in_flight;
|
||||||
static bool consumer_report_in_flight;
|
static bool consumer_report_in_flight;
|
||||||
|
static uint16_t keyboard_in_flight_sequence;
|
||||||
|
static uint16_t consumer_in_flight_sequence;
|
||||||
|
|
||||||
UDC_STATIC_BUF_DEFINE(keyboard_tx_buf, KEYBOARD_NKRO_REPORT_SIZE);
|
UDC_STATIC_BUF_DEFINE(keyboard_tx_buf, KEYBOARD_NKRO_REPORT_SIZE);
|
||||||
UDC_STATIC_BUF_DEFINE(consumer_tx_buf, KEYBOARD_CONSUMER_REPORT_SIZE);
|
UDC_STATIC_BUF_DEFINE(consumer_tx_buf, KEYBOARD_CONSUMER_REPORT_SIZE);
|
||||||
|
|
||||||
USBD_DEVICE_DEFINE(blinky_usbd, DEVICE_DT_GET(DT_NODELABEL(usbd)),
|
|
||||||
USB_HID_VID, USB_HID_PID);
|
|
||||||
|
|
||||||
USBD_DESC_LANG_DEFINE(blinky_lang);
|
|
||||||
USBD_DESC_MANUFACTURER_DEFINE(blinky_mfr, USB_HID_MANUFACTURER);
|
|
||||||
USBD_DESC_PRODUCT_DEFINE(blinky_product, USB_HID_PRODUCT);
|
|
||||||
|
|
||||||
USBD_DESC_CONFIG_DEFINE(blinky_fs_cfg_desc, "FS Configuration");
|
|
||||||
USBD_CONFIGURATION_DEFINE(blinky_fs_config, 0, 250, &blinky_fs_cfg_desc);
|
|
||||||
|
|
||||||
static const char *const class_blocklist[] = {
|
|
||||||
NULL,
|
|
||||||
};
|
|
||||||
|
|
||||||
static struct usb_hid_interface_state *iface_from_dev(const struct device *dev)
|
static struct usb_hid_interface_state *iface_from_dev(const struct device *dev)
|
||||||
{
|
{
|
||||||
for (size_t i = 0; i < ARRAY_SIZE(hid_ifaces); i++) {
|
for (size_t i = 0; i < ARRAY_SIZE(hid_ifaces); i++) {
|
||||||
@@ -142,10 +129,22 @@ static enum keyboard_protocol_mode usb_proto_to_keyboard_proto(uint8_t proto)
|
|||||||
: KEYBOARD_PROTOCOL_MODE_REPORT;
|
: KEYBOARD_PROTOCOL_MODE_REPORT;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static void reset_usb_runtime_state(void)
|
||||||
|
{
|
||||||
|
for (size_t i = 0; i < ARRAY_SIZE(hid_ifaces); i++) {
|
||||||
|
hid_ifaces[i].ready = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
usb_active = false;
|
||||||
|
keyboard_report_in_flight = false;
|
||||||
|
consumer_report_in_flight = false;
|
||||||
|
}
|
||||||
|
|
||||||
static void submit_set_protocol_event(enum keyboard_protocol_mode protocol_mode)
|
static void submit_set_protocol_event(enum keyboard_protocol_mode protocol_mode)
|
||||||
{
|
{
|
||||||
struct set_protocol_event *event = new_set_protocol_event();
|
struct set_protocol_event *event = new_set_protocol_event();
|
||||||
|
|
||||||
|
event->transport = HID_TRANSPORT_USB;
|
||||||
event->protocol_mode = protocol_mode;
|
event->protocol_mode = protocol_mode;
|
||||||
APP_EVENT_SUBMIT(event);
|
APP_EVENT_SUBMIT(event);
|
||||||
}
|
}
|
||||||
@@ -154,10 +153,48 @@ static void submit_hid_led_event(uint8_t led_bm)
|
|||||||
{
|
{
|
||||||
struct hid_led_event *event = new_hid_led_event();
|
struct hid_led_event *event = new_hid_led_event();
|
||||||
|
|
||||||
|
event->transport = HID_TRANSPORT_USB;
|
||||||
event->led_bm = led_bm;
|
event->led_bm = led_bm;
|
||||||
APP_EVENT_SUBMIT(event);
|
APP_EVENT_SUBMIT(event);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static void submit_transport_state_event(void)
|
||||||
|
{
|
||||||
|
struct hid_transport_state_event *event = new_hid_transport_state_event();
|
||||||
|
bool ready = running && usb_active;
|
||||||
|
|
||||||
|
event->transport = HID_TRANSPORT_USB;
|
||||||
|
event->ready = ready;
|
||||||
|
event->keys_ready = ready &&
|
||||||
|
hid_ifaces[USB_HID_INTERFACE_KEYBOARD].ready;
|
||||||
|
event->consumer_ready = ready &&
|
||||||
|
hid_ifaces[USB_HID_INTERFACE_CONSUMER].ready;
|
||||||
|
event->protocol_mode = keyboard_protocol_mode;
|
||||||
|
|
||||||
|
APP_EVENT_SUBMIT(event);
|
||||||
|
}
|
||||||
|
|
||||||
|
static void submit_hid_report_sent_event(enum keyboard_report_type report_type,
|
||||||
|
uint16_t sequence, bool error)
|
||||||
|
{
|
||||||
|
struct hid_report_sent_event *event = new_hid_report_sent_event();
|
||||||
|
|
||||||
|
event->transport = HID_TRANSPORT_USB;
|
||||||
|
event->report_type = report_type;
|
||||||
|
event->sequence = sequence;
|
||||||
|
event->error = error;
|
||||||
|
|
||||||
|
APP_EVENT_SUBMIT(event);
|
||||||
|
}
|
||||||
|
|
||||||
|
static void submit_usb_function_ready_event(void)
|
||||||
|
{
|
||||||
|
struct usb_function_ready_event *event = new_usb_function_ready_event();
|
||||||
|
|
||||||
|
event->function_mask = USB_FUNCTION_HID;
|
||||||
|
APP_EVENT_SUBMIT(event);
|
||||||
|
}
|
||||||
|
|
||||||
static void keyboard_iface_ready(const struct device *dev, const bool ready)
|
static void keyboard_iface_ready(const struct device *dev, const bool ready)
|
||||||
{
|
{
|
||||||
struct usb_hid_interface_state *iface = iface_from_dev(dev);
|
struct usb_hid_interface_state *iface = iface_from_dev(dev);
|
||||||
@@ -170,8 +207,9 @@ static void keyboard_iface_ready(const struct device *dev, const bool ready)
|
|||||||
if (!ready) {
|
if (!ready) {
|
||||||
keyboard_report_in_flight = false;
|
keyboard_report_in_flight = false;
|
||||||
}
|
}
|
||||||
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();
|
||||||
}
|
}
|
||||||
|
|
||||||
static int keyboard_get_report(const struct device *dev,
|
static int keyboard_get_report(const struct device *dev,
|
||||||
@@ -230,6 +268,7 @@ static void keyboard_set_protocol(const struct device *dev, const uint8_t proto)
|
|||||||
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(new_mode);
|
||||||
|
submit_transport_state_event();
|
||||||
}
|
}
|
||||||
|
|
||||||
static void keyboard_input_report_done(const struct device *dev,
|
static void keyboard_input_report_done(const struct device *dev,
|
||||||
@@ -239,6 +278,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,
|
||||||
|
keyboard_in_flight_sequence, false);
|
||||||
}
|
}
|
||||||
|
|
||||||
static void keyboard_output_report(const struct device *dev,
|
static void keyboard_output_report(const struct device *dev,
|
||||||
@@ -278,8 +319,9 @@ static void consumer_iface_ready(const struct device *dev, const bool ready)
|
|||||||
if (!ready) {
|
if (!ready) {
|
||||||
consumer_report_in_flight = false;
|
consumer_report_in_flight = false;
|
||||||
}
|
}
|
||||||
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();
|
||||||
}
|
}
|
||||||
|
|
||||||
static int consumer_get_report(const struct device *dev,
|
static int consumer_get_report(const struct device *dev,
|
||||||
@@ -337,6 +379,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,
|
||||||
|
consumer_in_flight_sequence, false);
|
||||||
}
|
}
|
||||||
|
|
||||||
static void consumer_output_report(const struct device *dev,
|
static void consumer_output_report(const struct device *dev,
|
||||||
@@ -359,79 +403,6 @@ static const struct hid_device_ops consumer_ops = {
|
|||||||
.output_report = consumer_output_report,
|
.output_report = consumer_output_report,
|
||||||
};
|
};
|
||||||
|
|
||||||
static void usbd_msg_cb(struct usbd_context *const usbd_ctx,
|
|
||||||
const struct usbd_msg *const msg)
|
|
||||||
{
|
|
||||||
ARG_UNUSED(usbd_ctx);
|
|
||||||
|
|
||||||
if (msg->type == USBD_MSG_VBUS_READY) {
|
|
||||||
if (!usb_enabled) {
|
|
||||||
int err = usbd_enable(&blinky_usbd);
|
|
||||||
|
|
||||||
if (err) {
|
|
||||||
LOG_ERR("usbd_enable failed (%d)", err);
|
|
||||||
} else {
|
|
||||||
usb_enabled = true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (msg->type == USBD_MSG_VBUS_REMOVED) {
|
|
||||||
if (usb_enabled) {
|
|
||||||
int err = usbd_disable(&blinky_usbd);
|
|
||||||
|
|
||||||
if (err) {
|
|
||||||
LOG_ERR("usbd_disable failed (%d)", err);
|
|
||||||
} else {
|
|
||||||
usb_enabled = false;
|
|
||||||
for (size_t i = 0; i < ARRAY_SIZE(hid_ifaces); i++) {
|
|
||||||
hid_ifaces[i].ready = false;
|
|
||||||
}
|
|
||||||
keyboard_report_in_flight = false;
|
|
||||||
consumer_report_in_flight = false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
static int usb_descriptors_init(void)
|
|
||||||
{
|
|
||||||
int err;
|
|
||||||
|
|
||||||
err = usbd_add_descriptor(&blinky_usbd, &blinky_lang);
|
|
||||||
if (err) {
|
|
||||||
return err;
|
|
||||||
}
|
|
||||||
|
|
||||||
err = usbd_add_descriptor(&blinky_usbd, &blinky_mfr);
|
|
||||||
if (err) {
|
|
||||||
return err;
|
|
||||||
}
|
|
||||||
|
|
||||||
err = usbd_add_descriptor(&blinky_usbd, &blinky_product);
|
|
||||||
if (err) {
|
|
||||||
return err;
|
|
||||||
}
|
|
||||||
|
|
||||||
err = usbd_add_configuration(&blinky_usbd, USBD_SPEED_FS, &blinky_fs_config);
|
|
||||||
if (err) {
|
|
||||||
return err;
|
|
||||||
}
|
|
||||||
|
|
||||||
err = usbd_register_all_classes(&blinky_usbd, USBD_SPEED_FS, 1, class_blocklist);
|
|
||||||
if (err) {
|
|
||||||
return err;
|
|
||||||
}
|
|
||||||
|
|
||||||
usbd_device_set_code_triple(&blinky_usbd, USBD_SPEED_FS, 0, 0, 0);
|
|
||||||
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
static int usb_hid_register_devices(void)
|
static int usb_hid_register_devices(void)
|
||||||
{
|
{
|
||||||
int err;
|
int err;
|
||||||
@@ -464,41 +435,9 @@ static int usb_hid_register_devices(void)
|
|||||||
|
|
||||||
static int module_init(void)
|
static int module_init(void)
|
||||||
{
|
{
|
||||||
int err;
|
reset_usb_runtime_state();
|
||||||
|
keyboard_protocol_mode = KEYBOARD_PROTOCOL_MODE_REPORT;
|
||||||
err = usb_hid_register_devices();
|
usb_function_prepared = false;
|
||||||
if (err) {
|
|
||||||
LOG_ERR("hid_device_register failed (%d)", err);
|
|
||||||
return err;
|
|
||||||
}
|
|
||||||
|
|
||||||
err = usbd_msg_register_cb(&blinky_usbd, usbd_msg_cb);
|
|
||||||
if (err) {
|
|
||||||
LOG_ERR("usbd_msg_register_cb failed (%d)", err);
|
|
||||||
return err;
|
|
||||||
}
|
|
||||||
|
|
||||||
err = usb_descriptors_init();
|
|
||||||
if (err) {
|
|
||||||
LOG_ERR("usb descriptor init failed (%d)", err);
|
|
||||||
return err;
|
|
||||||
}
|
|
||||||
|
|
||||||
err = usbd_init(&blinky_usbd);
|
|
||||||
if (err) {
|
|
||||||
LOG_ERR("usbd_init failed (%d)", err);
|
|
||||||
return err;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!usbd_can_detect_vbus(&blinky_usbd)) {
|
|
||||||
err = usbd_enable(&blinky_usbd);
|
|
||||||
if (err) {
|
|
||||||
LOG_ERR("usbd_enable failed (%d)", err);
|
|
||||||
return err;
|
|
||||||
}
|
|
||||||
|
|
||||||
usb_enabled = true;
|
|
||||||
}
|
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
@@ -510,19 +449,61 @@ static int module_start(void)
|
|||||||
}
|
}
|
||||||
|
|
||||||
running = true;
|
running = true;
|
||||||
|
submit_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();
|
||||||
}
|
}
|
||||||
|
|
||||||
static bool handle_keyboard_hid_report_event(const struct keyboard_hid_report_event *event)
|
static bool handle_usb_prepare_event(const struct usb_prepare_event *event)
|
||||||
{
|
{
|
||||||
int err;
|
int err;
|
||||||
|
|
||||||
if (!running || (event->mode != MODE_SWITCH_USB)) {
|
ARG_UNUSED(event);
|
||||||
|
|
||||||
|
if (!running || usb_function_prepared) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
err = usb_hid_register_devices();
|
||||||
|
if (err) {
|
||||||
|
LOG_ERR("hid_device_register failed (%d)", err);
|
||||||
|
module_set_state(MODULE_STATE_ERROR);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
usb_function_prepared = true;
|
||||||
|
submit_usb_function_ready_event();
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
static bool handle_usb_device_state_event(const struct usb_device_state_event *event)
|
||||||
|
{
|
||||||
|
bool new_usb_active = (event->state == USB_DEVICE_STATE_ACTIVE);
|
||||||
|
|
||||||
|
if (new_usb_active == usb_active) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!new_usb_active) {
|
||||||
|
reset_usb_runtime_state();
|
||||||
|
} else {
|
||||||
|
usb_active = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
submit_transport_state_event();
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
static bool handle_hid_tx_report_event(const struct hid_tx_report_event *event)
|
||||||
|
{
|
||||||
|
int err;
|
||||||
|
|
||||||
|
if (!running || !usb_active || (event->transport != HID_TRANSPORT_USB)) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -550,8 +531,11 @@ static bool handle_keyboard_hid_report_event(const struct keyboard_hid_report_ev
|
|||||||
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,
|
||||||
|
event->sequence, true);
|
||||||
} else {
|
} else {
|
||||||
keyboard_report_in_flight = true;
|
keyboard_report_in_flight = true;
|
||||||
|
keyboard_in_flight_sequence = event->sequence;
|
||||||
}
|
}
|
||||||
|
|
||||||
return false;
|
return false;
|
||||||
@@ -576,8 +560,11 @@ static bool handle_keyboard_hid_report_event(const struct keyboard_hid_report_ev
|
|||||||
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,
|
||||||
|
event->sequence, true);
|
||||||
} else {
|
} else {
|
||||||
consumer_report_in_flight = true;
|
consumer_report_in_flight = true;
|
||||||
|
consumer_in_flight_sequence = event->sequence;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -586,9 +573,16 @@ static bool handle_keyboard_hid_report_event(const struct keyboard_hid_report_ev
|
|||||||
|
|
||||||
static bool app_event_handler(const struct app_event_header *aeh)
|
static bool app_event_handler(const struct app_event_header *aeh)
|
||||||
{
|
{
|
||||||
if (is_keyboard_hid_report_event(aeh)) {
|
if (is_hid_tx_report_event(aeh)) {
|
||||||
return handle_keyboard_hid_report_event(
|
return handle_hid_tx_report_event(cast_hid_tx_report_event(aeh));
|
||||||
cast_keyboard_hid_report_event(aeh));
|
}
|
||||||
|
|
||||||
|
if (is_usb_device_state_event(aeh)) {
|
||||||
|
return handle_usb_device_state_event(cast_usb_device_state_event(aeh));
|
||||||
|
}
|
||||||
|
|
||||||
|
if (is_usb_prepare_event(aeh)) {
|
||||||
|
return handle_usb_prepare_event(cast_usb_prepare_event(aeh));
|
||||||
}
|
}
|
||||||
|
|
||||||
if (is_module_state_event(aeh)) {
|
if (is_module_state_event(aeh)) {
|
||||||
@@ -641,12 +635,13 @@ static bool app_event_handler(const struct app_event_header *aeh)
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
__ASSERT_NO_MSG(false);
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
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, keyboard_hid_report_event);
|
APP_EVENT_SUBSCRIBE(MODULE, hid_tx_report_event);
|
||||||
|
APP_EVENT_SUBSCRIBE(MODULE, usb_prepare_event);
|
||||||
|
APP_EVENT_SUBSCRIBE(MODULE, usb_device_state_event);
|
||||||
APP_EVENT_SUBSCRIBE_EARLY(MODULE, power_down_event);
|
APP_EVENT_SUBSCRIBE_EARLY(MODULE, power_down_event);
|
||||||
APP_EVENT_SUBSCRIBE(MODULE, wake_up_event);
|
APP_EVENT_SUBSCRIBE(MODULE, wake_up_event);
|
||||||
|
|||||||
2
sysbuild.conf
Normal file
2
sysbuild.conf
Normal file
@@ -0,0 +1,2 @@
|
|||||||
|
SB_CONFIG_BOOTLOADER_MCUBOOT=y
|
||||||
|
SB_CONFIG_MCUBOOT_MODE_SINGLE_APP=y
|
||||||
Reference in New Issue
Block a user