intertechno_v3.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475
  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. SubGhzProtocolStatus subghz_protocol_encoder_intertechno_v3_deserialize(
  138. void* context,
  139. FlipperFormat* flipper_format) {
  140. furi_assert(context);
  141. SubGhzProtocolEncoderIntertechno_V3* instance = context;
  142. SubGhzProtocolStatus ret = SubGhzProtocolStatusError;
  143. do {
  144. ret = subghz_block_generic_deserialize(&instance->generic, flipper_format);
  145. if(ret != SubGhzProtocolStatusOk) {
  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. ret = SubGhzProtocolStatusErrorValueBitCount;
  153. break;
  154. }
  155. //optional parameter parameter
  156. flipper_format_read_uint32(
  157. flipper_format, "Repeat", (uint32_t*)&instance->encoder.repeat, 1);
  158. if(!subghz_protocol_encoder_intertechno_v3_get_upload(instance)) {
  159. ret = SubGhzProtocolStatusErrorEncoderGetUpload;
  160. break;
  161. }
  162. instance->encoder.is_running = true;
  163. } while(false);
  164. return ret;
  165. }
  166. void subghz_protocol_encoder_intertechno_v3_stop(void* context) {
  167. SubGhzProtocolEncoderIntertechno_V3* instance = context;
  168. instance->encoder.is_running = false;
  169. }
  170. LevelDuration subghz_protocol_encoder_intertechno_v3_yield(void* context) {
  171. SubGhzProtocolEncoderIntertechno_V3* instance = context;
  172. if(instance->encoder.repeat == 0 || !instance->encoder.is_running) {
  173. instance->encoder.is_running = false;
  174. return level_duration_reset();
  175. }
  176. LevelDuration ret = instance->encoder.upload[instance->encoder.front];
  177. if(++instance->encoder.front == instance->encoder.size_upload) {
  178. instance->encoder.repeat--;
  179. instance->encoder.front = 0;
  180. }
  181. return ret;
  182. }
  183. void* subghz_protocol_decoder_intertechno_v3_alloc(SubGhzEnvironment* environment) {
  184. UNUSED(environment);
  185. SubGhzProtocolDecoderIntertechno_V3* instance =
  186. malloc(sizeof(SubGhzProtocolDecoderIntertechno_V3));
  187. instance->base.protocol = &subghz_protocol_intertechno_v3;
  188. instance->generic.protocol_name = instance->base.protocol->name;
  189. return instance;
  190. }
  191. void subghz_protocol_decoder_intertechno_v3_free(void* context) {
  192. furi_assert(context);
  193. SubGhzProtocolDecoderIntertechno_V3* instance = context;
  194. free(instance);
  195. }
  196. void subghz_protocol_decoder_intertechno_v3_reset(void* context) {
  197. furi_assert(context);
  198. SubGhzProtocolDecoderIntertechno_V3* instance = context;
  199. instance->decoder.parser_step = IntertechnoV3DecoderStepReset;
  200. }
  201. void subghz_protocol_decoder_intertechno_v3_feed(void* context, bool level, uint32_t duration) {
  202. furi_assert(context);
  203. SubGhzProtocolDecoderIntertechno_V3* instance = context;
  204. switch(instance->decoder.parser_step) {
  205. case IntertechnoV3DecoderStepReset:
  206. if((!level) &&
  207. (DURATION_DIFF(duration, subghz_protocol_intertechno_v3_const.te_short * 37) <
  208. subghz_protocol_intertechno_v3_const.te_delta * 15)) {
  209. instance->decoder.parser_step = IntertechnoV3DecoderStepStartSync;
  210. }
  211. break;
  212. case IntertechnoV3DecoderStepStartSync:
  213. if(level && (DURATION_DIFF(duration, subghz_protocol_intertechno_v3_const.te_short) <
  214. subghz_protocol_intertechno_v3_const.te_delta)) {
  215. instance->decoder.parser_step = IntertechnoV3DecoderStepFoundSync;
  216. } else {
  217. instance->decoder.parser_step = IntertechnoV3DecoderStepReset;
  218. }
  219. break;
  220. case IntertechnoV3DecoderStepFoundSync:
  221. if(!level && (DURATION_DIFF(duration, subghz_protocol_intertechno_v3_const.te_short * 10) <
  222. subghz_protocol_intertechno_v3_const.te_delta * 3)) {
  223. instance->decoder.parser_step = IntertechnoV3DecoderStepStartDuration;
  224. instance->decoder.decode_data = 0;
  225. instance->decoder.decode_count_bit = 0;
  226. } else {
  227. instance->decoder.parser_step = IntertechnoV3DecoderStepReset;
  228. }
  229. break;
  230. case IntertechnoV3DecoderStepStartDuration:
  231. if(level && (DURATION_DIFF(duration, subghz_protocol_intertechno_v3_const.te_short) <
  232. subghz_protocol_intertechno_v3_const.te_delta)) {
  233. instance->decoder.parser_step = IntertechnoV3DecoderStepSaveDuration;
  234. } else {
  235. instance->decoder.parser_step = IntertechnoV3DecoderStepReset;
  236. }
  237. break;
  238. case IntertechnoV3DecoderStepSaveDuration:
  239. if(!level) { //save interval
  240. if(duration >= (subghz_protocol_intertechno_v3_const.te_short * 11)) {
  241. instance->decoder.parser_step = IntertechnoV3DecoderStepStartSync;
  242. if((instance->decoder.decode_count_bit ==
  243. subghz_protocol_intertechno_v3_const.min_count_bit_for_found) ||
  244. (instance->decoder.decode_count_bit == INTERTECHNO_V3_DIMMING_COUNT_BIT)) {
  245. instance->generic.data = instance->decoder.decode_data;
  246. instance->generic.data_count_bit = instance->decoder.decode_count_bit;
  247. if(instance->base.callback)
  248. instance->base.callback(&instance->base, instance->base.context);
  249. }
  250. break;
  251. }
  252. instance->decoder.te_last = duration;
  253. instance->decoder.parser_step = IntertechnoV3DecoderStepCheckDuration;
  254. } else {
  255. instance->decoder.parser_step = IntertechnoV3DecoderStepReset;
  256. }
  257. break;
  258. case IntertechnoV3DecoderStepCheckDuration:
  259. if(level) {
  260. //Add 0 bit
  261. if((DURATION_DIFF(
  262. instance->decoder.te_last, subghz_protocol_intertechno_v3_const.te_short) <
  263. subghz_protocol_intertechno_v3_const.te_delta) &&
  264. (DURATION_DIFF(duration, subghz_protocol_intertechno_v3_const.te_short) <
  265. subghz_protocol_intertechno_v3_const.te_delta)) {
  266. subghz_protocol_blocks_add_bit(&instance->decoder, 0);
  267. instance->decoder.parser_step = IntertechnoV3DecoderStepEndDuration;
  268. } else if(
  269. //Add 1 bit
  270. (DURATION_DIFF(
  271. instance->decoder.te_last, subghz_protocol_intertechno_v3_const.te_long) <
  272. subghz_protocol_intertechno_v3_const.te_delta * 2) &&
  273. (DURATION_DIFF(duration, subghz_protocol_intertechno_v3_const.te_short) <
  274. subghz_protocol_intertechno_v3_const.te_delta)) {
  275. subghz_protocol_blocks_add_bit(&instance->decoder, 1);
  276. instance->decoder.parser_step = IntertechnoV3DecoderStepEndDuration;
  277. } else if(
  278. //Add dimm_state
  279. (DURATION_DIFF(
  280. instance->decoder.te_last, subghz_protocol_intertechno_v3_const.te_short) <
  281. subghz_protocol_intertechno_v3_const.te_delta * 2) &&
  282. (DURATION_DIFF(duration, subghz_protocol_intertechno_v3_const.te_short) <
  283. subghz_protocol_intertechno_v3_const.te_delta) &&
  284. (instance->decoder.decode_count_bit == 27)) {
  285. subghz_protocol_blocks_add_bit(&instance->decoder, 0);
  286. instance->decoder.parser_step = IntertechnoV3DecoderStepEndDuration;
  287. } else
  288. instance->decoder.parser_step = IntertechnoV3DecoderStepReset;
  289. } else {
  290. instance->decoder.parser_step = IntertechnoV3DecoderStepReset;
  291. }
  292. break;
  293. case IntertechnoV3DecoderStepEndDuration:
  294. if(!level && ((DURATION_DIFF(duration, subghz_protocol_intertechno_v3_const.te_short) <
  295. subghz_protocol_intertechno_v3_const.te_delta) ||
  296. (DURATION_DIFF(duration, subghz_protocol_intertechno_v3_const.te_long) <
  297. subghz_protocol_intertechno_v3_const.te_delta * 2))) {
  298. instance->decoder.parser_step = IntertechnoV3DecoderStepStartDuration;
  299. } else {
  300. instance->decoder.parser_step = IntertechnoV3DecoderStepReset;
  301. }
  302. break;
  303. }
  304. }
  305. /**
  306. * Analysis of received data
  307. * @param instance Pointer to a SubGhzBlockGeneric* instance
  308. */
  309. static void subghz_protocol_intertechno_v3_check_remote_controller(SubGhzBlockGeneric* instance) {
  310. /*
  311. * A frame is either 32 or 36 bits:
  312. *
  313. * _
  314. * start bit: | |__________ (T,10T)
  315. * _ _
  316. * '0': | |_| |_____ (T,T,T,5T)
  317. * _ _
  318. * '1': | |_____| |_ (T,5T,T,T)
  319. * _ _
  320. * dimm: | |_| |_ (T,T,T,T)
  321. *
  322. * _
  323. * stop bit: | |____...____ (T,38T)
  324. *
  325. * if frame 32 bits
  326. * SSSSSSSSSSSSSSSSSSSSSSSSSS all_ch on/off ~ch
  327. * Key:0x3F86C59F => 00111111100001101100010110 0 1 1111
  328. *
  329. * if frame 36 bits
  330. * SSSSSSSSSSSSSSSSSSSSSSSSSS all_ch dimm ~ch dimm_level
  331. * Key:0x42D2E8856 => 01000010110100101110100010 0 X 0101 0110
  332. *
  333. */
  334. if(instance->data_count_bit == subghz_protocol_intertechno_v3_const.min_count_bit_for_found) {
  335. instance->serial = (instance->data >> 6) & 0x3FFFFFF;
  336. if((instance->data >> 5) & 0x1) {
  337. instance->cnt = 1 << 5;
  338. } else {
  339. instance->cnt = (~instance->data & 0xF);
  340. }
  341. instance->btn = (instance->data >> 4) & 0x1;
  342. } else if(instance->data_count_bit == INTERTECHNO_V3_DIMMING_COUNT_BIT) {
  343. instance->serial = (instance->data >> 10) & 0x3FFFFFF;
  344. if((instance->data >> 9) & 0x1) {
  345. instance->cnt = 1 << 5;
  346. } else {
  347. instance->cnt = (~(instance->data >> 4) & 0xF);
  348. }
  349. instance->btn = (instance->data) & 0xF;
  350. } else {
  351. instance->serial = 0;
  352. instance->cnt = 0;
  353. instance->btn = 0;
  354. }
  355. }
  356. uint8_t subghz_protocol_decoder_intertechno_v3_get_hash_data(void* context) {
  357. furi_assert(context);
  358. SubGhzProtocolDecoderIntertechno_V3* instance = context;
  359. return subghz_protocol_blocks_get_hash_data(
  360. &instance->decoder, (instance->decoder.decode_count_bit / 8) + 1);
  361. }
  362. SubGhzProtocolStatus subghz_protocol_decoder_intertechno_v3_serialize(
  363. void* context,
  364. FlipperFormat* flipper_format,
  365. SubGhzRadioPreset* preset) {
  366. furi_assert(context);
  367. SubGhzProtocolDecoderIntertechno_V3* instance = context;
  368. return subghz_block_generic_serialize(&instance->generic, flipper_format, preset);
  369. }
  370. SubGhzProtocolStatus subghz_protocol_decoder_intertechno_v3_deserialize(
  371. void* context,
  372. FlipperFormat* flipper_format) {
  373. furi_assert(context);
  374. SubGhzProtocolDecoderIntertechno_V3* instance = context;
  375. SubGhzProtocolStatus ret = SubGhzProtocolStatusError;
  376. do {
  377. ret = subghz_block_generic_deserialize(&instance->generic, flipper_format);
  378. if(ret != SubGhzProtocolStatusOk) {
  379. break;
  380. }
  381. if((instance->generic.data_count_bit !=
  382. subghz_protocol_intertechno_v3_const.min_count_bit_for_found) &&
  383. (instance->generic.data_count_bit != INTERTECHNO_V3_DIMMING_COUNT_BIT)) {
  384. FURI_LOG_E(TAG, "Wrong number of bits in key");
  385. ret = SubGhzProtocolStatusErrorValueBitCount;
  386. break;
  387. }
  388. } while(false);
  389. return ret;
  390. }
  391. void subghz_protocol_decoder_intertechno_v3_get_string(void* context, FuriString* output) {
  392. furi_assert(context);
  393. SubGhzProtocolDecoderIntertechno_V3* instance = context;
  394. subghz_protocol_intertechno_v3_check_remote_controller(&instance->generic);
  395. furi_string_cat_printf(
  396. output,
  397. "%.11s %db\r\n"
  398. "Key:0x%08llX\r\n"
  399. "Sn:%07lX\r\n",
  400. instance->generic.protocol_name,
  401. instance->generic.data_count_bit,
  402. instance->generic.data,
  403. instance->generic.serial);
  404. if(instance->generic.data_count_bit ==
  405. subghz_protocol_intertechno_v3_const.min_count_bit_for_found) {
  406. if(instance->generic.cnt >> 5) {
  407. furi_string_cat_printf(
  408. output, "Ch: All Btn:%s\r\n", (instance->generic.btn ? "On" : "Off"));
  409. } else {
  410. furi_string_cat_printf(
  411. output,
  412. "Ch:" CH_PATTERN " Btn:%s\r\n",
  413. CNT_TO_CH(instance->generic.cnt),
  414. (instance->generic.btn ? "On" : "Off"));
  415. }
  416. } else if(instance->generic.data_count_bit == INTERTECHNO_V3_DIMMING_COUNT_BIT) {
  417. furi_string_cat_printf(
  418. output,
  419. "Ch:" CH_PATTERN " Dimm:%d%%\r\n",
  420. CNT_TO_CH(instance->generic.cnt),
  421. (int)(6.67 * (float)instance->generic.btn));
  422. }
  423. }