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
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
//! `longout` — integer output record with optional conditional-output
//! gating via the `OOPT` (output-execution-option) field.
//!
//! Manually implements [`Record`] rather than using `#[derive(EpicsRecord)]`
//! so the trait's [`Record::should_output`] hook can be overridden — the
//! derive macro emits only the four mandatory methods and there's no
//! opt-in for behaviour overrides. Once the macro grows a
//! `should_output_fn` knob this file can switch back to the derive form.
use crate::error::{CaError, CaResult};
use crate::server::record::{FieldDesc, Record};
use crate::types::{DbFieldType, EpicsValue};
pub struct LongoutRecord {
pub val: i32,
pub egu: String,
pub hopr: i32,
pub lopr: i32,
pub drvh: i32,
pub drvl: i32,
pub hihi: i32,
pub high: i32,
pub low: i32,
pub lolo: i32,
pub hhsv: i16,
pub hsv: i16,
pub lsv: i16,
pub llsv: i16,
pub hyst: f64,
pub lalm: f64,
pub ivoa: i16,
pub ivov: i32,
pub adel: f64,
pub mdel: f64,
pub alst: f64,
pub mlst: f64,
pub omsl: i16,
pub dol: String,
pub simm: i16,
pub siml: String,
pub siol: String,
pub sims: i16,
/// Output Execution Option (epics-base 7.0.8):
///
/// | OOPT | Meaning |
/// |------|------------------------|
/// | 0 | Every Time (default) |
/// | 1 | On Change (val ≠ pval) |
/// | 2 | When Zero |
/// | 3 | When Non-zero |
/// | 4 | Transition to Zero |
/// | 5 | Transition to Non-zero |
///
/// Consulted by [`Record::should_output`] before the framework
/// writes VAL to the OUT link / device. Unknown values default to
/// `false` (no output) matching C EPICS.
pub oopt: i16,
/// Previous VAL — captured after every output cycle so OOPT=1/4/5
/// (transition modes) can detect changes. Initially zero; loops
/// after the first successful output.
pub pval: i32,
/// Mirrors C `prec->outpvt` (longoutRecord.c).
///
/// * `true` — force a write on the next process cycle regardless
/// of the OOPT=On_Change `val != pval` comparison.
/// * `false` — On_Change uses the normal comparison.
///
/// Set true at construction (= C `init_record`'s
/// `outpvt = EXEC_OUTPUT`), cleared after every successful
/// output (`on_output_complete`). The OUT-change re-trigger
/// (PR #6c573b4 part 2) is wired through
/// [`Record::special("OUT", true)`] — `RecordInstance::put_common_field`
/// fires it after `common.out` is updated, so OUT is owned by
/// the RecordInstance (common-field side), not by this struct.
pub first_output_done: bool,
/// `OOCH` (Output Exec. on OUT Change) — C `menuYesNo`. When YES
/// (1), changing the OUT field at runtime forces the very next
/// process cycle to write the current VAL regardless of OOPT
/// (epics-base PR #6c573b4 part 2, `longoutRecord.c:223`).
pub ooch: i16,
}
impl Default for LongoutRecord {
fn default() -> Self {
Self {
val: 0,
egu: String::new(),
hopr: 0,
lopr: 0,
drvh: 0, // C defaults both to 0 (equal = no clamping)
drvl: 0,
hihi: 0,
high: 0,
low: 0,
lolo: 0,
hhsv: 0,
hsv: 0,
lsv: 0,
llsv: 0,
hyst: 0.0,
lalm: 0.0,
ivoa: 0,
ivov: 0,
adel: 0.0,
mdel: 0.0,
alst: 0.0,
mlst: 0.0,
omsl: 0,
dol: String::new(),
simm: 0,
siml: String::new(),
siol: String::new(),
sims: 0,
oopt: 0,
pval: 0,
first_output_done: false,
ooch: 0,
}
}
}
impl LongoutRecord {
pub fn new(val: i32) -> Self {
Self {
val,
..Default::default()
}
}
/// Compute whether the framework should propagate VAL to the OUT
/// link / device on this process cycle, based on OOPT semantics.
/// Public so unit tests don't have to spin up the full processing
/// loop to exercise each menu value.
pub fn compute_should_output(&self) -> bool {
// C parity (longoutRecord.c::conditional_write,
// PR #6c573b4): the first-cycle force-emit applies **only**
// to OOPT=On_Change (case 1 — `outpvt == EXEC_OUTPUT`).
// Modes When_Zero / When_Non_zero / Transitions evaluate
// their condition normally on the first cycle. Earlier this
// fn generalised the force-emit across every OOPT, which
// silently emitted output on initial putValues for the
// value-only modes (e.g. When_Non_zero + initial val=0).
match self.oopt {
0 => true,
1 => !self.first_output_done || self.val != self.pval,
2 => self.val == 0,
3 => self.val != 0,
4 => self.pval != 0 && self.val == 0,
5 => self.pval == 0 && self.val != 0,
_ => false,
}
}
}
static LONGOUT_FIELDS: &[FieldDesc] = &[
FieldDesc {
name: "VAL",
dbf_type: DbFieldType::Long,
read_only: false,
},
FieldDesc {
name: "EGU",
dbf_type: DbFieldType::String,
read_only: false,
},
FieldDesc {
name: "HOPR",
dbf_type: DbFieldType::Long,
read_only: false,
},
FieldDesc {
name: "LOPR",
dbf_type: DbFieldType::Long,
read_only: false,
},
FieldDesc {
name: "DRVH",
dbf_type: DbFieldType::Long,
read_only: false,
},
FieldDesc {
name: "DRVL",
dbf_type: DbFieldType::Long,
read_only: false,
},
FieldDesc {
name: "HIHI",
dbf_type: DbFieldType::Long,
read_only: false,
},
FieldDesc {
name: "HIGH",
dbf_type: DbFieldType::Long,
read_only: false,
},
FieldDesc {
name: "LOW",
dbf_type: DbFieldType::Long,
read_only: false,
},
FieldDesc {
name: "LOLO",
dbf_type: DbFieldType::Long,
read_only: false,
},
FieldDesc {
name: "HHSV",
dbf_type: DbFieldType::Short,
read_only: false,
},
FieldDesc {
name: "HSV",
dbf_type: DbFieldType::Short,
read_only: false,
},
FieldDesc {
name: "LSV",
dbf_type: DbFieldType::Short,
read_only: false,
},
FieldDesc {
name: "LLSV",
dbf_type: DbFieldType::Short,
read_only: false,
},
FieldDesc {
name: "HYST",
dbf_type: DbFieldType::Double,
read_only: false,
},
FieldDesc {
name: "LALM",
dbf_type: DbFieldType::Double,
read_only: false,
},
FieldDesc {
name: "IVOA",
dbf_type: DbFieldType::Short,
read_only: false,
},
FieldDesc {
name: "IVOV",
dbf_type: DbFieldType::Long,
read_only: false,
},
FieldDesc {
name: "ADEL",
dbf_type: DbFieldType::Double,
read_only: false,
},
FieldDesc {
name: "MDEL",
dbf_type: DbFieldType::Double,
read_only: false,
},
FieldDesc {
name: "ALST",
dbf_type: DbFieldType::Double,
read_only: false,
},
FieldDesc {
name: "MLST",
dbf_type: DbFieldType::Double,
read_only: false,
},
FieldDesc {
name: "OMSL",
dbf_type: DbFieldType::Short,
read_only: false,
},
FieldDesc {
name: "DOL",
dbf_type: DbFieldType::String,
read_only: false,
},
FieldDesc {
name: "SIMM",
dbf_type: DbFieldType::Short,
read_only: false,
},
FieldDesc {
name: "SIML",
dbf_type: DbFieldType::String,
read_only: false,
},
FieldDesc {
name: "SIOL",
dbf_type: DbFieldType::String,
read_only: false,
},
FieldDesc {
name: "SIMS",
dbf_type: DbFieldType::Short,
read_only: false,
},
FieldDesc {
name: "OOPT",
dbf_type: DbFieldType::Short,
read_only: false,
},
FieldDesc {
name: "PVAL",
dbf_type: DbFieldType::Long,
read_only: true,
},
FieldDesc {
name: "OOCH",
dbf_type: DbFieldType::Short,
read_only: false,
},
];
impl Record for LongoutRecord {
fn record_type(&self) -> &'static str {
"longout"
}
fn field_list(&self) -> &'static [FieldDesc] {
LONGOUT_FIELDS
}
fn get_field(&self, name: &str) -> Option<EpicsValue> {
match name {
"VAL" => Some(EpicsValue::Long(self.val)),
"EGU" => Some(EpicsValue::String(self.egu.clone())),
"HOPR" => Some(EpicsValue::Long(self.hopr)),
"LOPR" => Some(EpicsValue::Long(self.lopr)),
"DRVH" => Some(EpicsValue::Long(self.drvh)),
"DRVL" => Some(EpicsValue::Long(self.drvl)),
"HIHI" => Some(EpicsValue::Long(self.hihi)),
"HIGH" => Some(EpicsValue::Long(self.high)),
"LOW" => Some(EpicsValue::Long(self.low)),
"LOLO" => Some(EpicsValue::Long(self.lolo)),
"HHSV" => Some(EpicsValue::Short(self.hhsv)),
"HSV" => Some(EpicsValue::Short(self.hsv)),
"LSV" => Some(EpicsValue::Short(self.lsv)),
"LLSV" => Some(EpicsValue::Short(self.llsv)),
"HYST" => Some(EpicsValue::Double(self.hyst)),
"LALM" => Some(EpicsValue::Double(self.lalm)),
"IVOA" => Some(EpicsValue::Short(self.ivoa)),
"IVOV" => Some(EpicsValue::Long(self.ivov)),
"ADEL" => Some(EpicsValue::Double(self.adel)),
"MDEL" => Some(EpicsValue::Double(self.mdel)),
"ALST" => Some(EpicsValue::Double(self.alst)),
"MLST" => Some(EpicsValue::Double(self.mlst)),
"OMSL" => Some(EpicsValue::Short(self.omsl)),
"DOL" => Some(EpicsValue::String(self.dol.clone())),
"SIMM" => Some(EpicsValue::Short(self.simm)),
"SIML" => Some(EpicsValue::String(self.siml.clone())),
"SIOL" => Some(EpicsValue::String(self.siol.clone())),
"SIMS" => Some(EpicsValue::Short(self.sims)),
"OOPT" => Some(EpicsValue::Short(self.oopt)),
"PVAL" => Some(EpicsValue::Long(self.pval)),
"OOCH" => Some(EpicsValue::Short(self.ooch)),
_ => None,
}
}
fn put_field(&mut self, name: &str, value: EpicsValue) -> CaResult<()> {
self.validate_put(name, &value)?;
match name {
"VAL" => {
if let EpicsValue::Long(v) = value {
self.val = v;
}
}
"EGU" => {
if let EpicsValue::String(v) = value {
self.egu = v;
}
}
"HOPR" => {
if let EpicsValue::Long(v) = value {
self.hopr = v;
}
}
"LOPR" => {
if let EpicsValue::Long(v) = value {
self.lopr = v;
}
}
"DRVH" => {
if let EpicsValue::Long(v) = value {
self.drvh = v;
}
}
"DRVL" => {
if let EpicsValue::Long(v) = value {
self.drvl = v;
}
}
"HIHI" => {
if let EpicsValue::Long(v) = value {
self.hihi = v;
}
}
"HIGH" => {
if let EpicsValue::Long(v) = value {
self.high = v;
}
}
"LOW" => {
if let EpicsValue::Long(v) = value {
self.low = v;
}
}
"LOLO" => {
if let EpicsValue::Long(v) = value {
self.lolo = v;
}
}
"HHSV" => {
if let EpicsValue::Short(v) = value {
self.hhsv = v;
}
}
"HSV" => {
if let EpicsValue::Short(v) = value {
self.hsv = v;
}
}
"LSV" => {
if let EpicsValue::Short(v) = value {
self.lsv = v;
}
}
"LLSV" => {
if let EpicsValue::Short(v) = value {
self.llsv = v;
}
}
"HYST" => {
if let EpicsValue::Double(v) = value {
self.hyst = v;
}
}
"LALM" => {
if let EpicsValue::Double(v) = value {
self.lalm = v;
}
}
"IVOA" => {
if let EpicsValue::Short(v) = value {
self.ivoa = v;
}
}
"IVOV" => {
if let EpicsValue::Long(v) = value {
self.ivov = v;
}
}
"ADEL" => {
if let EpicsValue::Double(v) = value {
self.adel = v;
}
}
"MDEL" => {
if let EpicsValue::Double(v) = value {
self.mdel = v;
}
}
"ALST" => {
if let EpicsValue::Double(v) = value {
self.alst = v;
}
}
"MLST" => {
if let EpicsValue::Double(v) = value {
self.mlst = v;
}
}
"OMSL" => {
if let EpicsValue::Short(v) = value {
self.omsl = v;
}
}
"DOL" => {
if let EpicsValue::String(v) = value {
self.dol = v;
}
}
"SIMM" => {
if let EpicsValue::Short(v) = value {
self.simm = v;
}
}
"SIML" => {
if let EpicsValue::String(v) = value {
self.siml = v;
}
}
"SIOL" => {
if let EpicsValue::String(v) = value {
self.siol = v;
}
}
"SIMS" => {
if let EpicsValue::Short(v) = value {
self.sims = v;
}
}
"OOPT" => {
if let EpicsValue::Short(v) = value {
self.oopt = v;
}
}
"PVAL" => {
// Read-only — validate_put already rejected the call.
return Err(CaError::ReadOnlyField("PVAL".into()));
}
"OOCH" => {
if let EpicsValue::Short(v) = value {
self.ooch = v;
}
}
_ => return Err(CaError::FieldNotFound(name.to_string())),
}
self.on_put(name);
Ok(())
}
/// epics-base 7.0.8: gate the OUT-link / device write on OOPT.
/// Returns `false` for OOPT modes whose condition isn't met
/// (On Change / transition / when-zero / when-non-zero). The
/// framework's processing.rs reads this before issuing the
/// device write, so a `should_output() == false` cycle skips
/// the device call entirely.
fn should_output(&self) -> bool {
self.compute_should_output()
}
/// Latch PVAL after a successful output so OOPT=1/4/5 transition
/// detection on the next cycle has the right reference point.
/// Also marks `first_output_done` so subsequent cycles apply the
/// real OOPT comparison rather than the first-cycle force-emit.
fn on_output_complete(&mut self) {
self.pval = self.val;
self.first_output_done = true;
}
/// C `longoutRecord.c::special` (PR #6c573b4 part 2): when the
/// OUT link is rewritten at runtime and `OOCH=YES`, force the
/// next process cycle to emit output regardless of OOPT=On_Change
/// `val == pval` suppression. We fire on `after=true` because the
/// common `out` field is set inside `RecordInstance::put_common_field`
/// AFTER the `special(name, false)` validation hook — so by the
/// time we run, `instance.common.out` already holds the new value
/// (we don't need to read it; just react to the field name).
fn special(&mut self, field: &str, after: bool) -> crate::error::CaResult<()> {
if after && field == "OUT" && self.ooch == 1 {
self.first_output_done = false;
}
Ok(())
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn oopt_every_time_default() {
let r = LongoutRecord::new(42);
assert!(r.compute_should_output());
}
#[test]
fn oopt_on_change() {
let mut r = LongoutRecord::new(0);
r.oopt = 1;
r.first_output_done = true; // simulate post-first-cycle state
r.pval = 5;
r.val = 5;
assert!(!r.compute_should_output(), "val==pval suppresses output");
r.val = 7;
assert!(r.compute_should_output(), "val!=pval permits output");
}
#[test]
fn oopt_when_zero_and_nonzero() {
let mut r = LongoutRecord::new(0);
r.first_output_done = true;
r.oopt = 2; // When Zero
r.val = 0;
assert!(r.compute_should_output());
r.val = 1;
assert!(!r.compute_should_output());
r.oopt = 3; // When Non-zero
r.val = 0;
assert!(!r.compute_should_output());
r.val = 5;
assert!(r.compute_should_output());
}
#[test]
fn oopt_transitions() {
let mut r = LongoutRecord::new(0);
r.first_output_done = true;
r.oopt = 4; // Transition to Zero
r.pval = 5;
r.val = 0;
assert!(r.compute_should_output(), "nonzero→zero transition fires");
r.pval = 0;
r.val = 0;
assert!(!r.compute_should_output(), "zero→zero suppressed");
r.oopt = 5; // Transition to Non-zero
r.pval = 0;
r.val = 5;
assert!(r.compute_should_output(), "zero→nonzero transition fires");
r.pval = 5;
r.val = 5;
assert!(!r.compute_should_output(), "nonzero→nonzero suppressed");
}
#[test]
fn oopt_unknown_value_suppresses_output() {
// C EPICS treats unknown OOPT as "don't output" to fail-safe.
let mut r = LongoutRecord::new(0);
r.first_output_done = true;
r.oopt = 99;
assert!(!r.compute_should_output());
}
/// epics-base PR #6c573b4: the very first process cycle must
/// always emit, even when OOPT is in a transition mode whose
/// comparison says "no change". Before the fix, OOPT=1/4 with
/// the default val=pval=0 would silently swallow the initial
/// device write.
#[test]
fn oopt_on_change_first_cycle_forces_output() {
let mut r = LongoutRecord::new(0);
r.oopt = 1; // On Change
// val == pval == 0, first_output_done still false (default).
assert!(
r.compute_should_output(),
"first cycle must force output regardless of OOPT comparison"
);
// Simulate the framework calling on_output_complete after
// the device write succeeds.
r.on_output_complete();
assert!(r.first_output_done);
// Next cycle with val still equal to pval honours OOPT=1.
assert!(
!r.compute_should_output(),
"post-first cycle val==pval honours OOPT=On Change suppression"
);
}
#[test]
fn oopt_transition_modes_do_not_force_first_cycle() {
// C parity (longoutRecord.c::conditional_write,
// PR #6c573b4): only OOPT=On_Change (case 1) gets the
// first-cycle force-emit via `outpvt == EXEC_OUTPUT`.
// Transition modes (4=To_Zero, 5=To_Non_zero) and value
// modes (2=When_Zero, 3=When_Non_zero) evaluate their
// condition normally on the first cycle. With val == pval
// == 0:
// OOPT=4 (To Zero): pval!=0 && val==0 → false → no out
// OOPT=5 (To Non-zero): pval==0 && val!=0 → false → no out
let mut r = LongoutRecord::new(0);
r.oopt = 4;
assert!(
!r.compute_should_output(),
"OOPT=4 (Transition_To_Zero) first cycle val==pval==0: no output"
);
r.oopt = 5;
assert!(
!r.compute_should_output(),
"OOPT=5 (Transition_To_Non_zero) first cycle val==pval==0: no output"
);
}
#[test]
fn put_get_oopt_roundtrip() {
let mut r = LongoutRecord::new(0);
r.put_field("OOPT", EpicsValue::Short(3)).unwrap();
assert_eq!(r.get_field("OOPT"), Some(EpicsValue::Short(3)));
assert_eq!(r.oopt, 3);
}
#[test]
fn pval_is_read_only_via_put_field() {
let mut r = LongoutRecord::new(0);
let err = r.put_field("PVAL", EpicsValue::Long(42)).unwrap_err();
assert!(matches!(err, CaError::ReadOnlyField(_)));
}
/// PR #6c573b4 part 2 (`longoutRecord.c:222-225`): writing OUT at
/// runtime with `OOCH = YES` re-arms the next-cycle write under
/// OOPT=On_Change. Without OOCH=YES the OUT change is silent (C
/// requires the explicit opt-in). Exercised through the
/// `Record::special("OUT", true)` hook the way
/// `RecordInstance::put_common_field` fires it on a real OUT
/// rewrite.
#[test]
fn ooch_yes_out_change_forces_next_on_change_write() {
let mut r = LongoutRecord::new(0);
r.oopt = 1; // On_Change
r.first_output_done = true; // post-first-cycle: no force pending
r.val = 5;
r.pval = 5; // val == pval — normally On_Change would skip.
assert!(
!r.compute_should_output(),
"baseline: val==pval should suppress On_Change output"
);
// OOCH=YES then OUT change (fired via `special(OUT, true)`).
r.ooch = 1;
r.special("OUT", true).unwrap();
assert!(
r.compute_should_output(),
"OOCH=YES + OUT change must force the next On_Change write"
);
}
#[test]
fn ooch_no_out_change_is_silent() {
let mut r = LongoutRecord::new(0);
r.oopt = 1; // On_Change
r.first_output_done = true;
r.val = 5;
r.pval = 5;
// OOCH=NO (default 0) — OUT change must NOT trigger a write.
r.special("OUT", true).unwrap();
assert!(
!r.compute_should_output(),
"OOCH=NO: OUT change is silent, val==pval still suppresses output"
);
}
}