flipperzero 0.16.0

Rust for Flipper Zero
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
//! Furi Thread API.

use core::time;
#[cfg(feature = "alloc")]
use core::{
    ffi::{CStr, c_void},
    fmt,
    ptr::NonNull,
    str,
};

#[cfg(feature = "alloc")]
use alloc::{
    boxed::Box,
    ffi::{CString, NulError},
    string::String,
    sync::Arc,
};

use flipperzero_sys::{self as sys, FuriFlagNoClear, FuriFlagWaitAll, FuriFlagWaitAny, HasFlag};

use crate::furi::time::FuriDuration;

#[cfg(feature = "alloc")]
const MIN_STACK_SIZE: usize = 1024;

/// Thread factory, which can be used in order to configure the properties of a new thread.
#[cfg(feature = "alloc")]
#[cfg_attr(docsrs, doc(cfg(feature = "alloc")))]
pub struct Builder {
    /// Guaranteed to be UTF-8.
    name: Option<CString>,
    stack_size: Option<usize>,
    heap_trace_enabled: Option<bool>,
}

#[cfg(feature = "alloc")]
impl Default for Builder {
    fn default() -> Self {
        Self::new()
    }
}

#[cfg(feature = "alloc")]
impl Builder {
    /// Generates the base configuration for spawning a thread, from which configuration
    /// methods can be chained.
    pub fn new() -> Self {
        Self {
            name: None,
            stack_size: None,
            heap_trace_enabled: None,
        }
    }

    /// Names the thread-to-be.
    ///
    /// Returns an error if the name contains null bytes (`\0`).
    pub fn name(mut self, name: String) -> Result<Self, NulError> {
        CString::new(name).map(|name| {
            self.name = Some(name);
            self
        })
    }

    /// Sets the size of the stack (in bytes) for the new thread.
    pub fn stack_size(mut self, size: usize) -> Self {
        self.stack_size = Some(size);
        self
    }

    /// Enables heap tracing.
    ///
    /// By default, heap tracing is enabled if the Flipper Zero's "heap track mode" is
    /// either set to "All", or set to "Tree" and the parent's heap tracing is enabled.
    pub fn enable_heap_trace(mut self) -> Self {
        self.heap_trace_enabled = Some(true);
        self
    }

    /// Spawns a new thread by taking ownership of the `Builder`, and returns its
    /// [`JoinHandle`].
    pub fn spawn<F>(self, f: F) -> JoinHandle
    where
        F: FnOnce() -> i32,
        F: Send + 'static,
    {
        let Builder {
            name,
            stack_size,
            heap_trace_enabled,
        } = self;
        #[allow(clippy::arc_with_non_send_sync)] // TODO: is using `Arc` neccessary/sound here?
        let thread = Arc::new(Thread::new(name, stack_size, heap_trace_enabled));

        // We need to box twice because trait objects are fat pointers, so we need the
        // second box to obtain a thin pointer to use as the context.
        type ThreadBody = Box<dyn FnOnce() -> i32>;
        let thread_body: Box<ThreadBody> = Box::new(Box::new(f));
        unsafe extern "C" fn run_thread_body(context: *mut c_void) -> i32 {
            let thread_body = unsafe { Box::from_raw(context as *mut ThreadBody) };
            thread_body()
        }
        let callback: sys::FuriThreadCallback = Some(run_thread_body);
        let context = Box::into_raw(thread_body);

        unsafe extern "C" fn run_state_callback(
            _thread: *mut sys::FuriThread,
            state: sys::FuriThreadState,
            context: *mut c_void,
        ) {
            if state == sys::FuriThreadStateStopped {
                // SAFETY: We can drop the `Arc<Thread>` at the end of this function call,
                // because:
                // - `FuriThreadStateStopped` only occurs once.
                // - `FuriThreadStateStopped` is always the final state.
                let context = unsafe { Arc::from_raw(context as *mut Thread) };

                if let Some(thread) = Arc::into_inner(context) {
                    // SAFETY: No `Thread` instances exist at this point:
                    // - `JoinHandle` isn't Clone, and the one inside `JoinHandle` has
                    //   been dropped (because we were able to successfully extract the
                    //   `Thread` from the `Arc`).
                    // - Any obtained via `thread::current()` were dropped before the
                    //   thread stopped, because `Thread` isn't Send.
                    //
                    // Only two other pointers to the thread remain, and neither are read
                    // after this callback exits:
                    // - The one inside `furi_thread_body`.
                    // - The one inside the thread's local storage.
                    unsafe { sys::furi_thread_free(thread.thread.as_ptr()) };
                }
            }
        }
        let state_callback: sys::FuriThreadStateCallback = Some(run_state_callback);
        let state_context = Arc::into_raw(thread.clone());

        unsafe {
            sys::furi_thread_set_callback(thread.thread.as_ptr(), callback);
            sys::furi_thread_set_context(thread.thread.as_ptr(), context as *mut c_void);
            sys::furi_thread_set_state_callback(thread.thread.as_ptr(), state_callback);
            sys::furi_thread_set_state_context(
                thread.thread.as_ptr(),
                state_context as *mut c_void,
            );
            sys::furi_thread_start(thread.thread.as_ptr());
        }

        JoinHandle {
            context: Some(thread),
        }
    }
}

/// Spawns a new thread, returning a [`JoinHandle`] for it.
///
/// This call will create a thread using default parameters of [`Builder`]. If you want to
/// specify the stack size or the name of the thread, use that API instead.
#[cfg(feature = "alloc")]
#[cfg_attr(docsrs, doc(cfg(feature = "alloc")))]
pub fn spawn<F>(f: F) -> JoinHandle
where
    F: FnOnce() -> i32,
    F: Send + 'static,
{
    Builder::new().spawn(f)
}

/// Gets a handle to the thread that invokes it.
#[cfg(feature = "alloc")]
#[cfg_attr(docsrs, doc(cfg(feature = "alloc")))]
pub fn current() -> Thread {
    use alloc::borrow::ToOwned;

    let thread = NonNull::new(unsafe { sys::furi_thread_get_current() })
        .expect("furi_thread_get_current shouldn't return null");

    let name = {
        let name = unsafe { sys::furi_thread_get_name(sys::furi_thread_get_current_id()) };
        (!name.is_null())
            .then(|| {
                // SAFETY: The Flipper Zero firmware ensures that all thread names have a
                // null terminator.
                unsafe { CStr::from_ptr(name) }.to_owned()
            })
            .and_then(|name| {
                // Ensure that the name is valid UTF-8. This will be true for threads
                // created via `Builder`, but may not be true for the current thread.
                name.to_str().is_ok().then_some(name)
            })
    };

    Thread { name, thread }
}

/// Cooperatively gives up a timeslice to the OS scheduler.
pub fn yield_now() {
    unsafe { sys::furi_thread_yield() };
}

/// Puts the current thread to sleep for at least `duration`.
///
/// Durations under 1 hour are accurate to microseconds, while durations of
/// 1 hour or more are only accurate to milliseconds.
///
/// Will panic if requested to sleep for durations more than `2^32` microseconds (~49 days).
///
/// See [`sleep_ticks`] to sleep based on system timer ticks.
pub fn sleep(duration: core::time::Duration) {
    if duration > time::Duration::from_millis(u32::MAX as u64) {
        panic!("sleep exceeds maximum supported duration")
    }

    unsafe {
        // For durations of 1h+, use delay_ms so uint32_t doesn't overflow
        if duration < time::Duration::from_secs(3600) {
            sys::furi_delay_us(duration.as_micros() as u32);
        } else {
            sys::furi_delay_ms(duration.as_millis() as u32);
        }
    }
}

/// Puts the current thread to sleep for at least `duration`.
///
/// The maximum supported duration is `2^32` ticks (system timer dependent).
///
/// See [`sleep`] to sleep based on arbitary duration.
pub fn sleep_ticks(duration: FuriDuration) {
    unsafe {
        sys::furi_delay_tick(duration.as_ticks());
    }
}

/// A unique identifier for a running thread.
#[derive(Copy, Clone, PartialEq, Eq)]
#[repr(transparent)]
pub struct ThreadId(sys::FuriThreadId);

impl ThreadId {
    /// Get the `ThreadId` for the current thread.
    pub fn current() -> Self {
        ThreadId(unsafe { sys::furi_thread_get_current_id() })
    }

    /// Get the `ThreadId` for a specific C `FuriThread`.
    ///
    /// # Safety
    ///
    /// The thread pointer must be non-null and point to a valid `FuriThread`.
    pub unsafe fn from_furi_thread(thread: *mut sys::FuriThread) -> ThreadId {
        ThreadId(unsafe { sys::furi_thread_get_id(thread) })
    }
}

/// Set one-or-more notification flags on a thread.
///
/// Returns the value of the thread's notification flags after the specified `flags` have been set.
pub fn set_flags(thread_id: ThreadId, flags: u32) -> Result<u32, sys::furi::Status> {
    let result = unsafe { sys::furi_thread_flags_set(thread_id.0, flags) };

    if sys::FuriFlag(result).has_flag(sys::FuriFlagError) {
        return Err((result as i32).into());
    }

    Ok(result)
}

/// Clear one-or-more of the current thread's notification flags.
///
/// Returns the value of the current thread's notification flags after the specified `flags` have been cleared.
pub fn clear_flags(flags: u32) -> Result<u32, sys::furi::Status> {
    let result = unsafe { sys::furi_thread_flags_clear(flags) };

    if sys::FuriFlag(result).has_flag(sys::FuriFlagError) {
        return Err((result as i32).into());
    }

    Ok(result)
}

/// Get the value of the current thread's notification flags.
pub fn get_flags() -> Result<u32, sys::furi::Status> {
    let result = unsafe { sys::furi_thread_flags_get() };

    if sys::FuriFlag(result).has_flag(sys::FuriFlagError) {
        return Err((result as i32).into());
    }

    Ok(result)
}

/// Wait for up-to `timeout` for a change to any of the specified notification `flags` for the current thread.
///
/// If `clear`, then the specified flags will be cleared after a notification is received.
pub fn wait_any_flags(
    flags: u32,
    clear: bool,
    timeout: FuriDuration,
) -> Result<u32, sys::furi::Status> {
    let options = FuriFlagWaitAny.0 | (if clear { 0 } else { FuriFlagNoClear.0 });
    let result = unsafe { sys::furi_thread_flags_wait(flags, options, timeout.0) };

    if sys::FuriFlag(result).has_flag(sys::FuriFlagError) {
        return Err((result as i32).into());
    }

    Ok(result)
}

/// Wait for up-to `timeout` for a change to all of the specified notification `flags` for the current thread.
///
/// If `clear`, then the specified flags will be cleared after a notification is received.
pub fn wait_all_flags(
    flags: u32,
    clear: bool,
    timeout: FuriDuration,
) -> Result<u32, sys::furi::Status> {
    let options = FuriFlagWaitAll.0 | (if clear { 0 } else { FuriFlagNoClear.0 });
    let result = unsafe { sys::furi_thread_flags_wait(flags, options, timeout.0) };

    if sys::FuriFlag(result).has_flag(sys::FuriFlagError) {
        return Err((result as i32).into());
    }

    Ok(result)
}

/// A handle to a thread.
#[cfg(feature = "alloc")]
#[cfg_attr(docsrs, doc(cfg(feature = "alloc")))]
pub struct Thread {
    /// Guaranteed to be UTF-8.
    name: Option<CString>,
    thread: NonNull<sys::FuriThread>,
}

#[cfg(feature = "alloc")]
impl Thread {
    fn new(
        name: Option<CString>,
        stack_size: Option<usize>,
        heap_trace_enabled: Option<bool>,
    ) -> Self {
        let stack_size = stack_size.unwrap_or(MIN_STACK_SIZE);

        unsafe {
            let thread = sys::furi_thread_alloc();
            if let Some(name) = name.as_deref() {
                sys::furi_thread_set_name(thread, name.as_ptr());
            }
            sys::furi_thread_set_stack_size(thread, stack_size);
            if let Some(heap_trace_enabled) = heap_trace_enabled {
                if heap_trace_enabled {
                    sys::furi_thread_enable_heap_trace(thread);
                }
            }
            Thread {
                name,
                thread: NonNull::new_unchecked(thread),
            }
        }
    }

    /// Gets the thread's unique identifier.
    ///
    /// Returns `None` if the thread has terminated.
    pub fn id(&self) -> Option<ThreadId> {
        // TODO: The Rust stdlib generates its own unique IDs for threads that are valid
        // even after a thread terminates.
        let id = unsafe { sys::furi_thread_get_id(self.thread.as_ptr()) };
        if id.is_null() {
            None
        } else {
            Some(ThreadId(id))
        }
    }

    /// Gets the thread's name.
    ///
    /// Returns `None` if the thread has terminated, or is unnamed, or has a name that is
    /// not valid UTF-8.
    pub fn name(&self) -> Option<&str> {
        self.cname()
            .map(|s| unsafe { str::from_utf8_unchecked(s.to_bytes()) })
    }

    fn cname(&self) -> Option<&CStr> {
        self.name.as_deref()
    }
}

#[cfg(feature = "alloc")]
impl fmt::Debug for Thread {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("Thread")
            .field("name", &self.name())
            .finish_non_exhaustive()
    }
}

#[cfg(feature = "alloc")]
impl ufmt::uDebug for Thread {
    fn fmt<W>(&self, f: &mut ufmt::Formatter<'_, W>) -> Result<(), W::Error>
    where
        W: ufmt::uWrite + ?Sized,
    {
        // TODO: ufmt doesn't provide an impl of uDebug for &str.
        f.debug_struct("Thread")?.finish()
    }
}

/// An owned permission to join on a thread (block on its termination).
#[cfg(feature = "alloc")]
#[cfg_attr(docsrs, doc(cfg(feature = "alloc")))]
pub struct JoinHandle {
    context: Option<Arc<Thread>>,
}

#[cfg(feature = "alloc")]
impl Drop for JoinHandle {
    fn drop(&mut self) {
        let context = self
            .context
            .take()
            .expect("Drop should only be called once");

        if let Some(thread) = Arc::into_inner(context) {
            // We were able to successfully extract the `Thread` from the `Arc`. This
            // means there are no other references, so the thread is stopped and we can
            // free its memory.
            unsafe { sys::furi_thread_free(thread.thread.as_ptr()) };
        }
    }
}

#[cfg(feature = "alloc")]
impl JoinHandle {
    /// Extracts a handle to the underlying thread.
    pub fn thread(&self) -> &Thread {
        self.context.as_ref().expect("Drop has not been called")
    }

    /// Waits for the associated thread to finish.
    ///
    /// This function will return immediately if the associated thread has already
    /// finished.
    pub fn join(self) -> i32 {
        let thread = self.thread();
        unsafe {
            sys::furi_thread_join(thread.thread.as_ptr());
            sys::furi_thread_get_return_code(thread.thread.as_ptr())
        }
    }

    /// Checks if the associated thread has finished running its main function.
    pub fn is_finished(&self) -> bool {
        self.thread().id().is_none()
    }
}

#[cfg(feature = "alloc")]
impl fmt::Debug for JoinHandle {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("JoinHandle").finish_non_exhaustive()
    }
}

#[cfg(feature = "alloc")]
impl ufmt::uDebug for JoinHandle {
    fn fmt<W>(&self, f: &mut ufmt::Formatter<'_, W>) -> Result<(), W::Error>
    where
        W: ufmt::uWrite + ?Sized,
    {
        f.debug_struct("JoinHandle")?.finish()
    }
}