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. UNUSED(environment);
  62. SubGhzProtocolDecoderScherKhan* instance = malloc(sizeof(SubGhzProtocolDecoderScherKhan));
  63. instance->base.protocol = &subghz_protocol_scher_khan;
  64. instance->generic.protocol_name = instance->base.protocol->name;
  65. return instance;
  66. }
  67. void subghz_protocol_decoder_scher_khan_free(void* context) {
  68. furi_assert(context);
  69. SubGhzProtocolDecoderScherKhan* instance = context;
  70. free(instance);
  71. }
  72. void subghz_protocol_decoder_scher_khan_reset(void* context) {
  73. furi_assert(context);
  74. SubGhzProtocolDecoderScherKhan* instance = context;
  75. instance->decoder.parser_step = ScherKhanDecoderStepReset;
  76. }
  77. void subghz_protocol_decoder_scher_khan_feed(void* context, bool level, uint32_t duration) {
  78. furi_assert(context);
  79. SubGhzProtocolDecoderScherKhan* instance = context;
  80. switch(instance->decoder.parser_step) {
  81. case ScherKhanDecoderStepReset:
  82. if((level) && (DURATION_DIFF(duration, subghz_protocol_scher_khan_const.te_short * 2) <
  83. subghz_protocol_scher_khan_const.te_delta)) {
  84. instance->decoder.parser_step = ScherKhanDecoderStepCheckPreambula;
  85. instance->decoder.te_last = duration;
  86. instance->header_count = 0;
  87. }
  88. break;
  89. case ScherKhanDecoderStepCheckPreambula:
  90. if(level) {
  91. if((DURATION_DIFF(duration, subghz_protocol_scher_khan_const.te_short * 2) <
  92. subghz_protocol_scher_khan_const.te_delta) ||
  93. (DURATION_DIFF(duration, subghz_protocol_scher_khan_const.te_short) <
  94. subghz_protocol_scher_khan_const.te_delta)) {
  95. instance->decoder.te_last = duration;
  96. } else {
  97. instance->decoder.parser_step = ScherKhanDecoderStepReset;
  98. }
  99. } else if(
  100. (DURATION_DIFF(duration, subghz_protocol_scher_khan_const.te_short * 2) <
  101. subghz_protocol_scher_khan_const.te_delta) ||
  102. (DURATION_DIFF(duration, subghz_protocol_scher_khan_const.te_short) <
  103. subghz_protocol_scher_khan_const.te_delta)) {
  104. if(DURATION_DIFF(
  105. instance->decoder.te_last, subghz_protocol_scher_khan_const.te_short * 2) <
  106. subghz_protocol_scher_khan_const.te_delta) {
  107. // Found header
  108. instance->header_count++;
  109. break;
  110. } else if(
  111. DURATION_DIFF(
  112. instance->decoder.te_last, subghz_protocol_scher_khan_const.te_short) <
  113. subghz_protocol_scher_khan_const.te_delta) {
  114. // Found start bit
  115. if(instance->header_count >= 2) {
  116. instance->decoder.parser_step = ScherKhanDecoderStepSaveDuration;
  117. instance->decoder.decode_data = 0;
  118. instance->decoder.decode_count_bit = 1;
  119. } else {
  120. instance->decoder.parser_step = ScherKhanDecoderStepReset;
  121. }
  122. } else {
  123. instance->decoder.parser_step = ScherKhanDecoderStepReset;
  124. }
  125. } else {
  126. instance->decoder.parser_step = ScherKhanDecoderStepReset;
  127. }
  128. break;
  129. case ScherKhanDecoderStepSaveDuration:
  130. if(level) {
  131. if(duration >= (subghz_protocol_scher_khan_const.te_delta * 2UL +
  132. subghz_protocol_scher_khan_const.te_long)) {
  133. //Found stop bit
  134. instance->decoder.parser_step = ScherKhanDecoderStepReset;
  135. if(instance->decoder.decode_count_bit >=
  136. subghz_protocol_scher_khan_const.min_count_bit_for_found) {
  137. instance->generic.data = instance->decoder.decode_data;
  138. instance->generic.data_count_bit = instance->decoder.decode_count_bit;
  139. if(instance->base.callback)
  140. instance->base.callback(&instance->base, instance->base.context);
  141. }
  142. instance->decoder.decode_data = 0;
  143. instance->decoder.decode_count_bit = 0;
  144. break;
  145. } else {
  146. instance->decoder.te_last = duration;
  147. instance->decoder.parser_step = ScherKhanDecoderStepCheckDuration;
  148. }
  149. } else {
  150. instance->decoder.parser_step = ScherKhanDecoderStepReset;
  151. }
  152. break;
  153. case ScherKhanDecoderStepCheckDuration:
  154. if(!level) {
  155. if((DURATION_DIFF(
  156. instance->decoder.te_last, subghz_protocol_scher_khan_const.te_short) <
  157. subghz_protocol_scher_khan_const.te_delta) &&
  158. (DURATION_DIFF(duration, subghz_protocol_scher_khan_const.te_short) <
  159. subghz_protocol_scher_khan_const.te_delta)) {
  160. subghz_protocol_blocks_add_bit(&instance->decoder, 0);
  161. instance->decoder.parser_step = ScherKhanDecoderStepSaveDuration;
  162. } else if(
  163. (DURATION_DIFF(
  164. instance->decoder.te_last, subghz_protocol_scher_khan_const.te_long) <
  165. subghz_protocol_scher_khan_const.te_delta) &&
  166. (DURATION_DIFF(duration, subghz_protocol_scher_khan_const.te_long) <
  167. subghz_protocol_scher_khan_const.te_delta)) {
  168. subghz_protocol_blocks_add_bit(&instance->decoder, 1);
  169. instance->decoder.parser_step = ScherKhanDecoderStepSaveDuration;
  170. } else {
  171. instance->decoder.parser_step = ScherKhanDecoderStepReset;
  172. }
  173. } else {
  174. instance->decoder.parser_step = ScherKhanDecoderStepReset;
  175. }
  176. break;
  177. }
  178. }
  179. /**
  180. * Analysis of received data
  181. * @param instance Pointer to a SubGhzBlockGeneric* instance
  182. * @param protocol_name
  183. */
  184. static void subghz_protocol_scher_khan_check_remote_controller(
  185. SubGhzBlockGeneric* instance,
  186. const char** protocol_name) {
  187. /*
  188. * MAGICAR 51 bit 00000001A99121DE83C3 MAGIC CODE, Dynamic
  189. * 0E8C1619E830C -> 000011101000110000010110 0001 1001 1110 1000001100001100
  190. * 0E8C1629D830D -> 000011101000110000010110 0010 1001 1101 1000001100001101
  191. * 0E8C1649B830E -> 000011101000110000010110 0100 1001 1011 1000001100001110
  192. * 0E8C16897830F -> 000011101000110000010110 1000 1001 0111 1000001100001111
  193. * Serial Key Ser ~Key CNT
  194. */
  195. switch(instance->data_count_bit) {
  196. // case 35: //MAGIC CODE, Static
  197. // instance->protocol_name = "MAGIC CODE, Static";
  198. // break;
  199. case 51: //MAGIC CODE, Dynamic
  200. *protocol_name = "MAGIC CODE, Dynamic";
  201. instance->serial = ((instance->data >> 24) & 0xFFFFFF0) | ((instance->data >> 20) & 0x0F);
  202. instance->btn = (instance->data >> 24) & 0x0F;
  203. instance->cnt = instance->data & 0xFFFF;
  204. break;
  205. // case 57: //MAGIC CODE PRO / PRO2
  206. // instance->protocol_name = "MAGIC CODE PRO / PRO2";
  207. // break;
  208. default:
  209. *protocol_name = "Unknown";
  210. instance->serial = 0;
  211. instance->btn = 0;
  212. instance->cnt = 0;
  213. break;
  214. }
  215. }
  216. uint8_t subghz_protocol_decoder_scher_khan_get_hash_data(void* context) {
  217. furi_assert(context);
  218. SubGhzProtocolDecoderScherKhan* instance = context;
  219. return subghz_protocol_blocks_get_hash_data(
  220. &instance->decoder, (instance->decoder.decode_count_bit / 8) + 1);
  221. }
  222. bool subghz_protocol_decoder_scher_khan_serialize(
  223. void* context,
  224. FlipperFormat* flipper_format,
  225. SubGhzRadioPreset* preset) {
  226. furi_assert(context);
  227. SubGhzProtocolDecoderScherKhan* instance = context;
  228. return subghz_block_generic_serialize(&instance->generic, flipper_format, 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, FuriString* output) {
  236. furi_assert(context);
  237. SubGhzProtocolDecoderScherKhan* instance = context;
  238. subghz_protocol_scher_khan_check_remote_controller(
  239. &instance->generic, &instance->protocol_name);
  240. furi_string_cat_printf(
  241. output,
  242. "%s %dbit\r\n"
  243. "Key:0x%lX%08lX\r\n"
  244. "Sn:%07lX Btn:%X Cnt:%04lX\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. }