~liz/yooper

904941bf6423d70345859319b260f4556dace218 — Ellie Frost 2 years ago 348c2f4
Fix serialisation and headers
M yooper/src/discovery.rs => yooper/src/discovery.rs +6 -6
@@ 22,7 22,7 @@ use crate::{
    Error,
};

const VERSION: &'static str = env!("CARGO_PKG_VERSION");
const VERSION: &str = env!("CARGO_PKG_VERSION");

const SSDP_ADDRESS: Ipv4Addr = Ipv4Addr::new(239, 255, 255, 250);
const SSDP_PORT: u16 = 1900;


@@ 61,17 61,17 @@ impl Discovery {
        })
    }

    pub async fn start_search(&mut self, secs: u16) -> Result<(), Error> {
    pub async fn start_search(&mut self, secs: u8) -> Result<(), Error> {
        // TODO: secs should be between 1 and 5
        let msg = Message::MSearch(
            MSearch {
                max_wait: Some(secs.to_string()),
                max_wait: Some(secs),
                target: "ssdp:all".into(),
                user_agent: Some(self.user_agent.clone()),
                host: format!("{}:{}", SSDP_ADDRESS, SSDP_PORT),

                friendly_name: Some("yooper".into()),
                uuid: Some(self.uuid.to_string()),
                friendly_name: None, //Some("yooper".into()),
                uuid: None, // Some(self.uuid.to_string()),

                ..Default::default()
            }


@@ 82,7 82,7 @@ impl Discovery {
        Ok(())
    }

    pub async fn find(&mut self, secs: u16) -> Result<Vec<Device>, Error> {
    pub async fn find(&mut self, secs: u8) -> Result<Vec<Device>, Error> {
        let mut map: HashMap<SocketAddr, Device> = HashMap::new();
        self.start_search(secs).await?;


M yooper/src/ssdp/message.rs => yooper/src/ssdp/message.rs +9 -4
@@ 6,17 6,21 @@ pub use codec::Codec;

#[derive(ToHeaders, FromHeaders, Debug, PartialEq, Default)]
pub struct MSearch {
    pub host: String,

    /// Maximum wait time in seconds. shall be greater than or equal to 1 and should
    /// be less than 5 inclusive.
    #[header("cache-control")]
    pub max_wait: Option<String>,
    /// Field value contains Search Target.
    pub cache_control: Option<String>,

    #[header("mx")]
    pub max_wait: Option<u8>,

    pub host: String,

    pub man: types::ManDiscover,

    // TODO: enum
    /// Field value contains Search Target.
    #[header("st")]
    pub target: String,
    /// Field value shall begin with the following “product tokens” (defined


@@ 41,6 45,8 @@ pub struct MSearch {

#[derive(ToHeaders, FromHeaders, Debug, PartialEq)]
pub struct Available {
    pub host: String,

    /// after this duration, control points should assume the device (or
    /// service)  is  no  longer  available;  as  long  as  a  control  point
    /// has  received  at  least  one  advertisement  that  is  still  valid


@@ 54,7 60,6 @@ pub struct Available {
    /// Specified  by  UPnP  vendor. Single absolute URL (see RFC 3986)
    pub location: String,

    pub host: String,

    #[header("securelocation.upnp.org")]
    pub secure_location: Option<String>,

M yooper/src/ssdp/message/types.rs => yooper/src/ssdp/message/types.rs +2 -2
@@ 25,7 25,7 @@ pub struct ManDiscover;

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



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

M yooper/src/ssdp/packet/encoder.rs => yooper/src/ssdp/packet/encoder.rs +1 -1
@@ 16,7 16,7 @@ impl codec::Encoder<Packet> for Encoder {
        write!(dst, "{}\r\n", p.typ.to_string())?;
        p.headers
            .iter()
            .map(|(k, v)| write!(dst, "{}: {}\r\n", k, v))
            .map(|(k, v)| write!(dst, "{}: {}\r\n", k.to_uppercase(), v))
            .collect::<Result<(), std::fmt::Error>>()?;
        write!(dst, "\r\n")?;
        Ok(())