Skip to main content

epics_base_rs/server/
device_support.rs

1use crate::error::CaResult;
2use crate::server::record::{AlarmSeverity, ProcessAction, Record, RecordInstance, ScanType};
3
4/// Check if a DTYP string represents a soft/built-in device support
5/// that doesn't require an explicit device support registration.
6/// Matches C EPICS built-in soft device support names.
7pub fn is_soft_dtyp(dtyp: &str) -> bool {
8    dtyp.is_empty()
9        || dtyp == "Soft Channel"
10        || dtyp == "Raw Soft Channel"
11        || dtyp == "Async Soft Channel"
12        || dtyp == "Soft Timestamp"
13        || dtyp == "Sec Past Epoch"
14}
15
16/// Handle for waiting on asynchronous write completion.
17/// Returned by [`DeviceSupport::write_begin`] when the write is submitted
18/// to a worker queue rather than executed synchronously.
19pub trait WriteCompletion: Send + 'static {
20    /// Block until the write completes or timeout expires.
21    fn wait(&self, timeout: std::time::Duration) -> CaResult<()>;
22}
23
24/// Outcome of a device support read() call.
25///
26/// Allows device support to return side-effect actions (link writes,
27/// delayed reprocess) and signal that it has already performed the
28/// Result of a device support `read()` call.
29///
30/// # `ok()` vs `computed()`
31///
32/// This mirrors the C EPICS `read_ai()` return convention:
33///
34/// - **`ok()`** (C return 0): Device support wrote to RVAL. The record's
35///   `process()` will run its built-in conversion (e.g., ai applies
36///   `ROFF → ASLO/AOFF → LINR/ESLO/EOFF → smoothing` to produce VAL
37///   from RVAL).
38///
39/// - **`computed()`** (C return 2): Device support wrote to VAL directly.
40///   The record's `process()` will **skip** its conversion and use the
41///   VAL as-is. Use this when the device support provides engineering
42///   units directly (e.g., soft channel, asyn, custom drivers that
43///   call `record.put_field("VAL", ...)`).
44///
45/// **Common mistake:** returning `ok()` when VAL is set directly causes
46/// the record's conversion to overwrite VAL with a value derived from
47/// RVAL (typically 0), making the read appear broken.
48#[derive(Default)]
49pub struct DeviceReadOutcome {
50    /// Actions for the framework to execute (WriteDbLink, ReprocessAfter, etc.)
51    pub actions: Vec<ProcessAction>,
52    /// If true, the record's built-in conversion (e.g., ai RVAL→VAL)
53    /// is skipped. Set this when device support writes VAL directly.
54    pub did_compute: bool,
55}
56
57impl DeviceReadOutcome {
58    /// Device support wrote RVAL; record will run its conversion to produce VAL.
59    ///
60    /// C equivalent: `read_ai()` returns 0.
61    pub fn ok() -> Self {
62        Self::default()
63    }
64
65    /// Device support wrote VAL directly; record will skip conversion.
66    ///
67    /// C equivalent: `read_ai()` returns 2.
68    pub fn computed() -> Self {
69        Self {
70            did_compute: true,
71            actions: Vec::new(),
72        }
73    }
74
75    /// Shorthand for a computed read with actions.
76    pub fn computed_with(actions: Vec<ProcessAction>) -> Self {
77        Self {
78            did_compute: true,
79            actions,
80        }
81    }
82}
83
84/// Trait for custom device support implementations.
85/// When DTYP is set to something other than "" or "Soft Channel",
86/// the registered DeviceSupport is used instead of link resolution.
87pub trait DeviceSupport: Send + Sync + 'static {
88    fn init(&mut self, _record: &mut dyn Record) -> CaResult<()> {
89        Ok(())
90    }
91
92    /// Read from hardware into the record.
93    ///
94    /// Returns a `DeviceReadOutcome` containing:
95    /// - `actions`: side-effect actions (link writes, delayed reprocess)
96    ///   that the framework will execute after process()
97    /// - `did_compute`: if true, the record's built-in compute was already
98    ///   performed (e.g., device support ran PID), so process() should skip it
99    fn read(&mut self, record: &mut dyn Record) -> CaResult<DeviceReadOutcome> {
100        let _ = record;
101        Ok(DeviceReadOutcome::ok())
102    }
103
104    fn write(&mut self, record: &mut dyn Record) -> CaResult<()>;
105    fn dtyp(&self) -> &str;
106
107    /// Return the last alarm (status, severity) from the driver.
108    /// None means the driver does not override alarms.
109    fn last_alarm(&self) -> Option<(u16, u16)> {
110        None
111    }
112
113    /// Return the last timestamp from the driver.
114    /// None means the driver does not override timestamps.
115    fn last_timestamp(&self) -> Option<std::time::SystemTime> {
116        None
117    }
118
119    /// Called by the framework immediately before [`read()`](DeviceSupport::read)
120    /// to push a read-only snapshot of framework-owned `CommonFields`
121    /// state ([`crate::server::record::ProcessContext`]) that the device
122    /// support needs.
123    ///
124    /// `read()` receives only `&mut dyn Record`; it cannot reach
125    /// `RecordInstance.common`. C device support reads `dbCommon`
126    /// directly — `devTimeOfDay.c:122` selects its time format from
127    /// `psi->phas`. A driver that needs `phas`/`udf`/`tse`/`tsel`
128    /// overrides this to stash the values before `read()` runs.
129    ///
130    /// Additive framework-set-hook (same shape as
131    /// [`DeviceSupport::set_record_info`]). Default: ignore.
132    fn set_process_context(&mut self, _ctx: &crate::server::record::ProcessContext) {}
133
134    /// Called after init() with the record name and scan type.
135    fn set_record_info(&mut self, _name: &str, _scan: ScanType) {}
136
137    /// Forward parsed `info("key", "value")` directives from the .db
138    /// file to the device support. Default is a no-op; drivers that
139    /// react to specific tags (asyn `asyn:READBACK`, EtherCAT terminal
140    /// hints, etc.) override this. Called once after `set_record_info`
141    /// during builder wiring; not called again at runtime.
142    fn apply_record_info(&mut self, _info: &std::collections::HashMap<String, String>) {}
143
144    /// Return a receiver for I/O Intr scan notifications.
145    /// Only called for records with SCAN=I/O Intr.
146    fn io_intr_receiver(&mut self) -> Option<crate::runtime::sync::mpsc::Receiver<()>> {
147        None
148    }
149
150    /// Begin an asynchronous write (submit only, no blocking).
151    /// Returns `Some(handle)` if the write was submitted to a worker queue —
152    /// the caller should wait on the handle outside any record lock.
153    /// Returns `None` to fall back to synchronous [`write()`](DeviceSupport::write).
154    fn write_begin(
155        &mut self,
156        _record: &mut dyn Record,
157    ) -> CaResult<Option<Box<dyn WriteCompletion>>> {
158        Ok(None)
159    }
160
161    /// Handle a named command from the record's process() via
162    /// `ProcessAction::DeviceCommand`. This allows records to request
163    /// driver operations (e.g., scaler reset/arm/write_preset) without
164    /// holding a direct driver reference.
165    ///
166    /// `handle_command` runs AFTER the process snapshot has already been
167    /// built and notified, so any record field it mutates would not be
168    /// diffed by the snapshot path. The returned `Vec` names the record
169    /// fields the command changed; the framework posts a `DBE_VALUE`
170    /// monitor event for each, mirroring the explicit `db_post_events`
171    /// calls a C record makes from inside `process()` (e.g.
172    /// `scalerRecord.c:425-430` posts PR1/TP/FREQ after the driver
173    /// write-back). Return an empty `Vec` when no record field changed.
174    ///
175    /// Default: ignore, no fields changed.
176    fn handle_command(
177        &mut self,
178        _record: &mut dyn Record,
179        _command: &str,
180        _args: &[crate::types::EpicsValue],
181    ) -> CaResult<Vec<&'static str>> {
182        Ok(Vec::new())
183    }
184}
185
186/// Canonical device-support init sequence — the single owner of the
187/// "attach device support to a record" contract.
188///
189/// Both build paths ([`crate::server::ioc_app::wire_device_support`]
190/// and [`crate::server::ioc_builder::IocBuilder::build`]) MUST call
191/// this so a driver author can write one correct `init()`.
192///
193/// Order (C parity — `recGblInitConstantLink`-style field setup runs
194/// before `init_record`; `set_record_info` / `apply_record_info` are
195/// Rust extensions that supply that field context and therefore
196/// precede `init`):
197///
198/// 1. `set_record_info(name, scan)` — give the driver its record
199///    identity and scan mode.
200/// 2. `apply_record_info(info)` — forward `info(...)` tags so a
201///    driver that reads them inside `init()` sees a populated map.
202/// 3. `init(record)` — driver `init_record` equivalent.
203///
204/// On `init()` failure the record is flagged `INVALID` severity with
205/// a `SOFT` status and a diagnostic is logged — matching C
206/// `initDevSup`/`init_record` failure handling (the record is marked,
207/// not silently attached as healthy). On success, UDF is cleared if
208/// the driver produced a value.
209///
210/// The device is attached (`instance.device = Some(dev)`) regardless
211/// of init outcome so the record is addressable; a failed init leaves
212/// the alarm set.
213pub fn wire_device_to_record(instance: &mut RecordInstance, mut dev: Box<dyn DeviceSupport>) {
214    let name = instance.name.clone();
215    dev.set_record_info(&name, instance.common.scan);
216    dev.apply_record_info(&instance.info);
217    match dev.init(&mut *instance.record) {
218        Ok(()) => {
219            // Clear UDF if init successfully produced a value
220            // (e.g. an initial readback).
221            if instance.record.val().is_some() {
222                instance.common.udf = false;
223            }
224        }
225        Err(e) => {
226            eprintln!(
227                "device support init failed for record '{name}' (DTYP '{}'): {e}",
228                instance.common.dtyp
229            );
230            // Flag the record so the failure is observable rather
231            // than presenting a healthy-looking record.
232            instance.common.sevr = AlarmSeverity::Invalid;
233            instance.common.stat = crate::server::recgbl::alarm_status::SOFT_ALARM;
234        }
235    }
236    instance.device = Some(dev);
237}
238
239#[cfg(test)]
240mod tests {
241    use super::*;
242    use crate::error::CaError;
243    use crate::server::record::{AlarmSeverity, Record, RecordInstance, ScanType};
244    use crate::server::records::ai::AiRecord;
245    use std::collections::HashMap;
246    use std::sync::{Arc, Mutex};
247
248    /// Observed wiring state, shared with the test via `Arc` so it is
249    /// inspectable after the device is moved into the record.
250    #[derive(Default)]
251    struct WireObservation {
252        /// Info keys visible to `init()`.
253        info_at_init: Vec<String>,
254        /// Whether `set_record_info` ran before `init()`.
255        record_info_before_init: bool,
256        /// Whether `set_record_info` had run by the time `init` ran.
257        init_ran: bool,
258    }
259
260    /// Device support that records the wiring order and fails `init`.
261    struct ProbeDev {
262        obs: Arc<Mutex<WireObservation>>,
263        info: HashMap<String, String>,
264        record_info_set: bool,
265        fail_init: bool,
266    }
267    impl DeviceSupport for ProbeDev {
268        fn dtyp(&self) -> &str {
269            "ProbeDev"
270        }
271        fn write(&mut self, _record: &mut dyn Record) -> CaResult<()> {
272            Ok(())
273        }
274        fn set_record_info(&mut self, _name: &str, _scan: ScanType) {
275            self.record_info_set = true;
276        }
277        fn apply_record_info(&mut self, info: &HashMap<String, String>) {
278            self.info = info.clone();
279        }
280        fn init(&mut self, _record: &mut dyn Record) -> CaResult<()> {
281            let mut o = self.obs.lock().unwrap();
282            o.init_ran = true;
283            o.record_info_before_init = self.record_info_set;
284            o.info_at_init = self.info.keys().cloned().collect();
285            if self.fail_init {
286                Err(CaError::InvalidValue("device init failed".into()))
287            } else {
288                Ok(())
289            }
290        }
291    }
292
293    /// M2 regression: a device support whose `init()` returns `Err`
294    /// must NOT be attached as a healthy record — the record is
295    /// flagged INVALID severity with a SOFT status. (Pre-fix the
296    /// IocBuilder path discarded the error with `let _ =`.)
297    #[test]
298    fn wire_device_init_failure_flags_record_invalid() {
299        let mut instance = RecordInstance::new("TEST:AI".to_string(), AiRecord::new(0.0));
300        instance.common.dtyp = "ProbeDev".to_string();
301        let obs = Arc::new(Mutex::new(WireObservation::default()));
302        let dev = Box::new(ProbeDev {
303            obs: obs.clone(),
304            info: HashMap::new(),
305            record_info_set: false,
306            fail_init: true,
307        });
308
309        wire_device_to_record(&mut instance, dev);
310
311        assert_eq!(
312            instance.common.sevr,
313            AlarmSeverity::Invalid,
314            "failed device init must flag the record INVALID"
315        );
316        assert_eq!(
317            instance.common.stat,
318            crate::server::recgbl::alarm_status::SOFT_ALARM,
319        );
320        assert!(
321            instance.device.is_some(),
322            "device is still attached so the record is addressable"
323        );
324    }
325
326    /// M1 regression: the canonical wiring order is
327    /// set_record_info → apply_record_info → init. A driver reading
328    /// `info(...)` tags inside `init()` must see a populated map, and
329    /// `set_record_info` must have run first.
330    #[test]
331    fn wire_device_applies_info_and_record_info_before_init() {
332        let mut instance = RecordInstance::new("TEST:AI2".to_string(), AiRecord::new(0.0));
333        instance.common.dtyp = "ProbeDev".to_string();
334        instance.set_info("asyn:READBACK", "1");
335        let obs = Arc::new(Mutex::new(WireObservation::default()));
336        let dev = Box::new(ProbeDev {
337            obs: obs.clone(),
338            info: HashMap::new(),
339            record_info_set: false,
340            fail_init: false,
341        });
342
343        wire_device_to_record(&mut instance, dev);
344
345        let o = obs.lock().unwrap();
346        assert!(o.init_ran, "init must have run");
347        assert!(
348            o.record_info_before_init,
349            "set_record_info must run before init"
350        );
351        assert!(
352            o.info_at_init.iter().any(|k| k == "asyn:READBACK"),
353            "info(...) tags must be visible inside init()"
354        );
355    }
356}