#include #include #include #include "DataAccess/ArduinoSerial.h" constexpr char hexmap[] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'}; std::string hexStr(std::string str) { std::string s(str.size() * 2, ' '); for (int i = 0; i < str.size(); ++i) { s[2 * i] = hexmap[(str[i] & 0xF0) >> 4]; s[2 * i + 1] = hexmap[str[i] & 0x0F]; } return s; } std::vector &split(const std::string &s, char delim, std::vector &elems) { std::stringstream ss(s); std::string item; while (std::getline(ss, item, delim)) { elems.push_back(item); } return elems; } std::vector split(const std::string &s, char delim) { std::vector elems; split(s, delim, elems); return elems; } int lastSignal = 0; void sig_handler(int signo) { std::cout << "Signal catched " << signo << std::endl; lastSignal = signo; } int main() { signal(SIGINT, sig_handler); signal(SIGTERM, sig_handler); signal(SIGKILL, sig_handler); ArduinoSerial arduinoSerial("/dev/ttyUSB0", SERIAL_BAUDRATE); auto res = arduinoSerial.open(); if (!res) { res.print(); return 1; } while (lastSignal == 0) { auto data = arduinoSerial.read(); if (!data) { data.print(); return 2; } std::cout << (int) data.getData().first << " " << hexStr(data.getData().second) << " " << data.getData().second << std::endl; if (data.getData().first == PACKET_SELF_TEST) { SERIAL_PACKET_TYPE_INT status = ERROR_NONE; arduinoSerial.write((const char*)&status, sizeof(status)); } if (data.getData().first == PACKET_LOGIN) { std::vector login = split(data.getData().second, '\n'); if (login.size() == 2) { std::string uid = login[0]; std::string passwd = login[1]; SERIAL_PACKET_TYPE_INT status = (uid == "4242" ? LOGIN_OK : LOGIN_FAILED); arduinoSerial.write((const char *) &status, sizeof(status)); } } } return 0; }