Compare commits
5 Commits
c342a8d3f0
...
0a905d280d
| Author | SHA1 | Date | |
|---|---|---|---|
| 0a905d280d | |||
| 79af0eb025 | |||
| bc42a4dd63 | |||
| c4b205b8a1 | |||
| 78a6dc212d |
@@ -25,7 +25,7 @@ target_sources(app PRIVATE
|
||||
src/ble_adv_uuid16.c
|
||||
src/ble_bas_module.c
|
||||
src/ble_hid_module.c
|
||||
src/ble_serial_module.c
|
||||
src/ble_nus_module.c
|
||||
src/display_module.c
|
||||
src/encoder_module.c
|
||||
src/hid_flowctrl_module.c
|
||||
@@ -35,33 +35,27 @@ target_sources(app PRIVATE
|
||||
src/led_strip_module.c
|
||||
src/time_sync_module.c
|
||||
src/ui/ui_main.c
|
||||
src/cdc_wrapper_module.c
|
||||
src/protocol_module.c
|
||||
src/usb_cdc_module.c
|
||||
src/usb_device_module.c
|
||||
src/usb_hid_module.c
|
||||
src/events/bat_state_event.c
|
||||
src/events/ble_serial_rx_event.c
|
||||
src/events/ble_serial_tx_event.c
|
||||
src/events/cdc_proto_tx_event.c
|
||||
src/events/datetime_event.c
|
||||
src/events/encoder_event.c
|
||||
src/events/function_bitmap_state_event.c
|
||||
src/events/function_bitmap_update_event.c
|
||||
src/events/hid_led_event.c
|
||||
src/events/hid_report_sent_event.c
|
||||
src/events/hid_transport_state_event.c
|
||||
src/events/hid_tx_report_event.c
|
||||
src/events/led_strip_en_event.c
|
||||
src/events/key_function_event.c
|
||||
src/mode_switch_module.c
|
||||
src/events/keyboard_hid_report_event.c
|
||||
src/events/mode_switch_event.c
|
||||
src/events/proto_rx_event.c
|
||||
src/events/proto_tx_event.c
|
||||
src/events/set_protocol_event.c
|
||||
src/events/theme_rgb_update_event.c
|
||||
src/events/time_sync_event.c
|
||||
src/events/usb_cdc_rx_event.c
|
||||
src/events/usb_cdc_tx_event.c
|
||||
src/events/usb_device_state_event.c
|
||||
src/events/usb_function_ready_event.c
|
||||
src/events/usb_prepare_event.c
|
||||
src/events/usb_state_event.c
|
||||
)
|
||||
|
||||
@@ -1,12 +0,0 @@
|
||||
#ifndef BLINKY_CDC_WRAPPER_MODULE_H_
|
||||
#define BLINKY_CDC_WRAPPER_MODULE_H_
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* BLINKY_CDC_WRAPPER_MODULE_H_ */
|
||||
@@ -20,6 +20,16 @@ struct bat_state_event {
|
||||
|
||||
APP_EVENT_TYPE_DECLARE(bat_state_event);
|
||||
|
||||
static inline void submit_bat_state_event(uint8_t soc, bool charging, bool full)
|
||||
{
|
||||
struct bat_state_event *event = new_bat_state_event();
|
||||
|
||||
event->soc = soc;
|
||||
event->charging = charging;
|
||||
event->full = full;
|
||||
APP_EVENT_SUBMIT(event);
|
||||
}
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
@@ -1,22 +0,0 @@
|
||||
#ifndef BLINKY_BLE_SERIAL_RX_EVENT_H_
|
||||
#define BLINKY_BLE_SERIAL_RX_EVENT_H_
|
||||
|
||||
#include <app_event_manager.h>
|
||||
#include <app_event_manager_profiler_tracer.h>
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
struct ble_serial_rx_event {
|
||||
struct app_event_header header;
|
||||
struct event_dyndata dyndata;
|
||||
};
|
||||
|
||||
APP_EVENT_TYPE_DYNDATA_DECLARE(ble_serial_rx_event);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* BLINKY_BLE_SERIAL_RX_EVENT_H_ */
|
||||
@@ -1,22 +0,0 @@
|
||||
#ifndef BLINKY_BLE_SERIAL_TX_EVENT_H_
|
||||
#define BLINKY_BLE_SERIAL_TX_EVENT_H_
|
||||
|
||||
#include <app_event_manager.h>
|
||||
#include <app_event_manager_profiler_tracer.h>
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
struct ble_serial_tx_event {
|
||||
struct app_event_header header;
|
||||
struct event_dyndata dyndata;
|
||||
};
|
||||
|
||||
APP_EVENT_TYPE_DYNDATA_DECLARE(ble_serial_tx_event);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* BLINKY_BLE_SERIAL_TX_EVENT_H_ */
|
||||
@@ -1,23 +0,0 @@
|
||||
#ifndef BLINKY_CDC_PROTO_TX_EVENT_H_
|
||||
#define BLINKY_CDC_PROTO_TX_EVENT_H_
|
||||
|
||||
#include <app_event_manager.h>
|
||||
#include <app_event_manager_profiler_tracer.h>
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
struct cdc_proto_tx_event {
|
||||
struct app_event_header header;
|
||||
uint8_t type;
|
||||
struct event_dyndata dyndata;
|
||||
};
|
||||
|
||||
APP_EVENT_TYPE_DYNDATA_DECLARE(cdc_proto_tx_event);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* BLINKY_CDC_PROTO_TX_EVENT_H_ */
|
||||
@@ -1,6 +1,9 @@
|
||||
#ifndef BLINKY_DATETIME_EVENT_H_
|
||||
#define BLINKY_DATETIME_EVENT_H_
|
||||
|
||||
#include <errno.h>
|
||||
#include <string.h>
|
||||
|
||||
#include <app_event_manager.h>
|
||||
#include <app_event_manager_profiler_tracer.h>
|
||||
|
||||
@@ -19,6 +22,22 @@ struct datetime_event {
|
||||
|
||||
APP_EVENT_TYPE_DECLARE(datetime_event);
|
||||
|
||||
static inline int submit_datetime_event(const char *date_text, const char *time_text)
|
||||
{
|
||||
struct datetime_event *event = new_datetime_event();
|
||||
|
||||
if ((date_text == NULL) || (time_text == NULL)) {
|
||||
return -EINVAL;
|
||||
}
|
||||
|
||||
strncpy(event->date_text, date_text, sizeof(event->date_text));
|
||||
event->date_text[sizeof(event->date_text) - 1] = '\0';
|
||||
strncpy(event->time_text, time_text, sizeof(event->time_text));
|
||||
event->time_text[sizeof(event->time_text) - 1] = '\0';
|
||||
APP_EVENT_SUBMIT(event);
|
||||
return 0;
|
||||
}
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
@@ -15,6 +15,14 @@ struct encoder_event {
|
||||
|
||||
APP_EVENT_TYPE_DECLARE(encoder_event);
|
||||
|
||||
static inline void submit_encoder_event(int8_t detents)
|
||||
{
|
||||
struct encoder_event *event = new_encoder_event();
|
||||
|
||||
event->detents = detents;
|
||||
APP_EVENT_SUBMIT(event);
|
||||
}
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
41
inc/events/function_bitmap_state_event.h
Normal file
41
inc/events/function_bitmap_state_event.h
Normal file
@@ -0,0 +1,41 @@
|
||||
#ifndef BLINKY_FUNCTION_BITMAP_STATE_EVENT_H_
|
||||
#define BLINKY_FUNCTION_BITMAP_STATE_EVENT_H_
|
||||
|
||||
#include <errno.h>
|
||||
#include <string.h>
|
||||
|
||||
#include <app_event_manager.h>
|
||||
#include <app_event_manager_profiler_tracer.h>
|
||||
|
||||
#include "keyboard_core.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
struct function_bitmap_state_event {
|
||||
struct app_event_header header;
|
||||
uint8_t bitmap[KEYBOARD_PROTOCOL_BITMAP_BYTES];
|
||||
};
|
||||
|
||||
APP_EVENT_TYPE_DECLARE(function_bitmap_state_event);
|
||||
|
||||
static inline int submit_function_bitmap_state_event(const uint8_t *bitmap)
|
||||
{
|
||||
struct function_bitmap_state_event *event;
|
||||
|
||||
if (bitmap == NULL) {
|
||||
return -EINVAL;
|
||||
}
|
||||
|
||||
event = new_function_bitmap_state_event();
|
||||
memcpy(event->bitmap, bitmap, sizeof(event->bitmap));
|
||||
APP_EVENT_SUBMIT(event);
|
||||
return 0;
|
||||
}
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* BLINKY_FUNCTION_BITMAP_STATE_EVENT_H_ */
|
||||
@@ -1,6 +1,9 @@
|
||||
#ifndef BLINKY_FUNCTION_BITMAP_UPDATE_EVENT_H_
|
||||
#define BLINKY_FUNCTION_BITMAP_UPDATE_EVENT_H_
|
||||
|
||||
#include <errno.h>
|
||||
#include <string.h>
|
||||
|
||||
#include <app_event_manager.h>
|
||||
#include <app_event_manager_profiler_tracer.h>
|
||||
|
||||
@@ -17,6 +20,20 @@ struct function_bitmap_update_event {
|
||||
|
||||
APP_EVENT_TYPE_DECLARE(function_bitmap_update_event);
|
||||
|
||||
static inline int submit_function_bitmap_update_event(const uint8_t *bitmap)
|
||||
{
|
||||
struct function_bitmap_update_event *event;
|
||||
|
||||
if (bitmap == NULL) {
|
||||
return -EINVAL;
|
||||
}
|
||||
|
||||
event = new_function_bitmap_update_event();
|
||||
memcpy(event->bitmap, bitmap, sizeof(event->bitmap));
|
||||
APP_EVENT_SUBMIT(event);
|
||||
return 0;
|
||||
}
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
@@ -18,6 +18,15 @@ struct hid_led_event {
|
||||
|
||||
APP_EVENT_TYPE_DECLARE(hid_led_event);
|
||||
|
||||
static inline void submit_hid_led_event(enum hid_transport transport, uint8_t led_bm)
|
||||
{
|
||||
struct hid_led_event *event = new_hid_led_event();
|
||||
|
||||
event->transport = transport;
|
||||
event->led_bm = led_bm;
|
||||
APP_EVENT_SUBMIT(event);
|
||||
}
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
@@ -20,6 +20,19 @@ struct hid_report_sent_event {
|
||||
|
||||
APP_EVENT_TYPE_DECLARE(hid_report_sent_event);
|
||||
|
||||
static inline void submit_hid_report_sent_event(enum hid_transport transport,
|
||||
enum keyboard_report_type report_type,
|
||||
uint16_t sequence, bool error)
|
||||
{
|
||||
struct hid_report_sent_event *event = new_hid_report_sent_event();
|
||||
|
||||
event->transport = transport;
|
||||
event->report_type = report_type;
|
||||
event->sequence = sequence;
|
||||
event->error = error;
|
||||
APP_EVENT_SUBMIT(event);
|
||||
}
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
@@ -21,6 +21,20 @@ struct hid_transport_state_event {
|
||||
|
||||
APP_EVENT_TYPE_DECLARE(hid_transport_state_event);
|
||||
|
||||
static inline void submit_hid_transport_state_event(
|
||||
enum hid_transport transport, bool ready, bool keys_ready,
|
||||
bool consumer_ready, enum keyboard_protocol_mode protocol_mode)
|
||||
{
|
||||
struct hid_transport_state_event *event = new_hid_transport_state_event();
|
||||
|
||||
event->transport = transport;
|
||||
event->ready = ready;
|
||||
event->keys_ready = keys_ready;
|
||||
event->consumer_ready = consumer_ready;
|
||||
event->protocol_mode = protocol_mode;
|
||||
APP_EVENT_SUBMIT(event);
|
||||
}
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
@@ -1,6 +1,10 @@
|
||||
#ifndef BLINKY_HID_TX_REPORT_EVENT_H_
|
||||
#define BLINKY_HID_TX_REPORT_EVENT_H_
|
||||
|
||||
#include <errno.h>
|
||||
#include <stddef.h>
|
||||
#include <string.h>
|
||||
|
||||
#include <app_event_manager.h>
|
||||
#include <app_event_manager_profiler_tracer.h>
|
||||
|
||||
@@ -21,6 +25,31 @@ struct hid_tx_report_event {
|
||||
|
||||
APP_EVENT_TYPE_DYNDATA_DECLARE(hid_tx_report_event);
|
||||
|
||||
static inline int submit_hid_tx_report_event(enum hid_transport transport,
|
||||
enum keyboard_report_type report_type,
|
||||
enum keyboard_protocol_mode protocol_mode,
|
||||
uint16_t sequence,
|
||||
const uint8_t *data, size_t size)
|
||||
{
|
||||
struct hid_tx_report_event *event;
|
||||
|
||||
if ((data == NULL) && (size > 0U)) {
|
||||
return -EINVAL;
|
||||
}
|
||||
|
||||
event = new_hid_tx_report_event(size);
|
||||
event->transport = transport;
|
||||
event->report_type = report_type;
|
||||
event->protocol_mode = protocol_mode;
|
||||
event->sequence = sequence;
|
||||
if (size > 0U) {
|
||||
memcpy(event->dyndata.data, data, size);
|
||||
}
|
||||
|
||||
APP_EVENT_SUBMIT(event);
|
||||
return 0;
|
||||
}
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
@@ -1,28 +0,0 @@
|
||||
#ifndef BLINKY_KEY_FUNCTION_EVENT_H_
|
||||
#define BLINKY_KEY_FUNCTION_EVENT_H_
|
||||
|
||||
#include <app_event_manager.h>
|
||||
#include <app_event_manager_profiler_tracer.h>
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
enum key_function_action {
|
||||
KEY_FUNCTION_ACTION_RELEASE = 0U,
|
||||
KEY_FUNCTION_ACTION_PRESS = 1U,
|
||||
};
|
||||
|
||||
struct key_function_event {
|
||||
struct app_event_header header;
|
||||
uint16_t usage;
|
||||
uint8_t action;
|
||||
};
|
||||
|
||||
APP_EVENT_TYPE_DECLARE(key_function_event);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* BLINKY_KEY_FUNCTION_EVENT_H_ */
|
||||
@@ -1,6 +1,10 @@
|
||||
#ifndef BLINKY_KEYBOARD_HID_REPORT_EVENT_H_
|
||||
#define BLINKY_KEYBOARD_HID_REPORT_EVENT_H_
|
||||
|
||||
#include <errno.h>
|
||||
#include <stddef.h>
|
||||
#include <string.h>
|
||||
|
||||
#include <app_event_manager.h>
|
||||
#include <app_event_manager_profiler_tracer.h>
|
||||
|
||||
@@ -22,6 +26,30 @@ struct keyboard_hid_report_event {
|
||||
|
||||
APP_EVENT_TYPE_DYNDATA_DECLARE(keyboard_hid_report_event);
|
||||
|
||||
static inline int submit_keyboard_hid_report_event(
|
||||
enum mode_switch_mode mode, enum keyboard_report_type report_type,
|
||||
enum keyboard_protocol_mode protocol_mode,
|
||||
enum hid_queue_policy queue_policy, const uint8_t *data, size_t size)
|
||||
{
|
||||
struct keyboard_hid_report_event *event;
|
||||
|
||||
if ((data == NULL) && (size > 0U)) {
|
||||
return -EINVAL;
|
||||
}
|
||||
|
||||
event = new_keyboard_hid_report_event(size);
|
||||
event->mode = mode;
|
||||
event->report_type = report_type;
|
||||
event->protocol_mode = protocol_mode;
|
||||
event->queue_policy = queue_policy;
|
||||
if (size > 0U) {
|
||||
memcpy(event->dyndata.data, data, size);
|
||||
}
|
||||
|
||||
APP_EVENT_SUBMIT(event);
|
||||
return 0;
|
||||
}
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
@@ -17,6 +17,14 @@ struct led_strip_en_event {
|
||||
|
||||
APP_EVENT_TYPE_DECLARE(led_strip_en_event);
|
||||
|
||||
static inline void submit_led_strip_en_event(bool enabled)
|
||||
{
|
||||
struct led_strip_en_event *event = new_led_strip_en_event();
|
||||
|
||||
event->enabled = enabled;
|
||||
APP_EVENT_SUBMIT(event);
|
||||
}
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
@@ -22,6 +22,16 @@ struct mode_switch_event {
|
||||
|
||||
APP_EVENT_TYPE_DECLARE(mode_switch_event);
|
||||
|
||||
static inline void submit_mode_switch_event(enum mode_switch_mode mode,
|
||||
uint16_t voltage_mv)
|
||||
{
|
||||
struct mode_switch_event *event = new_mode_switch_event();
|
||||
|
||||
event->mode = mode;
|
||||
event->voltage_mv = voltage_mv;
|
||||
APP_EVENT_SUBMIT(event);
|
||||
}
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
18
inc/events/proto_common.h
Normal file
18
inc/events/proto_common.h
Normal file
@@ -0,0 +1,18 @@
|
||||
#ifndef BLINKY_PROTO_COMMON_H_
|
||||
#define BLINKY_PROTO_COMMON_H_
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
enum proto_transport {
|
||||
PROTO_TRANSPORT_USB_CDC = 0,
|
||||
PROTO_TRANSPORT_BLE_NUS,
|
||||
PROTO_TRANSPORT_COUNT,
|
||||
};
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* BLINKY_PROTO_COMMON_H_ */
|
||||
50
inc/events/proto_rx_event.h
Normal file
50
inc/events/proto_rx_event.h
Normal file
@@ -0,0 +1,50 @@
|
||||
#ifndef BLINKY_PROTO_RX_EVENT_H_
|
||||
#define BLINKY_PROTO_RX_EVENT_H_
|
||||
|
||||
#include <errno.h>
|
||||
#include <stddef.h>
|
||||
#include <string.h>
|
||||
|
||||
#include <app_event_manager.h>
|
||||
#include <app_event_manager_profiler_tracer.h>
|
||||
|
||||
#include "proto_common.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
struct proto_rx_event {
|
||||
struct app_event_header header;
|
||||
enum proto_transport transport;
|
||||
struct event_dyndata dyndata;
|
||||
};
|
||||
|
||||
APP_EVENT_TYPE_DYNDATA_DECLARE(proto_rx_event);
|
||||
|
||||
static inline int submit_proto_rx_event(enum proto_transport transport,
|
||||
const uint8_t *data, size_t len)
|
||||
{
|
||||
struct proto_rx_event *event;
|
||||
|
||||
if ((transport >= PROTO_TRANSPORT_COUNT) ||
|
||||
((data == NULL) && (len > 0U))) {
|
||||
return -EINVAL;
|
||||
}
|
||||
|
||||
event = new_proto_rx_event(len);
|
||||
event->transport = transport;
|
||||
|
||||
if (len > 0U) {
|
||||
memcpy(event->dyndata.data, data, len);
|
||||
}
|
||||
|
||||
APP_EVENT_SUBMIT(event);
|
||||
return 0;
|
||||
}
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* BLINKY_PROTO_RX_EVENT_H_ */
|
||||
50
inc/events/proto_tx_event.h
Normal file
50
inc/events/proto_tx_event.h
Normal file
@@ -0,0 +1,50 @@
|
||||
#ifndef BLINKY_PROTO_TX_EVENT_H_
|
||||
#define BLINKY_PROTO_TX_EVENT_H_
|
||||
|
||||
#include <errno.h>
|
||||
#include <stddef.h>
|
||||
#include <string.h>
|
||||
|
||||
#include <app_event_manager.h>
|
||||
#include <app_event_manager_profiler_tracer.h>
|
||||
|
||||
#include "proto_common.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
struct proto_tx_event {
|
||||
struct app_event_header header;
|
||||
enum proto_transport transport;
|
||||
struct event_dyndata dyndata;
|
||||
};
|
||||
|
||||
APP_EVENT_TYPE_DYNDATA_DECLARE(proto_tx_event);
|
||||
|
||||
static inline int submit_proto_tx_event(enum proto_transport transport,
|
||||
const uint8_t *data, size_t len)
|
||||
{
|
||||
struct proto_tx_event *event;
|
||||
|
||||
if ((transport >= PROTO_TRANSPORT_COUNT) ||
|
||||
((data == NULL) && (len > 0U))) {
|
||||
return -EINVAL;
|
||||
}
|
||||
|
||||
event = new_proto_tx_event(len);
|
||||
event->transport = transport;
|
||||
|
||||
if (len > 0U) {
|
||||
memcpy(event->dyndata.data, data, len);
|
||||
}
|
||||
|
||||
APP_EVENT_SUBMIT(event);
|
||||
return 0;
|
||||
}
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* BLINKY_PROTO_TX_EVENT_H_ */
|
||||
@@ -18,6 +18,16 @@ struct set_protocol_event {
|
||||
|
||||
APP_EVENT_TYPE_DECLARE(set_protocol_event);
|
||||
|
||||
static inline void submit_set_protocol_event(enum hid_transport transport,
|
||||
enum keyboard_protocol_mode protocol_mode)
|
||||
{
|
||||
struct set_protocol_event *event = new_set_protocol_event();
|
||||
|
||||
event->transport = transport;
|
||||
event->protocol_mode = protocol_mode;
|
||||
APP_EVENT_SUBMIT(event);
|
||||
}
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
@@ -17,6 +17,14 @@ struct theme_rgb_update_event {
|
||||
|
||||
APP_EVENT_TYPE_DECLARE(theme_rgb_update_event);
|
||||
|
||||
static inline void submit_theme_rgb_update_event(struct theme_rgb theme)
|
||||
{
|
||||
struct theme_rgb_update_event *event = new_theme_rgb_update_event();
|
||||
|
||||
event->theme = theme;
|
||||
APP_EVENT_SUBMIT(event);
|
||||
}
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
@@ -19,6 +19,20 @@ struct time_sync_event {
|
||||
|
||||
APP_EVENT_TYPE_DECLARE(time_sync_event);
|
||||
|
||||
static inline void submit_time_sync_event(uint32_t version, uint32_t flags,
|
||||
int32_t timezone_min, uint64_t utc_ms,
|
||||
uint32_t accuracy_ms)
|
||||
{
|
||||
struct time_sync_event *event = new_time_sync_event();
|
||||
|
||||
event->version = version;
|
||||
event->flags = flags;
|
||||
event->timezone_min = timezone_min;
|
||||
event->utc_ms = utc_ms;
|
||||
event->accuracy_ms = accuracy_ms;
|
||||
APP_EVENT_SUBMIT(event);
|
||||
}
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
@@ -1,22 +0,0 @@
|
||||
#ifndef BLINKY_USB_CDC_RX_EVENT_H_
|
||||
#define BLINKY_USB_CDC_RX_EVENT_H_
|
||||
|
||||
#include <app_event_manager.h>
|
||||
#include <app_event_manager_profiler_tracer.h>
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
struct usb_cdc_rx_event {
|
||||
struct app_event_header header;
|
||||
struct event_dyndata dyndata;
|
||||
};
|
||||
|
||||
APP_EVENT_TYPE_DYNDATA_DECLARE(usb_cdc_rx_event);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* BLINKY_USB_CDC_RX_EVENT_H_ */
|
||||
@@ -1,22 +0,0 @@
|
||||
#ifndef BLINKY_USB_CDC_TX_EVENT_H_
|
||||
#define BLINKY_USB_CDC_TX_EVENT_H_
|
||||
|
||||
#include <app_event_manager.h>
|
||||
#include <app_event_manager_profiler_tracer.h>
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
struct usb_cdc_tx_event {
|
||||
struct app_event_header header;
|
||||
struct event_dyndata dyndata;
|
||||
};
|
||||
|
||||
APP_EVENT_TYPE_DYNDATA_DECLARE(usb_cdc_tx_event);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* BLINKY_USB_CDC_TX_EVENT_H_ */
|
||||
@@ -1,24 +0,0 @@
|
||||
#ifndef BLINKY_USB_DEVICE_STATE_EVENT_H_
|
||||
#define BLINKY_USB_DEVICE_STATE_EVENT_H_
|
||||
|
||||
#include <app_event_manager.h>
|
||||
#include <app_event_manager_profiler_tracer.h>
|
||||
|
||||
#include "usb_device_module.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
struct usb_device_state_event {
|
||||
struct app_event_header header;
|
||||
enum usb_device_state state;
|
||||
};
|
||||
|
||||
APP_EVENT_TYPE_DECLARE(usb_device_state_event);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* BLINKY_USB_DEVICE_STATE_EVENT_H_ */
|
||||
@@ -1,24 +0,0 @@
|
||||
#ifndef BLINKY_USB_FUNCTION_READY_EVENT_H_
|
||||
#define BLINKY_USB_FUNCTION_READY_EVENT_H_
|
||||
|
||||
#include <app_event_manager.h>
|
||||
#include <app_event_manager_profiler_tracer.h>
|
||||
|
||||
#include "usb_device_module.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
struct usb_function_ready_event {
|
||||
struct app_event_header header;
|
||||
uint8_t function_mask;
|
||||
};
|
||||
|
||||
APP_EVENT_TYPE_DECLARE(usb_function_ready_event);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* BLINKY_USB_FUNCTION_READY_EVENT_H_ */
|
||||
@@ -1,21 +0,0 @@
|
||||
#ifndef BLINKY_USB_PREPARE_EVENT_H_
|
||||
#define BLINKY_USB_PREPARE_EVENT_H_
|
||||
|
||||
#include <app_event_manager.h>
|
||||
#include <app_event_manager_profiler_tracer.h>
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
struct usb_prepare_event {
|
||||
struct app_event_header header;
|
||||
};
|
||||
|
||||
APP_EVENT_TYPE_DECLARE(usb_prepare_event);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* BLINKY_USB_PREPARE_EVENT_H_ */
|
||||
82
inc/events/usb_state_event.h
Normal file
82
inc/events/usb_state_event.h
Normal file
@@ -0,0 +1,82 @@
|
||||
#ifndef BLINKY_USB_STATE_EVENT_H_
|
||||
#define BLINKY_USB_STATE_EVENT_H_
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
#include <app_event_manager.h>
|
||||
#include <app_event_manager_profiler_tracer.h>
|
||||
#include <zephyr/sys/util.h>
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
enum usb_state_event_op {
|
||||
USB_STATE_EVENT_OP_SET_BITS,
|
||||
USB_STATE_EVENT_OP_CLEAR_BITS,
|
||||
USB_STATE_EVENT_OP_SNAPSHOT,
|
||||
};
|
||||
|
||||
enum usb_state_flag {
|
||||
USB_STATEF_PREPARE = BIT(0),
|
||||
USB_STATEF_HID_READY = BIT(1),
|
||||
USB_STATEF_CDC_READY = BIT(2),
|
||||
USB_STATEF_STACK_READY = BIT(3),
|
||||
USB_STATEF_POWERED = BIT(4),
|
||||
USB_STATEF_ACTIVE = BIT(5),
|
||||
USB_STATEF_SUSPENDED = BIT(6),
|
||||
USB_STATEF_STACK_RUNNING = BIT(31),
|
||||
};
|
||||
|
||||
struct usb_state_event {
|
||||
struct app_event_header header;
|
||||
const void *src_module_id;
|
||||
const void *sink_module_id;
|
||||
uint8_t op;
|
||||
uint32_t flags;
|
||||
};
|
||||
|
||||
APP_EVENT_TYPE_DECLARE(usb_state_event);
|
||||
|
||||
static inline void submit_usb_state_event(const void *src_module_id,
|
||||
const void *sink_module_id,
|
||||
uint8_t op,
|
||||
uint32_t flags)
|
||||
{
|
||||
struct usb_state_event *event = new_usb_state_event();
|
||||
|
||||
event->src_module_id = src_module_id;
|
||||
event->sink_module_id = sink_module_id;
|
||||
event->op = op;
|
||||
event->flags = flags;
|
||||
APP_EVENT_SUBMIT(event);
|
||||
}
|
||||
|
||||
static inline void submit_usb_state_set(const void *src_module_id,
|
||||
const void *sink_module_id,
|
||||
uint32_t flags)
|
||||
{
|
||||
submit_usb_state_event(src_module_id, sink_module_id,
|
||||
USB_STATE_EVENT_OP_SET_BITS, flags);
|
||||
}
|
||||
|
||||
static inline void submit_usb_state_clear(const void *src_module_id,
|
||||
const void *sink_module_id,
|
||||
uint32_t flags)
|
||||
{
|
||||
submit_usb_state_event(src_module_id, sink_module_id,
|
||||
USB_STATE_EVENT_OP_CLEAR_BITS, flags);
|
||||
}
|
||||
|
||||
static inline void submit_usb_state_snapshot(const void *src_module_id,
|
||||
uint32_t flags)
|
||||
{
|
||||
submit_usb_state_event(src_module_id, NULL,
|
||||
USB_STATE_EVENT_OP_SNAPSHOT, flags);
|
||||
}
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* BLINKY_USB_STATE_EVENT_H_ */
|
||||
@@ -1,31 +1,25 @@
|
||||
#ifndef BLINKY_PROTOCOL_MODULE_H_
|
||||
#define BLINKY_PROTOCOL_MODULE_H_
|
||||
|
||||
#include <stdbool.h>
|
||||
#include <stddef.h>
|
||||
#include <stdint.h>
|
||||
|
||||
#include "proto_common.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
#define CDC_PROTO_TYPE_HELLO_REQ 0x01U
|
||||
#define CDC_PROTO_TYPE_HELLO_RSP 0x02U
|
||||
#define CDC_PROTO_TYPE_BITMAP 0x10U
|
||||
#define CDC_PROTO_TYPE_FUNCTION_KEY_EVENT 0x20U
|
||||
#define CDC_PROTO_TYPE_LED_STATE 0x21U
|
||||
#define CDC_PROTO_TYPE_TIME_SYNC 0x30U
|
||||
#define CDC_PROTO_TYPE_THEME_RGB 0x31U
|
||||
#define CDC_PROTO_TYPE_ACK 0x7EU
|
||||
#define CDC_PROTO_TYPE_ERROR 0x7FU
|
||||
|
||||
int protocol_module_process_cdc_packet(uint8_t req_type,
|
||||
int protocol_module_process_message(enum proto_transport transport,
|
||||
const uint8_t *req_payload,
|
||||
size_t req_payload_len,
|
||||
uint8_t *rsp_type,
|
||||
uint8_t *rsp_payload,
|
||||
size_t rsp_payload_buf_size,
|
||||
size_t *rsp_payload_len);
|
||||
|
||||
void protocol_module_reset_transport_state(enum proto_transport transport);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
@@ -1,26 +0,0 @@
|
||||
#ifndef BLINKY_USB_DEVICE_MODULE_H_
|
||||
#define BLINKY_USB_DEVICE_MODULE_H_
|
||||
|
||||
#include <stdbool.h>
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
enum usb_function {
|
||||
USB_FUNCTION_HID = 0x01,
|
||||
USB_FUNCTION_CDC_ACM = 0x02,
|
||||
};
|
||||
|
||||
enum usb_device_state {
|
||||
USB_DEVICE_STATE_DISCONNECTED,
|
||||
USB_DEVICE_STATE_POWERED,
|
||||
USB_DEVICE_STATE_ACTIVE,
|
||||
USB_DEVICE_STATE_SUSPENDED,
|
||||
};
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* BLINKY_USB_DEVICE_MODULE_H_ */
|
||||
1
prj.conf
1
prj.conf
@@ -45,6 +45,7 @@ CONFIG_CDC_ACM_SERIAL_INITIALIZE_AT_BOOT=n
|
||||
# BLE
|
||||
CONFIG_BT=y
|
||||
CONFIG_BT_PERIPHERAL=y
|
||||
CONFIG_BT_ZEPHYR_NUS=y
|
||||
CONFIG_BT_SMP=y
|
||||
CONFIG_BT_BONDABLE=y
|
||||
CONFIG_BT_SETTINGS=y
|
||||
|
||||
@@ -1 +1,2 @@
|
||||
Bitmap.usage_bitmap max_size:29
|
||||
FunctionKeyEvent.usage_bitmap max_size:29
|
||||
|
||||
@@ -17,14 +17,8 @@ message Bitmap {
|
||||
bytes usage_bitmap = 1;
|
||||
}
|
||||
|
||||
enum KeyAction {
|
||||
KEY_ACTION_RELEASE = 0;
|
||||
KEY_ACTION_PRESS = 1;
|
||||
}
|
||||
|
||||
message FunctionKeyEvent {
|
||||
uint32 usage = 1;
|
||||
KeyAction action = 2;
|
||||
bytes usage_bitmap = 1;
|
||||
}
|
||||
|
||||
message LedState {
|
||||
|
||||
@@ -81,30 +81,6 @@ static uint8_t battery_soc_from_mv(int voltage_mv)
|
||||
return (uint8_t)(bucket * 10);
|
||||
}
|
||||
|
||||
static void submit_bat_state_event(uint8_t soc, bool charging, bool full)
|
||||
{
|
||||
struct bat_state_event *event;
|
||||
|
||||
if (last_bat_state.valid &&
|
||||
(last_bat_state.soc == soc) &&
|
||||
(last_bat_state.charging == charging) &&
|
||||
(last_bat_state.full == full)) {
|
||||
return;
|
||||
}
|
||||
|
||||
last_bat_state.valid = true;
|
||||
last_bat_state.soc = soc;
|
||||
last_bat_state.charging = charging;
|
||||
last_bat_state.full = full;
|
||||
|
||||
event = new_bat_state_event();
|
||||
event->soc = soc;
|
||||
event->charging = charging;
|
||||
event->full = full;
|
||||
|
||||
APP_EVENT_SUBMIT(event);
|
||||
}
|
||||
|
||||
static void battery_sample_fn(struct k_work *work)
|
||||
{
|
||||
struct ip5306_status pmic_status;
|
||||
@@ -137,9 +113,18 @@ static void battery_sample_fn(struct k_work *work)
|
||||
}
|
||||
|
||||
voltage_mv = sensor_value_to_mv(&voltage);
|
||||
submit_bat_state_event(battery_soc_from_mv(voltage_mv),
|
||||
pmic_status.charging,
|
||||
pmic_status.full);
|
||||
uint8_t soc = battery_soc_from_mv(voltage_mv);
|
||||
|
||||
if (!last_bat_state.valid ||
|
||||
(last_bat_state.soc != soc) ||
|
||||
(last_bat_state.charging != pmic_status.charging) ||
|
||||
(last_bat_state.full != pmic_status.full)) {
|
||||
last_bat_state.valid = true;
|
||||
last_bat_state.soc = soc;
|
||||
last_bat_state.charging = pmic_status.charging;
|
||||
last_bat_state.full = pmic_status.full;
|
||||
submit_bat_state_event(soc, pmic_status.charging, pmic_status.full);
|
||||
}
|
||||
|
||||
reschedule:
|
||||
if (running) {
|
||||
|
||||
@@ -98,54 +98,19 @@ static const uint8_t hid_report_desc[] = {
|
||||
0xC0 /* End Collection */
|
||||
};
|
||||
|
||||
static void submit_set_protocol_event(void)
|
||||
static void submit_ble_transport_state_event(void)
|
||||
{
|
||||
struct set_protocol_event *event = new_set_protocol_event();
|
||||
|
||||
event->transport = HID_TRANSPORT_BLE;
|
||||
event->protocol_mode = protocol_mode;
|
||||
APP_EVENT_SUBMIT(event);
|
||||
}
|
||||
|
||||
static void submit_hid_led_event(uint8_t led_bm)
|
||||
{
|
||||
struct hid_led_event *event = new_hid_led_event();
|
||||
|
||||
event->transport = HID_TRANSPORT_BLE;
|
||||
event->led_bm = led_bm;
|
||||
APP_EVENT_SUBMIT(event);
|
||||
}
|
||||
|
||||
static void submit_transport_state_event(void)
|
||||
{
|
||||
struct hid_transport_state_event *event = new_hid_transport_state_event();
|
||||
bool ready = running && secured && (active_conn != NULL);
|
||||
|
||||
event->transport = HID_TRANSPORT_BLE;
|
||||
event->ready = ready;
|
||||
event->protocol_mode = protocol_mode;
|
||||
event->keys_ready = ready &&
|
||||
((protocol_mode == KEYBOARD_PROTOCOL_MODE_BOOT) ?
|
||||
submit_hid_transport_state_event(
|
||||
HID_TRANSPORT_BLE,
|
||||
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);
|
||||
keyboard_report_notify_enabled),
|
||||
ready && (protocol_mode == KEYBOARD_PROTOCOL_MODE_REPORT) &&
|
||||
consumer_report_notify_enabled,
|
||||
protocol_mode);
|
||||
}
|
||||
|
||||
static void input_report_notify_handler(uint8_t report_id, enum bt_hids_notify_evt evt)
|
||||
@@ -158,13 +123,13 @@ static void input_report_notify_handler(uint8_t report_id, enum bt_hids_notify_e
|
||||
consumer_report_notify_enabled = enabled;
|
||||
}
|
||||
|
||||
submit_transport_state_event();
|
||||
submit_ble_transport_state_event();
|
||||
}
|
||||
|
||||
static void boot_keyboard_notify_handler(enum bt_hids_notify_evt evt)
|
||||
{
|
||||
boot_keyboard_notify_enabled = (evt == BT_HIDS_CCCD_EVT_NOTIFY_ENABLED);
|
||||
submit_transport_state_event();
|
||||
submit_ble_transport_state_event();
|
||||
}
|
||||
|
||||
static void hid_report_complete_cb(struct bt_conn *conn, void *user_data)
|
||||
@@ -176,7 +141,8 @@ static void hid_report_complete_cb(struct bt_conn *conn, void *user_data)
|
||||
return;
|
||||
}
|
||||
|
||||
submit_hid_report_sent_event(in_flight.report_type, in_flight.sequence, false);
|
||||
submit_hid_report_sent_event(HID_TRANSPORT_BLE, in_flight.report_type,
|
||||
in_flight.sequence, false);
|
||||
in_flight.active = false;
|
||||
}
|
||||
|
||||
@@ -186,7 +152,7 @@ static void keyboard_led_report_common(struct bt_hids_rep *rep, bool write)
|
||||
return;
|
||||
}
|
||||
|
||||
submit_hid_led_event(rep->data[0]);
|
||||
submit_hid_led_event(HID_TRANSPORT_BLE, rep->data[0]);
|
||||
}
|
||||
|
||||
static void keyboard_led_report_handler(struct bt_hids_rep *rep,
|
||||
@@ -224,8 +190,8 @@ static void pm_evt_handler(enum bt_hids_pm_evt evt, struct bt_conn *conn)
|
||||
return;
|
||||
}
|
||||
|
||||
submit_set_protocol_event();
|
||||
submit_transport_state_event();
|
||||
submit_set_protocol_event(HID_TRANSPORT_BLE, protocol_mode);
|
||||
submit_ble_transport_state_event();
|
||||
}
|
||||
|
||||
static int module_init(void)
|
||||
@@ -272,7 +238,7 @@ static int module_start(void)
|
||||
}
|
||||
|
||||
running = true;
|
||||
submit_transport_state_event();
|
||||
submit_ble_transport_state_event();
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -285,7 +251,7 @@ static void module_pause(void)
|
||||
|
||||
in_flight.active = false;
|
||||
running = false;
|
||||
submit_transport_state_event();
|
||||
submit_ble_transport_state_event();
|
||||
}
|
||||
|
||||
static void reset_connection_state(void)
|
||||
@@ -311,12 +277,12 @@ static bool handle_ble_peer_event(const struct ble_peer_event *event)
|
||||
|
||||
active_conn = event->id;
|
||||
protocol_mode = KEYBOARD_PROTOCOL_MODE_REPORT;
|
||||
submit_set_protocol_event();
|
||||
submit_set_protocol_event(HID_TRANSPORT_BLE, protocol_mode);
|
||||
err = bt_hids_connected(&hids_obj, event->id);
|
||||
if (err) {
|
||||
LOG_ERR("bt_hids_connected failed (%d)", err);
|
||||
}
|
||||
submit_transport_state_event();
|
||||
submit_ble_transport_state_event();
|
||||
return false;
|
||||
|
||||
case PEER_STATE_SECURED:
|
||||
@@ -325,7 +291,7 @@ static bool handle_ble_peer_event(const struct ble_peer_event *event)
|
||||
}
|
||||
|
||||
secured = true;
|
||||
submit_transport_state_event();
|
||||
submit_ble_transport_state_event();
|
||||
return false;
|
||||
|
||||
case PEER_STATE_DISCONNECTED:
|
||||
@@ -339,7 +305,7 @@ static bool handle_ble_peer_event(const struct ble_peer_event *event)
|
||||
}
|
||||
|
||||
reset_connection_state();
|
||||
submit_transport_state_event();
|
||||
submit_ble_transport_state_event();
|
||||
return false;
|
||||
|
||||
default:
|
||||
@@ -387,7 +353,8 @@ static bool handle_hid_tx_report_event(const struct hid_tx_report_event *event)
|
||||
if (err) {
|
||||
in_flight.active = false;
|
||||
LOG_WRN("BLE keyboard report submit failed (%d)", err);
|
||||
submit_hid_report_sent_event(KEYBOARD_REPORT_TYPE_KEYS,
|
||||
submit_hid_report_sent_event(HID_TRANSPORT_BLE,
|
||||
KEYBOARD_REPORT_TYPE_KEYS,
|
||||
event->sequence, true);
|
||||
}
|
||||
|
||||
@@ -413,7 +380,8 @@ static bool handle_hid_tx_report_event(const struct hid_tx_report_event *event)
|
||||
if (err) {
|
||||
in_flight.active = false;
|
||||
LOG_WRN("BLE consumer report submit failed (%d)", err);
|
||||
submit_hid_report_sent_event(KEYBOARD_REPORT_TYPE_CONSUMER,
|
||||
submit_hid_report_sent_event(HID_TRANSPORT_BLE,
|
||||
KEYBOARD_REPORT_TYPE_CONSUMER,
|
||||
event->sequence, true);
|
||||
}
|
||||
}
|
||||
|
||||
215
src/ble_nus_module.c
Normal file
215
src/ble_nus_module.c
Normal file
@@ -0,0 +1,215 @@
|
||||
#include <errno.h>
|
||||
#include <stdbool.h>
|
||||
#include <stdint.h>
|
||||
|
||||
#include <app_event_manager.h>
|
||||
|
||||
#define MODULE ble_nus_module
|
||||
#include <caf/events/module_state_event.h>
|
||||
#include <caf/events/power_event.h>
|
||||
|
||||
#include <caf/events/ble_common_event.h>
|
||||
#include <zephyr/bluetooth/conn.h>
|
||||
#include <zephyr/bluetooth/services/nus.h>
|
||||
#include <zephyr/logging/log.h>
|
||||
|
||||
#include "proto_rx_event.h"
|
||||
#include "proto_tx_event.h"
|
||||
#include "protocol_module.h"
|
||||
|
||||
LOG_MODULE_REGISTER(MODULE, LOG_LEVEL_INF);
|
||||
|
||||
static struct bt_conn *active_conn;
|
||||
static bool initialized;
|
||||
static bool running;
|
||||
static bool ble_ready;
|
||||
static bool tx_notify_enabled;
|
||||
|
||||
static void notif_enabled(bool enabled, void *ctx)
|
||||
{
|
||||
ARG_UNUSED(ctx);
|
||||
|
||||
tx_notify_enabled = enabled;
|
||||
LOG_INF("BLE NUS TX notify %s", enabled ? "enabled" : "disabled");
|
||||
}
|
||||
|
||||
static void received(struct bt_conn *conn, const void *data, uint16_t len, void *ctx)
|
||||
{
|
||||
ARG_UNUSED(ctx);
|
||||
|
||||
if (!running || !ble_ready || (conn != active_conn)) {
|
||||
return;
|
||||
}
|
||||
|
||||
(void)submit_proto_rx_event(PROTO_TRANSPORT_BLE_NUS, data, len);
|
||||
}
|
||||
|
||||
static struct bt_nus_cb nus_listener = {
|
||||
.notif_enabled = notif_enabled,
|
||||
.received = received,
|
||||
};
|
||||
|
||||
static void reset_connection_state(void)
|
||||
{
|
||||
active_conn = NULL;
|
||||
tx_notify_enabled = false;
|
||||
protocol_module_reset_transport_state(PROTO_TRANSPORT_BLE_NUS);
|
||||
}
|
||||
|
||||
static int module_init(void)
|
||||
{
|
||||
int err;
|
||||
|
||||
err = bt_nus_cb_register(&nus_listener, NULL);
|
||||
if (err) {
|
||||
LOG_ERR("bt_nus_cb_register failed (%d)", err);
|
||||
return err;
|
||||
}
|
||||
|
||||
reset_connection_state();
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int module_start(void)
|
||||
{
|
||||
if (running) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
running = true;
|
||||
return 0;
|
||||
}
|
||||
|
||||
static void module_pause(void)
|
||||
{
|
||||
if (!running) {
|
||||
return;
|
||||
}
|
||||
|
||||
running = false;
|
||||
tx_notify_enabled = false;
|
||||
}
|
||||
|
||||
static bool handle_ble_peer_event(const struct ble_peer_event *event)
|
||||
{
|
||||
switch (event->state) {
|
||||
case PEER_STATE_CONNECTED:
|
||||
if (active_conn != NULL) {
|
||||
return false;
|
||||
}
|
||||
|
||||
active_conn = event->id;
|
||||
tx_notify_enabled = false;
|
||||
protocol_module_reset_transport_state(PROTO_TRANSPORT_BLE_NUS);
|
||||
return false;
|
||||
|
||||
case PEER_STATE_DISCONNECTED:
|
||||
if (active_conn != event->id) {
|
||||
return false;
|
||||
}
|
||||
|
||||
reset_connection_state();
|
||||
return false;
|
||||
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
static bool handle_proto_tx_event(const struct proto_tx_event *event)
|
||||
{
|
||||
int err;
|
||||
|
||||
if (event->transport != PROTO_TRANSPORT_BLE_NUS) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!running || !ble_ready || (active_conn == NULL) || !tx_notify_enabled) {
|
||||
return false;
|
||||
}
|
||||
|
||||
err = bt_nus_send(active_conn, event->dyndata.data, (uint16_t)event->dyndata.size);
|
||||
if (err) {
|
||||
LOG_WRN("bt_nus_send failed (%d)", err);
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
static bool app_event_handler(const struct app_event_header *aeh)
|
||||
{
|
||||
if (is_proto_tx_event(aeh)) {
|
||||
return handle_proto_tx_event(cast_proto_tx_event(aeh));
|
||||
}
|
||||
|
||||
if (is_ble_peer_event(aeh)) {
|
||||
return handle_ble_peer_event(cast_ble_peer_event(aeh));
|
||||
}
|
||||
|
||||
if (is_module_state_event(aeh)) {
|
||||
const struct module_state_event *event = cast_module_state_event(aeh);
|
||||
int err;
|
||||
|
||||
if (check_state(event, MODULE_ID(main), MODULE_STATE_READY)) {
|
||||
if (!initialized) {
|
||||
err = module_init();
|
||||
if (err) {
|
||||
module_set_state(MODULE_STATE_ERROR);
|
||||
return false;
|
||||
}
|
||||
|
||||
initialized = true;
|
||||
}
|
||||
|
||||
err = module_start();
|
||||
if (err) {
|
||||
module_set_state(MODULE_STATE_ERROR);
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
if (check_state(event, MODULE_ID(ble_state), MODULE_STATE_READY)) {
|
||||
ble_ready = true;
|
||||
if (running) {
|
||||
module_set_state(MODULE_STATE_READY);
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
if (is_power_down_event(aeh)) {
|
||||
if (initialized) {
|
||||
module_pause();
|
||||
module_set_state(MODULE_STATE_STANDBY);
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
if (is_wake_up_event(aeh)) {
|
||||
if (initialized) {
|
||||
int err = module_start();
|
||||
|
||||
if (err) {
|
||||
module_set_state(MODULE_STATE_ERROR);
|
||||
} else if (ble_ready) {
|
||||
module_set_state(MODULE_STATE_READY);
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
APP_EVENT_LISTENER(MODULE, app_event_handler);
|
||||
APP_EVENT_SUBSCRIBE(MODULE, module_state_event);
|
||||
APP_EVENT_SUBSCRIBE(MODULE, proto_tx_event);
|
||||
APP_EVENT_SUBSCRIBE_EARLY(MODULE, ble_peer_event);
|
||||
APP_EVENT_SUBSCRIBE_EARLY(MODULE, power_down_event);
|
||||
APP_EVENT_SUBSCRIBE(MODULE, wake_up_event);
|
||||
@@ -1,308 +0,0 @@
|
||||
#include <errno.h>
|
||||
#include <stdbool.h>
|
||||
#include <stdint.h>
|
||||
#include <string.h>
|
||||
|
||||
#include <app_event_manager.h>
|
||||
|
||||
#define MODULE ble_serial_module
|
||||
#include <caf/events/module_state_event.h>
|
||||
#include <caf/events/power_event.h>
|
||||
|
||||
#include <caf/events/ble_common_event.h>
|
||||
#include <zephyr/bluetooth/att.h>
|
||||
#include <zephyr/bluetooth/conn.h>
|
||||
#include <zephyr/bluetooth/gatt.h>
|
||||
#include <zephyr/bluetooth/uuid.h>
|
||||
#include <zephyr/logging/log.h>
|
||||
|
||||
#include "ble_serial_rx_event.h"
|
||||
#include "ble_serial_tx_event.h"
|
||||
|
||||
LOG_MODULE_REGISTER(MODULE, LOG_LEVEL_INF);
|
||||
|
||||
#define BLE_SERIAL_RX_MAX_LEN 64U
|
||||
#define BLE_SERIAL_MIN_PAYLOAD_LEN 32U
|
||||
#define BLE_SERIAL_NOTIFY_OVERHEAD 3U
|
||||
#define BLE_SERIAL_SERVICE_UUID_VAL \
|
||||
BT_UUID_128_ENCODE(0x0b7f6000, 0x38d2, 0x4f62, 0x8f6f, 0x36c4fd73a110)
|
||||
#define BLE_SERIAL_RX_UUID_VAL \
|
||||
BT_UUID_128_ENCODE(0x0b7f6001, 0x38d2, 0x4f62, 0x8f6f, 0x36c4fd73a110)
|
||||
#define BLE_SERIAL_TX_UUID_VAL \
|
||||
BT_UUID_128_ENCODE(0x0b7f6002, 0x38d2, 0x4f62, 0x8f6f, 0x36c4fd73a110)
|
||||
|
||||
static struct bt_conn *active_conn;
|
||||
static bool initialized;
|
||||
static bool running;
|
||||
static bool ble_ready;
|
||||
static bool secured;
|
||||
static bool tx_notify_enabled;
|
||||
|
||||
static const struct bt_uuid_128 ble_serial_service_uuid =
|
||||
BT_UUID_INIT_128(BLE_SERIAL_SERVICE_UUID_VAL);
|
||||
static const struct bt_uuid_128 ble_serial_rx_uuid =
|
||||
BT_UUID_INIT_128(BLE_SERIAL_RX_UUID_VAL);
|
||||
static const struct bt_uuid_128 ble_serial_tx_uuid =
|
||||
BT_UUID_INIT_128(BLE_SERIAL_TX_UUID_VAL);
|
||||
|
||||
static void submit_ble_serial_rx_event(const uint8_t *data, size_t len)
|
||||
{
|
||||
struct ble_serial_rx_event *event = new_ble_serial_rx_event(len);
|
||||
|
||||
memcpy(event->dyndata.data, data, len);
|
||||
APP_EVENT_SUBMIT(event);
|
||||
}
|
||||
|
||||
static void tx_ccc_cfg_changed(const struct bt_gatt_attr *attr, uint16_t value)
|
||||
{
|
||||
ARG_UNUSED(attr);
|
||||
|
||||
tx_notify_enabled = (value == BT_GATT_CCC_NOTIFY);
|
||||
LOG_INF("BLE serial TX notify %s", tx_notify_enabled ? "enabled" : "disabled");
|
||||
}
|
||||
|
||||
static ssize_t rx_write_handler(struct bt_conn *conn,
|
||||
const struct bt_gatt_attr *attr,
|
||||
const void *buf,
|
||||
uint16_t len,
|
||||
uint16_t offset,
|
||||
uint8_t flags)
|
||||
{
|
||||
ARG_UNUSED(attr);
|
||||
ARG_UNUSED(flags);
|
||||
|
||||
if (!running || !ble_ready || !secured || (conn != active_conn)) {
|
||||
return BT_GATT_ERR(BT_ATT_ERR_WRITE_REQ_REJECTED);
|
||||
}
|
||||
|
||||
if (offset != 0U) {
|
||||
return BT_GATT_ERR(BT_ATT_ERR_INVALID_OFFSET);
|
||||
}
|
||||
|
||||
if ((buf == NULL) || (len == 0U) || (len > BLE_SERIAL_RX_MAX_LEN)) {
|
||||
return BT_GATT_ERR(BT_ATT_ERR_INVALID_ATTRIBUTE_LEN);
|
||||
}
|
||||
|
||||
submit_ble_serial_rx_event(buf, len);
|
||||
return len;
|
||||
}
|
||||
|
||||
BT_GATT_SERVICE_DEFINE(ble_serial_svc,
|
||||
BT_GATT_PRIMARY_SERVICE(&ble_serial_service_uuid.uuid),
|
||||
BT_GATT_CHARACTERISTIC(&ble_serial_rx_uuid.uuid,
|
||||
BT_GATT_CHRC_WRITE | BT_GATT_CHRC_WRITE_WITHOUT_RESP,
|
||||
BT_GATT_PERM_WRITE_ENCRYPT,
|
||||
NULL, rx_write_handler, NULL),
|
||||
BT_GATT_CHARACTERISTIC(&ble_serial_tx_uuid.uuid,
|
||||
BT_GATT_CHRC_NOTIFY,
|
||||
BT_GATT_PERM_NONE,
|
||||
NULL, NULL, NULL),
|
||||
BT_GATT_CCC(tx_ccc_cfg_changed,
|
||||
BT_GATT_PERM_READ | BT_GATT_PERM_WRITE_ENCRYPT)
|
||||
);
|
||||
|
||||
static void mtu_updated(struct bt_conn *conn, uint16_t tx, uint16_t rx)
|
||||
{
|
||||
if (conn != active_conn) {
|
||||
return;
|
||||
}
|
||||
|
||||
LOG_INF("BLE serial MTU updated tx:%u rx:%u", tx, rx);
|
||||
}
|
||||
|
||||
static struct bt_gatt_cb gatt_callbacks = {
|
||||
.att_mtu_updated = mtu_updated,
|
||||
};
|
||||
|
||||
static size_t notify_payload_max(void)
|
||||
{
|
||||
uint16_t mtu;
|
||||
|
||||
if (active_conn == NULL) {
|
||||
return 0U;
|
||||
}
|
||||
|
||||
mtu = bt_gatt_get_mtu(active_conn);
|
||||
if (mtu <= BLE_SERIAL_NOTIFY_OVERHEAD) {
|
||||
return 0U;
|
||||
}
|
||||
|
||||
return (size_t)(mtu - BLE_SERIAL_NOTIFY_OVERHEAD);
|
||||
}
|
||||
|
||||
static void reset_connection_state(void)
|
||||
{
|
||||
active_conn = NULL;
|
||||
secured = false;
|
||||
tx_notify_enabled = false;
|
||||
}
|
||||
|
||||
static int module_init(void)
|
||||
{
|
||||
bt_gatt_cb_register(&gatt_callbacks);
|
||||
reset_connection_state();
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int module_start(void)
|
||||
{
|
||||
if (running) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
running = true;
|
||||
return 0;
|
||||
}
|
||||
|
||||
static void module_pause(void)
|
||||
{
|
||||
if (!running) {
|
||||
return;
|
||||
}
|
||||
|
||||
running = false;
|
||||
tx_notify_enabled = false;
|
||||
}
|
||||
|
||||
static bool handle_ble_peer_event(const struct ble_peer_event *event)
|
||||
{
|
||||
switch (event->state) {
|
||||
case PEER_STATE_CONNECTED:
|
||||
if (active_conn != NULL) {
|
||||
return false;
|
||||
}
|
||||
|
||||
active_conn = event->id;
|
||||
secured = false;
|
||||
tx_notify_enabled = false;
|
||||
return false;
|
||||
|
||||
case PEER_STATE_SECURED:
|
||||
if (active_conn != event->id) {
|
||||
return false;
|
||||
}
|
||||
|
||||
secured = true;
|
||||
return false;
|
||||
|
||||
case PEER_STATE_DISCONNECTED:
|
||||
if (active_conn != event->id) {
|
||||
return false;
|
||||
}
|
||||
|
||||
reset_connection_state();
|
||||
return false;
|
||||
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
static bool handle_ble_serial_tx_event(const struct ble_serial_tx_event *event)
|
||||
{
|
||||
size_t payload_max;
|
||||
int err;
|
||||
|
||||
if (!running || !ble_ready || (active_conn == NULL) || !secured || !tx_notify_enabled) {
|
||||
return false;
|
||||
}
|
||||
|
||||
payload_max = notify_payload_max();
|
||||
if (payload_max < BLE_SERIAL_MIN_PAYLOAD_LEN) {
|
||||
LOG_WRN("BLE serial MTU payload too small:%u", (uint32_t)payload_max);
|
||||
return false;
|
||||
}
|
||||
|
||||
if (event->dyndata.size > payload_max) {
|
||||
LOG_WRN("BLE serial TX too large len:%u max:%u",
|
||||
(uint32_t)event->dyndata.size, (uint32_t)payload_max);
|
||||
return false;
|
||||
}
|
||||
|
||||
err = bt_gatt_notify(active_conn, &ble_serial_svc.attrs[4],
|
||||
event->dyndata.data,
|
||||
(uint16_t)event->dyndata.size);
|
||||
if (err) {
|
||||
LOG_WRN("bt_gatt_notify failed (%d)", err);
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
static bool app_event_handler(const struct app_event_header *aeh)
|
||||
{
|
||||
if (is_ble_serial_tx_event(aeh)) {
|
||||
return handle_ble_serial_tx_event(cast_ble_serial_tx_event(aeh));
|
||||
}
|
||||
|
||||
if (is_ble_peer_event(aeh)) {
|
||||
return handle_ble_peer_event(cast_ble_peer_event(aeh));
|
||||
}
|
||||
|
||||
if (is_module_state_event(aeh)) {
|
||||
const struct module_state_event *event = cast_module_state_event(aeh);
|
||||
int err;
|
||||
|
||||
if (check_state(event, MODULE_ID(main), MODULE_STATE_READY)) {
|
||||
if (!initialized) {
|
||||
err = module_init();
|
||||
if (err) {
|
||||
module_set_state(MODULE_STATE_ERROR);
|
||||
return false;
|
||||
}
|
||||
|
||||
initialized = true;
|
||||
}
|
||||
|
||||
err = module_start();
|
||||
if (err) {
|
||||
module_set_state(MODULE_STATE_ERROR);
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
if (check_state(event, MODULE_ID(ble_state), MODULE_STATE_READY)) {
|
||||
ble_ready = true;
|
||||
if (running) {
|
||||
module_set_state(MODULE_STATE_READY);
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
if (is_power_down_event(aeh)) {
|
||||
if (initialized) {
|
||||
module_pause();
|
||||
module_set_state(MODULE_STATE_STANDBY);
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
if (is_wake_up_event(aeh)) {
|
||||
if (initialized) {
|
||||
int err = module_start();
|
||||
|
||||
if (err) {
|
||||
module_set_state(MODULE_STATE_ERROR);
|
||||
} else if (ble_ready) {
|
||||
module_set_state(MODULE_STATE_READY);
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
APP_EVENT_LISTENER(MODULE, app_event_handler);
|
||||
APP_EVENT_SUBSCRIBE(MODULE, module_state_event);
|
||||
APP_EVENT_SUBSCRIBE(MODULE, ble_serial_tx_event);
|
||||
APP_EVENT_SUBSCRIBE_EARLY(MODULE, ble_peer_event);
|
||||
APP_EVENT_SUBSCRIBE_EARLY(MODULE, power_down_event);
|
||||
APP_EVENT_SUBSCRIBE(MODULE, wake_up_event);
|
||||
@@ -1,299 +0,0 @@
|
||||
#include <errno.h>
|
||||
#include <stdbool.h>
|
||||
#include <stddef.h>
|
||||
#include <stdint.h>
|
||||
#include <string.h>
|
||||
|
||||
#include <app_event_manager.h>
|
||||
|
||||
#define MODULE cdc_wrapper_module
|
||||
#include <caf/events/module_state_event.h>
|
||||
#include <caf/events/power_event.h>
|
||||
|
||||
#include <zephyr/logging/log.h>
|
||||
|
||||
#include "cdc_proto_tx_event.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 response type:0x%02x len:%u",
|
||||
rsp_type, (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 bool handle_cdc_proto_tx_event(const struct cdc_proto_tx_event *event)
|
||||
{
|
||||
if (!running) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (event->dyndata.size > CDC_WRAPPER_MAX_PAYLOAD_LEN) {
|
||||
LOG_WRN("Drop CDC proto TX len:%u max:%u",
|
||||
(uint32_t)event->dyndata.size,
|
||||
(uint32_t)CDC_WRAPPER_MAX_PAYLOAD_LEN);
|
||||
return false;
|
||||
}
|
||||
|
||||
submit_tx_frame(event->type, event->dyndata.data, event->dyndata.size);
|
||||
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_cdc_proto_tx_event(aeh)) {
|
||||
return handle_cdc_proto_tx_event(cast_cdc_proto_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, cdc_proto_tx_event);
|
||||
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);
|
||||
@@ -39,23 +39,23 @@ static int64_t sensor_value_to_udeg(const struct sensor_value *value)
|
||||
return ((int64_t)value->val1 * 1000000LL) + value->val2;
|
||||
}
|
||||
|
||||
static void submit_detents(int32_t detents)
|
||||
static void submit_detents_batched(int32_t detents)
|
||||
{
|
||||
while (detents != 0) {
|
||||
struct encoder_event *event = new_encoder_event();
|
||||
int8_t event_detents;
|
||||
|
||||
if (detents > INT8_MAX) {
|
||||
event->detents = INT8_MAX;
|
||||
event_detents = INT8_MAX;
|
||||
detents -= INT8_MAX;
|
||||
} else if (detents < INT8_MIN) {
|
||||
event->detents = INT8_MIN;
|
||||
event_detents = INT8_MIN;
|
||||
detents -= INT8_MIN;
|
||||
} else {
|
||||
event->detents = (int8_t)detents;
|
||||
event_detents = (int8_t)detents;
|
||||
detents = 0;
|
||||
}
|
||||
|
||||
APP_EVENT_SUBMIT(event);
|
||||
submit_encoder_event(event_detents);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -89,7 +89,7 @@ static void encoder_report_work_handler(struct k_work *work)
|
||||
angle_remainder_udeg = total_udeg - ((int64_t)detents * ENCODER_DETENT_UDEG);
|
||||
|
||||
if (detents != 0) {
|
||||
submit_detents(detents);
|
||||
submit_detents_batched(detents);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1,73 +0,0 @@
|
||||
#include <ctype.h>
|
||||
#include <stdio.h>
|
||||
|
||||
#include "ble_serial_rx_event.h"
|
||||
|
||||
#define BLE_SERIAL_RX_EVENT_LOG_BUF_LEN 384
|
||||
|
||||
static void log_ble_serial_rx_event(const struct app_event_header *aeh)
|
||||
{
|
||||
const struct ble_serial_rx_event *event = cast_ble_serial_rx_event(aeh);
|
||||
char log_buf[BLE_SERIAL_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_ble_serial_rx_event(struct log_event_buf *buf,
|
||||
const struct app_event_header *aeh)
|
||||
{
|
||||
const struct ble_serial_rx_event *event = cast_ble_serial_rx_event(aeh);
|
||||
|
||||
nrf_profiler_log_encode_uint8(buf, (uint8_t)event->dyndata.size);
|
||||
}
|
||||
|
||||
APP_EVENT_INFO_DEFINE(ble_serial_rx_event,
|
||||
ENCODE(NRF_PROFILER_ARG_U8),
|
||||
ENCODE("len"),
|
||||
profile_ble_serial_rx_event);
|
||||
|
||||
APP_EVENT_TYPE_DEFINE(ble_serial_rx_event,
|
||||
log_ble_serial_rx_event,
|
||||
&ble_serial_rx_event_info,
|
||||
APP_EVENT_FLAGS_CREATE(
|
||||
APP_EVENT_TYPE_FLAGS_INIT_LOG_ENABLE));
|
||||
@@ -1,62 +0,0 @@
|
||||
#include <ctype.h>
|
||||
#include <stdio.h>
|
||||
|
||||
#include "ble_serial_tx_event.h"
|
||||
|
||||
#define BLE_SERIAL_TX_EVENT_LOG_BUF_LEN 256
|
||||
|
||||
static void log_ble_serial_tx_event(const struct app_event_header *aeh)
|
||||
{
|
||||
const struct ble_serial_tx_event *event = cast_ble_serial_tx_event(aeh);
|
||||
char log_buf[BLE_SERIAL_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_ble_serial_tx_event(struct log_event_buf *buf,
|
||||
const struct app_event_header *aeh)
|
||||
{
|
||||
const struct ble_serial_tx_event *event = cast_ble_serial_tx_event(aeh);
|
||||
|
||||
nrf_profiler_log_encode_uint8(buf, (uint8_t)event->dyndata.size);
|
||||
}
|
||||
|
||||
APP_EVENT_INFO_DEFINE(ble_serial_tx_event,
|
||||
ENCODE(NRF_PROFILER_ARG_U8),
|
||||
ENCODE("len"),
|
||||
profile_ble_serial_tx_event);
|
||||
|
||||
APP_EVENT_TYPE_DEFINE(ble_serial_tx_event,
|
||||
log_ble_serial_tx_event,
|
||||
&ble_serial_tx_event_info,
|
||||
APP_EVENT_FLAGS_CREATE(
|
||||
APP_EVENT_TYPE_FLAGS_INIT_LOG_ENABLE));
|
||||
@@ -1,29 +0,0 @@
|
||||
#include "cdc_proto_tx_event.h"
|
||||
|
||||
static void log_cdc_proto_tx_event(const struct app_event_header *aeh)
|
||||
{
|
||||
const struct cdc_proto_tx_event *event = cast_cdc_proto_tx_event(aeh);
|
||||
|
||||
APP_EVENT_MANAGER_LOG(aeh, "type:0x%02x len:%zu",
|
||||
event->type, event->dyndata.size);
|
||||
}
|
||||
|
||||
static void profile_cdc_proto_tx_event(struct log_event_buf *buf,
|
||||
const struct app_event_header *aeh)
|
||||
{
|
||||
const struct cdc_proto_tx_event *event = cast_cdc_proto_tx_event(aeh);
|
||||
|
||||
nrf_profiler_log_encode_uint8(buf, event->type);
|
||||
nrf_profiler_log_encode_uint8(buf, (uint8_t)event->dyndata.size);
|
||||
}
|
||||
|
||||
APP_EVENT_INFO_DEFINE(cdc_proto_tx_event,
|
||||
ENCODE(NRF_PROFILER_ARG_U8, NRF_PROFILER_ARG_U8),
|
||||
ENCODE("type", "len"),
|
||||
profile_cdc_proto_tx_event);
|
||||
|
||||
APP_EVENT_TYPE_DEFINE(cdc_proto_tx_event,
|
||||
log_cdc_proto_tx_event,
|
||||
&cdc_proto_tx_event_info,
|
||||
APP_EVENT_FLAGS_CREATE(
|
||||
APP_EVENT_TYPE_FLAGS_INIT_LOG_ENABLE));
|
||||
27
src/events/function_bitmap_state_event.c
Normal file
27
src/events/function_bitmap_state_event.c
Normal file
@@ -0,0 +1,27 @@
|
||||
#include "function_bitmap_state_event.h"
|
||||
|
||||
static void log_function_bitmap_state_event(const struct app_event_header *aeh)
|
||||
{
|
||||
const struct function_bitmap_state_event *event =
|
||||
cast_function_bitmap_state_event(aeh);
|
||||
|
||||
APP_EVENT_MANAGER_LOG(aeh, "bitmap_len:%zu", sizeof(event->bitmap));
|
||||
}
|
||||
|
||||
static void profile_function_bitmap_state_event(struct log_event_buf *buf,
|
||||
const struct app_event_header *aeh)
|
||||
{
|
||||
ARG_UNUSED(buf);
|
||||
ARG_UNUSED(aeh);
|
||||
}
|
||||
|
||||
APP_EVENT_INFO_DEFINE(function_bitmap_state_event,
|
||||
ENCODE(),
|
||||
ENCODE(),
|
||||
profile_function_bitmap_state_event);
|
||||
|
||||
APP_EVENT_TYPE_DEFINE(function_bitmap_state_event,
|
||||
log_function_bitmap_state_event,
|
||||
&function_bitmap_state_event_info,
|
||||
APP_EVENT_FLAGS_CREATE(
|
||||
APP_EVENT_TYPE_FLAGS_INIT_LOG_ENABLE));
|
||||
@@ -1,43 +0,0 @@
|
||||
#include "key_function_event.h"
|
||||
|
||||
static const char *action_name(uint8_t action)
|
||||
{
|
||||
switch (action) {
|
||||
case KEY_FUNCTION_ACTION_RELEASE:
|
||||
return "release";
|
||||
|
||||
case KEY_FUNCTION_ACTION_PRESS:
|
||||
return "press";
|
||||
|
||||
default:
|
||||
return "unknown";
|
||||
}
|
||||
}
|
||||
|
||||
static void log_key_function_event(const struct app_event_header *aeh)
|
||||
{
|
||||
const struct key_function_event *event = cast_key_function_event(aeh);
|
||||
|
||||
APP_EVENT_MANAGER_LOG(aeh, "usage:0x%04x action:%s",
|
||||
event->usage, action_name(event->action));
|
||||
}
|
||||
|
||||
static void profile_key_function_event(struct log_event_buf *buf,
|
||||
const struct app_event_header *aeh)
|
||||
{
|
||||
const struct key_function_event *event = cast_key_function_event(aeh);
|
||||
|
||||
nrf_profiler_log_encode_uint16(buf, event->usage);
|
||||
nrf_profiler_log_encode_uint8(buf, event->action);
|
||||
}
|
||||
|
||||
APP_EVENT_INFO_DEFINE(key_function_event,
|
||||
ENCODE(NRF_PROFILER_ARG_U16, NRF_PROFILER_ARG_U8),
|
||||
ENCODE("usage", "action"),
|
||||
profile_key_function_event);
|
||||
|
||||
APP_EVENT_TYPE_DEFINE(key_function_event,
|
||||
log_key_function_event,
|
||||
&key_function_event_info,
|
||||
APP_EVENT_FLAGS_CREATE(
|
||||
APP_EVENT_TYPE_FLAGS_INIT_LOG_ENABLE));
|
||||
42
src/events/proto_rx_event.c
Normal file
42
src/events/proto_rx_event.c
Normal file
@@ -0,0 +1,42 @@
|
||||
#include "proto_rx_event.h"
|
||||
|
||||
static const char *transport_name(enum proto_transport transport)
|
||||
{
|
||||
switch (transport) {
|
||||
case PROTO_TRANSPORT_USB_CDC:
|
||||
return "usb_cdc";
|
||||
case PROTO_TRANSPORT_BLE_NUS:
|
||||
return "ble_nus";
|
||||
default:
|
||||
return "?";
|
||||
}
|
||||
}
|
||||
|
||||
static void log_proto_rx_event(const struct app_event_header *aeh)
|
||||
{
|
||||
const struct proto_rx_event *event = cast_proto_rx_event(aeh);
|
||||
|
||||
APP_EVENT_MANAGER_LOG(aeh, "transport:%s len:%zu",
|
||||
transport_name(event->transport),
|
||||
event->dyndata.size);
|
||||
}
|
||||
|
||||
static void profile_proto_rx_event(struct log_event_buf *buf,
|
||||
const struct app_event_header *aeh)
|
||||
{
|
||||
const struct proto_rx_event *event = cast_proto_rx_event(aeh);
|
||||
|
||||
nrf_profiler_log_encode_uint8(buf, event->transport);
|
||||
nrf_profiler_log_encode_uint16(buf, (uint16_t)event->dyndata.size);
|
||||
}
|
||||
|
||||
APP_EVENT_INFO_DEFINE(proto_rx_event,
|
||||
ENCODE(NRF_PROFILER_ARG_U8, NRF_PROFILER_ARG_U16),
|
||||
ENCODE("transport", "len"),
|
||||
profile_proto_rx_event);
|
||||
|
||||
APP_EVENT_TYPE_DEFINE(proto_rx_event,
|
||||
log_proto_rx_event,
|
||||
&proto_rx_event_info,
|
||||
APP_EVENT_FLAGS_CREATE(
|
||||
APP_EVENT_TYPE_FLAGS_INIT_LOG_ENABLE));
|
||||
42
src/events/proto_tx_event.c
Normal file
42
src/events/proto_tx_event.c
Normal file
@@ -0,0 +1,42 @@
|
||||
#include "proto_tx_event.h"
|
||||
|
||||
static const char *transport_name(enum proto_transport transport)
|
||||
{
|
||||
switch (transport) {
|
||||
case PROTO_TRANSPORT_USB_CDC:
|
||||
return "usb_cdc";
|
||||
case PROTO_TRANSPORT_BLE_NUS:
|
||||
return "ble_nus";
|
||||
default:
|
||||
return "?";
|
||||
}
|
||||
}
|
||||
|
||||
static void log_proto_tx_event(const struct app_event_header *aeh)
|
||||
{
|
||||
const struct proto_tx_event *event = cast_proto_tx_event(aeh);
|
||||
|
||||
APP_EVENT_MANAGER_LOG(aeh, "transport:%s len:%zu",
|
||||
transport_name(event->transport),
|
||||
event->dyndata.size);
|
||||
}
|
||||
|
||||
static void profile_proto_tx_event(struct log_event_buf *buf,
|
||||
const struct app_event_header *aeh)
|
||||
{
|
||||
const struct proto_tx_event *event = cast_proto_tx_event(aeh);
|
||||
|
||||
nrf_profiler_log_encode_uint8(buf, event->transport);
|
||||
nrf_profiler_log_encode_uint16(buf, (uint16_t)event->dyndata.size);
|
||||
}
|
||||
|
||||
APP_EVENT_INFO_DEFINE(proto_tx_event,
|
||||
ENCODE(NRF_PROFILER_ARG_U8, NRF_PROFILER_ARG_U16),
|
||||
ENCODE("transport", "len"),
|
||||
profile_proto_tx_event);
|
||||
|
||||
APP_EVENT_TYPE_DEFINE(proto_tx_event,
|
||||
log_proto_tx_event,
|
||||
&proto_tx_event_info,
|
||||
APP_EVENT_FLAGS_CREATE(
|
||||
APP_EVENT_TYPE_FLAGS_INIT_LOG_ENABLE));
|
||||
@@ -1,73 +0,0 @@
|
||||
#include <ctype.h>
|
||||
#include <stdio.h>
|
||||
|
||||
#include "usb_cdc_rx_event.h"
|
||||
|
||||
#define USB_CDC_RX_EVENT_LOG_BUF_LEN 384
|
||||
|
||||
static void log_usb_cdc_rx_event(const struct app_event_header *aeh)
|
||||
{
|
||||
const struct usb_cdc_rx_event *event = cast_usb_cdc_rx_event(aeh);
|
||||
char log_buf[USB_CDC_RX_EVENT_LOG_BUF_LEN];
|
||||
int pos;
|
||||
|
||||
pos = snprintf(log_buf, sizeof(log_buf), "len:%zu ascii:\"",
|
||||
event->dyndata.size);
|
||||
if ((pos < 0) || (pos >= sizeof(log_buf))) {
|
||||
APP_EVENT_MANAGER_LOG(aeh, "log message preparation failure");
|
||||
return;
|
||||
}
|
||||
|
||||
for (size_t i = 0; i < event->dyndata.size; i++) {
|
||||
int tmp = snprintf(&log_buf[pos], sizeof(log_buf) - pos, "%c",
|
||||
isprint(event->dyndata.data[i]) ?
|
||||
event->dyndata.data[i] : '.');
|
||||
|
||||
if ((tmp < 0) || ((pos + tmp) >= sizeof(log_buf))) {
|
||||
APP_EVENT_MANAGER_LOG(aeh, "len:%zu ascii:\"...\"",
|
||||
event->dyndata.size);
|
||||
return;
|
||||
}
|
||||
|
||||
pos += tmp;
|
||||
}
|
||||
|
||||
pos += snprintf(&log_buf[pos], sizeof(log_buf) - pos, "\" hex:");
|
||||
if ((pos < 0) || (pos >= sizeof(log_buf))) {
|
||||
APP_EVENT_MANAGER_LOG(aeh, "len:%zu ascii:\"...\"",
|
||||
event->dyndata.size);
|
||||
return;
|
||||
}
|
||||
|
||||
for (size_t i = 0; i < event->dyndata.size; i++) {
|
||||
int tmp = snprintf(&log_buf[pos], sizeof(log_buf) - pos, " %02x",
|
||||
event->dyndata.data[i]);
|
||||
|
||||
if ((tmp < 0) || ((pos + tmp) >= sizeof(log_buf))) {
|
||||
break;
|
||||
}
|
||||
|
||||
pos += tmp;
|
||||
}
|
||||
|
||||
APP_EVENT_MANAGER_LOG(aeh, "%s", log_buf);
|
||||
}
|
||||
|
||||
static void profile_usb_cdc_rx_event(struct log_event_buf *buf,
|
||||
const struct app_event_header *aeh)
|
||||
{
|
||||
const struct usb_cdc_rx_event *event = cast_usb_cdc_rx_event(aeh);
|
||||
|
||||
nrf_profiler_log_encode_uint8(buf, (uint8_t)event->dyndata.size);
|
||||
}
|
||||
|
||||
APP_EVENT_INFO_DEFINE(usb_cdc_rx_event,
|
||||
ENCODE(NRF_PROFILER_ARG_U8),
|
||||
ENCODE("len"),
|
||||
profile_usb_cdc_rx_event);
|
||||
|
||||
APP_EVENT_TYPE_DEFINE(usb_cdc_rx_event,
|
||||
log_usb_cdc_rx_event,
|
||||
&usb_cdc_rx_event_info,
|
||||
APP_EVENT_FLAGS_CREATE(
|
||||
APP_EVENT_TYPE_FLAGS_INIT_LOG_ENABLE));
|
||||
@@ -1,62 +0,0 @@
|
||||
#include <ctype.h>
|
||||
#include <stdio.h>
|
||||
|
||||
#include "usb_cdc_tx_event.h"
|
||||
|
||||
#define USB_CDC_TX_EVENT_LOG_BUF_LEN 256
|
||||
|
||||
static void log_usb_cdc_tx_event(const struct app_event_header *aeh)
|
||||
{
|
||||
const struct usb_cdc_tx_event *event = cast_usb_cdc_tx_event(aeh);
|
||||
char log_buf[USB_CDC_TX_EVENT_LOG_BUF_LEN];
|
||||
int pos;
|
||||
|
||||
pos = snprintf(log_buf, sizeof(log_buf), "len:%zu ascii:\"",
|
||||
event->dyndata.size);
|
||||
if ((pos < 0) || (pos >= sizeof(log_buf))) {
|
||||
APP_EVENT_MANAGER_LOG(aeh, "log message preparation failure");
|
||||
return;
|
||||
}
|
||||
|
||||
for (size_t i = 0; i < event->dyndata.size; i++) {
|
||||
int tmp = snprintf(&log_buf[pos], sizeof(log_buf) - pos, "%c",
|
||||
isprint(event->dyndata.data[i]) ?
|
||||
event->dyndata.data[i] : '.');
|
||||
|
||||
if ((tmp < 0) || ((pos + tmp) >= sizeof(log_buf))) {
|
||||
APP_EVENT_MANAGER_LOG(aeh, "len:%zu ascii:\"...\"",
|
||||
event->dyndata.size);
|
||||
return;
|
||||
}
|
||||
|
||||
pos += tmp;
|
||||
}
|
||||
|
||||
pos += snprintf(&log_buf[pos], sizeof(log_buf) - pos, "\"");
|
||||
if ((pos < 0) || (pos >= sizeof(log_buf))) {
|
||||
APP_EVENT_MANAGER_LOG(aeh, "len:%zu ascii:\"...\"",
|
||||
event->dyndata.size);
|
||||
return;
|
||||
}
|
||||
|
||||
APP_EVENT_MANAGER_LOG(aeh, "%s", log_buf);
|
||||
}
|
||||
|
||||
static void profile_usb_cdc_tx_event(struct log_event_buf *buf,
|
||||
const struct app_event_header *aeh)
|
||||
{
|
||||
const struct usb_cdc_tx_event *event = cast_usb_cdc_tx_event(aeh);
|
||||
|
||||
nrf_profiler_log_encode_uint8(buf, (uint8_t)event->dyndata.size);
|
||||
}
|
||||
|
||||
APP_EVENT_INFO_DEFINE(usb_cdc_tx_event,
|
||||
ENCODE(NRF_PROFILER_ARG_U8),
|
||||
ENCODE("len"),
|
||||
profile_usb_cdc_tx_event);
|
||||
|
||||
APP_EVENT_TYPE_DEFINE(usb_cdc_tx_event,
|
||||
log_usb_cdc_tx_event,
|
||||
&usb_cdc_tx_event_info,
|
||||
APP_EVENT_FLAGS_CREATE(
|
||||
APP_EVENT_TYPE_FLAGS_INIT_LOG_ENABLE));
|
||||
@@ -1,46 +0,0 @@
|
||||
#include "usb_device_state_event.h"
|
||||
|
||||
static const char *usb_device_state_name(enum usb_device_state state)
|
||||
{
|
||||
switch (state) {
|
||||
case USB_DEVICE_STATE_DISCONNECTED:
|
||||
return "disconnected";
|
||||
case USB_DEVICE_STATE_POWERED:
|
||||
return "powered";
|
||||
case USB_DEVICE_STATE_ACTIVE:
|
||||
return "active";
|
||||
case USB_DEVICE_STATE_SUSPENDED:
|
||||
return "suspended";
|
||||
default:
|
||||
return "?";
|
||||
}
|
||||
}
|
||||
|
||||
static void log_usb_device_state_event(const struct app_event_header *aeh)
|
||||
{
|
||||
const struct usb_device_state_event *event =
|
||||
cast_usb_device_state_event(aeh);
|
||||
|
||||
APP_EVENT_MANAGER_LOG(aeh, "state:%s",
|
||||
usb_device_state_name(event->state));
|
||||
}
|
||||
|
||||
static void profile_usb_device_state_event(struct log_event_buf *buf,
|
||||
const struct app_event_header *aeh)
|
||||
{
|
||||
const struct usb_device_state_event *event =
|
||||
cast_usb_device_state_event(aeh);
|
||||
|
||||
nrf_profiler_log_encode_uint8(buf, event->state);
|
||||
}
|
||||
|
||||
APP_EVENT_INFO_DEFINE(usb_device_state_event,
|
||||
ENCODE(NRF_PROFILER_ARG_U8),
|
||||
ENCODE("state"),
|
||||
profile_usb_device_state_event);
|
||||
|
||||
APP_EVENT_TYPE_DEFINE(usb_device_state_event,
|
||||
log_usb_device_state_event,
|
||||
&usb_device_state_event_info,
|
||||
APP_EVENT_FLAGS_CREATE(
|
||||
APP_EVENT_TYPE_FLAGS_INIT_LOG_ENABLE));
|
||||
@@ -1,42 +0,0 @@
|
||||
#include "usb_function_ready_event.h"
|
||||
|
||||
static const char *usb_function_name(uint8_t function_mask)
|
||||
{
|
||||
switch (function_mask) {
|
||||
case USB_FUNCTION_HID:
|
||||
return "hid";
|
||||
case USB_FUNCTION_CDC_ACM:
|
||||
return "cdc_acm";
|
||||
default:
|
||||
return "?";
|
||||
}
|
||||
}
|
||||
|
||||
static void log_usb_function_ready_event(const struct app_event_header *aeh)
|
||||
{
|
||||
const struct usb_function_ready_event *event =
|
||||
cast_usb_function_ready_event(aeh);
|
||||
|
||||
APP_EVENT_MANAGER_LOG(aeh, "function:%s",
|
||||
usb_function_name(event->function_mask));
|
||||
}
|
||||
|
||||
static void profile_usb_function_ready_event(struct log_event_buf *buf,
|
||||
const struct app_event_header *aeh)
|
||||
{
|
||||
const struct usb_function_ready_event *event =
|
||||
cast_usb_function_ready_event(aeh);
|
||||
|
||||
nrf_profiler_log_encode_uint8(buf, event->function_mask);
|
||||
}
|
||||
|
||||
APP_EVENT_INFO_DEFINE(usb_function_ready_event,
|
||||
ENCODE(NRF_PROFILER_ARG_U8),
|
||||
ENCODE("function_mask"),
|
||||
profile_usb_function_ready_event);
|
||||
|
||||
APP_EVENT_TYPE_DEFINE(usb_function_ready_event,
|
||||
log_usb_function_ready_event,
|
||||
&usb_function_ready_event_info,
|
||||
APP_EVENT_FLAGS_CREATE(
|
||||
APP_EVENT_TYPE_FLAGS_INIT_LOG_ENABLE));
|
||||
@@ -1,24 +0,0 @@
|
||||
#include "usb_prepare_event.h"
|
||||
|
||||
static void log_usb_prepare_event(const struct app_event_header *aeh)
|
||||
{
|
||||
APP_EVENT_MANAGER_LOG(aeh, "prepare");
|
||||
}
|
||||
|
||||
static void profile_usb_prepare_event(struct log_event_buf *buf,
|
||||
const struct app_event_header *aeh)
|
||||
{
|
||||
ARG_UNUSED(buf);
|
||||
ARG_UNUSED(aeh);
|
||||
}
|
||||
|
||||
APP_EVENT_INFO_DEFINE(usb_prepare_event,
|
||||
ENCODE(),
|
||||
ENCODE(),
|
||||
profile_usb_prepare_event);
|
||||
|
||||
APP_EVENT_TYPE_DEFINE(usb_prepare_event,
|
||||
log_usb_prepare_event,
|
||||
&usb_prepare_event_info,
|
||||
APP_EVENT_FLAGS_CREATE(
|
||||
APP_EVENT_TYPE_FLAGS_INIT_LOG_ENABLE));
|
||||
54
src/events/usb_state_event.c
Normal file
54
src/events/usb_state_event.c
Normal file
@@ -0,0 +1,54 @@
|
||||
#include <caf/events/module_state_event.h>
|
||||
|
||||
#include "usb_state_event.h"
|
||||
|
||||
static const char *usb_state_event_op_name(uint8_t op)
|
||||
{
|
||||
switch ((enum usb_state_event_op)op) {
|
||||
case USB_STATE_EVENT_OP_SET_BITS:
|
||||
return "set_bits";
|
||||
case USB_STATE_EVENT_OP_CLEAR_BITS:
|
||||
return "clear_bits";
|
||||
case USB_STATE_EVENT_OP_SNAPSHOT:
|
||||
return "snapshot";
|
||||
default:
|
||||
return "?";
|
||||
}
|
||||
}
|
||||
|
||||
static const char *module_name_or_any(const void *module_id)
|
||||
{
|
||||
return (module_id != NULL) ? module_name_get(module_id) : "*";
|
||||
}
|
||||
|
||||
static void log_usb_state_event(const struct app_event_header *aeh)
|
||||
{
|
||||
const struct usb_state_event *event = cast_usb_state_event(aeh);
|
||||
|
||||
APP_EVENT_MANAGER_LOG(aeh,
|
||||
"src:%s sink:%s op:%s flags:0x%08x",
|
||||
module_name_or_any(event->src_module_id),
|
||||
module_name_or_any(event->sink_module_id),
|
||||
usb_state_event_op_name(event->op),
|
||||
event->flags);
|
||||
}
|
||||
|
||||
static void profile_usb_state_event(struct log_event_buf *buf,
|
||||
const struct app_event_header *aeh)
|
||||
{
|
||||
const struct usb_state_event *event = cast_usb_state_event(aeh);
|
||||
|
||||
nrf_profiler_log_encode_uint8(buf, event->op);
|
||||
nrf_profiler_log_encode_uint32(buf, event->flags);
|
||||
}
|
||||
|
||||
APP_EVENT_INFO_DEFINE(usb_state_event,
|
||||
ENCODE(NRF_PROFILER_ARG_U8, NRF_PROFILER_ARG_U32),
|
||||
ENCODE("op", "flags"),
|
||||
profile_usb_state_event);
|
||||
|
||||
APP_EVENT_TYPE_DEFINE(usb_state_event,
|
||||
log_usb_state_event,
|
||||
&usb_state_event_info,
|
||||
APP_EVENT_FLAGS_CREATE(
|
||||
APP_EVENT_TYPE_FLAGS_INIT_LOG_ENABLE));
|
||||
@@ -153,27 +153,6 @@ static bool transport_can_send_report(enum keyboard_report_type report_type)
|
||||
return state->consumer_ready;
|
||||
}
|
||||
|
||||
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;
|
||||
@@ -195,11 +174,14 @@ static void try_send_next(void)
|
||||
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);
|
||||
in_flight.active = true;
|
||||
in_flight.transport = transport;
|
||||
in_flight.report_type = pending_keys.report_type;
|
||||
in_flight.sequence = next_sequence++;
|
||||
(void)submit_hid_tx_report_event(
|
||||
transport, pending_keys.report_type,
|
||||
pending_keys.protocol_mode, in_flight.sequence,
|
||||
pending_keys.data, pending_keys.size);
|
||||
pending_keys.valid = false;
|
||||
return;
|
||||
}
|
||||
@@ -211,11 +193,14 @@ static void try_send_next(void)
|
||||
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);
|
||||
in_flight.active = true;
|
||||
in_flight.transport = transport;
|
||||
in_flight.report_type = queued.report_type;
|
||||
in_flight.sequence = next_sequence++;
|
||||
(void)submit_hid_tx_report_event(
|
||||
transport, queued.report_type,
|
||||
queued.protocol_mode, in_flight.sequence,
|
||||
queued.data, queued.size);
|
||||
return;
|
||||
}
|
||||
}
|
||||
@@ -226,9 +211,14 @@ static void try_send_next(void)
|
||||
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,
|
||||
in_flight.active = true;
|
||||
in_flight.transport = transport;
|
||||
in_flight.report_type = pending_consumer_latest.report_type;
|
||||
in_flight.sequence = next_sequence++;
|
||||
(void)submit_hid_tx_report_event(
|
||||
transport, pending_consumer_latest.report_type,
|
||||
pending_consumer_latest.protocol_mode,
|
||||
in_flight.sequence,
|
||||
pending_consumer_latest.data,
|
||||
pending_consumer_latest.size);
|
||||
pending_consumer_latest.valid = false;
|
||||
|
||||
@@ -15,10 +15,10 @@
|
||||
#include <zephyr/sys/util.h>
|
||||
|
||||
#include "encoder_event.h"
|
||||
#include "function_bitmap_state_event.h"
|
||||
#include "function_bitmap_update_event.h"
|
||||
#include "keyboard_core.h"
|
||||
#include "keyboard_hid_report_event.h"
|
||||
#include "key_function_event.h"
|
||||
#include "mode_switch_event.h"
|
||||
#include "set_protocol_event.h"
|
||||
|
||||
@@ -146,13 +146,40 @@ static const struct keymap_entry *keymap_get(uint16_t key_id)
|
||||
return NULL;
|
||||
}
|
||||
|
||||
static bool usage_bitmap_test(const uint8_t *bitmap, uint16_t usage_id)
|
||||
static bool usage_to_bitmap_pos(uint16_t usage_id, uint8_t *byte_idx,
|
||||
uint8_t *bit_idx)
|
||||
{
|
||||
if ((bitmap == NULL) || (usage_id > KEYBOARD_PROTOCOL_USAGE_MAX)) {
|
||||
if ((byte_idx == NULL) || (bit_idx == NULL)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return (bitmap[usage_id / 8U] & BIT(usage_id % 8U)) != 0U;
|
||||
if ((usage_id >= KEYBOARD_USAGE_FIRST_MODIFIER) &&
|
||||
(usage_id <= KEYBOARD_USAGE_LAST_MODIFIER)) {
|
||||
*byte_idx = 0U;
|
||||
*bit_idx = (uint8_t)(usage_id - KEYBOARD_USAGE_FIRST_MODIFIER);
|
||||
return true;
|
||||
}
|
||||
|
||||
if (usage_id <= KEYBOARD_NKRO_USAGE_MAX) {
|
||||
*byte_idx = (uint8_t)(1U + (usage_id / 8U));
|
||||
*bit_idx = (uint8_t)(usage_id % 8U);
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
static bool usage_bitmap_test(const uint8_t *bitmap, uint16_t usage_id)
|
||||
{
|
||||
uint8_t byte_idx;
|
||||
uint8_t bit_idx;
|
||||
|
||||
if ((bitmap == NULL) ||
|
||||
!usage_to_bitmap_pos(usage_id, &byte_idx, &bit_idx)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return (bitmap[byte_idx] & BIT(bit_idx)) != 0U;
|
||||
}
|
||||
|
||||
static bool usage_bitmap_write(uint8_t *bitmap, uint16_t usage_id, bool pressed)
|
||||
@@ -161,13 +188,12 @@ static bool usage_bitmap_write(uint8_t *bitmap, uint16_t usage_id, bool pressed)
|
||||
uint8_t bit_idx;
|
||||
bool was_pressed;
|
||||
|
||||
if ((bitmap == NULL) || (usage_id > KEYBOARD_PROTOCOL_USAGE_MAX)) {
|
||||
if ((bitmap == NULL) ||
|
||||
!usage_to_bitmap_pos(usage_id, &byte_idx, &bit_idx)) {
|
||||
LOG_WRN("Unsupported keyboard usage 0x%04x", usage_id);
|
||||
return false;
|
||||
}
|
||||
|
||||
byte_idx = usage_id / 8U;
|
||||
bit_idx = usage_id % 8U;
|
||||
was_pressed = (bitmap[byte_idx] & BIT(bit_idx)) != 0U;
|
||||
|
||||
if (was_pressed == pressed) {
|
||||
@@ -227,12 +253,14 @@ static void build_boot_report(uint8_t report[KEYBOARD_BOOT_REPORT_SIZE])
|
||||
|
||||
build_effective_hid_bitmap(effective_hid_bitmap);
|
||||
memset(report, 0, KEYBOARD_BOOT_REPORT_SIZE);
|
||||
report[0] = effective_hid_bitmap[KEYBOARD_PROTOCOL_BITMAP_BYTES - 1U];
|
||||
report[0] = effective_hid_bitmap[0];
|
||||
report[1] = KEYBOARD_BOOT_RESERVED_BYTE;
|
||||
|
||||
for (uint16_t usage_id = 0; usage_id <= KEYBOARD_NKRO_USAGE_MAX; usage_id++) {
|
||||
uint8_t byte_idx = usage_id / 8U;
|
||||
uint8_t bit_idx = usage_id % 8U;
|
||||
uint8_t byte_idx;
|
||||
uint8_t bit_idx;
|
||||
|
||||
(void)usage_to_bitmap_pos(usage_id, &byte_idx, &bit_idx);
|
||||
|
||||
if ((effective_hid_bitmap[byte_idx] & BIT(bit_idx)) == 0U) {
|
||||
continue;
|
||||
@@ -254,8 +282,8 @@ static void build_nkro_report(uint8_t report[KEYBOARD_NKRO_REPORT_SIZE])
|
||||
uint8_t effective_hid_bitmap[KEYBOARD_PROTOCOL_BITMAP_BYTES];
|
||||
|
||||
build_effective_hid_bitmap(effective_hid_bitmap);
|
||||
report[0] = effective_hid_bitmap[KEYBOARD_PROTOCOL_BITMAP_BYTES - 1U];
|
||||
memcpy(&report[1], effective_hid_bitmap, KEYBOARD_NKRO_BITMAP_BYTES);
|
||||
report[0] = effective_hid_bitmap[0];
|
||||
memcpy(&report[1], &effective_hid_bitmap[1], KEYBOARD_NKRO_BITMAP_BYTES);
|
||||
}
|
||||
|
||||
static uint16_t active_consumer_usage_get(void)
|
||||
@@ -274,32 +302,6 @@ static void build_consumer_report(uint8_t report[KEYBOARD_CONSUMER_REPORT_SIZE])
|
||||
sys_put_le16(active_consumer_usage_get(), report);
|
||||
}
|
||||
|
||||
static void submit_keyboard_report_event(enum keyboard_report_type report_type,
|
||||
enum hid_queue_policy queue_policy,
|
||||
const uint8_t *data, size_t size)
|
||||
{
|
||||
struct keyboard_hid_report_event *event =
|
||||
new_keyboard_hid_report_event(size);
|
||||
enum keyboard_protocol_mode protocol_mode = active_protocol_mode_get();
|
||||
|
||||
event->mode = current_mode;
|
||||
event->report_type = report_type;
|
||||
event->protocol_mode = protocol_mode;
|
||||
event->queue_policy = queue_policy;
|
||||
memcpy(event->dyndata.data, data, size);
|
||||
|
||||
APP_EVENT_SUBMIT(event);
|
||||
}
|
||||
|
||||
static void submit_key_function_event(uint16_t usage_id, uint8_t action)
|
||||
{
|
||||
struct key_function_event *event = new_key_function_event();
|
||||
|
||||
event->usage = usage_id;
|
||||
event->action = action;
|
||||
APP_EVENT_SUBMIT(event);
|
||||
}
|
||||
|
||||
static void submit_consumer_fifo_frame(uint16_t usage_id)
|
||||
{
|
||||
uint8_t report_buf[KEYBOARD_CONSUMER_REPORT_SIZE];
|
||||
@@ -311,10 +313,9 @@ static void submit_consumer_fifo_frame(uint16_t usage_id)
|
||||
}
|
||||
|
||||
sys_put_le16(usage_id, report_buf);
|
||||
submit_keyboard_report_event(KEYBOARD_REPORT_TYPE_CONSUMER,
|
||||
HID_QUEUE_POLICY_FIFO,
|
||||
report_buf,
|
||||
KEYBOARD_CONSUMER_REPORT_SIZE);
|
||||
(void)submit_keyboard_hid_report_event(
|
||||
current_mode, KEYBOARD_REPORT_TYPE_CONSUMER, protocol_mode,
|
||||
HID_QUEUE_POLICY_FIFO, report_buf, KEYBOARD_CONSUMER_REPORT_SIZE);
|
||||
}
|
||||
|
||||
static void submit_consumer_pulse_frames(enum keyboard_consumer_control control_id,
|
||||
@@ -374,10 +375,9 @@ static void emit_keys_report(bool force)
|
||||
memcpy(cache_buf, report_buf, report_size);
|
||||
*cache_valid = true;
|
||||
|
||||
submit_keyboard_report_event(KEYBOARD_REPORT_TYPE_KEYS,
|
||||
HID_QUEUE_POLICY_LATEST,
|
||||
report_buf,
|
||||
report_size);
|
||||
(void)submit_keyboard_hid_report_event(
|
||||
current_mode, KEYBOARD_REPORT_TYPE_KEYS, protocol_mode,
|
||||
HID_QUEUE_POLICY_LATEST, report_buf, report_size);
|
||||
}
|
||||
|
||||
static void emit_consumer_report(bool force)
|
||||
@@ -399,9 +399,9 @@ static void emit_consumer_report(bool force)
|
||||
memcpy(reports_cache.consumer_report, report_buf, KEYBOARD_CONSUMER_REPORT_SIZE);
|
||||
reports_cache.consumer_valid = true;
|
||||
|
||||
submit_keyboard_report_event(KEYBOARD_REPORT_TYPE_CONSUMER,
|
||||
HID_QUEUE_POLICY_LATEST,
|
||||
report_buf,
|
||||
(void)submit_keyboard_hid_report_event(
|
||||
current_mode, KEYBOARD_REPORT_TYPE_CONSUMER, protocol_mode,
|
||||
HID_QUEUE_POLICY_LATEST, report_buf,
|
||||
KEYBOARD_CONSUMER_REPORT_SIZE);
|
||||
}
|
||||
|
||||
@@ -414,20 +414,13 @@ static void emit_all_reports(bool force)
|
||||
}
|
||||
}
|
||||
|
||||
static void emit_function_release_events(void)
|
||||
static void emit_function_state_event(void)
|
||||
{
|
||||
for (uint16_t usage_id = 0; usage_id <= KEYBOARD_PROTOCOL_USAGE_MAX; usage_id++) {
|
||||
if (!usage_bitmap_test(keyboard_state.function_pressed_bitmap, usage_id)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
submit_key_function_event(usage_id, KEY_FUNCTION_ACTION_RELEASE);
|
||||
}
|
||||
(void)submit_function_bitmap_state_event(keyboard_state.pressed_usage_bitmap);
|
||||
}
|
||||
|
||||
static void emit_release_reports(enum mode_switch_mode mode)
|
||||
{
|
||||
struct keyboard_hid_report_event *event;
|
||||
uint8_t keys_report[KEYBOARD_NKRO_REPORT_SIZE] = { 0 };
|
||||
uint8_t consumer_report[KEYBOARD_CONSUMER_REPORT_SIZE] = { 0 };
|
||||
enum keyboard_protocol_mode protocol_mode = active_protocol_mode_get();
|
||||
@@ -435,21 +428,15 @@ static void emit_release_reports(enum mode_switch_mode mode)
|
||||
(protocol_mode == KEYBOARD_PROTOCOL_MODE_BOOT) ?
|
||||
KEYBOARD_BOOT_REPORT_SIZE : KEYBOARD_NKRO_REPORT_SIZE;
|
||||
|
||||
event = new_keyboard_hid_report_event(keys_report_size);
|
||||
event->mode = mode;
|
||||
event->report_type = KEYBOARD_REPORT_TYPE_KEYS;
|
||||
event->protocol_mode = protocol_mode;
|
||||
memcpy(event->dyndata.data, keys_report, keys_report_size);
|
||||
APP_EVENT_SUBMIT(event);
|
||||
(void)submit_keyboard_hid_report_event(
|
||||
mode, KEYBOARD_REPORT_TYPE_KEYS, protocol_mode,
|
||||
HID_QUEUE_POLICY_LATEST, keys_report, keys_report_size);
|
||||
|
||||
if (protocol_mode != KEYBOARD_PROTOCOL_MODE_BOOT) {
|
||||
event = new_keyboard_hid_report_event(KEYBOARD_CONSUMER_REPORT_SIZE);
|
||||
event->mode = mode;
|
||||
event->report_type = KEYBOARD_REPORT_TYPE_CONSUMER;
|
||||
event->protocol_mode = protocol_mode;
|
||||
memcpy(event->dyndata.data, consumer_report,
|
||||
(void)submit_keyboard_hid_report_event(
|
||||
mode, KEYBOARD_REPORT_TYPE_CONSUMER, protocol_mode,
|
||||
HID_QUEUE_POLICY_LATEST, consumer_report,
|
||||
KEYBOARD_CONSUMER_REPORT_SIZE);
|
||||
APP_EVENT_SUBMIT(event);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -486,7 +473,7 @@ static void module_pause(void)
|
||||
if (mode_valid) {
|
||||
emit_release_reports(current_mode);
|
||||
}
|
||||
emit_function_release_events();
|
||||
emit_function_state_event();
|
||||
|
||||
keyboard_state_clear();
|
||||
reports_cache_invalidate();
|
||||
@@ -532,10 +519,7 @@ static bool handle_button_event(const struct button_event *event)
|
||||
}
|
||||
|
||||
if (routed_to_function) {
|
||||
submit_key_function_event(entry->usage_id,
|
||||
event->pressed ?
|
||||
KEY_FUNCTION_ACTION_PRESS :
|
||||
KEY_FUNCTION_ACTION_RELEASE);
|
||||
emit_function_state_event();
|
||||
} else {
|
||||
emit_keys_report(false);
|
||||
}
|
||||
@@ -561,7 +545,7 @@ static bool handle_mode_switch_event(const struct mode_switch_event *event)
|
||||
mode_changed = mode_valid && (current_mode != event->mode);
|
||||
if (mode_changed) {
|
||||
emit_release_reports(current_mode);
|
||||
emit_function_release_events();
|
||||
emit_function_state_event();
|
||||
keyboard_state_clear();
|
||||
reports_cache_invalidate();
|
||||
}
|
||||
|
||||
@@ -91,11 +91,7 @@ static void mode_switch_sample_fn(struct k_work *work)
|
||||
enum mode_switch_mode mode = detect_mode(sample_mv);
|
||||
|
||||
if (force_report || !mode_valid || (mode != last_mode)) {
|
||||
struct mode_switch_event *event = new_mode_switch_event();
|
||||
|
||||
event->mode = mode;
|
||||
event->voltage_mv = sample_mv;
|
||||
APP_EVENT_SUBMIT(event);
|
||||
submit_mode_switch_event(mode, sample_mv);
|
||||
|
||||
last_mode = mode;
|
||||
mode_valid = true;
|
||||
|
||||
@@ -18,14 +18,14 @@
|
||||
|
||||
#include <proto/device_comm.pb.h>
|
||||
|
||||
#include "theme_rgb_update_event.h"
|
||||
#include "time_sync_event.h"
|
||||
#include "cdc_proto_tx_event.h"
|
||||
#include "function_bitmap_state_event.h"
|
||||
#include "function_bitmap_update_event.h"
|
||||
#include "hid_led_event.h"
|
||||
#include "key_function_event.h"
|
||||
#include "proto_rx_event.h"
|
||||
#include "proto_tx_event.h"
|
||||
#include "protocol_module.h"
|
||||
#include "usb_device_state_event.h"
|
||||
#include "theme_rgb_update_event.h"
|
||||
#include "time_sync_event.h"
|
||||
|
||||
LOG_MODULE_REGISTER(MODULE, LOG_LEVEL_INF);
|
||||
|
||||
@@ -34,39 +34,12 @@ LOG_MODULE_REGISTER(MODULE, LOG_LEVEL_INF);
|
||||
#define PROTOCOL_PRODUCT_ID 0x52F0U
|
||||
#define PROTOCOL_FIRMWARE_MAJOR 0U
|
||||
#define PROTOCOL_FIRMWARE_MINOR 0U
|
||||
#define PROTOCOL_CAPABILITY_FLAGS (BIT(0) | BIT(1) | BIT(2) | BIT(3))
|
||||
#define PROTOCOL_CAPABILITY_FLAGS (BIT(0) | BIT(1) | BIT(2) | BIT(3) | BIT(4))
|
||||
#define PROTOCOL_MAX_MSG_LEN 128U
|
||||
|
||||
static bool initialized;
|
||||
static bool running;
|
||||
static bool keyboard_core_ready;
|
||||
static bool usb_active;
|
||||
static bool hello_done;
|
||||
|
||||
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;
|
||||
case CDC_PROTO_TYPE_BITMAP:
|
||||
return body->which_body == CdcPacketBody_bitmap_tag;
|
||||
case CDC_PROTO_TYPE_FUNCTION_KEY_EVENT:
|
||||
return body->which_body == CdcPacketBody_function_key_event_tag;
|
||||
case CDC_PROTO_TYPE_LED_STATE:
|
||||
return body->which_body == CdcPacketBody_led_state_tag;
|
||||
case CDC_PROTO_TYPE_TIME_SYNC:
|
||||
return body->which_body == CdcPacketBody_time_sync_tag;
|
||||
case CDC_PROTO_TYPE_THEME_RGB:
|
||||
return body->which_body == CdcPacketBody_theme_rgb_tag;
|
||||
case CDC_PROTO_TYPE_ACK:
|
||||
return body->which_body == CdcPacketBody_ack_tag;
|
||||
case CDC_PROTO_TYPE_ERROR:
|
||||
return body->which_body == CdcPacketBody_error_tag;
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
}
|
||||
static bool hello_done[PROTO_TRANSPORT_COUNT];
|
||||
|
||||
static int decode_body(const uint8_t *payload, size_t payload_len,
|
||||
CdcPacketBody *body)
|
||||
@@ -147,20 +120,6 @@ static int encode_error(uint8_t error_type, ErrorCode error_code,
|
||||
return encode_body(&body, rsp_payload, rsp_payload_buf_size, rsp_payload_len);
|
||||
}
|
||||
|
||||
static int encode_function_key_event(uint16_t usage, uint8_t action,
|
||||
uint8_t *payload,
|
||||
size_t payload_buf_size,
|
||||
size_t *payload_len)
|
||||
{
|
||||
CdcPacketBody body = CdcPacketBody_init_zero;
|
||||
|
||||
body.which_body = CdcPacketBody_function_key_event_tag;
|
||||
body.body.function_key_event.usage = usage;
|
||||
body.body.function_key_event.action = (KeyAction)action;
|
||||
|
||||
return encode_body(&body, payload, payload_buf_size, payload_len);
|
||||
}
|
||||
|
||||
static int encode_led_state(uint32_t led_mask, uint8_t *payload,
|
||||
size_t payload_buf_size, size_t *payload_len)
|
||||
{
|
||||
@@ -172,120 +131,30 @@ static int encode_led_state(uint32_t led_mask, uint8_t *payload,
|
||||
return encode_body(&body, payload, payload_buf_size, payload_len);
|
||||
}
|
||||
|
||||
static int submit_cdc_proto_tx_event(uint8_t type, const uint8_t *payload,
|
||||
size_t payload_len)
|
||||
static int encode_function_bitmap_state(const uint8_t *bitmap, uint8_t *payload,
|
||||
size_t payload_buf_size,
|
||||
size_t *payload_len)
|
||||
{
|
||||
struct cdc_proto_tx_event *event;
|
||||
CdcPacketBody body = CdcPacketBody_init_zero;
|
||||
|
||||
if ((payload == NULL) && (payload_len > 0U)) {
|
||||
if (bitmap == NULL) {
|
||||
return -EINVAL;
|
||||
}
|
||||
|
||||
event = new_cdc_proto_tx_event(payload_len);
|
||||
event->type = type;
|
||||
if (payload_len > 0U) {
|
||||
memcpy(event->dyndata.data, payload, payload_len);
|
||||
}
|
||||
|
||||
APP_EVENT_SUBMIT(event);
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int submit_function_bitmap_update_event(const Bitmap *bitmap)
|
||||
{
|
||||
struct function_bitmap_update_event *event;
|
||||
|
||||
if ((bitmap == NULL) ||
|
||||
(bitmap->usage_bitmap.size != KEYBOARD_PROTOCOL_BITMAP_BYTES)) {
|
||||
return -EINVAL;
|
||||
}
|
||||
|
||||
event = new_function_bitmap_update_event();
|
||||
memcpy(event->bitmap, bitmap->usage_bitmap.bytes,
|
||||
body.which_body = CdcPacketBody_function_key_event_tag;
|
||||
body.body.function_key_event.usage_bitmap.size = KEYBOARD_PROTOCOL_BITMAP_BYTES;
|
||||
memcpy(body.body.function_key_event.usage_bitmap.bytes, bitmap,
|
||||
KEYBOARD_PROTOCOL_BITMAP_BYTES);
|
||||
APP_EVENT_SUBMIT(event);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int submit_theme_rgb_update_event(const ThemeRgb *theme_rgb)
|
||||
{
|
||||
struct theme_rgb_update_event *event;
|
||||
|
||||
if ((theme_rgb == NULL) ||
|
||||
(theme_rgb->red > 255U) ||
|
||||
(theme_rgb->green > 255U) ||
|
||||
(theme_rgb->blue > 255U)) {
|
||||
return -EINVAL;
|
||||
}
|
||||
|
||||
event = new_theme_rgb_update_event();
|
||||
event->theme.r = (uint8_t)theme_rgb->red;
|
||||
event->theme.g = (uint8_t)theme_rgb->green;
|
||||
event->theme.b = (uint8_t)theme_rgb->blue;
|
||||
APP_EVENT_SUBMIT(event);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int submit_time_sync_event(const TimeSync *time_sync)
|
||||
{
|
||||
struct time_sync_event *event;
|
||||
|
||||
if (time_sync == NULL) {
|
||||
return -EINVAL;
|
||||
}
|
||||
|
||||
event = new_time_sync_event();
|
||||
event->version = time_sync->version;
|
||||
event->flags = time_sync->flags;
|
||||
event->timezone_min = time_sync->timezone_min;
|
||||
event->utc_ms = time_sync->utc_ms;
|
||||
event->accuracy_ms = time_sync->accuracy_ms;
|
||||
APP_EVENT_SUBMIT(event);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int encode_error_response(uint8_t req_type, ErrorCode error_code,
|
||||
uint8_t *rsp_type, uint8_t *rsp_payload,
|
||||
size_t rsp_payload_buf_size,
|
||||
size_t *rsp_payload_len)
|
||||
{
|
||||
int err;
|
||||
|
||||
err = encode_error(req_type, error_code, rsp_payload,
|
||||
rsp_payload_buf_size, rsp_payload_len);
|
||||
if (err) {
|
||||
return err;
|
||||
}
|
||||
|
||||
*rsp_type = CDC_PROTO_TYPE_ERROR;
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int encode_ack_response(uint8_t acked_type, uint8_t *rsp_type,
|
||||
uint8_t *rsp_payload,
|
||||
size_t rsp_payload_buf_size,
|
||||
size_t *rsp_payload_len)
|
||||
{
|
||||
int err;
|
||||
|
||||
err = encode_ack(acked_type, rsp_payload, rsp_payload_buf_size,
|
||||
rsp_payload_len);
|
||||
if (err) {
|
||||
return err;
|
||||
}
|
||||
|
||||
*rsp_type = CDC_PROTO_TYPE_ACK;
|
||||
return 0;
|
||||
return encode_body(&body, payload, payload_buf_size, payload_len);
|
||||
}
|
||||
|
||||
static int module_init(void)
|
||||
{
|
||||
keyboard_core_ready = false;
|
||||
usb_active = false;
|
||||
hello_done = false;
|
||||
for (size_t i = 0; i < ARRAY_SIZE(hello_done); i++) {
|
||||
hello_done[i] = false;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -305,14 +174,25 @@ static void module_pause(void)
|
||||
return;
|
||||
}
|
||||
|
||||
hello_done = false;
|
||||
for (size_t i = 0; i < ARRAY_SIZE(hello_done); i++) {
|
||||
hello_done[i] = false;
|
||||
}
|
||||
|
||||
running = false;
|
||||
}
|
||||
|
||||
int protocol_module_process_cdc_packet(uint8_t req_type,
|
||||
void protocol_module_reset_transport_state(enum proto_transport transport)
|
||||
{
|
||||
if (transport >= PROTO_TRANSPORT_COUNT) {
|
||||
return;
|
||||
}
|
||||
|
||||
hello_done[transport] = false;
|
||||
}
|
||||
|
||||
int protocol_module_process_message(enum proto_transport transport,
|
||||
const uint8_t *req_payload,
|
||||
size_t req_payload_len,
|
||||
uint8_t *rsp_type,
|
||||
uint8_t *rsp_payload,
|
||||
size_t rsp_payload_buf_size,
|
||||
size_t *rsp_payload_len)
|
||||
@@ -320,7 +200,8 @@ int protocol_module_process_cdc_packet(uint8_t req_type,
|
||||
CdcPacketBody body;
|
||||
int err;
|
||||
|
||||
if ((rsp_type == NULL) || (rsp_payload == NULL) || (rsp_payload_len == NULL)) {
|
||||
if ((transport >= PROTO_TRANSPORT_COUNT) ||
|
||||
(rsp_payload == NULL) || (rsp_payload_len == NULL)) {
|
||||
return -EINVAL;
|
||||
}
|
||||
|
||||
@@ -330,160 +211,184 @@ int protocol_module_process_cdc_packet(uint8_t req_type,
|
||||
|
||||
err = decode_body(req_payload, req_payload_len, &body);
|
||||
if (err) {
|
||||
return encode_error_response(req_type, ErrorCode_ERROR_CODE_INVALID_LENGTH,
|
||||
rsp_type, rsp_payload,
|
||||
rsp_payload_buf_size, rsp_payload_len);
|
||||
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 encode_error_response(req_type, ErrorCode_ERROR_CODE_INVALID_PARAM,
|
||||
rsp_type, rsp_payload,
|
||||
rsp_payload_buf_size, rsp_payload_len);
|
||||
}
|
||||
|
||||
switch (req_type) {
|
||||
case CDC_PROTO_TYPE_HELLO_REQ:
|
||||
LOG_INF("HelloReq protocol_version:%u",
|
||||
body.body.hello_req.protocol_version);
|
||||
switch (body.which_body) {
|
||||
case CdcPacketBody_hello_req_tag:
|
||||
LOG_INF("HelloReq transport:%u protocol_version:%u",
|
||||
transport, body.body.hello_req.protocol_version);
|
||||
|
||||
if (body.body.hello_req.protocol_version != PROTOCOL_VERSION) {
|
||||
LOG_WRN("Unexpected protocol version:%u",
|
||||
body.body.hello_req.protocol_version);
|
||||
}
|
||||
|
||||
hello_done = true;
|
||||
err = encode_hello_rsp(rsp_payload, rsp_payload_buf_size, rsp_payload_len);
|
||||
if (err) {
|
||||
return err;
|
||||
}
|
||||
hello_done[transport] = true;
|
||||
return encode_hello_rsp(rsp_payload, rsp_payload_buf_size, rsp_payload_len);
|
||||
|
||||
*rsp_type = CDC_PROTO_TYPE_HELLO_RSP;
|
||||
return 0;
|
||||
|
||||
case CDC_PROTO_TYPE_BITMAP:
|
||||
if (!hello_done) {
|
||||
return encode_error_response(req_type, ErrorCode_ERROR_CODE_NOT_READY,
|
||||
rsp_type, rsp_payload,
|
||||
rsp_payload_buf_size,
|
||||
rsp_payload_len);
|
||||
}
|
||||
|
||||
if (!keyboard_core_ready) {
|
||||
return encode_error_response(req_type, ErrorCode_ERROR_CODE_NOT_READY,
|
||||
rsp_type, rsp_payload,
|
||||
rsp_payload_buf_size,
|
||||
case CdcPacketBody_bitmap_tag:
|
||||
if (!hello_done[transport]) {
|
||||
return encode_error(CdcPacketBody_bitmap_tag,
|
||||
ErrorCode_ERROR_CODE_NOT_READY,
|
||||
rsp_payload, rsp_payload_buf_size,
|
||||
rsp_payload_len);
|
||||
}
|
||||
|
||||
if (body.body.bitmap.usage_bitmap.size != KEYBOARD_PROTOCOL_BITMAP_BYTES) {
|
||||
LOG_WRN("Bitmap len:%u expected:%u",
|
||||
(unsigned int)body.body.bitmap.usage_bitmap.size,
|
||||
KEYBOARD_PROTOCOL_BITMAP_BYTES);
|
||||
return encode_error_response(req_type, ErrorCode_ERROR_CODE_INVALID_LENGTH,
|
||||
rsp_type, rsp_payload,
|
||||
rsp_payload_buf_size,
|
||||
return encode_error(CdcPacketBody_bitmap_tag,
|
||||
ErrorCode_ERROR_CODE_INVALID_LENGTH,
|
||||
rsp_payload, rsp_payload_buf_size,
|
||||
rsp_payload_len);
|
||||
}
|
||||
|
||||
err = submit_function_bitmap_update_event(&body.body.bitmap);
|
||||
err = submit_function_bitmap_update_event(
|
||||
body.body.bitmap.usage_bitmap.bytes);
|
||||
if (err) {
|
||||
return encode_error_response(req_type, ErrorCode_ERROR_CODE_INVALID_PARAM,
|
||||
rsp_type, rsp_payload,
|
||||
rsp_payload_buf_size,
|
||||
return encode_error(CdcPacketBody_bitmap_tag,
|
||||
ErrorCode_ERROR_CODE_INVALID_PARAM,
|
||||
rsp_payload, rsp_payload_buf_size,
|
||||
rsp_payload_len);
|
||||
}
|
||||
|
||||
return encode_ack_response(req_type, rsp_type, rsp_payload,
|
||||
return encode_ack(CdcPacketBody_bitmap_tag, rsp_payload,
|
||||
rsp_payload_buf_size, rsp_payload_len);
|
||||
|
||||
case CDC_PROTO_TYPE_TIME_SYNC:
|
||||
if (!hello_done) {
|
||||
return encode_error_response(req_type, ErrorCode_ERROR_CODE_NOT_READY,
|
||||
rsp_type, rsp_payload,
|
||||
rsp_payload_buf_size,
|
||||
case CdcPacketBody_time_sync_tag:
|
||||
if (!hello_done[transport]) {
|
||||
return encode_error(CdcPacketBody_time_sync_tag,
|
||||
ErrorCode_ERROR_CODE_NOT_READY,
|
||||
rsp_payload, rsp_payload_buf_size,
|
||||
rsp_payload_len);
|
||||
}
|
||||
|
||||
if (body.body.time_sync.version != 1U) {
|
||||
return encode_error_response(req_type, ErrorCode_ERROR_CODE_INVALID_PARAM,
|
||||
rsp_type, rsp_payload,
|
||||
rsp_payload_buf_size,
|
||||
return encode_error(CdcPacketBody_time_sync_tag,
|
||||
ErrorCode_ERROR_CODE_INVALID_PARAM,
|
||||
rsp_payload, rsp_payload_buf_size,
|
||||
rsp_payload_len);
|
||||
}
|
||||
|
||||
err = submit_time_sync_event(&body.body.time_sync);
|
||||
if (err) {
|
||||
return encode_error_response(req_type, ErrorCode_ERROR_CODE_INVALID_PARAM,
|
||||
rsp_type, rsp_payload,
|
||||
rsp_payload_buf_size,
|
||||
rsp_payload_len);
|
||||
}
|
||||
|
||||
return encode_ack_response(req_type, rsp_type, rsp_payload,
|
||||
submit_time_sync_event(body.body.time_sync.version,
|
||||
body.body.time_sync.flags,
|
||||
body.body.time_sync.timezone_min,
|
||||
body.body.time_sync.utc_ms,
|
||||
body.body.time_sync.accuracy_ms);
|
||||
return encode_ack(CdcPacketBody_time_sync_tag, rsp_payload,
|
||||
rsp_payload_buf_size, rsp_payload_len);
|
||||
|
||||
case CDC_PROTO_TYPE_THEME_RGB:
|
||||
if (!hello_done) {
|
||||
return encode_error_response(req_type, ErrorCode_ERROR_CODE_NOT_READY,
|
||||
rsp_type, rsp_payload,
|
||||
rsp_payload_buf_size,
|
||||
case CdcPacketBody_theme_rgb_tag:
|
||||
if (!hello_done[transport]) {
|
||||
return encode_error(CdcPacketBody_theme_rgb_tag,
|
||||
ErrorCode_ERROR_CODE_NOT_READY,
|
||||
rsp_payload, rsp_payload_buf_size,
|
||||
rsp_payload_len);
|
||||
}
|
||||
|
||||
err = submit_theme_rgb_update_event(&body.body.theme_rgb);
|
||||
if (err) {
|
||||
return encode_error_response(req_type, ErrorCode_ERROR_CODE_INVALID_PARAM,
|
||||
rsp_type, rsp_payload,
|
||||
rsp_payload_buf_size,
|
||||
if ((body.body.theme_rgb.red > 255U) ||
|
||||
(body.body.theme_rgb.green > 255U) ||
|
||||
(body.body.theme_rgb.blue > 255U)) {
|
||||
return encode_error(CdcPacketBody_theme_rgb_tag,
|
||||
ErrorCode_ERROR_CODE_INVALID_PARAM,
|
||||
rsp_payload, rsp_payload_buf_size,
|
||||
rsp_payload_len);
|
||||
}
|
||||
|
||||
return encode_ack_response(req_type, rsp_type, rsp_payload,
|
||||
submit_theme_rgb_update_event((struct theme_rgb) {
|
||||
.r = (uint8_t)body.body.theme_rgb.red,
|
||||
.g = (uint8_t)body.body.theme_rgb.green,
|
||||
.b = (uint8_t)body.body.theme_rgb.blue,
|
||||
});
|
||||
return encode_ack(CdcPacketBody_theme_rgb_tag, rsp_payload,
|
||||
rsp_payload_buf_size, rsp_payload_len);
|
||||
|
||||
default:
|
||||
LOG_WRN("Unsupported CDC protocol type:0x%02x", req_type);
|
||||
return encode_error_response(req_type, ErrorCode_ERROR_CODE_UNKNOWN_TYPE,
|
||||
rsp_type, rsp_payload,
|
||||
rsp_payload_buf_size, rsp_payload_len);
|
||||
LOG_WRN("Unsupported protobuf body case %d", body.which_body);
|
||||
return -ENOTSUP;
|
||||
}
|
||||
}
|
||||
|
||||
static bool handle_key_function_event(const struct key_function_event *event)
|
||||
static bool handle_proto_rx_event(const struct proto_rx_event *event)
|
||||
{
|
||||
uint8_t payload[64];
|
||||
size_t payload_len;
|
||||
uint8_t rsp_payload[PROTOCOL_MAX_MSG_LEN];
|
||||
size_t rsp_payload_len = 0U;
|
||||
int err;
|
||||
|
||||
if (!running || !usb_active || !hello_done) {
|
||||
if (!running) {
|
||||
return false;
|
||||
}
|
||||
|
||||
err = encode_function_key_event(event->usage, event->action, payload,
|
||||
err = protocol_module_process_message(event->transport,
|
||||
event->dyndata.data,
|
||||
event->dyndata.size,
|
||||
rsp_payload,
|
||||
sizeof(rsp_payload),
|
||||
&rsp_payload_len);
|
||||
if (err) {
|
||||
if (err != -ENOTSUP) {
|
||||
LOG_WRN("Protocol processing failed (%d)", err);
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
err = submit_proto_tx_event(event->transport, rsp_payload, rsp_payload_len);
|
||||
if (err) {
|
||||
LOG_WRN("Proto TX submit failed (%d)", err);
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
static bool handle_function_bitmap_state_event(
|
||||
const struct function_bitmap_state_event *event)
|
||||
{
|
||||
uint8_t payload[PROTOCOL_MAX_MSG_LEN];
|
||||
size_t payload_len;
|
||||
int err;
|
||||
|
||||
if (!running) {
|
||||
return false;
|
||||
}
|
||||
|
||||
for (enum proto_transport transport = 0; transport < PROTO_TRANSPORT_COUNT;
|
||||
transport++) {
|
||||
if (!hello_done[transport]) {
|
||||
continue;
|
||||
}
|
||||
|
||||
err = encode_function_bitmap_state(event->bitmap, payload,
|
||||
sizeof(payload), &payload_len);
|
||||
if (err) {
|
||||
LOG_WRN("FunctionKeyEvent encode failed (%d)", err);
|
||||
return false;
|
||||
}
|
||||
|
||||
err = submit_cdc_proto_tx_event(CDC_PROTO_TYPE_FUNCTION_KEY_EVENT,
|
||||
payload, payload_len);
|
||||
err = submit_proto_tx_event(transport, payload, payload_len);
|
||||
if (err) {
|
||||
LOG_WRN("FunctionKeyEvent submit failed (%d)", err);
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
static bool handle_hid_led_event(const struct hid_led_event *event)
|
||||
{
|
||||
uint8_t payload[64];
|
||||
uint8_t payload[PROTOCOL_MAX_MSG_LEN];
|
||||
size_t payload_len;
|
||||
int err;
|
||||
enum proto_transport transport;
|
||||
|
||||
if (!running || !usb_active || !hello_done) {
|
||||
if (!running) {
|
||||
return false;
|
||||
}
|
||||
|
||||
transport = (event->transport == HID_TRANSPORT_USB) ?
|
||||
PROTO_TRANSPORT_USB_CDC :
|
||||
PROTO_TRANSPORT_BLE_NUS;
|
||||
|
||||
if (!hello_done[transport]) {
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -493,7 +398,7 @@ static bool handle_hid_led_event(const struct hid_led_event *event)
|
||||
return false;
|
||||
}
|
||||
|
||||
err = submit_cdc_proto_tx_event(CDC_PROTO_TYPE_LED_STATE, payload, payload_len);
|
||||
err = submit_proto_tx_event(transport, payload, payload_len);
|
||||
if (err) {
|
||||
LOG_WRN("LedState submit failed (%d)", err);
|
||||
}
|
||||
@@ -501,30 +406,21 @@ static bool handle_hid_led_event(const struct hid_led_event *event)
|
||||
return false;
|
||||
}
|
||||
|
||||
static bool handle_usb_device_state_event(const struct usb_device_state_event *event)
|
||||
{
|
||||
usb_active = (event->state == USB_DEVICE_STATE_ACTIVE);
|
||||
if (!usb_active) {
|
||||
hello_done = false;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
static bool app_event_handler(const struct app_event_header *aeh)
|
||||
{
|
||||
if (is_key_function_event(aeh)) {
|
||||
return handle_key_function_event(cast_key_function_event(aeh));
|
||||
if (is_proto_rx_event(aeh)) {
|
||||
return handle_proto_rx_event(cast_proto_rx_event(aeh));
|
||||
}
|
||||
|
||||
if (is_function_bitmap_state_event(aeh)) {
|
||||
return handle_function_bitmap_state_event(
|
||||
cast_function_bitmap_state_event(aeh));
|
||||
}
|
||||
|
||||
if (is_hid_led_event(aeh)) {
|
||||
return handle_hid_led_event(cast_hid_led_event(aeh));
|
||||
}
|
||||
|
||||
if (is_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);
|
||||
int err;
|
||||
@@ -550,11 +446,6 @@ static bool app_event_handler(const struct app_event_header *aeh)
|
||||
return false;
|
||||
}
|
||||
|
||||
if (check_state(event, MODULE_ID(keyboard_core_module), MODULE_STATE_READY)) {
|
||||
keyboard_core_ready = true;
|
||||
return false;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -585,9 +476,9 @@ static bool app_event_handler(const struct app_event_header *aeh)
|
||||
}
|
||||
|
||||
APP_EVENT_LISTENER(MODULE, app_event_handler);
|
||||
APP_EVENT_SUBSCRIBE(MODULE, function_bitmap_state_event);
|
||||
APP_EVENT_SUBSCRIBE(MODULE, hid_led_event);
|
||||
APP_EVENT_SUBSCRIBE(MODULE, key_function_event);
|
||||
APP_EVENT_SUBSCRIBE(MODULE, module_state_event);
|
||||
APP_EVENT_SUBSCRIBE(MODULE, usb_device_state_event);
|
||||
APP_EVENT_SUBSCRIBE(MODULE, proto_rx_event);
|
||||
APP_EVENT_SUBSCRIBE_EARLY(MODULE, power_down_event);
|
||||
APP_EVENT_SUBSCRIBE(MODULE, wake_up_event);
|
||||
|
||||
@@ -26,12 +26,13 @@ static int32_t timezone_min;
|
||||
static uint64_t utc_ms_base;
|
||||
static int64_t uptime_ms_base;
|
||||
|
||||
static void submit_datetime_event(void)
|
||||
static void publish_datetime_event(void)
|
||||
{
|
||||
struct datetime_event *event = new_datetime_event();
|
||||
struct tm tm_buf;
|
||||
time_t seconds;
|
||||
int64_t local_ms;
|
||||
char date_text[DATETIME_EVENT_DATE_TEXT_LEN];
|
||||
char time_text[DATETIME_EVENT_TIME_TEXT_LEN];
|
||||
|
||||
local_ms = (int64_t)utc_ms_base +
|
||||
(k_uptime_get() - uptime_ms_base) +
|
||||
@@ -42,22 +43,24 @@ static void submit_datetime_event(void)
|
||||
|
||||
seconds = (time_t)(local_ms / 1000LL);
|
||||
if (gmtime_r(&seconds, &tm_buf) == NULL) {
|
||||
strncpy(event->date_text, "1970/01/01", sizeof(event->date_text));
|
||||
strncpy(event->time_text, "00:00:00", sizeof(event->time_text));
|
||||
strncpy(date_text, "1970/01/01", sizeof(date_text));
|
||||
date_text[sizeof(date_text) - 1] = '\0';
|
||||
strncpy(time_text, "00:00:00", sizeof(time_text));
|
||||
time_text[sizeof(time_text) - 1] = '\0';
|
||||
} else {
|
||||
snprintk(event->date_text, sizeof(event->date_text),
|
||||
snprintk(date_text, sizeof(date_text),
|
||||
"%04u/%02u/%02u",
|
||||
(unsigned int)(tm_buf.tm_year + 1900),
|
||||
(unsigned int)(tm_buf.tm_mon + 1),
|
||||
(unsigned int)tm_buf.tm_mday);
|
||||
snprintk(event->time_text, sizeof(event->time_text),
|
||||
snprintk(time_text, sizeof(time_text),
|
||||
"%02u:%02u:%02u",
|
||||
(unsigned int)tm_buf.tm_hour,
|
||||
(unsigned int)tm_buf.tm_min,
|
||||
(unsigned int)tm_buf.tm_sec);
|
||||
}
|
||||
|
||||
APP_EVENT_SUBMIT(event);
|
||||
(void)submit_datetime_event(date_text, time_text);
|
||||
}
|
||||
|
||||
static void refresh_work_handler(struct k_work *work)
|
||||
@@ -68,7 +71,7 @@ static void refresh_work_handler(struct k_work *work)
|
||||
return;
|
||||
}
|
||||
|
||||
submit_datetime_event();
|
||||
publish_datetime_event();
|
||||
k_work_reschedule(&refresh_work, TIME_SYNC_REFRESH_PERIOD);
|
||||
}
|
||||
|
||||
|
||||
@@ -207,8 +207,6 @@ void ui_main_init(const struct ui_main_model *model,
|
||||
|
||||
lv_obj_clean(screen);
|
||||
lv_obj_set_style_bg_color(screen, lv_color_hex(0x0F1115), 0);
|
||||
lv_obj_set_style_bg_grad_color(screen, lv_color_hex(0x1A1F29), 0);
|
||||
lv_obj_set_style_bg_grad_dir(screen, LV_GRAD_DIR_VER, 0);
|
||||
lv_obj_set_style_bg_opa(screen, LV_OPA_COVER, 0);
|
||||
lv_obj_set_style_text_color(screen, lv_color_white(), 0);
|
||||
lv_obj_set_style_pad_all(screen, 0, 0);
|
||||
|
||||
@@ -16,19 +16,19 @@
|
||||
#include <zephyr/sys/ring_buffer.h>
|
||||
#include <zephyr/sys/util.h>
|
||||
|
||||
#include "usb_cdc_rx_event.h"
|
||||
#include "usb_cdc_tx_event.h"
|
||||
#include "usb_function_ready_event.h"
|
||||
#include "usb_prepare_event.h"
|
||||
#include "usb_device_module.h"
|
||||
#include "usb_device_state_event.h"
|
||||
#include "proto_rx_event.h"
|
||||
#include "proto_tx_event.h"
|
||||
#include "protocol_module.h"
|
||||
#include "usb_state_event.h"
|
||||
|
||||
LOG_MODULE_REGISTER(MODULE, LOG_LEVEL_INF);
|
||||
|
||||
#define USB_CDC_RX_RING_BUF_SIZE 256
|
||||
#define USB_CDC_TX_RING_BUF_SIZE 256
|
||||
#define USB_CDC_RX_CHUNK_SIZE 32
|
||||
#define USB_CDC_PROTO_RX_BUF_SIZE 128
|
||||
#define USB_CDC_CONTROL_POLL_INTERVAL K_MSEC(100)
|
||||
#define USB_CDC_PROTO_RX_FLUSH_DELAY K_MSEC(3)
|
||||
#define USB_CDC_EXPECTED_BAUDRATE 115200U
|
||||
|
||||
static const struct device *const cdc_dev = DEVICE_DT_GET_ONE(zephyr_cdc_acm_uart);
|
||||
@@ -39,27 +39,21 @@ static struct ring_buf rx_ringbuf;
|
||||
static struct ring_buf tx_ringbuf;
|
||||
static struct k_work rx_work;
|
||||
static struct k_work_delayable control_work;
|
||||
static struct k_work_delayable rx_flush_work;
|
||||
static bool initialized;
|
||||
static bool running;
|
||||
static bool usb_active;
|
||||
static bool usb_function_prepared;
|
||||
static bool dtr_ready;
|
||||
static bool rx_enabled;
|
||||
static uint8_t proto_rx_buf[USB_CDC_PROTO_RX_BUF_SIZE];
|
||||
static size_t proto_rx_len;
|
||||
|
||||
static void submit_usb_cdc_rx_event(const uint8_t *data, size_t len)
|
||||
static bool is_usb_owner_snapshot(const struct usb_state_event *event)
|
||||
{
|
||||
struct usb_cdc_rx_event *event = new_usb_cdc_rx_event(len);
|
||||
|
||||
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);
|
||||
return (event->op == USB_STATE_EVENT_OP_SNAPSHOT) &&
|
||||
(event->src_module_id == MODULE_ID(usb_device_module)) &&
|
||||
(event->sink_module_id == NULL);
|
||||
}
|
||||
|
||||
static void reset_ring_buffers(void)
|
||||
@@ -78,6 +72,8 @@ static void disable_uart_io(void)
|
||||
uart_irq_tx_disable(cdc_dev);
|
||||
rx_enabled = false;
|
||||
dtr_ready = false;
|
||||
proto_rx_len = 0U;
|
||||
k_work_cancel_delayable(&rx_flush_work);
|
||||
reset_ring_buffers();
|
||||
}
|
||||
|
||||
@@ -145,8 +141,30 @@ static void rx_work_handler(struct k_work *work)
|
||||
return;
|
||||
}
|
||||
|
||||
submit_usb_cdc_rx_event(buffer, len);
|
||||
if ((proto_rx_len + len) > sizeof(proto_rx_buf)) {
|
||||
LOG_WRN("Drop oversized CDC protobuf message len:%u",
|
||||
(uint32_t)(proto_rx_len + len));
|
||||
proto_rx_len = 0U;
|
||||
}
|
||||
|
||||
if (len > 0U) {
|
||||
memcpy(&proto_rx_buf[proto_rx_len], buffer, len);
|
||||
proto_rx_len += len;
|
||||
k_work_reschedule(&rx_flush_work, USB_CDC_PROTO_RX_FLUSH_DELAY);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static void rx_flush_work_handler(struct k_work *work)
|
||||
{
|
||||
ARG_UNUSED(work);
|
||||
|
||||
if (!running || !usb_active || !dtr_ready || (proto_rx_len == 0U)) {
|
||||
return;
|
||||
}
|
||||
|
||||
(void)submit_proto_rx_event(PROTO_TRANSPORT_USB_CDC, proto_rx_buf, proto_rx_len);
|
||||
proto_rx_len = 0U;
|
||||
}
|
||||
|
||||
static void control_work_handler(struct k_work *work)
|
||||
@@ -262,8 +280,10 @@ static int module_init(void)
|
||||
reset_ring_buffers();
|
||||
k_work_init(&rx_work, rx_work_handler);
|
||||
k_work_init_delayable(&control_work, control_work_handler);
|
||||
k_work_init_delayable(&rx_flush_work, rx_flush_work_handler);
|
||||
uart_irq_callback_set(cdc_dev, cdc_interrupt_handler);
|
||||
usb_function_prepared = false;
|
||||
proto_rx_len = 0U;
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -288,26 +308,26 @@ static void module_pause(void)
|
||||
}
|
||||
|
||||
k_work_cancel_delayable(&control_work);
|
||||
k_work_cancel_delayable(&rx_flush_work);
|
||||
disable_uart_io();
|
||||
running = false;
|
||||
}
|
||||
|
||||
static bool handle_usb_prepare_event(const struct usb_prepare_event *event)
|
||||
static bool handle_usb_state_event(const struct usb_state_event *event)
|
||||
{
|
||||
ARG_UNUSED(event);
|
||||
|
||||
if (!running || usb_function_prepared) {
|
||||
if (!is_usb_owner_snapshot(event)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (running && !usb_function_prepared &&
|
||||
((event->flags & USB_STATEF_PREPARE) != 0U)) {
|
||||
usb_function_prepared = true;
|
||||
submit_usb_function_ready_event();
|
||||
return false;
|
||||
}
|
||||
submit_usb_state_set(MODULE_ID(MODULE),
|
||||
MODULE_ID(usb_device_module),
|
||||
USB_STATEF_CDC_READY);
|
||||
}
|
||||
|
||||
static bool handle_usb_device_state_event(const struct usb_device_state_event *event)
|
||||
{
|
||||
bool new_usb_active = (event->state == USB_DEVICE_STATE_ACTIVE);
|
||||
bool new_usb_active = (event->flags & USB_STATEF_ACTIVE) != 0U;
|
||||
|
||||
if (new_usb_active == usb_active) {
|
||||
return false;
|
||||
@@ -317,6 +337,8 @@ static bool handle_usb_device_state_event(const struct usb_device_state_event *e
|
||||
|
||||
if (!usb_active) {
|
||||
k_work_cancel_delayable(&control_work);
|
||||
k_work_cancel_delayable(&rx_flush_work);
|
||||
protocol_module_reset_transport_state(PROTO_TRANSPORT_USB_CDC);
|
||||
disable_uart_io();
|
||||
} else if (running) {
|
||||
k_work_reschedule(&control_work, K_NO_WAIT);
|
||||
@@ -325,11 +347,15 @@ static bool handle_usb_device_state_event(const struct usb_device_state_event *e
|
||||
return false;
|
||||
}
|
||||
|
||||
static bool handle_usb_cdc_tx_event(const struct usb_cdc_tx_event *event)
|
||||
static bool handle_proto_tx_event(const struct proto_tx_event *event)
|
||||
{
|
||||
uint32_t written;
|
||||
unsigned int key;
|
||||
|
||||
if (event->transport != PROTO_TRANSPORT_USB_CDC) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!running || !usb_active || !dtr_ready) {
|
||||
return false;
|
||||
}
|
||||
@@ -352,16 +378,12 @@ static bool handle_usb_cdc_tx_event(const struct usb_cdc_tx_event *event)
|
||||
|
||||
static bool app_event_handler(const struct app_event_header *aeh)
|
||||
{
|
||||
if (is_usb_device_state_event(aeh)) {
|
||||
return handle_usb_device_state_event(cast_usb_device_state_event(aeh));
|
||||
if (is_usb_state_event(aeh)) {
|
||||
return handle_usb_state_event(cast_usb_state_event(aeh));
|
||||
}
|
||||
|
||||
if (is_usb_prepare_event(aeh)) {
|
||||
return handle_usb_prepare_event(cast_usb_prepare_event(aeh));
|
||||
}
|
||||
|
||||
if (is_usb_cdc_tx_event(aeh)) {
|
||||
return handle_usb_cdc_tx_event(cast_usb_cdc_tx_event(aeh));
|
||||
if (is_proto_tx_event(aeh)) {
|
||||
return handle_proto_tx_event(cast_proto_tx_event(aeh));
|
||||
}
|
||||
|
||||
if (is_module_state_event(aeh)) {
|
||||
@@ -419,8 +441,7 @@ static bool app_event_handler(const struct app_event_header *aeh)
|
||||
|
||||
APP_EVENT_LISTENER(MODULE, app_event_handler);
|
||||
APP_EVENT_SUBSCRIBE(MODULE, module_state_event);
|
||||
APP_EVENT_SUBSCRIBE(MODULE, usb_prepare_event);
|
||||
APP_EVENT_SUBSCRIBE(MODULE, usb_device_state_event);
|
||||
APP_EVENT_SUBSCRIBE(MODULE, usb_cdc_tx_event);
|
||||
APP_EVENT_SUBSCRIBE(MODULE, proto_tx_event);
|
||||
APP_EVENT_SUBSCRIBE(MODULE, usb_state_event);
|
||||
APP_EVENT_SUBSCRIBE_EARLY(MODULE, power_down_event);
|
||||
APP_EVENT_SUBSCRIBE(MODULE, wake_up_event);
|
||||
|
||||
@@ -1,230 +0,0 @@
|
||||
#include <stdbool.h>
|
||||
#include <string.h>
|
||||
|
||||
#include <app_event_manager.h>
|
||||
|
||||
#define MODULE usb_cdc_test_module
|
||||
#include <caf/events/module_state_event.h>
|
||||
#include <caf/events/power_event.h>
|
||||
|
||||
#include <caf/events/ble_common_event.h>
|
||||
|
||||
#include <zephyr/kernel.h>
|
||||
#include <zephyr/logging/log.h>
|
||||
|
||||
#include "ble_serial_tx_event.h"
|
||||
#include "usb_cdc_tx_event.h"
|
||||
#include "usb_device_state_event.h"
|
||||
|
||||
LOG_MODULE_REGISTER(MODULE, LOG_LEVEL_INF);
|
||||
|
||||
#define USB_CDC_TEST_PERIOD K_SECONDS(1)
|
||||
|
||||
static const uint8_t hello_message[] = "hello\r\n";
|
||||
static const uint8_t ble_hello_message[] = "ble_hello\r\n";
|
||||
|
||||
static struct k_work_delayable hello_work;
|
||||
static bool initialized;
|
||||
static bool running;
|
||||
static bool usb_active;
|
||||
static bool ble_active;
|
||||
|
||||
static void submit_hello_message(void)
|
||||
{
|
||||
struct usb_cdc_tx_event *event =
|
||||
new_usb_cdc_tx_event(sizeof(hello_message) - 1U);
|
||||
|
||||
memcpy(event->dyndata.data, hello_message, sizeof(hello_message) - 1U);
|
||||
APP_EVENT_SUBMIT(event);
|
||||
}
|
||||
|
||||
static void submit_ble_hello_message(void)
|
||||
{
|
||||
struct ble_serial_tx_event *event =
|
||||
new_ble_serial_tx_event(sizeof(ble_hello_message) - 1U);
|
||||
|
||||
memcpy(event->dyndata.data, ble_hello_message, sizeof(ble_hello_message) - 1U);
|
||||
APP_EVENT_SUBMIT(event);
|
||||
}
|
||||
|
||||
static void hello_work_handler(struct k_work *work)
|
||||
{
|
||||
ARG_UNUSED(work);
|
||||
|
||||
if (!running) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (usb_active) {
|
||||
submit_hello_message();
|
||||
}
|
||||
|
||||
if (ble_active) {
|
||||
submit_ble_hello_message();
|
||||
}
|
||||
|
||||
k_work_reschedule(&hello_work, USB_CDC_TEST_PERIOD);
|
||||
}
|
||||
|
||||
static int module_init(void)
|
||||
{
|
||||
k_work_init_delayable(&hello_work, hello_work_handler);
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int module_start(void)
|
||||
{
|
||||
if (running) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
running = true;
|
||||
if (usb_active || ble_active) {
|
||||
k_work_reschedule(&hello_work, USB_CDC_TEST_PERIOD);
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static void module_pause(void)
|
||||
{
|
||||
if (!running) {
|
||||
return;
|
||||
}
|
||||
|
||||
k_work_cancel_delayable(&hello_work);
|
||||
running = false;
|
||||
}
|
||||
|
||||
static bool handle_usb_device_state_event(const struct usb_device_state_event *event)
|
||||
{
|
||||
bool new_usb_active = (event->state == USB_DEVICE_STATE_ACTIVE);
|
||||
|
||||
if (new_usb_active == usb_active) {
|
||||
return false;
|
||||
}
|
||||
|
||||
usb_active = new_usb_active;
|
||||
|
||||
if (!running) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (usb_active) {
|
||||
k_work_reschedule(&hello_work, USB_CDC_TEST_PERIOD);
|
||||
} else {
|
||||
k_work_cancel_delayable(&hello_work);
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
static bool handle_ble_peer_event(const struct ble_peer_event *event)
|
||||
{
|
||||
bool new_ble_active = ble_active;
|
||||
|
||||
switch (event->state) {
|
||||
case PEER_STATE_CONNECTED:
|
||||
new_ble_active = false;
|
||||
break;
|
||||
|
||||
case PEER_STATE_SECURED:
|
||||
new_ble_active = true;
|
||||
break;
|
||||
|
||||
case PEER_STATE_DISCONNECTED:
|
||||
new_ble_active = false;
|
||||
break;
|
||||
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
|
||||
if (new_ble_active == ble_active) {
|
||||
return false;
|
||||
}
|
||||
|
||||
ble_active = new_ble_active;
|
||||
|
||||
if (!running) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (usb_active || ble_active) {
|
||||
k_work_reschedule(&hello_work, USB_CDC_TEST_PERIOD);
|
||||
} else {
|
||||
k_work_cancel_delayable(&hello_work);
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
static bool app_event_handler(const struct app_event_header *aeh)
|
||||
{
|
||||
if (is_usb_device_state_event(aeh)) {
|
||||
return handle_usb_device_state_event(cast_usb_device_state_event(aeh));
|
||||
}
|
||||
|
||||
if (is_ble_peer_event(aeh)) {
|
||||
return handle_ble_peer_event(cast_ble_peer_event(aeh));
|
||||
}
|
||||
|
||||
if (is_module_state_event(aeh)) {
|
||||
const struct module_state_event *event = cast_module_state_event(aeh);
|
||||
|
||||
if (check_state(event, MODULE_ID(main), MODULE_STATE_READY)) {
|
||||
int err;
|
||||
|
||||
if (!initialized) {
|
||||
err = module_init();
|
||||
if (err) {
|
||||
module_set_state(MODULE_STATE_ERROR);
|
||||
return false;
|
||||
}
|
||||
|
||||
initialized = true;
|
||||
}
|
||||
|
||||
err = module_start();
|
||||
if (err) {
|
||||
module_set_state(MODULE_STATE_ERROR);
|
||||
} else {
|
||||
module_set_state(MODULE_STATE_READY);
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
if (is_power_down_event(aeh)) {
|
||||
if (initialized) {
|
||||
module_pause();
|
||||
module_set_state(MODULE_STATE_STANDBY);
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
if (is_wake_up_event(aeh)) {
|
||||
if (initialized) {
|
||||
int err = module_start();
|
||||
|
||||
if (err) {
|
||||
module_set_state(MODULE_STATE_ERROR);
|
||||
} else {
|
||||
module_set_state(MODULE_STATE_READY);
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
APP_EVENT_LISTENER(MODULE, app_event_handler);
|
||||
APP_EVENT_SUBSCRIBE(MODULE, module_state_event);
|
||||
APP_EVENT_SUBSCRIBE(MODULE, ble_peer_event);
|
||||
APP_EVENT_SUBSCRIBE(MODULE, usb_device_state_event);
|
||||
APP_EVENT_SUBSCRIBE_EARLY(MODULE, power_down_event);
|
||||
APP_EVENT_SUBSCRIBE(MODULE, wake_up_event);
|
||||
@@ -13,10 +13,7 @@
|
||||
|
||||
#include <caf/events/power_manager_event.h>
|
||||
|
||||
#include "usb_device_module.h"
|
||||
#include "usb_device_state_event.h"
|
||||
#include "usb_function_ready_event.h"
|
||||
#include "usb_prepare_event.h"
|
||||
#include "usb_state_event.h"
|
||||
|
||||
LOG_MODULE_REGISTER(MODULE, LOG_LEVEL_INF);
|
||||
|
||||
@@ -24,7 +21,7 @@ LOG_MODULE_REGISTER(MODULE, LOG_LEVEL_INF);
|
||||
#define USB_DEVICE_PID 0x52F0
|
||||
#define USB_DEVICE_MANUFACTURER "Atguigu"
|
||||
#define USB_DEVICE_PRODUCT "WH Mini Keyboard"
|
||||
#define USB_REQUIRED_FUNCTION_MASK (USB_FUNCTION_HID | USB_FUNCTION_CDC_ACM)
|
||||
#define USB_REQUIRED_READY_MASK (USB_STATEF_HID_READY | USB_STATEF_CDC_READY)
|
||||
|
||||
USBD_DEVICE_DEFINE(blinky_usbd, DEVICE_DT_GET(DT_NODELABEL(usbd)),
|
||||
USB_DEVICE_VID, USB_DEVICE_PID);
|
||||
@@ -42,35 +39,69 @@ static const char *const class_blocklist[] = {
|
||||
|
||||
static bool initialized;
|
||||
static bool running;
|
||||
static bool prepare_broadcasted;
|
||||
static bool usbd_initialized;
|
||||
static bool usb_enabled;
|
||||
static uint8_t ready_function_mask;
|
||||
static enum usb_device_state device_state = USB_DEVICE_STATE_DISCONNECTED;
|
||||
static uint32_t usb_state_flags;
|
||||
|
||||
static void submit_usb_device_state_event(enum usb_device_state state)
|
||||
enum usb_runtime_state {
|
||||
USB_RUNTIME_STATE_DISCONNECTED,
|
||||
USB_RUNTIME_STATE_POWERED,
|
||||
USB_RUNTIME_STATE_ACTIVE,
|
||||
USB_RUNTIME_STATE_SUSPENDED,
|
||||
};
|
||||
|
||||
static enum usb_runtime_state device_state = USB_RUNTIME_STATE_DISCONNECTED;
|
||||
|
||||
static void broadcast_usb_state(void)
|
||||
{
|
||||
struct usb_device_state_event *event = new_usb_device_state_event();
|
||||
|
||||
event->state = state;
|
||||
APP_EVENT_SUBMIT(event);
|
||||
submit_usb_state_snapshot(MODULE_ID(MODULE), usb_state_flags);
|
||||
}
|
||||
|
||||
static void submit_usb_prepare_event(void)
|
||||
static void set_usb_state_flags(uint32_t mask)
|
||||
{
|
||||
struct usb_prepare_event *event = new_usb_prepare_event();
|
||||
|
||||
APP_EVENT_SUBMIT(event);
|
||||
usb_state_flags |= mask;
|
||||
}
|
||||
|
||||
static void update_usb_device_state(enum usb_device_state state)
|
||||
static void clear_usb_state_flags(uint32_t mask)
|
||||
{
|
||||
usb_state_flags &= ~mask;
|
||||
}
|
||||
|
||||
static void update_usb_runtime_state(enum usb_runtime_state state)
|
||||
{
|
||||
if (device_state == state) {
|
||||
return;
|
||||
}
|
||||
|
||||
device_state = state;
|
||||
submit_usb_device_state_event(state);
|
||||
|
||||
switch (state) {
|
||||
case USB_RUNTIME_STATE_DISCONNECTED:
|
||||
clear_usb_state_flags(USB_STATEF_POWERED |
|
||||
USB_STATEF_ACTIVE |
|
||||
USB_STATEF_SUSPENDED);
|
||||
break;
|
||||
|
||||
case USB_RUNTIME_STATE_POWERED:
|
||||
set_usb_state_flags(USB_STATEF_POWERED);
|
||||
clear_usb_state_flags(USB_STATEF_ACTIVE | USB_STATEF_SUSPENDED);
|
||||
break;
|
||||
|
||||
case USB_RUNTIME_STATE_ACTIVE:
|
||||
set_usb_state_flags(USB_STATEF_POWERED | USB_STATEF_ACTIVE);
|
||||
clear_usb_state_flags(USB_STATEF_SUSPENDED);
|
||||
break;
|
||||
|
||||
case USB_RUNTIME_STATE_SUSPENDED:
|
||||
set_usb_state_flags(USB_STATEF_POWERED | USB_STATEF_SUSPENDED);
|
||||
clear_usb_state_flags(USB_STATEF_ACTIVE);
|
||||
break;
|
||||
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
broadcast_usb_state();
|
||||
}
|
||||
|
||||
static void update_power_manager_restriction(bool vbus_present)
|
||||
@@ -129,7 +160,7 @@ static void usbd_msg_cb(struct usbd_context *const usbd_ctx,
|
||||
LOG_ERR("usbd_enable failed (%d)", err);
|
||||
} else {
|
||||
usb_enabled = true;
|
||||
update_usb_device_state(USB_DEVICE_STATE_POWERED);
|
||||
update_usb_runtime_state(USB_RUNTIME_STATE_POWERED);
|
||||
}
|
||||
}
|
||||
break;
|
||||
@@ -145,26 +176,26 @@ static void usbd_msg_cb(struct usbd_context *const usbd_ctx,
|
||||
}
|
||||
|
||||
usb_enabled = false;
|
||||
update_usb_device_state(USB_DEVICE_STATE_DISCONNECTED);
|
||||
update_usb_runtime_state(USB_RUNTIME_STATE_DISCONNECTED);
|
||||
break;
|
||||
|
||||
case USBD_MSG_CONFIGURATION:
|
||||
if (msg->status) {
|
||||
update_usb_device_state(USB_DEVICE_STATE_ACTIVE);
|
||||
update_usb_runtime_state(USB_RUNTIME_STATE_ACTIVE);
|
||||
} else if (usb_enabled) {
|
||||
update_usb_device_state(USB_DEVICE_STATE_POWERED);
|
||||
update_usb_runtime_state(USB_RUNTIME_STATE_POWERED);
|
||||
}
|
||||
break;
|
||||
|
||||
case USBD_MSG_SUSPEND:
|
||||
if (usb_enabled) {
|
||||
update_usb_device_state(USB_DEVICE_STATE_SUSPENDED);
|
||||
update_usb_runtime_state(USB_RUNTIME_STATE_SUSPENDED);
|
||||
}
|
||||
break;
|
||||
|
||||
case USBD_MSG_RESUME:
|
||||
if (usb_enabled) {
|
||||
update_usb_device_state(USB_DEVICE_STATE_ACTIVE);
|
||||
update_usb_runtime_state(USB_RUNTIME_STATE_ACTIVE);
|
||||
}
|
||||
break;
|
||||
|
||||
@@ -196,6 +227,7 @@ static int usb_stack_init(void)
|
||||
}
|
||||
|
||||
usbd_initialized = true;
|
||||
set_usb_state_flags(USB_STATEF_STACK_READY);
|
||||
|
||||
if (!usbd_can_detect_vbus(&blinky_usbd)) {
|
||||
err = usbd_enable(&blinky_usbd);
|
||||
@@ -205,19 +237,33 @@ static int usb_stack_init(void)
|
||||
}
|
||||
|
||||
usb_enabled = true;
|
||||
update_usb_device_state(USB_DEVICE_STATE_POWERED);
|
||||
update_usb_runtime_state(USB_RUNTIME_STATE_POWERED);
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int try_start_usb_stack(void)
|
||||
{
|
||||
if (usbd_initialized) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
if ((usb_state_flags & USB_REQUIRED_READY_MASK) != USB_REQUIRED_READY_MASK) {
|
||||
set_usb_state_flags(USB_STATEF_PREPARE);
|
||||
return 0;
|
||||
}
|
||||
|
||||
clear_usb_state_flags(USB_STATEF_PREPARE);
|
||||
return usb_stack_init();
|
||||
}
|
||||
|
||||
static int module_init(void)
|
||||
{
|
||||
device_state = USB_DEVICE_STATE_DISCONNECTED;
|
||||
device_state = USB_RUNTIME_STATE_DISCONNECTED;
|
||||
usb_enabled = false;
|
||||
ready_function_mask = 0U;
|
||||
prepare_broadcasted = false;
|
||||
usbd_initialized = false;
|
||||
usb_state_flags = 0U;
|
||||
update_power_manager_restriction(false);
|
||||
|
||||
return 0;
|
||||
@@ -230,16 +276,12 @@ static int module_start(void)
|
||||
}
|
||||
|
||||
running = true;
|
||||
set_usb_state_flags(USB_STATEF_STACK_RUNNING);
|
||||
|
||||
if (usbd_initialized || prepare_broadcasted) {
|
||||
return 0;
|
||||
}
|
||||
int err = try_start_usb_stack();
|
||||
|
||||
ready_function_mask = 0U;
|
||||
prepare_broadcasted = true;
|
||||
submit_usb_prepare_event();
|
||||
|
||||
return 0;
|
||||
broadcast_usb_state();
|
||||
return err;
|
||||
}
|
||||
|
||||
static void module_pause(void)
|
||||
@@ -249,6 +291,8 @@ static void module_pause(void)
|
||||
}
|
||||
|
||||
running = false;
|
||||
clear_usb_state_flags(USB_STATEF_STACK_RUNNING);
|
||||
broadcast_usb_state();
|
||||
}
|
||||
|
||||
static bool handle_module_state_event(const struct module_state_event *event)
|
||||
@@ -281,28 +325,32 @@ static bool handle_module_state_event(const struct module_state_event *event)
|
||||
return false;
|
||||
}
|
||||
|
||||
static bool handle_usb_function_ready_event(const struct usb_function_ready_event *event)
|
||||
static bool handle_usb_state_event(const struct usb_state_event *event)
|
||||
{
|
||||
int err;
|
||||
|
||||
if (!running || !prepare_broadcasted || usbd_initialized) {
|
||||
if ((event->op == USB_STATE_EVENT_OP_SNAPSHOT) ||
|
||||
(event->sink_module_id != MODULE_ID(MODULE))) {
|
||||
return false;
|
||||
}
|
||||
|
||||
ready_function_mask |= event->function_mask;
|
||||
|
||||
if ((ready_function_mask & USB_REQUIRED_FUNCTION_MASK) !=
|
||||
USB_REQUIRED_FUNCTION_MASK) {
|
||||
if (event->op == USB_STATE_EVENT_OP_SET_BITS) {
|
||||
usb_state_flags |= event->flags;
|
||||
} else if (event->op == USB_STATE_EVENT_OP_CLEAR_BITS) {
|
||||
usb_state_flags &= ~event->flags;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
|
||||
err = usb_stack_init();
|
||||
int err = running ? try_start_usb_stack() : 0;
|
||||
if (err) {
|
||||
module_set_state(MODULE_STATE_ERROR);
|
||||
return false;
|
||||
}
|
||||
|
||||
broadcast_usb_state();
|
||||
|
||||
if (usbd_initialized) {
|
||||
module_set_state(MODULE_STATE_READY);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -312,8 +360,8 @@ static bool app_event_handler(const struct app_event_header *aeh)
|
||||
return handle_module_state_event(cast_module_state_event(aeh));
|
||||
}
|
||||
|
||||
if (is_usb_function_ready_event(aeh)) {
|
||||
return handle_usb_function_ready_event(cast_usb_function_ready_event(aeh));
|
||||
if (is_usb_state_event(aeh)) {
|
||||
return handle_usb_state_event(cast_usb_state_event(aeh));
|
||||
}
|
||||
|
||||
if (is_power_down_event(aeh)) {
|
||||
@@ -344,6 +392,6 @@ static bool app_event_handler(const struct app_event_header *aeh)
|
||||
|
||||
APP_EVENT_LISTENER(MODULE, app_event_handler);
|
||||
APP_EVENT_SUBSCRIBE(MODULE, module_state_event);
|
||||
APP_EVENT_SUBSCRIBE(MODULE, usb_function_ready_event);
|
||||
APP_EVENT_SUBSCRIBE(MODULE, usb_state_event);
|
||||
APP_EVENT_SUBSCRIBE_EARLY(MODULE, power_down_event);
|
||||
APP_EVENT_SUBSCRIBE(MODULE, wake_up_event);
|
||||
|
||||
@@ -21,10 +21,7 @@
|
||||
#include "hid_tx_report_event.h"
|
||||
#include "keyboard_core.h"
|
||||
#include "set_protocol_event.h"
|
||||
#include "usb_function_ready_event.h"
|
||||
#include "usb_prepare_event.h"
|
||||
#include "usb_device_module.h"
|
||||
#include "usb_device_state_event.h"
|
||||
#include "usb_state_event.h"
|
||||
|
||||
LOG_MODULE_REGISTER(MODULE, LOG_LEVEL_INF);
|
||||
|
||||
@@ -140,59 +137,23 @@ static void reset_usb_runtime_state(void)
|
||||
consumer_report_in_flight = false;
|
||||
}
|
||||
|
||||
static void submit_set_protocol_event(enum keyboard_protocol_mode protocol_mode)
|
||||
static void submit_usb_transport_state_event(void)
|
||||
{
|
||||
struct set_protocol_event *event = new_set_protocol_event();
|
||||
|
||||
event->transport = HID_TRANSPORT_USB;
|
||||
event->protocol_mode = protocol_mode;
|
||||
APP_EVENT_SUBMIT(event);
|
||||
}
|
||||
|
||||
static void submit_hid_led_event(uint8_t led_bm)
|
||||
{
|
||||
struct hid_led_event *event = new_hid_led_event();
|
||||
|
||||
event->transport = HID_TRANSPORT_USB;
|
||||
event->led_bm = led_bm;
|
||||
APP_EVENT_SUBMIT(event);
|
||||
}
|
||||
|
||||
static void submit_transport_state_event(void)
|
||||
{
|
||||
struct hid_transport_state_event *event = new_hid_transport_state_event();
|
||||
bool ready = running && usb_active;
|
||||
|
||||
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);
|
||||
submit_hid_transport_state_event(
|
||||
HID_TRANSPORT_USB,
|
||||
ready,
|
||||
ready && hid_ifaces[USB_HID_INTERFACE_KEYBOARD].ready,
|
||||
ready && hid_ifaces[USB_HID_INTERFACE_CONSUMER].ready,
|
||||
keyboard_protocol_mode);
|
||||
}
|
||||
|
||||
static void submit_hid_report_sent_event(enum keyboard_report_type report_type,
|
||||
uint16_t sequence, bool error)
|
||||
static bool is_usb_owner_snapshot(const struct usb_state_event *event)
|
||||
{
|
||||
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);
|
||||
return (event->op == USB_STATE_EVENT_OP_SNAPSHOT) &&
|
||||
(event->src_module_id == MODULE_ID(usb_device_module)) &&
|
||||
(event->sink_module_id == NULL);
|
||||
}
|
||||
|
||||
static void keyboard_iface_ready(const struct device *dev, const bool ready)
|
||||
@@ -209,7 +170,7 @@ static void keyboard_iface_ready(const struct device *dev, const bool ready)
|
||||
}
|
||||
|
||||
LOG_INF("%s interface %s", dev->name, ready ? "ready" : "not ready");
|
||||
submit_transport_state_event();
|
||||
submit_usb_transport_state_event();
|
||||
}
|
||||
|
||||
static int keyboard_get_report(const struct device *dev,
|
||||
@@ -267,8 +228,8 @@ static void keyboard_set_protocol(const struct device *dev, const uint8_t proto)
|
||||
keyboard_protocol_mode = new_mode;
|
||||
LOG_INF("USB keyboard protocol -> %s",
|
||||
(new_mode == KEYBOARD_PROTOCOL_MODE_BOOT) ? "boot" : "report");
|
||||
submit_set_protocol_event(new_mode);
|
||||
submit_transport_state_event();
|
||||
submit_set_protocol_event(HID_TRANSPORT_USB, new_mode);
|
||||
submit_usb_transport_state_event();
|
||||
}
|
||||
|
||||
static void keyboard_input_report_done(const struct device *dev,
|
||||
@@ -278,7 +239,8 @@ static void keyboard_input_report_done(const struct device *dev,
|
||||
|
||||
keyboard_report_in_flight = false;
|
||||
LOG_DBG("USB keyboard report sent by %s", dev->name);
|
||||
submit_hid_report_sent_event(KEYBOARD_REPORT_TYPE_KEYS,
|
||||
submit_hid_report_sent_event(HID_TRANSPORT_USB,
|
||||
KEYBOARD_REPORT_TYPE_KEYS,
|
||||
keyboard_in_flight_sequence, false);
|
||||
}
|
||||
|
||||
@@ -293,7 +255,7 @@ static void keyboard_output_report(const struct device *dev,
|
||||
return;
|
||||
}
|
||||
|
||||
submit_hid_led_event(buf[0]);
|
||||
submit_hid_led_event(HID_TRANSPORT_USB, buf[0]);
|
||||
}
|
||||
|
||||
static const struct hid_device_ops keyboard_ops = {
|
||||
@@ -321,7 +283,7 @@ static void consumer_iface_ready(const struct device *dev, const bool ready)
|
||||
}
|
||||
|
||||
LOG_INF("%s interface %s", dev->name, ready ? "ready" : "not ready");
|
||||
submit_transport_state_event();
|
||||
submit_usb_transport_state_event();
|
||||
}
|
||||
|
||||
static int consumer_get_report(const struct device *dev,
|
||||
@@ -379,7 +341,8 @@ static void consumer_input_report_done(const struct device *dev,
|
||||
|
||||
consumer_report_in_flight = false;
|
||||
LOG_DBG("USB consumer report sent by %s", dev->name);
|
||||
submit_hid_report_sent_event(KEYBOARD_REPORT_TYPE_CONSUMER,
|
||||
submit_hid_report_sent_event(HID_TRANSPORT_USB,
|
||||
KEYBOARD_REPORT_TYPE_CONSUMER,
|
||||
consumer_in_flight_sequence, false);
|
||||
}
|
||||
|
||||
@@ -449,27 +412,26 @@ static int module_start(void)
|
||||
}
|
||||
|
||||
running = true;
|
||||
submit_transport_state_event();
|
||||
submit_usb_transport_state_event();
|
||||
return 0;
|
||||
}
|
||||
|
||||
static void module_pause(void)
|
||||
{
|
||||
running = false;
|
||||
submit_transport_state_event();
|
||||
submit_usb_transport_state_event();
|
||||
}
|
||||
|
||||
static bool handle_usb_prepare_event(const struct usb_prepare_event *event)
|
||||
static bool handle_usb_state_event(const struct usb_state_event *event)
|
||||
{
|
||||
int err;
|
||||
|
||||
ARG_UNUSED(event);
|
||||
|
||||
if (!running || usb_function_prepared) {
|
||||
if (!is_usb_owner_snapshot(event)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
err = usb_hid_register_devices();
|
||||
if (running && !usb_function_prepared &&
|
||||
((event->flags & USB_STATEF_PREPARE) != 0U)) {
|
||||
int err = usb_hid_register_devices();
|
||||
|
||||
if (err) {
|
||||
LOG_ERR("hid_device_register failed (%d)", err);
|
||||
module_set_state(MODULE_STATE_ERROR);
|
||||
@@ -477,13 +439,12 @@ static bool handle_usb_prepare_event(const struct usb_prepare_event *event)
|
||||
}
|
||||
|
||||
usb_function_prepared = true;
|
||||
submit_usb_function_ready_event();
|
||||
return false;
|
||||
}
|
||||
submit_usb_state_set(MODULE_ID(MODULE),
|
||||
MODULE_ID(usb_device_module),
|
||||
USB_STATEF_HID_READY);
|
||||
}
|
||||
|
||||
static bool handle_usb_device_state_event(const struct usb_device_state_event *event)
|
||||
{
|
||||
bool new_usb_active = (event->state == USB_DEVICE_STATE_ACTIVE);
|
||||
bool new_usb_active = (event->flags & USB_STATEF_ACTIVE) != 0U;
|
||||
|
||||
if (new_usb_active == usb_active) {
|
||||
return false;
|
||||
@@ -495,7 +456,7 @@ static bool handle_usb_device_state_event(const struct usb_device_state_event *e
|
||||
usb_active = true;
|
||||
}
|
||||
|
||||
submit_transport_state_event();
|
||||
submit_usb_transport_state_event();
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -531,7 +492,8 @@ static bool handle_hid_tx_report_event(const struct hid_tx_report_event *event)
|
||||
keyboard_tx_buf);
|
||||
if (err) {
|
||||
LOG_WRN("USB keyboard report submit failed (%d)", err);
|
||||
submit_hid_report_sent_event(KEYBOARD_REPORT_TYPE_KEYS,
|
||||
submit_hid_report_sent_event(HID_TRANSPORT_USB,
|
||||
KEYBOARD_REPORT_TYPE_KEYS,
|
||||
event->sequence, true);
|
||||
} else {
|
||||
keyboard_report_in_flight = true;
|
||||
@@ -560,7 +522,8 @@ static bool handle_hid_tx_report_event(const struct hid_tx_report_event *event)
|
||||
consumer_tx_buf);
|
||||
if (err) {
|
||||
LOG_WRN("USB consumer report submit failed (%d)", err);
|
||||
submit_hid_report_sent_event(KEYBOARD_REPORT_TYPE_CONSUMER,
|
||||
submit_hid_report_sent_event(HID_TRANSPORT_USB,
|
||||
KEYBOARD_REPORT_TYPE_CONSUMER,
|
||||
event->sequence, true);
|
||||
} else {
|
||||
consumer_report_in_flight = true;
|
||||
@@ -577,12 +540,8 @@ static bool app_event_handler(const struct app_event_header *aeh)
|
||||
return handle_hid_tx_report_event(cast_hid_tx_report_event(aeh));
|
||||
}
|
||||
|
||||
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_state_event(aeh)) {
|
||||
return handle_usb_state_event(cast_usb_state_event(aeh));
|
||||
}
|
||||
|
||||
if (is_module_state_event(aeh)) {
|
||||
@@ -641,7 +600,6 @@ static bool app_event_handler(const struct app_event_header *aeh)
|
||||
APP_EVENT_LISTENER(MODULE, app_event_handler);
|
||||
APP_EVENT_SUBSCRIBE(MODULE, module_state_event);
|
||||
APP_EVENT_SUBSCRIBE(MODULE, hid_tx_report_event);
|
||||
APP_EVENT_SUBSCRIBE(MODULE, usb_prepare_event);
|
||||
APP_EVENT_SUBSCRIBE(MODULE, usb_device_state_event);
|
||||
APP_EVENT_SUBSCRIBE(MODULE, usb_state_event);
|
||||
APP_EVENT_SUBSCRIBE_EARLY(MODULE, power_down_event);
|
||||
APP_EVENT_SUBSCRIBE(MODULE, wake_up_event);
|
||||
|
||||
Reference in New Issue
Block a user