furi.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. #include "furi.h"
  2. #include "cmsis_os.h"
  3. #include <string.h>
  4. // TODO: this file contains printf, that not implemented on uC target
  5. #ifdef FURI_DEBUG
  6. #include <stdio.h>
  7. #endif
  8. #define MAX_RECORD_COUNT 32
  9. static FuriRecord records[MAX_RECORD_COUNT];
  10. static size_t current_buffer_idx = 0;
  11. // find record pointer by name
  12. static FuriRecord* find_record(const char* name) {
  13. if(name == NULL) return NULL;
  14. FuriRecord* res = NULL;
  15. for(size_t i = 0; i < MAX_RECORD_COUNT; i++) {
  16. if(records[i].name != NULL && strcmp(name, records[i].name) == 0) {
  17. res = &records[i];
  18. }
  19. }
  20. return res;
  21. }
  22. bool furi_create(const char* name, void* value, size_t size) {
  23. #ifdef FURI_DEBUG
  24. printf("[FURI] creating %s record\n", name);
  25. #endif
  26. if(current_buffer_idx >= MAX_RECORD_COUNT) {
  27. // max record count exceed
  28. #ifdef FURI_DEBUG
  29. printf("[FURI] create: max record count exceed\n");
  30. #endif
  31. return NULL;
  32. }
  33. records[current_buffer_idx].mute_counter = 0;
  34. records[current_buffer_idx].mutex = xSemaphoreCreateMutexStatic(
  35. &records[current_buffer_idx].mutex_buffer
  36. );
  37. records[current_buffer_idx].value = value;
  38. records[current_buffer_idx].size = size;
  39. records[current_buffer_idx].name = name;
  40. for(size_t i = 0; i < MAX_RECORD_SUBSCRIBERS; i++) {
  41. records[current_buffer_idx].subscribers[i].allocated = false;
  42. }
  43. current_buffer_idx++;
  44. return true;
  45. }
  46. FuriRecordSubscriber* furi_open(
  47. const char* name,
  48. bool solo,
  49. bool no_mute,
  50. FlipperRecordCallback value_callback,
  51. FlipperRecordStateCallback state_callback
  52. ) {
  53. #ifdef FURI_DEBUG
  54. printf("[FURI] opening %s record\n", name);
  55. #endif
  56. // get furi record by name
  57. FuriRecord* record = find_record(name);
  58. if(record == NULL) {
  59. // cannot find record
  60. #ifdef FURI_DEBUG
  61. printf("[FURI] cannot find record %s\n", name);
  62. #endif
  63. return NULL;
  64. }
  65. // allocate subscriber
  66. FuriRecordSubscriber* subscriber = NULL;
  67. for(size_t i = 0; i < MAX_RECORD_SUBSCRIBERS; i++) {
  68. if(!record->subscribers[i].allocated) {
  69. subscriber = &record->subscribers[i];
  70. break;
  71. }
  72. }
  73. if(subscriber == NULL) {
  74. // cannot add subscriber (full)
  75. #ifdef FURI_DEBUG
  76. printf("[FURI] open: cannot add subscriber (full)\n");
  77. #endif
  78. return NULL;
  79. }
  80. // increase mute_counter
  81. if(solo) {
  82. record->mute_counter++;
  83. }
  84. // set all parameters
  85. subscriber->allocated = true;
  86. subscriber->mute_counter = record->mute_counter;
  87. subscriber->no_mute = no_mute;
  88. subscriber->cb = value_callback;
  89. subscriber->state_cb = state_callback;
  90. subscriber->record = record;
  91. // register record in application
  92. FuriApp* current_task = find_task(xTaskGetCurrentTaskHandle());
  93. if(current_task != NULL) {
  94. current_task->records[current_task->records_count] = record;
  95. current_task->records_count++;
  96. } else {
  97. #ifdef FURI_DEBUG
  98. printf("[FURI] open: no current task\n");
  99. #endif
  100. }
  101. return subscriber;
  102. }
  103. void furi_close(FuriRecordSubscriber* handler) {
  104. #ifdef FURI_DEBUG
  105. printf("[FURI] closing %s record\n", handler->record->name);
  106. #endif
  107. // deallocate subscriber
  108. handler->allocated = false;
  109. // set mute counter to next max value
  110. uint8_t max_mute_counter = 0;
  111. for(size_t i = 0; i < MAX_RECORD_SUBSCRIBERS; i++) {
  112. if(handler->record->subscribers[i].allocated) {
  113. if(handler->record->subscribers[i].mute_counter > max_mute_counter) {
  114. max_mute_counter = handler->record->subscribers[i].mute_counter;
  115. }
  116. }
  117. }
  118. handler->record->mute_counter = max_mute_counter;
  119. }
  120. static void furi_notify(FuriRecordSubscriber* handler, const void* value, size_t size) {
  121. for(size_t i = 0; i < MAX_RECORD_SUBSCRIBERS; i++) {
  122. if(handler->record->subscribers[i].allocated) {
  123. if(handler->record->subscribers[i].cb != NULL) {
  124. handler->record->subscribers[i].cb(value, size);
  125. }
  126. }
  127. }
  128. }
  129. void* furi_take(FuriRecordSubscriber* handler) {
  130. // take mutex
  131. return handler->record->value;
  132. }
  133. void furi_give(FuriRecordSubscriber* handler) {
  134. // release mutex
  135. }
  136. bool furi_read(FuriRecordSubscriber* handler, void* value, size_t size) {
  137. #ifdef FURI_DEBUG
  138. printf("[FURI] read from %s\n", handler->record->name);
  139. #endif
  140. if(handler == NULL || handler->record == NULL || value == NULL) return false;
  141. if(size > handler->record->size) return false;
  142. // return false if read from pipe
  143. if(handler->record->value == NULL) return false;
  144. furi_take(handler);
  145. memcpy(value, handler->record->value, size);
  146. furi_give(handler);
  147. furi_notify(handler, value, size);
  148. return true;
  149. }
  150. bool furi_write(FuriRecordSubscriber* handler, const void* value, size_t size) {
  151. #ifdef FURI_DEBUG
  152. printf("[FURI] write to %s\n", handler->record->name);
  153. #endif
  154. if(handler == NULL || handler->record == NULL || value == NULL) {
  155. #ifdef FURI_DEBUG
  156. printf("[FURI] write: null param %x %x\n", (uint32_t)(size_t)handler, (uint32_t)(size_t)value);
  157. #endif
  158. return false;
  159. }
  160. // check if closed
  161. if(!handler->allocated) {
  162. #ifdef FURI_DEBUG
  163. printf("[FURI] write: handler closed\n");
  164. #endif
  165. return false;
  166. }
  167. if(handler->record->value != NULL && size > handler->record->size) {
  168. #ifdef FURI_DEBUG
  169. printf("[FURI] write: wrong size %d\n", (uint32_t)size);
  170. #endif
  171. return false;
  172. }
  173. // check mute
  174. if(
  175. handler->record->mute_counter != handler->mute_counter
  176. && !handler->no_mute
  177. ) {
  178. #ifdef FURI_DEBUG
  179. printf("[FURI] write: muted\n");
  180. #endif
  181. return false;
  182. }
  183. if(handler->record->value != NULL) {
  184. // real write to value
  185. furi_take(handler);
  186. memcpy(handler->record->value, value, size);
  187. furi_give(handler);
  188. // notify subscribers
  189. furi_notify(handler, handler->record->value, handler->record->size);
  190. } else {
  191. furi_notify(handler, value, size);
  192. }
  193. return true;
  194. }