aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorlonkaars <loek@pipeframe.xyz>2023-01-05 13:51:33 +0100
committerlonkaars <loek@pipeframe.xyz>2023-01-05 13:51:33 +0100
commit4e4b35df42044c0f72a6f50a7f0158db4f379748 (patch)
tree2a65d4cd40f13435cd96e6e2abf285601fa0eff6
parent2c2ff814653f37947f8c253b29f8b08520ae71b7 (diff)
fix `cd_s_cmd_node` alloc/create function
-rw-r--r--confui/makefile3
-rw-r--r--confui/serial.cpp9
-rw-r--r--shared/pclient.c18
-rw-r--r--shared/pclient.h4
-rw-r--r--shared/protocol.h4
5 files changed, 23 insertions, 15 deletions
diff --git a/confui/makefile b/confui/makefile
index e1faed6..2e22e20 100644
--- a/confui/makefile
+++ b/confui/makefile
@@ -18,3 +18,6 @@ FMT_FILES := $(filter-out %.pro,$(FMT_FILES)) # filter *.pro
format:
clang-format -i $(FMT_FILES)
clang-tidy --fix-errors $(FMT_FILES)
+
+compile_commands:
+ compiledb make -Bn
diff --git a/confui/serial.cpp b/confui/serial.cpp
index 09bd7db..c0e686b 100644
--- a/confui/serial.cpp
+++ b/confui/serial.cpp
@@ -100,17 +100,18 @@ void cd_cmd_ping(cd_s_bin* data) {
cd_uuid_t light_addrs[] = {
{ 0xff, 0xff, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00, 0xde, 0xad, 0xbe, 0xef, 0x00, 0x00, 0x00, 0x00 },
};
- cd_s_cmd_node* test = cd_cmd_node_alloc({
+ cd_s_cmd_node* test = cd_cmd_node_alloc("gert", {
.uuid = { 0xff, 0xff, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00, 0xde, 0xad, 0xbe, 0xef, 0x00, 0x00, 0x00, 0x00 },
.address = { 0xff, 0x00, 0xff, 0x00, 0xff, 0x00, },
.light_on = false,
.provisioned = false,
.button_pub = 0xdeadbeef,
- }, "gert", 1, light_addrs);
- cd_s_bin* testres = cd_cmd_res(CD_CMD_GET_NODE, 0xf88f, test->size, (uint8_t*) test);
+ }, 1, light_addrs);
+ cd_s_bin* testres = cd_cmd_res(CD_CMD_GET_NODE, 0xf88f, cd_cmd_node_sizeof(test), (uint8_t*) test);
+ free(test);
+
cd_pclient_send(testres);
free(testres);
- free(test);
}
void cd_cmd_response(cd_s_bin* data) {
diff --git a/shared/pclient.c b/shared/pclient.c
index 0464428..be0e0a7 100644
--- a/shared/pclient.c
+++ b/shared/pclient.c
@@ -107,17 +107,17 @@ cd_s_bin* cd_cmd_res(cd_e_scmds cmd, cd_cmd_id_t id, uint16_t len, uint8_t* data
msg->response_type = cmd;
msg->response_id = id;
msg->error = false;
- msg->response_size = len;
+ msg->response_size = cd_bin_hton16(len);
memcpy(msg->response_info, data, len);
return bin;
}
-cd_s_cmd_node* cd_cmd_node_alloc(cd_s_cmd_node base, const char* name, 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) {
size_t name_len = strlen(name);
- size_t links_len = sizeof(cd_uuid_t) * link_count;
- size_t size = sizeof(cd_s_cmd_node) + sizeof(char) * name_len + links_len;
- cd_s_cmd_node* node = malloc(size);
+ 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 = 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));
@@ -126,9 +126,11 @@ cd_s_cmd_node* cd_cmd_node_alloc(cd_s_cmd_node base, const char* name, uint16_t
node->provisioned = base.provisioned;
node->button_pub = cd_bin_hton32(base.button_pub);
node->link_count = cd_bin_hton16(link_count);
- node->size = cd_bin_hton16(size);
- memcpy((void *)&node->data_remainder, name, name_len); // copy name
- memcpy((void *)&node->data_remainder + name_len, links, links_len); // copy links
+ node->remaining_size = cd_bin_hton16(remaining_size);
+ void* cursor = (void*) &node->remaining_data[0];
+ memcpy(cursor, name, name_len); // copy name
+ cursor += name_len;
+ memcpy(cursor, links, links_size); // copy links
return node;
}
diff --git a/shared/pclient.h b/shared/pclient.h
index d0ddeb7..e1bb7e7 100644
--- a/shared/pclient.h
+++ b/shared/pclient.h
@@ -74,7 +74,9 @@ cd_s_bin* cd_cmd_res(cd_e_scmds cmd, cd_cmd_id_t id, uint16_t len, uint8_t* data
* @param link_count amount of lights this node controls
* @param links array of light node uuids
*/
-cd_s_cmd_node* cd_cmd_node_alloc(cd_s_cmd_node base, const char* name, 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_cmd_node_sizeof(node) ((sizeof(cd_s_cmd_node) + cd_bin_ntoh16(node->remaining_size)) /* NOLINT */)
#ifdef __cplusplus
}
diff --git a/shared/protocol.h b/shared/protocol.h
index 6f9697f..dd5bcc6 100644
--- a/shared/protocol.h
+++ b/shared/protocol.h
@@ -90,8 +90,8 @@ typedef struct {
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 size; /** @brief calculated size for convenience */
- const uint8_t data_remainder[]; /**
+ 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