ansonic.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  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. SubGhzProtocolStatus
  119. subghz_protocol_encoder_ansonic_deserialize(void* context, FlipperFormat* flipper_format) {
  120. furi_assert(context);
  121. SubGhzProtocolEncoderAnsonic* instance = context;
  122. SubGhzProtocolStatus res = SubGhzProtocolStatusError;
  123. do {
  124. res = subghz_block_generic_deserialize_check_count_bit(
  125. &instance->generic,
  126. flipper_format,
  127. subghz_protocol_ansonic_const.min_count_bit_for_found);
  128. if(res != SubGhzProtocolStatusOk) {
  129. FURI_LOG_E(TAG, "Deserialize error");
  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)) {
  136. res = SubGhzProtocolStatusErrorEncoderGetUpload;
  137. break;
  138. }
  139. instance->encoder.is_running = true;
  140. } while(false);
  141. return res;
  142. }
  143. void subghz_protocol_encoder_ansonic_stop(void* context) {
  144. SubGhzProtocolEncoderAnsonic* instance = context;
  145. instance->encoder.is_running = false;
  146. }
  147. LevelDuration subghz_protocol_encoder_ansonic_yield(void* context) {
  148. SubGhzProtocolEncoderAnsonic* instance = context;
  149. if(instance->encoder.repeat == 0 || !instance->encoder.is_running) {
  150. instance->encoder.is_running = 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_ansonic_alloc(SubGhzEnvironment* environment) {
  161. UNUSED(environment);
  162. SubGhzProtocolDecoderAnsonic* instance = malloc(sizeof(SubGhzProtocolDecoderAnsonic));
  163. instance->base.protocol = &subghz_protocol_ansonic;
  164. instance->generic.protocol_name = instance->base.protocol->name;
  165. return instance;
  166. }
  167. void subghz_protocol_decoder_ansonic_free(void* context) {
  168. furi_assert(context);
  169. SubGhzProtocolDecoderAnsonic* instance = context;
  170. free(instance);
  171. }
  172. void subghz_protocol_decoder_ansonic_reset(void* context) {
  173. furi_assert(context);
  174. SubGhzProtocolDecoderAnsonic* instance = context;
  175. instance->decoder.parser_step = AnsonicDecoderStepReset;
  176. }
  177. void subghz_protocol_decoder_ansonic_feed(void* context, bool level, uint32_t duration) {
  178. furi_assert(context);
  179. SubGhzProtocolDecoderAnsonic* instance = context;
  180. switch(instance->decoder.parser_step) {
  181. case AnsonicDecoderStepReset:
  182. if((!level) && (DURATION_DIFF(duration, subghz_protocol_ansonic_const.te_short * 35) <
  183. subghz_protocol_ansonic_const.te_delta * 35)) {
  184. //Found header Ansonic
  185. instance->decoder.parser_step = AnsonicDecoderStepFoundStartBit;
  186. }
  187. break;
  188. case AnsonicDecoderStepFoundStartBit:
  189. if(!level) {
  190. break;
  191. } else if(
  192. DURATION_DIFF(duration, subghz_protocol_ansonic_const.te_short) <
  193. subghz_protocol_ansonic_const.te_delta) {
  194. //Found start bit Ansonic
  195. instance->decoder.parser_step = AnsonicDecoderStepSaveDuration;
  196. instance->decoder.decode_data = 0;
  197. instance->decoder.decode_count_bit = 0;
  198. } else {
  199. instance->decoder.parser_step = AnsonicDecoderStepReset;
  200. }
  201. break;
  202. case AnsonicDecoderStepSaveDuration:
  203. if(!level) { //save interval
  204. if(duration >= (subghz_protocol_ansonic_const.te_short * 4)) {
  205. instance->decoder.parser_step = AnsonicDecoderStepFoundStartBit;
  206. if(instance->decoder.decode_count_bit >=
  207. subghz_protocol_ansonic_const.min_count_bit_for_found) {
  208. instance->generic.serial = 0x0;
  209. instance->generic.btn = 0x0;
  210. instance->generic.data = instance->decoder.decode_data;
  211. instance->generic.data_count_bit = instance->decoder.decode_count_bit;
  212. if(instance->base.callback)
  213. instance->base.callback(&instance->base, instance->base.context);
  214. }
  215. break;
  216. }
  217. instance->decoder.te_last = duration;
  218. instance->decoder.parser_step = AnsonicDecoderStepCheckDuration;
  219. } else {
  220. instance->decoder.parser_step = AnsonicDecoderStepReset;
  221. }
  222. break;
  223. case AnsonicDecoderStepCheckDuration:
  224. if(level) {
  225. if((DURATION_DIFF(instance->decoder.te_last, subghz_protocol_ansonic_const.te_short) <
  226. subghz_protocol_ansonic_const.te_delta) &&
  227. (DURATION_DIFF(duration, subghz_protocol_ansonic_const.te_long) <
  228. subghz_protocol_ansonic_const.te_delta)) {
  229. subghz_protocol_blocks_add_bit(&instance->decoder, 1);
  230. instance->decoder.parser_step = AnsonicDecoderStepSaveDuration;
  231. } else if(
  232. (DURATION_DIFF(instance->decoder.te_last, subghz_protocol_ansonic_const.te_long) <
  233. subghz_protocol_ansonic_const.te_delta) &&
  234. (DURATION_DIFF(duration, subghz_protocol_ansonic_const.te_short) <
  235. subghz_protocol_ansonic_const.te_delta)) {
  236. subghz_protocol_blocks_add_bit(&instance->decoder, 0);
  237. instance->decoder.parser_step = AnsonicDecoderStepSaveDuration;
  238. } else
  239. instance->decoder.parser_step = AnsonicDecoderStepReset;
  240. } else {
  241. instance->decoder.parser_step = AnsonicDecoderStepReset;
  242. }
  243. break;
  244. }
  245. }
  246. /**
  247. * Analysis of received data
  248. * @param instance Pointer to a SubGhzBlockGeneric* instance
  249. */
  250. static void subghz_protocol_ansonic_check_remote_controller(SubGhzBlockGeneric* instance) {
  251. /*
  252. * 12345678(10) k 9
  253. * AAA => 10101010 1 01 0
  254. *
  255. * 1...10 - DIP
  256. * k- KEY
  257. */
  258. instance->cnt = instance->data & 0xFFF;
  259. instance->btn = ((instance->data >> 1) & 0x3);
  260. }
  261. uint8_t subghz_protocol_decoder_ansonic_get_hash_data(void* context) {
  262. furi_assert(context);
  263. SubGhzProtocolDecoderAnsonic* instance = context;
  264. return subghz_protocol_blocks_get_hash_data(
  265. &instance->decoder, (instance->decoder.decode_count_bit / 8) + 1);
  266. }
  267. SubGhzProtocolStatus subghz_protocol_decoder_ansonic_serialize(
  268. void* context,
  269. FlipperFormat* flipper_format,
  270. SubGhzRadioPreset* preset) {
  271. furi_assert(context);
  272. SubGhzProtocolDecoderAnsonic* instance = context;
  273. return subghz_block_generic_serialize(&instance->generic, flipper_format, preset);
  274. }
  275. SubGhzProtocolStatus
  276. subghz_protocol_decoder_ansonic_deserialize(void* context, FlipperFormat* flipper_format) {
  277. furi_assert(context);
  278. SubGhzProtocolDecoderAnsonic* instance = context;
  279. return subghz_block_generic_deserialize_check_count_bit(
  280. &instance->generic, flipper_format, subghz_protocol_ansonic_const.min_count_bit_for_found);
  281. }
  282. void subghz_protocol_decoder_ansonic_get_string(void* context, FuriString* output) {
  283. furi_assert(context);
  284. SubGhzProtocolDecoderAnsonic* instance = context;
  285. subghz_protocol_ansonic_check_remote_controller(&instance->generic);
  286. furi_string_cat_printf(
  287. output,
  288. "%s %dbit\r\n"
  289. "Key:%03lX\r\n"
  290. "Btn:%X\r\n"
  291. "DIP:" DIP_PATTERN "\r\n",
  292. instance->generic.protocol_name,
  293. instance->generic.data_count_bit,
  294. (uint32_t)(instance->generic.data & 0xFFFFFFFF),
  295. instance->generic.btn,
  296. CNT_TO_DIP(instance->generic.cnt));
  297. }