infactory.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  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->btn = WS_NO_BTN;
  124. instance->temp =
  125. locale_fahrenheit_to_celsius(((float)((instance->data >> 12) & 0x0FFF) - 900.0f) / 10.0f);
  126. instance->humidity =
  127. (((instance->data >> 8) & 0x0F) * 10) + ((instance->data >> 4) & 0x0F); // BCD, 'A0'=100%rH
  128. instance->channel = instance->data & 0x03;
  129. }
  130. void ws_protocol_decoder_infactory_feed(void* context, bool level, uint32_t duration) {
  131. furi_assert(context);
  132. WSProtocolDecoderInfactory* instance = context;
  133. switch(instance->decoder.parser_step) {
  134. case InfactoryDecoderStepReset:
  135. if((level) && (DURATION_DIFF(duration, ws_protocol_infactory_const.te_short * 2) <
  136. ws_protocol_infactory_const.te_delta * 2)) {
  137. instance->decoder.parser_step = InfactoryDecoderStepCheckPreambule;
  138. instance->decoder.te_last = duration;
  139. instance->header_count = 0;
  140. }
  141. break;
  142. case InfactoryDecoderStepCheckPreambule:
  143. if(level) {
  144. instance->decoder.te_last = duration;
  145. } else {
  146. if((DURATION_DIFF(instance->decoder.te_last, ws_protocol_infactory_const.te_short * 2) <
  147. ws_protocol_infactory_const.te_delta * 2) &&
  148. (DURATION_DIFF(duration, ws_protocol_infactory_const.te_short * 2) <
  149. ws_protocol_infactory_const.te_delta * 2)) {
  150. //Found preambule
  151. instance->header_count++;
  152. } else if(
  153. (DURATION_DIFF(instance->decoder.te_last, ws_protocol_infactory_const.te_short) <
  154. ws_protocol_infactory_const.te_delta) &&
  155. (DURATION_DIFF(duration, ws_protocol_infactory_const.te_short * 16) <
  156. ws_protocol_infactory_const.te_delta * 8)) {
  157. //Found syncPrefix
  158. if(instance->header_count > 3) {
  159. instance->decoder.parser_step = InfactoryDecoderStepSaveDuration;
  160. instance->decoder.decode_data = 0;
  161. instance->decoder.decode_count_bit = 0;
  162. }
  163. } else {
  164. instance->decoder.parser_step = InfactoryDecoderStepReset;
  165. }
  166. }
  167. break;
  168. case InfactoryDecoderStepSaveDuration:
  169. if(level) {
  170. instance->decoder.te_last = duration;
  171. instance->decoder.parser_step = InfactoryDecoderStepCheckDuration;
  172. } else {
  173. instance->decoder.parser_step = InfactoryDecoderStepReset;
  174. }
  175. break;
  176. case InfactoryDecoderStepCheckDuration:
  177. if(!level) {
  178. if(duration >= ((uint32_t)ws_protocol_infactory_const.te_short * 30)) {
  179. //Found syncPostfix
  180. if((instance->decoder.decode_count_bit ==
  181. ws_protocol_infactory_const.min_count_bit_for_found) &&
  182. ws_protocol_infactory_check_crc(instance)) {
  183. instance->generic.data = instance->decoder.decode_data;
  184. instance->generic.data_count_bit = instance->decoder.decode_count_bit;
  185. ws_protocol_infactory_remote_controller(&instance->generic);
  186. if(instance->base.callback)
  187. instance->base.callback(&instance->base, instance->base.context);
  188. }
  189. instance->decoder.decode_data = 0;
  190. instance->decoder.decode_count_bit = 0;
  191. instance->decoder.parser_step = InfactoryDecoderStepReset;
  192. break;
  193. } else if(
  194. (DURATION_DIFF(instance->decoder.te_last, ws_protocol_infactory_const.te_short) <
  195. ws_protocol_infactory_const.te_delta) &&
  196. (DURATION_DIFF(duration, ws_protocol_infactory_const.te_long) <
  197. ws_protocol_infactory_const.te_delta * 2)) {
  198. subghz_protocol_blocks_add_bit(&instance->decoder, 0);
  199. instance->decoder.parser_step = InfactoryDecoderStepSaveDuration;
  200. } else if(
  201. (DURATION_DIFF(instance->decoder.te_last, ws_protocol_infactory_const.te_short) <
  202. ws_protocol_infactory_const.te_delta) &&
  203. (DURATION_DIFF(duration, ws_protocol_infactory_const.te_long * 2) <
  204. ws_protocol_infactory_const.te_delta * 4)) {
  205. subghz_protocol_blocks_add_bit(&instance->decoder, 1);
  206. instance->decoder.parser_step = InfactoryDecoderStepSaveDuration;
  207. } else {
  208. instance->decoder.parser_step = InfactoryDecoderStepReset;
  209. }
  210. } else {
  211. instance->decoder.parser_step = InfactoryDecoderStepReset;
  212. }
  213. break;
  214. }
  215. }
  216. uint8_t ws_protocol_decoder_infactory_get_hash_data(void* context) {
  217. furi_assert(context);
  218. WSProtocolDecoderInfactory* instance = context;
  219. return subghz_protocol_blocks_get_hash_data(
  220. &instance->decoder, (instance->decoder.decode_count_bit / 8) + 1);
  221. }
  222. SubGhzProtocolStatus ws_protocol_decoder_infactory_serialize(
  223. void* context,
  224. FlipperFormat* flipper_format,
  225. SubGhzRadioPreset* preset) {
  226. furi_assert(context);
  227. WSProtocolDecoderInfactory* instance = context;
  228. return ws_block_generic_serialize(&instance->generic, flipper_format, preset);
  229. }
  230. SubGhzProtocolStatus
  231. ws_protocol_decoder_infactory_deserialize(void* context, FlipperFormat* flipper_format) {
  232. furi_assert(context);
  233. WSProtocolDecoderInfactory* instance = context;
  234. return ws_block_generic_deserialize_check_count_bit(
  235. &instance->generic, flipper_format, ws_protocol_infactory_const.min_count_bit_for_found);
  236. }
  237. void ws_protocol_decoder_infactory_get_string(void* context, FuriString* output) {
  238. furi_assert(context);
  239. WSProtocolDecoderInfactory* instance = context;
  240. furi_string_printf(
  241. output,
  242. "%s %dbit\r\n"
  243. "Key:0x%lX%08lX\r\n"
  244. "Sn:0x%lX Ch:%d Bat:%d\r\n"
  245. "Temp:%3.1f C Hum:%d%%",
  246. instance->generic.protocol_name,
  247. instance->generic.data_count_bit,
  248. (uint32_t)(instance->generic.data >> 32),
  249. (uint32_t)(instance->generic.data),
  250. instance->generic.id,
  251. instance->generic.channel,
  252. instance->generic.battery_low,
  253. (double)instance->generic.temp,
  254. instance->generic.humidity);
  255. }