From aa262879f0495a1f5ce6d2388e5e95b7359b2354 Mon Sep 17 00:00:00 2001 From: Pierre HUBERT Date: Sat, 5 Oct 2024 20:00:57 +0200 Subject: [PATCH] Add route to download firmware on device --- central_backend/src/ota/ota_manager.rs | 6 ++++++ .../src/server/devices_api/devices_ota.rs | 21 +++++++++++++++++++ central_backend/src/server/devices_api/mod.rs | 1 + central_backend/src/server/servers.rs | 8 ++++++- 4 files changed, 35 insertions(+), 1 deletion(-) create mode 100644 central_backend/src/server/devices_api/devices_ota.rs diff --git a/central_backend/src/ota/ota_manager.rs b/central_backend/src/ota/ota_manager.rs index b1d56b0..ff38728 100644 --- a/central_backend/src/ota/ota_manager.rs +++ b/central_backend/src/ota/ota_manager.rs @@ -25,6 +25,12 @@ pub fn save_update( Ok(()) } +/// Get the content of an OTA update +pub fn get_ota_update(platform: OTAPlatform, version: &semver::Version) -> anyhow::Result> { + let path = AppConfig::get().path_ota_update(platform, version); + Ok(std::fs::read(path)?) +} + /// Get the list of OTA software updates for a platform pub fn get_ota_updates_for_platform(platform: OTAPlatform) -> anyhow::Result> { let ota_path = AppConfig::get().ota_platform_dir(platform); diff --git a/central_backend/src/server/devices_api/devices_ota.rs b/central_backend/src/server/devices_api/devices_ota.rs new file mode 100644 index 0000000..68ca525 --- /dev/null +++ b/central_backend/src/server/devices_api/devices_ota.rs @@ -0,0 +1,21 @@ +use crate::ota::ota_manager; +use crate::ota::ota_update::OTAPlatform; +use crate::server::custom_error::HttpResult; +use actix_web::{web, HttpResponse}; + +#[derive(serde::Deserialize)] +pub struct FirmwarePath { + platform: OTAPlatform, + version: semver::Version, +} + +/// Download firmware update +pub async fn retrieve_firmware(path: web::Path) -> HttpResult { + if !ota_manager::update_exists(path.platform, &path.version)? { + return Ok(HttpResponse::NotFound().json("The requested firmware was not found!")); + } + + let firmware = ota_manager::get_ota_update(path.platform, &path.version)?; + + Ok(HttpResponse::Ok().body(firmware)) +} diff --git a/central_backend/src/server/devices_api/mod.rs b/central_backend/src/server/devices_api/mod.rs index 58dcde0..5a43950 100644 --- a/central_backend/src/server/devices_api/mod.rs +++ b/central_backend/src/server/devices_api/mod.rs @@ -1,4 +1,5 @@ pub mod device_logging_controller; +pub mod devices_ota; pub mod jwt_parser; pub mod mgmt_controller; pub mod utils_controller; diff --git a/central_backend/src/server/servers.rs b/central_backend/src/server/servers.rs index 9c76ad7..a2d6c66 100644 --- a/central_backend/src/server/servers.rs +++ b/central_backend/src/server/servers.rs @@ -3,7 +3,9 @@ use crate::constants; use crate::crypto::pki; use crate::energy::energy_actor::EnergyActorAddr; use crate::server::auth_middleware::AuthChecker; -use crate::server::devices_api::{device_logging_controller, mgmt_controller, utils_controller}; +use crate::server::devices_api::{ + device_logging_controller, devices_ota, mgmt_controller, utils_controller, +}; use crate::server::unsecure_server::*; use crate::server::web_api::*; use crate::server::web_app_controller; @@ -251,6 +253,10 @@ pub async fn secure_server(energy_actor: EnergyActorAddr) -> anyhow::Result<()> "/devices_api/mgmt/sync", web::post().to(mgmt_controller::sync_device), ) + .route( + "/devices_api/ota/{platform}/{version}", + web::get().to(devices_ota::retrieve_firmware), + ) .route( "/devices_api/logging/record", web::post().to(device_logging_controller::report_log),