From 237bed1b728788d1f5b812b3fbb4889904a9a65b Mon Sep 17 00:00:00 2001 From: Kyle Schwarz Date: Tue, 18 Jul 2023 18:40:41 +0000 Subject: [PATCH] Device: Wait for device response in setRTC() --- device/device.cpp | 21 ++++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) diff --git a/device/device.cpp b/device/device.cpp index 819f9e8..0f23f4f 100644 --- a/device/device.cpp +++ b/device/device.cpp @@ -1,5 +1,6 @@ #include #include "icsneo/api/eventmanager.h" +#include "icsneo/communication/message/filter/main51messagefilter.h" #include "icsneo/communication/message/extendedresponsemessage.h" #include "icsneo/device/device.h" #include "icsneo/device/extensions/deviceextension.h" @@ -1751,7 +1752,7 @@ bool Device::setRTC(const std::chrono::time_point& ti // Create a vector of arguments to send as the payload to the communication command std::vector bytestream(sizeof(RTCCTIME)); auto rtcVals = (RTCCTIME*)bytestream.data(); - rtcVals->FracSec = (uint8_t)0X00; + rtcVals->FracSec = (uint8_t)0x00; rtcVals->Sec = (uint8_t)timeInfo->tm_sec; rtcVals->Min = (uint8_t)timeInfo->tm_min; rtcVals->Hour = (uint8_t)timeInfo->tm_hour; @@ -1760,8 +1761,22 @@ bool Device::setRTC(const std::chrono::time_point& ti rtcVals->Month = (uint8_t)timeInfo->tm_mon + 1; // [0-11] rtcVals->Year = (uint8_t)timeInfo->tm_year % 100; // divide by 100 and take remainder to get last 2 digits of year - com->sendCommand(Command::SetRTC, bytestream); - return true; + const auto generic = com->waitForMessageSync([&]() { + return com->sendCommand(Command::SetRTC, bytestream); + }, std::make_shared(Command::SetRTC), std::chrono::milliseconds(100)); + + if(!generic) { + report(APIEvent::Type::NoDeviceResponse, APIEvent::Severity::Error); + return false; + } + + auto m51msg = std::dynamic_pointer_cast(generic); + if(!m51msg || m51msg->data.size() != 1) { + report(APIEvent::Type::MessageFormattingError, APIEvent::Severity::Error); + return false; + } + + return m51msg->data.front(); } std::optional> Device::getSupportedFeatures() {