ansonic.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  1. #include "ansonic.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 "SubGhzProtocolAnsonic"
  8. #define DIP_PATTERN "%c%c%c%c%c%c%c%c%c%c"
  9. #define CNT_TO_DIP(dip) \
  10. (dip & 0x0800 ? '1' : '0'), (dip & 0x0400 ? '1' : '0'), (dip & 0x0200 ? '1' : '0'), \
  11. (dip & 0x0100 ? '1' : '0'), (dip & 0x0080 ? '1' : '0'), (dip & 0x0040 ? '1' : '0'), \
  12. (dip & 0x0020 ? '1' : '0'), (dip & 0x0010 ? '1' : '0'), (dip & 0x0001 ? '1' : '0'), \
  13. (dip & 0x0008 ? '1' : '0')
  14. static const SubGhzBlockConst subghz_protocol_ansonic_const = {
  15. .te_short = 555,
  16. .te_long = 1111,
  17. .te_delta = 120,
  18. .min_count_bit_for_found = 12,
  19. };
  20. struct SubGhzProtocolDecoderAnsonic {
  21. SubGhzProtocolDecoderBase base;
  22. SubGhzBlockDecoder decoder;
  23. SubGhzBlockGeneric generic;
  24. };
  25. struct SubGhzProtocolEncoderAnsonic {
  26. SubGhzProtocolEncoderBase base;
  27. SubGhzProtocolBlockEncoder encoder;
  28. SubGhzBlockGeneric generic;
  29. };
  30. typedef enum {
  31. AnsonicDecoderStepReset = 0,
  32. AnsonicDecoderStepFoundStartBit,
  33. AnsonicDecoderStepSaveDuration,
  34. AnsonicDecoderStepCheckDuration,
  35. } AnsonicDecoderStep;
  36. const SubGhzProtocolDecoder subghz_protocol_ansonic_decoder = {
  37. .alloc = subghz_protocol_decoder_ansonic_alloc,
  38. .free = subghz_protocol_decoder_ansonic_free,
  39. .feed = subghz_protocol_decoder_ansonic_feed,
  40. .reset = subghz_protocol_decoder_ansonic_reset,
  41. .get_hash_data = subghz_protocol_decoder_ansonic_get_hash_data,
  42. .serialize = subghz_protocol_decoder_ansonic_serialize,
  43. .deserialize = subghz_protocol_decoder_ansonic_deserialize,
  44. .get_string = subghz_protocol_decoder_ansonic_get_string,
  45. };
  46. const SubGhzProtocolEncoder subghz_protocol_ansonic_encoder = {
  47. .alloc = subghz_protocol_encoder_ansonic_alloc,
  48. .free = subghz_protocol_encoder_ansonic_free,
  49. .deserialize = subghz_protocol_encoder_ansonic_deserialize,
  50. .stop = subghz_protocol_encoder_ansonic_stop,
  51. .yield = subghz_protocol_encoder_ansonic_yield,
  52. };
  53. const SubGhzProtocol subghz_protocol_ansonic = {
  54. .name = SUBGHZ_PROTOCOL_ANSONIC_NAME,
  55. .type = SubGhzProtocolTypeStatic,
  56. .flag = SubGhzProtocolFlag_433 | SubGhzProtocolFlag_315 | SubGhzProtocolFlag_FM |
  57. SubGhzProtocolFlag_Decodable | SubGhzProtocolFlag_Load | SubGhzProtocolFlag_Save |
  58. SubGhzProtocolFlag_Send,
  59. .decoder = &subghz_protocol_ansonic_decoder,
  60. .encoder = &subghz_protocol_ansonic_encoder,
  61. };
  62. void* subghz_protocol_encoder_ansonic_alloc(SubGhzEnvironment* environment) {
  63. UNUSED(environment);
  64. SubGhzProtocolEncoderAnsonic* instance = malloc(sizeof(SubGhzProtocolEncoderAnsonic));
  65. instance->base.protocol = &subghz_protocol_ansonic;
  66. instance->generic.protocol_name = instance->base.protocol->name;
  67. instance->encoder.repeat = 10;
  68. instance->encoder.size_upload = 52;
  69. instance->encoder.upload = malloc(instance->encoder.size_upload * sizeof(LevelDuration));
  70. instance->encoder.is_running = false;
  71. return instance;
  72. }
  73. void subghz_protocol_encoder_ansonic_free(void* context) {
  74. furi_assert(context);
  75. SubGhzProtocolEncoderAnsonic* instance = context;
  76. free(instance->encoder.upload);
  77. free(instance);
  78. }
  79. /**
  80. * Generating an upload from data.
  81. * @param instance Pointer to a SubGhzProtocolEncoderAnsonic instance
  82. * @return true On success
  83. */
  84. static bool subghz_protocol_encoder_ansonic_get_upload(SubGhzProtocolEncoderAnsonic* instance) {
  85. furi_assert(instance);
  86. size_t index = 0;
  87. size_t size_upload = (instance->generic.data_count_bit * 2) + 2;
  88. if(size_upload > instance->encoder.size_upload) {
  89. FURI_LOG_E(TAG, "Size upload exceeds allocated encoder buffer.");
  90. return false;
  91. } else {
  92. instance->encoder.size_upload = size_upload;
  93. }
  94. //Send header
  95. instance->encoder.upload[index++] =
  96. level_duration_make(false, (uint32_t)subghz_protocol_ansonic_const.te_short * 35);
  97. //Send start bit
  98. instance->encoder.upload[index++] =
  99. level_duration_make(true, (uint32_t)subghz_protocol_ansonic_const.te_short);
  100. //Send key data
  101. for(uint8_t i = instance->generic.data_count_bit; i > 0; i--) {
  102. if(bit_read(instance->generic.data, i - 1)) {
  103. //send bit 1
  104. instance->encoder.upload[index++] =
  105. level_duration_make(false, (uint32_t)subghz_protocol_ansonic_const.te_short);
  106. instance->encoder.upload[index++] =
  107. level_duration_make(true, (uint32_t)subghz_protocol_ansonic_const.te_long);
  108. } else {
  109. //send bit 0
  110. instance->encoder.upload[index++] =
  111. level_duration_make(false, (uint32_t)subghz_protocol_ansonic_const.te_long);
  112. instance->encoder.upload[index++] =
  113. level_duration_make(true, (uint32_t)subghz_protocol_ansonic_const.te_short);
  114. }
  115. }
  116. return true;
  117. }
  118. bool subghz_protocol_encoder_ansonic_deserialize(void* context, FlipperFormat* flipper_format) {
  119. furi_assert(context);
  120. SubGhzProtocolEncoderAnsonic* instance = context;
  121. bool res = false;
  122. do {
  123. if(!subghz_block_generic_deserialize(&instance->generic, flipper_format)) {
  124. FURI_LOG_E(TAG, "Deserialize error");
  125. break;
  126. }
  127. if(instance->generic.data_count_bit !=
  128. subghz_protocol_ansonic_const.min_count_bit_for_found) {
  129. FURI_LOG_E(TAG, "Wrong number of bits in key");
  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_ansonic_get_upload(instance)) break;
  136. instance->encoder.is_running = true;
  137. res = true;
  138. } while(false);
  139. return res;
  140. }
  141. void subghz_protocol_encoder_ansonic_stop(void* context) {
  142. SubGhzProtocolEncoderAnsonic* instance = context;
  143. instance->encoder.is_running = false;
  144. }
  145. LevelDuration subghz_protocol_encoder_ansonic_yield(void* context) {
  146. SubGhzProtocolEncoderAnsonic* instance = context;
  147. if(instance->encoder.repeat == 0 || !instance->encoder.is_running) {
  148. instance->encoder.is_running = false;
  149. return level_duration_reset();
  150. }
  151. LevelDuration ret = instance->encoder.upload[instance->encoder.front];
  152. if(++instance->encoder.front == instance->encoder.size_upload) {
  153. instance->encoder.repeat--;
  154. instance->encoder.front = 0;
  155. }
  156. return ret;
  157. }
  158. void* subghz_protocol_decoder_ansonic_alloc(SubGhzEnvironment* environment) {
  159. UNUSED(environment);
  160. SubGhzProtocolDecoderAnsonic* instance = malloc(sizeof(SubGhzProtocolDecoderAnsonic));
  161. instance->base.protocol = &subghz_protocol_ansonic;
  162. instance->generic.protocol_name = instance->base.protocol->name;
  163. return instance;
  164. }
  165. void subghz_protocol_decoder_ansonic_free(void* context) {
  166. furi_assert(context);
  167. SubGhzProtocolDecoderAnsonic* instance = context;
  168. free(instance);
  169. }
  170. void subghz_protocol_decoder_ansonic_reset(void* context) {
  171. furi_assert(context);
  172. SubGhzProtocolDecoderAnsonic* instance = context;
  173. instance->decoder.parser_step = AnsonicDecoderStepReset;
  174. }
  175. void subghz_protocol_decoder_ansonic_feed(void* context, bool level, uint32_t duration) {
  176. furi_assert(context);
  177. SubGhzProtocolDecoderAnsonic* instance = context;
  178. switch(instance->decoder.parser_step) {
  179. case AnsonicDecoderStepReset:
  180. if((!level) && (DURATION_DIFF(duration, subghz_protocol_ansonic_const.te_short * 35) <
  181. subghz_protocol_ansonic_const.te_delta * 35)) {
  182. //Found header Ansonic
  183. instance->decoder.parser_step = AnsonicDecoderStepFoundStartBit;
  184. }
  185. break;
  186. case AnsonicDecoderStepFoundStartBit:
  187. if(!level) {
  188. break;
  189. } else if(
  190. DURATION_DIFF(duration, subghz_protocol_ansonic_const.te_short) <
  191. subghz_protocol_ansonic_const.te_delta) {
  192. //Found start bit Ansonic
  193. instance->decoder.parser_step = AnsonicDecoderStepSaveDuration;
  194. instance->decoder.decode_data = 0;
  195. instance->decoder.decode_count_bit = 0;
  196. } else {
  197. instance->decoder.parser_step = AnsonicDecoderStepReset;
  198. }
  199. break;
  200. case AnsonicDecoderStepSaveDuration:
  201. if(!level) { //save interval
  202. if(duration >= (subghz_protocol_ansonic_const.te_short * 4)) {
  203. instance->decoder.parser_step = AnsonicDecoderStepFoundStartBit;
  204. if(instance->decoder.decode_count_bit >=
  205. subghz_protocol_ansonic_const.min_count_bit_for_found) {
  206. instance->generic.serial = 0x0;
  207. instance->generic.btn = 0x0;
  208. instance->generic.data = instance->decoder.decode_data;
  209. instance->generic.data_count_bit = instance->decoder.decode_count_bit;
  210. if(instance->base.callback)
  211. instance->base.callback(&instance->base, instance->base.context);
  212. }
  213. break;
  214. }
  215. instance->decoder.te_last = duration;
  216. instance->decoder.parser_step = AnsonicDecoderStepCheckDuration;
  217. } else {
  218. instance->decoder.parser_step = AnsonicDecoderStepReset;
  219. }
  220. break;
  221. case AnsonicDecoderStepCheckDuration:
  222. if(level) {
  223. if((DURATION_DIFF(instance->decoder.te_last, subghz_protocol_ansonic_const.te_short) <
  224. subghz_protocol_ansonic_const.te_delta) &&
  225. (DURATION_DIFF(duration, subghz_protocol_ansonic_const.te_long) <
  226. subghz_protocol_ansonic_const.te_delta)) {
  227. subghz_protocol_blocks_add_bit(&instance->decoder, 1);
  228. instance->decoder.parser_step = AnsonicDecoderStepSaveDuration;
  229. } else if(
  230. (DURATION_DIFF(instance->decoder.te_last, subghz_protocol_ansonic_const.te_long) <
  231. subghz_protocol_ansonic_const.te_delta) &&
  232. (DURATION_DIFF(duration, subghz_protocol_ansonic_const.te_short) <
  233. subghz_protocol_ansonic_const.te_delta)) {
  234. subghz_protocol_blocks_add_bit(&instance->decoder, 0);
  235. instance->decoder.parser_step = AnsonicDecoderStepSaveDuration;
  236. } else
  237. instance->decoder.parser_step = AnsonicDecoderStepReset;
  238. } else {
  239. instance->decoder.parser_step = AnsonicDecoderStepReset;
  240. }
  241. break;
  242. }
  243. }
  244. /**
  245. * Analysis of received data
  246. * @param instance Pointer to a SubGhzBlockGeneric* instance
  247. */
  248. static void subghz_protocol_ansonic_check_remote_controller(SubGhzBlockGeneric* instance) {
  249. /*
  250. * 12345678(10) k 9
  251. * AAA => 10101010 1 01 0
  252. *
  253. * 1...10 - DIP
  254. * k- KEY
  255. */
  256. instance->cnt = instance->data & 0xFFF;
  257. instance->btn = ((instance->data >> 1) & 0x3);
  258. }
  259. uint8_t subghz_protocol_decoder_ansonic_get_hash_data(void* context) {
  260. furi_assert(context);
  261. SubGhzProtocolDecoderAnsonic* instance = context;
  262. return subghz_protocol_blocks_get_hash_data(
  263. &instance->decoder, (instance->decoder.decode_count_bit / 8) + 1);
  264. }
  265. bool subghz_protocol_decoder_ansonic_serialize(
  266. void* context,
  267. FlipperFormat* flipper_format,
  268. SubGhzRadioPreset* preset) {
  269. furi_assert(context);
  270. SubGhzProtocolDecoderAnsonic* instance = context;
  271. return subghz_block_generic_serialize(&instance->generic, flipper_format, preset);
  272. }
  273. bool subghz_protocol_decoder_ansonic_deserialize(void* context, FlipperFormat* flipper_format) {
  274. furi_assert(context);
  275. SubGhzProtocolDecoderAnsonic* instance = context;
  276. bool ret = false;
  277. do {
  278. if(!subghz_block_generic_deserialize(&instance->generic, flipper_format)) {
  279. break;
  280. }
  281. if(instance->generic.data_count_bit !=
  282. subghz_protocol_ansonic_const.min_count_bit_for_found) {
  283. FURI_LOG_E(TAG, "Wrong number of bits in key");
  284. break;
  285. }
  286. ret = true;
  287. } while(false);
  288. return ret;
  289. }
  290. void subghz_protocol_decoder_ansonic_get_string(void* context, FuriString* output) {
  291. furi_assert(context);
  292. SubGhzProtocolDecoderAnsonic* instance = context;
  293. subghz_protocol_ansonic_check_remote_controller(&instance->generic);
  294. furi_string_cat_printf(
  295. output,
  296. "%s %dbit\r\n"
  297. "Key:%03lX\r\n"
  298. "Btn:%X\r\n"
  299. "DIP:" DIP_PATTERN "\r\n",
  300. instance->generic.protocol_name,
  301. instance->generic.data_count_bit,
  302. (uint32_t)(instance->generic.data & 0xFFFFFFFF),
  303. instance->generic.btn,
  304. CNT_TO_DIP(instance->generic.cnt));
  305. }