pocsag_pager_app.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. #include "pocsag_pager_app_i.h"
  2. #include <furi.h>
  3. #include <furi_hal.h>
  4. #include <lib/flipper_format/flipper_format.h>
  5. #include "protocols/protocol_items.h"
  6. static bool pocsag_pager_app_custom_event_callback(void* context, uint32_t event) {
  7. furi_assert(context);
  8. POCSAGPagerApp* app = context;
  9. return scene_manager_handle_custom_event(app->scene_manager, event);
  10. }
  11. static bool pocsag_pager_app_back_event_callback(void* context) {
  12. furi_assert(context);
  13. POCSAGPagerApp* app = context;
  14. return scene_manager_handle_back_event(app->scene_manager);
  15. }
  16. static void pocsag_pager_app_tick_event_callback(void* context) {
  17. furi_assert(context);
  18. POCSAGPagerApp* app = context;
  19. scene_manager_handle_tick_event(app->scene_manager);
  20. }
  21. POCSAGPagerApp* pocsag_pager_app_alloc() {
  22. POCSAGPagerApp* app = malloc(sizeof(POCSAGPagerApp));
  23. // GUI
  24. app->gui = furi_record_open(RECORD_GUI);
  25. // View Dispatcher
  26. app->view_dispatcher = view_dispatcher_alloc();
  27. app->scene_manager = scene_manager_alloc(&pocsag_pager_scene_handlers, app);
  28. view_dispatcher_set_event_callback_context(app->view_dispatcher, app);
  29. view_dispatcher_set_custom_event_callback(
  30. app->view_dispatcher, pocsag_pager_app_custom_event_callback);
  31. view_dispatcher_set_navigation_event_callback(
  32. app->view_dispatcher, pocsag_pager_app_back_event_callback);
  33. view_dispatcher_set_tick_event_callback(
  34. app->view_dispatcher, pocsag_pager_app_tick_event_callback, 100);
  35. view_dispatcher_attach_to_gui(app->view_dispatcher, app->gui, ViewDispatcherTypeFullscreen);
  36. // Open Notification record
  37. app->notifications = furi_record_open(RECORD_NOTIFICATION);
  38. // Variable Item List
  39. app->variable_item_list = variable_item_list_alloc();
  40. view_dispatcher_add_view(
  41. app->view_dispatcher,
  42. POCSAGPagerViewVariableItemList,
  43. variable_item_list_get_view(app->variable_item_list));
  44. // SubMenu
  45. app->submenu = submenu_alloc();
  46. view_dispatcher_add_view(
  47. app->view_dispatcher, POCSAGPagerViewSubmenu, submenu_get_view(app->submenu));
  48. // Widget
  49. app->widget = widget_alloc();
  50. view_dispatcher_add_view(
  51. app->view_dispatcher, POCSAGPagerViewWidget, widget_get_view(app->widget));
  52. // Receiver
  53. app->pcsg_receiver = pcsg_view_receiver_alloc();
  54. view_dispatcher_add_view(
  55. app->view_dispatcher,
  56. POCSAGPagerViewReceiver,
  57. pcsg_view_receiver_get_view(app->pcsg_receiver));
  58. // Receiver Info
  59. app->pcsg_receiver_info = pcsg_view_receiver_info_alloc();
  60. view_dispatcher_add_view(
  61. app->view_dispatcher,
  62. POCSAGPagerViewReceiverInfo,
  63. pcsg_view_receiver_info_get_view(app->pcsg_receiver_info));
  64. //init setting
  65. app->setting = subghz_setting_alloc();
  66. //ToDo FIX file name setting
  67. subghz_setting_load(app->setting, EXT_PATH("pocsag/settings.txt"));
  68. //init Worker & Protocol & History
  69. app->lock = PCSGLockOff;
  70. app->txrx = malloc(sizeof(POCSAGPagerTxRx));
  71. app->txrx->preset = malloc(sizeof(SubGhzRadioPreset));
  72. app->txrx->preset->name = furi_string_alloc();
  73. furi_hal_power_suppress_charge_enter();
  74. // Radio Devices init & load
  75. subghz_devices_init();
  76. app->txrx->radio_device =
  77. radio_device_loader_set(app->txrx->radio_device, SubGhzRadioDeviceTypeExternalCC1101);
  78. subghz_devices_reset(app->txrx->radio_device);
  79. subghz_devices_idle(app->txrx->radio_device);
  80. // Custom Presets load without using config file
  81. FlipperFormat* temp_fm_preset = flipper_format_string_alloc();
  82. flipper_format_write_string_cstr(
  83. temp_fm_preset,
  84. (const char*)"Custom_preset_data",
  85. (const char*)"02 0D 0B 06 08 32 07 04 14 00 13 02 12 04 11 83 10 67 15 24 18 18 19 16 1D 91 1C 00 1B 07 20 FB 22 10 21 56 00 00 C0 00 00 00 00 00 00 00");
  86. flipper_format_rewind(temp_fm_preset);
  87. subghz_setting_load_custom_preset(app->setting, (const char*)"FM95", temp_fm_preset);
  88. flipper_format_free(temp_fm_preset);
  89. // custom presets loading - end
  90. pcsg_preset_init(app, "FM95", 439987500, NULL, 0);
  91. app->txrx->hopper_state = PCSGHopperStateOFF;
  92. app->txrx->history = pcsg_history_alloc();
  93. app->txrx->worker = subghz_worker_alloc();
  94. app->txrx->environment = subghz_environment_alloc();
  95. subghz_environment_set_protocol_registry(
  96. app->txrx->environment, (void*)&pocsag_pager_protocol_registry);
  97. app->txrx->receiver = subghz_receiver_alloc_init(app->txrx->environment);
  98. subghz_receiver_set_filter(app->txrx->receiver, SubGhzProtocolFlag_Decodable);
  99. subghz_worker_set_overrun_callback(
  100. app->txrx->worker, (SubGhzWorkerOverrunCallback)subghz_receiver_reset);
  101. subghz_worker_set_pair_callback(
  102. app->txrx->worker, (SubGhzWorkerPairCallback)subghz_receiver_decode);
  103. subghz_worker_set_context(app->txrx->worker, app->txrx->receiver);
  104. scene_manager_next_scene(app->scene_manager, POCSAGPagerSceneStart);
  105. return app;
  106. }
  107. void pocsag_pager_app_free(POCSAGPagerApp* app) {
  108. furi_assert(app);
  109. // Radio Devices sleep & off
  110. pcsg_sleep(app);
  111. radio_device_loader_end(app->txrx->radio_device);
  112. subghz_devices_deinit();
  113. // Submenu
  114. view_dispatcher_remove_view(app->view_dispatcher, POCSAGPagerViewSubmenu);
  115. submenu_free(app->submenu);
  116. // Variable Item List
  117. view_dispatcher_remove_view(app->view_dispatcher, POCSAGPagerViewVariableItemList);
  118. variable_item_list_free(app->variable_item_list);
  119. // Widget
  120. view_dispatcher_remove_view(app->view_dispatcher, POCSAGPagerViewWidget);
  121. widget_free(app->widget);
  122. // Receiver
  123. view_dispatcher_remove_view(app->view_dispatcher, POCSAGPagerViewReceiver);
  124. pcsg_view_receiver_free(app->pcsg_receiver);
  125. // Receiver Info
  126. view_dispatcher_remove_view(app->view_dispatcher, POCSAGPagerViewReceiverInfo);
  127. pcsg_view_receiver_info_free(app->pcsg_receiver_info);
  128. //setting
  129. subghz_setting_free(app->setting);
  130. //Worker & Protocol & History
  131. subghz_receiver_free(app->txrx->receiver);
  132. subghz_environment_free(app->txrx->environment);
  133. pcsg_history_free(app->txrx->history);
  134. subghz_worker_free(app->txrx->worker);
  135. furi_string_free(app->txrx->preset->name);
  136. free(app->txrx->preset);
  137. free(app->txrx);
  138. // View dispatcher
  139. view_dispatcher_free(app->view_dispatcher);
  140. scene_manager_free(app->scene_manager);
  141. // Notifications
  142. furi_record_close(RECORD_NOTIFICATION);
  143. app->notifications = NULL;
  144. // Close records
  145. furi_record_close(RECORD_GUI);
  146. furi_hal_power_suppress_charge_exit();
  147. free(app);
  148. }
  149. int32_t pocsag_pager_app(void* p) {
  150. UNUSED(p);
  151. POCSAGPagerApp* pocsag_pager_app = pocsag_pager_app_alloc();
  152. view_dispatcher_run(pocsag_pager_app->view_dispatcher);
  153. pocsag_pager_app_free(pocsag_pager_app);
  154. return 0;
  155. }