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.cpp121
1 files changed, 56 insertions, 65 deletions
diff --git a/confui/mesh_connector.cpp b/confui/mesh_connector.cpp
index a9742cf..5b65d31 100644
--- a/confui/mesh_connector.cpp
+++ b/confui/mesh_connector.cpp
@@ -1,38 +1,34 @@
-#include <stdio.h>
-#include <cstdio>
-#include <cstring>
#include <algorithm>
#include <array>
+#include <cstdio>
+#include <cstring>
+#include <stdio.h>
#include "mesh_connector.h"
using std::pair;
-void CDMeshConnector::refresh_config_sync() { }
-void CDMeshConnector::refresh_nodes_sync() { }
+void CDMeshConnector::refresh_config_sync() {}
+void CDMeshConnector::refresh_nodes_sync() {}
-cd_link_t CDMeshConnector::get_new_link_id() {
- return _fresh_link_id++;
-}
+cd_link_t CDMeshConnector::get_new_link_id() { return _fresh_link_id++; }
-cd_uid_t CDMeshConnector::get_new_node_id() {
- return _fresh_node_id++;
-}
+cd_uid_t CDMeshConnector::get_new_node_id() { return _fresh_node_id++; }
CDMeshConnector::CDMeshConnector() {
cd_uid_t berta = create_node({
- .address = { 0x00, 0xff, 0x21, 0x69, 0xf2, 0x31 },
- .name_len = 5,
- .name = "berta",
- .light_on = false,
+ .address = {0x00, 0xff, 0x21, 0x69, 0xf2, 0x31},
+ .name_len = 5,
+ .name = "berta",
+ .light_on = false,
.provisioned = false,
});
cd_uid_t gerrit = create_node({
- .address = { 0x0e, 0xf9, 0x46, 0x4d, 0xe8, 0x02 },
- .name_len = 6,
- .name = "gerrit",
- .light_on = false,
+ .address = {0x0e, 0xf9, 0x46, 0x4d, 0xe8, 0x02},
+ .name_len = 6,
+ .name = "gerrit",
+ .light_on = false,
.provisioned = false,
});
@@ -44,17 +40,17 @@ CDMeshConnector::CDMeshConnector() {
}
cd_uid_t CDMeshConnector::create_node(cd_s_node node) {
- cd_s_node* _node = (cd_s_node*) malloc(sizeof(cd_s_node));
+ cd_s_node *_node = (cd_s_node *)malloc(sizeof(cd_s_node));
// id
cd_uid_t id = get_new_node_id();
- _node->id = 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);
+ char *name = (char *)malloc(node.name_len);
memcpy(name, node.name, node.name_len);
_node->name = name;
// light on
@@ -62,7 +58,7 @@ cd_uid_t CDMeshConnector::create_node(cd_s_node node) {
// provisioned
_node->provisioned = node.provisioned;
- printf("(new) node[%d] = %.*s\n", id, (int) node.name_len, node.name);
+ printf("(new) node[%d] = %.*s\n", id, (int)node.name_len, node.name);
_nodes[id] = _node;
return id;
@@ -70,44 +66,46 @@ cd_uid_t CDMeshConnector::create_node(cd_s_node node) {
CDMeshConnector::~CDMeshConnector() {
// free all automation structs
- for (pair<cd_link_t, cd_s_automation*> link : _links)
- remove_link(link.first);
+ for (pair<cd_link_t, cd_s_automation *> link : _links) remove_link(link.first);
// free all node structs
- for (pair<cd_uid_t, cd_s_node*> node : _nodes)
- remove_node(node.first);
+ for (pair<cd_uid_t, cd_s_node *> node : _nodes) remove_node(node.first);
return;
}
-map<cd_uid_t, cd_s_node*> CDMeshConnector::get_nodes(bool provisioned) {
+map<cd_uid_t, cd_s_node *> CDMeshConnector::get_nodes(bool provisioned) {
if (provisioned == false) return _nodes;
// filter only if provisioned nodes are desired
- map<cd_uid_t, cd_s_node*> nodes;
- for (pair<cd_uid_t, cd_s_node*> node : _nodes) {
+ map<cd_uid_t, cd_s_node *> nodes;
+ for (pair<cd_uid_t, cd_s_node *> node : _nodes) {
if (node.second->provisioned == false) continue;
nodes[node.first] = node.second;
}
return nodes;
}
-map<cd_link_t, cd_s_automation*> CDMeshConnector::get_links(bool valid) {
+map<cd_link_t, cd_s_automation *> CDMeshConnector::get_links(bool valid) {
if (valid == false) return _links;
// filter only if valid links are desired
- map<cd_link_t, cd_s_automation*> links;
- for (pair<cd_link_t, cd_s_automation*> link : _links) {
+ map<cd_link_t, cd_s_automation *> links;
+ for (pair<cd_link_t, cd_s_automation *> link : _links) {
if (link.second->valid == false) continue;
links[link.first] = link.second;
}
return links;
}
-void CDMeshConnector::update_link(cd_s_automation* automation) {
+void CDMeshConnector::update_link(cd_s_automation *automation) {
printf("link[%d]", automation->id);
if (automation->valid) {
- printf(" = %.*s %s %.*s", (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);
+ printf(" = %.*s %s %.*s", (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);
} else {
printf(" (invalid)");
}
@@ -117,12 +115,12 @@ void CDMeshConnector::update_link(cd_s_automation* automation) {
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->id = id;
- automation->type = type;
- automation->button = _nodes[button];
- automation->light = _nodes[light];
- automation->valid = true;
+ cd_s_automation *automation = (cd_s_automation *)malloc(sizeof(cd_s_automation));
+ automation->id = id;
+ automation->type = type;
+ automation->button = _nodes[button];
+ automation->light = _nodes[light];
+ automation->valid = true;
printf("(new) ");
_links[id] = automation;
@@ -134,12 +132,12 @@ cd_link_t CDMeshConnector::create_link(cd_uid_t button, cd_uid_t light, enum cd_
cd_link_t CDMeshConnector::create_link() {
cd_link_t id = get_new_link_id();
- cd_s_automation* automation = (cd_s_automation*) malloc(sizeof(cd_s_automation));
- automation->id = id;
- automation->type = CD_AUTOMATION_TYPE_TOGGLE;
- automation->button = nullptr;
- automation->light = nullptr;
- automation->valid = false;
+ cd_s_automation *automation = (cd_s_automation *)malloc(sizeof(cd_s_automation));
+ automation->id = id;
+ automation->type = CD_AUTOMATION_TYPE_TOGGLE;
+ automation->button = nullptr;
+ automation->light = nullptr;
+ automation->valid = false;
printf("(new) ");
_links[id] = automation;
@@ -150,50 +148,43 @@ cd_link_t CDMeshConnector::create_link() {
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[link_handle] = nullptr;
return;
}
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]);
+ if (_nodes[node_handle] != nullptr) free(_nodes[node_handle]);
_nodes[node_handle] = nullptr;
return;
}
-void CDMeshConnector::update_node(cd_s_node* node_ptr) {
- printf("turning %.*s %s\n", (int) node_ptr->name_len, node_ptr->name, node_ptr->light_on ? "on" : "off");
+void CDMeshConnector::update_node(cd_s_node *node_ptr) {
+ printf("turning %.*s %s\n", (int)node_ptr->name_len, node_ptr->name, node_ptr->light_on ? "on" : "off");
return;
}
-void CDMeshConnector::network_join_node(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);
+ printf("join %.*s into network\n", (int)node_ptr->name_len, node_ptr->name);
return;
}
-void CDMeshConnector::network_remove_node(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);
+ printf("remove %.*s from network\n", (int)node_ptr->name_len, node_ptr->name);
return;
}
string CDMeshConnector::cd_mac_to_string(cd_mac_addr_t mac) {
- char* addr = nullptr;
+ 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;
free(addr);
return ret;
}
-cd_s_automation* CDMeshConnector::get_link(cd_link_t id) {
- return _links[id];
-}
-
-cd_s_node* CDMeshConnector::get_node(cd_uid_t id) {
- return _nodes[id];
-}
+cd_s_automation *CDMeshConnector::get_link(cd_link_t id) { return _links[id]; }
+cd_s_node *CDMeshConnector::get_node(cd_uid_t id) { return _nodes[id]; }