moddef-c
Loading...
Searching...
No Matches
command.h
Go to the documentation of this file.
1// SPDX-License-Identifier: Apache-2.0
2
3/* Command executor (spec §11.7): multi-step register procedures — write
4 * caller params, arm a trigger register, poll a status register until a
5 * condition holds, read results whose size may only be known at runtime
6 * (length_ref, §11.7.1).
7 *
8 * Tick-driven, no allocation, no timers: the caller drives
9 *
10 * md_cmd_exec_t ex;
11 * md_cmd_begin(&ex, &dev, MD_STR("start_transaction"), &params, sink, cap);
12 * while (md_cmd_tick(&ex, now_ms()) < MD_CMD_DONE)
13 * ; // yield to your scheduler; WAITING_POLL means "call later"
14 *
15 * from its own scheduler, supplying a monotonic millisecond clock. The
16 * library never sleeps and never reads a wall clock — poll interval_ms /
17 * timeout_ms are tracked against the caller-supplied now_ms, so runs are
18 * deterministic and unit-testable.
19 *
20 * Transport writes/reads larger than one Modbus PDU are chunked
21 * (≤123-word writes; reads chunk at MD_MAX_POINT_WORDS). Chunked
22 * string/bytes transfers require word-big order (the overwhelming norm).
23 *
24 * String/bytes read-step results land **untrimmed** in the caller's data
25 * sink (the raw register window bytes); numeric results are md_value_t.
26 * Params reference caller storage and must outlive the run. */
27#ifndef MODDEF_COMMAND_H
28#define MODDEF_COMMAND_H
29
30#include "moddef/device.h"
31
32/* Read-step bindings held per run (ReadStep.into slots). */
33#ifndef MD_CMD_MAX_BINDINGS
34#define MD_CMD_MAX_BINDINGS 4
35#endif
36
37/* Modbus single-PDU practical write cap (FC16). */
38#ifndef MD_CMD_MAX_WRITE_WORDS
39#define MD_CMD_MAX_WRITE_WORDS 123
40#endif
41
42/* Default poll interval when a PollStep omits interval_ms. */
43#ifndef MD_CMD_DEFAULT_INTERVAL_MS
44#define MD_CMD_DEFAULT_INTERVAL_MS 250
45#endif
46
47typedef enum md_cmd_status {
48 MD_CMD_RUNNING = 0, /* made progress; tick again when convenient */
49 MD_CMD_WAITING_POLL, /* poll interval pending; tick again later */
51 MD_CMD_ERROR /* inspect md_cmd_error() */
53
54typedef enum md_cmd_param_kind {
55 MD_CMD_PARAM_VALUE = 0, /* numeric md_value_t */
56 MD_CMD_PARAM_STR, /* STRING_ASCII / STRING_UTF8 param */
57 MD_CMD_PARAM_BYTES /* BYTES_RAW param */
59
60typedef struct md_cmd_param {
61 md_str_t field; /* CommandParam.field this value binds to */
62 uint8_t kind; /* md_cmd_param_kind_t */
63 md_value_t value; /* MD_CMD_PARAM_VALUE */
64 md_str_t str; /* MD_CMD_PARAM_STR */
65 md_bytes_t bytes; /* MD_CMD_PARAM_BYTES */
67
68typedef struct md_cmd_params {
70 uint8_t n;
72
73typedef struct md_cmd_binding {
74 md_str_t name; /* ReadStep.into */
75 bool is_data; /* payload lives in the data sink */
77 uint32_t data_off;
78 uint32_t data_len;
80
81typedef struct md_cmd_exec {
83 md_bytes_t cmd_raw; /* the Command message */
84 md_cmd_params_t params; /* caller storage */
85 md_wire_t steps; /* cursor over cmd_raw's step frames */
86 uint8_t status; /* md_cmd_status_t */
88
89 bool in_poll;
90 bool poll_first; /* evaluate immediately on the entering tick */
91 md_bytes_t poll_raw; /* current PollStep message */
93 uint32_t poll_due_ms;
94
96 uint8_t n_bindings;
97
98 uint8_t *data; /* caller sink for string/bytes read steps */
99 uint32_t data_cap;
100 uint32_t data_used;
102
103/* Locate command_id in the device's profile, validate that every required
104 * param is supplied, and arm the executor. data_buf/data_cap receive
105 * string/bytes read-step payloads (pass NULL/0 when the command has none). */
107 const md_cmd_params_t *params, uint8_t *data_buf,
108 size_t data_cap);
109
110/* Advance the run: executes steps until it completes (MD_CMD_DONE), fails
111 * (MD_CMD_ERROR), or reaches a poll whose interval has not elapsed
112 * (MD_CMD_WAITING_POLL). now_ms is any monotonic millisecond clock. */
114
115/* The failure cause after MD_CMD_ERROR (MD_OK otherwise). */
117
118/* Fetch a numeric result by CommandResult.field after MD_CMD_DONE. */
120
121/* Fetch a string/bytes result: a view into the caller's data sink. */
123 const uint8_t **p, size_t *len);
124
125#endif /* MODDEF_COMMAND_H */
md_err_t md_cmd_error(const md_cmd_exec_t *ex)
#define MD_CMD_MAX_BINDINGS
Definition command.h:34
struct md_cmd_binding md_cmd_binding_t
md_err_t md_cmd_begin(md_cmd_exec_t *ex, md_dev_t *dev, md_str_t command_id, const md_cmd_params_t *params, uint8_t *data_buf, size_t data_cap)
struct md_cmd_exec md_cmd_exec_t
struct md_cmd_params md_cmd_params_t
md_err_t md_cmd_result(const md_cmd_exec_t *ex, md_str_t field, md_value_t *out)
enum md_cmd_param_kind md_cmd_param_kind_t
struct md_cmd_param md_cmd_param_t
md_cmd_status
Definition command.h:47
@ MD_CMD_RUNNING
Definition command.h:48
@ MD_CMD_DONE
Definition command.h:50
@ MD_CMD_WAITING_POLL
Definition command.h:49
@ MD_CMD_ERROR
Definition command.h:51
enum md_cmd_status md_cmd_status_t
md_err_t md_cmd_result_data(const md_cmd_exec_t *ex, md_str_t field, const uint8_t **p, size_t *len)
md_cmd_status_t md_cmd_tick(md_cmd_exec_t *ex, uint32_t now_ms)
md_cmd_param_kind
Definition command.h:54
@ MD_CMD_PARAM_STR
Definition command.h:56
@ MD_CMD_PARAM_VALUE
Definition command.h:55
@ MD_CMD_PARAM_BYTES
Definition command.h:57
enum md_err md_err_t
Definition str.h:33
Definition command.h:73
bool is_data
Definition command.h:75
md_value_t value
Definition command.h:76
uint32_t data_len
Definition command.h:78
uint32_t data_off
Definition command.h:77
md_str_t name
Definition command.h:74
Definition command.h:81
md_cmd_binding_t bindings[MD_CMD_MAX_BINDINGS]
Definition command.h:95
bool in_poll
Definition command.h:89
md_cmd_params_t params
Definition command.h:84
uint32_t poll_due_ms
Definition command.h:93
uint32_t data_used
Definition command.h:100
md_bytes_t cmd_raw
Definition command.h:83
md_dev_t * dev
Definition command.h:82
uint32_t data_cap
Definition command.h:99
md_wire_t steps
Definition command.h:85
uint32_t poll_started_ms
Definition command.h:92
md_err_t err
Definition command.h:87
bool poll_first
Definition command.h:90
uint8_t n_bindings
Definition command.h:96
uint8_t status
Definition command.h:86
uint8_t * data
Definition command.h:98
md_bytes_t poll_raw
Definition command.h:91
Definition command.h:60
md_value_t value
Definition command.h:63
md_str_t field
Definition command.h:61
md_bytes_t bytes
Definition command.h:65
uint8_t kind
Definition command.h:62
md_str_t str
Definition command.h:64
Definition command.h:68
const md_cmd_param_t * items
Definition command.h:69
uint8_t n
Definition command.h:70
Definition device.h:30
Definition str.h:13
Definition codec.h:30
Definition wire.h:15