wizlight 0.1.0-beta.1

Async client for Philips WiZ smart bulbs over the local UDP protocol, plus the wizlight CLI
Documentation
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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
//! Subscriptions to unsolicited bulb state updates.

use std::collections::HashMap;
use std::io::ErrorKind;
use std::net::{IpAddr, Ipv4Addr, SocketAddr};
use std::pin::Pin;
use std::sync::{Arc, Mutex, MutexGuard, Weak};
use std::task::{Context, Poll};
use std::time::Duration;

use serde::Deserialize;
use serde_json::{Value, json};
use tokio::net::UdpSocket;
use tokio::sync::mpsc;
use tokio::task::JoinHandle;
use tokio::time::{Instant, sleep_until};

use crate::discovery::local_ip_towards;
use crate::error::{Error, Result};
use crate::protocol::{Pilot, Request};

/// The UDP port WiZ bulbs send push updates to.
pub const PUSH_PORT: u16 = 38900;

/// How often an active subscription is renewed at its bulb.
pub const PUSH_KEEPALIVE_INTERVAL: Duration = Duration::from_secs(20);

/// A typed reason that push updates cannot be used.
///
/// Callers can match this independently from transient request failures and
/// fall back to polling without inspecting an operating-system error string.
#[derive(Clone, Debug, PartialEq, Eq, thiserror::Error)]
#[non_exhaustive]
pub enum PushUnavailable {
    /// Another process already owns the requested listener address.
    #[error("push listener address {addr} is already in use")]
    PortInUse {
        /// The address that could not be bound.
        addr: SocketAddr,
    },
    /// The routing table did not yield a source address for this bulb.
    #[error("could not determine a local source IP towards {target}")]
    SourceIp {
        /// The bulb the source address was needed for.
        target: SocketAddr,
    },
    /// The listener stopped after its socket failed or its last subscriber left.
    #[error("the push listener has stopped")]
    ListenerStopped,
}

/// One unsolicited message from a subscribed bulb.
#[derive(Clone, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub enum PushEvent {
    /// A bulb state update.
    SyncPilot {
        /// Where the datagram came from.
        addr: SocketAddr,
        /// The state carried in the push.
        pilot: Pilot,
    },
    /// A bulb announcing itself after boot or reconnect.
    FirstBeat {
        /// Where the datagram came from.
        addr: SocketAddr,
        /// The bulb's lowercase, separator-free MAC.
        mac: String,
    },
}

/// Owns the shared UDP listener and its per-MAC subscription registry.
///
/// Bind one manager and use it for every bulb in the process. The operating
/// system permits only one listener on [`PUSH_PORT`]; if another process owns
/// it, [`bind`](PushManager::bind) returns a typed
/// [`PushUnavailable::PortInUse`] reason. The socket closes when the last
/// [`PushSubscription`] is dropped.
///
/// ```no_run
/// use std::net::{IpAddr, Ipv4Addr};
///
/// use wizlight::{Bulb, PushEvent, PushManager};
///
/// # async fn example() -> Result<(), wizlight::Error> {
/// let bulb = Bulb::connect(IpAddr::V4(Ipv4Addr::new(192, 168, 0, 5))).await?;
/// let pushes = PushManager::bind().await?;
/// let mut subscription = pushes.subscribe("9877d5230f0a", bulb.addr()).await?;
/// if let Some(PushEvent::SyncPilot { pilot, .. }) = subscription.recv().await {
///     println!("on={:?}", pilot.state);
/// }
/// # Ok(())
/// # }
/// ```
pub struct PushManager {
    shared: Arc<Shared>,
    local_addr: SocketAddr,
}

impl PushManager {
    /// Binds the standard all-interface listener on [`PUSH_PORT`].
    ///
    /// # Errors
    ///
    /// Returns [`Error::PushUnavailable`] with
    /// [`PushUnavailable::PortInUse`] when another listener owns the port, or
    /// [`Error::Io`] for another socket failure.
    pub async fn bind() -> Result<Self> {
        Self::bind_to(SocketAddr::new(
            IpAddr::V4(Ipv4Addr::UNSPECIFIED),
            PUSH_PORT,
        ))
        .await
    }

    /// Binds a different listener address.
    ///
    /// This is useful for a test bulb or a UDP forwarder. Real WiZ bulbs always
    /// push to [`PUSH_PORT`].
    ///
    /// # Errors
    ///
    /// As [`bind`](PushManager::bind).
    pub async fn bind_to(addr: SocketAddr) -> Result<Self> {
        let socket = match UdpSocket::bind(addr).await {
            Ok(socket) => Arc::new(socket),
            Err(error) if error.kind() == ErrorKind::AddrInUse => {
                return Err(PushUnavailable::PortInUse { addr }.into());
            }
            Err(error) => return Err(Error::Io(error)),
        };
        let local_addr = socket.local_addr()?;
        let shared = Arc::new(Shared::new(Arc::downgrade(&socket)));
        let listener = tokio::spawn(listen(Arc::clone(&socket), Arc::clone(&shared)));
        let keepalive = tokio::spawn(keepalive(socket, Arc::clone(&shared)));
        shared.set_workers(listener, keepalive);
        Ok(Self { shared, local_addr })
    }

    /// The address the listener is bound to.
    pub fn local_addr(&self) -> SocketAddr {
        self.local_addr
    }

    /// Re-registers every active subscription immediately.
    ///
    /// WiZ discovery sends `register: false` and may clear an existing push
    /// registration. Call this after a [`Discovery`](crate::Discovery) run that
    /// overlaps active subscriptions rather than waiting for the next periodic
    /// keepalive.
    ///
    /// # Errors
    ///
    /// Returns [`Error::PushUnavailable`] if the listener has stopped, or the
    /// first [`Error::Io`] from a registration send after trying every target.
    pub async fn refresh(&self) -> Result<()> {
        let socket = self
            .shared
            .socket
            .upgrade()
            .ok_or(PushUnavailable::ListenerStopped)?;
        let registrations = self
            .shared
            .registrations()
            .ok_or(PushUnavailable::ListenerStopped)?;
        send_registrations(&socket, registrations).await
    }

    /// Subscribes to one bulb, identified by its MAC.
    ///
    /// Registration is sent before this returns, then refreshed every
    /// [`PUSH_KEEPALIVE_INTERVAL`]. The advertised `phoneIp` is selected from
    /// the route towards this specific target, so bulbs on different interfaces
    /// receive the correct address.
    ///
    /// Only one live subscription per MAC is accepted by a manager. MAC matching
    /// is ASCII case-insensitive.
    ///
    /// # Errors
    ///
    /// Returns [`Error::PushUnavailable`] when no source IP can be selected or
    /// the listener has already stopped, [`Error::InvalidParam`] for a duplicate
    /// MAC, [`Error::Json`] if registration cannot be serialised, or [`Error::Io`]
    /// if its initial send fails.
    pub async fn subscribe(
        &self,
        mac: impl Into<String>,
        target: SocketAddr,
    ) -> Result<PushSubscription> {
        let mac = mac.into().to_ascii_lowercase();
        let phone_ip = local_ip_towards(target).ok_or(PushUnavailable::SourceIp { target })?;
        let payload = registration(phone_ip, true)?;
        let unregister = registration(phone_ip, false)?;
        let socket = self
            .shared
            .socket
            .upgrade()
            .ok_or(PushUnavailable::ListenerStopped)?;
        let (tx, rx) = mpsc::channel(16);
        let id = self
            .shared
            .insert(mac.clone(), target, payload.clone(), tx)?;

        if let Err(error) = socket.send_to(&payload, target).await {
            abort(self.shared.cancel(&mac, id));
            return Err(Error::Io(error));
        }

        Ok(PushSubscription {
            mac,
            id: Some(id),
            target,
            unregister,
            shared: Arc::clone(&self.shared),
            rx,
        })
    }
}

impl Drop for PushManager {
    fn drop(&mut self) {
        self.shared.stop_if_empty();
    }
}

impl std::fmt::Debug for PushManager {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("PushManager")
            .field("local_addr", &self.local_addr)
            .field("subscriptions", &self.shared.subscription_count())
            .finish()
    }
}

/// A cancellable stream of push events for one MAC.
///
/// Dropping this handle unregisters it on a best-effort basis and begins
/// stopping the listener when it was the last subscription. Use
/// [`cancel`](PushSubscription::cancel) to wait until the bulb is unregistered
/// and the listener port has been released. Dropping the manager does not
/// invalidate live subscriptions.
pub struct PushSubscription {
    mac: String,
    id: Option<u64>,
    target: SocketAddr,
    unregister: Vec<u8>,
    shared: Arc<Shared>,
    rx: mpsc::Receiver<PushEvent>,
}

impl PushSubscription {
    /// The lowercase MAC this handle receives events for.
    pub fn mac(&self) -> &str {
        &self.mac
    }

    /// Waits for the next valid `syncPilot` or `firstBeat` message.
    ///
    /// Returns `None` if the listener stops. Literal `test` datagrams, malformed
    /// JSON, unknown methods and pushes for other MACs are ignored. The listener
    /// buffers sixteen unread events per subscription and drops later events
    /// rather than let one slow consumer block every bulb.
    pub async fn recv(&mut self) -> Option<PushEvent> {
        self.rx.recv().await
    }

    /// Cancels this subscription and waits for its network cleanup.
    ///
    /// The bulb is sent `register: false`. If this was the manager's last
    /// subscription, this also waits for both worker tasks to stop and release
    /// the listener port.
    ///
    /// # Errors
    ///
    /// Returns [`Error::Io`] if the unregister datagram cannot be handed to the
    /// local socket. Local cancellation still completes in that case.
    pub async fn cancel(mut self) -> Result<()> {
        let socket = self.shared.socket.upgrade();
        let workers = self
            .id
            .take()
            .and_then(|id| self.shared.cancel(&self.mac, id));
        join(workers).await;
        match socket {
            Some(socket) => socket
                .send_to(&self.unregister, self.target)
                .await
                .map(|_| ())
                .map_err(Error::Io),
            None => Ok(()),
        }
    }
}

impl Drop for PushSubscription {
    fn drop(&mut self) {
        let Some(id) = self.id.take() else {
            return;
        };
        let socket = self.shared.socket.upgrade();
        let workers = self.shared.cancel(&self.mac, id);
        if let Some(socket) = socket {
            let _ = socket.try_send_to(&self.unregister, self.target);
        }
        abort(workers);
    }
}

impl std::fmt::Debug for PushSubscription {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("PushSubscription")
            .field("mac", &self.mac)
            .finish_non_exhaustive()
    }
}

impl futures_core::Stream for PushSubscription {
    type Item = PushEvent;

    fn poll_next(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> {
        self.rx.poll_recv(cx)
    }
}

struct Shared {
    socket: Weak<UdpSocket>,
    state: Mutex<State>,
}

impl Shared {
    fn new(socket: Weak<UdpSocket>) -> Self {
        Self {
            socket,
            state: Mutex::new(State::default()),
        }
    }

    fn state(&self) -> MutexGuard<'_, State> {
        self.state.lock().expect("push registry mutex poisoned")
    }

    fn set_workers(&self, listener: JoinHandle<()>, keepalive: JoinHandle<()>) {
        let mut state = self.state();
        state.workers = Some(Workers {
            listener,
            keepalive,
        });
    }

    fn insert(
        &self,
        mac: String,
        target: SocketAddr,
        payload: Vec<u8>,
        tx: mpsc::Sender<PushEvent>,
    ) -> Result<u64> {
        let mut state = self.state();
        if state.stopped {
            return Err(PushUnavailable::ListenerStopped.into());
        }
        if state.subscriptions.contains_key(&mac) {
            return Err(Error::InvalidParam {
                message: format!("a push subscription for {mac} already exists"),
            });
        }
        let id = state.next_id;
        state.next_id = state.next_id.wrapping_add(1);
        state.subscriptions.insert(
            mac,
            SubscriptionEntry {
                id,
                target,
                payload,
                tx,
            },
        );
        Ok(id)
    }

    fn cancel(&self, mac: &str, id: u64) -> Option<Workers> {
        let mut state = self.state();
        if state.subscriptions.get(mac).map(|entry| entry.id) == Some(id) {
            state.subscriptions.remove(mac);
        }
        state.stop_if_empty()
    }

    fn stop_if_empty(&self) {
        let workers = self.state().stop_if_empty();
        abort(workers);
    }

    fn stop(&self) {
        let workers = {
            let mut state = self.state();
            state.stopped = true;
            state.subscriptions.clear();
            state.workers.take()
        };
        abort(workers);
    }

    fn subscription_count(&self) -> usize {
        self.state().subscriptions.len()
    }

    fn registrations(&self) -> Option<Vec<(SocketAddr, Vec<u8>)>> {
        let state = self.state();
        if state.stopped {
            return None;
        }
        Some(
            state
                .subscriptions
                .values()
                .map(|entry| (entry.target, entry.payload.clone()))
                .collect(),
        )
    }

    fn sender(&self, mac: &str) -> Option<mpsc::Sender<PushEvent>> {
        self.state()
            .subscriptions
            .get(mac)
            .map(|entry| entry.tx.clone())
    }
}

#[derive(Default)]
struct State {
    subscriptions: HashMap<String, SubscriptionEntry>,
    workers: Option<Workers>,
    next_id: u64,
    stopped: bool,
}

struct SubscriptionEntry {
    id: u64,
    target: SocketAddr,
    payload: Vec<u8>,
    tx: mpsc::Sender<PushEvent>,
}

struct Workers {
    listener: JoinHandle<()>,
    keepalive: JoinHandle<()>,
}

impl State {
    fn stop_if_empty(&mut self) -> Option<Workers> {
        if self.subscriptions.is_empty() {
            self.stopped = true;
            self.workers.take()
        } else {
            None
        }
    }
}

fn abort(workers: Option<Workers>) {
    if let Some(workers) = workers {
        workers.listener.abort();
        workers.keepalive.abort();
    }
}

async fn join(workers: Option<Workers>) {
    if let Some(workers) = workers {
        workers.listener.abort();
        workers.keepalive.abort();
        let _ = workers.listener.await;
        let _ = workers.keepalive.await;
    }
}

async fn listen(socket: Arc<UdpSocket>, shared: Arc<Shared>) {
    let mut buf = vec![0u8; 4096];
    loop {
        let (n, addr) = match socket.recv_from(&mut buf).await {
            Ok(datagram) => datagram,
            Err(error) if error.kind() == ErrorKind::ConnectionReset => continue,
            Err(_) => {
                shared.stop();
                return;
            }
        };
        let Some((mac, event)) = parse_push(&buf[..n], addr) else {
            continue;
        };
        if let Some(tx) = shared.sender(&mac) {
            let _ = tx.try_send(event);
        }
    }
}

async fn keepalive(socket: Arc<UdpSocket>, shared: Arc<Shared>) {
    let mut next = Instant::now() + PUSH_KEEPALIVE_INTERVAL;
    loop {
        sleep_until(next).await;
        let Some(registrations) = shared.registrations() else {
            return;
        };
        let _ = send_registrations(&socket, registrations).await;
        next += PUSH_KEEPALIVE_INTERVAL;
    }
}

async fn send_registrations(
    socket: &UdpSocket,
    registrations: Vec<(SocketAddr, Vec<u8>)>,
) -> Result<()> {
    let mut first_error = None;
    for (target, payload) in registrations {
        if let Err(error) = socket.send_to(&payload, target).await {
            first_error.get_or_insert(error);
        }
    }
    first_error.map_or(Ok(()), |error| Err(Error::Io(error)))
}

#[derive(Deserialize)]
struct PushEnvelope {
    method: String,
    #[serde(default)]
    params: Value,
}

fn parse_push(datagram: &[u8], addr: SocketAddr) -> Option<(String, PushEvent)> {
    if datagram == b"test" {
        return None;
    }
    let envelope: PushEnvelope = serde_json::from_slice(datagram).ok()?;
    match envelope.method.as_str() {
        "syncPilot" => {
            let mut pilot: Pilot = serde_json::from_value(envelope.params).ok()?;
            let mac = pilot.mac.as_ref()?.to_ascii_lowercase();
            pilot.mac = Some(mac.clone());
            Some((mac, PushEvent::SyncPilot { addr, pilot }))
        }
        "firstBeat" => {
            let mac = envelope.params.get("mac")?.as_str()?.to_ascii_lowercase();
            Some((mac.clone(), PushEvent::FirstBeat { addr, mac }))
        }
        _ => None,
    }
}

fn registration(phone_ip: IpAddr, register: bool) -> Result<Vec<u8>> {
    let request = Request::with_params(
        "registration",
        &json!({
            "phoneMac": "AAAAAAAAAAAA",
            "register": register,
            "phoneIp": phone_ip,
        }),
    )?;
    Ok(serde_json::to_vec(&request)?)
}