19#ifndef MODDEF_COMMAND_HPP
20#define MODDEF_COMMAND_HPP
32#include "moddef/command.h"
46template <std::
size_t N>
51 auto* p = slot(field);
53 p->kind = MD_CMD_PARAM_VALUE;
57 bool value(std::string_view field, std::int64_t v) {
return value(field,
Value{md_value_i64(v)}); }
58 bool value(std::string_view field,
double v) {
return value(field,
Value{md_value_f64(v)}); }
61 bool str(std::string_view field, std::string_view s) {
62 auto* p = slot(field);
64 p->kind = MD_CMD_PARAM_STR;
70 bool bytes(std::string_view field, std::span<const std::uint8_t> b) {
71 auto* p = slot(field);
73 p->kind = MD_CMD_PARAM_BYTES;
74 p->bytes = md_bytes_t{b.data(),
static_cast<std::uint32_t
>(b.size())};
78 const md_cmd_params_t*
c_params() const noexcept {
return &view_; }
86 md_cmd_param_t* slot(std::string_view field) {
87 if (n_ >= N)
return nullptr;
88 auto* p = &items_[n_++];
89 *p = md_cmd_param_t{};
90 p->field =
to_c(field);
91 view_ = {items_,
static_cast<std::uint8_t
>(n_)};
95 md_cmd_param_t items_[N]{};
97 md_cmd_params_t view_{items_, 0};
108 const md_cmd_params_t* params,
109 std::span<std::uint8_t> sink = {}) {
111 if (
auto e = md_cmd_begin(&ex.ex_, dev.
c_dev(),
to_c(command_id), params,
112 sink.data(), sink.size());
114 return std::unexpected(
from_c(e));
120 return static_cast<CommandStatus>(md_cmd_tick(&ex_, now_ms));
129 if (
auto e = md_cmd_result(&ex_,
to_c(field), &v); e != MD_OK)
130 return std::unexpected(
from_c(e));
136 const std::uint8_t* p =
nullptr;
138 if (
auto e = md_cmd_result_data(&ex_,
to_c(field), &p, &len); e != MD_OK)
139 return std::unexpected(
from_c(e));
140 return std::span<const std::uint8_t>{p, len};
One armed command run.
Definition command.hpp:102
CommandStatus tick(std::uint32_t now_ms) noexcept
Advance the run; now_ms is any monotonic millisecond clock.
Definition command.hpp:119
Result< std::span< const std::uint8_t > > result_data(std::string_view field) const
String/bytes result: a view into the caller's data sink.
Definition command.hpp:135
Error error() const noexcept
The failure cause after CommandStatus::Error (Error::Ok otherwise).
Definition command.hpp:124
CommandExecutor(CommandExecutor &&) noexcept=default
static Result< CommandExecutor > begin(Device &dev, std::string_view command_id, const md_cmd_params_t *params, std::span< std::uint8_t > sink={})
Locate command_id in the device profile, validate required params, and arm the run.
Definition command.hpp:107
Result< Value > result(std::string_view field) const
Numeric result by CommandResult.field, after Done.
Definition command.hpp:127
Fixed-capacity param set for CommandExecutor::begin.
Definition command.hpp:47
bool str(std::string_view field, std::string_view s)
STRING_ASCII / STRING_UTF8 param.
Definition command.hpp:61
bool bytes(std::string_view field, std::span< const std::uint8_t > b)
BYTES_RAW param.
Definition command.hpp:70
CommandParams & operator=(const CommandParams &)=delete
bool value(std::string_view field, Value v)
Numeric param (CommandParam.field -> md_value_t).
Definition command.hpp:50
const md_cmd_params_t * c_params() const noexcept
Definition command.hpp:78
bool value(std::string_view field, double v)
Definition command.hpp:58
CommandParams(const CommandParams &)=delete
bool value(std::string_view field, std::int64_t v)
Definition command.hpp:57
A device bound to a transport: read and write its points by id.
Definition device.hpp:32
md_dev_t * c_dev() noexcept
The underlying C device (command executor, advanced use).
Definition device.hpp:87
A decoded point value: a typed view over the C core's md_value_t.
Definition value.hpp:38
const md_value_t & raw() const noexcept
Definition value.hpp:44
Device driver (spec §32.4).
Error type and Result<T> (spec §26.3/§26.4, §32).
Definition command.hpp:35
Error
Strongly typed mirror of md_err_t (same underlying values, so casts are free in both directions).
Definition error.hpp:25
CommandStatus
Definition command.hpp:37
std::expected< T, Error > Result
The canonical result type across the API; Result<void> for actions.
Definition error.hpp:57
md_err_t to_c(Error e) noexcept
Definition error.hpp:51
Error from_c(md_err_t e) noexcept
Definition error.hpp:50
std::string_view <-> md_str_t bridging.
Decoded value (spec §8, §13).