hormann.c 13 KB

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