auriol_ahfl.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. #include "auriol_ahfl.h"
  2. #define TAG "WSProtocolAuriol_AHFL"
  3. /*
  4. *
  5. Auriol AHFL 433B2 IPX4 sensor.
  6. Data layout:
  7. IIIIIIII-B-X-CC-TTTTTTTTTTTT-HHHHHHH0-FFFF-SSSSSS
  8. Exmpl.: 10111100-1-0-00-000011101000-01101100-0100-001011
  9. - I: id, 8 bit
  10. - B: where B is the battery status: 1=OK, 0=LOW, 1 bit
  11. - X: tx-button, 1 bit (might not work)
  12. - C: CC is the channel: 00=CH1, 01=CH2, 11=CH3, 2bit
  13. - T: temperature, 12 bit: 2's complement, scaled by 10
  14. - H: humidity, 7 bits data, 1 bit 0
  15. - F: always 0x4 (0100)
  16. - S: nibble sum, 6 bits
  17. * The sensor sends 42 bits 5 times,
  18. * the packets are ppm modulated (distance coding) with a pulse of ~500 us
  19. * followed by a short gap of ~1000 us for a 0 bit or a long ~2000 us gap for a
  20. * 1 bit, the sync gap is ~4000 us.
  21. *
  22. */
  23. #define AURIOL_AHFL_CONST_DATA 0b0100
  24. static const SubGhzBlockConst ws_protocol_auriol_ahfl_const = {
  25. .te_short = 500,
  26. .te_long = 2000,
  27. .te_delta = 150,
  28. .min_count_bit_for_found = 42,
  29. };
  30. struct WSProtocolDecoderAuriol_AHFL {
  31. SubGhzProtocolDecoderBase base;
  32. SubGhzBlockDecoder decoder;
  33. WSBlockGeneric generic;
  34. };
  35. struct WSProtocolEncoderAuriol_AHFL {
  36. SubGhzProtocolEncoderBase base;
  37. SubGhzProtocolEncoder encoder;
  38. WSBlockGeneric generic;
  39. };
  40. typedef enum {
  41. auriol_AHFLDecoderStepReset = 0,
  42. auriol_AHFLDecoderStepSaveDuration,
  43. auriol_AHFLDecoderStepCheckDuration,
  44. } auriol_AHFLDecoderStep;
  45. const SubGhzProtocolDecoder ws_protocol_auriol_ahfl_decoder = {
  46. .alloc = ws_protocol_decoder_auriol_ahfl_alloc,
  47. .free = ws_protocol_decoder_auriol_ahfl_free,
  48. .feed = ws_protocol_decoder_auriol_ahfl_feed,
  49. .reset = ws_protocol_decoder_auriol_ahfl_reset,
  50. .get_hash_data = ws_protocol_decoder_auriol_ahfl_get_hash_data,
  51. .serialize = ws_protocol_decoder_auriol_ahfl_serialize,
  52. .deserialize = ws_protocol_decoder_auriol_ahfl_deserialize,
  53. .get_string = ws_protocol_decoder_auriol_ahfl_get_string,
  54. };
  55. const SubGhzProtocolEncoder ws_protocol_auriol_ahfl_encoder = {
  56. .alloc = NULL,
  57. .free = NULL,
  58. .deserialize = NULL,
  59. .stop = NULL,
  60. .yield = NULL,
  61. };
  62. const SubGhzProtocol ws_protocol_auriol_ahfl = {
  63. .name = WS_PROTOCOL_AURIOL_AHFL_NAME,
  64. .type = SubGhzProtocolWeatherStation,
  65. .flag = SubGhzProtocolFlag_433 | SubGhzProtocolFlag_315 | SubGhzProtocolFlag_868 |
  66. SubGhzProtocolFlag_AM | SubGhzProtocolFlag_Decodable,
  67. .decoder = &ws_protocol_auriol_ahfl_decoder,
  68. .encoder = &ws_protocol_auriol_ahfl_encoder,
  69. };
  70. void* ws_protocol_decoder_auriol_ahfl_alloc(SubGhzEnvironment* environment) {
  71. UNUSED(environment);
  72. WSProtocolDecoderAuriol_AHFL* instance = malloc(sizeof(WSProtocolDecoderAuriol_AHFL));
  73. instance->base.protocol = &ws_protocol_auriol_ahfl;
  74. instance->generic.protocol_name = instance->base.protocol->name;
  75. return instance;
  76. }
  77. void ws_protocol_decoder_auriol_ahfl_free(void* context) {
  78. furi_assert(context);
  79. WSProtocolDecoderAuriol_AHFL* instance = context;
  80. free(instance);
  81. }
  82. void ws_protocol_decoder_auriol_ahfl_reset(void* context) {
  83. furi_assert(context);
  84. WSProtocolDecoderAuriol_AHFL* instance = context;
  85. instance->decoder.parser_step = auriol_AHFLDecoderStepReset;
  86. }
  87. static bool ws_protocol_auriol_ahfl_check(WSProtocolDecoderAuriol_AHFL* instance) {
  88. uint8_t type = (instance->decoder.decode_data >> 6) & 0x0F;
  89. if(type != AURIOL_AHFL_CONST_DATA) {
  90. // Fail const data check
  91. return false;
  92. }
  93. uint64_t payload = instance->decoder.decode_data >> 6;
  94. // Checksum is the last 6 bits of data
  95. uint8_t checksum_received = instance->decoder.decode_data & 0x3F;
  96. uint8_t checksum_calculated = 0;
  97. for(uint8_t i = 0; i < 9; i++) {
  98. checksum_calculated += (payload >> (i * 4)) & 0xF;
  99. }
  100. return checksum_received == checksum_calculated;
  101. }
  102. /**
  103. * Analysis of received data
  104. * @param instance Pointer to a WSBlockGeneric* instance
  105. */
  106. static void ws_protocol_auriol_ahfl_remote_controller(WSBlockGeneric* instance) {
  107. instance->id = instance->data >> 34;
  108. instance->battery_low = (instance->data >> 33) & 1;
  109. instance->btn = (instance->data >> 32) & 1;
  110. instance->channel = ((instance->data >> 30) & 0x3) + 1;
  111. if(!((instance->data >> 29) & 1)) {
  112. instance->temp = (float)((instance->data >> 18) & 0x07FF) / 10.0f;
  113. } else {
  114. instance->temp = (float)((~(instance->data >> 18) & 0x07FF) + 1) / -10.0f;
  115. }
  116. instance->humidity = (instance->data >> 11) & 0x7F;
  117. }
  118. void ws_protocol_decoder_auriol_ahfl_feed(void* context, bool level, uint32_t duration) {
  119. furi_assert(context);
  120. WSProtocolDecoderAuriol_AHFL* instance = context;
  121. switch(instance->decoder.parser_step) {
  122. case auriol_AHFLDecoderStepReset:
  123. if((!level) && (DURATION_DIFF(duration, ws_protocol_auriol_ahfl_const.te_short * 18) <
  124. ws_protocol_auriol_ahfl_const.te_delta)) {
  125. //Found syncPrefix
  126. instance->decoder.parser_step = auriol_AHFLDecoderStepSaveDuration;
  127. instance->decoder.decode_data = 0;
  128. instance->decoder.decode_count_bit = 0;
  129. }
  130. break;
  131. case auriol_AHFLDecoderStepSaveDuration:
  132. if(level) {
  133. instance->decoder.te_last = duration;
  134. instance->decoder.parser_step = auriol_AHFLDecoderStepCheckDuration;
  135. } else {
  136. instance->decoder.parser_step = auriol_AHFLDecoderStepReset;
  137. }
  138. break;
  139. case auriol_AHFLDecoderStepCheckDuration:
  140. if(!level) {
  141. if(DURATION_DIFF(instance->decoder.te_last, ws_protocol_auriol_ahfl_const.te_short) <
  142. ws_protocol_auriol_ahfl_const.te_delta) {
  143. if(DURATION_DIFF(duration, ws_protocol_auriol_ahfl_const.te_short * 18) <
  144. ws_protocol_auriol_ahfl_const.te_delta * 8) {
  145. //Found syncPostfix
  146. instance->decoder.parser_step = auriol_AHFLDecoderStepReset;
  147. if((instance->decoder.decode_count_bit ==
  148. ws_protocol_auriol_ahfl_const.min_count_bit_for_found) &&
  149. ws_protocol_auriol_ahfl_check(instance)) {
  150. instance->generic.data = instance->decoder.decode_data;
  151. instance->generic.data_count_bit = instance->decoder.decode_count_bit;
  152. ws_protocol_auriol_ahfl_remote_controller(&instance->generic);
  153. if(instance->base.callback) {
  154. instance->base.callback(&instance->base, instance->base.context);
  155. }
  156. } else if(instance->decoder.decode_count_bit == 1) {
  157. instance->decoder.parser_step = auriol_AHFLDecoderStepSaveDuration;
  158. }
  159. instance->decoder.decode_data = 0;
  160. instance->decoder.decode_count_bit = 0;
  161. } else if(
  162. DURATION_DIFF(duration, ws_protocol_auriol_ahfl_const.te_long) <
  163. ws_protocol_auriol_ahfl_const.te_delta * 2) {
  164. subghz_protocol_blocks_add_bit(&instance->decoder, 0);
  165. instance->decoder.parser_step = auriol_AHFLDecoderStepSaveDuration;
  166. } else if(
  167. DURATION_DIFF(duration, ws_protocol_auriol_ahfl_const.te_long * 2) <
  168. ws_protocol_auriol_ahfl_const.te_delta * 4) {
  169. subghz_protocol_blocks_add_bit(&instance->decoder, 1);
  170. instance->decoder.parser_step = auriol_AHFLDecoderStepSaveDuration;
  171. } else {
  172. instance->decoder.parser_step = auriol_AHFLDecoderStepReset;
  173. }
  174. } else {
  175. instance->decoder.parser_step = auriol_AHFLDecoderStepReset;
  176. }
  177. } else {
  178. instance->decoder.parser_step = auriol_AHFLDecoderStepReset;
  179. }
  180. break;
  181. }
  182. }
  183. uint8_t ws_protocol_decoder_auriol_ahfl_get_hash_data(void* context) {
  184. furi_assert(context);
  185. WSProtocolDecoderAuriol_AHFL* instance = context;
  186. return subghz_protocol_blocks_get_hash_data(
  187. &instance->decoder, (instance->decoder.decode_count_bit / 8) + 1);
  188. }
  189. SubGhzProtocolStatus ws_protocol_decoder_auriol_ahfl_serialize(
  190. void* context,
  191. FlipperFormat* flipper_format,
  192. SubGhzRadioPreset* preset) {
  193. furi_assert(context);
  194. WSProtocolDecoderAuriol_AHFL* instance = context;
  195. return ws_block_generic_serialize(&instance->generic, flipper_format, preset);
  196. }
  197. SubGhzProtocolStatus
  198. ws_protocol_decoder_auriol_ahfl_deserialize(void* context, FlipperFormat* flipper_format) {
  199. furi_assert(context);
  200. WSProtocolDecoderAuriol_AHFL* instance = context;
  201. return ws_block_generic_deserialize_check_count_bit(
  202. &instance->generic, flipper_format, ws_protocol_auriol_ahfl_const.min_count_bit_for_found);
  203. }
  204. void ws_protocol_decoder_auriol_ahfl_get_string(void* context, FuriString* output) {
  205. furi_assert(context);
  206. WSProtocolDecoderAuriol_AHFL* instance = context;
  207. furi_string_printf(
  208. output,
  209. "%s %dbit\r\n"
  210. "Key:0x%lX%08lX\r\n"
  211. "Sn:0x%lX Ch:%d Bat:%d\r\n"
  212. "Temp:%3.1f C Hum:%d%%",
  213. instance->generic.protocol_name,
  214. instance->generic.data_count_bit,
  215. (uint32_t)(instance->generic.data >> 32),
  216. (uint32_t)(instance->generic.data),
  217. instance->generic.id,
  218. instance->generic.channel,
  219. instance->generic.battery_low,
  220. (double)instance->generic.temp,
  221. instance->generic.humidity);
  222. }