From d7d421c6f26b50695aa93768564212dd0a9ad2dd Mon Sep 17 00:00:00 2001 From: Eduardo Silva Date: Tue, 22 Sep 2026 16:30:19 +0100 Subject: [PATCH] added the healthcheck command --- src/communication/commands.h | 1 + src/communication/packets.cpp | 8 ++ src/communication/packets.h | 9 ++ src/communication/walkieTalkie.cpp | 6 +- src/main.cpp | 207 +++++++++++++++-------------- 5 files changed, 127 insertions(+), 104 deletions(-) diff --git a/src/communication/commands.h b/src/communication/commands.h index c32fbf5..00ce0dc 100644 --- a/src/communication/commands.h +++ b/src/communication/commands.h @@ -12,6 +12,7 @@ typedef enum : uint8_t { CmdUpdateWinDims = 5, // NOTE: change this to a move cmd instead CmdUpdateWin = 6, CMDData = 7, + CMDHealthCheck = 9, } Command; char* CommandToStr(Command id); diff --git a/src/communication/packets.cpp b/src/communication/packets.cpp index dd404a5..1f656d2 100644 --- a/src/communication/packets.cpp +++ b/src/communication/packets.cpp @@ -24,3 +24,11 @@ void initWindowIDReply(WindowIDReply* pkt, int16_t id) { pkt->EndMarker = 0x03; pkt->wID = id; } + +void initHealthCheckReply(HealthCheckReply* pkt) { + memset(pkt, 0, sizeof(*pkt)); + + pkt->StartMarker = 0x02; + pkt->reply = 0x06; + pkt->EndMarker = 0x03; +} diff --git a/src/communication/packets.h b/src/communication/packets.h index eb2afa9..7ac3571 100644 --- a/src/communication/packets.h +++ b/src/communication/packets.h @@ -2,6 +2,7 @@ #define __PACKETS__ #include "UIDimensions.h" +#include #include typedef uint8_t PacketType; @@ -36,4 +37,12 @@ struct __attribute__((packed)) WindowIDReply { void initWindowIDReply(WindowIDReply* pkt, int16_t id); +struct __attribute__((packed)) HealthCheckReply { + uint8_t StartMarker; + uint8_t reply; + uint8_t EndMarker; +}; + +void initHealthCheckReply(HealthCheckReply* pkt); + #endif diff --git a/src/communication/walkieTalkie.cpp b/src/communication/walkieTalkie.cpp index 4b01db2..466d771 100644 --- a/src/communication/walkieTalkie.cpp +++ b/src/communication/walkieTalkie.cpp @@ -21,11 +21,15 @@ namespace WalkieTalkie { resetBuffer(); } + static inline bool validCmd(uint8_t* cmd) { + return !(*cmd == 0 || *cmd > CMDHealthCheck); + } + static inline void RecvCMDRoutine(byte *b) { LOG_TRACE(F("Reading the command byte\n")); uint8_t cmdVal = Command(*b); - if (cmdVal == 0 || cmdVal > CMDData) { + if (!validCmd(&cmdVal)) { LOG_TRACE(F("Invalid CMD byte: 0x%02x. Resetting parser.\n"), cmdVal); ResetStateMachine(); return; diff --git a/src/main.cpp b/src/main.cpp index ff75429..2ec41d6 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -133,120 +133,121 @@ void loop(void) { return; } - LOG_DEBUG(F("Command: %s\n"), CommandToStr(cmd)); - LOG_DEBUG(F("Response Payload Len: %d\n"), resp); + LOG_WARN(F("Command: %s\r\n"), CommandToStr(cmd)); + LOG_DEBUG(F("Response Payload Len: %d\r\n"), resp); // print_memory_stats(); - // If its not connected the only command we can receive is the CmdRequestID - if (!connected) { - if (cmd != CmdRequestID) { - return; - } + switch(cmd) { + case CmdRequestID: + IdentificationPacket papers; + initIdentificationPacket(&papers, ESLABS_DEVICE_NAME, ESLABS_DEVICE_ID); + WalkieTalkie::SendData(&papers); - // Handle the ID request, it should connect us. TODO: Improve this - IdentificationPacket papers; - initIdentificationPacket(&papers, ESLABS_DEVICE_NAME, ESLABS_DEVICE_ID); - WalkieTalkie::SendData(&papers); - - LOG_INFO(F("Connection request came in")); - - connected = true; - - } else { - switch(cmd) { - case CmdCreateWindow: { - int8_t newWindowID = Window::Create(payload, &mainScreen); - if (newWindowID < 0) { - LOG_WARN(F("Failed to add new window!\n")); - break; - } - - LOG_INFO(F("Successfully added a new window: %d\n"), newWindowID); - - // Now we return the ID of this new window - esdi should expect it - WindowIDReply wID; - initWindowIDReply(&wID, newWindowID); - WalkieTalkie::SendData(&wID); + LOG_INFO(F("Connection request came in\r\n")); + LOG_INFO(F("Replied with something \r\n\n")); + break; + case CmdCreateWindow: { + int8_t newWindowID = Window::Create(payload, &mainScreen); + if (newWindowID < 0) { + LOG_WARN(F("Failed to add new window!\n")); break; } - case CmdUpdateWinDims: { - LOG_INFO(F("Updating Win DIMS\n")); - bool res = Window::UpdateDims(payload, &mainScreen); - if (!res) { - LOG_WARN(F("Failed to update window dims\n")); - } - break; - }; - case CmdUpdateWin: { - LOG_INFO(F("UPDATEING WINDOW\n")); - bool res = Window::UpdateWindow(payload, &mainScreen); - if (!res) { - LOG_WARN(F("Failed to update window\n")); - } - break; - } - case CmdDestroyWindow: { - uint8_t destroyedWinID = Window::Destroy(payload, &mainScreen); - if (destroyedWinID < 0) { - LOG_WARN(F("Failed to destroy window\n")); - break; - } - LOG_INFO(F("Succesfully destroyed window: %d\n"), destroyedWinID); + LOG_INFO(F("Successfully added a new window: %d\n"), newWindowID); - break; - } - case CMDData: { - LOG_DEBUG(F("Data Received: %d bytes\r\n"), resp); - - // int pos = 0; - // for (int k = 0; k < resp; k++) { - // // Write 2 hex digits and a space to our local buffer - // // %02X ensures leading zeros (e.g., 0A instead of A) - // pos += sprintf(lineBuffer + pos, "%02X ", payload[k]); - // - // // Every 8 bytes OR if it's the very last byte in the payload - // if ((k + 1) % 8 == 0 || k == resp - 1) { - // // Send the completed line to the logger - // // We use LOG_DEBUG or similar so we don't spam [WARN] on every line - // LOG_DEBUG(F("%s\r\n"), lineBuffer); - // - // // Reset buffer position for the next line - // pos = 0; - // memset(lineBuffer, 0, sizeof(lineBuffer)); - // } - // } - // LOG_DEBUG(F("\r\n")); + // Now we return the ID of this new window - esdi should expect it + WindowIDReply wID; + initWindowIDReply(&wID, newWindowID); + WalkieTalkie::SendData(&wID); - Data::Parse(payload, resp, &mainScreen); - - break; - } - default: - LOG_ERROR(F("Command: %s\n"), CommandToStr(cmd)); - LOG_ERROR(F("Unknown Command: `%02X` has not been implemented yet.\n"), cmd); - int pos = 0; - for (int k = 0; k < resp; k++) { - // Write 2 hex digits and a space to our local buffer - // %02X ensures leading zeros (e.g., 0A instead of A) - pos += sprintf(lineBuffer + pos, "%02X ", payload[k]); - - // Every 8 bytes OR if it's the very last byte in the payload - if ((k + 1) % 8 == 0 || k == resp - 1) { - // Send the completed line to the logger - // We use LOG_DEBUG or similar so we don't spam [WARN] on every line - LOG_WARN(F("%s\r\n"), lineBuffer); - - // Reset buffer position for the next line - pos = 0; - memset(lineBuffer, 0, sizeof(lineBuffer)); - } - } - LOG_WARN(F("\r\n")); - break; + break; } + case CmdUpdateWinDims: { + LOG_INFO(F("Updating Win DIMS\n")); + bool res = Window::UpdateDims(payload, &mainScreen); + if (!res) { + LOG_WARN(F("Failed to update window dims\n")); + } + break; + }; + case CmdUpdateWin: { + LOG_INFO(F("UPDATEING WINDOW\n")); + bool res = Window::UpdateWindow(payload, &mainScreen); + if (!res) { + LOG_WARN(F("Failed to update window\n")); + } + break; + } + case CmdDestroyWindow: { + uint8_t destroyedWinID = Window::Destroy(payload, &mainScreen); + if (destroyedWinID < 0) { + LOG_WARN(F("Failed to destroy window\n")); + break; + } + + LOG_INFO(F("Succesfully destroyed window: %d\n"), destroyedWinID); + + break; + } + case CMDData: { + LOG_DEBUG(F("Data Received: %d bytes\r\n"), resp); + + // int pos = 0; + // for (int k = 0; k < resp; k++) { + // // Write 2 hex digits and a space to our local buffer + // // %02X ensures leading zeros (e.g., 0A instead of A) + // pos += sprintf(lineBuffer + pos, "%02X ", payload[k]); + // + // // Every 8 bytes OR if it's the very last byte in the payload + // if ((k + 1) % 8 == 0 || k == resp - 1) { + // // Send the completed line to the logger + // // We use LOG_DEBUG or similar so we don't spam [WARN] on every line + // LOG_DEBUG(F("%s\r\n"), lineBuffer); + // + // // Reset buffer position for the next line + // pos = 0; + // memset(lineBuffer, 0, sizeof(lineBuffer)); + // } + // } + // LOG_DEBUG(F("\r\n")); + + Data::Parse(payload, resp, &mainScreen); + + break; + } + case CMDHealthCheck: { + LOG_INFO(F("HealthCheck request came in \r\n")); + + HealthCheckReply packet; + initHealthCheckReply(&packet); + WalkieTalkie::SendData(&packet); + + break; + } + default: + LOG_ERROR(F("Command: %s\n"), CommandToStr(cmd)); + LOG_ERROR(F("Unknown Command: `%02X` has not been implemented yet.\n"), cmd); + int pos = 0; + for (int k = 0; k < resp; k++) { + // Write 2 hex digits and a space to our local buffer + // %02X ensures leading zeros (e.g., 0A instead of A) + pos += sprintf(lineBuffer + pos, "%02X ", payload[k]); + + // Every 8 bytes OR if it's the very last byte in the payload + if ((k + 1) % 8 == 0 || k == resp - 1) { + // Send the completed line to the logger + // We use LOG_DEBUG or similar so we don't spam [WARN] on every line + LOG_WARN(F("%s\r\n"), lineBuffer); + + // Reset buffer position for the next line + pos = 0; + memset(lineBuffer, 0, sizeof(lineBuffer)); + } + } + LOG_WARN(F("\r\n")); + break; } } }