subghz_txrx.c 22 KB

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