firmware-controller 0.5.0

Controller (actor) macro to decouple interactions between components, supporting both embassy (no_std) and tokio (std) backends.
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
use firmware_controller::controller;
use futures::StreamExt;

#[derive(Debug, PartialEq, Copy, Clone)]
pub enum State {
    Idle,
    Active,
    Error,
}

#[derive(Debug, PartialEq, Copy, Clone)]
pub enum Mode {
    Normal,
    Debug,
}

#[derive(Debug, PartialEq)]
pub enum TestError {
    InvalidState,
    OperationFailed,
}

#[controller]
mod test_controller {
    use super::*;

    pub struct Controller {
        #[controller(publish, getter = "get_current_state", setter = "change_state")]
        state: State,
        #[controller(publish, getter, setter)]
        mode: Mode,
        #[controller(setter)]
        counter: u32,
    }

    impl Controller {
        #[controller(signal)]
        pub async fn error_occurred(&self, code: u32, message: heapless::String<32>);

        #[controller(signal)]
        pub async fn operation_complete(&self);

        pub async fn increment(&mut self) -> u32 {
            self.counter += 1;
            self.counter
        }

        pub async fn get_counter(&self) -> u32 {
            self.counter
        }

        pub async fn activate(&mut self) -> Result<(), TestError> {
            if self.state != State::Idle {
                return Err(TestError::InvalidState);
            }
            self.set_state(State::Active).await;
            self.operation_complete().await;
            Ok(())
        }

        pub async fn trigger_error(&mut self) -> Result<(), TestError> {
            self.set_state(State::Error).await;
            self.error_occurred(42, "Test error".try_into().unwrap())
                .await;
            Err(TestError::OperationFailed)
        }

        pub async fn return_nothing(&self) {}
    }
}

use test_controller::*;

#[cfg(feature = "embassy")]
#[test]
fn test_controller_basic_functionality() {
    let controller = Controller::new(State::Idle, Mode::Normal, 0).unwrap();

    std::thread::spawn(move || {
        let executor = Box::leak(Box::new(embassy_executor::Executor::new()));
        executor.run(move |spawner| {
            spawner.spawn(controller_embassy_task(controller)).unwrap();
        });
    });

    futures::executor::block_on(async {
        run_basic_test().await;
    });
}

#[cfg(feature = "tokio")]
#[tokio::test]
async fn test_controller_basic_functionality() {
    let controller = Controller::new(State::Idle, Mode::Normal, 0).unwrap();
    tokio::spawn(controller_task(controller));
    tokio::task::yield_now().await;

    run_basic_test().await;
}

async fn run_basic_test() {
    let mut client = ControllerClient::new();

    // Test 1: Subscribe to state changes.
    let mut state_stream = client.receive_state_changed().expect("Failed to subscribe");

    // Test 1a: First poll returns the initial (current) value.
    let initial_state = state_stream
        .next()
        .await
        .expect("Should receive initial state");
    assert_eq!(initial_state, State::Idle, "Initial state should be Idle");

    // Test 2: Subscribe to signals.
    let mut error_stream = client
        .receive_error_occurred()
        .expect("Failed to subscribe to error");
    let mut complete_stream = client
        .receive_operation_complete()
        .expect("Failed to subscribe to complete");

    // Test 3: Call a method and verify return value.
    let counter = client.get_counter().await;
    assert_eq!(counter, 0, "Initial counter should be 0");

    // Test 4: Call increment and verify it increases.
    let counter = client.increment().await;
    assert_eq!(counter, 1, "Counter should be 1 after increment");

    let counter = client.increment().await;
    assert_eq!(counter, 2, "Counter should be 2 after second increment");

    // Test 5: Call method that changes state and emits signal.
    let activate_result = client.activate().await;
    assert!(
        activate_result.is_ok(),
        "Activate should succeed from Idle state"
    );

    // Verify we received the state change.
    let new_state = state_stream
        .next()
        .await
        .expect("Should receive state change");
    assert_eq!(new_state, State::Active, "New state should be Active");

    // Verify we received the operation_complete signal.
    let _complete = complete_stream
        .next()
        .await
        .expect("Should receive operation complete signal");

    // Test 6: Call method that returns error.
    let error_result = client.trigger_error().await;
    assert!(
        error_result.is_err(),
        "trigger_error should return an error"
    );
    assert_eq!(
        error_result.unwrap_err(),
        TestError::OperationFailed,
        "Should return OperationFailed error"
    );

    // Verify state changed to Error.
    let new_state = state_stream
        .next()
        .await
        .expect("Should receive state change");
    assert_eq!(new_state, State::Error, "New state should be Error");

    // Verify we received the error signal.
    let error_signal = error_stream
        .next()
        .await
        .expect("Should receive error signal");
    assert_eq!(error_signal.code, 42, "Error code should be 42");
    assert_eq!(
        error_signal.message.as_str(),
        "Test error",
        "Error message should match"
    );

    // Test 7: Try to activate again (should fail due to invalid state).
    let activate_result = client.activate().await;
    assert!(
        activate_result.is_err(),
        "Activate should fail from Error state"
    );
    assert_eq!(
        activate_result.unwrap_err(),
        TestError::InvalidState,
        "Should return InvalidState error"
    );

    // Test 8: Use setter to change mode.
    client.set_mode(Mode::Debug).await;

    // Test 9: Call method with no return value.
    client.return_nothing().await;

    // Test 10: Use getter with custom name to get state.
    let state = client.get_current_state().await;
    assert_eq!(state, State::Error, "State should be Error");

    // Test 11: Use getter with default field name to get mode.
    let mode = client.mode().await;
    assert_eq!(mode, Mode::Debug, "Mode should be Debug");

    // Test 12: Use setter with custom name (new syntax).
    client.change_state(State::Idle).await;
    let state = client.get_current_state().await;
    assert_eq!(
        state,
        State::Idle,
        "State should be Idle after change_state"
    );

    // Test 13: Use setter without publish (independent setter).
    client.set_counter(100).await;
    let counter = client.get_counter().await;
    assert_eq!(counter, 100, "Counter should be 100 after set_counter");
}

#[cfg(feature = "tokio")]
async fn controller_task(controller: Controller) {
    controller.run().await;
}

#[cfg(feature = "embassy")]
#[embassy_executor::task]
async fn controller_embassy_task(controller: Controller) {
    controller.run().await;
}

/// Test that visibility specifiers on struct fields are preserved.
#[controller]
mod visibility_test_controller {
    pub struct Controller {
        #[controller(getter)]
        pub public_field: u32,
        #[controller(getter)]
        pub(crate) crate_field: i32,
        #[controller(getter)]
        private_field: bool,
    }

    impl Controller {}
}

#[cfg(feature = "embassy")]
#[test]
fn test_visibility_on_fields() {
    let controller = visibility_test_controller::Controller::new(42, -1, true).unwrap();
    assert_eq!(controller.public_field, 42);
    assert_eq!(controller.crate_field, -1);

    std::thread::spawn(move || {
        let executor = Box::leak(Box::new(embassy_executor::Executor::new()));
        executor.run(move |spawner| {
            spawner
                .spawn(visibility_controller_task(controller))
                .unwrap();
        });
    });

    futures::executor::block_on(async {
        run_visibility_test().await;
    });
}

#[cfg(feature = "tokio")]
#[tokio::test]
async fn test_visibility_on_fields() {
    let controller = visibility_test_controller::Controller::new(42, -1, true).unwrap();
    assert_eq!(controller.public_field, 42);
    assert_eq!(controller.crate_field, -1);

    tokio::spawn(async move { controller.run().await });
    tokio::task::yield_now().await;

    run_visibility_test().await;
}

async fn run_visibility_test() {
    let client = visibility_test_controller::ControllerClient::new();
    assert_eq!(client.public_field().await, 42);
    assert_eq!(client.crate_field().await, -1);
    assert_eq!(client.private_field().await, true);
}

#[cfg(feature = "embassy")]
#[embassy_executor::task]
async fn visibility_controller_task(controller: visibility_test_controller::Controller) {
    controller.run().await;
}

use std::sync::atomic::{AtomicU32, Ordering};

static POLL_A_COUNT: AtomicU32 = AtomicU32::new(0);
static POLL_B_COUNT: AtomicU32 = AtomicU32::new(0);
static POLL_C_COUNT: AtomicU32 = AtomicU32::new(0);

/// Test poll methods with timeouts.
#[controller]
mod poll_test_controller {
    use super::*;

    pub struct Controller {
        #[controller(getter)]
        pub value: u32,
    }

    impl Controller {
        // Two methods with the same poll interval (50ms) - should be grouped.
        #[controller(poll_millis = 50)]
        pub async fn poll_a(&mut self) {
            POLL_A_COUNT.fetch_add(1, Ordering::SeqCst);
        }

        #[controller(poll_millis = 50)]
        pub async fn poll_b(&mut self) {
            POLL_B_COUNT.fetch_add(1, Ordering::SeqCst);
        }

        // Different poll interval (100ms).
        #[controller(poll_millis = 100)]
        pub async fn poll_c(&mut self) {
            POLL_C_COUNT.fetch_add(1, Ordering::SeqCst);
        }
    }
}

/// Test that poll methods are called at the expected intervals.
#[cfg(feature = "embassy")]
#[test]
fn poll_methods() {
    use embassy_time::{Duration, MockDriver};

    let driver = MockDriver::get();
    driver.reset();
    POLL_A_COUNT.store(0, Ordering::SeqCst);
    POLL_B_COUNT.store(0, Ordering::SeqCst);
    POLL_C_COUNT.store(0, Ordering::SeqCst);

    let controller = poll_test_controller::Controller::new(42).unwrap();
    assert_eq!(controller.value, 42);

    std::thread::spawn(move || {
        let executor = Box::leak(Box::new(embassy_executor::Executor::new()));
        executor.run(move |spawner| {
            spawner.spawn(poll_controller_task(controller)).unwrap();
        });
    });

    // Give the executor a moment to start.
    std::thread::sleep(std::time::Duration::from_millis(10));

    futures::executor::block_on(run_poll_test(|millis| async move {
        driver.advance(Duration::from_millis(millis));
        std::thread::sleep(std::time::Duration::from_millis(10));
    }));
}

#[cfg(feature = "tokio")]
#[tokio::test(start_paused = true)]
async fn poll_methods() {
    POLL_A_COUNT.store(0, Ordering::SeqCst);
    POLL_B_COUNT.store(0, Ordering::SeqCst);
    POLL_C_COUNT.store(0, Ordering::SeqCst);

    let controller = poll_test_controller::Controller::new(42).unwrap();
    assert_eq!(controller.value, 42);

    tokio::spawn(async move { controller.run().await });

    // Yield to let the controller task start and skip the initial ticks.
    for _ in 0..10 {
        tokio::task::yield_now().await;
    }

    run_poll_test(|millis| async move {
        tokio::time::advance(std::time::Duration::from_millis(millis)).await;
        for _ in 0..10 {
            tokio::task::yield_now().await;
        }
    })
    .await;
}

async fn run_poll_test<F, Fut>(advance_and_settle: F)
where
    F: Fn(u64) -> Fut,
    Fut: core::future::Future<Output = ()>,
{
    // Advance 50ms - poll_a and poll_b should fire once.
    advance_and_settle(50).await;
    assert_poll_counts(1, 1, 0);

    // Advance another 50ms (total 100ms).
    advance_and_settle(50).await;
    assert_poll_counts(2, 2, 1);

    // Advance another 100ms (total 200ms).
    advance_and_settle(100).await;
    assert_poll_counts(4, 4, 2);
}

fn assert_poll_counts(a: u32, b: u32, c: u32) {
    assert_eq!(POLL_A_COUNT.load(Ordering::SeqCst), a);
    assert_eq!(POLL_B_COUNT.load(Ordering::SeqCst), b);
    assert_eq!(POLL_C_COUNT.load(Ordering::SeqCst), c);
}

#[cfg(feature = "embassy")]
#[embassy_executor::task]
async fn poll_controller_task(controller: poll_test_controller::Controller) {
    controller.run().await;
}