oregon2.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404
  1. #include "oregon2.h"
  2. #include <lib/subghz/blocks/const.h>
  3. #include <lib/subghz/blocks/decoder.h>
  4. #include <lib/subghz/blocks/encoder.h>
  5. #include <lib/subghz/blocks/math.h>
  6. #include "ws_generic.h"
  7. #include <lib/toolbox/manchester_decoder.h>
  8. #include <lib/flipper_format/flipper_format_i.h>
  9. #define TAG "WSProtocolOregon2"
  10. static const SubGhzBlockConst ws_oregon2_const = {
  11. .te_long = 1000,
  12. .te_short = 500,
  13. .te_delta = 200,
  14. .min_count_bit_for_found = 32,
  15. };
  16. #define OREGON2_PREAMBLE_BITS 19
  17. #define OREGON2_PREAMBLE_MASK 0b1111111111111111111
  18. #define OREGON2_SENSOR_ID(d) (((d) >> 16) & 0xFFFF)
  19. #define OREGON2_CHECKSUM_BITS 8
  20. // 15 ones + 0101 (inverted A)
  21. #define OREGON2_PREAMBLE 0b1111111111111110101
  22. // bit indicating the low battery
  23. #define OREGON2_FLAG_BAT_LOW 0x4
  24. /// Documentation for Oregon Scientific protocols can be found here:
  25. /// http://wmrx00.sourceforge.net/Arduino/OregonScientific-RF-Protocols.pdf
  26. // Sensors ID
  27. #define ID_THGR122N 0x1d20
  28. #define ID_THGR968 0x1d30
  29. #define ID_BTHR918 0x5d50
  30. #define ID_BHTR968 0x5d60
  31. #define ID_RGR968 0x2d10
  32. #define ID_THR228N 0xec40
  33. #define ID_THN132N 0xec40 // same as THR228N but different packet size
  34. #define ID_RTGN318 0x0cc3 // warning: id is from 0x0cc3 and 0xfcc3
  35. #define ID_RTGN129 0x0cc3 // same as RTGN318 but different packet size
  36. #define ID_THGR810 0xf824 // This might be ID_THGR81, but what's true is lost in (git) history
  37. #define ID_THGR810a 0xf8b4 // unconfirmed version
  38. #define ID_THN802 0xc844
  39. #define ID_PCR800 0x2914
  40. #define ID_PCR800a 0x2d14 // Different PCR800 ID - AU version I think
  41. #define ID_WGR800 0x1984
  42. #define ID_WGR800a 0x1994 // unconfirmed version
  43. #define ID_WGR968 0x3d00
  44. #define ID_UV800 0xd874
  45. #define ID_THN129 0xcc43 // THN129 Temp only
  46. #define ID_RTHN129 0x0cd3 // RTHN129 Temp, clock sensors
  47. #define ID_BTHGN129 0x5d53 // Baro, Temp, Hygro sensor
  48. #define ID_UVR128 0xec70
  49. #define ID_THGR328N 0xcc23 // Temp & Hygro sensor similar to THR228N with 5 channel instead of 3
  50. #define ID_RTGR328N_1 0xdcc3 // RTGR328N_[1-5] RFclock(date &time)&Temp&Hygro sensor
  51. #define ID_RTGR328N_2 0xccc3
  52. #define ID_RTGR328N_3 0xbcc3
  53. #define ID_RTGR328N_4 0xacc3
  54. #define ID_RTGR328N_5 0x9cc3
  55. #define ID_RTGR328N_6 0x8ce3 // RTGR328N_6&7 RFclock(date &time)&Temp&Hygro sensor like THGR328N
  56. #define ID_RTGR328N_7 0x8ae3
  57. struct WSProtocolDecoderOregon2 {
  58. SubGhzProtocolDecoderBase base;
  59. SubGhzBlockDecoder decoder;
  60. WSBlockGeneric generic;
  61. ManchesterState manchester_state;
  62. bool prev_bit;
  63. bool have_bit;
  64. uint8_t var_bits;
  65. uint32_t var_data;
  66. };
  67. typedef struct WSProtocolDecoderOregon2 WSProtocolDecoderOregon2;
  68. typedef enum {
  69. Oregon2DecoderStepReset = 0,
  70. Oregon2DecoderStepFoundPreamble,
  71. Oregon2DecoderStepVarData,
  72. } Oregon2DecoderStep;
  73. void* ws_protocol_decoder_oregon2_alloc(SubGhzEnvironment* environment) {
  74. UNUSED(environment);
  75. WSProtocolDecoderOregon2* instance = malloc(sizeof(WSProtocolDecoderOregon2));
  76. instance->base.protocol = &ws_protocol_oregon2;
  77. instance->generic.protocol_name = instance->base.protocol->name;
  78. instance->generic.humidity = WS_NO_HUMIDITY;
  79. instance->generic.temp = WS_NO_TEMPERATURE;
  80. instance->generic.btn = WS_NO_BTN;
  81. instance->generic.channel = WS_NO_CHANNEL;
  82. instance->generic.battery_low = WS_NO_BATT;
  83. instance->generic.id = WS_NO_ID;
  84. return instance;
  85. }
  86. void ws_protocol_decoder_oregon2_free(void* context) {
  87. furi_assert(context);
  88. WSProtocolDecoderOregon2* instance = context;
  89. free(instance);
  90. }
  91. void ws_protocol_decoder_oregon2_reset(void* context) {
  92. furi_assert(context);
  93. WSProtocolDecoderOregon2* instance = context;
  94. instance->decoder.parser_step = Oregon2DecoderStepReset;
  95. instance->decoder.decode_data = 0UL;
  96. instance->decoder.decode_count_bit = 0;
  97. manchester_advance(
  98. instance->manchester_state, ManchesterEventReset, &instance->manchester_state, NULL);
  99. instance->have_bit = false;
  100. instance->var_data = 0;
  101. instance->var_bits = 0;
  102. }
  103. static ManchesterEvent level_and_duration_to_event(bool level, uint32_t duration) {
  104. bool is_long = false;
  105. if(DURATION_DIFF(duration, ws_oregon2_const.te_long) < ws_oregon2_const.te_delta) {
  106. is_long = true;
  107. } else if(DURATION_DIFF(duration, ws_oregon2_const.te_short) < ws_oregon2_const.te_delta) {
  108. is_long = false;
  109. } else {
  110. return ManchesterEventReset;
  111. }
  112. if(level)
  113. return is_long ? ManchesterEventLongHigh : ManchesterEventShortHigh;
  114. else
  115. return is_long ? ManchesterEventLongLow : ManchesterEventShortLow;
  116. }
  117. // From sensor id code return amount of bits in variable section
  118. // https://temofeev.ru/info/articles/o-dekodirovanii-protokola-pogodnykh-datchikov-oregon-scientific
  119. static uint8_t oregon2_sensor_id_var_bits(uint16_t sensor_id) {
  120. if(sensor_id == ID_THR228N) return 16;
  121. if(sensor_id == ID_THGR122N) return 24;
  122. return 0;
  123. }
  124. static void ws_oregon2_decode_const_data(WSBlockGeneric* ws_block) {
  125. ws_block->id = OREGON2_SENSOR_ID(ws_block->data);
  126. uint8_t ch_bits = (ws_block->data >> 12) & 0xF;
  127. ws_block->channel = 1;
  128. while(ch_bits > 1) {
  129. ws_block->channel++;
  130. ch_bits >>= 1;
  131. }
  132. ws_block->battery_low = (ws_block->data & OREGON2_FLAG_BAT_LOW) ? 1 : 0;
  133. }
  134. uint16_t bcd_decode_short(uint32_t data) {
  135. return (data & 0xF) * 10 + ((data >> 4) & 0xF);
  136. }
  137. static float ws_oregon2_decode_temp(uint32_t data) {
  138. int32_t temp_val;
  139. temp_val = bcd_decode_short(data >> 4);
  140. temp_val *= 10;
  141. temp_val += (data >> 12) & 0xF;
  142. if(data & 0xF) temp_val = -temp_val;
  143. return (float)temp_val / 10.0;
  144. }
  145. static void ws_oregon2_decode_var_data(WSBlockGeneric* ws_b, uint16_t sensor_id, uint32_t data) {
  146. if(sensor_id == ID_THR228N) {
  147. ws_b->temp = ws_oregon2_decode_temp(data);
  148. ws_b->humidity = WS_NO_HUMIDITY;
  149. return;
  150. }
  151. if(sensor_id == ID_THGR122N) {
  152. ws_b->humidity = bcd_decode_short(data);
  153. ws_b->temp = ws_oregon2_decode_temp(data >> 8);
  154. return;
  155. }
  156. }
  157. void ws_protocol_decoder_oregon2_feed(void* context, bool level, uint32_t duration) {
  158. furi_assert(context);
  159. WSProtocolDecoderOregon2* instance = context;
  160. // oregon v2.1 signal is inverted
  161. ManchesterEvent event = level_and_duration_to_event(!level, duration);
  162. bool data;
  163. // low-level bit sequence decoding
  164. if(event == ManchesterEventReset) {
  165. instance->decoder.parser_step = Oregon2DecoderStepReset;
  166. instance->have_bit = false;
  167. instance->decoder.decode_data = 0UL;
  168. instance->decoder.decode_count_bit = 0;
  169. }
  170. if(manchester_advance(instance->manchester_state, event, &instance->manchester_state, &data)) {
  171. if(instance->have_bit) {
  172. if(!instance->prev_bit && data) {
  173. subghz_protocol_blocks_add_bit(&instance->decoder, 1);
  174. } else if(instance->prev_bit && !data) {
  175. subghz_protocol_blocks_add_bit(&instance->decoder, 0);
  176. } else {
  177. ws_protocol_decoder_oregon2_reset(context);
  178. }
  179. instance->have_bit = false;
  180. } else {
  181. instance->prev_bit = data;
  182. instance->have_bit = true;
  183. }
  184. }
  185. switch(instance->decoder.parser_step) {
  186. case Oregon2DecoderStepReset:
  187. // waiting for fixed oregon2 preamble
  188. if(instance->decoder.decode_count_bit >= OREGON2_PREAMBLE_BITS &&
  189. ((instance->decoder.decode_data & OREGON2_PREAMBLE_MASK) == OREGON2_PREAMBLE)) {
  190. instance->decoder.parser_step = Oregon2DecoderStepFoundPreamble;
  191. instance->decoder.decode_count_bit = 0;
  192. instance->decoder.decode_data = 0UL;
  193. }
  194. break;
  195. case Oregon2DecoderStepFoundPreamble:
  196. // waiting for fixed oregon2 data
  197. if(instance->decoder.decode_count_bit == 32) {
  198. instance->generic.data = instance->decoder.decode_data;
  199. instance->generic.data_count_bit = instance->decoder.decode_count_bit;
  200. instance->decoder.decode_data = 0UL;
  201. instance->decoder.decode_count_bit = 0;
  202. // reverse nibbles in decoded data
  203. instance->generic.data = (instance->generic.data & 0x55555555) << 1 |
  204. (instance->generic.data & 0xAAAAAAAA) >> 1;
  205. instance->generic.data = (instance->generic.data & 0x33333333) << 2 |
  206. (instance->generic.data & 0xCCCCCCCC) >> 2;
  207. ws_oregon2_decode_const_data(&instance->generic);
  208. instance->var_bits =
  209. oregon2_sensor_id_var_bits(OREGON2_SENSOR_ID(instance->generic.data));
  210. if(!instance->var_bits) {
  211. // sensor is not supported, stop decoding, but showing the decoded fixed part
  212. instance->decoder.parser_step = Oregon2DecoderStepReset;
  213. if(instance->base.callback)
  214. instance->base.callback(&instance->base, instance->base.context);
  215. } else {
  216. instance->decoder.parser_step = Oregon2DecoderStepVarData;
  217. }
  218. }
  219. break;
  220. case Oregon2DecoderStepVarData:
  221. // waiting for variable (sensor-specific data)
  222. if(instance->decoder.decode_count_bit == instance->var_bits + OREGON2_CHECKSUM_BITS) {
  223. instance->var_data = instance->decoder.decode_data & 0xFFFFFFFF;
  224. // reverse nibbles in var data
  225. instance->var_data = (instance->var_data & 0x55555555) << 1 |
  226. (instance->var_data & 0xAAAAAAAA) >> 1;
  227. instance->var_data = (instance->var_data & 0x33333333) << 2 |
  228. (instance->var_data & 0xCCCCCCCC) >> 2;
  229. ws_oregon2_decode_var_data(
  230. &instance->generic,
  231. OREGON2_SENSOR_ID(instance->generic.data),
  232. instance->var_data >> OREGON2_CHECKSUM_BITS);
  233. instance->decoder.parser_step = Oregon2DecoderStepReset;
  234. if(instance->base.callback)
  235. instance->base.callback(&instance->base, instance->base.context);
  236. }
  237. break;
  238. }
  239. }
  240. uint8_t ws_protocol_decoder_oregon2_get_hash_data(void* context) {
  241. furi_assert(context);
  242. WSProtocolDecoderOregon2* instance = context;
  243. return subghz_protocol_blocks_get_hash_data(
  244. &instance->decoder, (instance->decoder.decode_count_bit / 8) + 1);
  245. }
  246. bool ws_protocol_decoder_oregon2_serialize(
  247. void* context,
  248. FlipperFormat* flipper_format,
  249. SubGhzRadioPreset* preset) {
  250. furi_assert(context);
  251. WSProtocolDecoderOregon2* instance = context;
  252. if(!ws_block_generic_serialize(&instance->generic, flipper_format, preset)) return false;
  253. uint32_t temp = instance->var_bits;
  254. if(!flipper_format_write_uint32(flipper_format, "VarBits", &temp, 1)) {
  255. FURI_LOG_E(TAG, "Error adding VarBits");
  256. return false;
  257. }
  258. if(!flipper_format_write_hex(
  259. flipper_format,
  260. "VarData",
  261. (const uint8_t*)&instance->var_data,
  262. sizeof(instance->var_data))) {
  263. FURI_LOG_E(TAG, "Error adding VarData");
  264. return false;
  265. }
  266. return true;
  267. }
  268. bool ws_protocol_decoder_oregon2_deserialize(void* context, FlipperFormat* flipper_format) {
  269. furi_assert(context);
  270. WSProtocolDecoderOregon2* instance = context;
  271. bool ret = false;
  272. uint32_t temp_data;
  273. do {
  274. if(!ws_block_generic_deserialize(&instance->generic, flipper_format)) {
  275. break;
  276. }
  277. if(!flipper_format_read_uint32(flipper_format, "VarBits", &temp_data, 1)) {
  278. FURI_LOG_E(TAG, "Missing VarLen");
  279. break;
  280. }
  281. instance->var_bits = (uint8_t)temp_data;
  282. if(!flipper_format_read_hex(
  283. flipper_format,
  284. "VarData",
  285. (uint8_t*)&instance->var_data,
  286. sizeof(instance->var_data))) {
  287. FURI_LOG_E(TAG, "Missing VarData");
  288. break;
  289. }
  290. if(instance->generic.data_count_bit != ws_oregon2_const.min_count_bit_for_found) {
  291. FURI_LOG_E(TAG, "Wrong number of bits in key: %d", instance->generic.data_count_bit);
  292. break;
  293. }
  294. ret = true;
  295. } while(false);
  296. return ret;
  297. }
  298. static void oregon2_append_check_sum(uint32_t fix_data, uint32_t var_data, FuriString* output) {
  299. uint8_t sum = fix_data & 0xF;
  300. uint8_t ref_sum = var_data & 0xFF;
  301. var_data >>= 8;
  302. for(uint8_t i = 1; i < 8; i++) {
  303. fix_data >>= 4;
  304. var_data >>= 4;
  305. sum += (fix_data & 0xF) + (var_data & 0xF);
  306. }
  307. // swap calculated sum nibbles
  308. sum = (((sum >> 4) & 0xF) | (sum << 4)) & 0xFF;
  309. if(sum == ref_sum)
  310. furi_string_cat_printf(output, "Sum ok: 0x%hhX", ref_sum);
  311. else
  312. furi_string_cat_printf(output, "Sum err: 0x%hhX vs 0x%hhX", ref_sum, sum);
  313. }
  314. void ws_protocol_decoder_oregon2_get_string(void* context, FuriString* output) {
  315. furi_assert(context);
  316. WSProtocolDecoderOregon2* instance = context;
  317. furi_string_cat_printf(
  318. output,
  319. "%s\r\n"
  320. "ID: 0x%04lX, ch: %d, bat: %d, rc: 0x%02lX\r\n",
  321. instance->generic.protocol_name,
  322. instance->generic.id,
  323. instance->generic.channel,
  324. instance->generic.battery_low,
  325. (uint32_t)(instance->generic.data >> 4) & 0xFF);
  326. if(instance->var_bits > 0) {
  327. furi_string_cat_printf(
  328. output,
  329. "Temp:%d.%d C Hum:%d%%",
  330. (int16_t)instance->generic.temp,
  331. abs(
  332. ((int16_t)(instance->generic.temp * 10) -
  333. (((int16_t)instance->generic.temp) * 10))),
  334. instance->generic.humidity);
  335. oregon2_append_check_sum((uint32_t)instance->generic.data, instance->var_data, output);
  336. }
  337. }
  338. const SubGhzProtocolDecoder ws_protocol_oregon2_decoder = {
  339. .alloc = ws_protocol_decoder_oregon2_alloc,
  340. .free = ws_protocol_decoder_oregon2_free,
  341. .feed = ws_protocol_decoder_oregon2_feed,
  342. .reset = ws_protocol_decoder_oregon2_reset,
  343. .get_hash_data = ws_protocol_decoder_oregon2_get_hash_data,
  344. .serialize = ws_protocol_decoder_oregon2_serialize,
  345. .deserialize = ws_protocol_decoder_oregon2_deserialize,
  346. .get_string = ws_protocol_decoder_oregon2_get_string,
  347. };
  348. const SubGhzProtocol ws_protocol_oregon2 = {
  349. .name = WS_PROTOCOL_OREGON2_NAME,
  350. .type = SubGhzProtocolWeatherStation,
  351. .flag = SubGhzProtocolFlag_433 | SubGhzProtocolFlag_AM | SubGhzProtocolFlag_Decodable,
  352. .decoder = &ws_protocol_oregon2_decoder,
  353. };