furi-deprecated.c 7.5 KB

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