zigbee-sim 0.3.0

A Zigbee network co-processor simulator (EZSP, ZNP, deCONZ) with simulated devices, for testing Zigbee hosts without hardware
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
559
560
561
562
563
564
565
566
567
568
569
570
571
//! The deCONZ front-end: a ConBee II over the deCONZ serial protocol.
//!
//! As the firmware does, it queues received frames and delivery confirms
//! and announces them through the device state, which the host polls for.
//! Network parameters are inputs to formation while offline and read back
//! the running network while connected.

use std::collections::{BTreeMap, HashMap, VecDeque};
use std::time::Duration;

use tokio::io::{AsyncRead, AsyncReadExt, AsyncWrite, AsyncWriteExt};
use tokio::sync::mpsc;
use zigbee_deconz::ids::{device_state, net_state, param, status};
use zigbee_deconz::{Address, ApsConfirm, ApsIndication, Decoder, Request, Response};
use zigbee_primitives::{ExtPanId, Ieee, Key};

use crate::device::Outgoing;
use crate::mesh::{Mesh, MeshEvent, Scheduled, SimNetwork};
use crate::ncp::{lock, SimError, Simulator};

/// The firmware version the simulator reports (a ConBee II).
pub(crate) const FIRMWARE: u32 = 0x2678_0700;
/// ZDO cluster of a network update notify.
const MGMT_NWK_UPDATE_NOTIFY: u16 = 0x8038;

/// A frame for the host that is not a reply.
#[derive(Debug)]
pub(crate) enum Pending {
    /// Tell the host the device state changed.
    StateChanged,
}

pub(crate) struct DeconzFront {
    params: HashMap<u8, Vec<u8>>,
    indications: VecDeque<ApsIndication>,
    confirms: VecDeque<ApsConfirm>,
    pub link_keys: BTreeMap<Ieee, Key>,
}

fn u16_of(v: Option<&Vec<u8>>) -> Option<u16> {
    v.and_then(|v| <[u8; 2]>::try_from(v.as_slice()).ok())
        .map(u16::from_le_bytes)
}

fn u32_of(v: Option<&Vec<u8>>) -> Option<u32> {
    v.and_then(|v| <[u8; 4]>::try_from(v.as_slice()).ok())
        .map(u32::from_le_bytes)
}

fn u64_of(v: Option<&Vec<u8>>) -> Option<u64> {
    v.and_then(|v| <[u8; 8]>::try_from(v.as_slice()).ok())
        .map(u64::from_le_bytes)
}

impl DeconzFront {
    pub fn new() -> DeconzFront {
        DeconzFront {
            params: HashMap::new(),
            indications: VecDeque::new(),
            confirms: VecDeque::new(),
            link_keys: BTreeMap::new(),
        }
    }

    fn state(&self, mesh: &Mesh) -> u8 {
        let mut s = if mesh.up {
            net_state::CONNECTED
        } else {
            net_state::OFFLINE
        };
        if !self.confirms.is_empty() {
            s |= device_state::CONFIRM_PENDING;
        }
        if !self.indications.is_empty() {
            s |= device_state::INDICATION_PENDING;
        }
        s | device_state::REQUEST_FREE
    }

    fn reset(&mut self, mesh: &mut Mesh) {
        // A new connection: the firmware keeps running.
        self.indications.clear();
        self.confirms.clear();
        let _ = mesh;
    }

    fn read(&self, mesh: &Mesh, p: u8, extra: &[u8]) -> Option<Vec<u8>> {
        let net = mesh.network.as_ref().filter(|_| mesh.up);
        Some(match p {
            param::MAC_ADDRESS => mesh.ieee().to_le_bytes().to_vec(),
            param::NWK_PANID => match net {
                Some(n) => n.pan_id.to_le_bytes().to_vec(),
                None => self.params.get(&p).cloned().unwrap_or(vec![0xFF, 0xFF]),
            },
            param::NWK_ADDRESS => (if mesh.up { 0u16 } else { 0xFFFE }).to_le_bytes().to_vec(),
            param::NWK_EXTENDED_PANID => net
                .map(|n| n.extended_pan_id.to_le_bytes())
                .unwrap_or([0; 8])
                .to_vec(),
            param::CURRENT_CHANNEL => vec![net.map(|n| n.channel).unwrap_or(0)],
            param::NWK_UPDATE_ID => match net {
                Some(n) => vec![n.nwk_update_id],
                None => self.params.get(&p).cloned().unwrap_or(vec![0]),
            },
            param::NWK_FRAME_COUNTER => match net {
                Some(n) => n.frame_counter.to_le_bytes().to_vec(),
                None => self.params.get(&p).cloned().unwrap_or(vec![0; 4]),
            },
            param::NETWORK_KEY => {
                let key = match net {
                    Some(n) => n.network_key.0.to_vec(),
                    None => self
                        .params
                        .get(&p)
                        .and_then(|v| v.get(1..).map(<[u8]>::to_vec))
                        .unwrap_or(vec![0; 16]),
                };
                let mut v = vec![extra.first().copied().unwrap_or(0)];
                v.extend_from_slice(&key);
                v
            }
            param::LINK_KEY => {
                let ieee = Ieee::from_le_bytes(extra.get(..8)?.try_into().ok()?);
                let key = self.link_keys.get(&ieee)?;
                let mut v = ieee.to_le_bytes().to_vec();
                v.extend_from_slice(&key.0);
                v
            }
            param::PROTOCOL_VERSION => 0x0108u16.to_le_bytes().to_vec(),
            param::PERMIT_JOIN => vec![u8::from(mesh.permit_open())],
            // Unset parameters read as the firmware's zero defaults.
            _ => match self.params.get(&p) {
                Some(v) => v.clone(),
                None => match p {
                    param::APS_DESIGNED_COORDINATOR
                    | param::SECURITY_MODE
                    | param::USE_PREDEFINED_NWK_PANID => vec![0],
                    param::CHANNEL_MASK | param::WATCHDOG_TTL => vec![0; 4],
                    param::APS_EXTENDED_PANID | param::TRUST_CENTER_ADDRESS => vec![0; 8],
                    param::APP_ZDP_RESPONSE_HANDLING => vec![0; 2],
                    _ => return None,
                },
            },
        })
    }

    fn write(&mut self, mesh: &mut Mesh, p: u8, value: &[u8]) -> u8 {
        match p {
            param::MAC_ADDRESS => {
                let Ok(bytes) = <[u8; 8]>::try_from(value) else {
                    return status::INVALID_VALUE;
                };
                let ieee = Ieee::from_le_bytes(bytes);
                mesh.ieee_override = (ieee != mesh.factory_ieee).then_some(ieee);
            }
            param::PERMIT_JOIN => {
                let seconds = value.first().copied().unwrap_or(0);
                if !mesh.up {
                    return status::NO_NETWORK;
                }
                mesh.allow_joins = seconds > 0;
                mesh.well_known_ok = true;
                mesh.permit(u16::from(seconds));
            }
            param::LINK_KEY => {
                let (Some(ieee), Some(key)) = (
                    value
                        .get(..8)
                        .and_then(|b| <[u8; 8]>::try_from(b).ok())
                        .map(Ieee::from_le_bytes),
                    value.get(8..24).and_then(|b| <[u8; 16]>::try_from(b).ok()),
                ) else {
                    return status::INVALID_VALUE;
                };
                self.link_keys.insert(ieee, Key(key));
                if ieee != mesh.ieee() {
                    mesh.join_keys.insert(ieee, Key(key));
                    if mesh.permit_open() {
                        mesh.schedule_joins();
                    }
                }
            }
            param::NWK_FRAME_COUNTER => {
                let Ok(bytes) = <[u8; 4]>::try_from(value) else {
                    return status::INVALID_VALUE;
                };
                if let Some(n) = mesh.network.as_mut() {
                    n.frame_counter = u32::from_le_bytes(bytes);
                }
                self.params.insert(p, value.to_vec());
            }
            param::APS_DESIGNED_COORDINATOR => {
                // No longer a coordinator, while offline: the network is
                // gone.
                if value.first() == Some(&0) && !mesh.up {
                    mesh.leave();
                }
                self.params.insert(p, value.to_vec());
            }
            param::NWK_UPDATE_ID => {
                if let (Some(n), Some(id)) = (mesh.network.as_mut(), value.first()) {
                    if mesh.up {
                        n.nwk_update_id = *id;
                    }
                }
                self.params.insert(p, value.to_vec());
            }
            _ => {
                self.params.insert(p, value.to_vec());
            }
        }
        status::SUCCESS
    }

    /// The network the parameters describe.
    fn configured(&self, mesh: &Mesh) -> Option<SimNetwork> {
        let designed = self.params.get(&param::APS_DESIGNED_COORDINATOR)?.first() == Some(&1);
        if !designed {
            return None;
        }
        let mask = u32_of(self.params.get(&param::CHANNEL_MASK)).unwrap_or(1 << 11);
        let key = self
            .params
            .get(&param::NETWORK_KEY)
            .and_then(|v| v.get(1..17))
            .and_then(|k| <[u8; 16]>::try_from(k).ok())
            .map(Key)?;
        Some(SimNetwork {
            channel: (11..=26u8).find(|c| mask & (1 << c) != 0).unwrap_or(11),
            pan_id: u16_of(self.params.get(&param::NWK_PANID)).unwrap_or(0x1A62),
            extended_pan_id: ExtPanId(
                u64_of(self.params.get(&param::APS_EXTENDED_PANID)).unwrap_or(mesh.ieee().0),
            ),
            nwk_update_id: self
                .params
                .get(&param::NWK_UPDATE_ID)
                .and_then(|v| v.first().copied())
                .unwrap_or(0),
            channel_mask: mask,
            network_key: key,
            key_sequence: 0,
            tc_root: Key([0; 16]),
            frame_counter: u32_of(self.params.get(&param::NWK_FRAME_COUNTER)).unwrap_or(0),
        })
    }

    fn change_state(&mut self, mesh: &mut Mesh, state: u8) -> u8 {
        match state {
            net_state::OFFLINE => {
                mesh.up = false;
                mesh.permit_until = None;
            }
            net_state::CONNECTED => {
                if mesh.up {
                    return status::SUCCESS;
                }
                // Parameters written since going offline form a new
                // network; unchanged ones resume the stored one.
                let wanted = self.configured(mesh);
                let same = match (&mesh.network, &wanted) {
                    (Some(n), Some(w)) => {
                        n.pan_id == w.pan_id
                            && n.extended_pan_id == w.extended_pan_id
                            && n.network_key == w.network_key
                    }
                    (Some(_), None) => true,
                    _ => false,
                };
                if same {
                    mesh.up = true;
                } else if let Some(w) = wanted {
                    mesh.leave();
                    mesh.form(w);
                } else {
                    return status::FAILURE;
                }
                // Values written before formation now belong to the network.
                self.params.remove(&param::NWK_FRAME_COUNTER);
            }
            _ => return status::INVALID_VALUE,
        }
        mesh.schedule(
            Duration::from_millis(5),
            Scheduled::Deconz(Pending::StateChanged),
        );
        status::SUCCESS
    }

    fn request(&mut self, mesh: &mut Mesh, request: Request) -> (Response, u8) {
        let ok = status::SUCCESS;
        match request {
            Request::Version => (Response::Version { version: FIRMWARE }, ok),
            Request::ReadParameter { param: p, extra } => match self.read(mesh, p, &extra) {
                Some(value) => (Response::ReadParameter { param: p, value }, ok),
                None => (
                    Response::ReadParameter {
                        param: p,
                        value: vec![],
                    },
                    status::UNSUPPORTED,
                ),
            },
            Request::WriteParameter { param: p, value } => {
                let s = self.write(mesh, p, &value);
                (Response::WriteParameter { param: p }, s)
            }
            Request::DeviceState => (
                Response::DeviceState {
                    device_state: self.state(mesh),
                },
                ok,
            ),
            Request::ChangeNetworkState { state } => {
                let s = self.change_state(mesh, state);
                (Response::ChangeNetworkState { state }, s)
            }
            Request::ApsDataRequest(a) => {
                if !mesh.up {
                    return (
                        Response::ApsDataRequest {
                            device_state: self.state(mesh),
                            request_id: a.request_id,
                        },
                        status::NO_NETWORK,
                    );
                }
                let mut delivered = true;
                match a.destination {
                    Address::Nwk(0x0000) if a.profile == 0 && a.cluster == 0x0038 => {
                        self.network_update(mesh, &a.asdu);
                    }
                    Address::Nwk(n) if n >= 0xFFF8 => {
                        if a.profile == 0 && a.cluster == 0x0038 {
                            self.network_update(mesh, &a.asdu);
                        }
                    }
                    Address::Nwk(n) | Address::NwkIeee(n, _) => {
                        delivered = mesh.unicast(
                            n,
                            &Outgoing {
                                profile: a.profile,
                                cluster: a.cluster,
                                source_endpoint: a.source_endpoint,
                                destination_endpoint: a.destination_endpoint.unwrap_or(0),
                                payload: a.asdu.clone(),
                            },
                        );
                    }
                    Address::Ieee(i) => {
                        delivered = match mesh.nwk_of(i) {
                            Some(n) => mesh.unicast(
                                n,
                                &Outgoing {
                                    profile: a.profile,
                                    cluster: a.cluster,
                                    source_endpoint: a.source_endpoint,
                                    destination_endpoint: a.destination_endpoint.unwrap_or(0),
                                    payload: a.asdu.clone(),
                                },
                            ),
                            None => false,
                        };
                    }
                    Address::Group(_) => {}
                }
                self.confirms.push_back(ApsConfirm {
                    device_state: 0,
                    request_id: a.request_id,
                    destination: a.destination,
                    destination_endpoint: a.destination_endpoint,
                    source_endpoint: a.source_endpoint,
                    status: if delivered { 0x00 } else { 0xE9 },
                });
                mesh.schedule(
                    Duration::from_millis(2),
                    Scheduled::Deconz(Pending::StateChanged),
                );
                (
                    Response::ApsDataRequest {
                        device_state: self.state(mesh),
                        request_id: a.request_id,
                    },
                    ok,
                )
            }
            Request::ApsDataConfirm => match self.confirms.pop_front() {
                Some(mut c) => {
                    c.device_state = self.state(mesh);
                    (Response::ApsDataConfirm(Box::new(c)), ok)
                }
                None => (
                    Response::DeviceState {
                        device_state: self.state(mesh),
                    },
                    status::FAILURE,
                ),
            },
            Request::ApsDataIndication => match self.indications.pop_front() {
                Some(mut i) => {
                    i.device_state = self.state(mesh);
                    (Response::ApsDataIndication(Box::new(i)), ok)
                }
                None => (
                    Response::DeviceState {
                        device_state: self.state(mesh),
                    },
                    status::FAILURE,
                ),
            },
        }
    }

    fn network_update(&mut self, mesh: &mut Mesh, asdu: &[u8]) {
        let tsn = asdu.first().copied().unwrap_or(0);
        if let Some(notify) = mesh.network_update(asdu.get(1..).unwrap_or(&[])) {
            let mut payload = vec![tsn];
            payload.extend_from_slice(&notify);
            self.indications.push_back(ApsIndication {
                device_state: 0,
                destination: Address::Nwk(0),
                destination_endpoint: 0,
                source: Address::Nwk(0),
                source_endpoint: 0,
                profile: 0,
                cluster: MGMT_NWK_UPDATE_NOTIFY,
                asdu: payload,
                lqi: Some(255),
                rssi: Some(0),
            });
        }
    }

    /// Queue what the network sent; returns whether the host should hear
    /// of a new device state.
    fn translate(&mut self, event: MeshEvent) -> bool {
        match event {
            // The firmware reports neither joins nor leaves; the host
            // learns of a device from its announce.
            MeshEvent::Joined { .. } | MeshEvent::Left { .. } => false,
            MeshEvent::Incoming {
                nwk,
                ieee,
                lqi,
                rssi,
                out,
                ..
            } => {
                self.indications.push_back(ApsIndication {
                    device_state: 0,
                    destination: Address::Nwk(0),
                    destination_endpoint: out.destination_endpoint,
                    source: Address::NwkIeee(nwk, ieee),
                    source_endpoint: out.source_endpoint,
                    profile: out.profile,
                    cluster: out.cluster,
                    asdu: out.payload,
                    lqi: Some(lqi),
                    rssi: Some(rssi),
                });
                true
            }
        }
    }
}

/// Serve one host connection over `io` until it ends.
pub(crate) async fn serve<T>(sim: &Simulator, io: T) -> Result<(), SimError>
where
    T: AsyncRead + AsyncWrite + Send + 'static,
{
    let (mut read, mut write) = tokio::io::split(io);
    let (action_tx, mut actions) = mpsc::unbounded_channel::<(Duration, Scheduled)>();
    let (due_tx, mut due) = mpsc::unbounded_channel::<Scheduled>();
    {
        let mut s = lock(&sim.state);
        let s = &mut *s;
        s.mesh.sink = Some(action_tx.clone());
        if let Some(front) = s.front.deconz() {
            front.reset(&mut s.mesh);
        }
    }
    let mut decoder = Decoder::new();
    let mut buf = [0u8; 256];
    let mut notice_seq: u8 = 0;
    let result = 'serve: loop {
        let frames: Vec<Vec<u8>> = tokio::select! {
            n = read.read(&mut buf) => {
                let n = match n {
                    Ok(0) => break Err(SimError::Closed("host closed the port".into())),
                    Ok(n) => n,
                    Err(e) => break Err(SimError::Closed(e.to_string())),
                };
                let mut out = Vec::new();
                for frame in decoder.push(buf.get(..n).unwrap_or(&[])) {
                    let Ok(frame) = frame else { continue };
                    let mut s = lock(&sim.state);
                    let s = &mut *s;
                    let Some(front) = s.front.deconz() else { continue };
                    match Request::decode(&frame) {
                        Ok(request) => {
                            tracing::trace!(?request, "sim: deconz request");
                            let (response, st) = front.request(&mut s.mesh, request);
                            // A failure echoes the command with no payload.
                            let reply = if st == status::SUCCESS {
                                response.encode(frame.seq, st)
                            } else {
                                zigbee_deconz::Frame::new(frame.command, frame.seq, st, vec![])
                            };
                            out.push(reply.encode());
                        }
                        Err(_) => out.push(
                            zigbee_deconz::Frame::new(frame.command, frame.seq, status::UNSUPPORTED, vec![]).encode(),
                        ),
                    }
                }
                out
            }
            Some((delay, action)) = actions.recv() => {
                let due_tx = due_tx.clone();
                tokio::spawn(async move {
                    tokio::time::sleep(delay).await;
                    let _ = due_tx.send(action);
                });
                continue;
            }
            Some(action) = due.recv() => {
                let mut s = lock(&sim.state);
                let s = &mut *s;
                let notify = match action {
                    Scheduled::Join(ieee) => {
                        s.mesh.join(ieee);
                        false
                    }
                    Scheduled::Remove(ieee) => {
                        s.mesh.leave_device(ieee);
                        false
                    }
                    Scheduled::Deconz(Pending::StateChanged) => true,
                    Scheduled::Mesh(event) => match s.front.deconz() {
                        Some(front) => front.translate(event),
                        None => false,
                    },
                    Scheduled::Ezsp(_) | Scheduled::Znp(_) => false,
                };
                if !notify {
                    continue;
                }
                let Some(front) = s.front.deconz() else { continue };
                notice_seq = notice_seq.wrapping_add(1);
                vec![Response::DeviceStateChanged { device_state: front.state(&s.mesh) }
                    .encode(notice_seq, status::SUCCESS)
                    .encode()]
            }
        };
        for bytes in frames {
            if write.write_all(&bytes).await.is_err() {
                break 'serve Err(SimError::Closed("write failed".into()));
            }
        }
    };
    let mut state = lock(&sim.state);
    if state
        .mesh
        .sink
        .as_ref()
        .is_some_and(|a| a.same_channel(&action_tx))
    {
        state.mesh.sink = None;
    }
    result
}