tinkerforge_async/bindings/
can_v2_bricklet.rs

1/* ***********************************************************
2 * This file was automatically generated on 2024-02-16.      *
3 *                                                           *
4 * Rust Bindings Version 2.0.20                              *
5 *                                                           *
6 * If you have a bugfix for this file and want to commit it, *
7 * please fix the bug in the generator. You can find a link  *
8 * to the generators git repository on tinkerforge.com       *
9 *************************************************************/
10
11//! Communicates with CAN bus devices.
12//!
13//! See also the documentation [here](https://www.tinkerforge.com/en/doc/Software/Bricklets/CANV2_Bricklet_Rust.html).
14#[allow(unused_imports)]
15use crate::{
16    base58::Uid, byte_converter::*, converting_high_level_callback_receiver::ConvertingHighLevelCallbackReceiver,
17    converting_receiver::BrickletRecvTimeoutError, device::*, error::TinkerforgeError, ip_connection::async_io::AsyncIpConnection,
18    low_level_traits::LowLevelRead,
19};
20#[allow(unused_imports)]
21use futures_core::Stream;
22#[allow(unused_imports)]
23use tokio_stream::StreamExt;
24pub enum CanV2BrickletFunction {
25    WriteFrameLowLevel,
26    ReadFrameLowLevel,
27    SetFrameReadCallbackConfiguration,
28    GetFrameReadCallbackConfiguration,
29    SetTransceiverConfiguration,
30    GetTransceiverConfiguration,
31    SetQueueConfigurationLowLevel,
32    GetQueueConfigurationLowLevel,
33    SetReadFilterConfiguration,
34    GetReadFilterConfiguration,
35    GetErrorLogLowLevel,
36    SetCommunicationLedConfig,
37    GetCommunicationLedConfig,
38    SetErrorLedConfig,
39    GetErrorLedConfig,
40    SetFrameReadableCallbackConfiguration,
41    GetFrameReadableCallbackConfiguration,
42    SetErrorOccurredCallbackConfiguration,
43    GetErrorOccurredCallbackConfiguration,
44    GetSpitfpErrorCount,
45    SetBootloaderMode,
46    GetBootloaderMode,
47    SetWriteFirmwarePointer,
48    WriteFirmware,
49    SetStatusLedConfig,
50    GetStatusLedConfig,
51    GetChipTemperature,
52    Reset,
53    WriteUid,
54    ReadUid,
55    GetIdentity,
56    CallbackFrameReadLowLevel,
57    CallbackFrameReadable,
58    CallbackErrorOccurred,
59}
60impl From<CanV2BrickletFunction> for u8 {
61    fn from(fun: CanV2BrickletFunction) -> Self {
62        match fun {
63            CanV2BrickletFunction::WriteFrameLowLevel => 1,
64            CanV2BrickletFunction::ReadFrameLowLevel => 2,
65            CanV2BrickletFunction::SetFrameReadCallbackConfiguration => 3,
66            CanV2BrickletFunction::GetFrameReadCallbackConfiguration => 4,
67            CanV2BrickletFunction::SetTransceiverConfiguration => 5,
68            CanV2BrickletFunction::GetTransceiverConfiguration => 6,
69            CanV2BrickletFunction::SetQueueConfigurationLowLevel => 7,
70            CanV2BrickletFunction::GetQueueConfigurationLowLevel => 8,
71            CanV2BrickletFunction::SetReadFilterConfiguration => 9,
72            CanV2BrickletFunction::GetReadFilterConfiguration => 10,
73            CanV2BrickletFunction::GetErrorLogLowLevel => 11,
74            CanV2BrickletFunction::SetCommunicationLedConfig => 12,
75            CanV2BrickletFunction::GetCommunicationLedConfig => 13,
76            CanV2BrickletFunction::SetErrorLedConfig => 14,
77            CanV2BrickletFunction::GetErrorLedConfig => 15,
78            CanV2BrickletFunction::SetFrameReadableCallbackConfiguration => 17,
79            CanV2BrickletFunction::GetFrameReadableCallbackConfiguration => 18,
80            CanV2BrickletFunction::SetErrorOccurredCallbackConfiguration => 20,
81            CanV2BrickletFunction::GetErrorOccurredCallbackConfiguration => 21,
82            CanV2BrickletFunction::GetSpitfpErrorCount => 234,
83            CanV2BrickletFunction::SetBootloaderMode => 235,
84            CanV2BrickletFunction::GetBootloaderMode => 236,
85            CanV2BrickletFunction::SetWriteFirmwarePointer => 237,
86            CanV2BrickletFunction::WriteFirmware => 238,
87            CanV2BrickletFunction::SetStatusLedConfig => 239,
88            CanV2BrickletFunction::GetStatusLedConfig => 240,
89            CanV2BrickletFunction::GetChipTemperature => 242,
90            CanV2BrickletFunction::Reset => 243,
91            CanV2BrickletFunction::WriteUid => 248,
92            CanV2BrickletFunction::ReadUid => 249,
93            CanV2BrickletFunction::GetIdentity => 255,
94            CanV2BrickletFunction::CallbackFrameReadLowLevel => 16,
95            CanV2BrickletFunction::CallbackFrameReadable => 19,
96            CanV2BrickletFunction::CallbackErrorOccurred => 22,
97        }
98    }
99}
100pub const CAN_V2_BRICKLET_FRAME_TYPE_STANDARD_DATA: u8 = 0;
101pub const CAN_V2_BRICKLET_FRAME_TYPE_STANDARD_REMOTE: u8 = 1;
102pub const CAN_V2_BRICKLET_FRAME_TYPE_EXTENDED_DATA: u8 = 2;
103pub const CAN_V2_BRICKLET_FRAME_TYPE_EXTENDED_REMOTE: u8 = 3;
104pub const CAN_V2_BRICKLET_TRANSCEIVER_MODE_NORMAL: u8 = 0;
105pub const CAN_V2_BRICKLET_TRANSCEIVER_MODE_LOOPBACK: u8 = 1;
106pub const CAN_V2_BRICKLET_TRANSCEIVER_MODE_READ_ONLY: u8 = 2;
107pub const CAN_V2_BRICKLET_FILTER_MODE_ACCEPT_ALL: u8 = 0;
108pub const CAN_V2_BRICKLET_FILTER_MODE_MATCH_STANDARD_ONLY: u8 = 1;
109pub const CAN_V2_BRICKLET_FILTER_MODE_MATCH_EXTENDED_ONLY: u8 = 2;
110pub const CAN_V2_BRICKLET_FILTER_MODE_MATCH_STANDARD_AND_EXTENDED: u8 = 3;
111pub const CAN_V2_BRICKLET_TRANSCEIVER_STATE_ACTIVE: u8 = 0;
112pub const CAN_V2_BRICKLET_TRANSCEIVER_STATE_PASSIVE: u8 = 1;
113pub const CAN_V2_BRICKLET_TRANSCEIVER_STATE_DISABLED: u8 = 2;
114pub const CAN_V2_BRICKLET_COMMUNICATION_LED_CONFIG_OFF: u8 = 0;
115pub const CAN_V2_BRICKLET_COMMUNICATION_LED_CONFIG_ON: u8 = 1;
116pub const CAN_V2_BRICKLET_COMMUNICATION_LED_CONFIG_SHOW_HEARTBEAT: u8 = 2;
117pub const CAN_V2_BRICKLET_COMMUNICATION_LED_CONFIG_SHOW_COMMUNICATION: u8 = 3;
118pub const CAN_V2_BRICKLET_ERROR_LED_CONFIG_OFF: u8 = 0;
119pub const CAN_V2_BRICKLET_ERROR_LED_CONFIG_ON: u8 = 1;
120pub const CAN_V2_BRICKLET_ERROR_LED_CONFIG_SHOW_HEARTBEAT: u8 = 2;
121pub const CAN_V2_BRICKLET_ERROR_LED_CONFIG_SHOW_TRANSCEIVER_STATE: u8 = 3;
122pub const CAN_V2_BRICKLET_ERROR_LED_CONFIG_SHOW_ERROR: u8 = 4;
123pub const CAN_V2_BRICKLET_BOOTLOADER_MODE_BOOTLOADER: u8 = 0;
124pub const CAN_V2_BRICKLET_BOOTLOADER_MODE_FIRMWARE: u8 = 1;
125pub const CAN_V2_BRICKLET_BOOTLOADER_MODE_BOOTLOADER_WAIT_FOR_REBOOT: u8 = 2;
126pub const CAN_V2_BRICKLET_BOOTLOADER_MODE_FIRMWARE_WAIT_FOR_REBOOT: u8 = 3;
127pub const CAN_V2_BRICKLET_BOOTLOADER_MODE_FIRMWARE_WAIT_FOR_ERASE_AND_REBOOT: u8 = 4;
128pub const CAN_V2_BRICKLET_BOOTLOADER_STATUS_OK: u8 = 0;
129pub const CAN_V2_BRICKLET_BOOTLOADER_STATUS_INVALID_MODE: u8 = 1;
130pub const CAN_V2_BRICKLET_BOOTLOADER_STATUS_NO_CHANGE: u8 = 2;
131pub const CAN_V2_BRICKLET_BOOTLOADER_STATUS_ENTRY_FUNCTION_NOT_PRESENT: u8 = 3;
132pub const CAN_V2_BRICKLET_BOOTLOADER_STATUS_DEVICE_IDENTIFIER_INCORRECT: u8 = 4;
133pub const CAN_V2_BRICKLET_BOOTLOADER_STATUS_CRC_MISMATCH: u8 = 5;
134pub const CAN_V2_BRICKLET_STATUS_LED_CONFIG_OFF: u8 = 0;
135pub const CAN_V2_BRICKLET_STATUS_LED_CONFIG_ON: u8 = 1;
136pub const CAN_V2_BRICKLET_STATUS_LED_CONFIG_SHOW_HEARTBEAT: u8 = 2;
137pub const CAN_V2_BRICKLET_STATUS_LED_CONFIG_SHOW_STATUS: u8 = 3;
138
139#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
140pub struct WriteFrameLowLevel {
141    pub success: bool,
142}
143impl FromByteSlice for WriteFrameLowLevel {
144    fn bytes_expected() -> usize {
145        1
146    }
147    fn from_le_byte_slice(bytes: &[u8]) -> WriteFrameLowLevel {
148        WriteFrameLowLevel { success: <bool>::from_le_byte_slice(&bytes[0..1]) }
149    }
150}
151
152#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
153pub struct ReadFrameLowLevel {
154    pub success: bool,
155    pub frame_type: u8,
156    pub identifier: u32,
157    pub data_length: u8,
158    pub data_data: [u8; 15],
159}
160impl FromByteSlice for ReadFrameLowLevel {
161    fn bytes_expected() -> usize {
162        22
163    }
164    fn from_le_byte_slice(bytes: &[u8]) -> ReadFrameLowLevel {
165        ReadFrameLowLevel {
166            success: <bool>::from_le_byte_slice(&bytes[0..1]),
167            frame_type: <u8>::from_le_byte_slice(&bytes[1..2]),
168            identifier: <u32>::from_le_byte_slice(&bytes[2..6]),
169            data_length: <u8>::from_le_byte_slice(&bytes[6..7]),
170            data_data: <[u8; 15]>::from_le_byte_slice(&bytes[7..22]),
171        }
172    }
173}
174impl LowLevelRead<u8, ReadFrameResult> for ReadFrameLowLevel {
175    fn ll_message_length(&self) -> usize {
176        self.data_length as usize
177    }
178
179    fn ll_message_chunk_offset(&self) -> usize {
180        0
181    }
182
183    fn ll_message_chunk_data(&self) -> &[u8] {
184        &self.data_data
185    }
186
187    fn get_result(&self) -> ReadFrameResult {
188        ReadFrameResult { success: self.success, frame_type: self.frame_type, identifier: self.identifier }
189    }
190}
191
192#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
193pub struct TransceiverConfiguration {
194    pub baud_rate: u32,
195    pub sample_point: u16,
196    pub transceiver_mode: u8,
197}
198impl FromByteSlice for TransceiverConfiguration {
199    fn bytes_expected() -> usize {
200        7
201    }
202    fn from_le_byte_slice(bytes: &[u8]) -> TransceiverConfiguration {
203        TransceiverConfiguration {
204            baud_rate: <u32>::from_le_byte_slice(&bytes[0..4]),
205            sample_point: <u16>::from_le_byte_slice(&bytes[4..6]),
206            transceiver_mode: <u8>::from_le_byte_slice(&bytes[6..7]),
207        }
208    }
209}
210
211#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
212pub struct SetQueueConfigurationLowLevel {}
213impl FromByteSlice for SetQueueConfigurationLowLevel {
214    fn bytes_expected() -> usize {
215        0
216    }
217    fn from_le_byte_slice(_bytes: &[u8]) -> SetQueueConfigurationLowLevel {
218        SetQueueConfigurationLowLevel {}
219    }
220}
221
222#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
223pub struct QueueConfigurationLowLevel {
224    pub write_buffer_size: u8,
225    pub write_buffer_timeout: i32,
226    pub write_backlog_size: u16,
227    pub read_buffer_sizes_length: u8,
228    pub read_buffer_sizes_data: [i8; 32],
229    pub read_backlog_size: u16,
230}
231impl FromByteSlice for QueueConfigurationLowLevel {
232    fn bytes_expected() -> usize {
233        42
234    }
235    fn from_le_byte_slice(bytes: &[u8]) -> QueueConfigurationLowLevel {
236        QueueConfigurationLowLevel {
237            write_buffer_size: <u8>::from_le_byte_slice(&bytes[0..1]),
238            write_buffer_timeout: <i32>::from_le_byte_slice(&bytes[1..5]),
239            write_backlog_size: <u16>::from_le_byte_slice(&bytes[5..7]),
240            read_buffer_sizes_length: <u8>::from_le_byte_slice(&bytes[7..8]),
241            read_buffer_sizes_data: <[i8; 32]>::from_le_byte_slice(&bytes[8..40]),
242            read_backlog_size: <u16>::from_le_byte_slice(&bytes[40..42]),
243        }
244    }
245}
246impl LowLevelRead<i8, QueueConfigurationResult> for QueueConfigurationLowLevel {
247    fn ll_message_length(&self) -> usize {
248        self.read_buffer_sizes_length as usize
249    }
250
251    fn ll_message_chunk_offset(&self) -> usize {
252        0
253    }
254
255    fn ll_message_chunk_data(&self) -> &[i8] {
256        &self.read_buffer_sizes_data
257    }
258
259    fn get_result(&self) -> QueueConfigurationResult {
260        QueueConfigurationResult {
261            write_buffer_size: self.write_buffer_size,
262            write_buffer_timeout: self.write_buffer_timeout,
263            write_backlog_size: self.write_backlog_size,
264            read_backlog_size: self.read_backlog_size,
265        }
266    }
267}
268
269#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
270pub struct ReadFilterConfiguration {
271    pub filter_mode: u8,
272    pub filter_mask: u32,
273    pub filter_identifier: u32,
274}
275impl FromByteSlice for ReadFilterConfiguration {
276    fn bytes_expected() -> usize {
277        9
278    }
279    fn from_le_byte_slice(bytes: &[u8]) -> ReadFilterConfiguration {
280        ReadFilterConfiguration {
281            filter_mode: <u8>::from_le_byte_slice(&bytes[0..1]),
282            filter_mask: <u32>::from_le_byte_slice(&bytes[1..5]),
283            filter_identifier: <u32>::from_le_byte_slice(&bytes[5..9]),
284        }
285    }
286}
287
288#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
289pub struct ErrorLogLowLevel {
290    pub transceiver_state: u8,
291    pub transceiver_write_error_level: u8,
292    pub transceiver_read_error_level: u8,
293    pub transceiver_stuffing_error_count: u32,
294    pub transceiver_format_error_count: u32,
295    pub transceiver_ack_error_count: u32,
296    pub transceiver_bit1_error_count: u32,
297    pub transceiver_bit0_error_count: u32,
298    pub transceiver_crc_error_count: u32,
299    pub write_buffer_timeout_error_count: u32,
300    pub read_buffer_overflow_error_count: u32,
301    pub read_buffer_overflow_error_occurred_length: u8,
302    pub read_buffer_overflow_error_occurred_data: [bool; 32],
303    pub read_backlog_overflow_error_count: u32,
304}
305impl FromByteSlice for ErrorLogLowLevel {
306    fn bytes_expected() -> usize {
307        44
308    }
309    fn from_le_byte_slice(bytes: &[u8]) -> ErrorLogLowLevel {
310        ErrorLogLowLevel {
311            transceiver_state: <u8>::from_le_byte_slice(&bytes[0..1]),
312            transceiver_write_error_level: <u8>::from_le_byte_slice(&bytes[1..2]),
313            transceiver_read_error_level: <u8>::from_le_byte_slice(&bytes[2..3]),
314            transceiver_stuffing_error_count: <u32>::from_le_byte_slice(&bytes[3..7]),
315            transceiver_format_error_count: <u32>::from_le_byte_slice(&bytes[7..11]),
316            transceiver_ack_error_count: <u32>::from_le_byte_slice(&bytes[11..15]),
317            transceiver_bit1_error_count: <u32>::from_le_byte_slice(&bytes[15..19]),
318            transceiver_bit0_error_count: <u32>::from_le_byte_slice(&bytes[19..23]),
319            transceiver_crc_error_count: <u32>::from_le_byte_slice(&bytes[23..27]),
320            write_buffer_timeout_error_count: <u32>::from_le_byte_slice(&bytes[27..31]),
321            read_buffer_overflow_error_count: <u32>::from_le_byte_slice(&bytes[31..35]),
322            read_buffer_overflow_error_occurred_length: <u8>::from_le_byte_slice(&bytes[35..36]),
323            read_buffer_overflow_error_occurred_data: <[bool; 32]>::from_le_byte_slice(&bytes[36..40]),
324            read_backlog_overflow_error_count: <u32>::from_le_byte_slice(&bytes[40..44]),
325        }
326    }
327}
328impl LowLevelRead<bool, ErrorLogResult> for ErrorLogLowLevel {
329    fn ll_message_length(&self) -> usize {
330        self.read_buffer_overflow_error_occurred_length as usize
331    }
332
333    fn ll_message_chunk_offset(&self) -> usize {
334        0
335    }
336
337    fn ll_message_chunk_data(&self) -> &[bool] {
338        &self.read_buffer_overflow_error_occurred_data
339    }
340
341    fn get_result(&self) -> ErrorLogResult {
342        ErrorLogResult {
343            transceiver_state: self.transceiver_state,
344            transceiver_write_error_level: self.transceiver_write_error_level,
345            transceiver_read_error_level: self.transceiver_read_error_level,
346            transceiver_stuffing_error_count: self.transceiver_stuffing_error_count,
347            transceiver_format_error_count: self.transceiver_format_error_count,
348            transceiver_ack_error_count: self.transceiver_ack_error_count,
349            transceiver_bit1_error_count: self.transceiver_bit1_error_count,
350            transceiver_bit0_error_count: self.transceiver_bit0_error_count,
351            transceiver_crc_error_count: self.transceiver_crc_error_count,
352            write_buffer_timeout_error_count: self.write_buffer_timeout_error_count,
353            read_buffer_overflow_error_count: self.read_buffer_overflow_error_count,
354            read_backlog_overflow_error_count: self.read_backlog_overflow_error_count,
355        }
356    }
357}
358
359#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
360pub struct FrameReadLowLevelEvent {
361    pub frame_type: u8,
362    pub identifier: u32,
363    pub data_length: u8,
364    pub data_data: [u8; 15],
365}
366impl FromByteSlice for FrameReadLowLevelEvent {
367    fn bytes_expected() -> usize {
368        21
369    }
370    fn from_le_byte_slice(bytes: &[u8]) -> FrameReadLowLevelEvent {
371        FrameReadLowLevelEvent {
372            frame_type: <u8>::from_le_byte_slice(&bytes[0..1]),
373            identifier: <u32>::from_le_byte_slice(&bytes[1..5]),
374            data_length: <u8>::from_le_byte_slice(&bytes[5..6]),
375            data_data: <[u8; 15]>::from_le_byte_slice(&bytes[6..21]),
376        }
377    }
378}
379impl LowLevelRead<u8, FrameReadResult> for FrameReadLowLevelEvent {
380    fn ll_message_length(&self) -> usize {
381        self.data_length as usize
382    }
383
384    fn ll_message_chunk_offset(&self) -> usize {
385        0
386    }
387
388    fn ll_message_chunk_data(&self) -> &[u8] {
389        &self.data_data
390    }
391
392    fn get_result(&self) -> FrameReadResult {
393        FrameReadResult { frame_type: self.frame_type, identifier: self.identifier }
394    }
395}
396
397#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
398pub struct SpitfpErrorCount {
399    pub error_count_ack_checksum: u32,
400    pub error_count_message_checksum: u32,
401    pub error_count_frame: u32,
402    pub error_count_overflow: u32,
403}
404impl FromByteSlice for SpitfpErrorCount {
405    fn bytes_expected() -> usize {
406        16
407    }
408    fn from_le_byte_slice(bytes: &[u8]) -> SpitfpErrorCount {
409        SpitfpErrorCount {
410            error_count_ack_checksum: <u32>::from_le_byte_slice(&bytes[0..4]),
411            error_count_message_checksum: <u32>::from_le_byte_slice(&bytes[4..8]),
412            error_count_frame: <u32>::from_le_byte_slice(&bytes[8..12]),
413            error_count_overflow: <u32>::from_le_byte_slice(&bytes[12..16]),
414        }
415    }
416}
417
418#[derive(Clone, Debug, Default, PartialEq, Eq, Hash)]
419pub struct Identity {
420    pub uid: String,
421    pub connected_uid: String,
422    pub position: char,
423    pub hardware_version: [u8; 3],
424    pub firmware_version: [u8; 3],
425    pub device_identifier: u16,
426}
427impl FromByteSlice for Identity {
428    fn bytes_expected() -> usize {
429        25
430    }
431    fn from_le_byte_slice(bytes: &[u8]) -> Identity {
432        Identity {
433            uid: <String>::from_le_byte_slice(&bytes[0..8]),
434            connected_uid: <String>::from_le_byte_slice(&bytes[8..16]),
435            position: <char>::from_le_byte_slice(&bytes[16..17]),
436            hardware_version: <[u8; 3]>::from_le_byte_slice(&bytes[17..20]),
437            firmware_version: <[u8; 3]>::from_le_byte_slice(&bytes[20..23]),
438            device_identifier: <u16>::from_le_byte_slice(&bytes[23..25]),
439        }
440    }
441}
442
443#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
444pub struct WriteFrameResult {
445    pub success: bool,
446}
447
448#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
449pub struct ReadFrameResult {
450    pub success: bool,
451    pub frame_type: u8,
452    pub identifier: u32,
453}
454
455#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
456pub struct SetQueueConfigurationResult {}
457
458#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
459pub struct QueueConfigurationResult {
460    pub write_buffer_size: u8,
461    pub write_buffer_timeout: i32,
462    pub write_backlog_size: u16,
463    pub read_backlog_size: u16,
464}
465
466#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
467pub struct ErrorLogResult {
468    pub transceiver_state: u8,
469    pub transceiver_write_error_level: u8,
470    pub transceiver_read_error_level: u8,
471    pub transceiver_stuffing_error_count: u32,
472    pub transceiver_format_error_count: u32,
473    pub transceiver_ack_error_count: u32,
474    pub transceiver_bit1_error_count: u32,
475    pub transceiver_bit0_error_count: u32,
476    pub transceiver_crc_error_count: u32,
477    pub write_buffer_timeout_error_count: u32,
478    pub read_buffer_overflow_error_count: u32,
479    pub read_backlog_overflow_error_count: u32,
480}
481
482#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
483pub struct FrameReadResult {
484    pub frame_type: u8,
485    pub identifier: u32,
486}
487
488/// Communicates with CAN bus devices
489#[derive(Clone)]
490pub struct CanV2Bricklet {
491    device: Device,
492}
493impl CanV2Bricklet {
494    pub const DEVICE_IDENTIFIER: u16 = 2107;
495    pub const DEVICE_DISPLAY_NAME: &'static str = "CAN Bricklet 2.0";
496    /// Creates an object with the unique device ID `uid`. This object can then be used after the IP Connection `ip_connection` is connected.
497    pub fn new(uid: Uid, connection: AsyncIpConnection) -> CanV2Bricklet {
498        let mut result = CanV2Bricklet { device: Device::new([2, 0, 10], uid, connection, Self::DEVICE_DISPLAY_NAME) };
499        result.device.response_expected[u8::from(CanV2BrickletFunction::WriteFrameLowLevel) as usize] = ResponseExpectedFlag::AlwaysTrue;
500        result.device.response_expected[u8::from(CanV2BrickletFunction::ReadFrameLowLevel) as usize] = ResponseExpectedFlag::AlwaysTrue;
501        result.device.response_expected[u8::from(CanV2BrickletFunction::SetFrameReadCallbackConfiguration) as usize] =
502            ResponseExpectedFlag::True;
503        result.device.response_expected[u8::from(CanV2BrickletFunction::GetFrameReadCallbackConfiguration) as usize] =
504            ResponseExpectedFlag::AlwaysTrue;
505        result.device.response_expected[u8::from(CanV2BrickletFunction::SetTransceiverConfiguration) as usize] =
506            ResponseExpectedFlag::False;
507        result.device.response_expected[u8::from(CanV2BrickletFunction::GetTransceiverConfiguration) as usize] =
508            ResponseExpectedFlag::AlwaysTrue;
509        result.device.response_expected[u8::from(CanV2BrickletFunction::SetQueueConfigurationLowLevel) as usize] =
510            ResponseExpectedFlag::True;
511        result.device.response_expected[u8::from(CanV2BrickletFunction::GetQueueConfigurationLowLevel) as usize] =
512            ResponseExpectedFlag::AlwaysTrue;
513        result.device.response_expected[u8::from(CanV2BrickletFunction::SetReadFilterConfiguration) as usize] = ResponseExpectedFlag::False;
514        result.device.response_expected[u8::from(CanV2BrickletFunction::GetReadFilterConfiguration) as usize] =
515            ResponseExpectedFlag::AlwaysTrue;
516        result.device.response_expected[u8::from(CanV2BrickletFunction::GetErrorLogLowLevel) as usize] = ResponseExpectedFlag::AlwaysTrue;
517        result.device.response_expected[u8::from(CanV2BrickletFunction::SetCommunicationLedConfig) as usize] = ResponseExpectedFlag::False;
518        result.device.response_expected[u8::from(CanV2BrickletFunction::GetCommunicationLedConfig) as usize] =
519            ResponseExpectedFlag::AlwaysTrue;
520        result.device.response_expected[u8::from(CanV2BrickletFunction::SetErrorLedConfig) as usize] = ResponseExpectedFlag::False;
521        result.device.response_expected[u8::from(CanV2BrickletFunction::GetErrorLedConfig) as usize] = ResponseExpectedFlag::AlwaysTrue;
522        result.device.response_expected[u8::from(CanV2BrickletFunction::SetFrameReadableCallbackConfiguration) as usize] =
523            ResponseExpectedFlag::True;
524        result.device.response_expected[u8::from(CanV2BrickletFunction::GetFrameReadableCallbackConfiguration) as usize] =
525            ResponseExpectedFlag::AlwaysTrue;
526        result.device.response_expected[u8::from(CanV2BrickletFunction::SetErrorOccurredCallbackConfiguration) as usize] =
527            ResponseExpectedFlag::True;
528        result.device.response_expected[u8::from(CanV2BrickletFunction::GetErrorOccurredCallbackConfiguration) as usize] =
529            ResponseExpectedFlag::AlwaysTrue;
530        result.device.response_expected[u8::from(CanV2BrickletFunction::GetSpitfpErrorCount) as usize] = ResponseExpectedFlag::AlwaysTrue;
531        result.device.response_expected[u8::from(CanV2BrickletFunction::SetBootloaderMode) as usize] = ResponseExpectedFlag::AlwaysTrue;
532        result.device.response_expected[u8::from(CanV2BrickletFunction::GetBootloaderMode) as usize] = ResponseExpectedFlag::AlwaysTrue;
533        result.device.response_expected[u8::from(CanV2BrickletFunction::SetWriteFirmwarePointer) as usize] = ResponseExpectedFlag::False;
534        result.device.response_expected[u8::from(CanV2BrickletFunction::WriteFirmware) as usize] = ResponseExpectedFlag::AlwaysTrue;
535        result.device.response_expected[u8::from(CanV2BrickletFunction::SetStatusLedConfig) as usize] = ResponseExpectedFlag::False;
536        result.device.response_expected[u8::from(CanV2BrickletFunction::GetStatusLedConfig) as usize] = ResponseExpectedFlag::AlwaysTrue;
537        result.device.response_expected[u8::from(CanV2BrickletFunction::GetChipTemperature) as usize] = ResponseExpectedFlag::AlwaysTrue;
538        result.device.response_expected[u8::from(CanV2BrickletFunction::Reset) as usize] = ResponseExpectedFlag::False;
539        result.device.response_expected[u8::from(CanV2BrickletFunction::WriteUid) as usize] = ResponseExpectedFlag::False;
540        result.device.response_expected[u8::from(CanV2BrickletFunction::ReadUid) as usize] = ResponseExpectedFlag::AlwaysTrue;
541        result.device.response_expected[u8::from(CanV2BrickletFunction::GetIdentity) as usize] = ResponseExpectedFlag::AlwaysTrue;
542        result
543    }
544
545    /// Returns the response expected flag for the function specified by the function ID parameter.
546    /// It is true if the function is expected to send a response, false otherwise.
547    ///
548    /// For getter functions this is enabled by default and cannot be disabled, because those
549    /// functions will always send a response. For callback configuration functions it is enabled
550    /// by default too, but can be disabled by [`set_response_expected`](crate::can_v2_bricklet::CanV2Bricklet::set_response_expected).
551    /// For setter functions it is disabled by default and can be enabled.
552    ///
553    /// Enabling the response expected flag for a setter function allows to detect timeouts
554    /// and other error conditions calls of this setter as well. The device will then send a response
555    /// for this purpose. If this flag is disabled for a setter function then no response is sent
556    /// and errors are silently ignored, because they cannot be detected.
557    ///
558    /// See [`set_response_expected`](crate::can_v2_bricklet::CanV2Bricklet::set_response_expected) for the list of function ID constants available for this function.
559    pub fn get_response_expected(&mut self, fun: CanV2BrickletFunction) -> Result<bool, GetResponseExpectedError> {
560        self.device.get_response_expected(u8::from(fun))
561    }
562
563    /// Changes the response expected flag of the function specified by the function ID parameter.
564    /// This flag can only be changed for setter (default value: false) and callback configuration
565    /// functions (default value: true). For getter functions it is always enabled.
566    ///
567    /// Enabling the response expected flag for a setter function allows to detect timeouts and
568    /// other error conditions calls of this setter as well. The device will then send a response
569    /// for this purpose. If this flag is disabled for a setter function then no response is sent
570    /// and errors are silently ignored, because they cannot be detected.
571    pub fn set_response_expected(&mut self, fun: CanV2BrickletFunction, response_expected: bool) -> Result<(), SetResponseExpectedError> {
572        self.device.set_response_expected(u8::from(fun), response_expected)
573    }
574
575    /// Changes the response expected flag for all setter and callback configuration functions of this device at once.
576    pub fn set_response_expected_all(&mut self, response_expected: bool) {
577        self.device.set_response_expected_all(response_expected)
578    }
579
580    /// Returns the version of the API definition (major, minor, revision) implemented by this API bindings.
581    /// This is neither the release version of this API bindings nor does it tell you anything about the represented Brick or Bricklet.
582    pub fn get_api_version(&self) -> [u8; 3] {
583        self.device.api_version
584    }
585
586    /// See [`get_frame_read_callback_receiver`](crate::can_v2::CANV2::get_frame_read_callback_receiver)
587    pub async fn get_frame_read_low_level_callback_receiver(&mut self) -> impl Stream<Item = FrameReadLowLevelEvent> {
588        self.device
589            .get_callback_receiver(u8::from(CanV2BrickletFunction::CallbackFrameReadLowLevel))
590            .await
591            .map(|p| FrameReadLowLevelEvent::from_le_byte_slice(p.body()))
592    }
593
594    /// This receiver is triggered if a data or remote frame was received by the CAN
595    /// transceiver. The received frame can be read with [`read_frame`].
596    /// If additional frames are received, but [`read_frame`] was not called yet, the receiver
597    /// will not trigger again.
598    ///
599    /// A configurable read filter can be used to define which frames should be
600    /// received by the CAN transceiver and put into the read queue (see
601    /// [`set_read_filter_configuration`]).
602    ///
603    /// To enable this receiver, use [`set_frame_readable_callback_configuration`].
604    ///
605    /// [`read_frame`]: #method.read_frame
606    /// [`set_read_filter_configuration`]: #method.set_read_filter_configuration
607    /// [`set_frame_readable_callback_configuration`]: #method.set_frame_readable_callback_configuration
608    /// .. versionadded:: 2.0.3$nbsp;(Plugin)
609    pub async fn get_frame_readable_callback_receiver(&mut self) -> impl Stream<Item = ()> {
610        self.device.get_callback_receiver(u8::from(CanV2BrickletFunction::CallbackFrameReadable)).await.map(|_p| ())
611    }
612
613    /// This receiver is triggered if any error occurred while writing, reading or transmitting CAN frames.
614    ///
615    /// The receiver is only triggered once until [`get_error_log`] is called. That function will return
616    /// details abount the error(s) occurred.
617    ///
618    /// To enable this receiver, use [`set_error_occurred_callback_configuration`].
619    ///
620    ///
621    /// .. versionadded:: 2.0.3$nbsp;(Plugin)
622    pub async fn get_error_occurred_callback_receiver(&mut self) -> impl Stream<Item = ()> {
623        self.device.get_callback_receiver(u8::from(CanV2BrickletFunction::CallbackErrorOccurred)).await.map(|_p| ())
624    }
625
626    /// Writes a data or remote frame to the write queue to be transmitted over the
627    /// CAN transceiver.
628    ///
629    /// The Bricklet supports the standard 11-bit (CAN 2.0A) and the additional extended
630    /// 29-bit (CAN 2.0B) identifiers. For standard frames the Bricklet uses bit 0 to 10
631    /// from the ``identifier`` parameter as standard 11-bit identifier. For extended
632    /// frames the Bricklet uses bit 0 to 28 from the ``identifier`` parameter as
633    /// extended 29-bit identifier.
634    ///
635    /// The ``data`` parameter can be up to 15 bytes long. For data frames up to 8 bytes
636    /// will be used as the actual data. The length (DLC) field in the data or remote
637    /// frame will be set to the actual length of the ``data`` parameter. This allows
638    /// to transmit data and remote frames with excess length. For remote frames only
639    /// the length of the ``data`` parameter is used. The actual ``data`` bytes are
640    /// ignored.
641    ///
642    /// Returns *true* if the frame was successfully added to the write queue. Returns
643    /// *false* if the frame could not be added because write queue is already full or
644    /// because the write buffer or the write backlog are configured with a size of
645    /// zero (see [`set_queue_configuration`]).
646    ///
647    /// The write queue can overflow if frames are written to it at a higher rate
648    /// than the Bricklet can transmitted them over the CAN transceiver. This may
649    /// happen if the CAN transceiver is configured as read-only or is using a low baud
650    /// rate (see [`set_transceiver_configuration`]). It can also happen if the CAN
651    /// bus is congested and the frame cannot be transmitted because it constantly loses
652    /// arbitration or because the CAN transceiver is currently disabled due to a high
653    /// write error level (see [`get_error_log`]).
654    ///
655    /// Associated constants:
656    /// * CAN_V2_BRICKLET_FRAME_TYPE_STANDARD_DATA
657    ///	* CAN_V2_BRICKLET_FRAME_TYPE_STANDARD_REMOTE
658    ///	* CAN_V2_BRICKLET_FRAME_TYPE_EXTENDED_DATA
659    ///	* CAN_V2_BRICKLET_FRAME_TYPE_EXTENDED_REMOTE
660    pub async fn write_frame_low_level(
661        &mut self,
662        frame_type: u8,
663        identifier: u32,
664        data_length: u8,
665        data_data: &[u8; 15],
666    ) -> Result<WriteFrameLowLevel, TinkerforgeError> {
667        let mut payload = [0; 21];
668        frame_type.write_to_slice(&mut payload[0..1]);
669        identifier.write_to_slice(&mut payload[1..5]);
670        data_length.write_to_slice(&mut payload[5..6]);
671        data_data.write_to_slice(&mut payload[6..21]);
672
673        #[allow(unused_variables)]
674        let result = self.device.get(u8::from(CanV2BrickletFunction::WriteFrameLowLevel), &payload).await?;
675        Ok(WriteFrameLowLevel::from_le_byte_slice(result.body()))
676    }
677
678    /// Tries to read the next data or remote frame from the read queue and returns it.
679    /// If a frame was successfully read, then the ``success`` return value is set to
680    /// *true* and the other return values contain the frame. If the read queue is
681    /// empty and no frame could be read, then the ``success`` return value is set to
682    /// *false* and the other return values contain invalid data.
683    ///
684    /// The ``identifier`` return value follows the identifier format described for
685    /// [`write_frame`].
686    ///
687    /// The ``data`` return value can be up to 15 bytes long. For data frames up to the
688    /// first 8 bytes are the actual received data. All bytes after the 8th byte are
689    /// always zero and only there to indicate the length of a data or remote frame
690    /// with excess length. For remote frames the length of the ``data`` return value
691    /// represents the requested length. The actual ``data`` bytes are always zero.
692    ///
693    /// A configurable read filter can be used to define which frames should be
694    /// received by the CAN transceiver and put into the read queue (see
695    /// [`set_read_filter_configuration`]).
696    ///
697    /// Instead of polling with this function, you can also use receivers. See the
698    /// [`set_frame_read_callback_configuration`] function and the [`get_frame_read_callback_receiver`]
699    /// callback.
700    ///
701    /// Associated constants:
702    /// * CAN_V2_BRICKLET_FRAME_TYPE_STANDARD_DATA
703    ///	* CAN_V2_BRICKLET_FRAME_TYPE_STANDARD_REMOTE
704    ///	* CAN_V2_BRICKLET_FRAME_TYPE_EXTENDED_DATA
705    ///	* CAN_V2_BRICKLET_FRAME_TYPE_EXTENDED_REMOTE
706    pub async fn read_frame_low_level(&mut self) -> Result<ReadFrameLowLevel, TinkerforgeError> {
707        let payload = [0; 0];
708
709        #[allow(unused_variables)]
710        let result = self.device.get(u8::from(CanV2BrickletFunction::ReadFrameLowLevel), &payload).await?;
711        Ok(ReadFrameLowLevel::from_le_byte_slice(result.body()))
712    }
713
714    /// Enables and disables the [`get_frame_read_callback_receiver`] receiver.
715    ///
716    /// By default the receiver is disabled. Enabling this receiver will disable the [`get_frame_readable_callback_receiver`] receiver.
717    pub async fn set_frame_read_callback_configuration(&mut self, enabled: bool) -> Result<(), TinkerforgeError> {
718        let mut payload = [0; 1];
719        enabled.write_to_slice(&mut payload[0..1]);
720
721        #[allow(unused_variables)]
722        let result = self.device.set(u8::from(CanV2BrickletFunction::SetFrameReadCallbackConfiguration), &payload).await?;
723        Ok(())
724    }
725
726    /// Returns *true* if the [`get_frame_read_callback_receiver`] receiver is enabled, *false* otherwise.
727    pub async fn get_frame_read_callback_configuration(&mut self) -> Result<bool, TinkerforgeError> {
728        let payload = [0; 0];
729
730        #[allow(unused_variables)]
731        let result = self.device.get(u8::from(CanV2BrickletFunction::GetFrameReadCallbackConfiguration), &payload).await?;
732        Ok(bool::from_le_byte_slice(result.body()))
733    }
734
735    /// Sets the transceiver configuration for the CAN bus communication.
736    ///
737    /// The CAN transceiver has three different modes:
738    ///
739    /// * Normal: Reads from and writes to the CAN bus and performs active bus
740    ///   error detection and acknowledgement.
741    /// * Loopback: All reads and writes are performed internally. The transceiver
742    ///   is disconnected from the actual CAN bus.
743    /// * Read-Only: Only reads from the CAN bus, but does neither active bus error
744    ///   detection nor acknowledgement. Only the receiving part of the transceiver
745    ///   is connected to the CAN bus.
746    ///
747    /// Associated constants:
748    /// * CAN_V2_BRICKLET_TRANSCEIVER_MODE_NORMAL
749    ///	* CAN_V2_BRICKLET_TRANSCEIVER_MODE_LOOPBACK
750    ///	* CAN_V2_BRICKLET_TRANSCEIVER_MODE_READ_ONLY
751    pub async fn set_transceiver_configuration(
752        &mut self,
753        baud_rate: u32,
754        sample_point: u16,
755        transceiver_mode: u8,
756    ) -> Result<(), TinkerforgeError> {
757        let mut payload = [0; 7];
758        baud_rate.write_to_slice(&mut payload[0..4]);
759        sample_point.write_to_slice(&mut payload[4..6]);
760        transceiver_mode.write_to_slice(&mut payload[6..7]);
761
762        #[allow(unused_variables)]
763        let result = self.device.set(u8::from(CanV2BrickletFunction::SetTransceiverConfiguration), &payload).await?;
764        Ok(())
765    }
766
767    /// Returns the configuration as set by [`set_transceiver_configuration`].
768    ///
769    /// Associated constants:
770    /// * CAN_V2_BRICKLET_TRANSCEIVER_MODE_NORMAL
771    ///	* CAN_V2_BRICKLET_TRANSCEIVER_MODE_LOOPBACK
772    ///	* CAN_V2_BRICKLET_TRANSCEIVER_MODE_READ_ONLY
773    pub async fn get_transceiver_configuration(&mut self) -> Result<TransceiverConfiguration, TinkerforgeError> {
774        let payload = [0; 0];
775
776        #[allow(unused_variables)]
777        let result = self.device.get(u8::from(CanV2BrickletFunction::GetTransceiverConfiguration), &payload).await?;
778        Ok(TransceiverConfiguration::from_le_byte_slice(result.body()))
779    }
780
781    /// Sets the write and read queue configuration.
782    ///
783    /// The CAN transceiver has 32 buffers in total in hardware for transmitting and
784    /// receiving frames. Additionally, the Bricklet has a backlog for 768 frames in
785    /// total in software. The buffers and the backlog can be freely assigned to the
786    /// write and read queues.
787    ///
788    /// [`write_frame`] writes a frame into the write backlog. The Bricklet moves
789    /// the frame from the backlog into a free write buffer. The CAN transceiver then
790    /// transmits the frame from the write buffer to the CAN bus. If there are no
791    /// write buffers (``write_buffer_size`` is zero) or there is no write backlog
792    /// (``write_backlog_size`` is zero) then no frames can be transmitted and
793    /// [`write_frame`] returns always *false*.
794    ///
795    /// The CAN transceiver receives a frame from the CAN bus and stores it into a
796    /// free read buffer. The Bricklet moves the frame from the read buffer into the
797    /// read backlog. [`read_frame`] reads the frame from the read backlog and
798    /// returns it. If there are no read buffers (``read_buffer_sizes`` is empty) or
799    /// there is no read backlog (``read_backlog_size`` is zero) then no frames can be
800    /// received and [`read_frame`] returns always *false*.
801    ///
802    /// There can be multiple read buffers, because the CAN transceiver cannot receive
803    /// data and remote frames into the same read buffer. A positive read buffer size
804    /// represents a data frame read buffer and a negative read buffer size represents
805    /// a remote frame read buffer. A read buffer size of zero is not allowed. By
806    /// default the first read buffer is configured for data frames and the second read
807    /// buffer is configured for remote frame. There can be up to 32 different read
808    /// buffers, assuming that no write buffer is used. Each read buffer has its own
809    /// filter configuration (see [`set_read_filter_configuration`]).
810    ///
811    /// A valid queue configuration fulfills these conditions::
812    ///
813    ///  write_buffer_size + abs(read_buffer_size_0) + abs(read_buffer_size_1) + ... + abs(read_buffer_size_31) <= 32
814    ///  write_backlog_size + read_backlog_size <= 768
815    ///
816    /// The write buffer timeout has three different modes that define how a failed
817    /// frame transmission should be handled:
818    ///
819    /// * Single-Shot (< 0): Only one transmission attempt will be made. If the
820    ///   transmission fails then the frame is discarded.
821    /// * Infinite (= 0): Infinite transmission attempts will be made. The frame will
822    ///   never be discarded.
823    /// * Milliseconds (> 0): A limited number of transmission attempts will be made.
824    ///   If the frame could not be transmitted successfully after the configured
825    ///   number of milliseconds then the frame is discarded.
826    ///
827    /// The current content of the queues is lost when this function is called.
828    pub async fn set_queue_configuration_low_level(
829        &mut self,
830        write_buffer_size: u8,
831        write_buffer_timeout: i32,
832        write_backlog_size: u16,
833        read_buffer_sizes_length: u8,
834        read_buffer_sizes_data: &[i8; 32],
835        read_backlog_size: u16,
836    ) -> Result<SetQueueConfigurationLowLevel, TinkerforgeError> {
837        let mut payload = [0; 42];
838        write_buffer_size.write_to_slice(&mut payload[0..1]);
839        write_buffer_timeout.write_to_slice(&mut payload[1..5]);
840        write_backlog_size.write_to_slice(&mut payload[5..7]);
841        read_buffer_sizes_length.write_to_slice(&mut payload[7..8]);
842        read_buffer_sizes_data.write_to_slice(&mut payload[8..40]);
843        read_backlog_size.write_to_slice(&mut payload[40..42]);
844
845        #[allow(unused_variables)]
846        let result = self.device.set(u8::from(CanV2BrickletFunction::SetQueueConfigurationLowLevel), &payload).await?.unwrap();
847        Ok(SetQueueConfigurationLowLevel::from_le_byte_slice(result.body()))
848    }
849
850    /// Returns the queue configuration as set by [`set_queue_configuration`].
851    pub async fn get_queue_configuration_low_level(&mut self) -> Result<QueueConfigurationLowLevel, TinkerforgeError> {
852        let payload = [0; 0];
853
854        #[allow(unused_variables)]
855        let result = self.device.get(u8::from(CanV2BrickletFunction::GetQueueConfigurationLowLevel), &payload).await?;
856        Ok(QueueConfigurationLowLevel::from_le_byte_slice(result.body()))
857    }
858
859    /// Set the read filter configuration for the given read buffer index. This can be
860    /// used to define which frames should be received by the CAN transceiver and put
861    /// into the read buffer.
862    ///
863    /// The read filter has four different modes that define if and how the filter mask
864    /// and the filter identifier are applied:
865    ///
866    /// * Accept-All: All frames are received.
867    /// * Match-Standard-Only: Only standard frames with a matching identifier are
868    ///   received.
869    /// * Match-Extended-Only: Only extended frames with a matching identifier are
870    ///   received.
871    /// * Match-Standard-And-Extended: Standard and extended frames with a matching
872    ///   identifier are received.
873    ///
874    /// The filter mask and filter identifier are used as bit masks. Their usage
875    /// depends on the mode:
876    ///
877    /// * Accept-All: Mask and identifier are ignored.
878    /// * Match-Standard-Only: Bit 0 to 10 (11 bits) of filter mask and filter
879    ///   identifier are used to match the 11-bit identifier of standard frames.
880    /// * Match-Extended-Only: Bit 0 to 28 (29 bits) of filter mask and filter
881    ///   identifier are used to match the 29-bit identifier of extended frames.
882    /// * Match-Standard-And-Extended: Bit 18 to 28 (11 bits) of filter mask and filter
883    ///   identifier are used to match the 11-bit identifier of standard frames, bit 0
884    ///   to 17 (18 bits) are ignored in this case. Bit 0 to 28 (29 bits) of filter
885    ///   mask and filter identifier are used to match the 29-bit identifier of extended
886    ///   frames.
887    ///
888    /// The filter mask and filter identifier are applied in this way: The filter mask
889    /// is used to select the frame identifier bits that should be compared to the
890    /// corresponding filter identifier bits. All unselected bits are automatically
891    /// accepted. All selected bits have to match the filter identifier to be accepted.
892    /// If all bits for the selected mode are accepted then the frame is accepted and
893    /// is added to the read buffer.
894    ///
895    ///  Filter Mask Bit| Filter Identifier Bit| Frame Identifier Bit| Result
896    ///  --- | --- | --- | ---
897    ///  0| X| X| Accept
898    ///  1| 0| 0| Accept
899    ///  1| 0| 1| Reject
900    ///  1| 1| 0| Reject
901    ///  1| 1| 1| Accept
902    ///
903    /// For example, to receive standard frames with identifier 0x123 only, the mode
904    /// can be set to Match-Standard-Only with 0x7FF as mask and 0x123 as identifier.
905    /// The mask of 0x7FF selects all 11 identifier bits for matching so that the
906    /// identifier has to be exactly 0x123 to be accepted.
907    ///
908    /// To accept identifier 0x123 and identifier 0x456 at the same time, just set
909    /// filter 2 to 0x456 and keep mask and filter 1 unchanged.
910    ///
911    /// There can be up to 32 different read filters configured at the same time,
912    /// because there can be up to 32 read buffer (see [`set_queue_configuration`]).
913    ///
914    /// The default mode is accept-all for all read buffers.
915    ///
916    /// Associated constants:
917    /// * CAN_V2_BRICKLET_FILTER_MODE_ACCEPT_ALL
918    ///	* CAN_V2_BRICKLET_FILTER_MODE_MATCH_STANDARD_ONLY
919    ///	* CAN_V2_BRICKLET_FILTER_MODE_MATCH_EXTENDED_ONLY
920    ///	* CAN_V2_BRICKLET_FILTER_MODE_MATCH_STANDARD_AND_EXTENDED
921    pub async fn set_read_filter_configuration(
922        &mut self,
923        buffer_index: u8,
924        filter_mode: u8,
925        filter_mask: u32,
926        filter_identifier: u32,
927    ) -> Result<(), TinkerforgeError> {
928        let mut payload = [0; 10];
929        buffer_index.write_to_slice(&mut payload[0..1]);
930        filter_mode.write_to_slice(&mut payload[1..2]);
931        filter_mask.write_to_slice(&mut payload[2..6]);
932        filter_identifier.write_to_slice(&mut payload[6..10]);
933
934        #[allow(unused_variables)]
935        let result = self.device.set(u8::from(CanV2BrickletFunction::SetReadFilterConfiguration), &payload).await?;
936        Ok(())
937    }
938
939    /// Returns the read filter configuration as set by [`set_read_filter_configuration`].
940    ///
941    /// Associated constants:
942    /// * CAN_V2_BRICKLET_FILTER_MODE_ACCEPT_ALL
943    ///	* CAN_V2_BRICKLET_FILTER_MODE_MATCH_STANDARD_ONLY
944    ///	* CAN_V2_BRICKLET_FILTER_MODE_MATCH_EXTENDED_ONLY
945    ///	* CAN_V2_BRICKLET_FILTER_MODE_MATCH_STANDARD_AND_EXTENDED
946    pub async fn get_read_filter_configuration(&mut self, buffer_index: u8) -> Result<ReadFilterConfiguration, TinkerforgeError> {
947        let mut payload = [0; 1];
948        buffer_index.write_to_slice(&mut payload[0..1]);
949
950        #[allow(unused_variables)]
951        let result = self.device.get(u8::from(CanV2BrickletFunction::GetReadFilterConfiguration), &payload).await?;
952        Ok(ReadFilterConfiguration::from_le_byte_slice(result.body()))
953    }
954
955    /// Returns information about different kinds of errors.
956    ///
957    /// The write and read error levels indicate the current level of stuffing, form,
958    /// acknowledgement, bit and checksum errors during CAN bus write and read
959    /// operations. For each of this error kinds there is also an individual counter.
960    ///
961    /// When the write error level extends 255 then the CAN transceiver gets disabled
962    /// and no frames can be transmitted or received anymore. The CAN transceiver will
963    /// automatically be activated again after the CAN bus is idle for a while.
964    ///
965    /// The write buffer timeout, read buffer and backlog overflow counts represents the
966    /// number of these errors:
967    ///
968    /// * A write buffer timeout occurs if a frame could not be transmitted before the
969    ///   configured write buffer timeout expired (see [`set_queue_configuration`]).
970    /// * A read buffer overflow occurs if a read buffer of the CAN transceiver
971    ///   still contains the last received frame when the next frame arrives. In this
972    ///   case the last received frame is lost. This happens if the CAN transceiver
973    ///   receives more frames than the Bricklet can handle. Using the read filter
974    ///   (see [`set_read_filter_configuration`]) can help to reduce the amount of
975    ///   received frames. This count is not exact, but a lower bound, because the
976    ///   Bricklet might not able detect all overflows if they occur in rapid succession.
977    /// * A read backlog overflow occurs if the read backlog of the Bricklet is already
978    ///   full when the next frame should be read from a read buffer of the CAN
979    ///   transceiver. In this case the frame in the read buffer is lost. This
980    ///   happens if the CAN transceiver receives more frames to be added to the read
981    ///   backlog than are removed from the read backlog using the [`read_frame`]
982    ///   function. Using the [`get_frame_read_callback_receiver`] receiver ensures that the read backlog
983    ///   can not overflow.
984    ///
985    /// The read buffer overflow counter counts the overflows of all configured read
986    /// buffers. Which read buffer exactly suffered from an overflow can be figured
987    /// out from the read buffer overflow occurrence list
988    /// (``read_buffer_overflow_error_occurred``). Reading the error log clears the
989    /// occurence list.
990    ///
991    /// Associated constants:
992    /// * CAN_V2_BRICKLET_TRANSCEIVER_STATE_ACTIVE
993    ///	* CAN_V2_BRICKLET_TRANSCEIVER_STATE_PASSIVE
994    ///	* CAN_V2_BRICKLET_TRANSCEIVER_STATE_DISABLED
995    pub async fn get_error_log_low_level(&mut self) -> Result<ErrorLogLowLevel, TinkerforgeError> {
996        let payload = [0; 0];
997
998        #[allow(unused_variables)]
999        let result = self.device.get(u8::from(CanV2BrickletFunction::GetErrorLogLowLevel), &payload).await?;
1000        Ok(ErrorLogLowLevel::from_le_byte_slice(result.body()))
1001    }
1002
1003    /// Sets the communication LED configuration. By default the LED shows
1004    /// CAN-Bus traffic, it flickers once for every 40 transmitted or received frames.
1005    ///
1006    /// You can also turn the LED permanently on/off or show a heartbeat.
1007    ///
1008    /// If the Bricklet is in bootloader mode, the LED is off.
1009    ///
1010    /// Associated constants:
1011    /// * CAN_V2_BRICKLET_COMMUNICATION_LED_CONFIG_OFF
1012    ///	* CAN_V2_BRICKLET_COMMUNICATION_LED_CONFIG_ON
1013    ///	* CAN_V2_BRICKLET_COMMUNICATION_LED_CONFIG_SHOW_HEARTBEAT
1014    ///	* CAN_V2_BRICKLET_COMMUNICATION_LED_CONFIG_SHOW_COMMUNICATION
1015    pub async fn set_communication_led_config(&mut self, config: u8) -> Result<(), TinkerforgeError> {
1016        let mut payload = [0; 1];
1017        config.write_to_slice(&mut payload[0..1]);
1018
1019        #[allow(unused_variables)]
1020        let result = self.device.set(u8::from(CanV2BrickletFunction::SetCommunicationLedConfig), &payload).await?;
1021        Ok(())
1022    }
1023
1024    /// Returns the configuration as set by [`set_communication_led_config`]
1025    ///
1026    /// Associated constants:
1027    /// * CAN_V2_BRICKLET_COMMUNICATION_LED_CONFIG_OFF
1028    ///	* CAN_V2_BRICKLET_COMMUNICATION_LED_CONFIG_ON
1029    ///	* CAN_V2_BRICKLET_COMMUNICATION_LED_CONFIG_SHOW_HEARTBEAT
1030    ///	* CAN_V2_BRICKLET_COMMUNICATION_LED_CONFIG_SHOW_COMMUNICATION
1031    pub async fn get_communication_led_config(&mut self) -> Result<u8, TinkerforgeError> {
1032        let payload = [0; 0];
1033
1034        #[allow(unused_variables)]
1035        let result = self.device.get(u8::from(CanV2BrickletFunction::GetCommunicationLedConfig), &payload).await?;
1036        Ok(u8::from_le_byte_slice(result.body()))
1037    }
1038
1039    /// Sets the error LED configuration.
1040    ///
1041    /// By default (show-transceiver-state) the error LED turns on if the CAN
1042    /// transceiver is passive or disabled state (see [`get_error_log`]). If
1043    /// the CAN transceiver is in active state the LED turns off.
1044    ///
1045    /// If the LED is configured as show-error then the error LED turns on if any error
1046    /// occurs. If you call this function with the show-error option again, the LED will
1047    /// turn off until the next error occurs.
1048    ///
1049    /// You can also turn the LED permanently on/off or show a heartbeat.
1050    ///
1051    /// If the Bricklet is in bootloader mode, the LED is off.
1052    ///
1053    /// Associated constants:
1054    /// * CAN_V2_BRICKLET_ERROR_LED_CONFIG_OFF
1055    ///	* CAN_V2_BRICKLET_ERROR_LED_CONFIG_ON
1056    ///	* CAN_V2_BRICKLET_ERROR_LED_CONFIG_SHOW_HEARTBEAT
1057    ///	* CAN_V2_BRICKLET_ERROR_LED_CONFIG_SHOW_TRANSCEIVER_STATE
1058    ///	* CAN_V2_BRICKLET_ERROR_LED_CONFIG_SHOW_ERROR
1059    pub async fn set_error_led_config(&mut self, config: u8) -> Result<(), TinkerforgeError> {
1060        let mut payload = [0; 1];
1061        config.write_to_slice(&mut payload[0..1]);
1062
1063        #[allow(unused_variables)]
1064        let result = self.device.set(u8::from(CanV2BrickletFunction::SetErrorLedConfig), &payload).await?;
1065        Ok(())
1066    }
1067
1068    /// Returns the configuration as set by [`set_error_led_config`].
1069    ///
1070    /// Associated constants:
1071    /// * CAN_V2_BRICKLET_ERROR_LED_CONFIG_OFF
1072    ///	* CAN_V2_BRICKLET_ERROR_LED_CONFIG_ON
1073    ///	* CAN_V2_BRICKLET_ERROR_LED_CONFIG_SHOW_HEARTBEAT
1074    ///	* CAN_V2_BRICKLET_ERROR_LED_CONFIG_SHOW_TRANSCEIVER_STATE
1075    ///	* CAN_V2_BRICKLET_ERROR_LED_CONFIG_SHOW_ERROR
1076    pub async fn get_error_led_config(&mut self) -> Result<u8, TinkerforgeError> {
1077        let payload = [0; 0];
1078
1079        #[allow(unused_variables)]
1080        let result = self.device.get(u8::from(CanV2BrickletFunction::GetErrorLedConfig), &payload).await?;
1081        Ok(u8::from_le_byte_slice(result.body()))
1082    }
1083
1084    /// Enables and disables the [`get_frame_readable_callback_receiver`] receiver.
1085    ///
1086    /// By default the receiver is disabled. Enabling this receiver will disable the [`get_frame_read_callback_receiver`] receiver.
1087    ///
1088    ///
1089    /// .. versionadded:: 2.0.3$nbsp;(Plugin)
1090    pub async fn set_frame_readable_callback_configuration(&mut self, enabled: bool) -> Result<(), TinkerforgeError> {
1091        let mut payload = [0; 1];
1092        enabled.write_to_slice(&mut payload[0..1]);
1093
1094        #[allow(unused_variables)]
1095        let result = self.device.set(u8::from(CanV2BrickletFunction::SetFrameReadableCallbackConfiguration), &payload).await?;
1096        Ok(())
1097    }
1098
1099    /// Returns *true* if the [`get_frame_readable_callback_receiver`] receiver is enabled, *false* otherwise.
1100    ///
1101    ///
1102    /// .. versionadded:: 2.0.3$nbsp;(Plugin)
1103    pub async fn get_frame_readable_callback_configuration(&mut self) -> Result<bool, TinkerforgeError> {
1104        let payload = [0; 0];
1105
1106        #[allow(unused_variables)]
1107        let result = self.device.get(u8::from(CanV2BrickletFunction::GetFrameReadableCallbackConfiguration), &payload).await?;
1108        Ok(bool::from_le_byte_slice(result.body()))
1109    }
1110
1111    /// Enables and disables the [`get_error_occurred_callback_receiver`] receiver.
1112    ///
1113    /// By default the receiver is disabled.
1114    ///
1115    ///
1116    /// .. versionadded:: 2.0.3$nbsp;(Plugin)
1117    pub async fn set_error_occurred_callback_configuration(&mut self, enabled: bool) -> Result<(), TinkerforgeError> {
1118        let mut payload = [0; 1];
1119        enabled.write_to_slice(&mut payload[0..1]);
1120
1121        #[allow(unused_variables)]
1122        let result = self.device.set(u8::from(CanV2BrickletFunction::SetErrorOccurredCallbackConfiguration), &payload).await?;
1123        Ok(())
1124    }
1125
1126    /// Returns *true* if the [`get_error_occurred_callback_receiver`] receiver is enabled, *false* otherwise.
1127    ///
1128    ///
1129    /// .. versionadded:: 2.0.3$nbsp;(Plugin)
1130    pub async fn get_error_occurred_callback_configuration(&mut self) -> Result<bool, TinkerforgeError> {
1131        let payload = [0; 0];
1132
1133        #[allow(unused_variables)]
1134        let result = self.device.get(u8::from(CanV2BrickletFunction::GetErrorOccurredCallbackConfiguration), &payload).await?;
1135        Ok(bool::from_le_byte_slice(result.body()))
1136    }
1137
1138    /// Returns the error count for the communication between Brick and Bricklet.
1139    ///
1140    /// The errors are divided into
1141    ///
1142    /// * ACK checksum errors,
1143    /// * message checksum errors,
1144    /// * framing errors and
1145    /// * overflow errors.
1146    ///
1147    /// The errors counts are for errors that occur on the Bricklet side. All
1148    /// Bricks have a similar function that returns the errors on the Brick side.
1149    pub async fn get_spitfp_error_count(&mut self) -> Result<SpitfpErrorCount, TinkerforgeError> {
1150        let payload = [0; 0];
1151
1152        #[allow(unused_variables)]
1153        let result = self.device.get(u8::from(CanV2BrickletFunction::GetSpitfpErrorCount), &payload).await?;
1154        Ok(SpitfpErrorCount::from_le_byte_slice(result.body()))
1155    }
1156
1157    /// Sets the bootloader mode and returns the status after the requested
1158    /// mode change was instigated.
1159    ///
1160    /// You can change from bootloader mode to firmware mode and vice versa. A change
1161    /// from bootloader mode to firmware mode will only take place if the entry function,
1162    /// device identifier and CRC are present and correct.
1163    ///
1164    /// This function is used by Brick Viewer during flashing. It should not be
1165    /// necessary to call it in a normal user program.
1166    ///
1167    /// Associated constants:
1168    /// * CAN_V2_BRICKLET_BOOTLOADER_MODE_BOOTLOADER
1169    ///	* CAN_V2_BRICKLET_BOOTLOADER_MODE_FIRMWARE
1170    ///	* CAN_V2_BRICKLET_BOOTLOADER_MODE_BOOTLOADER_WAIT_FOR_REBOOT
1171    ///	* CAN_V2_BRICKLET_BOOTLOADER_MODE_FIRMWARE_WAIT_FOR_REBOOT
1172    ///	* CAN_V2_BRICKLET_BOOTLOADER_MODE_FIRMWARE_WAIT_FOR_ERASE_AND_REBOOT
1173    ///	* CAN_V2_BRICKLET_BOOTLOADER_STATUS_OK
1174    ///	* CAN_V2_BRICKLET_BOOTLOADER_STATUS_INVALID_MODE
1175    ///	* CAN_V2_BRICKLET_BOOTLOADER_STATUS_NO_CHANGE
1176    ///	* CAN_V2_BRICKLET_BOOTLOADER_STATUS_ENTRY_FUNCTION_NOT_PRESENT
1177    ///	* CAN_V2_BRICKLET_BOOTLOADER_STATUS_DEVICE_IDENTIFIER_INCORRECT
1178    ///	* CAN_V2_BRICKLET_BOOTLOADER_STATUS_CRC_MISMATCH
1179    pub async fn set_bootloader_mode(&mut self, mode: u8) -> Result<u8, TinkerforgeError> {
1180        let mut payload = [0; 1];
1181        mode.write_to_slice(&mut payload[0..1]);
1182
1183        #[allow(unused_variables)]
1184        let result = self.device.get(u8::from(CanV2BrickletFunction::SetBootloaderMode), &payload).await?;
1185        Ok(u8::from_le_byte_slice(result.body()))
1186    }
1187
1188    /// Returns the current bootloader mode, see [`set_bootloader_mode`].
1189    ///
1190    /// Associated constants:
1191    /// * CAN_V2_BRICKLET_BOOTLOADER_MODE_BOOTLOADER
1192    ///	* CAN_V2_BRICKLET_BOOTLOADER_MODE_FIRMWARE
1193    ///	* CAN_V2_BRICKLET_BOOTLOADER_MODE_BOOTLOADER_WAIT_FOR_REBOOT
1194    ///	* CAN_V2_BRICKLET_BOOTLOADER_MODE_FIRMWARE_WAIT_FOR_REBOOT
1195    ///	* CAN_V2_BRICKLET_BOOTLOADER_MODE_FIRMWARE_WAIT_FOR_ERASE_AND_REBOOT
1196    pub async fn get_bootloader_mode(&mut self) -> Result<u8, TinkerforgeError> {
1197        let payload = [0; 0];
1198
1199        #[allow(unused_variables)]
1200        let result = self.device.get(u8::from(CanV2BrickletFunction::GetBootloaderMode), &payload).await?;
1201        Ok(u8::from_le_byte_slice(result.body()))
1202    }
1203
1204    /// Sets the firmware pointer for [`write_firmware`]. The pointer has
1205    /// to be increased by chunks of size 64. The data is written to flash
1206    /// every 4 chunks (which equals to one page of size 256).
1207    ///
1208    /// This function is used by Brick Viewer during flashing. It should not be
1209    /// necessary to call it in a normal user program.
1210    pub async fn set_write_firmware_pointer(&mut self, pointer: u32) -> Result<(), TinkerforgeError> {
1211        let mut payload = [0; 4];
1212        pointer.write_to_slice(&mut payload[0..4]);
1213
1214        #[allow(unused_variables)]
1215        let result = self.device.set(u8::from(CanV2BrickletFunction::SetWriteFirmwarePointer), &payload).await?;
1216        Ok(())
1217    }
1218
1219    /// Writes 64 Bytes of firmware at the position as written by
1220    /// [`set_write_firmware_pointer`] before. The firmware is written
1221    /// to flash every 4 chunks.
1222    ///
1223    /// You can only write firmware in bootloader mode.
1224    ///
1225    /// This function is used by Brick Viewer during flashing. It should not be
1226    /// necessary to call it in a normal user program.
1227    pub async fn write_firmware(&mut self, data: &[u8; 64]) -> Result<u8, TinkerforgeError> {
1228        let mut payload = [0; 64];
1229        data.write_to_slice(&mut payload[0..64]);
1230
1231        #[allow(unused_variables)]
1232        let result = self.device.get(u8::from(CanV2BrickletFunction::WriteFirmware), &payload).await?;
1233        Ok(u8::from_le_byte_slice(result.body()))
1234    }
1235
1236    /// Sets the status LED configuration. By default the LED shows
1237    /// communication traffic between Brick and Bricklet, it flickers once
1238    /// for every 10 received data packets.
1239    ///
1240    /// You can also turn the LED permanently on/off or show a heartbeat.
1241    ///
1242    /// If the Bricklet is in bootloader mode, the LED is will show heartbeat by default.
1243    ///
1244    /// Associated constants:
1245    /// * CAN_V2_BRICKLET_STATUS_LED_CONFIG_OFF
1246    ///	* CAN_V2_BRICKLET_STATUS_LED_CONFIG_ON
1247    ///	* CAN_V2_BRICKLET_STATUS_LED_CONFIG_SHOW_HEARTBEAT
1248    ///	* CAN_V2_BRICKLET_STATUS_LED_CONFIG_SHOW_STATUS
1249    pub async fn set_status_led_config(&mut self, config: u8) -> Result<(), TinkerforgeError> {
1250        let mut payload = [0; 1];
1251        config.write_to_slice(&mut payload[0..1]);
1252
1253        #[allow(unused_variables)]
1254        let result = self.device.set(u8::from(CanV2BrickletFunction::SetStatusLedConfig), &payload).await?;
1255        Ok(())
1256    }
1257
1258    /// Returns the configuration as set by [`set_status_led_config`]
1259    ///
1260    /// Associated constants:
1261    /// * CAN_V2_BRICKLET_STATUS_LED_CONFIG_OFF
1262    ///	* CAN_V2_BRICKLET_STATUS_LED_CONFIG_ON
1263    ///	* CAN_V2_BRICKLET_STATUS_LED_CONFIG_SHOW_HEARTBEAT
1264    ///	* CAN_V2_BRICKLET_STATUS_LED_CONFIG_SHOW_STATUS
1265    pub async fn get_status_led_config(&mut self) -> Result<u8, TinkerforgeError> {
1266        let payload = [0; 0];
1267
1268        #[allow(unused_variables)]
1269        let result = self.device.get(u8::from(CanV2BrickletFunction::GetStatusLedConfig), &payload).await?;
1270        Ok(u8::from_le_byte_slice(result.body()))
1271    }
1272
1273    /// Returns the temperature as measured inside the microcontroller. The
1274    /// value returned is not the ambient temperature!
1275    ///
1276    /// The temperature is only proportional to the real temperature and it has bad
1277    /// accuracy. Practically it is only useful as an indicator for
1278    /// temperature changes.
1279    pub async fn get_chip_temperature(&mut self) -> Result<i16, TinkerforgeError> {
1280        let payload = [0; 0];
1281
1282        #[allow(unused_variables)]
1283        let result = self.device.get(u8::from(CanV2BrickletFunction::GetChipTemperature), &payload).await?;
1284        Ok(i16::from_le_byte_slice(result.body()))
1285    }
1286
1287    /// Calling this function will reset the Bricklet. All configurations
1288    /// will be lost.
1289    ///
1290    /// After a reset you have to create new device objects,
1291    /// calling functions on the existing ones will result in
1292    /// undefined behavior!
1293    pub async fn reset(&mut self) -> Result<(), TinkerforgeError> {
1294        let payload = [0; 0];
1295
1296        #[allow(unused_variables)]
1297        let result = self.device.set(u8::from(CanV2BrickletFunction::Reset), &payload).await?;
1298        Ok(())
1299    }
1300
1301    /// Writes a new UID into flash. If you want to set a new UID
1302    /// you have to decode the Base58 encoded UID string into an
1303    /// integer first.
1304    ///
1305    /// We recommend that you use Brick Viewer to change the UID.
1306    pub async fn write_uid(&mut self, uid: u32) -> Result<(), TinkerforgeError> {
1307        let mut payload = [0; 4];
1308        uid.write_to_slice(&mut payload[0..4]);
1309
1310        #[allow(unused_variables)]
1311        let result = self.device.set(u8::from(CanV2BrickletFunction::WriteUid), &payload).await?;
1312        Ok(())
1313    }
1314
1315    /// Returns the current UID as an integer. Encode as
1316    /// Base58 to get the usual string version.
1317    pub async fn read_uid(&mut self) -> Result<u32, TinkerforgeError> {
1318        let payload = [0; 0];
1319
1320        #[allow(unused_variables)]
1321        let result = self.device.get(u8::from(CanV2BrickletFunction::ReadUid), &payload).await?;
1322        Ok(u32::from_le_byte_slice(result.body()))
1323    }
1324
1325    /// Returns the UID, the UID where the Bricklet is connected to,
1326    /// the position, the hardware and firmware version as well as the
1327    /// device identifier.
1328    ///
1329    /// The position can be 'a', 'b', 'c', 'd', 'e', 'f', 'g' or 'h' (Bricklet Port).
1330    /// A Bricklet connected to an [Isolator Bricklet](isolator_bricklet) is always at
1331    /// position 'z'.
1332    ///
1333    /// The device identifier numbers can be found [here](device_identifier).
1334    /// |device_identifier_constant|
1335    pub async fn get_identity(&mut self) -> Result<Identity, TinkerforgeError> {
1336        let payload = [0; 0];
1337
1338        #[allow(unused_variables)]
1339        let result = self.device.get(u8::from(CanV2BrickletFunction::GetIdentity), &payload).await?;
1340        Ok(Identity::from_le_byte_slice(result.body()))
1341    }
1342}