Skip to main content

edgehog_device_runtime/ota/
config.rs

1// This file is part of Edgehog.
2//
3// Copyright 2025 SECO Mind Srl
4//
5// Licensed under the Apache License, Version 2.0 (the "License");
6// you may not use this file except in compliance with the License.
7// You may obtain a copy of the License at
8//
9//    http://www.apache.org/licenses/LICENSE-2.0
10//
11// Unless required by applicable law or agreed to in writing, software
12// distributed under the License is distributed on an "AS IS" BASIS,
13// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14// See the License for the specific language governing permissions and
15// limitations under the License.
16//
17// SPDX-License-Identifier: Apache-2.0
18
19use std::fmt::Display;
20
21use serde::Deserialize;
22
23#[derive(Debug, Clone, Copy, Default, Deserialize, PartialEq, Eq)]
24pub struct OtaConfig {
25    #[serde(default)]
26    pub reboot: Reboot,
27    #[serde(default)]
28    pub streaming: bool,
29    /// RAUC configuration for the OTA
30    #[serde(default)]
31    pub rauc: RaucConfig,
32}
33
34#[derive(Debug, Clone, Copy, Default, Deserialize, PartialEq, Eq)]
35#[serde(rename_all = "lowercase")]
36pub enum Reboot {
37    #[default]
38    Default,
39    External,
40}
41
42/// Configuration for RAUC
43#[derive(Debug, Clone, Copy, Default, Deserialize, PartialEq, Eq)]
44pub struct RaucConfig {
45    /// DBUS socket to connect to
46    #[serde(default)]
47    pub dbus_socket: RaucDbus,
48}
49
50/// DBUS socket to communicate with the RAUC service
51#[derive(Debug, Clone, Copy, Default, Deserialize, PartialEq, Eq)]
52#[serde(rename_all = "lowercase")]
53pub enum RaucDbus {
54    /// Uses the system bus
55    #[default]
56    System,
57    /// Uses the current user session bus
58    Session,
59}
60
61impl Display for RaucDbus {
62    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
63        match self {
64            RaucDbus::System => write!(f, "system"),
65            RaucDbus::Session => write!(f, "session"),
66        }
67    }
68}
69
70#[cfg(test)]
71mod tests {
72    use pretty_assertions::assert_eq;
73
74    use super::*;
75
76    #[test]
77    fn should_deserialize() {
78        let string = r#"
79        reboot = "external"
80        streaming = true
81        "#;
82
83        let config: OtaConfig = toml::from_str(string).unwrap();
84
85        let exp = OtaConfig {
86            reboot: Reboot::External,
87            streaming: true,
88            rauc: RaucConfig {
89                dbus_socket: RaucDbus::System,
90            },
91        };
92
93        assert_eq!(config, exp);
94    }
95
96    #[test]
97    fn should_deserialize_default() {
98        let string = r#"
99        "#;
100
101        let config: OtaConfig = toml::from_str(string).unwrap();
102
103        let exp = OtaConfig {
104            reboot: Reboot::Default,
105            streaming: false,
106            rauc: RaucConfig {
107                dbus_socket: RaucDbus::System,
108            },
109        };
110
111        assert_eq!(config, exp);
112    }
113
114    #[test]
115    fn should_deserialize_rauc() {
116        let string = r#"
117        [rauc]
118        dbus_socket = "session"
119        "#;
120
121        let config: OtaConfig = toml::from_str(string).unwrap();
122
123        let exp = OtaConfig {
124            reboot: Reboot::Default,
125            streaming: false,
126            rauc: RaucConfig {
127                dbus_socket: RaucDbus::Session,
128            },
129        };
130
131        assert_eq!(config, exp);
132    }
133}