diff options
author | Aki <please@ignore.pl> | 2021-10-21 21:16:14 +0200 |
---|---|---|
committer | Aki <please@ignore.pl> | 2021-10-21 21:19:35 +0200 |
commit | 4806f1efa843d690fcd3d0a721291adbc7e96a79 (patch) | |
tree | 8c0e25ca969ff0bc8372b02f3403eafae2028303 /daemon/src | |
parent | 17f3ab464da4832861fc16b3886c098c424ed5dd (diff) | |
download | hwd-4806f1efa843d690fcd3d0a721291adbc7e96a79.zip hwd-4806f1efa843d690fcd3d0a721291adbc7e96a79.tar.gz hwd-4806f1efa843d690fcd3d0a721291adbc7e96a79.tar.bz2 |
Added memory to the daemon state
Diffstat (limited to 'daemon/src')
-rw-r--r-- | daemon/src/Memory.cpp | 41 | ||||
-rw-r--r-- | daemon/src/Memory.h | 22 | ||||
-rw-r--r-- | daemon/src/daemon.cpp | 7 |
3 files changed, 69 insertions, 1 deletions
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(); } |