oregon_v1.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  1. #include "oregon_v1.h"
  2. #include <lib/toolbox/manchester_decoder.h>
  3. #define TAG "WSProtocolOregon_V1"
  4. /*
  5. * Help
  6. * https://github.dev/merbanan/rtl_433/blob/bb1be7f186ac0fdb7dc5d77693847d96fb95281e/src/devices/oregon_scientific_v1.c
  7. *
  8. * OSv1 protocol.
  9. *
  10. * MC with nominal bit width of 2930 us.
  11. * Pulses are somewhat longer than nominal half-bit width, 1748 us / 3216 us,
  12. * Gaps are somewhat shorter than nominal half-bit width, 1176 us / 2640 us.
  13. * After 12 preamble bits there is 4200 us gap, 5780 us pulse, 5200 us gap.
  14. * And next 32 bit data
  15. *
  16. * Care must be taken with the gap after the sync pulse since it
  17. * is outside of the normal clocking. Because of this a data stream
  18. * beginning with a 0 will have data in this gap.
  19. *
  20. *
  21. * Data is in reverse order of bits
  22. * RevBit(data32bit)=> tib23atad
  23. *
  24. * tib23atad => xxxxxxxx | busuTTTT | ttttzzzz | ccuuiiii
  25. *
  26. * - i: ID
  27. * - x: CRC;
  28. * - u: unknown;
  29. * - b: battery low; flag to indicate low battery voltage
  30. * - s: temperature sign
  31. * - T: BCD, Temperature; in °C * 10
  32. * - t: BCD, Temperature; in °C * 1
  33. * - z: BCD, Temperature; in °C * 0.1
  34. * - c: Channel 00=CH1, 01=CH2, 10=CH3
  35. *
  36. */
  37. #define OREGON_V1_HEADER_OK 0xFF
  38. static const SubGhzBlockConst ws_protocol_oregon_v1_const = {
  39. .te_short = 1465,
  40. .te_long = 2930,
  41. .te_delta = 350,
  42. .min_count_bit_for_found = 32,
  43. };
  44. struct WSProtocolDecoderOregon_V1 {
  45. SubGhzProtocolDecoderBase base;
  46. SubGhzBlockDecoder decoder;
  47. WSBlockGeneric generic;
  48. ManchesterState manchester_state;
  49. uint16_t header_count;
  50. uint8_t first_bit;
  51. };
  52. struct WSProtocolEncoderOregon_V1 {
  53. SubGhzProtocolEncoderBase base;
  54. SubGhzProtocolBlockEncoder encoder;
  55. WSBlockGeneric generic;
  56. };
  57. typedef enum {
  58. Oregon_V1DecoderStepReset = 0,
  59. Oregon_V1DecoderStepFoundPreamble,
  60. Oregon_V1DecoderStepParse,
  61. } Oregon_V1DecoderStep;
  62. const SubGhzProtocolDecoder ws_protocol_oregon_v1_decoder = {
  63. .alloc = ws_protocol_decoder_oregon_v1_alloc,
  64. .free = ws_protocol_decoder_oregon_v1_free,
  65. .feed = ws_protocol_decoder_oregon_v1_feed,
  66. .reset = ws_protocol_decoder_oregon_v1_reset,
  67. .get_hash_data = ws_protocol_decoder_oregon_v1_get_hash_data,
  68. .serialize = ws_protocol_decoder_oregon_v1_serialize,
  69. .deserialize = ws_protocol_decoder_oregon_v1_deserialize,
  70. .get_string = ws_protocol_decoder_oregon_v1_get_string,
  71. };
  72. const SubGhzProtocolEncoder ws_protocol_oregon_v1_encoder = {
  73. .alloc = NULL,
  74. .free = NULL,
  75. .deserialize = NULL,
  76. .stop = NULL,
  77. .yield = NULL,
  78. };
  79. const SubGhzProtocol ws_protocol_oregon_v1 = {
  80. .name = WS_PROTOCOL_OREGON_V1_NAME,
  81. .type = SubGhzProtocolWeatherStation,
  82. .flag = SubGhzProtocolFlag_433 | SubGhzProtocolFlag_315 | SubGhzProtocolFlag_868 |
  83. SubGhzProtocolFlag_AM | SubGhzProtocolFlag_Decodable,
  84. .decoder = &ws_protocol_oregon_v1_decoder,
  85. .encoder = &ws_protocol_oregon_v1_encoder,
  86. };
  87. void* ws_protocol_decoder_oregon_v1_alloc(SubGhzEnvironment* environment) {
  88. UNUSED(environment);
  89. WSProtocolDecoderOregon_V1* instance = malloc(sizeof(WSProtocolDecoderOregon_V1));
  90. instance->base.protocol = &ws_protocol_oregon_v1;
  91. instance->generic.protocol_name = instance->base.protocol->name;
  92. return instance;
  93. }
  94. void ws_protocol_decoder_oregon_v1_free(void* context) {
  95. furi_assert(context);
  96. WSProtocolDecoderOregon_V1* instance = context;
  97. free(instance);
  98. }
  99. void ws_protocol_decoder_oregon_v1_reset(void* context) {
  100. furi_assert(context);
  101. WSProtocolDecoderOregon_V1* instance = context;
  102. instance->decoder.parser_step = Oregon_V1DecoderStepReset;
  103. }
  104. static bool ws_protocol_oregon_v1_check(WSProtocolDecoderOregon_V1* instance) {
  105. if(!instance->decoder.decode_data) return false;
  106. uint64_t data = subghz_protocol_blocks_reverse_key(instance->decoder.decode_data, 32);
  107. uint16_t crc = (data & 0xff) + ((data >> 8) & 0xff) + ((data >> 16) & 0xff);
  108. crc = (crc & 0xff) + ((crc >> 8) & 0xff);
  109. return (crc == ((data >> 24) & 0xFF));
  110. }
  111. /**
  112. * Analysis of received data
  113. * @param instance Pointer to a WSBlockGeneric* instance
  114. */
  115. static void ws_protocol_oregon_v1_remote_controller(WSBlockGeneric* instance) {
  116. uint64_t data = subghz_protocol_blocks_reverse_key(instance->data, 32);
  117. instance->id = data & 0xFF;
  118. instance->channel = ((data >> 6) & 0x03) + 1;
  119. float temp_raw =
  120. ((data >> 8) & 0x0F) * 0.1f + ((data >> 12) & 0x0F) + ((data >> 16) & 0x0F) * 10.0f;
  121. if(!((data >> 21) & 1)) {
  122. instance->temp = temp_raw;
  123. } else {
  124. instance->temp = -temp_raw;
  125. }
  126. instance->battery_low = !((instance->data >> 23) & 1ULL);
  127. instance->btn = WS_NO_BTN;
  128. instance->humidity = WS_NO_HUMIDITY;
  129. }
  130. void ws_protocol_decoder_oregon_v1_feed(void* context, bool level, uint32_t duration) {
  131. furi_assert(context);
  132. WSProtocolDecoderOregon_V1* instance = context;
  133. ManchesterEvent event = ManchesterEventReset;
  134. switch(instance->decoder.parser_step) {
  135. case Oregon_V1DecoderStepReset:
  136. if((level) && (DURATION_DIFF(duration, ws_protocol_oregon_v1_const.te_short) <
  137. ws_protocol_oregon_v1_const.te_delta)) {
  138. instance->decoder.parser_step = Oregon_V1DecoderStepFoundPreamble;
  139. instance->decoder.te_last = duration;
  140. instance->header_count = 0;
  141. }
  142. break;
  143. case Oregon_V1DecoderStepFoundPreamble:
  144. if(level) {
  145. //keep high levels, if they suit our durations
  146. if((DURATION_DIFF(duration, ws_protocol_oregon_v1_const.te_short) <
  147. ws_protocol_oregon_v1_const.te_delta) ||
  148. (DURATION_DIFF(duration, ws_protocol_oregon_v1_const.te_short * 4) <
  149. ws_protocol_oregon_v1_const.te_delta)) {
  150. instance->decoder.te_last = duration;
  151. } else {
  152. instance->decoder.parser_step = Oregon_V1DecoderStepReset;
  153. }
  154. } else if(
  155. //checking low levels
  156. (DURATION_DIFF(duration, ws_protocol_oregon_v1_const.te_short) <
  157. ws_protocol_oregon_v1_const.te_delta) &&
  158. (DURATION_DIFF(instance->decoder.te_last, ws_protocol_oregon_v1_const.te_short) <
  159. ws_protocol_oregon_v1_const.te_delta)) {
  160. // Found header
  161. instance->header_count++;
  162. } else if(
  163. (DURATION_DIFF(duration, ws_protocol_oregon_v1_const.te_short * 3) <
  164. ws_protocol_oregon_v1_const.te_delta) &&
  165. (DURATION_DIFF(instance->decoder.te_last, ws_protocol_oregon_v1_const.te_short) <
  166. ws_protocol_oregon_v1_const.te_delta)) {
  167. // check header
  168. if(instance->header_count > 7) {
  169. instance->header_count = OREGON_V1_HEADER_OK;
  170. }
  171. } else if(
  172. (instance->header_count == OREGON_V1_HEADER_OK) &&
  173. (DURATION_DIFF(instance->decoder.te_last, ws_protocol_oregon_v1_const.te_short * 4) <
  174. ws_protocol_oregon_v1_const.te_delta)) {
  175. //found all the necessary patterns
  176. instance->decoder.decode_data = 0;
  177. instance->decoder.decode_count_bit = 1;
  178. manchester_advance(
  179. instance->manchester_state,
  180. ManchesterEventReset,
  181. &instance->manchester_state,
  182. NULL);
  183. instance->decoder.parser_step = Oregon_V1DecoderStepParse;
  184. if(duration < ws_protocol_oregon_v1_const.te_short * 4) {
  185. instance->first_bit = 1;
  186. } else {
  187. instance->first_bit = 0;
  188. }
  189. } else {
  190. instance->decoder.parser_step = Oregon_V1DecoderStepReset;
  191. }
  192. break;
  193. case Oregon_V1DecoderStepParse:
  194. if(level) {
  195. if(DURATION_DIFF(duration, ws_protocol_oregon_v1_const.te_short) <
  196. ws_protocol_oregon_v1_const.te_delta) {
  197. event = ManchesterEventShortHigh;
  198. } else if(
  199. DURATION_DIFF(duration, ws_protocol_oregon_v1_const.te_long) <
  200. ws_protocol_oregon_v1_const.te_delta) {
  201. event = ManchesterEventLongHigh;
  202. } else {
  203. instance->decoder.parser_step = Oregon_V1DecoderStepReset;
  204. }
  205. } else {
  206. if(DURATION_DIFF(duration, ws_protocol_oregon_v1_const.te_short) <
  207. ws_protocol_oregon_v1_const.te_delta) {
  208. event = ManchesterEventShortLow;
  209. } else if(
  210. DURATION_DIFF(duration, ws_protocol_oregon_v1_const.te_long) <
  211. ws_protocol_oregon_v1_const.te_delta) {
  212. event = ManchesterEventLongLow;
  213. } else if(duration >= ((uint32_t)ws_protocol_oregon_v1_const.te_long * 2)) {
  214. if(instance->decoder.decode_count_bit ==
  215. ws_protocol_oregon_v1_const.min_count_bit_for_found) {
  216. if(instance->first_bit) {
  217. instance->decoder.decode_data = ~instance->decoder.decode_data | (1 << 31);
  218. }
  219. if(ws_protocol_oregon_v1_check(instance)) {
  220. instance->generic.data = instance->decoder.decode_data;
  221. instance->generic.data_count_bit = instance->decoder.decode_count_bit;
  222. ws_protocol_oregon_v1_remote_controller(&instance->generic);
  223. if(instance->base.callback)
  224. instance->base.callback(&instance->base, instance->base.context);
  225. }
  226. }
  227. instance->decoder.decode_data = 0;
  228. instance->decoder.decode_count_bit = 0;
  229. manchester_advance(
  230. instance->manchester_state,
  231. ManchesterEventReset,
  232. &instance->manchester_state,
  233. NULL);
  234. } else {
  235. instance->decoder.parser_step = Oregon_V1DecoderStepReset;
  236. }
  237. }
  238. if(event != ManchesterEventReset) {
  239. bool data;
  240. bool data_ok = manchester_advance(
  241. instance->manchester_state, event, &instance->manchester_state, &data);
  242. if(data_ok) {
  243. instance->decoder.decode_data = (instance->decoder.decode_data << 1) | !data;
  244. instance->decoder.decode_count_bit++;
  245. }
  246. }
  247. break;
  248. }
  249. }
  250. uint8_t ws_protocol_decoder_oregon_v1_get_hash_data(void* context) {
  251. furi_assert(context);
  252. WSProtocolDecoderOregon_V1* instance = context;
  253. return subghz_protocol_blocks_get_hash_data(
  254. &instance->decoder, (instance->decoder.decode_count_bit / 8) + 1);
  255. }
  256. SubGhzProtocolStatus ws_protocol_decoder_oregon_v1_serialize(
  257. void* context,
  258. FlipperFormat* flipper_format,
  259. SubGhzRadioPreset* preset) {
  260. furi_assert(context);
  261. WSProtocolDecoderOregon_V1* instance = context;
  262. return ws_block_generic_serialize(&instance->generic, flipper_format, preset);
  263. }
  264. SubGhzProtocolStatus
  265. ws_protocol_decoder_oregon_v1_deserialize(void* context, FlipperFormat* flipper_format) {
  266. furi_assert(context);
  267. WSProtocolDecoderOregon_V1* instance = context;
  268. return ws_block_generic_deserialize_check_count_bit(
  269. &instance->generic, flipper_format, ws_protocol_oregon_v1_const.min_count_bit_for_found);
  270. }
  271. void ws_protocol_decoder_oregon_v1_get_string(void* context, FuriString* output) {
  272. furi_assert(context);
  273. WSProtocolDecoderOregon_V1* instance = context;
  274. furi_string_printf(
  275. output,
  276. "%s %dbit\r\n"
  277. "Key:0x%lX%08lX\r\n"
  278. "Sn:0x%lX Ch:%d Bat:%d\r\n"
  279. "Temp:%3.1f C Hum:%d%%",
  280. instance->generic.protocol_name,
  281. instance->generic.data_count_bit,
  282. (uint32_t)(instance->generic.data >> 32),
  283. (uint32_t)(instance->generic.data),
  284. instance->generic.id,
  285. instance->generic.channel,
  286. instance->generic.battery_low,
  287. (double)instance->generic.temp,
  288. instance->generic.humidity);
  289. }