furi-deprecated.h 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. #pragma once
  2. #include "cmsis_os.h"
  3. #ifdef HAVE_FREERTOS
  4. #include <semphr.h>
  5. #endif
  6. #include <stdbool.h>
  7. #include <stdlib.h>
  8. #include <stdint.h>
  9. #include <string.h>
  10. #include <assert.h>
  11. #include "assets_icons.h"
  12. #define MAX_TASK_RECORDS 8
  13. #define MAX_RECORD_SUBSCRIBERS 8
  14. inline static void* furi_alloc(size_t size) {
  15. void* p = malloc(size);
  16. assert(p);
  17. return memset(p, 0, size);
  18. }
  19. /// application is just a function
  20. typedef void (*FlipperApplication)(void*);
  21. /// pointer to value callback function
  22. typedef void (*FlipperRecordCallback)(const void*, size_t, void*);
  23. typedef enum {
  24. FlipperRecordStateMute, ///< record open and mute this handler
  25. FlipperRecordStateUnmute, ///< record unmuted
  26. FlipperRecordStateDeleted ///< record owner halt
  27. } FlipperRecordState;
  28. /// pointer to state callback function
  29. typedef void (*FlipperRecordStateCallback)(FlipperRecordState, void*);
  30. struct _FuriRecord;
  31. typedef struct {
  32. bool allocated;
  33. FlipperRecordCallback cb; ///< value cb
  34. FlipperRecordStateCallback state_cb; ///< state cb
  35. uint8_t mute_counter; ///< see "wiki/FURI#mute-algorithm"
  36. bool no_mute;
  37. struct _FuriRecord* record; ///< parent record
  38. void* ctx;
  39. } FuriRecordSubscriber;
  40. /// FURI record handler
  41. struct _FuriRecord {
  42. const char* name;
  43. void* value;
  44. size_t size;
  45. StaticSemaphore_t mutex_buffer;
  46. SemaphoreHandle_t mutex;
  47. uint8_t mute_counter;
  48. FuriRecordSubscriber subscribers[MAX_RECORD_SUBSCRIBERS];
  49. };
  50. typedef struct _FuriRecord FuriRecord;
  51. /// store info about active task
  52. typedef struct {
  53. const char* name;
  54. FlipperApplication application;
  55. const char* prev_name;
  56. FlipperApplication prev;
  57. TaskHandle_t handler;
  58. uint8_t records_count; ///< count of records which task open
  59. FuriRecord* records[MAX_TASK_RECORDS]; ///< list of records which task open
  60. bool ready;
  61. } FuriApp;
  62. // application dependency info
  63. typedef struct {
  64. uint8_t count;
  65. const char** name;
  66. } FlipperAppLibrary;
  67. // application startup info
  68. typedef struct {
  69. FlipperApplication app;
  70. const char* name;
  71. FlipperAppLibrary libs;
  72. IconName icon;
  73. } FlipperStartupApp;
  74. // Init core
  75. bool furi_init(void);
  76. /*!
  77. Simply starts application.
  78. It call app entrypoint with param passed as argument.
  79. Useful for daemon applications and pop-up.
  80. */
  81. FuriApp* furiac_start(FlipperApplication app, const char* name, void* param);
  82. /*!
  83. Swtich to other application.
  84. FURI stop current app, call app entrypoint with param passed as
  85. argument and save current application entrypoint to prev field
  86. in current application registry.
  87. Useful for UI or "active" application.
  88. */
  89. void furiac_switch(FlipperApplication app, char* name, void* param);
  90. /*!
  91. Stop current application
  92. (stop thread and clear application's stack), start application
  93. from prev entry in current application registry, cleanup current
  94. application registry.
  95. */
  96. void furiac_exit(void* param);
  97. /*!
  98. Mark application as prepared and ready to perform actions
  99. */
  100. void furiac_ready();
  101. /*
  102. Wait for the libraries we depend on
  103. */
  104. void furiac_wait_libs(const FlipperAppLibrary* libs);
  105. /*!
  106. Stop specified app without returning to prev application.
  107. */
  108. bool furiac_kill(FuriApp* app);
  109. // find task pointer by handle
  110. FuriApp* find_task(TaskHandle_t handler);
  111. /*!
  112. Creates named FURI record.
  113. \param[in] name you can open this record anywhere
  114. \param[in] value pointer to data.
  115. \param[in] size size of data.
  116. If NULL, create FURI Pipe (only callbacks management, no data/mutex)
  117. Returns false if registry have not enough memory for creating.
  118. */
  119. bool furi_create_deprecated(const char* name, void* value, size_t size);
  120. /*!
  121. Opens existing FURI record by name.
  122. Returns NULL if record does not exist.
  123. \param[in] solo if true another applications handlers set into "muted" state.
  124. When appication has exited or record has closed, all handlers is unmuted.
  125. It may be useful for concurrently acces to resources like framebuffer or beeper.
  126. \param[in] no_mute if true, another applications cannot mute this handler.
  127. */
  128. FuriRecordSubscriber* furi_open_deprecated(
  129. const char* name,
  130. bool solo,
  131. bool no_mute,
  132. FlipperRecordCallback value_callback,
  133. FlipperRecordStateCallback state_callback,
  134. void* ctx);
  135. /*!
  136. */
  137. void furi_close(FuriRecordSubscriber* handler);
  138. /*!
  139. read message from record.
  140. Returns true if success, false otherwise (closed/non-existent record)
  141. Also return false if you try to read from FURI pipe
  142. TODO: enum return value with execution status
  143. */
  144. bool furi_read(FuriRecordSubscriber* record, void* data, size_t size);
  145. /*!
  146. write message to record.
  147. Returns true if success, false otherwise (closed/non-existent record or muted).
  148. TODO: enum return value with execution status
  149. */
  150. bool furi_write(FuriRecordSubscriber* record, const void* data, size_t size);
  151. /*!
  152. lock value mutex.
  153. It can be useful if records contain pointer to buffer which you want to change.
  154. You must call furi_give after operation on data and
  155. you shouldn't block executing between take and give calls
  156. Returns pointer to data, NULL if closed/non-existent record or muted
  157. TODO: enum return value with execution status
  158. */
  159. void* furi_take(FuriRecordSubscriber* record);
  160. /*!
  161. unlock value mutex.
  162. */
  163. void furi_give(FuriRecordSubscriber* record);
  164. /*!
  165. unlock value mutex and notify subscribers that data is chaned.
  166. */
  167. void furi_commit(FuriRecordSubscriber* handler);