Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: make terminal's hardcoded sizes configurable #798

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 7 additions & 6 deletions services/util/Terminal.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@

namespace services
{
Terminal::Terminal(hal::SerialCommunication& communication, services::Tracer& tracer)
: tracer(tracer)
, queue([this]
Terminal::Terminal(infra::MemoryRange<uint8_t> bufferQueue, infra::BoundedDeque<infra::BoundedString::WithStorage<MaxBuffer>>& history, hal::SerialCommunication& communication, services::Tracer& tracer)
: queue(bufferQueue, [this]
{
HandleInput();
})
, history(history)
, tracer(tracer)
{
communication.ReceiveData([this](infra::ConstByteRange data)
{
Expand Down Expand Up @@ -212,7 +213,7 @@ namespace services
void Terminal::OverwriteBuffer(infra::BoundedConstString element)
{
std::size_t previousSize = buffer.size();
buffer = element;
buffer.assign(element);

tracer.Continue() << '\r';
Print(state.prompt);
Expand Down Expand Up @@ -283,8 +284,8 @@ namespace services
return false;
}

TerminalWithCommandsImpl::TerminalWithCommandsImpl(hal::SerialCommunication& communication, services::Tracer& tracer)
: services::Terminal(communication, tracer)
TerminalWithCommandsImpl::TerminalWithCommandsImpl(infra::MemoryRange<uint8_t> bufferQueue, infra::BoundedDeque<infra::BoundedString::WithStorage<MaxBuffer>>& history, hal::SerialCommunication& communication, services::Tracer& tracer)
: services::Terminal(bufferQueue, history, communication, tracer)
{}

void TerminalWithCommandsImpl::OnData(infra::BoundedConstString data)
Expand Down
19 changes: 14 additions & 5 deletions services/util/Terminal.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,19 @@
#include "infra/util/BoundedString.hpp"
#include "infra/util/Observer.hpp"
#include "services/tracer/Tracer.hpp"
#include <cstddef>

namespace services
{
class Terminal
{
public:
explicit Terminal(hal::SerialCommunication& communication, services::Tracer& tracer);
constexpr static std::size_t MaxBuffer = 256;

template<std::size_t MaxQueueSize = 32, std::size_t MaxHistory = 4>
using WithMaxQueueAndMaxHistory = infra::WithStorage<infra::WithStorage<Terminal, std::array<uint8_t, MaxQueueSize + 1>>, infra::BoundedDeque<infra::BoundedString::WithStorage<MaxBuffer>>::WithMaxSize<MaxHistory>>;

explicit Terminal(infra::MemoryRange<uint8_t> bufferQueue, infra::BoundedDeque<infra::BoundedString::WithStorage<MaxBuffer>>& history, hal::SerialCommunication& communication, services::Tracer& tracer);

void Print(const char* message);

Expand Down Expand Up @@ -54,11 +60,11 @@ namespace services
};

private:
infra::QueueForOneReaderOneIrqWriter<uint8_t> queue;
infra::BoundedString::WithStorage<MaxBuffer> buffer;
infra::BoundedDeque<decltype(buffer)>& history;
TerminalState state;
services::Tracer& tracer;
infra::QueueForOneReaderOneIrqWriter<uint8_t>::WithStorage<32> queue;
infra::BoundedString::WithStorage<256> buffer;
infra::BoundedDeque<decltype(buffer)>::WithMaxSize<4> history;
};

class TerminalWithCommands;
Expand Down Expand Up @@ -107,7 +113,10 @@ namespace services
, public Terminal
{
public:
TerminalWithCommandsImpl(hal::SerialCommunication& communication, services::Tracer& tracer);
template<std::size_t MaxQueueSize = 32, std::size_t MaxHistory = 4>
using WithMaxQueueAndMaxHistory = infra::WithStorage<infra::WithStorage<TerminalWithCommandsImpl, std::array<uint8_t, MaxQueueSize + 1>>, infra::BoundedDeque<infra::BoundedString::WithStorage<MaxBuffer>>::WithMaxSize<MaxHistory>>;

TerminalWithCommandsImpl(infra::MemoryRange<uint8_t> bufferQueue, infra::BoundedDeque<infra::BoundedString::WithStorage<MaxBuffer>>& history, hal::SerialCommunication& communication, services::Tracer& tracer);

private:
void OnData(infra::BoundedConstString data) override;
Expand Down
9 changes: 5 additions & 4 deletions services/util/test/TestTerminal.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
#include "hal/interfaces/test_doubles/SerialCommunicationMock.hpp"
#include "infra/event/test_helper/EventDispatcherWithWeakPtrFixture.hpp"
#include "infra/stream/OutputStream.hpp"
#include "infra/util/Optional.hpp"
#include "infra/util/test_helper/MockCallback.hpp"
#include "infra/util/test_helper/MockHelpers.hpp"
#include "services/util/Terminal.hpp"
Expand Down Expand Up @@ -43,14 +42,16 @@ class TerminalTest
: public TerminalTestBase
{
protected:
services::Terminal terminal{ communication, tracer };
services::Terminal::WithMaxQueueAndMaxHistory<> terminal{ communication, tracer };
};

class TerminalCommandsStub
: public services::TerminalCommands
{
public:
TerminalCommandsStub(services::TerminalWithCommands& terminal)
using services::TerminalCommands::TerminalCommands;

explicit TerminalCommandsStub(services::TerminalWithCommands& terminal)
: services::TerminalCommands(terminal)
{}

Expand All @@ -72,7 +73,7 @@ class TerminalWithCommandsTest
: public TerminalTestBase
{
protected:
services::TerminalWithCommandsImpl terminal{ communication, tracer };
services::TerminalWithCommandsImpl::WithMaxQueueAndMaxHistory<> terminal{ communication, tracer };
TerminalCommandsStub commands{ terminal };
};

Expand Down
Loading