somfy_keytis.c 17 KB

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