use tr064::{Action, Client, Scpd, Service};
use crate::fault::Fault;
use crate::policy::{self, Class};
use crate::redact;
#[derive(Debug, thiserror::Error)]
pub enum Error {
#[error(transparent)]
Tr064(tr064::Error),
#[error("{fault}")]
Fault { fault: Fault, description: String },
#[error("{service} has no action {action}")]
UnknownAction { service: String, action: String },
#[error("{action} is {class}; only actions that read are called")]
Refused { action: String, class: Class },
#[error("{action} has no input {argument}")]
UnknownArgument { action: String, argument: String },
#[error("{action} needs the input {argument}")]
MissingArgument { action: String, argument: String },
#[error("{action} got the input {argument} more than once")]
DuplicateArgument { action: String, argument: String },
#[error("the {document} of the device is not valid: {reason}")]
InvalidDocument {
document: &'static str,
reason: String,
},
}
impl From<tr064::Error> for Error {
fn from(error: tr064::Error) -> Self {
match (Fault::of(&error), error) {
(Some(fault), tr064::Error::Upnp { description, .. }) => {
if let Some(wait) = fault.retry_after() {
let (status, retry_after_seconds) = (fault.status(), wait.as_secs());
tracing::debug!(status, retry_after_seconds, "the device asks to wait");
}
Self::Fault { fault, description }
}
(_, error) => Self::Tr064(error),
}
}
}
pub async fn service(client: &Client, reference: &str) -> Result<(Service, Scpd), Error> {
let description = client.description().await?;
let service = description.service(reference)?.clone();
let scpd = client.scpd(&service).await?;
Ok((service, scpd))
}
pub fn action<'a>(service: &Service, scpd: &'a Scpd, name: &str) -> Result<&'a Action, Error> {
scpd.action(name).ok_or_else(|| Error::UnknownAction {
service: service.service_id.clone(),
action: name.to_owned(),
})
}
pub fn check(
service: &Service,
action: &Action,
arguments: &[(String, String)],
) -> Result<(), Error> {
let class = policy::classify(service, action);
if !class.is_read() {
return Err(Error::Refused {
action: action.name.clone(),
class,
});
}
check_arguments(action, arguments)
}
pub fn check_arguments(action: &Action, arguments: &[(String, String)]) -> Result<(), Error> {
if let Some((name, _)) = arguments
.iter()
.find(|(name, _)| !action.inputs().any(|a| &a.name == name))
{
return Err(Error::UnknownArgument {
action: action.name.clone(),
argument: name.clone(),
});
}
if let Some(input) = action
.inputs()
.find(|a| !arguments.iter().any(|(name, _)| *name == a.name))
{
return Err(Error::MissingArgument {
action: action.name.clone(),
argument: input.name.clone(),
});
}
if let Some((_, (name, _))) = arguments
.iter()
.enumerate()
.find(|(i, (name, _))| arguments[..*i].iter().any(|(n, _)| n == name))
{
return Err(Error::DuplicateArgument {
action: action.name.clone(),
argument: name.clone(),
});
}
Ok(())
}
pub async fn call(
client: &Client,
service: &Service,
action: &Action,
arguments: &[(String, String)],
) -> Result<Vec<(String, String)>, Error> {
let out = send(client, service, action, arguments).await?;
Ok(out
.into_iter()
.map(|(name, value)| {
let value = redact::output(service, &name, &value);
(name, value)
})
.collect())
}
pub(crate) async fn send(
client: &Client,
service: &Service,
action: &Action,
arguments: &[(String, String)],
) -> Result<Vec<(String, String)>, Error> {
check(service, action, arguments)?;
let arguments: Vec<(&str, &str)> = arguments
.iter()
.map(|(n, v)| (n.as_str(), v.as_str()))
.collect();
Ok(client
.call(service, &action.name, &arguments)
.await?
.into_iter()
.collect())
}