somfy_telis.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  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. SubGhzProtocolDecoderSomfyTelis* instance = malloc(sizeof(SubGhzProtocolDecoderSomfyTelis));
  61. instance->base.protocol = &subghz_protocol_somfy_telis;
  62. instance->generic.protocol_name = instance->base.protocol->name;
  63. return instance;
  64. }
  65. void subghz_protocol_decoder_somfy_telis_free(void* context) {
  66. furi_assert(context);
  67. SubGhzProtocolDecoderSomfyTelis* instance = context;
  68. free(instance);
  69. }
  70. void subghz_protocol_decoder_somfy_telis_reset(void* context) {
  71. furi_assert(context);
  72. SubGhzProtocolDecoderSomfyTelis* instance = context;
  73. instance->decoder.parser_step = SomfyTelisDecoderStepReset;
  74. manchester_advance(
  75. instance->manchester_saved_state,
  76. ManchesterEventReset,
  77. &instance->manchester_saved_state,
  78. NULL);
  79. }
  80. /**
  81. * Сhecksum calculation.
  82. * @param data Вata for checksum calculation
  83. * @return CRC
  84. */
  85. static uint8_t subghz_protocol_somfy_telis_crc(uint64_t data) {
  86. uint8_t crc = 0;
  87. data &= 0xFFF0FFFFFFFFFF;
  88. for(uint8_t i = 0; i < 56; i += 8) {
  89. crc = crc ^ data >> i ^ (data >> (i + 4));
  90. }
  91. return crc & 0xf;
  92. }
  93. void subghz_protocol_decoder_somfy_telis_feed(void* context, bool level, uint32_t duration) {
  94. furi_assert(context);
  95. SubGhzProtocolDecoderSomfyTelis* instance = context;
  96. ManchesterEvent event = ManchesterEventReset;
  97. switch(instance->decoder.parser_step) {
  98. case SomfyTelisDecoderStepReset:
  99. if((level) && DURATION_DIFF(duration, subghz_protocol_somfy_telis_const.te_short * 4) <
  100. subghz_protocol_somfy_telis_const.te_delta * 4) {
  101. instance->decoder.parser_step = SomfyTelisDecoderStepFoundPreambula;
  102. instance->header_count++;
  103. }
  104. break;
  105. case SomfyTelisDecoderStepFoundPreambula:
  106. if((!level) && (DURATION_DIFF(duration, subghz_protocol_somfy_telis_const.te_short * 4) <
  107. subghz_protocol_somfy_telis_const.te_delta * 4)) {
  108. instance->decoder.parser_step = SomfyTelisDecoderStepCheckPreambula;
  109. } else {
  110. instance->header_count = 0;
  111. instance->decoder.parser_step = SomfyTelisDecoderStepReset;
  112. }
  113. break;
  114. case SomfyTelisDecoderStepCheckPreambula:
  115. if(level) {
  116. if(DURATION_DIFF(duration, subghz_protocol_somfy_telis_const.te_short * 4) <
  117. subghz_protocol_somfy_telis_const.te_delta * 4) {
  118. instance->decoder.parser_step = SomfyTelisDecoderStepFoundPreambula;
  119. instance->header_count++;
  120. } else if(
  121. (instance->header_count > 1) &&
  122. (DURATION_DIFF(duration, subghz_protocol_somfy_telis_const.te_short * 7) <
  123. subghz_protocol_somfy_telis_const.te_delta * 4)) {
  124. instance->decoder.parser_step = SomfyTelisDecoderStepDecoderData;
  125. instance->decoder.decode_data = 0;
  126. instance->decoder.decode_count_bit = 0;
  127. instance->header_count = 0;
  128. manchester_advance(
  129. instance->manchester_saved_state,
  130. ManchesterEventReset,
  131. &instance->manchester_saved_state,
  132. NULL);
  133. manchester_advance(
  134. instance->manchester_saved_state,
  135. ManchesterEventLongHigh,
  136. &instance->manchester_saved_state,
  137. NULL);
  138. }
  139. }
  140. break;
  141. case SomfyTelisDecoderStepDecoderData:
  142. if(!level) {
  143. if(DURATION_DIFF(duration, subghz_protocol_somfy_telis_const.te_short) <
  144. subghz_protocol_somfy_telis_const.te_delta) {
  145. event = ManchesterEventShortLow;
  146. } else if(
  147. DURATION_DIFF(duration, subghz_protocol_somfy_telis_const.te_long) <
  148. subghz_protocol_somfy_telis_const.te_delta) {
  149. event = ManchesterEventLongLow;
  150. } else if(
  151. duration >= (subghz_protocol_somfy_telis_const.te_long +
  152. subghz_protocol_somfy_telis_const.te_delta)) {
  153. if(instance->decoder.decode_count_bit ==
  154. subghz_protocol_somfy_telis_const.min_count_bit_for_found) {
  155. //check crc
  156. uint64_t data_tmp = instance->decoder.decode_data ^
  157. (instance->decoder.decode_data >> 8);
  158. if(((data_tmp >> 40) & 0xF) == subghz_protocol_somfy_telis_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 = SomfyTelisDecoderStepReset;
  178. } else {
  179. instance->decoder.parser_step = SomfyTelisDecoderStepReset;
  180. }
  181. } else {
  182. if(DURATION_DIFF(duration, subghz_protocol_somfy_telis_const.te_short) <
  183. subghz_protocol_somfy_telis_const.te_delta) {
  184. event = ManchesterEventShortHigh;
  185. } else if(
  186. DURATION_DIFF(duration, subghz_protocol_somfy_telis_const.te_long) <
  187. subghz_protocol_somfy_telis_const.te_delta) {
  188. event = ManchesterEventLongHigh;
  189. } else {
  190. instance->decoder.parser_step = SomfyTelisDecoderStepReset;
  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. instance->decoder.decode_data = (instance->decoder.decode_data << 1) | data;
  199. instance->decoder.decode_count_bit++;
  200. }
  201. }
  202. break;
  203. }
  204. }
  205. /**
  206. * Analysis of received data
  207. * @param instance Pointer to a SubGhzBlockGeneric* instance
  208. */
  209. static void subghz_protocol_somfy_telis_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 | | 67648 us | 30415 us |
  215. *
  216. * +--------+ +--------+ +---...---+
  217. * + +--------+ +--------+ +--+XXXX...XXX+-----...-----
  218. *
  219. * | hw. sync. | soft. | | Inter-frame
  220. * | | sync. | data | gap
  221. *
  222. *
  223. * encrypt | decrypt
  224. *
  225. * package 56 bit cnt key btn|crc cnt serial
  226. * 0xA7232323312222 - 0 => A7 8 0 | 00 00 | 12 13 00
  227. * 0xA7222223312222 - 1 => A7 8 5 | 00 01 | 12 13 00
  228. * 0xA7212123312222 - 2 => A7 8 6 | 00 02 | 12 13 00
  229. *
  230. * Key: “Encryption Key”, Most significant 4-bit are always 0xA, Least Significant bits is
  231. * a linear counter. In the Smoove Origin this counter is increased together with the
  232. * rolling code. But leaving this on a constant value also works. Gerardwr notes that
  233. * for some other types of remotes the MSB is not constant.
  234. * Btn: 4-bit Control codes, this indicates the button that is pressed
  235. * CRC: 4-bit Checksum.
  236. * Ctn: 16-bit rolling code (big-endian) increased with every button press.
  237. * Serial: 24-bit identifier of sending device (little-endian)
  238. *
  239. *
  240. * Decrypt
  241. *
  242. * uint8_t frame[7];
  243. * for (i=1; i < 7; i++) {
  244. * frame[i] = frame[i] ^ frame[i-1];
  245. * }
  246. * or
  247. * uint64 Decrypt = frame ^ (frame>>8);
  248. *
  249. * Btn
  250. *
  251. * Value Button(s) Description
  252. * 0x1 My Stop or move to favourite position
  253. * 0x2 Up Move up
  254. * 0x3 My + Up Set upper motor limit in initial programming mode
  255. * 0x4 Down Move down
  256. * 0x5 My + Down Set lower motor limit in initial programming mode
  257. * 0x6 Up + Down Change motor limit and initial programming mode
  258. * 0x8 Prog Used for (de-)registering remotes, see below
  259. * 0x9 Sun + Flag Enable sun and wind detector (SUN and FLAG symbol on the Telis Soliris RC)
  260. * 0xA Flag Disable sun detector (FLAG symbol on the Telis Soliris RC)
  261. *
  262. * CRC
  263. *
  264. * uint8_t frame[7];
  265. * for (i=0; i < 7; i++) {
  266. * cksum = cksum ^ frame[i] ^ (frame[i] >> 4);
  267. * }
  268. * cksum = cksum & 0xf;
  269. *
  270. */
  271. uint64_t data = instance->data ^ (instance->data >> 8);
  272. instance->btn = (data >> 44) & 0xF;
  273. instance->cnt = (data >> 24) & 0xFFFF;
  274. instance->serial = data & 0xFFFFFF;
  275. }
  276. /**
  277. * Get button name.
  278. * @param btn Button number, 4 bit
  279. */
  280. static const char* subghz_protocol_somfy_telis_get_name_button(uint8_t btn) {
  281. const char* name_btn[0x10] = {
  282. "Unknown",
  283. "My",
  284. "Up",
  285. "My+Up",
  286. "Down",
  287. "My+Down",
  288. "Up+Down",
  289. "0x07",
  290. "Prog",
  291. "Sun+Flag",
  292. "Flag",
  293. "0x0B",
  294. "0x0C",
  295. "0x0D",
  296. "0x0E",
  297. "0x0F"};
  298. return btn <= 0xf ? name_btn[btn] : name_btn[0];
  299. }
  300. uint8_t subghz_protocol_decoder_somfy_telis_get_hash_data(void* context) {
  301. furi_assert(context);
  302. SubGhzProtocolDecoderSomfyTelis* instance = context;
  303. return subghz_protocol_blocks_get_hash_data(
  304. &instance->decoder, (instance->decoder.decode_count_bit / 8) + 1);
  305. }
  306. bool subghz_protocol_decoder_somfy_telis_serialize(
  307. void* context,
  308. FlipperFormat* flipper_format,
  309. uint32_t frequency,
  310. FuriHalSubGhzPreset preset) {
  311. furi_assert(context);
  312. SubGhzProtocolDecoderSomfyTelis* instance = context;
  313. return subghz_block_generic_serialize(&instance->generic, flipper_format, frequency, preset);
  314. }
  315. bool subghz_protocol_decoder_somfy_telis_deserialize(void* context, FlipperFormat* flipper_format) {
  316. furi_assert(context);
  317. SubGhzProtocolDecoderSomfyTelis* instance = context;
  318. return subghz_block_generic_deserialize(&instance->generic, flipper_format);
  319. }
  320. void subghz_protocol_decoder_somfy_telis_get_string(void* context, string_t output) {
  321. furi_assert(context);
  322. SubGhzProtocolDecoderSomfyTelis* instance = context;
  323. subghz_protocol_somfy_telis_check_remote_controller(&instance->generic);
  324. string_cat_printf(
  325. output,
  326. "%s %db\r\n"
  327. "Key:0x%lX%08lX\r\n"
  328. "Sn:0x%06lX \r\n"
  329. "Cnt:0x%04X\r\n"
  330. "Btn:%s\r\n",
  331. instance->generic.protocol_name,
  332. instance->generic.data_count_bit,
  333. (uint32_t)(instance->generic.data >> 32),
  334. (uint32_t)instance->generic.data,
  335. instance->generic.serial,
  336. instance->generic.cnt,
  337. subghz_protocol_somfy_telis_get_name_button(instance->generic.btn));
  338. }