lacrosse_tx141thbv2.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  1. #include "lacrosse_tx141thbv2.h"
  2. #define TAG "WSProtocolLaCrosse_TX141THBv2"
  3. /*
  4. * Help
  5. * https://github.com/merbanan/rtl_433/blob/master/src/devices/lacrosse_tx141x.c
  6. *
  7. * iiii iiii | bkcc tttt | tttt tttt | hhhh hhhh | cccc cccc | u
  8. * - i: identification; changes on battery switch
  9. * - c: lfsr_digest8_reflect;
  10. * - u: unknown;
  11. * - b: battery low; flag to indicate low battery voltage
  12. * - h: Humidity;
  13. * - t: Temperature; in °F as binary number with one decimal place + 50 °F offset
  14. * - n: Channel; Channel number 1 - 3
  15. */
  16. static const SubGhzBlockConst ws_protocol_lacrosse_tx141thbv2_const = {
  17. .te_short = 250,
  18. .te_long = 500,
  19. .te_delta = 120,
  20. .min_count_bit_for_found = 41,
  21. };
  22. struct WSProtocolDecoderLaCrosse_TX141THBv2 {
  23. SubGhzProtocolDecoderBase base;
  24. SubGhzBlockDecoder decoder;
  25. WSBlockGeneric generic;
  26. uint16_t header_count;
  27. };
  28. struct WSProtocolEncoderLaCrosse_TX141THBv2 {
  29. SubGhzProtocolEncoderBase base;
  30. SubGhzProtocolBlockEncoder encoder;
  31. WSBlockGeneric generic;
  32. };
  33. typedef enum {
  34. LaCrosse_TX141THBv2DecoderStepReset = 0,
  35. LaCrosse_TX141THBv2DecoderStepCheckPreambule,
  36. LaCrosse_TX141THBv2DecoderStepSaveDuration,
  37. LaCrosse_TX141THBv2DecoderStepCheckDuration,
  38. } LaCrosse_TX141THBv2DecoderStep;
  39. const SubGhzProtocolDecoder ws_protocol_lacrosse_tx141thbv2_decoder = {
  40. .alloc = ws_protocol_decoder_lacrosse_tx141thbv2_alloc,
  41. .free = ws_protocol_decoder_lacrosse_tx141thbv2_free,
  42. .feed = ws_protocol_decoder_lacrosse_tx141thbv2_feed,
  43. .reset = ws_protocol_decoder_lacrosse_tx141thbv2_reset,
  44. .get_hash_data = ws_protocol_decoder_lacrosse_tx141thbv2_get_hash_data,
  45. .serialize = ws_protocol_decoder_lacrosse_tx141thbv2_serialize,
  46. .deserialize = ws_protocol_decoder_lacrosse_tx141thbv2_deserialize,
  47. .get_string = ws_protocol_decoder_lacrosse_tx141thbv2_get_string,
  48. };
  49. const SubGhzProtocolEncoder ws_protocol_lacrosse_tx141thbv2_encoder = {
  50. .alloc = NULL,
  51. .free = NULL,
  52. .deserialize = NULL,
  53. .stop = NULL,
  54. .yield = NULL,
  55. };
  56. const SubGhzProtocol ws_protocol_lacrosse_tx141thbv2 = {
  57. .name = WS_PROTOCOL_LACROSSE_TX141THBV2_NAME,
  58. .type = SubGhzProtocolWeatherStation,
  59. .flag = SubGhzProtocolFlag_433 | SubGhzProtocolFlag_315 | SubGhzProtocolFlag_868 |
  60. SubGhzProtocolFlag_AM | SubGhzProtocolFlag_Decodable,
  61. .decoder = &ws_protocol_lacrosse_tx141thbv2_decoder,
  62. .encoder = &ws_protocol_lacrosse_tx141thbv2_encoder,
  63. };
  64. void* ws_protocol_decoder_lacrosse_tx141thbv2_alloc(SubGhzEnvironment* environment) {
  65. UNUSED(environment);
  66. WSProtocolDecoderLaCrosse_TX141THBv2* instance =
  67. malloc(sizeof(WSProtocolDecoderLaCrosse_TX141THBv2));
  68. instance->base.protocol = &ws_protocol_lacrosse_tx141thbv2;
  69. instance->generic.protocol_name = instance->base.protocol->name;
  70. return instance;
  71. }
  72. void ws_protocol_decoder_lacrosse_tx141thbv2_free(void* context) {
  73. furi_assert(context);
  74. WSProtocolDecoderLaCrosse_TX141THBv2* instance = context;
  75. free(instance);
  76. }
  77. void ws_protocol_decoder_lacrosse_tx141thbv2_reset(void* context) {
  78. furi_assert(context);
  79. WSProtocolDecoderLaCrosse_TX141THBv2* instance = context;
  80. instance->decoder.parser_step = LaCrosse_TX141THBv2DecoderStepReset;
  81. }
  82. static bool
  83. ws_protocol_lacrosse_tx141thbv2_check_crc(WSProtocolDecoderLaCrosse_TX141THBv2* instance) {
  84. if(!instance->decoder.decode_data) return false;
  85. uint8_t msg[] = {
  86. instance->decoder.decode_data >> 33,
  87. instance->decoder.decode_data >> 25,
  88. instance->decoder.decode_data >> 17,
  89. instance->decoder.decode_data >> 9};
  90. uint8_t crc = subghz_protocol_blocks_lfsr_digest8_reflect(msg, 4, 0x31, 0xF4);
  91. return (crc == ((instance->decoder.decode_data >> 1) & 0xFF));
  92. }
  93. /**
  94. * Analysis of received data
  95. * @param instance Pointer to a WSBlockGeneric* instance
  96. */
  97. static void ws_protocol_lacrosse_tx141thbv2_remote_controller(WSBlockGeneric* instance) {
  98. instance->id = instance->data >> 33;
  99. instance->battery_low = (instance->data >> 32) & 1;
  100. instance->btn = (instance->data >> 31) & 1;
  101. instance->channel = ((instance->data >> 29) & 0x03) + 1;
  102. instance->temp = ((float)((instance->data >> 17) & 0x0FFF) - 500.0f) / 10.0f;
  103. instance->humidity = (instance->data >> 9) & 0xFF;
  104. }
  105. void ws_protocol_decoder_lacrosse_tx141thbv2_feed(void* context, bool level, uint32_t duration) {
  106. furi_assert(context);
  107. WSProtocolDecoderLaCrosse_TX141THBv2* instance = context;
  108. switch(instance->decoder.parser_step) {
  109. case LaCrosse_TX141THBv2DecoderStepReset:
  110. if((level) &&
  111. (DURATION_DIFF(duration, ws_protocol_lacrosse_tx141thbv2_const.te_short * 3) <
  112. ws_protocol_lacrosse_tx141thbv2_const.te_delta * 2)) {
  113. instance->decoder.parser_step = LaCrosse_TX141THBv2DecoderStepCheckPreambule;
  114. instance->decoder.te_last = duration;
  115. instance->header_count = 0;
  116. }
  117. break;
  118. case LaCrosse_TX141THBv2DecoderStepCheckPreambule:
  119. if(level) {
  120. instance->decoder.te_last = duration;
  121. } else {
  122. if((DURATION_DIFF(
  123. instance->decoder.te_last,
  124. ws_protocol_lacrosse_tx141thbv2_const.te_short * 3) <
  125. ws_protocol_lacrosse_tx141thbv2_const.te_delta * 2) &&
  126. (DURATION_DIFF(duration, ws_protocol_lacrosse_tx141thbv2_const.te_short * 3) <
  127. ws_protocol_lacrosse_tx141thbv2_const.te_delta * 2)) {
  128. //Found preambule
  129. instance->header_count++;
  130. } else if(instance->header_count == 4) {
  131. if((DURATION_DIFF(
  132. instance->decoder.te_last,
  133. ws_protocol_lacrosse_tx141thbv2_const.te_short) <
  134. ws_protocol_lacrosse_tx141thbv2_const.te_delta) &&
  135. (DURATION_DIFF(duration, ws_protocol_lacrosse_tx141thbv2_const.te_long) <
  136. ws_protocol_lacrosse_tx141thbv2_const.te_delta)) {
  137. instance->decoder.decode_data = 0;
  138. instance->decoder.decode_count_bit = 0;
  139. subghz_protocol_blocks_add_bit(&instance->decoder, 0);
  140. instance->decoder.parser_step = LaCrosse_TX141THBv2DecoderStepSaveDuration;
  141. } else if(
  142. (DURATION_DIFF(
  143. instance->decoder.te_last,
  144. ws_protocol_lacrosse_tx141thbv2_const.te_long) <
  145. ws_protocol_lacrosse_tx141thbv2_const.te_delta) &&
  146. (DURATION_DIFF(duration, ws_protocol_lacrosse_tx141thbv2_const.te_short) <
  147. ws_protocol_lacrosse_tx141thbv2_const.te_delta)) {
  148. instance->decoder.decode_data = 0;
  149. instance->decoder.decode_count_bit = 0;
  150. subghz_protocol_blocks_add_bit(&instance->decoder, 1);
  151. instance->decoder.parser_step = LaCrosse_TX141THBv2DecoderStepSaveDuration;
  152. } else {
  153. instance->decoder.parser_step = LaCrosse_TX141THBv2DecoderStepReset;
  154. }
  155. } else {
  156. instance->decoder.parser_step = LaCrosse_TX141THBv2DecoderStepReset;
  157. }
  158. }
  159. break;
  160. case LaCrosse_TX141THBv2DecoderStepSaveDuration:
  161. if(level) {
  162. instance->decoder.te_last = duration;
  163. instance->decoder.parser_step = LaCrosse_TX141THBv2DecoderStepCheckDuration;
  164. } else {
  165. instance->decoder.parser_step = LaCrosse_TX141THBv2DecoderStepReset;
  166. }
  167. break;
  168. case LaCrosse_TX141THBv2DecoderStepCheckDuration:
  169. if(!level) {
  170. if(((DURATION_DIFF(
  171. instance->decoder.te_last,
  172. ws_protocol_lacrosse_tx141thbv2_const.te_short * 3) <
  173. ws_protocol_lacrosse_tx141thbv2_const.te_delta * 2) &&
  174. (DURATION_DIFF(duration, ws_protocol_lacrosse_tx141thbv2_const.te_short * 3) <
  175. ws_protocol_lacrosse_tx141thbv2_const.te_delta * 2))) {
  176. if((instance->decoder.decode_count_bit ==
  177. ws_protocol_lacrosse_tx141thbv2_const.min_count_bit_for_found) &&
  178. ws_protocol_lacrosse_tx141thbv2_check_crc(instance)) {
  179. instance->generic.data = instance->decoder.decode_data;
  180. instance->generic.data_count_bit = instance->decoder.decode_count_bit;
  181. ws_protocol_lacrosse_tx141thbv2_remote_controller(&instance->generic);
  182. if(instance->base.callback)
  183. instance->base.callback(&instance->base, instance->base.context);
  184. }
  185. instance->decoder.decode_data = 0;
  186. instance->decoder.decode_count_bit = 0;
  187. instance->header_count = 1;
  188. instance->decoder.parser_step = LaCrosse_TX141THBv2DecoderStepCheckPreambule;
  189. break;
  190. } else if(
  191. (DURATION_DIFF(
  192. instance->decoder.te_last, ws_protocol_lacrosse_tx141thbv2_const.te_short) <
  193. ws_protocol_lacrosse_tx141thbv2_const.te_delta) &&
  194. (DURATION_DIFF(duration, ws_protocol_lacrosse_tx141thbv2_const.te_long) <
  195. ws_protocol_lacrosse_tx141thbv2_const.te_delta)) {
  196. subghz_protocol_blocks_add_bit(&instance->decoder, 0);
  197. instance->decoder.parser_step = LaCrosse_TX141THBv2DecoderStepSaveDuration;
  198. } else if(
  199. (DURATION_DIFF(
  200. instance->decoder.te_last, ws_protocol_lacrosse_tx141thbv2_const.te_long) <
  201. ws_protocol_lacrosse_tx141thbv2_const.te_delta) &&
  202. (DURATION_DIFF(duration, ws_protocol_lacrosse_tx141thbv2_const.te_short) <
  203. ws_protocol_lacrosse_tx141thbv2_const.te_delta)) {
  204. subghz_protocol_blocks_add_bit(&instance->decoder, 1);
  205. instance->decoder.parser_step = LaCrosse_TX141THBv2DecoderStepSaveDuration;
  206. } else {
  207. instance->decoder.parser_step = LaCrosse_TX141THBv2DecoderStepReset;
  208. }
  209. } else {
  210. instance->decoder.parser_step = LaCrosse_TX141THBv2DecoderStepReset;
  211. }
  212. break;
  213. }
  214. }
  215. uint8_t ws_protocol_decoder_lacrosse_tx141thbv2_get_hash_data(void* context) {
  216. furi_assert(context);
  217. WSProtocolDecoderLaCrosse_TX141THBv2* instance = context;
  218. return subghz_protocol_blocks_get_hash_data(
  219. &instance->decoder, (instance->decoder.decode_count_bit / 8) + 1);
  220. }
  221. bool ws_protocol_decoder_lacrosse_tx141thbv2_serialize(
  222. void* context,
  223. FlipperFormat* flipper_format,
  224. SubGhzRadioPreset* preset) {
  225. furi_assert(context);
  226. WSProtocolDecoderLaCrosse_TX141THBv2* instance = context;
  227. return ws_block_generic_serialize(&instance->generic, flipper_format, preset);
  228. }
  229. bool ws_protocol_decoder_lacrosse_tx141thbv2_deserialize(
  230. void* context,
  231. FlipperFormat* flipper_format) {
  232. furi_assert(context);
  233. WSProtocolDecoderLaCrosse_TX141THBv2* instance = context;
  234. bool ret = false;
  235. do {
  236. if(!ws_block_generic_deserialize(&instance->generic, flipper_format)) {
  237. break;
  238. }
  239. if(instance->generic.data_count_bit !=
  240. ws_protocol_lacrosse_tx141thbv2_const.min_count_bit_for_found) {
  241. FURI_LOG_E(TAG, "Wrong number of bits in key");
  242. break;
  243. }
  244. ret = true;
  245. } while(false);
  246. return ret;
  247. }
  248. void ws_protocol_decoder_lacrosse_tx141thbv2_get_string(void* context, FuriString* output) {
  249. furi_assert(context);
  250. WSProtocolDecoderLaCrosse_TX141THBv2* instance = context;
  251. furi_string_printf(
  252. output,
  253. "%s %dbit\r\n"
  254. "Key:0x%lX%08lX\r\n"
  255. "Sn:0x%lX Ch:%d Bat:%d\r\n"
  256. "Temp:%3.1f C Hum:%d%%",
  257. instance->generic.protocol_name,
  258. instance->generic.data_count_bit,
  259. (uint32_t)(instance->generic.data >> 32),
  260. (uint32_t)(instance->generic.data),
  261. instance->generic.id,
  262. instance->generic.channel,
  263. instance->generic.battery_low,
  264. (double)instance->generic.temp,
  265. instance->generic.humidity);
  266. }