somfy_telis.c 13 KB

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