radio_scanner_app.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  1. #include "radio_scanner_app.h"
  2. #include <furi.h>
  3. #include <furi_hal.h>
  4. #include <furi_hal_gpio.h>
  5. #include <gui/elements.h>
  6. #include <furi_hal_speaker.h>
  7. #include <subghz/devices/devices.h>
  8. #define TAG "RadioScannerApp"
  9. #define SUBGHZ_FREQUENCY_MIN 300000000
  10. #define SUBGHZ_FREQUENCY_MAX 928000000
  11. #define SUBGHZ_FREQUENCY_STEP 10000
  12. #define SUBGHZ_DEVICE_NAME "cc1101_int"
  13. static void radio_scanner_draw_callback(Canvas* canvas, void* context) {
  14. FURI_LOG_D(TAG, "Enter radio_scanner_draw_callback");
  15. RadioScannerApp* app = context;
  16. canvas_clear(canvas);
  17. canvas_set_font(canvas, FontPrimary);
  18. canvas_draw_str_aligned(canvas, 64, 2, AlignCenter, AlignTop, "Radio Scanner");
  19. canvas_set_font(canvas, FontSecondary);
  20. char freq_str[32];
  21. snprintf(freq_str, sizeof(freq_str), "Freq: %.2f MHz", (double)app->frequency / 1000000);
  22. canvas_draw_str_aligned(canvas, 64, 18, AlignCenter, AlignTop, freq_str);
  23. char rssi_str[32];
  24. snprintf(rssi_str, sizeof(rssi_str), "RSSI: %.2f", (double)app->rssi);
  25. canvas_draw_str_aligned(canvas, 64, 30, AlignCenter, AlignTop, rssi_str);
  26. char sensitivity_str[32];
  27. snprintf(sensitivity_str, sizeof(sensitivity_str), "Sens: %.2f", (double)app->sensitivity);
  28. canvas_draw_str_aligned(canvas, 64, 42, AlignCenter, AlignTop, sensitivity_str);
  29. canvas_draw_str_aligned(
  30. canvas, 64, 54, AlignCenter, AlignTop, app->scanning ? "Scanning..." : "Locked");
  31. FURI_LOG_D(TAG, "Exit radio_scanner_draw_callback");
  32. }
  33. static void radio_scanner_input_callback(InputEvent* input_event, void* context) {
  34. furi_assert(context);
  35. FURI_LOG_D(TAG, "Enter radio_scanner_input_callback");
  36. FURI_LOG_D(TAG, "Input event: type=%d, key=%d", input_event->type, input_event->key);
  37. FuriMessageQueue* event_queue = context;
  38. furi_message_queue_put(event_queue, input_event, FuriWaitForever);
  39. FURI_LOG_D(TAG, "Exit radio_scanner_input_callback");
  40. }
  41. static void radio_scanner_rx_callback(const void* data, size_t size, void* context) {
  42. (void)data;
  43. (void)context;
  44. FURI_LOG_D(TAG, "radio_scanner_rx_callback called with size: %zu", size);
  45. }
  46. static void radio_scanner_update_rssi(RadioScannerApp* app) {
  47. FURI_LOG_D(TAG, "Enter radio_scanner_update_rssi");
  48. if(app->radio_device) {
  49. app->rssi = subghz_devices_get_rssi(app->radio_device);
  50. FURI_LOG_D(TAG, "Updated RSSI: %f", (double)app->rssi);
  51. } else {
  52. FURI_LOG_E(TAG, "Radio device is NULL");
  53. app->rssi = -127.0f;
  54. }
  55. FURI_LOG_D(TAG, "Exit radio_scanner_update_rssi");
  56. }
  57. static bool radio_scanner_init_subghz(RadioScannerApp* app) {
  58. FURI_LOG_D(TAG, "Enter radio_scanner_init_subghz");
  59. subghz_devices_init();
  60. FURI_LOG_D(TAG, "SubGHz devices initialized");
  61. const SubGhzDevice* device = subghz_devices_get_by_name(SUBGHZ_DEVICE_NAME);
  62. if(!device) {
  63. FURI_LOG_E(TAG, "Failed to get SubGhzDevice");
  64. return false;
  65. }
  66. FURI_LOG_D(TAG, "SubGhzDevice obtained: %s", subghz_devices_get_name(device));
  67. app->radio_device = device;
  68. subghz_devices_begin(device);
  69. FURI_LOG_D(TAG, "SubGhzDevice begun");
  70. if(!subghz_devices_is_frequency_valid(device, app->frequency)) {
  71. FURI_LOG_E(TAG, "Invalid frequency: %lu", app->frequency);
  72. return false;
  73. }
  74. FURI_LOG_D(TAG, "Frequency is valid: %lu", app->frequency);
  75. subghz_devices_load_preset(device, FuriHalSubGhzPreset2FSKDev238Async, NULL);
  76. FURI_LOG_D(TAG, "Preset loaded");
  77. subghz_devices_set_frequency(device, app->frequency);
  78. FURI_LOG_D(TAG, "Frequency set to %lu", app->frequency);
  79. subghz_devices_start_async_rx(device, radio_scanner_rx_callback, app);
  80. FURI_LOG_D(TAG, "Asynchronous RX started");
  81. if(furi_hal_speaker_acquire(30)) {
  82. app->speaker_acquired = true;
  83. subghz_devices_set_async_mirror_pin(device, &gpio_speaker);
  84. FURI_LOG_D(TAG, "Speaker acquired and async mirror pin set");
  85. } else {
  86. app->speaker_acquired = false;
  87. FURI_LOG_E(TAG, "Failed to acquire speaker");
  88. }
  89. FURI_LOG_D(TAG, "Exit radio_scanner_init_subghz");
  90. return true;
  91. }
  92. static void radio_scanner_process_scanning(RadioScannerApp* app) {
  93. FURI_LOG_D(TAG, "Enter radio_scanner_process_scanning");
  94. radio_scanner_update_rssi(app);
  95. FURI_LOG_D(TAG, "RSSI after update: %f", (double)app->rssi);
  96. bool signal_detected = (app->rssi > app->sensitivity);
  97. FURI_LOG_D(TAG, "Signal detected: %d", signal_detected);
  98. if(signal_detected) {
  99. if(app->scanning) {
  100. app->scanning = false;
  101. FURI_LOG_D(TAG, "Scanning stopped");
  102. }
  103. } else {
  104. if(!app->scanning) {
  105. app->scanning = true;
  106. FURI_LOG_D(TAG, "Scanning started");
  107. }
  108. }
  109. if(app->scanning) {
  110. uint32_t new_frequency = (app->scan_direction == ScanDirectionUp)
  111. ? app->frequency + SUBGHZ_FREQUENCY_STEP
  112. : app->frequency - SUBGHZ_FREQUENCY_STEP;
  113. if(new_frequency > SUBGHZ_FREQUENCY_MAX || new_frequency < SUBGHZ_FREQUENCY_MIN) {
  114. new_frequency = SUBGHZ_FREQUENCY_MIN;
  115. FURI_LOG_D(TAG, "Frequency reset to minimum");
  116. }
  117. subghz_devices_stop_async_rx(app->radio_device);
  118. FURI_LOG_D(TAG, "Asynchronous RX stopped");
  119. subghz_devices_idle(app->radio_device);
  120. FURI_LOG_D(TAG, "Device set to idle");
  121. subghz_devices_set_frequency(app->radio_device, new_frequency);
  122. app->frequency = new_frequency;
  123. FURI_LOG_D(TAG, "Frequency set to %lu", app->frequency);
  124. subghz_devices_start_async_rx(app->radio_device, radio_scanner_rx_callback, app);
  125. FURI_LOG_D(TAG, "Asynchronous RX restarted");
  126. }
  127. FURI_LOG_D(TAG, "Exit radio_scanner_process_scanning");
  128. }
  129. RadioScannerApp* radio_scanner_app_alloc() {
  130. FURI_LOG_D(TAG, "Enter radio_scanner_app_alloc");
  131. RadioScannerApp* app = malloc(sizeof(RadioScannerApp));
  132. if(!app) {
  133. FURI_LOG_E(TAG, "Failed to allocate RadioScannerApp");
  134. return NULL;
  135. }
  136. FURI_LOG_D(TAG, "RadioScannerApp allocated");
  137. app->view_port = view_port_alloc();
  138. FURI_LOG_D(TAG, "ViewPort allocated");
  139. app->event_queue = furi_message_queue_alloc(8, sizeof(InputEvent));
  140. FURI_LOG_D(TAG, "Event queue allocated");
  141. app->running = true;
  142. app->frequency = 433920000;
  143. app->rssi = -100.0f;
  144. app->sensitivity = -70.0f;
  145. app->scanning = true;
  146. app->scan_direction = ScanDirectionUp;
  147. app->speaker_acquired = false;
  148. app->radio_device = NULL;
  149. view_port_draw_callback_set(app->view_port, radio_scanner_draw_callback, app);
  150. FURI_LOG_D(TAG, "Draw callback set");
  151. view_port_input_callback_set(app->view_port, radio_scanner_input_callback, app->event_queue);
  152. FURI_LOG_D(TAG, "Input callback set");
  153. FURI_LOG_D(TAG, "Exit radio_scanner_app_alloc");
  154. return app;
  155. }
  156. void radio_scanner_app_free(RadioScannerApp* app) {
  157. FURI_LOG_D(TAG, "Enter radio_scanner_app_free");
  158. if(app->speaker_acquired && furi_hal_speaker_is_mine()) {
  159. subghz_devices_set_async_mirror_pin(app->radio_device, NULL);
  160. furi_hal_speaker_release();
  161. app->speaker_acquired = false;
  162. FURI_LOG_D(TAG, "Speaker released");
  163. }
  164. if(app->radio_device) {
  165. subghz_devices_stop_async_rx(app->radio_device);
  166. FURI_LOG_D(TAG, "Asynchronous RX stopped");
  167. subghz_devices_end(app->radio_device);
  168. FURI_LOG_D(TAG, "SubGhzDevice stopped and ended");
  169. }
  170. subghz_devices_deinit();
  171. FURI_LOG_D(TAG, "SubGHz devices deinitialized");
  172. view_port_free(app->view_port);
  173. FURI_LOG_D(TAG, "ViewPort freed");
  174. furi_message_queue_free(app->event_queue);
  175. FURI_LOG_D(TAG, "Event queue freed");
  176. free(app);
  177. FURI_LOG_D(TAG, "RadioScannerApp memory freed");
  178. }
  179. int32_t radio_scanner_app(void* p) {
  180. UNUSED(p);
  181. FURI_LOG_D(TAG, "Enter radio_scanner_app");
  182. RadioScannerApp* app = radio_scanner_app_alloc();
  183. if(!app) {
  184. FURI_LOG_E(TAG, "Failed to allocate app");
  185. return -1;
  186. }
  187. Gui* gui = furi_record_open(RECORD_GUI);
  188. FURI_LOG_D(TAG, "GUI record opened");
  189. gui_add_view_port(gui, app->view_port, GuiLayerFullscreen);
  190. FURI_LOG_D(TAG, "ViewPort added to GUI");
  191. if(!radio_scanner_init_subghz(app)) {
  192. FURI_LOG_E(TAG, "Failed to initialize SubGHz");
  193. radio_scanner_app_free(app);
  194. return 255;
  195. }
  196. FURI_LOG_D(TAG, "SubGHz initialized successfully");
  197. InputEvent event;
  198. while(app->running) {
  199. FURI_LOG_D(TAG, "Main loop iteration");
  200. if(app->scanning) {
  201. FURI_LOG_D(TAG, "Scanning is active");
  202. radio_scanner_process_scanning(app);
  203. } else {
  204. FURI_LOG_D(TAG, "Scanning is inactive, updating RSSI");
  205. radio_scanner_update_rssi(app);
  206. }
  207. FURI_LOG_D(TAG, "Checking for input events");
  208. if(furi_message_queue_get(app->event_queue, &event, 10) == FuriStatusOk) {
  209. FURI_LOG_D(TAG, "Input event received: type=%d, key=%d", event.type, event.key);
  210. if(event.type == InputTypeShort) {
  211. if(event.key == InputKeyOk) {
  212. app->scanning = !app->scanning;
  213. FURI_LOG_D(TAG, "Toggled scanning: %d", app->scanning);
  214. } else if(event.key == InputKeyUp) {
  215. app->sensitivity += 1.0f;
  216. FURI_LOG_D(TAG, "Increased sensitivity: %f", (double)app->sensitivity);
  217. } else if(event.key == InputKeyDown) {
  218. app->sensitivity -= 1.0f;
  219. FURI_LOG_D(TAG, "Decreased sensitivity: %f", (double)app->sensitivity);
  220. } else if(event.key == InputKeyLeft) {
  221. app->scan_direction = ScanDirectionDown;
  222. FURI_LOG_D(TAG, "Scan direction set to down");
  223. } else if(event.key == InputKeyRight) {
  224. app->scan_direction = ScanDirectionUp;
  225. FURI_LOG_D(TAG, "Scan direction set to up");
  226. } else if(event.key == InputKeyBack) {
  227. app->running = false;
  228. FURI_LOG_D(TAG, "Exiting app");
  229. }
  230. }
  231. }
  232. view_port_update(app->view_port);
  233. furi_delay_ms(10);
  234. }
  235. if(app->speaker_acquired && furi_hal_speaker_is_mine()) {
  236. subghz_devices_set_async_mirror_pin(app->radio_device, NULL);
  237. furi_hal_speaker_release();
  238. app->speaker_acquired = false;
  239. FURI_LOG_D(TAG, "Speaker released at app exit");
  240. }
  241. gui_remove_view_port(gui, app->view_port);
  242. FURI_LOG_D(TAG, "ViewPort removed from GUI");
  243. furi_record_close(RECORD_GUI);
  244. FURI_LOG_D(TAG, "GUI record closed");
  245. radio_scanner_app_free(app);
  246. FURI_LOG_D(TAG, "Exit radio_scanner_app");
  247. return 0;
  248. }