|
| 1 | +// |
| 2 | +// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 3 | +// SPDX-License-Identifier: MIT |
| 4 | +// |
| 5 | + |
| 6 | +#ifndef OCVSMD_SDK_NODE_COMMAND_CLIENT_HPP_INCLUDED |
| 7 | +#define OCVSMD_SDK_NODE_COMMAND_CLIENT_HPP_INCLUDED |
| 8 | + |
| 9 | +#include "execution.hpp" |
| 10 | + |
| 11 | +#include <uavcan/node/ExecuteCommand_1_3.hpp> |
| 12 | + |
| 13 | +#include <cetl/pf17/cetlpf.hpp> |
| 14 | +#include <cetl/pf20/cetlpf.hpp> |
| 15 | + |
| 16 | +#include <chrono> |
| 17 | +#include <cstdint> |
| 18 | +#include <memory> |
| 19 | +#include <unordered_map> |
| 20 | + |
| 21 | +namespace ocvsmd |
| 22 | +{ |
| 23 | +namespace sdk |
| 24 | +{ |
| 25 | + |
| 26 | +/// Defines client side interface of the OCVSMD Node Exec Command component. |
| 27 | +/// |
| 28 | +class NodeCommandClient |
| 29 | +{ |
| 30 | +public: |
| 31 | + /// Defines the shared pointer type for the interface. |
| 32 | + /// |
| 33 | + using Ptr = std::shared_ptr<NodeCommandClient>; |
| 34 | + |
| 35 | + NodeCommandClient(NodeCommandClient&&) = delete; |
| 36 | + NodeCommandClient(const NodeCommandClient&) = delete; |
| 37 | + NodeCommandClient& operator=(NodeCommandClient&&) = delete; |
| 38 | + NodeCommandClient& operator=(const NodeCommandClient&) = delete; |
| 39 | + |
| 40 | + virtual ~NodeCommandClient() = default; |
| 41 | + |
| 42 | + /// Defines the result type of the command execution. |
| 43 | + /// |
| 44 | + /// On success, the result is a map of node IDs to their responses (`status` and `output` params). |
| 45 | + /// Missing Cyphal nodes (or failed to respond in a given timeout) are not included in the map. |
| 46 | + /// |
| 47 | + struct Command final |
| 48 | + { |
| 49 | + using NodeRequest = uavcan::node::ExecuteCommand_1_3::Request; |
| 50 | + using NodeResponse = uavcan::node::ExecuteCommand_1_3::Response; |
| 51 | + |
| 52 | + using Success = std::unordered_map<std::uint16_t, NodeResponse>; |
| 53 | + using Failure = int; // `errno`-like error code. |
| 54 | + using Result = cetl::variant<Success, Failure>; |
| 55 | + |
| 56 | + }; // Command |
| 57 | + |
| 58 | + /// Sends a Cyphal command to the specified Cyphal network nodes. |
| 59 | + /// |
| 60 | + /// On the OCVSMD engine side, the `node_request` is sent concurrently to all specified Cyphal nodes. |
| 61 | + /// Responses are sent back to the client side as they arrive. |
| 62 | + /// Result will be available when the last response has arrived, or the timeout has expired. |
| 63 | + /// |
| 64 | + /// @param node_ids The list of Cyphal node IDs to send the command to. Duplicates are ignored. |
| 65 | + /// @param node_request The Cyphal command request to send (aka broadcast) to the `node_ids`. |
| 66 | + /// @param timeout The maximum time to wait for all Cyphal node responses to arrive. |
| 67 | + /// @return An execution sender which emits the async overall result of the operation. |
| 68 | + /// |
| 69 | + virtual SenderOf<Command::Result>::Ptr sendCommand(const cetl::span<const std::uint16_t> node_ids, |
| 70 | + const Command::NodeRequest& node_request, |
| 71 | + const std::chrono::microseconds timeout) = 0; |
| 72 | + |
| 73 | + /// A convenience method for invoking `sendCommand` with COMMAND_RESTART. |
| 74 | + /// |
| 75 | + /// @param node_ids The list of Cyphal node IDs to send the command to. Duplicates are ignored. |
| 76 | + /// @param timeout The maximum time to wait for all Cyphal node responses to arrive. Default is 1 second. |
| 77 | + /// @return An execution sender which emits the async result of the operation. |
| 78 | + /// |
| 79 | + SenderOf<Command::Result>::Ptr restart( // |
| 80 | + const cetl::span<const std::uint16_t> node_ids, |
| 81 | + const std::chrono::microseconds timeout = std::chrono::seconds{1}); |
| 82 | + |
| 83 | + /// A convenience method for invoking `sendCommand` with COMMAND_BEGIN_SOFTWARE_UPDATE. |
| 84 | + /// |
| 85 | + /// @param node_ids The list of Cyphal node IDs to send the command to. Duplicates are ignored. |
| 86 | + /// @param file_path The path to the software update file. Limited to 255 characters. |
| 87 | + /// Relative to one of the roots configured in the file server. |
| 88 | + /// @param timeout The maximum time to wait for all Cyphal node responses to arrive. Default is 1 second. |
| 89 | + /// @return An execution sender which emits the async result of the operation. |
| 90 | + /// |
| 91 | + SenderOf<Command::Result>::Ptr beginSoftwareUpdate( // |
| 92 | + const cetl::span<const std::uint16_t> node_ids, |
| 93 | + const cetl::string_view file_path, |
| 94 | + const std::chrono::microseconds timeout = std::chrono::seconds{1}); |
| 95 | + |
| 96 | +protected: |
| 97 | + NodeCommandClient() = default; |
| 98 | + |
| 99 | + virtual cetl::pmr::memory_resource& getMemoryResource() const noexcept = 0; |
| 100 | + |
| 101 | +}; // NodeCommandClient |
| 102 | + |
| 103 | +} // namespace sdk |
| 104 | +} // namespace ocvsmd |
| 105 | + |
| 106 | +#endif // OCVSMD_SDK_NODE_COMMAND_CLIENT_HPP_INCLUDED |
0 commit comments