kia.c 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  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 = 61,
  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. UNUSED(environment);
  57. SubGhzProtocolDecoderKIA* instance = malloc(sizeof(SubGhzProtocolDecoderKIA));
  58. instance->base.protocol = &subghz_protocol_kia;
  59. instance->generic.protocol_name = instance->base.protocol->name;
  60. return instance;
  61. }
  62. void subghz_protocol_decoder_kia_free(void* context) {
  63. furi_assert(context);
  64. SubGhzProtocolDecoderKIA* instance = context;
  65. free(instance);
  66. }
  67. void subghz_protocol_decoder_kia_reset(void* context) {
  68. furi_assert(context);
  69. SubGhzProtocolDecoderKIA* instance = context;
  70. instance->decoder.parser_step = KIADecoderStepReset;
  71. }
  72. void subghz_protocol_decoder_kia_feed(void* context, bool level, uint32_t duration) {
  73. furi_assert(context);
  74. SubGhzProtocolDecoderKIA* instance = context;
  75. switch(instance->decoder.parser_step) {
  76. case KIADecoderStepReset:
  77. if((level) && (DURATION_DIFF(duration, subghz_protocol_kia_const.te_short) <
  78. subghz_protocol_kia_const.te_delta)) {
  79. instance->decoder.parser_step = KIADecoderStepCheckPreambula;
  80. instance->decoder.te_last = duration;
  81. instance->header_count = 0;
  82. }
  83. break;
  84. case KIADecoderStepCheckPreambula:
  85. if(level) {
  86. if((DURATION_DIFF(duration, subghz_protocol_kia_const.te_short) <
  87. subghz_protocol_kia_const.te_delta) ||
  88. (DURATION_DIFF(duration, subghz_protocol_kia_const.te_long) <
  89. subghz_protocol_kia_const.te_delta)) {
  90. instance->decoder.te_last = duration;
  91. } else {
  92. instance->decoder.parser_step = KIADecoderStepReset;
  93. }
  94. } else if(
  95. (DURATION_DIFF(duration, subghz_protocol_kia_const.te_short) <
  96. subghz_protocol_kia_const.te_delta) &&
  97. (DURATION_DIFF(instance->decoder.te_last, subghz_protocol_kia_const.te_short) <
  98. subghz_protocol_kia_const.te_delta)) {
  99. // Found header
  100. instance->header_count++;
  101. break;
  102. } else if(
  103. (DURATION_DIFF(duration, subghz_protocol_kia_const.te_long) <
  104. subghz_protocol_kia_const.te_delta) &&
  105. (DURATION_DIFF(instance->decoder.te_last, subghz_protocol_kia_const.te_long) <
  106. subghz_protocol_kia_const.te_delta)) {
  107. // Found start bit
  108. if(instance->header_count > 15) {
  109. instance->decoder.parser_step = KIADecoderStepSaveDuration;
  110. instance->decoder.decode_data = 0;
  111. instance->decoder.decode_count_bit = 1;
  112. subghz_protocol_blocks_add_bit(&instance->decoder, 1);
  113. } else {
  114. instance->decoder.parser_step = KIADecoderStepReset;
  115. }
  116. } else {
  117. instance->decoder.parser_step = KIADecoderStepReset;
  118. }
  119. break;
  120. case KIADecoderStepSaveDuration:
  121. if(level) {
  122. if(duration >=
  123. (subghz_protocol_kia_const.te_long + subghz_protocol_kia_const.te_delta * 2UL)) {
  124. //Found stop bit
  125. instance->decoder.parser_step = KIADecoderStepReset;
  126. if(instance->decoder.decode_count_bit ==
  127. subghz_protocol_kia_const.min_count_bit_for_found) {
  128. instance->generic.data = instance->decoder.decode_data;
  129. instance->generic.data_count_bit = instance->decoder.decode_count_bit;
  130. if(instance->base.callback)
  131. instance->base.callback(&instance->base, instance->base.context);
  132. }
  133. instance->decoder.decode_data = 0;
  134. instance->decoder.decode_count_bit = 0;
  135. break;
  136. } else {
  137. instance->decoder.te_last = duration;
  138. instance->decoder.parser_step = KIADecoderStepCheckDuration;
  139. }
  140. } else {
  141. instance->decoder.parser_step = KIADecoderStepReset;
  142. }
  143. break;
  144. case KIADecoderStepCheckDuration:
  145. if(!level) {
  146. if((DURATION_DIFF(instance->decoder.te_last, subghz_protocol_kia_const.te_short) <
  147. subghz_protocol_kia_const.te_delta) &&
  148. (DURATION_DIFF(duration, subghz_protocol_kia_const.te_short) <
  149. subghz_protocol_kia_const.te_delta)) {
  150. subghz_protocol_blocks_add_bit(&instance->decoder, 0);
  151. instance->decoder.parser_step = KIADecoderStepSaveDuration;
  152. } else if(
  153. (DURATION_DIFF(instance->decoder.te_last, subghz_protocol_kia_const.te_long) <
  154. subghz_protocol_kia_const.te_delta) &&
  155. (DURATION_DIFF(duration, subghz_protocol_kia_const.te_long) <
  156. subghz_protocol_kia_const.te_delta)) {
  157. subghz_protocol_blocks_add_bit(&instance->decoder, 1);
  158. instance->decoder.parser_step = KIADecoderStepSaveDuration;
  159. } else {
  160. instance->decoder.parser_step = KIADecoderStepReset;
  161. }
  162. } else {
  163. instance->decoder.parser_step = KIADecoderStepReset;
  164. }
  165. break;
  166. }
  167. }
  168. uint8_t subghz_protocol_kia_crc8(uint8_t* data, size_t len) {
  169. uint8_t crc = 0x08;
  170. size_t i, j;
  171. for(i = 0; i < len; i++) {
  172. crc ^= data[i];
  173. for(j = 0; j < 8; j++) {
  174. if((crc & 0x80) != 0)
  175. crc = (uint8_t)((crc << 1) ^ 0x7F);
  176. else
  177. crc <<= 1;
  178. }
  179. }
  180. return crc;
  181. }
  182. /**
  183. * Analysis of received data
  184. * @param instance Pointer to a SubGhzBlockGeneric* instance
  185. */
  186. static void subghz_protocol_kia_check_remote_controller(SubGhzBlockGeneric* instance) {
  187. /*
  188. * 0x0F 0112 43B04EC 1 7D
  189. * 0x0F 0113 43B04EC 1 DF
  190. * 0x0F 0114 43B04EC 1 30
  191. * 0x0F 0115 43B04EC 2 13
  192. * 0x0F 0116 43B04EC 3 F5
  193. * CNT Serial K CRC8 Kia (CRC8, poly 0x7f, start_crc 0x08)
  194. */
  195. instance->serial = (uint32_t)((instance->data >> 12) & 0x0FFFFFFF);
  196. instance->btn = (instance->data >> 8) & 0x0F;
  197. instance->cnt = (instance->data >> 40) & 0xFFFF;
  198. }
  199. uint8_t subghz_protocol_decoder_kia_get_hash_data(void* context) {
  200. furi_assert(context);
  201. SubGhzProtocolDecoderKIA* instance = context;
  202. return subghz_protocol_blocks_get_hash_data(
  203. &instance->decoder, (instance->decoder.decode_count_bit / 8) + 1);
  204. }
  205. bool subghz_protocol_decoder_kia_serialize(
  206. void* context,
  207. FlipperFormat* flipper_format,
  208. SubGhzRadioPreset* preset) {
  209. furi_assert(context);
  210. SubGhzProtocolDecoderKIA* instance = context;
  211. return subghz_block_generic_serialize(&instance->generic, flipper_format, preset);
  212. }
  213. bool subghz_protocol_decoder_kia_deserialize(void* context, FlipperFormat* flipper_format) {
  214. furi_assert(context);
  215. SubGhzProtocolDecoderKIA* instance = context;
  216. bool ret = false;
  217. do {
  218. if(!subghz_block_generic_deserialize(&instance->generic, flipper_format)) {
  219. break;
  220. }
  221. if(instance->generic.data_count_bit != subghz_protocol_kia_const.min_count_bit_for_found) {
  222. FURI_LOG_E(TAG, "Wrong number of bits in key");
  223. break;
  224. }
  225. ret = true;
  226. } while(false);
  227. return ret;
  228. }
  229. void subghz_protocol_decoder_kia_get_string(void* context, FuriString* output) {
  230. furi_assert(context);
  231. SubGhzProtocolDecoderKIA* instance = context;
  232. subghz_protocol_kia_check_remote_controller(&instance->generic);
  233. uint32_t code_found_hi = instance->generic.data >> 32;
  234. uint32_t code_found_lo = instance->generic.data & 0x00000000ffffffff;
  235. furi_string_cat_printf(
  236. output,
  237. "%s %dbit\r\n"
  238. "Key:%08lX%08lX\r\n"
  239. "Sn:%07lX Btn:%X Cnt:%04lX\r\n",
  240. instance->generic.protocol_name,
  241. instance->generic.data_count_bit,
  242. code_found_hi,
  243. code_found_lo,
  244. instance->generic.serial,
  245. instance->generic.btn,
  246. instance->generic.cnt);
  247. }