radio_scanner_app.c 13 KB

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