2024-06-10 20:53:08 +03:00
|
|
|
/**
|
|
|
|
* @file event_loop.h
|
|
|
|
* @brief Furi Event Loop
|
|
|
|
*
|
|
|
|
* This module is designed to handle application event loop in fully
|
|
|
|
* asynchronous, reactive nature. On the low level this modules is
|
|
|
|
* inspired by epoll/kqueue concept, on the high level by asyncio
|
|
|
|
* event loop.
|
|
|
|
*
|
|
|
|
* This module is trying to best fit into Furi OS, so we don't
|
|
|
|
* provide any compatibility with other event driven APIs. But
|
|
|
|
* programming concepts are the same, except some runtime
|
|
|
|
* limitations from our side.
|
2024-10-31 04:58:16 +03:00
|
|
|
*
|
|
|
|
* @warning Only ONE instance of FuriEventLoop per thread is possible. ALL FuriEventLoop
|
|
|
|
* funcitons MUST be called from the same thread that the instance was created in.
|
2024-06-10 20:53:08 +03:00
|
|
|
*/
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include "base.h"
|
|
|
|
|
|
|
|
#ifdef __cplusplus
|
|
|
|
extern "C" {
|
|
|
|
#endif
|
|
|
|
|
2024-08-07 06:49:41 +03:00
|
|
|
/**
|
|
|
|
* @brief Enumeration of event types, flags and masks.
|
|
|
|
*
|
|
|
|
* Only one event direction (In or Out) can be used per subscription.
|
|
|
|
* An object can have no more than one subscription for each direction.
|
|
|
|
*
|
|
|
|
* Additional flags that modify the behaviour can be
|
|
|
|
* set using the bitwise OR operation (see flag description).
|
|
|
|
*/
|
2024-06-10 20:53:08 +03:00
|
|
|
typedef enum {
|
2024-08-07 06:49:41 +03:00
|
|
|
/**
|
|
|
|
* @brief Subscribe to In events.
|
|
|
|
*
|
|
|
|
* In events occur on the following conditions:
|
|
|
|
* - One or more items were inserted into a FuriMessageQueue,
|
|
|
|
* - Enough data has been written to a FuriStreamBuffer,
|
|
|
|
* - A FuriSemaphore has been released at least once,
|
|
|
|
* - A FuriMutex has been released.
|
|
|
|
*/
|
|
|
|
FuriEventLoopEventIn = 0x00000001U,
|
|
|
|
/**
|
|
|
|
* @brief Subscribe to Out events.
|
|
|
|
*
|
|
|
|
* Out events occur on the following conditions:
|
|
|
|
* - One or more items were removed from a FuriMessageQueue,
|
|
|
|
* - Any amount of data has been read out of a FuriStreamBuffer,
|
|
|
|
* - A FuriSemaphore has been acquired at least once,
|
|
|
|
* - A FuriMutex has been acquired.
|
|
|
|
*/
|
|
|
|
FuriEventLoopEventOut = 0x00000002U,
|
|
|
|
/**
|
|
|
|
* @brief Special value containing the event direction bits, used internally.
|
|
|
|
*/
|
|
|
|
FuriEventLoopEventMask = 0x00000003U,
|
|
|
|
/**
|
|
|
|
* @brief Use edge triggered events.
|
|
|
|
*
|
|
|
|
* By default, level triggered events are used. A level above zero
|
|
|
|
* is reported based on the following conditions:
|
|
|
|
*
|
|
|
|
* In events:
|
|
|
|
* - a FuriMessageQueue contains one or more items,
|
|
|
|
* - a FuriStreamBuffer contains one or more bytes,
|
|
|
|
* - a FuriSemaphore can be acquired at least once,
|
|
|
|
* - a FuriMutex can be acquired.
|
|
|
|
*
|
|
|
|
* Out events:
|
|
|
|
* - a FuriMessageQueue has at least one item of free space,
|
|
|
|
* - a FuriStreamBuffer has at least one byte of free space,
|
|
|
|
* - a FuriSemaphore has been acquired at least once,
|
|
|
|
* - a FuriMutex has been acquired.
|
|
|
|
*
|
|
|
|
* If this flag is NOT set, the event will be generated repeatedly until
|
|
|
|
* the level becomes zero (e.g. all items have been removed from
|
|
|
|
* a FuriMessageQueue in case of the "In" event, etc.)
|
|
|
|
*
|
|
|
|
* If this flag IS set, then the above check is skipped and the event
|
|
|
|
* is generated ONLY when a change occurs, with the event direction
|
|
|
|
* (In or Out) taken into account.
|
|
|
|
*/
|
|
|
|
FuriEventLoopEventFlagEdge = 0x00000004U,
|
|
|
|
/**
|
|
|
|
* @brief Automatically unsubscribe from events after one time.
|
|
|
|
*
|
|
|
|
* By default, events will be generated each time the specified conditions
|
|
|
|
* have been met. If this flag IS set, the event subscription will be cancelled
|
|
|
|
* upon the first occurred event and no further events will be generated.
|
|
|
|
*/
|
|
|
|
FuriEventLoopEventFlagOnce = 0x00000008U,
|
|
|
|
/**
|
|
|
|
* @brief Special value containing the event flag bits, used internally.
|
|
|
|
*/
|
|
|
|
FuriEventLoopEventFlagMask = 0xFFFFFFFCU,
|
|
|
|
/**
|
|
|
|
* @brief Special value to force the enum to 32-bit values.
|
|
|
|
*/
|
|
|
|
FuriEventLoopEventReserved = UINT32_MAX,
|
2024-06-10 20:53:08 +03:00
|
|
|
} FuriEventLoopEvent;
|
|
|
|
|
|
|
|
/** Anonymous message queue type */
|
|
|
|
typedef struct FuriEventLoop FuriEventLoop;
|
|
|
|
|
|
|
|
/** Allocate Event Loop instance
|
|
|
|
*
|
|
|
|
* Couple things to keep in mind:
|
|
|
|
* - You can have 1 event_loop per 1 thread
|
|
|
|
* - You can not use event_loop instance in the other thread
|
2024-07-02 15:09:50 +03:00
|
|
|
* - Do not use blocking API to query object delegated to Event Loop
|
2024-06-10 20:53:08 +03:00
|
|
|
*
|
|
|
|
* @return The Event Loop instance
|
|
|
|
*/
|
|
|
|
FuriEventLoop* furi_event_loop_alloc(void);
|
|
|
|
|
|
|
|
/** Free Event Loop instance
|
|
|
|
*
|
|
|
|
* @param instance The Event Loop instance
|
|
|
|
*/
|
|
|
|
void furi_event_loop_free(FuriEventLoop* instance);
|
|
|
|
|
|
|
|
/** Continuously poll for events
|
|
|
|
*
|
|
|
|
* Can be stopped with `furi_event_loop_stop`
|
|
|
|
*
|
|
|
|
* @param instance The Event Loop instance
|
|
|
|
*/
|
|
|
|
void furi_event_loop_run(FuriEventLoop* instance);
|
|
|
|
|
|
|
|
/** Stop Event Loop instance
|
|
|
|
*
|
|
|
|
* @param instance The Event Loop instance
|
|
|
|
*/
|
|
|
|
void furi_event_loop_stop(FuriEventLoop* instance);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Tick related API
|
|
|
|
*/
|
|
|
|
|
|
|
|
/** Tick callback type
|
|
|
|
*
|
|
|
|
* @param context The context for callback
|
|
|
|
*/
|
|
|
|
typedef void (*FuriEventLoopTickCallback)(void* context);
|
|
|
|
|
|
|
|
/** Set Event Loop tick callback
|
|
|
|
*
|
2024-07-02 15:09:50 +03:00
|
|
|
* Tick callback is called periodically after specified inactivity time.
|
|
|
|
* It acts like a low-priority timer: it will only fire if there is time
|
|
|
|
* left after processing the synchronization primitives and the regular timers.
|
|
|
|
* Therefore, it is not monotonic: ticks will be skipped if the event loop is busy.
|
2024-06-10 20:53:08 +03:00
|
|
|
*
|
|
|
|
* @param instance The Event Loop instance
|
|
|
|
* @param[in] interval The tick interval
|
|
|
|
* @param[in] callback The callback to call
|
|
|
|
* @param context The context for callback
|
|
|
|
*/
|
|
|
|
void furi_event_loop_tick_set(
|
|
|
|
FuriEventLoop* instance,
|
|
|
|
uint32_t interval,
|
|
|
|
FuriEventLoopTickCallback callback,
|
|
|
|
void* context);
|
|
|
|
|
2024-07-02 15:09:50 +03:00
|
|
|
/*
|
|
|
|
* Deferred function call API
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Timer callback type for functions to be called in a deferred manner.
|
|
|
|
*
|
|
|
|
* @param[in,out] context pointer to a user-specific object that was provided during
|
|
|
|
* furi_event_loop_pend_callback() call
|
|
|
|
*/
|
|
|
|
typedef void (*FuriEventLoopPendingCallback)(void* context);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Call a function when all preceding timer commands are processed
|
|
|
|
*
|
|
|
|
* This function may be useful to call another function when the event loop has been started.
|
|
|
|
*
|
|
|
|
* @param[in,out] instance pointer to the current FuriEventLoop instance
|
|
|
|
* @param[in] callback pointer to the callback to be executed when previous commands have been processed
|
|
|
|
* @param[in,out] context pointer to a user-specific object (will be passed to the callback)
|
|
|
|
*/
|
|
|
|
void furi_event_loop_pend_callback(
|
|
|
|
FuriEventLoop* instance,
|
|
|
|
FuriEventLoopPendingCallback callback,
|
|
|
|
void* context);
|
|
|
|
|
2024-06-10 20:53:08 +03:00
|
|
|
/*
|
2024-08-07 06:49:41 +03:00
|
|
|
* Event subscription/notification APIs
|
2024-06-10 20:53:08 +03:00
|
|
|
*/
|
|
|
|
|
2024-08-07 06:49:41 +03:00
|
|
|
typedef void FuriEventLoopObject;
|
2024-06-10 20:53:08 +03:00
|
|
|
|
2024-08-07 06:49:41 +03:00
|
|
|
/** Callback type for event loop events
|
2024-06-10 20:53:08 +03:00
|
|
|
*
|
2024-08-07 06:49:41 +03:00
|
|
|
* @param object The object that triggered the event
|
|
|
|
* @param context The context that was provided upon subscription
|
2024-10-31 04:58:16 +03:00
|
|
|
*/
|
|
|
|
typedef void (*FuriEventLoopEventCallback)(FuriEventLoopObject* object, void* context);
|
|
|
|
|
|
|
|
/** Opaque event flag type */
|
|
|
|
typedef struct FuriEventFlag FuriEventFlag;
|
|
|
|
|
|
|
|
/** Subscribe to event flag events
|
2024-06-10 20:53:08 +03:00
|
|
|
*
|
2024-10-31 04:58:16 +03:00
|
|
|
* @warning you can only have one subscription for one event type.
|
|
|
|
*
|
|
|
|
* @param instance The Event Loop instance
|
|
|
|
* @param event_flag The event flag to add
|
|
|
|
* @param[in] event The Event Loop event to trigger on
|
|
|
|
* @param[in] callback The callback to call on event
|
|
|
|
* @param context The context for callback
|
2024-06-10 20:53:08 +03:00
|
|
|
*/
|
2024-10-31 04:58:16 +03:00
|
|
|
|
|
|
|
void furi_event_loop_subscribe_event_flag(
|
|
|
|
FuriEventLoop* instance,
|
|
|
|
FuriEventFlag* event_flag,
|
|
|
|
FuriEventLoopEvent event,
|
|
|
|
FuriEventLoopEventCallback callback,
|
|
|
|
void* context);
|
2024-08-07 06:49:41 +03:00
|
|
|
|
|
|
|
/** Opaque message queue type */
|
|
|
|
typedef struct FuriMessageQueue FuriMessageQueue;
|
2024-06-10 20:53:08 +03:00
|
|
|
|
|
|
|
/** Subscribe to message queue events
|
|
|
|
*
|
|
|
|
* @warning you can only have one subscription for one event type.
|
|
|
|
*
|
|
|
|
* @param instance The Event Loop instance
|
|
|
|
* @param message_queue The message queue to add
|
|
|
|
* @param[in] event The Event Loop event to trigger on
|
|
|
|
* @param[in] callback The callback to call on event
|
|
|
|
* @param context The context for callback
|
|
|
|
*/
|
2024-08-07 06:49:41 +03:00
|
|
|
void furi_event_loop_subscribe_message_queue(
|
2024-06-10 20:53:08 +03:00
|
|
|
FuriEventLoop* instance,
|
|
|
|
FuriMessageQueue* message_queue,
|
|
|
|
FuriEventLoopEvent event,
|
2024-08-07 06:49:41 +03:00
|
|
|
FuriEventLoopEventCallback callback,
|
|
|
|
void* context);
|
|
|
|
|
|
|
|
/** Opaque stream buffer type */
|
|
|
|
typedef struct FuriStreamBuffer FuriStreamBuffer;
|
|
|
|
|
|
|
|
/** Subscribe to stream buffer events
|
|
|
|
*
|
|
|
|
* @warning you can only have one subscription for one event type.
|
|
|
|
*
|
|
|
|
* @param instance The Event Loop instance
|
|
|
|
* @param stream_buffer The stream buffer to add
|
|
|
|
* @param[in] event The Event Loop event to trigger on
|
|
|
|
* @param[in] callback The callback to call on event
|
|
|
|
* @param context The context for callback
|
|
|
|
*/
|
|
|
|
void furi_event_loop_subscribe_stream_buffer(
|
|
|
|
FuriEventLoop* instance,
|
|
|
|
FuriStreamBuffer* stream_buffer,
|
|
|
|
FuriEventLoopEvent event,
|
|
|
|
FuriEventLoopEventCallback callback,
|
|
|
|
void* context);
|
|
|
|
|
|
|
|
/** Opaque semaphore type */
|
|
|
|
typedef struct FuriSemaphore FuriSemaphore;
|
|
|
|
|
|
|
|
/** Subscribe to semaphore events
|
|
|
|
*
|
|
|
|
* @warning you can only have one subscription for one event type.
|
|
|
|
*
|
|
|
|
* @param instance The Event Loop instance
|
|
|
|
* @param semaphore The semaphore to add
|
|
|
|
* @param[in] event The Event Loop event to trigger on
|
|
|
|
* @param[in] callback The callback to call on event
|
|
|
|
* @param context The context for callback
|
|
|
|
*/
|
|
|
|
void furi_event_loop_subscribe_semaphore(
|
|
|
|
FuriEventLoop* instance,
|
|
|
|
FuriSemaphore* semaphore,
|
|
|
|
FuriEventLoopEvent event,
|
|
|
|
FuriEventLoopEventCallback callback,
|
2024-06-10 20:53:08 +03:00
|
|
|
void* context);
|
|
|
|
|
2024-08-07 06:49:41 +03:00
|
|
|
/** Opaque mutex type */
|
|
|
|
typedef struct FuriMutex FuriMutex;
|
|
|
|
|
|
|
|
/** Subscribe to mutex events
|
|
|
|
*
|
|
|
|
* @warning you can only have one subscription for one event type.
|
2024-06-10 20:53:08 +03:00
|
|
|
*
|
|
|
|
* @param instance The Event Loop instance
|
2024-08-07 06:49:41 +03:00
|
|
|
* @param mutex The mutex to add
|
|
|
|
* @param[in] event The Event Loop event to trigger on
|
|
|
|
* @param[in] callback The callback to call on event
|
|
|
|
* @param context The context for callback
|
2024-06-10 20:53:08 +03:00
|
|
|
*/
|
2024-08-07 06:49:41 +03:00
|
|
|
void furi_event_loop_subscribe_mutex(
|
2024-06-10 20:53:08 +03:00
|
|
|
FuriEventLoop* instance,
|
2024-08-07 06:49:41 +03:00
|
|
|
FuriMutex* mutex,
|
|
|
|
FuriEventLoopEvent event,
|
|
|
|
FuriEventLoopEventCallback callback,
|
|
|
|
void* context);
|
|
|
|
|
|
|
|
/** Unsubscribe from events (common)
|
|
|
|
*
|
|
|
|
* @param instance The Event Loop instance
|
|
|
|
* @param object The object to unsubscribe from
|
|
|
|
*/
|
|
|
|
void furi_event_loop_unsubscribe(FuriEventLoop* instance, FuriEventLoopObject* object);
|
2024-06-10 20:53:08 +03:00
|
|
|
|
[FL-3893] JS modules (#3841)
* feat: backport js_gpio from unleashed
* feat: backport js_keyboard, TextInputModel::minimum_length from unleashed
* fix: api version inconsistency
* style: js_gpio
* build: fix submodule ._ .
* refactor: js_gpio
* docs: type declarations for gpio
* feat: gpio interrupts
* fix: js_gpio freeing, resetting and minor stylistic changes
* style: js_gpio
* style: mlib array, fixme's
* feat: js_gpio adc
* feat: js_event_loop
* docs: js_event_loop
* feat: js_event_loop subscription cancellation
* feat: js_event_loop + js_gpio integration
* fix: js_event_loop memory leak
* feat: stop event loop on back button
* test: js: basic, math, event_loop
* feat: js_event_loop queue
* feat: js linkage to previously loaded plugins
* build: fix ci errors
* feat: js module ordered teardown
* feat: js_gui_defer_free
* feat: basic hourglass view
* style: JS ASS (Argument Schema for Scripts)
* fix: js_event_loop mem leaks and lifetime problems
* fix: crashing test and pvs false positives
* feat: mjs custom obj destructors, gui submenu view
* refactor: yank js_gui_defer_free (yuck)
* refactor: maybe_unsubscribe
* empty_screen, docs, typing fix-ups
* docs: navigation event & demo
* feat: submenu setHeader
* feat: text_input
* feat: text_box
* docs: text_box availability
* ci: silence irrelevant pvs low priority warning
* style: use furistring
* style: _get_at -> _safe_get
* fix: built-in module name assignment
* feat: js_dialog; refactor, optimize: js_gui
* docs: js_gui
* ci: silence pvs warning: Memory allocation is infallible
* style: fix storage spelling
* feat: foreign pointer signature checks
* feat: js_storage
* docs: js_storage
* fix: my unit test was breaking other tests ;_;
* ci: fix ci?
* Make doxygen happy
* docs: flipper, math, notification, global
* style: review suggestions
* style: review fixups
* fix: badusb demo script
* docs: badusb
* ci: add nofl
* ci: make linter happy
* Bump api version
Co-authored-by: Aleksandr Kutuzov <alleteam@gmail.com>
2024-10-14 21:42:11 +03:00
|
|
|
/**
|
|
|
|
* @brief Checks if the loop is subscribed to an object of any kind
|
|
|
|
*
|
|
|
|
* @param instance Event Loop instance
|
|
|
|
* @param object Object to check
|
|
|
|
*/
|
|
|
|
bool furi_event_loop_is_subscribed(FuriEventLoop* instance, FuriEventLoopObject* object);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Convenience function for `if(is_subscribed()) unsubscribe()`
|
|
|
|
*/
|
|
|
|
static inline void
|
|
|
|
furi_event_loop_maybe_unsubscribe(FuriEventLoop* instance, FuriEventLoopObject* object) {
|
|
|
|
if(furi_event_loop_is_subscribed(instance, object))
|
|
|
|
furi_event_loop_unsubscribe(instance, object);
|
|
|
|
}
|
|
|
|
|
2024-06-10 20:53:08 +03:00
|
|
|
#ifdef __cplusplus
|
|
|
|
}
|
|
|
|
#endif
|