use core::num::NonZeroU16;
pub const SERVICE_TYPE: &str = "_smartenergy._tcp.local.";
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub struct Endpoint<'a> {
pub https_port: NonZeroU16,
pub capability_path: &'a str,
}
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum DiscoveryError {
InvalidVersion,
InvalidLevel,
MissingHttps,
InvalidPort,
InvalidPath,
}
impl<'a> Endpoint<'a> {
pub fn from_txt(https: Option<&str>, dcap: &'a str) -> Result<Self, DiscoveryError> {
let value = https.ok_or(DiscoveryError::MissingHttps)?;
let port = if value.is_empty() {
443
} else {
if !value.bytes().all(|b| b.is_ascii_digit()) {
return Err(DiscoveryError::InvalidPort);
}
value
.parse::<u16>()
.map_err(|_| DiscoveryError::InvalidPort)?
};
let https_port = NonZeroU16::new(port).ok_or(DiscoveryError::InvalidPort)?;
if !dcap.starts_with('/')
|| dcap.starts_with("//")
|| dcap.len() > 250
|| fluent_uri::UriRef::parse(dcap).is_err()
|| dcap
.bytes()
.any(|b| !b.is_ascii_graphic() || matches!(b, b'#' | b'\\'))
{
return Err(DiscoveryError::InvalidPath);
}
Ok(Self {
https_port,
capability_path: dcap,
})
}
}
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum Service {
Capabilities,
Billing,
DerPrograms,
DemandResponse,
EndDevices,
Device(crate::security::Sfdi),
Files,
Messaging,
MirrorUsagePoints,
Prepayment,
Responses,
SelfDevice,
Time,
Tariffs,
UsagePoints,
}
impl Service {
pub fn local_name(self) -> alloc::string::String {
let subtype = match self {
Self::Capabilities => return SERVICE_TYPE.into(),
Self::Billing => "bill",
Self::DerPrograms => "derp",
Self::DemandResponse => "dr",
Self::EndDevices => "edev",
Self::Device(id) => return alloc::format!("edev-{id}._sub.{SERVICE_TYPE}"),
Self::Files => "file",
Self::Messaging => "msg",
Self::MirrorUsagePoints => "mup",
Self::Prepayment => "ppy",
Self::Responses => "rsps",
Self::SelfDevice => "sdev",
Self::Time => "tm",
Self::Tariffs => "tp",
Self::UsagePoints => "upt",
};
alloc::format!("{subtype}._sub.{SERVICE_TYPE}")
}
}
pub fn from_record<'a>(
version: Option<&str>,
level: Option<&str>,
https: Option<&str>,
capability: Option<&'a str>,
function_path: Option<&str>,
service: Service,
) -> Result<Endpoint<'a>, DiscoveryError> {
if version != Some("1") {
return Err(DiscoveryError::InvalidVersion);
}
if !level
.and_then(|s| s.strip_prefix("+S").or_else(|| s.strip_prefix("-S")))
.is_some_and(|s| !s.is_empty() && s.bytes().all(|c| c.is_ascii_digit()))
{
return Err(DiscoveryError::InvalidLevel);
}
if service != Service::Capabilities {
Endpoint::from_txt(https, function_path.ok_or(DiscoveryError::InvalidPath)?)?;
}
Endpoint::from_txt(https, capability.ok_or(DiscoveryError::InvalidPath)?)
}