1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
use crate::Error;
use std::str::FromStr;

#[derive(PartialEq, Debug)]
pub struct Ext;

impl ToString for Ext {
    fn to_string(&self) -> String {
        String::new()
    }
}

impl FromStr for Ext {
    type Err = Error;
    fn from_str(s: &str) -> Result<Self, Self::Err> {
        match s {
            "" => Ok(Self {}),
            _ => Err(Error::IncorrectHeader("ext")),
        }
    }
}

#[derive(PartialEq, Debug, Default)]
pub struct ManDiscover;

impl ToString for ManDiscover {
    fn to_string(&self) -> String {
        String::from("\"ssdp:discover\"")
    }
}

impl FromStr for ManDiscover {
    type Err = Error;
    fn from_str(s: &str) -> Result<Self, Self::Err> {
        match s {
            "ssdp:discover" | "\"ssdp:discover\"" => Ok(Self {}),
            _ => Err(Error::IncorrectHeader("man")),
        }
    }
}

/// What kind of control point to search for
#[derive(PartialEq, Debug, Hash, Clone)]
pub enum SearchTarget {
    /// Search for all devices and services
    All,
    /// Search for root devices only
    RootDevice,
    /// Search for a particular device
    UUID(uuid::Uuid),

    /// Search for any device of this type, where device_type is defined by the UPnP forum
    Device {
        device_type: String,
        version: String,
    },
    /// Search for any service of this type, where service_type is defined by the UPnP forum
    Service {
        service_type: String,
        version: String,
    },

    /// Search for for any device of this type, where device_type is defined by a vendor
    VendorDevice {
        domain_name: String,
        device_type: String,
        version: String,
    },
    /// Search for for any service of this type, where service_type is defined by a vendor
    VendorService {
        domain_name: String,
        service_type: String,
        version: String,
    },

    /// Not everyone plays by the rules. A catch-all for non-standard search types
    Other(String),
}

impl ToString for SearchTarget {
    fn to_string(&self) -> std::string::String {
        use SearchTarget::*;

        match self {
            All => "ssdp:all".to_string(),
            RootDevice => "upnp:rootdevice".to_string(),
            UUID(uuid) => format!("uuid:{}", uuid.to_string()),
            Device {
                device_type,
                version,
            } => format!("urn:schemas-upnp-org:device:{}:{}", device_type, version),
            Service {
                service_type,
                version,
            } => format!("urn:schemas-upnp-org:service:{}:{}", service_type, version),
            VendorDevice {
                domain_name,
                device_type,
                version,
            } => format!("urn:{}:device:{}:{}", domain_name, device_type, version),
            VendorService {
                domain_name,
                service_type,
                version,
            } => format!("urn:{}:sercvice:{}:{}", domain_name, service_type, version),
            Other(s) => s.to_string(),
        }
    }
}

impl FromStr for SearchTarget {
    type Err = Error;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        use SearchTarget::*;

        Ok(match s.split(':').collect::<Vec<&str>>().as_slice() {
            ["ssdp", "all"] => All,
            ["upnp", "rootdevice"] => RootDevice,
            ["uuid", uuid] => UUID(uuid::Uuid::parse_str(uuid)?),
            ["urn", "schemas-upnp-org", "device", dt, v] => Device {
                device_type: (*dt).to_string(),
                version: (*v).to_string(),
            },
            ["urn", "schemas-upnp-org", "service", st, v] => Service {
                service_type: (*st).to_string(),
                version: (*v).to_string(),
            },
            ["urn", dn, "device", dt, v] => VendorDevice {
                domain_name: (*dn).to_string(),
                device_type: (*dt).to_string(),
                version: (*v).to_string(),
            },
            ["urn", dn, "service", st, v] => VendorService {
                domain_name: (*dn).to_string(),
                service_type: (*st).to_string(),
                version: (*v).to_string(),
            },
            _ => Other(s.to_owned()),
        })
    }
}

impl Default for SearchTarget {
    fn default() -> Self {
        Self::All
    }
}

#[derive(Default, PartialEq, Debug, Hash, Clone)]
pub struct UniqueServiceName {
    pub uuid: String,
    pub search_target: Option<SearchTarget>,
}

impl FromStr for UniqueServiceName {
    type Err = Error;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        match s.split("::").collect::<Vec<&str>>().as_slice() {
            [urn, st] if urn.starts_with("uuid:") => {
                let uuid = urn[5..].to_string();
                st.parse().map(|st| UniqueServiceName {
                    uuid,
                    search_target: Some(st),
                })
            }
            [urn] if urn.starts_with("uuid:") => Ok(UniqueServiceName {
                uuid: urn[5..].to_string(),
                search_target: None,
            }),
            _ => Err(Error::MalformedHeader("usn", s.to_owned())),
        }
    }
}

impl ToString for UniqueServiceName {
    fn to_string(&self) -> String {
        let out = format!("uuid:{}", self.uuid);
        if let Some(st) = &self.search_target {
            format!("{}::{}", out, st.to_string())
        } else {
            out
        }
    }
}