somfy_keytis.c 16 KB

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