dooya.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447
  1. #include "dooya.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 "SubGhzProtocolDooya"
  8. #define DOYA_SINGLE_CHANNEL 0xFF
  9. static const SubGhzBlockConst subghz_protocol_dooya_const = {
  10. .te_short = 366,
  11. .te_long = 733,
  12. .te_delta = 120,
  13. .min_count_bit_for_found = 40,
  14. };
  15. struct SubGhzProtocolDecoderDooya {
  16. SubGhzProtocolDecoderBase base;
  17. SubGhzBlockDecoder decoder;
  18. SubGhzBlockGeneric generic;
  19. };
  20. struct SubGhzProtocolEncoderDooya {
  21. SubGhzProtocolEncoderBase base;
  22. SubGhzProtocolBlockEncoder encoder;
  23. SubGhzBlockGeneric generic;
  24. };
  25. typedef enum {
  26. DooyaDecoderStepReset = 0,
  27. DooyaDecoderStepFoundStartBit,
  28. DooyaDecoderStepSaveDuration,
  29. DooyaDecoderStepCheckDuration,
  30. } DooyaDecoderStep;
  31. const SubGhzProtocolDecoder subghz_protocol_dooya_decoder = {
  32. .alloc = subghz_protocol_decoder_dooya_alloc,
  33. .free = subghz_protocol_decoder_dooya_free,
  34. .feed = subghz_protocol_decoder_dooya_feed,
  35. .reset = subghz_protocol_decoder_dooya_reset,
  36. .get_hash_data = subghz_protocol_decoder_dooya_get_hash_data,
  37. .serialize = subghz_protocol_decoder_dooya_serialize,
  38. .deserialize = subghz_protocol_decoder_dooya_deserialize,
  39. .get_string = subghz_protocol_decoder_dooya_get_string,
  40. };
  41. const SubGhzProtocolEncoder subghz_protocol_dooya_encoder = {
  42. .alloc = subghz_protocol_encoder_dooya_alloc,
  43. .free = subghz_protocol_encoder_dooya_free,
  44. .deserialize = subghz_protocol_encoder_dooya_deserialize,
  45. .stop = subghz_protocol_encoder_dooya_stop,
  46. .yield = subghz_protocol_encoder_dooya_yield,
  47. };
  48. const SubGhzProtocol subghz_protocol_dooya = {
  49. .name = SUBGHZ_PROTOCOL_DOOYA_NAME,
  50. .type = SubGhzProtocolTypeStatic,
  51. .flag = SubGhzProtocolFlag_433 | SubGhzProtocolFlag_315 | SubGhzProtocolFlag_AM |
  52. SubGhzProtocolFlag_Decodable | SubGhzProtocolFlag_Load | SubGhzProtocolFlag_Save |
  53. SubGhzProtocolFlag_Send,
  54. .decoder = &subghz_protocol_dooya_decoder,
  55. .encoder = &subghz_protocol_dooya_encoder,
  56. };
  57. void* subghz_protocol_encoder_dooya_alloc(SubGhzEnvironment* environment) {
  58. UNUSED(environment);
  59. SubGhzProtocolEncoderDooya* instance = malloc(sizeof(SubGhzProtocolEncoderDooya));
  60. instance->base.protocol = &subghz_protocol_dooya;
  61. instance->generic.protocol_name = instance->base.protocol->name;
  62. instance->encoder.repeat = 10;
  63. instance->encoder.size_upload = 128;
  64. instance->encoder.upload = malloc(instance->encoder.size_upload * sizeof(LevelDuration));
  65. instance->encoder.is_running = false;
  66. return instance;
  67. }
  68. void subghz_protocol_encoder_dooya_free(void* context) {
  69. furi_assert(context);
  70. SubGhzProtocolEncoderDooya* instance = context;
  71. free(instance->encoder.upload);
  72. free(instance);
  73. }
  74. /**
  75. * Generating an upload from data.
  76. * @param instance Pointer to a SubGhzProtocolEncoderDooya instance
  77. * @return true On success
  78. */
  79. static bool subghz_protocol_encoder_dooya_get_upload(SubGhzProtocolEncoderDooya* instance) {
  80. furi_assert(instance);
  81. size_t index = 0;
  82. size_t size_upload = (instance->generic.data_count_bit * 2) + 2;
  83. if(size_upload > instance->encoder.size_upload) {
  84. FURI_LOG_E(TAG, "Size upload exceeds allocated encoder buffer.");
  85. return false;
  86. } else {
  87. instance->encoder.size_upload = size_upload;
  88. }
  89. //Send header
  90. if(bit_read(instance->generic.data, 0)) {
  91. instance->encoder.upload[index++] = level_duration_make(
  92. false,
  93. (uint32_t)subghz_protocol_dooya_const.te_long * 12 +
  94. subghz_protocol_dooya_const.te_long);
  95. } else {
  96. instance->encoder.upload[index++] = level_duration_make(
  97. false,
  98. (uint32_t)subghz_protocol_dooya_const.te_long * 12 +
  99. subghz_protocol_dooya_const.te_short);
  100. }
  101. //Send start bit
  102. instance->encoder.upload[index++] =
  103. level_duration_make(true, (uint32_t)subghz_protocol_dooya_const.te_short * 13);
  104. instance->encoder.upload[index++] =
  105. level_duration_make(false, (uint32_t)subghz_protocol_dooya_const.te_long * 2);
  106. //Send key data
  107. for(uint8_t i = instance->generic.data_count_bit; i > 0; i--) {
  108. if(bit_read(instance->generic.data, i - 1)) {
  109. //send bit 1
  110. instance->encoder.upload[index++] =
  111. level_duration_make(true, (uint32_t)subghz_protocol_dooya_const.te_long);
  112. instance->encoder.upload[index++] =
  113. level_duration_make(false, (uint32_t)subghz_protocol_dooya_const.te_short);
  114. } else {
  115. //send bit 0
  116. instance->encoder.upload[index++] =
  117. level_duration_make(true, (uint32_t)subghz_protocol_dooya_const.te_short);
  118. instance->encoder.upload[index++] =
  119. level_duration_make(false, (uint32_t)subghz_protocol_dooya_const.te_long);
  120. }
  121. }
  122. return true;
  123. }
  124. bool subghz_protocol_encoder_dooya_deserialize(void* context, FlipperFormat* flipper_format) {
  125. furi_assert(context);
  126. SubGhzProtocolEncoderDooya* instance = context;
  127. bool res = false;
  128. do {
  129. if(!subghz_block_generic_deserialize(&instance->generic, flipper_format)) {
  130. FURI_LOG_E(TAG, "Deserialize error");
  131. break;
  132. }
  133. if(instance->generic.data_count_bit !=
  134. subghz_protocol_dooya_const.min_count_bit_for_found) {
  135. FURI_LOG_E(TAG, "Wrong number of bits in key");
  136. break;
  137. }
  138. //optional parameter parameter
  139. flipper_format_read_uint32(
  140. flipper_format, "Repeat", (uint32_t*)&instance->encoder.repeat, 1);
  141. if(!subghz_protocol_encoder_dooya_get_upload(instance)) break;
  142. instance->encoder.is_running = true;
  143. res = true;
  144. } while(false);
  145. return res;
  146. }
  147. void subghz_protocol_encoder_dooya_stop(void* context) {
  148. SubGhzProtocolEncoderDooya* instance = context;
  149. instance->encoder.is_running = false;
  150. }
  151. LevelDuration subghz_protocol_encoder_dooya_yield(void* context) {
  152. SubGhzProtocolEncoderDooya* instance = context;
  153. if(instance->encoder.repeat == 0 || !instance->encoder.is_running) {
  154. instance->encoder.is_running = false;
  155. return level_duration_reset();
  156. }
  157. LevelDuration ret = instance->encoder.upload[instance->encoder.front];
  158. if(++instance->encoder.front == instance->encoder.size_upload) {
  159. instance->encoder.repeat--;
  160. instance->encoder.front = 0;
  161. }
  162. return ret;
  163. }
  164. void* subghz_protocol_decoder_dooya_alloc(SubGhzEnvironment* environment) {
  165. UNUSED(environment);
  166. SubGhzProtocolDecoderDooya* instance = malloc(sizeof(SubGhzProtocolDecoderDooya));
  167. instance->base.protocol = &subghz_protocol_dooya;
  168. instance->generic.protocol_name = instance->base.protocol->name;
  169. return instance;
  170. }
  171. void subghz_protocol_decoder_dooya_free(void* context) {
  172. furi_assert(context);
  173. SubGhzProtocolDecoderDooya* instance = context;
  174. free(instance);
  175. }
  176. void subghz_protocol_decoder_dooya_reset(void* context) {
  177. furi_assert(context);
  178. SubGhzProtocolDecoderDooya* instance = context;
  179. instance->decoder.parser_step = DooyaDecoderStepReset;
  180. }
  181. void subghz_protocol_decoder_dooya_feed(void* context, bool level, uint32_t duration) {
  182. furi_assert(context);
  183. SubGhzProtocolDecoderDooya* instance = context;
  184. switch(instance->decoder.parser_step) {
  185. case DooyaDecoderStepReset:
  186. if((!level) && (DURATION_DIFF(duration, subghz_protocol_dooya_const.te_long * 12) <
  187. subghz_protocol_dooya_const.te_delta * 20)) {
  188. instance->decoder.parser_step = DooyaDecoderStepFoundStartBit;
  189. }
  190. break;
  191. case DooyaDecoderStepFoundStartBit:
  192. if(!level) {
  193. if(DURATION_DIFF(duration, subghz_protocol_dooya_const.te_long * 2) <
  194. subghz_protocol_dooya_const.te_delta * 3) {
  195. instance->decoder.parser_step = DooyaDecoderStepSaveDuration;
  196. instance->decoder.decode_data = 0;
  197. instance->decoder.decode_count_bit = 0;
  198. } else {
  199. instance->decoder.parser_step = DooyaDecoderStepReset;
  200. }
  201. } else if(
  202. DURATION_DIFF(duration, subghz_protocol_dooya_const.te_short * 13) <
  203. subghz_protocol_dooya_const.te_delta * 5) {
  204. break;
  205. } else {
  206. instance->decoder.parser_step = DooyaDecoderStepReset;
  207. }
  208. break;
  209. case DooyaDecoderStepSaveDuration:
  210. if(level) {
  211. instance->decoder.te_last = duration;
  212. instance->decoder.parser_step = DooyaDecoderStepCheckDuration;
  213. } else {
  214. instance->decoder.parser_step = DooyaDecoderStepReset;
  215. }
  216. break;
  217. case DooyaDecoderStepCheckDuration:
  218. if(!level) {
  219. if(duration >= (subghz_protocol_dooya_const.te_long * 4)) {
  220. //add last bit
  221. if(DURATION_DIFF(instance->decoder.te_last, subghz_protocol_dooya_const.te_short) <
  222. subghz_protocol_dooya_const.te_delta) {
  223. subghz_protocol_blocks_add_bit(&instance->decoder, 0);
  224. } else if(
  225. DURATION_DIFF(instance->decoder.te_last, subghz_protocol_dooya_const.te_long) <
  226. subghz_protocol_dooya_const.te_delta * 2) {
  227. subghz_protocol_blocks_add_bit(&instance->decoder, 1);
  228. } else {
  229. instance->decoder.parser_step = DooyaDecoderStepReset;
  230. break;
  231. }
  232. instance->decoder.parser_step = DooyaDecoderStepFoundStartBit;
  233. if(instance->decoder.decode_count_bit ==
  234. subghz_protocol_dooya_const.min_count_bit_for_found) {
  235. instance->generic.data = instance->decoder.decode_data;
  236. instance->generic.data_count_bit = instance->decoder.decode_count_bit;
  237. if(instance->base.callback)
  238. instance->base.callback(&instance->base, instance->base.context);
  239. }
  240. break;
  241. } else if(
  242. (DURATION_DIFF(instance->decoder.te_last, subghz_protocol_dooya_const.te_short) <
  243. subghz_protocol_dooya_const.te_delta) &&
  244. (DURATION_DIFF(duration, subghz_protocol_dooya_const.te_long) <
  245. subghz_protocol_dooya_const.te_delta * 2)) {
  246. subghz_protocol_blocks_add_bit(&instance->decoder, 0);
  247. instance->decoder.parser_step = DooyaDecoderStepSaveDuration;
  248. } else if(
  249. (DURATION_DIFF(instance->decoder.te_last, subghz_protocol_dooya_const.te_long) <
  250. subghz_protocol_dooya_const.te_delta * 2) &&
  251. (DURATION_DIFF(duration, subghz_protocol_dooya_const.te_short) <
  252. subghz_protocol_dooya_const.te_delta)) {
  253. subghz_protocol_blocks_add_bit(&instance->decoder, 1);
  254. instance->decoder.parser_step = DooyaDecoderStepSaveDuration;
  255. } else {
  256. instance->decoder.parser_step = DooyaDecoderStepReset;
  257. }
  258. } else {
  259. instance->decoder.parser_step = DooyaDecoderStepReset;
  260. }
  261. break;
  262. }
  263. }
  264. /**
  265. * Analysis of received data
  266. * @param instance Pointer to a SubGhzBlockGeneric* instance
  267. */
  268. static void subghz_protocol_somfy_telis_check_remote_controller(SubGhzBlockGeneric* instance) {
  269. /*
  270. * serial s/m ch key
  271. * long press down X * E1DC030533, 40b 111000011101110000000011 0000 0101 0011 0011
  272. *
  273. * short press down 3 * E1DC030533, 40b 111000011101110000000011 0000 0101 0011 0011
  274. * 3 * E1DC03053C, 40b 111000011101110000000011 0000 0101 0011 1100
  275. *
  276. * press stop X * E1DC030555, 40b 111000011101110000000011 0000 0101 0101 0101
  277. *
  278. * long press up X * E1DC030511, 40b 111000011101110000000011 0000 0101 0001 0001
  279. *
  280. * short press up 3 * E1DC030511, 40b 111000011101110000000011 0000 0101 0001 0001
  281. * 3 * E1DC03051E, 40b 111000011101110000000011 0000 0101 0001 1110
  282. *
  283. * serial: 3 byte serial number
  284. * s/m: single (b0000) / multi (b0001) channel console
  285. * ch: channel if single (always b0101) or multi
  286. * key: 0b00010001 - long press up
  287. * 0b00011110 - short press up
  288. * 0b00110011 - long press down
  289. * 0b00111100 - short press down
  290. * 0b01010101 - press stop
  291. * 0b01111001 - press up + down
  292. * 0b10000000 - press up + stop
  293. * 0b10000001 - press down + stop
  294. * 0b11001100 - press P2
  295. *
  296. */
  297. instance->serial = (instance->data >> 16);
  298. if((instance->data >> 12) & 0x0F) {
  299. instance->cnt = (instance->data >> 8) & 0x0F;
  300. } else {
  301. instance->cnt = DOYA_SINGLE_CHANNEL;
  302. }
  303. instance->btn = instance->data & 0xFF;
  304. }
  305. uint8_t subghz_protocol_decoder_dooya_get_hash_data(void* context) {
  306. furi_assert(context);
  307. SubGhzProtocolDecoderDooya* instance = context;
  308. return subghz_protocol_blocks_get_hash_data(
  309. &instance->decoder, (instance->decoder.decode_count_bit / 8) + 1);
  310. }
  311. bool subghz_protocol_decoder_dooya_serialize(
  312. void* context,
  313. FlipperFormat* flipper_format,
  314. SubGhzRadioPreset* preset) {
  315. furi_assert(context);
  316. SubGhzProtocolDecoderDooya* instance = context;
  317. return subghz_block_generic_serialize(&instance->generic, flipper_format, preset);
  318. }
  319. bool subghz_protocol_decoder_dooya_deserialize(void* context, FlipperFormat* flipper_format) {
  320. furi_assert(context);
  321. SubGhzProtocolDecoderDooya* instance = context;
  322. bool ret = false;
  323. do {
  324. if(!subghz_block_generic_deserialize(&instance->generic, flipper_format)) {
  325. break;
  326. }
  327. if(instance->generic.data_count_bit !=
  328. subghz_protocol_dooya_const.min_count_bit_for_found) {
  329. FURI_LOG_E(TAG, "Wrong number of bits in key");
  330. break;
  331. }
  332. ret = true;
  333. } while(false);
  334. return ret;
  335. }
  336. /**
  337. * Get button name.
  338. * @param btn Button number, 8 bit
  339. */
  340. static const char* subghz_protocol_dooya_get_name_button(uint8_t btn) {
  341. const char* btn_name;
  342. switch(btn) {
  343. case 0b00010001:
  344. btn_name = "Up_Long";
  345. break;
  346. case 0b00011110:
  347. btn_name = "Up_Short";
  348. break;
  349. case 0b00110011:
  350. btn_name = "Down_Long";
  351. break;
  352. case 0b00111100:
  353. btn_name = "Down_Short";
  354. break;
  355. case 0b01010101:
  356. btn_name = "Stop";
  357. break;
  358. case 0b01111001:
  359. btn_name = "Up+Down";
  360. break;
  361. case 0b10000000:
  362. btn_name = "Up+Stop";
  363. break;
  364. case 0b10000001:
  365. btn_name = "Down+Stop";
  366. break;
  367. case 0b11001100:
  368. btn_name = "P2";
  369. break;
  370. default:
  371. btn_name = "Unknown";
  372. break;
  373. }
  374. return btn_name;
  375. }
  376. void subghz_protocol_decoder_dooya_get_string(void* context, FuriString* output) {
  377. furi_assert(context);
  378. SubGhzProtocolDecoderDooya* instance = context;
  379. subghz_protocol_somfy_telis_check_remote_controller(&instance->generic);
  380. furi_string_cat_printf(
  381. output,
  382. "%s %dbit\r\n"
  383. "Key:0x%010llX\r\n"
  384. "Sn:0x%08lX\r\n"
  385. "Btn:%s\r\n",
  386. instance->generic.protocol_name,
  387. instance->generic.data_count_bit,
  388. instance->generic.data,
  389. instance->generic.serial,
  390. subghz_protocol_dooya_get_name_button(instance->generic.btn));
  391. if(instance->generic.cnt == DOYA_SINGLE_CHANNEL) {
  392. furi_string_cat_printf(output, "Ch:Single\r\n");
  393. } else {
  394. furi_string_cat_printf(output, "Ch:%lu\r\n", instance->generic.cnt);
  395. }
  396. }