megacode.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429
  1. #include "megacode.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. /*
  8. * Help
  9. * https://wiki.cuvoodoo.info/doku.php?id=megacode
  10. * https://wiki.cuvoodoo.info/lib/exe/fetch.php?media=megacode:megacode_1.pdf
  11. * https://fccid.io/EF4ACP00872/Test-Report/Megacode-2-112615.pdf
  12. * https://github.com/aaronsp777/megadecoder
  13. * https://github.com/rjmendez/Linear_keyfob
  14. * https://github.com/j07rdi/Linear_MegaCode_Garage_Remote
  15. *
  16. */
  17. #define TAG "SubGhzProtocolMegaCode"
  18. static const SubGhzBlockConst subghz_protocol_megacode_const = {
  19. .te_short = 1000,
  20. .te_long = 1000,
  21. .te_delta = 200,
  22. .min_count_bit_for_found = 24,
  23. };
  24. struct SubGhzProtocolDecoderMegaCode {
  25. SubGhzProtocolDecoderBase base;
  26. SubGhzBlockDecoder decoder;
  27. SubGhzBlockGeneric generic;
  28. uint8_t last_bit;
  29. };
  30. struct SubGhzProtocolEncoderMegaCode {
  31. SubGhzProtocolEncoderBase base;
  32. SubGhzProtocolBlockEncoder encoder;
  33. SubGhzBlockGeneric generic;
  34. };
  35. typedef enum {
  36. MegaCodeDecoderStepReset = 0,
  37. MegaCodeDecoderStepFoundStartBit,
  38. MegaCodeDecoderStepSaveDuration,
  39. MegaCodeDecoderStepCheckDuration,
  40. } MegaCodeDecoderStep;
  41. const SubGhzProtocolDecoder subghz_protocol_megacode_decoder = {
  42. .alloc = subghz_protocol_decoder_megacode_alloc,
  43. .free = subghz_protocol_decoder_megacode_free,
  44. .feed = subghz_protocol_decoder_megacode_feed,
  45. .reset = subghz_protocol_decoder_megacode_reset,
  46. .get_hash_data = subghz_protocol_decoder_megacode_get_hash_data,
  47. .serialize = subghz_protocol_decoder_megacode_serialize,
  48. .deserialize = subghz_protocol_decoder_megacode_deserialize,
  49. .get_string = subghz_protocol_decoder_megacode_get_string,
  50. };
  51. const SubGhzProtocolEncoder subghz_protocol_megacode_encoder = {
  52. .alloc = subghz_protocol_encoder_megacode_alloc,
  53. .free = subghz_protocol_encoder_megacode_free,
  54. .deserialize = subghz_protocol_encoder_megacode_deserialize,
  55. .stop = subghz_protocol_encoder_megacode_stop,
  56. .yield = subghz_protocol_encoder_megacode_yield,
  57. };
  58. const SubGhzProtocol subghz_protocol_megacode = {
  59. .name = SUBGHZ_PROTOCOL_MEGACODE_NAME,
  60. .type = SubGhzProtocolTypeStatic,
  61. .flag = SubGhzProtocolFlag_315 | SubGhzProtocolFlag_AM | SubGhzProtocolFlag_Decodable |
  62. SubGhzProtocolFlag_Load | SubGhzProtocolFlag_Save | SubGhzProtocolFlag_Send,
  63. .decoder = &subghz_protocol_megacode_decoder,
  64. .encoder = &subghz_protocol_megacode_encoder,
  65. };
  66. void* subghz_protocol_encoder_megacode_alloc(SubGhzEnvironment* environment) {
  67. UNUSED(environment);
  68. SubGhzProtocolEncoderMegaCode* instance = malloc(sizeof(SubGhzProtocolEncoderMegaCode));
  69. instance->base.protocol = &subghz_protocol_megacode;
  70. instance->generic.protocol_name = instance->base.protocol->name;
  71. instance->encoder.repeat = 10;
  72. instance->encoder.size_upload = 52;
  73. instance->encoder.upload = malloc(instance->encoder.size_upload * sizeof(LevelDuration));
  74. instance->encoder.is_running = false;
  75. return instance;
  76. }
  77. void subghz_protocol_encoder_megacode_free(void* context) {
  78. furi_assert(context);
  79. SubGhzProtocolEncoderMegaCode* instance = context;
  80. free(instance->encoder.upload);
  81. free(instance);
  82. }
  83. /**
  84. * Generating an upload from data.
  85. * @param instance Pointer to a SubGhzProtocolEncoderMegaCode instance
  86. * @return true On success
  87. */
  88. static bool subghz_protocol_encoder_megacode_get_upload(SubGhzProtocolEncoderMegaCode* instance) {
  89. furi_assert(instance);
  90. uint8_t last_bit = 0;
  91. size_t size_upload = (instance->generic.data_count_bit * 2);
  92. if(size_upload > instance->encoder.size_upload) {
  93. FURI_LOG_E(TAG, "Size upload exceeds allocated encoder buffer.");
  94. return false;
  95. } else {
  96. instance->encoder.size_upload = size_upload;
  97. }
  98. /*
  99. * Due to the nature of the protocol
  100. *
  101. * 00000 1
  102. * _____|-| = 1 becomes
  103. *
  104. * 00 1 000
  105. * __|-|___ = 0 becomes
  106. *
  107. * it's easier for us to generate an upload backwards
  108. */
  109. size_t index = size_upload - 1;
  110. // Send end level
  111. instance->encoder.upload[index--] =
  112. level_duration_make(true, (uint32_t)subghz_protocol_megacode_const.te_short);
  113. if(bit_read(instance->generic.data, 0)) {
  114. last_bit = 1;
  115. } else {
  116. last_bit = 0;
  117. }
  118. //Send key data
  119. for(uint8_t i = 1; i < instance->generic.data_count_bit; i++) {
  120. if(bit_read(instance->generic.data, i)) {
  121. //if bit 1
  122. instance->encoder.upload[index--] = level_duration_make(
  123. false,
  124. last_bit ? (uint32_t)subghz_protocol_megacode_const.te_short * 5 :
  125. (uint32_t)subghz_protocol_megacode_const.te_short * 2);
  126. last_bit = 1;
  127. } else {
  128. //if bit 0
  129. instance->encoder.upload[index--] = level_duration_make(
  130. false,
  131. last_bit ? (uint32_t)subghz_protocol_megacode_const.te_short * 8 :
  132. (uint32_t)subghz_protocol_megacode_const.te_short * 5);
  133. last_bit = 0;
  134. }
  135. instance->encoder.upload[index--] =
  136. level_duration_make(true, (uint32_t)subghz_protocol_megacode_const.te_short);
  137. }
  138. //Send PT_GUARD
  139. if(bit_read(instance->generic.data, 0)) {
  140. //if end bit 1
  141. instance->encoder.upload[index] =
  142. level_duration_make(false, (uint32_t)subghz_protocol_megacode_const.te_short * 11);
  143. } else {
  144. //if end bit 1
  145. instance->encoder.upload[index] =
  146. level_duration_make(false, (uint32_t)subghz_protocol_megacode_const.te_short * 14);
  147. }
  148. return true;
  149. }
  150. bool subghz_protocol_encoder_megacode_deserialize(void* context, FlipperFormat* flipper_format) {
  151. furi_assert(context);
  152. SubGhzProtocolEncoderMegaCode* instance = context;
  153. bool res = false;
  154. do {
  155. if(!subghz_block_generic_deserialize(&instance->generic, flipper_format)) {
  156. FURI_LOG_E(TAG, "Deserialize error");
  157. break;
  158. }
  159. if(instance->generic.data_count_bit !=
  160. subghz_protocol_megacode_const.min_count_bit_for_found) {
  161. FURI_LOG_E(TAG, "Wrong number of bits in key");
  162. break;
  163. }
  164. //optional parameter parameter
  165. flipper_format_read_uint32(
  166. flipper_format, "Repeat", (uint32_t*)&instance->encoder.repeat, 1);
  167. subghz_protocol_encoder_megacode_get_upload(instance);
  168. instance->encoder.is_running = true;
  169. res = true;
  170. } while(false);
  171. return res;
  172. }
  173. void subghz_protocol_encoder_megacode_stop(void* context) {
  174. SubGhzProtocolEncoderMegaCode* instance = context;
  175. instance->encoder.is_running = false;
  176. }
  177. LevelDuration subghz_protocol_encoder_megacode_yield(void* context) {
  178. SubGhzProtocolEncoderMegaCode* instance = context;
  179. if(instance->encoder.repeat == 0 || !instance->encoder.is_running) {
  180. instance->encoder.is_running = false;
  181. return level_duration_reset();
  182. }
  183. LevelDuration ret = instance->encoder.upload[instance->encoder.front];
  184. if(++instance->encoder.front == instance->encoder.size_upload) {
  185. instance->encoder.repeat--;
  186. instance->encoder.front = 0;
  187. }
  188. return ret;
  189. }
  190. void* subghz_protocol_decoder_megacode_alloc(SubGhzEnvironment* environment) {
  191. UNUSED(environment);
  192. SubGhzProtocolDecoderMegaCode* instance = malloc(sizeof(SubGhzProtocolDecoderMegaCode));
  193. instance->base.protocol = &subghz_protocol_megacode;
  194. instance->generic.protocol_name = instance->base.protocol->name;
  195. return instance;
  196. }
  197. void subghz_protocol_decoder_megacode_free(void* context) {
  198. furi_assert(context);
  199. SubGhzProtocolDecoderMegaCode* instance = context;
  200. free(instance);
  201. }
  202. void subghz_protocol_decoder_megacode_reset(void* context) {
  203. furi_assert(context);
  204. SubGhzProtocolDecoderMegaCode* instance = context;
  205. instance->decoder.parser_step = MegaCodeDecoderStepReset;
  206. }
  207. void subghz_protocol_decoder_megacode_feed(void* context, bool level, uint32_t duration) {
  208. furi_assert(context);
  209. SubGhzProtocolDecoderMegaCode* instance = context;
  210. switch(instance->decoder.parser_step) {
  211. case MegaCodeDecoderStepReset:
  212. if((!level) && (DURATION_DIFF(duration, subghz_protocol_megacode_const.te_short * 13) <
  213. subghz_protocol_megacode_const.te_delta * 17)) { //10..16ms
  214. //Found header MegaCode
  215. instance->decoder.parser_step = MegaCodeDecoderStepFoundStartBit;
  216. }
  217. break;
  218. case MegaCodeDecoderStepFoundStartBit:
  219. if(level && (DURATION_DIFF(duration, subghz_protocol_megacode_const.te_short) <
  220. subghz_protocol_megacode_const.te_delta)) {
  221. //Found start bit MegaCode
  222. instance->decoder.parser_step = MegaCodeDecoderStepSaveDuration;
  223. instance->decoder.decode_data = 0;
  224. instance->decoder.decode_count_bit = 0;
  225. subghz_protocol_blocks_add_bit(&instance->decoder, 1);
  226. instance->last_bit = 1;
  227. } else {
  228. instance->decoder.parser_step = MegaCodeDecoderStepReset;
  229. }
  230. break;
  231. case MegaCodeDecoderStepSaveDuration:
  232. if(!level) { //save interval
  233. if(duration >= (subghz_protocol_megacode_const.te_short * 10)) {
  234. instance->decoder.parser_step = MegaCodeDecoderStepReset;
  235. if(instance->decoder.decode_count_bit ==
  236. subghz_protocol_megacode_const.min_count_bit_for_found) {
  237. instance->generic.data = instance->decoder.decode_data;
  238. instance->generic.data_count_bit = instance->decoder.decode_count_bit;
  239. if(instance->base.callback)
  240. instance->base.callback(&instance->base, instance->base.context);
  241. }
  242. break;
  243. }
  244. if(!instance->last_bit) {
  245. instance->decoder.te_last = duration - subghz_protocol_megacode_const.te_short * 3;
  246. } else {
  247. instance->decoder.te_last = duration;
  248. }
  249. instance->decoder.parser_step = MegaCodeDecoderStepCheckDuration;
  250. } else {
  251. instance->decoder.parser_step = MegaCodeDecoderStepReset;
  252. }
  253. break;
  254. case MegaCodeDecoderStepCheckDuration:
  255. if(level) {
  256. if((DURATION_DIFF(
  257. instance->decoder.te_last, subghz_protocol_megacode_const.te_short * 5) <
  258. subghz_protocol_megacode_const.te_delta * 5) &&
  259. (DURATION_DIFF(duration, subghz_protocol_megacode_const.te_short) <
  260. subghz_protocol_megacode_const.te_delta)) {
  261. subghz_protocol_blocks_add_bit(&instance->decoder, 1);
  262. instance->last_bit = 1;
  263. instance->decoder.parser_step = MegaCodeDecoderStepSaveDuration;
  264. } else if(
  265. (DURATION_DIFF(
  266. instance->decoder.te_last, subghz_protocol_megacode_const.te_short * 2) <
  267. subghz_protocol_megacode_const.te_delta * 2) &&
  268. (DURATION_DIFF(duration, subghz_protocol_megacode_const.te_short) <
  269. subghz_protocol_megacode_const.te_delta)) {
  270. subghz_protocol_blocks_add_bit(&instance->decoder, 0);
  271. instance->last_bit = 0;
  272. instance->decoder.parser_step = MegaCodeDecoderStepSaveDuration;
  273. } else
  274. instance->decoder.parser_step = MegaCodeDecoderStepReset;
  275. } else {
  276. instance->decoder.parser_step = MegaCodeDecoderStepReset;
  277. }
  278. break;
  279. }
  280. }
  281. /**
  282. * Analysis of received data
  283. * @param instance Pointer to a SubGhzBlockGeneric* instance
  284. */
  285. static void subghz_protocol_megacode_check_remote_controller(SubGhzBlockGeneric* instance) {
  286. /*
  287. * Short: 1000 µs
  288. * Long: 1000 µs
  289. * Gap: 11000 .. 14000 µs
  290. * A Linear Megacode transmission consists of 24 bit frames starting with
  291. * the most significant bit and ending with the least. Each of the 24 bit
  292. * frames is 6 milliseconds wide and always contains a single 1 millisecond
  293. * pulse. A frame with more than 1 pulse or a frame with no pulse is invalid
  294. * and a receiver should reset and begin watching for another start bit.
  295. * Start bit is always 1.
  296. *
  297. *
  298. * Example (I created with my own remote):
  299. * Remote “A” has the code “17316”, a Facility Code of “3”, and a single button.
  300. * Start bit (S) = 1
  301. * Facility Code 3 (F) = 0011
  302. * Remote Code (Key) 17316 = 43A4 = 0100001110100100
  303. * Button (Btn) 1 = 001
  304. * S F Key Btn
  305. * Result = 1|0011|0100001110100100|001
  306. *
  307. * 00000 1
  308. * _____|-| = 1 becomes
  309. *
  310. * 00 1 000
  311. * __|-|___ = 0 becomes
  312. *
  313. * The device needs to transmit with a 9000 µs gap between retransmissions:
  314. * 000001 001000 001000 000001 000001 001000 000001 001000 001000 001000 001000 000001
  315. * 000001 000001 001000 000001 001000 001000 000001 001000 001000 001000 001000 000001
  316. * wait 9000 µs
  317. * 000001 001000 001000 000001 000001 001000 000001 001000 001000 001000 001000 000001
  318. * 000001 000001 001000 000001 001000 001000 000001 001000 001000 001000 001000 000001
  319. *
  320. */
  321. if((instance->data >> 23) == 1) {
  322. instance->serial = (instance->data >> 3) & 0xFFFF;
  323. instance->btn = instance->data & 0b111;
  324. instance->cnt = (instance->data >> 19) & 0b1111;
  325. } else {
  326. instance->serial = 0;
  327. instance->btn = 0;
  328. instance->cnt = 0;
  329. }
  330. }
  331. uint8_t subghz_protocol_decoder_megacode_get_hash_data(void* context) {
  332. furi_assert(context);
  333. SubGhzProtocolDecoderMegaCode* instance = context;
  334. return subghz_protocol_blocks_get_hash_data(
  335. &instance->decoder, (instance->decoder.decode_count_bit / 8) + 1);
  336. }
  337. bool subghz_protocol_decoder_megacode_serialize(
  338. void* context,
  339. FlipperFormat* flipper_format,
  340. SubGhzPresetDefinition* preset) {
  341. furi_assert(context);
  342. SubGhzProtocolDecoderMegaCode* instance = context;
  343. return subghz_block_generic_serialize(&instance->generic, flipper_format, preset);
  344. }
  345. bool subghz_protocol_decoder_megacode_deserialize(void* context, FlipperFormat* flipper_format) {
  346. furi_assert(context);
  347. SubGhzProtocolDecoderMegaCode* instance = context;
  348. bool ret = false;
  349. do {
  350. if(!subghz_block_generic_deserialize(&instance->generic, flipper_format)) {
  351. break;
  352. }
  353. if(instance->generic.data_count_bit !=
  354. subghz_protocol_megacode_const.min_count_bit_for_found) {
  355. FURI_LOG_E(TAG, "Wrong number of bits in key");
  356. break;
  357. }
  358. ret = true;
  359. } while(false);
  360. return ret;
  361. }
  362. void subghz_protocol_decoder_megacode_get_string(void* context, string_t output) {
  363. furi_assert(context);
  364. SubGhzProtocolDecoderMegaCode* instance = context;
  365. subghz_protocol_megacode_check_remote_controller(&instance->generic);
  366. string_cat_printf(
  367. output,
  368. "%s %dbit\r\n"
  369. "Key:0x%06lX\r\n"
  370. "Sn:0x%04lX - %d\r\n"
  371. "Facility:%X Btn:%X\r\n",
  372. instance->generic.protocol_name,
  373. instance->generic.data_count_bit,
  374. (uint32_t)instance->generic.data,
  375. instance->generic.serial,
  376. instance->generic.serial,
  377. instance->generic.cnt,
  378. instance->generic.btn);
  379. }