intertechno_v3.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472
  1. #include "intertechno_v3.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 "SubGhzProtocolIntertechnoV3"
  8. #define CH_PATTERN "%c%c%c%c"
  9. #define CNT_TO_CH(ch) \
  10. (ch & 0x8 ? '1' : '0'), (ch & 0x4 ? '1' : '0'), (ch & 0x2 ? '1' : '0'), (ch & 0x1 ? '1' : '0')
  11. #define INTERTECHNO_V3_DIMMING_COUNT_BIT 36
  12. static const SubGhzBlockConst subghz_protocol_intertechno_v3_const = {
  13. .te_short = 275,
  14. .te_long = 1375,
  15. .te_delta = 150,
  16. .min_count_bit_for_found = 32,
  17. };
  18. struct SubGhzProtocolDecoderIntertechno_V3 {
  19. SubGhzProtocolDecoderBase base;
  20. SubGhzBlockDecoder decoder;
  21. SubGhzBlockGeneric generic;
  22. };
  23. struct SubGhzProtocolEncoderIntertechno_V3 {
  24. SubGhzProtocolEncoderBase base;
  25. SubGhzProtocolBlockEncoder encoder;
  26. SubGhzBlockGeneric generic;
  27. };
  28. typedef enum {
  29. IntertechnoV3DecoderStepReset = 0,
  30. IntertechnoV3DecoderStepStartSync,
  31. IntertechnoV3DecoderStepFoundSync,
  32. IntertechnoV3DecoderStepStartDuration,
  33. IntertechnoV3DecoderStepSaveDuration,
  34. IntertechnoV3DecoderStepCheckDuration,
  35. IntertechnoV3DecoderStepEndDuration,
  36. } IntertechnoV3DecoderStep;
  37. const SubGhzProtocolDecoder subghz_protocol_intertechno_v3_decoder = {
  38. .alloc = subghz_protocol_decoder_intertechno_v3_alloc,
  39. .free = subghz_protocol_decoder_intertechno_v3_free,
  40. .feed = subghz_protocol_decoder_intertechno_v3_feed,
  41. .reset = subghz_protocol_decoder_intertechno_v3_reset,
  42. .get_hash_data = subghz_protocol_decoder_intertechno_v3_get_hash_data,
  43. .serialize = subghz_protocol_decoder_intertechno_v3_serialize,
  44. .deserialize = subghz_protocol_decoder_intertechno_v3_deserialize,
  45. .get_string = subghz_protocol_decoder_intertechno_v3_get_string,
  46. };
  47. const SubGhzProtocolEncoder subghz_protocol_intertechno_v3_encoder = {
  48. .alloc = subghz_protocol_encoder_intertechno_v3_alloc,
  49. .free = subghz_protocol_encoder_intertechno_v3_free,
  50. .deserialize = subghz_protocol_encoder_intertechno_v3_deserialize,
  51. .stop = subghz_protocol_encoder_intertechno_v3_stop,
  52. .yield = subghz_protocol_encoder_intertechno_v3_yield,
  53. };
  54. const SubGhzProtocol subghz_protocol_intertechno_v3 = {
  55. .name = SUBGHZ_PROTOCOL_INTERTECHNO_V3_NAME,
  56. .type = SubGhzProtocolTypeStatic,
  57. .flag = SubGhzProtocolFlag_433 | SubGhzProtocolFlag_315 | SubGhzProtocolFlag_868 |
  58. SubGhzProtocolFlag_AM | SubGhzProtocolFlag_Decodable | SubGhzProtocolFlag_Load |
  59. SubGhzProtocolFlag_Save | SubGhzProtocolFlag_Send,
  60. .decoder = &subghz_protocol_intertechno_v3_decoder,
  61. .encoder = &subghz_protocol_intertechno_v3_encoder,
  62. };
  63. void* subghz_protocol_encoder_intertechno_v3_alloc(SubGhzEnvironment* environment) {
  64. UNUSED(environment);
  65. SubGhzProtocolEncoderIntertechno_V3* instance =
  66. malloc(sizeof(SubGhzProtocolEncoderIntertechno_V3));
  67. instance->base.protocol = &subghz_protocol_intertechno_v3;
  68. instance->generic.protocol_name = instance->base.protocol->name;
  69. instance->encoder.repeat = 10;
  70. instance->encoder.size_upload = 256;
  71. instance->encoder.upload = malloc(instance->encoder.size_upload * sizeof(LevelDuration));
  72. instance->encoder.is_running = false;
  73. return instance;
  74. }
  75. void subghz_protocol_encoder_intertechno_v3_free(void* context) {
  76. furi_assert(context);
  77. SubGhzProtocolEncoderIntertechno_V3* instance = context;
  78. free(instance->encoder.upload);
  79. free(instance);
  80. }
  81. /**
  82. * Generating an upload from data.
  83. * @param instance Pointer to a SubGhzProtocolEncoderIntertechno_V3 instance
  84. * @return true On success
  85. */
  86. static bool subghz_protocol_encoder_intertechno_v3_get_upload(
  87. SubGhzProtocolEncoderIntertechno_V3* instance) {
  88. furi_assert(instance);
  89. size_t index = 0;
  90. //Send header
  91. instance->encoder.upload[index++] =
  92. level_duration_make(true, (uint32_t)subghz_protocol_intertechno_v3_const.te_short);
  93. instance->encoder.upload[index++] =
  94. level_duration_make(false, (uint32_t)subghz_protocol_intertechno_v3_const.te_short * 38);
  95. //Send sync
  96. instance->encoder.upload[index++] =
  97. level_duration_make(true, (uint32_t)subghz_protocol_intertechno_v3_const.te_short);
  98. instance->encoder.upload[index++] =
  99. level_duration_make(false, (uint32_t)subghz_protocol_intertechno_v3_const.te_short * 10);
  100. //Send key data
  101. for(uint8_t i = instance->generic.data_count_bit; i > 0; i--) {
  102. if((instance->generic.data_count_bit == INTERTECHNO_V3_DIMMING_COUNT_BIT) && (i == 9)) {
  103. //send bit dimm
  104. instance->encoder.upload[index++] =
  105. level_duration_make(true, (uint32_t)subghz_protocol_intertechno_v3_const.te_short);
  106. instance->encoder.upload[index++] = level_duration_make(
  107. false, (uint32_t)subghz_protocol_intertechno_v3_const.te_short);
  108. instance->encoder.upload[index++] =
  109. level_duration_make(true, (uint32_t)subghz_protocol_intertechno_v3_const.te_short);
  110. instance->encoder.upload[index++] = level_duration_make(
  111. false, (uint32_t)subghz_protocol_intertechno_v3_const.te_short);
  112. } else if(bit_read(instance->generic.data, i - 1)) {
  113. //send bit 1
  114. instance->encoder.upload[index++] =
  115. level_duration_make(true, (uint32_t)subghz_protocol_intertechno_v3_const.te_short);
  116. instance->encoder.upload[index++] =
  117. level_duration_make(false, (uint32_t)subghz_protocol_intertechno_v3_const.te_long);
  118. instance->encoder.upload[index++] =
  119. level_duration_make(true, (uint32_t)subghz_protocol_intertechno_v3_const.te_short);
  120. instance->encoder.upload[index++] = level_duration_make(
  121. false, (uint32_t)subghz_protocol_intertechno_v3_const.te_short);
  122. } else {
  123. //send bit 0
  124. instance->encoder.upload[index++] =
  125. level_duration_make(true, (uint32_t)subghz_protocol_intertechno_v3_const.te_short);
  126. instance->encoder.upload[index++] = level_duration_make(
  127. false, (uint32_t)subghz_protocol_intertechno_v3_const.te_short);
  128. instance->encoder.upload[index++] =
  129. level_duration_make(true, (uint32_t)subghz_protocol_intertechno_v3_const.te_short);
  130. instance->encoder.upload[index++] =
  131. level_duration_make(false, (uint32_t)subghz_protocol_intertechno_v3_const.te_long);
  132. }
  133. }
  134. instance->encoder.size_upload = index;
  135. return true;
  136. }
  137. bool subghz_protocol_encoder_intertechno_v3_deserialize(
  138. void* context,
  139. FlipperFormat* flipper_format) {
  140. furi_assert(context);
  141. SubGhzProtocolEncoderIntertechno_V3* instance = context;
  142. bool res = false;
  143. do {
  144. if(!subghz_block_generic_deserialize(&instance->generic, flipper_format)) {
  145. FURI_LOG_E(TAG, "Deserialize error");
  146. break;
  147. }
  148. if((instance->generic.data_count_bit !=
  149. subghz_protocol_intertechno_v3_const.min_count_bit_for_found) &&
  150. (instance->generic.data_count_bit != INTERTECHNO_V3_DIMMING_COUNT_BIT)) {
  151. FURI_LOG_E(TAG, "Wrong number of bits in key");
  152. break;
  153. }
  154. //optional parameter parameter
  155. flipper_format_read_uint32(
  156. flipper_format, "Repeat", (uint32_t*)&instance->encoder.repeat, 1);
  157. if(!subghz_protocol_encoder_intertechno_v3_get_upload(instance)) break;
  158. instance->encoder.is_running = true;
  159. res = true;
  160. } while(false);
  161. return res;
  162. }
  163. void subghz_protocol_encoder_intertechno_v3_stop(void* context) {
  164. SubGhzProtocolEncoderIntertechno_V3* instance = context;
  165. instance->encoder.is_running = false;
  166. }
  167. LevelDuration subghz_protocol_encoder_intertechno_v3_yield(void* context) {
  168. SubGhzProtocolEncoderIntertechno_V3* instance = context;
  169. if(instance->encoder.repeat == 0 || !instance->encoder.is_running) {
  170. instance->encoder.is_running = false;
  171. return level_duration_reset();
  172. }
  173. LevelDuration ret = instance->encoder.upload[instance->encoder.front];
  174. if(++instance->encoder.front == instance->encoder.size_upload) {
  175. instance->encoder.repeat--;
  176. instance->encoder.front = 0;
  177. }
  178. return ret;
  179. }
  180. void* subghz_protocol_decoder_intertechno_v3_alloc(SubGhzEnvironment* environment) {
  181. UNUSED(environment);
  182. SubGhzProtocolDecoderIntertechno_V3* instance =
  183. malloc(sizeof(SubGhzProtocolDecoderIntertechno_V3));
  184. instance->base.protocol = &subghz_protocol_intertechno_v3;
  185. instance->generic.protocol_name = instance->base.protocol->name;
  186. return instance;
  187. }
  188. void subghz_protocol_decoder_intertechno_v3_free(void* context) {
  189. furi_assert(context);
  190. SubGhzProtocolDecoderIntertechno_V3* instance = context;
  191. free(instance);
  192. }
  193. void subghz_protocol_decoder_intertechno_v3_reset(void* context) {
  194. furi_assert(context);
  195. SubGhzProtocolDecoderIntertechno_V3* instance = context;
  196. instance->decoder.parser_step = IntertechnoV3DecoderStepReset;
  197. }
  198. void subghz_protocol_decoder_intertechno_v3_feed(void* context, bool level, uint32_t duration) {
  199. furi_assert(context);
  200. SubGhzProtocolDecoderIntertechno_V3* instance = context;
  201. switch(instance->decoder.parser_step) {
  202. case IntertechnoV3DecoderStepReset:
  203. if((!level) &&
  204. (DURATION_DIFF(duration, subghz_protocol_intertechno_v3_const.te_short * 37) <
  205. subghz_protocol_intertechno_v3_const.te_delta * 15)) {
  206. instance->decoder.parser_step = IntertechnoV3DecoderStepStartSync;
  207. }
  208. break;
  209. case IntertechnoV3DecoderStepStartSync:
  210. if(level && (DURATION_DIFF(duration, subghz_protocol_intertechno_v3_const.te_short) <
  211. subghz_protocol_intertechno_v3_const.te_delta)) {
  212. instance->decoder.parser_step = IntertechnoV3DecoderStepFoundSync;
  213. } else {
  214. instance->decoder.parser_step = IntertechnoV3DecoderStepReset;
  215. }
  216. break;
  217. case IntertechnoV3DecoderStepFoundSync:
  218. if(!level && (DURATION_DIFF(duration, subghz_protocol_intertechno_v3_const.te_short * 10) <
  219. subghz_protocol_intertechno_v3_const.te_delta * 3)) {
  220. instance->decoder.parser_step = IntertechnoV3DecoderStepStartDuration;
  221. instance->decoder.decode_data = 0;
  222. instance->decoder.decode_count_bit = 0;
  223. } else {
  224. instance->decoder.parser_step = IntertechnoV3DecoderStepReset;
  225. }
  226. break;
  227. case IntertechnoV3DecoderStepStartDuration:
  228. if(level && (DURATION_DIFF(duration, subghz_protocol_intertechno_v3_const.te_short) <
  229. subghz_protocol_intertechno_v3_const.te_delta)) {
  230. instance->decoder.parser_step = IntertechnoV3DecoderStepSaveDuration;
  231. } else {
  232. instance->decoder.parser_step = IntertechnoV3DecoderStepReset;
  233. }
  234. break;
  235. case IntertechnoV3DecoderStepSaveDuration:
  236. if(!level) { //save interval
  237. if(duration >= (subghz_protocol_intertechno_v3_const.te_short * 11)) {
  238. instance->decoder.parser_step = IntertechnoV3DecoderStepStartSync;
  239. if((instance->decoder.decode_count_bit ==
  240. subghz_protocol_intertechno_v3_const.min_count_bit_for_found) ||
  241. (instance->decoder.decode_count_bit == INTERTECHNO_V3_DIMMING_COUNT_BIT)) {
  242. instance->generic.data = instance->decoder.decode_data;
  243. instance->generic.data_count_bit = instance->decoder.decode_count_bit;
  244. if(instance->base.callback)
  245. instance->base.callback(&instance->base, instance->base.context);
  246. }
  247. break;
  248. }
  249. instance->decoder.te_last = duration;
  250. instance->decoder.parser_step = IntertechnoV3DecoderStepCheckDuration;
  251. } else {
  252. instance->decoder.parser_step = IntertechnoV3DecoderStepReset;
  253. }
  254. break;
  255. case IntertechnoV3DecoderStepCheckDuration:
  256. if(level) {
  257. //Add 0 bit
  258. if((DURATION_DIFF(
  259. instance->decoder.te_last, subghz_protocol_intertechno_v3_const.te_short) <
  260. subghz_protocol_intertechno_v3_const.te_delta) &&
  261. (DURATION_DIFF(duration, subghz_protocol_intertechno_v3_const.te_short) <
  262. subghz_protocol_intertechno_v3_const.te_delta)) {
  263. subghz_protocol_blocks_add_bit(&instance->decoder, 0);
  264. instance->decoder.parser_step = IntertechnoV3DecoderStepEndDuration;
  265. } else if(
  266. //Add 1 bit
  267. (DURATION_DIFF(
  268. instance->decoder.te_last, subghz_protocol_intertechno_v3_const.te_long) <
  269. subghz_protocol_intertechno_v3_const.te_delta * 2) &&
  270. (DURATION_DIFF(duration, subghz_protocol_intertechno_v3_const.te_short) <
  271. subghz_protocol_intertechno_v3_const.te_delta)) {
  272. subghz_protocol_blocks_add_bit(&instance->decoder, 1);
  273. instance->decoder.parser_step = IntertechnoV3DecoderStepEndDuration;
  274. } else if(
  275. //Add dimm_state
  276. (DURATION_DIFF(
  277. instance->decoder.te_last, subghz_protocol_intertechno_v3_const.te_short) <
  278. subghz_protocol_intertechno_v3_const.te_delta * 2) &&
  279. (DURATION_DIFF(duration, subghz_protocol_intertechno_v3_const.te_short) <
  280. subghz_protocol_intertechno_v3_const.te_delta) &&
  281. (instance->decoder.decode_count_bit == 27)) {
  282. subghz_protocol_blocks_add_bit(&instance->decoder, 0);
  283. instance->decoder.parser_step = IntertechnoV3DecoderStepEndDuration;
  284. } else
  285. instance->decoder.parser_step = IntertechnoV3DecoderStepReset;
  286. } else {
  287. instance->decoder.parser_step = IntertechnoV3DecoderStepReset;
  288. }
  289. break;
  290. case IntertechnoV3DecoderStepEndDuration:
  291. if(!level && ((DURATION_DIFF(duration, subghz_protocol_intertechno_v3_const.te_short) <
  292. subghz_protocol_intertechno_v3_const.te_delta) ||
  293. (DURATION_DIFF(duration, subghz_protocol_intertechno_v3_const.te_long) <
  294. subghz_protocol_intertechno_v3_const.te_delta * 2))) {
  295. instance->decoder.parser_step = IntertechnoV3DecoderStepStartDuration;
  296. } else {
  297. instance->decoder.parser_step = IntertechnoV3DecoderStepReset;
  298. }
  299. break;
  300. }
  301. }
  302. /**
  303. * Analysis of received data
  304. * @param instance Pointer to a SubGhzBlockGeneric* instance
  305. */
  306. static void subghz_protocol_intertechno_v3_check_remote_controller(SubGhzBlockGeneric* instance) {
  307. /*
  308. * A frame is either 32 or 36 bits:
  309. *
  310. * _
  311. * start bit: | |__________ (T,10T)
  312. * _ _
  313. * '0': | |_| |_____ (T,T,T,5T)
  314. * _ _
  315. * '1': | |_____| |_ (T,5T,T,T)
  316. * _ _
  317. * dimm: | |_| |_ (T,T,T,T)
  318. *
  319. * _
  320. * stop bit: | |____...____ (T,38T)
  321. *
  322. * if frame 32 bits
  323. * SSSSSSSSSSSSSSSSSSSSSSSSSS all_ch on/off ~ch
  324. * Key:0x3F86C59F => 00111111100001101100010110 0 1 1111
  325. *
  326. * if frame 36 bits
  327. * SSSSSSSSSSSSSSSSSSSSSSSSSS all_ch dimm ~ch dimm_level
  328. * Key:0x42D2E8856 => 01000010110100101110100010 0 X 0101 0110
  329. *
  330. */
  331. if(instance->data_count_bit == subghz_protocol_intertechno_v3_const.min_count_bit_for_found) {
  332. instance->serial = (instance->data >> 6) & 0x3FFFFFF;
  333. if((instance->data >> 5) & 0x1) {
  334. instance->cnt = 1 << 5;
  335. } else {
  336. instance->cnt = (~instance->data & 0xF);
  337. }
  338. instance->btn = (instance->data >> 4) & 0x1;
  339. } else if(instance->data_count_bit == INTERTECHNO_V3_DIMMING_COUNT_BIT) {
  340. instance->serial = (instance->data >> 10) & 0x3FFFFFF;
  341. if((instance->data >> 9) & 0x1) {
  342. instance->cnt = 1 << 5;
  343. } else {
  344. instance->cnt = (~(instance->data >> 4) & 0xF);
  345. }
  346. instance->btn = (instance->data) & 0xF;
  347. } else {
  348. instance->serial = 0;
  349. instance->cnt = 0;
  350. instance->btn = 0;
  351. }
  352. }
  353. uint8_t subghz_protocol_decoder_intertechno_v3_get_hash_data(void* context) {
  354. furi_assert(context);
  355. SubGhzProtocolDecoderIntertechno_V3* instance = context;
  356. return subghz_protocol_blocks_get_hash_data(
  357. &instance->decoder, (instance->decoder.decode_count_bit / 8) + 1);
  358. }
  359. bool subghz_protocol_decoder_intertechno_v3_serialize(
  360. void* context,
  361. FlipperFormat* flipper_format,
  362. SubGhzRadioPreset* preset) {
  363. furi_assert(context);
  364. SubGhzProtocolDecoderIntertechno_V3* instance = context;
  365. return subghz_block_generic_serialize(&instance->generic, flipper_format, preset);
  366. }
  367. bool subghz_protocol_decoder_intertechno_v3_deserialize(
  368. void* context,
  369. FlipperFormat* flipper_format) {
  370. furi_assert(context);
  371. SubGhzProtocolDecoderIntertechno_V3* instance = context;
  372. bool ret = false;
  373. do {
  374. if(!subghz_block_generic_deserialize(&instance->generic, flipper_format)) {
  375. break;
  376. }
  377. if((instance->generic.data_count_bit !=
  378. subghz_protocol_intertechno_v3_const.min_count_bit_for_found) &&
  379. (instance->generic.data_count_bit != INTERTECHNO_V3_DIMMING_COUNT_BIT)) {
  380. FURI_LOG_E(TAG, "Wrong number of bits in key");
  381. break;
  382. }
  383. ret = true;
  384. } while(false);
  385. return ret;
  386. }
  387. void subghz_protocol_decoder_intertechno_v3_get_string(void* context, FuriString* output) {
  388. furi_assert(context);
  389. SubGhzProtocolDecoderIntertechno_V3* instance = context;
  390. subghz_protocol_intertechno_v3_check_remote_controller(&instance->generic);
  391. furi_string_cat_printf(
  392. output,
  393. "%.11s %db\r\n"
  394. "Key:0x%08llX\r\n"
  395. "Sn:%07lX\r\n",
  396. instance->generic.protocol_name,
  397. instance->generic.data_count_bit,
  398. instance->generic.data,
  399. instance->generic.serial);
  400. if(instance->generic.data_count_bit ==
  401. subghz_protocol_intertechno_v3_const.min_count_bit_for_found) {
  402. if(instance->generic.cnt >> 5) {
  403. furi_string_cat_printf(
  404. output, "Ch: All Btn:%s\r\n", (instance->generic.btn ? "On" : "Off"));
  405. } else {
  406. furi_string_cat_printf(
  407. output,
  408. "Ch:" CH_PATTERN " Btn:%s\r\n",
  409. CNT_TO_CH(instance->generic.cnt),
  410. (instance->generic.btn ? "On" : "Off"));
  411. }
  412. } else if(instance->generic.data_count_bit == INTERTECHNO_V3_DIMMING_COUNT_BIT) {
  413. furi_string_cat_printf(
  414. output,
  415. "Ch:" CH_PATTERN " Dimm:%d%%\r\n",
  416. CNT_TO_CH(instance->generic.cnt),
  417. (int)(6.67 * (float)instance->generic.btn));
  418. }
  419. }