power_smart.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394
  1. #include "power_smart.h"
  2. #include <lib/toolbox/manchester_decoder.h>
  3. #include <lib/toolbox/manchester_encoder.h>
  4. #include "../blocks/const.h"
  5. #include "../blocks/decoder.h"
  6. #include "../blocks/encoder.h"
  7. #include "../blocks/generic.h"
  8. #include "../blocks/math.h"
  9. #define TAG "SubGhzProtocolPowerSmart"
  10. #define POWER_SMART_PACKET_HEADER 0xFD000000AA000000
  11. #define POWER_SMART_PACKET_HEADER_MASK 0xFF000000FF000000
  12. #define CHANNEL_PATTERN "%c%c%c%c%c%c"
  13. #define CNT_TO_CHANNEL(dip) \
  14. (dip & 0x0001 ? '*' : '-'), (dip & 0x0002 ? '*' : '-'), (dip & 0x0004 ? '*' : '-'), \
  15. (dip & 0x0008 ? '*' : '-'), (dip & 0x0010 ? '*' : '-'), (dip & 0x0020 ? '*' : '-')
  16. static const SubGhzBlockConst subghz_protocol_power_smart_const = {
  17. .te_short = 225,
  18. .te_long = 450,
  19. .te_delta = 100,
  20. .min_count_bit_for_found = 64,
  21. };
  22. struct SubGhzProtocolDecoderPowerSmart {
  23. SubGhzProtocolDecoderBase base;
  24. SubGhzBlockDecoder decoder;
  25. SubGhzBlockGeneric generic;
  26. ManchesterState manchester_saved_state;
  27. uint16_t header_count;
  28. };
  29. struct SubGhzProtocolEncoderPowerSmart {
  30. SubGhzProtocolEncoderBase base;
  31. SubGhzProtocolBlockEncoder encoder;
  32. SubGhzBlockGeneric generic;
  33. };
  34. typedef enum {
  35. PowerSmartDecoderStepReset = 0,
  36. PowerSmartDecoderFoundHeader,
  37. PowerSmartDecoderStepDecoderData,
  38. } PowerSmartDecoderStep;
  39. const SubGhzProtocolDecoder subghz_protocol_power_smart_decoder = {
  40. .alloc = subghz_protocol_decoder_power_smart_alloc,
  41. .free = subghz_protocol_decoder_power_smart_free,
  42. .feed = subghz_protocol_decoder_power_smart_feed,
  43. .reset = subghz_protocol_decoder_power_smart_reset,
  44. .get_hash_data = subghz_protocol_decoder_power_smart_get_hash_data,
  45. .serialize = subghz_protocol_decoder_power_smart_serialize,
  46. .deserialize = subghz_protocol_decoder_power_smart_deserialize,
  47. .get_string = subghz_protocol_decoder_power_smart_get_string,
  48. };
  49. const SubGhzProtocolEncoder subghz_protocol_power_smart_encoder = {
  50. .alloc = subghz_protocol_encoder_power_smart_alloc,
  51. .free = subghz_protocol_encoder_power_smart_free,
  52. .deserialize = subghz_protocol_encoder_power_smart_deserialize,
  53. .stop = subghz_protocol_encoder_power_smart_stop,
  54. .yield = subghz_protocol_encoder_power_smart_yield,
  55. };
  56. const SubGhzProtocol subghz_protocol_power_smart = {
  57. .name = SUBGHZ_PROTOCOL_POWER_SMART_NAME,
  58. .type = SubGhzProtocolTypeStatic,
  59. .flag = SubGhzProtocolFlag_433 | SubGhzProtocolFlag_AM | SubGhzProtocolFlag_Decodable |
  60. SubGhzProtocolFlag_Load | SubGhzProtocolFlag_Save | SubGhzProtocolFlag_Send,
  61. .decoder = &subghz_protocol_power_smart_decoder,
  62. .encoder = &subghz_protocol_power_smart_encoder,
  63. };
  64. void* subghz_protocol_encoder_power_smart_alloc(SubGhzEnvironment* environment) {
  65. UNUSED(environment);
  66. SubGhzProtocolEncoderPowerSmart* instance = malloc(sizeof(SubGhzProtocolEncoderPowerSmart));
  67. instance->base.protocol = &subghz_protocol_power_smart;
  68. instance->generic.protocol_name = instance->base.protocol->name;
  69. instance->encoder.repeat = 10;
  70. instance->encoder.size_upload = 1024;
  71. instance->encoder.upload = malloc(instance->encoder.size_upload * sizeof(LevelDuration));
  72. instance->encoder.is_running = false;
  73. return instance;
  74. }
  75. void subghz_protocol_encoder_power_smart_free(void* context) {
  76. furi_assert(context);
  77. SubGhzProtocolEncoderPowerSmart* instance = context;
  78. free(instance->encoder.upload);
  79. free(instance);
  80. }
  81. static LevelDuration
  82. subghz_protocol_encoder_power_smart_add_duration_to_upload(ManchesterEncoderResult result) {
  83. LevelDuration data = {.duration = 0, .level = 0};
  84. switch(result) {
  85. case ManchesterEncoderResultShortLow:
  86. data.duration = subghz_protocol_power_smart_const.te_short;
  87. data.level = false;
  88. break;
  89. case ManchesterEncoderResultLongLow:
  90. data.duration = subghz_protocol_power_smart_const.te_long;
  91. data.level = false;
  92. break;
  93. case ManchesterEncoderResultLongHigh:
  94. data.duration = subghz_protocol_power_smart_const.te_long;
  95. data.level = true;
  96. break;
  97. case ManchesterEncoderResultShortHigh:
  98. data.duration = subghz_protocol_power_smart_const.te_short;
  99. data.level = true;
  100. break;
  101. default:
  102. furi_crash("SubGhz: ManchesterEncoderResult is incorrect.");
  103. break;
  104. }
  105. return level_duration_make(data.level, data.duration);
  106. }
  107. /**
  108. * Generating an upload from data.
  109. * @param instance Pointer to a SubGhzProtocolEncoderPowerSmart instance
  110. */
  111. static void
  112. subghz_protocol_encoder_power_smart_get_upload(SubGhzProtocolEncoderPowerSmart* instance) {
  113. furi_assert(instance);
  114. size_t index = 0;
  115. ManchesterEncoderState enc_state;
  116. manchester_encoder_reset(&enc_state);
  117. ManchesterEncoderResult result;
  118. for(int i = 8; i > 0; i--) {
  119. for(uint8_t i = instance->generic.data_count_bit; i > 0; i--) {
  120. if(!manchester_encoder_advance(
  121. &enc_state, !bit_read(instance->generic.data, i - 1), &result)) {
  122. instance->encoder.upload[index++] =
  123. subghz_protocol_encoder_power_smart_add_duration_to_upload(result);
  124. manchester_encoder_advance(
  125. &enc_state, !bit_read(instance->generic.data, i - 1), &result);
  126. }
  127. instance->encoder.upload[index++] =
  128. subghz_protocol_encoder_power_smart_add_duration_to_upload(result);
  129. }
  130. }
  131. instance->encoder.upload[index] = subghz_protocol_encoder_power_smart_add_duration_to_upload(
  132. manchester_encoder_finish(&enc_state));
  133. if(level_duration_get_level(instance->encoder.upload[index])) {
  134. index++;
  135. }
  136. instance->encoder.upload[index++] =
  137. level_duration_make(false, (uint32_t)subghz_protocol_power_smart_const.te_long * 1111);
  138. instance->encoder.size_upload = index;
  139. }
  140. /**
  141. * Analysis of received data
  142. * @param instance Pointer to a SubGhzBlockGeneric* instance
  143. */
  144. static void subghz_protocol_power_smart_remote_controller(SubGhzBlockGeneric* instance) {
  145. /*
  146. * Protocol: Manchester encoding, symbol rate ~2222.
  147. * Packet Format:
  148. * 0xFDXXXXYYAAZZZZWW where 0xFD and 0xAA sync word
  149. * XXXX = ~ZZZZ, YY=(~WW)-1
  150. * Example:
  151. * SYNC1 K1 CHANNEL DATA1 K2 DATA2 SYNC2 ~K1 ~CHANNEL ~DATA2 ~K2 (~DATA2)-1
  152. * 0xFD2137ACAADEC852 => 11111101 0 010000 10011011 1 10101100 10101010 1 1011110 1100100 0 01010010
  153. * 0xFDA137ACAA5EC852 => 11111101 1 010000 10011011 1 10101100 10101010 0 1011110 1100100 0 01010010
  154. * 0xFDA136ACAA5EC952 => 11111101 1 010000 10011011 0 10101100 10101010 0 1011110 1100100 1 01010010
  155. *
  156. * Key:
  157. * K1K2
  158. * 0 0 - key_unknown
  159. * 0 1 - key_down
  160. * 1 0 - key_up
  161. * 1 1 - key_stop
  162. *
  163. */
  164. instance->btn = ((instance->data >> 54) & 0x02) | ((instance->data >> 40) & 0x1);
  165. instance->serial = ((instance->data >> 33) & 0x3FFF00) | ((instance->data >> 32) & 0xFF);
  166. instance->cnt = ((instance->data >> 49) & 0x3F);
  167. }
  168. bool subghz_protocol_encoder_power_smart_deserialize(void* context, FlipperFormat* flipper_format) {
  169. furi_assert(context);
  170. SubGhzProtocolEncoderPowerSmart* instance = context;
  171. bool res = false;
  172. do {
  173. if(!subghz_block_generic_deserialize(&instance->generic, flipper_format)) {
  174. FURI_LOG_E(TAG, "Deserialize error");
  175. break;
  176. }
  177. if(instance->generic.data_count_bit !=
  178. subghz_protocol_power_smart_const.min_count_bit_for_found) {
  179. FURI_LOG_E(TAG, "Wrong number of bits in key");
  180. break;
  181. }
  182. //optional parameter parameter
  183. flipper_format_read_uint32(
  184. flipper_format, "Repeat", (uint32_t*)&instance->encoder.repeat, 1);
  185. subghz_protocol_power_smart_remote_controller(&instance->generic);
  186. subghz_protocol_encoder_power_smart_get_upload(instance);
  187. instance->encoder.is_running = true;
  188. res = true;
  189. } while(false);
  190. return res;
  191. }
  192. void subghz_protocol_encoder_power_smart_stop(void* context) {
  193. SubGhzProtocolEncoderPowerSmart* instance = context;
  194. instance->encoder.is_running = false;
  195. }
  196. LevelDuration subghz_protocol_encoder_power_smart_yield(void* context) {
  197. SubGhzProtocolEncoderPowerSmart* instance = context;
  198. if(instance->encoder.repeat == 0 || !instance->encoder.is_running) {
  199. instance->encoder.is_running = 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_power_smart_alloc(SubGhzEnvironment* environment) {
  210. UNUSED(environment);
  211. SubGhzProtocolDecoderPowerSmart* instance = malloc(sizeof(SubGhzProtocolDecoderPowerSmart));
  212. instance->base.protocol = &subghz_protocol_power_smart;
  213. instance->generic.protocol_name = instance->base.protocol->name;
  214. return instance;
  215. }
  216. void subghz_protocol_decoder_power_smart_free(void* context) {
  217. furi_assert(context);
  218. SubGhzProtocolDecoderPowerSmart* instance = context;
  219. free(instance);
  220. }
  221. void subghz_protocol_decoder_power_smart_reset(void* context) {
  222. furi_assert(context);
  223. SubGhzProtocolDecoderPowerSmart* instance = context;
  224. manchester_advance(
  225. instance->manchester_saved_state,
  226. ManchesterEventReset,
  227. &instance->manchester_saved_state,
  228. NULL);
  229. }
  230. bool subghz_protocol_power_smart_chek_valid(uint64_t packet) {
  231. uint32_t data_1 = (uint32_t)((packet >> 40) & 0xFFFF);
  232. uint32_t data_2 = (uint32_t)((~packet >> 8) & 0xFFFF);
  233. uint8_t data_3 = (uint8_t)(packet >> 32) & 0xFF;
  234. uint8_t data_4 = (uint8_t)(((~packet) & 0xFF) - 1);
  235. return (data_1 == data_2) && (data_3 == data_4);
  236. }
  237. void subghz_protocol_decoder_power_smart_feed(
  238. void* context,
  239. bool level,
  240. volatile uint32_t duration) {
  241. furi_assert(context);
  242. SubGhzProtocolDecoderPowerSmart* instance = context;
  243. ManchesterEvent event = ManchesterEventReset;
  244. if(!level) {
  245. if(DURATION_DIFF(duration, subghz_protocol_power_smart_const.te_short) <
  246. subghz_protocol_power_smart_const.te_delta) {
  247. event = ManchesterEventShortLow;
  248. } else if(
  249. DURATION_DIFF(duration, subghz_protocol_power_smart_const.te_long) <
  250. subghz_protocol_power_smart_const.te_delta * 2) {
  251. event = ManchesterEventLongLow;
  252. }
  253. } else {
  254. if(DURATION_DIFF(duration, subghz_protocol_power_smart_const.te_short) <
  255. subghz_protocol_power_smart_const.te_delta) {
  256. event = ManchesterEventShortHigh;
  257. } else if(
  258. DURATION_DIFF(duration, subghz_protocol_power_smart_const.te_long) <
  259. subghz_protocol_power_smart_const.te_delta * 2) {
  260. event = ManchesterEventLongHigh;
  261. }
  262. }
  263. if(event != ManchesterEventReset) {
  264. bool data;
  265. bool data_ok = manchester_advance(
  266. instance->manchester_saved_state, event, &instance->manchester_saved_state, &data);
  267. if(data_ok) {
  268. instance->decoder.decode_data = (instance->decoder.decode_data << 1) | !data;
  269. }
  270. if((instance->decoder.decode_data & POWER_SMART_PACKET_HEADER_MASK) ==
  271. POWER_SMART_PACKET_HEADER) {
  272. if(subghz_protocol_power_smart_chek_valid(instance->decoder.decode_data)) {
  273. instance->generic.data = instance->decoder.decode_data;
  274. instance->generic.data_count_bit =
  275. subghz_protocol_power_smart_const.min_count_bit_for_found;
  276. if(instance->base.callback)
  277. instance->base.callback(&instance->base, instance->base.context);
  278. instance->decoder.decode_data = 0;
  279. instance->decoder.decode_count_bit = 0;
  280. }
  281. }
  282. } else {
  283. instance->decoder.decode_data = 0;
  284. instance->decoder.decode_count_bit = 0;
  285. manchester_advance(
  286. instance->manchester_saved_state,
  287. ManchesterEventReset,
  288. &instance->manchester_saved_state,
  289. NULL);
  290. }
  291. }
  292. static const char* subghz_protocol_power_smart_get_name_button(uint8_t btn) {
  293. btn &= 0x3;
  294. const char* name_btn[0x4] = {"Unknown", "Down", "Up", "Stop"};
  295. return name_btn[btn];
  296. }
  297. uint8_t subghz_protocol_decoder_power_smart_get_hash_data(void* context) {
  298. furi_assert(context);
  299. SubGhzProtocolDecoderPowerSmart* instance = context;
  300. return subghz_protocol_blocks_get_hash_data(
  301. &instance->decoder, (instance->decoder.decode_count_bit / 8) + 1);
  302. }
  303. bool subghz_protocol_decoder_power_smart_serialize(
  304. void* context,
  305. FlipperFormat* flipper_format,
  306. SubGhzRadioPreset* preset) {
  307. furi_assert(context);
  308. SubGhzProtocolDecoderPowerSmart* instance = context;
  309. return subghz_block_generic_serialize(&instance->generic, flipper_format, preset);
  310. }
  311. bool subghz_protocol_decoder_power_smart_deserialize(void* context, FlipperFormat* flipper_format) {
  312. furi_assert(context);
  313. SubGhzProtocolDecoderPowerSmart* instance = context;
  314. bool ret = false;
  315. do {
  316. if(!subghz_block_generic_deserialize(&instance->generic, flipper_format)) {
  317. break;
  318. }
  319. if(instance->generic.data_count_bit !=
  320. subghz_protocol_power_smart_const.min_count_bit_for_found) {
  321. FURI_LOG_E(TAG, "Wrong number of bits in key");
  322. break;
  323. }
  324. ret = true;
  325. } while(false);
  326. return ret;
  327. }
  328. void subghz_protocol_decoder_power_smart_get_string(void* context, FuriString* output) {
  329. furi_assert(context);
  330. SubGhzProtocolDecoderPowerSmart* instance = context;
  331. subghz_protocol_power_smart_remote_controller(&instance->generic);
  332. furi_string_cat_printf(
  333. output,
  334. "%s %db\r\n"
  335. "Key:0x%lX%08lX\r\n"
  336. "Sn:0x%07lX \r\n"
  337. "Btn:%s\r\n"
  338. "Channel:" CHANNEL_PATTERN "\r\n",
  339. instance->generic.protocol_name,
  340. instance->generic.data_count_bit,
  341. (uint32_t)(instance->generic.data >> 32),
  342. (uint32_t)(instance->generic.data & 0xFFFFFFFF),
  343. instance->generic.serial,
  344. subghz_protocol_power_smart_get_name_button(instance->generic.btn),
  345. CNT_TO_CHANNEL(instance->generic.cnt));
  346. }