somfy_keytis.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447
  1. #include "somfy_keytis.h"
  2. #include <lib/toolbox/manchester_decoder.h>
  3. #include "../blocks/const.h"
  4. #include "../blocks/decoder.h"
  5. #include "../blocks/encoder.h"
  6. #include "../blocks/generic.h"
  7. #include "../blocks/math.h"
  8. #define TAG "SubGhzProtocolSomfyKeytis"
  9. static const SubGhzBlockConst subghz_protocol_somfy_keytis_const = {
  10. .te_short = 640,
  11. .te_long = 1280,
  12. .te_delta = 250,
  13. .min_count_bit_for_found = 80,
  14. };
  15. struct SubGhzProtocolDecoderSomfyKeytis {
  16. SubGhzProtocolDecoderBase base;
  17. SubGhzBlockDecoder decoder;
  18. SubGhzBlockGeneric generic;
  19. uint16_t header_count;
  20. ManchesterState manchester_saved_state;
  21. uint32_t press_duration_counter;
  22. };
  23. struct SubGhzProtocolEncoderSomfyKeytis {
  24. SubGhzProtocolEncoderBase base;
  25. SubGhzProtocolBlockEncoder encoder;
  26. SubGhzBlockGeneric generic;
  27. };
  28. typedef enum {
  29. SomfyKeytisDecoderStepReset = 0,
  30. SomfyKeytisDecoderStepCheckPreambula,
  31. SomfyKeytisDecoderStepFoundPreambula,
  32. SomfyKeytisDecoderStepStartDecode,
  33. SomfyKeytisDecoderStepDecoderData,
  34. } SomfyKeytisDecoderStep;
  35. const SubGhzProtocolDecoder subghz_protocol_somfy_keytis_decoder = {
  36. .alloc = subghz_protocol_decoder_somfy_keytis_alloc,
  37. .free = subghz_protocol_decoder_somfy_keytis_free,
  38. .feed = subghz_protocol_decoder_somfy_keytis_feed,
  39. .reset = subghz_protocol_decoder_somfy_keytis_reset,
  40. .get_hash_data = subghz_protocol_decoder_somfy_keytis_get_hash_data,
  41. .serialize = subghz_protocol_decoder_somfy_keytis_serialize,
  42. .deserialize = subghz_protocol_decoder_somfy_keytis_deserialize,
  43. .get_string = subghz_protocol_decoder_somfy_keytis_get_string,
  44. };
  45. const SubGhzProtocolEncoder subghz_protocol_somfy_keytis_encoder = {
  46. .alloc = NULL,
  47. .free = NULL,
  48. .deserialize = NULL,
  49. .stop = NULL,
  50. .yield = NULL,
  51. };
  52. const SubGhzProtocol subghz_protocol_somfy_keytis = {
  53. .name = SUBGHZ_PROTOCOL_SOMFY_KEYTIS_NAME,
  54. .type = SubGhzProtocolTypeDynamic,
  55. .flag = SubGhzProtocolFlag_433 | SubGhzProtocolFlag_868 | SubGhzProtocolFlag_AM |
  56. SubGhzProtocolFlag_Decodable,
  57. .decoder = &subghz_protocol_somfy_keytis_decoder,
  58. .encoder = &subghz_protocol_somfy_keytis_encoder,
  59. };
  60. void* subghz_protocol_decoder_somfy_keytis_alloc(SubGhzEnvironment* environment) {
  61. SubGhzProtocolDecoderSomfyKeytis* instance = malloc(sizeof(SubGhzProtocolDecoderSomfyKeytis));
  62. instance->base.protocol = &subghz_protocol_somfy_keytis;
  63. instance->generic.protocol_name = instance->base.protocol->name;
  64. return instance;
  65. }
  66. void subghz_protocol_decoder_somfy_keytis_free(void* context) {
  67. furi_assert(context);
  68. SubGhzProtocolDecoderSomfyKeytis* instance = context;
  69. free(instance);
  70. }
  71. void subghz_protocol_decoder_somfy_keytis_reset(void* context) {
  72. furi_assert(context);
  73. SubGhzProtocolDecoderSomfyKeytis* instance = context;
  74. instance->decoder.parser_step = SomfyKeytisDecoderStepReset;
  75. manchester_advance(
  76. instance->manchester_saved_state,
  77. ManchesterEventReset,
  78. &instance->manchester_saved_state,
  79. NULL);
  80. }
  81. /**
  82. * Сhecksum calculation.
  83. * @param data Вata for checksum calculation
  84. * @return CRC
  85. */
  86. static uint8_t subghz_protocol_somfy_keytis_crc(uint64_t data) {
  87. uint8_t crc = 0;
  88. data &= 0xFFF0FFFFFFFFFF;
  89. for(uint8_t i = 0; i < 56; i += 8) {
  90. crc = crc ^ data >> i ^ (data >> (i + 4));
  91. }
  92. return crc & 0xf;
  93. }
  94. void subghz_protocol_decoder_somfy_keytis_feed(void* context, bool level, uint32_t duration) {
  95. furi_assert(context);
  96. SubGhzProtocolDecoderSomfyKeytis* instance = context;
  97. ManchesterEvent event = ManchesterEventReset;
  98. switch(instance->decoder.parser_step) {
  99. case SomfyKeytisDecoderStepReset:
  100. if((level) && DURATION_DIFF(duration, subghz_protocol_somfy_keytis_const.te_short * 4) <
  101. subghz_protocol_somfy_keytis_const.te_delta * 4) {
  102. instance->decoder.parser_step = SomfyKeytisDecoderStepFoundPreambula;
  103. instance->header_count++;
  104. }
  105. break;
  106. case SomfyKeytisDecoderStepFoundPreambula:
  107. if((!level) && (DURATION_DIFF(duration, subghz_protocol_somfy_keytis_const.te_short * 4) <
  108. subghz_protocol_somfy_keytis_const.te_delta * 4)) {
  109. instance->decoder.parser_step = SomfyKeytisDecoderStepCheckPreambula;
  110. } else {
  111. instance->header_count = 0;
  112. instance->decoder.parser_step = SomfyKeytisDecoderStepReset;
  113. }
  114. break;
  115. case SomfyKeytisDecoderStepCheckPreambula:
  116. if(level) {
  117. if(DURATION_DIFF(duration, subghz_protocol_somfy_keytis_const.te_short * 4) <
  118. subghz_protocol_somfy_keytis_const.te_delta * 4) {
  119. instance->decoder.parser_step = SomfyKeytisDecoderStepFoundPreambula;
  120. instance->header_count++;
  121. } else if(
  122. (instance->header_count > 1) &&
  123. (DURATION_DIFF(duration, subghz_protocol_somfy_keytis_const.te_short * 7) <
  124. subghz_protocol_somfy_keytis_const.te_delta * 4)) {
  125. instance->decoder.parser_step = SomfyKeytisDecoderStepDecoderData;
  126. instance->decoder.decode_data = 0;
  127. instance->decoder.decode_count_bit = 0;
  128. instance->press_duration_counter = 0;
  129. manchester_advance(
  130. instance->manchester_saved_state,
  131. ManchesterEventReset,
  132. &instance->manchester_saved_state,
  133. NULL);
  134. manchester_advance(
  135. instance->manchester_saved_state,
  136. ManchesterEventLongHigh,
  137. &instance->manchester_saved_state,
  138. NULL);
  139. }
  140. }
  141. break;
  142. case SomfyKeytisDecoderStepDecoderData:
  143. if(!level) {
  144. if(DURATION_DIFF(duration, subghz_protocol_somfy_keytis_const.te_short) <
  145. subghz_protocol_somfy_keytis_const.te_delta) {
  146. event = ManchesterEventShortLow;
  147. } else if(
  148. DURATION_DIFF(duration, subghz_protocol_somfy_keytis_const.te_long) <
  149. subghz_protocol_somfy_keytis_const.te_delta) {
  150. event = ManchesterEventLongLow;
  151. } else if(
  152. duration >= (subghz_protocol_somfy_keytis_const.te_long +
  153. subghz_protocol_somfy_keytis_const.te_delta)) {
  154. if(instance->decoder.decode_count_bit ==
  155. subghz_protocol_somfy_keytis_const.min_count_bit_for_found) {
  156. //check crc
  157. uint64_t data_tmp = instance->generic.data ^ (instance->generic.data >> 8);
  158. if(((data_tmp >> 40) & 0xF) == subghz_protocol_somfy_keytis_crc(data_tmp)) {
  159. instance->generic.data = instance->decoder.decode_data;
  160. instance->generic.data_count_bit = instance->decoder.decode_count_bit;
  161. if(instance->base.callback)
  162. instance->base.callback(&instance->base, instance->base.context);
  163. }
  164. }
  165. instance->decoder.decode_data = 0;
  166. instance->decoder.decode_count_bit = 0;
  167. manchester_advance(
  168. instance->manchester_saved_state,
  169. ManchesterEventReset,
  170. &instance->manchester_saved_state,
  171. NULL);
  172. manchester_advance(
  173. instance->manchester_saved_state,
  174. ManchesterEventLongHigh,
  175. &instance->manchester_saved_state,
  176. NULL);
  177. instance->decoder.parser_step = SomfyKeytisDecoderStepReset;
  178. } else {
  179. instance->decoder.parser_step = SomfyKeytisDecoderStepReset;
  180. }
  181. } else {
  182. if(DURATION_DIFF(duration, subghz_protocol_somfy_keytis_const.te_short) <
  183. subghz_protocol_somfy_keytis_const.te_delta) {
  184. event = ManchesterEventShortHigh;
  185. } else if(
  186. DURATION_DIFF(duration, subghz_protocol_somfy_keytis_const.te_long) <
  187. subghz_protocol_somfy_keytis_const.te_delta) {
  188. event = ManchesterEventLongHigh;
  189. } else {
  190. instance->decoder.parser_step = SomfyKeytisDecoderStepReset;
  191. }
  192. }
  193. if(event != ManchesterEventReset) {
  194. bool data;
  195. bool data_ok = manchester_advance(
  196. instance->manchester_saved_state, event, &instance->manchester_saved_state, &data);
  197. if(data_ok) {
  198. if(instance->decoder.decode_count_bit < 56) {
  199. instance->decoder.decode_data = (instance->decoder.decode_data << 1) | data;
  200. } else {
  201. instance->press_duration_counter = (instance->press_duration_counter << 1) |
  202. data;
  203. }
  204. instance->decoder.decode_count_bit++;
  205. }
  206. }
  207. break;
  208. }
  209. }
  210. /**
  211. * Analysis of received data
  212. * @param instance Pointer to a SubGhzBlockGeneric* instance
  213. */
  214. static void subghz_protocol_somfy_keytis_check_remote_controller(SubGhzBlockGeneric* instance) {
  215. //https://pushstack.wordpress.com/somfy-rts-protocol/
  216. /*
  217. * 604 us
  218. * /
  219. * | 2416us | 2416us | 2416us | 2416us | 4550 us | |
  220. *
  221. * +--------+ +--------+ +---...---+
  222. * + +--------+ +--------+ +--+XXXX...XXX+
  223. *
  224. * | hw. sync. | soft. | |
  225. * | | sync. | data |
  226. *
  227. *
  228. * encrypt | decrypt
  229. *
  230. * package 80 bit pdc key btn crc cnt serial
  231. *
  232. * 0xA453537C4B9855 C40019 => 0xA 4 F 7 002F 37D3CD
  233. * 0xA453537C4B9855 C80026 => 0xA 4 F 7 002F 37D3CD
  234. * 0xA453537C4B9855 CC0033 => 0xA 4 F 7 002F 37D3CD
  235. * 0xA453537C4B9855 D00049 => 0xA 4 F 7 002F 37D3CD
  236. * 0xA453537C4B9855 D4005C => 0xA 4 F 7 002F 37D3CD
  237. * 0xA453537C4B9855 D80063 => 0xA 4 F 7 002F 37D3CD
  238. * 0xA453537C4B9855 DC0076 => 0xA 4 F 7 002F 37D3CD
  239. * 0xA453537C4B9855 E00086 => 0xA 4 F 7 002F 37D3CD
  240. * 0xA453537C4B9855 E40093 => 0xA 4 F 7 002F 37D3CD
  241. * 0xA453537C4B9855 E800AC => 0xA 4 F 7 002F 37D3CD
  242. * 0xA453537C4B9855 EC00B9 => 0xA 4 F 7 002F 37D3CD
  243. * 0xA453537C4B9855 F000C3 => 0xA 4 F 7 002F 37D3CD
  244. * 0xA453537C4B9855 F400D6 => 0xA 4 F 7 002F 37D3CD
  245. * 0xA453537C4B9855 F800E9 => 0xA 4 F 7 002F 37D3CD
  246. * 0xA453537C4B9855 FC00FC => 0xA 4 F 7 002F 37D3CD
  247. * 0xA453537C4B9855 FC0102 => 0xA 4 F 7 002F 37D3CD
  248. * 0xA453537C4B9855 FC0113 => 0xA 4 F 7 002F 37D3CD
  249. * 0xA453537C4B9855 FC0120 => 0xA 4 F 7 002F 37D3CD
  250. * ..........
  251. * 0xA453537C4B9855 FC048F => 0xA 4 F 7 002F 37D3CD
  252. *
  253. * Pdc: "Press Duration Counter" the total delay of the button is sent 72 parcels,
  254. * pdc cnt4b cnt8b pdc_crc
  255. * C40019 => 11 0001 00 0000 00000001 1001
  256. * C80026 => 11 0010 00 0000 00000010 0110
  257. * CC0033 => 11 0011 00 0000 00000011 0011
  258. * D00049 => 11 0100 00 0000 00000100 1001
  259. * D4005C => 11 0101 00 0000 00000101 1100
  260. * D80063 => 11 0110 00 0000 00000110 0011
  261. * DC0076 => 11 0111 00 0000 00000111 0110
  262. * E00086 => 11 1000 00 0000 00001000 0110
  263. * E40093 => 11 1001 00 0000 00001001 0011
  264. * E800AC => 11 1010 00 0000 00001010 1100
  265. * EC00B9 => 11 1011 00 0000 00001011 1001
  266. * F000C3 => 11 1100 00 0000 00001100 0011
  267. * F400D6 => 11 1101 00 0000 00001101 0110
  268. * F800E9 => 11 1110 00 0000 00001110 1001
  269. * FC00FC => 11 1111 00 0000 00001111 1100
  270. * FC0102 => 11 1111 00 0000 00010000 0010
  271. * FC0113 => 11 1111 00 0000 00010001 0011
  272. * FC0120 => 11 1111 00 0000 00010010 0000
  273. *
  274. * Cnt4b: 4-bit counter changes from 1 to 15 then always equals 15
  275. * Cnt8b: 8-bit counter changes from 1 to 72 (0x48)
  276. * Ppdc_crc:
  277. * uint8_t crc=0;
  278. * for(i=4; i<24; i+=4){
  279. * crc ^=(pdc>>i);
  280. * }
  281. * return crc;
  282. * example: crc = 1^0^0^4^C = 9
  283. * 11, 00, 0000: const
  284. *
  285. * Key: “Encryption Key”, Most significant 4-bit are always 0xA, Least Significant bits is
  286. * a linear counter. In the Smoove Origin this counter is increased together with the
  287. * rolling code. But leaving this on a constant value also works. Gerardwr notes that
  288. * for some other types of remotes the MSB is not constant.
  289. * Btn: 4-bit Control codes, this indicates the button that is pressed
  290. * CRC: 4-bit Checksum.
  291. * Ctn: 16-bit rolling code (big-endian) increased with every button press.
  292. * Serial: 24-bit identifier of sending device (little-endian)
  293. *
  294. *
  295. * Decrypt
  296. *
  297. * uint8_t frame[7];
  298. * for (i=1; i < 7; i++) {
  299. * frame[i] = frame[i] ^ frame[i-1];
  300. * }
  301. * or
  302. * uint64 Decrypt = frame ^ (frame>>8);
  303. *
  304. * CRC
  305. *
  306. * uint8_t frame[7];
  307. * for (i=0; i < 7; i++) {
  308. * crc = crc ^ frame[i] ^ (frame[i] >> 4);
  309. * }
  310. * crc = crc & 0xf;
  311. *
  312. */
  313. uint64_t data = instance->data ^ (instance->data >> 8);
  314. instance->btn = (data >> 48) & 0xF;
  315. instance->cnt = (data >> 24) & 0xFFFF;
  316. instance->serial = data & 0xFFFFFF;
  317. }
  318. /**
  319. * Get button name.
  320. * @param btn Button number, 4 bit
  321. */
  322. static const char* subghz_protocol_somfy_keytis_get_name_button(uint8_t btn) {
  323. const char* name_btn[0x10] = {
  324. "Unknown",
  325. "0x01",
  326. "0x02",
  327. "Prog",
  328. "Key_1",
  329. "0x05",
  330. "0x06",
  331. "0x07",
  332. "0x08",
  333. "0x09",
  334. "0x0A",
  335. "0x0B",
  336. "0x0C",
  337. "0x0D",
  338. "0x0E",
  339. "0x0F"};
  340. return btn <= 0xf ? name_btn[btn] : name_btn[0];
  341. }
  342. uint8_t subghz_protocol_decoder_somfy_keytis_get_hash_data(void* context) {
  343. furi_assert(context);
  344. SubGhzProtocolDecoderSomfyKeytis* instance = context;
  345. return subghz_protocol_blocks_get_hash_data(
  346. &instance->decoder, (instance->decoder.decode_count_bit / 8) + 1);
  347. }
  348. bool subghz_protocol_decoder_somfy_keytis_serialize(
  349. void* context,
  350. FlipperFormat* flipper_format,
  351. uint32_t frequency,
  352. FuriHalSubGhzPreset preset) {
  353. furi_assert(context);
  354. SubGhzProtocolDecoderSomfyKeytis* instance = context;
  355. bool res =
  356. subghz_block_generic_serialize(&instance->generic, flipper_format, frequency, preset);
  357. if(res && !flipper_format_write_uint32(
  358. flipper_format, "Duration_Counter", &instance->press_duration_counter, 1)) {
  359. FURI_LOG_E(TAG, "Unable to add Duration_Counter");
  360. res = false;
  361. }
  362. return res;
  363. }
  364. bool subghz_protocol_decoder_somfy_keytis_deserialize(void* context, FlipperFormat* flipper_format) {
  365. furi_assert(context);
  366. SubGhzProtocolDecoderSomfyKeytis* instance = context;
  367. bool res = false;
  368. do {
  369. if(!subghz_block_generic_deserialize(&instance->generic, flipper_format)) {
  370. FURI_LOG_E(TAG, "Deserialize error");
  371. break;
  372. }
  373. if(!flipper_format_rewind(flipper_format)) {
  374. FURI_LOG_E(TAG, "Rewind error");
  375. break;
  376. }
  377. if(!flipper_format_read_uint32(
  378. flipper_format,
  379. "Duration_Counter",
  380. (uint32_t*)&instance->press_duration_counter,
  381. 1)) {
  382. FURI_LOG_E(TAG, "Missing Duration_Counter");
  383. break;
  384. }
  385. res = true;
  386. } while(false);
  387. return res;
  388. }
  389. void subghz_protocol_decoder_somfy_keytis_get_string(void* context, string_t output) {
  390. furi_assert(context);
  391. SubGhzProtocolDecoderSomfyKeytis* instance = context;
  392. subghz_protocol_somfy_keytis_check_remote_controller(&instance->generic);
  393. string_cat_printf(
  394. output,
  395. "%s %db\r\n"
  396. "%lX%08lX%06lX\r\n"
  397. "Sn:0x%06lX \r\n"
  398. "Cnt:0x%04X\r\n"
  399. "Btn:%s\r\n",
  400. instance->generic.protocol_name,
  401. instance->generic.data_count_bit,
  402. (uint32_t)(instance->generic.data >> 32),
  403. (uint32_t)instance->generic.data,
  404. instance->press_duration_counter,
  405. instance->generic.serial,
  406. instance->generic.cnt,
  407. subghz_protocol_somfy_keytis_get_name_button(instance->generic.btn));
  408. }