hormann.c 13 KB

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