app.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. #include "app.h"
  2. #include "structures.h"
  3. /**
  4. * @brief Callback for exiting the application.
  5. * @details This function is called when user press back button. We return VIEW_NONE to
  6. * indicate that we want to exit the application.
  7. * @param context The context - unused
  8. * @return next view id
  9. */
  10. uint32_t uhf_reader_navigation_exit_callback(void* context) {
  11. UNUSED(context);
  12. return VIEW_NONE;
  13. }
  14. /**
  15. * @brief Handle submenu item selection.
  16. * @details This function is called when user selects an item from the submenu.
  17. * @param context The context - UHFReaderApp object.
  18. * @param index The UHFReaderSubmenuIndex item that was clicked.
  19. */
  20. void uhf_reader_submenu_callback(void* context, uint32_t index) {
  21. UHFReaderApp* App = (UHFReaderApp*)context;
  22. switch(index) {
  23. case UHFReaderSubmenuIndexRead:
  24. view_dispatcher_switch_to_view(App->ViewDispatcher, UHFReaderViewRead);
  25. break;
  26. case UHFReaderSubmenuIndexSaved:
  27. view_dispatcher_switch_to_view(App->ViewDispatcher, UHFReaderViewSaved);
  28. break;
  29. case UHFReaderSubmenuIndexConfig:
  30. view_dispatcher_switch_to_view(App->ViewDispatcher, UHFReaderViewConfigure);
  31. break;
  32. case UHFReaderSubmenuIndexAbout:
  33. view_dispatcher_switch_to_view(App->ViewDispatcher, UHFReaderViewAbout);
  34. break;
  35. default:
  36. break;
  37. }
  38. }
  39. /**
  40. * @brief Allocates Main Menu
  41. * @details Allocates the main submenu with the read, saved, config, and about submenu items
  42. * @param app The UHFReaderApp object.
  43. */
  44. void main_menu_alloc(UHFReaderApp* App) {
  45. App->Submenu = submenu_alloc();
  46. submenu_set_header(App->Submenu, "UHF RFID Reader");
  47. submenu_add_item(
  48. App->Submenu, "Read", UHFReaderSubmenuIndexRead, uhf_reader_submenu_callback, App);
  49. submenu_add_item(
  50. App->Submenu, "Saved", UHFReaderSubmenuIndexSaved, uhf_reader_submenu_callback, App);
  51. submenu_add_item(
  52. App->Submenu, "Configure", UHFReaderSubmenuIndexConfig, uhf_reader_submenu_callback, App);
  53. submenu_add_item(
  54. App->Submenu, "About", UHFReaderSubmenuIndexAbout, uhf_reader_submenu_callback, App);
  55. view_set_previous_callback(
  56. submenu_get_view(App->Submenu), uhf_reader_navigation_exit_callback);
  57. view_dispatcher_add_view(
  58. App->ViewDispatcher, UHFReaderViewSubmenu, submenu_get_view(App->Submenu));
  59. view_dispatcher_switch_to_view(App->ViewDispatcher, UHFReaderViewSubmenu);
  60. }
  61. /**
  62. * @brief Allocate the UHF RFID Reader Application.
  63. * @details This function allocates all resources for the UHF RFID Reader Application.
  64. * @return UHFReaderApp object.
  65. */
  66. static UHFReaderApp* uhf_reader_app_alloc() {
  67. //Allocating storage for the saved_epcs and index file
  68. UHFReaderApp* App = (UHFReaderApp*)malloc(sizeof(UHFReaderApp));
  69. Storage* Storage = furi_record_open(RECORD_STORAGE);
  70. FlipperFormat* File = flipper_format_file_alloc(Storage);
  71. FlipperFormat* IndexFile = flipper_format_file_alloc(Storage);
  72. App->TagStorage = Storage;
  73. App->EpcFile = File;
  74. App->EpcIndexFile = IndexFile;
  75. //Initializing the arrays for storing all tag information from the read screen
  76. App->EpcValues = (char**)malloc(150 * 26);
  77. App->TidValues = (char**)malloc(150 * 41);
  78. App->ResValues = (char**)malloc(150 * 17);
  79. App->MemValues = (char**)malloc(150 * 33);
  80. App->EpcToSave = (char*)malloc(25);
  81. App->NumberOfEpcsToRead = 0;
  82. //Initializing the indices for each array and the file name
  83. App->NameSize = 36;
  84. App->NameSizeParse = 27;
  85. App->CurEpcIndex = 26;
  86. App->CurTidIndex = 1;
  87. App->CurResIndex = 1;
  88. App->CurMemIndex = 1;
  89. App->FileName = (char*)malloc(App->NameSize);
  90. //Creating the initial GUI
  91. Gui* Gui = furi_record_open(RECORD_GUI);
  92. App->ViewDispatcher = view_dispatcher_alloc();
  93. //view_dispatcher_enable_queue(App->ViewDispatcher);
  94. view_dispatcher_attach_to_gui(App->ViewDispatcher, Gui, ViewDispatcherTypeFullscreen);
  95. view_dispatcher_set_event_callback_context(App->ViewDispatcher, App);
  96. //Allocating the different views, menus, and widgets for the app
  97. main_menu_alloc(App);
  98. view_config_alloc(App);
  99. view_read_alloc(App);
  100. view_saved_menu_alloc(App);
  101. view_tag_actions_alloc(App);
  102. view_lock_alloc(App);
  103. view_kill_alloc(App);
  104. view_write_alloc(App);
  105. view_epc_alloc(App);
  106. view_epc_info_alloc(App);
  107. view_delete_alloc(App);
  108. view_delete_success_alloc(App);
  109. view_about_alloc(App);
  110. App->Notifications = furi_record_open(RECORD_NOTIFICATION);
  111. #ifdef BACKLIGHT_ON
  112. notification_message(App->Notifications, &sequence_display_backlight_enforce_on);
  113. #endif
  114. //Create the UART helper object used to communicate with the RPi Zero via UART
  115. App->UartHelper = uart_helper_alloc();
  116. uart_helper_set_baud_rate(App->UartHelper, DEVICE_BAUDRATE);
  117. uart_helper_set_delimiter(App->UartHelper, LINE_DELIMITER, INCLUDE_LINE_DELIMITER);
  118. uart_helper_set_callback(App->UartHelper, uart_demo_process_line, App);
  119. return App;
  120. }
  121. /**
  122. * @brief Free the UHFReaderApp application.
  123. * @details This function frees the UHF RFID Reader application resources.
  124. * @param app The UHFReaderApp application object.
  125. */
  126. static void uhf_reader_app_free(UHFReaderApp* App) {
  127. #ifdef BACKLIGHT_ON
  128. notification_message(App->Notifications, &sequence_display_backlight_enforce_auto);
  129. #endif
  130. //Freeing the notification and storage records
  131. furi_record_close(RECORD_NOTIFICATION);
  132. furi_record_close(RECORD_STORAGE);
  133. furi_record_close(RECORD_GUI);
  134. //Freeing the UART helper
  135. if(App->UHFModuleType != YRM100X_MODULE){
  136. uart_helper_free(App->UartHelper);
  137. }
  138. else{
  139. //Free Tag Wrapper
  140. uhf_tag_wrapper_free(App->YRM100XWorker->uhf_tag_wrapper);
  141. //Freeing yrm100x worker
  142. uhf_worker_stop(App->YRM100XWorker);
  143. uhf_worker_free(App->YRM100XWorker);
  144. }
  145. //Freeing all views, widgets, and menus
  146. view_delete_free(App);
  147. view_delete_success_free(App);
  148. view_about_free(App);
  149. view_epc_free(App);
  150. view_epc_info_free(App);
  151. view_read_free(App);
  152. view_write_free(App);
  153. view_config_free(App);
  154. view_saved_free(App);
  155. view_tag_actions_free(App);
  156. view_lock_free(App);
  157. view_kill_free(App);
  158. //Freeing the main menu view
  159. view_dispatcher_remove_view(App->ViewDispatcher, UHFReaderViewSubmenu);
  160. submenu_free(App->Submenu);
  161. view_dispatcher_free(App->ViewDispatcher);
  162. //Freeing variables used in app
  163. furi_string_free(App->EpcName);
  164. furi_string_free(App->EpcDelete);
  165. furi_string_free(App->EpcNameDelete);
  166. furi_string_free(App->EpcToWrite);
  167. free(App->EpcValues);
  168. free(App->TidValues);
  169. free(App->ResValues);
  170. free(App->MemValues);
  171. free(App->EpcToSave);
  172. free(App);
  173. }
  174. /**
  175. * @brief Main function for UHF RFID Reader application.
  176. * @details This function is the entry point for the UHF RFID Reader application.
  177. * @param _p Input parameter - unused
  178. * @return 0 - Success
  179. */
  180. int32_t main_uhf_reader_app(void* _p) {
  181. UNUSED(_p);
  182. Expansion* expansion = furi_record_open(RECORD_EXPANSION);
  183. expansion_disable(expansion);
  184. bool PowerOn = false;
  185. if(!furi_hal_power_is_otg_enabled()) {
  186. furi_hal_power_enable_otg();
  187. PowerOn = true;
  188. }
  189. UHFReaderApp* App = uhf_reader_app_alloc();
  190. view_dispatcher_run(App->ViewDispatcher);
  191. if(PowerOn) {
  192. furi_hal_power_disable_otg();
  193. }
  194. uhf_reader_app_free(App);
  195. expansion_enable(expansion);
  196. furi_record_close(RECORD_EXPANSION);
  197. return 0;
  198. }