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
use crate::error::{CaError, CaResult};
use crate::server::record::{MENU_POST, MENU_YES_NO, ProcessOutcome, Record};
use crate::types::{EpicsValue, PvString};
/// EPICS `MAX_STRING_SIZE` — `val`/`oval`/`sval` are fixed 40-byte
/// buffers in C `stringinRecord.c`; every copy truncates at 40.
const MAX_STRING_SIZE: usize = 40;
/// Truncate `s` to at most `MAX_STRING_SIZE - 1` bytes. C
/// `strncpy(dst, src, sizeof(prec->val))` copies 39 payload bytes + an
/// implicit NUL byte for byte with no UTF-8 awareness, so the cut is on
/// a raw byte boundary and a non-UTF-8 VAL keeps its bytes verbatim.
fn truncate_string(s: PvString) -> PvString {
let max = MAX_STRING_SIZE - 1;
if s.len() <= max {
return s;
}
PvString::from_bytes(s.as_bytes()[..max].to_vec())
}
/// Stringin record — a 40-byte (`MAX_STRING_SIZE`) string input.
///
/// C `stringinRecord.c`: `val`, `oval` and `sval` are fixed 40-byte
/// buffers; `strncpy(..., sizeof(prec->val))` truncates every copy at
/// 40. The Rust port uses `String`, so every VAL/OVAL write must be
/// truncated to 39 chars + implicit NUL to match.
pub struct StringinRecord {
pub val: PvString,
pub oval: PvString,
pub simm: i16,
pub siml: String,
pub siol: String,
pub sims: i16,
pub sdly: f64,
/// `menu(stringinPOST)` Post Value Monitors (0=On Change, 1=Always).
pub mpst: i16,
/// `menu(stringinPOST)` Post Archive Monitors (0=On Change, 1=Always).
pub apst: i16,
/// C `monitor()`'s `strncmp(oval, val)` verdict for THIS cycle, captured
/// in `process()` before OVAL is committed — the framework reads it
/// afterwards, by which time `oval == val`.
value_changed: bool,
}
/// `menu(stringinPOST)` — `stringinRecord.dbd.pod`: 0 = On Change, 1 = Always.
const MENU_POST_ALWAYS: i16 = 1;
impl Default for StringinRecord {
fn default() -> Self {
Self {
val: PvString::new(),
oval: PvString::new(),
simm: 0,
siml: String::new(),
siol: String::new(),
sims: 0,
sdly: -1.0,
mpst: 0,
apst: 0,
value_changed: false,
}
}
}
impl StringinRecord {
pub fn new(val: &str) -> Self {
Self {
val: truncate_string(PvString::from(val)),
..Default::default()
}
}
}
impl Record for StringinRecord {
fn record_type(&self) -> &'static str {
"stringin"
}
/// `SIMM` is `DBF_MENU menu(menuYesNo)` (`stringinRecord.dbd.pod`): the
/// two-choice NO/YES simulation menu. `MPST`/`APST` are
/// `menu(stringinPOST)` (`stringinRecord.dbd.pod:21-24,95-107`), whose
/// value order ("On Change", "Always") matches `menu(menuPost)`. Served
/// as `DBR_ENUM` with these labels. `SIMS`/`OLDSIMM` are shared menus
/// resolved centrally.
fn menu_field_choices(&self, field: &str) -> Option<&'static [&'static str]> {
match field {
"SIMM" => Some(MENU_YES_NO),
"MPST" | "APST" => Some(MENU_POST),
_ => None,
}
}
/// C `stringinRecord.c::monitor` (:176-188) has no MDEL/ADEL deadband: the
/// VAL post is gated on `strncmp(oval, val)` plus the MPST/APST "Always"
/// override. Without this the record fell into the analog deadband owner,
/// which posts everything a `to_f64()` cannot measure — one VAL event per
/// subscriber per cycle on an unchanging string — and put a *numeric-looking*
/// string ("12") through the MDEL comparison, where C's `strncmp` sees
/// "12" → "12.0" as a change.
fn uses_monitor_deadband(&self) -> bool {
false
}
fn monitor_value_changed(&self) -> Option<bool> {
Some(self.value_changed)
}
/// C: `if (mpst == stringinPOST_Always) monitor_mask |= DBE_VALUE;`
/// `if (apst == stringinPOST_Always) monitor_mask |= DBE_LOG;`
fn monitor_always_post(&self) -> (bool, bool) {
(self.mpst == MENU_POST_ALWAYS, self.apst == MENU_POST_ALWAYS)
}
/// `stringinRecord.c::process` has NO unconditional UDF re-derive — unlike
/// `aiRecord.c:161` it never runs `prec->udf = isnan(prec->val)`. UDF is
/// cleared ONLY inside `devSiSoft.c::read_stringin` on a real read:
/// `if (!status && !dbLinkIsConstant(&prec->inp)) prec->udf = FALSE`
/// (and the SIOL simulation branch, `stringinRecord.c:209-211`). So a
/// process cycle that sources nothing — e.g. a `caput .UDF 1` driving a
/// Passive record with a constant/empty INP — must leave the client's UDF
/// put intact. The framework clears UDF on a genuine soft read via
/// `device_did_compute`; this opts out of the per-cycle blanket re-derive,
/// exactly like `stringout`/`lso`/`bo`/`longout`.
fn clears_udf(&self) -> bool {
false
}
/// `stringinRecord.c` has NO `recGblCheckUdf` / `UDF_ALARM` (unlike
/// `stringoutRecord.c:147`): an undefined stringin raises no alarm from UDF
/// (softIoc: `record(stringin,"X"){}` → UDF 1, STAT/SEVR = NO_ALARM). With
/// `clears_udf` false, UDF can now legitimately stay 1, so this MUST be
/// false or `rec_gbl_check_udf` would invent an alarm C never raises.
fn raises_udf_alarm(&self) -> bool {
false
}
fn process(&mut self) -> CaResult<ProcessOutcome> {
// C `stringinRecord.c::monitor` copies VAL into OVAL — but only on the
// `strncmp` mismatch that also raises DBE_VALUE|DBE_LOG. Capture the
// verdict here: the framework's monitor gate reads
// `monitor_value_changed()` after `process()` returns, by which point
// an unconditional copy would have erased it.
self.value_changed = self.oval != self.val;
if self.value_changed {
self.oval = self.val.clone();
}
Ok(ProcessOutcome::complete())
}
fn get_field(&self, name: &str) -> Option<EpicsValue> {
match name {
"VAL" => Some(EpicsValue::String(self.val.clone())),
"OVAL" => Some(EpicsValue::String(self.oval.clone())),
"SIMM" => Some(EpicsValue::Short(self.simm)),
"SIML" => Some(EpicsValue::String(self.siml.clone().into())),
"SIOL" => Some(EpicsValue::String(self.siol.clone().into())),
"SIMS" => Some(EpicsValue::Short(self.sims)),
"SDLY" => Some(EpicsValue::Double(self.sdly)),
"MPST" => Some(EpicsValue::Short(self.mpst)),
"APST" => Some(EpicsValue::Short(self.apst)),
_ => None,
}
}
fn put_field(&mut self, name: &str, value: EpicsValue) -> CaResult<()> {
match name {
"VAL" => match value {
// C truncates every VAL copy at MAX_STRING_SIZE (40).
EpicsValue::String(s) => {
self.val = truncate_string(s);
Ok(())
}
_ => Err(CaError::TypeMismatch("VAL".into())),
},
"OVAL" => match value {
EpicsValue::String(s) => {
self.oval = truncate_string(s);
Ok(())
}
_ => Err(CaError::TypeMismatch("OVAL".into())),
},
"SIMM" => match value {
EpicsValue::Short(v) => {
self.simm = v;
Ok(())
}
_ => Err(CaError::TypeMismatch("SIMM".into())),
},
"SIML" => match value {
EpicsValue::String(s) => {
self.siml = s.as_str_lossy().into_owned();
Ok(())
}
_ => Err(CaError::TypeMismatch("SIML".into())),
},
"SIOL" => match value {
EpicsValue::String(s) => {
self.siol = s.as_str_lossy().into_owned();
Ok(())
}
_ => Err(CaError::TypeMismatch("SIOL".into())),
},
"SIMS" => match value {
EpicsValue::Short(v) => {
self.sims = v;
Ok(())
}
_ => Err(CaError::TypeMismatch("SIMS".into())),
},
"SDLY" => match value {
EpicsValue::Double(v) => {
self.sdly = v;
Ok(())
}
_ => Err(CaError::TypeMismatch("SDLY".into())),
},
"MPST" => match value {
EpicsValue::Short(v) => {
self.mpst = v;
Ok(())
}
_ => Err(CaError::TypeMismatch("MPST".into())),
},
"APST" => match value {
EpicsValue::Short(v) => {
self.apst = v;
Ok(())
}
_ => Err(CaError::TypeMismatch("APST".into())),
},
_ => Err(CaError::FieldNotFound(name.to_string())),
}
}
}
#[cfg(test)]
mod tests {
use super::*;
/// a VAL longer than 39 chars is truncated to 39 + NUL.
#[test]
fn val_truncated_to_max_string_size() {
let long = "x".repeat(100);
let mut rec = StringinRecord::default();
rec.put_field("VAL", EpicsValue::String(long.into()))
.unwrap();
assert_eq!(rec.val.len(), 39, "VAL capped at MAX_STRING_SIZE-1");
}
/// A 39-char string is kept whole.
#[test]
fn val_at_limit_kept_whole() {
let s = "y".repeat(39);
let rec = StringinRecord::new(&s);
assert_eq!(rec.val.len(), 39);
}
/// MPST/APST are `menu(stringinPOST)` served as DBR_ENUM: the base
/// snapshot path promotes the stored Short to `Enum` and attaches the
/// wire-visible "On Change"/"Always" labels in `.dbd` value order.
#[test]
fn mpst_apst_snapshot_is_enum_with_post_labels() {
use crate::server::record::RecordInstance;
let mut rec = StringinRecord::default();
rec.put_field("MPST", EpicsValue::Short(1)).unwrap();
rec.put_field("APST", EpicsValue::Short(0)).unwrap();
assert_eq!(rec.get_field("MPST"), Some(EpicsValue::Short(1)));
let inst = RecordInstance::new("SI:MPST".into(), rec);
let snap = inst.snapshot_for_field("MPST").unwrap();
assert_eq!(snap.value, EpicsValue::Enum(1));
assert_eq!(
snap.enums.as_ref().unwrap().strings,
vec!["On Change", "Always"]
);
}
/// A non-UTF-8 VAL (`0xff 0x00 0x80`) put through the field path is
/// stored and served back byte for byte, never U+FFFD-mangled. The C
/// `stringinRecord` VAL is a fixed `char[40]`, so a non-UTF-8 value
/// round-trips unchanged (the byte-based truncate keeps the raw bytes).
#[test]
fn val_preserves_non_utf8_bytes() {
let mut rec = StringinRecord::default();
let raw = vec![0xffu8, 0x00, 0x80];
rec.put_field("VAL", EpicsValue::String(PvString::from_bytes(raw.clone())))
.expect("VAL put");
match rec.get_field("VAL") {
Some(EpicsValue::String(s)) => assert_eq!(
s.as_bytes(),
raw.as_slice(),
"VAL must round-trip the raw bytes, not lossily decode them"
),
other => panic!("expected EpicsValue::String, got {other:?}"),
}
}
}