industrial_protocols_asinterface/
lib.rs1use industrial_protocols_kernel::connection::Connection;
7use industrial_protocols_kernel::error::{ProtocolError, Result};
8use industrial_protocols_kernel::sdk::*;
9
10pub struct AsiProtocol {
11 pub slave_addr: u8,
12}
13impl Default for AsiProtocol {
14 fn default() -> Self {
15 Self::new()
16 }
17}
18
19impl AsiProtocol {
20 pub fn new() -> Self {
21 AsiProtocol { slave_addr: 0 }
22 }
23 pub fn slave(mut self, addr: u8) -> Self {
24 self.slave_addr = addr;
25 self
26 }
27}
28
29pub fn build_data_exchange(addr: u8, data: u8) -> Vec<u8> {
33 let st = 0x00; let control = 0x01; vec![st, control, addr & 0x1F, data & 0x0F, 0x00]
36}
37
38pub fn build_read_id(addr: u8) -> Vec<u8> {
39 vec![0x00, 0x03, addr & 0x1F, 0x0A, 0x00] }
41
42impl Protocol for AsiProtocol {
43 fn name(&self) -> &'static str {
44 "asinterface"
45 }
46 fn version(&self) -> &'static str {
47 "1.0.0"
48 }
49 fn category(&self) -> ProtocolCategory {
50 ProtocolCategory::Fieldbus
51 }
52 fn supported_variants(&self) -> &'static [&'static str] {
53 &["v3"]
54 }
55}
56
57impl ProtocolRead for AsiProtocol {
58 type Address = u8;
59 type Value = u8;
60 fn read(&self, conn: &mut dyn Connection, _addr: &Self::Address) -> Result<Self::Value> {
61 let f = build_data_exchange(self.slave_addr, 0);
62 let resp = conn.send(&f).map_err(ProtocolError::from)?;
63 Ok(if resp.len() >= 4 { resp[3] } else { 0 })
64 }
65 fn read_batch(&self, c: &mut dyn Connection, a: &[Self::Address]) -> Result<Vec<Self::Value>> {
66 a.iter().map(|x| self.read(c, x)).collect()
67 }
68 fn read_range(
69 &self,
70 c: &mut dyn Connection,
71 s: &Self::Address,
72 n: u16,
73 ) -> Result<Vec<Self::Value>> {
74 (0..n).map(|_| self.read(c, s)).collect()
75 }
76}
77
78impl ProtocolWrite for AsiProtocol {
79 type Address = u8;
80 type Value = u8;
81 fn write(
82 &self,
83 conn: &mut dyn Connection,
84 _addr: &Self::Address,
85 value: &Self::Value,
86 ) -> Result<()> {
87 conn.send(&build_data_exchange(self.slave_addr, *value))
88 .map_err(ProtocolError::from)?;
89 Ok(())
90 }
91 fn write_batch(
92 &self,
93 c: &mut dyn Connection,
94 i: &[(Self::Address, Self::Value)],
95 ) -> Result<()> {
96 for (a, v) in i {
97 self.write(c, a, v)?;
98 }
99 Ok(())
100 }
101}
102
103impl ProtocolHealth for AsiProtocol {
104 fn health(&self, conn: &mut dyn Connection) -> Result<HealthStatus> {
105 let start = std::time::Instant::now();
106 match conn.send(&build_read_id(self.slave_addr)) {
107 Ok(_) => Ok(HealthStatus {
108 state: HealthState::Healthy,
109 latency_ms: start.elapsed().as_millis() as u64,
110 last_seen: std::time::Instant::now(),
111 details: "asi read id ok".into(),
112 }),
113 Err(e) => Ok(HealthStatus {
114 state: HealthState::Unhealthy,
115 latency_ms: 0,
116 last_seen: std::time::Instant::now(),
117 details: format!("{e}"),
118 }),
119 }
120 }
121}
122
123impl ProtocolDiagnostics for AsiProtocol {
124 fn diagnostics(&self, _: &mut dyn Connection) -> Result<DiagnosticsReport> {
125 Ok(DiagnosticsReport {
126 device_id: format!("asi:{}", self.slave_addr),
127 uptime_secs: None,
128 error_count: 0,
129 last_error: None,
130 custom_fields: Default::default(),
131 })
132 }
133}
134
135#[cfg(test)]
136mod tests {
137 use super::*;
138 #[test]
139 fn test_exchange() {
140 let f = build_data_exchange(1, 0);
141 assert_eq!(f.len(), 5);
142 }
143 #[test]
144 fn test_meta() {
145 assert_eq!(AsiProtocol::new().name(), "asinterface");
146 }
147}