radio_scanner_app.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. #include "radio_scanner_app.h"
  2. #include <furi.h>
  3. #include <furi_hal.h>
  4. #include <gui/elements.h>
  5. #include <furi_hal_subghz.h>
  6. #include <furi_hal_speaker.h>
  7. #define TAG "RadioScannerApp"
  8. #define SUBGHZ_FREQUENCY_MIN 300000000
  9. #define SUBGHZ_FREQUENCY_MAX 928000000
  10. #define SUBGHZ_FREQUENCY_STEP 10000
  11. static void radio_scanner_draw_callback(Canvas* canvas, void* context) {
  12. FURI_LOG_D(TAG, "Entering draw callback");
  13. RadioScannerApp* app = context;
  14. canvas_clear(canvas);
  15. canvas_set_font(canvas, FontPrimary);
  16. canvas_draw_str_aligned(canvas, 64, 2, AlignCenter, AlignTop, "Radio Scanner");
  17. canvas_set_font(canvas, FontSecondary);
  18. char freq_str[32];
  19. snprintf(freq_str, sizeof(freq_str), "Freq: %.2f MHz", (double)app->frequency / 1000000);
  20. canvas_draw_str_aligned(canvas, 64, 18, AlignCenter, AlignTop, freq_str);
  21. char rssi_str[32];
  22. snprintf(rssi_str, sizeof(rssi_str), "RSSI: %.2f", (double)app->rssi);
  23. canvas_draw_str_aligned(canvas, 64, 30, AlignCenter, AlignTop, rssi_str);
  24. char sensitivity_str[32];
  25. snprintf(sensitivity_str, sizeof(sensitivity_str), "Sens: %.2f", (double)app->sensitivity);
  26. canvas_draw_str_aligned(canvas, 64, 42, AlignCenter, AlignTop, sensitivity_str);
  27. canvas_draw_str_aligned(canvas, 64, 54, AlignCenter, AlignTop, app->scanning ? "Scanning..." : "Locked");
  28. FURI_LOG_D(TAG, "Exiting draw callback");
  29. }
  30. static void radio_scanner_input_callback(InputEvent* input_event, void* context) {
  31. FURI_LOG_D(TAG, "Entering input callback");
  32. furi_assert(context);
  33. FuriMessageQueue* event_queue = context;
  34. furi_message_queue_put(event_queue, input_event, FuriWaitForever);
  35. FURI_LOG_D(TAG, "Exiting input callback");
  36. }
  37. static void radio_scanner_update_rssi(RadioScannerApp* app) {
  38. FURI_LOG_D(TAG, "Updating RSSI");
  39. app->rssi = furi_hal_subghz_get_rssi();
  40. FURI_LOG_D(TAG, "RSSI updated: %.2f", (double)app->rssi);
  41. }
  42. static bool radio_scanner_init_subghz(RadioScannerApp* app) {
  43. FURI_LOG_D(TAG, "Initializing SubGhz");
  44. furi_assert(app);
  45. furi_hal_subghz_reset();
  46. furi_hal_subghz_idle();
  47. if(!furi_hal_subghz_is_frequency_valid(app->frequency)) {
  48. FURI_LOG_E(TAG, "Invalid frequency: %lu", app->frequency);
  49. return false;
  50. }
  51. FURI_LOG_D(TAG, "Setting frequency: %lu", app->frequency);
  52. furi_hal_subghz_set_frequency(app->frequency);
  53. FURI_LOG_D(TAG, "Frequency set");
  54. furi_hal_subghz_rx();
  55. FURI_LOG_D(TAG, "SubGhz set to RX mode");
  56. FURI_LOG_D(TAG, "SubGhz initialization complete");
  57. return true;
  58. }
  59. static void subghz_txrx_rx(RadioScannerApp* app) {
  60. FURI_LOG_D(TAG, "Entering RX mode");
  61. furi_hal_subghz_idle();
  62. furi_hal_subghz_set_frequency(app->frequency);
  63. furi_hal_subghz_rx();
  64. }
  65. static void subghz_txrx_rx_end(void) {
  66. FURI_LOG_D(TAG, "Ending RX mode");
  67. furi_hal_subghz_idle();
  68. }
  69. static bool speaker_on(RadioScannerApp* app) {
  70. FURI_LOG_D(TAG, "Turning on speaker");
  71. int retry_count = 0;
  72. const int max_retries = 5;
  73. while(retry_count < max_retries) {
  74. if(furi_hal_speaker_acquire(30)) {
  75. FURI_LOG_D(TAG, "Speaker acquired");
  76. if(app->radio_device) {
  77. subghz_devices_set_async_mirror_pin(app->radio_device, &gpio_speaker);
  78. FURI_LOG_D(TAG, "Speaker on");
  79. return true;
  80. } else {
  81. FURI_LOG_E(TAG, "Radio device is NULL, cannot set async mirror pin");
  82. }
  83. } else {
  84. FURI_LOG_W(TAG, "Failed to acquire speaker, retrying");
  85. retry_count++;
  86. furi_delay_ms(100);
  87. }
  88. }
  89. FURI_LOG_E(TAG, "Failed to acquire speaker after maximum retries");
  90. return false;
  91. }
  92. static void speaker_off(RadioScannerApp* app) {
  93. FURI_LOG_D(TAG, "Turning off speaker");
  94. if(furi_hal_speaker_is_mine()) {
  95. if(app->radio_device) {
  96. subghz_devices_set_async_mirror_pin(app->radio_device, NULL);
  97. FURI_LOG_D(TAG, "Stopped routing RF signal to speaker");
  98. }
  99. furi_hal_speaker_release();
  100. FURI_LOG_D(TAG, "Speaker off");
  101. }
  102. }
  103. static void radio_scanner_process_scanning(RadioScannerApp* app) {
  104. FURI_LOG_D(TAG, "Processing scanning");
  105. radio_scanner_update_rssi(app);
  106. bool signal_detected = (app->rssi > app->sensitivity);
  107. if(signal_detected) {
  108. FURI_LOG_I(TAG, "Signal detected above sensitivity threshold");
  109. if(app->scanning) {
  110. FURI_LOG_D(TAG, "Locking frequency due to signal detection");
  111. speaker_on(app);
  112. app->scanning = false;
  113. }
  114. } else {
  115. FURI_LOG_D(TAG, "No signal detected, continue scanning");
  116. app->scanning = true;
  117. }
  118. if(app->scanning) {
  119. uint32_t new_frequency;
  120. if(app->scan_direction == ScanDirectionUp) {
  121. new_frequency = app->frequency + SUBGHZ_FREQUENCY_STEP;
  122. } else {
  123. new_frequency = app->frequency - SUBGHZ_FREQUENCY_STEP;
  124. }
  125. FURI_LOG_D(TAG, "Calculated new frequency: %lu", new_frequency);
  126. if(new_frequency > SUBGHZ_FREQUENCY_MAX || new_frequency < SUBGHZ_FREQUENCY_MIN) {
  127. new_frequency = SUBGHZ_FREQUENCY_MIN;
  128. FURI_LOG_D(TAG, "Frequency reset to minimum: %lu", new_frequency);
  129. }
  130. if(furi_hal_subghz_is_frequency_valid(new_frequency)) {
  131. FURI_LOG_D(TAG, "Setting new frequency: %lu", new_frequency);
  132. subghz_txrx_rx_end();
  133. app->frequency = new_frequency;
  134. subghz_txrx_rx(app);
  135. } else {
  136. FURI_LOG_W(TAG, "Invalid frequency: %lu, skipping", new_frequency);
  137. app->frequency = SUBGHZ_FREQUENCY_MIN;
  138. }
  139. }
  140. FURI_LOG_D(TAG, "Scanning process complete");
  141. }
  142. RadioScannerApp* radio_scanner_app_alloc() {
  143. FURI_LOG_D(TAG, "Allocating RadioScannerApp");
  144. RadioScannerApp* app = malloc(sizeof(RadioScannerApp));
  145. app->view_port = view_port_alloc();
  146. app->event_queue = furi_message_queue_alloc(8, sizeof(InputEvent));
  147. app->radio_device = NULL;
  148. app->running = true;
  149. app->frequency = 433920000;
  150. app->rssi = -100.0f;
  151. app->sensitivity = -105.0f;
  152. app->scanning = true;
  153. app->scan_direction = ScanDirectionUp;
  154. view_port_draw_callback_set(app->view_port, radio_scanner_draw_callback, app);
  155. view_port_input_callback_set(app->view_port, radio_scanner_input_callback, app->event_queue);
  156. FURI_LOG_D(TAG, "RadioScannerApp allocated");
  157. return app;
  158. }
  159. void radio_scanner_app_free(RadioScannerApp* app) {
  160. FURI_LOG_D(TAG, "Freeing RadioScannerApp");
  161. furi_assert(app);
  162. view_port_free(app->view_port);
  163. furi_message_queue_free(app->event_queue);
  164. free(app);
  165. FURI_LOG_D(TAG, "RadioScannerApp freed");
  166. }
  167. int32_t radio_scanner_app(void* p) {
  168. UNUSED(p);
  169. FURI_LOG_D(TAG, "Starting RadioScannerApp");
  170. RadioScannerApp* app = radio_scanner_app_alloc();
  171. FURI_LOG_D(TAG, "Opening GUI");
  172. Gui* gui = furi_record_open(RECORD_GUI);
  173. gui_add_view_port(gui, app->view_port, GuiLayerFullscreen);
  174. FURI_LOG_D(TAG, "Initializing SubGhz");
  175. if(!radio_scanner_init_subghz(app)) {
  176. FURI_LOG_E(TAG, "Failed to initialize SubGhz");
  177. radio_scanner_app_free(app);
  178. return 255;
  179. }
  180. FURI_LOG_D(TAG, "Entering main loop");
  181. InputEvent event;
  182. while(app->running) {
  183. if(app->scanning) {
  184. radio_scanner_process_scanning(app);
  185. } else {
  186. radio_scanner_update_rssi(app);
  187. }
  188. if(furi_message_queue_get(app->event_queue, &event, 10) == FuriStatusOk) {
  189. FURI_LOG_D(TAG, "Input event received: %d", event.key);
  190. if(event.type == InputTypeShort) {
  191. if(event.key == InputKeyOk) {
  192. FURI_LOG_D(TAG, "OK button pressed");
  193. app->scanning = !app->scanning;
  194. } else if(event.key == InputKeyUp) {
  195. FURI_LOG_D(TAG, "Up button pressed, increasing sensitivity");
  196. app->sensitivity += 5.0f;
  197. FURI_LOG_D(TAG, "New sensitivity: %.2f", (double)app->sensitivity);
  198. } else if(event.key == InputKeyDown) {
  199. FURI_LOG_D(TAG, "Down button pressed, decreasing sensitivity");
  200. app->sensitivity -= 5.0f;
  201. FURI_LOG_D(TAG, "New sensitivity: %.2f", (double)app->sensitivity);
  202. } else if(event.key == InputKeyLeft) {
  203. FURI_LOG_D(TAG, "Left button pressed, changing scan direction to down");
  204. app->scan_direction = ScanDirectionDown;
  205. } else if(event.key == InputKeyRight) {
  206. FURI_LOG_D(TAG, "Right button pressed, changing scan direction to up");
  207. app->scan_direction = ScanDirectionUp;
  208. } else if(event.key == InputKeyBack) {
  209. FURI_LOG_D(TAG, "Back button pressed, exiting app");
  210. app->running = false;
  211. }
  212. }
  213. }
  214. view_port_update(app->view_port);
  215. furi_delay_ms(10);
  216. }
  217. FURI_LOG_D(TAG, "Exiting main loop");
  218. furi_hal_subghz_idle();
  219. furi_hal_subghz_sleep();
  220. speaker_off(app);
  221. FURI_LOG_D(TAG, "Removing view port");
  222. gui_remove_view_port(gui, app->view_port);
  223. furi_record_close(RECORD_GUI);
  224. FURI_LOG_D(TAG, "Freeing app resources");
  225. radio_scanner_app_free(app);
  226. FURI_LOG_I(TAG, "RadioScannerApp finished");
  227. return 0;
  228. }