diff options
-rw-r--r-- | daemon/CMakeLists.txt | 1 | ||||
-rw-r--r-- | daemon/src/Memory.cpp | 41 | ||||
-rw-r--r-- | daemon/src/Memory.h | 22 | ||||
-rw-r--r-- | daemon/src/daemon.cpp | 7 |
4 files changed, 70 insertions, 1 deletions
diff --git a/daemon/CMakeLists.txt b/daemon/CMakeLists.txt index a68394c..4a310ec 100644 --- a/daemon/CMakeLists.txt +++ b/daemon/CMakeLists.txt @@ -3,6 +3,7 @@ add_executable(${PROJECT_NAME} src/Assembly.cpp src/daemon.cpp src/Gpio.cpp + src/Memory.cpp ) target_link_libraries(${PROJECT_NAME} PRIVATE rpclib::rpc diff --git a/daemon/src/Memory.cpp b/daemon/src/Memory.cpp new file mode 100644 index 0000000..ddd9e4c --- /dev/null +++ b/daemon/src/Memory.cpp @@ -0,0 +1,41 @@ +#include "Memory.h" + +#include <algorithm> +#include <cstddef> +#include <vector> + +#include "Assembly.h" + + +Memory::Memory() noexcept : + content {} +{} + + +void Memory::apply(AssemblyContext ctx) +{ + ctx.bind("/read", [&](std::size_t len, std::size_t off) -> std::vector<char> { return read(len, off); }); + ctx.bind("/write", [&](const std::vector<char> data, std::size_t off) -> bool { return write(data, off); }); +} + + +auto Memory::read(std::size_t len, std::size_t off) const -> std::vector<char> +{ + if (content.size() < off) + return {}; + auto real_len = std::min(len, content.size() - off); + auto data = content.data() + off; + return {data, data + real_len}; +} + + +bool Memory::write(const std::vector<char> data, std::size_t off) +{ + if (content.size() < off || data.empty()) + return false; + std::size_t in_data = 0; + std::size_t in_content = off; + for (; in_data < data.size() && in_content < content.size(); ++in_data, ++in_content) + content[in_content] = data[in_data]; + return true; +} diff --git a/daemon/src/Memory.h b/daemon/src/Memory.h new file mode 100644 index 0000000..2678355 --- /dev/null +++ b/daemon/src/Memory.h @@ -0,0 +1,22 @@ +#pragma once + +#include <array> +#include <cstddef> +#include <vector> + +#include "Assembly.h" + + +class Memory +{ +public: + Memory() noexcept; + void apply(AssemblyContext ctx); + + constexpr static std::size_t size = 10 * 1024; +private: + std::array<char, size> content; + + std::vector<char> read(std::size_t len, std::size_t off) const; + bool write(const std::vector<char> data, std::size_t off); +}; diff --git a/daemon/src/daemon.cpp b/daemon/src/daemon.cpp index 9c841f8..28b95a4 100644 --- a/daemon/src/daemon.cpp +++ b/daemon/src/daemon.cpp @@ -5,13 +5,18 @@ #include "Assembly.h" #include "Gpio.h" +#include "Memory.h" + + +static Gpio gpio; +static Memory memory; int main(int, char **) { rpc::server server(hwd::internal::port()); - Gpio gpio; Assembly assembly {server}; assembly.add("gpio", gpio); + assembly.add("memory", memory); server.run(); } |