subghz_txrx.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613
  1. #include "subghz_txrx_i.h" // IWYU pragma: keep
  2. #include <lib/subghz/subghz_protocol_registry.h>
  3. #include <applications/drivers/subghz/cc1101_ext/cc1101_ext_interconnect.h>
  4. #include <lib/subghz/devices/cc1101_int/cc1101_int_interconnect.h>
  5. #define TAG "SubGhz"
  6. static void subghz_txrx_radio_device_power_on(SubGhzTxRx* instance) {
  7. UNUSED(instance);
  8. uint8_t attempts = 5;
  9. while(--attempts > 0) {
  10. if(furi_hal_power_enable_otg()) break;
  11. }
  12. if(attempts == 0) {
  13. if(furi_hal_power_get_usb_voltage() < 4.5f) {
  14. FURI_LOG_E(
  15. TAG,
  16. "Error power otg enable. BQ2589 check otg fault = %d",
  17. furi_hal_power_check_otg_fault() ? 1 : 0);
  18. }
  19. }
  20. }
  21. static void subghz_txrx_radio_device_power_off(SubGhzTxRx* instance) {
  22. UNUSED(instance);
  23. if(furi_hal_power_is_otg_enabled()) furi_hal_power_disable_otg();
  24. }
  25. SubGhzTxRx* subghz_txrx_alloc(void) {
  26. SubGhzTxRx* instance = malloc(sizeof(SubGhzTxRx));
  27. instance->setting = subghz_setting_alloc();
  28. subghz_setting_load(instance->setting, EXT_PATH("subghz/assets/setting_user"));
  29. instance->preset = malloc(sizeof(SubGhzRadioPreset));
  30. instance->preset->name = furi_string_alloc();
  31. subghz_txrx_set_preset(
  32. instance, "AM650", subghz_setting_get_default_frequency(instance->setting), NULL, 0);
  33. instance->txrx_state = SubGhzTxRxStateSleep;
  34. subghz_txrx_hopper_set_state(instance, SubGhzHopperStateOFF);
  35. subghz_txrx_speaker_set_state(instance, SubGhzSpeakerStateDisable);
  36. instance->worker = subghz_worker_alloc();
  37. instance->fff_data = flipper_format_string_alloc();
  38. instance->environment = subghz_environment_alloc();
  39. instance->is_database_loaded =
  40. subghz_environment_load_keystore(instance->environment, SUBGHZ_KEYSTORE_DIR_NAME);
  41. subghz_environment_load_keystore(instance->environment, SUBGHZ_KEYSTORE_DIR_USER_NAME);
  42. subghz_environment_set_came_atomo_rainbow_table_file_name(
  43. instance->environment, SUBGHZ_CAME_ATOMO_DIR_NAME);
  44. subghz_environment_set_alutech_at_4n_rainbow_table_file_name(
  45. instance->environment, SUBGHZ_ALUTECH_AT_4N_DIR_NAME);
  46. subghz_environment_set_nice_flor_s_rainbow_table_file_name(
  47. instance->environment, SUBGHZ_NICE_FLOR_S_DIR_NAME);
  48. subghz_environment_set_protocol_registry(
  49. instance->environment, (void*)&subghz_protocol_registry);
  50. instance->receiver = subghz_receiver_alloc_init(instance->environment);
  51. subghz_worker_set_overrun_callback(
  52. instance->worker, (SubGhzWorkerOverrunCallback)subghz_receiver_reset);
  53. subghz_worker_set_pair_callback(
  54. instance->worker, (SubGhzWorkerPairCallback)subghz_receiver_decode);
  55. subghz_worker_set_context(instance->worker, instance->receiver);
  56. //set default device External
  57. subghz_devices_init();
  58. instance->radio_device_type = SubGhzRadioDeviceTypeInternal;
  59. instance->radio_device_type =
  60. subghz_txrx_radio_device_set(instance, SubGhzRadioDeviceTypeExternalCC1101);
  61. FURI_LOG_I(
  62. TAG,
  63. "radio device type = %s",
  64. instance->radio_device_type == SubGhzRadioDeviceTypeInternal ? "Internal" : "External");
  65. return instance;
  66. }
  67. void subghz_txrx_free(SubGhzTxRx* instance) {
  68. furi_assert(instance);
  69. if(instance->radio_device_type != SubGhzRadioDeviceTypeInternal) {
  70. subghz_txrx_radio_device_power_off(instance);
  71. subghz_devices_end(instance->radio_device);
  72. }
  73. subghz_devices_deinit();
  74. subghz_worker_free(instance->worker);
  75. subghz_receiver_free(instance->receiver);
  76. subghz_environment_free(instance->environment);
  77. flipper_format_free(instance->fff_data);
  78. furi_string_free(instance->preset->name);
  79. subghz_setting_free(instance->setting);
  80. free(instance->preset);
  81. free(instance);
  82. }
  83. bool subghz_txrx_is_database_loaded(SubGhzTxRx* instance) {
  84. furi_assert(instance);
  85. return instance->is_database_loaded;
  86. }
  87. void subghz_txrx_set_preset(
  88. SubGhzTxRx* instance,
  89. const char* preset_name,
  90. uint32_t frequency,
  91. uint8_t* preset_data,
  92. size_t preset_data_size) {
  93. furi_assert(instance);
  94. furi_string_set(instance->preset->name, preset_name);
  95. SubGhzRadioPreset* preset = instance->preset;
  96. preset->frequency = frequency;
  97. preset->data = preset_data;
  98. preset->data_size = preset_data_size;
  99. }
  100. const char* subghz_txrx_get_preset_name(SubGhzTxRx* instance, const char* preset) {
  101. UNUSED(instance);
  102. const char* preset_name = "";
  103. if(!strcmp(preset, "FuriHalSubGhzPresetOok270Async")) {
  104. preset_name = "AM270";
  105. } else if(!strcmp(preset, "FuriHalSubGhzPresetOok650Async")) {
  106. preset_name = "AM650";
  107. } else if(!strcmp(preset, "FuriHalSubGhzPreset2FSKDev238Async")) {
  108. preset_name = "FM238";
  109. } else if(!strcmp(preset, "FuriHalSubGhzPreset2FSKDev476Async")) {
  110. preset_name = "FM476";
  111. } else if(!strcmp(preset, "FuriHalSubGhzPresetCustom")) {
  112. preset_name = "CUSTOM";
  113. } else {
  114. FURI_LOG_E(TAG, "Unknown preset");
  115. }
  116. return preset_name;
  117. }
  118. SubGhzRadioPreset subghz_txrx_get_preset(SubGhzTxRx* instance) {
  119. furi_assert(instance);
  120. return *instance->preset;
  121. }
  122. void subghz_txrx_get_frequency_and_modulation(
  123. SubGhzTxRx* instance,
  124. FuriString* frequency,
  125. FuriString* modulation) {
  126. furi_assert(instance);
  127. SubGhzRadioPreset* preset = instance->preset;
  128. if(frequency != NULL) {
  129. furi_string_printf(
  130. frequency,
  131. "%03ld.%02ld",
  132. preset->frequency / 1000000 % 1000,
  133. preset->frequency / 10000 % 100);
  134. }
  135. if(modulation != NULL) {
  136. furi_string_printf(modulation, "%.2s", furi_string_get_cstr(preset->name));
  137. }
  138. }
  139. static void subghz_txrx_begin(SubGhzTxRx* instance, uint8_t* preset_data) {
  140. furi_assert(instance);
  141. subghz_devices_reset(instance->radio_device);
  142. subghz_devices_idle(instance->radio_device);
  143. subghz_devices_load_preset(instance->radio_device, FuriHalSubGhzPresetCustom, preset_data);
  144. instance->txrx_state = SubGhzTxRxStateIDLE;
  145. }
  146. static uint32_t subghz_txrx_rx(SubGhzTxRx* instance, uint32_t frequency) {
  147. furi_assert(instance);
  148. furi_assert(
  149. instance->txrx_state != SubGhzTxRxStateRx && instance->txrx_state != SubGhzTxRxStateSleep);
  150. subghz_devices_idle(instance->radio_device);
  151. uint32_t value = subghz_devices_set_frequency(instance->radio_device, frequency);
  152. subghz_devices_flush_rx(instance->radio_device);
  153. subghz_txrx_speaker_on(instance);
  154. subghz_devices_start_async_rx(
  155. instance->radio_device, subghz_worker_rx_callback, instance->worker);
  156. subghz_worker_start(instance->worker);
  157. instance->txrx_state = SubGhzTxRxStateRx;
  158. return value;
  159. }
  160. static void subghz_txrx_idle(SubGhzTxRx* instance) {
  161. furi_assert(instance);
  162. if(instance->txrx_state != SubGhzTxRxStateSleep) {
  163. subghz_devices_idle(instance->radio_device);
  164. subghz_txrx_speaker_off(instance);
  165. instance->txrx_state = SubGhzTxRxStateIDLE;
  166. }
  167. }
  168. static void subghz_txrx_rx_end(SubGhzTxRx* instance) {
  169. furi_assert(instance);
  170. furi_assert(instance->txrx_state == SubGhzTxRxStateRx);
  171. if(subghz_worker_is_running(instance->worker)) {
  172. subghz_worker_stop(instance->worker);
  173. subghz_devices_stop_async_rx(instance->radio_device);
  174. }
  175. subghz_devices_idle(instance->radio_device);
  176. subghz_txrx_speaker_off(instance);
  177. instance->txrx_state = SubGhzTxRxStateIDLE;
  178. }
  179. void subghz_txrx_sleep(SubGhzTxRx* instance) {
  180. furi_assert(instance);
  181. subghz_devices_sleep(instance->radio_device);
  182. instance->txrx_state = SubGhzTxRxStateSleep;
  183. }
  184. static bool subghz_txrx_tx(SubGhzTxRx* instance, uint32_t frequency) {
  185. furi_assert(instance);
  186. furi_assert(instance->txrx_state != SubGhzTxRxStateSleep);
  187. subghz_devices_idle(instance->radio_device);
  188. subghz_devices_set_frequency(instance->radio_device, frequency);
  189. bool ret = subghz_devices_set_tx(instance->radio_device);
  190. if(ret) {
  191. subghz_txrx_speaker_on(instance);
  192. instance->txrx_state = SubGhzTxRxStateTx;
  193. }
  194. return ret;
  195. }
  196. SubGhzTxRxStartTxState subghz_txrx_tx_start(SubGhzTxRx* instance, FlipperFormat* flipper_format) {
  197. furi_assert(instance);
  198. furi_assert(flipper_format);
  199. subghz_txrx_stop(instance);
  200. SubGhzTxRxStartTxState ret = SubGhzTxRxStartTxStateErrorParserOthers;
  201. FuriString* temp_str = furi_string_alloc();
  202. uint32_t repeat = 200;
  203. do {
  204. if(!flipper_format_rewind(flipper_format)) {
  205. FURI_LOG_E(TAG, "Rewind error");
  206. break;
  207. }
  208. if(!flipper_format_read_string(flipper_format, "Protocol", temp_str)) {
  209. FURI_LOG_E(TAG, "Missing Protocol");
  210. break;
  211. }
  212. if(!flipper_format_insert_or_update_uint32(flipper_format, "Repeat", &repeat, 1)) {
  213. FURI_LOG_E(TAG, "Unable Repeat");
  214. break;
  215. }
  216. ret = SubGhzTxRxStartTxStateOk;
  217. SubGhzRadioPreset* preset = instance->preset;
  218. instance->transmitter =
  219. subghz_transmitter_alloc_init(instance->environment, furi_string_get_cstr(temp_str));
  220. if(instance->transmitter) {
  221. if(subghz_transmitter_deserialize(instance->transmitter, flipper_format) ==
  222. SubGhzProtocolStatusOk) {
  223. if(strcmp(furi_string_get_cstr(preset->name), "") != 0) {
  224. subghz_txrx_begin(
  225. instance,
  226. subghz_setting_get_preset_data_by_name(
  227. instance->setting, furi_string_get_cstr(preset->name)));
  228. if(preset->frequency) {
  229. if(!subghz_txrx_tx(instance, preset->frequency)) {
  230. FURI_LOG_E(TAG, "Only Rx");
  231. ret = SubGhzTxRxStartTxStateErrorOnlyRx;
  232. }
  233. } else {
  234. ret = SubGhzTxRxStartTxStateErrorParserOthers;
  235. }
  236. } else {
  237. FURI_LOG_E(
  238. TAG, "Unknown name preset \" %s \"", furi_string_get_cstr(preset->name));
  239. ret = SubGhzTxRxStartTxStateErrorParserOthers;
  240. }
  241. if(ret == SubGhzTxRxStartTxStateOk) {
  242. //Start TX
  243. subghz_devices_start_async_tx(
  244. instance->radio_device, subghz_transmitter_yield, instance->transmitter);
  245. }
  246. } else {
  247. ret = SubGhzTxRxStartTxStateErrorParserOthers;
  248. }
  249. } else {
  250. ret = SubGhzTxRxStartTxStateErrorParserOthers;
  251. }
  252. if(ret != SubGhzTxRxStartTxStateOk) {
  253. subghz_transmitter_free(instance->transmitter);
  254. if(instance->txrx_state != SubGhzTxRxStateIDLE) {
  255. subghz_txrx_idle(instance);
  256. }
  257. }
  258. } while(false);
  259. furi_string_free(temp_str);
  260. return ret;
  261. }
  262. void subghz_txrx_rx_start(SubGhzTxRx* instance) {
  263. furi_assert(instance);
  264. subghz_txrx_stop(instance);
  265. subghz_txrx_begin(
  266. instance,
  267. subghz_setting_get_preset_data_by_name(
  268. subghz_txrx_get_setting(instance), furi_string_get_cstr(instance->preset->name)));
  269. subghz_txrx_rx(instance, instance->preset->frequency);
  270. }
  271. void subghz_txrx_set_need_save_callback(
  272. SubGhzTxRx* instance,
  273. SubGhzTxRxNeedSaveCallback callback,
  274. void* context) {
  275. furi_assert(instance);
  276. instance->need_save_callback = callback;
  277. instance->need_save_context = context;
  278. }
  279. static void subghz_txrx_tx_stop(SubGhzTxRx* instance) {
  280. furi_assert(instance);
  281. furi_assert(instance->txrx_state == SubGhzTxRxStateTx);
  282. //Stop TX
  283. subghz_devices_stop_async_tx(instance->radio_device);
  284. subghz_transmitter_stop(instance->transmitter);
  285. subghz_transmitter_free(instance->transmitter);
  286. //if protocol dynamic then we save the last upload
  287. if(instance->decoder_result->protocol->type == SubGhzProtocolTypeDynamic) {
  288. if(instance->need_save_callback) {
  289. instance->need_save_callback(instance->need_save_context);
  290. }
  291. }
  292. subghz_txrx_idle(instance);
  293. subghz_txrx_speaker_off(instance);
  294. }
  295. FlipperFormat* subghz_txrx_get_fff_data(SubGhzTxRx* instance) {
  296. furi_assert(instance);
  297. return instance->fff_data;
  298. }
  299. SubGhzSetting* subghz_txrx_get_setting(SubGhzTxRx* instance) {
  300. furi_assert(instance);
  301. return instance->setting;
  302. }
  303. void subghz_txrx_stop(SubGhzTxRx* instance) {
  304. furi_assert(instance);
  305. switch(instance->txrx_state) {
  306. case SubGhzTxRxStateTx:
  307. subghz_txrx_tx_stop(instance);
  308. subghz_txrx_speaker_unmute(instance);
  309. break;
  310. case SubGhzTxRxStateRx:
  311. subghz_txrx_rx_end(instance);
  312. subghz_txrx_speaker_mute(instance);
  313. break;
  314. default:
  315. break;
  316. }
  317. }
  318. void subghz_txrx_hopper_update(SubGhzTxRx* instance) {
  319. furi_assert(instance);
  320. switch(instance->hopper_state) {
  321. case SubGhzHopperStateOFF:
  322. case SubGhzHopperStatePause:
  323. return;
  324. case SubGhzHopperStateRSSITimeOut:
  325. if(instance->hopper_timeout != 0) {
  326. instance->hopper_timeout--;
  327. return;
  328. }
  329. break;
  330. default:
  331. break;
  332. }
  333. float rssi = -127.0f;
  334. if(instance->hopper_state != SubGhzHopperStateRSSITimeOut) {
  335. // See RSSI Calculation timings in CC1101 17.3 RSSI
  336. rssi = subghz_devices_get_rssi(instance->radio_device);
  337. // Stay if RSSI is high enough
  338. if(rssi > -90.0f) {
  339. instance->hopper_timeout = 10;
  340. instance->hopper_state = SubGhzHopperStateRSSITimeOut;
  341. return;
  342. }
  343. } else {
  344. instance->hopper_state = SubGhzHopperStateRunnig;
  345. }
  346. // Select next frequency
  347. if(instance->hopper_idx_frequency <
  348. subghz_setting_get_hopper_frequency_count(instance->setting) - 1) {
  349. instance->hopper_idx_frequency++;
  350. } else {
  351. instance->hopper_idx_frequency = 0;
  352. }
  353. if(instance->txrx_state == SubGhzTxRxStateRx) {
  354. subghz_txrx_rx_end(instance);
  355. };
  356. if(instance->txrx_state == SubGhzTxRxStateIDLE) {
  357. subghz_receiver_reset(instance->receiver);
  358. instance->preset->frequency =
  359. subghz_setting_get_hopper_frequency(instance->setting, instance->hopper_idx_frequency);
  360. subghz_txrx_rx(instance, instance->preset->frequency);
  361. }
  362. }
  363. SubGhzHopperState subghz_txrx_hopper_get_state(SubGhzTxRx* instance) {
  364. furi_assert(instance);
  365. return instance->hopper_state;
  366. }
  367. void subghz_txrx_hopper_set_state(SubGhzTxRx* instance, SubGhzHopperState state) {
  368. furi_assert(instance);
  369. instance->hopper_state = state;
  370. }
  371. void subghz_txrx_hopper_unpause(SubGhzTxRx* instance) {
  372. furi_assert(instance);
  373. if(instance->hopper_state == SubGhzHopperStatePause) {
  374. instance->hopper_state = SubGhzHopperStateRunnig;
  375. }
  376. }
  377. void subghz_txrx_hopper_pause(SubGhzTxRx* instance) {
  378. furi_assert(instance);
  379. if(instance->hopper_state == SubGhzHopperStateRunnig) {
  380. instance->hopper_state = SubGhzHopperStatePause;
  381. }
  382. }
  383. void subghz_txrx_speaker_on(SubGhzTxRx* instance) {
  384. furi_assert(instance);
  385. if(instance->speaker_state == SubGhzSpeakerStateEnable) {
  386. if(furi_hal_speaker_acquire(30)) {
  387. subghz_devices_set_async_mirror_pin(instance->radio_device, &gpio_speaker);
  388. } else {
  389. instance->speaker_state = SubGhzSpeakerStateDisable;
  390. }
  391. }
  392. }
  393. void subghz_txrx_speaker_off(SubGhzTxRx* instance) {
  394. furi_assert(instance);
  395. if(instance->speaker_state != SubGhzSpeakerStateDisable) {
  396. if(furi_hal_speaker_is_mine()) {
  397. subghz_devices_set_async_mirror_pin(instance->radio_device, NULL);
  398. furi_hal_speaker_release();
  399. if(instance->speaker_state == SubGhzSpeakerStateShutdown)
  400. instance->speaker_state = SubGhzSpeakerStateDisable;
  401. }
  402. }
  403. }
  404. void subghz_txrx_speaker_mute(SubGhzTxRx* instance) {
  405. furi_assert(instance);
  406. if(instance->speaker_state == SubGhzSpeakerStateEnable) {
  407. if(furi_hal_speaker_is_mine()) {
  408. subghz_devices_set_async_mirror_pin(instance->radio_device, NULL);
  409. }
  410. }
  411. }
  412. void subghz_txrx_speaker_unmute(SubGhzTxRx* instance) {
  413. furi_assert(instance);
  414. if(instance->speaker_state == SubGhzSpeakerStateEnable) {
  415. if(furi_hal_speaker_is_mine()) {
  416. subghz_devices_set_async_mirror_pin(instance->radio_device, &gpio_speaker);
  417. }
  418. }
  419. }
  420. void subghz_txrx_speaker_set_state(SubGhzTxRx* instance, SubGhzSpeakerState state) {
  421. furi_assert(instance);
  422. instance->speaker_state = state;
  423. }
  424. SubGhzSpeakerState subghz_txrx_speaker_get_state(SubGhzTxRx* instance) {
  425. furi_assert(instance);
  426. return instance->speaker_state;
  427. }
  428. bool subghz_txrx_load_decoder_by_name_protocol(SubGhzTxRx* instance, const char* name_protocol) {
  429. furi_assert(instance);
  430. furi_assert(name_protocol);
  431. bool res = false;
  432. instance->decoder_result =
  433. subghz_receiver_search_decoder_base_by_name(instance->receiver, name_protocol);
  434. if(instance->decoder_result) {
  435. res = true;
  436. }
  437. return res;
  438. }
  439. SubGhzProtocolDecoderBase* subghz_txrx_get_decoder(SubGhzTxRx* instance) {
  440. furi_assert(instance);
  441. return instance->decoder_result;
  442. }
  443. bool subghz_txrx_protocol_is_serializable(SubGhzTxRx* instance) {
  444. furi_assert(instance);
  445. return (instance->decoder_result->protocol->flag & SubGhzProtocolFlag_Save) ==
  446. SubGhzProtocolFlag_Save;
  447. }
  448. bool subghz_txrx_protocol_is_transmittable(SubGhzTxRx* instance, bool check_type) {
  449. furi_assert(instance);
  450. const SubGhzProtocol* protocol = instance->decoder_result->protocol;
  451. if(check_type) {
  452. return ((protocol->flag & SubGhzProtocolFlag_Send) == SubGhzProtocolFlag_Send) &&
  453. protocol->encoder->deserialize && protocol->type == SubGhzProtocolTypeStatic;
  454. }
  455. return ((protocol->flag & SubGhzProtocolFlag_Send) == SubGhzProtocolFlag_Send) &&
  456. protocol->encoder->deserialize;
  457. }
  458. void subghz_txrx_receiver_set_filter(SubGhzTxRx* instance, SubGhzProtocolFlag filter) {
  459. furi_assert(instance);
  460. subghz_receiver_set_filter(instance->receiver, filter);
  461. }
  462. void subghz_txrx_set_rx_calback(
  463. SubGhzTxRx* instance,
  464. SubGhzReceiverCallback callback,
  465. void* context) {
  466. subghz_receiver_set_rx_callback(instance->receiver, callback, context);
  467. }
  468. void subghz_txrx_set_raw_file_encoder_worker_callback_end(
  469. SubGhzTxRx* instance,
  470. SubGhzProtocolEncoderRAWCallbackEnd callback,
  471. void* context) {
  472. subghz_protocol_raw_file_encoder_worker_set_callback_end(
  473. (SubGhzProtocolEncoderRAW*)subghz_transmitter_get_protocol_instance(instance->transmitter),
  474. callback,
  475. context);
  476. }
  477. bool subghz_txrx_radio_device_is_external_connected(SubGhzTxRx* instance, const char* name) {
  478. furi_assert(instance);
  479. bool is_connect = false;
  480. bool is_otg_enabled = furi_hal_power_is_otg_enabled();
  481. if(!is_otg_enabled) {
  482. subghz_txrx_radio_device_power_on(instance);
  483. }
  484. const SubGhzDevice* device = subghz_devices_get_by_name(name);
  485. if(device) {
  486. is_connect = subghz_devices_is_connect(device);
  487. }
  488. if(!is_otg_enabled) {
  489. subghz_txrx_radio_device_power_off(instance);
  490. }
  491. return is_connect;
  492. }
  493. SubGhzRadioDeviceType
  494. subghz_txrx_radio_device_set(SubGhzTxRx* instance, SubGhzRadioDeviceType radio_device_type) {
  495. furi_assert(instance);
  496. if(radio_device_type == SubGhzRadioDeviceTypeExternalCC1101 &&
  497. subghz_txrx_radio_device_is_external_connected(instance, SUBGHZ_DEVICE_CC1101_EXT_NAME)) {
  498. subghz_txrx_radio_device_power_on(instance);
  499. instance->radio_device = subghz_devices_get_by_name(SUBGHZ_DEVICE_CC1101_EXT_NAME);
  500. subghz_devices_begin(instance->radio_device);
  501. instance->radio_device_type = SubGhzRadioDeviceTypeExternalCC1101;
  502. } else {
  503. subghz_txrx_radio_device_power_off(instance);
  504. if(instance->radio_device_type != SubGhzRadioDeviceTypeInternal) {
  505. subghz_devices_end(instance->radio_device);
  506. }
  507. instance->radio_device = subghz_devices_get_by_name(SUBGHZ_DEVICE_CC1101_INT_NAME);
  508. instance->radio_device_type = SubGhzRadioDeviceTypeInternal;
  509. }
  510. return instance->radio_device_type;
  511. }
  512. SubGhzRadioDeviceType subghz_txrx_radio_device_get(SubGhzTxRx* instance) {
  513. furi_assert(instance);
  514. return instance->radio_device_type;
  515. }
  516. float subghz_txrx_radio_device_get_rssi(SubGhzTxRx* instance) {
  517. furi_assert(instance);
  518. return subghz_devices_get_rssi(instance->radio_device);
  519. }
  520. const char* subghz_txrx_radio_device_get_name(SubGhzTxRx* instance) {
  521. furi_assert(instance);
  522. return subghz_devices_get_name(instance->radio_device);
  523. }
  524. bool subghz_txrx_radio_device_is_frequecy_valid(SubGhzTxRx* instance, uint32_t frequency) {
  525. furi_assert(instance);
  526. return subghz_devices_is_frequency_valid(instance->radio_device, frequency);
  527. }