subghz_txrx.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619
  1. #include "subghz_txrx_i.h"
  2. #include <lib/subghz/protocols/protocol_items.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() {
  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_D(TAG, "completed TXRX alloc");
  62. return instance;
  63. }
  64. void subghz_txrx_free(SubGhzTxRx* instance) {
  65. furi_assert(instance);
  66. FURI_LOG_D(TAG, "freeing TXRX");
  67. if(instance->radio_device_type != SubGhzRadioDeviceTypeInternal) {
  68. subghz_txrx_radio_device_power_off(instance);
  69. subghz_devices_end(instance->radio_device);
  70. }
  71. subghz_devices_deinit();
  72. subghz_worker_free(instance->worker);
  73. subghz_receiver_free(instance->receiver);
  74. subghz_environment_free(instance->environment);
  75. /*flipper_format_free(instance->fff_data);*/
  76. furi_string_free(instance->preset->name);
  77. subghz_setting_free(instance->setting);
  78. free(instance->preset);
  79. free(instance);
  80. }
  81. /*bool subghz_txrx_is_database_loaded(SubGhzTxRx* instance) {
  82. furi_assert(instance);
  83. return instance->is_database_loaded;
  84. }*/
  85. void subghz_txrx_set_preset(
  86. SubGhzTxRx* instance,
  87. const char* preset_name,
  88. uint32_t frequency,
  89. uint8_t* preset_data,
  90. size_t preset_data_size) {
  91. furi_assert(instance);
  92. furi_string_set(instance->preset->name, preset_name);
  93. SubGhzRadioPreset* preset = instance->preset;
  94. preset->frequency = frequency;
  95. preset->data = preset_data;
  96. preset->data_size = preset_data_size;
  97. }
  98. /*const char* subghz_txrx_get_preset_name(SubGhzTxRx* instance, const char* preset) {
  99. UNUSED(instance);
  100. const char* preset_name = "";
  101. if(!strcmp(preset, "FuriHalSubGhzPresetOok270Async")) {
  102. preset_name = "AM270";
  103. } else if(!strcmp(preset, "FuriHalSubGhzPresetOok650Async")) {
  104. preset_name = "AM650";
  105. } else if(!strcmp(preset, "FuriHalSubGhzPreset2FSKDev238Async")) {
  106. preset_name = "FM238";
  107. } else if(!strcmp(preset, "FuriHalSubGhzPreset2FSKDev476Async")) {
  108. preset_name = "FM476";
  109. } else if(!strcmp(preset, "FuriHalSubGhzPresetCustom")) {
  110. preset_name = "CUSTOM";
  111. } else {
  112. FURI_LOG_E(TAG, "Unknown preset");
  113. }
  114. return preset_name;
  115. }*/
  116. /*SubGhzRadioPreset subghz_txrx_get_preset(SubGhzTxRx* instance) {
  117. furi_assert(instance);
  118. return *instance->preset;
  119. }*/
  120. /*void subghz_txrx_get_frequency_and_modulation(
  121. SubGhzTxRx* instance,
  122. FuriString* frequency,
  123. FuriString* modulation) {
  124. furi_assert(instance);
  125. SubGhzRadioPreset* preset = instance->preset;
  126. if(frequency != NULL) {
  127. furi_string_printf(
  128. frequency,
  129. "%03ld.%02ld",
  130. preset->frequency / 1000000 % 1000,
  131. preset->frequency / 10000 % 100);
  132. }
  133. if(modulation != NULL) {
  134. furi_string_printf(modulation, "%.2s", furi_string_get_cstr(preset->name));
  135. }
  136. }*/
  137. static void subghz_txrx_begin(SubGhzTxRx* instance, uint8_t* preset_data) {
  138. furi_assert(instance);
  139. subghz_devices_reset(instance->radio_device);
  140. subghz_devices_idle(instance->radio_device);
  141. subghz_devices_load_preset(instance->radio_device, FuriHalSubGhzPresetCustom, preset_data);
  142. instance->txrx_state = SubGhzTxRxStateIDLE;
  143. FURI_LOG_D(TAG, "completed subghz_txrx_begin");
  144. }
  145. /*static uint32_t subghz_txrx_rx(SubGhzTxRx* instance, uint32_t frequency) {
  146. furi_assert(instance);
  147. furi_assert(
  148. instance->txrx_state != SubGhzTxRxStateRx && instance->txrx_state != SubGhzTxRxStateSleep);
  149. subghz_devices_idle(instance->radio_device);
  150. uint32_t value = subghz_devices_set_frequency(instance->radio_device, frequency);
  151. subghz_devices_flush_rx(instance->radio_device);
  152. subghz_txrx_speaker_on(instance);
  153. subghz_devices_start_async_rx(
  154. instance->radio_device, subghz_worker_rx_callback, instance->worker);
  155. subghz_worker_start(instance->worker);
  156. instance->txrx_state = SubGhzTxRxStateRx;
  157. return value;
  158. }*/
  159. static void subghz_txrx_idle(SubGhzTxRx* instance) {
  160. furi_assert(instance);
  161. if(instance->txrx_state != SubGhzTxRxStateSleep) {
  162. subghz_devices_idle(instance->radio_device);
  163. subghz_txrx_speaker_off(instance);
  164. instance->txrx_state = SubGhzTxRxStateIDLE;
  165. }
  166. FURI_LOG_D(TAG, "completed subghz_txrx_idle");
  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. FURI_LOG_D(TAG, "completed subghz_txrx_tx");
  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. FURI_LOG_D(TAG, "starting loop in subghz_txrx_tx_start");
  205. do {
  206. if(!flipper_format_rewind(flipper_format)) {
  207. FURI_LOG_E(TAG, "Rewind error");
  208. break;
  209. }
  210. if(!flipper_format_read_string(flipper_format, "Protocol", temp_str)) {
  211. FURI_LOG_E(TAG, "Missing Protocol");
  212. break;
  213. }
  214. if(!flipper_format_insert_or_update_uint32(flipper_format, "Repeat", &repeat, 1)) {
  215. FURI_LOG_E(TAG, "Unable Repeat");
  216. break;
  217. }
  218. ret = SubGhzTxRxStartTxStateOk;
  219. SubGhzRadioPreset* preset = instance->preset;
  220. instance->transmitter =
  221. subghz_transmitter_alloc_init(instance->environment, furi_string_get_cstr(temp_str));
  222. if(instance->transmitter) {
  223. if(subghz_transmitter_deserialize(instance->transmitter, flipper_format) ==
  224. SubGhzProtocolStatusOk) {
  225. if(strcmp(furi_string_get_cstr(preset->name), "") != 0) {
  226. subghz_txrx_begin(
  227. instance,
  228. subghz_setting_get_preset_data_by_name(
  229. instance->setting, furi_string_get_cstr(preset->name)));
  230. if(preset->frequency) {
  231. if(!subghz_txrx_tx(instance, preset->frequency)) {
  232. FURI_LOG_E(TAG, "Only Rx");
  233. ret = SubGhzTxRxStartTxStateErrorOnlyRx;
  234. }
  235. } else {
  236. ret = SubGhzTxRxStartTxStateErrorParserOthers;
  237. }
  238. } else {
  239. FURI_LOG_E(
  240. TAG, "Unknown name preset \" %s \"", furi_string_get_cstr(preset->name));
  241. ret = SubGhzTxRxStartTxStateErrorParserOthers;
  242. }
  243. if(ret == SubGhzTxRxStartTxStateOk) {
  244. //Start TX
  245. subghz_devices_start_async_tx(
  246. instance->radio_device, subghz_transmitter_yield, instance->transmitter);
  247. }
  248. } else {
  249. ret = SubGhzTxRxStartTxStateErrorParserOthers;
  250. }
  251. } else {
  252. ret = SubGhzTxRxStartTxStateErrorParserOthers;
  253. }
  254. if(ret != SubGhzTxRxStartTxStateOk) {
  255. subghz_transmitter_free(instance->transmitter);
  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. }*/