Skip to content
Open
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
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

Check warning on line 1 in include/serial/SerialBuffer.h

View workflow job for this annotation

GitHub Actions / C/C++ Linter

include/serial/SerialBuffer.h:1:1 [portability-avoid-pragma-once]

avoid 'pragma once' directive; use include guards instead

#include <cstdint>

Check failure on line 3 in include/serial/SerialBuffer.h

View workflow job for this annotation

GitHub Actions / C/C++ Linter

include/serial/SerialBuffer.h:3:10 [clang-diagnostic-error]

'cstdint' file not found
#include <cstring>
#include <string_view>

#include "Common.h"

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

Check warning on line 11 in include/serial/SerialBuffer.h

View workflow job for this annotation

GitHub Actions / C/C++ Linter

include/serial/SerialBuffer.h:11:5 [modernize-use-trailing-return-type]

use a trailing return type for this function
DISABLE_MOVE(SerialBuffer);

Check warning on line 12 in include/serial/SerialBuffer.h

View workflow job for this annotation

GitHub Actions / C/C++ Linter

include/serial/SerialBuffer.h:12:5 [modernize-use-trailing-return-type]

use a trailing return type for this function

public:
constexpr SerialBuffer()

Check warning on line 15 in include/serial/SerialBuffer.h

View workflow job for this annotation

GitHub Actions / C/C++ Linter

include/serial/SerialBuffer.h:15:15 [modernize-use-equals-default]

use '= default' to define a trivial default constructor

Check warning on line 15 in include/serial/SerialBuffer.h

View workflow job for this annotation

GitHub Actions / C/C++ Linter

include/serial/SerialBuffer.h:15:5 [cppcoreguidelines-pro-type-member-init]

constructor does not initialize these fields: m_size, m_capacity
: m_data(nullptr)
, m_size(0)
, m_capacity(0)
{
}
SerialBuffer(size_t capacity)

Check warning on line 21 in include/serial/SerialBuffer.h

View workflow job for this annotation

GitHub Actions / C/C++ Linter

include/serial/SerialBuffer.h:21:5 [cppcoreguidelines-pro-type-member-init]

constructor does not initialize these fields: m_size, m_capacity
: m_data(new char[capacity])
, m_size(0)
, m_capacity(capacity)
{
}
~SerialBuffer() { delete[] m_data; }

Check warning on line 27 in include/serial/SerialBuffer.h

View workflow job for this annotation

GitHub Actions / C/C++ Linter

include/serial/SerialBuffer.h:27:5 [modernize-use-equals-default]

use '= default' to define a trivial destructor

constexpr char* data() { return m_data; }

Check warning on line 29 in include/serial/SerialBuffer.h

View workflow job for this annotation

GitHub Actions / C/C++ Linter

include/serial/SerialBuffer.h:29:21 [modernize-use-trailing-return-type]

use a trailing return type for this function
constexpr size_t size() const { return m_size; }

Check warning on line 30 in include/serial/SerialBuffer.h

View workflow job for this annotation

GitHub Actions / C/C++ Linter

include/serial/SerialBuffer.h:30:22 [modernize-use-trailing-return-type]

use a trailing return type for this function

Check warning on line 30 in include/serial/SerialBuffer.h

View workflow job for this annotation

GitHub Actions / C/C++ Linter

include/serial/SerialBuffer.h:30:5 [modernize-use-nodiscard]

function 'size' should be marked [[nodiscard]]
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
@@ -1,6 +1,6 @@
#pragma once

#include <cstdint>

Check failure on line 3 in include/serial/SerialInputHandler.h

View workflow job for this annotation

GitHub Actions / C/C++ Linter

include/serial/SerialInputHandler.h:3:10 [clang-diagnostic-error]

'cstdint' file not found

// Making sense of the ESP32 Arduino core #defines
#if ARDUINO_USB_MODE && ARDUINO_USB_CDC_ON_BOOT
Expand Down Expand Up @@ -42,6 +42,6 @@
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>

Check failure on line 3 in include/serial/command_handlers/CommandArgument.h

View workflow job for this annotation

GitHub Actions / C/C++ Linter

include/serial/command_handlers/CommandArgument.h:3:10 [clang-diagnostic-error]

'string_view' file not found
#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
36 changes: 4 additions & 32 deletions include/serial/command_handlers/CommandEntry.h
Original file line number Diff line number Diff line change
@@ -1,28 +1,21 @@
#pragma once

Check warning on line 1 in include/serial/command_handlers/CommandEntry.h

View workflow job for this annotation

GitHub Actions / C/C++ Linter

include/serial/command_handlers/CommandEntry.h:1:1 [portability-avoid-pragma-once]

avoid 'pragma once' directive; use include guards instead

#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) noexcept;
CommandEntry(std::string_view name, std::string_view description, CommandHandler commandHandler) noexcept;

inline std::string_view name() const { return m_name; }

Check warning on line 15 in include/serial/command_handlers/CommandEntry.h

View workflow job for this annotation

GitHub Actions / C/C++ Linter

include/serial/command_handlers/CommandEntry.h:15:29 [modernize-use-trailing-return-type]

use a trailing return type for this function

Check warning on line 15 in include/serial/command_handlers/CommandEntry.h

View workflow job for this annotation

GitHub Actions / C/C++ Linter

include/serial/command_handlers/CommandEntry.h:15:5 [readability-redundant-inline-specifier]

function 'name' has inline specifier but is implicitly inlined

Check warning on line 15 in include/serial/command_handlers/CommandEntry.h

View workflow job for this annotation

GitHub Actions / C/C++ Linter

include/serial/command_handlers/CommandEntry.h:15:5 [modernize-use-nodiscard]

function 'name' should be marked [[nodiscard]]
inline std::string_view description() const { return m_description; }

Check warning on line 16 in include/serial/command_handlers/CommandEntry.h

View workflow job for this annotation

GitHub Actions / C/C++ Linter

include/serial/command_handlers/CommandEntry.h:16:29 [modernize-use-trailing-return-type]

use a trailing return type for this function

Check warning on line 16 in include/serial/command_handlers/CommandEntry.h

View workflow job for this annotation

GitHub Actions / C/C++ Linter

include/serial/command_handlers/CommandEntry.h:16:5 [readability-redundant-inline-specifier]

function 'description' has inline specifier but is implicitly inlined

Check warning on line 16 in include/serial/command_handlers/CommandEntry.h

View workflow job for this annotation

GitHub Actions / C/C++ Linter

include/serial/command_handlers/CommandEntry.h:16:5 [modernize-use-nodiscard]

function 'description' should be marked [[nodiscard]]
inline const std::vector<CommandArgument>& arguments() const { return m_arguments; }

Check warning on line 17 in include/serial/command_handlers/CommandEntry.h

View workflow job for this annotation

GitHub Actions / C/C++ Linter

include/serial/command_handlers/CommandEntry.h:17:48 [modernize-use-trailing-return-type]

use a trailing return type for this function

Check warning on line 17 in include/serial/command_handlers/CommandEntry.h

View workflow job for this annotation

GitHub Actions / C/C++ Linter

include/serial/command_handlers/CommandEntry.h:17:5 [readability-redundant-inline-specifier]

function 'arguments' has inline specifier but is implicitly inlined

Check warning on line 17 in include/serial/command_handlers/CommandEntry.h

View workflow job for this annotation

GitHub Actions / C/C++ Linter

include/serial/command_handlers/CommandEntry.h:17:5 [modernize-use-nodiscard]

function 'arguments' should be marked [[nodiscard]]
inline CommandHandler commandHandler() { return m_commandHandler; }
inline CommandHandler commandHandler() const { return m_commandHandler; }

CommandArgument& addArgument(std::string_view name, std::string_view constraint, std::string_view exampleValue, std::vector<std::string_view> constraintExtensions = {});

Expand All @@ -32,25 +25,4 @@
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 std::vector<CommandEntry>& commands() { return m_commands; }
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>

Check failure on line 3 in include/serial/command_handlers/CommandHandler.h

View workflow job for this annotation

GitHub Actions / C/C++ Linter

include/serial/command_handlers/CommandHandler.h:3:10 [clang-diagnostic-error]

'string_view' file not found

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