subghz_txrx.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617
  1. #include "subghz_txrx_i.h"
  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 <infrared_transmit.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. FuriHalInfraredTxPin tx_pin_detected = furi_hal_infrared_detect_tx_output();
  25. if(furi_hal_power_is_otg_enabled() && tx_pin_detected == FuriHalInfraredTxPinInternal) {
  26. furi_hal_power_disable_otg();
  27. }
  28. }
  29. SubGhzTxRx* subghz_txrx_alloc(void) {
  30. SubGhzTxRx* instance = malloc(sizeof(SubGhzTxRx));
  31. instance->setting = subghz_setting_alloc();
  32. subghz_setting_load(instance->setting, EXT_PATH("subghz/assets/setting_user"));
  33. instance->preset = malloc(sizeof(SubGhzRadioPreset));
  34. instance->preset->name = furi_string_alloc();
  35. subghz_txrx_set_preset(
  36. instance, "AM650", subghz_setting_get_default_frequency(instance->setting), NULL, 0);
  37. instance->txrx_state = SubGhzTxRxStateSleep;
  38. subghz_txrx_hopper_set_state(instance, SubGhzHopperStateOFF);
  39. subghz_txrx_speaker_set_state(instance, SubGhzSpeakerStateDisable);
  40. instance->worker = subghz_worker_alloc();
  41. instance->fff_data = flipper_format_string_alloc();
  42. instance->environment = subghz_environment_alloc();
  43. instance->is_database_loaded =
  44. subghz_environment_load_keystore(instance->environment, SUBGHZ_KEYSTORE_DIR_NAME);
  45. subghz_environment_load_keystore(instance->environment, SUBGHZ_KEYSTORE_DIR_USER_NAME);
  46. subghz_environment_set_came_atomo_rainbow_table_file_name(
  47. instance->environment, SUBGHZ_CAME_ATOMO_DIR_NAME);
  48. subghz_environment_set_alutech_at_4n_rainbow_table_file_name(
  49. instance->environment, SUBGHZ_ALUTECH_AT_4N_DIR_NAME);
  50. subghz_environment_set_nice_flor_s_rainbow_table_file_name(
  51. instance->environment, SUBGHZ_NICE_FLOR_S_DIR_NAME);
  52. subghz_environment_set_protocol_registry(
  53. instance->environment, (void*)&subghz_protocol_registry);
  54. instance->receiver = subghz_receiver_alloc_init(instance->environment);
  55. subghz_worker_set_overrun_callback(
  56. instance->worker, (SubGhzWorkerOverrunCallback)subghz_receiver_reset);
  57. subghz_worker_set_pair_callback(
  58. instance->worker, (SubGhzWorkerPairCallback)subghz_receiver_decode);
  59. subghz_worker_set_context(instance->worker, instance->receiver);
  60. //set default device External
  61. subghz_devices_init();
  62. instance->radio_device_type = SubGhzRadioDeviceTypeInternal;
  63. instance->radio_device_type =
  64. subghz_txrx_radio_device_set(instance, SubGhzRadioDeviceTypeExternalCC1101);
  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. FURI_LOG_D(TAG, "completed subghz_txrx_begin");
  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. FURI_LOG_D(TAG, "state not ok");
  255. subghz_transmitter_free(instance->transmitter); // Crashes here
  256. if(instance->txrx_state != SubGhzTxRxStateIDLE) {
  257. subghz_txrx_idle(instance);
  258. }
  259. }
  260. } while(false);
  261. furi_string_free(temp_str);
  262. return ret;
  263. }
  264. /*void subghz_txrx_rx_start(SubGhzTxRx* instance) {
  265. furi_assert(instance);
  266. subghz_txrx_stop(instance);
  267. subghz_txrx_begin(
  268. instance,
  269. subghz_setting_get_preset_data_by_name(
  270. subghz_txrx_get_setting(instance), furi_string_get_cstr(instance->preset->name)));
  271. subghz_txrx_rx(instance, instance->preset->frequency);
  272. }*/
  273. void subghz_txrx_set_need_save_callback(
  274. SubGhzTxRx* instance,
  275. SubGhzTxRxNeedSaveCallback callback,
  276. void* context) {
  277. furi_assert(instance);
  278. instance->need_save_callback = callback;
  279. instance->need_save_context = context;
  280. }
  281. static void subghz_txrx_tx_stop(SubGhzTxRx* instance) {
  282. furi_assert(instance);
  283. furi_assert(instance->txrx_state == SubGhzTxRxStateTx);
  284. //Stop TX
  285. subghz_devices_stop_async_tx(instance->radio_device);
  286. subghz_transmitter_stop(instance->transmitter);
  287. subghz_transmitter_free(instance->transmitter);
  288. //if protocol dynamic then we save the last upload
  289. if(instance->decoder_result->protocol->type == SubGhzProtocolTypeDynamic) {
  290. if(instance->need_save_callback) {
  291. instance->need_save_callback(instance->need_save_context);
  292. }
  293. }
  294. subghz_txrx_idle(instance);
  295. subghz_txrx_speaker_off(instance);
  296. }
  297. FlipperFormat* subghz_txrx_get_fff_data(SubGhzTxRx* instance) {
  298. furi_assert(instance);
  299. return instance->fff_data;
  300. }
  301. SubGhzSetting* subghz_txrx_get_setting(SubGhzTxRx* instance) {
  302. furi_assert(instance);
  303. return instance->setting;
  304. }
  305. void subghz_txrx_stop(SubGhzTxRx* instance) {
  306. furi_assert(instance);
  307. switch(instance->txrx_state) {
  308. case SubGhzTxRxStateTx:
  309. subghz_txrx_tx_stop(instance);
  310. subghz_txrx_speaker_unmute(instance);
  311. break;
  312. case SubGhzTxRxStateRx:
  313. //subghz_txrx_rx_end(instance);
  314. //subghz_txrx_speaker_mute(instance);
  315. break;
  316. default:
  317. break;
  318. }
  319. }
  320. /*void subghz_txrx_hopper_update(SubGhzTxRx* instance) {
  321. furi_assert(instance);
  322. switch(instance->hopper_state) {
  323. case SubGhzHopperStateOFF:
  324. case SubGhzHopperStatePause:
  325. return;
  326. case SubGhzHopperStateRSSITimeOut:
  327. if(instance->hopper_timeout != 0) {
  328. instance->hopper_timeout--;
  329. return;
  330. }
  331. break;
  332. default:
  333. break;
  334. }
  335. float rssi = -127.0f;
  336. if(instance->hopper_state != SubGhzHopperStateRSSITimeOut) {
  337. // See RSSI Calculation timings in CC1101 17.3 RSSI
  338. rssi = subghz_devices_get_rssi(instance->radio_device);
  339. // Stay if RSSI is high enough
  340. if(rssi > -90.0f) {
  341. instance->hopper_timeout = 10;
  342. instance->hopper_state = SubGhzHopperStateRSSITimeOut;
  343. return;
  344. }
  345. } else {
  346. instance->hopper_state = SubGhzHopperStateRunnig;
  347. }
  348. // Select next frequency
  349. if(instance->hopper_idx_frequency <
  350. subghz_setting_get_hopper_frequency_count(instance->setting) - 1) {
  351. instance->hopper_idx_frequency++;
  352. } else {
  353. instance->hopper_idx_frequency = 0;
  354. }
  355. if(instance->txrx_state == SubGhzTxRxStateRx) {
  356. subghz_txrx_rx_end(instance);
  357. };
  358. if(instance->txrx_state == SubGhzTxRxStateIDLE) {
  359. subghz_receiver_reset(instance->receiver);
  360. instance->preset->frequency =
  361. subghz_setting_get_hopper_frequency(instance->setting, instance->hopper_idx_frequency);
  362. subghz_txrx_rx(instance, instance->preset->frequency);
  363. }
  364. }*/
  365. /*SubGhzHopperState subghz_txrx_hopper_get_state(SubGhzTxRx* instance) {
  366. furi_assert(instance);
  367. return instance->hopper_state;
  368. }*/
  369. void subghz_txrx_hopper_set_state(SubGhzTxRx* instance, SubGhzHopperState state) {
  370. furi_assert(instance);
  371. instance->hopper_state = state;
  372. }
  373. /*void subghz_txrx_hopper_unpause(SubGhzTxRx* instance) {
  374. furi_assert(instance);
  375. if(instance->hopper_state == SubGhzHopperStatePause) {
  376. instance->hopper_state = SubGhzHopperStateRunnig;
  377. }
  378. }*/
  379. /*void subghz_txrx_hopper_pause(SubGhzTxRx* instance) {
  380. furi_assert(instance);
  381. if(instance->hopper_state == SubGhzHopperStateRunnig) {
  382. instance->hopper_state = SubGhzHopperStatePause;
  383. }
  384. }*/
  385. void subghz_txrx_speaker_on(SubGhzTxRx* instance) {
  386. furi_assert(instance);
  387. if(instance->speaker_state == SubGhzSpeakerStateEnable) {
  388. if(furi_hal_speaker_acquire(30)) {
  389. subghz_devices_set_async_mirror_pin(instance->radio_device, &gpio_speaker);
  390. } else {
  391. instance->speaker_state = SubGhzSpeakerStateDisable;
  392. }
  393. }
  394. }
  395. void subghz_txrx_speaker_off(SubGhzTxRx* instance) {
  396. furi_assert(instance);
  397. if(instance->speaker_state != SubGhzSpeakerStateDisable) {
  398. if(furi_hal_speaker_is_mine()) {
  399. subghz_devices_set_async_mirror_pin(instance->radio_device, NULL);
  400. furi_hal_speaker_release();
  401. if(instance->speaker_state == SubGhzSpeakerStateShutdown)
  402. instance->speaker_state = SubGhzSpeakerStateDisable;
  403. }
  404. }
  405. }
  406. /*void subghz_txrx_speaker_mute(SubGhzTxRx* instance) {
  407. furi_assert(instance);
  408. if(instance->speaker_state == SubGhzSpeakerStateEnable) {
  409. if(furi_hal_speaker_is_mine()) {
  410. subghz_devices_set_async_mirror_pin(instance->radio_device, NULL);
  411. }
  412. }
  413. }*/
  414. void subghz_txrx_speaker_unmute(SubGhzTxRx* instance) {
  415. furi_assert(instance);
  416. if(instance->speaker_state == SubGhzSpeakerStateEnable) {
  417. if(furi_hal_speaker_is_mine()) {
  418. subghz_devices_set_async_mirror_pin(instance->radio_device, &gpio_speaker);
  419. }
  420. }
  421. }
  422. void subghz_txrx_speaker_set_state(SubGhzTxRx* instance, SubGhzSpeakerState state) {
  423. furi_assert(instance);
  424. instance->speaker_state = state;
  425. }
  426. /*SubGhzSpeakerState subghz_txrx_speaker_get_state(SubGhzTxRx* instance) {
  427. furi_assert(instance);
  428. return instance->speaker_state;
  429. }*/
  430. bool subghz_txrx_load_decoder_by_name_protocol(SubGhzTxRx* instance, const char* name_protocol) {
  431. furi_assert(instance);
  432. furi_assert(name_protocol);
  433. bool res = false;
  434. instance->decoder_result =
  435. subghz_receiver_search_decoder_base_by_name(instance->receiver, name_protocol);
  436. if(instance->decoder_result) {
  437. res = true;
  438. }
  439. return res;
  440. }
  441. SubGhzProtocolDecoderBase* subghz_txrx_get_decoder(SubGhzTxRx* instance) {
  442. furi_assert(instance);
  443. return instance->decoder_result;
  444. }
  445. /*bool subghz_txrx_protocol_is_serializable(SubGhzTxRx* instance) {
  446. furi_assert(instance);
  447. return (
  448. (instance->decoder_result->protocol->flag & SubGhzProtocolFlag_Save) ==
  449. SubGhzProtocolFlag_Save);
  450. }*/
  451. /*bool subghz_txrx_protocol_is_transmittable(SubGhzTxRx* instance, bool check_type) {
  452. furi_assert(instance);
  453. const SubGhzProtocol* protocol = instance->decoder_result->protocol;
  454. if(check_type) {
  455. return (
  456. ((protocol->flag & SubGhzProtocolFlag_Send) == SubGhzProtocolFlag_Send) &&
  457. protocol->encoder->deserialize && protocol->type == SubGhzProtocolTypeStatic);
  458. }
  459. return (
  460. ((protocol->flag & SubGhzProtocolFlag_Send) == SubGhzProtocolFlag_Send) &&
  461. protocol->encoder->deserialize);
  462. }*/
  463. /*void subghz_txrx_receiver_set_filter(SubGhzTxRx* instance, SubGhzProtocolFlag filter) {
  464. furi_assert(instance);
  465. subghz_receiver_set_filter(instance->receiver, filter);
  466. }*/
  467. /*void subghz_txrx_set_rx_calback(
  468. SubGhzTxRx* instance,
  469. SubGhzReceiverCallback callback,
  470. void* context) {
  471. subghz_receiver_set_rx_callback(instance->receiver, callback, context);
  472. }*/
  473. void subghz_txrx_set_raw_file_encoder_worker_callback_end(
  474. SubGhzTxRx* instance,
  475. SubGhzProtocolEncoderRAWCallbackEnd callback,
  476. void* context) {
  477. subghz_protocol_raw_file_encoder_worker_set_callback_end(
  478. (SubGhzProtocolEncoderRAW*)subghz_transmitter_get_protocol_instance(instance->transmitter),
  479. callback,
  480. context);
  481. }
  482. bool subghz_txrx_radio_device_is_external_connected(SubGhzTxRx* instance, const char* name) {
  483. furi_assert(instance);
  484. bool is_connect = false;
  485. bool is_otg_enabled = furi_hal_power_is_otg_enabled();
  486. if(!is_otg_enabled) {
  487. subghz_txrx_radio_device_power_on(instance);
  488. }
  489. const SubGhzDevice* device = subghz_devices_get_by_name(name);
  490. if(device) {
  491. is_connect = subghz_devices_is_connect(device);
  492. }
  493. if(!is_otg_enabled) {
  494. subghz_txrx_radio_device_power_off(instance);
  495. }
  496. return is_connect;
  497. }
  498. SubGhzRadioDeviceType
  499. subghz_txrx_radio_device_set(SubGhzTxRx* instance, SubGhzRadioDeviceType radio_device_type) {
  500. furi_assert(instance);
  501. if(radio_device_type == SubGhzRadioDeviceTypeExternalCC1101 &&
  502. subghz_txrx_radio_device_is_external_connected(instance, SUBGHZ_DEVICE_CC1101_EXT_NAME)) {
  503. subghz_txrx_radio_device_power_on(instance);
  504. instance->radio_device = subghz_devices_get_by_name(SUBGHZ_DEVICE_CC1101_EXT_NAME);
  505. subghz_devices_begin(instance->radio_device);
  506. instance->radio_device_type = SubGhzRadioDeviceTypeExternalCC1101;
  507. } else {
  508. subghz_txrx_radio_device_power_off(instance);
  509. if(instance->radio_device_type != SubGhzRadioDeviceTypeInternal) {
  510. subghz_devices_end(instance->radio_device);
  511. }
  512. instance->radio_device = subghz_devices_get_by_name(SUBGHZ_DEVICE_CC1101_INT_NAME);
  513. instance->radio_device_type = SubGhzRadioDeviceTypeInternal;
  514. }
  515. return instance->radio_device_type;
  516. }
  517. /*SubGhzRadioDeviceType subghz_txrx_radio_device_get(SubGhzTxRx* instance) {
  518. furi_assert(instance);
  519. return instance->radio_device_type;
  520. }*/
  521. /*float subghz_txrx_radio_device_get_rssi(SubGhzTxRx* instance) {
  522. furi_assert(instance);
  523. return subghz_devices_get_rssi(instance->radio_device);
  524. }*/
  525. const char* subghz_txrx_radio_device_get_name(SubGhzTxRx* instance) {
  526. furi_assert(instance);
  527. return subghz_devices_get_name(instance->radio_device);
  528. }
  529. bool subghz_txrx_radio_device_is_frequecy_valid(SubGhzTxRx* instance, uint32_t frequency) {
  530. furi_assert(instance);
  531. return subghz_devices_is_frequency_valid(instance->radio_device, frequency);
  532. }