hormann.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  1. #include "hormann.h"
  2. #include "../blocks/const.h"
  3. #include "../blocks/decoder.h"
  4. #include "../blocks/encoder.h"
  5. #include "../blocks/generic.h"
  6. #include "../blocks/math.h"
  7. #define TAG "SubGhzProtocolHormannHSM"
  8. #define HORMANN_HSM_PATTERN 0xFF000000003
  9. static const SubGhzBlockConst subghz_protocol_hormann_const = {
  10. .te_short = 500,
  11. .te_long = 1000,
  12. .te_delta = 200,
  13. .min_count_bit_for_found = 44,
  14. };
  15. struct SubGhzProtocolDecoderHormann {
  16. SubGhzProtocolDecoderBase base;
  17. SubGhzBlockDecoder decoder;
  18. SubGhzBlockGeneric generic;
  19. };
  20. struct SubGhzProtocolEncoderHormann {
  21. SubGhzProtocolEncoderBase base;
  22. SubGhzProtocolBlockEncoder encoder;
  23. SubGhzBlockGeneric generic;
  24. };
  25. typedef enum {
  26. HormannDecoderStepReset = 0,
  27. HormannDecoderStepFoundStartHeader,
  28. HormannDecoderStepFoundHeader,
  29. HormannDecoderStepFoundStartBit,
  30. HormannDecoderStepSaveDuration,
  31. HormannDecoderStepCheckDuration,
  32. } HormannDecoderStep;
  33. const SubGhzProtocolDecoder subghz_protocol_hormann_decoder = {
  34. .alloc = subghz_protocol_decoder_hormann_alloc,
  35. .free = subghz_protocol_decoder_hormann_free,
  36. .feed = subghz_protocol_decoder_hormann_feed,
  37. .reset = subghz_protocol_decoder_hormann_reset,
  38. .get_hash_data = subghz_protocol_decoder_hormann_get_hash_data,
  39. .serialize = subghz_protocol_decoder_hormann_serialize,
  40. .deserialize = subghz_protocol_decoder_hormann_deserialize,
  41. .get_string = subghz_protocol_decoder_hormann_get_string,
  42. };
  43. const SubGhzProtocolEncoder subghz_protocol_hormann_encoder = {
  44. .alloc = subghz_protocol_encoder_hormann_alloc,
  45. .free = subghz_protocol_encoder_hormann_free,
  46. .deserialize = subghz_protocol_encoder_hormann_deserialize,
  47. .stop = subghz_protocol_encoder_hormann_stop,
  48. .yield = subghz_protocol_encoder_hormann_yield,
  49. };
  50. const SubGhzProtocol subghz_protocol_hormann = {
  51. .name = SUBGHZ_PROTOCOL_HORMANN_HSM_NAME,
  52. .type = SubGhzProtocolTypeStatic,
  53. .flag = SubGhzProtocolFlag_433 | SubGhzProtocolFlag_868 | SubGhzProtocolFlag_AM |
  54. SubGhzProtocolFlag_Decodable | SubGhzProtocolFlag_Load | SubGhzProtocolFlag_Save |
  55. SubGhzProtocolFlag_Send,
  56. .decoder = &subghz_protocol_hormann_decoder,
  57. .encoder = &subghz_protocol_hormann_encoder,
  58. };
  59. void* subghz_protocol_encoder_hormann_alloc(SubGhzEnvironment* environment) {
  60. UNUSED(environment);
  61. SubGhzProtocolEncoderHormann* instance = malloc(sizeof(SubGhzProtocolEncoderHormann));
  62. instance->base.protocol = &subghz_protocol_hormann;
  63. instance->generic.protocol_name = instance->base.protocol->name;
  64. instance->encoder.repeat = 10;
  65. instance->encoder.size_upload = 2048;
  66. instance->encoder.upload = malloc(instance->encoder.size_upload * sizeof(LevelDuration));
  67. instance->encoder.is_running = false;
  68. return instance;
  69. }
  70. void subghz_protocol_encoder_hormann_free(void* context) {
  71. furi_assert(context);
  72. SubGhzProtocolEncoderHormann* instance = context;
  73. free(instance->encoder.upload);
  74. free(instance);
  75. }
  76. /**
  77. * Generating an upload from data.
  78. * @param instance Pointer to a SubGhzProtocolEncoderHormann instance
  79. * @return true On success
  80. */
  81. static bool subghz_protocol_encoder_hormann_get_upload(SubGhzProtocolEncoderHormann* instance) {
  82. furi_assert(instance);
  83. size_t index = 0;
  84. size_t size_upload = (instance->generic.data_count_bit * 2 + 2) * 20 + 1;
  85. if(size_upload > instance->encoder.size_upload) {
  86. FURI_LOG_E(TAG, "Size upload exceeds allocated encoder buffer.");
  87. return false;
  88. } else {
  89. instance->encoder.size_upload = size_upload;
  90. }
  91. instance->encoder.repeat = 10; //original remote does 10 repeats
  92. for(size_t repeat = 0; repeat < 20; repeat++) {
  93. //Send start bit
  94. instance->encoder.upload[index++] =
  95. level_duration_make(true, (uint32_t)subghz_protocol_hormann_const.te_short * 24);
  96. instance->encoder.upload[index++] =
  97. level_duration_make(false, (uint32_t)subghz_protocol_hormann_const.te_short);
  98. //Send key data
  99. for(uint8_t i = instance->generic.data_count_bit; i > 0; i--) {
  100. if(bit_read(instance->generic.data, i - 1)) {
  101. //send bit 1
  102. instance->encoder.upload[index++] =
  103. level_duration_make(true, (uint32_t)subghz_protocol_hormann_const.te_long);
  104. instance->encoder.upload[index++] =
  105. level_duration_make(false, (uint32_t)subghz_protocol_hormann_const.te_short);
  106. } else {
  107. //send bit 0
  108. instance->encoder.upload[index++] =
  109. level_duration_make(true, (uint32_t)subghz_protocol_hormann_const.te_short);
  110. instance->encoder.upload[index++] =
  111. level_duration_make(false, (uint32_t)subghz_protocol_hormann_const.te_long);
  112. }
  113. }
  114. }
  115. instance->encoder.upload[index++] =
  116. level_duration_make(true, (uint32_t)subghz_protocol_hormann_const.te_short * 24);
  117. return true;
  118. }
  119. bool subghz_protocol_encoder_hormann_deserialize(void* context, FlipperFormat* flipper_format) {
  120. furi_assert(context);
  121. SubGhzProtocolEncoderHormann* instance = context;
  122. bool res = false;
  123. do {
  124. if(!subghz_block_generic_deserialize(&instance->generic, flipper_format)) {
  125. FURI_LOG_E(TAG, "Deserialize error");
  126. break;
  127. }
  128. if(instance->generic.data_count_bit !=
  129. subghz_protocol_hormann_const.min_count_bit_for_found) {
  130. FURI_LOG_E(TAG, "Wrong number of bits in key");
  131. break;
  132. }
  133. //optional parameter parameter
  134. flipper_format_read_uint32(
  135. flipper_format, "Repeat", (uint32_t*)&instance->encoder.repeat, 1);
  136. if(!subghz_protocol_encoder_hormann_get_upload(instance)) break;
  137. instance->encoder.is_running = true;
  138. res = true;
  139. } while(false);
  140. return res;
  141. }
  142. void subghz_protocol_encoder_hormann_stop(void* context) {
  143. SubGhzProtocolEncoderHormann* instance = context;
  144. instance->encoder.is_running = false;
  145. }
  146. LevelDuration subghz_protocol_encoder_hormann_yield(void* context) {
  147. SubGhzProtocolEncoderHormann* instance = context;
  148. if(instance->encoder.repeat == 0 || !instance->encoder.is_running) {
  149. instance->encoder.is_running = false;
  150. return level_duration_reset();
  151. }
  152. LevelDuration ret = instance->encoder.upload[instance->encoder.front];
  153. if(++instance->encoder.front == instance->encoder.size_upload) {
  154. instance->encoder.repeat--;
  155. instance->encoder.front = 0;
  156. }
  157. return ret;
  158. }
  159. void* subghz_protocol_decoder_hormann_alloc(SubGhzEnvironment* environment) {
  160. UNUSED(environment);
  161. SubGhzProtocolDecoderHormann* instance = malloc(sizeof(SubGhzProtocolDecoderHormann));
  162. instance->base.protocol = &subghz_protocol_hormann;
  163. instance->generic.protocol_name = instance->base.protocol->name;
  164. return instance;
  165. }
  166. void subghz_protocol_decoder_hormann_free(void* context) {
  167. furi_assert(context);
  168. SubGhzProtocolDecoderHormann* instance = context;
  169. free(instance);
  170. }
  171. static bool subghz_protocol_decoder_hormann_check_pattern(SubGhzProtocolDecoderHormann* instance) {
  172. return (instance->decoder.decode_data & HORMANN_HSM_PATTERN) == HORMANN_HSM_PATTERN;
  173. }
  174. void subghz_protocol_decoder_hormann_reset(void* context) {
  175. furi_assert(context);
  176. SubGhzProtocolDecoderHormann* instance = context;
  177. instance->decoder.parser_step = HormannDecoderStepReset;
  178. }
  179. void subghz_protocol_decoder_hormann_feed(void* context, bool level, uint32_t duration) {
  180. furi_assert(context);
  181. SubGhzProtocolDecoderHormann* instance = context;
  182. switch(instance->decoder.parser_step) {
  183. case HormannDecoderStepReset:
  184. if((level) && (DURATION_DIFF(duration, subghz_protocol_hormann_const.te_short * 24) <
  185. subghz_protocol_hormann_const.te_delta * 24)) {
  186. instance->decoder.parser_step = HormannDecoderStepFoundStartBit;
  187. }
  188. break;
  189. case HormannDecoderStepFoundStartBit:
  190. if((!level) && (DURATION_DIFF(duration, subghz_protocol_hormann_const.te_short) <
  191. subghz_protocol_hormann_const.te_delta)) {
  192. instance->decoder.parser_step = HormannDecoderStepSaveDuration;
  193. instance->decoder.decode_data = 0;
  194. instance->decoder.decode_count_bit = 0;
  195. } else {
  196. instance->decoder.parser_step = HormannDecoderStepReset;
  197. }
  198. break;
  199. case HormannDecoderStepSaveDuration:
  200. if(level) { //save interval
  201. if(duration >= (subghz_protocol_hormann_const.te_short * 5) &&
  202. subghz_protocol_decoder_hormann_check_pattern(instance)) {
  203. instance->decoder.parser_step = HormannDecoderStepFoundStartBit;
  204. if(instance->decoder.decode_count_bit >=
  205. subghz_protocol_hormann_const.min_count_bit_for_found) {
  206. instance->generic.data = instance->decoder.decode_data;
  207. instance->generic.data_count_bit = instance->decoder.decode_count_bit;
  208. if(instance->base.callback)
  209. instance->base.callback(&instance->base, instance->base.context);
  210. }
  211. break;
  212. }
  213. instance->decoder.te_last = duration;
  214. instance->decoder.parser_step = HormannDecoderStepCheckDuration;
  215. } else {
  216. instance->decoder.parser_step = HormannDecoderStepReset;
  217. }
  218. break;
  219. case HormannDecoderStepCheckDuration:
  220. if(!level) {
  221. if((DURATION_DIFF(instance->decoder.te_last, subghz_protocol_hormann_const.te_short) <
  222. subghz_protocol_hormann_const.te_delta) &&
  223. (DURATION_DIFF(duration, subghz_protocol_hormann_const.te_long) <
  224. subghz_protocol_hormann_const.te_delta)) {
  225. subghz_protocol_blocks_add_bit(&instance->decoder, 0);
  226. instance->decoder.parser_step = HormannDecoderStepSaveDuration;
  227. } else if(
  228. (DURATION_DIFF(instance->decoder.te_last, subghz_protocol_hormann_const.te_long) <
  229. subghz_protocol_hormann_const.te_delta) &&
  230. (DURATION_DIFF(duration, subghz_protocol_hormann_const.te_short) <
  231. subghz_protocol_hormann_const.te_delta)) {
  232. subghz_protocol_blocks_add_bit(&instance->decoder, 1);
  233. instance->decoder.parser_step = HormannDecoderStepSaveDuration;
  234. } else
  235. instance->decoder.parser_step = HormannDecoderStepReset;
  236. } else {
  237. instance->decoder.parser_step = HormannDecoderStepReset;
  238. }
  239. break;
  240. }
  241. }
  242. /**
  243. * Analysis of received data
  244. * @param instance Pointer to a SubGhzBlockGeneric* instance
  245. */
  246. static void subghz_protocol_hormann_check_remote_controller(SubGhzBlockGeneric* instance) {
  247. instance->btn = (instance->data >> 4) & 0xF;
  248. }
  249. uint8_t subghz_protocol_decoder_hormann_get_hash_data(void* context) {
  250. furi_assert(context);
  251. SubGhzProtocolDecoderHormann* instance = context;
  252. return subghz_protocol_blocks_get_hash_data(
  253. &instance->decoder, (instance->decoder.decode_count_bit / 8) + 1);
  254. }
  255. bool subghz_protocol_decoder_hormann_serialize(
  256. void* context,
  257. FlipperFormat* flipper_format,
  258. SubGhzRadioPreset* preset) {
  259. furi_assert(context);
  260. SubGhzProtocolDecoderHormann* instance = context;
  261. return subghz_block_generic_serialize(&instance->generic, flipper_format, preset);
  262. }
  263. bool subghz_protocol_decoder_hormann_deserialize(void* context, FlipperFormat* flipper_format) {
  264. furi_assert(context);
  265. SubGhzProtocolDecoderHormann* instance = context;
  266. bool ret = false;
  267. do {
  268. if(!subghz_block_generic_deserialize(&instance->generic, flipper_format)) {
  269. break;
  270. }
  271. if(instance->generic.data_count_bit !=
  272. subghz_protocol_hormann_const.min_count_bit_for_found) {
  273. FURI_LOG_E(TAG, "Wrong number of bits in key");
  274. break;
  275. }
  276. ret = true;
  277. } while(false);
  278. return ret;
  279. }
  280. void subghz_protocol_decoder_hormann_get_string(void* context, FuriString* output) {
  281. furi_assert(context);
  282. SubGhzProtocolDecoderHormann* instance = context;
  283. subghz_protocol_hormann_check_remote_controller(&instance->generic);
  284. furi_string_cat_printf(
  285. output,
  286. "%s\r\n"
  287. "%dbit\r\n"
  288. "Key:0x%03lX%08lX\r\n"
  289. "Btn:0x%01X\r\n",
  290. instance->generic.protocol_name,
  291. instance->generic.data_count_bit,
  292. (uint32_t)(instance->generic.data >> 32),
  293. (uint32_t)instance->generic.data,
  294. instance->generic.btn);
  295. }