diff options
author | lonkaars <loek@pipeframe.xyz> | 2023-01-05 13:51:33 +0100 |
---|---|---|
committer | lonkaars <loek@pipeframe.xyz> | 2023-01-05 13:51:33 +0100 |
commit | 4e4b35df42044c0f72a6f50a7f0158db4f379748 (patch) | |
tree | 2a65d4cd40f13435cd96e6e2abf285601fa0eff6 /shared | |
parent | 2c2ff814653f37947f8c253b29f8b08520ae71b7 (diff) |
fix `cd_s_cmd_node` alloc/create function
Diffstat (limited to 'shared')
-rw-r--r-- | shared/pclient.c | 18 | ||||
-rw-r--r-- | shared/pclient.h | 4 | ||||
-rw-r--r-- | shared/protocol.h | 4 |
3 files changed, 15 insertions, 11 deletions
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 |