diff options
Diffstat (limited to 'confui/serial.cpp')
-rw-r--r-- | confui/serial.cpp | 108 |
1 files changed, 53 insertions, 55 deletions
diff --git a/confui/serial.cpp b/confui/serial.cpp index b3f1cd6..210988c 100644 --- a/confui/serial.cpp +++ b/confui/serial.cpp @@ -1,14 +1,14 @@ #include "serial.h" -#include "../shared/serial_parse.h" #include "../shared/bin.h" #include "../shared/pclient.h" +#include "../shared/serial_parse.h" #include "mainwindow.h" #include "mesh_connector.h" -#include <iostream> #include <QDebug> #include <QSerialPort> #include <QSerialPortInfo> +#include <iostream> CDSerialConnector::CDSerialConnector() { this->_serial = new QSerialPort; @@ -30,22 +30,19 @@ void CDSerialConnector::action() { if (bytes > 0) _msg = _serial->readAll(); string std_string = _msg.toStdString(); - size_t size = std_string.size(); - const char* data = std_string.c_str(); - for (size_t i = 0; i < size; i++) - cd_serial_parse(data[i]); + size_t size = std_string.size(); + const char *data = std_string.c_str(); + for (size_t i = 0; i < size; i++) cd_serial_parse(data[i]); } void CDSerialConnector::write(QByteArray msg) { - if (-1 == _serial->write(msg)) - qDebug() << _serial->errorString(); + if (-1 == _serial->write(msg)) qDebug() << _serial->errorString(); } void CDSerialConnector::connect(string port) { _serial->setPortName(QString::fromStdString(port)); - if (!_serial->open(QIODevice::ReadWrite)) - qDebug() << _serial->errorString(); + if (!_serial->open(QIODevice::ReadWrite)) qDebug() << _serial->errorString(); QObject::connect(_serial, &QSerialPort::readyRead, [&] { action(); }); } @@ -59,96 +56,92 @@ QByteArray CDSerialConnector::get_data() { return _msg; } vector<string> CDSerialConnector::get_ports() { vector<string> ports; - for (QSerialPortInfo port : QSerialPortInfo::availablePorts()) - ports.push_back(port.portName().toStdString()); + for (QSerialPortInfo port : QSerialPortInfo::availablePorts()) ports.push_back(port.portName().toStdString()); return ports; } -string CDSerialConnector::get_port() { - return _serial->portName().toStdString(); -} +string CDSerialConnector::get_port() { return _serial->portName().toStdString(); } extern "C" { -void cd_pclient_send(cd_s_bin* data) { +void cd_pclient_send(cd_s_bin *data) { QByteArray converted; converted.append("\xff", 1); for (size_t i = 0; i < data->bytes; i++) { size_t byte = data->data[i]; - byte == 0xff ? converted.append("\xff\xff", 2) - : converted.append((char *) &byte, 1); + byte == 0xff ? converted.append("\xff\xff", 2) : converted.append((char *)&byte, 1); } g_cd_serial->write(converted); } // receive handlers (node only) -void cd_cmd_get_node(cd_s_bin* data) { (void) data; } -void cd_cmd_post_led(cd_s_bin* data) { (void) data; } -void cd_cmd_post_link(cd_s_bin* data) { (void) data; } -void cd_cmd_post_net(cd_s_bin* data) { (void) data; } +void cd_cmd_get_node(cd_s_bin *data) { (void)data; } +void cd_cmd_post_led(cd_s_bin *data) { (void)data; } +void cd_cmd_post_link(cd_s_bin *data) { (void)data; } +void cd_cmd_post_net(cd_s_bin *data) { (void)data; } -void cd_cmd_ping(cd_s_bin* data) { +void cd_cmd_ping(cd_s_bin *data) { CD_CAST_BIN(cd_s_cmd_ping, data, cast); cd_bin_repl_ntoh16(&cast->id); // fix endianness 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_s_bin *response = cd_cmd_res_status((cd_e_scmds)cast->opcode, cast->id, false); cd_pclient_send(response); free(response); } -void cd_cmd_response_get_node_parse_node(cd_s_cmd_node* node) { +void cd_cmd_response_get_node_parse_node(cd_s_cmd_node *node) { printf("yes i am node with name '%.*s'\n", node->name_len, node->remaining_data); printf("my light is %s and i am%s provisioned\n", node->light_on ? "on" : "off", node->provisioned ? "" : " not"); // get node handle - cd_uid_t node_id = g_cd_mesh_connector->get_or_create_node_by_uuid(node->uuid); - cd_s_node* gui_node = g_cd_mesh_connector->get_node(node_id); + cd_uid_t node_id = g_cd_mesh_connector->get_or_create_node_by_uuid(node->uuid); + cd_s_node *gui_node = g_cd_mesh_connector->get_node(node_id); // fill current node memcpy(gui_node->address, node->address, sizeof(cd_mac_addr_t)); memcpy(gui_node->uuid, node->uuid, sizeof(cd_uuid_t)); gui_node->name_len = node->name_len; if (gui_node->name != nullptr) free(gui_node->name); - char* name = (char*) malloc(node->name_len); + char *name = (char *)malloc(node->name_len); memcpy(name, node->remaining_data, node->name_len); - gui_node->name = name; - gui_node->light_on = !!node->light_on; + gui_node->name = name; + gui_node->light_on = !!node->light_on; gui_node->provisioned = !!node->provisioned; - cd_uuid_t* light_publish_addresses = (cd_uuid_t*) (&node->remaining_data[0] + node->name_len); + cd_uuid_t *light_publish_addresses = (cd_uuid_t *)(&node->remaining_data[0] + node->name_len); for (unsigned i = 0; i < node->link_count; i++) { // find or create light node - cd_uid_t light_id = g_cd_mesh_connector->get_or_create_node_by_uuid(light_publish_addresses[i]); - cd_s_node* gui_light = g_cd_mesh_connector->get_node(light_id); + cd_uid_t light_id = g_cd_mesh_connector->get_or_create_node_by_uuid(light_publish_addresses[i]); + cd_s_node *gui_light = g_cd_mesh_connector->get_node(light_id); memcpy(gui_light->uuid, light_publish_addresses[i], sizeof(cd_uuid_t)); // fill at least uuid (if node is not yet known) // find or create automation handle - cd_link_t link_id = g_cd_mesh_connector->get_or_create_link_by_uuid(gui_light->uuid, light_publish_addresses[i]); - cd_s_automation* gui_link = g_cd_mesh_connector->get_link(link_id); + cd_link_t link_id = g_cd_mesh_connector->get_or_create_link_by_uuid(gui_light->uuid, light_publish_addresses[i]); + cd_s_automation *gui_link = g_cd_mesh_connector->get_link(link_id); // fill automation gui_link->button = gui_node; - gui_link->light = gui_light; - gui_link->type = CD_AUTOMATION_TYPE_TOGGLE; //TODO: read from incoming data in future - gui_link->valid = true; + gui_link->light = gui_light; + gui_link->type = CD_AUTOMATION_TYPE_TOGGLE; // TODO: read from incoming data in future + gui_link->valid = true; } g_cd_main_window->update(); } -void cd_cmd_response_get_node(cd_s_bin* data) { +void cd_cmd_response_get_node(cd_s_bin *data) { CD_CAST_BIN(cd_s_cmd_response, data, response_cast); - cd_s_cmd_response_get_node* nodes = (cd_s_cmd_response_get_node*) &response_cast->response_info[0]; // yes + cd_s_cmd_response_get_node *nodes = (cd_s_cmd_response_get_node *)&response_cast->response_info[0]; // yes cd_bin_repl_ntoh16(&nodes->node_count); cd_bin_repl_ntoh16(&nodes->remaining_size); std::cout << "get nodes response with id " << response_cast->response_id << " received!" << std::endl; printf("counting %d node%s\n", nodes->node_count, nodes->node_count == 1 ? "" : "s"); - cd_s_cmd_node* cursor = &nodes->nodes[0]; + cd_s_cmd_node *cursor = &nodes->nodes[0]; for (unsigned int i = 0; i < nodes->node_count; i++) { cd_bin_repl_ntoh16(&cursor->remaining_size); cd_bin_repl_ntoh16(&cursor->link_count); @@ -159,40 +152,45 @@ void cd_cmd_response_get_node(cd_s_bin* data) { } } -void cd_cmd_response_ping(cd_s_bin* data) { +void cd_cmd_response_ping(cd_s_bin *data) { CD_CAST_BIN(cd_s_cmd_response, data, cast); std::cout << "ping response with id " << cast->response_id << " received!" << std::endl; } -void cd_cmd_response_post_led(cd_s_bin* data) { +void cd_cmd_response_post_led(cd_s_bin *data) { CD_CAST_BIN(cd_s_cmd_response, data, cast); if (cast->error) printf("POST_LED response with error for msg id 0x%04x", cast->response_id); } -void cd_cmd_response_post_link(cd_s_bin* data) { +void cd_cmd_response_post_link(cd_s_bin *data) { CD_CAST_BIN(cd_s_cmd_response, data, cast); if (cast->error) printf("POST_LINK response with error for msg id 0x%04x", cast->response_id); } -void cd_cmd_response_post_net(cd_s_bin* data) { +void cd_cmd_response_post_net(cd_s_bin *data) { CD_CAST_BIN(cd_s_cmd_response, data, cast); if (cast->error) printf("POST_NET response with error for msg id 0x%04x", cast->response_id); } -void cd_cmd_response(cd_s_bin* data) { +void cd_cmd_response(cd_s_bin *data) { CD_CAST_BIN(cd_s_cmd_response, data, cast); - + cd_bin_repl_ntoh16(&cast->id); cd_bin_repl_ntoh16(&cast->response_id); switch (cast->response_type) { - case CD_CMD_PING: return cd_cmd_response_ping(data); - case CD_CMD_GET_NODE: return cd_cmd_response_get_node(data); - case CD_CMD_POST_LED: return cd_cmd_response_post_led(data); - case CD_CMD_POST_LINK: return cd_cmd_post_link(data); - case CD_CMD_POST_NET: return cd_cmd_response_post_net(data); - default: return; + case CD_CMD_PING: + return cd_cmd_response_ping(data); + case CD_CMD_GET_NODE: + return cd_cmd_response_get_node(data); + case CD_CMD_POST_LED: + return cd_cmd_response_post_led(data); + case CD_CMD_POST_LINK: + return cd_cmd_post_link(data); + case CD_CMD_POST_NET: + return cd_cmd_response_post_net(data); + default: + return; } } - } |