chamberlain_code.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498
  1. #include "chamberlain_code.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 "SubGhzProtocolChamb_Code"
  8. #define CHAMBERLAIN_CODE_BIT_STOP 0b0001
  9. #define CHAMBERLAIN_CODE_BIT_1 0b0011
  10. #define CHAMBERLAIN_CODE_BIT_0 0b0111
  11. #define CHAMBERLAIN_7_CODE_MASK 0xF000000FF0F
  12. #define CHAMBERLAIN_8_CODE_MASK 0xF00000F00F
  13. #define CHAMBERLAIN_9_CODE_MASK 0xF000000000F
  14. #define CHAMBERLAIN_7_CODE_MASK_CHECK 0x10000001101
  15. #define CHAMBERLAIN_8_CODE_MASK_CHECK 0x1000001001
  16. #define CHAMBERLAIN_9_CODE_MASK_CHECK 0x10000000001
  17. #define CHAMBERLAIN_7_CODE_DIP_PATTERN "%c%c%c%c%c%c%c"
  18. #define CHAMBERLAIN_7_CODE_DATA_TO_DIP(dip) \
  19. (dip & 0x0040 ? '1' : '0'), (dip & 0x0020 ? '1' : '0'), (dip & 0x0010 ? '1' : '0'), \
  20. (dip & 0x0008 ? '1' : '0'), (dip & 0x0004 ? '1' : '0'), (dip & 0x0002 ? '1' : '0'), \
  21. (dip & 0x0001 ? '1' : '0')
  22. #define CHAMBERLAIN_8_CODE_DIP_PATTERN "%c%c%c%c%cx%c%c"
  23. #define CHAMBERLAIN_8_CODE_DATA_TO_DIP(dip) \
  24. (dip & 0x0080 ? '1' : '0'), (dip & 0x0040 ? '1' : '0'), (dip & 0x0020 ? '1' : '0'), \
  25. (dip & 0x0010 ? '1' : '0'), (dip & 0x0008 ? '1' : '0'), (dip & 0x0001 ? '1' : '0'), \
  26. (dip & 0x0002 ? '1' : '0')
  27. #define CHAMBERLAIN_9_CODE_DIP_PATTERN "%c%c%c%c%c%c%c%c%c"
  28. #define CHAMBERLAIN_9_CODE_DATA_TO_DIP(dip) \
  29. (dip & 0x0100 ? '1' : '0'), (dip & 0x0080 ? '1' : '0'), (dip & 0x0040 ? '1' : '0'), \
  30. (dip & 0x0020 ? '1' : '0'), (dip & 0x0010 ? '1' : '0'), (dip & 0x0008 ? '1' : '0'), \
  31. (dip & 0x0001 ? '1' : '0'), (dip & 0x0002 ? '1' : '0'), (dip & 0x0004 ? '1' : '0')
  32. static const SubGhzBlockConst subghz_protocol_chamb_code_const = {
  33. .te_short = 1000,
  34. .te_long = 3000,
  35. .te_delta = 200,
  36. .min_count_bit_for_found = 10,
  37. };
  38. struct SubGhzProtocolDecoderChamb_Code {
  39. SubGhzProtocolDecoderBase base;
  40. SubGhzBlockDecoder decoder;
  41. SubGhzBlockGeneric generic;
  42. };
  43. struct SubGhzProtocolEncoderChamb_Code {
  44. SubGhzProtocolEncoderBase base;
  45. SubGhzProtocolBlockEncoder encoder;
  46. SubGhzBlockGeneric generic;
  47. };
  48. typedef enum {
  49. Chamb_CodeDecoderStepReset = 0,
  50. Chamb_CodeDecoderStepFoundStartBit,
  51. Chamb_CodeDecoderStepSaveDuration,
  52. Chamb_CodeDecoderStepCheckDuration,
  53. } Chamb_CodeDecoderStep;
  54. const SubGhzProtocolDecoder subghz_protocol_chamb_code_decoder = {
  55. .alloc = subghz_protocol_decoder_chamb_code_alloc,
  56. .free = subghz_protocol_decoder_chamb_code_free,
  57. .feed = subghz_protocol_decoder_chamb_code_feed,
  58. .reset = subghz_protocol_decoder_chamb_code_reset,
  59. .get_hash_data = subghz_protocol_decoder_chamb_code_get_hash_data,
  60. .serialize = subghz_protocol_decoder_chamb_code_serialize,
  61. .deserialize = subghz_protocol_decoder_chamb_code_deserialize,
  62. .get_string = subghz_protocol_decoder_chamb_code_get_string,
  63. };
  64. const SubGhzProtocolEncoder subghz_protocol_chamb_code_encoder = {
  65. .alloc = subghz_protocol_encoder_chamb_code_alloc,
  66. .free = subghz_protocol_encoder_chamb_code_free,
  67. .deserialize = subghz_protocol_encoder_chamb_code_deserialize,
  68. .stop = subghz_protocol_encoder_chamb_code_stop,
  69. .yield = subghz_protocol_encoder_chamb_code_yield,
  70. };
  71. const SubGhzProtocol subghz_protocol_chamb_code = {
  72. .name = SUBGHZ_PROTOCOL_CHAMB_CODE_NAME,
  73. .type = SubGhzProtocolTypeStatic,
  74. .flag = SubGhzProtocolFlag_315 | SubGhzProtocolFlag_AM | SubGhzProtocolFlag_Decodable |
  75. SubGhzProtocolFlag_Load | SubGhzProtocolFlag_Save | SubGhzProtocolFlag_Send,
  76. .decoder = &subghz_protocol_chamb_code_decoder,
  77. .encoder = &subghz_protocol_chamb_code_encoder,
  78. };
  79. void* subghz_protocol_encoder_chamb_code_alloc(SubGhzEnvironment* environment) {
  80. UNUSED(environment);
  81. SubGhzProtocolEncoderChamb_Code* instance = malloc(sizeof(SubGhzProtocolEncoderChamb_Code));
  82. instance->base.protocol = &subghz_protocol_chamb_code;
  83. instance->generic.protocol_name = instance->base.protocol->name;
  84. instance->encoder.repeat = 10;
  85. instance->encoder.size_upload = 24;
  86. instance->encoder.upload = malloc(instance->encoder.size_upload * sizeof(LevelDuration));
  87. instance->encoder.is_running = false;
  88. return instance;
  89. }
  90. void subghz_protocol_encoder_chamb_code_free(void* context) {
  91. furi_assert(context);
  92. SubGhzProtocolEncoderChamb_Code* instance = context;
  93. free(instance->encoder.upload);
  94. free(instance);
  95. }
  96. static uint64_t subghz_protocol_chamb_bit_to_code(uint64_t data, uint8_t size) {
  97. uint64_t data_res = 0;
  98. for(uint8_t i = 0; i < size; i++) {
  99. if(!(bit_read(data, size - i - 1))) {
  100. data_res = data_res << 4 | CHAMBERLAIN_CODE_BIT_0;
  101. } else {
  102. data_res = data_res << 4 | CHAMBERLAIN_CODE_BIT_1;
  103. }
  104. }
  105. return data_res;
  106. }
  107. /**
  108. * Generating an upload from data.
  109. * @param instance Pointer to a SubGhzProtocolEncoderChamb_Code instance
  110. * @return true On success
  111. */
  112. static bool
  113. subghz_protocol_encoder_chamb_code_get_upload(SubGhzProtocolEncoderChamb_Code* instance) {
  114. furi_assert(instance);
  115. uint64_t data = subghz_protocol_chamb_bit_to_code(
  116. instance->generic.data, instance->generic.data_count_bit);
  117. switch(instance->generic.data_count_bit) {
  118. case 7:
  119. data = ((data >> 4) << 16) | (data & 0xF) << 4 | CHAMBERLAIN_7_CODE_MASK_CHECK;
  120. break;
  121. case 8:
  122. data = ((data >> 12) << 16) | (data & 0xFF) << 4 | CHAMBERLAIN_8_CODE_MASK_CHECK;
  123. break;
  124. case 9:
  125. data = (data << 4) | CHAMBERLAIN_9_CODE_MASK_CHECK;
  126. break;
  127. default:
  128. FURI_LOG_E(TAG, "Invalid bits count");
  129. return false;
  130. break;
  131. }
  132. #define UPLOAD_HEX_DATA_SIZE 10
  133. uint8_t upload_hex_data[UPLOAD_HEX_DATA_SIZE] = {0};
  134. size_t upload_hex_count_bit = 0;
  135. //insert guard time
  136. for(uint8_t i = 0; i < 36; i++) {
  137. subghz_protocol_blocks_set_bit_array(
  138. 0, upload_hex_data, upload_hex_count_bit++, UPLOAD_HEX_DATA_SIZE);
  139. }
  140. //insert data
  141. switch(instance->generic.data_count_bit) {
  142. case 7:
  143. case 9:
  144. for(uint8_t i = 44; i > 0; i--) {
  145. if(!bit_read(data, i - 1)) {
  146. subghz_protocol_blocks_set_bit_array(
  147. 0, upload_hex_data, upload_hex_count_bit++, UPLOAD_HEX_DATA_SIZE);
  148. } else {
  149. subghz_protocol_blocks_set_bit_array(
  150. 1, upload_hex_data, upload_hex_count_bit++, UPLOAD_HEX_DATA_SIZE);
  151. }
  152. }
  153. break;
  154. case 8:
  155. for(uint8_t i = 40; i > 0; i--) {
  156. if(!bit_read(data, i - 1)) {
  157. subghz_protocol_blocks_set_bit_array(
  158. 0, upload_hex_data, upload_hex_count_bit++, UPLOAD_HEX_DATA_SIZE);
  159. } else {
  160. subghz_protocol_blocks_set_bit_array(
  161. 1, upload_hex_data, upload_hex_count_bit++, UPLOAD_HEX_DATA_SIZE);
  162. }
  163. }
  164. break;
  165. }
  166. instance->encoder.size_upload = subghz_protocol_blocks_get_upload(
  167. upload_hex_data,
  168. upload_hex_count_bit,
  169. instance->encoder.upload,
  170. instance->encoder.size_upload,
  171. subghz_protocol_chamb_code_const.te_short);
  172. return true;
  173. }
  174. bool subghz_protocol_encoder_chamb_code_deserialize(void* context, FlipperFormat* flipper_format) {
  175. furi_assert(context);
  176. SubGhzProtocolEncoderChamb_Code* instance = context;
  177. bool res = false;
  178. do {
  179. if(!subghz_block_generic_deserialize(&instance->generic, flipper_format)) {
  180. FURI_LOG_E(TAG, "Deserialize error");
  181. break;
  182. }
  183. if(instance->generic.data_count_bit >
  184. subghz_protocol_chamb_code_const.min_count_bit_for_found) {
  185. FURI_LOG_E(TAG, "Wrong number of bits in key");
  186. break;
  187. }
  188. //optional parameter parameter
  189. flipper_format_read_uint32(
  190. flipper_format, "Repeat", (uint32_t*)&instance->encoder.repeat, 1);
  191. if(!subghz_protocol_encoder_chamb_code_get_upload(instance)) break;
  192. instance->encoder.is_running = true;
  193. res = true;
  194. } while(false);
  195. return res;
  196. }
  197. void subghz_protocol_encoder_chamb_code_stop(void* context) {
  198. SubGhzProtocolEncoderChamb_Code* instance = context;
  199. instance->encoder.is_running = false;
  200. }
  201. LevelDuration subghz_protocol_encoder_chamb_code_yield(void* context) {
  202. SubGhzProtocolEncoderChamb_Code* instance = context;
  203. if(instance->encoder.repeat == 0 || !instance->encoder.is_running) {
  204. instance->encoder.is_running = false;
  205. return level_duration_reset();
  206. }
  207. LevelDuration ret = instance->encoder.upload[instance->encoder.front];
  208. if(++instance->encoder.front == instance->encoder.size_upload) {
  209. instance->encoder.repeat--;
  210. instance->encoder.front = 0;
  211. }
  212. return ret;
  213. }
  214. void* subghz_protocol_decoder_chamb_code_alloc(SubGhzEnvironment* environment) {
  215. UNUSED(environment);
  216. SubGhzProtocolDecoderChamb_Code* instance = malloc(sizeof(SubGhzProtocolDecoderChamb_Code));
  217. instance->base.protocol = &subghz_protocol_chamb_code;
  218. instance->generic.protocol_name = instance->base.protocol->name;
  219. return instance;
  220. }
  221. void subghz_protocol_decoder_chamb_code_free(void* context) {
  222. furi_assert(context);
  223. SubGhzProtocolDecoderChamb_Code* instance = context;
  224. free(instance);
  225. }
  226. void subghz_protocol_decoder_chamb_code_reset(void* context) {
  227. furi_assert(context);
  228. SubGhzProtocolDecoderChamb_Code* instance = context;
  229. instance->decoder.parser_step = Chamb_CodeDecoderStepReset;
  230. }
  231. static bool subghz_protocol_chamb_code_to_bit(uint64_t* data, uint8_t size) {
  232. uint64_t data_tmp = data[0];
  233. uint64_t data_res = 0;
  234. for(uint8_t i = 0; i < size; i++) {
  235. if((data_tmp & 0xF) == CHAMBERLAIN_CODE_BIT_0) {
  236. bit_write(data_res, i, 0);
  237. } else if((data_tmp & 0xF) == CHAMBERLAIN_CODE_BIT_1) {
  238. bit_write(data_res, i, 1);
  239. } else {
  240. return false;
  241. }
  242. data_tmp >>= 4;
  243. }
  244. data[0] = data_res;
  245. return true;
  246. }
  247. static bool subghz_protocol_decoder_chamb_code_check_mask_and_parse(
  248. SubGhzProtocolDecoderChamb_Code* instance) {
  249. furi_assert(instance);
  250. if(instance->decoder.decode_count_bit >
  251. subghz_protocol_chamb_code_const.min_count_bit_for_found + 1)
  252. return false;
  253. if((instance->decoder.decode_data & CHAMBERLAIN_7_CODE_MASK) ==
  254. CHAMBERLAIN_7_CODE_MASK_CHECK) {
  255. instance->decoder.decode_count_bit = 7;
  256. instance->decoder.decode_data &= ~CHAMBERLAIN_7_CODE_MASK;
  257. instance->decoder.decode_data = (instance->decoder.decode_data >> 12) |
  258. ((instance->decoder.decode_data >> 4) & 0xF);
  259. } else if(
  260. (instance->decoder.decode_data & CHAMBERLAIN_8_CODE_MASK) ==
  261. CHAMBERLAIN_8_CODE_MASK_CHECK) {
  262. instance->decoder.decode_count_bit = 8;
  263. instance->decoder.decode_data &= ~CHAMBERLAIN_8_CODE_MASK;
  264. instance->decoder.decode_data = instance->decoder.decode_data >> 4 |
  265. CHAMBERLAIN_CODE_BIT_0 << 8; //DIP 6 no use
  266. } else if(
  267. (instance->decoder.decode_data & CHAMBERLAIN_9_CODE_MASK) ==
  268. CHAMBERLAIN_9_CODE_MASK_CHECK) {
  269. instance->decoder.decode_count_bit = 9;
  270. instance->decoder.decode_data &= ~CHAMBERLAIN_9_CODE_MASK;
  271. instance->decoder.decode_data >>= 4;
  272. } else {
  273. return false;
  274. }
  275. return subghz_protocol_chamb_code_to_bit(
  276. &instance->decoder.decode_data, instance->decoder.decode_count_bit);
  277. }
  278. void subghz_protocol_decoder_chamb_code_feed(void* context, bool level, uint32_t duration) {
  279. furi_assert(context);
  280. SubGhzProtocolDecoderChamb_Code* instance = context;
  281. switch(instance->decoder.parser_step) {
  282. case Chamb_CodeDecoderStepReset:
  283. if((!level) && (DURATION_DIFF(duration, subghz_protocol_chamb_code_const.te_short * 39) <
  284. subghz_protocol_chamb_code_const.te_delta * 20)) {
  285. //Found header Chamb_Code
  286. instance->decoder.parser_step = Chamb_CodeDecoderStepFoundStartBit;
  287. }
  288. break;
  289. case Chamb_CodeDecoderStepFoundStartBit:
  290. if((level) && (DURATION_DIFF(duration, subghz_protocol_chamb_code_const.te_short) <
  291. subghz_protocol_chamb_code_const.te_delta)) {
  292. //Found start bit Chamb_Code
  293. instance->decoder.decode_data = 0;
  294. instance->decoder.decode_count_bit = 0;
  295. instance->decoder.decode_data = instance->decoder.decode_data << 4 |
  296. CHAMBERLAIN_CODE_BIT_STOP;
  297. instance->decoder.decode_count_bit++;
  298. instance->decoder.parser_step = Chamb_CodeDecoderStepSaveDuration;
  299. } else {
  300. instance->decoder.parser_step = Chamb_CodeDecoderStepReset;
  301. }
  302. break;
  303. case Chamb_CodeDecoderStepSaveDuration:
  304. if(!level) { //save interval
  305. if(duration > subghz_protocol_chamb_code_const.te_short * 5) {
  306. if(instance->decoder.decode_count_bit >=
  307. subghz_protocol_chamb_code_const.min_count_bit_for_found) {
  308. instance->generic.serial = 0x0;
  309. instance->generic.btn = 0x0;
  310. if(subghz_protocol_decoder_chamb_code_check_mask_and_parse(instance)) {
  311. instance->generic.data = instance->decoder.decode_data;
  312. instance->generic.data_count_bit = instance->decoder.decode_count_bit;
  313. if(instance->base.callback)
  314. instance->base.callback(&instance->base, instance->base.context);
  315. }
  316. }
  317. instance->decoder.parser_step = Chamb_CodeDecoderStepReset;
  318. } else {
  319. instance->decoder.te_last = duration;
  320. instance->decoder.parser_step = Chamb_CodeDecoderStepCheckDuration;
  321. }
  322. } else {
  323. instance->decoder.parser_step = Chamb_CodeDecoderStepReset;
  324. }
  325. break;
  326. case Chamb_CodeDecoderStepCheckDuration:
  327. if(level) {
  328. if((DURATION_DIFF( //Found stop bit Chamb_Code
  329. instance->decoder.te_last,
  330. subghz_protocol_chamb_code_const.te_short * 3) <
  331. subghz_protocol_chamb_code_const.te_delta) &&
  332. (DURATION_DIFF(duration, subghz_protocol_chamb_code_const.te_short) <
  333. subghz_protocol_chamb_code_const.te_delta)) {
  334. instance->decoder.decode_data = instance->decoder.decode_data << 4 |
  335. CHAMBERLAIN_CODE_BIT_STOP;
  336. instance->decoder.decode_count_bit++;
  337. instance->decoder.parser_step = Chamb_CodeDecoderStepSaveDuration;
  338. } else if(
  339. (DURATION_DIFF(
  340. instance->decoder.te_last, subghz_protocol_chamb_code_const.te_short * 2) <
  341. subghz_protocol_chamb_code_const.te_delta) &&
  342. (DURATION_DIFF(duration, subghz_protocol_chamb_code_const.te_short * 2) <
  343. subghz_protocol_chamb_code_const.te_delta)) {
  344. instance->decoder.decode_data = instance->decoder.decode_data << 4 |
  345. CHAMBERLAIN_CODE_BIT_1;
  346. instance->decoder.decode_count_bit++;
  347. instance->decoder.parser_step = Chamb_CodeDecoderStepSaveDuration;
  348. } else if(
  349. (DURATION_DIFF(
  350. instance->decoder.te_last, subghz_protocol_chamb_code_const.te_short) <
  351. subghz_protocol_chamb_code_const.te_delta) &&
  352. (DURATION_DIFF(duration, subghz_protocol_chamb_code_const.te_short * 3) <
  353. subghz_protocol_chamb_code_const.te_delta)) {
  354. instance->decoder.decode_data = instance->decoder.decode_data << 4 |
  355. CHAMBERLAIN_CODE_BIT_0;
  356. instance->decoder.decode_count_bit++;
  357. instance->decoder.parser_step = Chamb_CodeDecoderStepSaveDuration;
  358. } else {
  359. instance->decoder.parser_step = Chamb_CodeDecoderStepReset;
  360. }
  361. } else {
  362. instance->decoder.parser_step = Chamb_CodeDecoderStepReset;
  363. }
  364. break;
  365. }
  366. }
  367. uint8_t subghz_protocol_decoder_chamb_code_get_hash_data(void* context) {
  368. furi_assert(context);
  369. SubGhzProtocolDecoderChamb_Code* instance = context;
  370. return subghz_protocol_blocks_get_hash_data(
  371. &instance->decoder, (instance->decoder.decode_count_bit / 8) + 1);
  372. }
  373. bool subghz_protocol_decoder_chamb_code_serialize(
  374. void* context,
  375. FlipperFormat* flipper_format,
  376. SubGhzPresetDefinition* preset) {
  377. furi_assert(context);
  378. SubGhzProtocolDecoderChamb_Code* instance = context;
  379. return subghz_block_generic_serialize(&instance->generic, flipper_format, preset);
  380. }
  381. bool subghz_protocol_decoder_chamb_code_deserialize(void* context, FlipperFormat* flipper_format) {
  382. furi_assert(context);
  383. SubGhzProtocolDecoderChamb_Code* instance = context;
  384. bool ret = false;
  385. do {
  386. if(!subghz_block_generic_deserialize(&instance->generic, flipper_format)) {
  387. break;
  388. }
  389. if(instance->generic.data_count_bit >
  390. subghz_protocol_chamb_code_const.min_count_bit_for_found) {
  391. FURI_LOG_E(TAG, "Wrong number of bits in key");
  392. break;
  393. }
  394. ret = true;
  395. } while(false);
  396. return ret;
  397. }
  398. void subghz_protocol_decoder_chamb_code_get_string(void* context, FuriString* output) {
  399. furi_assert(context);
  400. SubGhzProtocolDecoderChamb_Code* instance = context;
  401. uint32_t code_found_lo = instance->generic.data & 0x00000000ffffffff;
  402. uint64_t code_found_reverse = subghz_protocol_blocks_reverse_key(
  403. instance->generic.data, instance->generic.data_count_bit);
  404. uint32_t code_found_reverse_lo = code_found_reverse & 0x00000000ffffffff;
  405. furi_string_cat_printf(
  406. output,
  407. "%s %db\r\n"
  408. "Key:0x%03lX\r\n"
  409. "Yek:0x%03lX\r\n",
  410. instance->generic.protocol_name,
  411. instance->generic.data_count_bit,
  412. code_found_lo,
  413. code_found_reverse_lo);
  414. switch(instance->generic.data_count_bit) {
  415. case 7:
  416. furi_string_cat_printf(
  417. output,
  418. "DIP:" CHAMBERLAIN_7_CODE_DIP_PATTERN "\r\n",
  419. CHAMBERLAIN_7_CODE_DATA_TO_DIP(code_found_lo));
  420. break;
  421. case 8:
  422. furi_string_cat_printf(
  423. output,
  424. "DIP:" CHAMBERLAIN_8_CODE_DIP_PATTERN "\r\n",
  425. CHAMBERLAIN_8_CODE_DATA_TO_DIP(code_found_lo));
  426. break;
  427. case 9:
  428. furi_string_cat_printf(
  429. output,
  430. "DIP:" CHAMBERLAIN_9_CODE_DIP_PATTERN "\r\n",
  431. CHAMBERLAIN_9_CODE_DATA_TO_DIP(code_found_lo));
  432. break;
  433. default:
  434. break;
  435. }
  436. }