chamberlain_code.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483
  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_runing = 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_crash(TAG " unknown protocol.");
  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. //optional parameter parameter
  184. flipper_format_read_uint32(
  185. flipper_format, "Repeat", (uint32_t*)&instance->encoder.repeat, 1);
  186. subghz_protocol_encoder_chamb_code_get_upload(instance);
  187. instance->encoder.is_runing = true;
  188. res = true;
  189. } while(false);
  190. return res;
  191. }
  192. void subghz_protocol_encoder_chamb_code_stop(void* context) {
  193. SubGhzProtocolEncoderChamb_Code* instance = context;
  194. instance->encoder.is_runing = false;
  195. }
  196. LevelDuration subghz_protocol_encoder_chamb_code_yield(void* context) {
  197. SubGhzProtocolEncoderChamb_Code* instance = context;
  198. if(instance->encoder.repeat == 0 || !instance->encoder.is_runing) {
  199. instance->encoder.is_runing = false;
  200. return level_duration_reset();
  201. }
  202. LevelDuration ret = instance->encoder.upload[instance->encoder.front];
  203. if(++instance->encoder.front == instance->encoder.size_upload) {
  204. instance->encoder.repeat--;
  205. instance->encoder.front = 0;
  206. }
  207. return ret;
  208. }
  209. void* subghz_protocol_decoder_chamb_code_alloc(SubGhzEnvironment* environment) {
  210. UNUSED(environment);
  211. SubGhzProtocolDecoderChamb_Code* instance = malloc(sizeof(SubGhzProtocolDecoderChamb_Code));
  212. instance->base.protocol = &subghz_protocol_chamb_code;
  213. instance->generic.protocol_name = instance->base.protocol->name;
  214. return instance;
  215. }
  216. void subghz_protocol_decoder_chamb_code_free(void* context) {
  217. furi_assert(context);
  218. SubGhzProtocolDecoderChamb_Code* instance = context;
  219. free(instance);
  220. }
  221. void subghz_protocol_decoder_chamb_code_reset(void* context) {
  222. furi_assert(context);
  223. SubGhzProtocolDecoderChamb_Code* instance = context;
  224. instance->decoder.parser_step = Chamb_CodeDecoderStepReset;
  225. }
  226. static bool subghz_protocol_chamb_code_to_bit(uint64_t* data, uint8_t size) {
  227. uint64_t data_tmp = data[0];
  228. uint64_t data_res = 0;
  229. for(uint8_t i = 0; i < size; i++) {
  230. if((data_tmp & 0xF) == CHAMBERLAIN_CODE_BIT_0) {
  231. bit_write(data_res, i, 0);
  232. } else if((data_tmp & 0xF) == CHAMBERLAIN_CODE_BIT_1) {
  233. bit_write(data_res, i, 1);
  234. } else {
  235. return false;
  236. }
  237. data_tmp >>= 4;
  238. }
  239. data[0] = data_res;
  240. return true;
  241. }
  242. static bool subghz_protocol_decoder_chamb_code_check_mask_and_parse(
  243. SubGhzProtocolDecoderChamb_Code* instance) {
  244. furi_assert(instance);
  245. if(instance->decoder.decode_count_bit >
  246. subghz_protocol_chamb_code_const.min_count_bit_for_found + 1)
  247. return false;
  248. if((instance->decoder.decode_data & CHAMBERLAIN_7_CODE_MASK) ==
  249. CHAMBERLAIN_7_CODE_MASK_CHECK) {
  250. instance->decoder.decode_count_bit = 7;
  251. instance->decoder.decode_data &= ~CHAMBERLAIN_7_CODE_MASK;
  252. instance->decoder.decode_data = (instance->decoder.decode_data >> 12) |
  253. ((instance->decoder.decode_data >> 4) & 0xF);
  254. } else if(
  255. (instance->decoder.decode_data & CHAMBERLAIN_8_CODE_MASK) ==
  256. CHAMBERLAIN_8_CODE_MASK_CHECK) {
  257. instance->decoder.decode_count_bit = 8;
  258. instance->decoder.decode_data &= ~CHAMBERLAIN_8_CODE_MASK;
  259. instance->decoder.decode_data = instance->decoder.decode_data >> 4 |
  260. CHAMBERLAIN_CODE_BIT_0 << 8; //DIP 6 no use
  261. } else if(
  262. (instance->decoder.decode_data & CHAMBERLAIN_9_CODE_MASK) ==
  263. CHAMBERLAIN_9_CODE_MASK_CHECK) {
  264. instance->decoder.decode_count_bit = 9;
  265. instance->decoder.decode_data &= ~CHAMBERLAIN_9_CODE_MASK;
  266. instance->decoder.decode_data >>= 4;
  267. } else {
  268. return false;
  269. }
  270. return subghz_protocol_chamb_code_to_bit(
  271. &instance->decoder.decode_data, instance->decoder.decode_count_bit);
  272. }
  273. void subghz_protocol_decoder_chamb_code_feed(void* context, bool level, uint32_t duration) {
  274. furi_assert(context);
  275. SubGhzProtocolDecoderChamb_Code* instance = context;
  276. switch(instance->decoder.parser_step) {
  277. case Chamb_CodeDecoderStepReset:
  278. if((!level) && (DURATION_DIFF(duration, subghz_protocol_chamb_code_const.te_short * 39) <
  279. subghz_protocol_chamb_code_const.te_delta * 20)) {
  280. //Found header Chamb_Code
  281. instance->decoder.parser_step = Chamb_CodeDecoderStepFoundStartBit;
  282. }
  283. break;
  284. case Chamb_CodeDecoderStepFoundStartBit:
  285. if((level) && (DURATION_DIFF(duration, subghz_protocol_chamb_code_const.te_short) <
  286. subghz_protocol_chamb_code_const.te_delta)) {
  287. //Found start bit Chamb_Code
  288. instance->decoder.decode_data = 0;
  289. instance->decoder.decode_count_bit = 0;
  290. instance->decoder.decode_data = instance->decoder.decode_data << 4 |
  291. CHAMBERLAIN_CODE_BIT_STOP;
  292. instance->decoder.decode_count_bit++;
  293. instance->decoder.parser_step = Chamb_CodeDecoderStepSaveDuration;
  294. } else {
  295. instance->decoder.parser_step = Chamb_CodeDecoderStepReset;
  296. }
  297. break;
  298. case Chamb_CodeDecoderStepSaveDuration:
  299. if(!level) { //save interval
  300. if(duration > subghz_protocol_chamb_code_const.te_short * 5) {
  301. if(instance->decoder.decode_count_bit >=
  302. subghz_protocol_chamb_code_const.min_count_bit_for_found) {
  303. instance->generic.serial = 0x0;
  304. instance->generic.btn = 0x0;
  305. if(subghz_protocol_decoder_chamb_code_check_mask_and_parse(instance)) {
  306. instance->generic.data = instance->decoder.decode_data;
  307. instance->generic.data_count_bit = instance->decoder.decode_count_bit;
  308. if(instance->base.callback)
  309. instance->base.callback(&instance->base, instance->base.context);
  310. }
  311. }
  312. instance->decoder.parser_step = Chamb_CodeDecoderStepReset;
  313. } else {
  314. instance->decoder.te_last = duration;
  315. instance->decoder.parser_step = Chamb_CodeDecoderStepCheckDuration;
  316. }
  317. } else {
  318. instance->decoder.parser_step = Chamb_CodeDecoderStepReset;
  319. }
  320. break;
  321. case Chamb_CodeDecoderStepCheckDuration:
  322. if(level) {
  323. if((DURATION_DIFF( //Found stop bit Chamb_Code
  324. instance->decoder.te_last,
  325. subghz_protocol_chamb_code_const.te_short * 3) <
  326. subghz_protocol_chamb_code_const.te_delta) &&
  327. (DURATION_DIFF(duration, subghz_protocol_chamb_code_const.te_short) <
  328. subghz_protocol_chamb_code_const.te_delta)) {
  329. instance->decoder.decode_data = instance->decoder.decode_data << 4 |
  330. CHAMBERLAIN_CODE_BIT_STOP;
  331. instance->decoder.decode_count_bit++;
  332. instance->decoder.parser_step = Chamb_CodeDecoderStepSaveDuration;
  333. } else if(
  334. (DURATION_DIFF(
  335. instance->decoder.te_last, subghz_protocol_chamb_code_const.te_short * 2) <
  336. subghz_protocol_chamb_code_const.te_delta) &&
  337. (DURATION_DIFF(duration, subghz_protocol_chamb_code_const.te_short * 2) <
  338. subghz_protocol_chamb_code_const.te_delta)) {
  339. instance->decoder.decode_data = instance->decoder.decode_data << 4 |
  340. CHAMBERLAIN_CODE_BIT_1;
  341. instance->decoder.decode_count_bit++;
  342. instance->decoder.parser_step = Chamb_CodeDecoderStepSaveDuration;
  343. } else if(
  344. (DURATION_DIFF(
  345. instance->decoder.te_last, subghz_protocol_chamb_code_const.te_short) <
  346. subghz_protocol_chamb_code_const.te_delta) &&
  347. (DURATION_DIFF(duration, subghz_protocol_chamb_code_const.te_short * 3) <
  348. subghz_protocol_chamb_code_const.te_delta)) {
  349. instance->decoder.decode_data = instance->decoder.decode_data << 4 |
  350. CHAMBERLAIN_CODE_BIT_0;
  351. instance->decoder.decode_count_bit++;
  352. instance->decoder.parser_step = Chamb_CodeDecoderStepSaveDuration;
  353. } else {
  354. instance->decoder.parser_step = Chamb_CodeDecoderStepReset;
  355. }
  356. } else {
  357. instance->decoder.parser_step = Chamb_CodeDecoderStepReset;
  358. }
  359. break;
  360. }
  361. }
  362. uint8_t subghz_protocol_decoder_chamb_code_get_hash_data(void* context) {
  363. furi_assert(context);
  364. SubGhzProtocolDecoderChamb_Code* instance = context;
  365. return subghz_protocol_blocks_get_hash_data(
  366. &instance->decoder, (instance->decoder.decode_count_bit / 8) + 1);
  367. }
  368. bool subghz_protocol_decoder_chamb_code_serialize(
  369. void* context,
  370. FlipperFormat* flipper_format,
  371. uint32_t frequency,
  372. FuriHalSubGhzPreset preset) {
  373. furi_assert(context);
  374. SubGhzProtocolDecoderChamb_Code* instance = context;
  375. return subghz_block_generic_serialize(&instance->generic, flipper_format, frequency, preset);
  376. }
  377. bool subghz_protocol_decoder_chamb_code_deserialize(void* context, FlipperFormat* flipper_format) {
  378. furi_assert(context);
  379. SubGhzProtocolDecoderChamb_Code* instance = context;
  380. return subghz_block_generic_deserialize(&instance->generic, flipper_format);
  381. }
  382. void subghz_protocol_decoder_chamb_code_get_string(void* context, string_t output) {
  383. furi_assert(context);
  384. SubGhzProtocolDecoderChamb_Code* instance = context;
  385. uint32_t code_found_lo = instance->generic.data & 0x00000000ffffffff;
  386. uint64_t code_found_reverse = subghz_protocol_blocks_reverse_key(
  387. instance->generic.data, instance->generic.data_count_bit);
  388. uint32_t code_found_reverse_lo = code_found_reverse & 0x00000000ffffffff;
  389. string_cat_printf(
  390. output,
  391. "%s %db\r\n"
  392. "Key:0x%03lX\r\n"
  393. "Yek:0x%03lX\r\n",
  394. instance->generic.protocol_name,
  395. instance->generic.data_count_bit,
  396. code_found_lo,
  397. code_found_reverse_lo);
  398. switch(instance->generic.data_count_bit) {
  399. case 7:
  400. string_cat_printf(
  401. output,
  402. "DIP:" CHAMBERLAIN_7_CODE_DIP_PATTERN "\r\n",
  403. CHAMBERLAIN_7_CODE_DATA_TO_DIP(code_found_lo));
  404. break;
  405. case 8:
  406. string_cat_printf(
  407. output,
  408. "DIP:" CHAMBERLAIN_8_CODE_DIP_PATTERN "\r\n",
  409. CHAMBERLAIN_8_CODE_DATA_TO_DIP(code_found_lo));
  410. break;
  411. case 9:
  412. string_cat_printf(
  413. output,
  414. "DIP:" CHAMBERLAIN_9_CODE_DIP_PATTERN "\r\n",
  415. CHAMBERLAIN_9_CODE_DATA_TO_DIP(code_found_lo));
  416. break;
  417. default:
  418. break;
  419. }
  420. }