Skip to content

Improve serial command handling #319

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

Open
wants to merge 12 commits into
base: develop
Choose a base branch
from
84 changes: 84 additions & 0 deletions include/serial/SerialBuffer.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
#pragma once

#include <cstdint>
#include <cstring>
#include <string_view>

#include "Common.h"

namespace OpenShock::Serial {
class SerialBuffer {
DISABLE_COPY(SerialBuffer);
DISABLE_MOVE(SerialBuffer);

public:
constexpr SerialBuffer()
: m_data(nullptr)
, m_size(0)
, m_capacity(0)
{
}
SerialBuffer(size_t capacity)
: m_data(new char[capacity])
, m_size(0)
, m_capacity(capacity)
{
}
~SerialBuffer() { delete[] m_data; }

constexpr char* data() { return m_data; }
constexpr size_t size() const { return m_size; }
constexpr size_t capacity() const { return m_capacity; }
constexpr bool empty() const { return m_size == 0; }

constexpr void clear() { m_size = 0; }
void destroy()
{
delete[] m_data;
m_data = nullptr;
m_size = 0;
m_capacity = 0;
}

void reserve(size_t size)
{
size = (size + 31) & ~31; // Align to 32 bytes

if (size <= m_capacity) {
return;
}

char* newData = new char[size];
if (m_data != nullptr) {
memcpy(newData, m_data, m_size);
delete[] m_data;
}

m_data = newData;
m_capacity = size;
}

void push_back(char c)
{
if (m_size >= m_capacity) {
reserve(m_capacity + 16);
}

m_data[m_size++] = c;
}

constexpr void pop_back()
{
if (m_size > 0) {
--m_size;
}
}

constexpr operator std::string_view() const { return std::string_view(m_data, m_size); }

private:
char* m_data;
size_t m_size;
size_t m_capacity;
};
} // namespace OpenShock::Serial
2 changes: 1 addition & 1 deletion include/serial/SerialInputHandler.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@ namespace OpenShock::SerialInputHandler {
bool SerialEchoEnabled();
void SetSerialEchoEnabled(bool enabled);

void PrintWelcomeHeader();
void PrintBootInfo();
void PrintVersionInfo();
} // namespace OpenShock::SerialInputHandler
10 changes: 10 additions & 0 deletions include/serial/SerialReadResult.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#pragma once

namespace OpenShock::Serial {
enum class SerialReadResult {
NoData,
Data,
LineEnd,
AutoCompleteRequest,
};
} // namespace OpenShock::Serial
14 changes: 14 additions & 0 deletions include/serial/command_handlers/CommandArgument.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#pragma once

#include <string_view>
#include <vector>

namespace OpenShock::Serial {
class CommandArgument {
public:
std::string_view name;
std::string_view constraint;
std::string_view exampleValue;
std::vector<std::string_view> constraintExtensions;
};
} // namespace OpenShock::Serial
33 changes: 3 additions & 30 deletions include/serial/command_handlers/CommandEntry.h
Original file line number Diff line number Diff line change
@@ -1,19 +1,12 @@
#pragma once

#include "CommandArgument.h"
#include "CommandHandler.h"

#include <string_view>
#include <vector>

namespace OpenShock::Serial {
typedef void (*CommandHandler)(std::string_view arg, bool isAutomated);

class CommandArgument {
public:
std::string_view name;
std::string_view constraint;
std::string_view exampleValue;
std::vector<std::string_view> constraintExtensions;
};

class CommandEntry {
public:
CommandEntry(std::string_view description, CommandHandler commandHandler);
Expand All @@ -32,24 +25,4 @@ namespace OpenShock::Serial {
std::vector<CommandArgument> m_arguments;
CommandHandler m_commandHandler;
};

class CommandGroup {
public:
CommandGroup() = default;
CommandGroup(std::string_view name);
CommandGroup(CommandGroup&& other) = default;
CommandGroup(const CommandGroup& other) = default;
CommandGroup& operator=(CommandGroup&& other) = default;
CommandGroup& operator=(const CommandGroup& other) = default;

inline std::string_view name() const { return m_name; }
inline const std::vector<CommandEntry>& commands() const { return m_commands; }

CommandEntry& addCommand(std::string_view description, CommandHandler commandHandler);
CommandEntry& addCommand(std::string_view name, std::string_view description, CommandHandler commandHandler);

private:
std::string_view m_name;
std::vector<CommandEntry> m_commands;
};
} // namespace OpenShock::Serial
29 changes: 29 additions & 0 deletions include/serial/command_handlers/CommandGroup.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#pragma once

#include "CommandEntry.h"
#include "CommandHandler.h"

#include <string_view>
#include <vector>

namespace OpenShock::Serial {
class CommandGroup {
public:
CommandGroup() = default;
CommandGroup(std::string_view name);
CommandGroup(CommandGroup&& other) = default;
CommandGroup(const CommandGroup& other) = default;
CommandGroup& operator=(CommandGroup&& other) = default;
CommandGroup& operator=(const CommandGroup& other) = default;

constexpr std::string_view name() const { return m_name; }
const std::vector<CommandEntry>& commands() const { return m_commands; }

CommandEntry& addCommand(std::string_view description, CommandHandler commandHandler);
CommandEntry& addCommand(std::string_view name, std::string_view description, CommandHandler commandHandler);

private:
std::string_view m_name;
std::vector<CommandEntry> m_commands;
};
} // namespace OpenShock::Serial
7 changes: 7 additions & 0 deletions include/serial/command_handlers/CommandHandler.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#pragma once

#include <string_view>

namespace OpenShock::Serial {
typedef void (*CommandHandler)(std::string_view arg, bool isAutomated);
} // namespace OpenShock::Serial
2 changes: 1 addition & 1 deletion include/serial/command_handlers/index.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#pragma once

#include "serial/command_handlers/CommandEntry.h"
#include "serial/command_handlers/CommandGroup.h"

#include <vector>

Expand Down
Loading
Loading