Skip to content

Commit

Permalink
feat(rpc): Meta responses, naming convention fixes.
Browse files Browse the repository at this point in the history
* Message/enum types are PascalCase.
  • Loading branch information
petejohanson committed Mar 16, 2024
1 parent 9aeb3c0 commit d50b2dc
Show file tree
Hide file tree
Showing 12 changed files with 67 additions and 63 deletions.
1 change: 1 addition & 0 deletions app/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ if (CONFIG_ZMK_STUDIO)

zephyr_nanopb_sources(app
proto/zmk/studio.proto
proto/zmk/meta.proto
proto/zmk/core.proto
proto/zmk/behaviors.proto
proto/zmk/keymap.proto
Expand Down
9 changes: 4 additions & 5 deletions app/include/zmk/studio/rpc.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

#include <zephyr/sys/iterable_sections.h>

#include <proto/zmk/studio-msgs.pb.h>
#include <proto/zmk/studio.pb.h>

struct zmk_rpc_subsystem;

Expand Down Expand Up @@ -39,8 +39,7 @@ struct zmk_rpc_subsystem_handler {
} \
} \
LOG_ERR("No handler func found for %d", which_req); \
zmk_Response fallback_resp = zmk_Response_init_zero; \
return fallback_resp; \
return ZMK_RPC_RESPONSE(meta, simple_error, zmk_meta_ErrorConditions_RPC_NOT_FOUND); \
} \
STRUCT_SECTION_ITERABLE(zmk_rpc_subsystem, prefix##_subsystem) = { \
.func = subsystem_func_##prefix, \
Expand All @@ -52,7 +51,7 @@ struct zmk_rpc_subsystem_handler {
prefix##_subsystem_handler_##request_id) = { \
.func = request_id, \
.subsystem_choice = zmk_Request_##prefix##_tag, \
.request_choice = zmk_##prefix##_response_##request_id##_tag, \
.request_choice = zmk_##prefix##_Response_##request_id##_tag, \
};

#define ZMK_RPC_RESPONSE(subsys, _type, ...) \
Expand All @@ -68,7 +67,7 @@ struct zmk_rpc_subsystem_handler {
.subsys = \
{ \
.which_response_type = \
zmk_##subsys##_response_##_type##_tag, \
zmk_##subsys##_Response_##_type##_tag, \
.response_type = {._type = __VA_ARGS__}, \
}, \
}, \
Expand Down
5 changes: 3 additions & 2 deletions app/proto/zmk/behaviors.options
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
zmk.behaviors.list_all_behaviors_response.behaviors max_count:20
zmk.behaviors.get_behavior_details_response.friendly_name max_size:20
zmk.behaviors.ListAllBehaviorsResponse.behaviors max_count:20
zmk.behaviors.GetBehaviorDetailsResponse.friendly_name max_size:20
zmk.behaviors.behavior_binding_parameter_details.name max_size:20
40 changes: 28 additions & 12 deletions app/proto/zmk/behaviors.proto
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,29 @@ syntax = "proto3";

package zmk.behaviors;

message request {
message Request {
oneof request_type {
bool list_all_behaviors = 1;
get_behavior_details_request get_behavior_details = 2;
GetBehaviorDetailsRequest get_behavior_details = 2;
}
}

message get_behavior_details_request {
message GetBehaviorDetailsRequest {
uint32 behavior_id = 1;
}

message response {
message Response {
oneof response_type {
list_all_behaviors_response list_all_behaviors = 1;
get_behavior_details_response get_behavior_details = 2;
ListAllBehaviorsResponse list_all_behaviors = 1;
GetBehaviorDetailsResponse get_behavior_details = 2;
}
}

message list_all_behaviors_response {
message ListAllBehaviorsResponse {
repeated uint32 behaviors = 1;
}

message get_behavior_details_response {
message GetBehaviorDetailsResponse {
uint32 id = 1;
string friendly_name = 2;
behavior_binding_parameter_details param1 = 3;
Expand All @@ -36,8 +36,24 @@ message behavior_binding_parameter_details {
string name = 2;
}

enum behavior_binding_parameter_domain {
Nil = 0;
HIDUsage = 1;
LayerIndex = 2;
message behavior_binding_parameter_domain {
oneof type {
behavior_binding_parameter_domain_standard standard = 1;
behavior_binding_parameter_domain_custom custom = 2;
}
}

message behavior_binding_parameter_domain_custom {
repeated behavior_binding_parameter_value_description values = 1;
}

message behavior_binding_parameter_value_description {
int32 value = 1;
string name = 2;
}

enum behavior_binding_parameter_domain_standard {
NIL = 0;
HID_USAGE = 1;
LAYER_INDEX = 2;
}
8 changes: 4 additions & 4 deletions app/proto/zmk/core.proto
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,20 @@ syntax = "proto3";

package zmk.core;

message request {
message Request {
oneof request_type {
bool get_lock_status = 1;
bool unlock_request = 2;
bool lock_request = 3;
}
}

message get_lock_status_response {
message GetLockStatusResponse {
bool locked = 1;
}

message response {
message Response {
oneof response_type {
get_lock_status_response get_lock_status = 1;
GetLockStatusResponse get_lock_status = 1;
}
}
2 changes: 1 addition & 1 deletion app/proto/zmk/keymap.proto
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ syntax = "proto3";

package zmk.keymap;

message request {
message Request {
oneof request_type {
bool get_layer_summaries = 1;
}
Expand Down
16 changes: 16 additions & 0 deletions app/proto/zmk/meta.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
syntax = "proto3";

package zmk.meta;

enum ErrorConditions {
GENERIC = 0;
RPC_NOT_FOUND = 1;
MSG_DECODE_FAILED = 2;
MSG_ENCODE_FAILED = 3;
}

message Response {
oneof response_type {
ErrorConditions simple_error = 1;
}
}
30 changes: 7 additions & 23 deletions app/proto/zmk/studio.proto
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ syntax = "proto3";

package zmk;

import "meta.proto";
import "core.proto";
import "behaviors.proto";
import "keymap.proto";
Expand All @@ -11,29 +12,12 @@ message Request {
uint32 request_id = 1;

oneof subsystem {
zmk.core.request core = 2;
zmk.keymap.request keymap = 3;
zmk.behaviors.request behaviors = 4;
zmk.core.Request core = 2;
zmk.keymap.Request keymap = 3;
zmk.behaviors.Request behaviors = 4;
}
}


// keymap_request = {
// (0: get_layers_summary)
// }

// behavior_request = {
// (0: list_all_behaviors) /
// (1: get_behavior_details)
// }

// get_layers_summary = nil

// list_all_behaviors = nil
// get_behavior_details = [ behavior_id: uint ]

// ; Responses

message Response {
oneof type {
RequestResponse request_response = 1;
Expand All @@ -44,12 +28,12 @@ message Response {
message RequestResponse {
uint32 request_id = 1;
oneof subsystem {
zmk.core.response core = 2;
zmk.behaviors.response behaviors = 4;
zmk.meta.Response meta = 2;
zmk.core.Response core = 3;
zmk.behaviors.Response behaviors = 4;
}
}

message Notification {
uint32 notification_type = 1;

}
2 changes: 1 addition & 1 deletion app/src/studio/core_subsystem.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ ZMK_RPC_SUBSYSTEM(core)
#define CORE_RESPONSE(type, ...) ZMK_RPC_RESPONSE(core, type, __VA_ARGS__)

zmk_Response get_lock_status(const zmk_Request *req) {
zmk_core_get_lock_status_response resp = zmk_core_get_lock_status_response_init_zero;
zmk_core_GetLockStatusResponse resp = zmk_core_GetLockStatusResponse_init_zero;
resp.locked = true;

return CORE_RESPONSE(get_lock_status, resp);
Expand Down
2 changes: 0 additions & 2 deletions app/src/studio/gatt_rpc_interface.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@
#include <pb_encode.h>
#include <pb_decode.h>

#include <proto/zmk/studio-msgs.pb.h>

#include <zephyr/logging/log.h>

LOG_MODULE_DECLARE(zmk, CONFIG_ZMK_LOG_LEVEL);
Expand Down
10 changes: 2 additions & 8 deletions app/src/studio/rpc.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,17 @@ LOG_MODULE_DECLARE(zmk, CONFIG_ZMK_LOG_LEVEL);
#include <zmk/studio/rpc.h>

zmk_Response zmk_rpc_handle_request(const zmk_Request *req) {
LOG_WRN("Got a req of union type: %d", req->which_subsystem);
LOG_DBG("Request for subsystem %d", req->which_subsystem);
STRUCT_SECTION_FOREACH(zmk_rpc_subsystem, sub) {
if (sub->subsystem_choice == req->which_subsystem) {
LOG_WRN("Found a func!");
zmk_Response resp = sub->func(sub, req);
resp.type.request_response.request_id = req->request_id;

return resp;
}
}

LOG_WRN("No HANDLER FOUND");
// TODO: NOT SUPPORTED
zmk_Response resp = zmk_Response_init_zero;
return resp;
return ZMK_RPC_RESPONSE(meta, simple_error, zmk_meta_ErrorConditions_RPC_NOT_FOUND);
}

static struct zmk_rpc_subsystem *find_subsystem_for_choice(uint8_t choice) {
Expand All @@ -46,8 +42,6 @@ static int zmk_rpc_init(void) {
__ASSERT(sub != NULL, "RPC Handler for unknown subsystem choice %d",
handler->subsystem_choice);

LOG_DBG("Init handler %d sub %d", handler->request_choice, sub->subsystem_choice);

if (prev_choice < 0) {
sub->handlers_start_index = i;
} else if ((prev_choice != handler->subsystem_choice) && prev_sub) {
Expand Down
5 changes: 0 additions & 5 deletions app/src/studio/uart_rpc_interface.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,9 @@

#include "common.h"

// #include <studio-msgs_decode.h>
// #include <studio-msgs_encode.h>

#include <pb_encode.h>
#include <pb_decode.h>

#include <proto/zmk/studio-msgs.pb.h>

LOG_MODULE_DECLARE(zmk, CONFIG_ZMK_LOG_LEVEL);

#include <string.h>
Expand Down

0 comments on commit d50b2dc

Please sign in to comment.