acurite_606tx.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. #include "acurite_606tx.h"
  2. #define TAG "WSProtocolAcurite_606TX"
  3. /*
  4. * Help
  5. * https://github.com/merbanan/rtl_433/blob/5bef4e43133ac4c0e2d18d36f87c52b4f9458453/src/devices/acurite.c#L1644
  6. *
  7. * 0000 1111 | 0011 0000 | 0101 1100 | 1110 0111
  8. * iiii iiii | buuu tttt | tttt tttt | cccc cccc
  9. * - i: identification; changes on battery switch
  10. * - c: lfsr_digest8;
  11. * - u: unknown;
  12. * - b: battery low; flag to indicate low battery voltage
  13. * - t: Temperature; in °C
  14. *
  15. */
  16. static const SubGhzBlockConst ws_protocol_acurite_606tx_const = {
  17. .te_short = 500,
  18. .te_long = 2000,
  19. .te_delta = 150,
  20. .min_count_bit_for_found = 32,
  21. };
  22. struct WSProtocolDecoderAcurite_606TX {
  23. SubGhzProtocolDecoderBase base;
  24. SubGhzBlockDecoder decoder;
  25. WSBlockGeneric generic;
  26. };
  27. struct WSProtocolEncoderAcurite_606TX {
  28. SubGhzProtocolEncoderBase base;
  29. SubGhzProtocolBlockEncoder encoder;
  30. WSBlockGeneric generic;
  31. };
  32. typedef enum {
  33. Acurite_606TXDecoderStepReset = 0,
  34. Acurite_606TXDecoderStepSaveDuration,
  35. Acurite_606TXDecoderStepCheckDuration,
  36. } Acurite_606TXDecoderStep;
  37. const SubGhzProtocolDecoder ws_protocol_acurite_606tx_decoder = {
  38. .alloc = ws_protocol_decoder_acurite_606tx_alloc,
  39. .free = ws_protocol_decoder_acurite_606tx_free,
  40. .feed = ws_protocol_decoder_acurite_606tx_feed,
  41. .reset = ws_protocol_decoder_acurite_606tx_reset,
  42. .get_hash_data = ws_protocol_decoder_acurite_606tx_get_hash_data,
  43. .serialize = ws_protocol_decoder_acurite_606tx_serialize,
  44. .deserialize = ws_protocol_decoder_acurite_606tx_deserialize,
  45. .get_string = ws_protocol_decoder_acurite_606tx_get_string,
  46. };
  47. const SubGhzProtocolEncoder ws_protocol_acurite_606tx_encoder = {
  48. .alloc = NULL,
  49. .free = NULL,
  50. .deserialize = NULL,
  51. .stop = NULL,
  52. .yield = NULL,
  53. };
  54. const SubGhzProtocol ws_protocol_acurite_606tx = {
  55. .name = WS_PROTOCOL_ACURITE_606TX_NAME,
  56. .type = SubGhzProtocolWeatherStation,
  57. .flag = SubGhzProtocolFlag_433 | SubGhzProtocolFlag_315 | SubGhzProtocolFlag_868 |
  58. SubGhzProtocolFlag_AM | SubGhzProtocolFlag_Decodable,
  59. .decoder = &ws_protocol_acurite_606tx_decoder,
  60. .encoder = &ws_protocol_acurite_606tx_encoder,
  61. };
  62. void* ws_protocol_decoder_acurite_606tx_alloc(SubGhzEnvironment* environment) {
  63. UNUSED(environment);
  64. WSProtocolDecoderAcurite_606TX* instance = malloc(sizeof(WSProtocolDecoderAcurite_606TX));
  65. instance->base.protocol = &ws_protocol_acurite_606tx;
  66. instance->generic.protocol_name = instance->base.protocol->name;
  67. return instance;
  68. }
  69. void ws_protocol_decoder_acurite_606tx_free(void* context) {
  70. furi_assert(context);
  71. WSProtocolDecoderAcurite_606TX* instance = context;
  72. free(instance);
  73. }
  74. void ws_protocol_decoder_acurite_606tx_reset(void* context) {
  75. furi_assert(context);
  76. WSProtocolDecoderAcurite_606TX* instance = context;
  77. instance->decoder.parser_step = Acurite_606TXDecoderStepReset;
  78. }
  79. static bool ws_protocol_acurite_606tx_check(WSProtocolDecoderAcurite_606TX* instance) {
  80. if(!instance->decoder.decode_data) return false;
  81. uint8_t msg[] = {
  82. instance->decoder.decode_data >> 24,
  83. instance->decoder.decode_data >> 16,
  84. instance->decoder.decode_data >> 8};
  85. uint8_t crc = subghz_protocol_blocks_lfsr_digest8(msg, 3, 0x98, 0xF1);
  86. return (crc == (instance->decoder.decode_data & 0xFF));
  87. }
  88. /**
  89. * Analysis of received data
  90. * @param instance Pointer to a WSBlockGeneric* instance
  91. */
  92. static void ws_protocol_acurite_606tx_remote_controller(WSBlockGeneric* instance) {
  93. instance->id = (instance->data >> 24) & 0xFF;
  94. instance->battery_low = (instance->data >> 23) & 1;
  95. instance->channel = WS_NO_CHANNEL;
  96. if(!((instance->data >> 19) & 1)) {
  97. instance->temp = (float)((instance->data >> 8) & 0x07FF) / 10.0f;
  98. } else {
  99. instance->temp = (float)((~(instance->data >> 8) & 0x07FF) + 1) / -10.0f;
  100. }
  101. instance->btn = WS_NO_BTN;
  102. instance->humidity = WS_NO_HUMIDITY;
  103. }
  104. void ws_protocol_decoder_acurite_606tx_feed(void* context, bool level, uint32_t duration) {
  105. furi_assert(context);
  106. WSProtocolDecoderAcurite_606TX* instance = context;
  107. switch(instance->decoder.parser_step) {
  108. case Acurite_606TXDecoderStepReset:
  109. if((!level) && (DURATION_DIFF(duration, ws_protocol_acurite_606tx_const.te_short * 17) <
  110. ws_protocol_acurite_606tx_const.te_delta * 8)) {
  111. //Found syncPrefix
  112. instance->decoder.parser_step = Acurite_606TXDecoderStepSaveDuration;
  113. instance->decoder.decode_data = 0;
  114. instance->decoder.decode_count_bit = 0;
  115. }
  116. break;
  117. case Acurite_606TXDecoderStepSaveDuration:
  118. if(level) {
  119. instance->decoder.te_last = duration;
  120. instance->decoder.parser_step = Acurite_606TXDecoderStepCheckDuration;
  121. } else {
  122. instance->decoder.parser_step = Acurite_606TXDecoderStepReset;
  123. }
  124. break;
  125. case Acurite_606TXDecoderStepCheckDuration:
  126. if(!level) {
  127. if(DURATION_DIFF(instance->decoder.te_last, ws_protocol_acurite_606tx_const.te_short) <
  128. ws_protocol_acurite_606tx_const.te_delta) {
  129. if((DURATION_DIFF(duration, ws_protocol_acurite_606tx_const.te_short) <
  130. ws_protocol_acurite_606tx_const.te_delta) ||
  131. (duration > ws_protocol_acurite_606tx_const.te_long * 3)) {
  132. //Found syncPostfix
  133. instance->decoder.parser_step = Acurite_606TXDecoderStepReset;
  134. if((instance->decoder.decode_count_bit ==
  135. ws_protocol_acurite_606tx_const.min_count_bit_for_found) &&
  136. ws_protocol_acurite_606tx_check(instance)) {
  137. instance->generic.data = instance->decoder.decode_data;
  138. instance->generic.data_count_bit = instance->decoder.decode_count_bit;
  139. ws_protocol_acurite_606tx_remote_controller(&instance->generic);
  140. if(instance->base.callback)
  141. instance->base.callback(&instance->base, instance->base.context);
  142. }
  143. instance->decoder.decode_data = 0;
  144. instance->decoder.decode_count_bit = 0;
  145. } else if(
  146. DURATION_DIFF(duration, ws_protocol_acurite_606tx_const.te_long) <
  147. ws_protocol_acurite_606tx_const.te_delta * 2) {
  148. subghz_protocol_blocks_add_bit(&instance->decoder, 0);
  149. instance->decoder.parser_step = Acurite_606TXDecoderStepSaveDuration;
  150. } else if(
  151. DURATION_DIFF(duration, ws_protocol_acurite_606tx_const.te_long * 2) <
  152. ws_protocol_acurite_606tx_const.te_delta * 4) {
  153. subghz_protocol_blocks_add_bit(&instance->decoder, 1);
  154. instance->decoder.parser_step = Acurite_606TXDecoderStepSaveDuration;
  155. } else {
  156. instance->decoder.parser_step = Acurite_606TXDecoderStepReset;
  157. }
  158. } else {
  159. instance->decoder.parser_step = Acurite_606TXDecoderStepReset;
  160. }
  161. } else {
  162. instance->decoder.parser_step = Acurite_606TXDecoderStepReset;
  163. }
  164. break;
  165. }
  166. }
  167. uint8_t ws_protocol_decoder_acurite_606tx_get_hash_data(void* context) {
  168. furi_assert(context);
  169. WSProtocolDecoderAcurite_606TX* instance = context;
  170. return subghz_protocol_blocks_get_hash_data(
  171. &instance->decoder, (instance->decoder.decode_count_bit / 8) + 1);
  172. }
  173. SubGhzProtocolStatus ws_protocol_decoder_acurite_606tx_serialize(
  174. void* context,
  175. FlipperFormat* flipper_format,
  176. SubGhzRadioPreset* preset) {
  177. furi_assert(context);
  178. WSProtocolDecoderAcurite_606TX* instance = context;
  179. return ws_block_generic_serialize(&instance->generic, flipper_format, preset);
  180. }
  181. SubGhzProtocolStatus
  182. ws_protocol_decoder_acurite_606tx_deserialize(void* context, FlipperFormat* flipper_format) {
  183. furi_assert(context);
  184. WSProtocolDecoderAcurite_606TX* instance = context;
  185. return ws_block_generic_deserialize_check_count_bit(
  186. &instance->generic,
  187. flipper_format,
  188. ws_protocol_acurite_606tx_const.min_count_bit_for_found);
  189. }
  190. void ws_protocol_decoder_acurite_606tx_get_string(void* context, FuriString* output) {
  191. furi_assert(context);
  192. WSProtocolDecoderAcurite_606TX* instance = context;
  193. furi_string_printf(
  194. output,
  195. "%s %dbit\r\n"
  196. "Key:0x%lX%08lX\r\n"
  197. "Sn:0x%lX Ch:%d Bat:%d\r\n"
  198. "Temp:%3.1f C Hum:%d%%",
  199. instance->generic.protocol_name,
  200. instance->generic.data_count_bit,
  201. (uint32_t)(instance->generic.data >> 32),
  202. (uint32_t)(instance->generic.data),
  203. instance->generic.id,
  204. instance->generic.channel,
  205. instance->generic.battery_low,
  206. (double)instance->generic.temp,
  207. instance->generic.humidity);
  208. }