hormann.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  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. SubGhzProtocolStatus
  120. subghz_protocol_encoder_hormann_deserialize(void* context, FlipperFormat* flipper_format) {
  121. furi_assert(context);
  122. SubGhzProtocolEncoderHormann* instance = context;
  123. SubGhzProtocolStatus ret = SubGhzProtocolStatusError;
  124. do {
  125. ret = subghz_block_generic_deserialize_check_count_bit(
  126. &instance->generic,
  127. flipper_format,
  128. subghz_protocol_hormann_const.min_count_bit_for_found);
  129. if(ret != SubGhzProtocolStatusOk) {
  130. break;
  131. }
  132. //optional parameter parameter
  133. flipper_format_read_uint32(
  134. flipper_format, "Repeat", (uint32_t*)&instance->encoder.repeat, 1);
  135. if(!subghz_protocol_encoder_hormann_get_upload(instance)) {
  136. ret = SubGhzProtocolStatusErrorEncoderGetUpload;
  137. break;
  138. }
  139. instance->encoder.is_running = true;
  140. } while(false);
  141. return ret;
  142. }
  143. void subghz_protocol_encoder_hormann_stop(void* context) {
  144. SubGhzProtocolEncoderHormann* instance = context;
  145. instance->encoder.is_running = false;
  146. }
  147. LevelDuration subghz_protocol_encoder_hormann_yield(void* context) {
  148. SubGhzProtocolEncoderHormann* instance = context;
  149. if(instance->encoder.repeat == 0 || !instance->encoder.is_running) {
  150. instance->encoder.is_running = false;
  151. return level_duration_reset();
  152. }
  153. LevelDuration ret = instance->encoder.upload[instance->encoder.front];
  154. if(++instance->encoder.front == instance->encoder.size_upload) {
  155. instance->encoder.repeat--;
  156. instance->encoder.front = 0;
  157. }
  158. return ret;
  159. }
  160. void* subghz_protocol_decoder_hormann_alloc(SubGhzEnvironment* environment) {
  161. UNUSED(environment);
  162. SubGhzProtocolDecoderHormann* instance = malloc(sizeof(SubGhzProtocolDecoderHormann));
  163. instance->base.protocol = &subghz_protocol_hormann;
  164. instance->generic.protocol_name = instance->base.protocol->name;
  165. return instance;
  166. }
  167. void subghz_protocol_decoder_hormann_free(void* context) {
  168. furi_assert(context);
  169. SubGhzProtocolDecoderHormann* instance = context;
  170. free(instance);
  171. }
  172. static bool subghz_protocol_decoder_hormann_check_pattern(SubGhzProtocolDecoderHormann* instance) {
  173. return (instance->decoder.decode_data & HORMANN_HSM_PATTERN) == HORMANN_HSM_PATTERN;
  174. }
  175. void subghz_protocol_decoder_hormann_reset(void* context) {
  176. furi_assert(context);
  177. SubGhzProtocolDecoderHormann* instance = context;
  178. instance->decoder.parser_step = HormannDecoderStepReset;
  179. }
  180. void subghz_protocol_decoder_hormann_feed(void* context, bool level, uint32_t duration) {
  181. furi_assert(context);
  182. SubGhzProtocolDecoderHormann* instance = context;
  183. switch(instance->decoder.parser_step) {
  184. case HormannDecoderStepReset:
  185. if((level) && (DURATION_DIFF(duration, subghz_protocol_hormann_const.te_short * 24) <
  186. subghz_protocol_hormann_const.te_delta * 24)) {
  187. instance->decoder.parser_step = HormannDecoderStepFoundStartBit;
  188. }
  189. break;
  190. case HormannDecoderStepFoundStartBit:
  191. if((!level) && (DURATION_DIFF(duration, subghz_protocol_hormann_const.te_short) <
  192. subghz_protocol_hormann_const.te_delta)) {
  193. instance->decoder.parser_step = HormannDecoderStepSaveDuration;
  194. instance->decoder.decode_data = 0;
  195. instance->decoder.decode_count_bit = 0;
  196. } else {
  197. instance->decoder.parser_step = HormannDecoderStepReset;
  198. }
  199. break;
  200. case HormannDecoderStepSaveDuration:
  201. if(level) { //save interval
  202. if(duration >= (subghz_protocol_hormann_const.te_short * 5) &&
  203. subghz_protocol_decoder_hormann_check_pattern(instance)) {
  204. instance->decoder.parser_step = HormannDecoderStepFoundStartBit;
  205. if(instance->decoder.decode_count_bit >=
  206. subghz_protocol_hormann_const.min_count_bit_for_found) {
  207. instance->generic.data = instance->decoder.decode_data;
  208. instance->generic.data_count_bit = instance->decoder.decode_count_bit;
  209. if(instance->base.callback)
  210. instance->base.callback(&instance->base, instance->base.context);
  211. }
  212. break;
  213. }
  214. instance->decoder.te_last = duration;
  215. instance->decoder.parser_step = HormannDecoderStepCheckDuration;
  216. } else {
  217. instance->decoder.parser_step = HormannDecoderStepReset;
  218. }
  219. break;
  220. case HormannDecoderStepCheckDuration:
  221. if(!level) {
  222. if((DURATION_DIFF(instance->decoder.te_last, subghz_protocol_hormann_const.te_short) <
  223. subghz_protocol_hormann_const.te_delta) &&
  224. (DURATION_DIFF(duration, subghz_protocol_hormann_const.te_long) <
  225. subghz_protocol_hormann_const.te_delta)) {
  226. subghz_protocol_blocks_add_bit(&instance->decoder, 0);
  227. instance->decoder.parser_step = HormannDecoderStepSaveDuration;
  228. } else if(
  229. (DURATION_DIFF(instance->decoder.te_last, subghz_protocol_hormann_const.te_long) <
  230. subghz_protocol_hormann_const.te_delta) &&
  231. (DURATION_DIFF(duration, subghz_protocol_hormann_const.te_short) <
  232. subghz_protocol_hormann_const.te_delta)) {
  233. subghz_protocol_blocks_add_bit(&instance->decoder, 1);
  234. instance->decoder.parser_step = HormannDecoderStepSaveDuration;
  235. } else
  236. instance->decoder.parser_step = HormannDecoderStepReset;
  237. } else {
  238. instance->decoder.parser_step = HormannDecoderStepReset;
  239. }
  240. break;
  241. }
  242. }
  243. /**
  244. * Analysis of received data
  245. * @param instance Pointer to a SubGhzBlockGeneric* instance
  246. */
  247. static void subghz_protocol_hormann_check_remote_controller(SubGhzBlockGeneric* instance) {
  248. instance->btn = (instance->data >> 4) & 0xF;
  249. }
  250. uint8_t subghz_protocol_decoder_hormann_get_hash_data(void* context) {
  251. furi_assert(context);
  252. SubGhzProtocolDecoderHormann* instance = context;
  253. return subghz_protocol_blocks_get_hash_data(
  254. &instance->decoder, (instance->decoder.decode_count_bit / 8) + 1);
  255. }
  256. SubGhzProtocolStatus subghz_protocol_decoder_hormann_serialize(
  257. void* context,
  258. FlipperFormat* flipper_format,
  259. SubGhzRadioPreset* preset) {
  260. furi_assert(context);
  261. SubGhzProtocolDecoderHormann* instance = context;
  262. return subghz_block_generic_serialize(&instance->generic, flipper_format, preset);
  263. }
  264. SubGhzProtocolStatus
  265. subghz_protocol_decoder_hormann_deserialize(void* context, FlipperFormat* flipper_format) {
  266. furi_assert(context);
  267. SubGhzProtocolDecoderHormann* instance = context;
  268. return subghz_block_generic_deserialize_check_count_bit(
  269. &instance->generic, flipper_format, subghz_protocol_hormann_const.min_count_bit_for_found);
  270. }
  271. void subghz_protocol_decoder_hormann_get_string(void* context, FuriString* output) {
  272. furi_assert(context);
  273. SubGhzProtocolDecoderHormann* instance = context;
  274. subghz_protocol_hormann_check_remote_controller(&instance->generic);
  275. furi_string_cat_printf(
  276. output,
  277. "%s\r\n"
  278. "%dbit\r\n"
  279. "Key:0x%03lX%08lX\r\n"
  280. "Btn:0x%01X\r\n",
  281. instance->generic.protocol_name,
  282. instance->generic.data_count_bit,
  283. (uint32_t)(instance->generic.data >> 32),
  284. (uint32_t)instance->generic.data,
  285. instance->generic.btn);
  286. }