emos_e601x.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. #include "emos_e601x.h"
  2. #define TAG "WSProtocolEmosE601x"
  3. /*
  4. * Help
  5. * https://github.com/merbanan/rtl_433/blob/master/src/devices/emos_e6016.c
  6. *
  7. * Data Layout:
  8. *
  9. * PP PP PP II ?K KK KK KK CT TT HH SS DF XX RR
  10. *
  11. * - P: (24 bit) preamble
  12. * - I: (8 bit) ID
  13. * - ?: (2 bit) unknown
  14. * - K: (32 bit) datetime, fields are 6d-4d-5d 5d:6d:6d
  15. * - C: (2 bit) channel
  16. * - T: (12 bit) temperature, signed, scale 10
  17. * - H: (8 bit) humidity
  18. * - S: (8 bit) wind speed
  19. * - D: (4 bit) wind direction
  20. * - F: (4 bit) flags of (?B??), B is battery good indication
  21. * - X: (8 bit) checksum
  22. * - R: (8 bit) repeat counter
  23. */
  24. static const SubGhzBlockConst ws_protocol_emose601x_const = {
  25. .te_short = 260,
  26. .te_long = 800,
  27. .te_delta = 100,
  28. .min_count_bit_for_found = 24,
  29. };
  30. #define MAGIC_HEADER 0xaaa583
  31. struct WSProtocolDecoderEmosE601x {
  32. SubGhzProtocolDecoderBase base;
  33. SubGhzBlockDecoder decoder;
  34. WSBlockGeneric generic;
  35. uint64_t upper_decode_data;
  36. };
  37. struct WSProtocolEncoderEmosE601x {
  38. SubGhzProtocolEncoderBase base;
  39. SubGhzProtocolBlockEncoder encoder;
  40. WSBlockGeneric generic;
  41. };
  42. typedef enum {
  43. EmosE601xDecoderStepReset = 0,
  44. EmosE601xDecoderStepCheckPreamble,
  45. EmosE601xDecoderStepSaveDuration,
  46. EmosE601xDecoderStepCheckDuration,
  47. } EmosE601xDecoderStep;
  48. const SubGhzProtocolDecoder ws_protocol_emose601x_decoder = {
  49. .alloc = ws_protocol_decoder_emose601x_alloc,
  50. .free = ws_protocol_decoder_emose601x_free,
  51. .feed = ws_protocol_decoder_emose601x_feed,
  52. .reset = ws_protocol_decoder_emose601x_reset,
  53. .get_hash_data = ws_protocol_decoder_emose601x_get_hash_data,
  54. .serialize = ws_protocol_decoder_emose601x_serialize,
  55. .deserialize = ws_protocol_decoder_emose601x_deserialize,
  56. .get_string = ws_protocol_decoder_emose601x_get_string,
  57. };
  58. const SubGhzProtocolEncoder ws_protocol_emose601x_encoder = {
  59. .alloc = NULL,
  60. .free = NULL,
  61. .deserialize = NULL,
  62. .stop = NULL,
  63. .yield = NULL,
  64. };
  65. const SubGhzProtocol ws_protocol_emose601x = {
  66. .name = WS_PROTOCOL_EMOSE601X_NAME,
  67. .type = SubGhzProtocolWeatherStation,
  68. .flag = SubGhzProtocolFlag_433 | SubGhzProtocolFlag_315 | SubGhzProtocolFlag_868 |
  69. SubGhzProtocolFlag_AM | SubGhzProtocolFlag_Decodable,
  70. .decoder = &ws_protocol_emose601x_decoder,
  71. .encoder = &ws_protocol_emose601x_encoder,
  72. };
  73. void* ws_protocol_decoder_emose601x_alloc(SubGhzEnvironment* environment) {
  74. UNUSED(environment);
  75. WSProtocolDecoderEmosE601x* instance = malloc(sizeof(WSProtocolDecoderEmosE601x));
  76. instance->base.protocol = &ws_protocol_emose601x;
  77. instance->generic.protocol_name = instance->base.protocol->name;
  78. return instance;
  79. }
  80. void ws_protocol_decoder_emose601x_free(void* context) {
  81. furi_assert(context);
  82. WSProtocolDecoderEmosE601x* instance = context;
  83. free(instance);
  84. }
  85. void ws_protocol_decoder_emose601x_reset(void* context) {
  86. furi_assert(context);
  87. WSProtocolDecoderEmosE601x* instance = context;
  88. instance->decoder.parser_step = EmosE601xDecoderStepReset;
  89. }
  90. static bool ws_protocol_emose601x_check(WSProtocolDecoderEmosE601x* instance) {
  91. uint8_t msg[] = {
  92. instance->upper_decode_data >> 48,
  93. instance->upper_decode_data >> 40,
  94. instance->upper_decode_data >> 32,
  95. instance->upper_decode_data >> 24,
  96. instance->upper_decode_data >> 16,
  97. instance->upper_decode_data >> 8,
  98. instance->upper_decode_data,
  99. instance->decoder.decode_data >> 56,
  100. instance->decoder.decode_data >> 48,
  101. instance->decoder.decode_data >> 40,
  102. instance->decoder.decode_data >> 32,
  103. instance->decoder.decode_data >> 24,
  104. instance->decoder.decode_data >> 16};
  105. uint8_t sum = subghz_protocol_blocks_add_bytes(msg, 13);
  106. return (sum == ((instance->decoder.decode_data >> 8) & 0xff));
  107. }
  108. /**
  109. * Analysis of received data
  110. * @param instance Pointer to a WSBlockGeneric* instance
  111. */
  112. static void ws_protocol_emose601x_extract_data(WSProtocolDecoderEmosE601x* instance) {
  113. instance->generic.id = (instance->upper_decode_data >> 24) & 0xff;
  114. instance->generic.battery_low = (instance->decoder.decode_data >> 10) & 1;
  115. instance->generic.btn = WS_NO_BTN;
  116. int16_t temp = (instance->decoder.decode_data >> 40) & 0xfff;
  117. /* Handle signed data */
  118. if(temp & 0x800) {
  119. temp |= 0xf000;
  120. }
  121. instance->generic.temp = (float)temp / 10.0;
  122. instance->generic.humidity = (instance->decoder.decode_data >> 32) & 0xff;
  123. instance->generic.channel = (instance->decoder.decode_data >> 52) & 0x03;
  124. instance->generic.data = (instance->decoder.decode_data >> 16);
  125. }
  126. void ws_protocol_decoder_emose601x_feed(void* context, bool level, uint32_t duration) {
  127. furi_assert(context);
  128. WSProtocolDecoderEmosE601x* instance = context;
  129. switch(instance->decoder.parser_step) {
  130. case EmosE601xDecoderStepReset:
  131. if((level) && (DURATION_DIFF(duration, ws_protocol_emose601x_const.te_short * 7) <
  132. ws_protocol_emose601x_const.te_delta * 2)) {
  133. instance->decoder.parser_step = EmosE601xDecoderStepCheckPreamble;
  134. instance->decoder.te_last = duration;
  135. }
  136. break;
  137. case EmosE601xDecoderStepCheckPreamble:
  138. if(level) {
  139. instance->decoder.te_last = duration;
  140. } else {
  141. if((DURATION_DIFF(instance->decoder.te_last, ws_protocol_emose601x_const.te_short * 7) <
  142. ws_protocol_emose601x_const.te_delta * 2) &&
  143. (DURATION_DIFF(duration, ws_protocol_emose601x_const.te_short) <
  144. ws_protocol_emose601x_const.te_delta)) {
  145. //Found preamble
  146. instance->decoder.parser_step = EmosE601xDecoderStepSaveDuration;
  147. instance->decoder.decode_data = 0;
  148. instance->decoder.decode_count_bit = 0;
  149. } else {
  150. instance->decoder.parser_step = EmosE601xDecoderStepReset;
  151. }
  152. }
  153. break;
  154. case EmosE601xDecoderStepSaveDuration:
  155. if(level) {
  156. instance->decoder.te_last = duration;
  157. instance->decoder.parser_step = EmosE601xDecoderStepCheckDuration;
  158. } else {
  159. instance->decoder.parser_step = EmosE601xDecoderStepReset;
  160. }
  161. break;
  162. case EmosE601xDecoderStepCheckDuration:
  163. if(!level) {
  164. if((DURATION_DIFF(instance->decoder.te_last, ws_protocol_emose601x_const.te_short) <
  165. ws_protocol_emose601x_const.te_delta) &&
  166. (DURATION_DIFF(duration, ws_protocol_emose601x_const.te_long) <
  167. ws_protocol_emose601x_const.te_delta)) {
  168. subghz_protocol_blocks_add_to_128_bit(
  169. &instance->decoder, 0, &instance->upper_decode_data);
  170. instance->decoder.parser_step = EmosE601xDecoderStepSaveDuration;
  171. } else if(
  172. (DURATION_DIFF(instance->decoder.te_last, ws_protocol_emose601x_const.te_long) <
  173. ws_protocol_emose601x_const.te_delta) &&
  174. (DURATION_DIFF(duration, ws_protocol_emose601x_const.te_short) <
  175. ws_protocol_emose601x_const.te_delta)) {
  176. subghz_protocol_blocks_add_to_128_bit(
  177. &instance->decoder, 1, &instance->upper_decode_data);
  178. instance->decoder.parser_step = EmosE601xDecoderStepSaveDuration;
  179. } else {
  180. instance->decoder.parser_step = EmosE601xDecoderStepReset;
  181. break;
  182. }
  183. /* Bail out if the header doesn't match */
  184. if(instance->decoder.decode_count_bit ==
  185. ws_protocol_emose601x_const.min_count_bit_for_found) {
  186. if(instance->decoder.decode_data != MAGIC_HEADER) {
  187. instance->decoder.parser_step = EmosE601xDecoderStepReset;
  188. break;
  189. }
  190. }
  191. if(instance->decoder.decode_count_bit == 120) {
  192. instance->generic.data_count_bit = instance->decoder.decode_count_bit;
  193. if(ws_protocol_emose601x_check(instance)) {
  194. ws_protocol_emose601x_extract_data(instance);
  195. if(instance->base.callback)
  196. instance->base.callback(&instance->base, instance->base.context);
  197. }
  198. break;
  199. }
  200. } else {
  201. instance->decoder.parser_step = EmosE601xDecoderStepReset;
  202. }
  203. break;
  204. }
  205. }
  206. uint8_t ws_protocol_decoder_emose601x_get_hash_data(void* context) {
  207. furi_assert(context);
  208. WSProtocolDecoderEmosE601x* instance = context;
  209. return subghz_protocol_blocks_get_hash_data(
  210. &instance->decoder, (instance->decoder.decode_count_bit / 8) + 1);
  211. }
  212. SubGhzProtocolStatus ws_protocol_decoder_emose601x_serialize(
  213. void* context,
  214. FlipperFormat* flipper_format,
  215. SubGhzRadioPreset* preset) {
  216. furi_assert(context);
  217. WSProtocolDecoderEmosE601x* instance = context;
  218. return ws_block_generic_serialize(&instance->generic, flipper_format, preset);
  219. }
  220. SubGhzProtocolStatus
  221. ws_protocol_decoder_emose601x_deserialize(void* context, FlipperFormat* flipper_format) {
  222. furi_assert(context);
  223. WSProtocolDecoderEmosE601x* instance = context;
  224. return ws_block_generic_deserialize_check_count_bit(
  225. &instance->generic, flipper_format, ws_protocol_emose601x_const.min_count_bit_for_found);
  226. }
  227. void ws_protocol_decoder_emose601x_get_string(void* context, FuriString* output) {
  228. furi_assert(context);
  229. WSProtocolDecoderEmosE601x* instance = context;
  230. furi_string_printf(
  231. output,
  232. "%s %dbit\r\n"
  233. "Code:0x%ld\r\n"
  234. "Ch:%d Bat:%d\r\n"
  235. "Temp:%3.1f C Hum:%d%%",
  236. instance->generic.protocol_name,
  237. instance->generic.data_count_bit,
  238. instance->generic.id,
  239. instance->generic.channel,
  240. instance->generic.battery_low,
  241. (double)instance->generic.temp,
  242. instance->generic.humidity);
  243. }