hormann.c 13 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. 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. static bool subghz_protocol_encoder_hormann_get_upload(SubGhzProtocolEncoderHormann* instance) {
  75. furi_assert(instance);
  76. size_t index = 0;
  77. size_t size_upload = 3 + (instance->generic.data_count_bit * 2 + 2) * 20 + 1;
  78. if(size_upload > instance->encoder.size_upload) {
  79. FURI_LOG_E(TAG, "Size upload exceeds allocated encoder buffer.");
  80. return false;
  81. } else {
  82. instance->encoder.size_upload = size_upload;
  83. }
  84. //Send header
  85. instance->encoder.upload[index++] =
  86. level_duration_make(false, (uint32_t)subghz_protocol_hormann_const.te_short * 64);
  87. instance->encoder.upload[index++] =
  88. level_duration_make(true, (uint32_t)subghz_protocol_hormann_const.te_short * 64);
  89. instance->encoder.upload[index++] =
  90. level_duration_make(false, (uint32_t)subghz_protocol_hormann_const.te_short * 64);
  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. //optional parameter parameter
  129. flipper_format_read_uint32(
  130. flipper_format, "Repeat", (uint32_t*)&instance->encoder.repeat, 1);
  131. subghz_protocol_encoder_hormann_get_upload(instance);
  132. instance->encoder.is_runing = true;
  133. res = true;
  134. } while(false);
  135. return res;
  136. }
  137. void subghz_protocol_encoder_hormann_stop(void* context) {
  138. SubGhzProtocolEncoderHormann* instance = context;
  139. instance->encoder.is_runing = false;
  140. }
  141. LevelDuration subghz_protocol_encoder_hormann_yield(void* context) {
  142. SubGhzProtocolEncoderHormann* instance = context;
  143. if(instance->encoder.repeat == 0 || !instance->encoder.is_runing) {
  144. instance->encoder.is_runing = false;
  145. return level_duration_reset();
  146. }
  147. LevelDuration ret = instance->encoder.upload[instance->encoder.front];
  148. if(++instance->encoder.front == instance->encoder.size_upload) {
  149. instance->encoder.repeat--;
  150. instance->encoder.front = 0;
  151. }
  152. return ret;
  153. }
  154. void* subghz_protocol_decoder_hormann_alloc(SubGhzEnvironment* environment) {
  155. SubGhzProtocolDecoderHormann* instance = malloc(sizeof(SubGhzProtocolDecoderHormann));
  156. instance->base.protocol = &subghz_protocol_hormann;
  157. instance->generic.protocol_name = instance->base.protocol->name;
  158. return instance;
  159. }
  160. void subghz_protocol_decoder_hormann_free(void* context) {
  161. furi_assert(context);
  162. SubGhzProtocolDecoderHormann* instance = context;
  163. free(instance);
  164. }
  165. void subghz_protocol_decoder_hormann_reset(void* context) {
  166. furi_assert(context);
  167. SubGhzProtocolDecoderHormann* instance = context;
  168. instance->decoder.parser_step = HormannDecoderStepReset;
  169. }
  170. void subghz_protocol_decoder_hormann_feed(void* context, bool level, uint32_t duration) {
  171. furi_assert(context);
  172. SubGhzProtocolDecoderHormann* instance = context;
  173. switch(instance->decoder.parser_step) {
  174. case HormannDecoderStepReset:
  175. if((level) && (DURATION_DIFF(duration, subghz_protocol_hormann_const.te_short * 64) <
  176. subghz_protocol_hormann_const.te_delta * 64)) {
  177. instance->decoder.parser_step = HormannDecoderStepFoundStartHeader;
  178. }
  179. break;
  180. case HormannDecoderStepFoundStartHeader:
  181. if((!level) && (DURATION_DIFF(duration, subghz_protocol_hormann_const.te_short * 64) <
  182. subghz_protocol_hormann_const.te_delta * 64)) {
  183. instance->decoder.parser_step = HormannDecoderStepFoundHeader;
  184. } else {
  185. instance->decoder.parser_step = HormannDecoderStepReset;
  186. }
  187. break;
  188. case HormannDecoderStepFoundHeader:
  189. if((level) && (DURATION_DIFF(duration, subghz_protocol_hormann_const.te_short * 24) <
  190. subghz_protocol_hormann_const.te_delta * 24)) {
  191. instance->decoder.parser_step = HormannDecoderStepFoundStartBit;
  192. } else {
  193. instance->decoder.parser_step = HormannDecoderStepReset;
  194. }
  195. break;
  196. case HormannDecoderStepFoundStartBit:
  197. if((!level) && (DURATION_DIFF(duration, subghz_protocol_hormann_const.te_short) <
  198. subghz_protocol_hormann_const.te_delta)) {
  199. instance->decoder.parser_step = HormannDecoderStepSaveDuration;
  200. instance->decoder.decode_data = 0;
  201. instance->decoder.decode_count_bit = 0;
  202. } else {
  203. instance->decoder.parser_step = HormannDecoderStepReset;
  204. }
  205. break;
  206. case HormannDecoderStepSaveDuration:
  207. if(level) { //save interval
  208. if(duration >= (subghz_protocol_hormann_const.te_short * 5)) {
  209. instance->decoder.parser_step = HormannDecoderStepFoundStartBit;
  210. if(instance->decoder.decode_count_bit >=
  211. subghz_protocol_hormann_const.min_count_bit_for_found) {
  212. instance->generic.data = instance->decoder.decode_data;
  213. instance->generic.data_count_bit = instance->decoder.decode_count_bit;
  214. if(instance->base.callback)
  215. instance->base.callback(&instance->base, instance->base.context);
  216. }
  217. break;
  218. }
  219. instance->decoder.te_last = duration;
  220. instance->decoder.parser_step = HormannDecoderStepCheckDuration;
  221. } else {
  222. instance->decoder.parser_step = HormannDecoderStepReset;
  223. }
  224. break;
  225. case HormannDecoderStepCheckDuration:
  226. if(!level) {
  227. if((DURATION_DIFF(instance->decoder.te_last, subghz_protocol_hormann_const.te_short) <
  228. subghz_protocol_hormann_const.te_delta) &&
  229. (DURATION_DIFF(duration, subghz_protocol_hormann_const.te_long) <
  230. subghz_protocol_hormann_const.te_delta)) {
  231. subghz_protocol_blocks_add_bit(&instance->decoder, 0);
  232. instance->decoder.parser_step = HormannDecoderStepSaveDuration;
  233. } else if(
  234. (DURATION_DIFF(instance->decoder.te_last, subghz_protocol_hormann_const.te_long) <
  235. subghz_protocol_hormann_const.te_delta) &&
  236. (DURATION_DIFF(duration, subghz_protocol_hormann_const.te_short) <
  237. subghz_protocol_hormann_const.te_delta)) {
  238. subghz_protocol_blocks_add_bit(&instance->decoder, 1);
  239. instance->decoder.parser_step = HormannDecoderStepSaveDuration;
  240. } else
  241. instance->decoder.parser_step = HormannDecoderStepReset;
  242. } else {
  243. instance->decoder.parser_step = HormannDecoderStepReset;
  244. }
  245. break;
  246. }
  247. }
  248. static void subghz_protocol_hormann_check_remote_controller(SubGhzBlockGeneric* instance) {
  249. instance->btn = (instance->data >> 4) & 0xF;
  250. }
  251. uint8_t subghz_protocol_decoder_hormann_get_hash_data(void* context) {
  252. furi_assert(context);
  253. SubGhzProtocolDecoderHormann* instance = context;
  254. return subghz_protocol_blocks_get_hash_data(
  255. &instance->decoder, (instance->decoder.decode_count_bit / 8) + 1);
  256. }
  257. bool subghz_protocol_decoder_hormann_serialize(
  258. void* context,
  259. FlipperFormat* flipper_format,
  260. uint32_t frequency,
  261. FuriHalSubGhzPreset preset) {
  262. furi_assert(context);
  263. SubGhzProtocolDecoderHormann* instance = context;
  264. return subghz_block_generic_serialize(&instance->generic, flipper_format, frequency, preset);
  265. }
  266. bool subghz_protocol_decoder_hormann_deserialize(void* context, FlipperFormat* flipper_format) {
  267. furi_assert(context);
  268. SubGhzProtocolDecoderHormann* instance = context;
  269. return subghz_block_generic_deserialize(&instance->generic, flipper_format);
  270. }
  271. void subghz_protocol_decoder_hormann_get_string(void* context, string_t output) {
  272. furi_assert(context);
  273. SubGhzProtocolDecoderHormann* instance = context;
  274. subghz_protocol_hormann_check_remote_controller(&instance->generic);
  275. 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. }