ambient_weather.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. #include "ambient_weather.h"
  2. #include <lib/toolbox/manchester_decoder.h>
  3. #define TAG "WSProtocolAmbient_Weather"
  4. /*
  5. * Help
  6. * https://github.com/merbanan/rtl_433/blob/master/src/devices/ambient_weather.c
  7. *
  8. * Decode Ambient Weather F007TH, F012TH, TF 30.3208.02, SwitchDoc F016TH.
  9. * Devices supported:
  10. * - Ambient Weather F007TH Thermo-Hygrometer.
  11. * - Ambient Weather F012TH Indoor/Display Thermo-Hygrometer.
  12. * - TFA senders 30.3208.02 from the TFA "Klima-Monitor" 30.3054,
  13. * - SwitchDoc Labs F016TH.
  14. * This decoder handles the 433mhz/868mhz thermo-hygrometers.
  15. * The 915mhz (WH*) family of devices use different modulation/encoding.
  16. * Byte 0 Byte 1 Byte 2 Byte 3 Byte 4 Byte 5
  17. * xxxxMMMM IIIIIIII BCCCTTTT TTTTTTTT HHHHHHHH MMMMMMMM
  18. * - x: Unknown 0x04 on F007TH/F012TH
  19. * - M: Model Number?, 0x05 on F007TH/F012TH/SwitchDocLabs F016TH
  20. * - I: ID byte (8 bits), volatie, changes at power up,
  21. * - B: Battery Low
  22. * - C: Channel (3 bits 1-8) - F007TH set by Dip switch, F012TH soft setting
  23. * - T: Temperature 12 bits - Fahrenheit * 10 + 400
  24. * - H: Humidity (8 bits)
  25. * - M: Message integrity check LFSR Digest-8, gen 0x98, key 0x3e, init 0x64
  26. *
  27. * three repeats without gap
  28. * full preamble is 0x00145 (the last bits might not be fixed, e.g. 0x00146)
  29. * and on decoding also 0xffd45
  30. */
  31. #define AMBIENT_WEATHER_PACKET_HEADER_1 0xFFD440000000000 //0xffd45 .. 0xffd46
  32. #define AMBIENT_WEATHER_PACKET_HEADER_2 0x001440000000000 //0x00145 .. 0x00146
  33. #define AMBIENT_WEATHER_PACKET_HEADER_MASK 0xFFFFC0000000000
  34. static const SubGhzBlockConst ws_protocol_ambient_weather_const = {
  35. .te_short = 500,
  36. .te_long = 1000,
  37. .te_delta = 120,
  38. .min_count_bit_for_found = 48,
  39. };
  40. struct WSProtocolDecoderAmbient_Weather {
  41. SubGhzProtocolDecoderBase base;
  42. SubGhzBlockDecoder decoder;
  43. WSBlockGeneric generic;
  44. ManchesterState manchester_saved_state;
  45. uint16_t header_count;
  46. };
  47. struct WSProtocolEncoderAmbient_Weather {
  48. SubGhzProtocolEncoderBase base;
  49. SubGhzProtocolBlockEncoder encoder;
  50. WSBlockGeneric generic;
  51. };
  52. const SubGhzProtocolDecoder ws_protocol_ambient_weather_decoder = {
  53. .alloc = ws_protocol_decoder_ambient_weather_alloc,
  54. .free = ws_protocol_decoder_ambient_weather_free,
  55. .feed = ws_protocol_decoder_ambient_weather_feed,
  56. .reset = ws_protocol_decoder_ambient_weather_reset,
  57. .get_hash_data = ws_protocol_decoder_ambient_weather_get_hash_data,
  58. .serialize = ws_protocol_decoder_ambient_weather_serialize,
  59. .deserialize = ws_protocol_decoder_ambient_weather_deserialize,
  60. .get_string = ws_protocol_decoder_ambient_weather_get_string,
  61. };
  62. const SubGhzProtocolEncoder ws_protocol_ambient_weather_encoder = {
  63. .alloc = NULL,
  64. .free = NULL,
  65. .deserialize = NULL,
  66. .stop = NULL,
  67. .yield = NULL,
  68. };
  69. const SubGhzProtocol ws_protocol_ambient_weather = {
  70. .name = WS_PROTOCOL_AMBIENT_WEATHER_NAME,
  71. .type = SubGhzProtocolWeatherStation,
  72. .flag = SubGhzProtocolFlag_433 | SubGhzProtocolFlag_315 | SubGhzProtocolFlag_868 |
  73. SubGhzProtocolFlag_AM | SubGhzProtocolFlag_Decodable,
  74. .decoder = &ws_protocol_ambient_weather_decoder,
  75. .encoder = &ws_protocol_ambient_weather_encoder,
  76. };
  77. void* ws_protocol_decoder_ambient_weather_alloc(SubGhzEnvironment* environment) {
  78. UNUSED(environment);
  79. WSProtocolDecoderAmbient_Weather* instance = malloc(sizeof(WSProtocolDecoderAmbient_Weather));
  80. instance->base.protocol = &ws_protocol_ambient_weather;
  81. instance->generic.protocol_name = instance->base.protocol->name;
  82. return instance;
  83. }
  84. void ws_protocol_decoder_ambient_weather_free(void* context) {
  85. furi_assert(context);
  86. WSProtocolDecoderAmbient_Weather* instance = context;
  87. free(instance);
  88. }
  89. void ws_protocol_decoder_ambient_weather_reset(void* context) {
  90. furi_assert(context);
  91. WSProtocolDecoderAmbient_Weather* instance = context;
  92. manchester_advance(
  93. instance->manchester_saved_state,
  94. ManchesterEventReset,
  95. &instance->manchester_saved_state,
  96. NULL);
  97. }
  98. static bool ws_protocol_ambient_weather_check_crc(WSProtocolDecoderAmbient_Weather* instance) {
  99. uint8_t msg[] = {
  100. instance->decoder.decode_data >> 40,
  101. instance->decoder.decode_data >> 32,
  102. instance->decoder.decode_data >> 24,
  103. instance->decoder.decode_data >> 16,
  104. instance->decoder.decode_data >> 8};
  105. uint8_t crc = subghz_protocol_blocks_lfsr_digest8(msg, 5, 0x98, 0x3e) ^ 0x64;
  106. return (crc == (uint8_t)(instance->decoder.decode_data & 0xFF));
  107. }
  108. /**
  109. * Analysis of received data
  110. * @param instance Pointer to a WSBlockGeneric* instance
  111. */
  112. static void ws_protocol_ambient_weather_remote_controller(WSBlockGeneric* instance) {
  113. instance->id = (instance->data >> 32) & 0xFF;
  114. instance->battery_low = (instance->data >> 31) & 1;
  115. instance->channel = ((instance->data >> 28) & 0x07) + 1;
  116. instance->temp =
  117. locale_fahrenheit_to_celsius(((float)((instance->data >> 16) & 0x0FFF) - 400.0f) / 10.0f);
  118. instance->humidity = (instance->data >> 8) & 0xFF;
  119. instance->btn = WS_NO_BTN;
  120. // ToDo maybe it won't be needed
  121. /*
  122. Sanity checks to reduce false positives and other bad data
  123. Packets with Bad data often pass the MIC check.
  124. - humidity > 100 (such as 255) and
  125. - temperatures > 140 F (such as 369.5 F and 348.8 F
  126. Specs in the F007TH and F012TH manuals state the range is:
  127. - Temperature: -40 to 140 F
  128. - Humidity: 10 to 99%
  129. @todo - sanity check b[0] "model number"
  130. - 0x45 - F007TH and F012TH
  131. - 0x?5 - SwitchDocLabs F016TH temperature sensor (based on comment b[0] & 0x0f == 5)
  132. - ? - TFA 30.3208.02
  133. if (instance->humidity < 0 || instance->humidity > 100) {
  134. ERROR;
  135. }
  136. if (instance->temp < -40.0 || instance->temp > 140.0) {
  137. ERROR;
  138. }
  139. */
  140. }
  141. void ws_protocol_decoder_ambient_weather_feed(void* context, bool level, uint32_t duration) {
  142. furi_assert(context);
  143. WSProtocolDecoderAmbient_Weather* instance = context;
  144. ManchesterEvent event = ManchesterEventReset;
  145. if(!level) {
  146. if(DURATION_DIFF(duration, ws_protocol_ambient_weather_const.te_short) <
  147. ws_protocol_ambient_weather_const.te_delta) {
  148. event = ManchesterEventShortLow;
  149. } else if(
  150. DURATION_DIFF(duration, ws_protocol_ambient_weather_const.te_long) <
  151. ws_protocol_ambient_weather_const.te_delta * 2) {
  152. event = ManchesterEventLongLow;
  153. }
  154. } else {
  155. if(DURATION_DIFF(duration, ws_protocol_ambient_weather_const.te_short) <
  156. ws_protocol_ambient_weather_const.te_delta) {
  157. event = ManchesterEventShortHigh;
  158. } else if(
  159. DURATION_DIFF(duration, ws_protocol_ambient_weather_const.te_long) <
  160. ws_protocol_ambient_weather_const.te_delta * 2) {
  161. event = ManchesterEventLongHigh;
  162. }
  163. }
  164. if(event != ManchesterEventReset) {
  165. bool data;
  166. bool data_ok = manchester_advance(
  167. instance->manchester_saved_state, event, &instance->manchester_saved_state, &data);
  168. if(data_ok) {
  169. instance->decoder.decode_data = (instance->decoder.decode_data << 1) | !data;
  170. }
  171. if(((instance->decoder.decode_data & AMBIENT_WEATHER_PACKET_HEADER_MASK) ==
  172. AMBIENT_WEATHER_PACKET_HEADER_1) ||
  173. ((instance->decoder.decode_data & AMBIENT_WEATHER_PACKET_HEADER_MASK) ==
  174. AMBIENT_WEATHER_PACKET_HEADER_2)) {
  175. if(ws_protocol_ambient_weather_check_crc(instance)) {
  176. instance->generic.data = instance->decoder.decode_data;
  177. instance->generic.data_count_bit =
  178. ws_protocol_ambient_weather_const.min_count_bit_for_found;
  179. ws_protocol_ambient_weather_remote_controller(&instance->generic);
  180. if(instance->base.callback)
  181. instance->base.callback(&instance->base, instance->base.context);
  182. instance->decoder.decode_data = 0;
  183. instance->decoder.decode_count_bit = 0;
  184. }
  185. }
  186. } else {
  187. instance->decoder.decode_data = 0;
  188. instance->decoder.decode_count_bit = 0;
  189. manchester_advance(
  190. instance->manchester_saved_state,
  191. ManchesterEventReset,
  192. &instance->manchester_saved_state,
  193. NULL);
  194. }
  195. }
  196. uint8_t ws_protocol_decoder_ambient_weather_get_hash_data(void* context) {
  197. furi_assert(context);
  198. WSProtocolDecoderAmbient_Weather* instance = context;
  199. return subghz_protocol_blocks_get_hash_data(
  200. &instance->decoder, (instance->decoder.decode_count_bit / 8) + 1);
  201. }
  202. SubGhzProtocolStatus ws_protocol_decoder_ambient_weather_serialize(
  203. void* context,
  204. FlipperFormat* flipper_format,
  205. SubGhzRadioPreset* preset) {
  206. furi_assert(context);
  207. WSProtocolDecoderAmbient_Weather* instance = context;
  208. return ws_block_generic_serialize(&instance->generic, flipper_format, preset);
  209. }
  210. SubGhzProtocolStatus
  211. ws_protocol_decoder_ambient_weather_deserialize(void* context, FlipperFormat* flipper_format) {
  212. furi_assert(context);
  213. WSProtocolDecoderAmbient_Weather* instance = context;
  214. return ws_block_generic_deserialize_check_count_bit(
  215. &instance->generic,
  216. flipper_format,
  217. ws_protocol_ambient_weather_const.min_count_bit_for_found);
  218. }
  219. void ws_protocol_decoder_ambient_weather_get_string(void* context, FuriString* output) {
  220. furi_assert(context);
  221. WSProtocolDecoderAmbient_Weather* instance = context;
  222. furi_string_printf(
  223. output,
  224. "%s %dbit\r\n"
  225. "Key:0x%lX%08lX\r\n"
  226. "Sn:0x%lX Ch:%d Bat:%d\r\n"
  227. "Temp:%3.1f C Hum:%d%%",
  228. instance->generic.protocol_name,
  229. instance->generic.data_count_bit,
  230. (uint32_t)(instance->generic.data >> 32),
  231. (uint32_t)(instance->generic.data),
  232. instance->generic.id,
  233. instance->generic.channel,
  234. instance->generic.battery_low,
  235. (double)instance->generic.temp,
  236. instance->generic.humidity);
  237. }