furi.h 4.3 KB

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