Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
111 changes: 4 additions & 107 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 2 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ license = "MIT OR Apache-2.0"
name = "omnect-device-service"
readme = "README.md"
repository = "https://github.com/omnect/omnect-device-service.git"
version = "0.41.8"
version = "0.41.10"

[dependencies]
actix-server = { version = "2.6", default-features = false }
Expand All @@ -25,12 +25,11 @@ freedesktop_entry_parser = { version = "1.3", default-features = false }
futures = { version = "0.3", default-features = false }
futures-util = { version = "0.3", default-features = false }
glob = { version = "0.3", default-features = false }
inotify = { version = "0.11", default-features = false, features = ["stream"] }
lazy_static = { version = "1.5", default-features = false }
log = { version = "0.4", default-features = false }
log-panics = { version = "2", default-features = false }
modemmanager = { git = "https://github.com/omnect/modemmanager.git", tag = "0.3.4", default-features = false, optional = true }
notify = { version = "8.0", default-features = false }
notify-debouncer-full = { version = "0.5", default-features = false }
regex-lite = { version = "0.1", default-features = true }
reqwest = { version = "0.12", default-features = false, features = [
"default-tls",
Expand Down
46 changes: 28 additions & 18 deletions src/twin/consent.rs
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
use crate::{
common::{from_json_file, to_json_file},
twin::{Feature, feature::*},
twin::feature::{self, *},
};
use anyhow::{Context, Result, bail, ensure};
use azure_iot_sdk::client::IotMessage;
use inotify::WatchMask;
use log::{info, warn};
use notify_debouncer_full::{Debouncer, NoCache, notify::*};
use serde::{Deserialize, Serialize};
use serde_json::json;
use std::{collections::HashMap, env, path::Path};
use std::{collections::HashMap, env, path::PathBuf};
use tokio::sync::mpsc::Sender;

macro_rules! consent_path {
() => {
Path::new(&env::var("CONSENT_DIR_PATH").unwrap_or("/etc/omnect/consent".to_string()))
PathBuf::from(&env::var("CONSENT_DIR_PATH").unwrap_or("/etc/omnect/consent".to_string()))
};
}

Expand Down Expand Up @@ -53,9 +53,8 @@ pub struct ConsentConfig {
reset_consent_on_fail: bool,
}

#[derive(Default)]
pub struct DeviceUpdateConsent {
file_observer: Option<Debouncer<INotifyWatcher, NoCache>>,
file_map: HashMap<std::ffi::c_int, PathBuf>,
tx_reported_properties: Option<Sender<serde_json::Value>>,
}

Expand Down Expand Up @@ -87,20 +86,15 @@ impl Feature for DeviceUpdateConsent {
.await
}

fn command_request_stream(&mut self) -> CommandRequestStreamResult {
let (file_observer, stream) = file_modified_stream::<DeviceUpdateConsent>(vec![
request_consent_path!().as_path(),
history_consent_path!().as_path(),
])
.context("command_request_stream: cannot create file_modified_stream")?;
self.file_observer = Some(file_observer);
Ok(Some(stream))
}

async fn command(&mut self, cmd: &Command) -> CommandResult {
match cmd {
Command::FileModified(file) => {
self.report_consent(from_json_file(&file.path)?).await?;
Command::WatchPath(cmd) => {
self.report_consent(from_json_file(
self.file_map
.get(&cmd.event.wd.get_watch_descriptor_id())
.context("cannot find file for descriptor")?,
)?)
.await?;
}
Command::DesiredGeneralConsent(cmd) => {
self.update_general_consent(cmd).await?;
Expand All @@ -119,6 +113,22 @@ impl DeviceUpdateConsent {
const USER_CONSENT_VERSION: u8 = 1;
const ID: &'static str = "device_update_consent";

pub fn new() -> Result<Self> {
let mut file_map = HashMap::new();

for path in [request_consent_path!(), history_consent_path!()] {
file_map.insert(
feature::add_watch::<Self>(&path, WatchMask::MODIFY)?.get_watch_descriptor_id(),
path,
);
}

Ok(DeviceUpdateConsent {
tx_reported_properties: None,
file_map,
})
}

fn user_consent(&self, cmd: &UserConsentCommand) -> CommandResult {
info!("user consent requested: {cmd:?}");

Expand Down
27 changes: 12 additions & 15 deletions src/twin/factory_reset.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@ use crate::{
bootloader_env,
common::from_json_file,
systemd,
twin::{Feature, feature::*},
twin::feature::{self, *},
web_service,
};
use anyhow::{Context, Result, bail};
use azure_iot_sdk::client::IotMessage;
use inotify::WatchMask;
use log::{debug, info, warn};
use notify_debouncer_full::{Debouncer, NoCache, notify::*};
use serde::{Deserialize, Serialize};
use serde_json::{from_reader, json};
use serde_repr::*;
Expand Down Expand Up @@ -37,8 +37,10 @@ macro_rules! config_path {

macro_rules! custom_config_dir_path {
() => {
env::var("FACTORY_RESET_CUSTOM_CONFIG_DIR_PATH")
.unwrap_or("/etc/omnect/factory-reset.d".to_string())
Path::new(
&env::var("FACTORY_RESET_CUSTOM_CONFIG_DIR_PATH")
.unwrap_or("/etc/omnect/factory-reset.d".to_string()),
)
};
}

Expand Down Expand Up @@ -92,7 +94,6 @@ struct FactoryResetReport {
pub struct FactoryReset {
tx_reported_properties: Option<Sender<serde_json::Value>>,
report: FactoryResetReport,
dir_observer: Option<Debouncer<INotifyWatcher, NoCache>>,
}

impl Feature for FactoryReset {
Expand Down Expand Up @@ -136,17 +137,9 @@ impl Feature for FactoryReset {
Ok(())
}

fn command_request_stream(&mut self) -> CommandRequestStreamResult {
let (dir_observer, stream) =
dir_modified_stream::<FactoryReset>(vec![&Path::new(&custom_config_dir_path!())])
.context("command_request_stream: cannot create dir_modified_stream")?;
self.dir_observer = Some(dir_observer);
Ok(Some(stream))
}

async fn command(&mut self, cmd: &Command) -> CommandResult {
match cmd {
Command::DirModified(_) => {
Command::WatchPath(_) => {
let keys = FactoryReset::factory_reset_keys()?;

if keys != self.report.keys {
Expand Down Expand Up @@ -193,10 +186,14 @@ impl FactoryReset {
result: FactoryReset::factory_reset_result()?,
};

feature::add_watch::<Self>(
custom_config_dir_path!(),
WatchMask::CREATE | WatchMask::DELETE,
)?;

Ok(FactoryReset {
tx_reported_properties: None,
report,
dir_observer: None,
})
}

Expand Down
Loading
Loading