epics_base_rs/types/dbr.rs
1use crate::error::{CaError, CaResult};
2
3// DBR type ranges (matches db_access.h):
4// native (0..=6), STS (7..=13), TIME (14..=20), GR (21..=27),
5// CTRL (28..=34), special PUT_ACKT (35), PUT_ACKS (36),
6// STSACK_STRING (37), CLASS_NAME (38).
7
8// Native scalar types (also exposed via DbFieldType enum)
9pub const DBR_STRING: u16 = 0;
10pub const DBR_SHORT: u16 = 1;
11pub const DBR_FLOAT: u16 = 2;
12pub const DBR_ENUM: u16 = 3;
13pub const DBR_CHAR: u16 = 4;
14pub const DBR_LONG: u16 = 5;
15pub const DBR_DOUBLE: u16 = 6;
16// `DBR_INT` is the libca alias for `DBR_SHORT`.
17pub const DBR_INT: u16 = DBR_SHORT;
18
19// Status-only metadata layer (CA-261)
20pub const DBR_STS_STRING: u16 = 7;
21pub const DBR_STS_SHORT: u16 = 8;
22pub const DBR_STS_FLOAT: u16 = 9;
23pub const DBR_STS_ENUM: u16 = 10;
24pub const DBR_STS_CHAR: u16 = 11;
25pub const DBR_STS_LONG: u16 = 12;
26pub const DBR_STS_DOUBLE: u16 = 13;
27pub const DBR_STS_INT: u16 = DBR_STS_SHORT;
28
29// Status + timestamp layer (CA-262)
30pub const DBR_TIME_STRING: u16 = 14;
31pub const DBR_TIME_SHORT: u16 = 15;
32pub const DBR_TIME_FLOAT: u16 = 16;
33pub const DBR_TIME_ENUM: u16 = 17;
34pub const DBR_TIME_CHAR: u16 = 18;
35pub const DBR_TIME_LONG: u16 = 19;
36pub const DBR_TIME_DOUBLE: u16 = 20;
37pub const DBR_TIME_INT: u16 = DBR_TIME_SHORT;
38
39// Status + graphic (display limits / units / precision) layer (CA-263)
40pub const DBR_GR_STRING: u16 = 21;
41pub const DBR_GR_SHORT: u16 = 22;
42pub const DBR_GR_FLOAT: u16 = 23;
43pub const DBR_GR_ENUM: u16 = 24;
44pub const DBR_GR_CHAR: u16 = 25;
45pub const DBR_GR_LONG: u16 = 26;
46pub const DBR_GR_DOUBLE: u16 = 27;
47pub const DBR_GR_INT: u16 = DBR_GR_SHORT;
48
49// Status + graphic + control limits layer (CA-264)
50pub const DBR_CTRL_STRING: u16 = 28;
51pub const DBR_CTRL_SHORT: u16 = 29;
52pub const DBR_CTRL_FLOAT: u16 = 30;
53pub const DBR_CTRL_ENUM: u16 = 31;
54pub const DBR_CTRL_CHAR: u16 = 32;
55pub const DBR_CTRL_LONG: u16 = 33;
56pub const DBR_CTRL_DOUBLE: u16 = 34;
57pub const DBR_CTRL_INT: u16 = DBR_CTRL_SHORT;
58
59// Special alarm-acknowledgement / introspection types
60pub const DBR_PUT_ACKT: u16 = 35;
61pub const DBR_PUT_ACKS: u16 = 36;
62pub const DBR_STSACK_STRING: u16 = 37;
63/// Returns the IOC's record-type class name as a 40-byte string
64/// (CA-268, db_access.h: `DBR_CLASS_NAME`).
65pub const DBR_CLASS_NAME: u16 = 38;
66
67/// Last allocated DBR type code, matching the C `LAST_BUFFER_TYPE` macro.
68pub const LAST_BUFFER_TYPE: u16 = DBR_CLASS_NAME;
69
70/// EPICS DBR field types (native types only)
71#[derive(Debug, Clone, Copy, PartialEq, Eq)]
72#[repr(u16)]
73pub enum DbFieldType {
74 String = 0,
75 Short = 1, // aka Int16
76 Float = 2,
77 Enum = 3,
78 Char = 4, // aka UInt8
79 Long = 5, // aka Int32
80 Double = 6,
81 /// Internal-only type for int64in/int64out records.
82 /// No CA wire type 7 exists; over CA these PVs appear as Double (type 6).
83 Int64 = 7,
84 /// Internal-only type for unsigned 64-bit EPICS fields (C `DBF_UINT64`,
85 /// dbStatic `dbfType` index 8). The CA wire protocol has no 64-bit
86 /// type, so over CA these PVs appear as Double (type 6); over PVA they
87 /// are served natively as `ulong`. Mirrors `Int64`'s CA handling.
88 UInt64 = 8,
89 /// Internal-only type for unsigned 16-bit EPICS fields (C `DBF_USHORT`,
90 /// dbStatic `dbfType`). The CA wire protocol has no unsigned types, so
91 /// the IOC promotes `DBF_USHORT` to the next signed type that holds its
92 /// full `0..=65535` range — `DBR_LONG` (the C `dbDBRnewToDBRold` table,
93 /// `db_convert.h`: `5, /*DBR_USHORT to DBR_LONG*/`). Over PVA pvxs
94 /// serves it natively as `ushort` (`ioc/typeutils.cpp:38-40`:
95 /// `DBR_USHORT -> TypeCode::UInt16`). The discriminant is an internal
96 /// marker (not a CA wire code); see [`Self::ca_wire_type`].
97 UShort = 9,
98 /// Internal-only type for unsigned 32-bit EPICS fields (C `DBF_ULONG`,
99 /// dbStatic `dbfType`). `0..=4294967295` does not fit in `i32`, so the
100 /// IOC promotes `DBF_ULONG` to `DBR_DOUBLE` over CA exactly like
101 /// `UInt64`/`Int64` (`db_convert.h`: `6, /*DBR_ULONG to DBR_DOUBLE*/`).
102 /// Over PVA pvxs serves it natively as `uint` (`ioc/typeutils.cpp:43-44`:
103 /// `DBR_ULONG -> TypeCode::UInt32`).
104 ULong = 10,
105}
106
107impl DbFieldType {
108 pub fn from_u16(v: u16) -> CaResult<Self> {
109 match v {
110 0 => Ok(Self::String),
111 1 => Ok(Self::Short),
112 2 => Ok(Self::Float),
113 3 => Ok(Self::Enum),
114 4 => Ok(Self::Char),
115 5 => Ok(Self::Long),
116 6 => Ok(Self::Double),
117 _ => Err(CaError::UnsupportedType(v)),
118 }
119 }
120
121 /// Size in bytes for a single element of this type's native carrier.
122 ///
123 /// This is the carrier width (`UShort` = 2, `ULong` = 4), not the
124 /// CA-wire-promoted width: the CA value path always promotes via
125 /// [`crate::types::EpicsValue::dbr_type`] first and sizes buffers off
126 /// the promoted type (`UShort`→`Long`=4, `ULong`→`Double`=8), so this
127 /// width is never used to size a CA value array for the unsigned types.
128 pub fn element_size(&self) -> usize {
129 match self {
130 Self::String => 40, // MAX_STRING_SIZE
131 Self::Short | Self::Enum | Self::UShort => 2,
132 Self::Float | Self::Long | Self::ULong => 4,
133 Self::Char => 1,
134 Self::Double | Self::Int64 | Self::UInt64 => 8,
135 }
136 }
137
138 /// Return the wire type code as a `u16`. The internal-only types have
139 /// no CA wire code, so they report the signed CA type the IOC promotes
140 /// them to (C `dbDBRnewToDBRold`, `db_convert.h`): `Int64`/`UInt64`/
141 /// `ULong` → `DBR_DOUBLE` (6), `UShort` → `DBR_LONG` (5, the smallest
142 /// signed CA type that holds the full `0..=65535` range).
143 fn ca_wire_type(&self) -> u16 {
144 match self {
145 Self::Int64 | Self::UInt64 | Self::ULong => Self::Double as u16,
146 Self::UShort => Self::Long as u16,
147 other => *other as u16,
148 }
149 }
150
151 /// Return the `DBR_STS_xxx` type code for this native type
152 /// (Int64 maps to `DBR_STS_DOUBLE`).
153 pub fn sts_dbr_type(&self) -> u16 {
154 self.ca_wire_type() + 7
155 }
156
157 /// Return the `DBR_TIME_xxx` type code for this native type
158 /// (Int64 maps to `DBR_TIME_DOUBLE`).
159 pub fn time_dbr_type(&self) -> u16 {
160 self.ca_wire_type() + 14
161 }
162
163 /// Return the `DBR_GR_xxx` type code for this native type
164 /// (Int64 maps to `DBR_GR_DOUBLE`).
165 pub fn gr_dbr_type(&self) -> u16 {
166 self.ca_wire_type() + 21
167 }
168
169 /// Return the `DBR_CTRL_xxx` type code for this native type
170 /// (Int64 maps to `DBR_CTRL_DOUBLE`).
171 pub fn ctrl_dbr_type(&self) -> u16 {
172 self.ca_wire_type() + 28
173 }
174
175 /// Calculate total buffer size for N elements of this type.
176 /// Equivalent to C EPICS dbValueSize(type) * count.
177 pub fn buffer_size(&self, count: usize) -> usize {
178 self.element_size() * count
179 }
180
181 /// Map field type to request type (C EPICS mapDBFToDBR).
182 /// DBF_MENU and DBF_DEVICE map to DBR_ENUM in C EPICS.
183 /// In Rust these are already represented as DbFieldType::Enum,
184 /// so this is an identity mapping for documentation/completeness.
185 pub fn to_dbr_type(&self) -> DbFieldType {
186 *self
187 }
188}
189
190/// dbStatic link-field classes — the three `dbfType` values that mark a
191/// record field as a *link* rather than a value
192/// (`dbFldTypes.h`: `DBF_INLINK`=14, `DBF_OUTLINK`=15, `DBF_FWDLINK`=16).
193///
194/// pvxs rejects a QSRV group PUT to any field whose
195/// `dbChannelFinalFieldType` falls in `DBF_INLINK..=DBF_FWDLINK`
196/// (`ioc/groupsource.cpp:596-606`). The Rust port has no dbStatic field
197/// table, so [`dbf_link_class`] reconstructs the same classification from
198/// the EPICS Base / synApps `*.dbd(.pod)` link-field families and returns
199/// the matching class. Consumers gate "is this field a link" on
200/// `dbf_link_class(..).is_some()` (or [`is_link_dbf_type`] when they
201/// already hold a dbStatic code), rather than maintaining their own
202/// partial spelling lists.
203#[derive(Debug, Clone, Copy, PartialEq, Eq)]
204#[repr(u8)]
205pub enum DbfLinkClass {
206 /// `DBF_INLINK` (14) — an input link (`INP`, `DOL`, `SIML`, …).
207 InLink = DBF_INLINK,
208 /// `DBF_OUTLINK` (15) — an output link (`OUT`, `LNKn`, …).
209 OutLink = DBF_OUTLINK,
210 /// `DBF_FWDLINK` (16) — a forward link (`FLNK`).
211 FwdLink = DBF_FWDLINK,
212}
213
214/// `dbFldTypes.h` `DBF_INLINK`.
215pub const DBF_INLINK: u8 = 14;
216/// `dbFldTypes.h` `DBF_OUTLINK`.
217pub const DBF_OUTLINK: u8 = 15;
218/// `dbFldTypes.h` `DBF_FWDLINK`.
219pub const DBF_FWDLINK: u8 = 16;
220
221impl DbfLinkClass {
222 /// The dbStatic `dbfType` numeric code (`dbFldTypes.h`).
223 pub fn dbf_type(self) -> u8 {
224 self as u8
225 }
226}
227
228/// True iff `dbf_type` is a link class — the exact
229/// `DBF_INLINK <= t <= DBF_FWDLINK` range check pvxs applies in
230/// `ioc/groupsource.cpp:596-606`. Use when a caller already holds a
231/// dbStatic field-type code; [`dbf_link_class`] is the name-keyed entry
232/// point for the Rust port, which carries no dbStatic table.
233pub fn is_link_dbf_type(dbf_type: u8) -> bool {
234 (DBF_INLINK..=DBF_FWDLINK).contains(&dbf_type)
235}
236
237/// Classify a record field by its dbStatic link class, or `None` when it
238/// is not a link field. This is the single canonical owner of the
239/// "is this field a link" rule for the Rust port — the structural
240/// replacement for the partial, per-consumer name lists that the
241/// `groupsource.cpp:596-606` review flagged (a record-type-blind
242/// spelling list re-opens the bypass for every record that names a link
243/// field outside the list).
244///
245/// `record_type` is the record's `recordType` (`ai`, `bo`, `seq`, …); it
246/// is consulted to resolve the two direction-ambiguous fields that share a
247/// spelling across record families:
248/// - `SIOL` is `DBF_OUTLINK` on output records and `DBF_INLINK` on input
249/// records (compare `boRecord.dbd.pod:318` `SIOL,DBF_OUTLINK` vs
250/// `aiRecord`/`biRecord` `SIOL,DBF_INLINK`).
251/// - `LNK0..LNKF` is `DBF_FWDLINK` on `fanoutRecord` (the multi-forward
252/// fan-out family) and `DBF_OUTLINK` on `seqRecord` / synApps
253/// `sseqRecord` (compare `fanoutRecord.dbd.pod` `field(LNK0,DBF_FWDLINK)`
254/// vs `seqRecord.dbd.pod` `field(LNK0,DBF_OUTLINK)`).
255///
256/// Every other family has a fixed class across all record types.
257///
258/// Names and classes are taken verbatim from EPICS Base / synApps
259/// `*.dbd(.pod)`:
260/// - dbCommon: `FLNK`→Fwd, `SDIS`/`TSEL`→In.
261/// - `INP`/`DOL`/`SIML`/`NVL`/`SVL`/`SUBL`/`SELL`→In, `OUT`→Out.
262/// - `SIOL`→Out on output records, else In.
263/// - `INPA..INPU`/`INP0..INP9`→In; `OUTA..OUTU`→Out.
264/// - `DOL0..DOL9`/`DOLA..DOLF`→In.
265/// - `LNK0..LNK9`/`LNKA..LNKF`→Fwd on `fanout`, else Out.
266pub fn dbf_link_class(record_type: &str, field: &str) -> Option<DbfLinkClass> {
267 use DbfLinkClass::*;
268 let f = field.trim().to_ascii_uppercase();
269
270 // Exact dbCommon + record-specific named link fields.
271 match f.as_str() {
272 "FLNK" => return Some(FwdLink),
273 "SDIS" | "TSEL" | "INP" | "DOL" | "SIML" | "NVL" | "SVL" | "SUBL" | "SELL" => {
274 return Some(InLink);
275 }
276 "OUT" => return Some(OutLink),
277 "SIOL" => {
278 return Some(if is_output_record_type(record_type) {
279 OutLink
280 } else {
281 InLink
282 });
283 }
284 _ => {}
285 }
286
287 // Indexed / lettered link families: a known prefix plus exactly one
288 // alphanumeric suffix character. The DBDs use `A..U` / `0..9` / `A..F`,
289 // but any single-alnum suffix on these prefixes is a link in every
290 // base record that defines the family, so a one-char-suffix rule is
291 // both complete and on the safe (reject) side for custom records.
292 let one_alnum = |rest: &str| rest.len() == 1 && rest.as_bytes()[0].is_ascii_alphanumeric();
293 // `LNK0..LNKF` is direction-ambiguous by record type the same way
294 // `SIOL` is: `fanoutRecord` declares the family as `DBF_FWDLINK`
295 // (multi-forward fan-out), every other family that uses the spelling
296 // (`seqRecord`, synApps `sseqRecord`) declares it `DBF_OUTLINK`.
297 // Resolve it once here so the prefix table below carries a single
298 // class per spelling rather than collapsing fanout's forward links to
299 // output links by prefix.
300 let lnk_class = if is_fanout_link_record(record_type) {
301 FwdLink
302 } else {
303 OutLink
304 };
305 for (prefix, class) in [
306 ("INP", InLink),
307 ("DOL", InLink),
308 ("OUT", OutLink),
309 ("LNK", lnk_class),
310 ] {
311 if let Some(rest) = f.strip_prefix(prefix) {
312 if one_alnum(rest) {
313 return Some(class);
314 }
315 }
316 }
317 None
318}
319
320/// Record types whose `SIOL` simulation-output link is `DBF_OUTLINK`
321/// (the output records). Input records declare `SIOL` as `DBF_INLINK`.
322/// Same output-record set as the device-write records — compare
323/// `crate::server::record::Record::can_device_write`.
324fn is_output_record_type(record_type: &str) -> bool {
325 matches!(
326 record_type,
327 "ao" | "bo" | "longout" | "int64out" | "mbbo" | "mbboDirect" | "stringout" | "lso" | "aao"
328 )
329}
330
331/// `fanoutRecord` is the one Base family whose `LNK0..LNKF` fields are
332/// `DBF_FWDLINK` (forward links fired in numerical order). Every other
333/// family that uses the same `LNK*` spelling — `seqRecord`, synApps
334/// `sseqRecord` — declares them `DBF_OUTLINK`. Compare
335/// `fanoutRecord.dbd.pod` `field(LNK0,DBF_FWDLINK)` vs `seqRecord.dbd.pod`
336/// `field(LNK0,DBF_OUTLINK)`.
337fn is_fanout_link_record(record_type: &str) -> bool {
338 record_type == "fanout"
339}
340
341/// Calculate buffer size for a DBR type including metadata, matching C
342/// `dbr_size_n(TYPE, COUNT) = dbr_size[TYPE] + (COUNT-1)*dbr_value_size[TYPE]`.
343///
344/// the metadata length is taken from
345/// [`crate::types::codec::dbr_meta_size`] — the single owner that the
346/// serializers (`serialize_dbr` / `encode_dbr`) emit against — so the
347/// explicit-count pad/truncate and no-read-access frame paths size
348/// TIME / GR / CTRL bodies exactly as the encoder writes them. A
349/// `metadata_matches_encoded_length` test pins `encoded_len ==
350/// dbr_buffer_size` across the whole (dbr_type, native) matrix, so the
351/// sizer can no longer drift from the encoder.
352pub fn dbr_buffer_size(dbr_type: u16, native_type: DbFieldType, count: usize) -> usize {
353 // DBR_CLASS_NAME (38) is always one MAX_STRING_SIZE (40) string,
354 // regardless of `count` or `native_type` — it carries no value[]
355 // array, so the generic meta+value formula does not apply.
356 if dbr_type == DBR_CLASS_NAME {
357 return 40;
358 }
359 let value_size = native_type.element_size() * count;
360 crate::types::codec::dbr_meta_size(dbr_type, native_type) + value_size
361}
362
363/// Extract the native DBF type index (0-6) from any DBR type code.
364fn dbr_native_index(dbr_type: u16) -> Option<u16> {
365 match dbr_type {
366 0..=6 => Some(dbr_type),
367 7..=13 => Some(dbr_type - 7),
368 14..=20 => Some(dbr_type - 14),
369 21..=27 => Some(dbr_type - 21),
370 28..=34 => Some(dbr_type - 28),
371 // Alarm-acknowledge writes carry a single u16, so map them to
372 // Short for codec purposes. STSACK_STRING returns a string body
373 // so it maps to String.
374 35 | 36 => Some(1), // DBR_PUT_ACKT / DBR_PUT_ACKS — u16
375 37 => Some(0), // DBR_STSACK_STRING — value is a string
376 // DBR_CLASS_NAME is a single fixed 40-byte string carrying the
377 // record's recordType. Treat as String for codec purposes.
378 38 => Some(0),
379 _ => None,
380 }
381}
382
383pub fn native_type_for_dbr(dbr_type: u16) -> CaResult<DbFieldType> {
384 match dbr_native_index(dbr_type) {
385 Some(idx) => DbFieldType::from_u16(idx),
386 None => Err(CaError::UnsupportedType(dbr_type)),
387 }
388}
389
390/// DBR request-type names indexed by type code, mirroring the C
391/// `dbr_text[]` table (`ca/src/client/access.cpp`). Index 0 =
392/// `DBR_STRING` … index 38 = `DBR_CLASS_NAME`.
393const DBR_TEXT: [&str; (LAST_BUFFER_TYPE + 1) as usize] = [
394 "DBR_STRING",
395 "DBR_SHORT",
396 "DBR_FLOAT",
397 "DBR_ENUM",
398 "DBR_CHAR",
399 "DBR_LONG",
400 "DBR_DOUBLE",
401 "DBR_STS_STRING",
402 "DBR_STS_SHORT",
403 "DBR_STS_FLOAT",
404 "DBR_STS_ENUM",
405 "DBR_STS_CHAR",
406 "DBR_STS_LONG",
407 "DBR_STS_DOUBLE",
408 "DBR_TIME_STRING",
409 "DBR_TIME_SHORT",
410 "DBR_TIME_FLOAT",
411 "DBR_TIME_ENUM",
412 "DBR_TIME_CHAR",
413 "DBR_TIME_LONG",
414 "DBR_TIME_DOUBLE",
415 "DBR_GR_STRING",
416 "DBR_GR_SHORT",
417 "DBR_GR_FLOAT",
418 "DBR_GR_ENUM",
419 "DBR_GR_CHAR",
420 "DBR_GR_LONG",
421 "DBR_GR_DOUBLE",
422 "DBR_CTRL_STRING",
423 "DBR_CTRL_SHORT",
424 "DBR_CTRL_FLOAT",
425 "DBR_CTRL_ENUM",
426 "DBR_CTRL_CHAR",
427 "DBR_CTRL_LONG",
428 "DBR_CTRL_DOUBLE",
429 "DBR_PUT_ACKT",
430 "DBR_PUT_ACKS",
431 "DBR_STSACK_STRING",
432 "DBR_CLASS_NAME",
433];
434
435/// Resolve a DBR request-type name to its type code, mirroring the C
436/// `dbr_text_to_type` macro (`db_access.h`): an exact, **case-sensitive**
437/// `strcmp` search of the `dbr_text[]` table. Returns the matching code
438/// (`0..=38`) or `None` when no name matches.
439///
440/// The case sensitivity is faithful to C — the `caget`/`caput` tools
441/// feed `-d <type>` straight through this search, so `-d DBR_TIME_FLOAT`
442/// resolves while `-d dbr_time_float` does not (the C tool then reverts
443/// to its plain/native request). Callers that accept the bare family
444/// (`caget -d TIME_FLOAT`) retry with a `DBR_` prefix, exactly as
445/// `caget.c` does.
446pub fn dbr_text_to_type(text: &str) -> Option<u16> {
447 DBR_TEXT.iter().position(|&n| n == text).map(|i| i as u16)
448}
449
450#[cfg(test)]
451mod buffer_size_tests {
452 use super::*;
453
454 /// STS meta size is per-type. `dbr_sts_double` carries a
455 /// 4-byte `dbr_long_t` RISC_pad (db_access.h:233-238) → meta 8.
456 #[test]
457 fn sts_double_meta_is_8() {
458 // scalar: 8 (meta) + 8 (value) = 16
459 assert_eq!(dbr_buffer_size(DBR_STS_DOUBLE, DbFieldType::Double, 1), 16);
460 // n elements: 8 + 8*n
461 assert_eq!(
462 dbr_buffer_size(DBR_STS_DOUBLE, DbFieldType::Double, 5),
463 8 + 8 * 5
464 );
465 }
466
467 /// `dbr_sts_char` carries a 1-byte RISC_pad
468 /// (db_access.h:218-223) → meta 5.
469 #[test]
470 fn sts_char_meta_is_5() {
471 assert_eq!(dbr_buffer_size(DBR_STS_CHAR, DbFieldType::Char, 1), 6);
472 assert_eq!(dbr_buffer_size(DBR_STS_CHAR, DbFieldType::Char, 10), 5 + 10);
473 }
474
475 /// types with no STS RISC pad keep the flat 4-byte meta.
476 #[test]
477 fn sts_short_meta_is_4() {
478 assert_eq!(dbr_buffer_size(DBR_STS_SHORT, DbFieldType::Short, 1), 6);
479 assert_eq!(dbr_buffer_size(DBR_STS_LONG, DbFieldType::Long, 1), 8);
480 assert_eq!(dbr_buffer_size(DBR_STS_FLOAT, DbFieldType::Float, 1), 8);
481 }
482
483 /// Plain values carry no metadata.
484 #[test]
485 fn plain_value_size_only() {
486 assert_eq!(dbr_buffer_size(DBR_DOUBLE, DbFieldType::Double, 3), 24);
487 }
488
489 /// TIME structs carry a per-type RISC pad before `value[0]`
490 /// (C `dbr_time_*`, db_access.h:250-300). The pre-fix flat 12-byte
491 /// TIME meta truncated double/short/enum/char bodies.
492 #[test]
493 fn time_meta_includes_risc_pad() {
494 // double: 12 + RISC_pad(4) + value(8) = 24 (was wrongly 20).
495 assert_eq!(dbr_buffer_size(DBR_TIME_DOUBLE, DbFieldType::Double, 1), 24);
496 // short/enum: 12 + pad(2) + value(2) = 16.
497 assert_eq!(dbr_buffer_size(DBR_TIME_SHORT, DbFieldType::Short, 1), 16);
498 assert_eq!(dbr_buffer_size(DBR_TIME_ENUM, DbFieldType::Enum, 1), 16);
499 // char: 12 + pad(3) + value(1) = 16.
500 assert_eq!(dbr_buffer_size(DBR_TIME_CHAR, DbFieldType::Char, 1), 16);
501 // float/long: no pad (value already 4-aligned at offset 12).
502 assert_eq!(dbr_buffer_size(DBR_TIME_FLOAT, DbFieldType::Float, 1), 16);
503 assert_eq!(dbr_buffer_size(DBR_TIME_LONG, DbFieldType::Long, 1), 16);
504 // Explicit count scales the value array after the pad.
505 assert_eq!(
506 dbr_buffer_size(DBR_TIME_DOUBLE, DbFieldType::Double, 4),
507 16 + 8 * 4
508 );
509 }
510
511 /// GR/CTRL metadata is per native type (the pre-fix single
512 /// broad formula over-padded short/char/float/long and dropped the
513 /// enum `no_str` word).
514 #[test]
515 fn gr_ctrl_meta_is_per_type() {
516 // GR (6 limits): head(4) + layout.
517 assert_eq!(dbr_buffer_size(DBR_GR_SHORT, DbFieldType::Short, 1), 24 + 2);
518 assert_eq!(dbr_buffer_size(DBR_GR_FLOAT, DbFieldType::Float, 1), 40 + 4);
519 assert_eq!(
520 dbr_buffer_size(DBR_GR_DOUBLE, DbFieldType::Double, 1),
521 64 + 8
522 );
523 assert_eq!(dbr_buffer_size(DBR_GR_CHAR, DbFieldType::Char, 1), 19 + 1);
524 assert_eq!(dbr_buffer_size(DBR_GR_LONG, DbFieldType::Long, 1), 36 + 4);
525 // Enum: head(4) + no_str(2) + 16*26 strings = 422, value(2).
526 assert_eq!(dbr_buffer_size(DBR_GR_ENUM, DbFieldType::Enum, 1), 422 + 2);
527 // CTRL adds two control limits.
528 assert_eq!(
529 dbr_buffer_size(DBR_CTRL_DOUBLE, DbFieldType::Double, 1),
530 80 + 8
531 );
532 assert_eq!(
533 dbr_buffer_size(DBR_CTRL_SHORT, DbFieldType::Short, 1),
534 28 + 2
535 );
536 assert_eq!(dbr_buffer_size(DBR_CTRL_CHAR, DbFieldType::Char, 1), 21 + 1);
537 }
538}
539
540#[cfg(test)]
541mod dbf_link_class_tests {
542 use super::*;
543
544 #[test]
545 fn dbcommon_links_classified_uniformly() {
546 // Present on every record (dbCommon.dbd).
547 assert_eq!(dbf_link_class("ai", "FLNK"), Some(DbfLinkClass::FwdLink));
548 assert_eq!(dbf_link_class("ao", "SDIS"), Some(DbfLinkClass::InLink));
549 assert_eq!(dbf_link_class("calc", "TSEL"), Some(DbfLinkClass::InLink));
550 }
551
552 #[test]
553 fn record_specific_link_families_the_old_name_list_missed() {
554 // The families the reviewed partial spelling list omitted, each a
555 // DBF_INLINK/OUTLINK in EPICS Base `*.dbd.pod`:
556 // seqRecord DOL0 (INLINK) / LNK0 (OUTLINK) / DOLA / DOLF / LNKF
557 assert_eq!(dbf_link_class("seq", "DOL0"), Some(DbfLinkClass::InLink));
558 assert_eq!(dbf_link_class("seq", "LNK0"), Some(DbfLinkClass::OutLink));
559 assert_eq!(dbf_link_class("seq", "DOLA"), Some(DbfLinkClass::InLink));
560 assert_eq!(dbf_link_class("seq", "DOLF"), Some(DbfLinkClass::InLink));
561 assert_eq!(dbf_link_class("seq", "LNKF"), Some(DbfLinkClass::OutLink));
562 // selRecord NVL (INLINK); histogramRecord SVL (INLINK)
563 assert_eq!(dbf_link_class("sel", "NVL"), Some(DbfLinkClass::InLink));
564 assert_eq!(
565 dbf_link_class("histogram", "SVL"),
566 Some(DbfLinkClass::InLink)
567 );
568 // calc/aSub INPA..INPU (INLINK); fanout/aSub OUTA (OUTLINK)
569 assert_eq!(dbf_link_class("calc", "INPA"), Some(DbfLinkClass::InLink));
570 assert_eq!(dbf_link_class("aSub", "INPU"), Some(DbfLinkClass::InLink));
571 assert_eq!(
572 dbf_link_class("fanout", "OUTA"),
573 Some(DbfLinkClass::OutLink)
574 );
575 // printf INP0..INP9 (INLINK)
576 assert_eq!(dbf_link_class("printf", "INP0"), Some(DbfLinkClass::InLink));
577 }
578
579 #[test]
580 fn siol_class_depends_on_record_direction() {
581 // boRecord.dbd.pod:318 SIOL=DBF_OUTLINK; ai/bi SIOL=DBF_INLINK.
582 assert_eq!(dbf_link_class("bo", "SIOL"), Some(DbfLinkClass::OutLink));
583 assert_eq!(dbf_link_class("ao", "SIOL"), Some(DbfLinkClass::OutLink));
584 assert_eq!(dbf_link_class("ai", "SIOL"), Some(DbfLinkClass::InLink));
585 assert_eq!(dbf_link_class("bi", "SIOL"), Some(DbfLinkClass::InLink));
586 // SIML is always DBF_INLINK regardless of direction.
587 assert_eq!(dbf_link_class("bo", "SIML"), Some(DbfLinkClass::InLink));
588 assert_eq!(dbf_link_class("ai", "SIML"), Some(DbfLinkClass::InLink));
589 }
590
591 /// `LNK0..LNKF` shares
592 /// its spelling across two DBF classes — `fanoutRecord` declares the
593 /// family `DBF_FWDLINK`, while `seqRecord`/`sseqRecord` declare it
594 /// `DBF_OUTLINK`. The classifier must resolve by `record_type`, not
595 /// collapse every `LNK*` to OutLink by prefix.
596 #[test]
597 fn lnk_class_depends_on_record_type() {
598 // fanoutRecord.dbd.pod field(LNK0,DBF_FWDLINK) … field(LNKF,…).
599 assert_eq!(
600 dbf_link_class("fanout", "LNK0"),
601 Some(DbfLinkClass::FwdLink),
602 "fanout LNK0 is DBF_FWDLINK (16), not OUTLINK"
603 );
604 assert_eq!(
605 dbf_link_class("fanout", "LNKF"),
606 Some(DbfLinkClass::FwdLink),
607 "fanout LNKF is DBF_FWDLINK (16), not OUTLINK"
608 );
609 // seqRecord.dbd.pod field(LNK0,DBF_OUTLINK): the default direction
610 // for the shared spelling.
611 assert_eq!(dbf_link_class("seq", "LNK0"), Some(DbfLinkClass::OutLink));
612 assert_eq!(dbf_link_class("seq", "LNKF"), Some(DbfLinkClass::OutLink));
613 // synApps sseqRecord LNK1..LNK9 are also DBF_OUTLINK (LNK10 is the
614 // two-char spelling the one-alnum suffix rule deliberately rejects).
615 assert_eq!(dbf_link_class("sseq", "LNK1"), Some(DbfLinkClass::OutLink));
616 // fanout's dbCommon FLNK is still a forward link (unchanged).
617 assert_eq!(
618 dbf_link_class("fanout", "FLNK"),
619 Some(DbfLinkClass::FwdLink)
620 );
621 }
622
623 #[test]
624 fn plain_value_fields_are_not_links() {
625 for f in [
626 "VAL", "EGU", "PREC", "HOPR", "RVAL", "DESC", "A", "B", "OVAL",
627 ] {
628 assert_eq!(dbf_link_class("ai", f), None, "{f} must not be a link");
629 }
630 }
631
632 #[test]
633 fn case_insensitive_and_trims() {
634 assert_eq!(dbf_link_class("ai", "inp"), Some(DbfLinkClass::InLink));
635 assert_eq!(dbf_link_class("ao", " OUT "), Some(DbfLinkClass::OutLink));
636 }
637
638 #[test]
639 fn dbf_codes_and_range_match_pvxs() {
640 assert_eq!(DbfLinkClass::InLink.dbf_type(), 14);
641 assert_eq!(DbfLinkClass::OutLink.dbf_type(), 15);
642 assert_eq!(DbfLinkClass::FwdLink.dbf_type(), 16);
643 // pvxs groupsource.cpp:596-606 range check.
644 assert!(is_link_dbf_type(DBF_INLINK));
645 assert!(is_link_dbf_type(DBF_OUTLINK));
646 assert!(is_link_dbf_type(DBF_FWDLINK));
647 assert!(!is_link_dbf_type(13)); // DBF_DEVICE
648 assert!(!is_link_dbf_type(17)); // DBF_NOACCESS
649 assert!(!is_link_dbf_type(0)); // DBF_STRING
650 }
651}
652
653#[cfg(test)]
654mod dbr_text_tests {
655 use super::*;
656
657 #[test]
658 fn resolves_every_type_name_to_its_code() {
659 // Round-trip the whole table: each name resolves to its index.
660 for (code, name) in DBR_TEXT.iter().enumerate() {
661 assert_eq!(
662 dbr_text_to_type(name),
663 Some(code as u16),
664 "{name} should resolve to {code}"
665 );
666 }
667 // Boundary names spot-check (the cited High-finding values).
668 assert_eq!(dbr_text_to_type("DBR_STRING"), Some(DBR_STRING));
669 assert_eq!(dbr_text_to_type("DBR_TIME_FLOAT"), Some(DBR_TIME_FLOAT));
670 assert_eq!(
671 dbr_text_to_type("DBR_STSACK_STRING"),
672 Some(DBR_STSACK_STRING)
673 );
674 assert_eq!(dbr_text_to_type("DBR_CLASS_NAME"), Some(DBR_CLASS_NAME));
675 }
676
677 #[test]
678 fn is_case_sensitive_like_c_strcmp() {
679 // C `dbr_text_to_type` uses `strcmp`, so lowercase does not
680 // match — the C tools then revert to their plain request.
681 assert_eq!(dbr_text_to_type("dbr_time_float"), None);
682 assert_eq!(dbr_text_to_type("DBR_Time_Float"), None);
683 }
684
685 #[test]
686 fn unknown_and_bare_family_names_do_not_match() {
687 // Bare family names need the caller's `DBR_` retry — the raw
688 // search rejects them, matching C's first-pass `strcmp`.
689 assert_eq!(dbr_text_to_type("TIME_FLOAT"), None);
690 assert_eq!(dbr_text_to_type("DOUBLE"), None);
691 assert_eq!(dbr_text_to_type("DBR_NONSENSE"), None);
692 assert_eq!(dbr_text_to_type(""), None);
693 }
694}