hormann.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  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. UNUSED(environment);
  60. SubGhzProtocolEncoderHormann* instance = malloc(sizeof(SubGhzProtocolEncoderHormann));
  61. instance->base.protocol = &subghz_protocol_hormann;
  62. instance->generic.protocol_name = instance->base.protocol->name;
  63. instance->encoder.repeat = 10;
  64. instance->encoder.size_upload = 2048;
  65. instance->encoder.upload = malloc(instance->encoder.size_upload * sizeof(LevelDuration));
  66. instance->encoder.is_runing = false;
  67. return instance;
  68. }
  69. void subghz_protocol_encoder_hormann_free(void* context) {
  70. furi_assert(context);
  71. SubGhzProtocolEncoderHormann* instance = context;
  72. free(instance->encoder.upload);
  73. free(instance);
  74. }
  75. /**
  76. * Generating an upload from data.
  77. * @param instance Pointer to a SubGhzProtocolEncoderHormann instance
  78. * @return true On success
  79. */
  80. static bool subghz_protocol_encoder_hormann_get_upload(SubGhzProtocolEncoderHormann* instance) {
  81. furi_assert(instance);
  82. size_t index = 0;
  83. size_t size_upload = 3 + (instance->generic.data_count_bit * 2 + 2) * 20 + 1;
  84. if(size_upload > instance->encoder.size_upload) {
  85. FURI_LOG_E(TAG, "Size upload exceeds allocated encoder buffer.");
  86. return false;
  87. } else {
  88. instance->encoder.size_upload = size_upload;
  89. }
  90. //Send header
  91. instance->encoder.upload[index++] =
  92. level_duration_make(false, (uint32_t)subghz_protocol_hormann_const.te_short * 64);
  93. instance->encoder.upload[index++] =
  94. level_duration_make(true, (uint32_t)subghz_protocol_hormann_const.te_short * 64);
  95. instance->encoder.upload[index++] =
  96. level_duration_make(false, (uint32_t)subghz_protocol_hormann_const.te_short * 64);
  97. instance->encoder.repeat = 10; //original remote does 10 repeats
  98. for(size_t repeat = 0; repeat < 20; repeat++) {
  99. //Send start bit
  100. instance->encoder.upload[index++] =
  101. level_duration_make(true, (uint32_t)subghz_protocol_hormann_const.te_short * 24);
  102. instance->encoder.upload[index++] =
  103. level_duration_make(false, (uint32_t)subghz_protocol_hormann_const.te_short);
  104. //Send key data
  105. for(uint8_t i = instance->generic.data_count_bit; i > 0; i--) {
  106. if(bit_read(instance->generic.data, i - 1)) {
  107. //send bit 1
  108. instance->encoder.upload[index++] =
  109. level_duration_make(true, (uint32_t)subghz_protocol_hormann_const.te_long);
  110. instance->encoder.upload[index++] =
  111. level_duration_make(false, (uint32_t)subghz_protocol_hormann_const.te_short);
  112. } else {
  113. //send bit 0
  114. instance->encoder.upload[index++] =
  115. level_duration_make(true, (uint32_t)subghz_protocol_hormann_const.te_short);
  116. instance->encoder.upload[index++] =
  117. level_duration_make(false, (uint32_t)subghz_protocol_hormann_const.te_long);
  118. }
  119. }
  120. }
  121. instance->encoder.upload[index++] =
  122. level_duration_make(true, (uint32_t)subghz_protocol_hormann_const.te_short * 24);
  123. return true;
  124. }
  125. bool subghz_protocol_encoder_hormann_deserialize(void* context, FlipperFormat* flipper_format) {
  126. furi_assert(context);
  127. SubGhzProtocolEncoderHormann* instance = context;
  128. bool res = false;
  129. do {
  130. if(!subghz_block_generic_deserialize(&instance->generic, flipper_format)) {
  131. FURI_LOG_E(TAG, "Deserialize error");
  132. break;
  133. }
  134. //optional parameter parameter
  135. flipper_format_read_uint32(
  136. flipper_format, "Repeat", (uint32_t*)&instance->encoder.repeat, 1);
  137. subghz_protocol_encoder_hormann_get_upload(instance);
  138. instance->encoder.is_runing = true;
  139. res = true;
  140. } while(false);
  141. return res;
  142. }
  143. void subghz_protocol_encoder_hormann_stop(void* context) {
  144. SubGhzProtocolEncoderHormann* instance = context;
  145. instance->encoder.is_runing = false;
  146. }
  147. LevelDuration subghz_protocol_encoder_hormann_yield(void* context) {
  148. SubGhzProtocolEncoderHormann* instance = context;
  149. if(instance->encoder.repeat == 0 || !instance->encoder.is_runing) {
  150. instance->encoder.is_runing = 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. void subghz_protocol_decoder_hormann_reset(void* context) {
  173. furi_assert(context);
  174. SubGhzProtocolDecoderHormann* instance = context;
  175. instance->decoder.parser_step = HormannDecoderStepReset;
  176. }
  177. void subghz_protocol_decoder_hormann_feed(void* context, bool level, uint32_t duration) {
  178. furi_assert(context);
  179. SubGhzProtocolDecoderHormann* instance = context;
  180. switch(instance->decoder.parser_step) {
  181. case HormannDecoderStepReset:
  182. if((level) && (DURATION_DIFF(duration, subghz_protocol_hormann_const.te_short * 64) <
  183. subghz_protocol_hormann_const.te_delta * 64)) {
  184. instance->decoder.parser_step = HormannDecoderStepFoundStartHeader;
  185. }
  186. break;
  187. case HormannDecoderStepFoundStartHeader:
  188. if((!level) && (DURATION_DIFF(duration, subghz_protocol_hormann_const.te_short * 64) <
  189. subghz_protocol_hormann_const.te_delta * 64)) {
  190. instance->decoder.parser_step = HormannDecoderStepFoundHeader;
  191. } else {
  192. instance->decoder.parser_step = HormannDecoderStepReset;
  193. }
  194. break;
  195. case HormannDecoderStepFoundHeader:
  196. if((level) && (DURATION_DIFF(duration, subghz_protocol_hormann_const.te_short * 24) <
  197. subghz_protocol_hormann_const.te_delta * 24)) {
  198. instance->decoder.parser_step = HormannDecoderStepFoundStartBit;
  199. } else {
  200. instance->decoder.parser_step = HormannDecoderStepReset;
  201. }
  202. break;
  203. case HormannDecoderStepFoundStartBit:
  204. if((!level) && (DURATION_DIFF(duration, subghz_protocol_hormann_const.te_short) <
  205. subghz_protocol_hormann_const.te_delta)) {
  206. instance->decoder.parser_step = HormannDecoderStepSaveDuration;
  207. instance->decoder.decode_data = 0;
  208. instance->decoder.decode_count_bit = 0;
  209. } else {
  210. instance->decoder.parser_step = HormannDecoderStepReset;
  211. }
  212. break;
  213. case HormannDecoderStepSaveDuration:
  214. if(level) { //save interval
  215. if(duration >= (subghz_protocol_hormann_const.te_short * 5)) {
  216. instance->decoder.parser_step = HormannDecoderStepFoundStartBit;
  217. if(instance->decoder.decode_count_bit >=
  218. subghz_protocol_hormann_const.min_count_bit_for_found) {
  219. instance->generic.data = instance->decoder.decode_data;
  220. instance->generic.data_count_bit = instance->decoder.decode_count_bit;
  221. if(instance->base.callback)
  222. instance->base.callback(&instance->base, instance->base.context);
  223. }
  224. break;
  225. }
  226. instance->decoder.te_last = duration;
  227. instance->decoder.parser_step = HormannDecoderStepCheckDuration;
  228. } else {
  229. instance->decoder.parser_step = HormannDecoderStepReset;
  230. }
  231. break;
  232. case HormannDecoderStepCheckDuration:
  233. if(!level) {
  234. if((DURATION_DIFF(instance->decoder.te_last, subghz_protocol_hormann_const.te_short) <
  235. subghz_protocol_hormann_const.te_delta) &&
  236. (DURATION_DIFF(duration, subghz_protocol_hormann_const.te_long) <
  237. subghz_protocol_hormann_const.te_delta)) {
  238. subghz_protocol_blocks_add_bit(&instance->decoder, 0);
  239. instance->decoder.parser_step = HormannDecoderStepSaveDuration;
  240. } else if(
  241. (DURATION_DIFF(instance->decoder.te_last, subghz_protocol_hormann_const.te_long) <
  242. subghz_protocol_hormann_const.te_delta) &&
  243. (DURATION_DIFF(duration, subghz_protocol_hormann_const.te_short) <
  244. subghz_protocol_hormann_const.te_delta)) {
  245. subghz_protocol_blocks_add_bit(&instance->decoder, 1);
  246. instance->decoder.parser_step = HormannDecoderStepSaveDuration;
  247. } else
  248. instance->decoder.parser_step = HormannDecoderStepReset;
  249. } else {
  250. instance->decoder.parser_step = HormannDecoderStepReset;
  251. }
  252. break;
  253. }
  254. }
  255. /**
  256. * Analysis of received data
  257. * @param instance Pointer to a SubGhzBlockGeneric* instance
  258. */
  259. static void subghz_protocol_hormann_check_remote_controller(SubGhzBlockGeneric* instance) {
  260. instance->btn = (instance->data >> 4) & 0xF;
  261. }
  262. uint8_t subghz_protocol_decoder_hormann_get_hash_data(void* context) {
  263. furi_assert(context);
  264. SubGhzProtocolDecoderHormann* instance = context;
  265. return subghz_protocol_blocks_get_hash_data(
  266. &instance->decoder, (instance->decoder.decode_count_bit / 8) + 1);
  267. }
  268. bool subghz_protocol_decoder_hormann_serialize(
  269. void* context,
  270. FlipperFormat* flipper_format,
  271. uint32_t frequency,
  272. FuriHalSubGhzPreset preset) {
  273. furi_assert(context);
  274. SubGhzProtocolDecoderHormann* instance = context;
  275. return subghz_block_generic_serialize(&instance->generic, flipper_format, frequency, preset);
  276. }
  277. bool subghz_protocol_decoder_hormann_deserialize(void* context, FlipperFormat* flipper_format) {
  278. furi_assert(context);
  279. SubGhzProtocolDecoderHormann* instance = context;
  280. return subghz_block_generic_deserialize(&instance->generic, flipper_format);
  281. }
  282. void subghz_protocol_decoder_hormann_get_string(void* context, string_t output) {
  283. furi_assert(context);
  284. SubGhzProtocolDecoderHormann* instance = context;
  285. subghz_protocol_hormann_check_remote_controller(&instance->generic);
  286. string_cat_printf(
  287. output,
  288. "%s\r\n"
  289. "%dbit\r\n"
  290. "Key:0x%03lX%08lX\r\n"
  291. "Btn:0x%01X\r\n",
  292. instance->generic.protocol_name,
  293. instance->generic.data_count_bit,
  294. (uint32_t)(instance->generic.data >> 32),
  295. (uint32_t)instance->generic.data,
  296. instance->generic.btn);
  297. }