acurite_606tx.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  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. (DURATION_DIFF(duration, ws_protocol_acurite_606tx_const.te_short) <
  130. ws_protocol_acurite_606tx_const.te_delta)) {
  131. //Found syncPostfix
  132. instance->decoder.parser_step = Acurite_606TXDecoderStepReset;
  133. if((instance->decoder.decode_count_bit ==
  134. ws_protocol_acurite_606tx_const.min_count_bit_for_found) &&
  135. ws_protocol_acurite_606tx_check(instance)) {
  136. instance->generic.data = instance->decoder.decode_data;
  137. instance->generic.data_count_bit = instance->decoder.decode_count_bit;
  138. ws_protocol_acurite_606tx_remote_controller(&instance->generic);
  139. if(instance->base.callback)
  140. instance->base.callback(&instance->base, instance->base.context);
  141. }
  142. instance->decoder.decode_data = 0;
  143. instance->decoder.decode_count_bit = 0;
  144. break;
  145. } else if(
  146. (DURATION_DIFF(
  147. instance->decoder.te_last, ws_protocol_acurite_606tx_const.te_short) <
  148. ws_protocol_acurite_606tx_const.te_delta) &&
  149. (DURATION_DIFF(duration, ws_protocol_acurite_606tx_const.te_long) <
  150. ws_protocol_acurite_606tx_const.te_delta * 2)) {
  151. subghz_protocol_blocks_add_bit(&instance->decoder, 0);
  152. instance->decoder.parser_step = Acurite_606TXDecoderStepSaveDuration;
  153. } else if(
  154. (DURATION_DIFF(
  155. instance->decoder.te_last, ws_protocol_acurite_606tx_const.te_short) <
  156. ws_protocol_acurite_606tx_const.te_delta) &&
  157. (DURATION_DIFF(duration, ws_protocol_acurite_606tx_const.te_long * 2) <
  158. ws_protocol_acurite_606tx_const.te_delta * 4)) {
  159. subghz_protocol_blocks_add_bit(&instance->decoder, 1);
  160. instance->decoder.parser_step = Acurite_606TXDecoderStepSaveDuration;
  161. } else {
  162. instance->decoder.parser_step = Acurite_606TXDecoderStepReset;
  163. }
  164. } else {
  165. instance->decoder.parser_step = Acurite_606TXDecoderStepReset;
  166. }
  167. break;
  168. }
  169. }
  170. uint8_t ws_protocol_decoder_acurite_606tx_get_hash_data(void* context) {
  171. furi_assert(context);
  172. WSProtocolDecoderAcurite_606TX* instance = context;
  173. return subghz_protocol_blocks_get_hash_data(
  174. &instance->decoder, (instance->decoder.decode_count_bit / 8) + 1);
  175. }
  176. bool ws_protocol_decoder_acurite_606tx_serialize(
  177. void* context,
  178. FlipperFormat* flipper_format,
  179. SubGhzRadioPreset* preset) {
  180. furi_assert(context);
  181. WSProtocolDecoderAcurite_606TX* instance = context;
  182. return ws_block_generic_serialize(&instance->generic, flipper_format, preset);
  183. }
  184. bool ws_protocol_decoder_acurite_606tx_deserialize(void* context, FlipperFormat* flipper_format) {
  185. furi_assert(context);
  186. WSProtocolDecoderAcurite_606TX* instance = context;
  187. bool ret = false;
  188. do {
  189. if(!ws_block_generic_deserialize(&instance->generic, flipper_format)) {
  190. break;
  191. }
  192. if(instance->generic.data_count_bit !=
  193. ws_protocol_acurite_606tx_const.min_count_bit_for_found) {
  194. FURI_LOG_E(TAG, "Wrong number of bits in key");
  195. break;
  196. }
  197. ret = true;
  198. } while(false);
  199. return ret;
  200. }
  201. void ws_protocol_decoder_acurite_606tx_get_string(void* context, FuriString* output) {
  202. furi_assert(context);
  203. WSProtocolDecoderAcurite_606TX* instance = context;
  204. furi_string_printf(
  205. output,
  206. "%s %dbit\r\n"
  207. "Key:0x%lX%08lX\r\n"
  208. "Sn:0x%lX Ch:%d Bat:%d\r\n"
  209. "Temp:%d.%d C Hum:%d%%",
  210. instance->generic.protocol_name,
  211. instance->generic.data_count_bit,
  212. (uint32_t)(instance->generic.data >> 32),
  213. (uint32_t)(instance->generic.data),
  214. instance->generic.id,
  215. instance->generic.channel,
  216. instance->generic.battery_low,
  217. (int16_t)instance->generic.temp,
  218. abs(((int16_t)(instance->generic.temp * 10) - (((int16_t)instance->generic.temp) * 10))),
  219. instance->generic.humidity);
  220. }