subghz_txrx.c 22 KB

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