1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
use crate::application_protocol::primitives::data_value::{Enumerated, Time};

use super::{
    error::{Error, Unimplemented},
    helper::decode_unsigned,
    io::{Reader, Writer},
    spec::Binary,
    tag::{ApplicationTagNumber, Tag, TagNumber},
};

// A simplified version of the ApplicationDataValue struct to avoid a recursive structure
#[derive(Debug, Clone)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub enum SimpleApplicationDataValue {
    Boolean(bool),
    SignedInt(i32),
    UnsignedInt(u32),
    Real(f32),
    Double(f64),
    Enumerated(Enumerated),
}

impl SimpleApplicationDataValue {
    pub fn tag(&self) -> Tag {
        match self {
            Self::Boolean(_) => Tag::new(TagNumber::Application(ApplicationTagNumber::Boolean), 1),
            Self::SignedInt(_) => {
                Tag::new(TagNumber::Application(ApplicationTagNumber::SignedInt), 4)
            }
            Self::UnsignedInt(_) => {
                Tag::new(TagNumber::Application(ApplicationTagNumber::UnsignedInt), 4)
            }
            Self::Real(_) => Tag::new(TagNumber::Application(ApplicationTagNumber::Real), 4),
            Self::Double(_) => Tag::new(TagNumber::Application(ApplicationTagNumber::Double), 8),
            Self::Enumerated(_) => {
                Tag::new(TagNumber::Application(ApplicationTagNumber::Enumerated), 1)
            }
        }
    }
    pub fn decode(tag: &Tag, reader: &mut Reader, buf: &[u8]) -> Result<Self, Error> {
        let tag_num = match &tag.number {
            TagNumber::Application(x) => x,
            unknown => {
                return Err(Error::TagNotSupported((
                    "SimpleApplicationDataValue decode",
                    unknown.clone(),
                )));
            }
        };

        match tag_num {
            ApplicationTagNumber::Boolean => {
                let value = tag.value > 0;
                Ok(SimpleApplicationDataValue::Boolean(value))
            }
            ApplicationTagNumber::UnsignedInt => {
                let value = decode_unsigned(tag.value, reader, buf)? as u32;
                Ok(SimpleApplicationDataValue::UnsignedInt(value))
            }
            ApplicationTagNumber::Real => {
                if tag.value != 4 {
                    return Err(Error::InvalidValue(
                        "real value tag should have length of 4",
                    ));
                }
                Ok(SimpleApplicationDataValue::Real(f32::from_be_bytes(
                    reader.read_bytes(buf)?,
                )))
            }
            ApplicationTagNumber::Enumerated => {
                let value = decode_unsigned(tag.value, reader, buf)? as u32;
                let value = if value > 0 { Binary::On } else { Binary::Off };
                let value = Enumerated::Binary(value);
                Ok(SimpleApplicationDataValue::Enumerated(value))
            }

            x => Err(Error::Unimplemented(Unimplemented::ApplicationTagNumber(
                x.clone(),
            ))),
        }
    }

    pub fn encode(&self, writer: &mut Writer) {
        match self {
            Self::Boolean(x) => writer.push(*x as u8),
            Self::SignedInt(x) => writer.extend_from_slice(&x.to_be_bytes()),
            Self::UnsignedInt(x) => writer.extend_from_slice(&x.to_be_bytes()),
            Self::Real(x) => writer.extend_from_slice(&x.to_be_bytes()),
            Self::Double(x) => writer.extend_from_slice(&x.to_be_bytes()),
            Self::Enumerated(Enumerated::Binary(x)) => writer.push(x.clone() as u32 as u8),
            x => unimplemented!("{:?}", x),
        }
    }
}

#[derive(Debug, Clone)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct TimeValue {
    pub time: Time,
    pub value: SimpleApplicationDataValue,
}

impl TimeValue {
    pub const LEN: u32 = 4;

    pub fn decode(tag: &Tag, reader: &mut Reader, buf: &[u8]) -> Result<TimeValue, Error> {
        // 4 bytes
        if tag.value != Self::LEN {
            return Err(Error::Length((
                "time tag should have length value 4",
                tag.value,
            )));
        }
        let time = match &tag.number {
            TagNumber::Application(ApplicationTagNumber::Time) => Time::decode(reader, buf)?,
            number => {
                return Err(Error::TagNotSupported((
                    "TimeValue decode time application tag expected",
                    number.clone(),
                )))
            }
        };
        let tag = Tag::decode(reader, buf)?;
        let value = SimpleApplicationDataValue::decode(&tag, reader, buf)?;
        Ok(TimeValue { time, value })
    }

    pub fn encode(&self, writer: &mut Writer) {
        let tag = Tag::new(
            TagNumber::Application(ApplicationTagNumber::Time),
            Self::LEN,
        );
        tag.encode(writer);
        self.time.encode(writer);
        let tag = self.value.tag();
        tag.encode(writer);
        self.value.encode(writer);
    }
}