tx_8300.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. #include "tx_8300.h"
  2. #define TAG "WSProtocolTX_8300"
  3. /*
  4. * Help
  5. * https://github.com/merbanan/rtl_433/blob/master/src/devices/ambientweather_tx8300.c
  6. *
  7. * Ambient Weather TX-8300 (also sold as TFA 30.3211.02).
  8. * 1970us pulse with variable gap (third pulse 3920 us).
  9. * Above 79% humidity, gap after third pulse is 5848 us.
  10. * - Bit 1 : 1970us pulse with 3888 us gap
  11. * - Bit 0 : 1970us pulse with 1936 us gap
  12. * 74 bit (2 bit preamble and 72 bit data => 9 bytes => 18 nibbles)
  13. * The preamble seems to be a repeat counter (00, and 01 seen),
  14. * the first 4 bytes are data,
  15. * the second 4 bytes the same data inverted,
  16. * the last byte is a checksum.
  17. * Preamble format (2 bits):
  18. * [1 bit (0)] [1 bit rolling count]
  19. * Payload format (32 bits):
  20. * HHHHhhhh ??CCNIII IIIITTTT ttttuuuu
  21. * - H = First BCD digit humidity (the MSB might be distorted by the demod)
  22. * - h = Second BCD digit humidity, invalid humidity seems to be 0x0e
  23. * - ? = Likely battery flag, 2 bits
  24. * - C = Channel, 2 bits
  25. * - N = Negative temperature sign bit
  26. * - I = ID, 7-bit
  27. * - T = First BCD digit temperature
  28. * - t = Second BCD digit temperature
  29. * - u = Third BCD digit temperature
  30. * The Checksum seems to covers the 4 data bytes and is something like Fletcher-8.
  31. **/
  32. #define TX_8300_PACKAGE_SIZE 32
  33. static const SubGhzBlockConst ws_protocol_tx_8300_const = {
  34. .te_short = 1940,
  35. .te_long = 3880,
  36. .te_delta = 250,
  37. .min_count_bit_for_found = 72,
  38. };
  39. struct WSProtocolDecoderTX_8300 {
  40. SubGhzProtocolDecoderBase base;
  41. SubGhzBlockDecoder decoder;
  42. WSBlockGeneric generic;
  43. uint32_t package_1;
  44. uint32_t package_2;
  45. };
  46. struct WSProtocolEncoderTX_8300 {
  47. SubGhzProtocolEncoderBase base;
  48. SubGhzProtocolBlockEncoder encoder;
  49. WSBlockGeneric generic;
  50. };
  51. typedef enum {
  52. TX_8300DecoderStepReset = 0,
  53. TX_8300DecoderStepCheckPreambule,
  54. TX_8300DecoderStepSaveDuration,
  55. TX_8300DecoderStepCheckDuration,
  56. } TX_8300DecoderStep;
  57. const SubGhzProtocolDecoder ws_protocol_tx_8300_decoder = {
  58. .alloc = ws_protocol_decoder_tx_8300_alloc,
  59. .free = ws_protocol_decoder_tx_8300_free,
  60. .feed = ws_protocol_decoder_tx_8300_feed,
  61. .reset = ws_protocol_decoder_tx_8300_reset,
  62. .get_hash_data = ws_protocol_decoder_tx_8300_get_hash_data,
  63. .serialize = ws_protocol_decoder_tx_8300_serialize,
  64. .deserialize = ws_protocol_decoder_tx_8300_deserialize,
  65. .get_string = ws_protocol_decoder_tx_8300_get_string,
  66. };
  67. const SubGhzProtocolEncoder ws_protocol_tx_8300_encoder = {
  68. .alloc = NULL,
  69. .free = NULL,
  70. .deserialize = NULL,
  71. .stop = NULL,
  72. .yield = NULL,
  73. };
  74. const SubGhzProtocol ws_protocol_tx_8300 = {
  75. .name = WS_PROTOCOL_TX_8300_NAME,
  76. .type = SubGhzProtocolWeatherStation,
  77. .flag = SubGhzProtocolFlag_433 | SubGhzProtocolFlag_315 | SubGhzProtocolFlag_868 |
  78. SubGhzProtocolFlag_AM | SubGhzProtocolFlag_Decodable,
  79. .decoder = &ws_protocol_tx_8300_decoder,
  80. .encoder = &ws_protocol_tx_8300_encoder,
  81. };
  82. void* ws_protocol_decoder_tx_8300_alloc(SubGhzEnvironment* environment) {
  83. UNUSED(environment);
  84. WSProtocolDecoderTX_8300* instance = malloc(sizeof(WSProtocolDecoderTX_8300));
  85. instance->base.protocol = &ws_protocol_tx_8300;
  86. instance->generic.protocol_name = instance->base.protocol->name;
  87. return instance;
  88. }
  89. void ws_protocol_decoder_tx_8300_free(void* context) {
  90. furi_assert(context);
  91. WSProtocolDecoderTX_8300* instance = context;
  92. free(instance);
  93. }
  94. void ws_protocol_decoder_tx_8300_reset(void* context) {
  95. furi_assert(context);
  96. WSProtocolDecoderTX_8300* instance = context;
  97. instance->decoder.parser_step = TX_8300DecoderStepReset;
  98. }
  99. static bool ws_protocol_tx_8300_check_crc(WSProtocolDecoderTX_8300* instance) {
  100. if(!instance->package_2) return false;
  101. if(instance->package_1 != ~instance->package_2) return false;
  102. uint16_t x = 0;
  103. uint16_t y = 0;
  104. for(int i = 0; i < 32; i += 4) {
  105. x += (instance->package_1 >> i) & 0x0F;
  106. y += (instance->package_1 >> i) & 0x05;
  107. }
  108. uint8_t crc = (~x & 0xF) << 4 | (~y & 0xF);
  109. return (crc == ((instance->decoder.decode_data) & 0xFF));
  110. }
  111. /**
  112. * Analysis of received data
  113. * @param instance Pointer to a WSBlockGeneric* instance
  114. */
  115. static void ws_protocol_tx_8300_remote_controller(WSBlockGeneric* instance) {
  116. instance->humidity = (((instance->data >> 28) & 0x0F) * 10) + ((instance->data >> 24) & 0x0F);
  117. instance->btn = WS_NO_BTN;
  118. if(!((instance->data >> 22) & 0x03))
  119. instance->battery_low = 0;
  120. else
  121. instance->battery_low = 1;
  122. instance->channel = (instance->data >> 20) & 0x03;
  123. instance->id = (instance->data >> 12) & 0x7F;
  124. float temp_raw = ((instance->data >> 8) & 0x0F) * 10.0f + ((instance->data >> 4) & 0x0F) +
  125. (instance->data & 0x0F) * 0.1f;
  126. if(!((instance->data >> 19) & 1)) {
  127. instance->temp = temp_raw;
  128. } else {
  129. instance->temp = -temp_raw;
  130. }
  131. }
  132. void ws_protocol_decoder_tx_8300_feed(void* context, bool level, uint32_t duration) {
  133. furi_assert(context);
  134. WSProtocolDecoderTX_8300* instance = context;
  135. switch(instance->decoder.parser_step) {
  136. case TX_8300DecoderStepReset:
  137. if((level) && (DURATION_DIFF(duration, ws_protocol_tx_8300_const.te_short * 2) <
  138. ws_protocol_tx_8300_const.te_delta)) {
  139. instance->decoder.parser_step = TX_8300DecoderStepCheckPreambule;
  140. }
  141. break;
  142. case TX_8300DecoderStepCheckPreambule:
  143. if((!level) && ((DURATION_DIFF(duration, ws_protocol_tx_8300_const.te_short * 2) <
  144. ws_protocol_tx_8300_const.te_delta) ||
  145. (DURATION_DIFF(duration, ws_protocol_tx_8300_const.te_short * 3) <
  146. ws_protocol_tx_8300_const.te_delta))) {
  147. instance->decoder.parser_step = TX_8300DecoderStepSaveDuration;
  148. instance->decoder.decode_data = 0;
  149. instance->decoder.decode_count_bit = 1;
  150. instance->package_1 = 0;
  151. instance->package_2 = 0;
  152. } else {
  153. instance->decoder.parser_step = TX_8300DecoderStepReset;
  154. }
  155. break;
  156. case TX_8300DecoderStepSaveDuration:
  157. if(level) {
  158. instance->decoder.te_last = duration;
  159. instance->decoder.parser_step = TX_8300DecoderStepCheckDuration;
  160. } else {
  161. instance->decoder.parser_step = TX_8300DecoderStepReset;
  162. }
  163. break;
  164. case TX_8300DecoderStepCheckDuration:
  165. if(!level) {
  166. if(duration >= ((uint32_t)ws_protocol_tx_8300_const.te_short * 5)) {
  167. //Found syncPostfix
  168. if((instance->decoder.decode_count_bit ==
  169. ws_protocol_tx_8300_const.min_count_bit_for_found) &&
  170. ws_protocol_tx_8300_check_crc(instance)) {
  171. instance->generic.data = instance->package_1;
  172. instance->generic.data_count_bit = instance->decoder.decode_count_bit;
  173. ws_protocol_tx_8300_remote_controller(&instance->generic);
  174. if(instance->base.callback)
  175. instance->base.callback(&instance->base, instance->base.context);
  176. }
  177. instance->decoder.decode_data = 0;
  178. instance->decoder.decode_count_bit = 1;
  179. instance->decoder.parser_step = TX_8300DecoderStepReset;
  180. break;
  181. } else if(
  182. (DURATION_DIFF(instance->decoder.te_last, ws_protocol_tx_8300_const.te_short) <
  183. ws_protocol_tx_8300_const.te_delta) &&
  184. (DURATION_DIFF(duration, ws_protocol_tx_8300_const.te_long) <
  185. ws_protocol_tx_8300_const.te_delta * 2)) {
  186. subghz_protocol_blocks_add_bit(&instance->decoder, 1);
  187. instance->decoder.parser_step = TX_8300DecoderStepSaveDuration;
  188. } else if(
  189. (DURATION_DIFF(instance->decoder.te_last, ws_protocol_tx_8300_const.te_short) <
  190. ws_protocol_tx_8300_const.te_delta) &&
  191. (DURATION_DIFF(duration, ws_protocol_tx_8300_const.te_short) <
  192. ws_protocol_tx_8300_const.te_delta)) {
  193. subghz_protocol_blocks_add_bit(&instance->decoder, 0);
  194. instance->decoder.parser_step = TX_8300DecoderStepSaveDuration;
  195. } else {
  196. instance->decoder.parser_step = TX_8300DecoderStepReset;
  197. }
  198. if(instance->decoder.decode_count_bit == TX_8300_PACKAGE_SIZE) {
  199. instance->package_1 = instance->decoder.decode_data;
  200. instance->decoder.decode_data = 0;
  201. } else if(instance->decoder.decode_count_bit == TX_8300_PACKAGE_SIZE * 2) {
  202. instance->package_2 = instance->decoder.decode_data;
  203. instance->decoder.decode_data = 0;
  204. }
  205. } else {
  206. instance->decoder.parser_step = TX_8300DecoderStepReset;
  207. }
  208. break;
  209. }
  210. }
  211. uint8_t ws_protocol_decoder_tx_8300_get_hash_data(void* context) {
  212. furi_assert(context);
  213. WSProtocolDecoderTX_8300* instance = context;
  214. return subghz_protocol_blocks_get_hash_data(
  215. &instance->decoder, (instance->decoder.decode_count_bit / 8) + 1);
  216. }
  217. SubGhzProtocolStatus ws_protocol_decoder_tx_8300_serialize(
  218. void* context,
  219. FlipperFormat* flipper_format,
  220. SubGhzRadioPreset* preset) {
  221. furi_assert(context);
  222. WSProtocolDecoderTX_8300* instance = context;
  223. return ws_block_generic_serialize(&instance->generic, flipper_format, preset);
  224. }
  225. SubGhzProtocolStatus
  226. ws_protocol_decoder_tx_8300_deserialize(void* context, FlipperFormat* flipper_format) {
  227. furi_assert(context);
  228. WSProtocolDecoderTX_8300* instance = context;
  229. return ws_block_generic_deserialize_check_count_bit(
  230. &instance->generic, flipper_format, ws_protocol_tx_8300_const.min_count_bit_for_found);
  231. }
  232. void ws_protocol_decoder_tx_8300_get_string(void* context, FuriString* output) {
  233. furi_assert(context);
  234. WSProtocolDecoderTX_8300* instance = context;
  235. furi_string_printf(
  236. output,
  237. "%s %dbit\r\n"
  238. "Key:0x%lX%08lX\r\n"
  239. "Sn:0x%lX Ch:%d Bat:%d\r\n"
  240. "Temp:%3.1f C Hum:%d%%",
  241. instance->generic.protocol_name,
  242. instance->generic.data_count_bit,
  243. (uint32_t)(instance->generic.data >> 32),
  244. (uint32_t)(instance->generic.data),
  245. instance->generic.id,
  246. instance->generic.channel,
  247. instance->generic.battery_low,
  248. (double)instance->generic.temp,
  249. instance->generic.humidity);
  250. }