subghz_txrx.c 21 KB

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