aboutsummaryrefslogtreecommitdiff
path: root/confui/mesh_connector.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'confui/mesh_connector.cpp')
-rw-r--r--confui/mesh_connector.cpp170
1 files changed, 84 insertions, 86 deletions
diff --git a/confui/mesh_connector.cpp b/confui/mesh_connector.cpp
index c87d8c5..d32855a 100644
--- a/confui/mesh_connector.cpp
+++ b/confui/mesh_connector.cpp
@@ -8,78 +8,85 @@
using std::pair;
-cd_link_t CDMeshConnector::get_new_map_id() {
- return _fresh_map_id++;
+void CDMeshConnector::refresh_config_sync() { }
+void CDMeshConnector::refresh_nodes_sync() { }
+
+cd_link_t CDMeshConnector::get_new_link_id() {
+ return _fresh_link_id++;
+}
+
+cd_uid_t CDMeshConnector::get_new_node_id() {
+ return _fresh_node_id++;
}
CDMeshConnector::CDMeshConnector() {
- printf("adding dummy node berta...\n");
-
- char* temp_node_berta_name = (char*) malloc(5);
- memcpy(temp_node_berta_name, "berta", 5);
-
- cd_s_node* temp_node_berta = (cd_s_node*) malloc(sizeof(cd_s_node));
- temp_node_berta->name = temp_node_berta_name;
- temp_node_berta->name_len = 5;
- temp_node_berta->light_on = false;
- temp_node_berta->provisioned = false;
- cd_mac_addr_t temp_node_berta_address = { 0x00, 0xff, 0x21, 0x69, 0xf2, 0x31 };
- memcpy(temp_node_berta->address, temp_node_berta_address, 6);
-
- printf("adding dummy node gerrit...\n");
- char* temp_node_gerrit_name = (char*) malloc(6);
- memcpy(temp_node_gerrit_name, "gerrit", 6);
-
- cd_s_node* temp_node_gerrit = (cd_s_node*) malloc(sizeof(cd_s_node));
- temp_node_gerrit->name = temp_node_gerrit_name;
- temp_node_gerrit->name_len = 6;
- temp_node_gerrit->light_on = false;
- temp_node_gerrit->provisioned = false;
- cd_mac_addr_t temp_node_gerrit_address = { 0x0e, 0xf9, 0x46, 0x4d, 0xe8, 0x02 };
- memcpy(temp_node_gerrit->address, temp_node_gerrit_address, 6);
-
- _nodes.push_back(temp_node_berta);
- _nodes.push_back(temp_node_gerrit);
- create_link(temp_node_berta, temp_node_berta, CD_AUTOMATION_TYPE_TOGGLE);
- create_link(temp_node_berta, temp_node_gerrit, CD_AUTOMATION_TYPE_TOGGLE);
- create_link(temp_node_gerrit, temp_node_berta, CD_AUTOMATION_TYPE_TURN_OFF);
- create_link(temp_node_gerrit, temp_node_gerrit, CD_AUTOMATION_TYPE_TURN_ON);
+ cd_uid_t berta = create_node({
+ .id = 0, // automatically assigned (satisfy compiler)
+ .address = { 0x00, 0xff, 0x21, 0x69, 0xf2, 0x31 },
+ .name_len = 5,
+ .name = "berta",
+ .light_on = false,
+ .provisioned = false,
+ });
+
+ cd_uid_t gerrit = create_node({
+ .id = 0, // automatically assigned (satisfy compiler)
+ .address = { 0x0e, 0xf9, 0x46, 0x4d, 0xe8, 0x02 },
+ .name_len = 6,
+ .name = "gerrit",
+ .light_on = false,
+ .provisioned = false,
+ });
+
+ create_link(berta, berta, CD_AUTOMATION_TYPE_TOGGLE);
+ create_link(berta, berta, CD_AUTOMATION_TYPE_TOGGLE);
+ create_link(gerrit, berta, CD_AUTOMATION_TYPE_TURN_OFF);
+ create_link(gerrit, gerrit, CD_AUTOMATION_TYPE_TURN_ON);
return;
}
-CDMeshConnector::~CDMeshConnector() {
- map<cd_link_t, cd_s_automation*> links = get_links();
- vector<cd_s_node*> nodes = get_raw_nodes();
+cd_uid_t CDMeshConnector::create_node(cd_s_node node) {
+ cd_s_node* _node = (cd_s_node*) malloc(sizeof(cd_s_node));
- // free all automation structs
- for (pair<cd_link_t, cd_s_automation*> link : links)
- remove_link(link.first);
+ // id
+ cd_uid_t id = get_new_node_id();
+ _node->id = id;
+ // address
+ memcpy(_node->address, node.address, sizeof(cd_mac_addr_t));
+ // name len
+ _node->name_len = node.name_len;
+ // name
+ char* name = (char*) malloc(node.name_len);
+ memcpy(name, node.name, node.name_len);
+ _node->name = name;
+ // light on
+ _node->light_on = node.light_on;
+ // provisioned
+ _node->provisioned = node.provisioned;
- // free all node structs
- for (cd_s_node* node : nodes)
- if (node != nullptr) free(node);
+ printf("(new) node[%d] = %.*s\n", id, (int) node.name_len, node.name);
- return;
+ _nodes[id] = _node;
+ return id;
}
-void CDMeshConnector::refresh_config_sync() {
- return;
-}
+CDMeshConnector::~CDMeshConnector() {
+ // free all automation structs
+ for (pair<cd_link_t, cd_s_automation*> link : _links)
+ remove_link(link.first);
-void CDMeshConnector::refresh_nodes_sync() {
- return;
-}
+ // free all node structs
+ for (pair<cd_uid_t, cd_s_node*> node : _nodes)
+ remove_node(node.first);
-vector<cd_s_node*> CDMeshConnector::get_raw_nodes() {
- return _nodes;
+ return;
}
-map<cd_mac_addr_cpp_t, cd_s_node*> CDMeshConnector::get_nodes(bool provisioned) {
- map<cd_mac_addr_cpp_t, cd_s_node*> nodes;
- vector<cd_s_node*> raw_nodes = get_raw_nodes();
- for (cd_s_node* node : raw_nodes) {
- if (provisioned && (node->provisioned == false)) continue;
- nodes[cd_mac_to_cpp_arr(node->address)] = node;
+map<cd_uid_t, cd_s_node*> CDMeshConnector::get_nodes(bool provisioned) {
+ map<cd_uid_t, cd_s_node*> nodes;
+ for (pair<cd_uid_t, cd_s_node*> node : _nodes) {
+ if (provisioned && (node.second->provisioned == false)) continue;
+ nodes[node.first] = node.second;
}
return nodes;
}
@@ -88,45 +95,40 @@ map<cd_link_t, cd_s_automation*> CDMeshConnector::get_links() {
return _links;
}
-vector<cd_s_automation*> CDMeshConnector::get_config() {
- vector<cd_s_automation*> automations;
- map<cd_link_t, cd_s_automation*> links = get_links();
- for (pair<cd_link_t, cd_s_automation*> link : links)
- automations.push_back(link.second);
- return automations;
-}
-
-void CDMeshConnector::set_link(cd_link_t link, cd_s_automation* automation) {
+void CDMeshConnector::update_link(cd_link_t link, cd_s_automation* automation) {
_links[link] = automation;
printf("link[%d] = %.*s %s %.*s\n", link, (int) automation->button->name_len, automation->button->name, automation->type == CD_AUTOMATION_TYPE_TOGGLE ? "toggles" : automation->type == CD_AUTOMATION_TYPE_TURN_OFF ? "turns off" : "turns on", (int) automation->light->name_len, automation->light->name);
}
-cd_link_t CDMeshConnector::create_link(cd_s_node* button, cd_s_node* light, enum cd_e_automation_type type) {
+cd_link_t CDMeshConnector::create_link(cd_uid_t button, cd_uid_t light, enum cd_e_automation_type type) {
+ cd_link_t id = get_new_link_id();
+
cd_s_automation* automation = (cd_s_automation*) malloc(sizeof(cd_s_automation));
- automation->button = button;
- automation->light = light;
+ automation->id = id;
automation->type = type;
+ automation->button = _nodes[button];
+ automation->light = _nodes[light];
- cd_link_t link = get_new_map_id();
printf("(new) ");
- set_link(link, automation);
+ update_link(id, automation);
- return link;
+ return id;
}
void CDMeshConnector::remove_link(cd_link_t link_handle) {
printf("remove link[%d]\n", link_handle);
- if (_links[link_handle] != nullptr) free(_links[link_handle]);
+ if (_links[link_handle] != nullptr)
+ free(_links[link_handle]);
_links.erase(link_handle);
return;
}
-cd_s_node* CDMeshConnector::get_node_by_id(cd_mac_addr_t address) {
- cd_s_node* node = std::find_if(_nodes.begin(), _nodes.end(),
- [address] (const cd_s_node* n) {
- return strncmp((char*)n->address, (char*)address, 6);
- })[0];
- return node;
+void CDMeshConnector::remove_node(cd_uid_t node_handle) {
+ printf("remove node[%d]\n", node_handle);
+ if (_nodes[node_handle] != nullptr)
+ free(_nodes[node_handle]);
+ _nodes.erase(node_handle);
+ return;
}
void CDMeshConnector::update_node(cd_s_node* node_ptr) {
@@ -134,19 +136,19 @@ void CDMeshConnector::update_node(cd_s_node* node_ptr) {
return;
}
-void CDMeshConnector::node_join_network(cd_s_node* node_ptr) {
+void CDMeshConnector::network_join_node(cd_s_node* node_ptr) {
node_ptr->provisioned = true;
printf("join %.*s into network\n", (int) node_ptr->name_len, node_ptr->name);
return;
}
-void CDMeshConnector::node_remove_network(cd_s_node* node_ptr) {
+void CDMeshConnector::network_remove_node(cd_s_node* node_ptr) {
node_ptr->provisioned = false;
printf("remove %.*s from network\n", (int) node_ptr->name_len, node_ptr->name);
return;
}
-string cd_mac_to_string(cd_mac_addr_t mac) {
+string CDMeshConnector::cd_mac_to_string(cd_mac_addr_t mac) {
char* addr = nullptr;
asprintf(&addr, "%02x:%02x:%02x:%02x:%02x:%02x", mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
string ret = addr;
@@ -154,7 +156,3 @@ string cd_mac_to_string(cd_mac_addr_t mac) {
return ret;
}
-cd_mac_addr_cpp_t cd_mac_to_cpp_arr(cd_mac_addr_t mac) {
- return { mac[0], mac[1], mac[2], mac[3], mac[4], mac[5] };
-}
-