uhf_app.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. #include "uhf_app_i.h"
  2. char* convertToHexString(const uint8_t* array, size_t length) {
  3. if(array == NULL || length == 0) {
  4. return NULL;
  5. }
  6. // Each byte takes 3 characters in the hex representation (2 characters + space), plus 1 for the null terminator
  7. size_t hexLength = (length * 3) + 1;
  8. char* hexArray = (char*)malloc(hexLength * sizeof(char));
  9. if(hexArray == NULL) {
  10. return NULL;
  11. }
  12. size_t index = 0;
  13. for(size_t i = 0; i < length; i++) {
  14. index += snprintf(&hexArray[index], hexLength - index, "%02x ", array[i]);
  15. }
  16. hexArray[hexLength - 1] = '\0';
  17. return hexArray;
  18. }
  19. bool uhf_save_data(UHFResponseData* uhf_response_data, Storage* storage, const char* filename) {
  20. bool saved = false;
  21. if(!storage_dir_exists(storage, UHF_APPS_DATA_FOLDER)) {
  22. storage_simply_mkdir(storage, UHF_APPS_DATA_FOLDER);
  23. }
  24. if(!storage_dir_exists(storage, UHF_APPS_STORAGE_FOLDER)) {
  25. storage_simply_mkdir(storage, UHF_APPS_STORAGE_FOLDER);
  26. }
  27. File* file = storage_file_alloc(storage);
  28. char file_path_result[100];
  29. strcpy(file_path_result, UHF_APPS_STORAGE_FOLDER);
  30. strcat(file_path_result, "/");
  31. strcat(file_path_result, filename);
  32. strcat(file_path_result, UHF_FILE_EXTENSION);
  33. if(storage_file_open(file, file_path_result, FSAM_WRITE, FSOM_OPEN_ALWAYS)) {
  34. storage_file_seek(file, 0, true);
  35. storage_file_truncate(file);
  36. char* hex_data =
  37. convertToHexString(uhf_response_data->data->data, uhf_response_data->data->length);
  38. storage_file_write(file, "UHF App Version : ", 19);
  39. storage_file_write(file, UHF_DATA_VERSION, 2);
  40. storage_file_write(file, "\n", 1);
  41. storage_file_write(file, hex_data, strlen(hex_data));
  42. storage_file_close(file);
  43. storage_file_free(file);
  44. saved = true;
  45. }
  46. return saved;
  47. }
  48. bool uhf_custom_event_callback(void* ctx, uint32_t event) {
  49. furi_assert(ctx);
  50. UHFApp* uhf_app = ctx;
  51. return scene_manager_handle_custom_event(uhf_app->scene_manager, event);
  52. }
  53. bool uhf_back_event_callback(void* ctx) {
  54. furi_assert(ctx);
  55. UHFApp* uhf_app = ctx;
  56. return scene_manager_handle_back_event(uhf_app->scene_manager);
  57. }
  58. void uhf_tick_event_callback(void* ctx) {
  59. furi_assert(ctx);
  60. UHFApp* uhf_app = ctx;
  61. scene_manager_handle_tick_event(uhf_app->scene_manager);
  62. }
  63. UHFApp* uhf_alloc() {
  64. UHFApp* uhf_app = (UHFApp*)malloc(sizeof(UHFApp));
  65. uhf_app->worker = (UHFWorker*)uhf_worker_alloc();
  66. uhf_app->view_dispatcher = view_dispatcher_alloc();
  67. uhf_app->scene_manager = scene_manager_alloc(&uhf_scene_handlers, uhf_app);
  68. view_dispatcher_enable_queue(uhf_app->view_dispatcher);
  69. view_dispatcher_set_event_callback_context(uhf_app->view_dispatcher, uhf_app);
  70. view_dispatcher_set_custom_event_callback(uhf_app->view_dispatcher, uhf_custom_event_callback);
  71. view_dispatcher_set_navigation_event_callback(
  72. uhf_app->view_dispatcher, uhf_back_event_callback);
  73. view_dispatcher_set_tick_event_callback(
  74. uhf_app->view_dispatcher, uhf_tick_event_callback, 100);
  75. // Open GUI record
  76. uhf_app->gui = furi_record_open(RECORD_GUI);
  77. view_dispatcher_attach_to_gui(
  78. uhf_app->view_dispatcher, uhf_app->gui, ViewDispatcherTypeFullscreen);
  79. // Storage
  80. uhf_app->storage = furi_record_open(RECORD_STORAGE);
  81. // Open Notification record
  82. uhf_app->notifications = furi_record_open(RECORD_NOTIFICATION);
  83. // Submenu
  84. uhf_app->submenu = submenu_alloc();
  85. view_dispatcher_add_view(
  86. uhf_app->view_dispatcher, UHFViewMenu, submenu_get_view(uhf_app->submenu));
  87. // Popup
  88. uhf_app->popup = popup_alloc();
  89. view_dispatcher_add_view(
  90. uhf_app->view_dispatcher, UHFViewPopup, popup_get_view(uhf_app->popup));
  91. // Loading
  92. uhf_app->loading = loading_alloc();
  93. view_dispatcher_add_view(
  94. uhf_app->view_dispatcher, UHFViewLoading, loading_get_view(uhf_app->loading));
  95. // Text Input
  96. uhf_app->text_input = text_input_alloc();
  97. view_dispatcher_add_view(
  98. uhf_app->view_dispatcher, UHFViewTextInput, text_input_get_view(uhf_app->text_input));
  99. // Custom Widget
  100. uhf_app->widget = widget_alloc();
  101. view_dispatcher_add_view(
  102. uhf_app->view_dispatcher, UHFViewWidget, widget_get_view(uhf_app->widget));
  103. return uhf_app;
  104. }
  105. void uhf_free(UHFApp* uhf_app) {
  106. furi_assert(uhf_app);
  107. // Submenu
  108. view_dispatcher_remove_view(uhf_app->view_dispatcher, UHFViewMenu);
  109. submenu_free(uhf_app->submenu);
  110. // Popup
  111. view_dispatcher_remove_view(uhf_app->view_dispatcher, UHFViewPopup);
  112. popup_free(uhf_app->popup);
  113. // Loading
  114. view_dispatcher_remove_view(uhf_app->view_dispatcher, UHFViewLoading);
  115. loading_free(uhf_app->loading);
  116. // TextInput
  117. view_dispatcher_remove_view(uhf_app->view_dispatcher, UHFViewTextInput);
  118. text_input_free(uhf_app->text_input);
  119. // Custom Widget
  120. view_dispatcher_remove_view(uhf_app->view_dispatcher, UHFViewWidget);
  121. widget_free(uhf_app->widget);
  122. // Worker
  123. uhf_worker_stop(uhf_app->worker);
  124. uhf_worker_free(uhf_app->worker);
  125. // View Dispatcher
  126. view_dispatcher_free(uhf_app->view_dispatcher);
  127. // Scene Manager
  128. scene_manager_free(uhf_app->scene_manager);
  129. // GUI
  130. furi_record_close(RECORD_GUI);
  131. uhf_app->gui = NULL;
  132. // Storage
  133. furi_record_close(RECORD_STORAGE);
  134. uhf_app->storage = NULL;
  135. // Notifications
  136. furi_record_close(RECORD_NOTIFICATION);
  137. uhf_app->notifications = NULL;
  138. free(uhf_app);
  139. }
  140. // void uhf_text_store_set(UHFApp* uhf_app, const char* text, ...) {
  141. // va_list args;
  142. // va_start(args, text);
  143. // vsnprintf(uhf_app->text_store, sizeof(uhf_app->text_store), text, args);
  144. // va_end(args);
  145. // }
  146. // void uhf_text_store_clear(UHFApp* uhf_app) {
  147. // memset(uhf_app->text_store, 0, sizeof(uhf_app->text_store));
  148. // }
  149. // ==================
  150. static const NotificationSequence uhf_sequence_blink_start_cyan = {
  151. &message_blink_start_10,
  152. &message_blink_set_color_cyan,
  153. &message_do_not_reset,
  154. NULL,
  155. };
  156. static const NotificationSequence uhf_sequence_blink_stop = {
  157. &message_blink_stop,
  158. NULL,
  159. };
  160. void uhf_blink_start(UHFApp* uhf_app) {
  161. notification_message(uhf_app->notifications, &uhf_sequence_blink_start_cyan);
  162. }
  163. void uhf_blink_stop(UHFApp* uhf_app) {
  164. notification_message(uhf_app->notifications, &uhf_sequence_blink_stop);
  165. }
  166. void uhf_show_loading_popup(void* ctx, bool show) {
  167. UHFApp* uhf_app = ctx;
  168. TaskHandle_t timer_task = xTaskGetHandle(configTIMER_SERVICE_TASK_NAME);
  169. if(show) {
  170. // Raise timer priority so that animations can play
  171. vTaskPrioritySet(timer_task, configMAX_PRIORITIES - 1);
  172. view_dispatcher_switch_to_view(uhf_app->view_dispatcher, UHFViewLoading);
  173. } else {
  174. // Restore default timer priority
  175. vTaskPrioritySet(timer_task, configTIMER_TASK_PRIORITY);
  176. }
  177. }
  178. int32_t uhf_app_main(void* ctx) {
  179. UNUSED(ctx);
  180. UHFApp* uhf_app = uhf_alloc();
  181. scene_manager_next_scene(uhf_app->scene_manager, UHFSceneStart);
  182. view_dispatcher_run(uhf_app->view_dispatcher);
  183. uhf_free(uhf_app);
  184. return 0;
  185. }