#include #include #include #include #include "mesh_connector.h" using std::pair; cd_link_t CDMeshConnector::get_new_map_id() { return _fresh_map_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 berta...\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 = { 0x00, 0xff, 0x21, 0x69, 0xf2, 0x31 }; memcpy(temp_node_gerrit->address, temp_node_gerrit_address, 6); _nodes.push_back(temp_node_berta); _nodes.push_back(temp_node_gerrit); set_link(temp_node_berta, temp_node_berta, CD_AUTOMATION_TYPE_TOGGLE); set_link(temp_node_berta, temp_node_gerrit, CD_AUTOMATION_TYPE_TOGGLE); set_link(temp_node_gerrit, temp_node_berta, CD_AUTOMATION_TYPE_TURN_OFF); set_link(temp_node_gerrit, temp_node_gerrit, CD_AUTOMATION_TYPE_TURN_ON); return; } CDMeshConnector::~CDMeshConnector() { map links = get_links(); vector nodes = get_nodes(); // free all automation structs for (pair link : links) remove_link(link.first); // free all node structs for (cd_s_node* node : nodes) if (node != nullptr) free(node); return; } void CDMeshConnector::refresh_config_sync() { return; } void CDMeshConnector::refresh_nodes_sync() { return; } vector CDMeshConnector::get_nodes() { return _nodes; } map CDMeshConnector::get_links() { return _links; } vector CDMeshConnector::get_config() { vector automations; map links = get_links(); for (pair link : links) automations.push_back(link.second); return automations; } cd_link_t CDMeshConnector::set_link(cd_s_node* button, cd_s_node* light, enum cd_e_automation_type action) { cd_s_automation* automation = (cd_s_automation*) malloc(sizeof(cd_s_automation)); automation->button = button; automation->light = light; automation->type = action; cd_link_t link = get_new_map_id(); _links[link] = automation; printf("new link[%d]: %.*s %s %.*s\n", link, (int) button->name_len, button->name, action == CD_AUTOMATION_TYPE_TOGGLE ? "toggles" : action == CD_AUTOMATION_TYPE_TURN_OFF ? "turns off" : "turns on", (int) light->name_len, light->name); return 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]); _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::set_node(cd_s_node* node_ptr, bool light_status) { node_ptr->light_on = light_status; printf("turning %.*s %s\n", (int) node_ptr->name_len, node_ptr->name, node_ptr->light_on ? "on" : "off"); return; } void CDMeshConnector::node_join_network(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) { node_ptr->provisioned = false; printf("remove %.*s from network\n", (int) node_ptr->name_len, node_ptr->name); return; } string cd_node_mac_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; free(addr); return ret; }