somfy_telis.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387
  1. #include "somfy_telis.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 "SubGhzProtocolSomfyTelis"
  9. static const SubGhzBlockConst subghz_protocol_somfy_telis_const = {
  10. .te_short = 640,
  11. .te_long = 1280,
  12. .te_delta = 250,
  13. .min_count_bit_for_found = 56,
  14. };
  15. struct SubGhzProtocolDecoderSomfyTelis {
  16. SubGhzProtocolDecoderBase base;
  17. SubGhzBlockDecoder decoder;
  18. SubGhzBlockGeneric generic;
  19. uint16_t header_count;
  20. ManchesterState manchester_saved_state;
  21. };
  22. struct SubGhzProtocolEncoderSomfyTelis {
  23. SubGhzProtocolEncoderBase base;
  24. SubGhzProtocolBlockEncoder encoder;
  25. SubGhzBlockGeneric generic;
  26. };
  27. typedef enum {
  28. SomfyTelisDecoderStepReset = 0,
  29. SomfyTelisDecoderStepCheckPreambula,
  30. SomfyTelisDecoderStepFoundPreambula,
  31. SomfyTelisDecoderStepStartDecode,
  32. SomfyTelisDecoderStepDecoderData,
  33. } SomfyTelisDecoderStep;
  34. const SubGhzProtocolDecoder subghz_protocol_somfy_telis_decoder = {
  35. .alloc = subghz_protocol_decoder_somfy_telis_alloc,
  36. .free = subghz_protocol_decoder_somfy_telis_free,
  37. .feed = subghz_protocol_decoder_somfy_telis_feed,
  38. .reset = subghz_protocol_decoder_somfy_telis_reset,
  39. .get_hash_data = subghz_protocol_decoder_somfy_telis_get_hash_data,
  40. .serialize = subghz_protocol_decoder_somfy_telis_serialize,
  41. .deserialize = subghz_protocol_decoder_somfy_telis_deserialize,
  42. .get_string = subghz_protocol_decoder_somfy_telis_get_string,
  43. };
  44. const SubGhzProtocolEncoder subghz_protocol_somfy_telis_encoder = {
  45. .alloc = NULL,
  46. .free = NULL,
  47. .deserialize = NULL,
  48. .stop = NULL,
  49. .yield = NULL,
  50. };
  51. const SubGhzProtocol subghz_protocol_somfy_telis = {
  52. .name = SUBGHZ_PROTOCOL_SOMFY_TELIS_NAME,
  53. .type = SubGhzProtocolTypeDynamic,
  54. .flag = SubGhzProtocolFlag_433 | SubGhzProtocolFlag_868 | SubGhzProtocolFlag_AM |
  55. SubGhzProtocolFlag_Decodable,
  56. .decoder = &subghz_protocol_somfy_telis_decoder,
  57. .encoder = &subghz_protocol_somfy_telis_encoder,
  58. };
  59. void* subghz_protocol_decoder_somfy_telis_alloc(SubGhzEnvironment* environment) {
  60. UNUSED(environment);
  61. SubGhzProtocolDecoderSomfyTelis* instance = malloc(sizeof(SubGhzProtocolDecoderSomfyTelis));
  62. instance->base.protocol = &subghz_protocol_somfy_telis;
  63. instance->generic.protocol_name = instance->base.protocol->name;
  64. return instance;
  65. }
  66. void subghz_protocol_decoder_somfy_telis_free(void* context) {
  67. furi_assert(context);
  68. SubGhzProtocolDecoderSomfyTelis* instance = context;
  69. free(instance);
  70. }
  71. void subghz_protocol_decoder_somfy_telis_reset(void* context) {
  72. furi_assert(context);
  73. SubGhzProtocolDecoderSomfyTelis* instance = context;
  74. instance->decoder.parser_step = SomfyTelisDecoderStepReset;
  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_telis_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_telis_feed(void* context, bool level, uint32_t duration) {
  95. furi_assert(context);
  96. SubGhzProtocolDecoderSomfyTelis* instance = context;
  97. ManchesterEvent event = ManchesterEventReset;
  98. switch(instance->decoder.parser_step) {
  99. case SomfyTelisDecoderStepReset:
  100. if((level) && DURATION_DIFF(duration, subghz_protocol_somfy_telis_const.te_short * 4) <
  101. subghz_protocol_somfy_telis_const.te_delta * 4) {
  102. instance->decoder.parser_step = SomfyTelisDecoderStepFoundPreambula;
  103. instance->header_count++;
  104. }
  105. break;
  106. case SomfyTelisDecoderStepFoundPreambula:
  107. if((!level) && (DURATION_DIFF(duration, subghz_protocol_somfy_telis_const.te_short * 4) <
  108. subghz_protocol_somfy_telis_const.te_delta * 4)) {
  109. instance->decoder.parser_step = SomfyTelisDecoderStepCheckPreambula;
  110. } else {
  111. instance->header_count = 0;
  112. instance->decoder.parser_step = SomfyTelisDecoderStepReset;
  113. }
  114. break;
  115. case SomfyTelisDecoderStepCheckPreambula:
  116. if(level) {
  117. if(DURATION_DIFF(duration, subghz_protocol_somfy_telis_const.te_short * 4) <
  118. subghz_protocol_somfy_telis_const.te_delta * 4) {
  119. instance->decoder.parser_step = SomfyTelisDecoderStepFoundPreambula;
  120. instance->header_count++;
  121. } else if(
  122. (instance->header_count > 1) &&
  123. (DURATION_DIFF(duration, subghz_protocol_somfy_telis_const.te_short * 7) <
  124. subghz_protocol_somfy_telis_const.te_delta * 4)) {
  125. instance->decoder.parser_step = SomfyTelisDecoderStepDecoderData;
  126. instance->decoder.decode_data = 0;
  127. instance->decoder.decode_count_bit = 0;
  128. instance->header_count = 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 SomfyTelisDecoderStepDecoderData:
  143. if(!level) {
  144. if(DURATION_DIFF(duration, subghz_protocol_somfy_telis_const.te_short) <
  145. subghz_protocol_somfy_telis_const.te_delta) {
  146. event = ManchesterEventShortLow;
  147. } else if(
  148. DURATION_DIFF(duration, subghz_protocol_somfy_telis_const.te_long) <
  149. subghz_protocol_somfy_telis_const.te_delta) {
  150. event = ManchesterEventLongLow;
  151. } else if(
  152. duration >= (subghz_protocol_somfy_telis_const.te_long +
  153. subghz_protocol_somfy_telis_const.te_delta)) {
  154. if(instance->decoder.decode_count_bit ==
  155. subghz_protocol_somfy_telis_const.min_count_bit_for_found) {
  156. //check crc
  157. uint64_t data_tmp = instance->decoder.decode_data ^
  158. (instance->decoder.decode_data >> 8);
  159. if(((data_tmp >> 40) & 0xF) == subghz_protocol_somfy_telis_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 = SomfyTelisDecoderStepReset;
  179. } else {
  180. instance->decoder.parser_step = SomfyTelisDecoderStepReset;
  181. }
  182. } else {
  183. if(DURATION_DIFF(duration, subghz_protocol_somfy_telis_const.te_short) <
  184. subghz_protocol_somfy_telis_const.te_delta) {
  185. event = ManchesterEventShortHigh;
  186. } else if(
  187. DURATION_DIFF(duration, subghz_protocol_somfy_telis_const.te_long) <
  188. subghz_protocol_somfy_telis_const.te_delta) {
  189. event = ManchesterEventLongHigh;
  190. } else {
  191. instance->decoder.parser_step = SomfyTelisDecoderStepReset;
  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. instance->decoder.decode_data = (instance->decoder.decode_data << 1) | data;
  200. instance->decoder.decode_count_bit++;
  201. }
  202. }
  203. break;
  204. }
  205. }
  206. /**
  207. * Analysis of received data
  208. * @param instance Pointer to a SubGhzBlockGeneric* instance
  209. */
  210. static void subghz_protocol_somfy_telis_check_remote_controller(SubGhzBlockGeneric* instance) {
  211. //https://pushstack.wordpress.com/somfy-rts-protocol/
  212. /*
  213. * 604 us
  214. * /
  215. * | 2416us | 2416us | 2416us | 2416us | 4550 us | | 67648 us | 30415 us |
  216. *
  217. * +--------+ +--------+ +---...---+
  218. * + +--------+ +--------+ +--+XXXX...XXX+-----...-----
  219. *
  220. * | hw. sync. | soft. | | Inter-frame
  221. * | | sync. | data | gap
  222. *
  223. *
  224. * encrypt | decrypt
  225. *
  226. * package 56 bit cnt key btn|crc cnt serial
  227. * 0xA7232323312222 - 0 => A7 8 0 | 00 00 | 12 13 00
  228. * 0xA7222223312222 - 1 => A7 8 5 | 00 01 | 12 13 00
  229. * 0xA7212123312222 - 2 => A7 8 6 | 00 02 | 12 13 00
  230. *
  231. * Key: “Encryption Key”, Most significant 4-bit are always 0xA, Least Significant bits is
  232. * a linear counter. In the Smoove Origin this counter is increased together with the
  233. * rolling code. But leaving this on a constant value also works. Gerardwr notes that
  234. * for some other types of remotes the MSB is not constant.
  235. * Btn: 4-bit Control codes, this indicates the button that is pressed
  236. * CRC: 4-bit Checksum.
  237. * Ctn: 16-bit rolling code (big-endian) increased with every button press.
  238. * Serial: 24-bit identifier of sending device (little-endian)
  239. *
  240. *
  241. * Decrypt
  242. *
  243. * uint8_t frame[7];
  244. * for (i=1; i < 7; i++) {
  245. * frame[i] = frame[i] ^ frame[i-1];
  246. * }
  247. * or
  248. * uint64 Decrypt = frame ^ (frame>>8);
  249. *
  250. * Btn
  251. *
  252. * Value Button(s) Description
  253. * 0x1 My Stop or move to favourite position
  254. * 0x2 Up Move up
  255. * 0x3 My + Up Set upper motor limit in initial programming mode
  256. * 0x4 Down Move down
  257. * 0x5 My + Down Set lower motor limit in initial programming mode
  258. * 0x6 Up + Down Change motor limit and initial programming mode
  259. * 0x8 Prog Used for (de-)registering remotes, see below
  260. * 0x9 Sun + Flag Enable sun and wind detector (SUN and FLAG symbol on the Telis Soliris RC)
  261. * 0xA Flag Disable sun detector (FLAG symbol on the Telis Soliris RC)
  262. *
  263. * CRC
  264. *
  265. * uint8_t frame[7];
  266. * for (i=0; i < 7; i++) {
  267. * cksum = cksum ^ frame[i] ^ (frame[i] >> 4);
  268. * }
  269. * cksum = cksum & 0xf;
  270. *
  271. */
  272. uint64_t data = instance->data ^ (instance->data >> 8);
  273. instance->btn = (data >> 44) & 0xF;
  274. instance->cnt = (data >> 24) & 0xFFFF;
  275. instance->serial = data & 0xFFFFFF;
  276. }
  277. /**
  278. * Get button name.
  279. * @param btn Button number, 4 bit
  280. */
  281. static const char* subghz_protocol_somfy_telis_get_name_button(uint8_t btn) {
  282. const char* name_btn[0x10] = {
  283. "Unknown",
  284. "My",
  285. "Up",
  286. "My+Up",
  287. "Down",
  288. "My+Down",
  289. "Up+Down",
  290. "0x07",
  291. "Prog",
  292. "Sun+Flag",
  293. "Flag",
  294. "0x0B",
  295. "0x0C",
  296. "0x0D",
  297. "0x0E",
  298. "0x0F"};
  299. return btn <= 0xf ? name_btn[btn] : name_btn[0];
  300. }
  301. uint8_t subghz_protocol_decoder_somfy_telis_get_hash_data(void* context) {
  302. furi_assert(context);
  303. SubGhzProtocolDecoderSomfyTelis* instance = context;
  304. return subghz_protocol_blocks_get_hash_data(
  305. &instance->decoder, (instance->decoder.decode_count_bit / 8) + 1);
  306. }
  307. bool subghz_protocol_decoder_somfy_telis_serialize(
  308. void* context,
  309. FlipperFormat* flipper_format,
  310. SubGhzRadioPreset* preset) {
  311. furi_assert(context);
  312. SubGhzProtocolDecoderSomfyTelis* instance = context;
  313. return subghz_block_generic_serialize(&instance->generic, flipper_format, preset);
  314. }
  315. bool subghz_protocol_decoder_somfy_telis_deserialize(void* context, FlipperFormat* flipper_format) {
  316. furi_assert(context);
  317. SubGhzProtocolDecoderSomfyTelis* instance = context;
  318. bool ret = false;
  319. do {
  320. if(!subghz_block_generic_deserialize(&instance->generic, flipper_format)) {
  321. break;
  322. }
  323. if(instance->generic.data_count_bit !=
  324. subghz_protocol_somfy_telis_const.min_count_bit_for_found) {
  325. FURI_LOG_E(TAG, "Wrong number of bits in key");
  326. break;
  327. }
  328. ret = true;
  329. } while(false);
  330. return ret;
  331. }
  332. void subghz_protocol_decoder_somfy_telis_get_string(void* context, FuriString* output) {
  333. furi_assert(context);
  334. SubGhzProtocolDecoderSomfyTelis* instance = context;
  335. subghz_protocol_somfy_telis_check_remote_controller(&instance->generic);
  336. furi_string_cat_printf(
  337. output,
  338. "%s %db\r\n"
  339. "Key:0x%lX%08lX\r\n"
  340. "Sn:0x%06lX \r\n"
  341. "Cnt:0x%04lX\r\n"
  342. "Btn:%s\r\n",
  343. instance->generic.protocol_name,
  344. instance->generic.data_count_bit,
  345. (uint32_t)(instance->generic.data >> 32),
  346. (uint32_t)instance->generic.data,
  347. instance->generic.serial,
  348. instance->generic.cnt,
  349. subghz_protocol_somfy_telis_get_name_button(instance->generic.btn));
  350. }