radio_scanner_app.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423
  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 RADIO_SCANNER_DEFAULT_FREQ 433920000
  10. #define RADIO_SCANNER_DEFAULT_RSSI (-100.0f)
  11. #define RADIO_SCANNER_DEFAULT_SENSITIVITY (-85.0f)
  12. #define RADIO_SCANNER_BUFFER_SZ 32
  13. #define SUBGHZ_FREQUENCY_MIN 300000000
  14. #define SUBGHZ_FREQUENCY_MAX 928000000
  15. #define SUBGHZ_FREQUENCY_STEP 10000
  16. #define SUBGHZ_DEVICE_NAME "cc1101_int"
  17. static void radio_scanner_draw_callback(Canvas* canvas, void* context) {
  18. furi_assert(canvas);
  19. furi_assert(context);
  20. #ifdef FURI_DEBUG
  21. FURI_LOG_D(TAG, "Enter radio_scanner_draw_callback");
  22. #endif
  23. RadioScannerApp* app = (RadioScannerApp*)context;
  24. canvas_clear(canvas);
  25. canvas_set_font(canvas, FontPrimary);
  26. canvas_draw_str_aligned(canvas, 64, 2, AlignCenter, AlignTop, "Radio Scanner");
  27. canvas_set_font(canvas, FontSecondary);
  28. char freq_str[RADIO_SCANNER_BUFFER_SZ + 1] = {0};
  29. snprintf(freq_str, RADIO_SCANNER_BUFFER_SZ, "Freq: %.2f MHz", (double)app->frequency / 1000000);
  30. canvas_draw_str_aligned(canvas, 64, 18, AlignCenter, AlignTop, freq_str);
  31. char rssi_str[RADIO_SCANNER_BUFFER_SZ + 1] = {0};
  32. snprintf(rssi_str, RADIO_SCANNER_BUFFER_SZ, "RSSI: %.2f", (double)app->rssi);
  33. canvas_draw_str_aligned(canvas, 64, 30, AlignCenter, AlignTop, rssi_str);
  34. char sensitivity_str[RADIO_SCANNER_BUFFER_SZ + 1] = {0};
  35. snprintf(sensitivity_str, RADIO_SCANNER_BUFFER_SZ, "Sens: %.2f", (double)app->sensitivity);
  36. canvas_draw_str_aligned(canvas, 64, 42, AlignCenter, AlignTop, sensitivity_str);
  37. canvas_draw_str_aligned(
  38. canvas, 64, 54, AlignCenter, AlignTop, app->scanning ? "Scanning..." : "Locked");
  39. #ifdef FURI_DEBUG
  40. FURI_LOG_D(TAG, "Exit radio_scanner_draw_callback");
  41. #endif
  42. }
  43. static void radio_scanner_input_callback(InputEvent* input_event, void* context) {
  44. furi_assert(context);
  45. #ifdef FURI_DEBUG
  46. FURI_LOG_D(TAG, "Enter radio_scanner_input_callback");
  47. FURI_LOG_D(TAG, "Input event: type=%d, key=%d", input_event->type, input_event->key);
  48. #endif
  49. FuriMessageQueue* event_queue = context;
  50. furi_message_queue_put(event_queue, input_event, FuriWaitForever);
  51. FURI_LOG_D(TAG, "Exit radio_scanner_input_callback");
  52. }
  53. static void radio_scanner_rx_callback(const void* data, size_t size, void* context) {
  54. UNUSED(data);
  55. UNUSED(context);
  56. #ifdef FURI_DEBUG
  57. FURI_LOG_D(TAG, "radio_scanner_rx_callback called with size: %zu", size);
  58. #endif
  59. }
  60. static void radio_scanner_update_rssi(RadioScannerApp* app) {
  61. furi_assert(app);
  62. #ifdef FURI_DEBUG
  63. FURI_LOG_D(TAG, "Enter radio_scanner_update_rssi");
  64. #endif
  65. if(app->radio_device) {
  66. app->rssi = subghz_devices_get_rssi(app->radio_device);
  67. #ifdef FURI_DEBUG
  68. FURI_LOG_D(TAG, "Updated RSSI: %f", (double)app->rssi);
  69. #endif
  70. } else {
  71. FURI_LOG_E(TAG, "Radio device is NULL");
  72. app->rssi = RADIO_SCANNER_DEFAULT_RSSI;
  73. }
  74. #ifdef FURI_DEBUG
  75. FURI_LOG_D(TAG, "Exit radio_scanner_update_rssi");
  76. #endif
  77. }
  78. static bool radio_scanner_init_subghz(RadioScannerApp* app) {
  79. furi_assert(app);
  80. #ifdef FURI_DEBUG
  81. FURI_LOG_D(TAG, "Enter radio_scanner_init_subghz");
  82. #endif
  83. subghz_devices_init();
  84. #ifdef FURI_DEBUG
  85. FURI_LOG_D(TAG, "SubGHz devices initialized");
  86. #endif
  87. const SubGhzDevice* device = subghz_devices_get_by_name(SUBGHZ_DEVICE_NAME);
  88. if(!device) {
  89. FURI_LOG_E(TAG, "Failed to get SubGhzDevice");
  90. return false;
  91. }
  92. FURI_LOG_I(TAG, "SubGhzDevice obtained: %s", subghz_devices_get_name(device));
  93. app->radio_device = device;
  94. subghz_devices_begin(device);
  95. subghz_devices_reset(device);
  96. #ifdef FURI_DEBUG
  97. FURI_LOG_D(TAG, "SubGhzDevice begun");
  98. #endif
  99. if(!subghz_devices_is_frequency_valid(device, app->frequency)) {
  100. FURI_LOG_E(TAG, "Invalid frequency: %lu", app->frequency);
  101. return false;
  102. }
  103. #ifdef FURI_DEBUG
  104. FURI_LOG_D(TAG, "Frequency is valid: %lu", app->frequency);
  105. #endif
  106. subghz_devices_load_preset(device, FuriHalSubGhzPreset2FSKDev238Async, NULL);
  107. #ifdef FURI_DEBUG
  108. FURI_LOG_D(TAG, "Preset loaded");
  109. #endif
  110. subghz_devices_set_frequency(device, app->frequency);
  111. #ifdef FURI_DEBUG
  112. FURI_LOG_D(TAG, "Frequency set to %lu", app->frequency);
  113. #endif
  114. subghz_devices_start_async_rx(device, radio_scanner_rx_callback, app);
  115. #ifdef FURI_DEBUG
  116. FURI_LOG_D(TAG, "Asynchronous RX started");
  117. #endif
  118. if(furi_hal_speaker_acquire(30)) {
  119. app->speaker_acquired = true;
  120. subghz_devices_set_async_mirror_pin(device, &gpio_speaker);
  121. #ifdef FURI_DEBUG
  122. FURI_LOG_D(TAG, "Speaker acquired and async mirror pin set");
  123. #endif
  124. } else {
  125. app->speaker_acquired = false;
  126. FURI_LOG_E(TAG, "Failed to acquire speaker");
  127. }
  128. #ifdef FURI_DEBUG
  129. FURI_LOG_D(TAG, "Exit radio_scanner_init_subghz");
  130. #endif
  131. return true;
  132. }
  133. static void radio_scanner_process_scanning(RadioScannerApp* app) {
  134. furi_assert(app);
  135. #ifdef FURI_DEBUG
  136. FURI_LOG_D(TAG, "Enter radio_scanner_process_scanning");
  137. #endif
  138. radio_scanner_update_rssi(app);
  139. #ifdef FURI_DEBUG
  140. FURI_LOG_D(TAG, "RSSI after update: %f", (double)app->rssi);
  141. #endif
  142. bool signal_detected = (app->rssi > app->sensitivity);
  143. #ifdef FURI_DEBUG
  144. FURI_LOG_D(TAG, "Signal detected: %d", signal_detected);
  145. #endif
  146. if(signal_detected) {
  147. if(app->scanning) {
  148. app->scanning = false;
  149. #ifdef FURI_DEBUG
  150. FURI_LOG_D(TAG, "Scanning stopped");
  151. #endif
  152. }
  153. } else {
  154. if(!app->scanning) {
  155. app->scanning = true;
  156. #ifdef FURI_DEBUG
  157. FURI_LOG_D(TAG, "Scanning started");
  158. #endif
  159. }
  160. }
  161. if(!app->scanning) {
  162. #ifdef FURI_DEBUG
  163. FURI_LOG_D(TAG, "Exit radio_scanner_process_scanning");
  164. return;
  165. #endif
  166. }
  167. uint32_t new_frequency = (app->scan_direction == ScanDirectionUp) ?
  168. app->frequency + SUBGHZ_FREQUENCY_STEP :
  169. app->frequency - SUBGHZ_FREQUENCY_STEP;
  170. if(!subghz_devices_is_frequency_valid(app->radio_device, new_frequency)) {
  171. if(app->scan_direction == ScanDirectionUp) {
  172. if(new_frequency < 387000000) {
  173. new_frequency = 387000000;
  174. } else if(new_frequency < 779000000) {
  175. new_frequency = 779000000;
  176. } else if(new_frequency > SUBGHZ_FREQUENCY_MAX) {
  177. new_frequency = SUBGHZ_FREQUENCY_MIN;
  178. }
  179. } else {
  180. if(new_frequency > 464000000) {
  181. new_frequency = 464000000;
  182. } else if(new_frequency > 348000000) {
  183. new_frequency = 348000000;
  184. } else if(new_frequency < SUBGHZ_FREQUENCY_MIN) {
  185. new_frequency = SUBGHZ_FREQUENCY_MAX;
  186. }
  187. }
  188. #ifdef FURI_DEBUG
  189. FURI_LOG_D(TAG, "Adjusted frequency to next valid range: %lu", new_frequency);
  190. #endif
  191. }
  192. subghz_devices_flush_rx(app->radio_device);
  193. subghz_devices_stop_async_rx(app->radio_device);
  194. #ifdef FURI_DEBUG
  195. FURI_LOG_D(TAG, "Asynchronous RX stopped");
  196. #endif
  197. subghz_devices_idle(app->radio_device);
  198. #ifdef FURI_DEBUG
  199. FURI_LOG_D(TAG, "Device set to idle");
  200. #endif
  201. app->frequency = new_frequency;
  202. subghz_devices_set_frequency(app->radio_device, app->frequency);
  203. #ifdef FURI_DEBUG
  204. FURI_LOG_D(TAG, "Frequency set to %lu", app->frequency);
  205. #endif
  206. subghz_devices_start_async_rx(app->radio_device, radio_scanner_rx_callback, app);
  207. #ifdef FURI_DEBUG
  208. FURI_LOG_D(TAG, "Asynchronous RX restarted");
  209. FURI_LOG_D(TAG, "Exit radio_scanner_process_scanning");
  210. #endif
  211. }
  212. RadioScannerApp* radio_scanner_app_alloc() {
  213. #ifdef FURI_DEBUG
  214. FURI_LOG_D(TAG, "Enter radio_scanner_app_alloc");
  215. #endif
  216. RadioScannerApp* app = malloc(sizeof(RadioScannerApp));
  217. if(!app) {
  218. FURI_LOG_E(TAG, "Failed to allocate RadioScannerApp");
  219. return NULL;
  220. }
  221. #ifdef FURI_DEBUG
  222. FURI_LOG_D(TAG, "RadioScannerApp allocated");
  223. #endif
  224. app->view_port = view_port_alloc();
  225. #ifdef FURI_DEBUG
  226. FURI_LOG_D(TAG, "ViewPort allocated");
  227. #endif
  228. app->event_queue = furi_message_queue_alloc(8, sizeof(InputEvent));
  229. #ifdef FURI_DEBUG
  230. FURI_LOG_D(TAG, "Event queue allocated");
  231. #endif
  232. app->running = true;
  233. app->frequency = RADIO_SCANNER_DEFAULT_FREQ;
  234. app->rssi = RADIO_SCANNER_DEFAULT_RSSI;
  235. app->sensitivity = RADIO_SCANNER_DEFAULT_SENSITIVITY;
  236. app->scanning = true;
  237. app->scan_direction = ScanDirectionUp;
  238. app->speaker_acquired = false;
  239. app->radio_device = NULL;
  240. view_port_draw_callback_set(app->view_port, radio_scanner_draw_callback, app);
  241. #ifdef FURI_DEBUG
  242. FURI_LOG_D(TAG, "Draw callback set");
  243. #endif
  244. view_port_input_callback_set(app->view_port, radio_scanner_input_callback, app->event_queue);
  245. #ifdef FURI_DEBUG
  246. FURI_LOG_D(TAG, "Input callback set");
  247. FURI_LOG_D(TAG, "Exit radio_scanner_app_alloc");
  248. #endif
  249. app->gui = furi_record_open(RECORD_GUI);
  250. #ifdef FURI_DEBUG
  251. FURI_LOG_D(TAG, "GUI record opened");
  252. #endif
  253. gui_add_view_port(app->gui, app->view_port, GuiLayerFullscreen);
  254. #ifdef FURI_DEBUG
  255. FURI_LOG_D(TAG, "ViewPort added to GUI");
  256. #endif
  257. return app;
  258. }
  259. void radio_scanner_app_free(RadioScannerApp* app) {
  260. furi_assert(app);
  261. #ifdef FURI_DEBUG
  262. FURI_LOG_D(TAG, "Enter radio_scanner_app_free");
  263. #endif
  264. if(app->speaker_acquired && furi_hal_speaker_is_mine()) {
  265. subghz_devices_set_async_mirror_pin(app->radio_device, NULL);
  266. furi_hal_speaker_release();
  267. app->speaker_acquired = false;
  268. #ifdef FURI_DEBUG
  269. FURI_LOG_D(TAG, "Speaker released");
  270. #endif
  271. }
  272. if(app->radio_device) {
  273. subghz_devices_flush_rx(app->radio_device);
  274. subghz_devices_stop_async_rx(app->radio_device);
  275. #ifdef FURI_DEBUG
  276. FURI_LOG_D(TAG, "Asynchronous RX stopped");
  277. #endif
  278. subghz_devices_idle(app->radio_device);
  279. subghz_devices_sleep(app->radio_device);
  280. subghz_devices_end(app->radio_device);
  281. #ifdef FURI_DEBUG
  282. FURI_LOG_D(TAG, "SubGhzDevice stopped and ended");
  283. #endif
  284. }
  285. subghz_devices_deinit();
  286. #ifdef FURI_DEBUG
  287. FURI_LOG_D(TAG, "SubGHz devices de-initialized");
  288. #endif
  289. gui_remove_view_port(app->gui, app->view_port);
  290. #ifdef FURI_DEBUG
  291. FURI_LOG_D(TAG, "ViewPort removed from GUI");
  292. #endif
  293. view_port_free(app->view_port);
  294. #ifdef FURI_DEBUG
  295. FURI_LOG_D(TAG, "ViewPort freed");
  296. #endif
  297. furi_message_queue_free(app->event_queue);
  298. #ifdef FURI_DEBUG
  299. FURI_LOG_D(TAG, "Event queue freed");
  300. #endif
  301. furi_record_close(RECORD_GUI);
  302. #ifdef FURI_DEBUG
  303. FURI_LOG_D(TAG, "GUI record closed");
  304. #endif
  305. free(app);
  306. #ifdef FURI_DEBUG
  307. FURI_LOG_D(TAG, "RadioScannerApp memory freed");
  308. #endif
  309. }
  310. int32_t radio_scanner_app(void* p) {
  311. UNUSED(p);
  312. FURI_LOG_I(TAG, "Enter radio_scanner_app");
  313. RadioScannerApp* app = radio_scanner_app_alloc();
  314. if(!app) {
  315. FURI_LOG_E(TAG, "Failed to allocate app");
  316. return 1;
  317. }
  318. if(!radio_scanner_init_subghz(app)) {
  319. FURI_LOG_E(TAG, "Failed to initialize SubGHz");
  320. radio_scanner_app_free(app);
  321. return 255;
  322. }
  323. #ifdef FURI_DEBUG
  324. FURI_LOG_D(TAG, "SubGHz initialized successfully");
  325. #endif
  326. InputEvent event;
  327. while(app->running) {
  328. #ifdef FURI_DEBUG
  329. FURI_LOG_D(TAG, "Main loop iteration");
  330. #endif
  331. if(app->scanning) {
  332. #ifdef FURI_DEBUG
  333. FURI_LOG_D(TAG, "Scanning is active");
  334. #endif
  335. radio_scanner_process_scanning(app);
  336. } else {
  337. #ifdef FURI_DEBUG
  338. FURI_LOG_D(TAG, "Scanning is inactive, updating RSSI");
  339. #endif
  340. radio_scanner_update_rssi(app);
  341. }
  342. #ifdef FURI_DEBUG
  343. FURI_LOG_D(TAG, "Checking for input events");
  344. #endif
  345. if(furi_message_queue_get(app->event_queue, &event, 10) == FuriStatusOk) {
  346. #ifdef FURI_DEBUG
  347. FURI_LOG_D(TAG, "Input event received: type=%d, key=%d", event.type, event.key);
  348. #endif
  349. if(event.type == InputTypeShort) {
  350. if(event.key == InputKeyOk) {
  351. app->scanning = !app->scanning;
  352. FURI_LOG_I(TAG, "Toggled scanning: %d", app->scanning);
  353. } else if(event.key == InputKeyUp) {
  354. app->sensitivity += 1.0f;
  355. FURI_LOG_I(TAG, "Increased sensitivity: %f", (double)app->sensitivity);
  356. } else if(event.key == InputKeyDown) {
  357. app->sensitivity -= 1.0f;
  358. FURI_LOG_I(TAG, "Decreased sensitivity: %f", (double)app->sensitivity);
  359. } else if(event.key == InputKeyLeft) {
  360. app->scan_direction = ScanDirectionDown;
  361. FURI_LOG_I(TAG, "Scan direction set to down");
  362. } else if(event.key == InputKeyRight) {
  363. app->scan_direction = ScanDirectionUp;
  364. FURI_LOG_I(TAG, "Scan direction set to up");
  365. } else if(event.key == InputKeyBack) {
  366. app->running = false;
  367. FURI_LOG_I(TAG, "Exiting app");
  368. }
  369. }
  370. }
  371. view_port_update(app->view_port);
  372. furi_delay_ms(10);
  373. }
  374. radio_scanner_app_free(app);
  375. #ifdef FURI_DEBUG
  376. FURI_LOG_D(TAG, "Exit radio_scanner_app");
  377. #endif
  378. return 0;
  379. }