-
-
Notifications
You must be signed in to change notification settings - Fork 31
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #229 from 107-systems/port-id-config-reg
[example/host] Port ID shall be configurable via register.
- Loading branch information
Showing
5 changed files
with
243 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
87 changes: 87 additions & 0 deletions
87
examples/host-example-01-opencyphal-basic-node/kv_host.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
/** | ||
* This software is distributed under the terms of the MIT License. | ||
* Copyright (c) 2023 LXRobotics. | ||
* Author: Alexander Entinger <[email protected]> | ||
* Contributors: https://github.com/107-systems/107-Arduino-Cyphal-Support/graphs/contributors. | ||
*/ | ||
|
||
/************************************************************************************** | ||
* INCLUDE | ||
**************************************************************************************/ | ||
|
||
#include "kv_host.h" | ||
|
||
#if !defined(__GNUC__) || (__GNUC__ >= 11) | ||
|
||
#include <cstdio> | ||
|
||
#include <sstream> | ||
#include <fstream> | ||
|
||
/************************************************************************************** | ||
* NAMESPACE | ||
**************************************************************************************/ | ||
|
||
namespace cyphal::support::platform::storage::host | ||
{ | ||
|
||
/************************************************************************************** | ||
* PRIVATE FREE FUNCTIONS | ||
**************************************************************************************/ | ||
|
||
[[nodiscard]] static inline std::string toFilename(std::string_view const key) | ||
{ | ||
auto const key_hash = std::hash<std::string_view>{}(key); | ||
std::stringstream key_filename; | ||
key_filename << key_hash; | ||
return key_filename.str(); | ||
} | ||
|
||
/************************************************************************************** | ||
* PUBLIC MEMBER FUNCTIONS | ||
**************************************************************************************/ | ||
|
||
auto KeyValueStorage::get(const std::string_view key, const std::size_t size, void* const data) const | ||
-> std::variant<Error, std::size_t> | ||
{ | ||
std::ifstream in(toFilename(key), std::ifstream::in | std::ifstream::binary); | ||
|
||
if (!in.good()) | ||
return Error::Existence; | ||
|
||
in.read(static_cast<char *>(data), size); | ||
size_t const bytes_read = in ? size : in.gcount(); | ||
in.close(); | ||
|
||
return bytes_read; | ||
} | ||
|
||
auto KeyValueStorage::put(const std::string_view key, const std::size_t size, const void* const data) | ||
-> std::optional<Error> | ||
{ | ||
std::ofstream out(toFilename(key), std::ofstream::in | std::ofstream::binary | std::ofstream::trunc); | ||
|
||
if (!out.good()) | ||
return Error::Existence; | ||
|
||
out.write(static_cast<const char *>(data), size); | ||
out.close(); | ||
|
||
return std::nullopt; | ||
} | ||
|
||
auto KeyValueStorage::drop(const std::string_view key) -> std::optional<Error> | ||
{ | ||
if (std::remove(toFilename(key).c_str())) | ||
return Error::IO; | ||
|
||
return std::nullopt; | ||
} | ||
|
||
/************************************************************************************** | ||
* NAMESPACE | ||
**************************************************************************************/ | ||
|
||
} /* cyphal::support::platform::storage::host */ | ||
|
||
#endif /* !defined(__GNUC__) || (__GNUC__ >= 11) */ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
/** | ||
* This software is distributed under the terms of the MIT License. | ||
* Copyright (c) 2023 LXRobotics. | ||
* Author: Alexander Entinger <[email protected]> | ||
* Contributors: https://github.com/107-systems/107-Arduino-Cyphal-Support/graphs/contributors. | ||
*/ | ||
|
||
#pragma once | ||
|
||
/************************************************************************************** | ||
* INCLUDE | ||
**************************************************************************************/ | ||
|
||
#include <107-Arduino-Cyphal.h> | ||
|
||
#if !defined(__GNUC__) || (__GNUC__ >= 11) | ||
|
||
/************************************************************************************** | ||
* NAMESPACE | ||
**************************************************************************************/ | ||
|
||
namespace cyphal::support::platform::storage::host | ||
{ | ||
|
||
/************************************************************************************** | ||
* CLASS DECLARATION | ||
**************************************************************************************/ | ||
|
||
class KeyValueStorage final : public interface::KeyValueStorage | ||
{ | ||
public: | ||
KeyValueStorage() { } | ||
virtual ~KeyValueStorage() { } | ||
|
||
/// The return value is the number of bytes read into the buffer or the error. | ||
[[nodiscard]] virtual auto get(const std::string_view key, const std::size_t size, void* const data) const | ||
-> std::variant<Error, std::size_t> override; | ||
|
||
/// Existing data, if any, is replaced entirely. New file and its parent directories created implicitly. | ||
/// Either all or none of the data bytes are written. | ||
[[nodiscard]] virtual auto put(const std::string_view key, const std::size_t size, const void* const data) | ||
-> std::optional<Error> override; | ||
|
||
/// Remove key. If the key does not exist, the existence error is returned. | ||
[[nodiscard]] virtual auto drop(const std::string_view key) -> std::optional<Error> override; | ||
}; | ||
|
||
/************************************************************************************** | ||
* NAMESPACE | ||
**************************************************************************************/ | ||
|
||
} /* cyphal::support::platform::storage::host */ | ||
|
||
#endif /* !defined(__GNUC__) || (__GNUC__ >= 11) */ |