pyrinas_shared/
ota.rs

1pub mod v1;
2pub mod v2;
3
4use std::{convert::TryFrom, fmt, str};
5
6use serde::{Deserialize, Serialize};
7use serde_repr::*;
8
9/// Determine which OTAPackage being used
10#[derive(Debug, Serialize_repr, Deserialize_repr, Clone, Eq, PartialEq)]
11#[repr(u8)]
12pub enum OtaVersion {
13    V1 = 1,
14    V2 = 2,
15}
16
17impl TryFrom<u8> for OtaVersion {
18    type Error = ();
19
20    fn try_from(v: u8) -> Result<Self, Self::Error> {
21        match v {
22            x if x == OtaVersion::V1 as u8 => Ok(OtaVersion::V1),
23            x if x == OtaVersion::V2 as u8 => Ok(OtaVersion::V2),
24            _ => Err(()),
25        }
26    }
27}
28
29/// Struct that gets serialized for OTA support
30#[derive(Debug, Serialize, Deserialize, Clone, Eq, PartialEq)]
31pub struct OTAPackageVersion {
32    pub major: u8,
33    pub minor: u8,
34    pub patch: u8,
35    pub commit: u8,
36    pub hash: [u8; 8],
37}
38
39/// Implents display for package version for easy to_string() calls
40impl fmt::Display for OTAPackageVersion {
41    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
42        let hash = match str::from_utf8(&self.hash) {
43            Ok(h) => h,
44            Err(_e) => "unknown",
45        };
46
47        write!(
48            f,
49            "{}.{}.{}-{}-{}",
50            self.major, self.minor, self.patch, self.commit, hash
51        )
52    }
53}
54
55/// Used for passing different verioned ota data
56#[derive(Debug, Serialize, Deserialize, Clone)]
57pub struct OtaUpdateVersioned {
58    pub v1: Option<v1::OtaUpdate>,
59    pub v2: Option<v2::OtaUpdate>,
60}