kia.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. #include "kia.h"
  2. #include "../blocks/const.h"
  3. #include "../blocks/decoder.h"
  4. #include "../blocks/encoder.h"
  5. #include "../blocks/generic.h"
  6. #include "../blocks/math.h"
  7. #define TAG "SubGhzProtocoKIA"
  8. static const SubGhzBlockConst subghz_protocol_kia_const = {
  9. .te_short = 250,
  10. .te_long = 500,
  11. .te_delta = 100,
  12. .min_count_bit_for_found = 60,
  13. };
  14. struct SubGhzProtocolDecoderKIA {
  15. SubGhzProtocolDecoderBase base;
  16. SubGhzBlockDecoder decoder;
  17. SubGhzBlockGeneric generic;
  18. uint16_t header_count;
  19. };
  20. struct SubGhzProtocolEncoderKIA {
  21. SubGhzProtocolEncoderBase base;
  22. SubGhzProtocolBlockEncoder encoder;
  23. SubGhzBlockGeneric generic;
  24. };
  25. typedef enum {
  26. KIADecoderStepReset = 0,
  27. KIADecoderStepCheckPreambula,
  28. KIADecoderStepSaveDuration,
  29. KIADecoderStepCheckDuration,
  30. } KIADecoderStep;
  31. const SubGhzProtocolDecoder subghz_protocol_kia_decoder = {
  32. .alloc = subghz_protocol_decoder_kia_alloc,
  33. .free = subghz_protocol_decoder_kia_free,
  34. .feed = subghz_protocol_decoder_kia_feed,
  35. .reset = subghz_protocol_decoder_kia_reset,
  36. .get_hash_data = subghz_protocol_decoder_kia_get_hash_data,
  37. .serialize = subghz_protocol_decoder_kia_serialize,
  38. .deserialize = subghz_protocol_decoder_kia_deserialize,
  39. .get_string = subghz_protocol_decoder_kia_get_string,
  40. };
  41. const SubGhzProtocolEncoder subghz_protocol_kia_encoder = {
  42. .alloc = NULL,
  43. .free = NULL,
  44. .deserialize = NULL,
  45. .stop = NULL,
  46. .yield = NULL,
  47. };
  48. const SubGhzProtocol subghz_protocol_kia = {
  49. .name = SUBGHZ_PROTOCOL_KIA_NAME,
  50. .type = SubGhzProtocolTypeDynamic,
  51. .flag = SubGhzProtocolFlag_433 | SubGhzProtocolFlag_FM | SubGhzProtocolFlag_Decodable,
  52. .decoder = &subghz_protocol_kia_decoder,
  53. .encoder = &subghz_protocol_kia_encoder,
  54. };
  55. void* subghz_protocol_decoder_kia_alloc(SubGhzEnvironment* environment) {
  56. SubGhzProtocolDecoderKIA* instance = malloc(sizeof(SubGhzProtocolDecoderKIA));
  57. instance->base.protocol = &subghz_protocol_kia;
  58. instance->generic.protocol_name = instance->base.protocol->name;
  59. return instance;
  60. }
  61. void subghz_protocol_decoder_kia_free(void* context) {
  62. furi_assert(context);
  63. SubGhzProtocolDecoderKIA* instance = context;
  64. free(instance);
  65. }
  66. void subghz_protocol_decoder_kia_reset(void* context) {
  67. furi_assert(context);
  68. SubGhzProtocolDecoderKIA* instance = context;
  69. instance->decoder.parser_step = KIADecoderStepReset;
  70. }
  71. void subghz_protocol_decoder_kia_feed(void* context, bool level, uint32_t duration) {
  72. furi_assert(context);
  73. SubGhzProtocolDecoderKIA* instance = context;
  74. switch(instance->decoder.parser_step) {
  75. case KIADecoderStepReset:
  76. if((level) && (DURATION_DIFF(duration, subghz_protocol_kia_const.te_short) <
  77. subghz_protocol_kia_const.te_delta)) {
  78. instance->decoder.parser_step = KIADecoderStepCheckPreambula;
  79. instance->decoder.te_last = duration;
  80. instance->header_count = 0;
  81. }
  82. break;
  83. case KIADecoderStepCheckPreambula:
  84. if(level) {
  85. if((DURATION_DIFF(duration, subghz_protocol_kia_const.te_short) <
  86. subghz_protocol_kia_const.te_delta) ||
  87. (DURATION_DIFF(duration, subghz_protocol_kia_const.te_long) <
  88. subghz_protocol_kia_const.te_delta)) {
  89. instance->decoder.te_last = duration;
  90. } else {
  91. instance->decoder.parser_step = KIADecoderStepReset;
  92. }
  93. } else if(
  94. (DURATION_DIFF(duration, subghz_protocol_kia_const.te_short) <
  95. subghz_protocol_kia_const.te_delta) &&
  96. (DURATION_DIFF(instance->decoder.te_last, subghz_protocol_kia_const.te_short) <
  97. subghz_protocol_kia_const.te_delta)) {
  98. // Found header
  99. instance->header_count++;
  100. break;
  101. } else if(
  102. (DURATION_DIFF(duration, subghz_protocol_kia_const.te_long) <
  103. subghz_protocol_kia_const.te_delta) &&
  104. (DURATION_DIFF(instance->decoder.te_last, subghz_protocol_kia_const.te_long) <
  105. subghz_protocol_kia_const.te_delta)) {
  106. // Found start bit
  107. if(instance->header_count > 15) {
  108. instance->decoder.parser_step = KIADecoderStepSaveDuration;
  109. instance->decoder.decode_data = 0;
  110. instance->decoder.decode_count_bit = 1;
  111. subghz_protocol_blocks_add_bit(&instance->decoder, 1);
  112. } else {
  113. instance->decoder.parser_step = KIADecoderStepReset;
  114. }
  115. } else {
  116. instance->decoder.parser_step = KIADecoderStepReset;
  117. }
  118. break;
  119. case KIADecoderStepSaveDuration:
  120. if(level) {
  121. if(duration >=
  122. (subghz_protocol_kia_const.te_long + subghz_protocol_kia_const.te_delta * 2)) {
  123. //Found stop bit
  124. instance->decoder.parser_step = KIADecoderStepReset;
  125. if(instance->decoder.decode_count_bit >=
  126. subghz_protocol_kia_const.min_count_bit_for_found) {
  127. instance->generic.data = instance->decoder.decode_data;
  128. instance->generic.data_count_bit = instance->decoder.decode_count_bit;
  129. if(instance->base.callback)
  130. instance->base.callback(&instance->base, instance->base.context);
  131. }
  132. instance->decoder.decode_data = 0;
  133. instance->decoder.decode_count_bit = 0;
  134. break;
  135. } else {
  136. instance->decoder.te_last = duration;
  137. instance->decoder.parser_step = KIADecoderStepCheckDuration;
  138. }
  139. } else {
  140. instance->decoder.parser_step = KIADecoderStepReset;
  141. }
  142. break;
  143. case KIADecoderStepCheckDuration:
  144. if(!level) {
  145. if((DURATION_DIFF(instance->decoder.te_last, subghz_protocol_kia_const.te_short) <
  146. subghz_protocol_kia_const.te_delta) &&
  147. (DURATION_DIFF(duration, subghz_protocol_kia_const.te_short) <
  148. subghz_protocol_kia_const.te_delta)) {
  149. subghz_protocol_blocks_add_bit(&instance->decoder, 0);
  150. instance->decoder.parser_step = KIADecoderStepSaveDuration;
  151. } else if(
  152. (DURATION_DIFF(instance->decoder.te_last, subghz_protocol_kia_const.te_long) <
  153. subghz_protocol_kia_const.te_delta) &&
  154. (DURATION_DIFF(duration, subghz_protocol_kia_const.te_long) <
  155. subghz_protocol_kia_const.te_delta)) {
  156. subghz_protocol_blocks_add_bit(&instance->decoder, 1);
  157. instance->decoder.parser_step = KIADecoderStepSaveDuration;
  158. } else {
  159. instance->decoder.parser_step = KIADecoderStepReset;
  160. }
  161. } else {
  162. instance->decoder.parser_step = KIADecoderStepReset;
  163. }
  164. break;
  165. }
  166. }
  167. uint8_t subghz_protocol_kia_crc8(uint8_t* data, size_t len) {
  168. uint8_t crc = 0x08;
  169. size_t i, j;
  170. for(i = 0; i < len; i++) {
  171. crc ^= data[i];
  172. for(j = 0; j < 8; j++) {
  173. if((crc & 0x80) != 0)
  174. crc = (uint8_t)((crc << 1) ^ 0x7F);
  175. else
  176. crc <<= 1;
  177. }
  178. }
  179. return crc;
  180. }
  181. /**
  182. * Analysis of received data
  183. * @param instance Pointer to a SubGhzBlockGeneric* instance
  184. */
  185. static void subghz_protocol_kia_check_remote_controller(SubGhzBlockGeneric* instance) {
  186. /*
  187. * 0x0F 0112 43B04EC 1 7D
  188. * 0x0F 0113 43B04EC 1 DF
  189. * 0x0F 0114 43B04EC 1 30
  190. * 0x0F 0115 43B04EC 2 13
  191. * 0x0F 0116 43B04EC 3 F5
  192. * CNT Serial K CRC8 Kia (CRC8, poly 0x7f, start_crc 0x08)
  193. */
  194. instance->serial = (uint32_t)((instance->data >> 12) & 0x0FFFFFFF);
  195. instance->btn = (instance->data >> 8) & 0x0F;
  196. instance->cnt = (instance->data >> 40) & 0xFFFF;
  197. }
  198. uint8_t subghz_protocol_decoder_kia_get_hash_data(void* context) {
  199. furi_assert(context);
  200. SubGhzProtocolDecoderKIA* instance = context;
  201. return subghz_protocol_blocks_get_hash_data(
  202. &instance->decoder, (instance->decoder.decode_count_bit / 8) + 1);
  203. }
  204. bool subghz_protocol_decoder_kia_serialize(
  205. void* context,
  206. FlipperFormat* flipper_format,
  207. uint32_t frequency,
  208. FuriHalSubGhzPreset preset) {
  209. furi_assert(context);
  210. SubGhzProtocolDecoderKIA* instance = context;
  211. return subghz_block_generic_serialize(&instance->generic, flipper_format, frequency, preset);
  212. }
  213. bool subghz_protocol_decoder_kia_deserialize(void* context, FlipperFormat* flipper_format) {
  214. furi_assert(context);
  215. SubGhzProtocolDecoderKIA* instance = context;
  216. return subghz_block_generic_deserialize(&instance->generic, flipper_format);
  217. }
  218. void subghz_protocol_decoder_kia_get_string(void* context, string_t output) {
  219. furi_assert(context);
  220. SubGhzProtocolDecoderKIA* instance = context;
  221. subghz_protocol_kia_check_remote_controller(&instance->generic);
  222. uint32_t code_found_hi = instance->generic.data >> 32;
  223. uint32_t code_found_lo = instance->generic.data & 0x00000000ffffffff;
  224. string_cat_printf(
  225. output,
  226. "%s %dbit\r\n"
  227. "Key:%08lX%08lX\r\n"
  228. "Sn:%07lX Btn:%lX Cnt:%04X\r\n",
  229. instance->generic.protocol_name,
  230. instance->generic.data_count_bit,
  231. code_found_hi,
  232. code_found_lo,
  233. instance->generic.serial,
  234. instance->generic.btn,
  235. instance->generic.cnt);
  236. }