aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorlonkaars <loek@pipeframe.xyz>2022-12-13 18:09:23 +0100
committerlonkaars <loek@pipeframe.xyz>2022-12-13 18:09:23 +0100
commit2c2ff814653f37947f8c253b29f8b08520ae71b7 (patch)
tree6b265b0e7731c9a7ac4d46b68900def8f44ac911
parent9c6c30156aae3bf20a3f1b1d3b0766bb3c924412 (diff)
[WIP] node with pub/sub address and uuid
-rw-r--r--confui/serial.cpp23
-rw-r--r--shared/bin.h2
-rw-r--r--shared/pclient.c31
-rw-r--r--shared/pclient.h17
-rw-r--r--shared/protocol.h17
5 files changed, 73 insertions, 17 deletions
diff --git a/confui/serial.cpp b/confui/serial.cpp
index 1518abd..09bd7db 100644
--- a/confui/serial.cpp
+++ b/confui/serial.cpp
@@ -92,10 +92,25 @@ void cd_cmd_ping(cd_s_bin* data) {
std::cout << "ping request with id " << cast->id << " received!" << std::endl;
- cd_s_bin* response = cd_cmd_res_status((cd_e_scmds) cast->opcode, cast->id, false);
- cd_pclient_send(response);
- free(response);
- response = nullptr;
+ // cd_s_bin* response = cd_cmd_res_status((cd_e_scmds) cast->opcode, cast->id, false);
+ // cd_pclient_send(response);
+ // free(response);
+ // response = nullptr;
+
+ 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({
+ .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);
+ cd_pclient_send(testres);
+ free(testres);
+ free(test);
}
void cd_cmd_response(cd_s_bin* data) {
diff --git a/shared/bin.h b/shared/bin.h
index 0e16ec0..7506655 100644
--- a/shared/bin.h
+++ b/shared/bin.h
@@ -18,7 +18,7 @@ extern "C" {
extern uint8_t g_cd_endianness;
-/** cast `in.data` to `type out` */
+/** @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 */
diff --git a/shared/pclient.c b/shared/pclient.c
index 41ace27..0464428 100644
--- a/shared/pclient.c
+++ b/shared/pclient.c
@@ -99,23 +99,40 @@ cd_s_bin* cd_cmd_res_status(cd_e_scmds cmd, cd_cmd_id_t id, bool error) {
return bin;
}
-cd_s_bin* cd_cmd_res_get_node(cd_e_scmds cmd, cd_cmd_id_t id, uint16_t node_count, cd_s_cmd_node* nodes) {
- CD_CREATE_MSG_BIN(cd_s_cmd_response, msg, bin);
+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->response_type = cmd;
msg->response_id = id;
msg->error = false;
-
- // TODO: test this??
- msg->response_size = sizeof(cd_s_cmd_response_get_node) + sizeof(cd_s_cmd_node) * node_count;
- cd_s_cmd_response_get_node* get_node_response_ptr = (cd_s_cmd_response_get_node*) msg->response_info;
- memcpy(get_node_response_ptr->nodes, nodes, sizeof(cd_s_cmd_node) * node_count);
+ msg->response_size = 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) {
+ 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);
+
+ 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->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
+
+ return node;
+}
+
#ifdef __cplusplus
}
#endif
diff --git a/shared/pclient.h b/shared/pclient.h
index cf0f182..d0ddeb7 100644
--- a/shared/pclient.h
+++ b/shared/pclient.h
@@ -59,13 +59,22 @@ cd_s_bin* cd_cmd_gen_post_net_rm(cd_uuid_t uuid);
*/
cd_s_bin* cd_cmd_res_status(cd_e_scmds cmd, cd_cmd_id_t id, bool error);
/**
- * @brief generate RESPONSE command with GET_NODE response_info
+ * @brief generate RESPONSE command with response_info
* @param cmd original command opcode
* @param id original command id
- * @param node_count amount of nodes in `nodes`
- * @param nodes pointer to array of `cd_s_cmd_node`
+ * @param len size of `data` in bytes
+ * @param data pointer to data
*/
-cd_s_bin* cd_cmd_res_get_node(cd_e_scmds cmd, cd_cmd_id_t id, uint16_t node_count, cd_s_cmd_node* nodes);
+cd_s_bin* cd_cmd_res(cd_e_scmds cmd, cd_cmd_id_t id, uint16_t len, uint8_t* data);
+/**
+ * @brief allocate and fill cd_s_cmd_node struct
+ *
+ * @param base base struct with values that can be initialized using an initialization list
+ * @param name node name (length is calculated at runtime using strlen())
+ * @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);
#ifdef __cplusplus
}
diff --git a/shared/protocol.h b/shared/protocol.h
index 63b4a9e..6f9697f 100644
--- a/shared/protocol.h
+++ b/shared/protocol.h
@@ -19,6 +19,9 @@ typedef uint8_t cd_mac_addr_t[6];
/** @brief uuid (ffeeddcc-bbaa-9988-7766-554433221100) */
typedef uint8_t cd_uuid_t[16];
+/** @brief pub/sub address type */
+typedef uint32_t cd_mesh_psub_addr;
+
/** @brief command opcode (identifies message type) */
typedef uint8_t cd_cmd_opcode_t;
/** @brief command id (identifies messages uniquely) */
@@ -85,7 +88,19 @@ typedef struct {
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 */
- const char name[]; /** @brief user-friendly node name */
+ 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[]; /**
+ * @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 {