subghz_txrx.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630
  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. #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. FURI_LOG_D(TAG, "looping");
  207. if(!flipper_format_rewind(flipper_format)) {
  208. FURI_LOG_E(TAG, "Rewind error");
  209. break;
  210. }
  211. if(!flipper_format_read_string(flipper_format, "Protocol", temp_str)) {
  212. FURI_LOG_E(TAG, "Missing Protocol");
  213. break;
  214. }
  215. if(!flipper_format_insert_or_update_uint32(flipper_format, "Repeat", &repeat, 1)) {
  216. FURI_LOG_E(TAG, "Unable Repeat");
  217. break;
  218. }
  219. //FURI_LOG_D(TAG, "File loaded");
  220. ret = SubGhzTxRxStartTxStateOk;
  221. SubGhzRadioPreset* preset = instance->preset;
  222. instance->transmitter =
  223. subghz_transmitter_alloc_init(instance->environment, furi_string_get_cstr(temp_str));
  224. if(instance->transmitter) {
  225. FURI_LOG_D(TAG, "transmitter found");
  226. if(subghz_transmitter_deserialize(instance->transmitter, flipper_format) ==
  227. SubGhzProtocolStatusOk) {
  228. //if (false) {
  229. FURI_LOG_D(TAG, "deserialization");
  230. if(strcmp(furi_string_get_cstr(preset->name), "") != 0) {
  231. FURI_LOG_D(TAG, "got preset name");
  232. subghz_txrx_begin(
  233. instance,
  234. subghz_setting_get_preset_data_by_name(
  235. instance->setting, furi_string_get_cstr(preset->name)));
  236. FURI_LOG_D(TAG, "loaded preset by name");
  237. if(preset->frequency) {
  238. if(!subghz_txrx_tx(instance, preset->frequency)) {
  239. FURI_LOG_E(TAG, "Only Rx");
  240. ret = SubGhzTxRxStartTxStateErrorOnlyRx;
  241. }
  242. FURI_LOG_D(TAG, "got frequency");
  243. } else {
  244. ret = SubGhzTxRxStartTxStateErrorParserOthers;
  245. }
  246. } else {
  247. FURI_LOG_E(
  248. TAG, "Unknown name preset \" %s \"", furi_string_get_cstr(preset->name));
  249. ret = SubGhzTxRxStartTxStateErrorParserOthers;
  250. }
  251. if(ret == SubGhzTxRxStartTxStateOk) {
  252. //Start TX
  253. FURI_LOG_D(TAG, "starting Async TX");
  254. subghz_devices_start_async_tx(
  255. instance->radio_device, subghz_transmitter_yield, instance->transmitter);
  256. }
  257. } else {
  258. FURI_LOG_D(TAG, "no deserialization");
  259. ret = SubGhzTxRxStartTxStateErrorParserOthers;
  260. }
  261. } else {
  262. ret = SubGhzTxRxStartTxStateErrorParserOthers;
  263. }
  264. if(ret != SubGhzTxRxStartTxStateOk) {
  265. FURI_LOG_D(TAG, "state not ok");
  266. subghz_transmitter_free(instance->transmitter); // Crashes here
  267. if(instance->txrx_state != SubGhzTxRxStateIDLE) {
  268. subghz_txrx_idle(instance);
  269. }
  270. }
  271. } while(false);
  272. furi_string_free(temp_str);
  273. return ret;
  274. }
  275. /*void subghz_txrx_rx_start(SubGhzTxRx* instance) {
  276. furi_assert(instance);
  277. subghz_txrx_stop(instance);
  278. subghz_txrx_begin(
  279. instance,
  280. subghz_setting_get_preset_data_by_name(
  281. subghz_txrx_get_setting(instance), furi_string_get_cstr(instance->preset->name)));
  282. subghz_txrx_rx(instance, instance->preset->frequency);
  283. }*/
  284. /*void subghz_txrx_set_need_save_callback(
  285. SubGhzTxRx* instance,
  286. SubGhzTxRxNeedSaveCallback callback,
  287. void* context) {
  288. furi_assert(instance);
  289. instance->need_save_callback = callback;
  290. instance->need_save_context = context;
  291. }*/
  292. static void subghz_txrx_tx_stop(SubGhzTxRx* instance) {
  293. furi_assert(instance);
  294. furi_assert(instance->txrx_state == SubGhzTxRxStateTx);
  295. //Stop TX
  296. subghz_devices_stop_async_tx(instance->radio_device);
  297. subghz_transmitter_stop(instance->transmitter);
  298. subghz_transmitter_free(instance->transmitter);
  299. //if protocol dynamic then we save the last upload
  300. /*if(instance->decoder_result->protocol->type == SubGhzProtocolTypeDynamic) {
  301. if(instance->need_save_callback) {
  302. instance->need_save_callback(instance->need_save_context);
  303. }
  304. }*/
  305. subghz_txrx_idle(instance);
  306. subghz_txrx_speaker_off(instance);
  307. }
  308. FlipperFormat* subghz_txrx_get_fff_data(SubGhzTxRx* instance) {
  309. furi_assert(instance);
  310. return instance->fff_data;
  311. }
  312. SubGhzSetting* subghz_txrx_get_setting(SubGhzTxRx* instance) {
  313. furi_assert(instance);
  314. return instance->setting;
  315. }
  316. void subghz_txrx_stop(SubGhzTxRx* instance) {
  317. furi_assert(instance);
  318. switch(instance->txrx_state) {
  319. case SubGhzTxRxStateTx:
  320. subghz_txrx_tx_stop(instance);
  321. subghz_txrx_speaker_unmute(instance);
  322. break;
  323. case SubGhzTxRxStateRx:
  324. //subghz_txrx_rx_end(instance);
  325. //subghz_txrx_speaker_mute(instance);
  326. break;
  327. default:
  328. break;
  329. }
  330. }
  331. /*void subghz_txrx_hopper_update(SubGhzTxRx* instance) {
  332. furi_assert(instance);
  333. switch(instance->hopper_state) {
  334. case SubGhzHopperStateOFF:
  335. case SubGhzHopperStatePause:
  336. return;
  337. case SubGhzHopperStateRSSITimeOut:
  338. if(instance->hopper_timeout != 0) {
  339. instance->hopper_timeout--;
  340. return;
  341. }
  342. break;
  343. default:
  344. break;
  345. }
  346. float rssi = -127.0f;
  347. if(instance->hopper_state != SubGhzHopperStateRSSITimeOut) {
  348. // See RSSI Calculation timings in CC1101 17.3 RSSI
  349. rssi = subghz_devices_get_rssi(instance->radio_device);
  350. // Stay if RSSI is high enough
  351. if(rssi > -90.0f) {
  352. instance->hopper_timeout = 10;
  353. instance->hopper_state = SubGhzHopperStateRSSITimeOut;
  354. return;
  355. }
  356. } else {
  357. instance->hopper_state = SubGhzHopperStateRunnig;
  358. }
  359. // Select next frequency
  360. if(instance->hopper_idx_frequency <
  361. subghz_setting_get_hopper_frequency_count(instance->setting) - 1) {
  362. instance->hopper_idx_frequency++;
  363. } else {
  364. instance->hopper_idx_frequency = 0;
  365. }
  366. if(instance->txrx_state == SubGhzTxRxStateRx) {
  367. subghz_txrx_rx_end(instance);
  368. };
  369. if(instance->txrx_state == SubGhzTxRxStateIDLE) {
  370. subghz_receiver_reset(instance->receiver);
  371. instance->preset->frequency =
  372. subghz_setting_get_hopper_frequency(instance->setting, instance->hopper_idx_frequency);
  373. subghz_txrx_rx(instance, instance->preset->frequency);
  374. }
  375. }*/
  376. /*SubGhzHopperState subghz_txrx_hopper_get_state(SubGhzTxRx* instance) {
  377. furi_assert(instance);
  378. return instance->hopper_state;
  379. }*/
  380. void subghz_txrx_hopper_set_state(SubGhzTxRx* instance, SubGhzHopperState state) {
  381. furi_assert(instance);
  382. instance->hopper_state = state;
  383. }
  384. /*void subghz_txrx_hopper_unpause(SubGhzTxRx* instance) {
  385. furi_assert(instance);
  386. if(instance->hopper_state == SubGhzHopperStatePause) {
  387. instance->hopper_state = SubGhzHopperStateRunnig;
  388. }
  389. }*/
  390. /*void subghz_txrx_hopper_pause(SubGhzTxRx* instance) {
  391. furi_assert(instance);
  392. if(instance->hopper_state == SubGhzHopperStateRunnig) {
  393. instance->hopper_state = SubGhzHopperStatePause;
  394. }
  395. }*/
  396. void subghz_txrx_speaker_on(SubGhzTxRx* instance) {
  397. furi_assert(instance);
  398. if(instance->speaker_state == SubGhzSpeakerStateEnable) {
  399. if(furi_hal_speaker_acquire(30)) {
  400. subghz_devices_set_async_mirror_pin(instance->radio_device, &gpio_speaker);
  401. } else {
  402. instance->speaker_state = SubGhzSpeakerStateDisable;
  403. }
  404. }
  405. }
  406. void subghz_txrx_speaker_off(SubGhzTxRx* instance) {
  407. furi_assert(instance);
  408. if(instance->speaker_state != SubGhzSpeakerStateDisable) {
  409. if(furi_hal_speaker_is_mine()) {
  410. subghz_devices_set_async_mirror_pin(instance->radio_device, NULL);
  411. furi_hal_speaker_release();
  412. if(instance->speaker_state == SubGhzSpeakerStateShutdown)
  413. instance->speaker_state = SubGhzSpeakerStateDisable;
  414. }
  415. }
  416. }
  417. /*void subghz_txrx_speaker_mute(SubGhzTxRx* instance) {
  418. furi_assert(instance);
  419. if(instance->speaker_state == SubGhzSpeakerStateEnable) {
  420. if(furi_hal_speaker_is_mine()) {
  421. subghz_devices_set_async_mirror_pin(instance->radio_device, NULL);
  422. }
  423. }
  424. }*/
  425. void subghz_txrx_speaker_unmute(SubGhzTxRx* instance) {
  426. furi_assert(instance);
  427. if(instance->speaker_state == SubGhzSpeakerStateEnable) {
  428. if(furi_hal_speaker_is_mine()) {
  429. subghz_devices_set_async_mirror_pin(instance->radio_device, &gpio_speaker);
  430. }
  431. }
  432. }
  433. void subghz_txrx_speaker_set_state(SubGhzTxRx* instance, SubGhzSpeakerState state) {
  434. furi_assert(instance);
  435. instance->speaker_state = state;
  436. }
  437. /*SubGhzSpeakerState subghz_txrx_speaker_get_state(SubGhzTxRx* instance) {
  438. furi_assert(instance);
  439. return instance->speaker_state;
  440. }*/
  441. bool subghz_txrx_load_decoder_by_name_protocol(SubGhzTxRx* instance, const char* name_protocol) {
  442. furi_assert(instance);
  443. furi_assert(name_protocol);
  444. bool res = false;
  445. instance->decoder_result =
  446. subghz_receiver_search_decoder_base_by_name(instance->receiver, name_protocol);
  447. if(instance->decoder_result) {
  448. res = true;
  449. }
  450. return res;
  451. }
  452. SubGhzProtocolDecoderBase* subghz_txrx_get_decoder(SubGhzTxRx* instance) {
  453. furi_assert(instance);
  454. return instance->decoder_result;
  455. }
  456. /*bool subghz_txrx_protocol_is_serializable(SubGhzTxRx* instance) {
  457. furi_assert(instance);
  458. return (
  459. (instance->decoder_result->protocol->flag & SubGhzProtocolFlag_Save) ==
  460. SubGhzProtocolFlag_Save);
  461. }*/
  462. /*bool subghz_txrx_protocol_is_transmittable(SubGhzTxRx* instance, bool check_type) {
  463. furi_assert(instance);
  464. const SubGhzProtocol* protocol = instance->decoder_result->protocol;
  465. if(check_type) {
  466. return (
  467. ((protocol->flag & SubGhzProtocolFlag_Send) == SubGhzProtocolFlag_Send) &&
  468. protocol->encoder->deserialize && protocol->type == SubGhzProtocolTypeStatic);
  469. }
  470. return (
  471. ((protocol->flag & SubGhzProtocolFlag_Send) == SubGhzProtocolFlag_Send) &&
  472. protocol->encoder->deserialize);
  473. }*/
  474. /*void subghz_txrx_receiver_set_filter(SubGhzTxRx* instance, SubGhzProtocolFlag filter) {
  475. furi_assert(instance);
  476. subghz_receiver_set_filter(instance->receiver, filter);
  477. }*/
  478. /*void subghz_txrx_set_rx_calback(
  479. SubGhzTxRx* instance,
  480. SubGhzReceiverCallback callback,
  481. void* context) {
  482. subghz_receiver_set_rx_callback(instance->receiver, callback, context);
  483. }*/
  484. void subghz_txrx_set_raw_file_encoder_worker_callback_end(
  485. SubGhzTxRx* instance,
  486. SubGhzProtocolEncoderRAWCallbackEnd callback,
  487. void* context) {
  488. subghz_protocol_raw_file_encoder_worker_set_callback_end(
  489. (SubGhzProtocolEncoderRAW*)subghz_transmitter_get_protocol_instance(instance->transmitter),
  490. callback,
  491. context);
  492. }
  493. bool subghz_txrx_radio_device_is_external_connected(SubGhzTxRx* instance, const char* name) {
  494. furi_assert(instance);
  495. bool is_connect = false;
  496. bool is_otg_enabled = furi_hal_power_is_otg_enabled();
  497. if(!is_otg_enabled) {
  498. subghz_txrx_radio_device_power_on(instance);
  499. }
  500. const SubGhzDevice* device = subghz_devices_get_by_name(name);
  501. if(device) {
  502. is_connect = subghz_devices_is_connect(device);
  503. }
  504. if(!is_otg_enabled) {
  505. subghz_txrx_radio_device_power_off(instance);
  506. }
  507. return is_connect;
  508. }
  509. SubGhzRadioDeviceType
  510. subghz_txrx_radio_device_set(SubGhzTxRx* instance, SubGhzRadioDeviceType radio_device_type) {
  511. furi_assert(instance);
  512. if(radio_device_type == SubGhzRadioDeviceTypeExternalCC1101 &&
  513. subghz_txrx_radio_device_is_external_connected(instance, SUBGHZ_DEVICE_CC1101_EXT_NAME)) {
  514. subghz_txrx_radio_device_power_on(instance);
  515. instance->radio_device = subghz_devices_get_by_name(SUBGHZ_DEVICE_CC1101_EXT_NAME);
  516. subghz_devices_begin(instance->radio_device);
  517. instance->radio_device_type = SubGhzRadioDeviceTypeExternalCC1101;
  518. } else {
  519. subghz_txrx_radio_device_power_off(instance);
  520. if(instance->radio_device_type != SubGhzRadioDeviceTypeInternal) {
  521. subghz_devices_end(instance->radio_device);
  522. }
  523. instance->radio_device = subghz_devices_get_by_name(SUBGHZ_DEVICE_CC1101_INT_NAME);
  524. instance->radio_device_type = SubGhzRadioDeviceTypeInternal;
  525. }
  526. return instance->radio_device_type;
  527. }
  528. /*SubGhzRadioDeviceType subghz_txrx_radio_device_get(SubGhzTxRx* instance) {
  529. furi_assert(instance);
  530. return instance->radio_device_type;
  531. }*/
  532. /*float subghz_txrx_radio_device_get_rssi(SubGhzTxRx* instance) {
  533. furi_assert(instance);
  534. return subghz_devices_get_rssi(instance->radio_device);
  535. }*/
  536. const char* subghz_txrx_radio_device_get_name(SubGhzTxRx* instance) {
  537. furi_assert(instance);
  538. return subghz_devices_get_name(instance->radio_device);
  539. }
  540. bool subghz_txrx_radio_device_is_frequecy_valid(SubGhzTxRx* instance, uint32_t frequency) {
  541. furi_assert(instance);
  542. return subghz_devices_is_frequency_valid(instance->radio_device, frequency);
  543. }