sma_proto/client/error.rs
1/******************************************************************************\
2 sma-proto - A SMA Speedwire protocol library
3 Copyright (C) 2024 Max Maisel
4
5 This program is free software: you can redistribute it and/or modify
6 it under the terms of the GNU Affero General Public License as published by
7 the Free Software Foundation, either version 3 of the License, or
8 (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU Affero General Public License for more details.
14
15 You should have received a copy of the GNU Affero General Public License
16 along with this program. If not, see <https://www.gnu.org/licenses/>.
17\******************************************************************************/
18
19use crate::inverter::{InvalidPasswordError, SmaInvCounter};
20
21/// Errors returned from SMA speedwire client.
22#[derive(Clone, Debug)]
23pub enum ClientError {
24 /// A SMA speedwire protocol error.
25 ProtocolError(crate::Error),
26 /// An operating system IO error.
27 IoError(std::io::ErrorKind),
28 /// An operating system clock error.
29 TimeError(std::time::SystemTimeError),
30 /// The SMA device returned an error.
31 DeviceError(u16),
32 /// An additional start of fragment packet was received.
33 ExtraSofPacket(SmaInvCounter),
34 /// Login was rejected by the device.
35 LoginFailed,
36 /// Invalid input password error.
37 InvalidPasswordError(InvalidPasswordError),
38}
39
40impl From<std::io::Error> for ClientError {
41 fn from(e: std::io::Error) -> Self {
42 Self::IoError(e.kind())
43 }
44}
45
46impl From<std::time::SystemTimeError> for ClientError {
47 fn from(e: std::time::SystemTimeError) -> Self {
48 Self::TimeError(e)
49 }
50}
51
52impl From<crate::Error> for ClientError {
53 fn from(e: crate::Error) -> Self {
54 Self::ProtocolError(e)
55 }
56}
57
58impl From<InvalidPasswordError> for ClientError {
59 fn from(e: InvalidPasswordError) -> Self {
60 Self::InvalidPasswordError(e)
61 }
62}
63
64impl core::fmt::Display for ClientError {
65 fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result {
66 match self {
67 Self::IoError(e) => {
68 write!(f, "{e}")
69 }
70 Self::TimeError(e) => {
71 write!(f, "{e}")
72 }
73 Self::ProtocolError(e) => {
74 write!(f, "{e}")
75 }
76 Self::DeviceError(ec) => {
77 write!(f, "The SMA device returned error code {ec:X}")
78 }
79 Self::ExtraSofPacket(counter) => {
80 write!(
81 f,
82 "Received additional start fragment {}:{}",
83 counter.packet_id, counter.fragment_id
84 )
85 }
86 Self::LoginFailed => {
87 write!(f, "The supplied password was rejected")
88 }
89 Self::InvalidPasswordError(e) => {
90 write!(f, "{e}")
91 }
92 }
93 }
94}