diff --git a/AGENTS.md b/AGENTS.md index caa6336..9085a80 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -129,7 +129,7 @@ again when the spec changes). `make test` runs it. Worked examples: `apps/calend | `reminders` | `0x11` | `reminders` | iOS | open reminders (≤ 20): id, due day and minute, flags, list colour, title | | `reminders.complete` | `0x12` | `reminders` | iOS | marks one done | | `location` | `0x20` | `location` | both | lat/lon in 0.1°, fix age, locality name | -| `weather` | `0x30` | `weather` | both | now and up to 7 days | +| `weather` | `0x30` | `weather` | both | now, the next hours (up to 24: temperature, code, precipitation chance, night) and up to 7 days. Hours need a companion of 2026-11-20 or later: an older one answers `BAD_REQUEST` (Android) or without the hourly record (iOS), so ask again with `hours` 0 and show no hours | | `http.get` | `0x40` | `http:` | both | status, length, body (≤ 2048 B) of an `https://` GET | | `http.request` | `0x41` | `http:` | both | any method with a body (multi-part), the same reply | | `notifications` | `0x50` | `notifications` | Android | the notifications shown now (≤ 10) | diff --git a/crates/companion-rpc/src/lib.rs b/crates/companion-rpc/src/lib.rs index feecd3a..98f1099 100644 --- a/crates/companion-rpc/src/lib.rs +++ b/crates/companion-rpc/src/lib.rs @@ -216,9 +216,14 @@ pub fn location(id: u8) -> Msg { Msg::small(id, method::LOCATION, &[]) } -/// `weather`: the current record, then daily ones for `days` (0-7) days. -pub fn weather(id: u8, days: u8) -> Msg { - Msg::small(id, method::WEATHER, &[days]) +/// `weather`: the current record, the hourly one for the next `hours` (0-24, over counts as 24), then daily ones +/// for `days` (0-7) days. `hours` 0 asks the way every companion understands; a companion from before hours +/// answers `BAD_REQUEST` (Android) or without the hourly record (iOS) to anything else. +pub fn weather(id: u8, days: u8, hours: u8) -> Msg { + if hours == 0 { + return Msg::small(id, method::WEATHER, &[days]); + } + Msg::small(id, method::WEATHER, &[days, hours.min(24)]) } /// `http.get` of an `https://` URL of at most 123 bytes. @@ -349,7 +354,7 @@ pub fn storage_put<'a>(id: u8, key: &str, value: &[u8], out: &'a mut [u8; JOINED #[derive(Clone, Copy, Debug)] pub enum Job<'a> { Web { method: u8, url: &'a str, body: &'a [u8] }, - Weather { days: u8 }, + Weather { days: u8, hours: u8 }, Calendar { from_day: i8, days: u8, limit: u8 }, } @@ -372,10 +377,11 @@ pub fn job_set<'a>(id: u8, job: u8, minutes: u16, what: Job<'_>, target: u8, out out[3] = 0; 5 + http_payload(&mut out[5..], method, url, body)? } - Job::Weather { days } => { + Job::Weather { days, hours } => { out[3] = 1; out[5] = days; - 6 + out[6] = hours.min(24); + if hours == 0 { 6 } else { 7 } } Job::Calendar { from_day, days, limit } => { out[3] = 2; @@ -689,13 +695,24 @@ impl Location { } } -/// `weather` records: the current one first, then one per day. +/// One hour of `Weather::Hours`: `temp` °C, `code` WMO, `precip` probability %, `night` for a moon instead of a sun. +#[derive(Clone, Copy, PartialEq, Eq, Debug)] +pub struct Hour { + pub temp: i8, + pub code: u8, + pub precip: u8, + pub night: bool, +} + +/// `weather` records: the current one first, then the hourly one if hours were asked, then one per day. #[derive(Clone, Copy, PartialEq, Eq, Debug)] pub enum Weather { /// `temp` 0.1 °C, `code` WMO, `wind` m/s, `humidity` %, `age` minutes since fetched (≤ 255). Now { temp: i16, code: u8, wind: u8, humidity: u8, age: u8 }, /// `day` 0 = today, `min`/`max` °C, `precip` probability %. Day { day: i8, min: i8, max: i8, code: u8, precip: u8 }, + /// `n` (≤ 24) consecutive hours from `hour`, the local hour (0-23) that contains now: read them with `hour(i)`. + Hours { hour: u8, n: u8, data: [u8; 72] }, } impl Weather { @@ -704,6 +721,24 @@ impl Weather { match r.u8()? { 0 => Some(Weather::Now { temp: r.u16()? as i16, code: r.u8()?, wind: r.u8()?, humidity: r.u8()?, age: r.u8()? }), 1 => Some(Weather::Day { day: r.u8()? as i8, min: r.u8()? as i8, max: r.u8()? as i8, code: r.u8()?, precip: r.u8()? }), + 2 => { + let hour = r.u8()?; + let n = (r.0.len() / 3).min(24); // a cut entry is dropped + let mut data = [0; 72]; + data[..n * 3].copy_from_slice(&r.0[..n * 3]); + Some(Weather::Hours { hour, n: n as u8, data }) + } + _ => None, + } + } + + /// Hour `i` of an hourly record, 0 the one that contains now. + pub fn hour(&self, i: usize) -> Option { + match self { + Weather::Hours { n, data, .. } if i < *n as usize => { + let e = &data[i * 3..]; + Some(Hour { temp: e[0] as i8, code: e[1], precip: e[2] & 0x7F, night: e[2] & 0x80 != 0 }) + } _ => None, } } @@ -856,8 +891,8 @@ mod tests { assert!(http_request(1, http::GET, "http://e.com", b"", &mut out).is_none()); assert!(storage_put(1, "k", &[0; JOINED_MAX - 2], &mut out).is_some_and(|p| p.total() == 9)); assert!(storage_put(1, "k", &[0; JOINED_MAX - 1], &mut out).is_none()); - assert!(job_set(1, 0, 30, Job::Weather { days: 3 }, JOB_WIDGET, &mut out).is_none()); - assert!(job_set(1, 4, 30, Job::Weather { days: 3 }, JOB_BLOB, &mut out).is_none()); + assert!(job_set(1, 0, 30, Job::Weather { days: 3, hours: 0 }, JOB_WIDGET, &mut out).is_none()); + assert!(job_set(1, 4, 30, Job::Weather { days: 3, hours: 0 }, JOB_BLOB, &mut out).is_none()); } #[test] diff --git a/crates/companion-rpc/tests/rpc-vectors.json b/crates/companion-rpc/tests/rpc-vectors.json index cf37535..5ae0b35 100644 --- a/crates/companion-rpc/tests/rpc-vectors.json +++ b/crates/companion-rpc/tests/rpc-vectors.json @@ -178,6 +178,111 @@ "precip": 40 }, "hex": "0101f9ff0328" + }, + { + "in": { + "kind": "hours", + "hour": 13, + "hours": [ + { + "temp": -3, + "code": 3, + "precip": 20, + "night": false + }, + { + "temp": -4.4, + "code": 71, + "precip": 100, + "night": true + } + ] + }, + "hex": "020dfd0314fc47e4" + }, + { + "in": { + "kind": "now", + "temp": -0.25, + "code": 0, + "wind": 1, + "humidity": 50, + "age_min": 0 + }, + "hex": "00fdff00013200", + "note": "a half rounds away from zero: -2.5 tenths are -3" + }, + { + "in": { + "kind": "day", + "day": 0, + "min": -2.5, + "max": -0.5, + "code": 0, + "precip": 0 + }, + "hex": "0100fdff0000", + "note": "halves away from zero" + }, + { + "in": { + "kind": "hours", + "hour": 0, + "hours": [ + { + "temp": -0.5, + "code": 0, + "precip": 0, + "night": true + }, + { + "temp": -2.5, + "code": 0, + "precip": 0, + "night": true + }, + { + "temp": 2.5, + "code": 0, + "precip": 0, + "night": true + } + ] + }, + "hex": "0200ff0080fd0080030080", + "note": "halves away from zero" + } + ], + "weather_args": [ + { + "args": "03", + "days": 3, + "hours": 0 + }, + { + "args": "030c", + "days": 3, + "hours": 12 + }, + { + "args": "0718", + "days": 7, + "hours": 24 + }, + { + "args": "0319", + "bad": true, + "note": "over 24 hours" + }, + { + "args": "030c00", + "bad": true, + "note": "a third byte" + }, + { + "args": "", + "bad": true, + "note": "no days" } ], "http": [ @@ -789,6 +894,18 @@ "cap": "http:example.com" } }, + { + "payload": "011e000101030c", + "job": { + "id": 1, + "minutes": 30, + "kind": 1, + "target": 1, + "spec": "030c", + "cap": "weather" + }, + "note": "the weather with 12 hours" + }, { "payload": "001e00010003", "bad": true, @@ -828,6 +945,11 @@ "payload": "000100", "bad": true, "note": "cut short" + }, + { + "payload": "011e0001010319", + "bad": true, + "note": "weather with 25 hours" } ] } diff --git a/crates/companion-rpc/tests/vectors.rs b/crates/companion-rpc/tests/vectors.rs index 2a40fc8..f93f908 100644 --- a/crates/companion-rpc/tests/vectors.rs +++ b/crates/companion-rpc/tests/vectors.rs @@ -266,6 +266,22 @@ fn location() { fn weather() { for r in vectors().get("weather").arr() { let i = r.get("in"); + if i.get("kind").str() == "hours" { + let Some(w @ Weather::Hours { hour, n, .. }) = Weather::decode(&r.get("hex").hex()) else { panic!("not hours") }; + let hours = i.get("hours").arr(); + assert_eq!((hour as i64, n as usize), (i.get("hour").int(), hours.len())); + for (k, h) in hours.iter().enumerate() { + let want = Hour { + temp: h.get("temp").num().round() as i8, + code: h.get("code").int() as u8, + precip: h.get("precip").int() as u8, + night: h.get("night").truthy(), + }; + assert_eq!(w.hour(k), Some(want)); + } + assert_eq!(w.hour(hours.len()), None); + continue; + } let want = if i.get("kind").str() == "now" { Weather::Now { temp: (i.get("temp").num() * 10.0).round() as i16, @@ -277,8 +293,8 @@ fn weather() { } else { Weather::Day { day: i.get("day").int() as i8, - min: i.get("min").int() as i8, - max: i.get("max").int() as i8, + min: i.get("min").num().round() as i8, + max: i.get("max").num().round() as i8, code: i.get("code").int() as u8, precip: i.get("precip").int() as u8, } @@ -287,6 +303,30 @@ fn weather() { } } +#[test] +fn weather_args() { + for r in vectors().get("weather_args").arr() { + if r.get("bad").truthy() { + continue; // what the phone refuses; `weather` can't build them except hours over 24, cut below + } + let m = companion_rpc::weather(9, r.get("days").int() as u8, r.get("hours").int() as u8); + assert_eq!(&m[..], &[&hex("52315109")[..], &[0x30], &r.get("args").hex()[..]].concat()[..]); + } + assert_eq!(&companion_rpc::weather(9, 3, 30)[..], &hex("5231510930").iter().copied().chain([3, 24]).collect::>()[..], "hours over 24 are 24"); +} + +#[test] +fn weather_hours_odd_lengths() { + // a cut entry is dropped; more than 24 hours are 24; no entry at all is still a record + let Some(w @ Weather::Hours { n: 1, .. }) = Weather::decode(&[2, 5, 1, 2, 3, 9, 9]) else { panic!() }; + assert_eq!(w.hour(0), Some(Hour { temp: 1, code: 2, precip: 3, night: false })); + let long = [&[2u8, 23][..], &[0x80u8; 90][..]].concat(); + let Some(w @ Weather::Hours { n: 24, hour: 23, .. }) = Weather::decode(&long) else { panic!() }; + assert_eq!(w.hour(23), Some(Hour { temp: -128, code: 0x80, precip: 0, night: true })); + assert!(matches!(Weather::decode(&[2, 7]), Some(Weather::Hours { n: 0, .. }))); + assert_eq!(Weather::decode(&[2]), None); +} + #[test] fn http() { let v = vectors(); @@ -397,7 +437,7 @@ fn jobs() { let (url_len, url) = (spec[1] as usize, &spec[2..2 + spec[1] as usize]); Job::Web { method: spec[0], url: std::str::from_utf8(url).unwrap(), body: &spec[2 + url_len..] } } - 1 => Job::Weather { days: spec[0] }, + 1 => Job::Weather { days: spec[0], hours: spec.get(1).copied().unwrap_or(0) }, _ => Job::Calendar { from_day: spec[0] as i8, days: spec[1], limit: spec[2] }, }; // the vectors' `minutes` is what the phone makes of the payload's; build from the payload's own diff --git a/docs/companion.md b/docs/companion.md index e365975..b1e8698 100644 --- a/docs/companion.md +++ b/docs/companion.md @@ -18,7 +18,7 @@ phone answers `UNAVAILABLE` and `caps` doesn't list it. Request and record layou | `calendar.events` | `calendar` | both | the next 1-7 days of events | | `reminders`, `reminders.complete` | `reminders` | iOS | open reminders; mark one done | | `location` | `location` | both | 0.1° and the town | -| `weather` | `weather` | both | now and up to 7 days | +| `weather` | `weather` | both | now, the next hours (up to 24) and up to 7 days; hours need a companion of 2026-11-20 or later | | `http.get`, `http.request` | `http:` | both | an HTTPS request to that host, body ≤ 2048 B | | `notifications` | `notifications` | Android | the notifications shown now | | `notify` | `notify` | both | a notification on the phone, or "find my phone" |