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
//!
//! ## Integration test of `AWS IoT Shadows`
//!
//!
//! This test simulates updates of the shadow state from both device side &
//! cloud side. Cloud side updates are done by publishing directly to the shadow
//! topics, and ignoring the resulting update accepted response. Device side
//! updates are done through the shadow API provided by this crate.
//!
//! The test runs through the following update sequence:
//! 1. Setup clean starting point (`desired = null, reported = null`)
//! 2. Do a `GetShadow` request to sync empty state
//! 3. Update to initial shadow state from the device
//! 4. Assert on the initial state
//! 5. Update state from device
//! 6. Assert on shadow state
//! 7. Update state from cloud
//! 8. Assert on shadow state
//! 9. Update state from device
//! 10. Assert on shadow state
//! 11. Update state from cloud
//! 12. Assert on shadow state
//!

mod common;

use core::fmt::Write;

use common::{clock::SysClock, credentials, network::Network};
use embedded_nal::Ipv4Addr;
use mqttrust::Mqtt;
use mqttrust_core::{bbqueue::BBBuffer, EventLoop, MqttOptions, Notification};
use native_tls::TlsConnector;
use rustot::shadows::{
    derive::ShadowState, topics::Topic, Patch, Shadow, ShadowPatch, ShadowState,
};
use serde::{de::DeserializeOwned, Deserialize, Serialize};

use smlang::statemachine;

const Q_SIZE: usize = 1024 * 6;
static mut Q: BBBuffer<Q_SIZE> = BBBuffer::new();

#[derive(Debug, Clone, Default, PartialEq, Eq, PartialOrd, Ord)]
pub struct ConfigId(pub u8);

impl Serialize for ConfigId {
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: serde::Serializer,
    {
        let mut str = heapless::String::<3>::new();
        write!(str, "{}", self.0).map_err(serde::ser::Error::custom)?;
        serializer.serialize_str(&str)
    }
}

impl<'de> Deserialize<'de> for ConfigId {
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where
        D: serde::Deserializer<'de>,
    {
        heapless::String::<3>::deserialize(deserializer)?
            .parse()
            .map(ConfigId)
            .map_err(serde::de::Error::custom)
    }
}

impl From<u8> for ConfigId {
    fn from(v: u8) -> Self {
        Self(v)
    }
}

#[derive(Debug, Clone, Default, PartialEq, Serialize, Deserialize)]
pub struct NetworkMap<K: Eq, V, const N: usize>(heapless::LinearMap<K, Option<Patch<V>>, N>);

impl<K, V, const N: usize> NetworkMap<K, V, N>
where
    K: Eq,
{
    pub fn insert(&mut self, k: impl Into<K>, v: V) -> Result<(), ()> {
        self.0.insert(k.into(), Some(Patch::Set(v))).map_err(drop)?;
        Ok(())
    }

    pub fn remove(&mut self, k: impl Into<K>) -> Result<(), ()> {
        self.0.insert(k.into(), None).map_err(drop)?;
        Ok(())
    }
}

impl<K, V, const N: usize> ShadowPatch for NetworkMap<K, V, N>
where
    K: Clone + Default + Eq + Serialize + DeserializeOwned,
    V: Clone + Default + Serialize + DeserializeOwned,
{
    type PatchState = NetworkMap<K, V, N>;

    fn apply_patch(&mut self, opt: Self::PatchState) {
        for (id, network) in opt.0.into_iter() {
            match network {
                Some(Patch::Set(v)) => {
                    self.insert(id.clone(), v.clone()).ok();
                }
                None | Some(Patch::Unset) => {
                    self.remove(id.clone()).ok();
                }
            }
        }
    }
}

const MAX_NETWORKS: usize = 5;
type KnownNetworks = NetworkMap<ConfigId, ConnectionOptions, MAX_NETWORKS>;

#[derive(Debug, Clone, Default, Serialize, Deserialize, ShadowState)]
#[shadow("wifi")]
pub struct WifiConfig {
    pub enabled: bool,

    pub known_networks: KnownNetworks,
}

#[derive(Debug, Clone, Default, PartialEq, Serialize, Deserialize)]
pub struct ConnectionOptions {
    pub ssid: heapless::String<64>,
    pub password: Option<heapless::String<64>>,

    pub ip: Option<Ipv4Addr>,
    pub subnet: Option<Ipv4Addr>,
    pub gateway: Option<Ipv4Addr>,
}

#[derive(Debug, Clone)]
pub enum UpdateAction {
    Insert(u8, ConnectionOptions),
    Remove(u8),
    Enabled(bool),
}

statemachine! {
    transitions: {
        *Begin + Delete = DeleteShadow,
        DeleteShadow + Get = GetShadow,
        GetShadow + Load / load_initial = LoadShadow(Option<KnownNetworks>),
        LoadShadow(Option<KnownNetworks>) + CheckInitial / check_initial = Check(Option<KnownNetworks>),
        UpdateFromDevice(UpdateAction) + CheckState / check = Check(Option<KnownNetworks>),
        UpdateFromCloud(UpdateAction) + Ack = AckUpdate,
        AckUpdate + CheckState / check_cloud = Check(Option<KnownNetworks>),
        Check(Option<KnownNetworks>) + UpdateStateFromDevice / get_next_device = UpdateFromDevice(UpdateAction),
        Check(Option<KnownNetworks>) + UpdateStateFromCloud / get_next_cloud = UpdateFromCloud(UpdateAction),
    }
}

impl core::fmt::Debug for States {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Self::Begin => write!(f, "Self::Begin"),
            Self::DeleteShadow => write!(f, "Self::DeleteShadow"),
            Self::GetShadow => write!(f, "Self::GetShadow"),
            Self::AckUpdate => write!(f, "Self::AckUpdate"),
            Self::LoadShadow(t) => write!(f, "Self::LoadShadow({:?})", t),
            Self::UpdateFromDevice(t) => write!(f, "Self::UpdateFromDevice({:?})", t),
            Self::UpdateFromCloud(t) => write!(f, "Self::UpdateFromCloud({:?})", t),
            Self::Check(t) => write!(f, "Self::Check({:?})", t),
        }
    }
}

fn asserts(id: usize) -> ConnectionOptions {
    match id {
        0 => ConnectionOptions {
            ssid: heapless::String::from("MySSID"),
            password: None,
            ip: None,
            subnet: None,
            gateway: None,
        },
        1 => ConnectionOptions {
            ssid: heapless::String::from("MyProtectedSSID"),
            password: Some(heapless::String::from("SecretPass")),
            ip: None,
            subnet: None,
            gateway: None,
        },
        2 => ConnectionOptions {
            ssid: heapless::String::from("CloudSSID"),
            password: Some(heapless::String::from("SecretCloudPass")),
            ip: Some(Ipv4Addr::new(1, 2, 3, 4)),
            subnet: None,
            gateway: None,
        },
        _ => panic!("Unknown assert ID"),
    }
}

pub struct TestContext<'a> {
    shadow: Shadow<'a, WifiConfig, mqttrust_core::Client<'static, 'static, Q_SIZE>>,
    update_cnt: u8,
}

impl<'a> StateMachineContext for TestContext<'a> {
    fn check_initial(
        &mut self,
        _last_update_action: &Option<KnownNetworks>,
    ) -> Option<KnownNetworks> {
        self.check(&UpdateAction::Remove(0))
    }

    fn check_cloud(&mut self) -> Option<KnownNetworks> {
        self.check(&UpdateAction::Remove(0))
    }

    fn check(&mut self, _last_update_action: &UpdateAction) -> Option<KnownNetworks> {
        let mut known_networks = KnownNetworks::default();

        match self.update_cnt {
            0 => {
                // After load_initial
                known_networks.insert(0, asserts(0)).unwrap();
                known_networks.insert(1, asserts(1)).unwrap();
            }
            1 => {
                // After get_next_device
                known_networks.remove(0).unwrap();
                known_networks.insert(1, asserts(1)).unwrap();
            }
            2 => {
                // After get_next_cloud
                known_networks.remove(0).unwrap();
                known_networks.insert(1, asserts(1)).unwrap();
                known_networks.insert(2, asserts(2)).unwrap();
            }
            3 => {
                // After get_next_device
                known_networks.insert(0, asserts(0)).unwrap();
                known_networks.insert(1, asserts(1)).unwrap();
                known_networks.insert(2, asserts(2)).unwrap();
            }
            4 => {
                // After get_next_cloud
                known_networks.insert(0, asserts(0)).unwrap();
                known_networks.insert(1, asserts(1)).unwrap();
                known_networks.remove(2).unwrap();
            }
            5 => return None,
            _ => {}
        }

        Some(known_networks)
    }

    fn get_next_device(&mut self, _: &Option<KnownNetworks>) -> UpdateAction {
        self.update_cnt += 1;
        match self.update_cnt {
            1 => UpdateAction::Remove(0),
            3 => UpdateAction::Insert(0, asserts(0)),
            5 => UpdateAction::Remove(0),
            _ => panic!("Unexpected update counter in `get_next_device`"),
        }
    }

    fn get_next_cloud(&mut self, _: &Option<KnownNetworks>) -> UpdateAction {
        self.update_cnt += 1;

        match self.update_cnt {
            2 => UpdateAction::Insert(2, asserts(2)),
            4 => UpdateAction::Remove(2),
            _ => panic!("Unexpected update counter in `get_next_cloud`"),
        }
    }

    fn load_initial(&mut self) -> Option<KnownNetworks> {
        let mut known_networks = KnownNetworks::default();
        known_networks.insert(0, asserts(0)).unwrap();
        known_networks.insert(1, asserts(1)).unwrap();
        Some(known_networks)
    }
}

impl<'a> StateMachine<TestContext<'a>> {
    pub fn spin(
        &mut self,
        notification: Notification,
        mqtt_client: &mqttrust_core::Client<'static, 'static, Q_SIZE>,
    ) -> bool {
        log::info!("State: {:?}", self.state());
        match (self.state(), notification) {
            (&States::Begin, Notification::Suback(_)) => {
                self.process_event(Events::Delete).unwrap();
            }
            (&States::DeleteShadow, Notification::Suback(_)) => {
                mqtt_client
                    .publish(
                        &Topic::Update
                            .format::<128>(
                                mqtt_client.client_id(),
                                <WifiConfig as ShadowState>::NAME,
                            )
                            .unwrap(),
                        b"{\"state\":{\"desired\":null,\"reported\":null}}",
                        mqttrust::QoS::AtLeastOnce,
                    )
                    .unwrap();

                self.process_event(Events::Get).unwrap();
            }
            (&States::GetShadow, Notification::Publish(publish))
                if matches!(
                    publish.topic_name.as_str(),
                    "$aws/things/rustot-test/shadow/name/wifi/update/accepted"
                ) =>
            {
                self.context_mut().shadow.get_shadow().unwrap();
                self.process_event(Events::Load).unwrap();
            }
            (&States::LoadShadow(ref initial_map), Notification::Publish(publish))
                if matches!(
                    publish.topic_name.as_str(),
                    "$aws/things/rustot-test/shadow/name/wifi/get/accepted"
                ) =>
            {
                let initial_map = initial_map.clone();

                self.context_mut()
                    .shadow
                    .update(|_current, desired| {
                        desired.known_networks = Some(initial_map.unwrap());
                    })
                    .unwrap();
                self.process_event(Events::CheckInitial).unwrap();
            }
            (&States::UpdateFromDevice(ref update_action), Notification::Publish(publish))
                if matches!(
                    publish.topic_name.as_str(),
                    "$aws/things/rustot-test/shadow/name/wifi/get/accepted"
                ) =>
            {
                let action = update_action.clone();
                self.context_mut()
                    .shadow
                    .update(|current, desired| match action {
                        UpdateAction::Insert(id, options) => {
                            let mut desired_map = current.known_networks.clone();
                            desired_map.insert(id, options).unwrap();
                            desired.known_networks = Some(desired_map);
                        }
                        UpdateAction::Remove(id) => {
                            let mut desired_map = current.known_networks.clone();
                            desired_map.remove(id).unwrap();
                            desired.known_networks = Some(desired_map);
                        }
                        UpdateAction::Enabled(en) => {
                            desired.enabled = Some(en);
                        }
                    })
                    .unwrap();
                self.process_event(Events::CheckState).unwrap();
            }
            (&States::UpdateFromCloud(ref update_action), Notification::Publish(publish))
                if matches!(
                    publish.topic_name.as_str(),
                    "$aws/things/rustot-test/shadow/name/wifi/get/accepted"
                ) =>
            {
                let desired_known_networks = match update_action {
                    UpdateAction::Insert(id, options) => format!(
                        "\"known_networks\": {{\"{}\":{{\"set\":{}}}}}",
                        id,
                        serde_json_core::to_string::<_, 256>(options).unwrap()
                    ),
                    UpdateAction::Remove(id) => {
                        format!("\"known_networks\": {{\"{}\":\"unset\"}}", id)
                    }
                    &UpdateAction::Enabled(en) => format!("\"enabled\": {}", en),
                };

                let payload = format!(
                    "{{\"state\":{{\"desired\":{{{}}}, \"reported\":{}}}}}",
                    desired_known_networks,
                    serde_json_core::to_string::<_, 512>(self.context().shadow.get()).unwrap()
                );

                log::debug!("Update from cloud: {:?}", payload);

                mqtt_client
                    .publish(
                        &Topic::Update
                            .format::<128>(
                                mqtt_client.client_id(),
                                <WifiConfig as ShadowState>::NAME,
                            )
                            .unwrap(),
                        payload.as_bytes(),
                        mqttrust::QoS::AtLeastOnce,
                    )
                    .unwrap();
                self.process_event(Events::Ack).unwrap();
            }
            (&States::AckUpdate, Notification::Publish(publish))
                if matches!(
                    publish.topic_name.as_str(),
                    "$aws/things/rustot-test/shadow/name/wifi/update/delta"
                ) =>
            {
                self.context_mut()
                    .shadow
                    .handle_message(&publish.topic_name, &publish.payload)
                    .unwrap();

                self.process_event(Events::CheckState).unwrap();
            }
            (&States::Check(ref expected_map), Notification::Publish(publish))
                if matches!(
                    publish.topic_name.as_str(),
                    "$aws/things/rustot-test/shadow/name/wifi/update/accepted"
                        | "$aws/things/rustot-test/shadow/name/wifi/update/delta"
                ) =>
            {
                let expected = expected_map.clone();
                self.context_mut()
                    .shadow
                    .handle_message(&publish.topic_name, &publish.payload)
                    .unwrap();

                match expected {
                    Some(expected_map) => {
                        assert_eq!(self.context().shadow.get().known_networks, expected_map);
                        self.context_mut().shadow.get_shadow().unwrap();
                        let event = if self.context().update_cnt % 2 == 0 {
                            Events::UpdateStateFromDevice
                        } else {
                            Events::UpdateStateFromCloud
                        };
                        self.process_event(event).unwrap();
                    }
                    None => return true,
                }
            }
            (_, Notification::Publish(publish)) => {
                log::warn!("TOPIC: {}", publish.topic_name);
                self.context_mut()
                    .shadow
                    .handle_message(&publish.topic_name, &publish.payload)
                    .unwrap();
            }
            _ => {}
        }

        false
    }
}

#[test]
fn test_shadows() {
    env_logger::init();

    let (p, c) = unsafe { Q.try_split_framed().unwrap() };

    log::info!("Starting shadows test...");

    let hostname = credentials::HOSTNAME.unwrap();
    let (thing_name, identity) = credentials::identity();

    let connector = TlsConnector::builder()
        .identity(identity)
        .add_root_certificate(credentials::root_ca())
        .build()
        .unwrap();

    let mut network = Network::new_tls(connector, std::string::String::from(hostname));

    let mut mqtt_eventloop = EventLoop::new(
        c,
        SysClock::new(),
        MqttOptions::new(thing_name, hostname.into(), 8883).set_clean_session(true),
    );

    let mqtt_client = mqttrust_core::Client::new(p, thing_name);

    let mut test_state = StateMachine::new(TestContext {
        shadow: Shadow::new(WifiConfig::default(), &mqtt_client, true).unwrap(),
        update_cnt: 0,
    });

    loop {
        if nb::block!(mqtt_eventloop.connect(&mut network)).expect("to connect to mqtt") {
            log::info!("Successfully connected to broker");
        }

        match mqtt_eventloop.yield_event(&mut network) {
            Ok(notification) => {
                if test_state.spin(notification, &mqtt_client) {
                    break;
                }
            }
            Err(_) => {}
        }
    }
}