secplus_v1.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370
  1. #include "secplus_v1.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. /*
  8. * Help
  9. * https://github.com/argilo/secplus
  10. * https://github.com/merbanan/rtl_433/blob/master/src/devices/secplus_v1.c
  11. */
  12. #define TAG "SubGhzProtocoSecPlus_v1"
  13. #define SECPLUS_V1_BIT_ERR -1 //0b0000
  14. #define SECPLUS_V1_BIT_0 0 //0b0001
  15. #define SECPLUS_V1_BIT_1 1 //0b0011
  16. #define SECPLUS_V1_BIT_2 2 //0b0111
  17. #define SECPLUS_V1_PACKET_1_HEADER 0x00
  18. #define SECPLUS_V1_PACKET_2_HEADER 0x02
  19. #define SECPLUS_V1_PACKET_1_INDEX_BASE 0
  20. #define SECPLUS_V1_PACKET_2_INDEX_BASE 21
  21. #define SECPLUS_V1_PACKET_1_ACCEPTED (1 << 0)
  22. #define SECPLUS_V1_PACKET_2_ACCEPTED (1 << 1)
  23. static const SubGhzBlockConst subghz_protocol_secplus_v1_const = {
  24. .te_short = 500,
  25. .te_long = 1500,
  26. .te_delta = 100,
  27. .min_count_bit_for_found = 21,
  28. };
  29. struct SubGhzProtocolDecoderSecPlus_v1 {
  30. SubGhzProtocolDecoderBase base;
  31. SubGhzBlockDecoder decoder;
  32. SubGhzBlockGeneric generic;
  33. uint8_t packet_accepted;
  34. uint8_t base_packet_index;
  35. uint8_t data_array[44];
  36. };
  37. struct SubGhzProtocolEncoderSecPlus_v1 {
  38. SubGhzProtocolEncoderBase base;
  39. SubGhzProtocolBlockEncoder encoder;
  40. SubGhzBlockGeneric generic;
  41. };
  42. typedef enum {
  43. SecPlus_v1DecoderStepReset = 0,
  44. SecPlus_v1DecoderStepSearchStartBit,
  45. SecPlus_v1DecoderStepSaveDuration,
  46. SecPlus_v1DecoderStepDecoderData,
  47. } SecPlus_v1DecoderStep;
  48. const SubGhzProtocolDecoder subghz_protocol_secplus_v1_decoder = {
  49. .alloc = subghz_protocol_decoder_secplus_v1_alloc,
  50. .free = subghz_protocol_decoder_secplus_v1_free,
  51. .feed = subghz_protocol_decoder_secplus_v1_feed,
  52. .reset = subghz_protocol_decoder_secplus_v1_reset,
  53. .get_hash_data = subghz_protocol_decoder_secplus_v1_get_hash_data,
  54. .serialize = subghz_protocol_decoder_secplus_v1_serialize,
  55. .deserialize = subghz_protocol_decoder_secplus_v1_deserialize,
  56. .get_string = subghz_protocol_decoder_secplus_v1_get_string,
  57. };
  58. const SubGhzProtocolEncoder subghz_protocol_secplus_v1_encoder = {
  59. .alloc = NULL,
  60. .free = NULL,
  61. .deserialize = NULL,
  62. .stop = NULL,
  63. .yield = NULL,
  64. };
  65. const SubGhzProtocol subghz_protocol_secplus_v1 = {
  66. .name = SUBGHZ_PROTOCOL_SECPLUS_V1_NAME,
  67. .type = SubGhzProtocolTypeDynamic,
  68. .flag = SubGhzProtocolFlag_315 | SubGhzProtocolFlag_AM | SubGhzProtocolFlag_Decodable,
  69. .decoder = &subghz_protocol_secplus_v1_decoder,
  70. .encoder = &subghz_protocol_secplus_v1_encoder,
  71. };
  72. void* subghz_protocol_decoder_secplus_v1_alloc(SubGhzEnvironment* environment) {
  73. UNUSED(environment);
  74. SubGhzProtocolDecoderSecPlus_v1* instance = malloc(sizeof(SubGhzProtocolDecoderSecPlus_v1));
  75. instance->base.protocol = &subghz_protocol_secplus_v1;
  76. instance->generic.protocol_name = instance->base.protocol->name;
  77. return instance;
  78. }
  79. void subghz_protocol_decoder_secplus_v1_free(void* context) {
  80. furi_assert(context);
  81. SubGhzProtocolDecoderSecPlus_v1* instance = context;
  82. free(instance);
  83. }
  84. void subghz_protocol_decoder_secplus_v1_reset(void* context) {
  85. furi_assert(context);
  86. // SubGhzProtocolDecoderSecPlus_v1* instance = context;
  87. // does not reset the decoder because you need to get 2 parts of the package
  88. }
  89. /**
  90. * Security+ 1.0 half-message decoding
  91. * @param instance SubGhzProtocolDecoderSecPlus_v1*
  92. */
  93. static void subghz_protocol_secplus_v1_decode(SubGhzProtocolDecoderSecPlus_v1* instance) {
  94. uint32_t rolling = 0;
  95. uint32_t fixed = 0;
  96. uint32_t acc = 0;
  97. uint8_t digit = 0;
  98. //decode packet 1
  99. for(uint8_t i = 1; i < 21; i += 2) {
  100. digit = instance->data_array[i];
  101. rolling = (rolling * 3) + digit;
  102. acc += digit;
  103. digit = (60 + instance->data_array[i + 1] - acc) % 3;
  104. fixed = (fixed * 3) + digit;
  105. acc += digit;
  106. }
  107. acc = 0;
  108. //decode packet 2
  109. for(uint8_t i = 22; i < 42; i += 2) {
  110. digit = instance->data_array[i];
  111. rolling = (rolling * 3) + digit;
  112. acc += digit;
  113. digit = (60 + instance->data_array[i + 1] - acc) % 3;
  114. fixed = (fixed * 3) + digit;
  115. acc += digit;
  116. }
  117. rolling = subghz_protocol_blocks_reverse_key(rolling, 32);
  118. instance->generic.data = (uint64_t)fixed << 32 | rolling;
  119. instance->generic.data_count_bit =
  120. subghz_protocol_secplus_v1_const.min_count_bit_for_found * 2;
  121. }
  122. void subghz_protocol_decoder_secplus_v1_feed(void* context, bool level, uint32_t duration) {
  123. furi_assert(context);
  124. SubGhzProtocolDecoderSecPlus_v1* instance = context;
  125. switch(instance->decoder.parser_step) {
  126. case SecPlus_v1DecoderStepReset:
  127. if((!level) && (DURATION_DIFF(duration, subghz_protocol_secplus_v1_const.te_short * 120) <
  128. subghz_protocol_secplus_v1_const.te_delta * 120)) {
  129. //Found header Security+ 1.0
  130. instance->decoder.parser_step = SecPlus_v1DecoderStepSearchStartBit;
  131. instance->decoder.decode_data = 0;
  132. instance->decoder.decode_count_bit = 0;
  133. instance->packet_accepted = 0;
  134. memset(instance->data_array, 0, sizeof(instance->data_array));
  135. }
  136. break;
  137. case SecPlus_v1DecoderStepSearchStartBit:
  138. if(level) {
  139. if(DURATION_DIFF(duration, subghz_protocol_secplus_v1_const.te_short) <
  140. subghz_protocol_secplus_v1_const.te_delta) {
  141. instance->base_packet_index = SECPLUS_V1_PACKET_1_INDEX_BASE;
  142. instance
  143. ->data_array[instance->decoder.decode_count_bit + instance->base_packet_index] =
  144. SECPLUS_V1_BIT_0;
  145. instance->decoder.decode_count_bit++;
  146. instance->decoder.parser_step = SecPlus_v1DecoderStepSaveDuration;
  147. } else if(
  148. DURATION_DIFF(duration, subghz_protocol_secplus_v1_const.te_long) <
  149. subghz_protocol_secplus_v1_const.te_delta) {
  150. instance->base_packet_index = SECPLUS_V1_PACKET_2_INDEX_BASE;
  151. instance
  152. ->data_array[instance->decoder.decode_count_bit + instance->base_packet_index] =
  153. SECPLUS_V1_BIT_2;
  154. instance->decoder.decode_count_bit++;
  155. instance->decoder.parser_step = SecPlus_v1DecoderStepSaveDuration;
  156. } else {
  157. instance->decoder.parser_step = SecPlus_v1DecoderStepReset;
  158. }
  159. } else {
  160. instance->decoder.parser_step = SecPlus_v1DecoderStepReset;
  161. }
  162. break;
  163. case SecPlus_v1DecoderStepSaveDuration:
  164. if(!level) { //save interval
  165. if(DURATION_DIFF(duration, subghz_protocol_secplus_v1_const.te_short * 120) <
  166. subghz_protocol_secplus_v1_const.te_delta * 120) {
  167. if(instance->decoder.decode_count_bit ==
  168. subghz_protocol_secplus_v1_const.min_count_bit_for_found) {
  169. if(instance->base_packet_index == SECPLUS_V1_PACKET_1_INDEX_BASE)
  170. instance->packet_accepted |= SECPLUS_V1_PACKET_1_ACCEPTED;
  171. if(instance->base_packet_index == SECPLUS_V1_PACKET_2_INDEX_BASE)
  172. instance->packet_accepted |= SECPLUS_V1_PACKET_2_ACCEPTED;
  173. if(instance->packet_accepted ==
  174. (SECPLUS_V1_PACKET_1_ACCEPTED | SECPLUS_V1_PACKET_2_ACCEPTED)) {
  175. subghz_protocol_secplus_v1_decode(instance);
  176. if(instance->base.callback)
  177. instance->base.callback(&instance->base, instance->base.context);
  178. instance->decoder.parser_step = SecPlus_v1DecoderStepReset;
  179. }
  180. }
  181. instance->decoder.parser_step = SecPlus_v1DecoderStepSearchStartBit;
  182. instance->decoder.decode_data = 0;
  183. instance->decoder.decode_count_bit = 0;
  184. } else {
  185. instance->decoder.te_last = duration;
  186. instance->decoder.parser_step = SecPlus_v1DecoderStepDecoderData;
  187. }
  188. } else {
  189. instance->decoder.parser_step = SecPlus_v1DecoderStepReset;
  190. }
  191. break;
  192. case SecPlus_v1DecoderStepDecoderData:
  193. if(level && (instance->decoder.decode_count_bit <=
  194. subghz_protocol_secplus_v1_const.min_count_bit_for_found)) {
  195. if((DURATION_DIFF(
  196. instance->decoder.te_last, subghz_protocol_secplus_v1_const.te_short * 3) <
  197. subghz_protocol_secplus_v1_const.te_delta * 3) &&
  198. (DURATION_DIFF(duration, subghz_protocol_secplus_v1_const.te_short) <
  199. subghz_protocol_secplus_v1_const.te_delta)) {
  200. instance
  201. ->data_array[instance->decoder.decode_count_bit + instance->base_packet_index] =
  202. SECPLUS_V1_BIT_0;
  203. instance->decoder.decode_count_bit++;
  204. instance->decoder.parser_step = SecPlus_v1DecoderStepSaveDuration;
  205. } else if(
  206. (DURATION_DIFF(
  207. instance->decoder.te_last, subghz_protocol_secplus_v1_const.te_short * 2) <
  208. subghz_protocol_secplus_v1_const.te_delta * 2) &&
  209. (DURATION_DIFF(duration, subghz_protocol_secplus_v1_const.te_short * 2) <
  210. subghz_protocol_secplus_v1_const.te_delta * 2)) {
  211. instance
  212. ->data_array[instance->decoder.decode_count_bit + instance->base_packet_index] =
  213. SECPLUS_V1_BIT_1;
  214. instance->decoder.decode_count_bit++;
  215. instance->decoder.parser_step = SecPlus_v1DecoderStepSaveDuration;
  216. } else if(
  217. (DURATION_DIFF(
  218. instance->decoder.te_last, subghz_protocol_secplus_v1_const.te_short) <
  219. subghz_protocol_secplus_v1_const.te_delta) &&
  220. (DURATION_DIFF(duration, subghz_protocol_secplus_v1_const.te_short * 3) <
  221. subghz_protocol_secplus_v1_const.te_delta * 3)) {
  222. instance
  223. ->data_array[instance->decoder.decode_count_bit + instance->base_packet_index] =
  224. SECPLUS_V1_BIT_2;
  225. instance->decoder.decode_count_bit++;
  226. instance->decoder.parser_step = SecPlus_v1DecoderStepSaveDuration;
  227. } else {
  228. instance->decoder.parser_step = SecPlus_v1DecoderStepReset;
  229. }
  230. } else {
  231. instance->decoder.parser_step = SecPlus_v1DecoderStepReset;
  232. }
  233. break;
  234. }
  235. }
  236. uint8_t subghz_protocol_decoder_secplus_v1_get_hash_data(void* context) {
  237. furi_assert(context);
  238. SubGhzProtocolDecoderSecPlus_v1* instance = context;
  239. return subghz_protocol_blocks_get_hash_data(
  240. &instance->decoder, (instance->decoder.decode_count_bit / 8) + 1);
  241. }
  242. bool subghz_protocol_decoder_secplus_v1_serialize(
  243. void* context,
  244. FlipperFormat* flipper_format,
  245. uint32_t frequency,
  246. FuriHalSubGhzPreset preset) {
  247. furi_assert(context);
  248. SubGhzProtocolDecoderSecPlus_v1* instance = context;
  249. return subghz_block_generic_serialize(&instance->generic, flipper_format, frequency, preset);
  250. }
  251. bool subghz_protocol_decoder_secplus_v1_deserialize(void* context, FlipperFormat* flipper_format) {
  252. furi_assert(context);
  253. SubGhzProtocolDecoderSecPlus_v1* instance = context;
  254. return subghz_block_generic_deserialize(&instance->generic, flipper_format);
  255. }
  256. void subghz_protocol_decoder_secplus_v1_get_string(void* context, string_t output) {
  257. furi_assert(context);
  258. SubGhzProtocolDecoderSecPlus_v1* instance = context;
  259. uint32_t fixed = (instance->generic.data >> 32) & 0xFFFFFFFF;
  260. instance->generic.cnt = instance->generic.data & 0xFFFFFFFF;
  261. instance->generic.btn = fixed % 3;
  262. uint8_t id0 = (fixed / 3) % 3;
  263. uint8_t id1 = (fixed / 9) % 3;
  264. uint16_t pin = 0;
  265. string_cat_printf(
  266. output,
  267. "%s %db\r\n"
  268. "Key:0x%lX%08lX\r\n"
  269. "id1:%d id0:%d",
  270. instance->generic.protocol_name,
  271. instance->generic.data_count_bit,
  272. (uint32_t)(instance->generic.data >> 32),
  273. (uint32_t)instance->generic.data,
  274. id1,
  275. id0);
  276. if(id1 == 0) {
  277. // (fixed // 3**3) % (3**7) 3^3=27 3^73=72187
  278. instance->generic.serial = (fixed / 27) % 2187;
  279. // pin = (fixed // 3**10) % (3**9) 3^10=59049 3^9=19683
  280. pin = (fixed / 59049) % 19683;
  281. if(pin <= 9999) {
  282. string_cat_printf(output, " pin:%d", pin);
  283. } else if(10000 <= pin && pin <= 11029) {
  284. string_cat_printf(output, " pin:enter");
  285. }
  286. int pin_suffix = 0;
  287. // pin_suffix = (fixed // 3**19) % 3 3^19=1162261467
  288. pin_suffix = (fixed / 1162261467) % 3;
  289. if(pin_suffix == 1) {
  290. string_cat_printf(output, " #\r\n");
  291. } else if(pin_suffix == 2) {
  292. string_cat_printf(output, " *\r\n");
  293. } else {
  294. string_cat_printf(output, "\r\n");
  295. }
  296. string_cat_printf(
  297. output,
  298. "Sn:0x%08lX\r\n"
  299. "Cnt:0x%03X\r\n"
  300. "Sw_id:0x%X\r\n",
  301. instance->generic.serial,
  302. instance->generic.cnt,
  303. instance->generic.btn);
  304. } else {
  305. //id = fixed / 27;
  306. instance->generic.serial = fixed / 27;
  307. if(instance->generic.btn == 1) {
  308. string_cat_printf(output, " Btn:left\r\n");
  309. } else if(instance->generic.btn == 0) {
  310. string_cat_printf(output, " Btn:middle\r\n");
  311. } else if(instance->generic.btn == 2) {
  312. string_cat_printf(output, " Btn:right\r\n");
  313. }
  314. string_cat_printf(
  315. output,
  316. "Sn:0x%08lX\r\n"
  317. "Cnt:0x%03X\r\n"
  318. "Sw_id:0x%X\r\n",
  319. instance->generic.serial,
  320. instance->generic.cnt,
  321. instance->generic.btn);
  322. }
  323. }