lacrosse_tx.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  1. #include "lacrosse_tx.h"
  2. #define TAG "WSProtocolLaCrosse_TX"
  3. /*
  4. * Help
  5. * https://github.com/merbanan/rtl_433/blob/master/src/devices/lacrosse.c
  6. *
  7. *
  8. * LaCrosse TX 433 Mhz Temperature and Humidity Sensors.
  9. * - Tested: TX-7U and TX-6U (Temperature only)
  10. * - Not Tested but should work: TX-3, TX-4
  11. * - also TFA Dostmann 30.3120.90 sensor (for e.g. 35.1018.06 (WS-9015) station)
  12. * - also TFA Dostmann 30.3121 sensor
  13. * Protocol Documentation: http://www.f6fbb.org/domo/sensors/tx3_th.php
  14. * Message is 44 bits, 11 x 4 bit nybbles:
  15. * [00] [cnt = 10] [type] [addr] [addr + parity] [v1] [v2] [v3] [iv1] [iv2] [check]
  16. * Notes:
  17. * - Zero Pulses are longer (1,400 uS High, 1,000 uS Low) = 2,400 uS
  18. * - One Pulses are shorter ( 550 uS High, 1,000 uS Low) = 1,600 uS
  19. * - Sensor id changes when the battery is changed
  20. * - Primary Value are BCD with one decimal place: vvv = 12.3
  21. * - Secondary value is integer only intval = 12, seems to be a repeat of primary
  22. * This may actually be an additional data check because the 4 bit checksum
  23. * and parity bit is pretty week at detecting errors.
  24. * - Temperature is in Celsius with 50.0 added (to handle negative values)
  25. * - Humidity values appear to be integer precision, decimal always 0.
  26. * - There is a 4 bit checksum and a parity bit covering the three digit value
  27. * - Parity check for TX-3 and TX-4 might be different.
  28. * - Msg sent with one repeat after 30 mS
  29. * - Temperature and humidity are sent as separate messages
  30. * - Frequency for each sensor may be could be off by as much as 50-75 khz
  31. * - LaCrosse Sensors in other frequency ranges (915 Mhz) use FSK not OOK
  32. * so they can't be decoded by rtl_433 currently.
  33. * - Temperature and Humidity are sent in different messages bursts.
  34. */
  35. #define LACROSSE_TX_GAP 1000
  36. #define LACROSSE_TX_BIT_SIZE 44
  37. #define LACROSSE_TX_SUNC_PATTERN 0x0A000000000
  38. #define LACROSSE_TX_SUNC_MASK 0x0F000000000
  39. #define LACROSSE_TX_MSG_TYPE_TEMP 0x00
  40. #define LACROSSE_TX_MSG_TYPE_HUM 0x0E
  41. static const SubGhzBlockConst ws_protocol_lacrosse_tx_const = {
  42. .te_short = 550,
  43. .te_long = 1300,
  44. .te_delta = 120,
  45. .min_count_bit_for_found = 40,
  46. };
  47. struct WSProtocolDecoderLaCrosse_TX {
  48. SubGhzProtocolDecoderBase base;
  49. SubGhzBlockDecoder decoder;
  50. WSBlockGeneric generic;
  51. uint16_t header_count;
  52. };
  53. struct WSProtocolEncoderLaCrosse_TX {
  54. SubGhzProtocolEncoderBase base;
  55. SubGhzProtocolBlockEncoder encoder;
  56. WSBlockGeneric generic;
  57. };
  58. typedef enum {
  59. LaCrosse_TXDecoderStepReset = 0,
  60. LaCrosse_TXDecoderStepCheckPreambule,
  61. LaCrosse_TXDecoderStepSaveDuration,
  62. LaCrosse_TXDecoderStepCheckDuration,
  63. } LaCrosse_TXDecoderStep;
  64. const SubGhzProtocolDecoder ws_protocol_lacrosse_tx_decoder = {
  65. .alloc = ws_protocol_decoder_lacrosse_tx_alloc,
  66. .free = ws_protocol_decoder_lacrosse_tx_free,
  67. .feed = ws_protocol_decoder_lacrosse_tx_feed,
  68. .reset = ws_protocol_decoder_lacrosse_tx_reset,
  69. .get_hash_data = ws_protocol_decoder_lacrosse_tx_get_hash_data,
  70. .serialize = ws_protocol_decoder_lacrosse_tx_serialize,
  71. .deserialize = ws_protocol_decoder_lacrosse_tx_deserialize,
  72. .get_string = ws_protocol_decoder_lacrosse_tx_get_string,
  73. };
  74. const SubGhzProtocolEncoder ws_protocol_lacrosse_tx_encoder = {
  75. .alloc = NULL,
  76. .free = NULL,
  77. .deserialize = NULL,
  78. .stop = NULL,
  79. .yield = NULL,
  80. };
  81. const SubGhzProtocol ws_protocol_lacrosse_tx = {
  82. .name = WS_PROTOCOL_LACROSSE_TX_NAME,
  83. .type = SubGhzProtocolWeatherStation,
  84. .flag = SubGhzProtocolFlag_433 | SubGhzProtocolFlag_315 | SubGhzProtocolFlag_868 |
  85. SubGhzProtocolFlag_AM | SubGhzProtocolFlag_Decodable,
  86. .decoder = &ws_protocol_lacrosse_tx_decoder,
  87. .encoder = &ws_protocol_lacrosse_tx_encoder,
  88. };
  89. void* ws_protocol_decoder_lacrosse_tx_alloc(SubGhzEnvironment* environment) {
  90. UNUSED(environment);
  91. WSProtocolDecoderLaCrosse_TX* instance = malloc(sizeof(WSProtocolDecoderLaCrosse_TX));
  92. instance->base.protocol = &ws_protocol_lacrosse_tx;
  93. instance->generic.protocol_name = instance->base.protocol->name;
  94. return instance;
  95. }
  96. void ws_protocol_decoder_lacrosse_tx_free(void* context) {
  97. furi_assert(context);
  98. WSProtocolDecoderLaCrosse_TX* instance = context;
  99. free(instance);
  100. }
  101. void ws_protocol_decoder_lacrosse_tx_reset(void* context) {
  102. furi_assert(context);
  103. WSProtocolDecoderLaCrosse_TX* instance = context;
  104. instance->header_count = 0;
  105. instance->decoder.parser_step = LaCrosse_TXDecoderStepReset;
  106. }
  107. static bool ws_protocol_lacrosse_tx_check_crc(WSProtocolDecoderLaCrosse_TX* instance) {
  108. if(!instance->decoder.decode_data) return false;
  109. uint8_t msg[] = {
  110. (instance->decoder.decode_data >> 36) & 0x0F,
  111. (instance->decoder.decode_data >> 32) & 0x0F,
  112. (instance->decoder.decode_data >> 28) & 0x0F,
  113. (instance->decoder.decode_data >> 24) & 0x0F,
  114. (instance->decoder.decode_data >> 20) & 0x0F,
  115. (instance->decoder.decode_data >> 16) & 0x0F,
  116. (instance->decoder.decode_data >> 12) & 0x0F,
  117. (instance->decoder.decode_data >> 8) & 0x0F,
  118. (instance->decoder.decode_data >> 4) & 0x0F};
  119. uint8_t crc = subghz_protocol_blocks_add_bytes(msg, 9);
  120. return ((crc & 0x0F) == ((instance->decoder.decode_data) & 0x0F));
  121. }
  122. /**
  123. * Analysis of received data
  124. * @param instance Pointer to a WSBlockGeneric* instance
  125. */
  126. static void ws_protocol_lacrosse_tx_remote_controller(WSBlockGeneric* instance) {
  127. uint8_t msg_type = (instance->data >> 32) & 0x0F;
  128. instance->id = (((instance->data >> 28) & 0x0F) << 3) | (((instance->data >> 24) & 0x0F) >> 1);
  129. float msg_value = (float)((instance->data >> 20) & 0x0F) * 10.0f +
  130. (float)((instance->data >> 16) & 0x0F) +
  131. (float)((instance->data >> 12) & 0x0F) * 0.1f;
  132. if(msg_type == LACROSSE_TX_MSG_TYPE_TEMP) { //-V1051
  133. instance->temp = msg_value - 50.0f;
  134. instance->humidity = WS_NO_HUMIDITY;
  135. } else if(msg_type == LACROSSE_TX_MSG_TYPE_HUM) {
  136. //ToDo for verification, records are needed with sensors maintaining temperature and temperature for this standard
  137. instance->humidity = (uint8_t)msg_value;
  138. } else {
  139. furi_crash("WS: WSProtocolLaCrosse_TX incorrect msg_type.");
  140. }
  141. instance->btn = WS_NO_BTN;
  142. instance->battery_low = WS_NO_BATT;
  143. instance->channel = WS_NO_CHANNEL;
  144. }
  145. void ws_protocol_decoder_lacrosse_tx_feed(void* context, bool level, uint32_t duration) {
  146. furi_assert(context);
  147. WSProtocolDecoderLaCrosse_TX* instance = context;
  148. switch(instance->decoder.parser_step) {
  149. case LaCrosse_TXDecoderStepReset:
  150. if((!level) && (DURATION_DIFF(duration, LACROSSE_TX_GAP) <
  151. ws_protocol_lacrosse_tx_const.te_delta * 2)) {
  152. instance->decoder.parser_step = LaCrosse_TXDecoderStepCheckPreambule;
  153. instance->header_count = 0;
  154. }
  155. break;
  156. case LaCrosse_TXDecoderStepCheckPreambule:
  157. if(level) {
  158. if((DURATION_DIFF(duration, ws_protocol_lacrosse_tx_const.te_short) <
  159. ws_protocol_lacrosse_tx_const.te_delta) &&
  160. (instance->header_count > 1)) {
  161. instance->decoder.parser_step = LaCrosse_TXDecoderStepCheckDuration;
  162. instance->decoder.decode_data = 0;
  163. instance->decoder.decode_count_bit = 0;
  164. instance->decoder.te_last = duration;
  165. } else if(duration > (ws_protocol_lacrosse_tx_const.te_long * 2)) {
  166. instance->decoder.parser_step = LaCrosse_TXDecoderStepReset;
  167. }
  168. } else {
  169. if(DURATION_DIFF(duration, LACROSSE_TX_GAP) <
  170. ws_protocol_lacrosse_tx_const.te_delta * 2) {
  171. instance->decoder.te_last = duration;
  172. instance->header_count++;
  173. } else {
  174. instance->decoder.parser_step = LaCrosse_TXDecoderStepReset;
  175. }
  176. }
  177. break;
  178. case LaCrosse_TXDecoderStepSaveDuration:
  179. if(level) {
  180. instance->decoder.te_last = duration;
  181. instance->decoder.parser_step = LaCrosse_TXDecoderStepCheckDuration;
  182. } else {
  183. instance->decoder.parser_step = LaCrosse_TXDecoderStepReset;
  184. }
  185. break;
  186. case LaCrosse_TXDecoderStepCheckDuration:
  187. if(!level) {
  188. if(duration > LACROSSE_TX_GAP * 3) {
  189. if(DURATION_DIFF(
  190. instance->decoder.te_last, ws_protocol_lacrosse_tx_const.te_short) <
  191. ws_protocol_lacrosse_tx_const.te_delta) {
  192. subghz_protocol_blocks_add_bit(&instance->decoder, 1);
  193. instance->decoder.parser_step = LaCrosse_TXDecoderStepSaveDuration;
  194. } else if(
  195. DURATION_DIFF(
  196. instance->decoder.te_last, ws_protocol_lacrosse_tx_const.te_long) <
  197. ws_protocol_lacrosse_tx_const.te_delta) {
  198. subghz_protocol_blocks_add_bit(&instance->decoder, 0);
  199. instance->decoder.parser_step = LaCrosse_TXDecoderStepSaveDuration;
  200. }
  201. if((instance->decoder.decode_data & LACROSSE_TX_SUNC_MASK) ==
  202. LACROSSE_TX_SUNC_PATTERN) {
  203. if(ws_protocol_lacrosse_tx_check_crc(instance)) {
  204. instance->generic.data = instance->decoder.decode_data;
  205. instance->generic.data_count_bit = LACROSSE_TX_BIT_SIZE;
  206. ws_protocol_lacrosse_tx_remote_controller(&instance->generic);
  207. if(instance->base.callback)
  208. instance->base.callback(&instance->base, instance->base.context);
  209. }
  210. }
  211. instance->decoder.decode_data = 0;
  212. instance->decoder.decode_count_bit = 0;
  213. instance->header_count = 0;
  214. instance->decoder.parser_step = LaCrosse_TXDecoderStepReset;
  215. break;
  216. } else if(
  217. (DURATION_DIFF(instance->decoder.te_last, ws_protocol_lacrosse_tx_const.te_short) <
  218. ws_protocol_lacrosse_tx_const.te_delta) &&
  219. (DURATION_DIFF(duration, LACROSSE_TX_GAP) <
  220. ws_protocol_lacrosse_tx_const.te_delta * 2)) {
  221. subghz_protocol_blocks_add_bit(&instance->decoder, 1);
  222. instance->decoder.parser_step = LaCrosse_TXDecoderStepSaveDuration;
  223. } else if(
  224. (DURATION_DIFF(instance->decoder.te_last, ws_protocol_lacrosse_tx_const.te_long) <
  225. ws_protocol_lacrosse_tx_const.te_delta) &&
  226. (DURATION_DIFF(duration, LACROSSE_TX_GAP) <
  227. ws_protocol_lacrosse_tx_const.te_delta * 2)) {
  228. subghz_protocol_blocks_add_bit(&instance->decoder, 0);
  229. instance->decoder.parser_step = LaCrosse_TXDecoderStepSaveDuration;
  230. } else {
  231. instance->decoder.parser_step = LaCrosse_TXDecoderStepReset;
  232. }
  233. } else {
  234. instance->decoder.parser_step = LaCrosse_TXDecoderStepReset;
  235. }
  236. break;
  237. }
  238. }
  239. uint8_t ws_protocol_decoder_lacrosse_tx_get_hash_data(void* context) {
  240. furi_assert(context);
  241. WSProtocolDecoderLaCrosse_TX* instance = context;
  242. return subghz_protocol_blocks_get_hash_data(
  243. &instance->decoder, (instance->decoder.decode_count_bit / 8) + 1);
  244. }
  245. SubGhzProtocolStatus ws_protocol_decoder_lacrosse_tx_serialize(
  246. void* context,
  247. FlipperFormat* flipper_format,
  248. SubGhzRadioPreset* preset) {
  249. furi_assert(context);
  250. WSProtocolDecoderLaCrosse_TX* instance = context;
  251. return ws_block_generic_serialize(&instance->generic, flipper_format, preset);
  252. }
  253. SubGhzProtocolStatus
  254. ws_protocol_decoder_lacrosse_tx_deserialize(void* context, FlipperFormat* flipper_format) {
  255. furi_assert(context);
  256. WSProtocolDecoderLaCrosse_TX* instance = context;
  257. return ws_block_generic_deserialize_check_count_bit(
  258. &instance->generic, flipper_format, ws_protocol_lacrosse_tx_const.min_count_bit_for_found);
  259. }
  260. void ws_protocol_decoder_lacrosse_tx_get_string(void* context, FuriString* output) {
  261. furi_assert(context);
  262. WSProtocolDecoderLaCrosse_TX* instance = context;
  263. furi_string_printf(
  264. output,
  265. "%s %dbit\r\n"
  266. "Key:0x%lX%08lX\r\n"
  267. "Sn:0x%lX Ch:%d Bat:%d\r\n"
  268. "Temp:%3.1f C Hum:%d%%",
  269. instance->generic.protocol_name,
  270. instance->generic.data_count_bit,
  271. (uint32_t)(instance->generic.data >> 32),
  272. (uint32_t)(instance->generic.data),
  273. instance->generic.id,
  274. instance->generic.channel,
  275. instance->generic.battery_low,
  276. (double)instance->generic.temp,
  277. instance->generic.humidity);
  278. }