Skip to content

Commit

Permalink
usb_comm: Make use of iterable section to decouple handler registration
Browse files Browse the repository at this point in the history
  • Loading branch information
xingrz committed Feb 16, 2023
1 parent 85f89f9 commit 14439b4
Show file tree
Hide file tree
Showing 8 changed files with 74 additions and 77 deletions.
2 changes: 2 additions & 0 deletions config/app/usb_comm/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,6 @@ zephyr_library_link_libraries(hw75_proto)
zephyr_library_sources(usb_comm_hid.c)
zephyr_library_sources(usb_comm_proto.c)

zephyr_linker_sources(DATA_SECTIONS usb_comm_handler.ld)

add_subdirectory(handler)
35 changes: 14 additions & 21 deletions config/app/usb_comm/handler/handler.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,31 +5,24 @@

#pragma once

#include <device.h>

#include "usb_comm.pb.h"

typedef bool (*usb_comm_handler_t)(const usb_comm_MessageH2D *h2d, usb_comm_MessageD2H *d2h,
const void *bytes, uint32_t bytes_len);

bool handle_version(const usb_comm_MessageH2D *h2d, usb_comm_MessageD2H *d2h, const void *bytes,
uint32_t bytes_len);

bool handle_motor_get_state(const usb_comm_MessageH2D *h2d, usb_comm_MessageD2H *d2h,
const void *bytes, uint32_t bytes_len);
bool handle_knob_get_config(const usb_comm_MessageH2D *h2d, usb_comm_MessageD2H *d2h,
const void *bytes, uint32_t bytes_len);
bool handle_knob_set_config(const usb_comm_MessageH2D *h2d, usb_comm_MessageD2H *d2h,
const void *bytes, uint32_t bytes_len);
struct usb_comm_handler_config {
usb_comm_Action action;
pb_size_t response_payload;
usb_comm_handler_t handler;
};

bool handle_rgb_control(const usb_comm_MessageH2D *h2d, usb_comm_MessageD2H *d2h, const void *bytes,
uint32_t bytes_len);
bool handle_rgb_get_state(const usb_comm_MessageH2D *h2d, usb_comm_MessageD2H *d2h,
const void *bytes, uint32_t bytes_len);
bool handle_rgb_set_state(const usb_comm_MessageH2D *h2d, usb_comm_MessageD2H *d2h,
const void *bytes, uint32_t bytes_len);
bool handle_rgb_get_indicator(const usb_comm_MessageH2D *h2d, usb_comm_MessageD2H *d2h,
const void *bytes, uint32_t bytes_len);
bool handle_rgb_set_indicator(const usb_comm_MessageH2D *h2d, usb_comm_MessageD2H *d2h,
const void *bytes, uint32_t bytes_len);
#define USB_COMM_DEFINE_HANDLER(name) static STRUCT_SECTION_ITERABLE(usb_comm_handler_config, name)

bool handle_eink_set_image(const usb_comm_MessageH2D *h2d, usb_comm_MessageD2H *d2h,
const void *bytes, uint32_t bytes_len);
#define USB_COMM_HANDLER_DEFINE(_action, _payload, _handler) \
USB_COMM_DEFINE_HANDLER(usb_comm_handler_##_handler) = { \
.action = _action, \
.response_payload = _payload, \
.handler = _handler, \
};
7 changes: 5 additions & 2 deletions config/app/usb_comm/handler/handler_eink.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ LOG_MODULE_DECLARE(zmk, CONFIG_ZMK_LOG_LEVEL);
#include <pb_encode.h>
#include <pb_decode.h>

bool handle_eink_set_image(const usb_comm_MessageH2D *h2d, usb_comm_MessageD2H *d2h,
const void *bytes, uint32_t bytes_len)
static bool handle_eink_set_image(const usb_comm_MessageH2D *h2d, usb_comm_MessageD2H *d2h,
const void *bytes, uint32_t bytes_len)
{
const usb_comm_EinkImage *req = &h2d->payload.eink_image;
usb_comm_EinkImage *res = &d2h->payload.eink_image;
Expand All @@ -30,3 +30,6 @@ bool handle_eink_set_image(const usb_comm_MessageH2D *h2d, usb_comm_MessageD2H *

return true;
}

USB_COMM_HANDLER_DEFINE(usb_comm_Action_EINK_SET_IMAGE, usb_comm_MessageD2H_eink_image_tag,
handle_eink_set_image);
21 changes: 15 additions & 6 deletions config/app/usb_comm/handler/handler_knob.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ static const struct device *motor;

static struct motor_state state = {};

bool handle_motor_get_state(const usb_comm_MessageH2D *h2d, usb_comm_MessageD2H *d2h,
const void *bytes, uint32_t bytes_len)
static bool handle_motor_get_state(const usb_comm_MessageH2D *h2d, usb_comm_MessageD2H *d2h,
const void *bytes, uint32_t bytes_len)
{
usb_comm_MotorState *res = &d2h->payload.motor_state;

Expand All @@ -39,8 +39,11 @@ bool handle_motor_get_state(const usb_comm_MessageH2D *h2d, usb_comm_MessageD2H
return true;
}

bool handle_knob_get_config(const usb_comm_MessageH2D *h2d, usb_comm_MessageD2H *d2h,
const void *bytes, uint32_t bytes_len)
USB_COMM_HANDLER_DEFINE(usb_comm_Action_MOTOR_GET_STATE, usb_comm_MessageD2H_motor_state_tag,
handle_motor_get_state);

static bool handle_knob_get_config(const usb_comm_MessageH2D *h2d, usb_comm_MessageD2H *d2h,
const void *bytes, uint32_t bytes_len)
{
usb_comm_KnobConfig *res = &d2h->payload.knob_config;

Expand All @@ -54,8 +57,11 @@ bool handle_knob_get_config(const usb_comm_MessageH2D *h2d, usb_comm_MessageD2H
return true;
}

bool handle_knob_set_config(const usb_comm_MessageH2D *h2d, usb_comm_MessageD2H *d2h,
const void *bytes, uint32_t bytes_len)
USB_COMM_HANDLER_DEFINE(usb_comm_Action_KNOB_GET_CONFIG, usb_comm_MessageD2H_knob_config_tag,
handle_knob_get_config);

static bool handle_knob_set_config(const usb_comm_MessageH2D *h2d, usb_comm_MessageD2H *d2h,
const void *bytes, uint32_t bytes_len)
{
const usb_comm_KnobConfig *req = &h2d->payload.knob_config;

Expand All @@ -73,6 +79,9 @@ bool handle_knob_set_config(const usb_comm_MessageH2D *h2d, usb_comm_MessageD2H
return handle_knob_get_config(h2d, d2h, NULL, 0);
}

USB_COMM_HANDLER_DEFINE(usb_comm_Action_KNOB_SET_CONFIG, usb_comm_MessageD2H_knob_config_tag,
handle_knob_set_config);

static int handler_knob_init(const struct device *dev)
{
ARG_UNUSED(dev);
Expand Down
38 changes: 28 additions & 10 deletions config/app/usb_comm/handler/handler_rgb.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,11 @@
#include <zmk/rgb_underglow.h>
#include <app/indicator.h>

bool handle_rgb_control(const usb_comm_MessageH2D *h2d, usb_comm_MessageD2H *d2h, const void *bytes,
uint32_t bytes_len)
static bool handle_rgb_get_state(const usb_comm_MessageH2D *h2d, usb_comm_MessageD2H *d2h,
const void *bytes, uint32_t bytes_len);

static bool handle_rgb_control(const usb_comm_MessageH2D *h2d, usb_comm_MessageD2H *d2h,
const void *bytes, uint32_t bytes_len)
{
const usb_comm_RgbControl *req = &h2d->payload.rgb_control;

Expand Down Expand Up @@ -56,8 +59,11 @@ bool handle_rgb_control(const usb_comm_MessageH2D *h2d, usb_comm_MessageD2H *d2h
return handle_rgb_get_state(h2d, d2h, NULL, 0);
}

bool handle_rgb_get_state(const usb_comm_MessageH2D *h2d, usb_comm_MessageD2H *d2h,
const void *bytes, uint32_t bytes_len)
USB_COMM_HANDLER_DEFINE(usb_comm_Action_RGB_CONTROL, usb_comm_MessageD2H_rgb_state_tag,
handle_rgb_control);

static bool handle_rgb_get_state(const usb_comm_MessageH2D *h2d, usb_comm_MessageD2H *d2h,
const void *bytes, uint32_t bytes_len)
{
usb_comm_RgbState *res = &d2h->payload.rgb_state;

Expand All @@ -77,8 +83,11 @@ bool handle_rgb_get_state(const usb_comm_MessageH2D *h2d, usb_comm_MessageD2H *d
return true;
}

bool handle_rgb_set_state(const usb_comm_MessageH2D *h2d, usb_comm_MessageD2H *d2h,
const void *bytes, uint32_t bytes_len)
USB_COMM_HANDLER_DEFINE(usb_comm_Action_RGB_GET_STATE, usb_comm_MessageD2H_rgb_state_tag,
handle_rgb_get_state);

static bool handle_rgb_set_state(const usb_comm_MessageH2D *h2d, usb_comm_MessageD2H *d2h,
const void *bytes, uint32_t bytes_len)
{
const usb_comm_RgbState *req = &h2d->payload.rgb_state;

Expand All @@ -103,8 +112,11 @@ bool handle_rgb_set_state(const usb_comm_MessageH2D *h2d, usb_comm_MessageD2H *d
return handle_rgb_get_state(h2d, d2h, bytes, bytes_len);
}

bool handle_rgb_get_indicator(const usb_comm_MessageH2D *h2d, usb_comm_MessageD2H *d2h,
const void *bytes, uint32_t bytes_len)
USB_COMM_HANDLER_DEFINE(usb_comm_Action_RGB_SET_STATE, usb_comm_MessageD2H_rgb_state_tag,
handle_rgb_set_state);

static bool handle_rgb_get_indicator(const usb_comm_MessageH2D *h2d, usb_comm_MessageD2H *d2h,
const void *bytes, uint32_t bytes_len)
{
usb_comm_RgbIndicator *res = &d2h->payload.rgb_indicator;

Expand All @@ -120,8 +132,11 @@ bool handle_rgb_get_indicator(const usb_comm_MessageH2D *h2d, usb_comm_MessageD2
return true;
}

bool handle_rgb_set_indicator(const usb_comm_MessageH2D *h2d, usb_comm_MessageD2H *d2h,
const void *bytes, uint32_t bytes_len)
USB_COMM_HANDLER_DEFINE(usb_comm_Action_RGB_GET_INDICATOR, usb_comm_MessageD2H_rgb_indicator_tag,
handle_rgb_get_indicator);

static bool handle_rgb_set_indicator(const usb_comm_MessageH2D *h2d, usb_comm_MessageD2H *d2h,
const void *bytes, uint32_t bytes_len)
{
const usb_comm_RgbIndicator *req = &h2d->payload.rgb_indicator;

Expand All @@ -137,3 +152,6 @@ bool handle_rgb_set_indicator(const usb_comm_MessageH2D *h2d, usb_comm_MessageD2

return handle_rgb_get_indicator(h2d, d2h, bytes, bytes_len);
}

USB_COMM_HANDLER_DEFINE(usb_comm_Action_RGB_SET_INDICATOR, usb_comm_MessageD2H_rgb_indicator_tag,
handle_rgb_set_indicator);
6 changes: 4 additions & 2 deletions config/app/usb_comm/handler/handler_version.c
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ static bool write_string(pb_ostream_t *stream, const pb_field_t *field, void *co
return pb_encode_string(stream, (uint8_t *)str, strlen(str));
}

bool handle_version(const usb_comm_MessageH2D *h2d, usb_comm_MessageD2H *d2h, const void *bytes,
uint32_t bytes_len)
static bool handle_version(const usb_comm_MessageH2D *h2d, usb_comm_MessageD2H *d2h,
const void *bytes, uint32_t bytes_len)
{
usb_comm_Version *res = &d2h->payload.version;
res->zephyr_version.funcs.encode = write_string;
Expand Down Expand Up @@ -64,3 +64,5 @@ bool handle_version(const usb_comm_MessageH2D *h2d, usb_comm_MessageD2H *d2h, co

return true;
}

USB_COMM_HANDLER_DEFINE(usb_comm_Action_VERSION, usb_comm_MessageD2H_version_tag, handle_version);
1 change: 1 addition & 0 deletions config/app/usb_comm/usb_comm_handler.ld
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ITERABLE_SECTION_RAM(usb_comm_handler_config, 4)
41 changes: 5 additions & 36 deletions config/app/usb_comm/usb_comm_proto.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,38 +31,6 @@ static uint8_t usb_tx_buf[CONFIG_HW75_USB_COMM_MAX_TX_MESSAGE_SIZE];
static uint8_t bytes_field[CONFIG_HW75_USB_COMM_MAX_BYTES_FIELD_SIZE];
static uint32_t bytes_field_len = 0;

static struct {
usb_comm_Action action;
pb_size_t which_payload;
usb_comm_handler_t handler;
} handlers[] = {
{ usb_comm_Action_VERSION, usb_comm_MessageD2H_version_tag, handle_version },

#ifdef CONFIG_HW75_USB_COMM_FEATURE_KNOB
{ usb_comm_Action_MOTOR_GET_STATE, usb_comm_MessageD2H_motor_state_tag,
handle_motor_get_state },
{ usb_comm_Action_KNOB_GET_CONFIG, usb_comm_MessageD2H_knob_config_tag,
handle_knob_get_config },
{ usb_comm_Action_KNOB_SET_CONFIG, usb_comm_MessageD2H_knob_config_tag,
handle_knob_set_config },
#endif // CONFIG_HW75_USB_COMM_FEATURE_KNOB

#ifdef CONFIG_HW75_USB_COMM_FEATURE_RGB
{ usb_comm_Action_RGB_CONTROL, usb_comm_MessageD2H_rgb_state_tag, handle_rgb_control },
{ usb_comm_Action_RGB_GET_STATE, usb_comm_MessageD2H_rgb_state_tag, handle_rgb_get_state },
{ usb_comm_Action_RGB_SET_STATE, usb_comm_MessageD2H_rgb_state_tag, handle_rgb_set_state },
{ usb_comm_Action_RGB_GET_INDICATOR, usb_comm_MessageD2H_rgb_indicator_tag,
handle_rgb_get_indicator },
{ usb_comm_Action_RGB_SET_INDICATOR, usb_comm_MessageD2H_rgb_indicator_tag,
handle_rgb_set_indicator },
#endif // CONFIG_HW75_USB_COMM_FEATURE_RGB

#ifdef CONFIG_HW75_USB_COMM_FEATURE_EINK
{ usb_comm_Action_EINK_SET_IMAGE, usb_comm_MessageD2H_eink_image_tag,
handle_eink_set_image },
#endif // CONFIG_HW75_USB_COMM_FEATURE_EINK
};

#if CONFIG_HW75_USB_COMM_MAX_BYTES_FIELD_SIZE
static bool read_bytes_field(pb_istream_t *stream, const pb_field_t *field, void **arg)
{
Expand Down Expand Up @@ -123,10 +91,11 @@ static void usb_comm_handle_message()
d2h.action = h2d.action;
d2h.which_payload = usb_comm_MessageD2H_nop_tag;

for (size_t i = 0; i < ARRAY_SIZE(handlers); i++) {
if (handlers[i].action == h2d.action) {
if (handlers[i].handler(&h2d, &d2h, bytes_field, bytes_field_len)) {
d2h.which_payload = handlers[i].which_payload;
STRUCT_SECTION_FOREACH(usb_comm_handler_config, config)
{
if (config->action == h2d.action) {
if (config->handler(&h2d, &d2h, bytes_field, bytes_field_len)) {
d2h.which_payload = config->response_payload;
}
break;
}
Expand Down

0 comments on commit 14439b4

Please sign in to comment.