radio_scanner_app.c 13 KB

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