magellan.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445
  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. bool subghz_protocol_encoder_magellan_deserialize(void* context, FlipperFormat* flipper_format) {
  127. furi_assert(context);
  128. SubGhzProtocolEncoderMagellan* instance = context;
  129. bool res = false;
  130. do {
  131. if(!subghz_block_generic_deserialize(&instance->generic, flipper_format)) {
  132. FURI_LOG_E(TAG, "Deserialize error");
  133. break;
  134. }
  135. if(instance->generic.data_count_bit !=
  136. subghz_protocol_magellan_const.min_count_bit_for_found) {
  137. FURI_LOG_E(TAG, "Wrong number of bits in key");
  138. break;
  139. }
  140. //optional parameter parameter
  141. flipper_format_read_uint32(
  142. flipper_format, "Repeat", (uint32_t*)&instance->encoder.repeat, 1);
  143. if(!subghz_protocol_encoder_magellan_get_upload(instance)) break;
  144. instance->encoder.is_running = true;
  145. res = true;
  146. } while(false);
  147. return res;
  148. }
  149. void subghz_protocol_encoder_magellan_stop(void* context) {
  150. SubGhzProtocolEncoderMagellan* instance = context;
  151. instance->encoder.is_running = false;
  152. }
  153. LevelDuration subghz_protocol_encoder_magellan_yield(void* context) {
  154. SubGhzProtocolEncoderMagellan* instance = context;
  155. if(instance->encoder.repeat == 0 || !instance->encoder.is_running) {
  156. instance->encoder.is_running = false;
  157. return level_duration_reset();
  158. }
  159. LevelDuration ret = instance->encoder.upload[instance->encoder.front];
  160. if(++instance->encoder.front == instance->encoder.size_upload) {
  161. instance->encoder.repeat--;
  162. instance->encoder.front = 0;
  163. }
  164. return ret;
  165. }
  166. void* subghz_protocol_decoder_magellan_alloc(SubGhzEnvironment* environment) {
  167. UNUSED(environment);
  168. SubGhzProtocolDecoderMagellan* instance = malloc(sizeof(SubGhzProtocolDecoderMagellan));
  169. instance->base.protocol = &subghz_protocol_magellan;
  170. instance->generic.protocol_name = instance->base.protocol->name;
  171. return instance;
  172. }
  173. void subghz_protocol_decoder_magellan_free(void* context) {
  174. furi_assert(context);
  175. SubGhzProtocolDecoderMagellan* instance = context;
  176. free(instance);
  177. }
  178. void subghz_protocol_decoder_magellan_reset(void* context) {
  179. furi_assert(context);
  180. SubGhzProtocolDecoderMagellan* instance = context;
  181. instance->decoder.parser_step = MagellanDecoderStepReset;
  182. }
  183. uint8_t subghz_protocol_magellan_crc8(uint8_t* data, size_t len) {
  184. uint8_t crc = 0x00;
  185. size_t i, j;
  186. for(i = 0; i < len; i++) {
  187. crc ^= data[i];
  188. for(j = 0; j < 8; j++) {
  189. if((crc & 0x80) != 0)
  190. crc = (uint8_t)((crc << 1) ^ 0x31);
  191. else
  192. crc <<= 1;
  193. }
  194. }
  195. return crc;
  196. }
  197. static bool subghz_protocol_magellan_check_crc(SubGhzProtocolDecoderMagellan* instance) {
  198. uint8_t data[3] = {
  199. instance->decoder.decode_data >> 24,
  200. instance->decoder.decode_data >> 16,
  201. instance->decoder.decode_data >> 8};
  202. return (instance->decoder.decode_data & 0xFF) ==
  203. subghz_protocol_magellan_crc8(data, sizeof(data));
  204. }
  205. void subghz_protocol_decoder_magellan_feed(void* context, bool level, uint32_t duration) {
  206. furi_assert(context);
  207. SubGhzProtocolDecoderMagellan* instance = context;
  208. switch(instance->decoder.parser_step) {
  209. case MagellanDecoderStepReset:
  210. if((level) && (DURATION_DIFF(duration, subghz_protocol_magellan_const.te_short) <
  211. subghz_protocol_magellan_const.te_delta)) {
  212. instance->decoder.parser_step = MagellanDecoderStepCheckPreambula;
  213. instance->decoder.te_last = duration;
  214. instance->header_count = 0;
  215. }
  216. break;
  217. case MagellanDecoderStepCheckPreambula:
  218. if(level) {
  219. instance->decoder.te_last = duration;
  220. } else {
  221. if((DURATION_DIFF(instance->decoder.te_last, subghz_protocol_magellan_const.te_short) <
  222. subghz_protocol_magellan_const.te_delta) &&
  223. (DURATION_DIFF(duration, subghz_protocol_magellan_const.te_short) <
  224. subghz_protocol_magellan_const.te_delta)) {
  225. // Found header
  226. instance->header_count++;
  227. } else if(
  228. (DURATION_DIFF(instance->decoder.te_last, subghz_protocol_magellan_const.te_short) <
  229. subghz_protocol_magellan_const.te_delta) &&
  230. (DURATION_DIFF(duration, subghz_protocol_magellan_const.te_long) <
  231. subghz_protocol_magellan_const.te_delta * 2) &&
  232. (instance->header_count > 10)) {
  233. instance->decoder.parser_step = MagellanDecoderStepFoundPreambula;
  234. } else {
  235. instance->decoder.parser_step = MagellanDecoderStepReset;
  236. }
  237. }
  238. break;
  239. case MagellanDecoderStepFoundPreambula:
  240. if(level) {
  241. instance->decoder.te_last = duration;
  242. } else {
  243. if((DURATION_DIFF(
  244. instance->decoder.te_last, subghz_protocol_magellan_const.te_short * 6) <
  245. subghz_protocol_magellan_const.te_delta * 3) &&
  246. (DURATION_DIFF(duration, subghz_protocol_magellan_const.te_long) <
  247. subghz_protocol_magellan_const.te_delta * 2)) {
  248. instance->decoder.parser_step = MagellanDecoderStepSaveDuration;
  249. instance->decoder.decode_data = 0;
  250. instance->decoder.decode_count_bit = 0;
  251. } else {
  252. instance->decoder.parser_step = MagellanDecoderStepReset;
  253. }
  254. }
  255. break;
  256. case MagellanDecoderStepSaveDuration:
  257. if(level) {
  258. instance->decoder.te_last = duration;
  259. instance->decoder.parser_step = MagellanDecoderStepCheckDuration;
  260. } else {
  261. instance->decoder.parser_step = MagellanDecoderStepReset;
  262. }
  263. break;
  264. case MagellanDecoderStepCheckDuration:
  265. if(!level) {
  266. if((DURATION_DIFF(instance->decoder.te_last, subghz_protocol_magellan_const.te_short) <
  267. subghz_protocol_magellan_const.te_delta) &&
  268. (DURATION_DIFF(duration, subghz_protocol_magellan_const.te_long) <
  269. subghz_protocol_magellan_const.te_delta)) {
  270. subghz_protocol_blocks_add_bit(&instance->decoder, 1);
  271. instance->decoder.parser_step = MagellanDecoderStepSaveDuration;
  272. } else if(
  273. (DURATION_DIFF(instance->decoder.te_last, subghz_protocol_magellan_const.te_long) <
  274. subghz_protocol_magellan_const.te_delta) &&
  275. (DURATION_DIFF(duration, subghz_protocol_magellan_const.te_short) <
  276. subghz_protocol_magellan_const.te_delta)) {
  277. subghz_protocol_blocks_add_bit(&instance->decoder, 0);
  278. instance->decoder.parser_step = MagellanDecoderStepSaveDuration;
  279. } else if(duration >= (subghz_protocol_magellan_const.te_long * 3)) {
  280. //Found stop bit
  281. if((instance->decoder.decode_count_bit ==
  282. subghz_protocol_magellan_const.min_count_bit_for_found) &&
  283. subghz_protocol_magellan_check_crc(instance)) {
  284. instance->generic.data = instance->decoder.decode_data;
  285. instance->generic.data_count_bit = instance->decoder.decode_count_bit;
  286. if(instance->base.callback)
  287. instance->base.callback(&instance->base, instance->base.context);
  288. }
  289. instance->decoder.decode_data = 0;
  290. instance->decoder.decode_count_bit = 0;
  291. instance->decoder.parser_step = MagellanDecoderStepReset;
  292. } else {
  293. instance->decoder.parser_step = MagellanDecoderStepReset;
  294. }
  295. } else {
  296. instance->decoder.parser_step = MagellanDecoderStepReset;
  297. }
  298. break;
  299. }
  300. }
  301. /**
  302. * Analysis of received data
  303. * @param instance Pointer to a SubGhzBlockGeneric* instance
  304. */
  305. static void subghz_protocol_magellan_check_remote_controller(SubGhzBlockGeneric* instance) {
  306. /*
  307. * package 32b data 24b CRC8
  308. * 0x037AE4828 => 001101111010111001001000 00101000
  309. *
  310. * 0x037AE48 (flipped in reverse bit sequence) => 0x1275EC
  311. *
  312. * 0x1275EC => 0x12-event codes, 0x75EC-serial (dec 117236)
  313. *
  314. * event codes
  315. * bit_0: 1-Open/Motion, 0-close/ok
  316. * bit_1: 1-Tamper On (alarm), 0-Tamper Off (ok)
  317. * bit_2: ?
  318. * bit_3: 1-power on
  319. * bit_4: model type - wireless reed
  320. * bit_5: model type - motion sensor
  321. * bit_6: ?
  322. * bit_7: ?
  323. *
  324. */
  325. uint64_t data_rev = subghz_protocol_blocks_reverse_key(instance->data >> 8, 24);
  326. instance->serial = data_rev & 0xFFFF;
  327. instance->btn = (data_rev >> 16) & 0xFF;
  328. }
  329. static void subghz_protocol_magellan_get_event_serialize(uint8_t event, FuriString* output) {
  330. furi_string_cat_printf(
  331. output,
  332. "%s%s%s%s%s%s%s%s",
  333. ((event >> 4) & 0x1 ? (event & 0x1 ? " Open" : " Close") :
  334. (event & 0x1 ? " Motion" : " Ok")),
  335. ((event >> 1) & 0x1 ? ", Tamper On\n(Alarm)" : ""),
  336. ((event >> 2) & 0x1 ? ", ?" : ""),
  337. ((event >> 3) & 0x1 ? ", Power On" : ""),
  338. ((event >> 4) & 0x1 ? ", MT:Wireless_Reed" : ""),
  339. ((event >> 5) & 0x1 ? ", MT:Motion_Sensor" : ""),
  340. ((event >> 6) & 0x1 ? ", ?" : ""),
  341. ((event >> 7) & 0x1 ? ", ?" : ""));
  342. }
  343. uint8_t subghz_protocol_decoder_magellan_get_hash_data(void* context) {
  344. furi_assert(context);
  345. SubGhzProtocolDecoderMagellan* instance = context;
  346. return subghz_protocol_blocks_get_hash_data(
  347. &instance->decoder, (instance->decoder.decode_count_bit / 8) + 1);
  348. }
  349. bool subghz_protocol_decoder_magellan_serialize(
  350. void* context,
  351. FlipperFormat* flipper_format,
  352. SubGhzRadioPreset* preset) {
  353. furi_assert(context);
  354. SubGhzProtocolDecoderMagellan* instance = context;
  355. return subghz_block_generic_serialize(&instance->generic, flipper_format, preset);
  356. }
  357. bool subghz_protocol_decoder_magellan_deserialize(void* context, FlipperFormat* flipper_format) {
  358. furi_assert(context);
  359. SubGhzProtocolDecoderMagellan* instance = context;
  360. bool ret = false;
  361. do {
  362. if(!subghz_block_generic_deserialize(&instance->generic, flipper_format)) {
  363. break;
  364. }
  365. if(instance->generic.data_count_bit !=
  366. subghz_protocol_magellan_const.min_count_bit_for_found) {
  367. FURI_LOG_E(TAG, "Wrong number of bits in key");
  368. break;
  369. }
  370. ret = true;
  371. } while(false);
  372. return ret;
  373. }
  374. void subghz_protocol_decoder_magellan_get_string(void* context, FuriString* output) {
  375. furi_assert(context);
  376. SubGhzProtocolDecoderMagellan* instance = context;
  377. subghz_protocol_magellan_check_remote_controller(&instance->generic);
  378. furi_string_cat_printf(
  379. output,
  380. "%s %dbit\r\n"
  381. "Key:0x%08lX\r\n"
  382. "Sn:%03ld%03ld, Event:0x%02X\r\n"
  383. "Stat:",
  384. instance->generic.protocol_name,
  385. instance->generic.data_count_bit,
  386. (uint32_t)(instance->generic.data & 0xFFFFFFFF),
  387. (instance->generic.serial >> 8) & 0xFF,
  388. instance->generic.serial & 0xFF,
  389. instance->generic.btn);
  390. subghz_protocol_magellan_get_event_serialize(instance->generic.btn, output);
  391. }