From 913040fd40d0f9197bd82e20c1e215b72af0fa76 Mon Sep 17 00:00:00 2001 From: Rob Ekelmans Date: Sun, 22 Oct 2023 18:18:46 +0000 Subject: [PATCH 01/21] Create StUartBootloaderCommandHandler --- services/st_util/CMakeLists.txt | 7 + .../st_util/StBootloaderCommandHandler.hpp | 37 ++ .../StUartBootloaderCommandHandler.cpp | 563 +++++++++++++++++ .../StUartBootloaderCommandHandler.hpp | 203 +++++++ services/st_util/test/CMakeLists.txt | 14 + .../TestStUartBootloaderCommandHandler.cpp | 570 ++++++++++++++++++ 6 files changed, 1394 insertions(+) create mode 100644 services/st_util/StBootloaderCommandHandler.hpp create mode 100644 services/st_util/StUartBootloaderCommandHandler.cpp create mode 100644 services/st_util/StUartBootloaderCommandHandler.hpp create mode 100644 services/st_util/test/CMakeLists.txt create mode 100644 services/st_util/test/TestStUartBootloaderCommandHandler.cpp diff --git a/services/st_util/CMakeLists.txt b/services/st_util/CMakeLists.txt index 3c9456fd..f03d8d73 100644 --- a/services/st_util/CMakeLists.txt +++ b/services/st_util/CMakeLists.txt @@ -10,9 +10,16 @@ target_sources(services.st_util PRIVATE FlashOnStUartProgrammer.hpp StUartProgrammer.cpp StUartProgrammer.hpp + StBootloaderCommandHandler.hpp + StUartBootloaderCommandHandler.cpp + StUartBootloaderCommandHandler.hpp ) target_link_libraries(services.st_util PUBLIC hal.interfaces services.util ) + +if (EMIL_BUILD_TESTS) + add_subdirectory(test) +endif() \ No newline at end of file diff --git a/services/st_util/StBootloaderCommandHandler.hpp b/services/st_util/StBootloaderCommandHandler.hpp new file mode 100644 index 00000000..0f722c4e --- /dev/null +++ b/services/st_util/StBootloaderCommandHandler.hpp @@ -0,0 +1,37 @@ +#ifndef SERVICES_ST_UTIL_ST_BOOTLOADER_COMMAND_HANDLER_HPP +#define SERVICES_ST_UTIL_ST_BOOTLOADER_COMMAND_HANDLER_HPP + +#include "infra/util//Function.hpp" + +namespace services +{ + class StBootloaderCommandHandler + { + protected: + StBootloaderCommandHandler() = default; + StBootloaderCommandHandler(const StBootloaderCommandHandler& other) = delete; + StBootloaderCommandHandler& operator=(const StBootloaderCommandHandler& other) = delete; + ~StBootloaderCommandHandler() = default; + + public: + virtual void GetCommand(infra::ByteRange& commands, const infra::Function& onDone) = 0; + virtual void GetVersion(const infra::Function& onDone) = 0; + virtual void GetId(const infra::Function& onDone) = 0; + virtual void ReadMemory(uint32_t address, uint8_t size, infra::ByteRange& data, const infra::Function& onDone) = 0; + virtual void Go(uint32_t address, const infra::Function& onDone) = 0; + virtual void WriteMemory(uint32_t address, infra::ConstByteRange data, const infra::Function& onDone) = 0; + virtual void Erase(uint8_t subcommand, const infra::Function& onDone) = 0; + virtual void Erase(uint8_t nPages, infra::ConstByteRange pages, const infra::Function& onDone) = 0; + virtual void ExtendedErase(uint16_t subcommand, const infra::Function& onDone) = 0; + virtual void ExtendedErase(uint8_t nPages, infra::ConstByteRange pages, const infra::Function& onDone) = 0; + virtual void Special(uint16_t subcommand, infra::ConstByteRange txData, infra::ByteRange& rxData, infra::ByteRange& rxStatus, const infra::Function& onDone) = 0; + virtual void ExtendedSpecial(uint16_t subcommand, infra::ConstByteRange txData1, infra::ConstByteRange txData2, infra::ByteRange& rxData, const infra::Function& onDone) = 0; + virtual void WriteProtect(infra::ConstByteRange sectors, const infra::Function& onDone) = 0; + virtual void WriteUnprotect(const infra::Function& onDone) = 0; + virtual void ReadoutProtect(const infra::Function& onDone) = 0; + virtual void ReadoutUnprotect(const infra::Function& onDone) = 0; + virtual void GetChecksum(uint32_t address, uint32_t memAreaSize, uint32_t crcPolinomial, uint32_t crcInitialization, const infra::Function& onDone) = 0; + }; +} + +#endif diff --git a/services/st_util/StUartBootloaderCommandHandler.cpp b/services/st_util/StUartBootloaderCommandHandler.cpp new file mode 100644 index 00000000..0e40cd7b --- /dev/null +++ b/services/st_util/StUartBootloaderCommandHandler.cpp @@ -0,0 +1,563 @@ +#include "services/st_util/StUartBootloaderCommandHandler.hpp" +#include "infra/util/ByteRange.hpp" +#include "infra/util/Endian.hpp" +#include "infra/util/Function.hpp" +#include "infra/util/MemoryRange.hpp" +#include "infra/util/Optional.hpp" +#include +#include +#include + +namespace +{ + constexpr uint8_t uartInitializationData = 0x7f; + + constexpr uint8_t getCommand = 0x00; + constexpr uint8_t getVersion = 0x01; + constexpr uint8_t getId = 0x02; + constexpr uint8_t readMemory = 0x11; + constexpr uint8_t go = 0x21; + constexpr uint8_t writeMemory = 0x31; + constexpr uint8_t erase = 0x43; + constexpr uint8_t extendedErase = 0x44; + constexpr uint8_t special = 0x50; + constexpr uint8_t extendedSpecial = 0x51; + constexpr uint8_t writeProtect = 0x63; + constexpr uint8_t writeUnprotect = 0x73; + constexpr uint8_t readoutProtect = 0x82; + constexpr uint8_t readoutUnprotect = 0x92; + constexpr uint8_t getChecksum = 0xa1; + + constexpr uint8_t ack = 0x79; + constexpr uint8_t nack = 0x1F; + + constexpr auto commandTimeout = std::chrono::seconds(1); +} + +namespace services +{ + StUartBootloaderCommandHandler::StUartBootloaderCommandHandler(hal::SerialCommunication& serial, const infra::Function& onInitialized, const infra::Function& onError) + : serial(serial) + , onInitialized(onInitialized) + , onError(onError) + , queue([this]() + { + DataReceived(); + }) + { + serial.ReceiveData([this](auto data) + { + queue.AddFromInterrupt(data); + }); + + InitializeUartBootloader(); + } + + StUartBootloaderCommandHandler::~StUartBootloaderCommandHandler() + { + serial.ReceiveData([](auto data) {}); + } + + void StUartBootloaderCommandHandler::GetCommand(infra::ByteRange& commands, const infra::Function& onDone) + { + if (commands.size() < 15) + std::abort(); + + commandActions.emplace_back(infra::InPlaceType(), *this, getCommand); + commandActions.emplace_back(infra::InPlaceType(), *this); + commandActions.emplace_back(infra::InPlaceType(), *this, commands); + commandActions.emplace_back(infra::InPlaceType(), *this); + + timeout.Start(commandTimeout, [this]() + { + OnError("Timeout getting commands"); + }); + + ExecuteCommand([this, onDone, &commands]() + { + uint8_t major = commands.front() >> 4; + uint8_t minor = commands.front() & 0xf; + + for (auto i = 0; i < commands.size() - 1; ++i) + commands[i] = commands[i + 1]; + + commands.back() = 0; + commands = infra::DiscardTail(commands, 1); + + onDone(major, minor); + }); + } + + void StUartBootloaderCommandHandler::GetVersion(const infra::Function& onDone) + { + internalRange = infra::MakeByteRange(internalBuffer); + + commandActions.emplace_back(infra::InPlaceType(), *this, getVersion); + commandActions.emplace_back(infra::InPlaceType(), *this); + commandActions.emplace_back(infra::InPlaceType(), *this, internalRange, 3); + commandActions.emplace_back(infra::InPlaceType(), *this); + + timeout.Start(commandTimeout, [this]() + { + OnError("Timeout getting version"); + }); + + ExecuteCommand([this, onDone]() + { + uint8_t major = internalBuffer[0] >> 4; + uint8_t minor = internalBuffer[0] & 0xf; + onDone(major, minor); + }); + } + + void StUartBootloaderCommandHandler::GetId(const infra::Function& onDone) + { + internalRange = infra::MakeByteRange(internalBuffer); + + commandActions.emplace_back(infra::InPlaceType(), *this, getId); + commandActions.emplace_back(infra::InPlaceType(), *this); + commandActions.emplace_back(infra::InPlaceType(), *this, internalRange); + commandActions.emplace_back(infra::InPlaceType(), *this); + + timeout.Start(commandTimeout, [this]() + { + OnError("Timeout getting id"); + }); + + ExecuteCommand([this, onDone]() + { + onDone((internalBuffer[0] << 8) | internalBuffer[1]); + }); + } + + void StUartBootloaderCommandHandler::ReadMemory(uint32_t address, uint8_t size, infra::ByteRange& data, const infra::Function& onDone) + { + this->address = address; + this->size = size - 1; + + commandActions.emplace_back(infra::InPlaceType(), *this, readMemory); + commandActions.emplace_back(infra::InPlaceType(), *this); + commandActions.emplace_back(infra::InPlaceType(), *this, infra::MakeByteRange(this->address)); + commandActions.emplace_back(infra::InPlaceType(), *this); + commandActions.emplace_back(infra::InPlaceType(), *this, this->size); + commandActions.emplace_back(infra::InPlaceType(), *this); + commandActions.emplace_back(infra::InPlaceType(), *this, data, size); + + timeout.Start(commandTimeout, [this]() + { + OnError("Timeout reading memory"); + }); + + ExecuteCommand(onDone); + } + + void StUartBootloaderCommandHandler::Go(uint32_t address, const infra::Function& onDone) + { + this->address = address; + + commandActions.emplace_back(infra::InPlaceType(), *this, go); + commandActions.emplace_back(infra::InPlaceType(), *this); + commandActions.emplace_back(infra::InPlaceType(), *this, infra::MakeByteRange(this->address)); + commandActions.emplace_back(infra::InPlaceType(), *this); + + timeout.Start(commandTimeout, [this]() + { + OnError("Timeout go"); + }); + + ExecuteCommand(onDone); + } + + void StUartBootloaderCommandHandler::WriteMemory(uint32_t address, infra::ConstByteRange data, const infra::Function& onDone) + { + if (data.size() > 256) + std::abort(); + + this->address = address; + internalBuffer[0] = static_cast(data.size() + 1); + std::copy(data.begin(), data.begin() + data.size(), internalBuffer.begin() + 1); + + commandActions.emplace_back(infra::InPlaceType(), *this, writeMemory); + commandActions.emplace_back(infra::InPlaceType(), *this); + commandActions.emplace_back(infra::InPlaceType(), *this, infra::MakeByteRange(this->address)); + commandActions.emplace_back(infra::InPlaceType(), *this); + commandActions.emplace_back(infra::InPlaceType(), *this, infra::MakeRange(internalBuffer.begin(), internalBuffer.begin() + internalBuffer[0])); + commandActions.emplace_back(infra::InPlaceType(), *this); + + timeout.Start(commandTimeout, [this]() + { + OnError("Timeout writing memory"); + }); + + ExecuteCommand(onDone); + } + + void StUartBootloaderCommandHandler::Erase(uint8_t subcommand, const infra::Function& onDone) + { + commandActions.emplace_back(infra::InPlaceType(), *this, erase); + commandActions.emplace_back(infra::InPlaceType(), *this); + commandActions.emplace_back(infra::InPlaceType(), *this, subcommand); + commandActions.emplace_back(infra::InPlaceType(), *this); + + timeout.Start(commandTimeout, [this]() + { + OnError("Timeout erasing memory"); + }); + + ExecuteCommand(onDone); + } + + void StUartBootloaderCommandHandler::Erase(uint8_t nPages, infra::ConstByteRange pages, const infra::Function& onDone) + { + if (pages.size() > 256) + std::abort(); + + internalBuffer[0] = static_cast(pages.size() + 1); + std::copy(pages.begin(), pages.begin() + pages.size(), internalBuffer.begin() + 1); + + commandActions.emplace_back(infra::InPlaceType(), *this, erase); + commandActions.emplace_back(infra::InPlaceType(), *this); + commandActions.emplace_back(infra::InPlaceType(), *this, infra::MakeRange(internalBuffer.begin(), internalBuffer.begin() + pages.size() + 1)); + commandActions.emplace_back(infra::InPlaceType(), *this); + + timeout.Start(commandTimeout, [this]() + { + OnError("Timeout erasing memory"); + }); + + ExecuteCommand(onDone); + } + + void StUartBootloaderCommandHandler::ExtendedErase(uint16_t subcommand, const infra::Function& onDone) + { + this->subcommand = subcommand; + + commandActions.emplace_back(infra::InPlaceType(), *this, extendedErase); + commandActions.emplace_back(infra::InPlaceType(), *this); + commandActions.emplace_back(infra::InPlaceType(), *this, infra::MakeByteRange(this->subcommand)); + commandActions.emplace_back(infra::InPlaceType(), *this); + + timeout.Start(commandTimeout, [this]() + { + OnError("Timeout extended erasing memory"); + }); + + ExecuteCommand(onDone); + } + + void StUartBootloaderCommandHandler::ExtendedErase(uint8_t nPages, infra::ConstByteRange pages, const infra::Function& onDone) + { + if (pages.size() > 256) + std::abort(); + + internalBuffer[0] = static_cast(nPages >> 8); + internalBuffer[1] = static_cast(nPages & 0xff) + 1; + std::copy(pages.begin(), pages.begin() + pages.size(), internalBuffer.begin() + 2); + + commandActions.emplace_back(infra::InPlaceType(), *this, extendedErase); + commandActions.emplace_back(infra::InPlaceType(), *this); + commandActions.emplace_back(infra::InPlaceType(), *this, infra::MakeRange(internalBuffer.begin(), internalBuffer.begin() + pages.size() + 2)); + commandActions.emplace_back(infra::InPlaceType(), *this); + + timeout.Start(commandTimeout, [this]() + { + OnError("Timeout extended erasing memory"); + }); + + ExecuteCommand(onDone); + } + + void StUartBootloaderCommandHandler::Special(uint16_t subcommand, infra::ConstByteRange txData, infra::ByteRange& rxData, infra::ByteRange& rxStatus, const infra::Function& onDone) + { + this->subcommand = subcommand; + + internalBuffer[0] = static_cast(txData.size() >> 8); + internalBuffer[1] = static_cast(txData.size() & 0xff); + + commandActions.emplace_back(infra::InPlaceType(), *this, special); + commandActions.emplace_back(infra::InPlaceType(), *this); + commandActions.emplace_back(infra::InPlaceType(), *this, infra::MakeByteRange(this->subcommand)); + commandActions.emplace_back(infra::InPlaceType(), *this); + commandActions.emplace_back(infra::InPlaceType(), *this, infra::MakeRange(internalBuffer.begin(), internalBuffer.begin() + 2)); + if (!txData.empty()) + commandActions.emplace_back(infra::InPlaceType(), *this, txData); + commandActions.emplace_back(infra::InPlaceType(), *this); + commandActions.emplace_back(infra::InPlaceType(), *this, rxData); + commandActions.emplace_back(infra::InPlaceType(), *this, rxStatus); + + timeout.Start(commandTimeout, [this]() + { + OnError("Timeout special command"); + }); + + ExecuteCommand(onDone); + } + + void StUartBootloaderCommandHandler::ExtendedSpecial(uint16_t subcommand, infra::ConstByteRange txData1, infra::ConstByteRange txData2, infra::ByteRange& rxData, const infra::Function& onDone) + { + this->subcommand = subcommand; + + internalBuffer[0] = static_cast(txData1.size() >> 8); + internalBuffer[1] = static_cast(txData1.size() & 0xff); + internalBuffer[2] = static_cast(txData2.size() >> 8); + internalBuffer[3] = static_cast(txData2.size() & 0xff); + + commandActions.emplace_back(infra::InPlaceType(), *this, extendedSpecial); + commandActions.emplace_back(infra::InPlaceType(), *this); + commandActions.emplace_back(infra::InPlaceType(), *this, infra::MakeByteRange(this->subcommand)); + commandActions.emplace_back(infra::InPlaceType(), *this); + commandActions.emplace_back(infra::InPlaceType(), *this, infra::MakeRange(internalBuffer.begin(), internalBuffer.begin() + 2)); + if (!txData1.empty()) + commandActions.emplace_back(infra::InPlaceType(), *this, txData1); + commandActions.emplace_back(infra::InPlaceType(), *this); + commandActions.emplace_back(infra::InPlaceType(), *this, infra::MakeRange(internalBuffer.begin() + 2, internalBuffer.begin() + 4)); + if (!txData2.empty()) + commandActions.emplace_back(infra::InPlaceType(), *this, txData2); + commandActions.emplace_back(infra::InPlaceType(), *this); + commandActions.emplace_back(infra::InPlaceType(), *this, rxData); + + timeout.Start(commandTimeout, [this]() + { + OnError("Timeout extended special command"); + }); + + ExecuteCommand(onDone); + } + + void StUartBootloaderCommandHandler::WriteProtect(infra::ConstByteRange sectors, const infra::Function& onDone) + { + std::abort(); + } + + void StUartBootloaderCommandHandler::WriteUnprotect(const infra::Function& onDone) + { + std::abort(); + } + + void StUartBootloaderCommandHandler::ReadoutProtect(const infra::Function& onDone) + { + std::abort(); + } + + void StUartBootloaderCommandHandler::ReadoutUnprotect(const infra::Function& onDone) + { + std::abort(); + } + + void StUartBootloaderCommandHandler::GetChecksum(uint32_t address, uint32_t memAreaSize, uint32_t crcPolinomial, uint32_t crcInitialization, const infra::Function& onDone) + { + std::abort(); + } + + void StUartBootloaderCommandHandler::InitializeUartBootloader() + { + commandActions.emplace_back(infra::InPlaceType(), *this, infra::MakeByteRange(uartInitializationData)); + commandActions.emplace_back(infra::InPlaceType(), *this); + + timeout.Start(commandTimeout, [this]() + { + OnError("Timeout waiting for bootloader initialization"); + }); + + ExecuteCommand([this]() + { + onInitialized(); + }); + } + + void StUartBootloaderCommandHandler::ExecuteCommand(const infra::Function) + sizeof(infra::ByteRange)>& onCommandExecuted) + { + this->onCommandExecuted = onCommandExecuted; + TryHandleTransmitAction(); + } + + void StUartBootloaderCommandHandler::TryHandleTransmitAction() + { + if (commandActions.front()->GetActionType() == ActionType::transmit) + { + static auto transmitaction = commandActions.front(); + transmitaction = commandActions.front(); + + commandActions.pop_front(); + transmitaction->SendData(); + } + } + + void StUartBootloaderCommandHandler::TryHandleNextAction() + { + commandActions.pop_front(); + + if (commandActions.empty()) + { + timeout.Cancel(); + onCommandExecuted(); + return; + } + + if (commandActions.front()->GetActionType() == ActionType::receive) + commandActions.front()->DataReceived(); + else + TryHandleTransmitAction(); + } + + void StUartBootloaderCommandHandler::SendData(infra::ConstByteRange data, uint8_t checksum) + { + claimer.Claim([this, data, checksum]() + { + serial.SendData(data, [this, checksum]() + { + serial.SendData(infra::MakeByteRange(checksum), [this]() + { + claimer.Release(); + TryHandleTransmitAction(); + }); + }); + }); + } + + void StUartBootloaderCommandHandler::SendData(infra::ConstByteRange data) + { + claimer.Claim([this, data]() + { + serial.SendData(data, [this]() + { + claimer.Release(); + TryHandleTransmitAction(); + }); + }); + } + + void StUartBootloaderCommandHandler::DataReceived() + { + if (!commandActions.empty()) + commandActions.front()->DataReceived(); + } + + void StUartBootloaderCommandHandler::OnError(infra::BoundedConstString reason) + { + timeout.Cancel(); + serial.ReceiveData([](auto data) {}); + if (onError) + onError(reason); + } + + StUartBootloaderCommandHandler::Action::Action(StUartBootloaderCommandHandler& handler, StUartBootloaderCommandHandler::ActionType type) + : handler(handler) + , type(type) + {} + + void StUartBootloaderCommandHandler::Action::SendData() + { + std::abort(); + } + + void StUartBootloaderCommandHandler::Action::DataReceived() + { + std::abort(); + } + + StUartBootloaderCommandHandler::ActionType StUartBootloaderCommandHandler::Action::GetActionType() const + { + return type; + } + + StUartBootloaderCommandHandler::ReceiveAction::ReceiveAction(StUartBootloaderCommandHandler& handler) + : StUartBootloaderCommandHandler::Action(handler, StUartBootloaderCommandHandler::ActionType::receive) + {} + + StUartBootloaderCommandHandler::TransmitAction::TransmitAction(StUartBootloaderCommandHandler& handler) + : StUartBootloaderCommandHandler::Action(handler, StUartBootloaderCommandHandler::ActionType::transmit) + {} + + void StUartBootloaderCommandHandler::ReceiveAckAction::DataReceived() + { + auto byte = handler.queue.Get(); + + if (byte == ack) + handler.TryHandleNextAction(); + else + handler.OnError("NACK received"); + } + + StUartBootloaderCommandHandler::ReceiveBufferAction::ReceiveBufferAction(StUartBootloaderCommandHandler& handler, infra::ByteRange& data) + : StUartBootloaderCommandHandler::ReceiveAction(handler) + , data(data) + {} + + StUartBootloaderCommandHandler::ReceiveBufferAction::ReceiveBufferAction(StUartBootloaderCommandHandler& handler, infra::ByteRange& data, const std::size_t nBytes) + : StUartBootloaderCommandHandler::ReceiveAction(handler) + , data(data) + { + this->nBytes.Emplace(nBytes); + } + + void StUartBootloaderCommandHandler::ReceiveBufferAction::DataReceived() + { + if (handler.queue.Empty()) + return; + + infra::ByteInputStream stream(handler.queue.ContiguousRange()); + + if (!nBytes) + ExtractNumberOfBytes(stream); + + auto buffer = infra::Head(infra::DiscardHead(data, nBytesReceived), std::min(stream.Available(), *nBytes - nBytesReceived)); + stream >> buffer; + handler.queue.Consume(buffer.size()); + + nBytesReceived += buffer.size(); + if (nBytesReceived == nBytes) + { + data.shrink_from_back_to(nBytesReceived); + handler.TryHandleNextAction(); + } + } + + void StUartBootloaderCommandHandler::ReceiveRegularBufferAction::ExtractNumberOfBytes(infra::ByteInputStream& stream) + { + nBytes.Emplace(stream.Extract() + 1); + handler.queue.Consume(sizeof(uint8_t)); + } + + void StUartBootloaderCommandHandler::ReceiveSpecialBufferAction::ExtractNumberOfBytes(infra::ByteInputStream& stream) + { + nBytes.Emplace(stream.Extract>()); + handler.queue.Consume(sizeof(uint16_t)); + } + + StUartBootloaderCommandHandler::TransmitRawAction::TransmitRawAction(StUartBootloaderCommandHandler& handler, infra::ConstByteRange data) + : StUartBootloaderCommandHandler::TransmitAction(handler) + , data(data) + {} + + void StUartBootloaderCommandHandler::TransmitRawAction::SendData() + { + handler.SendData(data); + } + + StUartBootloaderCommandHandler::TransmitWithTwosComplementChecksum::TransmitWithTwosComplementChecksum(StUartBootloaderCommandHandler& handler, uint8_t data) + : StUartBootloaderCommandHandler::TransmitAction(handler) + , data(data) + {} + + void StUartBootloaderCommandHandler::TransmitWithTwosComplementChecksum::SendData() + { + auto checksum = data ^ 0xff; + handler.SendData(infra::MakeByteRange(data), checksum); + } + + StUartBootloaderCommandHandler::TransmitWithChecksumAction::TransmitWithChecksumAction(StUartBootloaderCommandHandler& handler, infra::ConstByteRange data) + : StUartBootloaderCommandHandler::TransmitAction(handler) + , data(data) + {} + + void StUartBootloaderCommandHandler::TransmitWithChecksumAction::SendData() + { + auto checksum = 0; + + for (auto b : data) + checksum ^= b; + + handler.SendData(data, checksum); + } +} \ No newline at end of file diff --git a/services/st_util/StUartBootloaderCommandHandler.hpp b/services/st_util/StUartBootloaderCommandHandler.hpp new file mode 100644 index 00000000..17e879f7 --- /dev/null +++ b/services/st_util/StUartBootloaderCommandHandler.hpp @@ -0,0 +1,203 @@ +#ifndef SERVICES_ST_UTIL_ST_UART_BOOTLOADER_COMMAND_HANDLER_HPP +#define SERVICES_ST_UTIL_ST_UART_BOOTLOADER_COMMAND_HANDLER_HPP + +#include "hal/interfaces/SerialCommunication.hpp" +#include "infra/event/ClaimableResource.hpp" +#include "infra/event/QueueForOneReaderOneIrqWriter.hpp" +#include "infra/timer/Timer.hpp" +#include "infra/util/BoundedString.hpp" +#include "infra/util/ByteRange.hpp" +#include "infra/stream/ByteInputStream.hpp" +#include "infra/util/Optional.hpp" +#include "services/st_util/StBootloaderCommandHandler.hpp" +#include "infra/util/PolymorphicVariant.hpp" +#include +#include +#include "infra/util/Endian.hpp" +#include + +namespace services +{ + class StUartBootloaderCommandHandler + : public StBootloaderCommandHandler + { + public: + StUartBootloaderCommandHandler(hal::SerialCommunication& serial, const infra::Function& onInitialized, const infra::Function& onError); + ~StUartBootloaderCommandHandler(); + + // Implementation of StBootloaderCommandHandler + void GetCommand(infra::ByteRange& commands, const infra::Function& onDone) override; + void GetVersion(const infra::Function& onDone) override; + void GetId(const infra::Function& onDone) override; + void ReadMemory(uint32_t address, uint8_t size, infra::ByteRange& data, const infra::Function& onDone) override; + void Go(uint32_t address, const infra::Function& onDone) override; + void WriteMemory(uint32_t address, infra::ConstByteRange data, const infra::Function& onDone) override; + void Erase(uint8_t subcommand, const infra::Function& onDone) override; + void Erase(uint8_t nPages, infra::ConstByteRange pages, const infra::Function& onDone) override; + void ExtendedErase(uint16_t subcommand, const infra::Function& onDone) override; + void ExtendedErase(uint8_t nPages, infra::ConstByteRange pages, const infra::Function& onDone) override; + void Special(uint16_t subcommand, infra::ConstByteRange txData, infra::ByteRange& rxData, infra::ByteRange& rxStatus, const infra::Function& onDone) override; + void ExtendedSpecial(uint16_t subcommand, infra::ConstByteRange txData1, infra::ConstByteRange txData2, infra::ByteRange& rxData, const infra::Function& onDone) override; + void WriteProtect(infra::ConstByteRange sectors, const infra::Function& onDone) override; + void WriteUnprotect(const infra::Function& onDone) override; + void ReadoutProtect(const infra::Function& onDone) override; + void ReadoutUnprotect(const infra::Function& onDone) override; + void GetChecksum(uint32_t address, uint32_t memAreaSize, uint32_t crcPolinomial, uint32_t crcInitialization, const infra::Function& onDone) override; + + private: + void InitializeUartBootloader(); + void ExecuteCommand(const infra::Function) + sizeof(infra::ByteRange)>& onCommandExecuted); + void TryHandleTransmitAction(); + void TryHandleNextAction(); + void SendData(infra::ConstByteRange data, uint8_t checksum); + void SendData(infra::ConstByteRange data); + void DataReceived(); + void OnError(infra::BoundedConstString reason); + + private: + enum class ActionType : uint8_t + { + receive, + transmit, + }; + + class Action + { + public: + Action(StUartBootloaderCommandHandler& handler, ActionType type); + Action(const Action& other) = default; + Action& operator=(const Action& other) = default; + virtual ~Action() = default; + + virtual void SendData(); + virtual void DataReceived(); + + ActionType GetActionType() const; + + protected: + StUartBootloaderCommandHandler& handler; + + private: + ActionType type; + }; + + class ReceiveAction + : public Action + { + public: + explicit ReceiveAction(StUartBootloaderCommandHandler& handler); + }; + + class ReceiveAckAction + : public ReceiveAction + { + public: + using ReceiveAction::ReceiveAction; + + void DataReceived() override; + }; + + class ReceiveBufferAction + : public ReceiveAction + { + public: + ReceiveBufferAction(StUartBootloaderCommandHandler& handler, infra::ByteRange& data); + ReceiveBufferAction(StUartBootloaderCommandHandler& handler, infra::ByteRange& data, const std::size_t nBytes); + + void DataReceived() override; + + protected: + virtual void ExtractNumberOfBytes(infra::ByteInputStream& stream) {}; + + protected: + infra::ByteRange& data; + infra::Optional nBytes; + std::size_t nBytesReceived = 0; + }; + + class ReceiveRegularBufferAction + : public ReceiveBufferAction + { + public: + using ReceiveBufferAction::ReceiveBufferAction; + + protected: + void ExtractNumberOfBytes(infra::ByteInputStream& stream) override; + }; + + class ReceiveSpecialBufferAction + : public ReceiveBufferAction + { + public: + using ReceiveBufferAction::ReceiveBufferAction; + + protected: + void ExtractNumberOfBytes(infra::ByteInputStream& stream) override; + }; + + class TransmitAction + : public Action + { + public: + explicit TransmitAction(StUartBootloaderCommandHandler& handler); + }; + + class TransmitRawAction + : public TransmitAction + { + public: + TransmitRawAction(StUartBootloaderCommandHandler& handler, infra::ConstByteRange data); + + void SendData() override; + + private: + infra::ConstByteRange data; + }; + + class TransmitWithTwosComplementChecksum + : public TransmitAction + { + public: + TransmitWithTwosComplementChecksum(StUartBootloaderCommandHandler& handler, uint8_t data); + + void SendData() override; + + private: + uint8_t data; + }; + + class TransmitWithChecksumAction + : public TransmitAction + { + public: + TransmitWithChecksumAction(StUartBootloaderCommandHandler& handler, infra::ConstByteRange data); + + void SendData() override; + + private: + infra::ConstByteRange data; + }; + + private: + hal::SerialCommunication& serial; + infra::AutoResetFunction onInitialized; + infra::AutoResetFunction onError; + infra::QueueForOneReaderOneIrqWriter::WithStorage<257> queue; + infra::ClaimableResource resource; + infra::ClaimableResource::Claimer startClaimer{ resource }; + infra::ClaimableResource::Claimer::WithSize<2 * sizeof(StUartBootloaderCommandHandler*) + 3 * sizeof(infra::ConstByteRange)> claimer{ resource }; + + infra::TimerSingleShot timeout; + std::array internalBuffer; + infra::ByteRange internalRange; + + std::deque> commandActions; + infra::AutoResetFunction) + sizeof(infra::ByteRange)> onCommandExecuted; + + infra::BigEndian address; + infra::BigEndian subcommand; + uint8_t size; + }; +} + +#endif diff --git a/services/st_util/test/CMakeLists.txt b/services/st_util/test/CMakeLists.txt new file mode 100644 index 00000000..eae06246 --- /dev/null +++ b/services/st_util/test/CMakeLists.txt @@ -0,0 +1,14 @@ +add_executable(services.st_util_test) +emil_add_test(services.st_util_test) + +target_sources(services.st_util_test PRIVATE + TestStUartBootloaderCommandHandler.cpp +) + +target_link_libraries(services.st_util_test PUBLIC + gmock_main + services.st_util + hal.interfaces_test_doubles + infra.util_test_helper + infra.timer_test_helper +) diff --git a/services/st_util/test/TestStUartBootloaderCommandHandler.cpp b/services/st_util/test/TestStUartBootloaderCommandHandler.cpp new file mode 100644 index 00000000..7b8bc4bb --- /dev/null +++ b/services/st_util/test/TestStUartBootloaderCommandHandler.cpp @@ -0,0 +1,570 @@ +#include "hal/interfaces/test_doubles/SerialCommunicationMock.hpp" +#include "infra/timer/Timer.hpp" +#include "infra/timer/test_helper/ClockFixture.hpp" +#include "infra/util/BoundedString.hpp" +#include "infra/util/ByteRange.hpp" +#include "infra/util/Function.hpp" +#include "infra/util/test_helper/MockCallback.hpp" +#include "services/st_util/StUartBootloaderCommandHandler.hpp" +#include "gmock/gmock.h" +#include "gtest/gtest.h" +#include +#include +#include +#include + +class StUartBootloaderCommandHandlerTest + : public testing::Test + , public infra::ClockFixture +{ +public: + void Initialize() + { + ForwardTime(std::chrono::milliseconds(20)); + serialCommunication.dataReceived(std::vector{ 0x79 }); + + EXPECT_CALL(onInitializedMock, callback()); + ExecuteAllActions(); + } + + void ExpectSendData(std::vector data, infra::Function onSendData = infra::emptyFunction) + { + EXPECT_CALL(serialCommunication, SendDataMock(data)).WillOnce(testing::Invoke([this, onSendData](infra::ConstByteRange data) + { + onSendData(); + serialCommunication.actionOnCompletion(); + })); + } + + void ExpectSendData(std::vector data, uint8_t checksum) + { + ExpectSendData(data, [this, checksum]() + { + ExpectSendData({ checksum }); + }); + } + + void ExpectSendCommand(uint8_t command, uint8_t checksum) + { + ExpectSendData({ command }, [this, checksum]() + { + ExpectSendData({ checksum }); + }); + } + + void ExpectErrorOnTimeout(infra::Duration timeout, std::string errorMessage) + { + EXPECT_CALL(onErrorMock, callback(infra::BoundedConstString{ errorMessage })); + ForwardTime(timeout); + } + + void ExpectReceiveData(std::vector data) + { + serialCommunication.dataReceived(data); + ExecuteAllActions(); + } + +public: + testing::StrictMock serialCommunication; + testing::StrictMock> onInitializedMock; + testing::StrictMock> onErrorMock; + infra::Execute execute{ [this] + { + ExpectSendData({ 0x7f }); + } }; + const infra::Function onInitialized{ [this]() + { + onInitializedMock.callback(); + } }; + const infra::Function onError{ [this](infra::BoundedConstString reason) + { + onErrorMock.callback(reason); + } }; + services::StUartBootloaderCommandHandler handler{ serialCommunication, onInitialized, onError }; +}; + +TEST_F(StUartBootloaderCommandHandlerTest, creation_initializes_uart_bootloader) +{ + Initialize(); +} + +TEST_F(StUartBootloaderCommandHandlerTest, creation_initializes_uart_bootloader_fails) +{ + infra::BoundedConstString::WithStorage<64> errorMessage("Timeout waiting for bootloader initialization"); + EXPECT_CALL(onErrorMock, callback(errorMessage)); + ForwardTime(std::chrono::milliseconds(1000)); +} + +// TEST_F(StUartBootloaderCommandHandlerTest, receive_nack) +// TEST_F(StUartBootloaderCommandHandlerTest, receive_failing_stream) +// TEST_F(StUartBootloaderCommandHandlerTest, receive_invalid_data) + +TEST_F(StUartBootloaderCommandHandlerTest, GetCommand) +{ + Initialize(); + infra::VerifyingFunctionMock ondone(3, 2); + std::array commandsbuffer = {}; + infra::ByteRange commands(commandsbuffer); + + ExpectSendCommand(0x00, 0xff); + handler.GetCommand(commands, ondone); + ExpectReceiveData({ 0x79, 0x0b, 0x32, 0x00, 0x01, 0x02, 0x11, 0x21, 0x31, 0x44, 0x63, 0x73, 0x82, 0x92, 0x79 }); + + EXPECT_EQ(commands.size(), 0x0b); + EXPECT_EQ((std::array{ 0x00, 0x01, 0x02, 0x11, 0x21, 0x31, 0x44, 0x63, 0x73, 0x82, 0x92 }), commandsbuffer); +} + +TEST_F(StUartBootloaderCommandHandlerTest, GetCommand_receive_intermittend) +{ + Initialize(); + infra::VerifyingFunctionMock ondone(3, 2); + std::array commandsbuffer = {}; + infra::ByteRange commands(commandsbuffer); + + ExpectSendCommand(0x00, 0xff); + handler.GetCommand(commands, ondone); + ExpectReceiveData({ 0x79, 0x0b, 0x32, 0x00, 0x01 }); + ExpectReceiveData({ 0x02, 0x11, 0x21, 0x31, 0x44, 0x63, 0x73, 0x82, 0x92, 0x79 }); + + EXPECT_EQ((std::array{ 0x00, 0x01, 0x02, 0x11, 0x21, 0x31, 0x44, 0x63, 0x73, 0x82, 0x92 }), commandsbuffer); +} + +TEST_F(StUartBootloaderCommandHandlerTest, GetCommand_timeout) +{ + Initialize(); + testing::StrictMock> ondone; + std::array commandsbuffer = {}; + infra::ByteRange commands(commandsbuffer); + + ExpectSendCommand(0x00, 0xff); + handler.GetCommand(commands, [&ondone](uint8_t, uint8_t) + { + ondone.callback(); + }); + ExpectErrorOnTimeout(std::chrono::seconds(1), "Timeout getting commands"); +} + +TEST_F(StUartBootloaderCommandHandlerTest, GetVersion) +{ + Initialize(); + infra::VerifyingFunctionMock ondone(3, 1); + + ExpectSendCommand(0x01, 0xfe); + handler.GetVersion(ondone); + ExpectReceiveData({ 0x79, 0x31, 0x00, 0x00, 0x79 }); +} + +TEST_F(StUartBootloaderCommandHandlerTest, GetVersion_timeout) +{ + Initialize(); + testing::StrictMock> ondone; + + ExpectSendCommand(0x01, 0xfe); + handler.GetVersion([&ondone](uint8_t, uint8_t) + { + ondone.callback(); + }); + ExpectErrorOnTimeout(std::chrono::seconds(1), "Timeout getting version"); +} + +TEST_F(StUartBootloaderCommandHandlerTest, GetId) +{ + Initialize(); + infra::VerifyingFunctionMock ondone(0x0495); + + ExpectSendCommand(0x02, 0xfd); + handler.GetId(ondone); + ExpectReceiveData({ 0x79, 0x01, 0x04, 0x95, 0x79 }); +} + +TEST_F(StUartBootloaderCommandHandlerTest, GetId_timeout) +{ + Initialize(); + testing::StrictMock> ondone; + + ExpectSendCommand(0x02, 0xfd); + handler.GetId([&ondone](uint16_t) + { + ondone.callback(); + }); + ExpectErrorOnTimeout(std::chrono::seconds(1), "Timeout getting id"); +} + +TEST_F(StUartBootloaderCommandHandlerTest, ReadMemory) +{ + Initialize(); + infra::VerifyingFunctionMock ondone; + uint32_t address = 0x01020304; + std::array dataBuffer = {}; + infra::ByteRange data(dataBuffer); + + ExpectSendCommand(0x11, 0xEE); + handler.ReadMemory(address, 3, data, ondone); + + ExpectSendData({ 0x01, 0x02, 0x03, 0x04 }, 0x04); + ExpectReceiveData({ 0x79 }); + + ExpectSendCommand(0x02, 0xFD); + ExpectReceiveData({ 0x79 }); + + ExpectReceiveData({ 0x79 }); + ExpectReceiveData({ 0x05, 0x06, 0x07 }); + + EXPECT_EQ((std::array{ 0x05, 0x06, 0x07, 0x00 }), dataBuffer); +} + +TEST_F(StUartBootloaderCommandHandlerTest, ReadMemory_timeout) +{ + Initialize(); + testing::StrictMock> ondone; + std::array dataBuffer = {}; + infra::ByteRange data(dataBuffer); + + ExpectSendCommand(0x11, 0xEE); + handler.ReadMemory(0x01020304, 3, data, [&ondone]() + { + ondone.callback(); + }); + ExpectErrorOnTimeout(std::chrono::seconds(1), "Timeout reading memory"); +} + +TEST_F(StUartBootloaderCommandHandlerTest, Go) +{ + Initialize(); + infra::VerifyingFunctionMock ondone; + uint32_t address = 0x01020304; + + ExpectSendCommand(0x21, 0xde); + handler.Go(address, ondone); + + ExpectSendData({ 0x01, 0x02, 0x03, 0x04 }, 0x04); + ExpectReceiveData({ 0x79 }); + + ExpectReceiveData({ 0x79 }); +} + +TEST_F(StUartBootloaderCommandHandlerTest, Go_timeout) +{ + Initialize(); + testing::StrictMock> ondone; + uint32_t address = 0x01020304; + + ExpectSendCommand(0x21, 0xde); + handler.Go(address, [&ondone]() + { + ondone.callback(); + }); + ExpectErrorOnTimeout(std::chrono::seconds(1), "Timeout go"); +} + +TEST_F(StUartBootloaderCommandHandlerTest, WriteMemory) +{ + Initialize(); + infra::VerifyingFunctionMock ondone; + std::array data = { 0x05, 0x06, 0x07, 0x08 }; + uint32_t address = 0x01020304; + + ExpectSendCommand(0x31, 0xce); + handler.WriteMemory(address, infra::MakeByteRange(data), ondone); + + ExpectSendData({ 0x01, 0x02, 0x03, 0x04 }, 0x04); + ExpectReceiveData({ 0x79 }); + + ExpectSendData({ 0x05, 0x05, 0x06, 0x07, 0x08 }, 0x09); + ExpectReceiveData({ 0x79 }); + ExpectReceiveData({ 0x79 }); +} + +TEST_F(StUartBootloaderCommandHandlerTest, WriteMemory_timeout) +{ + Initialize(); + testing::StrictMock> ondone; + std::array data; + uint32_t address = 0x01020304; + + ExpectSendCommand(0x31, 0xce); + handler.WriteMemory(address, infra::MakeByteRange(data), [&ondone]() + { + ondone.callback(); + }); + ExpectErrorOnTimeout(std::chrono::seconds(1), "Timeout writing memory"); +} + +TEST_F(StUartBootloaderCommandHandlerTest, Erase_global) +{ + Initialize(); + infra::VerifyingFunctionMock ondone; + + ExpectSendCommand(0x43, 0xbc); + handler.Erase(0xff, ondone); + ExpectSendCommand(0xff, 0x00); + ExpectReceiveData({ 0x79 }); + ExpectReceiveData({ 0x79 }); +} + +TEST_F(StUartBootloaderCommandHandlerTest, Erase_pages) +{ + Initialize(); + infra::VerifyingFunctionMock ondone; + std::array pages = { 0x01, 0x02, 0x03, 0x04 }; + + ExpectSendCommand(0x43, 0xbc); + handler.Erase(4, infra::MakeByteRange(pages), ondone); + ExpectSendData({ 0x05, 0x01, 0x02, 0x03, 0x04 }, 0x01); + ExpectReceiveData({ 0x79 }); + ExpectReceiveData({ 0x79 }); +} + +TEST_F(StUartBootloaderCommandHandlerTest, Erase_timeout) +{ + Initialize(); + testing::StrictMock> ondone; + std::array data; + + ExpectSendCommand(0x43, 0xbc); + handler.Erase(0xff, infra::ByteRange{}, [&ondone]() + { + ondone.callback(); + }); + ExpectErrorOnTimeout(std::chrono::seconds(1), "Timeout erasing memory"); +} + +TEST_F(StUartBootloaderCommandHandlerTest, ExtendedErase_global) +{ + Initialize(); + infra::VerifyingFunctionMock ondone; + + ExpectSendCommand(0x44, 0xbb); + handler.ExtendedErase(0xfffe, ondone); + ExpectSendData({ 0xff, 0xfe }, 0x01); + ExpectReceiveData({ 0x79 }); + ExpectReceiveData({ 0x79 }); +} + +TEST_F(StUartBootloaderCommandHandlerTest, ExtendedErase_pages) +{ + Initialize(); + infra::VerifyingFunctionMock ondone; + std::array, 4> pages = { 0x0001, 0x0002, 0x0003, 0x0004 }; + + ExpectSendCommand(0x44, 0xbb); + handler.ExtendedErase(4, infra::MakeByteRange(pages), ondone); + ExpectSendData({ 0x00, 0x05, 0x00, 0x01, 0x00, 0x02, 0x00, 0x03, 0x00, 0x04 }, 0x01); + ExpectReceiveData({ 0x79 }); + ExpectReceiveData({ 0x79 }); +} + +TEST_F(StUartBootloaderCommandHandlerTest, ExtendedErase_timeout) +{ + Initialize(); + testing::StrictMock> ondone; + + ExpectSendCommand(0x44, 0xbb); + handler.ExtendedErase(0xffff, [&ondone]() + { + ondone.callback(); + }); + ExpectErrorOnTimeout(std::chrono::seconds(1), "Timeout extended erasing memory"); +} + +TEST_F(StUartBootloaderCommandHandlerTest, Special) +{ + Initialize(); + infra::VerifyingFunctionMock ondone; + uint16_t subcommand = 0x54; + std::array txData = { 0x01, 0x02, 0x03, 0x04 }; + std::array rxDataBuffer; + std::array rxStatusBuffer; + infra::ByteRange rxData(rxDataBuffer); + infra::ByteRange rxStatus(rxStatusBuffer); + + ExpectSendCommand(0x50, 0xaf); + handler.Special(subcommand, infra::MakeConstByteRange(txData), rxData, rxStatus, ondone); + + ExpectSendData({ 0x00, 0x54 }, 0x54); + ExpectReceiveData({ 0x79 }); + + ExpectSendData({0x00, 0x04}); + ExpectSendData({ 0x01, 0x02, 0x03, 0x04 }, 0x04); + ExpectReceiveData({ 0x79 }); + ExpectReceiveData({ 0x79 }); + + ExpectReceiveData({ 0x00, 0x04, 0x05, 0x06, 0x07, 0x08 }); + ExpectReceiveData({ 0x00, 0x04, 0x09, 0x0a, 0x0b, 0x0c }); + + EXPECT_EQ((std::array{ 0x05, 0x06, 0x07, 0x08 }), rxDataBuffer); + EXPECT_EQ((std::array{ 0x09, 0x0a, 0x0b, 0x0c }), rxStatusBuffer); +} + +TEST_F(StUartBootloaderCommandHandlerTest, Special_empty_rxData) +{ + Initialize(); + infra::VerifyingFunctionMock ondone; + uint16_t subcommand = 0x54; + std::array txData = { 0x01, 0x02, 0x03, 0x04 }; + std::array rxDataBuffer = {}; + std::array rxStatusBuffer; + infra::ByteRange rxData(rxDataBuffer); + infra::ByteRange rxStatus(rxStatusBuffer); + + ExpectSendCommand(0x50, 0xaf); + handler.Special(subcommand, infra::MakeConstByteRange(txData), rxData, rxStatus, ondone); + + ExpectSendData({ 0x00, 0x54 }, 0x54); + ExpectReceiveData({ 0x79 }); + + ExpectSendData({0x00, 0x04}); + ExpectSendData({ 0x01, 0x02, 0x03, 0x04 }, 0x04); + ExpectReceiveData({ 0x79 }); + ExpectReceiveData({ 0x79 }); + + ExpectReceiveData({ 0x00, 0x00 }); + ExpectReceiveData({ 0x00, 0x04, 0x09, 0x0a, 0x0b, 0x0c }); + + EXPECT_EQ((std::array{ }), rxDataBuffer); + EXPECT_EQ((std::array{ 0x09, 0x0a, 0x0b, 0x0c }), rxStatusBuffer); +} + +TEST_F(StUartBootloaderCommandHandlerTest, Special_empty_rxStatus) +{ + Initialize(); + infra::VerifyingFunctionMock ondone; + uint16_t subcommand = 0x54; + std::array txData = { 0x01, 0x02, 0x03, 0x04 }; + std::array rxDataBuffer; + std::array rxStatusBuffer = {}; + infra::ByteRange rxData(rxDataBuffer); + infra::ByteRange rxStatus(rxStatusBuffer); + + ExpectSendCommand(0x50, 0xaf); + handler.Special(subcommand, infra::MakeConstByteRange(txData), rxData, rxStatus, ondone); + + ExpectSendData({ 0x00, 0x54 }, 0x54); + ExpectReceiveData({ 0x79 }); + + ExpectSendData({0x00, 0x04}); + ExpectSendData({ 0x01, 0x02, 0x03, 0x04 }, 0x04); + ExpectReceiveData({ 0x79 }); + ExpectReceiveData({ 0x79 }); + + ExpectReceiveData({ 0x00, 0x04, 0x05, 0x06, 0x07, 0x08 }); + ExpectReceiveData({ 0x00, 0x00 }); + + EXPECT_EQ((std::array{ 0x05, 0x06, 0x07, 0x08 }), rxDataBuffer); + EXPECT_EQ((std::array{ }), rxStatusBuffer); +} + +TEST_F(StUartBootloaderCommandHandlerTest, Special_empty_rxData_rxStatus) +{ + Initialize(); + infra::VerifyingFunctionMock ondone; + uint16_t subcommand = 0x54; + std::array txData = { 0x01, 0x02, 0x03, 0x04 }; + std::array rxDataBuffer = {}; + std::array rxStatusBuffer = {}; + infra::ByteRange rxData(rxDataBuffer); + infra::ByteRange rxStatus(rxStatusBuffer); + + ExpectSendCommand(0x50, 0xaf); + handler.Special(subcommand, infra::MakeConstByteRange(txData), rxData, rxStatus, ondone); + + ExpectSendData({ 0x00, 0x54 }, 0x54); + ExpectReceiveData({ 0x79 }); + + ExpectSendData({0x00, 0x04}); + ExpectSendData({ 0x01, 0x02, 0x03, 0x04 }, 0x04); + ExpectReceiveData({ 0x79 }); + ExpectReceiveData({ 0x79 }); + + ExpectReceiveData({ 0x00, 0x00 }); + ExpectReceiveData({ 0x00, 0x00 }); + + EXPECT_EQ((std::array{ }), rxDataBuffer); + EXPECT_EQ((std::array{ }), rxStatusBuffer); +} + +TEST_F(StUartBootloaderCommandHandlerTest, Special_empty_txData) +{ + Initialize(); + infra::VerifyingFunctionMock ondone; + uint16_t subcommand = 0x54; + std::array rxDataBuffer; + std::array rxStatusBuffer; + infra::ByteRange rxData(rxDataBuffer); + infra::ByteRange rxStatus(rxStatusBuffer); + + ExpectSendCommand(0x50, 0xaf); + handler.Special(subcommand, infra::ConstByteRange{}, rxData, rxStatus, ondone); + + ExpectSendData({ 0x00, 0x54 }, 0x54); + ExpectReceiveData({ 0x79 }); + + ExpectSendData({0x00, 0x00}); + ExpectReceiveData({ 0x79 }); + ExpectReceiveData({ 0x79 }); + + ExpectReceiveData({ 0x00, 0x04, 0x05, 0x06, 0x07, 0x08 }); + ExpectReceiveData({ 0x00, 0x04, 0x09, 0x0a, 0x0b, 0x0c }); + + EXPECT_EQ((std::array{ 0x05, 0x06, 0x07, 0x08 }), rxDataBuffer); + EXPECT_EQ((std::array{ 0x09, 0x0a, 0x0b, 0x0c }), rxStatusBuffer); +} + +TEST_F(StUartBootloaderCommandHandlerTest, Special_timeout) +{ + Initialize(); + testing::StrictMock> ondone; + infra::ByteRange rxData; + infra::ByteRange rxStatus; + + ExpectSendCommand(0x50, 0xaf); + handler.Special(0x54, infra::ConstByteRange{}, rxData, rxStatus, [&ondone]() + { + ondone.callback(); + }); + ExpectErrorOnTimeout(std::chrono::seconds(1), "Timeout special command"); +} + +TEST_F(StUartBootloaderCommandHandlerTest, ExtendedSpecial) +{ + Initialize(); + infra::VerifyingFunctionMock ondone; + uint16_t subcommand = 0x54; + std::array txData1 = { 0x01, 0x02, 0x03, 0x04 }; + std::array txData2 = { 0x05, 0x06, 0x07, 0x08 }; + std::array rxDataBuffer; + infra::ByteRange rxData(rxDataBuffer); + + ExpectSendCommand(0x51, 0xae); + handler.ExtendedSpecial(subcommand, infra::MakeConstByteRange(txData1), infra::MakeConstByteRange(txData2), rxData, ondone); + + ExpectSendData({ 0x00, 0x54 }, 0x54); + ExpectReceiveData({ 0x79 }); + + ExpectSendData({0x00, 0x04}); + ExpectSendData({ 0x01, 0x02, 0x03, 0x04 }, 0x04); + ExpectReceiveData({ 0x79 }); + + ExpectSendData({0x00, 0x04}); + ExpectSendData({ 0x05, 0x06, 0x07, 0x08 }, 0x0c); + ExpectReceiveData({ 0x79 }); + ExpectReceiveData({ 0x79 }); + + ExpectReceiveData({ 0x00, 0x04, 0x09, 0x0a, 0x0b, 0x0c }); + + EXPECT_EQ((std::array{ 0x09, 0x0a, 0x0b, 0x0c }), rxDataBuffer); +} + +TEST_F(StUartBootloaderCommandHandlerTest, ExtendedSpecial_timeout) +{ + Initialize(); + testing::StrictMock> ondone; + infra::ByteRange rxData; + + ExpectSendCommand(0x51, 0xae); + handler.ExtendedSpecial(0x54, infra::ConstByteRange{}, infra::ConstByteRange{}, rxData, [&ondone]() + { + ondone.callback(); + }); + ExpectErrorOnTimeout(std::chrono::seconds(1), "Timeout extended special command"); +} From 20a387a8dab7e600c31966799777582a25b0de95 Mon Sep 17 00:00:00 2001 From: Rob Ekelmans Date: Sun, 22 Oct 2023 18:25:40 +0000 Subject: [PATCH 02/21] Rename ReceiveRegularBufferAction to ReceiveBufferAction --- .../st_util/StUartBootloaderCommandHandler.cpp | 10 +++++----- .../st_util/StUartBootloaderCommandHandler.hpp | 14 ++------------ 2 files changed, 7 insertions(+), 17 deletions(-) diff --git a/services/st_util/StUartBootloaderCommandHandler.cpp b/services/st_util/StUartBootloaderCommandHandler.cpp index 0e40cd7b..c257b55f 100644 --- a/services/st_util/StUartBootloaderCommandHandler.cpp +++ b/services/st_util/StUartBootloaderCommandHandler.cpp @@ -65,7 +65,7 @@ namespace services commandActions.emplace_back(infra::InPlaceType(), *this, getCommand); commandActions.emplace_back(infra::InPlaceType(), *this); - commandActions.emplace_back(infra::InPlaceType(), *this, commands); + commandActions.emplace_back(infra::InPlaceType(), *this, commands); commandActions.emplace_back(infra::InPlaceType(), *this); timeout.Start(commandTimeout, [this]() @@ -94,7 +94,7 @@ namespace services commandActions.emplace_back(infra::InPlaceType(), *this, getVersion); commandActions.emplace_back(infra::InPlaceType(), *this); - commandActions.emplace_back(infra::InPlaceType(), *this, internalRange, 3); + commandActions.emplace_back(infra::InPlaceType(), *this, internalRange, 3); commandActions.emplace_back(infra::InPlaceType(), *this); timeout.Start(commandTimeout, [this]() @@ -116,7 +116,7 @@ namespace services commandActions.emplace_back(infra::InPlaceType(), *this, getId); commandActions.emplace_back(infra::InPlaceType(), *this); - commandActions.emplace_back(infra::InPlaceType(), *this, internalRange); + commandActions.emplace_back(infra::InPlaceType(), *this, internalRange); commandActions.emplace_back(infra::InPlaceType(), *this); timeout.Start(commandTimeout, [this]() @@ -141,7 +141,7 @@ namespace services commandActions.emplace_back(infra::InPlaceType(), *this); commandActions.emplace_back(infra::InPlaceType(), *this, this->size); commandActions.emplace_back(infra::InPlaceType(), *this); - commandActions.emplace_back(infra::InPlaceType(), *this, data, size); + commandActions.emplace_back(infra::InPlaceType(), *this, data, size); timeout.Start(commandTimeout, [this]() { @@ -513,7 +513,7 @@ namespace services } } - void StUartBootloaderCommandHandler::ReceiveRegularBufferAction::ExtractNumberOfBytes(infra::ByteInputStream& stream) + void StUartBootloaderCommandHandler::ReceiveBufferAction::ExtractNumberOfBytes(infra::ByteInputStream& stream) { nBytes.Emplace(stream.Extract() + 1); handler.queue.Consume(sizeof(uint8_t)); diff --git a/services/st_util/StUartBootloaderCommandHandler.hpp b/services/st_util/StUartBootloaderCommandHandler.hpp index 17e879f7..01ee8bf9 100644 --- a/services/st_util/StUartBootloaderCommandHandler.hpp +++ b/services/st_util/StUartBootloaderCommandHandler.hpp @@ -107,7 +107,7 @@ namespace services void DataReceived() override; protected: - virtual void ExtractNumberOfBytes(infra::ByteInputStream& stream) {}; + virtual void ExtractNumberOfBytes(infra::ByteInputStream& stream); protected: infra::ByteRange& data; @@ -115,16 +115,6 @@ namespace services std::size_t nBytesReceived = 0; }; - class ReceiveRegularBufferAction - : public ReceiveBufferAction - { - public: - using ReceiveBufferAction::ReceiveBufferAction; - - protected: - void ExtractNumberOfBytes(infra::ByteInputStream& stream) override; - }; - class ReceiveSpecialBufferAction : public ReceiveBufferAction { @@ -191,7 +181,7 @@ namespace services std::array internalBuffer; infra::ByteRange internalRange; - std::deque> commandActions; + std::deque> commandActions; infra::AutoResetFunction) + sizeof(infra::ByteRange)> onCommandExecuted; infra::BigEndian address; From 7e4b5eeb598e5297512ff49646abe1a6d09f2cf8 Mon Sep 17 00:00:00 2001 From: Rob Ekelmans Date: Mon, 23 Oct 2023 06:27:25 +0000 Subject: [PATCH 03/21] StUartBootloaderCommandHandler some refactoring --- .../StUartBootloaderCommandHandler.cpp | 67 ++++++------- .../StUartBootloaderCommandHandler.hpp | 28 +++--- .../TestStUartBootloaderCommandHandler.cpp | 99 ++++++++++--------- 3 files changed, 90 insertions(+), 104 deletions(-) diff --git a/services/st_util/StUartBootloaderCommandHandler.cpp b/services/st_util/StUartBootloaderCommandHandler.cpp index c257b55f..313ae087 100644 --- a/services/st_util/StUartBootloaderCommandHandler.cpp +++ b/services/st_util/StUartBootloaderCommandHandler.cpp @@ -1,11 +1,4 @@ #include "services/st_util/StUartBootloaderCommandHandler.hpp" -#include "infra/util/ByteRange.hpp" -#include "infra/util/Endian.hpp" -#include "infra/util/Function.hpp" -#include "infra/util/MemoryRange.hpp" -#include "infra/util/Optional.hpp" -#include -#include #include namespace @@ -126,20 +119,20 @@ namespace services ExecuteCommand([this, onDone]() { - onDone((internalBuffer[0] << 8) | internalBuffer[1]); + uint16_t id = (internalBuffer[0] << 8) | internalBuffer[1]; + onDone(id); }); } void StUartBootloaderCommandHandler::ReadMemory(uint32_t address, uint8_t size, infra::ByteRange& data, const infra::Function& onDone) { this->address = address; - this->size = size - 1; commandActions.emplace_back(infra::InPlaceType(), *this, readMemory); commandActions.emplace_back(infra::InPlaceType(), *this); - commandActions.emplace_back(infra::InPlaceType(), *this, infra::MakeByteRange(this->address)); + commandActions.emplace_back(infra::InPlaceType(), *this, infra::MakeConstByteRange(this->address)); commandActions.emplace_back(infra::InPlaceType(), *this); - commandActions.emplace_back(infra::InPlaceType(), *this, this->size); + commandActions.emplace_back(infra::InPlaceType(), *this, size - 1); commandActions.emplace_back(infra::InPlaceType(), *this); commandActions.emplace_back(infra::InPlaceType(), *this, data, size); @@ -157,7 +150,7 @@ namespace services commandActions.emplace_back(infra::InPlaceType(), *this, go); commandActions.emplace_back(infra::InPlaceType(), *this); - commandActions.emplace_back(infra::InPlaceType(), *this, infra::MakeByteRange(this->address)); + commandActions.emplace_back(infra::InPlaceType(), *this, infra::MakeConstByteRange(this->address)); commandActions.emplace_back(infra::InPlaceType(), *this); timeout.Start(commandTimeout, [this]() @@ -174,14 +167,16 @@ namespace services std::abort(); this->address = address; + internalBuffer[0] = static_cast(data.size() + 1); std::copy(data.begin(), data.begin() + data.size(), internalBuffer.begin() + 1); + internalRange = infra::MakeRange(internalBuffer.begin(), internalBuffer.begin() + internalBuffer[0]); commandActions.emplace_back(infra::InPlaceType(), *this, writeMemory); commandActions.emplace_back(infra::InPlaceType(), *this); - commandActions.emplace_back(infra::InPlaceType(), *this, infra::MakeByteRange(this->address)); + commandActions.emplace_back(infra::InPlaceType(), *this, infra::MakeConstByteRange(this->address)); commandActions.emplace_back(infra::InPlaceType(), *this); - commandActions.emplace_back(infra::InPlaceType(), *this, infra::MakeRange(internalBuffer.begin(), internalBuffer.begin() + internalBuffer[0])); + commandActions.emplace_back(infra::InPlaceType(), *this, internalRange); commandActions.emplace_back(infra::InPlaceType(), *this); timeout.Start(commandTimeout, [this]() @@ -214,10 +209,11 @@ namespace services internalBuffer[0] = static_cast(pages.size() + 1); std::copy(pages.begin(), pages.begin() + pages.size(), internalBuffer.begin() + 1); + internalRange = infra::MakeRange(internalBuffer.begin(), internalBuffer.begin() + internalBuffer[0]); commandActions.emplace_back(infra::InPlaceType(), *this, erase); commandActions.emplace_back(infra::InPlaceType(), *this); - commandActions.emplace_back(infra::InPlaceType(), *this, infra::MakeRange(internalBuffer.begin(), internalBuffer.begin() + pages.size() + 1)); + commandActions.emplace_back(infra::InPlaceType(), *this, internalRange); commandActions.emplace_back(infra::InPlaceType(), *this); timeout.Start(commandTimeout, [this]() @@ -234,7 +230,7 @@ namespace services commandActions.emplace_back(infra::InPlaceType(), *this, extendedErase); commandActions.emplace_back(infra::InPlaceType(), *this); - commandActions.emplace_back(infra::InPlaceType(), *this, infra::MakeByteRange(this->subcommand)); + commandActions.emplace_back(infra::InPlaceType(), *this, infra::MakeConstByteRange(this->subcommand)); commandActions.emplace_back(infra::InPlaceType(), *this); timeout.Start(commandTimeout, [this]() @@ -253,10 +249,11 @@ namespace services internalBuffer[0] = static_cast(nPages >> 8); internalBuffer[1] = static_cast(nPages & 0xff) + 1; std::copy(pages.begin(), pages.begin() + pages.size(), internalBuffer.begin() + 2); + internalRange = infra::MakeRange(internalBuffer.begin(), internalBuffer.begin() + pages.size() + 2); commandActions.emplace_back(infra::InPlaceType(), *this, extendedErase); commandActions.emplace_back(infra::InPlaceType(), *this); - commandActions.emplace_back(infra::InPlaceType(), *this, infra::MakeRange(internalBuffer.begin(), internalBuffer.begin() + pages.size() + 2)); + commandActions.emplace_back(infra::InPlaceType(), *this, internalRange); commandActions.emplace_back(infra::InPlaceType(), *this); timeout.Start(commandTimeout, [this]() @@ -402,28 +399,20 @@ namespace services void StUartBootloaderCommandHandler::SendData(infra::ConstByteRange data, uint8_t checksum) { - claimer.Claim([this, data, checksum]() + serial.SendData(data, [this, checksum]() { - serial.SendData(data, [this, checksum]() + serial.SendData(infra::MakeConstByteRange(checksum), [this]() { - serial.SendData(infra::MakeByteRange(checksum), [this]() - { - claimer.Release(); - TryHandleTransmitAction(); - }); + TryHandleTransmitAction(); }); }); } void StUartBootloaderCommandHandler::SendData(infra::ConstByteRange data) { - claimer.Claim([this, data]() + serial.SendData(data, [this]() { - serial.SendData(data, [this]() - { - claimer.Release(); - TryHandleTransmitAction(); - }); + TryHandleTransmitAction(); }); } @@ -484,11 +473,11 @@ namespace services , data(data) {} - StUartBootloaderCommandHandler::ReceiveBufferAction::ReceiveBufferAction(StUartBootloaderCommandHandler& handler, infra::ByteRange& data, const std::size_t nBytes) + StUartBootloaderCommandHandler::ReceiveBufferAction::ReceiveBufferAction(StUartBootloaderCommandHandler& handler, infra::ByteRange& data, const std::size_t nBytesTotal) : StUartBootloaderCommandHandler::ReceiveAction(handler) , data(data) { - this->nBytes.Emplace(nBytes); + this->nBytesTotal.Emplace(nBytesTotal); } void StUartBootloaderCommandHandler::ReceiveBufferAction::DataReceived() @@ -498,15 +487,15 @@ namespace services infra::ByteInputStream stream(handler.queue.ContiguousRange()); - if (!nBytes) + if (!nBytesTotal) ExtractNumberOfBytes(stream); - auto buffer = infra::Head(infra::DiscardHead(data, nBytesReceived), std::min(stream.Available(), *nBytes - nBytesReceived)); + auto buffer = infra::Head(infra::DiscardHead(data, nBytesReceived), std::min(stream.Available(), *nBytesTotal - nBytesReceived)); stream >> buffer; handler.queue.Consume(buffer.size()); nBytesReceived += buffer.size(); - if (nBytesReceived == nBytes) + if (nBytesReceived == nBytesTotal) { data.shrink_from_back_to(nBytesReceived); handler.TryHandleNextAction(); @@ -515,14 +504,14 @@ namespace services void StUartBootloaderCommandHandler::ReceiveBufferAction::ExtractNumberOfBytes(infra::ByteInputStream& stream) { - nBytes.Emplace(stream.Extract() + 1); - handler.queue.Consume(sizeof(uint8_t)); + nBytesTotal.Emplace(stream.Extract() + 1); + handler.queue.Consume(sizeof(uint8_t)); } void StUartBootloaderCommandHandler::ReceiveSpecialBufferAction::ExtractNumberOfBytes(infra::ByteInputStream& stream) { - nBytes.Emplace(stream.Extract>()); - handler.queue.Consume(sizeof(uint16_t)); + nBytesTotal.Emplace(stream.Extract>()); + handler.queue.Consume(sizeof(uint16_t)); } StUartBootloaderCommandHandler::TransmitRawAction::TransmitRawAction(StUartBootloaderCommandHandler& handler, infra::ConstByteRange data) diff --git a/services/st_util/StUartBootloaderCommandHandler.hpp b/services/st_util/StUartBootloaderCommandHandler.hpp index 01ee8bf9..c6753e54 100644 --- a/services/st_util/StUartBootloaderCommandHandler.hpp +++ b/services/st_util/StUartBootloaderCommandHandler.hpp @@ -2,19 +2,18 @@ #define SERVICES_ST_UTIL_ST_UART_BOOTLOADER_COMMAND_HANDLER_HPP #include "hal/interfaces/SerialCommunication.hpp" -#include "infra/event/ClaimableResource.hpp" #include "infra/event/QueueForOneReaderOneIrqWriter.hpp" +#include "infra/stream/ByteInputStream.hpp" #include "infra/timer/Timer.hpp" +#include "infra/util/AutoResetFunction.hpp" +#include "infra/util/BoundedDeque.hpp" #include "infra/util/BoundedString.hpp" -#include "infra/util/ByteRange.hpp" -#include "infra/stream/ByteInputStream.hpp" +#include "infra/util/Endian.hpp" #include "infra/util/Optional.hpp" -#include "services/st_util/StBootloaderCommandHandler.hpp" #include "infra/util/PolymorphicVariant.hpp" +#include "services/st_util/StBootloaderCommandHandler.hpp" #include #include -#include "infra/util/Endian.hpp" -#include namespace services { @@ -47,8 +46,8 @@ namespace services private: void InitializeUartBootloader(); void ExecuteCommand(const infra::Function) + sizeof(infra::ByteRange)>& onCommandExecuted); - void TryHandleTransmitAction(); void TryHandleNextAction(); + void TryHandleTransmitAction(); void SendData(infra::ConstByteRange data, uint8_t checksum); void SendData(infra::ConstByteRange data); void DataReceived(); @@ -102,7 +101,7 @@ namespace services { public: ReceiveBufferAction(StUartBootloaderCommandHandler& handler, infra::ByteRange& data); - ReceiveBufferAction(StUartBootloaderCommandHandler& handler, infra::ByteRange& data, const std::size_t nBytes); + ReceiveBufferAction(StUartBootloaderCommandHandler& handler, infra::ByteRange& data, const std::size_t nBytesTotal); void DataReceived() override; @@ -111,7 +110,7 @@ namespace services protected: infra::ByteRange& data; - infra::Optional nBytes; + infra::Optional nBytesTotal; std::size_t nBytesReceived = 0; }; @@ -173,20 +172,15 @@ namespace services infra::AutoResetFunction onInitialized; infra::AutoResetFunction onError; infra::QueueForOneReaderOneIrqWriter::WithStorage<257> queue; - infra::ClaimableResource resource; - infra::ClaimableResource::Claimer startClaimer{ resource }; - infra::ClaimableResource::Claimer::WithSize<2 * sizeof(StUartBootloaderCommandHandler*) + 3 * sizeof(infra::ConstByteRange)> claimer{ resource }; + infra::BoundedDeque>::WithMaxSize<12> commandActions; + infra::AutoResetFunction) + sizeof(infra::ByteRange)> onCommandExecuted; infra::TimerSingleShot timeout; + std::array internalBuffer; infra::ByteRange internalRange; - - std::deque> commandActions; - infra::AutoResetFunction) + sizeof(infra::ByteRange)> onCommandExecuted; - infra::BigEndian address; infra::BigEndian subcommand; - uint8_t size; }; } diff --git a/services/st_util/test/TestStUartBootloaderCommandHandler.cpp b/services/st_util/test/TestStUartBootloaderCommandHandler.cpp index 7b8bc4bb..01dfefca 100644 --- a/services/st_util/test/TestStUartBootloaderCommandHandler.cpp +++ b/services/st_util/test/TestStUartBootloaderCommandHandler.cpp @@ -1,17 +1,9 @@ #include "hal/interfaces/test_doubles/SerialCommunicationMock.hpp" -#include "infra/timer/Timer.hpp" #include "infra/timer/test_helper/ClockFixture.hpp" -#include "infra/util/BoundedString.hpp" -#include "infra/util/ByteRange.hpp" -#include "infra/util/Function.hpp" #include "infra/util/test_helper/MockCallback.hpp" #include "services/st_util/StUartBootloaderCommandHandler.hpp" #include "gmock/gmock.h" #include "gtest/gtest.h" -#include -#include -#include -#include class StUartBootloaderCommandHandlerTest : public testing::Test @@ -20,7 +12,6 @@ class StUartBootloaderCommandHandlerTest public: void Initialize() { - ForwardTime(std::chrono::milliseconds(20)); serialCommunication.dataReceived(std::vector{ 0x79 }); EXPECT_CALL(onInitializedMock, callback()); @@ -44,9 +35,9 @@ class StUartBootloaderCommandHandlerTest }); } - void ExpectSendCommand(uint8_t command, uint8_t checksum) + void ExpectSendData(uint8_t data, uint8_t checksum) { - ExpectSendData({ command }, [this, checksum]() + ExpectSendData({ data }, [this, checksum]() { ExpectSendData({ checksum }); }); @@ -68,10 +59,6 @@ class StUartBootloaderCommandHandlerTest testing::StrictMock serialCommunication; testing::StrictMock> onInitializedMock; testing::StrictMock> onErrorMock; - infra::Execute execute{ [this] - { - ExpectSendData({ 0x7f }); - } }; const infra::Function onInitialized{ [this]() { onInitializedMock.callback(); @@ -80,6 +67,10 @@ class StUartBootloaderCommandHandlerTest { onErrorMock.callback(reason); } }; + infra::Execute execute{ [this] + { + ExpectSendData({ 0x7f }); + } }; services::StUartBootloaderCommandHandler handler{ serialCommunication, onInitialized, onError }; }; @@ -95,9 +86,21 @@ TEST_F(StUartBootloaderCommandHandlerTest, creation_initializes_uart_bootloader_ ForwardTime(std::chrono::milliseconds(1000)); } -// TEST_F(StUartBootloaderCommandHandlerTest, receive_nack) -// TEST_F(StUartBootloaderCommandHandlerTest, receive_failing_stream) -// TEST_F(StUartBootloaderCommandHandlerTest, receive_invalid_data) +TEST_F(StUartBootloaderCommandHandlerTest, receive_nack) +{ + Initialize(); + testing::StrictMock> ondone; + std::array commandsbuffer = {}; + infra::ByteRange commands(commandsbuffer); + + ExpectSendData(0x00, 0xff); + handler.GetCommand(commands, [&ondone](uint8_t, uint8_t) + { + ondone.callback(); + }); + EXPECT_CALL(onErrorMock, callback(infra::BoundedConstString{ "NACK received" })); + ExpectReceiveData({ 0x1f }); +} TEST_F(StUartBootloaderCommandHandlerTest, GetCommand) { @@ -106,7 +109,7 @@ TEST_F(StUartBootloaderCommandHandlerTest, GetCommand) std::array commandsbuffer = {}; infra::ByteRange commands(commandsbuffer); - ExpectSendCommand(0x00, 0xff); + ExpectSendData(0x00, 0xff); handler.GetCommand(commands, ondone); ExpectReceiveData({ 0x79, 0x0b, 0x32, 0x00, 0x01, 0x02, 0x11, 0x21, 0x31, 0x44, 0x63, 0x73, 0x82, 0x92, 0x79 }); @@ -114,14 +117,14 @@ TEST_F(StUartBootloaderCommandHandlerTest, GetCommand) EXPECT_EQ((std::array{ 0x00, 0x01, 0x02, 0x11, 0x21, 0x31, 0x44, 0x63, 0x73, 0x82, 0x92 }), commandsbuffer); } -TEST_F(StUartBootloaderCommandHandlerTest, GetCommand_receive_intermittend) +TEST_F(StUartBootloaderCommandHandlerTest, GetCommand_receive_in_parts) { Initialize(); infra::VerifyingFunctionMock ondone(3, 2); std::array commandsbuffer = {}; infra::ByteRange commands(commandsbuffer); - ExpectSendCommand(0x00, 0xff); + ExpectSendData(0x00, 0xff); handler.GetCommand(commands, ondone); ExpectReceiveData({ 0x79, 0x0b, 0x32, 0x00, 0x01 }); ExpectReceiveData({ 0x02, 0x11, 0x21, 0x31, 0x44, 0x63, 0x73, 0x82, 0x92, 0x79 }); @@ -136,7 +139,7 @@ TEST_F(StUartBootloaderCommandHandlerTest, GetCommand_timeout) std::array commandsbuffer = {}; infra::ByteRange commands(commandsbuffer); - ExpectSendCommand(0x00, 0xff); + ExpectSendData(0x00, 0xff); handler.GetCommand(commands, [&ondone](uint8_t, uint8_t) { ondone.callback(); @@ -149,7 +152,7 @@ TEST_F(StUartBootloaderCommandHandlerTest, GetVersion) Initialize(); infra::VerifyingFunctionMock ondone(3, 1); - ExpectSendCommand(0x01, 0xfe); + ExpectSendData(0x01, 0xfe); handler.GetVersion(ondone); ExpectReceiveData({ 0x79, 0x31, 0x00, 0x00, 0x79 }); } @@ -159,7 +162,7 @@ TEST_F(StUartBootloaderCommandHandlerTest, GetVersion_timeout) Initialize(); testing::StrictMock> ondone; - ExpectSendCommand(0x01, 0xfe); + ExpectSendData(0x01, 0xfe); handler.GetVersion([&ondone](uint8_t, uint8_t) { ondone.callback(); @@ -172,7 +175,7 @@ TEST_F(StUartBootloaderCommandHandlerTest, GetId) Initialize(); infra::VerifyingFunctionMock ondone(0x0495); - ExpectSendCommand(0x02, 0xfd); + ExpectSendData(0x02, 0xfd); handler.GetId(ondone); ExpectReceiveData({ 0x79, 0x01, 0x04, 0x95, 0x79 }); } @@ -182,7 +185,7 @@ TEST_F(StUartBootloaderCommandHandlerTest, GetId_timeout) Initialize(); testing::StrictMock> ondone; - ExpectSendCommand(0x02, 0xfd); + ExpectSendData(0x02, 0xfd); handler.GetId([&ondone](uint16_t) { ondone.callback(); @@ -198,13 +201,13 @@ TEST_F(StUartBootloaderCommandHandlerTest, ReadMemory) std::array dataBuffer = {}; infra::ByteRange data(dataBuffer); - ExpectSendCommand(0x11, 0xEE); + ExpectSendData(0x11, 0xEE); handler.ReadMemory(address, 3, data, ondone); ExpectSendData({ 0x01, 0x02, 0x03, 0x04 }, 0x04); ExpectReceiveData({ 0x79 }); - ExpectSendCommand(0x02, 0xFD); + ExpectSendData(0x02, 0xFD); ExpectReceiveData({ 0x79 }); ExpectReceiveData({ 0x79 }); @@ -220,7 +223,7 @@ TEST_F(StUartBootloaderCommandHandlerTest, ReadMemory_timeout) std::array dataBuffer = {}; infra::ByteRange data(dataBuffer); - ExpectSendCommand(0x11, 0xEE); + ExpectSendData(0x11, 0xEE); handler.ReadMemory(0x01020304, 3, data, [&ondone]() { ondone.callback(); @@ -234,7 +237,7 @@ TEST_F(StUartBootloaderCommandHandlerTest, Go) infra::VerifyingFunctionMock ondone; uint32_t address = 0x01020304; - ExpectSendCommand(0x21, 0xde); + ExpectSendData(0x21, 0xde); handler.Go(address, ondone); ExpectSendData({ 0x01, 0x02, 0x03, 0x04 }, 0x04); @@ -249,7 +252,7 @@ TEST_F(StUartBootloaderCommandHandlerTest, Go_timeout) testing::StrictMock> ondone; uint32_t address = 0x01020304; - ExpectSendCommand(0x21, 0xde); + ExpectSendData(0x21, 0xde); handler.Go(address, [&ondone]() { ondone.callback(); @@ -264,7 +267,7 @@ TEST_F(StUartBootloaderCommandHandlerTest, WriteMemory) std::array data = { 0x05, 0x06, 0x07, 0x08 }; uint32_t address = 0x01020304; - ExpectSendCommand(0x31, 0xce); + ExpectSendData(0x31, 0xce); handler.WriteMemory(address, infra::MakeByteRange(data), ondone); ExpectSendData({ 0x01, 0x02, 0x03, 0x04 }, 0x04); @@ -282,7 +285,7 @@ TEST_F(StUartBootloaderCommandHandlerTest, WriteMemory_timeout) std::array data; uint32_t address = 0x01020304; - ExpectSendCommand(0x31, 0xce); + ExpectSendData(0x31, 0xce); handler.WriteMemory(address, infra::MakeByteRange(data), [&ondone]() { ondone.callback(); @@ -295,9 +298,9 @@ TEST_F(StUartBootloaderCommandHandlerTest, Erase_global) Initialize(); infra::VerifyingFunctionMock ondone; - ExpectSendCommand(0x43, 0xbc); + ExpectSendData(0x43, 0xbc); handler.Erase(0xff, ondone); - ExpectSendCommand(0xff, 0x00); + ExpectSendData(0xff, 0x00); ExpectReceiveData({ 0x79 }); ExpectReceiveData({ 0x79 }); } @@ -308,7 +311,7 @@ TEST_F(StUartBootloaderCommandHandlerTest, Erase_pages) infra::VerifyingFunctionMock ondone; std::array pages = { 0x01, 0x02, 0x03, 0x04 }; - ExpectSendCommand(0x43, 0xbc); + ExpectSendData(0x43, 0xbc); handler.Erase(4, infra::MakeByteRange(pages), ondone); ExpectSendData({ 0x05, 0x01, 0x02, 0x03, 0x04 }, 0x01); ExpectReceiveData({ 0x79 }); @@ -321,7 +324,7 @@ TEST_F(StUartBootloaderCommandHandlerTest, Erase_timeout) testing::StrictMock> ondone; std::array data; - ExpectSendCommand(0x43, 0xbc); + ExpectSendData(0x43, 0xbc); handler.Erase(0xff, infra::ByteRange{}, [&ondone]() { ondone.callback(); @@ -334,7 +337,7 @@ TEST_F(StUartBootloaderCommandHandlerTest, ExtendedErase_global) Initialize(); infra::VerifyingFunctionMock ondone; - ExpectSendCommand(0x44, 0xbb); + ExpectSendData(0x44, 0xbb); handler.ExtendedErase(0xfffe, ondone); ExpectSendData({ 0xff, 0xfe }, 0x01); ExpectReceiveData({ 0x79 }); @@ -347,7 +350,7 @@ TEST_F(StUartBootloaderCommandHandlerTest, ExtendedErase_pages) infra::VerifyingFunctionMock ondone; std::array, 4> pages = { 0x0001, 0x0002, 0x0003, 0x0004 }; - ExpectSendCommand(0x44, 0xbb); + ExpectSendData(0x44, 0xbb); handler.ExtendedErase(4, infra::MakeByteRange(pages), ondone); ExpectSendData({ 0x00, 0x05, 0x00, 0x01, 0x00, 0x02, 0x00, 0x03, 0x00, 0x04 }, 0x01); ExpectReceiveData({ 0x79 }); @@ -359,7 +362,7 @@ TEST_F(StUartBootloaderCommandHandlerTest, ExtendedErase_timeout) Initialize(); testing::StrictMock> ondone; - ExpectSendCommand(0x44, 0xbb); + ExpectSendData(0x44, 0xbb); handler.ExtendedErase(0xffff, [&ondone]() { ondone.callback(); @@ -378,7 +381,7 @@ TEST_F(StUartBootloaderCommandHandlerTest, Special) infra::ByteRange rxData(rxDataBuffer); infra::ByteRange rxStatus(rxStatusBuffer); - ExpectSendCommand(0x50, 0xaf); + ExpectSendData(0x50, 0xaf); handler.Special(subcommand, infra::MakeConstByteRange(txData), rxData, rxStatus, ondone); ExpectSendData({ 0x00, 0x54 }, 0x54); @@ -407,7 +410,7 @@ TEST_F(StUartBootloaderCommandHandlerTest, Special_empty_rxData) infra::ByteRange rxData(rxDataBuffer); infra::ByteRange rxStatus(rxStatusBuffer); - ExpectSendCommand(0x50, 0xaf); + ExpectSendData(0x50, 0xaf); handler.Special(subcommand, infra::MakeConstByteRange(txData), rxData, rxStatus, ondone); ExpectSendData({ 0x00, 0x54 }, 0x54); @@ -436,7 +439,7 @@ TEST_F(StUartBootloaderCommandHandlerTest, Special_empty_rxStatus) infra::ByteRange rxData(rxDataBuffer); infra::ByteRange rxStatus(rxStatusBuffer); - ExpectSendCommand(0x50, 0xaf); + ExpectSendData(0x50, 0xaf); handler.Special(subcommand, infra::MakeConstByteRange(txData), rxData, rxStatus, ondone); ExpectSendData({ 0x00, 0x54 }, 0x54); @@ -465,7 +468,7 @@ TEST_F(StUartBootloaderCommandHandlerTest, Special_empty_rxData_rxStatus) infra::ByteRange rxData(rxDataBuffer); infra::ByteRange rxStatus(rxStatusBuffer); - ExpectSendCommand(0x50, 0xaf); + ExpectSendData(0x50, 0xaf); handler.Special(subcommand, infra::MakeConstByteRange(txData), rxData, rxStatus, ondone); ExpectSendData({ 0x00, 0x54 }, 0x54); @@ -493,7 +496,7 @@ TEST_F(StUartBootloaderCommandHandlerTest, Special_empty_txData) infra::ByteRange rxData(rxDataBuffer); infra::ByteRange rxStatus(rxStatusBuffer); - ExpectSendCommand(0x50, 0xaf); + ExpectSendData(0x50, 0xaf); handler.Special(subcommand, infra::ConstByteRange{}, rxData, rxStatus, ondone); ExpectSendData({ 0x00, 0x54 }, 0x54); @@ -517,7 +520,7 @@ TEST_F(StUartBootloaderCommandHandlerTest, Special_timeout) infra::ByteRange rxData; infra::ByteRange rxStatus; - ExpectSendCommand(0x50, 0xaf); + ExpectSendData(0x50, 0xaf); handler.Special(0x54, infra::ConstByteRange{}, rxData, rxStatus, [&ondone]() { ondone.callback(); @@ -535,7 +538,7 @@ TEST_F(StUartBootloaderCommandHandlerTest, ExtendedSpecial) std::array rxDataBuffer; infra::ByteRange rxData(rxDataBuffer); - ExpectSendCommand(0x51, 0xae); + ExpectSendData(0x51, 0xae); handler.ExtendedSpecial(subcommand, infra::MakeConstByteRange(txData1), infra::MakeConstByteRange(txData2), rxData, ondone); ExpectSendData({ 0x00, 0x54 }, 0x54); @@ -561,7 +564,7 @@ TEST_F(StUartBootloaderCommandHandlerTest, ExtendedSpecial_timeout) testing::StrictMock> ondone; infra::ByteRange rxData; - ExpectSendCommand(0x51, 0xae); + ExpectSendData(0x51, 0xae); handler.ExtendedSpecial(0x54, infra::ConstByteRange{}, infra::ConstByteRange{}, rxData, [&ondone]() { ondone.callback(); From 50fb73e21b3ffbd72edc03f4a036f8367ab639b4 Mon Sep 17 00:00:00 2001 From: EkelmansPh <58972933+EkelmansPh@users.noreply.github.com> Date: Mon, 23 Oct 2023 08:29:36 +0200 Subject: [PATCH 04/21] Update services/st_util/CMakeLists.txt --- services/st_util/CMakeLists.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/services/st_util/CMakeLists.txt b/services/st_util/CMakeLists.txt index f03d8d73..6fb29d7c 100644 --- a/services/st_util/CMakeLists.txt +++ b/services/st_util/CMakeLists.txt @@ -22,4 +22,4 @@ target_link_libraries(services.st_util PUBLIC if (EMIL_BUILD_TESTS) add_subdirectory(test) -endif() \ No newline at end of file +endif() From 4a30ce7f1c9f34985cd383e8eb8a9e79173a5006 Mon Sep 17 00:00:00 2001 From: EkelmansPh <58972933+EkelmansPh@users.noreply.github.com> Date: Mon, 23 Oct 2023 08:30:40 +0200 Subject: [PATCH 05/21] Update services/st_util/StUartBootloaderCommandHandler.cpp --- services/st_util/StUartBootloaderCommandHandler.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/services/st_util/StUartBootloaderCommandHandler.cpp b/services/st_util/StUartBootloaderCommandHandler.cpp index 313ae087..854d504c 100644 --- a/services/st_util/StUartBootloaderCommandHandler.cpp +++ b/services/st_util/StUartBootloaderCommandHandler.cpp @@ -549,4 +549,4 @@ namespace services handler.SendData(data, checksum); } -} \ No newline at end of file +} From 8feaa2fcd708dfc50f8d3a0e7e0548d9e91346da Mon Sep 17 00:00:00 2001 From: EkelmansPh <58972933+EkelmansPh@users.noreply.github.com> Date: Mon, 23 Oct 2023 08:33:58 +0200 Subject: [PATCH 06/21] Apply suggestions from code review Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> --- .../TestStUartBootloaderCommandHandler.cpp | 22 +++++++++---------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/services/st_util/test/TestStUartBootloaderCommandHandler.cpp b/services/st_util/test/TestStUartBootloaderCommandHandler.cpp index 01dfefca..51266273 100644 --- a/services/st_util/test/TestStUartBootloaderCommandHandler.cpp +++ b/services/st_util/test/TestStUartBootloaderCommandHandler.cpp @@ -387,7 +387,7 @@ TEST_F(StUartBootloaderCommandHandlerTest, Special) ExpectSendData({ 0x00, 0x54 }, 0x54); ExpectReceiveData({ 0x79 }); - ExpectSendData({0x00, 0x04}); + ExpectSendData({ 0x00, 0x04 }); ExpectSendData({ 0x01, 0x02, 0x03, 0x04 }, 0x04); ExpectReceiveData({ 0x79 }); ExpectReceiveData({ 0x79 }); @@ -416,7 +416,7 @@ TEST_F(StUartBootloaderCommandHandlerTest, Special_empty_rxData) ExpectSendData({ 0x00, 0x54 }, 0x54); ExpectReceiveData({ 0x79 }); - ExpectSendData({0x00, 0x04}); + ExpectSendData({ 0x00, 0x04 }); ExpectSendData({ 0x01, 0x02, 0x03, 0x04 }, 0x04); ExpectReceiveData({ 0x79 }); ExpectReceiveData({ 0x79 }); @@ -424,7 +424,7 @@ TEST_F(StUartBootloaderCommandHandlerTest, Special_empty_rxData) ExpectReceiveData({ 0x00, 0x00 }); ExpectReceiveData({ 0x00, 0x04, 0x09, 0x0a, 0x0b, 0x0c }); - EXPECT_EQ((std::array{ }), rxDataBuffer); + EXPECT_EQ((std::array{}), rxDataBuffer); EXPECT_EQ((std::array{ 0x09, 0x0a, 0x0b, 0x0c }), rxStatusBuffer); } @@ -445,7 +445,7 @@ TEST_F(StUartBootloaderCommandHandlerTest, Special_empty_rxStatus) ExpectSendData({ 0x00, 0x54 }, 0x54); ExpectReceiveData({ 0x79 }); - ExpectSendData({0x00, 0x04}); + ExpectSendData({ 0x00, 0x04 }); ExpectSendData({ 0x01, 0x02, 0x03, 0x04 }, 0x04); ExpectReceiveData({ 0x79 }); ExpectReceiveData({ 0x79 }); @@ -454,7 +454,7 @@ TEST_F(StUartBootloaderCommandHandlerTest, Special_empty_rxStatus) ExpectReceiveData({ 0x00, 0x00 }); EXPECT_EQ((std::array{ 0x05, 0x06, 0x07, 0x08 }), rxDataBuffer); - EXPECT_EQ((std::array{ }), rxStatusBuffer); + EXPECT_EQ((std::array{}), rxStatusBuffer); } TEST_F(StUartBootloaderCommandHandlerTest, Special_empty_rxData_rxStatus) @@ -474,7 +474,7 @@ TEST_F(StUartBootloaderCommandHandlerTest, Special_empty_rxData_rxStatus) ExpectSendData({ 0x00, 0x54 }, 0x54); ExpectReceiveData({ 0x79 }); - ExpectSendData({0x00, 0x04}); + ExpectSendData({ 0x00, 0x04 }); ExpectSendData({ 0x01, 0x02, 0x03, 0x04 }, 0x04); ExpectReceiveData({ 0x79 }); ExpectReceiveData({ 0x79 }); @@ -482,8 +482,8 @@ TEST_F(StUartBootloaderCommandHandlerTest, Special_empty_rxData_rxStatus) ExpectReceiveData({ 0x00, 0x00 }); ExpectReceiveData({ 0x00, 0x00 }); - EXPECT_EQ((std::array{ }), rxDataBuffer); - EXPECT_EQ((std::array{ }), rxStatusBuffer); + EXPECT_EQ((std::array{}), rxDataBuffer); + EXPECT_EQ((std::array{}), rxStatusBuffer); } TEST_F(StUartBootloaderCommandHandlerTest, Special_empty_txData) @@ -502,7 +502,7 @@ TEST_F(StUartBootloaderCommandHandlerTest, Special_empty_txData) ExpectSendData({ 0x00, 0x54 }, 0x54); ExpectReceiveData({ 0x79 }); - ExpectSendData({0x00, 0x00}); + ExpectSendData({ 0x00, 0x00 }); ExpectReceiveData({ 0x79 }); ExpectReceiveData({ 0x79 }); @@ -544,11 +544,11 @@ TEST_F(StUartBootloaderCommandHandlerTest, ExtendedSpecial) ExpectSendData({ 0x00, 0x54 }, 0x54); ExpectReceiveData({ 0x79 }); - ExpectSendData({0x00, 0x04}); + ExpectSendData({ 0x00, 0x04 }); ExpectSendData({ 0x01, 0x02, 0x03, 0x04 }, 0x04); ExpectReceiveData({ 0x79 }); - ExpectSendData({0x00, 0x04}); + ExpectSendData({ 0x00, 0x04 }); ExpectSendData({ 0x05, 0x06, 0x07, 0x08 }, 0x0c); ExpectReceiveData({ 0x79 }); ExpectReceiveData({ 0x79 }); From e3a02955eb14844101f671d827ea5df6d4e1c006 Mon Sep 17 00:00:00 2001 From: Rob Ekelmans Date: Thu, 26 Oct 2023 09:13:25 +0000 Subject: [PATCH 07/21] Resolve PR comments. --- .../StUartBootloaderCommandHandler.cpp | 110 ++++++------------ .../StUartBootloaderCommandHandler.hpp | 18 ++- 2 files changed, 51 insertions(+), 77 deletions(-) diff --git a/services/st_util/StUartBootloaderCommandHandler.cpp b/services/st_util/StUartBootloaderCommandHandler.cpp index 854d504c..917eeb35 100644 --- a/services/st_util/StUartBootloaderCommandHandler.cpp +++ b/services/st_util/StUartBootloaderCommandHandler.cpp @@ -58,14 +58,10 @@ namespace services commandActions.emplace_back(infra::InPlaceType(), *this, getCommand); commandActions.emplace_back(infra::InPlaceType(), *this); - commandActions.emplace_back(infra::InPlaceType(), *this, commands); + commandActions.emplace_back(infra::InPlaceType(), *this, commands); commandActions.emplace_back(infra::InPlaceType(), *this); - timeout.Start(commandTimeout, [this]() - { - OnError("Timeout getting commands"); - }); - + SetCommandTimeout("Timeout getting commands"); ExecuteCommand([this, onDone, &commands]() { uint8_t major = commands.front() >> 4; @@ -87,14 +83,10 @@ namespace services commandActions.emplace_back(infra::InPlaceType(), *this, getVersion); commandActions.emplace_back(infra::InPlaceType(), *this); - commandActions.emplace_back(infra::InPlaceType(), *this, internalRange, 3); + commandActions.emplace_back(infra::InPlaceType(), *this, internalRange, 3); commandActions.emplace_back(infra::InPlaceType(), *this); - timeout.Start(commandTimeout, [this]() - { - OnError("Timeout getting version"); - }); - + SetCommandTimeout("Timeout getting version"); ExecuteCommand([this, onDone]() { uint8_t major = internalBuffer[0] >> 4; @@ -109,14 +101,10 @@ namespace services commandActions.emplace_back(infra::InPlaceType(), *this, getId); commandActions.emplace_back(infra::InPlaceType(), *this); - commandActions.emplace_back(infra::InPlaceType(), *this, internalRange); + commandActions.emplace_back(infra::InPlaceType(), *this, internalRange); commandActions.emplace_back(infra::InPlaceType(), *this); - timeout.Start(commandTimeout, [this]() - { - OnError("Timeout getting id"); - }); - + SetCommandTimeout("Timeout getting id"); ExecuteCommand([this, onDone]() { uint16_t id = (internalBuffer[0] << 8) | internalBuffer[1]; @@ -134,13 +122,9 @@ namespace services commandActions.emplace_back(infra::InPlaceType(), *this); commandActions.emplace_back(infra::InPlaceType(), *this, size - 1); commandActions.emplace_back(infra::InPlaceType(), *this); - commandActions.emplace_back(infra::InPlaceType(), *this, data, size); - - timeout.Start(commandTimeout, [this]() - { - OnError("Timeout reading memory"); - }); + commandActions.emplace_back(infra::InPlaceType(), *this, data, size); + SetCommandTimeout("Timeout reading memory"); ExecuteCommand(onDone); } @@ -153,11 +137,7 @@ namespace services commandActions.emplace_back(infra::InPlaceType(), *this, infra::MakeConstByteRange(this->address)); commandActions.emplace_back(infra::InPlaceType(), *this); - timeout.Start(commandTimeout, [this]() - { - OnError("Timeout go"); - }); - + SetCommandTimeout("Timeout go"); ExecuteCommand(onDone); } @@ -179,11 +159,7 @@ namespace services commandActions.emplace_back(infra::InPlaceType(), *this, internalRange); commandActions.emplace_back(infra::InPlaceType(), *this); - timeout.Start(commandTimeout, [this]() - { - OnError("Timeout writing memory"); - }); - + SetCommandTimeout("Timeout writing memory"); ExecuteCommand(onDone); } @@ -194,11 +170,7 @@ namespace services commandActions.emplace_back(infra::InPlaceType(), *this, subcommand); commandActions.emplace_back(infra::InPlaceType(), *this); - timeout.Start(commandTimeout, [this]() - { - OnError("Timeout erasing memory"); - }); - + SetCommandTimeout("Timeout erasing memory"); ExecuteCommand(onDone); } @@ -216,14 +188,11 @@ namespace services commandActions.emplace_back(infra::InPlaceType(), *this, internalRange); commandActions.emplace_back(infra::InPlaceType(), *this); - timeout.Start(commandTimeout, [this]() - { - OnError("Timeout erasing memory"); - }); - + SetCommandTimeout("Timeout erasing memory"); ExecuteCommand(onDone); } + // enum class instead of uint16_t subcommand void StUartBootloaderCommandHandler::ExtendedErase(uint16_t subcommand, const infra::Function& onDone) { this->subcommand = subcommand; @@ -233,14 +202,11 @@ namespace services commandActions.emplace_back(infra::InPlaceType(), *this, infra::MakeConstByteRange(this->subcommand)); commandActions.emplace_back(infra::InPlaceType(), *this); - timeout.Start(commandTimeout, [this]() - { - OnError("Timeout extended erasing memory"); - }); - + SetCommandTimeout("Timeout extended erasing memory"); ExecuteCommand(onDone); } + // uint16_t instead of uint8_t nPages void StUartBootloaderCommandHandler::ExtendedErase(uint8_t nPages, infra::ConstByteRange pages, const infra::Function& onDone) { if (pages.size() > 256) @@ -256,11 +222,7 @@ namespace services commandActions.emplace_back(infra::InPlaceType(), *this, internalRange); commandActions.emplace_back(infra::InPlaceType(), *this); - timeout.Start(commandTimeout, [this]() - { - OnError("Timeout extended erasing memory"); - }); - + SetCommandTimeout("Timeout extended erasing memory"); ExecuteCommand(onDone); } @@ -279,14 +241,10 @@ namespace services if (!txData.empty()) commandActions.emplace_back(infra::InPlaceType(), *this, txData); commandActions.emplace_back(infra::InPlaceType(), *this); - commandActions.emplace_back(infra::InPlaceType(), *this, rxData); - commandActions.emplace_back(infra::InPlaceType(), *this, rxStatus); - - timeout.Start(commandTimeout, [this]() - { - OnError("Timeout special command"); - }); + commandActions.emplace_back(infra::InPlaceType(), *this, rxData); + commandActions.emplace_back(infra::InPlaceType(), *this, rxStatus); + SetCommandTimeout("Timeout special command"); ExecuteCommand(onDone); } @@ -311,16 +269,13 @@ namespace services if (!txData2.empty()) commandActions.emplace_back(infra::InPlaceType(), *this, txData2); commandActions.emplace_back(infra::InPlaceType(), *this); - commandActions.emplace_back(infra::InPlaceType(), *this, rxData); - - timeout.Start(commandTimeout, [this]() - { - OnError("Timeout extended special command"); - }); + commandActions.emplace_back(infra::InPlaceType(), *this, rxData); + SetCommandTimeout("Timeout extended special command"); ExecuteCommand(onDone); } + // Get rid of the aborts below, and remove the functions from the template void StUartBootloaderCommandHandler::WriteProtect(infra::ConstByteRange sectors, const infra::Function& onDone) { std::abort(); @@ -351,11 +306,7 @@ namespace services commandActions.emplace_back(infra::InPlaceType(), *this, infra::MakeByteRange(uartInitializationData)); commandActions.emplace_back(infra::InPlaceType(), *this); - timeout.Start(commandTimeout, [this]() - { - OnError("Timeout waiting for bootloader initialization"); - }); - + SetCommandTimeout("Timeout waiting for bootloader initialization"); ExecuteCommand([this]() { onInitialized(); @@ -368,6 +319,8 @@ namespace services TryHandleTransmitAction(); } + // Remove the aborts for each action, and leave them empty. + // Get rid of ActionType void StUartBootloaderCommandHandler::TryHandleTransmitAction() { if (commandActions.front()->GetActionType() == ActionType::transmit) @@ -422,6 +375,15 @@ namespace services commandActions.front()->DataReceived(); } + void StUartBootloaderCommandHandler::SetCommandTimeout(infra::BoundedConstString reason) + { + timeoutReason = reason; + timeout.Start(commandTimeout, [this]() + { + OnError(timeoutReason); + }); + } + void StUartBootloaderCommandHandler::OnError(infra::BoundedConstString reason) { timeout.Cancel(); @@ -502,13 +464,13 @@ namespace services } } - void StUartBootloaderCommandHandler::ReceiveBufferAction::ExtractNumberOfBytes(infra::ByteInputStream& stream) + void StUartBootloaderCommandHandler::ReceiveSmallBufferAction::ExtractNumberOfBytes(infra::ByteInputStream& stream) { nBytesTotal.Emplace(stream.Extract() + 1); handler.queue.Consume(sizeof(uint8_t)); } - void StUartBootloaderCommandHandler::ReceiveSpecialBufferAction::ExtractNumberOfBytes(infra::ByteInputStream& stream) + void StUartBootloaderCommandHandler::ReceiveBigBufferAction::ExtractNumberOfBytes(infra::ByteInputStream& stream) { nBytesTotal.Emplace(stream.Extract>()); handler.queue.Consume(sizeof(uint16_t)); diff --git a/services/st_util/StUartBootloaderCommandHandler.hpp b/services/st_util/StUartBootloaderCommandHandler.hpp index c6753e54..8e0a73cd 100644 --- a/services/st_util/StUartBootloaderCommandHandler.hpp +++ b/services/st_util/StUartBootloaderCommandHandler.hpp @@ -51,6 +51,7 @@ namespace services void SendData(infra::ConstByteRange data, uint8_t checksum); void SendData(infra::ConstByteRange data); void DataReceived(); + void SetCommandTimeout(infra::BoundedConstString reason); void OnError(infra::BoundedConstString reason); private: @@ -106,7 +107,7 @@ namespace services void DataReceived() override; protected: - virtual void ExtractNumberOfBytes(infra::ByteInputStream& stream); + virtual void ExtractNumberOfBytes(infra::ByteInputStream& stream) = 0; protected: infra::ByteRange& data; @@ -114,7 +115,17 @@ namespace services std::size_t nBytesReceived = 0; }; - class ReceiveSpecialBufferAction + class ReceiveSmallBufferAction + : public ReceiveBufferAction + { + public: + using ReceiveBufferAction::ReceiveBufferAction; + + protected: + void ExtractNumberOfBytes(infra::ByteInputStream& stream) override; + }; + + class ReceiveBigBufferAction : public ReceiveBufferAction { public: @@ -173,8 +184,9 @@ namespace services infra::AutoResetFunction onError; infra::QueueForOneReaderOneIrqWriter::WithStorage<257> queue; - infra::BoundedDeque>::WithMaxSize<12> commandActions; + infra::BoundedDeque>::WithMaxSize<12> commandActions; infra::AutoResetFunction) + sizeof(infra::ByteRange)> onCommandExecuted; + infra::BoundedString::WithStorage<46> timeoutReason; infra::TimerSingleShot timeout; std::array internalBuffer; From 839ca372a2d8a8ccc42928175c67a095fb0e8ead Mon Sep 17 00:00:00 2001 From: Rob Ekelmans Date: Thu, 26 Oct 2023 11:15:47 +0000 Subject: [PATCH 08/21] Resolve PR comments. --- .../st_util/StBootloaderCommandHandler.hpp | 18 +- .../StUartBootloaderCommandHandler.cpp | 195 ++++++++---------- .../StUartBootloaderCommandHandler.hpp | 15 +- .../TestStUartBootloaderCommandHandler.cpp | 6 +- 4 files changed, 106 insertions(+), 128 deletions(-) diff --git a/services/st_util/StBootloaderCommandHandler.hpp b/services/st_util/StBootloaderCommandHandler.hpp index 0f722c4e..116e8b9a 100644 --- a/services/st_util/StBootloaderCommandHandler.hpp +++ b/services/st_util/StBootloaderCommandHandler.hpp @@ -5,6 +5,13 @@ namespace services { + enum class MassEraseSubcommand + { + global = 0xffff, + bankOne = 0xfffE, + bankTwo = 0xfffD, + }; + class StBootloaderCommandHandler { protected: @@ -20,17 +27,12 @@ namespace services virtual void ReadMemory(uint32_t address, uint8_t size, infra::ByteRange& data, const infra::Function& onDone) = 0; virtual void Go(uint32_t address, const infra::Function& onDone) = 0; virtual void WriteMemory(uint32_t address, infra::ConstByteRange data, const infra::Function& onDone) = 0; - virtual void Erase(uint8_t subcommand, const infra::Function& onDone) = 0; + virtual void Erase(const infra::Function& onDone) = 0; virtual void Erase(uint8_t nPages, infra::ConstByteRange pages, const infra::Function& onDone) = 0; - virtual void ExtendedErase(uint16_t subcommand, const infra::Function& onDone) = 0; - virtual void ExtendedErase(uint8_t nPages, infra::ConstByteRange pages, const infra::Function& onDone) = 0; + virtual void ExtendedErase(MassEraseSubcommand subcommand, const infra::Function& onDone) = 0; + virtual void ExtendedErase(uint16_t nPages, infra::ConstByteRange pages, const infra::Function& onDone) = 0; virtual void Special(uint16_t subcommand, infra::ConstByteRange txData, infra::ByteRange& rxData, infra::ByteRange& rxStatus, const infra::Function& onDone) = 0; virtual void ExtendedSpecial(uint16_t subcommand, infra::ConstByteRange txData1, infra::ConstByteRange txData2, infra::ByteRange& rxData, const infra::Function& onDone) = 0; - virtual void WriteProtect(infra::ConstByteRange sectors, const infra::Function& onDone) = 0; - virtual void WriteUnprotect(const infra::Function& onDone) = 0; - virtual void ReadoutProtect(const infra::Function& onDone) = 0; - virtual void ReadoutUnprotect(const infra::Function& onDone) = 0; - virtual void GetChecksum(uint32_t address, uint32_t memAreaSize, uint32_t crcPolinomial, uint32_t crcInitialization, const infra::Function& onDone) = 0; }; } diff --git a/services/st_util/StUartBootloaderCommandHandler.cpp b/services/st_util/StUartBootloaderCommandHandler.cpp index 917eeb35..181408c2 100644 --- a/services/st_util/StUartBootloaderCommandHandler.cpp +++ b/services/st_util/StUartBootloaderCommandHandler.cpp @@ -1,4 +1,5 @@ #include "services/st_util/StUartBootloaderCommandHandler.hpp" +#include "infra/util/ReallyAssert.hpp" #include namespace @@ -53,13 +54,12 @@ namespace services void StUartBootloaderCommandHandler::GetCommand(infra::ByteRange& commands, const infra::Function& onDone) { - if (commands.size() < 15) - std::abort(); + really_assert(commands.size() >= 15); - commandActions.emplace_back(infra::InPlaceType(), *this, getCommand); - commandActions.emplace_back(infra::InPlaceType(), *this); - commandActions.emplace_back(infra::InPlaceType(), *this, commands); - commandActions.emplace_back(infra::InPlaceType(), *this); + AddCommand(getCommand); + AddCommand(); + AddCommand(commands); + AddCommand(); SetCommandTimeout("Timeout getting commands"); ExecuteCommand([this, onDone, &commands]() @@ -71,7 +71,7 @@ namespace services commands[i] = commands[i + 1]; commands.back() = 0; - commands = infra::DiscardTail(commands, 1); + commands.pop_back(); onDone(major, minor); }); @@ -81,10 +81,10 @@ namespace services { internalRange = infra::MakeByteRange(internalBuffer); - commandActions.emplace_back(infra::InPlaceType(), *this, getVersion); - commandActions.emplace_back(infra::InPlaceType(), *this); - commandActions.emplace_back(infra::InPlaceType(), *this, internalRange, 3); - commandActions.emplace_back(infra::InPlaceType(), *this); + AddCommand(getVersion); + AddCommand(); + AddCommand(internalRange, 3); + AddCommand(); SetCommandTimeout("Timeout getting version"); ExecuteCommand([this, onDone]() @@ -99,10 +99,10 @@ namespace services { internalRange = infra::MakeByteRange(internalBuffer); - commandActions.emplace_back(infra::InPlaceType(), *this, getId); - commandActions.emplace_back(infra::InPlaceType(), *this); - commandActions.emplace_back(infra::InPlaceType(), *this, internalRange); - commandActions.emplace_back(infra::InPlaceType(), *this); + AddCommand(getId); + AddCommand(); + AddCommand(internalRange); + AddCommand(); SetCommandTimeout("Timeout getting id"); ExecuteCommand([this, onDone]() @@ -116,13 +116,13 @@ namespace services { this->address = address; - commandActions.emplace_back(infra::InPlaceType(), *this, readMemory); - commandActions.emplace_back(infra::InPlaceType(), *this); - commandActions.emplace_back(infra::InPlaceType(), *this, infra::MakeConstByteRange(this->address)); - commandActions.emplace_back(infra::InPlaceType(), *this); - commandActions.emplace_back(infra::InPlaceType(), *this, size - 1); - commandActions.emplace_back(infra::InPlaceType(), *this); - commandActions.emplace_back(infra::InPlaceType(), *this, data, size); + AddCommand(readMemory); + AddCommand(); + AddCommand(infra::MakeConstByteRange(this->address)); + AddCommand(); + AddCommand(size - 1); + AddCommand(); + AddCommand(data, size); SetCommandTimeout("Timeout reading memory"); ExecuteCommand(onDone); @@ -132,10 +132,10 @@ namespace services { this->address = address; - commandActions.emplace_back(infra::InPlaceType(), *this, go); - commandActions.emplace_back(infra::InPlaceType(), *this); - commandActions.emplace_back(infra::InPlaceType(), *this, infra::MakeConstByteRange(this->address)); - commandActions.emplace_back(infra::InPlaceType(), *this); + AddCommand(go); + AddCommand(); + AddCommand(infra::MakeConstByteRange(this->address)); + AddCommand(); SetCommandTimeout("Timeout go"); ExecuteCommand(onDone); @@ -143,8 +143,7 @@ namespace services void StUartBootloaderCommandHandler::WriteMemory(uint32_t address, infra::ConstByteRange data, const infra::Function& onDone) { - if (data.size() > 256) - std::abort(); + really_assert(data.size() <= 256); this->address = address; @@ -152,23 +151,25 @@ namespace services std::copy(data.begin(), data.begin() + data.size(), internalBuffer.begin() + 1); internalRange = infra::MakeRange(internalBuffer.begin(), internalBuffer.begin() + internalBuffer[0]); - commandActions.emplace_back(infra::InPlaceType(), *this, writeMemory); - commandActions.emplace_back(infra::InPlaceType(), *this); - commandActions.emplace_back(infra::InPlaceType(), *this, infra::MakeConstByteRange(this->address)); - commandActions.emplace_back(infra::InPlaceType(), *this); - commandActions.emplace_back(infra::InPlaceType(), *this, internalRange); - commandActions.emplace_back(infra::InPlaceType(), *this); + AddCommand(writeMemory); + AddCommand(); + AddCommand(infra::MakeConstByteRange(this->address)); + AddCommand(); + AddCommand(internalRange); + AddCommand(); SetCommandTimeout("Timeout writing memory"); ExecuteCommand(onDone); } - void StUartBootloaderCommandHandler::Erase(uint8_t subcommand, const infra::Function& onDone) + void StUartBootloaderCommandHandler::Erase(const infra::Function& onDone) { - commandActions.emplace_back(infra::InPlaceType(), *this, erase); - commandActions.emplace_back(infra::InPlaceType(), *this); - commandActions.emplace_back(infra::InPlaceType(), *this, subcommand); - commandActions.emplace_back(infra::InPlaceType(), *this); + constexpr uint8_t globalEraseSubCommand = 0xff; + + AddCommand(erase); + AddCommand(); + AddCommand(globalEraseSubCommand); + AddCommand(); SetCommandTimeout("Timeout erasing memory"); ExecuteCommand(onDone); @@ -176,51 +177,47 @@ namespace services void StUartBootloaderCommandHandler::Erase(uint8_t nPages, infra::ConstByteRange pages, const infra::Function& onDone) { - if (pages.size() > 256) - std::abort(); + really_assert(pages.size() <= 256); internalBuffer[0] = static_cast(pages.size() + 1); std::copy(pages.begin(), pages.begin() + pages.size(), internalBuffer.begin() + 1); internalRange = infra::MakeRange(internalBuffer.begin(), internalBuffer.begin() + internalBuffer[0]); - commandActions.emplace_back(infra::InPlaceType(), *this, erase); - commandActions.emplace_back(infra::InPlaceType(), *this); - commandActions.emplace_back(infra::InPlaceType(), *this, internalRange); - commandActions.emplace_back(infra::InPlaceType(), *this); + AddCommand(erase); + AddCommand(); + AddCommand(internalRange); + AddCommand(); SetCommandTimeout("Timeout erasing memory"); ExecuteCommand(onDone); } - // enum class instead of uint16_t subcommand - void StUartBootloaderCommandHandler::ExtendedErase(uint16_t subcommand, const infra::Function& onDone) + void StUartBootloaderCommandHandler::ExtendedErase(MassEraseSubcommand subcommand, const infra::Function& onDone) { - this->subcommand = subcommand; + this->subcommand = static_cast(subcommand); - commandActions.emplace_back(infra::InPlaceType(), *this, extendedErase); - commandActions.emplace_back(infra::InPlaceType(), *this); - commandActions.emplace_back(infra::InPlaceType(), *this, infra::MakeConstByteRange(this->subcommand)); - commandActions.emplace_back(infra::InPlaceType(), *this); + AddCommand(extendedErase); + AddCommand(); + AddCommand(infra::MakeConstByteRange(this->subcommand)); + AddCommand(); SetCommandTimeout("Timeout extended erasing memory"); ExecuteCommand(onDone); } - // uint16_t instead of uint8_t nPages - void StUartBootloaderCommandHandler::ExtendedErase(uint8_t nPages, infra::ConstByteRange pages, const infra::Function& onDone) + void StUartBootloaderCommandHandler::ExtendedErase(uint16_t nPages, infra::ConstByteRange pages, const infra::Function& onDone) { - if (pages.size() > 256) - std::abort(); + really_assert(pages.size() <= 256); internalBuffer[0] = static_cast(nPages >> 8); internalBuffer[1] = static_cast(nPages & 0xff) + 1; std::copy(pages.begin(), pages.begin() + pages.size(), internalBuffer.begin() + 2); internalRange = infra::MakeRange(internalBuffer.begin(), internalBuffer.begin() + pages.size() + 2); - commandActions.emplace_back(infra::InPlaceType(), *this, extendedErase); - commandActions.emplace_back(infra::InPlaceType(), *this); - commandActions.emplace_back(infra::InPlaceType(), *this, internalRange); - commandActions.emplace_back(infra::InPlaceType(), *this); + AddCommand(extendedErase); + AddCommand(); + AddCommand(internalRange); + AddCommand(); SetCommandTimeout("Timeout extended erasing memory"); ExecuteCommand(onDone); @@ -233,16 +230,16 @@ namespace services internalBuffer[0] = static_cast(txData.size() >> 8); internalBuffer[1] = static_cast(txData.size() & 0xff); - commandActions.emplace_back(infra::InPlaceType(), *this, special); - commandActions.emplace_back(infra::InPlaceType(), *this); - commandActions.emplace_back(infra::InPlaceType(), *this, infra::MakeByteRange(this->subcommand)); - commandActions.emplace_back(infra::InPlaceType(), *this); - commandActions.emplace_back(infra::InPlaceType(), *this, infra::MakeRange(internalBuffer.begin(), internalBuffer.begin() + 2)); + AddCommand(special); + AddCommand(); + AddCommand(infra::MakeByteRange(this->subcommand)); + AddCommand(); + AddCommand(infra::MakeRange(internalBuffer.begin(), internalBuffer.begin() + 2)); if (!txData.empty()) - commandActions.emplace_back(infra::InPlaceType(), *this, txData); - commandActions.emplace_back(infra::InPlaceType(), *this); - commandActions.emplace_back(infra::InPlaceType(), *this, rxData); - commandActions.emplace_back(infra::InPlaceType(), *this, rxStatus); + AddCommand(txData); + AddCommand(); + AddCommand(rxData); + AddCommand(rxStatus); SetCommandTimeout("Timeout special command"); ExecuteCommand(onDone); @@ -257,54 +254,28 @@ namespace services internalBuffer[2] = static_cast(txData2.size() >> 8); internalBuffer[3] = static_cast(txData2.size() & 0xff); - commandActions.emplace_back(infra::InPlaceType(), *this, extendedSpecial); - commandActions.emplace_back(infra::InPlaceType(), *this); - commandActions.emplace_back(infra::InPlaceType(), *this, infra::MakeByteRange(this->subcommand)); - commandActions.emplace_back(infra::InPlaceType(), *this); - commandActions.emplace_back(infra::InPlaceType(), *this, infra::MakeRange(internalBuffer.begin(), internalBuffer.begin() + 2)); + AddCommand(extendedSpecial); + AddCommand(); + AddCommand(infra::MakeByteRange(this->subcommand)); + AddCommand(); + AddCommand(infra::MakeRange(internalBuffer.begin(), internalBuffer.begin() + 2)); if (!txData1.empty()) - commandActions.emplace_back(infra::InPlaceType(), *this, txData1); - commandActions.emplace_back(infra::InPlaceType(), *this); - commandActions.emplace_back(infra::InPlaceType(), *this, infra::MakeRange(internalBuffer.begin() + 2, internalBuffer.begin() + 4)); + AddCommand(txData1); + AddCommand(); + AddCommand(infra::MakeRange(internalBuffer.begin() + 2, internalBuffer.begin() + 4)); if (!txData2.empty()) - commandActions.emplace_back(infra::InPlaceType(), *this, txData2); - commandActions.emplace_back(infra::InPlaceType(), *this); - commandActions.emplace_back(infra::InPlaceType(), *this, rxData); + AddCommand(txData2); + AddCommand(); + AddCommand(rxData); SetCommandTimeout("Timeout extended special command"); ExecuteCommand(onDone); } - // Get rid of the aborts below, and remove the functions from the template - void StUartBootloaderCommandHandler::WriteProtect(infra::ConstByteRange sectors, const infra::Function& onDone) - { - std::abort(); - } - - void StUartBootloaderCommandHandler::WriteUnprotect(const infra::Function& onDone) - { - std::abort(); - } - - void StUartBootloaderCommandHandler::ReadoutProtect(const infra::Function& onDone) - { - std::abort(); - } - - void StUartBootloaderCommandHandler::ReadoutUnprotect(const infra::Function& onDone) - { - std::abort(); - } - - void StUartBootloaderCommandHandler::GetChecksum(uint32_t address, uint32_t memAreaSize, uint32_t crcPolinomial, uint32_t crcInitialization, const infra::Function& onDone) - { - std::abort(); - } - void StUartBootloaderCommandHandler::InitializeUartBootloader() { - commandActions.emplace_back(infra::InPlaceType(), *this, infra::MakeByteRange(uartInitializationData)); - commandActions.emplace_back(infra::InPlaceType(), *this); + AddCommand(infra::MakeByteRange(uartInitializationData)); + AddCommand(); SetCommandTimeout("Timeout waiting for bootloader initialization"); ExecuteCommand([this]() @@ -313,6 +284,12 @@ namespace services }); } + template + void StUartBootloaderCommandHandler::AddCommand(Args&&... args) + { + commandActions.emplace_back(infra::InPlaceType(), *this, std::forward(args)...); + } + void StUartBootloaderCommandHandler::ExecuteCommand(const infra::Function) + sizeof(infra::ByteRange)>& onCommandExecuted) { this->onCommandExecuted = onCommandExecuted; @@ -388,7 +365,7 @@ namespace services { timeout.Cancel(); serial.ReceiveData([](auto data) {}); - if (onError) + if (onError != nullptr) onError(reason); } diff --git a/services/st_util/StUartBootloaderCommandHandler.hpp b/services/st_util/StUartBootloaderCommandHandler.hpp index 8e0a73cd..b3830f72 100644 --- a/services/st_util/StUartBootloaderCommandHandler.hpp +++ b/services/st_util/StUartBootloaderCommandHandler.hpp @@ -31,20 +31,19 @@ namespace services void ReadMemory(uint32_t address, uint8_t size, infra::ByteRange& data, const infra::Function& onDone) override; void Go(uint32_t address, const infra::Function& onDone) override; void WriteMemory(uint32_t address, infra::ConstByteRange data, const infra::Function& onDone) override; - void Erase(uint8_t subcommand, const infra::Function& onDone) override; + void Erase(const infra::Function& onDone) override; void Erase(uint8_t nPages, infra::ConstByteRange pages, const infra::Function& onDone) override; - void ExtendedErase(uint16_t subcommand, const infra::Function& onDone) override; - void ExtendedErase(uint8_t nPages, infra::ConstByteRange pages, const infra::Function& onDone) override; + void ExtendedErase(MassEraseSubcommand subcommand, const infra::Function& onDone) override; + void ExtendedErase(uint16_t nPages, infra::ConstByteRange pages, const infra::Function& onDone) override; void Special(uint16_t subcommand, infra::ConstByteRange txData, infra::ByteRange& rxData, infra::ByteRange& rxStatus, const infra::Function& onDone) override; void ExtendedSpecial(uint16_t subcommand, infra::ConstByteRange txData1, infra::ConstByteRange txData2, infra::ByteRange& rxData, const infra::Function& onDone) override; - void WriteProtect(infra::ConstByteRange sectors, const infra::Function& onDone) override; - void WriteUnprotect(const infra::Function& onDone) override; - void ReadoutProtect(const infra::Function& onDone) override; - void ReadoutUnprotect(const infra::Function& onDone) override; - void GetChecksum(uint32_t address, uint32_t memAreaSize, uint32_t crcPolinomial, uint32_t crcInitialization, const infra::Function& onDone) override; private: void InitializeUartBootloader(); + + template + void AddCommand(Args&&... args); + void ExecuteCommand(const infra::Function) + sizeof(infra::ByteRange)>& onCommandExecuted); void TryHandleNextAction(); void TryHandleTransmitAction(); diff --git a/services/st_util/test/TestStUartBootloaderCommandHandler.cpp b/services/st_util/test/TestStUartBootloaderCommandHandler.cpp index 51266273..a25c5eae 100644 --- a/services/st_util/test/TestStUartBootloaderCommandHandler.cpp +++ b/services/st_util/test/TestStUartBootloaderCommandHandler.cpp @@ -299,7 +299,7 @@ TEST_F(StUartBootloaderCommandHandlerTest, Erase_global) infra::VerifyingFunctionMock ondone; ExpectSendData(0x43, 0xbc); - handler.Erase(0xff, ondone); + handler.Erase(ondone); ExpectSendData(0xff, 0x00); ExpectReceiveData({ 0x79 }); ExpectReceiveData({ 0x79 }); @@ -338,7 +338,7 @@ TEST_F(StUartBootloaderCommandHandlerTest, ExtendedErase_global) infra::VerifyingFunctionMock ondone; ExpectSendData(0x44, 0xbb); - handler.ExtendedErase(0xfffe, ondone); + handler.ExtendedErase(services::MassEraseSubcommand::bankOne, ondone); ExpectSendData({ 0xff, 0xfe }, 0x01); ExpectReceiveData({ 0x79 }); ExpectReceiveData({ 0x79 }); @@ -363,7 +363,7 @@ TEST_F(StUartBootloaderCommandHandlerTest, ExtendedErase_timeout) testing::StrictMock> ondone; ExpectSendData(0x44, 0xbb); - handler.ExtendedErase(0xffff, [&ondone]() + handler.ExtendedErase(services::MassEraseSubcommand::global, [&ondone]() { ondone.callback(); }); From 8443f888ed6f92b80b45e1f2055010e6f89c64ed Mon Sep 17 00:00:00 2001 From: Rob Ekelmans Date: Thu, 26 Oct 2023 14:44:41 +0000 Subject: [PATCH 09/21] Resolve PR comments --- .../StUartBootloaderCommandHandler.cpp | 19 ++++++++----------- .../StUartBootloaderCommandHandler.hpp | 4 ++-- 2 files changed, 10 insertions(+), 13 deletions(-) diff --git a/services/st_util/StUartBootloaderCommandHandler.cpp b/services/st_util/StUartBootloaderCommandHandler.cpp index 181408c2..b2d25570 100644 --- a/services/st_util/StUartBootloaderCommandHandler.cpp +++ b/services/st_util/StUartBootloaderCommandHandler.cpp @@ -36,7 +36,7 @@ namespace services , onError(onError) , queue([this]() { - DataReceived(); + TryHandleDataReceived(); }) { serial.ReceiveData([this](auto data) @@ -310,6 +310,12 @@ namespace services } } + void StUartBootloaderCommandHandler::TryHandleDataReceived() + { + if (!queue.Empty()) + commandActions.front()->DataReceived(); + } + void StUartBootloaderCommandHandler::TryHandleNextAction() { commandActions.pop_front(); @@ -322,7 +328,7 @@ namespace services } if (commandActions.front()->GetActionType() == ActionType::receive) - commandActions.front()->DataReceived(); + TryHandleDataReceived(); else TryHandleTransmitAction(); } @@ -346,12 +352,6 @@ namespace services }); } - void StUartBootloaderCommandHandler::DataReceived() - { - if (!commandActions.empty()) - commandActions.front()->DataReceived(); - } - void StUartBootloaderCommandHandler::SetCommandTimeout(infra::BoundedConstString reason) { timeoutReason = reason; @@ -421,9 +421,6 @@ namespace services void StUartBootloaderCommandHandler::ReceiveBufferAction::DataReceived() { - if (handler.queue.Empty()) - return; - infra::ByteInputStream stream(handler.queue.ContiguousRange()); if (!nBytesTotal) diff --git a/services/st_util/StUartBootloaderCommandHandler.hpp b/services/st_util/StUartBootloaderCommandHandler.hpp index b3830f72..fdae275c 100644 --- a/services/st_util/StUartBootloaderCommandHandler.hpp +++ b/services/st_util/StUartBootloaderCommandHandler.hpp @@ -45,11 +45,11 @@ namespace services void AddCommand(Args&&... args); void ExecuteCommand(const infra::Function) + sizeof(infra::ByteRange)>& onCommandExecuted); - void TryHandleNextAction(); void TryHandleTransmitAction(); + void TryHandleDataReceived(); + void TryHandleNextAction(); void SendData(infra::ConstByteRange data, uint8_t checksum); void SendData(infra::ConstByteRange data); - void DataReceived(); void SetCommandTimeout(infra::BoundedConstString reason); void OnError(infra::BoundedConstString reason); From db0da1a2a000510371d3c84484dfb88a46205a02 Mon Sep 17 00:00:00 2001 From: Rob Ekelmans Date: Mon, 30 Oct 2023 10:25:30 +0000 Subject: [PATCH 10/21] Process PR comments --- .../st_util/StBootloaderCommandHandler.hpp | 8 +- .../StUartBootloaderCommandHandler.cpp | 130 ++++++++++-------- .../StUartBootloaderCommandHandler.hpp | 45 ++++-- .../TestStUartBootloaderCommandHandler.cpp | 64 +++++---- 4 files changed, 151 insertions(+), 96 deletions(-) diff --git a/services/st_util/StBootloaderCommandHandler.hpp b/services/st_util/StBootloaderCommandHandler.hpp index 116e8b9a..58b4b28d 100644 --- a/services/st_util/StBootloaderCommandHandler.hpp +++ b/services/st_util/StBootloaderCommandHandler.hpp @@ -24,13 +24,13 @@ namespace services virtual void GetCommand(infra::ByteRange& commands, const infra::Function& onDone) = 0; virtual void GetVersion(const infra::Function& onDone) = 0; virtual void GetId(const infra::Function& onDone) = 0; - virtual void ReadMemory(uint32_t address, uint8_t size, infra::ByteRange& data, const infra::Function& onDone) = 0; + virtual void ReadMemory(uint32_t address, infra::ByteRange& data, const infra::Function& onDone) = 0; virtual void Go(uint32_t address, const infra::Function& onDone) = 0; virtual void WriteMemory(uint32_t address, infra::ConstByteRange data, const infra::Function& onDone) = 0; - virtual void Erase(const infra::Function& onDone) = 0; - virtual void Erase(uint8_t nPages, infra::ConstByteRange pages, const infra::Function& onDone) = 0; + virtual void GlobalErase(const infra::Function& onDone) = 0; + virtual void Erase(infra::ConstByteRange pages, const infra::Function& onDone) = 0; virtual void ExtendedErase(MassEraseSubcommand subcommand, const infra::Function& onDone) = 0; - virtual void ExtendedErase(uint16_t nPages, infra::ConstByteRange pages, const infra::Function& onDone) = 0; + virtual void ExtendedErase(infra::ConstByteRange pages, const infra::Function& onDone) = 0; virtual void Special(uint16_t subcommand, infra::ConstByteRange txData, infra::ByteRange& rxData, infra::ByteRange& rxStatus, const infra::Function& onDone) = 0; virtual void ExtendedSpecial(uint16_t subcommand, infra::ConstByteRange txData1, infra::ConstByteRange txData2, infra::ByteRange& rxData, const infra::Function& onDone) = 0; }; diff --git a/services/st_util/StUartBootloaderCommandHandler.cpp b/services/st_util/StUartBootloaderCommandHandler.cpp index b2d25570..81e60f94 100644 --- a/services/st_util/StUartBootloaderCommandHandler.cpp +++ b/services/st_util/StUartBootloaderCommandHandler.cpp @@ -1,4 +1,5 @@ #include "services/st_util/StUartBootloaderCommandHandler.hpp" +#include "infra/util/ByteRange.hpp" #include "infra/util/ReallyAssert.hpp" #include @@ -112,17 +113,17 @@ namespace services }); } - void StUartBootloaderCommandHandler::ReadMemory(uint32_t address, uint8_t size, infra::ByteRange& data, const infra::Function& onDone) + void StUartBootloaderCommandHandler::ReadMemory(uint32_t address, infra::ByteRange& data, const infra::Function& onDone) { this->address = address; AddCommand(readMemory); AddCommand(); - AddCommand(infra::MakeConstByteRange(this->address)); + AddCommand(infra::MakeConstByteRange(this->address)); AddCommand(); - AddCommand(size - 1); + AddCommand(data.size() - 1); AddCommand(); - AddCommand(data, size); + AddCommand(data, data.size()); SetCommandTimeout("Timeout reading memory"); ExecuteCommand(onDone); @@ -134,7 +135,7 @@ namespace services AddCommand(go); AddCommand(); - AddCommand(infra::MakeConstByteRange(this->address)); + AddCommand(infra::MakeConstByteRange(this->address)); AddCommand(); SetCommandTimeout("Timeout go"); @@ -147,22 +148,18 @@ namespace services this->address = address; - internalBuffer[0] = static_cast(data.size() + 1); - std::copy(data.begin(), data.begin() + data.size(), internalBuffer.begin() + 1); - internalRange = infra::MakeRange(internalBuffer.begin(), internalBuffer.begin() + internalBuffer[0]); - AddCommand(writeMemory); AddCommand(); - AddCommand(infra::MakeConstByteRange(this->address)); + AddCommand(infra::MakeConstByteRange(this->address)); AddCommand(); - AddCommand(internalRange); + AddCommand(data); AddCommand(); SetCommandTimeout("Timeout writing memory"); ExecuteCommand(onDone); } - void StUartBootloaderCommandHandler::Erase(const infra::Function& onDone) + void StUartBootloaderCommandHandler::GlobalErase(const infra::Function& onDone) { constexpr uint8_t globalEraseSubCommand = 0xff; @@ -175,17 +172,13 @@ namespace services ExecuteCommand(onDone); } - void StUartBootloaderCommandHandler::Erase(uint8_t nPages, infra::ConstByteRange pages, const infra::Function& onDone) + void StUartBootloaderCommandHandler::Erase(infra::ConstByteRange pages, const infra::Function& onDone) { - really_assert(pages.size() <= 256); - - internalBuffer[0] = static_cast(pages.size() + 1); - std::copy(pages.begin(), pages.begin() + pages.size(), internalBuffer.begin() + 1); - internalRange = infra::MakeRange(internalBuffer.begin(), internalBuffer.begin() + internalBuffer[0]); + really_assert(pages.size() <= 255); AddCommand(erase); AddCommand(); - AddCommand(internalRange); + AddCommand(pages); AddCommand(); SetCommandTimeout("Timeout erasing memory"); @@ -198,25 +191,21 @@ namespace services AddCommand(extendedErase); AddCommand(); - AddCommand(infra::MakeConstByteRange(this->subcommand)); + AddCommand(infra::MakeConstByteRange(this->subcommand)); AddCommand(); SetCommandTimeout("Timeout extended erasing memory"); ExecuteCommand(onDone); } - void StUartBootloaderCommandHandler::ExtendedErase(uint16_t nPages, infra::ConstByteRange pages, const infra::Function& onDone) + void StUartBootloaderCommandHandler::ExtendedErase(infra::ConstByteRange pages, const infra::Function& onDone) { really_assert(pages.size() <= 256); - - internalBuffer[0] = static_cast(nPages >> 8); - internalBuffer[1] = static_cast(nPages & 0xff) + 1; - std::copy(pages.begin(), pages.begin() + pages.size(), internalBuffer.begin() + 2); - internalRange = infra::MakeRange(internalBuffer.begin(), internalBuffer.begin() + pages.size() + 2); + really_assert(pages.size() % 2 == 0); AddCommand(extendedErase); AddCommand(); - AddCommand(internalRange); + AddCommand((pages.size() / 2) - 1, pages); AddCommand(); SetCommandTimeout("Timeout extended erasing memory"); @@ -225,18 +214,15 @@ namespace services void StUartBootloaderCommandHandler::Special(uint16_t subcommand, infra::ConstByteRange txData, infra::ByteRange& rxData, infra::ByteRange& rxStatus, const infra::Function& onDone) { - this->subcommand = subcommand; + really_assert(txData.size() <= 128); - internalBuffer[0] = static_cast(txData.size() >> 8); - internalBuffer[1] = static_cast(txData.size() & 0xff); + this->subcommand = subcommand; AddCommand(special); AddCommand(); - AddCommand(infra::MakeByteRange(this->subcommand)); + AddCommand(infra::MakeByteRange(this->subcommand)); AddCommand(); - AddCommand(infra::MakeRange(internalBuffer.begin(), internalBuffer.begin() + 2)); - if (!txData.empty()) - AddCommand(txData); + AddCommand(txData.size(), txData); AddCommand(); AddCommand(rxData); AddCommand(rxStatus); @@ -247,24 +233,18 @@ namespace services void StUartBootloaderCommandHandler::ExtendedSpecial(uint16_t subcommand, infra::ConstByteRange txData1, infra::ConstByteRange txData2, infra::ByteRange& rxData, const infra::Function& onDone) { - this->subcommand = subcommand; + really_assert(txData1.size() <= 128); + really_assert(txData2.size() <= 1024); - internalBuffer[0] = static_cast(txData1.size() >> 8); - internalBuffer[1] = static_cast(txData1.size() & 0xff); - internalBuffer[2] = static_cast(txData2.size() >> 8); - internalBuffer[3] = static_cast(txData2.size() & 0xff); + this->subcommand = subcommand; AddCommand(extendedSpecial); AddCommand(); - AddCommand(infra::MakeByteRange(this->subcommand)); + AddCommand(infra::MakeByteRange(this->subcommand)); AddCommand(); - AddCommand(infra::MakeRange(internalBuffer.begin(), internalBuffer.begin() + 2)); - if (!txData1.empty()) - AddCommand(txData1); + AddCommand(txData1.size(), txData1); AddCommand(); - AddCommand(infra::MakeRange(internalBuffer.begin() + 2, internalBuffer.begin() + 4)); - if (!txData2.empty()) - AddCommand(txData2); + AddCommand(txData2.size(), txData2); AddCommand(); AddCommand(rxData); @@ -337,10 +317,7 @@ namespace services { serial.SendData(data, [this, checksum]() { - serial.SendData(infra::MakeConstByteRange(checksum), [this]() - { - TryHandleTransmitAction(); - }); + SendData(infra::MakeConstByteRange(checksum)); }); } @@ -468,21 +445,58 @@ namespace services void StUartBootloaderCommandHandler::TransmitWithTwosComplementChecksum::SendData() { auto checksum = data ^ 0xff; - handler.SendData(infra::MakeByteRange(data), checksum); + handler.SendData(infra::MakeConstByteRange(data), checksum); } - StUartBootloaderCommandHandler::TransmitWithChecksumAction::TransmitWithChecksumAction(StUartBootloaderCommandHandler& handler, infra::ConstByteRange data) + StUartBootloaderCommandHandler::TransmitChecksummedBuffer::TransmitChecksummedBuffer(StUartBootloaderCommandHandler& handler, infra::ConstByteRange data) : StUartBootloaderCommandHandler::TransmitAction(handler) , data(data) - {} + { + for (auto& b : data) + AddToChecksum(b); + } - void StUartBootloaderCommandHandler::TransmitWithChecksumAction::SendData() + void StUartBootloaderCommandHandler::TransmitChecksummedBuffer::AddToChecksum(const uint8_t& byte) { - auto checksum = 0; + checksum ^= byte; + } - for (auto b : data) - checksum ^= b; + void StUartBootloaderCommandHandler::TransmitChecksummedBuffer::SendData() + { + if (data.empty()) + handler.SendData(infra::MakeConstByteRange(checksum)); + else + handler.SendData(data, checksum); + } - handler.SendData(data, checksum); + StUartBootloaderCommandHandler::TransmitSmallBuffer::TransmitSmallBuffer(StUartBootloaderCommandHandler& handler, infra::ConstByteRange data) + : StUartBootloaderCommandHandler::TransmitChecksummedBuffer(handler, data) + { + size = data.size() - 1; + AddToChecksum(size); + } + + void StUartBootloaderCommandHandler::TransmitSmallBuffer::SendData() + { + handler.serial.SendData(infra::MakeConstByteRange(size), [this]() + { + StUartBootloaderCommandHandler::TransmitChecksummedBuffer::SendData(); + }); + } + + StUartBootloaderCommandHandler::TransmitBigBuffer::TransmitBigBuffer(StUartBootloaderCommandHandler& handler, uint16_t size, infra::ConstByteRange data) + : StUartBootloaderCommandHandler::TransmitChecksummedBuffer(handler, data) + , size(size) + { + AddToChecksum(static_cast(size >> 8)); + AddToChecksum(static_cast(size & 0xff)); + } + + void StUartBootloaderCommandHandler::TransmitBigBuffer::SendData() + { + handler.serial.SendData(infra::MakeConstByteRange(size), [this]() + { + StUartBootloaderCommandHandler::TransmitChecksummedBuffer::SendData(); + }); } } diff --git a/services/st_util/StUartBootloaderCommandHandler.hpp b/services/st_util/StUartBootloaderCommandHandler.hpp index fdae275c..b4d2d466 100644 --- a/services/st_util/StUartBootloaderCommandHandler.hpp +++ b/services/st_util/StUartBootloaderCommandHandler.hpp @@ -8,7 +8,10 @@ #include "infra/util/AutoResetFunction.hpp" #include "infra/util/BoundedDeque.hpp" #include "infra/util/BoundedString.hpp" +#include "infra/util/BoundedVector.hpp" +#include "infra/util/ByteRange.hpp" #include "infra/util/Endian.hpp" +#include "infra/util/Function.hpp" #include "infra/util/Optional.hpp" #include "infra/util/PolymorphicVariant.hpp" #include "services/st_util/StBootloaderCommandHandler.hpp" @@ -28,13 +31,13 @@ namespace services void GetCommand(infra::ByteRange& commands, const infra::Function& onDone) override; void GetVersion(const infra::Function& onDone) override; void GetId(const infra::Function& onDone) override; - void ReadMemory(uint32_t address, uint8_t size, infra::ByteRange& data, const infra::Function& onDone) override; + void ReadMemory(uint32_t address, infra::ByteRange& data, const infra::Function& onDone) override; void Go(uint32_t address, const infra::Function& onDone) override; void WriteMemory(uint32_t address, infra::ConstByteRange data, const infra::Function& onDone) override; - void Erase(const infra::Function& onDone) override; - void Erase(uint8_t nPages, infra::ConstByteRange pages, const infra::Function& onDone) override; + void GlobalErase(const infra::Function& onDone) override; + void Erase(infra::ConstByteRange pages, const infra::Function& onDone) override; void ExtendedErase(MassEraseSubcommand subcommand, const infra::Function& onDone) override; - void ExtendedErase(uint16_t nPages, infra::ConstByteRange pages, const infra::Function& onDone) override; + void ExtendedErase(infra::ConstByteRange pages, const infra::Function& onDone) override; void Special(uint16_t subcommand, infra::ConstByteRange txData, infra::ByteRange& rxData, infra::ByteRange& rxStatus, const infra::Function& onDone) override; void ExtendedSpecial(uint16_t subcommand, infra::ConstByteRange txData1, infra::ConstByteRange txData2, infra::ByteRange& rxData, const infra::Function& onDone) override; @@ -165,16 +168,42 @@ namespace services uint8_t data; }; - class TransmitWithChecksumAction + class TransmitChecksummedBuffer : public TransmitAction { public: - TransmitWithChecksumAction(StUartBootloaderCommandHandler& handler, infra::ConstByteRange data); + TransmitChecksummedBuffer(StUartBootloaderCommandHandler& handler, infra::ConstByteRange data); + void AddToChecksum(const uint8_t& byte); void SendData() override; private: infra::ConstByteRange data; + uint8_t checksum; + }; + + class TransmitSmallBuffer + : public TransmitChecksummedBuffer + { + public: + TransmitSmallBuffer(StUartBootloaderCommandHandler& handler, infra::ConstByteRange data); + + void SendData() override; + + private: + uint8_t size; + }; + + class TransmitBigBuffer + : public TransmitChecksummedBuffer + { + public: + TransmitBigBuffer(StUartBootloaderCommandHandler& handler, uint16_t size, infra::ConstByteRange data); + + void SendData() override; + + private: + infra::BigEndian size; }; private: @@ -183,12 +212,12 @@ namespace services infra::AutoResetFunction onError; infra::QueueForOneReaderOneIrqWriter::WithStorage<257> queue; - infra::BoundedDeque>::WithMaxSize<12> commandActions; + infra::BoundedDeque>::WithMaxSize<12> commandActions; infra::AutoResetFunction) + sizeof(infra::ByteRange)> onCommandExecuted; infra::BoundedString::WithStorage<46> timeoutReason; infra::TimerSingleShot timeout; - std::array internalBuffer; + std::array internalBuffer; infra::ByteRange internalRange; infra::BigEndian address; infra::BigEndian subcommand; diff --git a/services/st_util/test/TestStUartBootloaderCommandHandler.cpp b/services/st_util/test/TestStUartBootloaderCommandHandler.cpp index a25c5eae..8f0911b0 100644 --- a/services/st_util/test/TestStUartBootloaderCommandHandler.cpp +++ b/services/st_util/test/TestStUartBootloaderCommandHandler.cpp @@ -27,6 +27,20 @@ class StUartBootloaderCommandHandlerTest })); } + void ExpectSendData(std::vector data1, std::vector data2, uint8_t checksum) + { + static std::vector staticData2; + staticData2 = data2; + + ExpectSendData(data1, [this, checksum]() + { + ExpectSendData(staticData2, [this, checksum]() + { + ExpectSendData({ checksum }); + }); + }); + } + void ExpectSendData(std::vector data, uint8_t checksum) { ExpectSendData(data, [this, checksum]() @@ -198,11 +212,11 @@ TEST_F(StUartBootloaderCommandHandlerTest, ReadMemory) Initialize(); infra::VerifyingFunctionMock ondone; uint32_t address = 0x01020304; - std::array dataBuffer = {}; + std::array dataBuffer = {}; infra::ByteRange data(dataBuffer); ExpectSendData(0x11, 0xEE); - handler.ReadMemory(address, 3, data, ondone); + handler.ReadMemory(address, data, ondone); ExpectSendData({ 0x01, 0x02, 0x03, 0x04 }, 0x04); ExpectReceiveData({ 0x79 }); @@ -213,7 +227,7 @@ TEST_F(StUartBootloaderCommandHandlerTest, ReadMemory) ExpectReceiveData({ 0x79 }); ExpectReceiveData({ 0x05, 0x06, 0x07 }); - EXPECT_EQ((std::array{ 0x05, 0x06, 0x07, 0x00 }), dataBuffer); + EXPECT_EQ((std::array{ 0x05, 0x06, 0x07 }), dataBuffer); } TEST_F(StUartBootloaderCommandHandlerTest, ReadMemory_timeout) @@ -224,7 +238,7 @@ TEST_F(StUartBootloaderCommandHandlerTest, ReadMemory_timeout) infra::ByteRange data(dataBuffer); ExpectSendData(0x11, 0xEE); - handler.ReadMemory(0x01020304, 3, data, [&ondone]() + handler.ReadMemory(0x01020304, data, [&ondone]() { ondone.callback(); }); @@ -273,7 +287,7 @@ TEST_F(StUartBootloaderCommandHandlerTest, WriteMemory) ExpectSendData({ 0x01, 0x02, 0x03, 0x04 }, 0x04); ExpectReceiveData({ 0x79 }); - ExpectSendData({ 0x05, 0x05, 0x06, 0x07, 0x08 }, 0x09); + ExpectSendData({ 0x03 }, { 0x05, 0x06, 0x07, 0x08 }, 0x0f); ExpectReceiveData({ 0x79 }); ExpectReceiveData({ 0x79 }); } @@ -293,13 +307,13 @@ TEST_F(StUartBootloaderCommandHandlerTest, WriteMemory_timeout) ExpectErrorOnTimeout(std::chrono::seconds(1), "Timeout writing memory"); } -TEST_F(StUartBootloaderCommandHandlerTest, Erase_global) +TEST_F(StUartBootloaderCommandHandlerTest, GlobalErase) { Initialize(); infra::VerifyingFunctionMock ondone; ExpectSendData(0x43, 0xbc); - handler.Erase(ondone); + handler.GlobalErase(ondone); ExpectSendData(0xff, 0x00); ExpectReceiveData({ 0x79 }); ExpectReceiveData({ 0x79 }); @@ -312,8 +326,8 @@ TEST_F(StUartBootloaderCommandHandlerTest, Erase_pages) std::array pages = { 0x01, 0x02, 0x03, 0x04 }; ExpectSendData(0x43, 0xbc); - handler.Erase(4, infra::MakeByteRange(pages), ondone); - ExpectSendData({ 0x05, 0x01, 0x02, 0x03, 0x04 }, 0x01); + handler.Erase(infra::MakeByteRange(pages), ondone); + ExpectSendData({ 0x03 }, { 0x01, 0x02, 0x03, 0x04 }, 0x07); ExpectReceiveData({ 0x79 }); ExpectReceiveData({ 0x79 }); } @@ -325,14 +339,14 @@ TEST_F(StUartBootloaderCommandHandlerTest, Erase_timeout) std::array data; ExpectSendData(0x43, 0xbc); - handler.Erase(0xff, infra::ByteRange{}, [&ondone]() + handler.Erase(infra::ByteRange{}, [&ondone]() { ondone.callback(); }); ExpectErrorOnTimeout(std::chrono::seconds(1), "Timeout erasing memory"); } -TEST_F(StUartBootloaderCommandHandlerTest, ExtendedErase_global) +TEST_F(StUartBootloaderCommandHandlerTest, ExtendedErase_mass) { Initialize(); infra::VerifyingFunctionMock ondone; @@ -351,8 +365,8 @@ TEST_F(StUartBootloaderCommandHandlerTest, ExtendedErase_pages) std::array, 4> pages = { 0x0001, 0x0002, 0x0003, 0x0004 }; ExpectSendData(0x44, 0xbb); - handler.ExtendedErase(4, infra::MakeByteRange(pages), ondone); - ExpectSendData({ 0x00, 0x05, 0x00, 0x01, 0x00, 0x02, 0x00, 0x03, 0x00, 0x04 }, 0x01); + handler.ExtendedErase(infra::MakeByteRange(pages), ondone); + ExpectSendData({ 0x00, 0x03 }, { 0x00, 0x01, 0x00, 0x02, 0x00, 0x03, 0x00, 0x04 }, 0x07); ExpectReceiveData({ 0x79 }); ExpectReceiveData({ 0x79 }); } @@ -387,8 +401,7 @@ TEST_F(StUartBootloaderCommandHandlerTest, Special) ExpectSendData({ 0x00, 0x54 }, 0x54); ExpectReceiveData({ 0x79 }); - ExpectSendData({ 0x00, 0x04 }); - ExpectSendData({ 0x01, 0x02, 0x03, 0x04 }, 0x04); + ExpectSendData({ 0x00, 0x04 }, { 0x01, 0x02, 0x03, 0x04 }, 0x00); ExpectReceiveData({ 0x79 }); ExpectReceiveData({ 0x79 }); @@ -416,8 +429,7 @@ TEST_F(StUartBootloaderCommandHandlerTest, Special_empty_rxData) ExpectSendData({ 0x00, 0x54 }, 0x54); ExpectReceiveData({ 0x79 }); - ExpectSendData({ 0x00, 0x04 }); - ExpectSendData({ 0x01, 0x02, 0x03, 0x04 }, 0x04); + ExpectSendData({ 0x00, 0x04 }, { 0x01, 0x02, 0x03, 0x04 }, 0x00); ExpectReceiveData({ 0x79 }); ExpectReceiveData({ 0x79 }); @@ -445,8 +457,7 @@ TEST_F(StUartBootloaderCommandHandlerTest, Special_empty_rxStatus) ExpectSendData({ 0x00, 0x54 }, 0x54); ExpectReceiveData({ 0x79 }); - ExpectSendData({ 0x00, 0x04 }); - ExpectSendData({ 0x01, 0x02, 0x03, 0x04 }, 0x04); + ExpectSendData({ 0x00, 0x04 }, { 0x01, 0x02, 0x03, 0x04 }, 0x00); ExpectReceiveData({ 0x79 }); ExpectReceiveData({ 0x79 }); @@ -474,8 +485,7 @@ TEST_F(StUartBootloaderCommandHandlerTest, Special_empty_rxData_rxStatus) ExpectSendData({ 0x00, 0x54 }, 0x54); ExpectReceiveData({ 0x79 }); - ExpectSendData({ 0x00, 0x04 }); - ExpectSendData({ 0x01, 0x02, 0x03, 0x04 }, 0x04); + ExpectSendData({ 0x00, 0x04 }, { 0x01, 0x02, 0x03, 0x04 }, 0x00); ExpectReceiveData({ 0x79 }); ExpectReceiveData({ 0x79 }); @@ -502,7 +512,11 @@ TEST_F(StUartBootloaderCommandHandlerTest, Special_empty_txData) ExpectSendData({ 0x00, 0x54 }, 0x54); ExpectReceiveData({ 0x79 }); - ExpectSendData({ 0x00, 0x00 }); + ExpectSendData({ + 0x00, + 0x00, + }, + 0x00); ExpectReceiveData({ 0x79 }); ExpectReceiveData({ 0x79 }); @@ -544,12 +558,10 @@ TEST_F(StUartBootloaderCommandHandlerTest, ExtendedSpecial) ExpectSendData({ 0x00, 0x54 }, 0x54); ExpectReceiveData({ 0x79 }); - ExpectSendData({ 0x00, 0x04 }); - ExpectSendData({ 0x01, 0x02, 0x03, 0x04 }, 0x04); + ExpectSendData({ 0x00, 0x04 }, { 0x01, 0x02, 0x03, 0x04 }, 0x00); ExpectReceiveData({ 0x79 }); - ExpectSendData({ 0x00, 0x04 }); - ExpectSendData({ 0x05, 0x06, 0x07, 0x08 }, 0x0c); + ExpectSendData({ 0x00, 0x04 }, { 0x05, 0x06, 0x07, 0x08 }, 0x08); ExpectReceiveData({ 0x79 }); ExpectReceiveData({ 0x79 }); From df793877edcc2ec66822d632d5d49b86b3edc0c5 Mon Sep 17 00:00:00 2001 From: Rob Ekelmans Date: Mon, 30 Oct 2023 10:52:22 +0000 Subject: [PATCH 11/21] add ack to end of special command --- services/st_util/StUartBootloaderCommandHandler.cpp | 6 +++++- services/st_util/StUartBootloaderCommandHandler.hpp | 2 +- .../st_util/test/TestStUartBootloaderCommandHandler.cpp | 6 ++++++ 3 files changed, 12 insertions(+), 2 deletions(-) diff --git a/services/st_util/StUartBootloaderCommandHandler.cpp b/services/st_util/StUartBootloaderCommandHandler.cpp index 81e60f94..31a2302f 100644 --- a/services/st_util/StUartBootloaderCommandHandler.cpp +++ b/services/st_util/StUartBootloaderCommandHandler.cpp @@ -117,9 +117,11 @@ namespace services { this->address = address; + static auto range = infra::MakeConstByteRange(this->address); + AddCommand(readMemory); AddCommand(); - AddCommand(infra::MakeConstByteRange(this->address)); + AddCommand(range); AddCommand(); AddCommand(data.size() - 1); AddCommand(); @@ -226,6 +228,7 @@ namespace services AddCommand(); AddCommand(rxData); AddCommand(rxStatus); + AddCommand(); SetCommandTimeout("Timeout special command"); ExecuteCommand(onDone); @@ -247,6 +250,7 @@ namespace services AddCommand(txData2.size(), txData2); AddCommand(); AddCommand(rxData); + AddCommand(); SetCommandTimeout("Timeout extended special command"); ExecuteCommand(onDone); diff --git a/services/st_util/StUartBootloaderCommandHandler.hpp b/services/st_util/StUartBootloaderCommandHandler.hpp index b4d2d466..f1e56b14 100644 --- a/services/st_util/StUartBootloaderCommandHandler.hpp +++ b/services/st_util/StUartBootloaderCommandHandler.hpp @@ -179,7 +179,7 @@ namespace services private: infra::ConstByteRange data; - uint8_t checksum; + uint8_t checksum = 0; }; class TransmitSmallBuffer diff --git a/services/st_util/test/TestStUartBootloaderCommandHandler.cpp b/services/st_util/test/TestStUartBootloaderCommandHandler.cpp index 8f0911b0..d8093385 100644 --- a/services/st_util/test/TestStUartBootloaderCommandHandler.cpp +++ b/services/st_util/test/TestStUartBootloaderCommandHandler.cpp @@ -407,6 +407,7 @@ TEST_F(StUartBootloaderCommandHandlerTest, Special) ExpectReceiveData({ 0x00, 0x04, 0x05, 0x06, 0x07, 0x08 }); ExpectReceiveData({ 0x00, 0x04, 0x09, 0x0a, 0x0b, 0x0c }); + ExpectReceiveData({ 0x79 }); EXPECT_EQ((std::array{ 0x05, 0x06, 0x07, 0x08 }), rxDataBuffer); EXPECT_EQ((std::array{ 0x09, 0x0a, 0x0b, 0x0c }), rxStatusBuffer); @@ -435,6 +436,7 @@ TEST_F(StUartBootloaderCommandHandlerTest, Special_empty_rxData) ExpectReceiveData({ 0x00, 0x00 }); ExpectReceiveData({ 0x00, 0x04, 0x09, 0x0a, 0x0b, 0x0c }); + ExpectReceiveData({ 0x79 }); EXPECT_EQ((std::array{}), rxDataBuffer); EXPECT_EQ((std::array{ 0x09, 0x0a, 0x0b, 0x0c }), rxStatusBuffer); @@ -463,6 +465,7 @@ TEST_F(StUartBootloaderCommandHandlerTest, Special_empty_rxStatus) ExpectReceiveData({ 0x00, 0x04, 0x05, 0x06, 0x07, 0x08 }); ExpectReceiveData({ 0x00, 0x00 }); + ExpectReceiveData({ 0x79 }); EXPECT_EQ((std::array{ 0x05, 0x06, 0x07, 0x08 }), rxDataBuffer); EXPECT_EQ((std::array{}), rxStatusBuffer); @@ -491,6 +494,7 @@ TEST_F(StUartBootloaderCommandHandlerTest, Special_empty_rxData_rxStatus) ExpectReceiveData({ 0x00, 0x00 }); ExpectReceiveData({ 0x00, 0x00 }); + ExpectReceiveData({ 0x79 }); EXPECT_EQ((std::array{}), rxDataBuffer); EXPECT_EQ((std::array{}), rxStatusBuffer); @@ -522,6 +526,7 @@ TEST_F(StUartBootloaderCommandHandlerTest, Special_empty_txData) ExpectReceiveData({ 0x00, 0x04, 0x05, 0x06, 0x07, 0x08 }); ExpectReceiveData({ 0x00, 0x04, 0x09, 0x0a, 0x0b, 0x0c }); + ExpectReceiveData({ 0x79 }); EXPECT_EQ((std::array{ 0x05, 0x06, 0x07, 0x08 }), rxDataBuffer); EXPECT_EQ((std::array{ 0x09, 0x0a, 0x0b, 0x0c }), rxStatusBuffer); @@ -566,6 +571,7 @@ TEST_F(StUartBootloaderCommandHandlerTest, ExtendedSpecial) ExpectReceiveData({ 0x79 }); ExpectReceiveData({ 0x00, 0x04, 0x09, 0x0a, 0x0b, 0x0c }); + ExpectReceiveData({ 0x79 }); EXPECT_EQ((std::array{ 0x09, 0x0a, 0x0b, 0x0c }), rxDataBuffer); } From 8c094e791a3d7e508d8d68487ea5333f2dd094cd Mon Sep 17 00:00:00 2001 From: Rob Ekelmans Date: Mon, 30 Oct 2023 13:35:08 +0000 Subject: [PATCH 12/21] Process review comments --- .../StUartBootloaderCommandHandler.cpp | 98 +++++++------------ .../StUartBootloaderCommandHandler.hpp | 60 ++++-------- .../TestStUartBootloaderCommandHandler.cpp | 6 +- 3 files changed, 56 insertions(+), 108 deletions(-) diff --git a/services/st_util/StUartBootloaderCommandHandler.cpp b/services/st_util/StUartBootloaderCommandHandler.cpp index 31a2302f..b9658bdd 100644 --- a/services/st_util/StUartBootloaderCommandHandler.cpp +++ b/services/st_util/StUartBootloaderCommandHandler.cpp @@ -1,5 +1,6 @@ #include "services/st_util/StUartBootloaderCommandHandler.hpp" #include "infra/util/ByteRange.hpp" +#include "infra/util/Function.hpp" #include "infra/util/ReallyAssert.hpp" #include @@ -117,11 +118,9 @@ namespace services { this->address = address; - static auto range = infra::MakeConstByteRange(this->address); - AddCommand(readMemory); AddCommand(); - AddCommand(range); + AddCommand(infra::MakeConstByteRange(this->address)); AddCommand(); AddCommand(data.size() - 1); AddCommand(); @@ -277,21 +276,13 @@ namespace services void StUartBootloaderCommandHandler::ExecuteCommand(const infra::Function) + sizeof(infra::ByteRange)>& onCommandExecuted) { this->onCommandExecuted = onCommandExecuted; - TryHandleTransmitAction(); + StartAction(); } - // Remove the aborts for each action, and leave them empty. - // Get rid of ActionType - void StUartBootloaderCommandHandler::TryHandleTransmitAction() + void StUartBootloaderCommandHandler::StartAction() { - if (commandActions.front()->GetActionType() == ActionType::transmit) - { - static auto transmitaction = commandActions.front(); - transmitaction = commandActions.front(); - - commandActions.pop_front(); - transmitaction->SendData(); - } + commandActions.front()->Start(); + TryHandleDataReceived(); } void StUartBootloaderCommandHandler::TryHandleDataReceived() @@ -300,21 +291,20 @@ namespace services commandActions.front()->DataReceived(); } - void StUartBootloaderCommandHandler::TryHandleNextAction() + void StUartBootloaderCommandHandler::OnCommandExecuted() + { + timeout.Cancel(); + onCommandExecuted(); + } + + void StUartBootloaderCommandHandler::OnActionExecuted() { commandActions.pop_front(); if (commandActions.empty()) - { - timeout.Cancel(); - onCommandExecuted(); - return; - } - - if (commandActions.front()->GetActionType() == ActionType::receive) - TryHandleDataReceived(); + OnCommandExecuted(); else - TryHandleTransmitAction(); + StartAction(); } void StUartBootloaderCommandHandler::SendData(infra::ConstByteRange data, uint8_t checksum) @@ -329,7 +319,7 @@ namespace services { serial.SendData(data, [this]() { - TryHandleTransmitAction(); + OnActionExecuted(); }); } @@ -350,32 +340,14 @@ namespace services onError(reason); } - StUartBootloaderCommandHandler::Action::Action(StUartBootloaderCommandHandler& handler, StUartBootloaderCommandHandler::ActionType type) + StUartBootloaderCommandHandler::Action::Action(StUartBootloaderCommandHandler& handler) : handler(handler) - , type(type) {} - void StUartBootloaderCommandHandler::Action::SendData() - { - std::abort(); - } - - void StUartBootloaderCommandHandler::Action::DataReceived() - { - std::abort(); - } - - StUartBootloaderCommandHandler::ActionType StUartBootloaderCommandHandler::Action::GetActionType() const - { - return type; - } - - StUartBootloaderCommandHandler::ReceiveAction::ReceiveAction(StUartBootloaderCommandHandler& handler) - : StUartBootloaderCommandHandler::Action(handler, StUartBootloaderCommandHandler::ActionType::receive) + void StUartBootloaderCommandHandler::Action::Start() {} - StUartBootloaderCommandHandler::TransmitAction::TransmitAction(StUartBootloaderCommandHandler& handler) - : StUartBootloaderCommandHandler::Action(handler, StUartBootloaderCommandHandler::ActionType::transmit) + void StUartBootloaderCommandHandler::Action::DataReceived() {} void StUartBootloaderCommandHandler::ReceiveAckAction::DataReceived() @@ -383,18 +355,18 @@ namespace services auto byte = handler.queue.Get(); if (byte == ack) - handler.TryHandleNextAction(); + handler.OnActionExecuted(); else handler.OnError("NACK received"); } StUartBootloaderCommandHandler::ReceiveBufferAction::ReceiveBufferAction(StUartBootloaderCommandHandler& handler, infra::ByteRange& data) - : StUartBootloaderCommandHandler::ReceiveAction(handler) + : StUartBootloaderCommandHandler::Action(handler) , data(data) {} StUartBootloaderCommandHandler::ReceiveBufferAction::ReceiveBufferAction(StUartBootloaderCommandHandler& handler, infra::ByteRange& data, const std::size_t nBytesTotal) - : StUartBootloaderCommandHandler::ReceiveAction(handler) + : StUartBootloaderCommandHandler::Action(handler) , data(data) { this->nBytesTotal.Emplace(nBytesTotal); @@ -402,6 +374,7 @@ namespace services void StUartBootloaderCommandHandler::ReceiveBufferAction::DataReceived() { + // add test case for wraparound infra::ByteInputStream stream(handler.queue.ContiguousRange()); if (!nBytesTotal) @@ -415,10 +388,12 @@ namespace services if (nBytesReceived == nBytesTotal) { data.shrink_from_back_to(nBytesReceived); - handler.TryHandleNextAction(); + handler.OnActionExecuted(); } } + // ReceivePredefinedBuffer + void StUartBootloaderCommandHandler::ReceiveSmallBufferAction::ExtractNumberOfBytes(infra::ByteInputStream& stream) { nBytesTotal.Emplace(stream.Extract() + 1); @@ -427,33 +402,34 @@ namespace services void StUartBootloaderCommandHandler::ReceiveBigBufferAction::ExtractNumberOfBytes(infra::ByteInputStream& stream) { + // 2 bytes received? nBytesTotal.Emplace(stream.Extract>()); handler.queue.Consume(sizeof(uint16_t)); } StUartBootloaderCommandHandler::TransmitRawAction::TransmitRawAction(StUartBootloaderCommandHandler& handler, infra::ConstByteRange data) - : StUartBootloaderCommandHandler::TransmitAction(handler) + : StUartBootloaderCommandHandler::Action(handler) , data(data) {} - void StUartBootloaderCommandHandler::TransmitRawAction::SendData() + void StUartBootloaderCommandHandler::TransmitRawAction::Start() { handler.SendData(data); } StUartBootloaderCommandHandler::TransmitWithTwosComplementChecksum::TransmitWithTwosComplementChecksum(StUartBootloaderCommandHandler& handler, uint8_t data) - : StUartBootloaderCommandHandler::TransmitAction(handler) + : StUartBootloaderCommandHandler::Action(handler) , data(data) {} - void StUartBootloaderCommandHandler::TransmitWithTwosComplementChecksum::SendData() + void StUartBootloaderCommandHandler::TransmitWithTwosComplementChecksum::Start() { auto checksum = data ^ 0xff; handler.SendData(infra::MakeConstByteRange(data), checksum); } StUartBootloaderCommandHandler::TransmitChecksummedBuffer::TransmitChecksummedBuffer(StUartBootloaderCommandHandler& handler, infra::ConstByteRange data) - : StUartBootloaderCommandHandler::TransmitAction(handler) + : StUartBootloaderCommandHandler::Action(handler) , data(data) { for (auto& b : data) @@ -465,7 +441,7 @@ namespace services checksum ^= byte; } - void StUartBootloaderCommandHandler::TransmitChecksummedBuffer::SendData() + void StUartBootloaderCommandHandler::TransmitChecksummedBuffer::Start() { if (data.empty()) handler.SendData(infra::MakeConstByteRange(checksum)); @@ -480,11 +456,11 @@ namespace services AddToChecksum(size); } - void StUartBootloaderCommandHandler::TransmitSmallBuffer::SendData() + void StUartBootloaderCommandHandler::TransmitSmallBuffer::Start() { handler.serial.SendData(infra::MakeConstByteRange(size), [this]() { - StUartBootloaderCommandHandler::TransmitChecksummedBuffer::SendData(); + StUartBootloaderCommandHandler::TransmitChecksummedBuffer::Start(); }); } @@ -496,11 +472,11 @@ namespace services AddToChecksum(static_cast(size & 0xff)); } - void StUartBootloaderCommandHandler::TransmitBigBuffer::SendData() + void StUartBootloaderCommandHandler::TransmitBigBuffer::Start() { handler.serial.SendData(infra::MakeConstByteRange(size), [this]() { - StUartBootloaderCommandHandler::TransmitChecksummedBuffer::SendData(); + StUartBootloaderCommandHandler::TransmitChecksummedBuffer::Start(); }); } } diff --git a/services/st_util/StUartBootloaderCommandHandler.hpp b/services/st_util/StUartBootloaderCommandHandler.hpp index f1e56b14..82140b3d 100644 --- a/services/st_util/StUartBootloaderCommandHandler.hpp +++ b/services/st_util/StUartBootloaderCommandHandler.hpp @@ -48,59 +48,42 @@ namespace services void AddCommand(Args&&... args); void ExecuteCommand(const infra::Function) + sizeof(infra::ByteRange)>& onCommandExecuted); - void TryHandleTransmitAction(); + void StartAction(); void TryHandleDataReceived(); - void TryHandleNextAction(); + void OnCommandExecuted(); + void OnActionExecuted(); void SendData(infra::ConstByteRange data, uint8_t checksum); void SendData(infra::ConstByteRange data); void SetCommandTimeout(infra::BoundedConstString reason); void OnError(infra::BoundedConstString reason); private: - enum class ActionType : uint8_t - { - receive, - transmit, - }; - class Action { public: - Action(StUartBootloaderCommandHandler& handler, ActionType type); - Action(const Action& other) = default; - Action& operator=(const Action& other) = default; + Action(StUartBootloaderCommandHandler& handler); + Action(const Action& other) = delete; + Action& operator=(const Action& other) = delete; virtual ~Action() = default; - virtual void SendData(); + virtual void Start(); virtual void DataReceived(); - ActionType GetActionType() const; - protected: StUartBootloaderCommandHandler& handler; - - private: - ActionType type; - }; - - class ReceiveAction - : public Action - { - public: - explicit ReceiveAction(StUartBootloaderCommandHandler& handler); }; class ReceiveAckAction - : public ReceiveAction + : public Action { public: - using ReceiveAction::ReceiveAction; + using Action::Action; void DataReceived() override; }; class ReceiveBufferAction - : public ReceiveAction + : public Action { public: ReceiveBufferAction(StUartBootloaderCommandHandler& handler, infra::ByteRange& data); @@ -137,45 +120,38 @@ namespace services void ExtractNumberOfBytes(infra::ByteInputStream& stream) override; }; - class TransmitAction - : public Action - { - public: - explicit TransmitAction(StUartBootloaderCommandHandler& handler); - }; - class TransmitRawAction - : public TransmitAction + : public Action { public: TransmitRawAction(StUartBootloaderCommandHandler& handler, infra::ConstByteRange data); - void SendData() override; + void Start() override; private: infra::ConstByteRange data; }; class TransmitWithTwosComplementChecksum - : public TransmitAction + : public Action { public: TransmitWithTwosComplementChecksum(StUartBootloaderCommandHandler& handler, uint8_t data); - void SendData() override; + void Start() override; private: uint8_t data; }; class TransmitChecksummedBuffer - : public TransmitAction + : public Action { public: TransmitChecksummedBuffer(StUartBootloaderCommandHandler& handler, infra::ConstByteRange data); void AddToChecksum(const uint8_t& byte); - void SendData() override; + void Start() override; private: infra::ConstByteRange data; @@ -188,7 +164,7 @@ namespace services public: TransmitSmallBuffer(StUartBootloaderCommandHandler& handler, infra::ConstByteRange data); - void SendData() override; + void Start() override; private: uint8_t size; @@ -200,7 +176,7 @@ namespace services public: TransmitBigBuffer(StUartBootloaderCommandHandler& handler, uint16_t size, infra::ConstByteRange data); - void SendData() override; + void Start() override; private: infra::BigEndian size; diff --git a/services/st_util/test/TestStUartBootloaderCommandHandler.cpp b/services/st_util/test/TestStUartBootloaderCommandHandler.cpp index d8093385..e271339b 100644 --- a/services/st_util/test/TestStUartBootloaderCommandHandler.cpp +++ b/services/st_util/test/TestStUartBootloaderCommandHandler.cpp @@ -516,11 +516,7 @@ TEST_F(StUartBootloaderCommandHandlerTest, Special_empty_txData) ExpectSendData({ 0x00, 0x54 }, 0x54); ExpectReceiveData({ 0x79 }); - ExpectSendData({ - 0x00, - 0x00, - }, - 0x00); + ExpectSendData({ 0x00, 0x00 }, 0x00); ExpectReceiveData({ 0x79 }); ExpectReceiveData({ 0x79 }); From 02cca4deaf67f961e5520a9279fdf27cd1b1a64d Mon Sep 17 00:00:00 2001 From: Rob Ekelmans Date: Mon, 30 Oct 2023 13:44:47 +0000 Subject: [PATCH 13/21] Add ReceivePredefinedBuffer Action --- .../StUartBootloaderCommandHandler.cpp | 21 ++++++++++--------- .../StUartBootloaderCommandHandler.hpp | 16 ++++++++++++-- 2 files changed, 25 insertions(+), 12 deletions(-) diff --git a/services/st_util/StUartBootloaderCommandHandler.cpp b/services/st_util/StUartBootloaderCommandHandler.cpp index b9658bdd..c86354bb 100644 --- a/services/st_util/StUartBootloaderCommandHandler.cpp +++ b/services/st_util/StUartBootloaderCommandHandler.cpp @@ -85,7 +85,7 @@ namespace services AddCommand(getVersion); AddCommand(); - AddCommand(internalRange, 3); + AddCommand(internalRange, 3); AddCommand(); SetCommandTimeout("Timeout getting version"); @@ -124,7 +124,7 @@ namespace services AddCommand(); AddCommand(data.size() - 1); AddCommand(); - AddCommand(data, data.size()); + AddCommand(data, data.size()); SetCommandTimeout("Timeout reading memory"); ExecuteCommand(onDone); @@ -365,13 +365,6 @@ namespace services , data(data) {} - StUartBootloaderCommandHandler::ReceiveBufferAction::ReceiveBufferAction(StUartBootloaderCommandHandler& handler, infra::ByteRange& data, const std::size_t nBytesTotal) - : StUartBootloaderCommandHandler::Action(handler) - , data(data) - { - this->nBytesTotal.Emplace(nBytesTotal); - } - void StUartBootloaderCommandHandler::ReceiveBufferAction::DataReceived() { // add test case for wraparound @@ -392,7 +385,15 @@ namespace services } } - // ReceivePredefinedBuffer + StUartBootloaderCommandHandler::ReceivePredefinedBuffer::ReceivePredefinedBuffer(StUartBootloaderCommandHandler& handler, infra::ByteRange& data, const std::size_t size) + : StUartBootloaderCommandHandler::ReceiveBufferAction(handler, data) + , size(size) + {} + + void StUartBootloaderCommandHandler::ReceivePredefinedBuffer::ExtractNumberOfBytes([[maybe_unused]] infra::ByteInputStream& stream) + { + nBytesTotal.Emplace(size); + } void StUartBootloaderCommandHandler::ReceiveSmallBufferAction::ExtractNumberOfBytes(infra::ByteInputStream& stream) { diff --git a/services/st_util/StUartBootloaderCommandHandler.hpp b/services/st_util/StUartBootloaderCommandHandler.hpp index 82140b3d..bbe21e50 100644 --- a/services/st_util/StUartBootloaderCommandHandler.hpp +++ b/services/st_util/StUartBootloaderCommandHandler.hpp @@ -87,7 +87,6 @@ namespace services { public: ReceiveBufferAction(StUartBootloaderCommandHandler& handler, infra::ByteRange& data); - ReceiveBufferAction(StUartBootloaderCommandHandler& handler, infra::ByteRange& data, const std::size_t nBytesTotal); void DataReceived() override; @@ -100,6 +99,19 @@ namespace services std::size_t nBytesReceived = 0; }; + class ReceivePredefinedBuffer + : public ReceiveBufferAction + { + public: + ReceivePredefinedBuffer(StUartBootloaderCommandHandler& handler, infra::ByteRange& data, const std::size_t size); + + protected: + void ExtractNumberOfBytes(infra::ByteInputStream& stream) override; + + private: + const std::size_t size; + }; + class ReceiveSmallBufferAction : public ReceiveBufferAction { @@ -188,7 +200,7 @@ namespace services infra::AutoResetFunction onError; infra::QueueForOneReaderOneIrqWriter::WithStorage<257> queue; - infra::BoundedDeque>::WithMaxSize<12> commandActions; + infra::BoundedDeque>::WithMaxSize<12> commandActions; infra::AutoResetFunction) + sizeof(infra::ByteRange)> onCommandExecuted; infra::BoundedString::WithStorage<46> timeoutReason; infra::TimerSingleShot timeout; From 8c85307cfbfe0c727dd697409ad3656742919515 Mon Sep 17 00:00:00 2001 From: Rob Ekelmans Date: Mon, 30 Oct 2023 15:46:01 +0000 Subject: [PATCH 14/21] Process PR comments --- .../StUartBootloaderCommandHandler.cpp | 163 +++++++++--------- .../StUartBootloaderCommandHandler.hpp | 43 ++--- .../TestStUartBootloaderCommandHandler.cpp | 45 +++++ 3 files changed, 142 insertions(+), 109 deletions(-) diff --git a/services/st_util/StUartBootloaderCommandHandler.cpp b/services/st_util/StUartBootloaderCommandHandler.cpp index c86354bb..36987b65 100644 --- a/services/st_util/StUartBootloaderCommandHandler.cpp +++ b/services/st_util/StUartBootloaderCommandHandler.cpp @@ -1,8 +1,4 @@ #include "services/st_util/StUartBootloaderCommandHandler.hpp" -#include "infra/util/ByteRange.hpp" -#include "infra/util/Function.hpp" -#include "infra/util/ReallyAssert.hpp" -#include namespace { @@ -58,10 +54,10 @@ namespace services { really_assert(commands.size() >= 15); - AddCommand(getCommand); - AddCommand(); - AddCommand(commands); - AddCommand(); + AddCommandAction(getCommand); + AddCommandAction(); + AddCommandAction(commands); + AddCommandAction(); SetCommandTimeout("Timeout getting commands"); ExecuteCommand([this, onDone, &commands]() @@ -83,10 +79,10 @@ namespace services { internalRange = infra::MakeByteRange(internalBuffer); - AddCommand(getVersion); - AddCommand(); - AddCommand(internalRange, 3); - AddCommand(); + AddCommandAction(getVersion); + AddCommandAction(); + AddCommandAction(internalRange, 3); + AddCommandAction(); SetCommandTimeout("Timeout getting version"); ExecuteCommand([this, onDone]() @@ -101,10 +97,10 @@ namespace services { internalRange = infra::MakeByteRange(internalBuffer); - AddCommand(getId); - AddCommand(); - AddCommand(internalRange); - AddCommand(); + AddCommandAction(getId); + AddCommandAction(); + AddCommandAction(internalRange); + AddCommandAction(); SetCommandTimeout("Timeout getting id"); ExecuteCommand([this, onDone]() @@ -118,13 +114,13 @@ namespace services { this->address = address; - AddCommand(readMemory); - AddCommand(); - AddCommand(infra::MakeConstByteRange(this->address)); - AddCommand(); - AddCommand(data.size() - 1); - AddCommand(); - AddCommand(data, data.size()); + AddCommandAction(readMemory); + AddCommandAction(); + AddCommandAction(infra::MakeConstByteRange(this->address)); + AddCommandAction(); + AddCommandAction(data.size() - 1); + AddCommandAction(); + AddCommandAction(data, data.size()); SetCommandTimeout("Timeout reading memory"); ExecuteCommand(onDone); @@ -134,10 +130,10 @@ namespace services { this->address = address; - AddCommand(go); - AddCommand(); - AddCommand(infra::MakeConstByteRange(this->address)); - AddCommand(); + AddCommandAction(go); + AddCommandAction(); + AddCommandAction(infra::MakeConstByteRange(this->address)); + AddCommandAction(); SetCommandTimeout("Timeout go"); ExecuteCommand(onDone); @@ -149,12 +145,12 @@ namespace services this->address = address; - AddCommand(writeMemory); - AddCommand(); - AddCommand(infra::MakeConstByteRange(this->address)); - AddCommand(); - AddCommand(data); - AddCommand(); + AddCommandAction(writeMemory); + AddCommandAction(); + AddCommandAction(infra::MakeConstByteRange(this->address)); + AddCommandAction(); + AddCommandAction(data); + AddCommandAction(); SetCommandTimeout("Timeout writing memory"); ExecuteCommand(onDone); @@ -164,10 +160,10 @@ namespace services { constexpr uint8_t globalEraseSubCommand = 0xff; - AddCommand(erase); - AddCommand(); - AddCommand(globalEraseSubCommand); - AddCommand(); + AddCommandAction(erase); + AddCommandAction(); + AddCommandAction(globalEraseSubCommand); + AddCommandAction(); SetCommandTimeout("Timeout erasing memory"); ExecuteCommand(onDone); @@ -177,10 +173,10 @@ namespace services { really_assert(pages.size() <= 255); - AddCommand(erase); - AddCommand(); - AddCommand(pages); - AddCommand(); + AddCommandAction(erase); + AddCommandAction(); + AddCommandAction(pages); + AddCommandAction(); SetCommandTimeout("Timeout erasing memory"); ExecuteCommand(onDone); @@ -190,10 +186,10 @@ namespace services { this->subcommand = static_cast(subcommand); - AddCommand(extendedErase); - AddCommand(); - AddCommand(infra::MakeConstByteRange(this->subcommand)); - AddCommand(); + AddCommandAction(extendedErase); + AddCommandAction(); + AddCommandAction(infra::MakeConstByteRange(this->subcommand)); + AddCommandAction(); SetCommandTimeout("Timeout extended erasing memory"); ExecuteCommand(onDone); @@ -204,10 +200,10 @@ namespace services really_assert(pages.size() <= 256); really_assert(pages.size() % 2 == 0); - AddCommand(extendedErase); - AddCommand(); - AddCommand((pages.size() / 2) - 1, pages); - AddCommand(); + AddCommandAction(extendedErase); + AddCommandAction(); + AddCommandAction((pages.size() / 2) - 1, pages); + AddCommandAction(); SetCommandTimeout("Timeout extended erasing memory"); ExecuteCommand(onDone); @@ -219,15 +215,15 @@ namespace services this->subcommand = subcommand; - AddCommand(special); - AddCommand(); - AddCommand(infra::MakeByteRange(this->subcommand)); - AddCommand(); - AddCommand(txData.size(), txData); - AddCommand(); - AddCommand(rxData); - AddCommand(rxStatus); - AddCommand(); + AddCommandAction(special); + AddCommandAction(); + AddCommandAction(infra::MakeByteRange(this->subcommand)); + AddCommandAction(); + AddCommandAction(txData.size(), txData); + AddCommandAction(); + AddCommandAction(rxData); + AddCommandAction(rxStatus); + AddCommandAction(); SetCommandTimeout("Timeout special command"); ExecuteCommand(onDone); @@ -240,16 +236,16 @@ namespace services this->subcommand = subcommand; - AddCommand(extendedSpecial); - AddCommand(); - AddCommand(infra::MakeByteRange(this->subcommand)); - AddCommand(); - AddCommand(txData1.size(), txData1); - AddCommand(); - AddCommand(txData2.size(), txData2); - AddCommand(); - AddCommand(rxData); - AddCommand(); + AddCommandAction(extendedSpecial); + AddCommandAction(); + AddCommandAction(infra::MakeByteRange(this->subcommand)); + AddCommandAction(); + AddCommandAction(txData1.size(), txData1); + AddCommandAction(); + AddCommandAction(txData2.size(), txData2); + AddCommandAction(); + AddCommandAction(rxData); + AddCommandAction(); SetCommandTimeout("Timeout extended special command"); ExecuteCommand(onDone); @@ -257,8 +253,8 @@ namespace services void StUartBootloaderCommandHandler::InitializeUartBootloader() { - AddCommand(infra::MakeByteRange(uartInitializationData)); - AddCommand(); + AddCommandAction(infra::MakeByteRange(uartInitializationData)); + AddCommandAction(); SetCommandTimeout("Timeout waiting for bootloader initialization"); ExecuteCommand([this]() @@ -268,7 +264,7 @@ namespace services } template - void StUartBootloaderCommandHandler::AddCommand(Args&&... args) + void StUartBootloaderCommandHandler::AddCommandAction(Args&&... args) { commandActions.emplace_back(infra::InPlaceType(), *this, std::forward(args)...); } @@ -350,7 +346,7 @@ namespace services void StUartBootloaderCommandHandler::Action::DataReceived() {} - void StUartBootloaderCommandHandler::ReceiveAckAction::DataReceived() + void StUartBootloaderCommandHandler::ReceiveAck::DataReceived() { auto byte = handler.queue.Get(); @@ -360,22 +356,23 @@ namespace services handler.OnError("NACK received"); } - StUartBootloaderCommandHandler::ReceiveBufferAction::ReceiveBufferAction(StUartBootloaderCommandHandler& handler, infra::ByteRange& data) + StUartBootloaderCommandHandler::ReceiveBuffer::ReceiveBuffer(StUartBootloaderCommandHandler& handler, infra::ByteRange& data) : StUartBootloaderCommandHandler::Action(handler) , data(data) {} - void StUartBootloaderCommandHandler::ReceiveBufferAction::DataReceived() + void StUartBootloaderCommandHandler::ReceiveBuffer::DataReceived() { // add test case for wraparound - infra::ByteInputStream stream(handler.queue.ContiguousRange()); + infra::QueueForOneReaderOneIrqWriter::StreamReader reader(handler.queue); + infra::DataInputStream::WithErrorPolicy stream(reader, infra::noFail); if (!nBytesTotal) ExtractNumberOfBytes(stream); auto buffer = infra::Head(infra::DiscardHead(data, nBytesReceived), std::min(stream.Available(), *nBytesTotal - nBytesReceived)); stream >> buffer; - handler.queue.Consume(buffer.size()); + reader.Commit(); nBytesReceived += buffer.size(); if (nBytesReceived == nBytesTotal) @@ -386,34 +383,32 @@ namespace services } StUartBootloaderCommandHandler::ReceivePredefinedBuffer::ReceivePredefinedBuffer(StUartBootloaderCommandHandler& handler, infra::ByteRange& data, const std::size_t size) - : StUartBootloaderCommandHandler::ReceiveBufferAction(handler, data) + : StUartBootloaderCommandHandler::ReceiveBuffer(handler, data) , size(size) {} - void StUartBootloaderCommandHandler::ReceivePredefinedBuffer::ExtractNumberOfBytes([[maybe_unused]] infra::ByteInputStream& stream) + void StUartBootloaderCommandHandler::ReceivePredefinedBuffer::ExtractNumberOfBytes([[maybe_unused]] infra::DataInputStream& stream) { nBytesTotal.Emplace(size); } - void StUartBootloaderCommandHandler::ReceiveSmallBufferAction::ExtractNumberOfBytes(infra::ByteInputStream& stream) + void StUartBootloaderCommandHandler::ReceiveSmallBuffer::ExtractNumberOfBytes(infra::DataInputStream& stream) { nBytesTotal.Emplace(stream.Extract() + 1); - handler.queue.Consume(sizeof(uint8_t)); } - void StUartBootloaderCommandHandler::ReceiveBigBufferAction::ExtractNumberOfBytes(infra::ByteInputStream& stream) + void StUartBootloaderCommandHandler::ReceiveBigBuffer::ExtractNumberOfBytes(infra::DataInputStream& stream) { // 2 bytes received? nBytesTotal.Emplace(stream.Extract>()); - handler.queue.Consume(sizeof(uint16_t)); } - StUartBootloaderCommandHandler::TransmitRawAction::TransmitRawAction(StUartBootloaderCommandHandler& handler, infra::ConstByteRange data) + StUartBootloaderCommandHandler::TransmitRaw::TransmitRaw(StUartBootloaderCommandHandler& handler, infra::ConstByteRange data) : StUartBootloaderCommandHandler::Action(handler) , data(data) {} - void StUartBootloaderCommandHandler::TransmitRawAction::Start() + void StUartBootloaderCommandHandler::TransmitRaw::Start() { handler.SendData(data); } diff --git a/services/st_util/StUartBootloaderCommandHandler.hpp b/services/st_util/StUartBootloaderCommandHandler.hpp index bbe21e50..cd2ae382 100644 --- a/services/st_util/StUartBootloaderCommandHandler.hpp +++ b/services/st_util/StUartBootloaderCommandHandler.hpp @@ -3,20 +3,13 @@ #include "hal/interfaces/SerialCommunication.hpp" #include "infra/event/QueueForOneReaderOneIrqWriter.hpp" -#include "infra/stream/ByteInputStream.hpp" #include "infra/timer/Timer.hpp" #include "infra/util/AutoResetFunction.hpp" #include "infra/util/BoundedDeque.hpp" #include "infra/util/BoundedString.hpp" -#include "infra/util/BoundedVector.hpp" -#include "infra/util/ByteRange.hpp" #include "infra/util/Endian.hpp" -#include "infra/util/Function.hpp" -#include "infra/util/Optional.hpp" #include "infra/util/PolymorphicVariant.hpp" #include "services/st_util/StBootloaderCommandHandler.hpp" -#include -#include namespace services { @@ -45,7 +38,7 @@ namespace services void InitializeUartBootloader(); template - void AddCommand(Args&&... args); + void AddCommandAction(Args&&... args); void ExecuteCommand(const infra::Function) + sizeof(infra::ByteRange)>& onCommandExecuted); void StartAction(); @@ -73,7 +66,7 @@ namespace services StUartBootloaderCommandHandler& handler; }; - class ReceiveAckAction + class ReceiveAck : public Action { public: @@ -82,16 +75,16 @@ namespace services void DataReceived() override; }; - class ReceiveBufferAction + class ReceiveBuffer : public Action { public: - ReceiveBufferAction(StUartBootloaderCommandHandler& handler, infra::ByteRange& data); + ReceiveBuffer(StUartBootloaderCommandHandler& handler, infra::ByteRange& data); void DataReceived() override; protected: - virtual void ExtractNumberOfBytes(infra::ByteInputStream& stream) = 0; + virtual void ExtractNumberOfBytes(infra::DataInputStream& stream) = 0; protected: infra::ByteRange& data; @@ -100,43 +93,43 @@ namespace services }; class ReceivePredefinedBuffer - : public ReceiveBufferAction + : public ReceiveBuffer { public: ReceivePredefinedBuffer(StUartBootloaderCommandHandler& handler, infra::ByteRange& data, const std::size_t size); protected: - void ExtractNumberOfBytes(infra::ByteInputStream& stream) override; + void ExtractNumberOfBytes(infra::DataInputStream& stream) override; private: const std::size_t size; }; - class ReceiveSmallBufferAction - : public ReceiveBufferAction + class ReceiveSmallBuffer + : public ReceiveBuffer { public: - using ReceiveBufferAction::ReceiveBufferAction; + using ReceiveBuffer::ReceiveBuffer; protected: - void ExtractNumberOfBytes(infra::ByteInputStream& stream) override; + void ExtractNumberOfBytes(infra::DataInputStream& stream) override; }; - class ReceiveBigBufferAction - : public ReceiveBufferAction + class ReceiveBigBuffer + : public ReceiveBuffer { public: - using ReceiveBufferAction::ReceiveBufferAction; + using ReceiveBuffer::ReceiveBuffer; protected: - void ExtractNumberOfBytes(infra::ByteInputStream& stream) override; + void ExtractNumberOfBytes(infra::DataInputStream& stream) override; }; - class TransmitRawAction + class TransmitRaw : public Action { public: - TransmitRawAction(StUartBootloaderCommandHandler& handler, infra::ConstByteRange data); + TransmitRaw(StUartBootloaderCommandHandler& handler, infra::ConstByteRange data); void Start() override; @@ -200,7 +193,7 @@ namespace services infra::AutoResetFunction onError; infra::QueueForOneReaderOneIrqWriter::WithStorage<257> queue; - infra::BoundedDeque>::WithMaxSize<12> commandActions; + infra::BoundedDeque>::WithMaxSize<12> commandActions; infra::AutoResetFunction) + sizeof(infra::ByteRange)> onCommandExecuted; infra::BoundedString::WithStorage<46> timeoutReason; infra::TimerSingleShot timeout; diff --git a/services/st_util/test/TestStUartBootloaderCommandHandler.cpp b/services/st_util/test/TestStUartBootloaderCommandHandler.cpp index e271339b..22af89fb 100644 --- a/services/st_util/test/TestStUartBootloaderCommandHandler.cpp +++ b/services/st_util/test/TestStUartBootloaderCommandHandler.cpp @@ -1,9 +1,14 @@ #include "hal/interfaces/test_doubles/SerialCommunicationMock.hpp" #include "infra/timer/test_helper/ClockFixture.hpp" +#include "infra/util/MemoryRange.hpp" #include "infra/util/test_helper/MockCallback.hpp" #include "services/st_util/StUartBootloaderCommandHandler.hpp" #include "gmock/gmock.h" #include "gtest/gtest.h" +#include +#include +#include +#include class StUartBootloaderCommandHandlerTest : public testing::Test @@ -585,3 +590,43 @@ TEST_F(StUartBootloaderCommandHandlerTest, ExtendedSpecial_timeout) }); ExpectErrorOnTimeout(std::chrono::seconds(1), "Timeout extended special command"); } + +TEST_F(StUartBootloaderCommandHandlerTest, receive_data_with_wrapped_around_queue) +{ + Initialize(); + infra::VerifyingFunctionMock ondone; + uint16_t subcommand = 0x54; + + std::vector rxDataBuffer; + rxDataBuffer.resize(255, 0); + infra::ByteRange rxData(rxDataBuffer); + + std::array rxStatusBuffer; + infra::ByteRange rxStatus(rxStatusBuffer); + + ExpectSendData(0x50, 0xaf); + handler.Special(subcommand, infra::ConstByteRange{}, rxData, rxStatus, ondone); + + ExpectSendData({ 0x00, 0x54 }, 0x54); + ExpectReceiveData({ 0x79 }); + + ExpectSendData({ 0x00, 0x00 }, 0x00); + ExpectReceiveData({ 0x79 }); + ExpectReceiveData({ 0x79 }); + + std::vector rxDataSimulated; + rxDataSimulated.emplace_back(0x00); + rxDataSimulated.emplace_back(0xff); + rxDataSimulated.resize(256, 0xaa); + rxDataSimulated.emplace_back(0xbb); + + ExpectReceiveData(rxDataSimulated); + ExpectReceiveData({ 0x00, 0x04, 0x09, 0x0a, 0x0b, 0x0c }); + ExpectReceiveData({ 0x79 }); + + infra::ConstByteRange rxDataSimulatedRange(rxDataSimulated.data(), rxDataSimulated.data() + rxDataSimulated.size()); + rxDataSimulatedRange = infra::DiscardHead(rxDataSimulatedRange, 2); + EXPECT_TRUE(infra::ContentsEqual(rxDataSimulatedRange, rxData)); + + EXPECT_EQ((std::array{ 0x09, 0x0a, 0x0b, 0x0c }), rxStatusBuffer); +} From 91753366e18815a54ece773e34f552d3be725b16 Mon Sep 17 00:00:00 2001 From: Rob Ekelmans Date: Mon, 30 Oct 2023 16:21:04 +0000 Subject: [PATCH 15/21] Process PR comments --- .../st_util/StBootloaderCommandHandler.hpp | 4 +- .../StUartBootloaderCommandHandler.cpp | 26 ++++++++----- .../StUartBootloaderCommandHandler.hpp | 12 +++--- .../TestStUartBootloaderCommandHandler.cpp | 38 +++++++++++++++++-- 4 files changed, 58 insertions(+), 22 deletions(-) diff --git a/services/st_util/StBootloaderCommandHandler.hpp b/services/st_util/StBootloaderCommandHandler.hpp index 58b4b28d..f1fe81d3 100644 --- a/services/st_util/StBootloaderCommandHandler.hpp +++ b/services/st_util/StBootloaderCommandHandler.hpp @@ -27,9 +27,9 @@ namespace services virtual void ReadMemory(uint32_t address, infra::ByteRange& data, const infra::Function& onDone) = 0; virtual void Go(uint32_t address, const infra::Function& onDone) = 0; virtual void WriteMemory(uint32_t address, infra::ConstByteRange data, const infra::Function& onDone) = 0; - virtual void GlobalErase(const infra::Function& onDone) = 0; + virtual void MassErase(const infra::Function& onDone) = 0; virtual void Erase(infra::ConstByteRange pages, const infra::Function& onDone) = 0; - virtual void ExtendedErase(MassEraseSubcommand subcommand, const infra::Function& onDone) = 0; + virtual void ExtendedMassErase(MassEraseSubcommand subcommand, const infra::Function& onDone) = 0; virtual void ExtendedErase(infra::ConstByteRange pages, const infra::Function& onDone) = 0; virtual void Special(uint16_t subcommand, infra::ConstByteRange txData, infra::ByteRange& rxData, infra::ByteRange& rxStatus, const infra::Function& onDone) = 0; virtual void ExtendedSpecial(uint16_t subcommand, infra::ConstByteRange txData1, infra::ConstByteRange txData2, infra::ByteRange& rxData, const infra::Function& onDone) = 0; diff --git a/services/st_util/StUartBootloaderCommandHandler.cpp b/services/st_util/StUartBootloaderCommandHandler.cpp index 36987b65..98977cd8 100644 --- a/services/st_util/StUartBootloaderCommandHandler.cpp +++ b/services/st_util/StUartBootloaderCommandHandler.cpp @@ -1,4 +1,7 @@ #include "services/st_util/StUartBootloaderCommandHandler.hpp" +#include "infra/util/Optional.hpp" +#include +#include namespace { @@ -156,7 +159,7 @@ namespace services ExecuteCommand(onDone); } - void StUartBootloaderCommandHandler::GlobalErase(const infra::Function& onDone) + void StUartBootloaderCommandHandler::MassErase(const infra::Function& onDone) { constexpr uint8_t globalEraseSubCommand = 0xff; @@ -182,7 +185,7 @@ namespace services ExecuteCommand(onDone); } - void StUartBootloaderCommandHandler::ExtendedErase(MassEraseSubcommand subcommand, const infra::Function& onDone) + void StUartBootloaderCommandHandler::ExtendedMassErase(MassEraseSubcommand subcommand, const infra::Function& onDone) { this->subcommand = static_cast(subcommand); @@ -363,12 +366,15 @@ namespace services void StUartBootloaderCommandHandler::ReceiveBuffer::DataReceived() { - // add test case for wraparound infra::QueueForOneReaderOneIrqWriter::StreamReader reader(handler.queue); infra::DataInputStream::WithErrorPolicy stream(reader, infra::noFail); - if (!nBytesTotal) - ExtractNumberOfBytes(stream); + if (nBytesTotal == infra::none) + { + TryExtractNumberOfBytes(stream); + if (nBytesTotal == infra::none) + return; + } auto buffer = infra::Head(infra::DiscardHead(data, nBytesReceived), std::min(stream.Available(), *nBytesTotal - nBytesReceived)); stream >> buffer; @@ -387,20 +393,20 @@ namespace services , size(size) {} - void StUartBootloaderCommandHandler::ReceivePredefinedBuffer::ExtractNumberOfBytes([[maybe_unused]] infra::DataInputStream& stream) + void StUartBootloaderCommandHandler::ReceivePredefinedBuffer::TryExtractNumberOfBytes([[maybe_unused]] infra::DataInputStream& stream) { nBytesTotal.Emplace(size); } - void StUartBootloaderCommandHandler::ReceiveSmallBuffer::ExtractNumberOfBytes(infra::DataInputStream& stream) + void StUartBootloaderCommandHandler::ReceiveSmallBuffer::TryExtractNumberOfBytes(infra::DataInputStream& stream) { nBytesTotal.Emplace(stream.Extract() + 1); } - void StUartBootloaderCommandHandler::ReceiveBigBuffer::ExtractNumberOfBytes(infra::DataInputStream& stream) + void StUartBootloaderCommandHandler::ReceiveBigBuffer::TryExtractNumberOfBytes(infra::DataInputStream& stream) { - // 2 bytes received? - nBytesTotal.Emplace(stream.Extract>()); + if (stream.Available() >= sizeof(uint16_t)) + nBytesTotal.Emplace(stream.Extract>()); } StUartBootloaderCommandHandler::TransmitRaw::TransmitRaw(StUartBootloaderCommandHandler& handler, infra::ConstByteRange data) diff --git a/services/st_util/StUartBootloaderCommandHandler.hpp b/services/st_util/StUartBootloaderCommandHandler.hpp index cd2ae382..3778c58f 100644 --- a/services/st_util/StUartBootloaderCommandHandler.hpp +++ b/services/st_util/StUartBootloaderCommandHandler.hpp @@ -27,9 +27,9 @@ namespace services void ReadMemory(uint32_t address, infra::ByteRange& data, const infra::Function& onDone) override; void Go(uint32_t address, const infra::Function& onDone) override; void WriteMemory(uint32_t address, infra::ConstByteRange data, const infra::Function& onDone) override; - void GlobalErase(const infra::Function& onDone) override; + void MassErase(const infra::Function& onDone) override; void Erase(infra::ConstByteRange pages, const infra::Function& onDone) override; - void ExtendedErase(MassEraseSubcommand subcommand, const infra::Function& onDone) override; + void ExtendedMassErase(MassEraseSubcommand subcommand, const infra::Function& onDone) override; void ExtendedErase(infra::ConstByteRange pages, const infra::Function& onDone) override; void Special(uint16_t subcommand, infra::ConstByteRange txData, infra::ByteRange& rxData, infra::ByteRange& rxStatus, const infra::Function& onDone) override; void ExtendedSpecial(uint16_t subcommand, infra::ConstByteRange txData1, infra::ConstByteRange txData2, infra::ByteRange& rxData, const infra::Function& onDone) override; @@ -84,7 +84,7 @@ namespace services void DataReceived() override; protected: - virtual void ExtractNumberOfBytes(infra::DataInputStream& stream) = 0; + virtual void TryExtractNumberOfBytes(infra::DataInputStream& stream) = 0; protected: infra::ByteRange& data; @@ -99,7 +99,7 @@ namespace services ReceivePredefinedBuffer(StUartBootloaderCommandHandler& handler, infra::ByteRange& data, const std::size_t size); protected: - void ExtractNumberOfBytes(infra::DataInputStream& stream) override; + void TryExtractNumberOfBytes(infra::DataInputStream& stream) override; private: const std::size_t size; @@ -112,7 +112,7 @@ namespace services using ReceiveBuffer::ReceiveBuffer; protected: - void ExtractNumberOfBytes(infra::DataInputStream& stream) override; + void TryExtractNumberOfBytes(infra::DataInputStream& stream) override; }; class ReceiveBigBuffer @@ -122,7 +122,7 @@ namespace services using ReceiveBuffer::ReceiveBuffer; protected: - void ExtractNumberOfBytes(infra::DataInputStream& stream) override; + void TryExtractNumberOfBytes(infra::DataInputStream& stream) override; }; class TransmitRaw diff --git a/services/st_util/test/TestStUartBootloaderCommandHandler.cpp b/services/st_util/test/TestStUartBootloaderCommandHandler.cpp index 22af89fb..bbf80c1f 100644 --- a/services/st_util/test/TestStUartBootloaderCommandHandler.cpp +++ b/services/st_util/test/TestStUartBootloaderCommandHandler.cpp @@ -312,13 +312,13 @@ TEST_F(StUartBootloaderCommandHandlerTest, WriteMemory_timeout) ExpectErrorOnTimeout(std::chrono::seconds(1), "Timeout writing memory"); } -TEST_F(StUartBootloaderCommandHandlerTest, GlobalErase) +TEST_F(StUartBootloaderCommandHandlerTest, MassErase) { Initialize(); infra::VerifyingFunctionMock ondone; ExpectSendData(0x43, 0xbc); - handler.GlobalErase(ondone); + handler.MassErase(ondone); ExpectSendData(0xff, 0x00); ExpectReceiveData({ 0x79 }); ExpectReceiveData({ 0x79 }); @@ -357,7 +357,7 @@ TEST_F(StUartBootloaderCommandHandlerTest, ExtendedErase_mass) infra::VerifyingFunctionMock ondone; ExpectSendData(0x44, 0xbb); - handler.ExtendedErase(services::MassEraseSubcommand::bankOne, ondone); + handler.ExtendedMassErase(services::MassEraseSubcommand::bankOne, ondone); ExpectSendData({ 0xff, 0xfe }, 0x01); ExpectReceiveData({ 0x79 }); ExpectReceiveData({ 0x79 }); @@ -382,7 +382,7 @@ TEST_F(StUartBootloaderCommandHandlerTest, ExtendedErase_timeout) testing::StrictMock> ondone; ExpectSendData(0x44, 0xbb); - handler.ExtendedErase(services::MassEraseSubcommand::global, [&ondone]() + handler.ExtendedMassErase(services::MassEraseSubcommand::global, [&ondone]() { ondone.callback(); }); @@ -630,3 +630,33 @@ TEST_F(StUartBootloaderCommandHandlerTest, receive_data_with_wrapped_around_queu EXPECT_EQ((std::array{ 0x09, 0x0a, 0x0b, 0x0c }), rxStatusBuffer); } + +TEST_F(StUartBootloaderCommandHandlerTest, Special_receive_buffer_size_received_in_parts) +{ + Initialize(); + infra::VerifyingFunctionMock ondone; + uint16_t subcommand = 0x54; + std::array rxDataBuffer; + std::array rxStatusBuffer; + infra::ByteRange rxData(rxDataBuffer); + infra::ByteRange rxStatus(rxStatusBuffer); + + ExpectSendData(0x50, 0xaf); + handler.Special(subcommand, infra::ConstByteRange{}, rxData, rxStatus, ondone); + + ExpectSendData({ 0x00, 0x54 }, 0x54); + ExpectReceiveData({ 0x79 }); + + ExpectSendData({ 0x00, 0x00 }, 0x00); + ExpectReceiveData({ 0x79 }); + ExpectReceiveData({ 0x79 }); + + ExpectReceiveData({ 0x00}); + ExpectReceiveData({ 0x04, 0x05, 0x06, 0x07, 0x08 }); + ExpectReceiveData({ 0x00 }); + ExpectReceiveData({ 0x04, 0x09, 0x0a, 0x0b, 0x0c }); + ExpectReceiveData({ 0x79 }); + + EXPECT_EQ((std::array{ 0x05, 0x06, 0x07, 0x08 }), rxDataBuffer); + EXPECT_EQ((std::array{ 0x09, 0x0a, 0x0b, 0x0c }), rxStatusBuffer); +} From 656de079c2f26bf108c219d7f4cc2d900fff7c31 Mon Sep 17 00:00:00 2001 From: EkelmansPh <58972933+EkelmansPh@users.noreply.github.com> Date: Mon, 30 Oct 2023 19:31:26 +0100 Subject: [PATCH 16/21] Update services/st_util/test/TestStUartBootloaderCommandHandler.cpp Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> --- services/st_util/test/TestStUartBootloaderCommandHandler.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/services/st_util/test/TestStUartBootloaderCommandHandler.cpp b/services/st_util/test/TestStUartBootloaderCommandHandler.cpp index bbf80c1f..b3548cdf 100644 --- a/services/st_util/test/TestStUartBootloaderCommandHandler.cpp +++ b/services/st_util/test/TestStUartBootloaderCommandHandler.cpp @@ -651,7 +651,7 @@ TEST_F(StUartBootloaderCommandHandlerTest, Special_receive_buffer_size_received_ ExpectReceiveData({ 0x79 }); ExpectReceiveData({ 0x79 }); - ExpectReceiveData({ 0x00}); + ExpectReceiveData({ 0x00 }); ExpectReceiveData({ 0x04, 0x05, 0x06, 0x07, 0x08 }); ExpectReceiveData({ 0x00 }); ExpectReceiveData({ 0x04, 0x09, 0x0a, 0x0b, 0x0c }); From 958d71cf0f56dc632c996b3735f92f296486d7ae Mon Sep 17 00:00:00 2001 From: Rob Ekelmans Date: Mon, 30 Oct 2023 22:03:00 +0000 Subject: [PATCH 17/21] Process PR comments --- .../st_util/StBootloaderCommandHandler.hpp | 1 + .../StUartBootloaderCommandHandler.cpp | 102 +++++++++--------- .../StUartBootloaderCommandHandler.hpp | 32 +++--- 3 files changed, 74 insertions(+), 61 deletions(-) diff --git a/services/st_util/StBootloaderCommandHandler.hpp b/services/st_util/StBootloaderCommandHandler.hpp index f1fe81d3..ef371844 100644 --- a/services/st_util/StBootloaderCommandHandler.hpp +++ b/services/st_util/StBootloaderCommandHandler.hpp @@ -2,6 +2,7 @@ #define SERVICES_ST_UTIL_ST_BOOTLOADER_COMMAND_HANDLER_HPP #include "infra/util//Function.hpp" +#include "infra/util/ByteRange.hpp" namespace services { diff --git a/services/st_util/StUartBootloaderCommandHandler.cpp b/services/st_util/StUartBootloaderCommandHandler.cpp index 98977cd8..c51bf064 100644 --- a/services/st_util/StUartBootloaderCommandHandler.cpp +++ b/services/st_util/StUartBootloaderCommandHandler.cpp @@ -17,14 +17,8 @@ namespace constexpr uint8_t extendedErase = 0x44; constexpr uint8_t special = 0x50; constexpr uint8_t extendedSpecial = 0x51; - constexpr uint8_t writeProtect = 0x63; - constexpr uint8_t writeUnprotect = 0x73; - constexpr uint8_t readoutProtect = 0x82; - constexpr uint8_t readoutUnprotect = 0x92; - constexpr uint8_t getChecksum = 0xa1; constexpr uint8_t ack = 0x79; - constexpr uint8_t nack = 0x1F; constexpr auto commandTimeout = std::chrono::seconds(1); } @@ -63,7 +57,7 @@ namespace services AddCommandAction(); SetCommandTimeout("Timeout getting commands"); - ExecuteCommand([this, onDone, &commands]() + ExecuteCommand([onDone, &commands]() { uint8_t major = commands.front() >> 4; uint8_t minor = commands.front() & 0xf; @@ -80,8 +74,6 @@ namespace services void StUartBootloaderCommandHandler::GetVersion(const infra::Function& onDone) { - internalRange = infra::MakeByteRange(internalBuffer); - AddCommandAction(getVersion); AddCommandAction(); AddCommandAction(internalRange, 3); @@ -98,8 +90,6 @@ namespace services void StUartBootloaderCommandHandler::GetId(const infra::Function& onDone) { - internalRange = infra::MakeByteRange(internalBuffer); - AddCommandAction(getId); AddCommandAction(); AddCommandAction(internalRange); @@ -115,6 +105,8 @@ namespace services void StUartBootloaderCommandHandler::ReadMemory(uint32_t address, infra::ByteRange& data, const infra::Function& onDone) { + really_assert(data.size() <= 255); + this->address = address; AddCommandAction(readMemory); @@ -144,7 +136,7 @@ namespace services void StUartBootloaderCommandHandler::WriteMemory(uint32_t address, infra::ConstByteRange data, const infra::Function& onDone) { - really_assert(data.size() <= 256); + really_assert(data.size() <= 255); this->address = address; @@ -200,12 +192,11 @@ namespace services void StUartBootloaderCommandHandler::ExtendedErase(infra::ConstByteRange pages, const infra::Function& onDone) { - really_assert(pages.size() <= 256); really_assert(pages.size() % 2 == 0); AddCommandAction(extendedErase); AddCommandAction(); - AddCommandAction((pages.size() / 2) - 1, pages); + AddCommandAction(pages, (pages.size() / 2) - 1); AddCommandAction(); SetCommandTimeout("Timeout extended erasing memory"); @@ -222,7 +213,7 @@ namespace services AddCommandAction(); AddCommandAction(infra::MakeByteRange(this->subcommand)); AddCommandAction(); - AddCommandAction(txData.size(), txData); + AddCommandAction(txData, txData.size()); AddCommandAction(); AddCommandAction(rxData); AddCommandAction(rxStatus); @@ -243,9 +234,9 @@ namespace services AddCommandAction(); AddCommandAction(infra::MakeByteRange(this->subcommand)); AddCommandAction(); - AddCommandAction(txData1.size(), txData1); + AddCommandAction(txData1, txData1.size()); AddCommandAction(); - AddCommandAction(txData2.size(), txData2); + AddCommandAction(txData2, txData2.size()); AddCommandAction(); AddCommandAction(rxData); AddCommandAction(); @@ -275,10 +266,10 @@ namespace services void StUartBootloaderCommandHandler::ExecuteCommand(const infra::Function) + sizeof(infra::ByteRange)>& onCommandExecuted) { this->onCommandExecuted = onCommandExecuted; - StartAction(); + StartCurrentAction(); } - void StUartBootloaderCommandHandler::StartAction() + void StUartBootloaderCommandHandler::StartCurrentAction() { commandActions.front()->Start(); TryHandleDataReceived(); @@ -303,23 +294,7 @@ namespace services if (commandActions.empty()) OnCommandExecuted(); else - StartAction(); - } - - void StUartBootloaderCommandHandler::SendData(infra::ConstByteRange data, uint8_t checksum) - { - serial.SendData(data, [this, checksum]() - { - SendData(infra::MakeConstByteRange(checksum)); - }); - } - - void StUartBootloaderCommandHandler::SendData(infra::ConstByteRange data) - { - serial.SendData(data, [this]() - { - OnActionExecuted(); - }); + StartCurrentAction(); } void StUartBootloaderCommandHandler::SetCommandTimeout(infra::BoundedConstString reason) @@ -339,6 +314,22 @@ namespace services onError(reason); } + void StUartBootloaderCommandHandler::SendData(infra::ConstByteRange data, uint8_t checksum) + { + serial.SendData(data, [this, checksum]() + { + SendData(infra::MakeConstByteRange(checksum)); + }); + } + + void StUartBootloaderCommandHandler::SendData(infra::ConstByteRange data) + { + serial.SendData(data, [this]() + { + OnActionExecuted(); + }); + } + StUartBootloaderCommandHandler::Action::Action(StUartBootloaderCommandHandler& handler) : handler(handler) {} @@ -369,18 +360,31 @@ namespace services infra::QueueForOneReaderOneIrqWriter::StreamReader reader(handler.queue); infra::DataInputStream::WithErrorPolicy stream(reader, infra::noFail); + if (!TotalNumberOfBytesAvailable(stream)) + return; + + RetreiveData(reader, stream); + CheckActionExecuted(); + } + + bool StUartBootloaderCommandHandler::ReceiveBuffer::TotalNumberOfBytesAvailable(infra::DataInputStream& stream) + { if (nBytesTotal == infra::none) - { - TryExtractNumberOfBytes(stream); - if (nBytesTotal == infra::none) - return; - } + TryRetreiveNumberOfBytes(stream); + + return nBytesTotal != infra::none; + } + void StUartBootloaderCommandHandler::ReceiveBuffer::RetreiveData(infra::QueueForOneReaderOneIrqWriter::StreamReader& reader, infra::DataInputStream& stream) + { auto buffer = infra::Head(infra::DiscardHead(data, nBytesReceived), std::min(stream.Available(), *nBytesTotal - nBytesReceived)); stream >> buffer; reader.Commit(); - nBytesReceived += buffer.size(); + } + + void StUartBootloaderCommandHandler::ReceiveBuffer::CheckActionExecuted() + { if (nBytesReceived == nBytesTotal) { data.shrink_from_back_to(nBytesReceived); @@ -393,17 +397,17 @@ namespace services , size(size) {} - void StUartBootloaderCommandHandler::ReceivePredefinedBuffer::TryExtractNumberOfBytes([[maybe_unused]] infra::DataInputStream& stream) + void StUartBootloaderCommandHandler::ReceivePredefinedBuffer::TryRetreiveNumberOfBytes([[maybe_unused]] infra::DataInputStream& stream) { nBytesTotal.Emplace(size); } - void StUartBootloaderCommandHandler::ReceiveSmallBuffer::TryExtractNumberOfBytes(infra::DataInputStream& stream) + void StUartBootloaderCommandHandler::ReceiveSmallBuffer::TryRetreiveNumberOfBytes(infra::DataInputStream& stream) { nBytesTotal.Emplace(stream.Extract() + 1); } - void StUartBootloaderCommandHandler::ReceiveBigBuffer::TryExtractNumberOfBytes(infra::DataInputStream& stream) + void StUartBootloaderCommandHandler::ReceiveBigBuffer::TryRetreiveNumberOfBytes(infra::DataInputStream& stream) { if (stream.Available() >= sizeof(uint16_t)) nBytesTotal.Emplace(stream.Extract>()); @@ -426,7 +430,7 @@ namespace services void StUartBootloaderCommandHandler::TransmitWithTwosComplementChecksum::Start() { - auto checksum = data ^ 0xff; + uint8_t checksum = data ^ 0xff; handler.SendData(infra::MakeConstByteRange(data), checksum); } @@ -434,8 +438,8 @@ namespace services : StUartBootloaderCommandHandler::Action(handler) , data(data) { - for (auto& b : data) - AddToChecksum(b); + for (auto& byte : data) + AddToChecksum(byte); } void StUartBootloaderCommandHandler::TransmitChecksummedBuffer::AddToChecksum(const uint8_t& byte) @@ -466,7 +470,7 @@ namespace services }); } - StUartBootloaderCommandHandler::TransmitBigBuffer::TransmitBigBuffer(StUartBootloaderCommandHandler& handler, uint16_t size, infra::ConstByteRange data) + StUartBootloaderCommandHandler::TransmitBigBuffer::TransmitBigBuffer(StUartBootloaderCommandHandler& handler, infra::ConstByteRange data, uint16_t size) : StUartBootloaderCommandHandler::TransmitChecksummedBuffer(handler, data) , size(size) { diff --git a/services/st_util/StUartBootloaderCommandHandler.hpp b/services/st_util/StUartBootloaderCommandHandler.hpp index 3778c58f..c46841c3 100644 --- a/services/st_util/StUartBootloaderCommandHandler.hpp +++ b/services/st_util/StUartBootloaderCommandHandler.hpp @@ -7,6 +7,7 @@ #include "infra/util/AutoResetFunction.hpp" #include "infra/util/BoundedDeque.hpp" #include "infra/util/BoundedString.hpp" +#include "infra/util/ByteRange.hpp" #include "infra/util/Endian.hpp" #include "infra/util/PolymorphicVariant.hpp" #include "services/st_util/StBootloaderCommandHandler.hpp" @@ -14,11 +15,11 @@ namespace services { class StUartBootloaderCommandHandler - : public StBootloaderCommandHandler + : protected StBootloaderCommandHandler { public: StUartBootloaderCommandHandler(hal::SerialCommunication& serial, const infra::Function& onInitialized, const infra::Function& onError); - ~StUartBootloaderCommandHandler(); + virtual ~StUartBootloaderCommandHandler(); // Implementation of StBootloaderCommandHandler void GetCommand(infra::ByteRange& commands, const infra::Function& onDone) override; @@ -41,14 +42,14 @@ namespace services void AddCommandAction(Args&&... args); void ExecuteCommand(const infra::Function) + sizeof(infra::ByteRange)>& onCommandExecuted); - void StartAction(); + void StartCurrentAction(); void TryHandleDataReceived(); void OnCommandExecuted(); void OnActionExecuted(); - void SendData(infra::ConstByteRange data, uint8_t checksum); - void SendData(infra::ConstByteRange data); void SetCommandTimeout(infra::BoundedConstString reason); void OnError(infra::BoundedConstString reason); + void SendData(infra::ConstByteRange data, uint8_t checksum); + void SendData(infra::ConstByteRange data); private: class Action @@ -84,7 +85,12 @@ namespace services void DataReceived() override; protected: - virtual void TryExtractNumberOfBytes(infra::DataInputStream& stream) = 0; + virtual void TryRetreiveNumberOfBytes(infra::DataInputStream& stream) = 0; + + private: + bool TotalNumberOfBytesAvailable(infra::DataInputStream& stream); + void RetreiveData(infra::QueueForOneReaderOneIrqWriter::StreamReader& reader, infra::DataInputStream& stream); + void CheckActionExecuted(); protected: infra::ByteRange& data; @@ -99,7 +105,7 @@ namespace services ReceivePredefinedBuffer(StUartBootloaderCommandHandler& handler, infra::ByteRange& data, const std::size_t size); protected: - void TryExtractNumberOfBytes(infra::DataInputStream& stream) override; + void TryRetreiveNumberOfBytes(infra::DataInputStream& stream) override; private: const std::size_t size; @@ -112,7 +118,7 @@ namespace services using ReceiveBuffer::ReceiveBuffer; protected: - void TryExtractNumberOfBytes(infra::DataInputStream& stream) override; + void TryRetreiveNumberOfBytes(infra::DataInputStream& stream) override; }; class ReceiveBigBuffer @@ -122,7 +128,7 @@ namespace services using ReceiveBuffer::ReceiveBuffer; protected: - void TryExtractNumberOfBytes(infra::DataInputStream& stream) override; + void TryRetreiveNumberOfBytes(infra::DataInputStream& stream) override; }; class TransmitRaw @@ -155,9 +161,11 @@ namespace services public: TransmitChecksummedBuffer(StUartBootloaderCommandHandler& handler, infra::ConstByteRange data); - void AddToChecksum(const uint8_t& byte); void Start() override; + protected: + void AddToChecksum(const uint8_t& byte); + private: infra::ConstByteRange data; uint8_t checksum = 0; @@ -179,7 +187,7 @@ namespace services : public TransmitChecksummedBuffer { public: - TransmitBigBuffer(StUartBootloaderCommandHandler& handler, uint16_t size, infra::ConstByteRange data); + TransmitBigBuffer(StUartBootloaderCommandHandler& handler, infra::ConstByteRange data, uint16_t size); void Start() override; @@ -199,7 +207,7 @@ namespace services infra::TimerSingleShot timeout; std::array internalBuffer; - infra::ByteRange internalRange; + infra::ByteRange internalRange{ internalBuffer }; infra::BigEndian address; infra::BigEndian subcommand; }; From 07766822984dcb44b01aead79e6869a51f496cb1 Mon Sep 17 00:00:00 2001 From: Rob Ekelmans Date: Mon, 6 Nov 2023 14:33:06 +0000 Subject: [PATCH 18/21] chore: rename to StBootloaderCommandHandlerUart --- services/st_util/CMakeLists.txt | 4 +- ...cpp => StBootloaderCommandHandlerUart.cpp} | 118 +++++++++--------- ...hpp => StBootloaderCommandHandlerUart.hpp} | 28 ++--- services/st_util/test/CMakeLists.txt | 2 +- ...=> TestStBootloaderCommandHandlerUart.cpp} | 70 +++++------ 5 files changed, 111 insertions(+), 111 deletions(-) rename services/st_util/{StUartBootloaderCommandHandler.cpp => StBootloaderCommandHandlerUart.cpp} (74%) rename services/st_util/{StUartBootloaderCommandHandler.hpp => StBootloaderCommandHandlerUart.hpp} (86%) rename services/st_util/test/{TestStUartBootloaderCommandHandler.cpp => TestStBootloaderCommandHandlerUart.cpp} (90%) diff --git a/services/st_util/CMakeLists.txt b/services/st_util/CMakeLists.txt index 6fb29d7c..9fd4f69f 100644 --- a/services/st_util/CMakeLists.txt +++ b/services/st_util/CMakeLists.txt @@ -11,8 +11,8 @@ target_sources(services.st_util PRIVATE StUartProgrammer.cpp StUartProgrammer.hpp StBootloaderCommandHandler.hpp - StUartBootloaderCommandHandler.cpp - StUartBootloaderCommandHandler.hpp + StBootloaderCommandHandlerUart.cpp + StBootloaderCommandHandlerUart.hpp ) target_link_libraries(services.st_util PUBLIC diff --git a/services/st_util/StUartBootloaderCommandHandler.cpp b/services/st_util/StBootloaderCommandHandlerUart.cpp similarity index 74% rename from services/st_util/StUartBootloaderCommandHandler.cpp rename to services/st_util/StBootloaderCommandHandlerUart.cpp index c51bf064..d425caf7 100644 --- a/services/st_util/StUartBootloaderCommandHandler.cpp +++ b/services/st_util/StBootloaderCommandHandlerUart.cpp @@ -1,4 +1,4 @@ -#include "services/st_util/StUartBootloaderCommandHandler.hpp" +#include "services/st_util/StBootloaderCommandHandlerUart.hpp" #include "infra/util/Optional.hpp" #include #include @@ -25,7 +25,7 @@ namespace namespace services { - StUartBootloaderCommandHandler::StUartBootloaderCommandHandler(hal::SerialCommunication& serial, const infra::Function& onInitialized, const infra::Function& onError) + StBootloaderCommandHandlerUart::StBootloaderCommandHandlerUart(hal::SerialCommunication& serial, const infra::Function& onInitialized, const infra::Function& onError) : serial(serial) , onInitialized(onInitialized) , onError(onError) @@ -42,12 +42,12 @@ namespace services InitializeUartBootloader(); } - StUartBootloaderCommandHandler::~StUartBootloaderCommandHandler() + StBootloaderCommandHandlerUart::~StBootloaderCommandHandlerUart() { serial.ReceiveData([](auto data) {}); } - void StUartBootloaderCommandHandler::GetCommand(infra::ByteRange& commands, const infra::Function& onDone) + void StBootloaderCommandHandlerUart::GetCommand(infra::ByteRange& commands, const infra::Function& onDone) { really_assert(commands.size() >= 15); @@ -72,7 +72,7 @@ namespace services }); } - void StUartBootloaderCommandHandler::GetVersion(const infra::Function& onDone) + void StBootloaderCommandHandlerUart::GetVersion(const infra::Function& onDone) { AddCommandAction(getVersion); AddCommandAction(); @@ -88,7 +88,7 @@ namespace services }); } - void StUartBootloaderCommandHandler::GetId(const infra::Function& onDone) + void StBootloaderCommandHandlerUart::GetId(const infra::Function& onDone) { AddCommandAction(getId); AddCommandAction(); @@ -103,7 +103,7 @@ namespace services }); } - void StUartBootloaderCommandHandler::ReadMemory(uint32_t address, infra::ByteRange& data, const infra::Function& onDone) + void StBootloaderCommandHandlerUart::ReadMemory(uint32_t address, infra::ByteRange& data, const infra::Function& onDone) { really_assert(data.size() <= 255); @@ -121,7 +121,7 @@ namespace services ExecuteCommand(onDone); } - void StUartBootloaderCommandHandler::Go(uint32_t address, const infra::Function& onDone) + void StBootloaderCommandHandlerUart::Go(uint32_t address, const infra::Function& onDone) { this->address = address; @@ -134,7 +134,7 @@ namespace services ExecuteCommand(onDone); } - void StUartBootloaderCommandHandler::WriteMemory(uint32_t address, infra::ConstByteRange data, const infra::Function& onDone) + void StBootloaderCommandHandlerUart::WriteMemory(uint32_t address, infra::ConstByteRange data, const infra::Function& onDone) { really_assert(data.size() <= 255); @@ -151,7 +151,7 @@ namespace services ExecuteCommand(onDone); } - void StUartBootloaderCommandHandler::MassErase(const infra::Function& onDone) + void StBootloaderCommandHandlerUart::MassErase(const infra::Function& onDone) { constexpr uint8_t globalEraseSubCommand = 0xff; @@ -164,7 +164,7 @@ namespace services ExecuteCommand(onDone); } - void StUartBootloaderCommandHandler::Erase(infra::ConstByteRange pages, const infra::Function& onDone) + void StBootloaderCommandHandlerUart::Erase(infra::ConstByteRange pages, const infra::Function& onDone) { really_assert(pages.size() <= 255); @@ -177,7 +177,7 @@ namespace services ExecuteCommand(onDone); } - void StUartBootloaderCommandHandler::ExtendedMassErase(MassEraseSubcommand subcommand, const infra::Function& onDone) + void StBootloaderCommandHandlerUart::ExtendedMassErase(MassEraseSubcommand subcommand, const infra::Function& onDone) { this->subcommand = static_cast(subcommand); @@ -190,7 +190,7 @@ namespace services ExecuteCommand(onDone); } - void StUartBootloaderCommandHandler::ExtendedErase(infra::ConstByteRange pages, const infra::Function& onDone) + void StBootloaderCommandHandlerUart::ExtendedErase(infra::ConstByteRange pages, const infra::Function& onDone) { really_assert(pages.size() % 2 == 0); @@ -203,7 +203,7 @@ namespace services ExecuteCommand(onDone); } - void StUartBootloaderCommandHandler::Special(uint16_t subcommand, infra::ConstByteRange txData, infra::ByteRange& rxData, infra::ByteRange& rxStatus, const infra::Function& onDone) + void StBootloaderCommandHandlerUart::Special(uint16_t subcommand, infra::ConstByteRange txData, infra::ByteRange& rxData, infra::ByteRange& rxStatus, const infra::Function& onDone) { really_assert(txData.size() <= 128); @@ -223,7 +223,7 @@ namespace services ExecuteCommand(onDone); } - void StUartBootloaderCommandHandler::ExtendedSpecial(uint16_t subcommand, infra::ConstByteRange txData1, infra::ConstByteRange txData2, infra::ByteRange& rxData, const infra::Function& onDone) + void StBootloaderCommandHandlerUart::ExtendedSpecial(uint16_t subcommand, infra::ConstByteRange txData1, infra::ConstByteRange txData2, infra::ByteRange& rxData, const infra::Function& onDone) { really_assert(txData1.size() <= 128); really_assert(txData2.size() <= 1024); @@ -245,7 +245,7 @@ namespace services ExecuteCommand(onDone); } - void StUartBootloaderCommandHandler::InitializeUartBootloader() + void StBootloaderCommandHandlerUart::InitializeUartBootloader() { AddCommandAction(infra::MakeByteRange(uartInitializationData)); AddCommandAction(); @@ -258,36 +258,36 @@ namespace services } template - void StUartBootloaderCommandHandler::AddCommandAction(Args&&... args) + void StBootloaderCommandHandlerUart::AddCommandAction(Args&&... args) { commandActions.emplace_back(infra::InPlaceType(), *this, std::forward(args)...); } - void StUartBootloaderCommandHandler::ExecuteCommand(const infra::Function) + sizeof(infra::ByteRange)>& onCommandExecuted) + void StBootloaderCommandHandlerUart::ExecuteCommand(const infra::Function) + sizeof(infra::ByteRange)>& onCommandExecuted) { this->onCommandExecuted = onCommandExecuted; StartCurrentAction(); } - void StUartBootloaderCommandHandler::StartCurrentAction() + void StBootloaderCommandHandlerUart::StartCurrentAction() { commandActions.front()->Start(); TryHandleDataReceived(); } - void StUartBootloaderCommandHandler::TryHandleDataReceived() + void StBootloaderCommandHandlerUart::TryHandleDataReceived() { if (!queue.Empty()) commandActions.front()->DataReceived(); } - void StUartBootloaderCommandHandler::OnCommandExecuted() + void StBootloaderCommandHandlerUart::OnCommandExecuted() { timeout.Cancel(); onCommandExecuted(); } - void StUartBootloaderCommandHandler::OnActionExecuted() + void StBootloaderCommandHandlerUart::OnActionExecuted() { commandActions.pop_front(); @@ -297,7 +297,7 @@ namespace services StartCurrentAction(); } - void StUartBootloaderCommandHandler::SetCommandTimeout(infra::BoundedConstString reason) + void StBootloaderCommandHandlerUart::SetCommandTimeout(infra::BoundedConstString reason) { timeoutReason = reason; timeout.Start(commandTimeout, [this]() @@ -306,7 +306,7 @@ namespace services }); } - void StUartBootloaderCommandHandler::OnError(infra::BoundedConstString reason) + void StBootloaderCommandHandlerUart::OnError(infra::BoundedConstString reason) { timeout.Cancel(); serial.ReceiveData([](auto data) {}); @@ -314,7 +314,7 @@ namespace services onError(reason); } - void StUartBootloaderCommandHandler::SendData(infra::ConstByteRange data, uint8_t checksum) + void StBootloaderCommandHandlerUart::SendData(infra::ConstByteRange data, uint8_t checksum) { serial.SendData(data, [this, checksum]() { @@ -322,7 +322,7 @@ namespace services }); } - void StUartBootloaderCommandHandler::SendData(infra::ConstByteRange data) + void StBootloaderCommandHandlerUart::SendData(infra::ConstByteRange data) { serial.SendData(data, [this]() { @@ -330,17 +330,17 @@ namespace services }); } - StUartBootloaderCommandHandler::Action::Action(StUartBootloaderCommandHandler& handler) + StBootloaderCommandHandlerUart::Action::Action(StBootloaderCommandHandlerUart& handler) : handler(handler) {} - void StUartBootloaderCommandHandler::Action::Start() + void StBootloaderCommandHandlerUart::Action::Start() {} - void StUartBootloaderCommandHandler::Action::DataReceived() + void StBootloaderCommandHandlerUart::Action::DataReceived() {} - void StUartBootloaderCommandHandler::ReceiveAck::DataReceived() + void StBootloaderCommandHandlerUart::ReceiveAck::DataReceived() { auto byte = handler.queue.Get(); @@ -350,12 +350,12 @@ namespace services handler.OnError("NACK received"); } - StUartBootloaderCommandHandler::ReceiveBuffer::ReceiveBuffer(StUartBootloaderCommandHandler& handler, infra::ByteRange& data) - : StUartBootloaderCommandHandler::Action(handler) + StBootloaderCommandHandlerUart::ReceiveBuffer::ReceiveBuffer(StBootloaderCommandHandlerUart& handler, infra::ByteRange& data) + : StBootloaderCommandHandlerUart::Action(handler) , data(data) {} - void StUartBootloaderCommandHandler::ReceiveBuffer::DataReceived() + void StBootloaderCommandHandlerUart::ReceiveBuffer::DataReceived() { infra::QueueForOneReaderOneIrqWriter::StreamReader reader(handler.queue); infra::DataInputStream::WithErrorPolicy stream(reader, infra::noFail); @@ -367,7 +367,7 @@ namespace services CheckActionExecuted(); } - bool StUartBootloaderCommandHandler::ReceiveBuffer::TotalNumberOfBytesAvailable(infra::DataInputStream& stream) + bool StBootloaderCommandHandlerUart::ReceiveBuffer::TotalNumberOfBytesAvailable(infra::DataInputStream& stream) { if (nBytesTotal == infra::none) TryRetreiveNumberOfBytes(stream); @@ -375,7 +375,7 @@ namespace services return nBytesTotal != infra::none; } - void StUartBootloaderCommandHandler::ReceiveBuffer::RetreiveData(infra::QueueForOneReaderOneIrqWriter::StreamReader& reader, infra::DataInputStream& stream) + void StBootloaderCommandHandlerUart::ReceiveBuffer::RetreiveData(infra::QueueForOneReaderOneIrqWriter::StreamReader& reader, infra::DataInputStream& stream) { auto buffer = infra::Head(infra::DiscardHead(data, nBytesReceived), std::min(stream.Available(), *nBytesTotal - nBytesReceived)); stream >> buffer; @@ -383,7 +383,7 @@ namespace services nBytesReceived += buffer.size(); } - void StUartBootloaderCommandHandler::ReceiveBuffer::CheckActionExecuted() + void StBootloaderCommandHandlerUart::ReceiveBuffer::CheckActionExecuted() { if (nBytesReceived == nBytesTotal) { @@ -392,62 +392,62 @@ namespace services } } - StUartBootloaderCommandHandler::ReceivePredefinedBuffer::ReceivePredefinedBuffer(StUartBootloaderCommandHandler& handler, infra::ByteRange& data, const std::size_t size) - : StUartBootloaderCommandHandler::ReceiveBuffer(handler, data) + StBootloaderCommandHandlerUart::ReceivePredefinedBuffer::ReceivePredefinedBuffer(StBootloaderCommandHandlerUart& handler, infra::ByteRange& data, const std::size_t size) + : StBootloaderCommandHandlerUart::ReceiveBuffer(handler, data) , size(size) {} - void StUartBootloaderCommandHandler::ReceivePredefinedBuffer::TryRetreiveNumberOfBytes([[maybe_unused]] infra::DataInputStream& stream) + void StBootloaderCommandHandlerUart::ReceivePredefinedBuffer::TryRetreiveNumberOfBytes([[maybe_unused]] infra::DataInputStream& stream) { nBytesTotal.Emplace(size); } - void StUartBootloaderCommandHandler::ReceiveSmallBuffer::TryRetreiveNumberOfBytes(infra::DataInputStream& stream) + void StBootloaderCommandHandlerUart::ReceiveSmallBuffer::TryRetreiveNumberOfBytes(infra::DataInputStream& stream) { nBytesTotal.Emplace(stream.Extract() + 1); } - void StUartBootloaderCommandHandler::ReceiveBigBuffer::TryRetreiveNumberOfBytes(infra::DataInputStream& stream) + void StBootloaderCommandHandlerUart::ReceiveBigBuffer::TryRetreiveNumberOfBytes(infra::DataInputStream& stream) { if (stream.Available() >= sizeof(uint16_t)) nBytesTotal.Emplace(stream.Extract>()); } - StUartBootloaderCommandHandler::TransmitRaw::TransmitRaw(StUartBootloaderCommandHandler& handler, infra::ConstByteRange data) - : StUartBootloaderCommandHandler::Action(handler) + StBootloaderCommandHandlerUart::TransmitRaw::TransmitRaw(StBootloaderCommandHandlerUart& handler, infra::ConstByteRange data) + : StBootloaderCommandHandlerUart::Action(handler) , data(data) {} - void StUartBootloaderCommandHandler::TransmitRaw::Start() + void StBootloaderCommandHandlerUart::TransmitRaw::Start() { handler.SendData(data); } - StUartBootloaderCommandHandler::TransmitWithTwosComplementChecksum::TransmitWithTwosComplementChecksum(StUartBootloaderCommandHandler& handler, uint8_t data) - : StUartBootloaderCommandHandler::Action(handler) + StBootloaderCommandHandlerUart::TransmitWithTwosComplementChecksum::TransmitWithTwosComplementChecksum(StBootloaderCommandHandlerUart& handler, uint8_t data) + : StBootloaderCommandHandlerUart::Action(handler) , data(data) {} - void StUartBootloaderCommandHandler::TransmitWithTwosComplementChecksum::Start() + void StBootloaderCommandHandlerUart::TransmitWithTwosComplementChecksum::Start() { uint8_t checksum = data ^ 0xff; handler.SendData(infra::MakeConstByteRange(data), checksum); } - StUartBootloaderCommandHandler::TransmitChecksummedBuffer::TransmitChecksummedBuffer(StUartBootloaderCommandHandler& handler, infra::ConstByteRange data) - : StUartBootloaderCommandHandler::Action(handler) + StBootloaderCommandHandlerUart::TransmitChecksummedBuffer::TransmitChecksummedBuffer(StBootloaderCommandHandlerUart& handler, infra::ConstByteRange data) + : StBootloaderCommandHandlerUart::Action(handler) , data(data) { for (auto& byte : data) AddToChecksum(byte); } - void StUartBootloaderCommandHandler::TransmitChecksummedBuffer::AddToChecksum(const uint8_t& byte) + void StBootloaderCommandHandlerUart::TransmitChecksummedBuffer::AddToChecksum(const uint8_t& byte) { checksum ^= byte; } - void StUartBootloaderCommandHandler::TransmitChecksummedBuffer::Start() + void StBootloaderCommandHandlerUart::TransmitChecksummedBuffer::Start() { if (data.empty()) handler.SendData(infra::MakeConstByteRange(checksum)); @@ -455,34 +455,34 @@ namespace services handler.SendData(data, checksum); } - StUartBootloaderCommandHandler::TransmitSmallBuffer::TransmitSmallBuffer(StUartBootloaderCommandHandler& handler, infra::ConstByteRange data) - : StUartBootloaderCommandHandler::TransmitChecksummedBuffer(handler, data) + StBootloaderCommandHandlerUart::TransmitSmallBuffer::TransmitSmallBuffer(StBootloaderCommandHandlerUart& handler, infra::ConstByteRange data) + : StBootloaderCommandHandlerUart::TransmitChecksummedBuffer(handler, data) { size = data.size() - 1; AddToChecksum(size); } - void StUartBootloaderCommandHandler::TransmitSmallBuffer::Start() + void StBootloaderCommandHandlerUart::TransmitSmallBuffer::Start() { handler.serial.SendData(infra::MakeConstByteRange(size), [this]() { - StUartBootloaderCommandHandler::TransmitChecksummedBuffer::Start(); + StBootloaderCommandHandlerUart::TransmitChecksummedBuffer::Start(); }); } - StUartBootloaderCommandHandler::TransmitBigBuffer::TransmitBigBuffer(StUartBootloaderCommandHandler& handler, infra::ConstByteRange data, uint16_t size) - : StUartBootloaderCommandHandler::TransmitChecksummedBuffer(handler, data) + StBootloaderCommandHandlerUart::TransmitBigBuffer::TransmitBigBuffer(StBootloaderCommandHandlerUart& handler, infra::ConstByteRange data, uint16_t size) + : StBootloaderCommandHandlerUart::TransmitChecksummedBuffer(handler, data) , size(size) { AddToChecksum(static_cast(size >> 8)); AddToChecksum(static_cast(size & 0xff)); } - void StUartBootloaderCommandHandler::TransmitBigBuffer::Start() + void StBootloaderCommandHandlerUart::TransmitBigBuffer::Start() { handler.serial.SendData(infra::MakeConstByteRange(size), [this]() { - StUartBootloaderCommandHandler::TransmitChecksummedBuffer::Start(); + StBootloaderCommandHandlerUart::TransmitChecksummedBuffer::Start(); }); } } diff --git a/services/st_util/StUartBootloaderCommandHandler.hpp b/services/st_util/StBootloaderCommandHandlerUart.hpp similarity index 86% rename from services/st_util/StUartBootloaderCommandHandler.hpp rename to services/st_util/StBootloaderCommandHandlerUart.hpp index c46841c3..364c0c76 100644 --- a/services/st_util/StUartBootloaderCommandHandler.hpp +++ b/services/st_util/StBootloaderCommandHandlerUart.hpp @@ -14,12 +14,12 @@ namespace services { - class StUartBootloaderCommandHandler + class StBootloaderCommandHandlerUart : protected StBootloaderCommandHandler { public: - StUartBootloaderCommandHandler(hal::SerialCommunication& serial, const infra::Function& onInitialized, const infra::Function& onError); - virtual ~StUartBootloaderCommandHandler(); + StBootloaderCommandHandlerUart(hal::SerialCommunication& serial, const infra::Function& onInitialized, const infra::Function& onError); + virtual ~StBootloaderCommandHandlerUart(); // Implementation of StBootloaderCommandHandler void GetCommand(infra::ByteRange& commands, const infra::Function& onDone) override; @@ -41,7 +41,7 @@ namespace services template void AddCommandAction(Args&&... args); - void ExecuteCommand(const infra::Function) + sizeof(infra::ByteRange)>& onCommandExecuted); + void ExecuteCommand(const infra::Function) + sizeof(infra::ByteRange)>& onCommandExecuted); void StartCurrentAction(); void TryHandleDataReceived(); void OnCommandExecuted(); @@ -55,7 +55,7 @@ namespace services class Action { public: - Action(StUartBootloaderCommandHandler& handler); + Action(StBootloaderCommandHandlerUart& handler); Action(const Action& other) = delete; Action& operator=(const Action& other) = delete; virtual ~Action() = default; @@ -64,7 +64,7 @@ namespace services virtual void DataReceived(); protected: - StUartBootloaderCommandHandler& handler; + StBootloaderCommandHandlerUart& handler; }; class ReceiveAck @@ -80,7 +80,7 @@ namespace services : public Action { public: - ReceiveBuffer(StUartBootloaderCommandHandler& handler, infra::ByteRange& data); + ReceiveBuffer(StBootloaderCommandHandlerUart& handler, infra::ByteRange& data); void DataReceived() override; @@ -102,7 +102,7 @@ namespace services : public ReceiveBuffer { public: - ReceivePredefinedBuffer(StUartBootloaderCommandHandler& handler, infra::ByteRange& data, const std::size_t size); + ReceivePredefinedBuffer(StBootloaderCommandHandlerUart& handler, infra::ByteRange& data, const std::size_t size); protected: void TryRetreiveNumberOfBytes(infra::DataInputStream& stream) override; @@ -135,7 +135,7 @@ namespace services : public Action { public: - TransmitRaw(StUartBootloaderCommandHandler& handler, infra::ConstByteRange data); + TransmitRaw(StBootloaderCommandHandlerUart& handler, infra::ConstByteRange data); void Start() override; @@ -147,7 +147,7 @@ namespace services : public Action { public: - TransmitWithTwosComplementChecksum(StUartBootloaderCommandHandler& handler, uint8_t data); + TransmitWithTwosComplementChecksum(StBootloaderCommandHandlerUart& handler, uint8_t data); void Start() override; @@ -159,7 +159,7 @@ namespace services : public Action { public: - TransmitChecksummedBuffer(StUartBootloaderCommandHandler& handler, infra::ConstByteRange data); + TransmitChecksummedBuffer(StBootloaderCommandHandlerUart& handler, infra::ConstByteRange data); void Start() override; @@ -175,7 +175,7 @@ namespace services : public TransmitChecksummedBuffer { public: - TransmitSmallBuffer(StUartBootloaderCommandHandler& handler, infra::ConstByteRange data); + TransmitSmallBuffer(StBootloaderCommandHandlerUart& handler, infra::ConstByteRange data); void Start() override; @@ -187,7 +187,7 @@ namespace services : public TransmitChecksummedBuffer { public: - TransmitBigBuffer(StUartBootloaderCommandHandler& handler, infra::ConstByteRange data, uint16_t size); + TransmitBigBuffer(StBootloaderCommandHandlerUart& handler, infra::ConstByteRange data, uint16_t size); void Start() override; @@ -202,7 +202,7 @@ namespace services infra::QueueForOneReaderOneIrqWriter::WithStorage<257> queue; infra::BoundedDeque>::WithMaxSize<12> commandActions; - infra::AutoResetFunction) + sizeof(infra::ByteRange)> onCommandExecuted; + infra::AutoResetFunction) + sizeof(infra::ByteRange)> onCommandExecuted; infra::BoundedString::WithStorage<46> timeoutReason; infra::TimerSingleShot timeout; diff --git a/services/st_util/test/CMakeLists.txt b/services/st_util/test/CMakeLists.txt index eae06246..ca4563a8 100644 --- a/services/st_util/test/CMakeLists.txt +++ b/services/st_util/test/CMakeLists.txt @@ -2,7 +2,7 @@ add_executable(services.st_util_test) emil_add_test(services.st_util_test) target_sources(services.st_util_test PRIVATE - TestStUartBootloaderCommandHandler.cpp + TestStBootloaderCommandHandlerUart.cpp ) target_link_libraries(services.st_util_test PUBLIC diff --git a/services/st_util/test/TestStUartBootloaderCommandHandler.cpp b/services/st_util/test/TestStBootloaderCommandHandlerUart.cpp similarity index 90% rename from services/st_util/test/TestStUartBootloaderCommandHandler.cpp rename to services/st_util/test/TestStBootloaderCommandHandlerUart.cpp index b3548cdf..3effcae2 100644 --- a/services/st_util/test/TestStUartBootloaderCommandHandler.cpp +++ b/services/st_util/test/TestStBootloaderCommandHandlerUart.cpp @@ -2,7 +2,7 @@ #include "infra/timer/test_helper/ClockFixture.hpp" #include "infra/util/MemoryRange.hpp" #include "infra/util/test_helper/MockCallback.hpp" -#include "services/st_util/StUartBootloaderCommandHandler.hpp" +#include "services/st_util/StBootloaderCommandHandlerUart.hpp" #include "gmock/gmock.h" #include "gtest/gtest.h" #include @@ -10,7 +10,7 @@ #include #include -class StUartBootloaderCommandHandlerTest +class StBootloaderCommandHandlerUartTest : public testing::Test , public infra::ClockFixture { @@ -90,22 +90,22 @@ class StUartBootloaderCommandHandlerTest { ExpectSendData({ 0x7f }); } }; - services::StUartBootloaderCommandHandler handler{ serialCommunication, onInitialized, onError }; + services::StBootloaderCommandHandlerUart handler{ serialCommunication, onInitialized, onError }; }; -TEST_F(StUartBootloaderCommandHandlerTest, creation_initializes_uart_bootloader) +TEST_F(StBootloaderCommandHandlerUartTest, creation_initializes_uart_bootloader) { Initialize(); } -TEST_F(StUartBootloaderCommandHandlerTest, creation_initializes_uart_bootloader_fails) +TEST_F(StBootloaderCommandHandlerUartTest, creation_initializes_uart_bootloader_fails) { infra::BoundedConstString::WithStorage<64> errorMessage("Timeout waiting for bootloader initialization"); EXPECT_CALL(onErrorMock, callback(errorMessage)); ForwardTime(std::chrono::milliseconds(1000)); } -TEST_F(StUartBootloaderCommandHandlerTest, receive_nack) +TEST_F(StBootloaderCommandHandlerUartTest, receive_nack) { Initialize(); testing::StrictMock> ondone; @@ -121,7 +121,7 @@ TEST_F(StUartBootloaderCommandHandlerTest, receive_nack) ExpectReceiveData({ 0x1f }); } -TEST_F(StUartBootloaderCommandHandlerTest, GetCommand) +TEST_F(StBootloaderCommandHandlerUartTest, GetCommand) { Initialize(); infra::VerifyingFunctionMock ondone(3, 2); @@ -136,7 +136,7 @@ TEST_F(StUartBootloaderCommandHandlerTest, GetCommand) EXPECT_EQ((std::array{ 0x00, 0x01, 0x02, 0x11, 0x21, 0x31, 0x44, 0x63, 0x73, 0x82, 0x92 }), commandsbuffer); } -TEST_F(StUartBootloaderCommandHandlerTest, GetCommand_receive_in_parts) +TEST_F(StBootloaderCommandHandlerUartTest, GetCommand_receive_in_parts) { Initialize(); infra::VerifyingFunctionMock ondone(3, 2); @@ -151,7 +151,7 @@ TEST_F(StUartBootloaderCommandHandlerTest, GetCommand_receive_in_parts) EXPECT_EQ((std::array{ 0x00, 0x01, 0x02, 0x11, 0x21, 0x31, 0x44, 0x63, 0x73, 0x82, 0x92 }), commandsbuffer); } -TEST_F(StUartBootloaderCommandHandlerTest, GetCommand_timeout) +TEST_F(StBootloaderCommandHandlerUartTest, GetCommand_timeout) { Initialize(); testing::StrictMock> ondone; @@ -166,7 +166,7 @@ TEST_F(StUartBootloaderCommandHandlerTest, GetCommand_timeout) ExpectErrorOnTimeout(std::chrono::seconds(1), "Timeout getting commands"); } -TEST_F(StUartBootloaderCommandHandlerTest, GetVersion) +TEST_F(StBootloaderCommandHandlerUartTest, GetVersion) { Initialize(); infra::VerifyingFunctionMock ondone(3, 1); @@ -176,7 +176,7 @@ TEST_F(StUartBootloaderCommandHandlerTest, GetVersion) ExpectReceiveData({ 0x79, 0x31, 0x00, 0x00, 0x79 }); } -TEST_F(StUartBootloaderCommandHandlerTest, GetVersion_timeout) +TEST_F(StBootloaderCommandHandlerUartTest, GetVersion_timeout) { Initialize(); testing::StrictMock> ondone; @@ -189,7 +189,7 @@ TEST_F(StUartBootloaderCommandHandlerTest, GetVersion_timeout) ExpectErrorOnTimeout(std::chrono::seconds(1), "Timeout getting version"); } -TEST_F(StUartBootloaderCommandHandlerTest, GetId) +TEST_F(StBootloaderCommandHandlerUartTest, GetId) { Initialize(); infra::VerifyingFunctionMock ondone(0x0495); @@ -199,7 +199,7 @@ TEST_F(StUartBootloaderCommandHandlerTest, GetId) ExpectReceiveData({ 0x79, 0x01, 0x04, 0x95, 0x79 }); } -TEST_F(StUartBootloaderCommandHandlerTest, GetId_timeout) +TEST_F(StBootloaderCommandHandlerUartTest, GetId_timeout) { Initialize(); testing::StrictMock> ondone; @@ -212,7 +212,7 @@ TEST_F(StUartBootloaderCommandHandlerTest, GetId_timeout) ExpectErrorOnTimeout(std::chrono::seconds(1), "Timeout getting id"); } -TEST_F(StUartBootloaderCommandHandlerTest, ReadMemory) +TEST_F(StBootloaderCommandHandlerUartTest, ReadMemory) { Initialize(); infra::VerifyingFunctionMock ondone; @@ -235,7 +235,7 @@ TEST_F(StUartBootloaderCommandHandlerTest, ReadMemory) EXPECT_EQ((std::array{ 0x05, 0x06, 0x07 }), dataBuffer); } -TEST_F(StUartBootloaderCommandHandlerTest, ReadMemory_timeout) +TEST_F(StBootloaderCommandHandlerUartTest, ReadMemory_timeout) { Initialize(); testing::StrictMock> ondone; @@ -250,7 +250,7 @@ TEST_F(StUartBootloaderCommandHandlerTest, ReadMemory_timeout) ExpectErrorOnTimeout(std::chrono::seconds(1), "Timeout reading memory"); } -TEST_F(StUartBootloaderCommandHandlerTest, Go) +TEST_F(StBootloaderCommandHandlerUartTest, Go) { Initialize(); infra::VerifyingFunctionMock ondone; @@ -265,7 +265,7 @@ TEST_F(StUartBootloaderCommandHandlerTest, Go) ExpectReceiveData({ 0x79 }); } -TEST_F(StUartBootloaderCommandHandlerTest, Go_timeout) +TEST_F(StBootloaderCommandHandlerUartTest, Go_timeout) { Initialize(); testing::StrictMock> ondone; @@ -279,7 +279,7 @@ TEST_F(StUartBootloaderCommandHandlerTest, Go_timeout) ExpectErrorOnTimeout(std::chrono::seconds(1), "Timeout go"); } -TEST_F(StUartBootloaderCommandHandlerTest, WriteMemory) +TEST_F(StBootloaderCommandHandlerUartTest, WriteMemory) { Initialize(); infra::VerifyingFunctionMock ondone; @@ -297,7 +297,7 @@ TEST_F(StUartBootloaderCommandHandlerTest, WriteMemory) ExpectReceiveData({ 0x79 }); } -TEST_F(StUartBootloaderCommandHandlerTest, WriteMemory_timeout) +TEST_F(StBootloaderCommandHandlerUartTest, WriteMemory_timeout) { Initialize(); testing::StrictMock> ondone; @@ -312,7 +312,7 @@ TEST_F(StUartBootloaderCommandHandlerTest, WriteMemory_timeout) ExpectErrorOnTimeout(std::chrono::seconds(1), "Timeout writing memory"); } -TEST_F(StUartBootloaderCommandHandlerTest, MassErase) +TEST_F(StBootloaderCommandHandlerUartTest, MassErase) { Initialize(); infra::VerifyingFunctionMock ondone; @@ -324,7 +324,7 @@ TEST_F(StUartBootloaderCommandHandlerTest, MassErase) ExpectReceiveData({ 0x79 }); } -TEST_F(StUartBootloaderCommandHandlerTest, Erase_pages) +TEST_F(StBootloaderCommandHandlerUartTest, Erase_pages) { Initialize(); infra::VerifyingFunctionMock ondone; @@ -337,7 +337,7 @@ TEST_F(StUartBootloaderCommandHandlerTest, Erase_pages) ExpectReceiveData({ 0x79 }); } -TEST_F(StUartBootloaderCommandHandlerTest, Erase_timeout) +TEST_F(StBootloaderCommandHandlerUartTest, Erase_timeout) { Initialize(); testing::StrictMock> ondone; @@ -351,7 +351,7 @@ TEST_F(StUartBootloaderCommandHandlerTest, Erase_timeout) ExpectErrorOnTimeout(std::chrono::seconds(1), "Timeout erasing memory"); } -TEST_F(StUartBootloaderCommandHandlerTest, ExtendedErase_mass) +TEST_F(StBootloaderCommandHandlerUartTest, ExtendedErase_mass) { Initialize(); infra::VerifyingFunctionMock ondone; @@ -363,7 +363,7 @@ TEST_F(StUartBootloaderCommandHandlerTest, ExtendedErase_mass) ExpectReceiveData({ 0x79 }); } -TEST_F(StUartBootloaderCommandHandlerTest, ExtendedErase_pages) +TEST_F(StBootloaderCommandHandlerUartTest, ExtendedErase_pages) { Initialize(); infra::VerifyingFunctionMock ondone; @@ -376,7 +376,7 @@ TEST_F(StUartBootloaderCommandHandlerTest, ExtendedErase_pages) ExpectReceiveData({ 0x79 }); } -TEST_F(StUartBootloaderCommandHandlerTest, ExtendedErase_timeout) +TEST_F(StBootloaderCommandHandlerUartTest, ExtendedErase_timeout) { Initialize(); testing::StrictMock> ondone; @@ -389,7 +389,7 @@ TEST_F(StUartBootloaderCommandHandlerTest, ExtendedErase_timeout) ExpectErrorOnTimeout(std::chrono::seconds(1), "Timeout extended erasing memory"); } -TEST_F(StUartBootloaderCommandHandlerTest, Special) +TEST_F(StBootloaderCommandHandlerUartTest, Special) { Initialize(); infra::VerifyingFunctionMock ondone; @@ -418,7 +418,7 @@ TEST_F(StUartBootloaderCommandHandlerTest, Special) EXPECT_EQ((std::array{ 0x09, 0x0a, 0x0b, 0x0c }), rxStatusBuffer); } -TEST_F(StUartBootloaderCommandHandlerTest, Special_empty_rxData) +TEST_F(StBootloaderCommandHandlerUartTest, Special_empty_rxData) { Initialize(); infra::VerifyingFunctionMock ondone; @@ -447,7 +447,7 @@ TEST_F(StUartBootloaderCommandHandlerTest, Special_empty_rxData) EXPECT_EQ((std::array{ 0x09, 0x0a, 0x0b, 0x0c }), rxStatusBuffer); } -TEST_F(StUartBootloaderCommandHandlerTest, Special_empty_rxStatus) +TEST_F(StBootloaderCommandHandlerUartTest, Special_empty_rxStatus) { Initialize(); infra::VerifyingFunctionMock ondone; @@ -476,7 +476,7 @@ TEST_F(StUartBootloaderCommandHandlerTest, Special_empty_rxStatus) EXPECT_EQ((std::array{}), rxStatusBuffer); } -TEST_F(StUartBootloaderCommandHandlerTest, Special_empty_rxData_rxStatus) +TEST_F(StBootloaderCommandHandlerUartTest, Special_empty_rxData_rxStatus) { Initialize(); infra::VerifyingFunctionMock ondone; @@ -505,7 +505,7 @@ TEST_F(StUartBootloaderCommandHandlerTest, Special_empty_rxData_rxStatus) EXPECT_EQ((std::array{}), rxStatusBuffer); } -TEST_F(StUartBootloaderCommandHandlerTest, Special_empty_txData) +TEST_F(StBootloaderCommandHandlerUartTest, Special_empty_txData) { Initialize(); infra::VerifyingFunctionMock ondone; @@ -533,7 +533,7 @@ TEST_F(StUartBootloaderCommandHandlerTest, Special_empty_txData) EXPECT_EQ((std::array{ 0x09, 0x0a, 0x0b, 0x0c }), rxStatusBuffer); } -TEST_F(StUartBootloaderCommandHandlerTest, Special_timeout) +TEST_F(StBootloaderCommandHandlerUartTest, Special_timeout) { Initialize(); testing::StrictMock> ondone; @@ -548,7 +548,7 @@ TEST_F(StUartBootloaderCommandHandlerTest, Special_timeout) ExpectErrorOnTimeout(std::chrono::seconds(1), "Timeout special command"); } -TEST_F(StUartBootloaderCommandHandlerTest, ExtendedSpecial) +TEST_F(StBootloaderCommandHandlerUartTest, ExtendedSpecial) { Initialize(); infra::VerifyingFunctionMock ondone; @@ -577,7 +577,7 @@ TEST_F(StUartBootloaderCommandHandlerTest, ExtendedSpecial) EXPECT_EQ((std::array{ 0x09, 0x0a, 0x0b, 0x0c }), rxDataBuffer); } -TEST_F(StUartBootloaderCommandHandlerTest, ExtendedSpecial_timeout) +TEST_F(StBootloaderCommandHandlerUartTest, ExtendedSpecial_timeout) { Initialize(); testing::StrictMock> ondone; @@ -591,7 +591,7 @@ TEST_F(StUartBootloaderCommandHandlerTest, ExtendedSpecial_timeout) ExpectErrorOnTimeout(std::chrono::seconds(1), "Timeout extended special command"); } -TEST_F(StUartBootloaderCommandHandlerTest, receive_data_with_wrapped_around_queue) +TEST_F(StBootloaderCommandHandlerUartTest, receive_data_with_wrapped_around_queue) { Initialize(); infra::VerifyingFunctionMock ondone; @@ -631,7 +631,7 @@ TEST_F(StUartBootloaderCommandHandlerTest, receive_data_with_wrapped_around_queu EXPECT_EQ((std::array{ 0x09, 0x0a, 0x0b, 0x0c }), rxStatusBuffer); } -TEST_F(StUartBootloaderCommandHandlerTest, Special_receive_buffer_size_received_in_parts) +TEST_F(StBootloaderCommandHandlerUartTest, Special_receive_buffer_size_received_in_parts) { Initialize(); infra::VerifyingFunctionMock ondone; From c3a1f9e6255bdbd2043d7fa589f1d1a5f9bb9223 Mon Sep 17 00:00:00 2001 From: Rob Ekelmans Date: Mon, 6 Nov 2023 14:35:05 +0000 Subject: [PATCH 19/21] chore: StBootloaderCommandHandlerUartTest ExpectReceiveData -> ReceiveData --- .../TestStBootloaderCommandHandlerUart.cpp | 150 +++++++++--------- 1 file changed, 75 insertions(+), 75 deletions(-) diff --git a/services/st_util/test/TestStBootloaderCommandHandlerUart.cpp b/services/st_util/test/TestStBootloaderCommandHandlerUart.cpp index 3effcae2..1b3c0497 100644 --- a/services/st_util/test/TestStBootloaderCommandHandlerUart.cpp +++ b/services/st_util/test/TestStBootloaderCommandHandlerUart.cpp @@ -68,7 +68,7 @@ class StBootloaderCommandHandlerUartTest ForwardTime(timeout); } - void ExpectReceiveData(std::vector data) + void ReceiveData(std::vector data) { serialCommunication.dataReceived(data); ExecuteAllActions(); @@ -118,7 +118,7 @@ TEST_F(StBootloaderCommandHandlerUartTest, receive_nack) ondone.callback(); }); EXPECT_CALL(onErrorMock, callback(infra::BoundedConstString{ "NACK received" })); - ExpectReceiveData({ 0x1f }); + ReceiveData({ 0x1f }); } TEST_F(StBootloaderCommandHandlerUartTest, GetCommand) @@ -130,7 +130,7 @@ TEST_F(StBootloaderCommandHandlerUartTest, GetCommand) ExpectSendData(0x00, 0xff); handler.GetCommand(commands, ondone); - ExpectReceiveData({ 0x79, 0x0b, 0x32, 0x00, 0x01, 0x02, 0x11, 0x21, 0x31, 0x44, 0x63, 0x73, 0x82, 0x92, 0x79 }); + ReceiveData({ 0x79, 0x0b, 0x32, 0x00, 0x01, 0x02, 0x11, 0x21, 0x31, 0x44, 0x63, 0x73, 0x82, 0x92, 0x79 }); EXPECT_EQ(commands.size(), 0x0b); EXPECT_EQ((std::array{ 0x00, 0x01, 0x02, 0x11, 0x21, 0x31, 0x44, 0x63, 0x73, 0x82, 0x92 }), commandsbuffer); @@ -145,8 +145,8 @@ TEST_F(StBootloaderCommandHandlerUartTest, GetCommand_receive_in_parts) ExpectSendData(0x00, 0xff); handler.GetCommand(commands, ondone); - ExpectReceiveData({ 0x79, 0x0b, 0x32, 0x00, 0x01 }); - ExpectReceiveData({ 0x02, 0x11, 0x21, 0x31, 0x44, 0x63, 0x73, 0x82, 0x92, 0x79 }); + ReceiveData({ 0x79, 0x0b, 0x32, 0x00, 0x01 }); + ReceiveData({ 0x02, 0x11, 0x21, 0x31, 0x44, 0x63, 0x73, 0x82, 0x92, 0x79 }); EXPECT_EQ((std::array{ 0x00, 0x01, 0x02, 0x11, 0x21, 0x31, 0x44, 0x63, 0x73, 0x82, 0x92 }), commandsbuffer); } @@ -173,7 +173,7 @@ TEST_F(StBootloaderCommandHandlerUartTest, GetVersion) ExpectSendData(0x01, 0xfe); handler.GetVersion(ondone); - ExpectReceiveData({ 0x79, 0x31, 0x00, 0x00, 0x79 }); + ReceiveData({ 0x79, 0x31, 0x00, 0x00, 0x79 }); } TEST_F(StBootloaderCommandHandlerUartTest, GetVersion_timeout) @@ -196,7 +196,7 @@ TEST_F(StBootloaderCommandHandlerUartTest, GetId) ExpectSendData(0x02, 0xfd); handler.GetId(ondone); - ExpectReceiveData({ 0x79, 0x01, 0x04, 0x95, 0x79 }); + ReceiveData({ 0x79, 0x01, 0x04, 0x95, 0x79 }); } TEST_F(StBootloaderCommandHandlerUartTest, GetId_timeout) @@ -224,13 +224,13 @@ TEST_F(StBootloaderCommandHandlerUartTest, ReadMemory) handler.ReadMemory(address, data, ondone); ExpectSendData({ 0x01, 0x02, 0x03, 0x04 }, 0x04); - ExpectReceiveData({ 0x79 }); + ReceiveData({ 0x79 }); ExpectSendData(0x02, 0xFD); - ExpectReceiveData({ 0x79 }); + ReceiveData({ 0x79 }); - ExpectReceiveData({ 0x79 }); - ExpectReceiveData({ 0x05, 0x06, 0x07 }); + ReceiveData({ 0x79 }); + ReceiveData({ 0x05, 0x06, 0x07 }); EXPECT_EQ((std::array{ 0x05, 0x06, 0x07 }), dataBuffer); } @@ -260,9 +260,9 @@ TEST_F(StBootloaderCommandHandlerUartTest, Go) handler.Go(address, ondone); ExpectSendData({ 0x01, 0x02, 0x03, 0x04 }, 0x04); - ExpectReceiveData({ 0x79 }); + ReceiveData({ 0x79 }); - ExpectReceiveData({ 0x79 }); + ReceiveData({ 0x79 }); } TEST_F(StBootloaderCommandHandlerUartTest, Go_timeout) @@ -290,11 +290,11 @@ TEST_F(StBootloaderCommandHandlerUartTest, WriteMemory) handler.WriteMemory(address, infra::MakeByteRange(data), ondone); ExpectSendData({ 0x01, 0x02, 0x03, 0x04 }, 0x04); - ExpectReceiveData({ 0x79 }); + ReceiveData({ 0x79 }); ExpectSendData({ 0x03 }, { 0x05, 0x06, 0x07, 0x08 }, 0x0f); - ExpectReceiveData({ 0x79 }); - ExpectReceiveData({ 0x79 }); + ReceiveData({ 0x79 }); + ReceiveData({ 0x79 }); } TEST_F(StBootloaderCommandHandlerUartTest, WriteMemory_timeout) @@ -320,8 +320,8 @@ TEST_F(StBootloaderCommandHandlerUartTest, MassErase) ExpectSendData(0x43, 0xbc); handler.MassErase(ondone); ExpectSendData(0xff, 0x00); - ExpectReceiveData({ 0x79 }); - ExpectReceiveData({ 0x79 }); + ReceiveData({ 0x79 }); + ReceiveData({ 0x79 }); } TEST_F(StBootloaderCommandHandlerUartTest, Erase_pages) @@ -333,8 +333,8 @@ TEST_F(StBootloaderCommandHandlerUartTest, Erase_pages) ExpectSendData(0x43, 0xbc); handler.Erase(infra::MakeByteRange(pages), ondone); ExpectSendData({ 0x03 }, { 0x01, 0x02, 0x03, 0x04 }, 0x07); - ExpectReceiveData({ 0x79 }); - ExpectReceiveData({ 0x79 }); + ReceiveData({ 0x79 }); + ReceiveData({ 0x79 }); } TEST_F(StBootloaderCommandHandlerUartTest, Erase_timeout) @@ -359,8 +359,8 @@ TEST_F(StBootloaderCommandHandlerUartTest, ExtendedErase_mass) ExpectSendData(0x44, 0xbb); handler.ExtendedMassErase(services::MassEraseSubcommand::bankOne, ondone); ExpectSendData({ 0xff, 0xfe }, 0x01); - ExpectReceiveData({ 0x79 }); - ExpectReceiveData({ 0x79 }); + ReceiveData({ 0x79 }); + ReceiveData({ 0x79 }); } TEST_F(StBootloaderCommandHandlerUartTest, ExtendedErase_pages) @@ -372,8 +372,8 @@ TEST_F(StBootloaderCommandHandlerUartTest, ExtendedErase_pages) ExpectSendData(0x44, 0xbb); handler.ExtendedErase(infra::MakeByteRange(pages), ondone); ExpectSendData({ 0x00, 0x03 }, { 0x00, 0x01, 0x00, 0x02, 0x00, 0x03, 0x00, 0x04 }, 0x07); - ExpectReceiveData({ 0x79 }); - ExpectReceiveData({ 0x79 }); + ReceiveData({ 0x79 }); + ReceiveData({ 0x79 }); } TEST_F(StBootloaderCommandHandlerUartTest, ExtendedErase_timeout) @@ -404,15 +404,15 @@ TEST_F(StBootloaderCommandHandlerUartTest, Special) handler.Special(subcommand, infra::MakeConstByteRange(txData), rxData, rxStatus, ondone); ExpectSendData({ 0x00, 0x54 }, 0x54); - ExpectReceiveData({ 0x79 }); + ReceiveData({ 0x79 }); ExpectSendData({ 0x00, 0x04 }, { 0x01, 0x02, 0x03, 0x04 }, 0x00); - ExpectReceiveData({ 0x79 }); - ExpectReceiveData({ 0x79 }); + ReceiveData({ 0x79 }); + ReceiveData({ 0x79 }); - ExpectReceiveData({ 0x00, 0x04, 0x05, 0x06, 0x07, 0x08 }); - ExpectReceiveData({ 0x00, 0x04, 0x09, 0x0a, 0x0b, 0x0c }); - ExpectReceiveData({ 0x79 }); + ReceiveData({ 0x00, 0x04, 0x05, 0x06, 0x07, 0x08 }); + ReceiveData({ 0x00, 0x04, 0x09, 0x0a, 0x0b, 0x0c }); + ReceiveData({ 0x79 }); EXPECT_EQ((std::array{ 0x05, 0x06, 0x07, 0x08 }), rxDataBuffer); EXPECT_EQ((std::array{ 0x09, 0x0a, 0x0b, 0x0c }), rxStatusBuffer); @@ -433,15 +433,15 @@ TEST_F(StBootloaderCommandHandlerUartTest, Special_empty_rxData) handler.Special(subcommand, infra::MakeConstByteRange(txData), rxData, rxStatus, ondone); ExpectSendData({ 0x00, 0x54 }, 0x54); - ExpectReceiveData({ 0x79 }); + ReceiveData({ 0x79 }); ExpectSendData({ 0x00, 0x04 }, { 0x01, 0x02, 0x03, 0x04 }, 0x00); - ExpectReceiveData({ 0x79 }); - ExpectReceiveData({ 0x79 }); + ReceiveData({ 0x79 }); + ReceiveData({ 0x79 }); - ExpectReceiveData({ 0x00, 0x00 }); - ExpectReceiveData({ 0x00, 0x04, 0x09, 0x0a, 0x0b, 0x0c }); - ExpectReceiveData({ 0x79 }); + ReceiveData({ 0x00, 0x00 }); + ReceiveData({ 0x00, 0x04, 0x09, 0x0a, 0x0b, 0x0c }); + ReceiveData({ 0x79 }); EXPECT_EQ((std::array{}), rxDataBuffer); EXPECT_EQ((std::array{ 0x09, 0x0a, 0x0b, 0x0c }), rxStatusBuffer); @@ -462,15 +462,15 @@ TEST_F(StBootloaderCommandHandlerUartTest, Special_empty_rxStatus) handler.Special(subcommand, infra::MakeConstByteRange(txData), rxData, rxStatus, ondone); ExpectSendData({ 0x00, 0x54 }, 0x54); - ExpectReceiveData({ 0x79 }); + ReceiveData({ 0x79 }); ExpectSendData({ 0x00, 0x04 }, { 0x01, 0x02, 0x03, 0x04 }, 0x00); - ExpectReceiveData({ 0x79 }); - ExpectReceiveData({ 0x79 }); + ReceiveData({ 0x79 }); + ReceiveData({ 0x79 }); - ExpectReceiveData({ 0x00, 0x04, 0x05, 0x06, 0x07, 0x08 }); - ExpectReceiveData({ 0x00, 0x00 }); - ExpectReceiveData({ 0x79 }); + ReceiveData({ 0x00, 0x04, 0x05, 0x06, 0x07, 0x08 }); + ReceiveData({ 0x00, 0x00 }); + ReceiveData({ 0x79 }); EXPECT_EQ((std::array{ 0x05, 0x06, 0x07, 0x08 }), rxDataBuffer); EXPECT_EQ((std::array{}), rxStatusBuffer); @@ -491,15 +491,15 @@ TEST_F(StBootloaderCommandHandlerUartTest, Special_empty_rxData_rxStatus) handler.Special(subcommand, infra::MakeConstByteRange(txData), rxData, rxStatus, ondone); ExpectSendData({ 0x00, 0x54 }, 0x54); - ExpectReceiveData({ 0x79 }); + ReceiveData({ 0x79 }); ExpectSendData({ 0x00, 0x04 }, { 0x01, 0x02, 0x03, 0x04 }, 0x00); - ExpectReceiveData({ 0x79 }); - ExpectReceiveData({ 0x79 }); + ReceiveData({ 0x79 }); + ReceiveData({ 0x79 }); - ExpectReceiveData({ 0x00, 0x00 }); - ExpectReceiveData({ 0x00, 0x00 }); - ExpectReceiveData({ 0x79 }); + ReceiveData({ 0x00, 0x00 }); + ReceiveData({ 0x00, 0x00 }); + ReceiveData({ 0x79 }); EXPECT_EQ((std::array{}), rxDataBuffer); EXPECT_EQ((std::array{}), rxStatusBuffer); @@ -519,15 +519,15 @@ TEST_F(StBootloaderCommandHandlerUartTest, Special_empty_txData) handler.Special(subcommand, infra::ConstByteRange{}, rxData, rxStatus, ondone); ExpectSendData({ 0x00, 0x54 }, 0x54); - ExpectReceiveData({ 0x79 }); + ReceiveData({ 0x79 }); ExpectSendData({ 0x00, 0x00 }, 0x00); - ExpectReceiveData({ 0x79 }); - ExpectReceiveData({ 0x79 }); + ReceiveData({ 0x79 }); + ReceiveData({ 0x79 }); - ExpectReceiveData({ 0x00, 0x04, 0x05, 0x06, 0x07, 0x08 }); - ExpectReceiveData({ 0x00, 0x04, 0x09, 0x0a, 0x0b, 0x0c }); - ExpectReceiveData({ 0x79 }); + ReceiveData({ 0x00, 0x04, 0x05, 0x06, 0x07, 0x08 }); + ReceiveData({ 0x00, 0x04, 0x09, 0x0a, 0x0b, 0x0c }); + ReceiveData({ 0x79 }); EXPECT_EQ((std::array{ 0x05, 0x06, 0x07, 0x08 }), rxDataBuffer); EXPECT_EQ((std::array{ 0x09, 0x0a, 0x0b, 0x0c }), rxStatusBuffer); @@ -562,17 +562,17 @@ TEST_F(StBootloaderCommandHandlerUartTest, ExtendedSpecial) handler.ExtendedSpecial(subcommand, infra::MakeConstByteRange(txData1), infra::MakeConstByteRange(txData2), rxData, ondone); ExpectSendData({ 0x00, 0x54 }, 0x54); - ExpectReceiveData({ 0x79 }); + ReceiveData({ 0x79 }); ExpectSendData({ 0x00, 0x04 }, { 0x01, 0x02, 0x03, 0x04 }, 0x00); - ExpectReceiveData({ 0x79 }); + ReceiveData({ 0x79 }); ExpectSendData({ 0x00, 0x04 }, { 0x05, 0x06, 0x07, 0x08 }, 0x08); - ExpectReceiveData({ 0x79 }); - ExpectReceiveData({ 0x79 }); + ReceiveData({ 0x79 }); + ReceiveData({ 0x79 }); - ExpectReceiveData({ 0x00, 0x04, 0x09, 0x0a, 0x0b, 0x0c }); - ExpectReceiveData({ 0x79 }); + ReceiveData({ 0x00, 0x04, 0x09, 0x0a, 0x0b, 0x0c }); + ReceiveData({ 0x79 }); EXPECT_EQ((std::array{ 0x09, 0x0a, 0x0b, 0x0c }), rxDataBuffer); } @@ -608,11 +608,11 @@ TEST_F(StBootloaderCommandHandlerUartTest, receive_data_with_wrapped_around_queu handler.Special(subcommand, infra::ConstByteRange{}, rxData, rxStatus, ondone); ExpectSendData({ 0x00, 0x54 }, 0x54); - ExpectReceiveData({ 0x79 }); + ReceiveData({ 0x79 }); ExpectSendData({ 0x00, 0x00 }, 0x00); - ExpectReceiveData({ 0x79 }); - ExpectReceiveData({ 0x79 }); + ReceiveData({ 0x79 }); + ReceiveData({ 0x79 }); std::vector rxDataSimulated; rxDataSimulated.emplace_back(0x00); @@ -620,9 +620,9 @@ TEST_F(StBootloaderCommandHandlerUartTest, receive_data_with_wrapped_around_queu rxDataSimulated.resize(256, 0xaa); rxDataSimulated.emplace_back(0xbb); - ExpectReceiveData(rxDataSimulated); - ExpectReceiveData({ 0x00, 0x04, 0x09, 0x0a, 0x0b, 0x0c }); - ExpectReceiveData({ 0x79 }); + ReceiveData(rxDataSimulated); + ReceiveData({ 0x00, 0x04, 0x09, 0x0a, 0x0b, 0x0c }); + ReceiveData({ 0x79 }); infra::ConstByteRange rxDataSimulatedRange(rxDataSimulated.data(), rxDataSimulated.data() + rxDataSimulated.size()); rxDataSimulatedRange = infra::DiscardHead(rxDataSimulatedRange, 2); @@ -645,17 +645,17 @@ TEST_F(StBootloaderCommandHandlerUartTest, Special_receive_buffer_size_received_ handler.Special(subcommand, infra::ConstByteRange{}, rxData, rxStatus, ondone); ExpectSendData({ 0x00, 0x54 }, 0x54); - ExpectReceiveData({ 0x79 }); + ReceiveData({ 0x79 }); ExpectSendData({ 0x00, 0x00 }, 0x00); - ExpectReceiveData({ 0x79 }); - ExpectReceiveData({ 0x79 }); - - ExpectReceiveData({ 0x00 }); - ExpectReceiveData({ 0x04, 0x05, 0x06, 0x07, 0x08 }); - ExpectReceiveData({ 0x00 }); - ExpectReceiveData({ 0x04, 0x09, 0x0a, 0x0b, 0x0c }); - ExpectReceiveData({ 0x79 }); + ReceiveData({ 0x79 }); + ReceiveData({ 0x79 }); + + ReceiveData({ 0x00 }); + ReceiveData({ 0x04, 0x05, 0x06, 0x07, 0x08 }); + ReceiveData({ 0x00 }); + ReceiveData({ 0x04, 0x09, 0x0a, 0x0b, 0x0c }); + ReceiveData({ 0x79 }); EXPECT_EQ((std::array{ 0x05, 0x06, 0x07, 0x08 }), rxDataBuffer); EXPECT_EQ((std::array{ 0x09, 0x0a, 0x0b, 0x0c }), rxStatusBuffer); From 5cf21a883bf4fb1a21153da6d6aa2aa05e1a933d Mon Sep 17 00:00:00 2001 From: Rob Ekelmans Date: Mon, 6 Nov 2023 14:43:54 +0000 Subject: [PATCH 20/21] test: StBootloaderCommandHandlerUart extend test coverage --- .../StBootloaderCommandHandlerUart.cpp | 4 +- .../TestStBootloaderCommandHandlerUart.cpp | 37 +++++++++++++++++-- 2 files changed, 36 insertions(+), 5 deletions(-) diff --git a/services/st_util/StBootloaderCommandHandlerUart.cpp b/services/st_util/StBootloaderCommandHandlerUart.cpp index d425caf7..32b4dd48 100644 --- a/services/st_util/StBootloaderCommandHandlerUart.cpp +++ b/services/st_util/StBootloaderCommandHandlerUart.cpp @@ -160,7 +160,7 @@ namespace services AddCommandAction(globalEraseSubCommand); AddCommandAction(); - SetCommandTimeout("Timeout erasing memory"); + SetCommandTimeout("Timeout mass erasing memory"); ExecuteCommand(onDone); } @@ -186,7 +186,7 @@ namespace services AddCommandAction(infra::MakeConstByteRange(this->subcommand)); AddCommandAction(); - SetCommandTimeout("Timeout extended erasing memory"); + SetCommandTimeout("Timeout extended mass erasing memory"); ExecuteCommand(onDone); } diff --git a/services/st_util/test/TestStBootloaderCommandHandlerUart.cpp b/services/st_util/test/TestStBootloaderCommandHandlerUart.cpp index 1b3c0497..6c16632c 100644 --- a/services/st_util/test/TestStBootloaderCommandHandlerUart.cpp +++ b/services/st_util/test/TestStBootloaderCommandHandlerUart.cpp @@ -324,6 +324,21 @@ TEST_F(StBootloaderCommandHandlerUartTest, MassErase) ReceiveData({ 0x79 }); } +TEST_F(StBootloaderCommandHandlerUartTest, MassErase_timeout) +{ + Initialize(); + testing::StrictMock> ondone; + std::array data; + uint32_t address = 0x01020304; + + ExpectSendData(0x43, 0xbc); + handler.MassErase([&ondone]() + { + ondone.callback(); + }); + ExpectErrorOnTimeout(std::chrono::seconds(1), "Timeout mass erasing memory"); +} + TEST_F(StBootloaderCommandHandlerUartTest, Erase_pages) { Initialize(); @@ -351,7 +366,7 @@ TEST_F(StBootloaderCommandHandlerUartTest, Erase_timeout) ExpectErrorOnTimeout(std::chrono::seconds(1), "Timeout erasing memory"); } -TEST_F(StBootloaderCommandHandlerUartTest, ExtendedErase_mass) +TEST_F(StBootloaderCommandHandlerUartTest, ExtendedMassErase) { Initialize(); infra::VerifyingFunctionMock ondone; @@ -363,6 +378,21 @@ TEST_F(StBootloaderCommandHandlerUartTest, ExtendedErase_mass) ReceiveData({ 0x79 }); } +TEST_F(StBootloaderCommandHandlerUartTest, ExtendedMassErase_timeout) +{ + Initialize(); + testing::StrictMock> ondone; + std::array data; + uint32_t address = 0x01020304; + + ExpectSendData(0x44, 0xbb); + handler.ExtendedMassErase(services::MassEraseSubcommand::bankTwo, [&ondone]() + { + ondone.callback(); + }); + ExpectErrorOnTimeout(std::chrono::seconds(1), "Timeout extended mass erasing memory"); +} + TEST_F(StBootloaderCommandHandlerUartTest, ExtendedErase_pages) { Initialize(); @@ -376,13 +406,14 @@ TEST_F(StBootloaderCommandHandlerUartTest, ExtendedErase_pages) ReceiveData({ 0x79 }); } -TEST_F(StBootloaderCommandHandlerUartTest, ExtendedErase_timeout) +TEST_F(StBootloaderCommandHandlerUartTest, ExtendedErase_pages_timeout) { Initialize(); testing::StrictMock> ondone; + std::array, 4> pages = { 0x0001, 0x0002, 0x0003, 0x0004 }; ExpectSendData(0x44, 0xbb); - handler.ExtendedMassErase(services::MassEraseSubcommand::global, [&ondone]() + handler.ExtendedErase(infra::MakeByteRange(pages), [&ondone]() { ondone.callback(); }); From 882be34abaa73c99df51ba30fa9dba045ff61a95 Mon Sep 17 00:00:00 2001 From: Rob Ekelmans Date: Mon, 6 Nov 2023 14:50:43 +0000 Subject: [PATCH 21/21] chore: StBootloaderCommandHandler -> StBootloaderCommunicator --- services/st_util/CMakeLists.txt | 6 +- ...ndler.hpp => StBootloaderCommunicator.hpp} | 10 +- ...t.cpp => StBootloaderCommunicatorUart.cpp} | 118 +++++++++--------- ...t.hpp => StBootloaderCommunicatorUart.hpp} | 34 ++--- services/st_util/test/CMakeLists.txt | 2 +- ...p => TestStBootloaderCommunicatorUart.cpp} | 74 +++++------ 6 files changed, 122 insertions(+), 122 deletions(-) rename services/st_util/{StBootloaderCommandHandler.hpp => StBootloaderCommunicator.hpp} (85%) rename services/st_util/{StBootloaderCommandHandlerUart.cpp => StBootloaderCommunicatorUart.cpp} (65%) rename services/st_util/{StBootloaderCommandHandlerUart.hpp => StBootloaderCommunicatorUart.hpp} (81%) rename services/st_util/test/{TestStBootloaderCommandHandlerUart.cpp => TestStBootloaderCommunicatorUart.cpp} (89%) diff --git a/services/st_util/CMakeLists.txt b/services/st_util/CMakeLists.txt index 9fd4f69f..85fdbdb3 100644 --- a/services/st_util/CMakeLists.txt +++ b/services/st_util/CMakeLists.txt @@ -10,9 +10,9 @@ target_sources(services.st_util PRIVATE FlashOnStUartProgrammer.hpp StUartProgrammer.cpp StUartProgrammer.hpp - StBootloaderCommandHandler.hpp - StBootloaderCommandHandlerUart.cpp - StBootloaderCommandHandlerUart.hpp + StBootloaderCommunicator.hpp + StBootloaderCommunicatorUart.cpp + StBootloaderCommunicatorUart.hpp ) target_link_libraries(services.st_util PUBLIC diff --git a/services/st_util/StBootloaderCommandHandler.hpp b/services/st_util/StBootloaderCommunicator.hpp similarity index 85% rename from services/st_util/StBootloaderCommandHandler.hpp rename to services/st_util/StBootloaderCommunicator.hpp index ef371844..1df9aa8d 100644 --- a/services/st_util/StBootloaderCommandHandler.hpp +++ b/services/st_util/StBootloaderCommunicator.hpp @@ -13,13 +13,13 @@ namespace services bankTwo = 0xfffD, }; - class StBootloaderCommandHandler + class StBootloaderCommunicator { protected: - StBootloaderCommandHandler() = default; - StBootloaderCommandHandler(const StBootloaderCommandHandler& other) = delete; - StBootloaderCommandHandler& operator=(const StBootloaderCommandHandler& other) = delete; - ~StBootloaderCommandHandler() = default; + StBootloaderCommunicator() = default; + StBootloaderCommunicator(const StBootloaderCommunicator& other) = delete; + StBootloaderCommunicator& operator=(const StBootloaderCommunicator& other) = delete; + ~StBootloaderCommunicator() = default; public: virtual void GetCommand(infra::ByteRange& commands, const infra::Function& onDone) = 0; diff --git a/services/st_util/StBootloaderCommandHandlerUart.cpp b/services/st_util/StBootloaderCommunicatorUart.cpp similarity index 65% rename from services/st_util/StBootloaderCommandHandlerUart.cpp rename to services/st_util/StBootloaderCommunicatorUart.cpp index 32b4dd48..0a7d12bd 100644 --- a/services/st_util/StBootloaderCommandHandlerUart.cpp +++ b/services/st_util/StBootloaderCommunicatorUart.cpp @@ -1,4 +1,4 @@ -#include "services/st_util/StBootloaderCommandHandlerUart.hpp" +#include "services/st_util/StBootloaderCommunicatorUart.hpp" #include "infra/util/Optional.hpp" #include #include @@ -25,7 +25,7 @@ namespace namespace services { - StBootloaderCommandHandlerUart::StBootloaderCommandHandlerUart(hal::SerialCommunication& serial, const infra::Function& onInitialized, const infra::Function& onError) + StBootloaderCommunicatorUart::StBootloaderCommunicatorUart(hal::SerialCommunication& serial, const infra::Function& onInitialized, const infra::Function& onError) : serial(serial) , onInitialized(onInitialized) , onError(onError) @@ -42,12 +42,12 @@ namespace services InitializeUartBootloader(); } - StBootloaderCommandHandlerUart::~StBootloaderCommandHandlerUart() + StBootloaderCommunicatorUart::~StBootloaderCommunicatorUart() { serial.ReceiveData([](auto data) {}); } - void StBootloaderCommandHandlerUart::GetCommand(infra::ByteRange& commands, const infra::Function& onDone) + void StBootloaderCommunicatorUart::GetCommand(infra::ByteRange& commands, const infra::Function& onDone) { really_assert(commands.size() >= 15); @@ -72,7 +72,7 @@ namespace services }); } - void StBootloaderCommandHandlerUart::GetVersion(const infra::Function& onDone) + void StBootloaderCommunicatorUart::GetVersion(const infra::Function& onDone) { AddCommandAction(getVersion); AddCommandAction(); @@ -88,7 +88,7 @@ namespace services }); } - void StBootloaderCommandHandlerUart::GetId(const infra::Function& onDone) + void StBootloaderCommunicatorUart::GetId(const infra::Function& onDone) { AddCommandAction(getId); AddCommandAction(); @@ -103,7 +103,7 @@ namespace services }); } - void StBootloaderCommandHandlerUart::ReadMemory(uint32_t address, infra::ByteRange& data, const infra::Function& onDone) + void StBootloaderCommunicatorUart::ReadMemory(uint32_t address, infra::ByteRange& data, const infra::Function& onDone) { really_assert(data.size() <= 255); @@ -121,7 +121,7 @@ namespace services ExecuteCommand(onDone); } - void StBootloaderCommandHandlerUart::Go(uint32_t address, const infra::Function& onDone) + void StBootloaderCommunicatorUart::Go(uint32_t address, const infra::Function& onDone) { this->address = address; @@ -134,7 +134,7 @@ namespace services ExecuteCommand(onDone); } - void StBootloaderCommandHandlerUart::WriteMemory(uint32_t address, infra::ConstByteRange data, const infra::Function& onDone) + void StBootloaderCommunicatorUart::WriteMemory(uint32_t address, infra::ConstByteRange data, const infra::Function& onDone) { really_assert(data.size() <= 255); @@ -151,7 +151,7 @@ namespace services ExecuteCommand(onDone); } - void StBootloaderCommandHandlerUart::MassErase(const infra::Function& onDone) + void StBootloaderCommunicatorUart::MassErase(const infra::Function& onDone) { constexpr uint8_t globalEraseSubCommand = 0xff; @@ -164,7 +164,7 @@ namespace services ExecuteCommand(onDone); } - void StBootloaderCommandHandlerUart::Erase(infra::ConstByteRange pages, const infra::Function& onDone) + void StBootloaderCommunicatorUart::Erase(infra::ConstByteRange pages, const infra::Function& onDone) { really_assert(pages.size() <= 255); @@ -177,7 +177,7 @@ namespace services ExecuteCommand(onDone); } - void StBootloaderCommandHandlerUart::ExtendedMassErase(MassEraseSubcommand subcommand, const infra::Function& onDone) + void StBootloaderCommunicatorUart::ExtendedMassErase(MassEraseSubcommand subcommand, const infra::Function& onDone) { this->subcommand = static_cast(subcommand); @@ -190,7 +190,7 @@ namespace services ExecuteCommand(onDone); } - void StBootloaderCommandHandlerUart::ExtendedErase(infra::ConstByteRange pages, const infra::Function& onDone) + void StBootloaderCommunicatorUart::ExtendedErase(infra::ConstByteRange pages, const infra::Function& onDone) { really_assert(pages.size() % 2 == 0); @@ -203,7 +203,7 @@ namespace services ExecuteCommand(onDone); } - void StBootloaderCommandHandlerUart::Special(uint16_t subcommand, infra::ConstByteRange txData, infra::ByteRange& rxData, infra::ByteRange& rxStatus, const infra::Function& onDone) + void StBootloaderCommunicatorUart::Special(uint16_t subcommand, infra::ConstByteRange txData, infra::ByteRange& rxData, infra::ByteRange& rxStatus, const infra::Function& onDone) { really_assert(txData.size() <= 128); @@ -223,7 +223,7 @@ namespace services ExecuteCommand(onDone); } - void StBootloaderCommandHandlerUart::ExtendedSpecial(uint16_t subcommand, infra::ConstByteRange txData1, infra::ConstByteRange txData2, infra::ByteRange& rxData, const infra::Function& onDone) + void StBootloaderCommunicatorUart::ExtendedSpecial(uint16_t subcommand, infra::ConstByteRange txData1, infra::ConstByteRange txData2, infra::ByteRange& rxData, const infra::Function& onDone) { really_assert(txData1.size() <= 128); really_assert(txData2.size() <= 1024); @@ -245,7 +245,7 @@ namespace services ExecuteCommand(onDone); } - void StBootloaderCommandHandlerUart::InitializeUartBootloader() + void StBootloaderCommunicatorUart::InitializeUartBootloader() { AddCommandAction(infra::MakeByteRange(uartInitializationData)); AddCommandAction(); @@ -258,36 +258,36 @@ namespace services } template - void StBootloaderCommandHandlerUart::AddCommandAction(Args&&... args) + void StBootloaderCommunicatorUart::AddCommandAction(Args&&... args) { commandActions.emplace_back(infra::InPlaceType(), *this, std::forward(args)...); } - void StBootloaderCommandHandlerUart::ExecuteCommand(const infra::Function) + sizeof(infra::ByteRange)>& onCommandExecuted) + void StBootloaderCommunicatorUart::ExecuteCommand(const infra::Function) + sizeof(infra::ByteRange)>& onCommandExecuted) { this->onCommandExecuted = onCommandExecuted; StartCurrentAction(); } - void StBootloaderCommandHandlerUart::StartCurrentAction() + void StBootloaderCommunicatorUart::StartCurrentAction() { commandActions.front()->Start(); TryHandleDataReceived(); } - void StBootloaderCommandHandlerUart::TryHandleDataReceived() + void StBootloaderCommunicatorUart::TryHandleDataReceived() { if (!queue.Empty()) commandActions.front()->DataReceived(); } - void StBootloaderCommandHandlerUart::OnCommandExecuted() + void StBootloaderCommunicatorUart::OnCommandExecuted() { timeout.Cancel(); onCommandExecuted(); } - void StBootloaderCommandHandlerUart::OnActionExecuted() + void StBootloaderCommunicatorUart::OnActionExecuted() { commandActions.pop_front(); @@ -297,7 +297,7 @@ namespace services StartCurrentAction(); } - void StBootloaderCommandHandlerUart::SetCommandTimeout(infra::BoundedConstString reason) + void StBootloaderCommunicatorUart::SetCommandTimeout(infra::BoundedConstString reason) { timeoutReason = reason; timeout.Start(commandTimeout, [this]() @@ -306,7 +306,7 @@ namespace services }); } - void StBootloaderCommandHandlerUart::OnError(infra::BoundedConstString reason) + void StBootloaderCommunicatorUart::OnError(infra::BoundedConstString reason) { timeout.Cancel(); serial.ReceiveData([](auto data) {}); @@ -314,7 +314,7 @@ namespace services onError(reason); } - void StBootloaderCommandHandlerUart::SendData(infra::ConstByteRange data, uint8_t checksum) + void StBootloaderCommunicatorUart::SendData(infra::ConstByteRange data, uint8_t checksum) { serial.SendData(data, [this, checksum]() { @@ -322,7 +322,7 @@ namespace services }); } - void StBootloaderCommandHandlerUart::SendData(infra::ConstByteRange data) + void StBootloaderCommunicatorUart::SendData(infra::ConstByteRange data) { serial.SendData(data, [this]() { @@ -330,17 +330,17 @@ namespace services }); } - StBootloaderCommandHandlerUart::Action::Action(StBootloaderCommandHandlerUart& handler) + StBootloaderCommunicatorUart::Action::Action(StBootloaderCommunicatorUart& handler) : handler(handler) {} - void StBootloaderCommandHandlerUart::Action::Start() + void StBootloaderCommunicatorUart::Action::Start() {} - void StBootloaderCommandHandlerUart::Action::DataReceived() + void StBootloaderCommunicatorUart::Action::DataReceived() {} - void StBootloaderCommandHandlerUart::ReceiveAck::DataReceived() + void StBootloaderCommunicatorUart::ReceiveAck::DataReceived() { auto byte = handler.queue.Get(); @@ -350,12 +350,12 @@ namespace services handler.OnError("NACK received"); } - StBootloaderCommandHandlerUart::ReceiveBuffer::ReceiveBuffer(StBootloaderCommandHandlerUart& handler, infra::ByteRange& data) - : StBootloaderCommandHandlerUart::Action(handler) + StBootloaderCommunicatorUart::ReceiveBuffer::ReceiveBuffer(StBootloaderCommunicatorUart& handler, infra::ByteRange& data) + : StBootloaderCommunicatorUart::Action(handler) , data(data) {} - void StBootloaderCommandHandlerUart::ReceiveBuffer::DataReceived() + void StBootloaderCommunicatorUart::ReceiveBuffer::DataReceived() { infra::QueueForOneReaderOneIrqWriter::StreamReader reader(handler.queue); infra::DataInputStream::WithErrorPolicy stream(reader, infra::noFail); @@ -367,7 +367,7 @@ namespace services CheckActionExecuted(); } - bool StBootloaderCommandHandlerUart::ReceiveBuffer::TotalNumberOfBytesAvailable(infra::DataInputStream& stream) + bool StBootloaderCommunicatorUart::ReceiveBuffer::TotalNumberOfBytesAvailable(infra::DataInputStream& stream) { if (nBytesTotal == infra::none) TryRetreiveNumberOfBytes(stream); @@ -375,7 +375,7 @@ namespace services return nBytesTotal != infra::none; } - void StBootloaderCommandHandlerUart::ReceiveBuffer::RetreiveData(infra::QueueForOneReaderOneIrqWriter::StreamReader& reader, infra::DataInputStream& stream) + void StBootloaderCommunicatorUart::ReceiveBuffer::RetreiveData(infra::QueueForOneReaderOneIrqWriter::StreamReader& reader, infra::DataInputStream& stream) { auto buffer = infra::Head(infra::DiscardHead(data, nBytesReceived), std::min(stream.Available(), *nBytesTotal - nBytesReceived)); stream >> buffer; @@ -383,7 +383,7 @@ namespace services nBytesReceived += buffer.size(); } - void StBootloaderCommandHandlerUart::ReceiveBuffer::CheckActionExecuted() + void StBootloaderCommunicatorUart::ReceiveBuffer::CheckActionExecuted() { if (nBytesReceived == nBytesTotal) { @@ -392,62 +392,62 @@ namespace services } } - StBootloaderCommandHandlerUart::ReceivePredefinedBuffer::ReceivePredefinedBuffer(StBootloaderCommandHandlerUart& handler, infra::ByteRange& data, const std::size_t size) - : StBootloaderCommandHandlerUart::ReceiveBuffer(handler, data) + StBootloaderCommunicatorUart::ReceivePredefinedBuffer::ReceivePredefinedBuffer(StBootloaderCommunicatorUart& handler, infra::ByteRange& data, const std::size_t size) + : StBootloaderCommunicatorUart::ReceiveBuffer(handler, data) , size(size) {} - void StBootloaderCommandHandlerUart::ReceivePredefinedBuffer::TryRetreiveNumberOfBytes([[maybe_unused]] infra::DataInputStream& stream) + void StBootloaderCommunicatorUart::ReceivePredefinedBuffer::TryRetreiveNumberOfBytes([[maybe_unused]] infra::DataInputStream& stream) { nBytesTotal.Emplace(size); } - void StBootloaderCommandHandlerUart::ReceiveSmallBuffer::TryRetreiveNumberOfBytes(infra::DataInputStream& stream) + void StBootloaderCommunicatorUart::ReceiveSmallBuffer::TryRetreiveNumberOfBytes(infra::DataInputStream& stream) { nBytesTotal.Emplace(stream.Extract() + 1); } - void StBootloaderCommandHandlerUart::ReceiveBigBuffer::TryRetreiveNumberOfBytes(infra::DataInputStream& stream) + void StBootloaderCommunicatorUart::ReceiveBigBuffer::TryRetreiveNumberOfBytes(infra::DataInputStream& stream) { if (stream.Available() >= sizeof(uint16_t)) nBytesTotal.Emplace(stream.Extract>()); } - StBootloaderCommandHandlerUart::TransmitRaw::TransmitRaw(StBootloaderCommandHandlerUart& handler, infra::ConstByteRange data) - : StBootloaderCommandHandlerUart::Action(handler) + StBootloaderCommunicatorUart::TransmitRaw::TransmitRaw(StBootloaderCommunicatorUart& handler, infra::ConstByteRange data) + : StBootloaderCommunicatorUart::Action(handler) , data(data) {} - void StBootloaderCommandHandlerUart::TransmitRaw::Start() + void StBootloaderCommunicatorUart::TransmitRaw::Start() { handler.SendData(data); } - StBootloaderCommandHandlerUart::TransmitWithTwosComplementChecksum::TransmitWithTwosComplementChecksum(StBootloaderCommandHandlerUart& handler, uint8_t data) - : StBootloaderCommandHandlerUart::Action(handler) + StBootloaderCommunicatorUart::TransmitWithTwosComplementChecksum::TransmitWithTwosComplementChecksum(StBootloaderCommunicatorUart& handler, uint8_t data) + : StBootloaderCommunicatorUart::Action(handler) , data(data) {} - void StBootloaderCommandHandlerUart::TransmitWithTwosComplementChecksum::Start() + void StBootloaderCommunicatorUart::TransmitWithTwosComplementChecksum::Start() { uint8_t checksum = data ^ 0xff; handler.SendData(infra::MakeConstByteRange(data), checksum); } - StBootloaderCommandHandlerUart::TransmitChecksummedBuffer::TransmitChecksummedBuffer(StBootloaderCommandHandlerUart& handler, infra::ConstByteRange data) - : StBootloaderCommandHandlerUart::Action(handler) + StBootloaderCommunicatorUart::TransmitChecksummedBuffer::TransmitChecksummedBuffer(StBootloaderCommunicatorUart& handler, infra::ConstByteRange data) + : StBootloaderCommunicatorUart::Action(handler) , data(data) { for (auto& byte : data) AddToChecksum(byte); } - void StBootloaderCommandHandlerUart::TransmitChecksummedBuffer::AddToChecksum(const uint8_t& byte) + void StBootloaderCommunicatorUart::TransmitChecksummedBuffer::AddToChecksum(const uint8_t& byte) { checksum ^= byte; } - void StBootloaderCommandHandlerUart::TransmitChecksummedBuffer::Start() + void StBootloaderCommunicatorUart::TransmitChecksummedBuffer::Start() { if (data.empty()) handler.SendData(infra::MakeConstByteRange(checksum)); @@ -455,34 +455,34 @@ namespace services handler.SendData(data, checksum); } - StBootloaderCommandHandlerUart::TransmitSmallBuffer::TransmitSmallBuffer(StBootloaderCommandHandlerUart& handler, infra::ConstByteRange data) - : StBootloaderCommandHandlerUart::TransmitChecksummedBuffer(handler, data) + StBootloaderCommunicatorUart::TransmitSmallBuffer::TransmitSmallBuffer(StBootloaderCommunicatorUart& handler, infra::ConstByteRange data) + : StBootloaderCommunicatorUart::TransmitChecksummedBuffer(handler, data) { size = data.size() - 1; AddToChecksum(size); } - void StBootloaderCommandHandlerUart::TransmitSmallBuffer::Start() + void StBootloaderCommunicatorUart::TransmitSmallBuffer::Start() { handler.serial.SendData(infra::MakeConstByteRange(size), [this]() { - StBootloaderCommandHandlerUart::TransmitChecksummedBuffer::Start(); + StBootloaderCommunicatorUart::TransmitChecksummedBuffer::Start(); }); } - StBootloaderCommandHandlerUart::TransmitBigBuffer::TransmitBigBuffer(StBootloaderCommandHandlerUart& handler, infra::ConstByteRange data, uint16_t size) - : StBootloaderCommandHandlerUart::TransmitChecksummedBuffer(handler, data) + StBootloaderCommunicatorUart::TransmitBigBuffer::TransmitBigBuffer(StBootloaderCommunicatorUart& handler, infra::ConstByteRange data, uint16_t size) + : StBootloaderCommunicatorUart::TransmitChecksummedBuffer(handler, data) , size(size) { AddToChecksum(static_cast(size >> 8)); AddToChecksum(static_cast(size & 0xff)); } - void StBootloaderCommandHandlerUart::TransmitBigBuffer::Start() + void StBootloaderCommunicatorUart::TransmitBigBuffer::Start() { handler.serial.SendData(infra::MakeConstByteRange(size), [this]() { - StBootloaderCommandHandlerUart::TransmitChecksummedBuffer::Start(); + StBootloaderCommunicatorUart::TransmitChecksummedBuffer::Start(); }); } } diff --git a/services/st_util/StBootloaderCommandHandlerUart.hpp b/services/st_util/StBootloaderCommunicatorUart.hpp similarity index 81% rename from services/st_util/StBootloaderCommandHandlerUart.hpp rename to services/st_util/StBootloaderCommunicatorUart.hpp index 364c0c76..32327688 100644 --- a/services/st_util/StBootloaderCommandHandlerUart.hpp +++ b/services/st_util/StBootloaderCommunicatorUart.hpp @@ -10,18 +10,18 @@ #include "infra/util/ByteRange.hpp" #include "infra/util/Endian.hpp" #include "infra/util/PolymorphicVariant.hpp" -#include "services/st_util/StBootloaderCommandHandler.hpp" +#include "services/st_util/StBootloaderCommunicator.hpp" namespace services { - class StBootloaderCommandHandlerUart - : protected StBootloaderCommandHandler + class StBootloaderCommunicatorUart + : protected StBootloaderCommunicator { public: - StBootloaderCommandHandlerUart(hal::SerialCommunication& serial, const infra::Function& onInitialized, const infra::Function& onError); - virtual ~StBootloaderCommandHandlerUart(); + StBootloaderCommunicatorUart(hal::SerialCommunication& serial, const infra::Function& onInitialized, const infra::Function& onError); + virtual ~StBootloaderCommunicatorUart(); - // Implementation of StBootloaderCommandHandler + // Implementation of StBootloaderCommunicator void GetCommand(infra::ByteRange& commands, const infra::Function& onDone) override; void GetVersion(const infra::Function& onDone) override; void GetId(const infra::Function& onDone) override; @@ -41,7 +41,7 @@ namespace services template void AddCommandAction(Args&&... args); - void ExecuteCommand(const infra::Function) + sizeof(infra::ByteRange)>& onCommandExecuted); + void ExecuteCommand(const infra::Function) + sizeof(infra::ByteRange)>& onCommandExecuted); void StartCurrentAction(); void TryHandleDataReceived(); void OnCommandExecuted(); @@ -55,7 +55,7 @@ namespace services class Action { public: - Action(StBootloaderCommandHandlerUart& handler); + Action(StBootloaderCommunicatorUart& handler); Action(const Action& other) = delete; Action& operator=(const Action& other) = delete; virtual ~Action() = default; @@ -64,7 +64,7 @@ namespace services virtual void DataReceived(); protected: - StBootloaderCommandHandlerUart& handler; + StBootloaderCommunicatorUart& handler; }; class ReceiveAck @@ -80,7 +80,7 @@ namespace services : public Action { public: - ReceiveBuffer(StBootloaderCommandHandlerUart& handler, infra::ByteRange& data); + ReceiveBuffer(StBootloaderCommunicatorUart& handler, infra::ByteRange& data); void DataReceived() override; @@ -102,7 +102,7 @@ namespace services : public ReceiveBuffer { public: - ReceivePredefinedBuffer(StBootloaderCommandHandlerUart& handler, infra::ByteRange& data, const std::size_t size); + ReceivePredefinedBuffer(StBootloaderCommunicatorUart& handler, infra::ByteRange& data, const std::size_t size); protected: void TryRetreiveNumberOfBytes(infra::DataInputStream& stream) override; @@ -135,7 +135,7 @@ namespace services : public Action { public: - TransmitRaw(StBootloaderCommandHandlerUart& handler, infra::ConstByteRange data); + TransmitRaw(StBootloaderCommunicatorUart& handler, infra::ConstByteRange data); void Start() override; @@ -147,7 +147,7 @@ namespace services : public Action { public: - TransmitWithTwosComplementChecksum(StBootloaderCommandHandlerUart& handler, uint8_t data); + TransmitWithTwosComplementChecksum(StBootloaderCommunicatorUart& handler, uint8_t data); void Start() override; @@ -159,7 +159,7 @@ namespace services : public Action { public: - TransmitChecksummedBuffer(StBootloaderCommandHandlerUart& handler, infra::ConstByteRange data); + TransmitChecksummedBuffer(StBootloaderCommunicatorUart& handler, infra::ConstByteRange data); void Start() override; @@ -175,7 +175,7 @@ namespace services : public TransmitChecksummedBuffer { public: - TransmitSmallBuffer(StBootloaderCommandHandlerUart& handler, infra::ConstByteRange data); + TransmitSmallBuffer(StBootloaderCommunicatorUart& handler, infra::ConstByteRange data); void Start() override; @@ -187,7 +187,7 @@ namespace services : public TransmitChecksummedBuffer { public: - TransmitBigBuffer(StBootloaderCommandHandlerUart& handler, infra::ConstByteRange data, uint16_t size); + TransmitBigBuffer(StBootloaderCommunicatorUart& handler, infra::ConstByteRange data, uint16_t size); void Start() override; @@ -202,7 +202,7 @@ namespace services infra::QueueForOneReaderOneIrqWriter::WithStorage<257> queue; infra::BoundedDeque>::WithMaxSize<12> commandActions; - infra::AutoResetFunction) + sizeof(infra::ByteRange)> onCommandExecuted; + infra::AutoResetFunction) + sizeof(infra::ByteRange)> onCommandExecuted; infra::BoundedString::WithStorage<46> timeoutReason; infra::TimerSingleShot timeout; diff --git a/services/st_util/test/CMakeLists.txt b/services/st_util/test/CMakeLists.txt index ca4563a8..68d21e9e 100644 --- a/services/st_util/test/CMakeLists.txt +++ b/services/st_util/test/CMakeLists.txt @@ -2,7 +2,7 @@ add_executable(services.st_util_test) emil_add_test(services.st_util_test) target_sources(services.st_util_test PRIVATE - TestStBootloaderCommandHandlerUart.cpp + TestStBootloaderCommunicatorUart.cpp ) target_link_libraries(services.st_util_test PUBLIC diff --git a/services/st_util/test/TestStBootloaderCommandHandlerUart.cpp b/services/st_util/test/TestStBootloaderCommunicatorUart.cpp similarity index 89% rename from services/st_util/test/TestStBootloaderCommandHandlerUart.cpp rename to services/st_util/test/TestStBootloaderCommunicatorUart.cpp index 6c16632c..286e5ac1 100644 --- a/services/st_util/test/TestStBootloaderCommandHandlerUart.cpp +++ b/services/st_util/test/TestStBootloaderCommunicatorUart.cpp @@ -2,7 +2,7 @@ #include "infra/timer/test_helper/ClockFixture.hpp" #include "infra/util/MemoryRange.hpp" #include "infra/util/test_helper/MockCallback.hpp" -#include "services/st_util/StBootloaderCommandHandlerUart.hpp" +#include "services/st_util/StBootloaderCommunicatorUart.hpp" #include "gmock/gmock.h" #include "gtest/gtest.h" #include @@ -10,7 +10,7 @@ #include #include -class StBootloaderCommandHandlerUartTest +class StBootloaderCommunicatorUartTest : public testing::Test , public infra::ClockFixture { @@ -90,22 +90,22 @@ class StBootloaderCommandHandlerUartTest { ExpectSendData({ 0x7f }); } }; - services::StBootloaderCommandHandlerUart handler{ serialCommunication, onInitialized, onError }; + services::StBootloaderCommunicatorUart handler{ serialCommunication, onInitialized, onError }; }; -TEST_F(StBootloaderCommandHandlerUartTest, creation_initializes_uart_bootloader) +TEST_F(StBootloaderCommunicatorUartTest, creation_initializes_uart_bootloader) { Initialize(); } -TEST_F(StBootloaderCommandHandlerUartTest, creation_initializes_uart_bootloader_fails) +TEST_F(StBootloaderCommunicatorUartTest, creation_initializes_uart_bootloader_fails) { infra::BoundedConstString::WithStorage<64> errorMessage("Timeout waiting for bootloader initialization"); EXPECT_CALL(onErrorMock, callback(errorMessage)); ForwardTime(std::chrono::milliseconds(1000)); } -TEST_F(StBootloaderCommandHandlerUartTest, receive_nack) +TEST_F(StBootloaderCommunicatorUartTest, receive_nack) { Initialize(); testing::StrictMock> ondone; @@ -121,7 +121,7 @@ TEST_F(StBootloaderCommandHandlerUartTest, receive_nack) ReceiveData({ 0x1f }); } -TEST_F(StBootloaderCommandHandlerUartTest, GetCommand) +TEST_F(StBootloaderCommunicatorUartTest, GetCommand) { Initialize(); infra::VerifyingFunctionMock ondone(3, 2); @@ -136,7 +136,7 @@ TEST_F(StBootloaderCommandHandlerUartTest, GetCommand) EXPECT_EQ((std::array{ 0x00, 0x01, 0x02, 0x11, 0x21, 0x31, 0x44, 0x63, 0x73, 0x82, 0x92 }), commandsbuffer); } -TEST_F(StBootloaderCommandHandlerUartTest, GetCommand_receive_in_parts) +TEST_F(StBootloaderCommunicatorUartTest, GetCommand_receive_in_parts) { Initialize(); infra::VerifyingFunctionMock ondone(3, 2); @@ -151,7 +151,7 @@ TEST_F(StBootloaderCommandHandlerUartTest, GetCommand_receive_in_parts) EXPECT_EQ((std::array{ 0x00, 0x01, 0x02, 0x11, 0x21, 0x31, 0x44, 0x63, 0x73, 0x82, 0x92 }), commandsbuffer); } -TEST_F(StBootloaderCommandHandlerUartTest, GetCommand_timeout) +TEST_F(StBootloaderCommunicatorUartTest, GetCommand_timeout) { Initialize(); testing::StrictMock> ondone; @@ -166,7 +166,7 @@ TEST_F(StBootloaderCommandHandlerUartTest, GetCommand_timeout) ExpectErrorOnTimeout(std::chrono::seconds(1), "Timeout getting commands"); } -TEST_F(StBootloaderCommandHandlerUartTest, GetVersion) +TEST_F(StBootloaderCommunicatorUartTest, GetVersion) { Initialize(); infra::VerifyingFunctionMock ondone(3, 1); @@ -176,7 +176,7 @@ TEST_F(StBootloaderCommandHandlerUartTest, GetVersion) ReceiveData({ 0x79, 0x31, 0x00, 0x00, 0x79 }); } -TEST_F(StBootloaderCommandHandlerUartTest, GetVersion_timeout) +TEST_F(StBootloaderCommunicatorUartTest, GetVersion_timeout) { Initialize(); testing::StrictMock> ondone; @@ -189,7 +189,7 @@ TEST_F(StBootloaderCommandHandlerUartTest, GetVersion_timeout) ExpectErrorOnTimeout(std::chrono::seconds(1), "Timeout getting version"); } -TEST_F(StBootloaderCommandHandlerUartTest, GetId) +TEST_F(StBootloaderCommunicatorUartTest, GetId) { Initialize(); infra::VerifyingFunctionMock ondone(0x0495); @@ -199,7 +199,7 @@ TEST_F(StBootloaderCommandHandlerUartTest, GetId) ReceiveData({ 0x79, 0x01, 0x04, 0x95, 0x79 }); } -TEST_F(StBootloaderCommandHandlerUartTest, GetId_timeout) +TEST_F(StBootloaderCommunicatorUartTest, GetId_timeout) { Initialize(); testing::StrictMock> ondone; @@ -212,7 +212,7 @@ TEST_F(StBootloaderCommandHandlerUartTest, GetId_timeout) ExpectErrorOnTimeout(std::chrono::seconds(1), "Timeout getting id"); } -TEST_F(StBootloaderCommandHandlerUartTest, ReadMemory) +TEST_F(StBootloaderCommunicatorUartTest, ReadMemory) { Initialize(); infra::VerifyingFunctionMock ondone; @@ -235,7 +235,7 @@ TEST_F(StBootloaderCommandHandlerUartTest, ReadMemory) EXPECT_EQ((std::array{ 0x05, 0x06, 0x07 }), dataBuffer); } -TEST_F(StBootloaderCommandHandlerUartTest, ReadMemory_timeout) +TEST_F(StBootloaderCommunicatorUartTest, ReadMemory_timeout) { Initialize(); testing::StrictMock> ondone; @@ -250,7 +250,7 @@ TEST_F(StBootloaderCommandHandlerUartTest, ReadMemory_timeout) ExpectErrorOnTimeout(std::chrono::seconds(1), "Timeout reading memory"); } -TEST_F(StBootloaderCommandHandlerUartTest, Go) +TEST_F(StBootloaderCommunicatorUartTest, Go) { Initialize(); infra::VerifyingFunctionMock ondone; @@ -265,7 +265,7 @@ TEST_F(StBootloaderCommandHandlerUartTest, Go) ReceiveData({ 0x79 }); } -TEST_F(StBootloaderCommandHandlerUartTest, Go_timeout) +TEST_F(StBootloaderCommunicatorUartTest, Go_timeout) { Initialize(); testing::StrictMock> ondone; @@ -279,7 +279,7 @@ TEST_F(StBootloaderCommandHandlerUartTest, Go_timeout) ExpectErrorOnTimeout(std::chrono::seconds(1), "Timeout go"); } -TEST_F(StBootloaderCommandHandlerUartTest, WriteMemory) +TEST_F(StBootloaderCommunicatorUartTest, WriteMemory) { Initialize(); infra::VerifyingFunctionMock ondone; @@ -297,7 +297,7 @@ TEST_F(StBootloaderCommandHandlerUartTest, WriteMemory) ReceiveData({ 0x79 }); } -TEST_F(StBootloaderCommandHandlerUartTest, WriteMemory_timeout) +TEST_F(StBootloaderCommunicatorUartTest, WriteMemory_timeout) { Initialize(); testing::StrictMock> ondone; @@ -312,7 +312,7 @@ TEST_F(StBootloaderCommandHandlerUartTest, WriteMemory_timeout) ExpectErrorOnTimeout(std::chrono::seconds(1), "Timeout writing memory"); } -TEST_F(StBootloaderCommandHandlerUartTest, MassErase) +TEST_F(StBootloaderCommunicatorUartTest, MassErase) { Initialize(); infra::VerifyingFunctionMock ondone; @@ -324,7 +324,7 @@ TEST_F(StBootloaderCommandHandlerUartTest, MassErase) ReceiveData({ 0x79 }); } -TEST_F(StBootloaderCommandHandlerUartTest, MassErase_timeout) +TEST_F(StBootloaderCommunicatorUartTest, MassErase_timeout) { Initialize(); testing::StrictMock> ondone; @@ -339,7 +339,7 @@ TEST_F(StBootloaderCommandHandlerUartTest, MassErase_timeout) ExpectErrorOnTimeout(std::chrono::seconds(1), "Timeout mass erasing memory"); } -TEST_F(StBootloaderCommandHandlerUartTest, Erase_pages) +TEST_F(StBootloaderCommunicatorUartTest, Erase_pages) { Initialize(); infra::VerifyingFunctionMock ondone; @@ -352,7 +352,7 @@ TEST_F(StBootloaderCommandHandlerUartTest, Erase_pages) ReceiveData({ 0x79 }); } -TEST_F(StBootloaderCommandHandlerUartTest, Erase_timeout) +TEST_F(StBootloaderCommunicatorUartTest, Erase_timeout) { Initialize(); testing::StrictMock> ondone; @@ -366,7 +366,7 @@ TEST_F(StBootloaderCommandHandlerUartTest, Erase_timeout) ExpectErrorOnTimeout(std::chrono::seconds(1), "Timeout erasing memory"); } -TEST_F(StBootloaderCommandHandlerUartTest, ExtendedMassErase) +TEST_F(StBootloaderCommunicatorUartTest, ExtendedMassErase) { Initialize(); infra::VerifyingFunctionMock ondone; @@ -378,7 +378,7 @@ TEST_F(StBootloaderCommandHandlerUartTest, ExtendedMassErase) ReceiveData({ 0x79 }); } -TEST_F(StBootloaderCommandHandlerUartTest, ExtendedMassErase_timeout) +TEST_F(StBootloaderCommunicatorUartTest, ExtendedMassErase_timeout) { Initialize(); testing::StrictMock> ondone; @@ -393,7 +393,7 @@ TEST_F(StBootloaderCommandHandlerUartTest, ExtendedMassErase_timeout) ExpectErrorOnTimeout(std::chrono::seconds(1), "Timeout extended mass erasing memory"); } -TEST_F(StBootloaderCommandHandlerUartTest, ExtendedErase_pages) +TEST_F(StBootloaderCommunicatorUartTest, ExtendedErase_pages) { Initialize(); infra::VerifyingFunctionMock ondone; @@ -406,7 +406,7 @@ TEST_F(StBootloaderCommandHandlerUartTest, ExtendedErase_pages) ReceiveData({ 0x79 }); } -TEST_F(StBootloaderCommandHandlerUartTest, ExtendedErase_pages_timeout) +TEST_F(StBootloaderCommunicatorUartTest, ExtendedErase_pages_timeout) { Initialize(); testing::StrictMock> ondone; @@ -420,7 +420,7 @@ TEST_F(StBootloaderCommandHandlerUartTest, ExtendedErase_pages_timeout) ExpectErrorOnTimeout(std::chrono::seconds(1), "Timeout extended erasing memory"); } -TEST_F(StBootloaderCommandHandlerUartTest, Special) +TEST_F(StBootloaderCommunicatorUartTest, Special) { Initialize(); infra::VerifyingFunctionMock ondone; @@ -449,7 +449,7 @@ TEST_F(StBootloaderCommandHandlerUartTest, Special) EXPECT_EQ((std::array{ 0x09, 0x0a, 0x0b, 0x0c }), rxStatusBuffer); } -TEST_F(StBootloaderCommandHandlerUartTest, Special_empty_rxData) +TEST_F(StBootloaderCommunicatorUartTest, Special_empty_rxData) { Initialize(); infra::VerifyingFunctionMock ondone; @@ -478,7 +478,7 @@ TEST_F(StBootloaderCommandHandlerUartTest, Special_empty_rxData) EXPECT_EQ((std::array{ 0x09, 0x0a, 0x0b, 0x0c }), rxStatusBuffer); } -TEST_F(StBootloaderCommandHandlerUartTest, Special_empty_rxStatus) +TEST_F(StBootloaderCommunicatorUartTest, Special_empty_rxStatus) { Initialize(); infra::VerifyingFunctionMock ondone; @@ -507,7 +507,7 @@ TEST_F(StBootloaderCommandHandlerUartTest, Special_empty_rxStatus) EXPECT_EQ((std::array{}), rxStatusBuffer); } -TEST_F(StBootloaderCommandHandlerUartTest, Special_empty_rxData_rxStatus) +TEST_F(StBootloaderCommunicatorUartTest, Special_empty_rxData_rxStatus) { Initialize(); infra::VerifyingFunctionMock ondone; @@ -536,7 +536,7 @@ TEST_F(StBootloaderCommandHandlerUartTest, Special_empty_rxData_rxStatus) EXPECT_EQ((std::array{}), rxStatusBuffer); } -TEST_F(StBootloaderCommandHandlerUartTest, Special_empty_txData) +TEST_F(StBootloaderCommunicatorUartTest, Special_empty_txData) { Initialize(); infra::VerifyingFunctionMock ondone; @@ -564,7 +564,7 @@ TEST_F(StBootloaderCommandHandlerUartTest, Special_empty_txData) EXPECT_EQ((std::array{ 0x09, 0x0a, 0x0b, 0x0c }), rxStatusBuffer); } -TEST_F(StBootloaderCommandHandlerUartTest, Special_timeout) +TEST_F(StBootloaderCommunicatorUartTest, Special_timeout) { Initialize(); testing::StrictMock> ondone; @@ -579,7 +579,7 @@ TEST_F(StBootloaderCommandHandlerUartTest, Special_timeout) ExpectErrorOnTimeout(std::chrono::seconds(1), "Timeout special command"); } -TEST_F(StBootloaderCommandHandlerUartTest, ExtendedSpecial) +TEST_F(StBootloaderCommunicatorUartTest, ExtendedSpecial) { Initialize(); infra::VerifyingFunctionMock ondone; @@ -608,7 +608,7 @@ TEST_F(StBootloaderCommandHandlerUartTest, ExtendedSpecial) EXPECT_EQ((std::array{ 0x09, 0x0a, 0x0b, 0x0c }), rxDataBuffer); } -TEST_F(StBootloaderCommandHandlerUartTest, ExtendedSpecial_timeout) +TEST_F(StBootloaderCommunicatorUartTest, ExtendedSpecial_timeout) { Initialize(); testing::StrictMock> ondone; @@ -622,7 +622,7 @@ TEST_F(StBootloaderCommandHandlerUartTest, ExtendedSpecial_timeout) ExpectErrorOnTimeout(std::chrono::seconds(1), "Timeout extended special command"); } -TEST_F(StBootloaderCommandHandlerUartTest, receive_data_with_wrapped_around_queue) +TEST_F(StBootloaderCommunicatorUartTest, receive_data_with_wrapped_around_queue) { Initialize(); infra::VerifyingFunctionMock ondone; @@ -662,7 +662,7 @@ TEST_F(StBootloaderCommandHandlerUartTest, receive_data_with_wrapped_around_queu EXPECT_EQ((std::array{ 0x09, 0x0a, 0x0b, 0x0c }), rxStatusBuffer); } -TEST_F(StBootloaderCommandHandlerUartTest, Special_receive_buffer_size_received_in_parts) +TEST_F(StBootloaderCommunicatorUartTest, Special_receive_buffer_size_received_in_parts) { Initialize(); infra::VerifyingFunctionMock ondone;