radio_scanner_app.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  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(!subghz_devices_is_frequency_valid(app->radio_device, new_frequency)) {
  114. if(app->scan_direction == ScanDirectionUp) {
  115. if(new_frequency < 348000000) {
  116. new_frequency = 387000000;
  117. } else if(new_frequency < 464000000) {
  118. new_frequency = 779000000;
  119. } else {
  120. new_frequency = 300000000;
  121. }
  122. } else {
  123. if(new_frequency > 779000000) {
  124. new_frequency = 464000000;
  125. } else if(new_frequency > 387000000) {
  126. new_frequency = 348000000;
  127. } else {
  128. new_frequency = 928000000;
  129. }
  130. }
  131. FURI_LOG_D(TAG, "Adjusted frequency to next valid range: %lu", new_frequency);
  132. }
  133. subghz_devices_stop_async_rx(app->radio_device);
  134. FURI_LOG_D(TAG, "Asynchronous RX stopped");
  135. subghz_devices_idle(app->radio_device);
  136. FURI_LOG_D(TAG, "Device set to idle");
  137. app->frequency = new_frequency;
  138. subghz_devices_set_frequency(app->radio_device, app->frequency);
  139. FURI_LOG_D(TAG, "Frequency set to %lu", app->frequency);
  140. subghz_devices_start_async_rx(app->radio_device, radio_scanner_rx_callback, app);
  141. FURI_LOG_D(TAG, "Asynchronous RX restarted");
  142. }
  143. FURI_LOG_D(TAG, "Exit radio_scanner_process_scanning");
  144. }
  145. RadioScannerApp* radio_scanner_app_alloc() {
  146. FURI_LOG_D(TAG, "Enter radio_scanner_app_alloc");
  147. RadioScannerApp* app = malloc(sizeof(RadioScannerApp));
  148. if(!app) {
  149. FURI_LOG_E(TAG, "Failed to allocate RadioScannerApp");
  150. return NULL;
  151. }
  152. FURI_LOG_D(TAG, "RadioScannerApp allocated");
  153. app->view_port = view_port_alloc();
  154. FURI_LOG_D(TAG, "ViewPort allocated");
  155. app->event_queue = furi_message_queue_alloc(8, sizeof(InputEvent));
  156. FURI_LOG_D(TAG, "Event queue allocated");
  157. app->running = true;
  158. app->frequency = 433920000;
  159. app->rssi = -100.0f;
  160. app->sensitivity = -85.0f;
  161. app->scanning = true;
  162. app->scan_direction = ScanDirectionUp;
  163. app->speaker_acquired = false;
  164. app->radio_device = NULL;
  165. view_port_draw_callback_set(app->view_port, radio_scanner_draw_callback, app);
  166. FURI_LOG_D(TAG, "Draw callback set");
  167. view_port_input_callback_set(app->view_port, radio_scanner_input_callback, app->event_queue);
  168. FURI_LOG_D(TAG, "Input callback set");
  169. FURI_LOG_D(TAG, "Exit radio_scanner_app_alloc");
  170. return app;
  171. }
  172. void radio_scanner_app_free(RadioScannerApp* app) {
  173. FURI_LOG_D(TAG, "Enter radio_scanner_app_free");
  174. if(app->speaker_acquired && furi_hal_speaker_is_mine()) {
  175. subghz_devices_set_async_mirror_pin(app->radio_device, NULL);
  176. furi_hal_speaker_release();
  177. app->speaker_acquired = false;
  178. FURI_LOG_D(TAG, "Speaker released");
  179. }
  180. if(app->radio_device) {
  181. subghz_devices_stop_async_rx(app->radio_device);
  182. FURI_LOG_D(TAG, "Asynchronous RX stopped");
  183. subghz_devices_end(app->radio_device);
  184. FURI_LOG_D(TAG, "SubGhzDevice stopped and ended");
  185. }
  186. subghz_devices_deinit();
  187. FURI_LOG_D(TAG, "SubGHz devices deinitialized");
  188. view_port_free(app->view_port);
  189. FURI_LOG_D(TAG, "ViewPort freed");
  190. furi_message_queue_free(app->event_queue);
  191. FURI_LOG_D(TAG, "Event queue freed");
  192. free(app);
  193. FURI_LOG_D(TAG, "RadioScannerApp memory freed");
  194. }
  195. int32_t radio_scanner_app(void* p) {
  196. UNUSED(p);
  197. FURI_LOG_D(TAG, "Enter radio_scanner_app");
  198. RadioScannerApp* app = radio_scanner_app_alloc();
  199. if(!app) {
  200. FURI_LOG_E(TAG, "Failed to allocate app");
  201. return -1;
  202. }
  203. Gui* gui = furi_record_open(RECORD_GUI);
  204. FURI_LOG_D(TAG, "GUI record opened");
  205. gui_add_view_port(gui, app->view_port, GuiLayerFullscreen);
  206. FURI_LOG_D(TAG, "ViewPort added to GUI");
  207. if(!radio_scanner_init_subghz(app)) {
  208. FURI_LOG_E(TAG, "Failed to initialize SubGHz");
  209. radio_scanner_app_free(app);
  210. return 255;
  211. }
  212. FURI_LOG_D(TAG, "SubGHz initialized successfully");
  213. InputEvent event;
  214. while(app->running) {
  215. FURI_LOG_D(TAG, "Main loop iteration");
  216. if(app->scanning) {
  217. FURI_LOG_D(TAG, "Scanning is active");
  218. radio_scanner_process_scanning(app);
  219. } else {
  220. FURI_LOG_D(TAG, "Scanning is inactive, updating RSSI");
  221. radio_scanner_update_rssi(app);
  222. }
  223. FURI_LOG_D(TAG, "Checking for input events");
  224. if(furi_message_queue_get(app->event_queue, &event, 10) == FuriStatusOk) {
  225. FURI_LOG_D(TAG, "Input event received: type=%d, key=%d", event.type, event.key);
  226. if(event.type == InputTypeShort) {
  227. if(event.key == InputKeyOk) {
  228. app->scanning = !app->scanning;
  229. FURI_LOG_D(TAG, "Toggled scanning: %d", app->scanning);
  230. } else if(event.key == InputKeyUp) {
  231. app->sensitivity += 1.0f;
  232. FURI_LOG_D(TAG, "Increased sensitivity: %f", (double)app->sensitivity);
  233. } else if(event.key == InputKeyDown) {
  234. app->sensitivity -= 1.0f;
  235. FURI_LOG_D(TAG, "Decreased sensitivity: %f", (double)app->sensitivity);
  236. } else if(event.key == InputKeyLeft) {
  237. app->scan_direction = ScanDirectionDown;
  238. FURI_LOG_D(TAG, "Scan direction set to down");
  239. } else if(event.key == InputKeyRight) {
  240. app->scan_direction = ScanDirectionUp;
  241. FURI_LOG_D(TAG, "Scan direction set to up");
  242. } else if(event.key == InputKeyBack) {
  243. app->running = false;
  244. FURI_LOG_D(TAG, "Exiting app");
  245. }
  246. }
  247. }
  248. view_port_update(app->view_port);
  249. furi_delay_ms(10);
  250. }
  251. if(app->speaker_acquired && furi_hal_speaker_is_mine()) {
  252. subghz_devices_set_async_mirror_pin(app->radio_device, NULL);
  253. furi_hal_speaker_release();
  254. app->speaker_acquired = false;
  255. FURI_LOG_D(TAG, "Speaker released at app exit");
  256. }
  257. gui_remove_view_port(gui, app->view_port);
  258. FURI_LOG_D(TAG, "ViewPort removed from GUI");
  259. furi_record_close(RECORD_GUI);
  260. FURI_LOG_D(TAG, "GUI record closed");
  261. radio_scanner_app_free(app);
  262. FURI_LOG_D(TAG, "Exit radio_scanner_app");
  263. return 0;
  264. }