megacode.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414
  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_runing = 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. //optional parameter parameter
  160. flipper_format_read_uint32(
  161. flipper_format, "Repeat", (uint32_t*)&instance->encoder.repeat, 1);
  162. subghz_protocol_encoder_megacode_get_upload(instance);
  163. instance->encoder.is_runing = true;
  164. res = true;
  165. } while(false);
  166. return res;
  167. }
  168. void subghz_protocol_encoder_megacode_stop(void* context) {
  169. SubGhzProtocolEncoderMegaCode* instance = context;
  170. instance->encoder.is_runing = false;
  171. }
  172. LevelDuration subghz_protocol_encoder_megacode_yield(void* context) {
  173. SubGhzProtocolEncoderMegaCode* instance = context;
  174. if(instance->encoder.repeat == 0 || !instance->encoder.is_runing) {
  175. instance->encoder.is_runing = false;
  176. return level_duration_reset();
  177. }
  178. LevelDuration ret = instance->encoder.upload[instance->encoder.front];
  179. if(++instance->encoder.front == instance->encoder.size_upload) {
  180. instance->encoder.repeat--;
  181. instance->encoder.front = 0;
  182. }
  183. return ret;
  184. }
  185. void* subghz_protocol_decoder_megacode_alloc(SubGhzEnvironment* environment) {
  186. UNUSED(environment);
  187. SubGhzProtocolDecoderMegaCode* instance = malloc(sizeof(SubGhzProtocolDecoderMegaCode));
  188. instance->base.protocol = &subghz_protocol_megacode;
  189. instance->generic.protocol_name = instance->base.protocol->name;
  190. return instance;
  191. }
  192. void subghz_protocol_decoder_megacode_free(void* context) {
  193. furi_assert(context);
  194. SubGhzProtocolDecoderMegaCode* instance = context;
  195. free(instance);
  196. }
  197. void subghz_protocol_decoder_megacode_reset(void* context) {
  198. furi_assert(context);
  199. SubGhzProtocolDecoderMegaCode* instance = context;
  200. instance->decoder.parser_step = MegaCodeDecoderStepReset;
  201. }
  202. void subghz_protocol_decoder_megacode_feed(void* context, bool level, uint32_t duration) {
  203. furi_assert(context);
  204. SubGhzProtocolDecoderMegaCode* instance = context;
  205. switch(instance->decoder.parser_step) {
  206. case MegaCodeDecoderStepReset:
  207. if((!level) && (DURATION_DIFF(duration, subghz_protocol_megacode_const.te_short * 13) <
  208. subghz_protocol_megacode_const.te_delta * 17)) { //10..16ms
  209. //Found header MegaCode
  210. instance->decoder.parser_step = MegaCodeDecoderStepFoundStartBit;
  211. }
  212. break;
  213. case MegaCodeDecoderStepFoundStartBit:
  214. if(level && (DURATION_DIFF(duration, subghz_protocol_megacode_const.te_short) <
  215. subghz_protocol_megacode_const.te_delta)) {
  216. //Found start bit MegaCode
  217. instance->decoder.parser_step = MegaCodeDecoderStepSaveDuration;
  218. instance->decoder.decode_data = 0;
  219. instance->decoder.decode_count_bit = 0;
  220. subghz_protocol_blocks_add_bit(&instance->decoder, 1);
  221. instance->last_bit = 1;
  222. } else {
  223. instance->decoder.parser_step = MegaCodeDecoderStepReset;
  224. }
  225. break;
  226. case MegaCodeDecoderStepSaveDuration:
  227. if(!level) { //save interval
  228. if(duration >= (subghz_protocol_megacode_const.te_short * 10)) {
  229. instance->decoder.parser_step = MegaCodeDecoderStepReset;
  230. if(instance->decoder.decode_count_bit >=
  231. subghz_protocol_megacode_const.min_count_bit_for_found) {
  232. instance->generic.data = instance->decoder.decode_data;
  233. instance->generic.data_count_bit = instance->decoder.decode_count_bit;
  234. if(instance->base.callback)
  235. instance->base.callback(&instance->base, instance->base.context);
  236. }
  237. break;
  238. }
  239. if(!instance->last_bit) {
  240. instance->decoder.te_last = duration - subghz_protocol_megacode_const.te_short * 3;
  241. } else {
  242. instance->decoder.te_last = duration;
  243. }
  244. instance->decoder.parser_step = MegaCodeDecoderStepCheckDuration;
  245. } else {
  246. instance->decoder.parser_step = MegaCodeDecoderStepReset;
  247. }
  248. break;
  249. case MegaCodeDecoderStepCheckDuration:
  250. if(level) {
  251. if((DURATION_DIFF(
  252. instance->decoder.te_last, subghz_protocol_megacode_const.te_short * 5) <
  253. subghz_protocol_megacode_const.te_delta * 5) &&
  254. (DURATION_DIFF(duration, subghz_protocol_megacode_const.te_short) <
  255. subghz_protocol_megacode_const.te_delta)) {
  256. subghz_protocol_blocks_add_bit(&instance->decoder, 1);
  257. instance->last_bit = 1;
  258. instance->decoder.parser_step = MegaCodeDecoderStepSaveDuration;
  259. } else if(
  260. (DURATION_DIFF(
  261. instance->decoder.te_last, subghz_protocol_megacode_const.te_short * 2) <
  262. subghz_protocol_megacode_const.te_delta * 2) &&
  263. (DURATION_DIFF(duration, subghz_protocol_megacode_const.te_short) <
  264. subghz_protocol_megacode_const.te_delta)) {
  265. subghz_protocol_blocks_add_bit(&instance->decoder, 0);
  266. instance->last_bit = 0;
  267. instance->decoder.parser_step = MegaCodeDecoderStepSaveDuration;
  268. } else
  269. instance->decoder.parser_step = MegaCodeDecoderStepReset;
  270. } else {
  271. instance->decoder.parser_step = MegaCodeDecoderStepReset;
  272. }
  273. break;
  274. }
  275. }
  276. /**
  277. * Analysis of received data
  278. * @param instance Pointer to a SubGhzBlockGeneric* instance
  279. */
  280. static void subghz_protocol_megacode_check_remote_controller(SubGhzBlockGeneric* instance) {
  281. /*
  282. * Short: 1000 µs
  283. * Long: 1000 µs
  284. * Gap: 11000 .. 14000 µs
  285. * A Linear Megacode transmission consists of 24 bit frames starting with
  286. * the most significant bit and ending with the least. Each of the 24 bit
  287. * frames is 6 milliseconds wide and always contains a single 1 millisecond
  288. * pulse. A frame with more than 1 pulse or a frame with no pulse is invalid
  289. * and a receiver should reset and begin watching for another start bit.
  290. * Start bit is always 1.
  291. *
  292. *
  293. * Example (I created with my own remote):
  294. * Remote “A” has the code “17316”, a Facility Code of “3”, and a single button.
  295. * Start bit (S) = 1
  296. * Facility Code 3 (F) = 0011
  297. * Remote Code (Key) 17316 = 43A4 = 0100001110100100
  298. * Button (Btn) 1 = 001
  299. * S F Key Btn
  300. * Result = 1|0011|0100001110100100|001
  301. *
  302. * 00000 1
  303. * _____|-| = 1 becomes
  304. *
  305. * 00 1 000
  306. * __|-|___ = 0 becomes
  307. *
  308. * The device needs to transmit with a 9000 µs gap between retransmissions:
  309. * 000001 001000 001000 000001 000001 001000 000001 001000 001000 001000 001000 000001
  310. * 000001 000001 001000 000001 001000 001000 000001 001000 001000 001000 001000 000001
  311. * wait 9000 µs
  312. * 000001 001000 001000 000001 000001 001000 000001 001000 001000 001000 001000 000001
  313. * 000001 000001 001000 000001 001000 001000 000001 001000 001000 001000 001000 000001
  314. *
  315. */
  316. if((instance->data >> 23) == 1) {
  317. instance->serial = (instance->data >> 3) & 0xFFFF;
  318. instance->btn = instance->data & 0b111;
  319. instance->cnt = (instance->data >> 19) & 0b1111;
  320. } else {
  321. instance->serial = 0;
  322. instance->btn = 0;
  323. instance->cnt = 0;
  324. }
  325. }
  326. uint8_t subghz_protocol_decoder_megacode_get_hash_data(void* context) {
  327. furi_assert(context);
  328. SubGhzProtocolDecoderMegaCode* instance = context;
  329. return subghz_protocol_blocks_get_hash_data(
  330. &instance->decoder, (instance->decoder.decode_count_bit / 8) + 1);
  331. }
  332. bool subghz_protocol_decoder_megacode_serialize(
  333. void* context,
  334. FlipperFormat* flipper_format,
  335. uint32_t frequency,
  336. FuriHalSubGhzPreset preset) {
  337. furi_assert(context);
  338. SubGhzProtocolDecoderMegaCode* instance = context;
  339. return subghz_block_generic_serialize(&instance->generic, flipper_format, frequency, preset);
  340. }
  341. bool subghz_protocol_decoder_megacode_deserialize(void* context, FlipperFormat* flipper_format) {
  342. furi_assert(context);
  343. SubGhzProtocolDecoderMegaCode* instance = context;
  344. return subghz_block_generic_deserialize(&instance->generic, flipper_format);
  345. }
  346. void subghz_protocol_decoder_megacode_get_string(void* context, string_t output) {
  347. furi_assert(context);
  348. SubGhzProtocolDecoderMegaCode* instance = context;
  349. subghz_protocol_megacode_check_remote_controller(&instance->generic);
  350. string_cat_printf(
  351. output,
  352. "%s %dbit\r\n"
  353. "Key:0x%06lX\r\n"
  354. "Sn:0x%04lX - %d\r\n"
  355. "Facility:%X Btn:%X\r\n",
  356. instance->generic.protocol_name,
  357. instance->generic.data_count_bit,
  358. (uint32_t)instance->generic.data,
  359. instance->generic.serial,
  360. instance->generic.serial,
  361. instance->generic.cnt,
  362. instance->generic.btn);
  363. }