furi.h 5.0 KB

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