furi.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. #include "furi.h"
  2. #include "cmsis_os.h"
  3. #include <string.h>
  4. #define DEBUG
  5. #ifdef 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 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 DEBUG
  29. printf("[FURI] 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. return true;
  44. }
  45. FuriRecordHandler furi_open(
  46. const char* name,
  47. bool solo,
  48. bool no_mute,
  49. FlipperRecordCallback value_callback,
  50. FlipperRecordStateCallback state_callback
  51. ) {
  52. #ifdef DEBUG
  53. printf("[FURI] opening %s record\n", name);
  54. #endif
  55. // get furi record by name
  56. FuriRecord* record = find_record(name);
  57. if(record == NULL) {
  58. // cannot find record
  59. #ifdef DEBUG
  60. printf("[FURI] cannot find record %s\n", name);
  61. #endif
  62. FuriRecordHandler res = {.record = NULL, .subscriber = NULL};
  63. return res;
  64. }
  65. // allocate subscriber
  66. FuriRecordSubscriber* subscriber = NULL;
  67. for(size_t i = 0; i < MAX_RECORD_SUBSCRIBERS; i++) {
  68. if(!records[current_buffer_idx].subscribers[i].allocated) {
  69. subscriber = &records[current_buffer_idx].subscribers[i];
  70. break;
  71. }
  72. }
  73. if(subscriber == NULL) {
  74. // cannot add subscriber (full)
  75. #ifdef DEBUG
  76. printf("[FURI] cannot add subscriber (full)\n");
  77. #endif
  78. FuriRecordHandler res = {.record = NULL, .subscriber = NULL};
  79. return res;
  80. }
  81. // increase mute_counter
  82. if(solo) {
  83. record->mute_counter++;
  84. }
  85. // set all parameters
  86. subscriber->allocated = true;
  87. subscriber->mute_counter = record->mute_counter;
  88. subscriber->no_mute = no_mute;
  89. subscriber->cb = value_callback;
  90. subscriber->state_cb = state_callback;
  91. // register record in application
  92. FuriApp* current_task = find_task(xTaskGetCurrentTaskHandle());
  93. current_task->records[current_task->records_count] = record;
  94. current_task->records_count++;
  95. FuriRecordHandler res = {.record = record, .subscriber = subscriber};
  96. return res;
  97. }
  98. void furi_close(FuriRecordHandler* handler) {
  99. #ifdef DEBUG
  100. printf("[FURI] closing %s record\n", handler->record->name);
  101. #endif
  102. // deallocate subscriber
  103. handler->subscriber->allocated = false;
  104. // set mute counter to next max value
  105. uint8_t max_mute_counter = 0;
  106. for(size_t i = 0; i < MAX_RECORD_SUBSCRIBERS; i++) {
  107. if(handler->record->subscribers[i].allocated) {
  108. if(handler->record->subscribers[i].mute_counter > max_mute_counter) {
  109. max_mute_counter = handler->record->subscribers[i].mute_counter;
  110. }
  111. }
  112. }
  113. handler->record->mute_counter = max_mute_counter;
  114. }
  115. static void furi_notify(FuriRecordHandler* handler, const void* value, size_t size) {
  116. for(size_t i = 0; i < MAX_RECORD_SUBSCRIBERS; i++) {
  117. if(handler->record->subscribers[i].allocated) {
  118. if(handler->record->subscribers[i].cb != NULL) {
  119. handler->record->subscribers[i].cb(value, size);
  120. }
  121. }
  122. }
  123. }
  124. void* furi_take(FuriRecordHandler* handler) {
  125. // take mutex
  126. return handler->record->value;
  127. }
  128. void furi_give(FuriRecordHandler* handler) {
  129. // release mutex
  130. }
  131. bool furi_read(FuriRecordHandler* handler, void* value, size_t size) {
  132. #ifdef DEBUG
  133. printf("[FURI] read from %s\n", handler->record->name);
  134. #endif
  135. if(handler == NULL || handler->record == NULL || value == NULL) return false;
  136. if(size > handler->record->size) return false;
  137. // return false if read from pipe
  138. if(handler->record->value == NULL) return false;
  139. furi_take(handler);
  140. memcpy(value, handler->record->value, size);
  141. furi_give(handler);
  142. furi_notify(handler, value, size);
  143. return true;
  144. }
  145. bool furi_write(FuriRecordHandler* handler, const void* value, size_t size) {
  146. #ifdef DEBUG
  147. printf("[FURI] write to %s\n", handler->record->name);
  148. #endif
  149. if(handler == NULL || handler->record == NULL || value == NULL) return false;
  150. // check if closed
  151. if(!handler->subscriber->allocated) return false;
  152. if(handler->record->value != NULL && size > handler->record->size) return false;
  153. // check mute
  154. if(
  155. handler->record->mute_counter != handler->subscriber->mute_counter
  156. && !handler->subscriber->no_mute
  157. ) return false;
  158. if(handler->record->value != NULL) {
  159. // real write to value
  160. furi_take(handler);
  161. memcpy(handler->record->value, value, size);
  162. furi_give(handler);
  163. // notify subscribers
  164. furi_notify(handler, handler->record->value, handler->record->size);
  165. } else {
  166. furi_notify(handler, value, size);
  167. }
  168. return true;
  169. }