std-rs 0.30.0

Rust port of EPICS std module (epid, throttle, timestamp records)
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
//! Delay-and-Do state machine — native Rust port of `delayDo.st`.
//!
//! Implements a state machine that waits for a standby condition,
//! monitors an active condition, and after the active condition
//! clears (with a configurable delay), triggers an action.
//!
//! # State Machine
//!
//! ```text
//!   init ──► idle ◄──────────────────────────┐
//!            │  ▲                              │
//!            │  └── maybeStandby ◄── disable  │
//!            ▼                       ▲        │
//!         standby ──► maybeWait ──► waiting ──► action
//!            ▲            │           │
//!            │            ▼           ▼
//!            │          idle       active ──► waiting
//!            └──────────────────────┘
//! ```

use std::time::{Duration, Instant};

/// States of the delay-do state machine.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum DelayDoState {
    Init,
    Disable,
    MaybeStandby,
    Idle,
    Standby,
    MaybeWait,
    Active,
    Waiting,
    Action,
}

impl DelayDoState {
    /// True where the state's `when` clauses are exhaustive, so SNL leaves it
    /// on the same evaluation that entered it rather than waiting for a new
    /// event: `init`'s `pvConnectCount() == pvAssignCount()`
    /// (`delayDo.st:35`), `maybeStandby`'s standby/active/!standby triple
    /// (`:58-74`), `maybeWait`'s active/efTest/!efTest triple (`:121-138`),
    /// and `action`'s bare `when ()` (`:201`). A runner must keep stepping
    /// while this holds.
    pub fn is_transient(self) -> bool {
        matches!(
            self,
            DelayDoState::Init
                | DelayDoState::MaybeStandby
                | DelayDoState::MaybeWait
                | DelayDoState::Action
        )
    }

    /// True where entering the state writes its name to `{P}{R}:state`.
    /// `maybeStandby` and `maybeWait` are the two exceptions, and
    /// deliberately so — "the state doesn't last long enough"
    /// (`delayDo.st:51-52`, `:112-113`) is why their `PVPUTSTR` calls are
    /// commented out upstream.
    pub fn is_published(self) -> bool {
        !matches!(self, DelayDoState::MaybeStandby | DelayDoState::MaybeWait)
    }
}

impl std::fmt::Display for DelayDoState {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            DelayDoState::Init => write!(f, "init"),
            DelayDoState::Disable => write!(f, "disable"),
            DelayDoState::MaybeStandby => write!(f, "maybeStandby"),
            DelayDoState::Idle => write!(f, "idle"),
            DelayDoState::Standby => write!(f, "standby"),
            DelayDoState::MaybeWait => write!(f, "maybeWait"),
            DelayDoState::Active => write!(f, "active"),
            DelayDoState::Waiting => write!(f, "waiting"),
            DelayDoState::Action => write!(f, "action"),
        }
    }
}

/// Input signals for the delay-do state machine.
///
/// Each `_changed` field is a *monitor event*, not a level comparison: SNL's
/// `sync` posts the event flag on every monitor callback for the variable,
/// including one that carries the same value (an alarm-only update) or a fall
/// back to zero. Pass `true` on the step a callback arrived, whatever the new
/// value is; [`DelayDoController::step`] latches it exactly as SNL does.
#[derive(Debug, Clone, Copy)]
pub struct DelayDoInputs {
    /// Enable/disable control
    pub enable: bool,
    /// A monitor event arrived on "enable" this step (SNL `enable_mon`)
    pub enable_changed: bool,
    /// Standby condition
    pub standby: bool,
    /// A monitor event arrived on "standby" this step (SNL `standby_mon`)
    pub standby_changed: bool,
    /// Active condition
    pub active: bool,
    /// A monitor event arrived on "active" this step (SNL `active_mon`)
    pub active_changed: bool,
}

/// An SNL event flag (`evflag`), latched.
///
/// STD-12: `EvFlag(_VAR_)` (`seqPVmacros.h:131-134`) expands to
/// `monitor _VAR_; evflag _VAR_##_mon; sync _VAR_ _VAR_##_mon`, so the flag is
/// raised by *any* monitor event on the variable and stays raised until an
/// `efTestAndClear` or `efClear` evaluates — across state transitions included.
/// Reading a per-step "changed" input directly models an edge instead, which
/// drops every event arriving in a state whose clauses do not test that flag,
/// and keeps a stale one wherever C would have cleared it. Both directions were
/// observable in the ported transition table, so all three flags go through
/// this type rather than the cited one alone.
#[derive(Debug, Clone, Copy, Default)]
struct EventFlag(bool);

impl EventFlag {
    /// SNL `sync`: a monitor callback arrived, whatever value it carried.
    fn sync(&mut self, monitor_event: bool) {
        self.0 |= monitor_event;
    }

    /// SNL `efTest` — read without clearing.
    fn test(self) -> bool {
        self.0
    }

    /// SNL `efTestAndClear` — read and clear. Clears whether or not it was
    /// raised, and only when the clause that names it is actually reached, so
    /// it must stay the left operand of every `&&` that mirrors C's.
    fn test_and_clear(&mut self) -> bool {
        std::mem::replace(&mut self.0, false)
    }

    /// SNL `efClear`.
    fn clear(&mut self) {
        self.0 = false;
    }
}

/// Output actions from the delay-do state machine.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum DelayDoAction {
    /// No action this step.
    None,
    /// Process the action sequence (doSeq).
    ProcessAction,
}

/// The delay-do controller.
pub struct DelayDoController {
    pub state: DelayDoState,
    /// Delay period before triggering the action.
    pub delay_period: Duration,
    /// Whether to resume waiting when re-entering from standby.
    resume_waiting: bool,
    /// SNL `enable_mon` / `standby_mon` / `active_mon`.
    enable_mon: EventFlag,
    standby_mon: EventFlag,
    active_mon: EventFlag,
    /// `waiting`'s armed `delay(delayPeriod)` clause (`delayDo.st:189`):
    /// when the state was entered, and the `delay_period` the clause was
    /// evaluated with AT entry. SNL evaluates a `delay()` argument once, on
    /// entry, so a monitor on `{P}{R}:delay` that lands mid-wait retimes the
    /// NEXT wait and not the one in flight. `Some` exactly in `waiting`.
    wait: Option<(Instant, Duration)>,
}

impl Default for DelayDoController {
    fn default() -> Self {
        Self {
            state: DelayDoState::Init,
            delay_period: Duration::from_secs(0),
            resume_waiting: false,
            enable_mon: EventFlag::default(),
            standby_mon: EventFlag::default(),
            active_mon: EventFlag::default(),
            wait: None,
        }
    }
}

impl DelayDoController {
    pub fn new(delay_secs: f64) -> Self {
        Self {
            delay_period: epics_base_rs::runtime::time::duration_from_secs(delay_secs),
            ..Default::default()
        }
    }

    /// Advance the state machine given current inputs.
    /// Returns the action to take (if any) and the new state.
    pub fn step(&mut self, inputs: &DelayDoInputs) -> (DelayDoAction, DelayDoState) {
        let action;

        // SNL `sync` delivers every monitor event to its flag before any state's
        // clauses run, whatever state the machine is in — that is what makes the
        // flags survive the transient states (`init`, `maybeStandby`,
        // `maybeWait`, `action`) whose clauses name none of them.
        self.enable_mon.sync(inputs.enable_changed);
        self.standby_mon.sync(inputs.standby_changed);
        self.active_mon.sync(inputs.active_changed);

        match self.state {
            DelayDoState::Init => {
                action = DelayDoAction::None;
                self.resume_waiting = false;
                self.state = DelayDoState::Idle;
            }

            DelayDoState::Disable => {
                action = DelayDoAction::None;
                if self.enable_mon.test_and_clear() && inputs.enable {
                    // delayDo.st:49 — only events after re-enabling may act.
                    self.active_mon.clear();
                    self.state = DelayDoState::MaybeStandby;
                }
            }

            DelayDoState::MaybeStandby => {
                action = DelayDoAction::None;
                if inputs.standby {
                    self.state = DelayDoState::Standby;
                } else if inputs.active {
                    self.state = DelayDoState::Active;
                } else {
                    self.state = DelayDoState::Idle;
                }
            }

            DelayDoState::Idle => {
                action = DelayDoAction::None;
                if self.enable_mon.test_and_clear() && !inputs.enable {
                    self.state = DelayDoState::Disable;
                } else if self.standby_mon.test_and_clear() && inputs.standby {
                    self.state = DelayDoState::Standby;
                } else if self.active_mon.test_and_clear() && inputs.active {
                    self.state = DelayDoState::Active;
                }
            }

            DelayDoState::Standby => {
                action = DelayDoAction::None;
                // `standby` names no active_mon clause, so the flag accumulates
                // here — that accumulation is the whole point of `maybeWait`.
                if self.enable_mon.test_and_clear() && !inputs.enable {
                    self.resume_waiting = false;
                    self.state = DelayDoState::Disable;
                } else if self.standby_mon.test_and_clear() && !inputs.standby {
                    self.state = DelayDoState::MaybeWait;
                }
            }

            DelayDoState::MaybeWait => {
                action = DelayDoAction::None;
                if inputs.active {
                    self.state = DelayDoState::Active;
                } else if self.active_mon.test() || self.resume_waiting {
                    // delayDo.st:130-132 — `efTest` then an explicit `efClear`.
                    self.active_mon.clear();
                    self.wait = Some((Instant::now(), self.delay_period));
                    self.state = DelayDoState::Waiting;
                } else {
                    self.state = DelayDoState::Idle;
                }
            }

            DelayDoState::Active => {
                action = DelayDoAction::None;
                if self.enable_mon.test_and_clear() && !inputs.enable {
                    self.state = DelayDoState::Disable;
                } else if self.standby_mon.test_and_clear() && inputs.standby {
                    self.state = DelayDoState::Standby;
                } else if self.active_mon.test_and_clear() && !inputs.active {
                    self.wait = Some((Instant::now(), self.delay_period));
                    self.state = DelayDoState::Waiting;
                }
            }

            DelayDoState::Waiting => {
                if self.enable_mon.test_and_clear() && !inputs.enable {
                    action = DelayDoAction::None;
                    self.state = DelayDoState::Disable;
                    self.wait = None;
                } else if self.standby_mon.test_and_clear() && inputs.standby {
                    action = DelayDoAction::None;
                    self.resume_waiting = true;
                    self.state = DelayDoState::Standby;
                    self.wait = None;
                } else if self.active_mon.test_and_clear() && inputs.active {
                    action = DelayDoAction::None;
                    self.state = DelayDoState::Active;
                    self.wait = None;
                } else if let Some((start, period)) = self.wait {
                    if start.elapsed() >= period {
                        self.resume_waiting = false;
                        self.wait = None;
                        self.state = DelayDoState::Action;
                        action = DelayDoAction::None;
                    } else {
                        action = DelayDoAction::None;
                    }
                } else {
                    action = DelayDoAction::None;
                }
            }

            DelayDoState::Action => {
                action = DelayDoAction::ProcessAction;
                self.state = DelayDoState::Idle;
            }
        }

        (action, self.state)
    }

    /// SNL `when ( delay( delayPeriod ) )` (`delayDo.st:189`) — how long a
    /// runner must wait before re-evaluating, or `None` where no clause is
    /// time-based and monitors are the only wake-up. `wait` is set on every
    /// entry to `waiting` and cleared on every exit from it, so this answers
    /// `Some` exactly in the one state that has a delay clause.
    pub fn delay_remaining(&self) -> Option<Duration> {
        let (start, period) = self.wait?;
        Some(period.saturating_sub(start.elapsed()))
    }
}

// ---------------------------------------------------------------------------
// Runner — binds the state machine above to `delayDo.db`
// ---------------------------------------------------------------------------

use epics_base_rs::server::database::PvDatabase;
use epics_base_rs::server::database::db_access::{DbChannel, DbMultiMonitor, alloc_origin};

/// The `P` and `R` macros of
/// `program delayDo("name=delayDo,P=xxx:,R=delayDo1")` (`delayDo.st:1`).
#[derive(Debug, Clone)]
pub struct DelayDoConfig {
    pub prefix: String,
    pub record: String,
}

impl DelayDoConfig {
    pub fn new(prefix: &str, record: &str) -> Self {
        Self {
            prefix: prefix.to_string(),
            record: record.to_string(),
        }
    }

    /// `{P}{R}:<leaf>` — the shape every `PV(...)` line in `delayDo.st` uses
    /// (`:22-28`), and the one `delayDo.db` declares its records with.
    pub fn pv(&self, leaf: &str) -> String {
        format!("{}{}:{}", self.prefix, self.record, leaf)
    }
}

/// `DEBUG_PRINT(level, msg)` (`seqPVmacros.h:231-236`) — printed only while
/// `{P}{R}:debug` is at or above the level, with the program name in the
/// header as `DEBUG_PRINT_HEADER` writes it.
fn debug_print(debug_flag: i32, level: i32, msg: &str) {
    if debug_flag >= level {
        println!("<delayDo.st,{level},delayDo> {msg}");
    }
}

/// Run `delayDo.st` against the records `delayDo.db` loaded.
///
/// The state machine above is pure; this is the half that gives it PVs. It
/// owns the two things SNL's runtime owns and the controller cannot: when to
/// evaluate, and what to write.
///
/// **When to evaluate.** SNL leaves a state whose `when` clauses are
/// exhaustive on the same evaluation that entered it, so a stable state is
/// reached by stepping while [`DelayDoState::is_transient`] holds. It then
/// blocks — on monitors alone, or on whichever of a monitor and the armed
/// `delay(delayPeriod)` clause comes first. `{P}{R}:delay` and `{P}{R}:debug`
/// appear in no `when` condition, only inside action blocks, so a monitor on
/// either updates the runner and does NOT re-evaluate: an evaluation runs
/// `efTestAndClear` on the flags its clauses name, and one triggered by a
/// variable no clause tests would consume an event that had not been acted
/// on.
///
/// One deviation, and it is loud rather than silent: a PV that `delayDo.db`
/// never loaded leaves C in `init` forever, because `pvConnectCount()` never
/// reaches `pvAssignCount()` (`:35`). Here it is an error return, so the
/// caller's `eprintln!` names it.
pub async fn run(
    config: DelayDoConfig,
    db: PvDatabase,
) -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
    let origin = alloc_origin();

    // `PV(..., EvFlag)` and `PV(..., Monitor)` — the five assigned inputs.
    let pv_enable = config.pv("enable");
    let pv_standby = config.pv("standbyCalc");
    let pv_active = config.pv("activeCalc");
    let pv_delay = config.pv("delay");
    let pv_debug = config.pv("debug");
    let monitored = vec![
        pv_enable.clone(),
        pv_standby.clone(),
        pv_active.clone(),
        pv_delay.clone(),
        pv_debug.clone(),
    ];

    let ch_enable = DbChannel::with_origin(&db, &pv_enable, origin);
    let ch_standby = DbChannel::with_origin(&db, &pv_standby, origin);
    let ch_active = DbChannel::with_origin(&db, &pv_active, origin);
    let ch_delay = DbChannel::with_origin(&db, &pv_delay, origin);
    let ch_debug = DbChannel::with_origin(&db, &pv_debug, origin);

    // `PV(..., NoMon)` — the two outputs. `doSeq` is assigned to the field
    // `.PROC` (`:27`), so the put processes the sseq rather than setting a
    // value on it.
    let ch_state = DbChannel::with_origin(&db, &config.pv("state"), origin);
    let ch_doseq = DbChannel::with_origin(&db, &format!("{}.PROC", config.pv("doSeq")), origin);

    let mut monitor = DbMultiMonitor::new_filtered(&db, &monitored, origin).await;
    if monitor.sub_count() != monitored.len() {
        return Err(format!(
            "delayDo: {} of the {} PVs it assigns are not in the database ({})",
            monitored.len() - monitor.sub_count(),
            monitored.len(),
            monitored.join(", ")
        )
        .into());
    }

    let mut debug_flag = ch_debug.get_i32().await;
    let mut ctrl = DelayDoController::new(ch_delay.get_f64().await);
    // SNL's `monitor` delivers the variable's current value at connect, so the
    // machine starts on levels rather than on zeroes. `enable` is a `short`
    // and the two calcs are `int` (`:23-25`), and C truncates on the way in —
    // a calc result of 0.5 is a false `standby`, not a true one.
    let mut inputs = DelayDoInputs {
        enable: ch_enable.get_i16().await != 0,
        enable_changed: false,
        standby: ch_standby.get_i32().await != 0,
        standby_changed: false,
        active: ch_active.get_i32().await != 0,
        active_changed: false,
    };

    loop {
        loop {
            let previous = ctrl.state;
            let (action, state) = ctrl.step(&inputs);
            // One evaluation consumes the monitor events it was given; a
            // transient state's re-evaluation is not a second arrival.
            inputs.enable_changed = false;
            inputs.standby_changed = false;
            inputs.active_changed = false;

            if action == DelayDoAction::ProcessAction {
                // `PVPUT(doSeq, 1)` before `PVPUTSTR(seqState, "idle")`
                // (`:204-206`) — the sseq runs, then the state PV catches up.
                let _ = ch_doseq.put_i32_process(1).await;
            }
            if state != previous {
                debug_print(debug_flag, 3, &format!("{previous} -> {state}"));
                if state.is_published() {
                    let _ = ch_state.put_string_process(&state.to_string()).await;
                }
            }
            if !state.is_transient() {
                break;
            }
        }

        loop {
            let woken = match ctrl.delay_remaining() {
                Some(remaining) => tokio::time::timeout(remaining, monitor.wait_change())
                    .await
                    .ok(),
                None => Some(monitor.wait_change().await),
            };
            let Some((pv, value)) = woken else {
                // The armed `delay(delayPeriod)` clause came first.
                break;
            };
            if pv == pv_enable {
                inputs.enable = (value as i16) != 0;
                inputs.enable_changed = true;
                break;
            } else if pv == pv_standby {
                inputs.standby = (value as i32) != 0;
                inputs.standby_changed = true;
                break;
            } else if pv == pv_active {
                inputs.active = (value as i32) != 0;
                inputs.active_changed = true;
                break;
            } else if pv == pv_delay {
                ctrl.delay_period = epics_base_rs::runtime::time::duration_from_secs(value);
            } else if pv == pv_debug {
                debug_flag = value as i32;
            }
        }
    }
}