marantec.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393
  1. #include "marantec.h"
  2. #include <lib/toolbox/manchester_decoder.h>
  3. #include <lib/toolbox/manchester_encoder.h>
  4. #include "../blocks/const.h"
  5. #include "../blocks/decoder.h"
  6. #include "../blocks/encoder.h"
  7. #include "../blocks/generic.h"
  8. #include "../blocks/math.h"
  9. #define TAG "SubGhzProtocolMarantec"
  10. static const SubGhzBlockConst subghz_protocol_marantec_const = {
  11. .te_short = 1000,
  12. .te_long = 2000,
  13. .te_delta = 200,
  14. .min_count_bit_for_found = 49,
  15. };
  16. struct SubGhzProtocolDecoderMarantec {
  17. SubGhzProtocolDecoderBase base;
  18. SubGhzBlockDecoder decoder;
  19. SubGhzBlockGeneric generic;
  20. ManchesterState manchester_saved_state;
  21. uint16_t header_count;
  22. };
  23. struct SubGhzProtocolEncoderMarantec {
  24. SubGhzProtocolEncoderBase base;
  25. SubGhzProtocolBlockEncoder encoder;
  26. SubGhzBlockGeneric generic;
  27. };
  28. typedef enum {
  29. MarantecDecoderStepReset = 0,
  30. MarantecDecoderFoundHeader,
  31. MarantecDecoderStepDecoderData,
  32. } MarantecDecoderStep;
  33. const SubGhzProtocolDecoder subghz_protocol_marantec_decoder = {
  34. .alloc = subghz_protocol_decoder_marantec_alloc,
  35. .free = subghz_protocol_decoder_marantec_free,
  36. .feed = subghz_protocol_decoder_marantec_feed,
  37. .reset = subghz_protocol_decoder_marantec_reset,
  38. .get_hash_data = subghz_protocol_decoder_marantec_get_hash_data,
  39. .serialize = subghz_protocol_decoder_marantec_serialize,
  40. .deserialize = subghz_protocol_decoder_marantec_deserialize,
  41. .get_string = subghz_protocol_decoder_marantec_get_string,
  42. };
  43. const SubGhzProtocolEncoder subghz_protocol_marantec_encoder = {
  44. .alloc = subghz_protocol_encoder_marantec_alloc,
  45. .free = subghz_protocol_encoder_marantec_free,
  46. .deserialize = subghz_protocol_encoder_marantec_deserialize,
  47. .stop = subghz_protocol_encoder_marantec_stop,
  48. .yield = subghz_protocol_encoder_marantec_yield,
  49. };
  50. const SubGhzProtocol subghz_protocol_marantec = {
  51. .name = SUBGHZ_PROTOCOL_MARANTEC_NAME,
  52. .type = SubGhzProtocolTypeStatic,
  53. .flag = SubGhzProtocolFlag_433 | SubGhzProtocolFlag_AM | SubGhzProtocolFlag_Decodable |
  54. SubGhzProtocolFlag_Load | SubGhzProtocolFlag_Save | SubGhzProtocolFlag_Send,
  55. .decoder = &subghz_protocol_marantec_decoder,
  56. .encoder = &subghz_protocol_marantec_encoder,
  57. };
  58. void* subghz_protocol_encoder_marantec_alloc(SubGhzEnvironment* environment) {
  59. UNUSED(environment);
  60. SubGhzProtocolEncoderMarantec* instance = malloc(sizeof(SubGhzProtocolEncoderMarantec));
  61. instance->base.protocol = &subghz_protocol_marantec;
  62. instance->generic.protocol_name = instance->base.protocol->name;
  63. instance->encoder.repeat = 10;
  64. instance->encoder.size_upload = 256;
  65. instance->encoder.upload = malloc(instance->encoder.size_upload * sizeof(LevelDuration));
  66. instance->encoder.is_running = false;
  67. return instance;
  68. }
  69. void subghz_protocol_encoder_marantec_free(void* context) {
  70. furi_assert(context);
  71. SubGhzProtocolEncoderMarantec* instance = context;
  72. free(instance->encoder.upload);
  73. free(instance);
  74. }
  75. static LevelDuration
  76. subghz_protocol_encoder_marantec_add_duration_to_upload(ManchesterEncoderResult result) {
  77. LevelDuration data = {.duration = 0, .level = 0};
  78. switch(result) {
  79. case ManchesterEncoderResultShortLow:
  80. data.duration = subghz_protocol_marantec_const.te_short;
  81. data.level = false;
  82. break;
  83. case ManchesterEncoderResultLongLow:
  84. data.duration = subghz_protocol_marantec_const.te_long;
  85. data.level = false;
  86. break;
  87. case ManchesterEncoderResultLongHigh:
  88. data.duration = subghz_protocol_marantec_const.te_long;
  89. data.level = true;
  90. break;
  91. case ManchesterEncoderResultShortHigh:
  92. data.duration = subghz_protocol_marantec_const.te_short;
  93. data.level = true;
  94. break;
  95. default:
  96. furi_crash("SubGhz: ManchesterEncoderResult is incorrect.");
  97. break;
  98. }
  99. return level_duration_make(data.level, data.duration);
  100. }
  101. /**
  102. * Generating an upload from data.
  103. * @param instance Pointer to a SubGhzProtocolEncoderMarantec instance
  104. */
  105. static void subghz_protocol_encoder_marantec_get_upload(SubGhzProtocolEncoderMarantec* instance) {
  106. furi_assert(instance);
  107. size_t index = 0;
  108. ManchesterEncoderState enc_state;
  109. manchester_encoder_reset(&enc_state);
  110. ManchesterEncoderResult result;
  111. if(!manchester_encoder_advance(
  112. &enc_state,
  113. bit_read(instance->generic.data, instance->generic.data_count_bit - 1),
  114. &result)) {
  115. instance->encoder.upload[index++] =
  116. subghz_protocol_encoder_marantec_add_duration_to_upload(result);
  117. manchester_encoder_advance(
  118. &enc_state,
  119. bit_read(instance->generic.data, instance->generic.data_count_bit - 1),
  120. &result);
  121. }
  122. instance->encoder.upload[index++] =
  123. level_duration_make(false, (uint32_t)subghz_protocol_marantec_const.te_long * 5);
  124. for(uint8_t i = instance->generic.data_count_bit - 1; i > 0; i--) {
  125. if(!manchester_encoder_advance(
  126. &enc_state, bit_read(instance->generic.data, i - 1), &result)) {
  127. instance->encoder.upload[index++] =
  128. subghz_protocol_encoder_marantec_add_duration_to_upload(result);
  129. manchester_encoder_advance(
  130. &enc_state, bit_read(instance->generic.data, i - 1), &result);
  131. }
  132. instance->encoder.upload[index++] =
  133. subghz_protocol_encoder_marantec_add_duration_to_upload(result);
  134. }
  135. instance->encoder.upload[index] = subghz_protocol_encoder_marantec_add_duration_to_upload(
  136. manchester_encoder_finish(&enc_state));
  137. if(level_duration_get_level(instance->encoder.upload[index])) {
  138. index++;
  139. }
  140. instance->encoder.size_upload = index;
  141. }
  142. uint8_t subghz_protocol_marantec_crc8(uint8_t* data, size_t len) {
  143. uint8_t crc = 0x08;
  144. size_t i, j;
  145. for(i = 0; i < len; i++) {
  146. crc ^= data[i];
  147. for(j = 0; j < 8; j++) {
  148. if((crc & 0x80) != 0)
  149. crc = (uint8_t)((crc << 1) ^ 0x1D);
  150. else
  151. crc <<= 1;
  152. }
  153. }
  154. return crc;
  155. }
  156. /**
  157. * Analysis of received data
  158. * @param instance Pointer to a SubGhzBlockGeneric* instance
  159. */
  160. static void subghz_protocol_marantec_remote_controller(SubGhzBlockGeneric* instance) {
  161. instance->btn = (instance->data >> 16) & 0xF;
  162. instance->serial = ((instance->data >> 12) & 0xFFFFFF00) | ((instance->data >> 8) & 0xFF);
  163. }
  164. bool subghz_protocol_encoder_marantec_deserialize(void* context, FlipperFormat* flipper_format) {
  165. furi_assert(context);
  166. SubGhzProtocolEncoderMarantec* instance = context;
  167. bool res = false;
  168. do {
  169. if(!subghz_block_generic_deserialize(&instance->generic, flipper_format)) {
  170. FURI_LOG_E(TAG, "Deserialize error");
  171. break;
  172. }
  173. if(instance->generic.data_count_bit !=
  174. subghz_protocol_marantec_const.min_count_bit_for_found) {
  175. FURI_LOG_E(TAG, "Wrong number of bits in key");
  176. break;
  177. }
  178. //optional parameter parameter
  179. flipper_format_read_uint32(
  180. flipper_format, "Repeat", (uint32_t*)&instance->encoder.repeat, 1);
  181. subghz_protocol_marantec_remote_controller(&instance->generic);
  182. subghz_protocol_encoder_marantec_get_upload(instance);
  183. instance->encoder.is_running = true;
  184. res = true;
  185. } while(false);
  186. return res;
  187. }
  188. void subghz_protocol_encoder_marantec_stop(void* context) {
  189. SubGhzProtocolEncoderMarantec* instance = context;
  190. instance->encoder.is_running = false;
  191. }
  192. LevelDuration subghz_protocol_encoder_marantec_yield(void* context) {
  193. SubGhzProtocolEncoderMarantec* instance = context;
  194. if(instance->encoder.repeat == 0 || !instance->encoder.is_running) {
  195. instance->encoder.is_running = false;
  196. return level_duration_reset();
  197. }
  198. LevelDuration ret = instance->encoder.upload[instance->encoder.front];
  199. if(++instance->encoder.front == instance->encoder.size_upload) {
  200. instance->encoder.repeat--;
  201. instance->encoder.front = 0;
  202. }
  203. return ret;
  204. }
  205. void* subghz_protocol_decoder_marantec_alloc(SubGhzEnvironment* environment) {
  206. UNUSED(environment);
  207. SubGhzProtocolDecoderMarantec* instance = malloc(sizeof(SubGhzProtocolDecoderMarantec));
  208. instance->base.protocol = &subghz_protocol_marantec;
  209. instance->generic.protocol_name = instance->base.protocol->name;
  210. return instance;
  211. }
  212. void subghz_protocol_decoder_marantec_free(void* context) {
  213. furi_assert(context);
  214. SubGhzProtocolDecoderMarantec* instance = context;
  215. free(instance);
  216. }
  217. void subghz_protocol_decoder_marantec_reset(void* context) {
  218. furi_assert(context);
  219. SubGhzProtocolDecoderMarantec* instance = context;
  220. manchester_advance(
  221. instance->manchester_saved_state,
  222. ManchesterEventReset,
  223. &instance->manchester_saved_state,
  224. NULL);
  225. }
  226. void subghz_protocol_decoder_marantec_feed(void* context, bool level, volatile uint32_t duration) {
  227. furi_assert(context);
  228. SubGhzProtocolDecoderMarantec* instance = context;
  229. ManchesterEvent event = ManchesterEventReset;
  230. switch(instance->decoder.parser_step) {
  231. case MarantecDecoderStepReset:
  232. if((!level) && (DURATION_DIFF(duration, subghz_protocol_marantec_const.te_long * 5) <
  233. subghz_protocol_marantec_const.te_delta * 8)) {
  234. //Found header marantec
  235. instance->decoder.parser_step = MarantecDecoderStepDecoderData;
  236. instance->decoder.decode_data = 1;
  237. instance->decoder.decode_count_bit = 1;
  238. manchester_advance(
  239. instance->manchester_saved_state,
  240. ManchesterEventReset,
  241. &instance->manchester_saved_state,
  242. NULL);
  243. }
  244. break;
  245. case MarantecDecoderStepDecoderData:
  246. if(!level) {
  247. if(DURATION_DIFF(duration, subghz_protocol_marantec_const.te_short) <
  248. subghz_protocol_marantec_const.te_delta) {
  249. event = ManchesterEventShortLow;
  250. } else if(
  251. DURATION_DIFF(duration, subghz_protocol_marantec_const.te_long) <
  252. subghz_protocol_marantec_const.te_delta) {
  253. event = ManchesterEventLongLow;
  254. } else if(
  255. duration >= ((uint32_t)subghz_protocol_marantec_const.te_long * 2 +
  256. subghz_protocol_marantec_const.te_delta)) {
  257. if(instance->decoder.decode_count_bit ==
  258. subghz_protocol_marantec_const.min_count_bit_for_found) {
  259. instance->generic.data = instance->decoder.decode_data;
  260. instance->generic.data_count_bit = instance->decoder.decode_count_bit;
  261. if(instance->base.callback)
  262. instance->base.callback(&instance->base, instance->base.context);
  263. }
  264. instance->decoder.decode_data = 1;
  265. instance->decoder.decode_count_bit = 1;
  266. manchester_advance(
  267. instance->manchester_saved_state,
  268. ManchesterEventReset,
  269. &instance->manchester_saved_state,
  270. NULL);
  271. } else {
  272. instance->decoder.parser_step = MarantecDecoderStepReset;
  273. }
  274. } else {
  275. if(DURATION_DIFF(duration, subghz_protocol_marantec_const.te_short) <
  276. subghz_protocol_marantec_const.te_delta) {
  277. event = ManchesterEventShortHigh;
  278. } else if(
  279. DURATION_DIFF(duration, subghz_protocol_marantec_const.te_long) <
  280. subghz_protocol_marantec_const.te_delta) {
  281. event = ManchesterEventLongHigh;
  282. } else {
  283. instance->decoder.parser_step = MarantecDecoderStepReset;
  284. }
  285. }
  286. if(event != ManchesterEventReset) {
  287. bool data;
  288. bool data_ok = manchester_advance(
  289. instance->manchester_saved_state, event, &instance->manchester_saved_state, &data);
  290. if(data_ok) {
  291. instance->decoder.decode_data = (instance->decoder.decode_data << 1) | data;
  292. instance->decoder.decode_count_bit++;
  293. }
  294. }
  295. break;
  296. }
  297. }
  298. uint8_t subghz_protocol_decoder_marantec_get_hash_data(void* context) {
  299. furi_assert(context);
  300. SubGhzProtocolDecoderMarantec* instance = context;
  301. return subghz_protocol_blocks_get_hash_data(
  302. &instance->decoder, (instance->decoder.decode_count_bit / 8) + 1);
  303. }
  304. bool subghz_protocol_decoder_marantec_serialize(
  305. void* context,
  306. FlipperFormat* flipper_format,
  307. SubGhzRadioPreset* preset) {
  308. furi_assert(context);
  309. SubGhzProtocolDecoderMarantec* instance = context;
  310. return subghz_block_generic_serialize(&instance->generic, flipper_format, preset);
  311. }
  312. bool subghz_protocol_decoder_marantec_deserialize(void* context, FlipperFormat* flipper_format) {
  313. furi_assert(context);
  314. SubGhzProtocolDecoderMarantec* instance = context;
  315. bool ret = false;
  316. do {
  317. if(!subghz_block_generic_deserialize(&instance->generic, flipper_format)) {
  318. break;
  319. }
  320. if(instance->generic.data_count_bit !=
  321. subghz_protocol_marantec_const.min_count_bit_for_found) {
  322. FURI_LOG_E(TAG, "Wrong number of bits in key");
  323. break;
  324. }
  325. ret = true;
  326. } while(false);
  327. return ret;
  328. }
  329. void subghz_protocol_decoder_marantec_get_string(void* context, FuriString* output) {
  330. furi_assert(context);
  331. SubGhzProtocolDecoderMarantec* instance = context;
  332. subghz_protocol_marantec_remote_controller(&instance->generic);
  333. furi_string_cat_printf(
  334. output,
  335. "%s %db\r\n"
  336. "Key:0x%lX%08lX\r\n"
  337. "Sn:0x%07lX \r\n"
  338. "Btn:%X\r\n",
  339. instance->generic.protocol_name,
  340. instance->generic.data_count_bit,
  341. (uint32_t)(instance->generic.data >> 32),
  342. (uint32_t)(instance->generic.data & 0xFFFFFFFF),
  343. instance->generic.serial,
  344. instance->generic.btn);
  345. }