scher_khan.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  1. #include "scher_khan.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. //https://phreakerclub.com/72
  8. //https://phreakerclub.com/forum/showthread.php?t=7&page=2
  9. //https://phreakerclub.com/forum/showthread.php?t=274&highlight=magicar
  10. //!!! https://phreakerclub.com/forum/showthread.php?t=489&highlight=magicar&page=5
  11. #define TAG "SubGhzProtocolScherKhan"
  12. static const SubGhzBlockConst subghz_protocol_scher_khan_const = {
  13. .te_short = 750,
  14. .te_long = 1100,
  15. .te_delta = 150,
  16. .min_count_bit_for_found = 35,
  17. };
  18. struct SubGhzProtocolDecoderScherKhan {
  19. SubGhzProtocolDecoderBase base;
  20. SubGhzBlockDecoder decoder;
  21. SubGhzBlockGeneric generic;
  22. uint16_t header_count;
  23. const char* protocol_name;
  24. };
  25. struct SubGhzProtocolEncoderScherKhan {
  26. SubGhzProtocolEncoderBase base;
  27. SubGhzProtocolBlockEncoder encoder;
  28. SubGhzBlockGeneric generic;
  29. };
  30. typedef enum {
  31. ScherKhanDecoderStepReset = 0,
  32. ScherKhanDecoderStepCheckPreambula,
  33. ScherKhanDecoderStepSaveDuration,
  34. ScherKhanDecoderStepCheckDuration,
  35. } ScherKhanDecoderStep;
  36. const SubGhzProtocolDecoder subghz_protocol_scher_khan_decoder = {
  37. .alloc = subghz_protocol_decoder_scher_khan_alloc,
  38. .free = subghz_protocol_decoder_scher_khan_free,
  39. .feed = subghz_protocol_decoder_scher_khan_feed,
  40. .reset = subghz_protocol_decoder_scher_khan_reset,
  41. .get_hash_data = subghz_protocol_decoder_scher_khan_get_hash_data,
  42. .serialize = subghz_protocol_decoder_scher_khan_serialize,
  43. .deserialize = subghz_protocol_decoder_scher_khan_deserialize,
  44. .get_string = subghz_protocol_decoder_scher_khan_get_string,
  45. };
  46. const SubGhzProtocolEncoder subghz_protocol_scher_khan_encoder = {
  47. .alloc = NULL,
  48. .free = NULL,
  49. .deserialize = NULL,
  50. .stop = NULL,
  51. .yield = NULL,
  52. };
  53. const SubGhzProtocol subghz_protocol_scher_khan = {
  54. .name = SUBGHZ_PROTOCOL_SCHER_KHAN_NAME,
  55. .type = SubGhzProtocolTypeDynamic,
  56. .flag = SubGhzProtocolFlag_433 | SubGhzProtocolFlag_FM | SubGhzProtocolFlag_Decodable,
  57. .decoder = &subghz_protocol_scher_khan_decoder,
  58. .encoder = &subghz_protocol_scher_khan_encoder,
  59. };
  60. void* subghz_protocol_decoder_scher_khan_alloc(SubGhzEnvironment* environment) {
  61. SubGhzProtocolDecoderScherKhan* instance = malloc(sizeof(SubGhzProtocolDecoderScherKhan));
  62. instance->base.protocol = &subghz_protocol_scher_khan;
  63. instance->generic.protocol_name = instance->base.protocol->name;
  64. return instance;
  65. }
  66. void subghz_protocol_decoder_scher_khan_free(void* context) {
  67. furi_assert(context);
  68. SubGhzProtocolDecoderScherKhan* instance = context;
  69. free(instance);
  70. }
  71. void subghz_protocol_decoder_scher_khan_reset(void* context) {
  72. furi_assert(context);
  73. SubGhzProtocolDecoderScherKhan* instance = context;
  74. instance->decoder.parser_step = ScherKhanDecoderStepReset;
  75. }
  76. void subghz_protocol_decoder_scher_khan_feed(void* context, bool level, uint32_t duration) {
  77. furi_assert(context);
  78. SubGhzProtocolDecoderScherKhan* instance = context;
  79. switch(instance->decoder.parser_step) {
  80. case ScherKhanDecoderStepReset:
  81. if((level) && (DURATION_DIFF(duration, subghz_protocol_scher_khan_const.te_short * 2) <
  82. subghz_protocol_scher_khan_const.te_delta)) {
  83. instance->decoder.parser_step = ScherKhanDecoderStepCheckPreambula;
  84. instance->decoder.te_last = duration;
  85. instance->header_count = 0;
  86. }
  87. break;
  88. case ScherKhanDecoderStepCheckPreambula:
  89. if(level) {
  90. if((DURATION_DIFF(duration, subghz_protocol_scher_khan_const.te_short * 2) <
  91. subghz_protocol_scher_khan_const.te_delta) ||
  92. (DURATION_DIFF(duration, subghz_protocol_scher_khan_const.te_short) <
  93. subghz_protocol_scher_khan_const.te_delta)) {
  94. instance->decoder.te_last = duration;
  95. } else {
  96. instance->decoder.parser_step = ScherKhanDecoderStepReset;
  97. }
  98. } else if(
  99. (DURATION_DIFF(duration, subghz_protocol_scher_khan_const.te_short * 2) <
  100. subghz_protocol_scher_khan_const.te_delta) ||
  101. (DURATION_DIFF(duration, subghz_protocol_scher_khan_const.te_short) <
  102. subghz_protocol_scher_khan_const.te_delta)) {
  103. if(DURATION_DIFF(
  104. instance->decoder.te_last, subghz_protocol_scher_khan_const.te_short * 2) <
  105. subghz_protocol_scher_khan_const.te_delta) {
  106. // Found header
  107. instance->header_count++;
  108. break;
  109. } else if(
  110. DURATION_DIFF(
  111. instance->decoder.te_last, subghz_protocol_scher_khan_const.te_short) <
  112. subghz_protocol_scher_khan_const.te_delta) {
  113. // Found start bit
  114. if(instance->header_count >= 2) {
  115. instance->decoder.parser_step = ScherKhanDecoderStepSaveDuration;
  116. instance->decoder.decode_data = 0;
  117. instance->decoder.decode_count_bit = 1;
  118. } else {
  119. instance->decoder.parser_step = ScherKhanDecoderStepReset;
  120. }
  121. } else {
  122. instance->decoder.parser_step = ScherKhanDecoderStepReset;
  123. }
  124. } else {
  125. instance->decoder.parser_step = ScherKhanDecoderStepReset;
  126. }
  127. break;
  128. case ScherKhanDecoderStepSaveDuration:
  129. if(level) {
  130. if(duration >= (subghz_protocol_scher_khan_const.te_long +
  131. subghz_protocol_scher_khan_const.te_delta * 2)) {
  132. //Found stop bit
  133. instance->decoder.parser_step = ScherKhanDecoderStepReset;
  134. if(instance->decoder.decode_count_bit >=
  135. subghz_protocol_scher_khan_const.min_count_bit_for_found) {
  136. instance->generic.data = instance->decoder.decode_data;
  137. instance->generic.data_count_bit = instance->decoder.decode_count_bit;
  138. if(instance->base.callback)
  139. instance->base.callback(&instance->base, instance->base.context);
  140. }
  141. instance->decoder.decode_data = 0;
  142. instance->decoder.decode_count_bit = 0;
  143. break;
  144. } else {
  145. instance->decoder.te_last = duration;
  146. instance->decoder.parser_step = ScherKhanDecoderStepCheckDuration;
  147. }
  148. } else {
  149. instance->decoder.parser_step = ScherKhanDecoderStepReset;
  150. }
  151. break;
  152. case ScherKhanDecoderStepCheckDuration:
  153. if(!level) {
  154. if((DURATION_DIFF(
  155. instance->decoder.te_last, subghz_protocol_scher_khan_const.te_short) <
  156. subghz_protocol_scher_khan_const.te_delta) &&
  157. (DURATION_DIFF(duration, subghz_protocol_scher_khan_const.te_short) <
  158. subghz_protocol_scher_khan_const.te_delta)) {
  159. subghz_protocol_blocks_add_bit(&instance->decoder, 0);
  160. instance->decoder.parser_step = ScherKhanDecoderStepSaveDuration;
  161. } else if(
  162. (DURATION_DIFF(
  163. instance->decoder.te_last, subghz_protocol_scher_khan_const.te_long) <
  164. subghz_protocol_scher_khan_const.te_delta) &&
  165. (DURATION_DIFF(duration, subghz_protocol_scher_khan_const.te_long) <
  166. subghz_protocol_scher_khan_const.te_delta)) {
  167. subghz_protocol_blocks_add_bit(&instance->decoder, 1);
  168. instance->decoder.parser_step = ScherKhanDecoderStepSaveDuration;
  169. } else {
  170. instance->decoder.parser_step = ScherKhanDecoderStepReset;
  171. }
  172. } else {
  173. instance->decoder.parser_step = ScherKhanDecoderStepReset;
  174. }
  175. break;
  176. }
  177. }
  178. /**
  179. * Analysis of received data
  180. * @param instance Pointer to a SubGhzBlockGeneric* instance
  181. * @param protocol_name
  182. */
  183. static void subghz_protocol_scher_khan_check_remote_controller(
  184. SubGhzBlockGeneric* instance,
  185. const char** protocol_name) {
  186. /*
  187. * MAGICAR 51 bit 00000001A99121DE83C3 MAGIC CODE, Dinamic
  188. * 0E8C1619E830C -> 000011101000110000010110 0001 1001 1110 1000001100001100
  189. * 0E8C1629D830D -> 000011101000110000010110 0010 1001 1101 1000001100001101
  190. * 0E8C1649B830E -> 000011101000110000010110 0100 1001 1011 1000001100001110
  191. * 0E8C16897830F -> 000011101000110000010110 1000 1001 0111 1000001100001111
  192. * Serial Key Ser ~Key CNT
  193. */
  194. switch(instance->data_count_bit) {
  195. // case 35: //MAGIC CODE, Static
  196. // instance->protocol_name = "MAGIC CODE, Static";
  197. // break;
  198. case 51: //MAGIC CODE, Dinamic
  199. *protocol_name = "MAGIC CODE, Dinamic";
  200. instance->serial = ((instance->data >> 24) & 0xFFFFFF0) | ((instance->data >> 20) & 0x0F);
  201. instance->btn = (instance->data >> 24) & 0x0F;
  202. instance->cnt = instance->data & 0xFFFF;
  203. break;
  204. // case 57: //MAGIC CODE PRO / PRO2
  205. // instance->protocol_name = "MAGIC CODE PRO / PRO2";
  206. // break;
  207. default:
  208. instance->protocol_name = "Unknown";
  209. instance->serial = 0;
  210. instance->btn = 0;
  211. instance->cnt = 0;
  212. break;
  213. }
  214. }
  215. uint8_t subghz_protocol_decoder_scher_khan_get_hash_data(void* context) {
  216. furi_assert(context);
  217. SubGhzProtocolDecoderScherKhan* instance = context;
  218. return subghz_protocol_blocks_get_hash_data(
  219. &instance->decoder, (instance->decoder.decode_count_bit / 8) + 1);
  220. }
  221. bool subghz_protocol_decoder_scher_khan_serialize(
  222. void* context,
  223. FlipperFormat* flipper_format,
  224. uint32_t frequency,
  225. FuriHalSubGhzPreset preset) {
  226. furi_assert(context);
  227. SubGhzProtocolDecoderScherKhan* instance = context;
  228. return subghz_block_generic_serialize(&instance->generic, flipper_format, frequency, preset);
  229. }
  230. bool subghz_protocol_decoder_scher_khan_deserialize(void* context, FlipperFormat* flipper_format) {
  231. furi_assert(context);
  232. SubGhzProtocolDecoderScherKhan* instance = context;
  233. return subghz_block_generic_deserialize(&instance->generic, flipper_format);
  234. }
  235. void subghz_protocol_decoder_scher_khan_get_string(void* context, string_t output) {
  236. furi_assert(context);
  237. SubGhzProtocolDecoderScherKhan* instance = context;
  238. subghz_protocol_scher_khan_check_remote_controller(
  239. &instance->generic, &instance->protocol_name);
  240. string_cat_printf(
  241. output,
  242. "%s %dbit\r\n"
  243. "Key:0x%lX%08lX\r\n"
  244. "Sn:%07lX Btn:%lX Cnt:%04X\r\n"
  245. "Pt: %s\r\n",
  246. instance->generic.protocol_name,
  247. instance->generic.data_count_bit,
  248. (uint32_t)(instance->generic.data >> 32),
  249. (uint32_t)instance->generic.data,
  250. instance->generic.serial,
  251. instance->generic.btn,
  252. instance->generic.cnt,
  253. instance->protocol_name);
  254. }