Skip to main content

libicsneoc2_sys/
lib.rs

1#![allow(non_upper_case_globals)]
2#![allow(non_camel_case_types)]
3#![allow(non_snake_case)]
4#![allow(rustdoc::broken_intra_doc_links)]
5
6include!("bindings.rs");
7
8use std::ffi::CString;
9
10/// Error returned when a raw integer does not map to a known enum variant.
11#[derive(Debug, Clone, Copy, PartialEq, Eq)]
12pub struct InvalidEnumValue(pub i64);
13
14impl std::fmt::Display for InvalidEnumValue {
15    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
16        write!(f, "invalid enum discriminant: {}", self.0)
17    }
18}
19
20impl std::error::Error for InvalidEnumValue {}
21
22/// Generates a `TryFrom<$raw> for $enum` impl that matches each variant by
23/// its `#[repr(i32)]` discriminant, returning [`InvalidEnumValue`] on mismatch.
24macro_rules! impl_try_from {
25    ($raw:ty => $enum:ident { $($variant:ident),* $(,)? }) => {
26        // Compile-time exhaustiveness check (same-crate, so #[non_exhaustive] is irrelevant)
27        const _: fn($enum) = |v| match v {
28            $($enum::$variant => {},)*
29        };
30        impl TryFrom<$raw> for $enum {
31            type Error = InvalidEnumValue;
32            fn try_from(val: $raw) -> std::result::Result<Self, Self::Error> {
33                match val as i32 {
34                    $(x if x == $enum::$variant as i32 => Ok($enum::$variant),)*
35                    _ => Err(InvalidEnumValue(val as i64)),
36                }
37            }
38        }
39    };
40}
41
42// ---------------------------------------------------------------------------
43// Error
44// ---------------------------------------------------------------------------
45
46impl_try_from!(icsneoc2_error_t => Error {
47    Success, InvalidParameters, OpenFailed, GoOnlineFailed,
48    EnableMessagePollingFailed, SyncRtcFailed, GetMessagesFailed,
49    InvalidType, RtcFailure, GetSettingsFailure, SetSettingsFailure,
50    TransmitMessageFailed, StringCopyFailed, InvalidDevice,
51    InvalidMessage, OutOfMemory, FormatDiskFailed, ScriptStartFailed,
52    ScriptStopFailed, ScriptClearFailed, ScriptUploadFailed,
53    ScriptLoadPrepareFailed, CloseFailed, ReconnectFailed, Maxsize,
54});
55
56impl From<Error> for icsneoc2_error_t {
57    fn from(val: Error) -> Self {
58        val as icsneoc2_error_t
59    }
60}
61
62// ---------------------------------------------------------------------------
63// Netid
64// ---------------------------------------------------------------------------
65
66impl_try_from!(icsneoc2_netid_t => Netid {
67    Device, Dwcan01, Dwcan08, Swcan01, Lsftcan01, Fordscp, J1708, Aux,
68    J1850vpw, Iso9141, DiskData, Main51, Red, Sci, Iso914102, Iso14230,
69    Lin01, Ae01, Ae02, Ae03, RedExtMemoryread, RedIntMemoryread,
70    RedDflashRead, NeoMemorySdread, CanErrbits, NeoMemoryWriteDone,
71    RedWaveCan1Logical, RedWaveCan2Logical, RedWaveLin1Logical,
72    RedWaveLin2Logical, RedWaveLin1Analog, RedWaveLin2Analog,
73    RedWaveMiscAnalog, RedWaveMiscdio2Logical, RedNetworkComEnableEx,
74    RedNeoviNetwork, RedReadBaudSettings, RedOldformat, RedScopeCapture,
75    RedHardwareExcep, RedGetRtc, Iso914103, Dwcan02, Dwcan03, Ae04, Ae05,
76    Iso914104, Lin02, Lin03, Lin04, MostUnused, RedAppError, Cgi,
77    ResetStatus, FbStatus, AppSignalStatus, ReadDatalinkCmTxMsg,
78    ReadDatalinkCmRxMsg, LoggingOverflow, ReadSettings, Dwcan04, Dwcan05,
79    Rs232, Uart01, Uart02, Uart03, Uart04, Swcan02, EthernetDaq,
80    DataToHost, TextapiToHost, Spi01, Ae06, RedVbat, Ae07, Ae08, Ae09,
81    Ae10, Ae11, Flexray01a, Flexray01b, Flexray02a, Flexray02b, Lin05,
82    Flexray01, Flexray02, Ae12, I2c01, Most25, Most50, Most150, Ethernet01,
83    Gmfsa, Tcp, Dwcan06, Dwcan07, Lin06, Lsftcan02, LogicalDiskInfo,
84    WiviCommand, ScriptStatus, EthPhyControl, ExtendedCommand, ExtendedData,
85    FlexrayControl, CoreminiPreload, HwComLatencyTest, DeviceStatus, Udp,
86    ForwardedMessage, I2c02, I2c03, I2c04, Ethernet02, EthernetTxWrap,
87    A2b01, A2b02, Ethernet03, Wbms01, Dwcan09, Dwcan10, Dwcan11, Dwcan12,
88    Dwcan13, Dwcan14, Dwcan15, Dwcan16, Lin07, Lin08, Spi02, Mdio01,
89    Mdio02, Mdio03, Mdio04, Mdio05, Mdio06, Mdio07, Mdio08, Ae13, Ae14,
90    Ae15, Ae16, Spi03, Spi04, Spi05, Spi06, Spi07, Spi08, Lin09, Lin10,
91    Lin11, Lin12, Lin13, Lin14, Lin15, Lin16, Maxsize, Any, Invalid,
92});
93
94impl From<Netid> for icsneoc2_netid_t {
95    fn from(val: Netid) -> Self {
96        val as icsneoc2_netid_t
97    }
98}
99
100// ---------------------------------------------------------------------------
101// NetworkType
102// ---------------------------------------------------------------------------
103
104impl_try_from!(icsneoc2_network_type_t => NetworkType {
105    Invalid, Internal, Can, Lin, Flexray, Most, Ethernet, Lsftcan, Swcan,
106    Iso9141, I2c, A2b, Spi, Mdio, AutomotiveEthernet, Maxsize, Any, Other,
107});
108
109impl From<NetworkType> for icsneoc2_network_type_t {
110    fn from(val: NetworkType) -> Self {
111        val as icsneoc2_network_type_t
112    }
113}
114
115// ---------------------------------------------------------------------------
116// Devicetype
117// ---------------------------------------------------------------------------
118
119impl_try_from!(icsneoc2_devicetype_t => Devicetype {
120    Unknown, Blue, EcuAvb, RadSupermoon, DwVcan, RadMoon2, RadMars, Vcan41,
121    Fire, RadPluto, Vcan42El, RadioCanhub, NeoEcu12, Obd2LcBadge,
122    RadMoonDuo, Fire3, Vcan3, RadJupiter, Vcan4Industrial, RadGigastar,
123    Red2, Etherbadge, RadA2b, RadEpsilon, RadEpsilonXl, RadGalaxy2,
124    RadMoon3, RadComet, Fire3Flexray, Connect, RadComet3, RadMoonT1s,
125    RadGigastar2, Fire3T1sLin, Fire3T1sSent, RadGemini, Red, Ecu, Ievb,
126    Pendant, Obd2Pro, EcuchipUart, Plasma, NeoAnalog, CtObd, Ion, RadStar,
127    Vcan44, Vcan42, CmProbe, Eevb, VcanRf, Fire2, Flex, RadGalaxy,
128    RadStar2, Vividcan, Obd2Sim, Maxsize,
129});
130
131impl From<Devicetype> for icsneoc2_devicetype_t {
132    fn from(val: Devicetype) -> Self {
133        val as icsneoc2_devicetype_t
134    }
135}
136
137// ---------------------------------------------------------------------------
138// LinMode
139// ---------------------------------------------------------------------------
140
141impl_try_from!(icsneoc2_lin_mode_t => LinMode {
142    Sleep, Slow, Normal, Fast, Maxsize,
143});
144
145impl From<LinMode> for icsneoc2_lin_mode_t {
146    fn from(val: LinMode) -> Self {
147        val as icsneoc2_lin_mode_t
148    }
149}
150
151// ---------------------------------------------------------------------------
152// AeLinkMode
153// ---------------------------------------------------------------------------
154
155impl_try_from!(icsneoc2_ae_link_mode_t => AeLinkMode {
156    Invalid, Auto, Master, Slave, Maxsize,
157});
158
159impl From<AeLinkMode> for icsneoc2_ae_link_mode_t {
160    fn from(val: AeLinkMode) -> Self {
161        (val as i32) as icsneoc2_ae_link_mode_t
162    }
163}
164
165// ---------------------------------------------------------------------------
166// EthPhyLinkMode
167// ---------------------------------------------------------------------------
168
169impl_try_from!(icsneoc2_eth_phy_link_mode_t => EthPhyLinkMode {
170    AutoNegotiation, EthPhyLinkMode10mbpsHalfduplex,
171    EthPhyLinkMode10mbpsFullDuplex, EthPhyLinkMode100mbpsHalfDuplex,
172    EthPhyLinkMode100mbpsFullDuplex, EthPhyLinkMode1gbpsHalfDuplex,
173    EthPhyLinkMode1gbpsFullDuplex, EthPhyLinkMode25gbpsFullDuplex,
174    EthPhyLinkMode5gbpsFullDuplex, EthPhyLinkMode10gbpsFullDuplex, Maxsize,
175});
176
177impl From<EthPhyLinkMode> for icsneoc2_eth_phy_link_mode_t {
178    fn from(val: EthPhyLinkMode) -> Self {
179        val as icsneoc2_eth_phy_link_mode_t
180    }
181}
182
183// ---------------------------------------------------------------------------
184// MiscIoAnalogVoltage
185// ---------------------------------------------------------------------------
186
187impl_try_from!(icsneoc2_misc_io_analog_voltage_t => MiscIoAnalogVoltage {
188    V0, V1, V2, V3, V4, V5, Maxsize,
189});
190
191impl From<MiscIoAnalogVoltage> for icsneoc2_misc_io_analog_voltage_t {
192    fn from(val: MiscIoAnalogVoltage) -> Self {
193        (val as u32) as icsneoc2_misc_io_analog_voltage_t
194    }
195}
196
197// ---------------------------------------------------------------------------
198// IoType
199// ---------------------------------------------------------------------------
200
201impl_try_from!(icsneoc2_io_type_t => IoType {
202    EthActivation, UsbHostPower, BackupPowerEn, BackupPowerGood, Misc,
203    Emisc, Maxsize,
204});
205
206impl From<IoType> for icsneoc2_io_type_t {
207    fn from(val: IoType) -> Self {
208        val as icsneoc2_io_type_t
209    }
210}
211
212// ---------------------------------------------------------------------------
213// DiskLayout
214// ---------------------------------------------------------------------------
215
216impl_try_from!(icsneoc2_disk_layout_t => DiskLayout {
217    Spanned, Raid0, Maxsize,
218});
219
220impl From<DiskLayout> for icsneoc2_disk_layout_t {
221    fn from(val: DiskLayout) -> Self {
222        (val as i32) as icsneoc2_disk_layout_t
223    }
224}
225
226// ---------------------------------------------------------------------------
227// MemoryType
228// ---------------------------------------------------------------------------
229
230impl_try_from!(icsneoc2_memory_type_t => MemoryType {
231    Flash, Sd, Maxsize,
232});
233
234impl From<MemoryType> for icsneoc2_memory_type_t {
235    fn from(val: MemoryType) -> Self {
236        (val as i32) as icsneoc2_memory_type_t
237    }
238}
239
240impl std::fmt::Display for Netid {
241    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
242        let mut buf: Vec<u8> = vec![0; 255];
243        let mut buf_len: usize = buf.len();
244        let res = unsafe {
245            icsneoc2_netid_name_get(
246                icsneoc2_netid_t::from(*self),
247                buf.as_mut_ptr() as *mut i8,
248                &raw mut buf_len,
249            )
250        };
251        if Error::try_from(res) != Ok(Error::Success) {
252            return write!(f, "{self:?}");
253        }
254        buf.resize(buf_len, 0);
255        match CString::new(buf).map(|c| c.into_string()) {
256            Ok(Ok(s)) => write!(f, "{s}"),
257            _ => write!(f, "{self:?}"),
258        }
259    }
260}
261
262impl std::fmt::Display for Devicetype {
263    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
264        let mut buf: Vec<u8> = vec![0; 255];
265        let mut buf_len: usize = buf.len();
266        let res = unsafe {
267            icsneoc2_device_type_name_get(
268                icsneoc2_devicetype_t::from(*self),
269                buf.as_mut_ptr() as *mut i8,
270                &raw mut buf_len,
271            )
272        };
273        if Error::try_from(res) != Ok(Error::Success) {
274            return write!(f, "{self:?}");
275        }
276        buf.resize(buf_len, 0);
277        match CString::new(buf).map(|c| c.into_string()) {
278            Ok(Ok(s)) => write!(f, "{s}"),
279            _ => write!(f, "{self:?}"),
280        }
281    }
282}
283
284impl std::fmt::Display for NetworkType {
285    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
286        let mut buf: Vec<u8> = vec![0; 255];
287        let mut buf_len: usize = buf.len();
288        let res = unsafe {
289            icsneoc2_network_type_name_get(
290                icsneoc2_network_type_t::from(*self),
291                buf.as_mut_ptr() as *mut i8,
292                &raw mut buf_len,
293            )
294        };
295        if Error::try_from(res) != Ok(Error::Success) {
296            return write!(f, "{self:?}");
297        }
298        buf.resize(buf_len, 0);
299        match CString::new(buf).map(|c| c.into_string()) {
300            Ok(Ok(s)) => write!(f, "{s}"),
301            _ => write!(f, "{self:?}"),
302        }
303    }
304}
305
306impl std::fmt::LowerHex for Netid {
307    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
308        std::fmt::LowerHex::fmt(&(*self as u32), f)
309    }
310}