xremote_sg_remote.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. #include "subghz/types.h"
  2. #include <lib/toolbox/path.h>
  3. #include <lib/subghz/types.h>
  4. #include <lib/subghz/subghz_worker.h>
  5. #include <lib/subghz/subghz_setting.h>
  6. #include <lib/subghz/receiver.h>
  7. #include <lib/subghz/transmitter.h>
  8. #include <lib/subghz/registry.h>
  9. //#include <lib/subghz/protocols/keeloq.h>
  10. //#include <lib/subghz/protocols/secplus_v1.h>
  11. //#include <lib/subghz/protocols/secplus_v2.h>
  12. //#include <lib/subghz/protocols/princeton_for_testing.h>
  13. //#include <lib/subghz/blocks/math.h>
  14. //#include <lib/subghz/protocols/raw.h>
  15. //#include <lib/subghz/protocols/bin_raw.h>
  16. //#include <lib/subghz/protocols/protocol_items.h>
  17. //#include <lib/subghz/protocols/protocol_items.c>
  18. #include <lib/subghz/subghz_keystore.h>
  19. //#include <lib/subghz/subghz_file_encoder_worker.h>
  20. #include <gui/modules/variable_item_list.h>
  21. #include "xremote_sg_remote.h"
  22. #define TAG "Xremote"
  23. struct SubGhzRemote {
  24. FuriString* name;
  25. FuriString* path;
  26. SubGhzTxRx* txrx;
  27. uint32_t frequency;
  28. SubGhzSetting* setting;
  29. };
  30. const char* xremote_sg_remote_get_name(SubGhzRemote* remote) {
  31. return furi_string_get_cstr(remote->name);
  32. }
  33. void subghz_preset_init(
  34. void* context,
  35. const char* preset_name,
  36. uint32_t frequency,
  37. uint8_t* preset_data,
  38. size_t preset_data_size) {
  39. furi_assert(context);
  40. SubGhzRemote* remote = context;
  41. furi_string_set(remote->txrx->preset->name, preset_name);
  42. remote->txrx->preset->frequency = frequency;
  43. remote->txrx->preset->data = preset_data;
  44. remote->txrx->preset->data_size = preset_data_size;
  45. }
  46. const char* subghz_txrx_radio_device_get_name(SubGhzTxRx* instance) {
  47. furi_assert(instance);
  48. return subghz_devices_get_name(instance->radio_device);
  49. }
  50. SubGhzRemote* xremote_sg_remote_alloc() {
  51. SubGhzRemote* remote = malloc(sizeof(SubGhzRemote));
  52. remote->name = furi_string_alloc();
  53. remote->path = furi_string_alloc();
  54. // SubGhz Settings
  55. remote->setting = subghz_setting_alloc();
  56. subghz_setting_load(remote->setting, EXT_PATH("subghz/assets/setting_user"));
  57. remote->txrx = malloc(sizeof(SubGhzTxRx));
  58. remote->txrx->preset = malloc(sizeof(SubGhzRadioPreset));
  59. remote->txrx->preset->name = furi_string_alloc();
  60. subghz_preset_init(
  61. remote, "AM650", subghz_setting_get_default_frequency(remote->setting), NULL, 0);
  62. remote->txrx->fff_data = flipper_format_string_alloc();
  63. remote->txrx->environment = subghz_environment_alloc();
  64. /*subghz_environment_set_came_atomo_rainbow_table_file_name(
  65. remote->txrx->environment, EXT_PATH("subghz/assets/came_atomo"));
  66. subghz_environment_set_alutech_at_4n_rainbow_table_file_name(
  67. remote->txrx->environment, EXT_PATH("subghz/assets/alutech_at_4n"));
  68. subghz_environment_set_nice_flor_s_rainbow_table_file_name(
  69. remote->txrx->environment, EXT_PATH("subghz/assets/nice_flor_s"));
  70. subghz_environment_set_protocol_registry(
  71. remote->txrx->environment, (void*)&subghz_protocol_registry);
  72. remote->txrx->receiver = subghz_receiver_alloc_init(remote->txrx->environment);*/
  73. //remote->txrx->worker = subghz_worker_alloc();
  74. return remote;
  75. }
  76. void xremote_sg_remote_free(SubGhzRemote* remote) {
  77. furi_string_free(remote->path);
  78. furi_string_free(remote->name);
  79. // TXRX
  80. subghz_receiver_free(remote->txrx->receiver);
  81. subghz_environment_free(remote->txrx->environment);
  82. flipper_format_free(remote->txrx->fff_data);
  83. furi_string_free(remote->txrx->preset->name);
  84. free(remote->txrx->preset);
  85. free(remote->txrx);
  86. free(remote);
  87. }
  88. bool xremtoe_sg_set_preset(SubGhzRemote* remote, const char* preset) {
  89. if(!strcmp(preset, "FuriHalSubGhzPresetOok270Async")) {
  90. furi_string_set(remote->txrx->preset->name, "AM270");
  91. } else if(!strcmp(preset, "FuriHalSubGhzPresetOok650Async")) {
  92. furi_string_set(remote->txrx->preset->name, "AM650");
  93. } else if(!strcmp(preset, "FuriHalSubGhzPreset2FSKDev238Async")) {
  94. furi_string_set(remote->txrx->preset->name, "FM238");
  95. } else if(!strcmp(preset, "FuriHalSubGhzPreset2FSKDev476Async")) {
  96. furi_string_set(remote->txrx->preset->name, "FM476");
  97. } else if(!strcmp(preset, "FuriHalSubGhzPresetCustom")) {
  98. furi_string_set(remote->txrx->preset->name, "CUSTOM");
  99. } else {
  100. FURI_LOG_E(TAG, "Unknown preset");
  101. return false;
  102. }
  103. return true;
  104. }
  105. uint32_t xremote_sg_remote_get_frequency(SubGhzRemote* remote) {
  106. return remote->txrx->preset->frequency;
  107. }
  108. const char* xremote_sg_remote_get_preset(SubGhzRemote* remote) {
  109. return furi_string_get_cstr(remote->txrx->preset->name);
  110. }
  111. bool xremote_sg_remote_load(SubGhzRemote* remote, FuriString* path) {
  112. Storage* storage = furi_record_open(RECORD_STORAGE);
  113. FlipperFormat* ff = flipper_format_buffered_file_alloc(storage);
  114. FuriString* buf;
  115. buf = furi_string_alloc();
  116. uint32_t temp_data32;
  117. FURI_LOG_I(TAG, "loading SG Remote: \'%s\'", furi_string_get_cstr(path));
  118. bool success = false;
  119. do {
  120. if(!flipper_format_buffered_file_open_existing(ff, furi_string_get_cstr(path))) break;
  121. const char* fullPath = furi_string_get_cstr(path);
  122. char* fileName = strrchr(fullPath, '/') + 1;
  123. char* dotPosition = strrchr(fileName, '.');
  124. if(dotPosition != NULL) { // check if there is a dot in the file name
  125. *dotPosition = '\0'; // set the dot position to NULL character to truncate the string
  126. }
  127. //remote->name = fileName;
  128. furi_string_set_str(remote->name, fileName);
  129. uint32_t version;
  130. if(!flipper_format_read_header(ff, buf, &version)) break;
  131. if(!furi_string_equal(buf, "Flipper SubGhz RAW File") || (version != 1)) break;
  132. if(!flipper_format_read_uint32(ff, "Frequency", &temp_data32, 1)) {
  133. FURI_LOG_E(TAG, "Missing Frequency");
  134. break;
  135. }
  136. remote->frequency = temp_data32;
  137. if(!flipper_format_read_string(ff, "Preset", buf)) {
  138. FURI_LOG_E(TAG, "Missing Preset");
  139. break;
  140. }
  141. if(!xremtoe_sg_set_preset(remote, furi_string_get_cstr(buf))) {
  142. break;
  143. }
  144. if(!strcmp(furi_string_get_cstr(buf), "FuriHalSubGhzPresetCustom")) {
  145. //Todo add Custom_preset_module
  146. //delete preset if it already exists
  147. subghz_setting_delete_custom_preset(
  148. remote->setting, furi_string_get_cstr(remote->txrx->preset->name));
  149. //load custom preset from file
  150. if(!subghz_setting_load_custom_preset(
  151. remote->setting, furi_string_get_cstr(remote->txrx->preset->name), ff)) {
  152. FURI_LOG_E(TAG, "Missing Custom preset");
  153. break;
  154. }
  155. }
  156. if(!flipper_format_read_string(ff, "Protocol", buf)) {
  157. FURI_LOG_E(TAG, "Missing Protocol");
  158. break;
  159. }
  160. if(!strcmp(furi_string_get_cstr(buf), "RAW")) {
  161. subghz_protocol_raw_gen_fff_data(
  162. remote->txrx->fff_data,
  163. furi_string_get_cstr(path),
  164. subghz_txrx_radio_device_get_name(remote->txrx));
  165. } else {
  166. stream_copy_full(
  167. flipper_format_get_raw_stream(ff),
  168. flipper_format_get_raw_stream(remote->txrx->fff_data));
  169. }
  170. /*remote->txrx->decoder_result = subghz_receiver_search_decoder_base_by_name(
  171. remote->txrx->receiver, furi_string_get_cstr(buf));*/
  172. if(remote->txrx->decoder_result) {
  173. SubGhzProtocolStatus status = subghz_protocol_decoder_base_deserialize(
  174. remote->txrx->decoder_result, remote->txrx->fff_data);
  175. if(status != SubGhzProtocolStatusOk) {
  176. //load_key_state = SubGhzLoadKeyStateProtocolDescriptionErr;
  177. success = false;
  178. break;
  179. }
  180. } else {
  181. FURI_LOG_E(TAG, "Protocol not found");
  182. break;
  183. }
  184. success = true;
  185. } while(false);
  186. furi_string_free(buf);
  187. flipper_format_free(ff);
  188. furi_record_close(RECORD_STORAGE);
  189. return success;
  190. }