subghz_i.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. #include "subghz_i.h"
  2. #include <math.h>
  3. #include <furi.h>
  4. #include <furi-hal.h>
  5. #include <input/input.h>
  6. #include <gui/elements.h>
  7. #include <notification/notification-messages.h>
  8. #include "file-worker.h"
  9. #include "../notification/notification.h"
  10. void subghz_begin(FuriHalSubGhzPreset preset) {
  11. furi_hal_subghz_reset();
  12. furi_hal_subghz_idle();
  13. furi_hal_subghz_load_preset(preset);
  14. hal_gpio_init(&gpio_cc1101_g0, GpioModeInput, GpioPullNo, GpioSpeedLow);
  15. }
  16. void subghz_rx(uint32_t frequency) {
  17. furi_hal_subghz_idle();
  18. furi_hal_subghz_set_frequency_and_path(frequency);
  19. hal_gpio_init(&gpio_cc1101_g0, GpioModeInput, GpioPullNo, GpioSpeedLow);
  20. furi_hal_subghz_flush_rx();
  21. furi_hal_subghz_rx();
  22. }
  23. void subghz_tx(uint32_t frequency) {
  24. furi_hal_subghz_idle();
  25. furi_hal_subghz_set_frequency_and_path(frequency);
  26. hal_gpio_init(&gpio_cc1101_g0, GpioModeOutputPushPull, GpioPullNo, GpioSpeedLow);
  27. hal_gpio_write(&gpio_cc1101_g0, true);
  28. furi_hal_subghz_tx();
  29. }
  30. void subghz_idle(void) {
  31. furi_hal_subghz_idle();
  32. }
  33. void subghz_end(void) {
  34. furi_hal_subghz_sleep();
  35. }
  36. void subghz_transmitter_tx_start(void* context) {
  37. SubGhz* subghz = context;
  38. subghz->encoder = subghz_protocol_encoder_common_alloc();
  39. subghz->encoder->repeat = 200; //max repeat with the button held down
  40. //get upload
  41. if(subghz->protocol_result->get_upload_protocol) {
  42. if(subghz->protocol_result->get_upload_protocol(subghz->protocol_result, subghz->encoder)) {
  43. subghz_begin(FuriHalSubGhzPresetOokAsync);
  44. subghz_tx(433920000);
  45. //Start TX
  46. furi_hal_subghz_start_async_tx(subghz_protocol_encoder_common_yield, subghz->encoder);
  47. }
  48. }
  49. }
  50. void subghz_transmitter_tx_stop(void* context) {
  51. SubGhz* subghz = context;
  52. //Stop TX
  53. furi_hal_subghz_stop_async_tx();
  54. subghz_end();
  55. subghz_protocol_encoder_common_free(subghz->encoder);
  56. //if protocol dynamic then we save the last upload
  57. if(subghz->protocol_result->type_protocol == TYPE_PROTOCOL_DYNAMIC) {
  58. subghz_save_protocol_to_file(subghz, subghz->text_store);
  59. }
  60. notification_message(subghz->notifications, &sequence_reset_red);
  61. }
  62. bool subghz_key_load(SubGhz* subghz, const char* file_path) {
  63. furi_assert(subghz);
  64. furi_assert(file_path);
  65. FileWorker* file_worker = file_worker_alloc(false);
  66. // Load device data
  67. bool loaded = false;
  68. string_t path;
  69. string_init_set_str(path, file_path);
  70. string_t temp_str;
  71. string_init(temp_str);
  72. do {
  73. if(!file_worker_open(file_worker, string_get_cstr(path), FSAM_READ, FSOM_OPEN_EXISTING)) {
  74. break;
  75. }
  76. // Read and parse name protocol from 1st line
  77. if(!file_worker_read_until(file_worker, temp_str, '\n')) {
  78. break;
  79. }
  80. // strlen("Protocol: ") = 10
  81. string_right(temp_str, 10);
  82. subghz->protocol_result =
  83. subghz_protocol_get_by_name(subghz->protocol, string_get_cstr(temp_str));
  84. if(subghz->protocol_result == NULL) {
  85. file_worker_show_error(file_worker, "Cannot parse\nfile");
  86. break;
  87. }
  88. if(!subghz->protocol_result->to_load_protocol(file_worker, subghz->protocol_result)) {
  89. file_worker_show_error(file_worker, "Cannot parse\nfile");
  90. break;
  91. }
  92. loaded = true;
  93. } while(0);
  94. string_clear(temp_str);
  95. string_clear(path);
  96. file_worker_close(file_worker);
  97. file_worker_free(file_worker);
  98. return loaded;
  99. }
  100. bool subghz_save_protocol_to_file(void* context, const char* dev_name) {
  101. SubGhz* subghz = context;
  102. FileWorker* file_worker = file_worker_alloc(false);
  103. string_t dev_file_name;
  104. string_init(dev_file_name);
  105. string_t temp_str;
  106. string_init(temp_str);
  107. bool saved = false;
  108. do {
  109. // Create subghz folder directory if necessary
  110. if(!file_worker_mkdir(file_worker, SUBGHZ_APP_FOLDER)) {
  111. break;
  112. }
  113. // Create saved directory if necessary
  114. if(!file_worker_mkdir(file_worker, SUBGHZ_APP_PATH_FOLDER)) {
  115. break;
  116. }
  117. // First remove subghz device file if it was saved
  118. string_printf(
  119. dev_file_name, "%s/%s%s", SUBGHZ_APP_PATH_FOLDER, dev_name, SUBGHZ_APP_EXTENSION);
  120. if(!file_worker_remove(file_worker, string_get_cstr(dev_file_name))) {
  121. break;
  122. }
  123. // Open file
  124. if(!file_worker_open(
  125. file_worker, string_get_cstr(dev_file_name), FSAM_WRITE, FSOM_CREATE_ALWAYS)) {
  126. break;
  127. }
  128. //Get string save
  129. subghz->protocol_result->to_save_string(subghz->protocol_result, temp_str);
  130. // Prepare and write data to file
  131. if(!file_worker_write(file_worker, string_get_cstr(temp_str), string_size(temp_str))) {
  132. break;
  133. }
  134. saved = true;
  135. } while(0);
  136. string_clear(temp_str);
  137. string_clear(dev_file_name);
  138. file_worker_close(file_worker);
  139. file_worker_free(file_worker);
  140. return saved;
  141. }
  142. bool subghz_saved_protocol_select(SubGhz* subghz) {
  143. furi_assert(subghz);
  144. FileWorker* file_worker = file_worker_alloc(false);
  145. string_t protocol_file_name;
  146. string_init(protocol_file_name);
  147. string_t temp_str;
  148. string_init(temp_str);
  149. // Input events and views are managed by file_select
  150. bool res = file_worker_file_select(
  151. file_worker,
  152. SUBGHZ_APP_PATH_FOLDER,
  153. SUBGHZ_APP_EXTENSION,
  154. subghz->text_store,
  155. sizeof(subghz->text_store),
  156. NULL);
  157. if(res) {
  158. // Get key file path
  159. string_printf(
  160. protocol_file_name,
  161. "%s/%s%s",
  162. SUBGHZ_APP_PATH_FOLDER,
  163. subghz->text_store,
  164. SUBGHZ_APP_EXTENSION);
  165. } else {
  166. string_clear(temp_str);
  167. string_clear(protocol_file_name);
  168. file_worker_close(file_worker);
  169. file_worker_free(file_worker);
  170. return res;
  171. }
  172. res = false;
  173. do {
  174. if(!file_worker_open(
  175. file_worker, string_get_cstr(protocol_file_name), FSAM_READ, FSOM_OPEN_EXISTING)) {
  176. break;
  177. }
  178. // Read and parse name protocol from 1st line
  179. if(!file_worker_read_until(file_worker, temp_str, '\n')) {
  180. break;
  181. }
  182. // strlen("Protocol: ") = 10
  183. string_right(temp_str, 10);
  184. subghz->protocol_result =
  185. subghz_protocol_get_by_name(subghz->protocol, string_get_cstr(temp_str));
  186. if(subghz->protocol_result == NULL) {
  187. file_worker_show_error(file_worker, "Cannot parse\nfile");
  188. break;
  189. }
  190. if(!subghz->protocol_result->to_load_protocol(file_worker, subghz->protocol_result)) {
  191. file_worker_show_error(file_worker, "Cannot parse\nfile");
  192. break;
  193. }
  194. res = true;
  195. } while(0);
  196. string_clear(temp_str);
  197. string_clear(protocol_file_name);
  198. file_worker_close(file_worker);
  199. file_worker_free(file_worker);
  200. return res;
  201. }
  202. uint32_t subghz_random_serial(void) {
  203. static bool rand_generator_inited = false;
  204. if(!rand_generator_inited) {
  205. srand(DWT->CYCCNT);
  206. rand_generator_inited = true;
  207. }
  208. return (uint32_t)rand();
  209. }