subghz_protocol_gate_tx.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. #include "subghz_protocol_gate_tx.h"
  2. struct SubGhzProtocolGateTX {
  3. SubGhzProtocolCommon common;
  4. };
  5. typedef enum {
  6. GateTXDecoderStepReset = 0,
  7. GateTXDecoderStepFoundStartBit,
  8. GateTXDecoderStepSaveDuration,
  9. GateTXDecoderStepCheckDuration,
  10. } GateTXDecoderStep;
  11. SubGhzProtocolGateTX* subghz_protocol_gate_tx_alloc(void) {
  12. SubGhzProtocolGateTX* instance = furi_alloc(sizeof(SubGhzProtocolGateTX));
  13. instance->common.name = "GateTX";
  14. instance->common.code_min_count_bit_for_found = 24;
  15. instance->common.te_short = 350;
  16. instance->common.te_long = 700;
  17. instance->common.te_delta = 100;
  18. instance->common.type_protocol = SubGhzProtocolCommonTypeStatic;
  19. instance->common.to_string = (SubGhzProtocolCommonToStr)subghz_protocol_gate_tx_to_str;
  20. instance->common.to_save_string =
  21. (SubGhzProtocolCommonGetStrSave)subghz_protocol_gate_tx_to_save_str;
  22. instance->common.to_load_protocol_from_file =
  23. (SubGhzProtocolCommonLoadFromFile)subghz_protocol_gate_tx_to_load_protocol_from_file;
  24. instance->common.to_load_protocol =
  25. (SubGhzProtocolCommonLoadFromRAW)subghz_decoder_gate_tx_to_load_protocol;
  26. instance->common.get_upload_protocol =
  27. (SubGhzProtocolCommonEncoderGetUpLoad)subghz_protocol_gate_tx_send_key;
  28. return instance;
  29. }
  30. void subghz_protocol_gate_tx_free(SubGhzProtocolGateTX* instance) {
  31. furi_assert(instance);
  32. free(instance);
  33. }
  34. bool subghz_protocol_gate_tx_send_key(
  35. SubGhzProtocolGateTX* instance,
  36. SubGhzProtocolCommonEncoder* encoder) {
  37. furi_assert(instance);
  38. furi_assert(encoder);
  39. size_t index = 0;
  40. encoder->size_upload = (instance->common.code_last_count_bit * 2) + 2;
  41. if(encoder->size_upload > SUBGHZ_ENCODER_UPLOAD_MAX_SIZE) return false;
  42. //Send header
  43. encoder->upload[index++] =
  44. level_duration_make(false, (uint32_t)instance->common.te_short * 49);
  45. //Send start bit
  46. encoder->upload[index++] = level_duration_make(true, (uint32_t)instance->common.te_long);
  47. //Send key data
  48. for(uint8_t i = instance->common.code_last_count_bit; i > 0; i--) {
  49. if(bit_read(instance->common.code_last_found, i - 1)) {
  50. //send bit 1
  51. encoder->upload[index++] =
  52. level_duration_make(false, (uint32_t)instance->common.te_long);
  53. encoder->upload[index++] =
  54. level_duration_make(true, (uint32_t)instance->common.te_short);
  55. } else {
  56. //send bit 0
  57. encoder->upload[index++] =
  58. level_duration_make(false, (uint32_t)instance->common.te_short);
  59. encoder->upload[index++] =
  60. level_duration_make(true, (uint32_t)instance->common.te_long);
  61. }
  62. }
  63. return true;
  64. }
  65. void subghz_protocol_gate_tx_reset(SubGhzProtocolGateTX* instance) {
  66. instance->common.parser_step = GateTXDecoderStepReset;
  67. }
  68. /** Analysis of received data
  69. *
  70. * @param instance SubGhzProtocolFaacSLH instance
  71. */
  72. void subghz_protocol_gate_tx_check_remote_controller(SubGhzProtocolGateTX* instance) {
  73. uint32_t code_found_reverse = subghz_protocol_common_reverse_key(
  74. instance->common.code_last_found, instance->common.code_last_count_bit);
  75. instance->common.serial = (code_found_reverse & 0xFF) << 12 |
  76. ((code_found_reverse >> 8) & 0xFF) << 4 |
  77. ((code_found_reverse >> 20) & 0x0F);
  78. instance->common.btn = ((code_found_reverse >> 16) & 0x0F);
  79. }
  80. void subghz_protocol_gate_tx_parse(SubGhzProtocolGateTX* instance, bool level, uint32_t duration) {
  81. switch(instance->common.parser_step) {
  82. case GateTXDecoderStepReset:
  83. if((!level) && (DURATION_DIFF(duration, instance->common.te_short * 47) <
  84. instance->common.te_delta * 47)) {
  85. //Found Preambula
  86. instance->common.parser_step = GateTXDecoderStepFoundStartBit;
  87. } else {
  88. instance->common.parser_step = GateTXDecoderStepReset;
  89. }
  90. break;
  91. case GateTXDecoderStepFoundStartBit:
  92. if(level &&
  93. ((DURATION_DIFF(duration, instance->common.te_long) < instance->common.te_delta * 3))) {
  94. //Found start bit
  95. instance->common.parser_step = GateTXDecoderStepSaveDuration;
  96. instance->common.code_found = 0;
  97. instance->common.code_count_bit = 0;
  98. } else {
  99. instance->common.parser_step = GateTXDecoderStepReset;
  100. }
  101. break;
  102. case GateTXDecoderStepSaveDuration:
  103. if(!level) {
  104. if(duration >= (instance->common.te_short * 10 + instance->common.te_delta)) {
  105. instance->common.parser_step = GateTXDecoderStepFoundStartBit;
  106. if(instance->common.code_count_bit >=
  107. instance->common.code_min_count_bit_for_found) {
  108. instance->common.code_last_found = instance->common.code_found;
  109. instance->common.code_last_count_bit = instance->common.code_count_bit;
  110. if(instance->common.callback)
  111. instance->common.callback(
  112. (SubGhzProtocolCommon*)instance, instance->common.context);
  113. }
  114. instance->common.code_found = 0;
  115. instance->common.code_count_bit = 0;
  116. break;
  117. } else {
  118. instance->common.te_last = duration;
  119. instance->common.parser_step = GateTXDecoderStepCheckDuration;
  120. }
  121. }
  122. break;
  123. case GateTXDecoderStepCheckDuration:
  124. if(level) {
  125. if((DURATION_DIFF(instance->common.te_last, instance->common.te_short) <
  126. instance->common.te_delta) &&
  127. (DURATION_DIFF(duration, instance->common.te_long) <
  128. instance->common.te_delta * 3)) {
  129. subghz_protocol_common_add_bit(&instance->common, 0);
  130. instance->common.parser_step = GateTXDecoderStepSaveDuration;
  131. } else if(
  132. (DURATION_DIFF(instance->common.te_last, instance->common.te_long) <
  133. instance->common.te_delta * 3) &&
  134. (DURATION_DIFF(duration, instance->common.te_short) < instance->common.te_delta)) {
  135. subghz_protocol_common_add_bit(&instance->common, 1);
  136. instance->common.parser_step = GateTXDecoderStepSaveDuration;
  137. } else {
  138. instance->common.parser_step = GateTXDecoderStepReset;
  139. }
  140. } else {
  141. instance->common.parser_step = GateTXDecoderStepReset;
  142. }
  143. break;
  144. }
  145. }
  146. void subghz_protocol_gate_tx_to_str(SubGhzProtocolGateTX* instance, string_t output) {
  147. subghz_protocol_gate_tx_check_remote_controller(instance);
  148. string_cat_printf(
  149. output,
  150. "%s %dbit\r\n"
  151. "Key:%06lX\r\n"
  152. "Sn:%05lX Btn:%lX\r\n",
  153. instance->common.name,
  154. instance->common.code_last_count_bit,
  155. (uint32_t)(instance->common.code_last_found & 0xFFFFFF),
  156. instance->common.serial,
  157. instance->common.btn);
  158. }
  159. void subghz_protocol_gate_tx_to_save_str(SubGhzProtocolGateTX* instance, string_t output) {
  160. string_printf(
  161. output,
  162. "Protocol: %s\n"
  163. "Bit: %d\n"
  164. "Key: %08lX\n",
  165. instance->common.name,
  166. instance->common.code_last_count_bit,
  167. (uint32_t)(instance->common.code_last_found & 0x00000000ffffffff));
  168. }
  169. bool subghz_protocol_gate_tx_to_load_protocol_from_file(
  170. FileWorker* file_worker,
  171. SubGhzProtocolGateTX* instance,
  172. const char* file_path) {
  173. bool loaded = false;
  174. string_t temp_str;
  175. string_init(temp_str);
  176. int res = 0;
  177. int data = 0;
  178. do {
  179. // Read and parse bit data from 2nd line
  180. if(!file_worker_read_until(file_worker, temp_str, '\n')) {
  181. break;
  182. }
  183. res = sscanf(string_get_cstr(temp_str), "Bit: %d\n", &data);
  184. if(res != 1) {
  185. break;
  186. }
  187. instance->common.code_last_count_bit = (uint8_t)data;
  188. // Read and parse key data from 3nd line
  189. if(!file_worker_read_until(file_worker, temp_str, '\n')) {
  190. break;
  191. }
  192. uint32_t temp_key = 0;
  193. res = sscanf(string_get_cstr(temp_str), "Key: %08lX\n", &temp_key);
  194. if(res != 1) {
  195. break;
  196. }
  197. instance->common.code_last_found = (uint64_t)temp_key;
  198. subghz_protocol_gate_tx_check_remote_controller(instance);
  199. loaded = true;
  200. } while(0);
  201. string_clear(temp_str);
  202. return loaded;
  203. }
  204. void subghz_decoder_gate_tx_to_load_protocol(SubGhzProtocolGateTX* instance, void* context) {
  205. furi_assert(context);
  206. furi_assert(instance);
  207. SubGhzProtocolCommonLoad* data = context;
  208. instance->common.code_last_found = data->code_found;
  209. instance->common.code_last_count_bit = data->code_count_bit;
  210. subghz_protocol_gate_tx_check_remote_controller(instance);
  211. }