aquarium_control/sensors/
i2c_error.rs

1/* Copyright 2025 Uwe Martin
2
3Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
4
5The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
6
7THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
8*/
9use thiserror::Error;
10
11/// Contains error definition occurring in communication via I2C
12#[derive(Error, Debug)]
13pub enum I2cError {
14    /// Initialization of the I2C interface failed.
15    #[cfg(all(target_os = "linux", feature = "target_hw"))]
16    #[error("[{location}] Initialization of I2C interface failed.")]
17    InitializationFailure {
18        location: String,
19
20        #[source]
21        source: rppal::i2c::Error,
22    },
23
24    /// Initialization of the I2C interface failed.
25    #[cfg(all(target_os = "linux", feature = "target_hw"))]
26    #[error("[{location}] Setting timeout for transactions failed.")]
27    SetTimeoutError {
28        location: String,
29
30        #[source]
31        source: rppal::i2c::Error,
32    },
33
34    /// The message could not be sent.
35    #[cfg(all(target_os = "linux", feature = "target_hw"))]
36    #[error("[{location}] I2C write operation failed.")]
37    WriteFailure {
38        location: String,
39
40        #[source]
41        source: rppal::i2c::Error,
42    },
43
44    /// I2C write operation failed.
45    #[cfg(all(test, not(any(target_os = "linux", feature = "target_hw"))))]
46    #[error("[{0}] I2C write operation failed.")]
47    WriteFailure(String),
48
49    /// I2C write operation failed.
50    #[cfg(all(test, not(feature = "target_hw"), target_os = "linux"))]
51    #[error("[{0}] I2C write operation failed.")]
52    WriteFailure(String),
53
54    /// Command could not be sent completely to the sensor unit.
55    #[cfg(all(target_os = "linux", feature = "target_hw"))]
56    #[error("[{0}] I2C write operation was incomplete: Target={1}, Realized={2}")]
57    InsufficientWrite(String, usize, usize),
58
59    /// Response from the sensor unit could not be received completely.
60    #[cfg(all(target_os = "linux", feature = "target_hw"))]
61    #[error("[{location}] I2C read operation was incomplete")]
62    InsufficientRead {
63        location: String,
64
65        #[source]
66        source: rppal::i2c::Error,
67    },
68
69    /// Represents a failure to set the address of the I2C sensor.
70    #[cfg(all(target_os = "linux", feature = "target_hw"))]
71    #[error("[{location}] Failed to set I2C slave address to {address}")]
72    SetAddressError {
73        location: String,
74        address: u16,
75
76        #[source]
77        source: rppal::i2c::Error,
78    },
79
80    /// Response length from bus participant does not match the expected value.
81    #[cfg(all(target_os = "linux", feature = "target_hw"))]
82    #[error("[{0}] I2C response has incorrect length: Expected: {1} Received: {2}")]
83    IncorrectResponseLength(String, usize, usize),
84
85    /// Command is too long for buffer
86    #[allow(unused)] // used in conditionally compiled code
87    #[error("[{location}]: I2C command (length: {actual_len}) exceeds the maximum allowed length ({max_len}).")]
88    CommandTooLong {
89        location: String,
90        max_len: usize,
91        actual_len: usize,
92    },
93
94    /// Provided buffer is too small for the expected response.
95    #[cfg(all(target_os = "linux", feature = "target_hw"))]
96    #[error("[{location}]: Provided buffer (size: {buffer_size}) is too small for the expected response (size: {expected_size}).")]
97    ProvidedBufferTooSmall {
98        location: String,
99        buffer_size: usize,
100        expected_size: usize,
101    },
102}