diff options
author | lonkaars <loek@pipeframe.xyz> | 2023-01-07 22:11:12 +0100 |
---|---|---|
committer | lonkaars <loek@pipeframe.xyz> | 2023-01-07 22:11:12 +0100 |
commit | 1c747510457c40330f8bd4506e725889c3d012e2 (patch) | |
tree | 5ed807fe726deedd51e84efb8916e71d38afdc18 | |
parent | 456efce00c0ed85061352a38edddc0a3f39c7804 (diff) |
`clang-format`
-rw-r--r-- | shared/bin.c | 11 | ||||
-rw-r--r-- | shared/bin.h | 17 | ||||
-rw-r--r-- | shared/consts.h | 1 | ||||
-rw-r--r-- | shared/pclient.c | 97 | ||||
-rw-r--r-- | shared/pclient.h | 24 | ||||
-rw-r--r-- | shared/protocol.c | 23 | ||||
-rw-r--r-- | shared/protocol.h | 116 | ||||
-rw-r--r-- | shared/serial_parse.c | 7 | ||||
-rw-r--r-- | shared/serial_parse.h | 3 |
9 files changed, 136 insertions, 163 deletions
diff --git a/shared/bin.c b/shared/bin.c index 22873b6..83395e3 100644 --- a/shared/bin.c +++ b/shared/bin.c @@ -55,8 +55,7 @@ cd_s_bin *cd_bin_from_uint32_t(uint32_t data) { uint32_t cd_bin_hton32(uint32_t h32) { if (g_cd_endianness == CD_ENDIAN_BIG) return h32; - return ((h32 & _BYTE_0) << _SHIFT_3B) | ((h32 & _BYTE_1) << _SHIFT_1B) | - ((h32 & _BYTE_2) >> _SHIFT_1B) | ((h32 & _BYTE_3) >> _SHIFT_3B); + return ((h32 & _BYTE_0) << _SHIFT_3B) | ((h32 & _BYTE_1) << _SHIFT_1B) | ((h32 & _BYTE_2) >> _SHIFT_1B) | ((h32 & _BYTE_3) >> _SHIFT_3B); } #pragma GCC diagnostic pop @@ -67,12 +66,12 @@ uint16_t cd_bin_hton16(uint16_t h16) { uint32_t cd_bin_ntoh32(uint32_t n32) { return cd_bin_hton32(n32); } uint16_t cd_bin_ntoh16(uint16_t n16) { return cd_bin_hton16(n16); } -uint32_t cd_bin_ntohd(uint8_t* n, size_t s) { return cd_bin_htond(n, s); } +uint32_t cd_bin_ntohd(uint8_t *n, size_t s) { return cd_bin_htond(n, s); } -uint32_t cd_bin_htond(uint8_t* h, size_t s) { +uint32_t cd_bin_htond(uint8_t *h, size_t s) { if (s == sizeof(uint8_t)) return *h; - else if (s == sizeof(uint16_t)) return cd_bin_hton16(*(uint16_t*) h); - else if (s == sizeof(uint32_t)) return cd_bin_hton32(*(uint32_t*) h); + else if (s == sizeof(uint16_t)) return cd_bin_hton16(*(uint16_t *)h); + else if (s == sizeof(uint32_t)) return cd_bin_hton32(*(uint32_t *)h); else return 0; } diff --git a/shared/bin.h b/shared/bin.h index 4a932c2..8aab08f 100644 --- a/shared/bin.h +++ b/shared/bin.h @@ -10,6 +10,7 @@ */ #include <stdint.h> +#include <stdlib.h> #include "memory.h" @@ -22,11 +23,12 @@ extern uint8_t g_cd_endianness; /** @brief cast `in.data` to `type out` */ #define CD_CAST_BIN(type, in, out) type *out = (type *)&in->data; #define CD_CREATE_MSG_BIN(type, normal, bin) CD_CREATE_MSG_SIZE_BIN(type, sizeof(type), normal, bin) -/** @brief */ -#define CD_CREATE_MSG_SIZE_BIN(type, size, normal, bin) \ - cd_s_bin *bin = CD_MALLOC(sizeof(cd_s_bin) + size); \ - bin->bytes = size; \ +/** @brief create cd_s_bin struct with space for struct and cast data field of cd_s_bin to struct pointer */ +#define CD_CREATE_MSG_SIZE_BIN(type, size, normal, bin) \ + cd_s_bin *bin = CD_MALLOC(sizeof(cd_s_bin) + size); \ + bin->bytes = size; \ type *normal = (type *)&bin->data; +// TODO: document above macro but better /** @brief hold binary data with fixed size */ typedef struct { @@ -58,14 +60,14 @@ uint16_t cd_bin_ntoh16(uint16_t n16); * * @param n pointer to number * @param s size of number in bytes - * + * * @return 32-bit integer regardless of `s` * * this function is exclusively used by the CD_DYN_MEMBER_SIZEOF macro in * shared/protocol.c */ -uint32_t cd_bin_ntohd(uint8_t* n, size_t s); -uint32_t cd_bin_htond(uint8_t* h, size_t s); +uint32_t cd_bin_ntohd(uint8_t *n, size_t s); +uint32_t cd_bin_htond(uint8_t *h, size_t s); /** @brief replace 32-bit value from host endian to network (big-endian) */ void cd_bin_repl_hton32(uint32_t *h32); @@ -79,4 +81,3 @@ void cd_bin_repl_ntoh16(uint16_t *n16); #ifdef __cplusplus } #endif - diff --git a/shared/consts.h b/shared/consts.h index 57426bf..b68c1a0 100644 --- a/shared/consts.h +++ b/shared/consts.h @@ -8,4 +8,3 @@ #define CD_ERROR_BUFFER_SIZE 16 /** @brief size of the serial communication buffer (in messages, not bytes) */ #define CD_SERCOMM_BUFFER_SIZE 16 - diff --git a/shared/pclient.c b/shared/pclient.c index a7d9b00..6dc431c 100644 --- a/shared/pclient.c +++ b/shared/pclient.c @@ -1,51 +1,51 @@ #include <string.h> -#include "protocol.h" -#include "pclient.h" #include "bin.h" +#include "pclient.h" +#include "protocol.h" #ifdef __cplusplus extern "C" { #endif -cd_s_bin* cd_cmd_gen_ping() { +cd_s_bin *cd_cmd_gen_ping() { CD_CREATE_MSG_BIN(cd_s_cmd_ping, msg, bin); msg->opcode = CD_CMD_PING; - msg->id = cd_bin_hton16(cd_protocol_fresh_message_id()); + msg->id = cd_bin_hton16(cd_protocol_fresh_message_id()); return bin; } -cd_s_bin* cd_cmd_gen_get_node(bool all, cd_uuid_t uuid) { +cd_s_bin *cd_cmd_gen_get_node(bool all, cd_uuid_t uuid) { CD_CREATE_MSG_BIN(cd_s_cmd_get_node, msg, bin); msg->opcode = CD_CMD_GET_NODE; - msg->id = cd_bin_hton16(cd_protocol_fresh_message_id()); - msg->all = all; + msg->id = cd_bin_hton16(cd_protocol_fresh_message_id()); + msg->all = all; if (uuid != NULL) memcpy(msg->uuid, uuid, sizeof(cd_uuid_t)); else memset(msg->uuid, 0, sizeof(cd_uuid_t)); return bin; } -cd_s_bin* cd_cmd_gen_post_led(bool on, cd_uuid_t uuid) { +cd_s_bin *cd_cmd_gen_post_led(bool on, cd_uuid_t uuid) { CD_CREATE_MSG_BIN(cd_s_cmd_post_led, msg, bin); msg->opcode = CD_CMD_POST_LED; - msg->id = cd_bin_hton16(cd_protocol_fresh_message_id()); - msg->on = on; + msg->id = cd_bin_hton16(cd_protocol_fresh_message_id()); + msg->on = on; memcpy(msg->uuid, uuid, sizeof(cd_uuid_t)); return bin; } -cd_s_bin* cd_cmd_gen_post_link_add(cd_uuid_t button, cd_uuid_t light, cd_e_cmd_link_type type) { +cd_s_bin *cd_cmd_gen_post_link_add(cd_uuid_t button, cd_uuid_t light, cd_e_cmd_link_type type) { CD_CREATE_MSG_BIN(cd_s_cmd_post_link, msg, bin); msg->opcode = CD_CMD_POST_LINK; - msg->id = cd_bin_hton16(cd_protocol_fresh_message_id()); - msg->add = true; + msg->id = cd_bin_hton16(cd_protocol_fresh_message_id()); + msg->add = true; memcpy(msg->button, button, sizeof(cd_uuid_t)); memcpy(msg->led, light, sizeof(cd_uuid_t)); msg->type = type; @@ -53,12 +53,12 @@ cd_s_bin* cd_cmd_gen_post_link_add(cd_uuid_t button, cd_uuid_t light, cd_e_cmd_l return bin; } -cd_s_bin* cd_cmd_gen_post_link_rm(cd_uuid_t button, cd_uuid_t light) { +cd_s_bin *cd_cmd_gen_post_link_rm(cd_uuid_t button, cd_uuid_t light) { CD_CREATE_MSG_BIN(cd_s_cmd_post_link, msg, bin); msg->opcode = CD_CMD_POST_LINK; - msg->id = cd_bin_hton16(cd_protocol_fresh_message_id()); - msg->add = false; + msg->id = cd_bin_hton16(cd_protocol_fresh_message_id()); + msg->add = false; memcpy(msg->button, button, sizeof(cd_uuid_t)); memcpy(msg->led, light, sizeof(cd_uuid_t)); msg->type = 0; @@ -66,70 +66,70 @@ cd_s_bin* cd_cmd_gen_post_link_rm(cd_uuid_t button, cd_uuid_t light) { return bin; } -cd_s_bin* cd_cmd_gen_post_net_add(cd_uuid_t uuid) { +cd_s_bin *cd_cmd_gen_post_net_add(cd_uuid_t uuid) { CD_CREATE_MSG_BIN(cd_s_cmd_post_net, msg, bin); msg->opcode = CD_CMD_POST_NET; - msg->id = cd_bin_hton16(cd_protocol_fresh_message_id()); - msg->join = true; + msg->id = cd_bin_hton16(cd_protocol_fresh_message_id()); + msg->join = true; memcpy(msg->uuid, uuid, sizeof(cd_uuid_t)); return bin; } -cd_s_bin* cd_cmd_gen_post_net_rm(cd_uuid_t uuid) { +cd_s_bin *cd_cmd_gen_post_net_rm(cd_uuid_t uuid) { CD_CREATE_MSG_BIN(cd_s_cmd_post_net, msg, bin); msg->opcode = CD_CMD_POST_NET; - msg->id = cd_bin_hton16(cd_protocol_fresh_message_id()); - msg->join = false; + msg->id = cd_bin_hton16(cd_protocol_fresh_message_id()); + msg->join = false; memcpy(msg->uuid, uuid, sizeof(cd_uuid_t)); return bin; } -cd_s_bin* cd_cmd_res_status(cd_e_scmds cmd, cd_cmd_id_t id, bool error) { +cd_s_bin *cd_cmd_res_status(cd_e_scmds cmd, cd_cmd_id_t id, bool error) { CD_CREATE_MSG_BIN(cd_s_cmd_response, msg, bin); - msg->opcode = CD_CMD_RESPONSE; - msg->id = cd_bin_hton16(cd_protocol_fresh_message_id()); + msg->opcode = CD_CMD_RESPONSE; + msg->id = cd_bin_hton16(cd_protocol_fresh_message_id()); msg->response_type = cmd; - msg->response_id = id; - msg->error = error; + msg->response_id = id; + msg->error = error; msg->response_size = 0; return bin; } -cd_s_bin* cd_cmd_res(cd_e_scmds cmd, cd_cmd_id_t id, uint16_t len, uint8_t* data) { +cd_s_bin *cd_cmd_res(cd_e_scmds cmd, cd_cmd_id_t id, uint16_t len, uint8_t *data) { CD_CREATE_MSG_SIZE_BIN(cd_s_cmd_response, sizeof(cd_s_cmd_response) + len, msg, bin); - msg->opcode = CD_CMD_RESPONSE; - msg->id = cd_bin_hton16(cd_protocol_fresh_message_id()); + msg->opcode = CD_CMD_RESPONSE; + msg->id = cd_bin_hton16(cd_protocol_fresh_message_id()); msg->response_type = cmd; - msg->response_id = id; - msg->error = false; + msg->response_id = id; + msg->error = false; msg->response_size = cd_bin_hton16(len); memcpy(msg->response_info, data, len); return bin; } -cd_s_cmd_node* cd_cmd_node_alloc(const char* name, cd_s_cmd_node base, uint16_t link_count, cd_uuid_t* links) { - size_t name_len = strlen(name); - size_t links_size = sizeof(cd_uuid_t) * link_count; +cd_s_cmd_node *cd_cmd_node_alloc(const char *name, cd_s_cmd_node base, uint16_t link_count, cd_uuid_t *links) { + size_t name_len = strlen(name); + size_t links_size = sizeof(cd_uuid_t) * link_count; size_t remaining_size = sizeof(char) * name_len + links_size; - cd_s_cmd_node* node = CD_MALLOC(sizeof(cd_s_cmd_node) + remaining_size); - + cd_s_cmd_node *node = CD_MALLOC(sizeof(cd_s_cmd_node) + remaining_size); + memcpy(node->uuid, base.uuid, sizeof(cd_uuid_t)); memcpy(node->address, base.address, sizeof(cd_mac_addr_t)); - node->name_len = name_len; - node->light_on = base.light_on; - node->provisioned = base.provisioned; - node->button_pub = cd_bin_hton32(base.button_pub); - node->link_count = cd_bin_hton16(link_count); + node->name_len = name_len; + node->light_on = base.light_on; + node->provisioned = base.provisioned; + node->button_pub = cd_bin_hton32(base.button_pub); + node->link_count = cd_bin_hton16(link_count); node->remaining_size = cd_bin_hton16(remaining_size); - void* cursor = (void*) &node->remaining_data[0]; + void *cursor = (void *)&node->remaining_data[0]; memcpy(cursor, name, name_len); // copy name cursor += name_len; memcpy(cursor, links, links_size); // copy links @@ -137,18 +137,18 @@ cd_s_cmd_node* cd_cmd_node_alloc(const char* name, cd_s_cmd_node base, uint16_t return node; } -cd_s_cmd_response_get_node* cd_cmd_get_node_res_from_node_arr(uint16_t size, cd_s_cmd_node* arr[]) { +cd_s_cmd_response_get_node *cd_cmd_get_node_res_from_node_arr(uint16_t size, cd_s_cmd_node *arr[]) { size_t remaining_size = 0; for (unsigned int i = 0; i < size; i++) { remaining_size += sizeof(cd_s_cmd_node) + cd_bin_ntoh16(arr[i]->remaining_size); } - cd_s_cmd_response_get_node* response = CD_MALLOC(sizeof(cd_s_cmd_response_get_node) + remaining_size); - response->node_count = cd_bin_hton16(size); - response->remaining_size = cd_bin_hton16(remaining_size); + cd_s_cmd_response_get_node *response = CD_MALLOC(sizeof(cd_s_cmd_response_get_node) + remaining_size); + response->node_count = cd_bin_hton16(size); + response->remaining_size = cd_bin_hton16(remaining_size); - void* cursor = response->nodes; + void *cursor = response->nodes; for (unsigned int i = 0; i < size; i++) { size_t size = sizeof(cd_s_cmd_node) + cd_bin_ntoh16(arr[i]->remaining_size); memcpy(cursor, arr[i], size); @@ -161,4 +161,3 @@ cd_s_cmd_response_get_node* cd_cmd_get_node_res_from_node_arr(uint16_t size, cd_ #ifdef __cplusplus } #endif - diff --git a/shared/pclient.h b/shared/pclient.h index 03a8a25..f53ff39 100644 --- a/shared/pclient.h +++ b/shared/pclient.h @@ -11,45 +11,45 @@ extern "C" { #endif /** @brief send data over platform standard serial out (doesn't free `data`) */ -void cd_pclient_send(cd_s_bin* data); +void cd_pclient_send(cd_s_bin *data); /** @brief generate PING command */ -cd_s_bin* cd_cmd_gen_ping(); +cd_s_bin *cd_cmd_gen_ping(); /** * @brief generate GET_NODE command * @param all get all nodes * @param uuid get specific node by uuid */ -cd_s_bin* cd_cmd_gen_get_node(bool all, cd_uuid_t uuid); +cd_s_bin *cd_cmd_gen_get_node(bool all, cd_uuid_t uuid); /** * @brief generate POST_LED command * @param on light status * @param uuid node to set light of */ -cd_s_bin* cd_cmd_gen_post_led(bool on, cd_uuid_t uuid); +cd_s_bin *cd_cmd_gen_post_led(bool on, cd_uuid_t uuid); /** * @brief generate POST_LINK command to add or update link * @param button button node uuid * @param light light node uuid * @param type type of link to set */ -cd_s_bin* cd_cmd_gen_post_link_add(cd_uuid_t button, cd_uuid_t light, cd_e_cmd_link_type type); +cd_s_bin *cd_cmd_gen_post_link_add(cd_uuid_t button, cd_uuid_t light, cd_e_cmd_link_type type); /** * @brief generate POST_LINK command to remove link * @param button button node uuid * @param light light node uuid */ -cd_s_bin* cd_cmd_gen_post_link_rm(cd_uuid_t button, cd_uuid_t light); +cd_s_bin *cd_cmd_gen_post_link_rm(cd_uuid_t button, cd_uuid_t light); /** * @brief generate POST_NET command to provision node into network * @param uuid node uuid */ -cd_s_bin* cd_cmd_gen_post_net_add(cd_uuid_t uuid); +cd_s_bin *cd_cmd_gen_post_net_add(cd_uuid_t uuid); /** * @brief generate POST_NET command to provision node out of network * @param uuid node uuid */ -cd_s_bin* cd_cmd_gen_post_net_rm(cd_uuid_t uuid); +cd_s_bin *cd_cmd_gen_post_net_rm(cd_uuid_t uuid); /** * @brief generate generic RESPONSE command with error field and no response_info @@ -57,7 +57,7 @@ cd_s_bin* cd_cmd_gen_post_net_rm(cd_uuid_t uuid); * @param id original command id * @param error `true` if some error occurred */ -cd_s_bin* cd_cmd_res_status(cd_e_scmds cmd, cd_cmd_id_t id, bool error); +cd_s_bin *cd_cmd_res_status(cd_e_scmds cmd, cd_cmd_id_t id, bool error); /** * @brief generate RESPONSE command with response_info * @param cmd original command opcode @@ -65,13 +65,13 @@ cd_s_bin* cd_cmd_res_status(cd_e_scmds cmd, cd_cmd_id_t id, bool error); * @param len size of `data` in bytes * @param data pointer to data */ -cd_s_bin* cd_cmd_res(cd_e_scmds cmd, cd_cmd_id_t id, uint16_t len, uint8_t* data); +cd_s_bin *cd_cmd_res(cd_e_scmds cmd, cd_cmd_id_t id, uint16_t len, uint8_t *data); /** * @brief generate cd_s_cmd_response_get_node struct from array of cd_s_cmd_node pointers * @param size length of array * @param arr array of pointer to cd_s_cmd_node */ -cd_s_cmd_response_get_node* cd_cmd_get_node_res_from_node_arr(uint16_t size, cd_s_cmd_node* arr[]); +cd_s_cmd_response_get_node *cd_cmd_get_node_res_from_node_arr(uint16_t size, cd_s_cmd_node *arr[]); /** * @brief allocate and fill cd_s_cmd_node struct * @@ -80,7 +80,7 @@ cd_s_cmd_response_get_node* cd_cmd_get_node_res_from_node_arr(uint16_t size, cd_ * @param link_count amount of lights this node controls * @param links array of light node uuids */ -cd_s_cmd_node* cd_cmd_node_alloc(const char* name, cd_s_cmd_node base, uint16_t link_count, cd_uuid_t* links); +cd_s_cmd_node *cd_cmd_node_alloc(const char *name, cd_s_cmd_node base, uint16_t link_count, cd_uuid_t *links); #define cd_remaining_sizeof(type, input_struct) ((sizeof(type) + cd_bin_ntoh16(input_struct->remaining_size)) /* NOLINT */) #define cd_cmd_node_sizeof(node) (cd_remaining_sizeof(cd_s_cmd_node, node) /* NOLINT */) diff --git a/shared/protocol.c b/shared/protocol.c index 943604c..c1217b2 100644 --- a/shared/protocol.c +++ b/shared/protocol.c @@ -6,14 +6,14 @@ extern "C" { #endif -cd_cmd_id_t g_cd_protocol_fresh_message_id = 0; +cd_cmd_id_t g_cd_protocol_fresh_message_id = 0; size_t cd_cmd_sizeof(uint8_t data[CD_SERIAL_READ_BUFFER_SIZE], uint8_t data_length) { cd_cmd_opcode_t opcode = data[0]; if (CD_CMD_HANDLERS_SIZE[opcode] > 0) return CD_CMD_HANDLERS_SIZE[opcode]; cd_s_bin *copy = cd_bin_s_alloc(data_length, data); - size_t length = (*CD_CMD_HANDLERS_SIZEOF[opcode])(copy); + size_t length = (*CD_CMD_HANDLERS_SIZEOF[opcode])(copy); CD_FREE(copy); @@ -39,19 +39,14 @@ size_t cd_cmd_sizeof(uint8_t data[CD_SERIAL_READ_BUFFER_SIZE], uint8_t data_leng * size += cd_bin_ntohd(&data->data[dyn_member_offset], dyn_member_size); * return size; */ -#define CD_DYN_MEMBER_SIZEOF(data, struct_t, length_field) \ - sizeof(struct_t) + ( \ - (data->bytes >= (offsetof(struct_t, length_field) + sizeof(((struct_t*)0)->length_field))) ? \ - (cd_bin_ntohd(&data->data[offsetof(struct_t, length_field)], sizeof(((struct_t*)0)->length_field))) :\ - 0); - -size_t cd_cmd_response_sizeof(cd_s_bin* data) { - return CD_DYN_MEMBER_SIZEOF(data, cd_s_cmd_response, response_size); -} +#define CD_DYN_MEMBER_SIZEOF(data, struct_t, length_field) \ + sizeof(struct_t) + ((data->bytes >= (offsetof(struct_t, length_field) + sizeof(((struct_t *)0)->length_field))) \ + ? (cd_bin_ntohd(&data->data[offsetof(struct_t, length_field)], sizeof(((struct_t *)0)->length_field))) \ + : 0); -cd_cmd_id_t cd_protocol_fresh_message_id() { - return g_cd_protocol_fresh_message_id++; -} +size_t cd_cmd_response_sizeof(cd_s_bin *data) { return CD_DYN_MEMBER_SIZEOF(data, cd_s_cmd_response, response_size); } + +cd_cmd_id_t cd_protocol_fresh_message_id() { return g_cd_protocol_fresh_message_id++; } #ifdef __cplusplus } diff --git a/shared/protocol.h b/shared/protocol.h index b9a2c93..5f2370f 100644 --- a/shared/protocol.h +++ b/shared/protocol.h @@ -30,7 +30,7 @@ typedef uint16_t cd_cmd_id_t; typedef uint8_t cd_cmd_bool_t; /** @brief cmd handler function signature */ -typedef void (cd_cmd_handler_t)(cd_s_bin *data); +typedef void(cd_cmd_handler_t)(cd_s_bin *data); /** @brief used for numbering messages */ extern cd_cmd_id_t g_cd_protocol_fresh_message_id; @@ -42,81 +42,81 @@ cd_cmd_id_t cd_protocol_fresh_message_id(); typedef struct { cd_cmd_opcode_t opcode; /** @brief cmd opcode */ - cd_cmd_id_t id; /** @brief message id */ + cd_cmd_id_t id; /** @brief message id */ } cd_s_cmd_ping; typedef struct { cd_cmd_opcode_t opcode; /** @brief cmd opcode */ - cd_cmd_id_t id; /** @brief message id */ - cd_cmd_bool_t all; /** @brief get all known nodes */ - cd_uuid_t uuid; /** @brief node uuid to get details from, ignored if `all` = `true` */ + cd_cmd_id_t id; /** @brief message id */ + cd_cmd_bool_t all; /** @brief get all known nodes */ + cd_uuid_t uuid; /** @brief node uuid to get details from, ignored if `all` = `true` */ } cd_s_cmd_get_node; typedef struct { cd_cmd_opcode_t opcode; /** @brief cmd opcode */ - cd_cmd_id_t id; /** @brief message id */ - cd_cmd_bool_t on; /** @brief new led status */ - cd_uuid_t uuid; /** @brief node uuid to set */ + cd_cmd_id_t id; /** @brief message id */ + cd_cmd_bool_t on; /** @brief new led status */ + cd_uuid_t uuid; /** @brief node uuid to set */ } cd_s_cmd_post_led; typedef enum { - CD_CMD_LINK_TYPE_TOGGLE = 0x00, /** @brief button toggles light */ - CD_CMD_LINK_TYPE_TURN_ON = 0x01, /** @brief button always turns on light (regardless of previous state) */ + CD_CMD_LINK_TYPE_TOGGLE = 0x00, /** @brief button toggles light */ + CD_CMD_LINK_TYPE_TURN_ON = 0x01, /** @brief button always turns on light (regardless of previous state) */ CD_CMD_LINK_TYPE_TURN_OFF = 0x02, /** @brief button always turns off light (regardless of previous state) */ } cd_e_cmd_link_type; typedef struct { cd_cmd_opcode_t opcode; /** @brief cmd opcode */ - cd_cmd_id_t id; /** @brief message id */ - cd_uuid_t button; /** @brief uuid of button node */ - cd_uuid_t led; /** @brief uuid of led node */ - cd_cmd_bool_t add; /** @brief `true` to create/overwrite link, `false` to remove link */ - uint8_t type; /** @brief link type cd_e_cmd_link_type */ + cd_cmd_id_t id; /** @brief message id */ + cd_uuid_t button; /** @brief uuid of button node */ + cd_uuid_t led; /** @brief uuid of led node */ + cd_cmd_bool_t add; /** @brief `true` to create/overwrite link, `false` to remove link */ + uint8_t type; /** @brief link type cd_e_cmd_link_type */ } cd_s_cmd_post_link; typedef struct { cd_cmd_opcode_t opcode; /** @brief cmd opcode */ - cd_cmd_id_t id; /** @brief message id */ - cd_uuid_t uuid; /** @brief node uuid */ - cd_cmd_bool_t join; /** @brief `true` to join network, `false` to leave network */ + cd_cmd_id_t id; /** @brief message id */ + cd_uuid_t uuid; /** @brief node uuid */ + cd_cmd_bool_t join; /** @brief `true` to join network, `false` to leave network */ } cd_s_cmd_post_net; /** @brief protocol node */ typedef struct { - cd_uuid_t uuid; /** @brief node network uuid */ - cd_mac_addr_t address; /** @brief node bluetooth mac address */ - uint8_t name_len; /** @brief name length in bytes */ - cd_cmd_bool_t light_on; /** @brief state of light on node */ - cd_cmd_bool_t provisioned; /** @brief whether the node is provisioned into the network */ - cd_mesh_psub_addr button_pub; /** @brief button publish address */ - uint16_t link_count; /** @brief amount of addresses to publish button press to */ - uint16_t remaining_size; /** @brief calculated size of remaining_data for convenience */ + cd_uuid_t uuid; /** @brief node network uuid */ + cd_mac_addr_t address; /** @brief node bluetooth mac address */ + uint8_t name_len; /** @brief name length in bytes */ + cd_cmd_bool_t light_on; /** @brief state of light on node */ + cd_cmd_bool_t provisioned; /** @brief whether the node is provisioned into the network */ + cd_mesh_psub_addr button_pub; /** @brief button publish address */ + uint16_t link_count; /** @brief amount of addresses to publish button press to */ + uint16_t remaining_size; /** @brief calculated size of remaining_data for convenience */ const uint8_t remaining_data[]; /** * @brief remaining data (name and link array) * * this data is stored adjacently in memory * and is cast when reading/writing this * struct - * + * * 1. char[] name * 2. cd_uuid_t[] light_publish_addresses; */ } cd_s_cmd_node; typedef struct { - uint16_t node_count; /** amount of nodes in nodes[] */ + uint16_t node_count; /** amount of nodes in nodes[] */ uint16_t remaining_size; /** remaining size (for convenience) */ - cd_s_cmd_node nodes[]; /** nodes adjacent in memory (should be accessed using pointer arithmetic) */ + cd_s_cmd_node nodes[]; /** nodes adjacent in memory (should be accessed using pointer arithmetic) */ } cd_s_cmd_response_get_node; typedef struct { - cd_cmd_opcode_t opcode; /** @brief cmd opcode */ - cd_cmd_id_t id; /** @brief response message id */ - cd_cmd_bool_t error; /** @brief `true` if some error occurred */ + cd_cmd_opcode_t opcode; /** @brief cmd opcode */ + cd_cmd_id_t id; /** @brief response message id */ + cd_cmd_bool_t error; /** @brief `true` if some error occurred */ cd_cmd_opcode_t response_type; /** @brief response type, used to cast type of `response_info` */ - cd_cmd_id_t response_id; /** @brief original message id */ - uint16_t response_size; /** @brief size of remaining response */ - uint8_t response_info[]; /** @brief (CAST) remaining response struct, not read if `response_size`=`0` */ + cd_cmd_id_t response_id; /** @brief original message id */ + uint16_t response_size; /** @brief size of remaining response */ + uint8_t response_info[]; /** @brief (CAST) remaining response struct, not read if `response_size`=`0` */ } cd_s_cmd_response; #pragma pack(pop) @@ -127,55 +127,37 @@ void cd_cmd_handle(uint8_t data[CD_SERIAL_READ_BUFFER_SIZE], uint8_t length); size_t cd_cmd_sizeof(uint8_t data[CD_SERIAL_READ_BUFFER_SIZE], uint8_t length); /** @brief calculate dynamic size of response message */ -size_t cd_cmd_response_sizeof(cd_s_bin* data); +size_t cd_cmd_response_sizeof(cd_s_bin *data); // down here is the garbage manual duplicate code section for constants #define CD_CMD_COUNT 6 typedef enum { - CD_CMD_PING = 0x00, /** @brief send ping */ - CD_CMD_GET_NODE = 0x01, /** @brief get node(s) */ - CD_CMD_POST_LED = 0x02, /** @brief set led */ + CD_CMD_PING = 0x00, /** @brief send ping */ + CD_CMD_GET_NODE = 0x01, /** @brief get node(s) */ + CD_CMD_POST_LED = 0x02, /** @brief set led */ CD_CMD_POST_LINK = 0x03, /** @brief set/remove link */ - CD_CMD_POST_NET = 0x04, /** @brief (un)register node with network */ - CD_CMD_RESPONSE = 0x05, /** @brief response message */ + CD_CMD_POST_NET = 0x04, /** @brief (un)register node with network */ + CD_CMD_RESPONSE = 0x05, /** @brief response message */ } cd_e_scmds; -cd_cmd_handler_t cd_cmd_ping, - cd_cmd_get_node, - cd_cmd_post_led, - cd_cmd_post_link, - cd_cmd_post_net, - cd_cmd_response; +cd_cmd_handler_t cd_cmd_ping, cd_cmd_get_node, cd_cmd_post_led, cd_cmd_post_link, cd_cmd_post_net, cd_cmd_response; /** @brief constant message sizes, 0 for dynamic size */ static const size_t CD_CMD_HANDLERS_SIZE[CD_CMD_COUNT] = { - [CD_CMD_PING] = sizeof(cd_s_cmd_ping), - [CD_CMD_GET_NODE] = sizeof(cd_s_cmd_get_node), - [CD_CMD_POST_LED] = sizeof(cd_s_cmd_post_led), - [CD_CMD_POST_LINK] = sizeof(cd_s_cmd_post_link), - [CD_CMD_POST_NET] = sizeof(cd_s_cmd_post_net), - [CD_CMD_RESPONSE] = 0, + [CD_CMD_PING] = sizeof(cd_s_cmd_ping), [CD_CMD_GET_NODE] = sizeof(cd_s_cmd_get_node), [CD_CMD_POST_LED] = sizeof(cd_s_cmd_post_led), + [CD_CMD_POST_LINK] = sizeof(cd_s_cmd_post_link), [CD_CMD_POST_NET] = sizeof(cd_s_cmd_post_net), [CD_CMD_RESPONSE] = 0, }; /** @brief constant message sizes, 0 for dynamic size */ -static size_t (* const CD_CMD_HANDLERS_SIZEOF[CD_CMD_COUNT])(cd_s_bin*) = { - [CD_CMD_PING] = NULL, - [CD_CMD_GET_NODE] = NULL, - [CD_CMD_POST_LED] = NULL, - [CD_CMD_POST_LINK] = NULL, - [CD_CMD_POST_NET] = NULL, - [CD_CMD_RESPONSE] = &cd_cmd_response_sizeof, +static size_t (*const CD_CMD_HANDLERS_SIZEOF[CD_CMD_COUNT])(cd_s_bin *) = { + [CD_CMD_PING] = NULL, [CD_CMD_GET_NODE] = NULL, [CD_CMD_POST_LED] = NULL, [CD_CMD_POST_LINK] = NULL, [CD_CMD_POST_NET] = NULL, [CD_CMD_RESPONSE] = &cd_cmd_response_sizeof, }; /** @brief stores message handlers in array with opcode as index */ -static cd_cmd_handler_t* const CD_CMD_HANDLERS[CD_CMD_COUNT] = { - [CD_CMD_PING] = &cd_cmd_ping, - [CD_CMD_GET_NODE] = &cd_cmd_get_node, - [CD_CMD_POST_LED] = &cd_cmd_post_led, - [CD_CMD_POST_LINK] = &cd_cmd_post_link, - [CD_CMD_POST_NET] = &cd_cmd_post_net, - [CD_CMD_RESPONSE] = &cd_cmd_response, +static cd_cmd_handler_t *const CD_CMD_HANDLERS[CD_CMD_COUNT] = { + [CD_CMD_PING] = &cd_cmd_ping, [CD_CMD_GET_NODE] = &cd_cmd_get_node, [CD_CMD_POST_LED] = &cd_cmd_post_led, + [CD_CMD_POST_LINK] = &cd_cmd_post_link, [CD_CMD_POST_NET] = &cd_cmd_post_net, [CD_CMD_RESPONSE] = &cd_cmd_response, }; #ifdef __cplusplus diff --git a/shared/serial_parse.c b/shared/serial_parse.c index 1397918..f580fee 100644 --- a/shared/serial_parse.c +++ b/shared/serial_parse.c @@ -1,5 +1,5 @@ -#include <string.h> #include <memory.h> +#include <string.h> #include "consts.h" #include "serial_parse.h" @@ -45,10 +45,10 @@ bool cd_serial_parse(uint8_t byte) { } void cd_cmd_handle(uint8_t data[CD_SERIAL_READ_BUFFER_SIZE], uint8_t data_length) { - cd_s_bin *copy = cd_bin_s_alloc(data_length, data); + cd_s_bin *copy = cd_bin_s_alloc(data_length, data); if (data[0] >= CD_CMD_COUNT) return; - cd_cmd_handler_t* handler = CD_CMD_HANDLERS[data[0]]; + cd_cmd_handler_t *handler = CD_CMD_HANDLERS[data[0]]; if (handler == NULL) return; (*handler)(copy); @@ -59,4 +59,3 @@ void cd_cmd_handle(uint8_t data[CD_SERIAL_READ_BUFFER_SIZE], uint8_t data_length #ifdef __cplusplus } #endif - diff --git a/shared/serial_parse.h b/shared/serial_parse.h index 1e9c404..2174436 100644 --- a/shared/serial_parse.h +++ b/shared/serial_parse.h @@ -2,8 +2,8 @@ /** @file serial_parse.h */ -#include <stdint.h> #include <stdbool.h> +#include <stdint.h> #include "protocol.h" @@ -23,4 +23,3 @@ bool cd_serial_parse(uint8_t byte); #ifdef __cplusplus } #endif - |