magellan.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437
  1. #include "magellan.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 "SubGhzProtocolMagellan"
  8. static const SubGhzBlockConst subghz_protocol_magellan_const = {
  9. .te_short = 200,
  10. .te_long = 400,
  11. .te_delta = 100,
  12. .min_count_bit_for_found = 32,
  13. };
  14. struct SubGhzProtocolDecoderMagellan {
  15. SubGhzProtocolDecoderBase base;
  16. SubGhzBlockDecoder decoder;
  17. SubGhzBlockGeneric generic;
  18. uint16_t header_count;
  19. };
  20. struct SubGhzProtocolEncoderMagellan {
  21. SubGhzProtocolEncoderBase base;
  22. SubGhzProtocolBlockEncoder encoder;
  23. SubGhzBlockGeneric generic;
  24. };
  25. typedef enum {
  26. MagellanDecoderStepReset = 0,
  27. MagellanDecoderStepCheckPreambula,
  28. MagellanDecoderStepFoundPreambula,
  29. MagellanDecoderStepSaveDuration,
  30. MagellanDecoderStepCheckDuration,
  31. } MagellanDecoderStep;
  32. const SubGhzProtocolDecoder subghz_protocol_magellan_decoder = {
  33. .alloc = subghz_protocol_decoder_magellan_alloc,
  34. .free = subghz_protocol_decoder_magellan_free,
  35. .feed = subghz_protocol_decoder_magellan_feed,
  36. .reset = subghz_protocol_decoder_magellan_reset,
  37. .get_hash_data = subghz_protocol_decoder_magellan_get_hash_data,
  38. .serialize = subghz_protocol_decoder_magellan_serialize,
  39. .deserialize = subghz_protocol_decoder_magellan_deserialize,
  40. .get_string = subghz_protocol_decoder_magellan_get_string,
  41. };
  42. const SubGhzProtocolEncoder subghz_protocol_magellan_encoder = {
  43. .alloc = subghz_protocol_encoder_magellan_alloc,
  44. .free = subghz_protocol_encoder_magellan_free,
  45. .deserialize = subghz_protocol_encoder_magellan_deserialize,
  46. .stop = subghz_protocol_encoder_magellan_stop,
  47. .yield = subghz_protocol_encoder_magellan_yield,
  48. };
  49. const SubGhzProtocol subghz_protocol_magellan = {
  50. .name = SUBGHZ_PROTOCOL_MAGELLAN_NAME,
  51. .type = SubGhzProtocolTypeStatic,
  52. .flag = SubGhzProtocolFlag_433 | SubGhzProtocolFlag_AM | SubGhzProtocolFlag_Decodable |
  53. SubGhzProtocolFlag_Load | SubGhzProtocolFlag_Save | SubGhzProtocolFlag_Send,
  54. .decoder = &subghz_protocol_magellan_decoder,
  55. .encoder = &subghz_protocol_magellan_encoder,
  56. };
  57. void* subghz_protocol_encoder_magellan_alloc(SubGhzEnvironment* environment) {
  58. UNUSED(environment);
  59. SubGhzProtocolEncoderMagellan* instance = malloc(sizeof(SubGhzProtocolEncoderMagellan));
  60. instance->base.protocol = &subghz_protocol_magellan;
  61. instance->generic.protocol_name = instance->base.protocol->name;
  62. instance->encoder.repeat = 10;
  63. instance->encoder.size_upload = 256;
  64. instance->encoder.upload = malloc(instance->encoder.size_upload * sizeof(LevelDuration));
  65. instance->encoder.is_running = false;
  66. return instance;
  67. }
  68. void subghz_protocol_encoder_magellan_free(void* context) {
  69. furi_assert(context);
  70. SubGhzProtocolEncoderMagellan* instance = context;
  71. free(instance->encoder.upload);
  72. free(instance);
  73. }
  74. /**
  75. * Generating an upload from data.
  76. * @param instance Pointer to a SubGhzProtocolEncoderMagellan instance
  77. * @return true On success
  78. */
  79. static bool subghz_protocol_encoder_magellan_get_upload(SubGhzProtocolEncoderMagellan* instance) {
  80. furi_assert(instance);
  81. size_t index = 0;
  82. //Send header
  83. instance->encoder.upload[index++] =
  84. level_duration_make(true, (uint32_t)subghz_protocol_magellan_const.te_short * 4);
  85. instance->encoder.upload[index++] =
  86. level_duration_make(false, (uint32_t)subghz_protocol_magellan_const.te_short);
  87. for(uint8_t i = 0; i < 12; i++) {
  88. instance->encoder.upload[index++] =
  89. level_duration_make(true, (uint32_t)subghz_protocol_magellan_const.te_short);
  90. instance->encoder.upload[index++] =
  91. level_duration_make(false, (uint32_t)subghz_protocol_magellan_const.te_short);
  92. }
  93. instance->encoder.upload[index++] =
  94. level_duration_make(true, (uint32_t)subghz_protocol_magellan_const.te_short);
  95. instance->encoder.upload[index++] =
  96. level_duration_make(false, (uint32_t)subghz_protocol_magellan_const.te_long);
  97. //Send start bit
  98. instance->encoder.upload[index++] =
  99. level_duration_make(true, (uint32_t)subghz_protocol_magellan_const.te_long * 3);
  100. instance->encoder.upload[index++] =
  101. level_duration_make(false, (uint32_t)subghz_protocol_magellan_const.te_long);
  102. //Send key data
  103. for(uint8_t i = instance->generic.data_count_bit; i > 0; i--) {
  104. if(bit_read(instance->generic.data, i - 1)) {
  105. //send bit 1
  106. instance->encoder.upload[index++] =
  107. level_duration_make(true, (uint32_t)subghz_protocol_magellan_const.te_short);
  108. instance->encoder.upload[index++] =
  109. level_duration_make(false, (uint32_t)subghz_protocol_magellan_const.te_long);
  110. } else {
  111. //send bit 0
  112. instance->encoder.upload[index++] =
  113. level_duration_make(true, (uint32_t)subghz_protocol_magellan_const.te_long);
  114. instance->encoder.upload[index++] =
  115. level_duration_make(false, (uint32_t)subghz_protocol_magellan_const.te_short);
  116. }
  117. }
  118. //Send stop bit
  119. instance->encoder.upload[index++] =
  120. level_duration_make(true, (uint32_t)subghz_protocol_magellan_const.te_short);
  121. instance->encoder.upload[index++] =
  122. level_duration_make(false, (uint32_t)subghz_protocol_magellan_const.te_long * 100);
  123. instance->encoder.size_upload = index;
  124. return true;
  125. }
  126. SubGhzProtocolStatus
  127. subghz_protocol_encoder_magellan_deserialize(void* context, FlipperFormat* flipper_format) {
  128. furi_assert(context);
  129. SubGhzProtocolEncoderMagellan* instance = context;
  130. SubGhzProtocolStatus ret = SubGhzProtocolStatusError;
  131. do {
  132. ret = subghz_block_generic_deserialize_check_count_bit(
  133. &instance->generic,
  134. flipper_format,
  135. subghz_protocol_magellan_const.min_count_bit_for_found);
  136. if(ret != SubGhzProtocolStatusOk) {
  137. break;
  138. }
  139. //optional parameter parameter
  140. flipper_format_read_uint32(
  141. flipper_format, "Repeat", (uint32_t*)&instance->encoder.repeat, 1);
  142. if(!subghz_protocol_encoder_magellan_get_upload(instance)) {
  143. ret = SubGhzProtocolStatusErrorEncoderGetUpload;
  144. break;
  145. }
  146. instance->encoder.is_running = true;
  147. } while(false);
  148. return ret;
  149. }
  150. void subghz_protocol_encoder_magellan_stop(void* context) {
  151. SubGhzProtocolEncoderMagellan* instance = context;
  152. instance->encoder.is_running = false;
  153. }
  154. LevelDuration subghz_protocol_encoder_magellan_yield(void* context) {
  155. SubGhzProtocolEncoderMagellan* instance = context;
  156. if(instance->encoder.repeat == 0 || !instance->encoder.is_running) {
  157. instance->encoder.is_running = false;
  158. return level_duration_reset();
  159. }
  160. LevelDuration ret = instance->encoder.upload[instance->encoder.front];
  161. if(++instance->encoder.front == instance->encoder.size_upload) {
  162. instance->encoder.repeat--;
  163. instance->encoder.front = 0;
  164. }
  165. return ret;
  166. }
  167. void* subghz_protocol_decoder_magellan_alloc(SubGhzEnvironment* environment) {
  168. UNUSED(environment);
  169. SubGhzProtocolDecoderMagellan* instance = malloc(sizeof(SubGhzProtocolDecoderMagellan));
  170. instance->base.protocol = &subghz_protocol_magellan;
  171. instance->generic.protocol_name = instance->base.protocol->name;
  172. return instance;
  173. }
  174. void subghz_protocol_decoder_magellan_free(void* context) {
  175. furi_assert(context);
  176. SubGhzProtocolDecoderMagellan* instance = context;
  177. free(instance);
  178. }
  179. void subghz_protocol_decoder_magellan_reset(void* context) {
  180. furi_assert(context);
  181. SubGhzProtocolDecoderMagellan* instance = context;
  182. instance->decoder.parser_step = MagellanDecoderStepReset;
  183. }
  184. uint8_t subghz_protocol_magellan_crc8(uint8_t* data, size_t len) {
  185. uint8_t crc = 0x00;
  186. size_t i, j;
  187. for(i = 0; i < len; i++) {
  188. crc ^= data[i];
  189. for(j = 0; j < 8; j++) {
  190. if((crc & 0x80) != 0)
  191. crc = (uint8_t)((crc << 1) ^ 0x31);
  192. else
  193. crc <<= 1;
  194. }
  195. }
  196. return crc;
  197. }
  198. static bool subghz_protocol_magellan_check_crc(SubGhzProtocolDecoderMagellan* instance) {
  199. uint8_t data[3] = {
  200. instance->decoder.decode_data >> 24,
  201. instance->decoder.decode_data >> 16,
  202. instance->decoder.decode_data >> 8};
  203. return (instance->decoder.decode_data & 0xFF) ==
  204. subghz_protocol_magellan_crc8(data, sizeof(data));
  205. }
  206. void subghz_protocol_decoder_magellan_feed(void* context, bool level, uint32_t duration) {
  207. furi_assert(context);
  208. SubGhzProtocolDecoderMagellan* instance = context;
  209. switch(instance->decoder.parser_step) {
  210. case MagellanDecoderStepReset:
  211. if((level) && (DURATION_DIFF(duration, subghz_protocol_magellan_const.te_short) <
  212. subghz_protocol_magellan_const.te_delta)) {
  213. instance->decoder.parser_step = MagellanDecoderStepCheckPreambula;
  214. instance->decoder.te_last = duration;
  215. instance->header_count = 0;
  216. }
  217. break;
  218. case MagellanDecoderStepCheckPreambula:
  219. if(level) {
  220. instance->decoder.te_last = duration;
  221. } else {
  222. if((DURATION_DIFF(instance->decoder.te_last, subghz_protocol_magellan_const.te_short) <
  223. subghz_protocol_magellan_const.te_delta) &&
  224. (DURATION_DIFF(duration, subghz_protocol_magellan_const.te_short) <
  225. subghz_protocol_magellan_const.te_delta)) {
  226. // Found header
  227. instance->header_count++;
  228. } else if(
  229. (DURATION_DIFF(instance->decoder.te_last, subghz_protocol_magellan_const.te_short) <
  230. subghz_protocol_magellan_const.te_delta) &&
  231. (DURATION_DIFF(duration, subghz_protocol_magellan_const.te_long) <
  232. subghz_protocol_magellan_const.te_delta * 2) &&
  233. (instance->header_count > 10)) {
  234. instance->decoder.parser_step = MagellanDecoderStepFoundPreambula;
  235. } else {
  236. instance->decoder.parser_step = MagellanDecoderStepReset;
  237. }
  238. }
  239. break;
  240. case MagellanDecoderStepFoundPreambula:
  241. if(level) {
  242. instance->decoder.te_last = duration;
  243. } else {
  244. if((DURATION_DIFF(
  245. instance->decoder.te_last, subghz_protocol_magellan_const.te_short * 6) <
  246. subghz_protocol_magellan_const.te_delta * 3) &&
  247. (DURATION_DIFF(duration, subghz_protocol_magellan_const.te_long) <
  248. subghz_protocol_magellan_const.te_delta * 2)) {
  249. instance->decoder.parser_step = MagellanDecoderStepSaveDuration;
  250. instance->decoder.decode_data = 0;
  251. instance->decoder.decode_count_bit = 0;
  252. } else {
  253. instance->decoder.parser_step = MagellanDecoderStepReset;
  254. }
  255. }
  256. break;
  257. case MagellanDecoderStepSaveDuration:
  258. if(level) {
  259. instance->decoder.te_last = duration;
  260. instance->decoder.parser_step = MagellanDecoderStepCheckDuration;
  261. } else {
  262. instance->decoder.parser_step = MagellanDecoderStepReset;
  263. }
  264. break;
  265. case MagellanDecoderStepCheckDuration:
  266. if(!level) {
  267. if((DURATION_DIFF(instance->decoder.te_last, subghz_protocol_magellan_const.te_short) <
  268. subghz_protocol_magellan_const.te_delta) &&
  269. (DURATION_DIFF(duration, subghz_protocol_magellan_const.te_long) <
  270. subghz_protocol_magellan_const.te_delta)) {
  271. subghz_protocol_blocks_add_bit(&instance->decoder, 1);
  272. instance->decoder.parser_step = MagellanDecoderStepSaveDuration;
  273. } else if(
  274. (DURATION_DIFF(instance->decoder.te_last, subghz_protocol_magellan_const.te_long) <
  275. subghz_protocol_magellan_const.te_delta) &&
  276. (DURATION_DIFF(duration, subghz_protocol_magellan_const.te_short) <
  277. subghz_protocol_magellan_const.te_delta)) {
  278. subghz_protocol_blocks_add_bit(&instance->decoder, 0);
  279. instance->decoder.parser_step = MagellanDecoderStepSaveDuration;
  280. } else if(duration >= (subghz_protocol_magellan_const.te_long * 3)) {
  281. //Found stop bit
  282. if((instance->decoder.decode_count_bit ==
  283. subghz_protocol_magellan_const.min_count_bit_for_found) &&
  284. subghz_protocol_magellan_check_crc(instance)) {
  285. instance->generic.data = instance->decoder.decode_data;
  286. instance->generic.data_count_bit = instance->decoder.decode_count_bit;
  287. if(instance->base.callback)
  288. instance->base.callback(&instance->base, instance->base.context);
  289. }
  290. instance->decoder.decode_data = 0;
  291. instance->decoder.decode_count_bit = 0;
  292. instance->decoder.parser_step = MagellanDecoderStepReset;
  293. } else {
  294. instance->decoder.parser_step = MagellanDecoderStepReset;
  295. }
  296. } else {
  297. instance->decoder.parser_step = MagellanDecoderStepReset;
  298. }
  299. break;
  300. }
  301. }
  302. /**
  303. * Analysis of received data
  304. * @param instance Pointer to a SubGhzBlockGeneric* instance
  305. */
  306. static void subghz_protocol_magellan_check_remote_controller(SubGhzBlockGeneric* instance) {
  307. /*
  308. * package 32b data 24b CRC8
  309. * 0x037AE4828 => 001101111010111001001000 00101000
  310. *
  311. * 0x037AE48 (flipped in reverse bit sequence) => 0x1275EC
  312. *
  313. * 0x1275EC => 0x12-event codes, 0x75EC-serial (dec 117236)
  314. *
  315. * event codes
  316. * bit_0: 1-Open/Motion, 0-close/ok
  317. * bit_1: 1-Tamper On (alarm), 0-Tamper Off (ok)
  318. * bit_2: ?
  319. * bit_3: 1-power on
  320. * bit_4: model type - wireless reed
  321. * bit_5: model type - motion sensor
  322. * bit_6: ?
  323. * bit_7: ?
  324. *
  325. */
  326. uint64_t data_rev = subghz_protocol_blocks_reverse_key(instance->data >> 8, 24);
  327. instance->serial = data_rev & 0xFFFF;
  328. instance->btn = (data_rev >> 16) & 0xFF;
  329. }
  330. static void subghz_protocol_magellan_get_event_serialize(uint8_t event, FuriString* output) {
  331. furi_string_cat_printf(
  332. output,
  333. "%s%s%s%s%s%s%s%s",
  334. ((event >> 4) & 0x1 ? (event & 0x1 ? " Open" : " Close") :
  335. (event & 0x1 ? " Motion" : " Ok")),
  336. ((event >> 1) & 0x1 ? ", Tamper On\n(Alarm)" : ""),
  337. ((event >> 2) & 0x1 ? ", ?" : ""),
  338. ((event >> 3) & 0x1 ? ", Power On" : ""),
  339. ((event >> 4) & 0x1 ? ", MT:Wireless_Reed" : ""),
  340. ((event >> 5) & 0x1 ? ", MT:Motion_Sensor" : ""),
  341. ((event >> 6) & 0x1 ? ", ?" : ""),
  342. ((event >> 7) & 0x1 ? ", ?" : ""));
  343. }
  344. uint8_t subghz_protocol_decoder_magellan_get_hash_data(void* context) {
  345. furi_assert(context);
  346. SubGhzProtocolDecoderMagellan* instance = context;
  347. return subghz_protocol_blocks_get_hash_data(
  348. &instance->decoder, (instance->decoder.decode_count_bit / 8) + 1);
  349. }
  350. SubGhzProtocolStatus subghz_protocol_decoder_magellan_serialize(
  351. void* context,
  352. FlipperFormat* flipper_format,
  353. SubGhzRadioPreset* preset) {
  354. furi_assert(context);
  355. SubGhzProtocolDecoderMagellan* instance = context;
  356. return subghz_block_generic_serialize(&instance->generic, flipper_format, preset);
  357. }
  358. SubGhzProtocolStatus
  359. subghz_protocol_decoder_magellan_deserialize(void* context, FlipperFormat* flipper_format) {
  360. furi_assert(context);
  361. SubGhzProtocolDecoderMagellan* instance = context;
  362. return subghz_block_generic_deserialize_check_count_bit(
  363. &instance->generic,
  364. flipper_format,
  365. subghz_protocol_magellan_const.min_count_bit_for_found);
  366. }
  367. void subghz_protocol_decoder_magellan_get_string(void* context, FuriString* output) {
  368. furi_assert(context);
  369. SubGhzProtocolDecoderMagellan* instance = context;
  370. subghz_protocol_magellan_check_remote_controller(&instance->generic);
  371. furi_string_cat_printf(
  372. output,
  373. "%s %dbit\r\n"
  374. "Key:0x%08lX\r\n"
  375. "Sn:%03ld%03ld, Event:0x%02X\r\n"
  376. "Stat:",
  377. instance->generic.protocol_name,
  378. instance->generic.data_count_bit,
  379. (uint32_t)(instance->generic.data & 0xFFFFFFFF),
  380. (instance->generic.serial >> 8) & 0xFF,
  381. instance->generic.serial & 0xFF,
  382. instance->generic.btn);
  383. subghz_protocol_magellan_get_event_serialize(instance->generic.btn, output);
  384. }