infactory.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  1. #include "infactory.h"
  2. #define TAG "WSProtocolInfactory"
  3. /*
  4. * Help
  5. * https://github.com/merbanan/rtl_433/blob/master/src/devices/infactory.c
  6. *
  7. * Analysis using Genuino (see http://gitlab.com/hp-uno, e.g. uno_log_433):
  8. * Observed On-Off-Key (OOK) data pattern:
  9. * preamble syncPrefix data...(40 bit) syncPostfix
  10. * HHLL HHLL HHLL HHLL HLLLLLLLLLLLLLLLL (HLLLL HLLLLLLLL HLLLL HLLLLLLLL ....) HLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLL
  11. * Breakdown:
  12. * - four preamble pairs '1'/'0' each with a length of ca. 1000us
  13. * - syncPre, syncPost, data0, data1 have a '1' start pulse of ca. 500us
  14. * - syncPre pulse before dataPtr has a '0' pulse length of ca. 8000us
  15. * - data0 (0-bits) have then a '0' pulse length of ca. 2000us
  16. * - data1 (1-bits) have then a '0' pulse length of ca. 4000us
  17. * - syncPost after dataPtr has a '0' pulse length of ca. 16000us
  18. * This analysis is the reason for the new r_device definitions below.
  19. * NB: pulse_slicer_ppm does not use .gap_limit if .tolerance is set.
  20. *
  21. * Outdoor sensor, transmits temperature and humidity data
  22. * - inFactory NC-3982-913/NX-5817-902, Pearl (for FWS-686 station)
  23. * - nor-tec 73383 (weather station + sensor), Schou Company AS, Denmark
  24. * - DAY 73365 (weather station + sensor), Schou Company AS, Denmark
  25. * Known brand names: inFactory, nor-tec, GreenBlue, DAY. Manufacturer in China.
  26. * Transmissions includes an id. Every 60 seconds the sensor transmits 6 packets:
  27. * 0000 1111 | 0011 0000 | 0101 1100 | 1110 0111 | 0110 0001
  28. * iiii iiii | cccc ub?? | tttt tttt | tttt hhhh | hhhh ??nn
  29. * - i: identification; changes on battery switch
  30. * - c: CRC-4; CCITT checksum, see below for computation specifics
  31. * - u: unknown; (sometimes set at power-on, but not always)
  32. * - b: battery low; flag to indicate low battery voltage
  33. * - h: Humidity; BCD-encoded, each nibble is one digit, 'A0' means 100%rH
  34. * - t: Temperature; in °F as binary number with one decimal place + 90 °F offset
  35. * - n: Channel; Channel number 1 - 3
  36. *
  37. */
  38. static const SubGhzBlockConst ws_protocol_infactory_const = {
  39. .te_short = 500,
  40. .te_long = 2000,
  41. .te_delta = 150,
  42. .min_count_bit_for_found = 40,
  43. };
  44. struct WSProtocolDecoderInfactory {
  45. SubGhzProtocolDecoderBase base;
  46. SubGhzBlockDecoder decoder;
  47. WSBlockGeneric generic;
  48. uint16_t header_count;
  49. };
  50. struct WSProtocolEncoderInfactory {
  51. SubGhzProtocolEncoderBase base;
  52. SubGhzProtocolBlockEncoder encoder;
  53. WSBlockGeneric generic;
  54. };
  55. typedef enum {
  56. InfactoryDecoderStepReset = 0,
  57. InfactoryDecoderStepCheckPreambule,
  58. InfactoryDecoderStepSaveDuration,
  59. InfactoryDecoderStepCheckDuration,
  60. } InfactoryDecoderStep;
  61. const SubGhzProtocolDecoder ws_protocol_infactory_decoder = {
  62. .alloc = ws_protocol_decoder_infactory_alloc,
  63. .free = ws_protocol_decoder_infactory_free,
  64. .feed = ws_protocol_decoder_infactory_feed,
  65. .reset = ws_protocol_decoder_infactory_reset,
  66. .get_hash_data = ws_protocol_decoder_infactory_get_hash_data,
  67. .serialize = ws_protocol_decoder_infactory_serialize,
  68. .deserialize = ws_protocol_decoder_infactory_deserialize,
  69. .get_string = ws_protocol_decoder_infactory_get_string,
  70. };
  71. const SubGhzProtocolEncoder ws_protocol_infactory_encoder = {
  72. .alloc = NULL,
  73. .free = NULL,
  74. .deserialize = NULL,
  75. .stop = NULL,
  76. .yield = NULL,
  77. };
  78. const SubGhzProtocol ws_protocol_infactory = {
  79. .name = WS_PROTOCOL_INFACTORY_NAME,
  80. .type = SubGhzProtocolWeatherStation,
  81. .flag = SubGhzProtocolFlag_433 | SubGhzProtocolFlag_315 | SubGhzProtocolFlag_868 |
  82. SubGhzProtocolFlag_AM | SubGhzProtocolFlag_Decodable,
  83. .decoder = &ws_protocol_infactory_decoder,
  84. .encoder = &ws_protocol_infactory_encoder,
  85. };
  86. void* ws_protocol_decoder_infactory_alloc(SubGhzEnvironment* environment) {
  87. UNUSED(environment);
  88. WSProtocolDecoderInfactory* instance = malloc(sizeof(WSProtocolDecoderInfactory));
  89. instance->base.protocol = &ws_protocol_infactory;
  90. instance->generic.protocol_name = instance->base.protocol->name;
  91. return instance;
  92. }
  93. void ws_protocol_decoder_infactory_free(void* context) {
  94. furi_assert(context);
  95. WSProtocolDecoderInfactory* instance = context;
  96. free(instance);
  97. }
  98. void ws_protocol_decoder_infactory_reset(void* context) {
  99. furi_assert(context);
  100. WSProtocolDecoderInfactory* instance = context;
  101. instance->decoder.parser_step = InfactoryDecoderStepReset;
  102. }
  103. static bool ws_protocol_infactory_check_crc(WSProtocolDecoderInfactory* instance) {
  104. uint8_t msg[] = {
  105. instance->decoder.decode_data >> 32,
  106. (((instance->decoder.decode_data >> 24) & 0x0F) | (instance->decoder.decode_data & 0x0F)
  107. << 4),
  108. instance->decoder.decode_data >> 16,
  109. instance->decoder.decode_data >> 8,
  110. instance->decoder.decode_data};
  111. uint8_t crc =
  112. subghz_protocol_blocks_crc4(msg, 4, 0x13, 0); // Koopmann 0x9, CCITT-4; FP-4; ITU-T G.704
  113. crc ^= msg[4] >> 4; // last nibble is only XORed
  114. return (crc == ((instance->decoder.decode_data >> 28) & 0x0F));
  115. }
  116. /**
  117. * Analysis of received data
  118. * @param instance Pointer to a WSBlockGeneric* instance
  119. */
  120. static void ws_protocol_infactory_remote_controller(WSBlockGeneric* instance) {
  121. instance->id = instance->data >> 32;
  122. instance->battery_low = (instance->data >> 26) & 1;
  123. instance->temp = ws_block_generic_fahrenheit_to_celsius(
  124. ((float)((instance->data >> 12) & 0x0FFF) - 900.0f) / 10.0f);
  125. instance->humidity =
  126. (((instance->data >> 8) & 0x0F) * 10) + ((instance->data >> 4) & 0x0F); // BCD, 'A0'=100%rH
  127. instance->channel = instance->data & 0x03;
  128. }
  129. void ws_protocol_decoder_infactory_feed(void* context, bool level, uint32_t duration) {
  130. furi_assert(context);
  131. WSProtocolDecoderInfactory* instance = context;
  132. switch(instance->decoder.parser_step) {
  133. case InfactoryDecoderStepReset:
  134. if((level) && (DURATION_DIFF(duration, ws_protocol_infactory_const.te_short * 2) <
  135. ws_protocol_infactory_const.te_delta * 2)) {
  136. instance->decoder.parser_step = InfactoryDecoderStepCheckPreambule;
  137. instance->decoder.te_last = duration;
  138. instance->header_count = 0;
  139. }
  140. break;
  141. case InfactoryDecoderStepCheckPreambule:
  142. if(level) {
  143. instance->decoder.te_last = duration;
  144. } else {
  145. if((DURATION_DIFF(instance->decoder.te_last, ws_protocol_infactory_const.te_short * 2) <
  146. ws_protocol_infactory_const.te_delta * 2) &&
  147. (DURATION_DIFF(duration, ws_protocol_infactory_const.te_short * 2) <
  148. ws_protocol_infactory_const.te_delta * 2)) {
  149. //Found preambule
  150. instance->header_count++;
  151. } else if(
  152. (DURATION_DIFF(instance->decoder.te_last, ws_protocol_infactory_const.te_short) <
  153. ws_protocol_infactory_const.te_delta) &&
  154. (DURATION_DIFF(duration, ws_protocol_infactory_const.te_short * 16) <
  155. ws_protocol_infactory_const.te_delta * 8)) {
  156. //Found syncPrefix
  157. if(instance->header_count > 3) {
  158. instance->decoder.parser_step = InfactoryDecoderStepSaveDuration;
  159. instance->decoder.decode_data = 0;
  160. instance->decoder.decode_count_bit = 0;
  161. }
  162. } else {
  163. instance->decoder.parser_step = InfactoryDecoderStepReset;
  164. }
  165. }
  166. break;
  167. case InfactoryDecoderStepSaveDuration:
  168. if(level) {
  169. instance->decoder.te_last = duration;
  170. instance->decoder.parser_step = InfactoryDecoderStepCheckDuration;
  171. } else {
  172. instance->decoder.parser_step = InfactoryDecoderStepReset;
  173. }
  174. break;
  175. case InfactoryDecoderStepCheckDuration:
  176. if(!level) {
  177. if(duration >= ((uint32_t)ws_protocol_infactory_const.te_short * 30)) {
  178. //Found syncPostfix
  179. if((instance->decoder.decode_count_bit ==
  180. ws_protocol_infactory_const.min_count_bit_for_found) &&
  181. ws_protocol_infactory_check_crc(instance)) {
  182. instance->generic.data = instance->decoder.decode_data;
  183. instance->generic.data_count_bit = instance->decoder.decode_count_bit;
  184. ws_protocol_infactory_remote_controller(&instance->generic);
  185. if(instance->base.callback)
  186. instance->base.callback(&instance->base, instance->base.context);
  187. }
  188. instance->decoder.decode_data = 0;
  189. instance->decoder.decode_count_bit = 0;
  190. instance->decoder.parser_step = InfactoryDecoderStepReset;
  191. break;
  192. } else if(
  193. (DURATION_DIFF(instance->decoder.te_last, ws_protocol_infactory_const.te_short) <
  194. ws_protocol_infactory_const.te_delta) &&
  195. (DURATION_DIFF(duration, ws_protocol_infactory_const.te_long) <
  196. ws_protocol_infactory_const.te_delta * 2)) {
  197. subghz_protocol_blocks_add_bit(&instance->decoder, 0);
  198. instance->decoder.parser_step = InfactoryDecoderStepSaveDuration;
  199. } else if(
  200. (DURATION_DIFF(instance->decoder.te_last, ws_protocol_infactory_const.te_short) <
  201. ws_protocol_infactory_const.te_delta) &&
  202. (DURATION_DIFF(duration, ws_protocol_infactory_const.te_long * 2) <
  203. ws_protocol_infactory_const.te_delta * 4)) {
  204. subghz_protocol_blocks_add_bit(&instance->decoder, 1);
  205. instance->decoder.parser_step = InfactoryDecoderStepSaveDuration;
  206. } else {
  207. instance->decoder.parser_step = InfactoryDecoderStepReset;
  208. }
  209. } else {
  210. instance->decoder.parser_step = InfactoryDecoderStepReset;
  211. }
  212. break;
  213. }
  214. }
  215. uint8_t ws_protocol_decoder_infactory_get_hash_data(void* context) {
  216. furi_assert(context);
  217. WSProtocolDecoderInfactory* 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_infactory_serialize(
  222. void* context,
  223. FlipperFormat* flipper_format,
  224. SubGhzRadioPreset* preset) {
  225. furi_assert(context);
  226. WSProtocolDecoderInfactory* instance = context;
  227. return ws_block_generic_serialize(&instance->generic, flipper_format, preset);
  228. }
  229. bool ws_protocol_decoder_infactory_deserialize(void* context, FlipperFormat* flipper_format) {
  230. furi_assert(context);
  231. WSProtocolDecoderInfactory* instance = context;
  232. bool ret = false;
  233. do {
  234. if(!ws_block_generic_deserialize(&instance->generic, flipper_format)) {
  235. break;
  236. }
  237. if(instance->generic.data_count_bit !=
  238. ws_protocol_infactory_const.min_count_bit_for_found) {
  239. FURI_LOG_E(TAG, "Wrong number of bits in key");
  240. break;
  241. }
  242. ret = true;
  243. } while(false);
  244. return ret;
  245. }
  246. void ws_protocol_decoder_infactory_get_string(void* context, FuriString* output) {
  247. furi_assert(context);
  248. WSProtocolDecoderInfactory* instance = context;
  249. furi_string_printf(
  250. output,
  251. "%s %dbit\r\n"
  252. "Key:0x%lX%08lX\r\n"
  253. "Sn:0x%lX Ch:%d Bat:%d\r\n"
  254. "Temp:%d.%d C Hum:%d%%",
  255. instance->generic.protocol_name,
  256. instance->generic.data_count_bit,
  257. (uint32_t)(instance->generic.data >> 32),
  258. (uint32_t)(instance->generic.data),
  259. instance->generic.id,
  260. instance->generic.channel,
  261. instance->generic.battery_low,
  262. (int16_t)instance->generic.temp,
  263. abs(((int16_t)(instance->generic.temp * 10) - (((int16_t)instance->generic.temp) * 10))),
  264. instance->generic.humidity);
  265. }