oregon2.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429
  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_RTHN129_1 0x9cd3
  48. #define ID_RTHN129_2 0xacd3
  49. #define ID_RTHN129_3 0xbcd3
  50. #define ID_RTHN129_4 0xccd3
  51. #define ID_RTHN129_5 0xdcd3
  52. #define ID_BTHGN129 0x5d53 // Baro, Temp, Hygro sensor
  53. #define ID_UVR128 0xec70
  54. #define ID_THGR328N 0xcc23 // Temp & Hygro sensor similar to THR228N with 5 channel instead of 3
  55. #define ID_RTGR328N_1 0xdcc3 // RTGR328N_[1-5] RFclock(date &time)&Temp&Hygro sensor
  56. #define ID_RTGR328N_2 0xccc3
  57. #define ID_RTGR328N_3 0xbcc3
  58. #define ID_RTGR328N_4 0xacc3
  59. #define ID_RTGR328N_5 0x9cc3
  60. #define ID_RTGR328N_6 0x8ce3 // RTGR328N_6&7 RFclock(date &time)&Temp&Hygro sensor like THGR328N
  61. #define ID_RTGR328N_7 0x8ae3
  62. struct WSProtocolDecoderOregon2 {
  63. SubGhzProtocolDecoderBase base;
  64. SubGhzBlockDecoder decoder;
  65. WSBlockGeneric generic;
  66. ManchesterState manchester_state;
  67. bool prev_bit;
  68. bool have_bit;
  69. uint8_t var_bits;
  70. uint32_t var_data;
  71. };
  72. typedef struct WSProtocolDecoderOregon2 WSProtocolDecoderOregon2;
  73. typedef enum {
  74. Oregon2DecoderStepReset = 0,
  75. Oregon2DecoderStepFoundPreamble,
  76. Oregon2DecoderStepVarData,
  77. } Oregon2DecoderStep;
  78. void* ws_protocol_decoder_oregon2_alloc(SubGhzEnvironment* environment) {
  79. UNUSED(environment);
  80. WSProtocolDecoderOregon2* instance = malloc(sizeof(WSProtocolDecoderOregon2));
  81. instance->base.protocol = &ws_protocol_oregon2;
  82. instance->generic.protocol_name = instance->base.protocol->name;
  83. instance->generic.humidity = WS_NO_HUMIDITY;
  84. instance->generic.temp = WS_NO_TEMPERATURE;
  85. instance->generic.btn = WS_NO_BTN;
  86. instance->generic.channel = WS_NO_CHANNEL;
  87. instance->generic.battery_low = WS_NO_BATT;
  88. instance->generic.id = WS_NO_ID;
  89. return instance;
  90. }
  91. void ws_protocol_decoder_oregon2_free(void* context) {
  92. furi_assert(context);
  93. WSProtocolDecoderOregon2* instance = context;
  94. free(instance);
  95. }
  96. void ws_protocol_decoder_oregon2_reset(void* context) {
  97. furi_assert(context);
  98. WSProtocolDecoderOregon2* instance = context;
  99. instance->decoder.parser_step = Oregon2DecoderStepReset;
  100. instance->decoder.decode_data = 0UL;
  101. instance->decoder.decode_count_bit = 0;
  102. manchester_advance(
  103. instance->manchester_state, ManchesterEventReset, &instance->manchester_state, NULL);
  104. instance->have_bit = false;
  105. instance->var_data = 0;
  106. instance->var_bits = 0;
  107. }
  108. static ManchesterEvent level_and_duration_to_event(bool level, uint32_t duration) {
  109. bool is_long = false;
  110. if(DURATION_DIFF(duration, ws_oregon2_const.te_long) < ws_oregon2_const.te_delta) {
  111. is_long = true;
  112. } else if(DURATION_DIFF(duration, ws_oregon2_const.te_short) < ws_oregon2_const.te_delta) {
  113. is_long = false;
  114. } else {
  115. return ManchesterEventReset;
  116. }
  117. if(level)
  118. return is_long ? ManchesterEventLongHigh : ManchesterEventShortHigh;
  119. else
  120. return is_long ? ManchesterEventLongLow : ManchesterEventShortLow;
  121. }
  122. // From sensor id code return amount of bits in variable section
  123. // https://temofeev.ru/info/articles/o-dekodirovanii-protokola-pogodnykh-datchikov-oregon-scientific
  124. static uint8_t oregon2_sensor_id_var_bits(uint16_t sensor_id) {
  125. switch(sensor_id) {
  126. case ID_THR228N:
  127. case ID_RTHN129_1:
  128. case ID_RTHN129_2:
  129. case ID_RTHN129_3:
  130. case ID_RTHN129_4:
  131. case ID_RTHN129_5:
  132. return 16;
  133. case ID_THGR122N:
  134. return 24;
  135. default:
  136. return 0;
  137. }
  138. }
  139. static void ws_oregon2_decode_const_data(WSBlockGeneric* ws_block) {
  140. ws_block->id = OREGON2_SENSOR_ID(ws_block->data);
  141. uint8_t ch_bits = (ws_block->data >> 12) & 0xF;
  142. ws_block->channel = 1;
  143. while(ch_bits > 1) {
  144. ws_block->channel++;
  145. ch_bits >>= 1;
  146. }
  147. ws_block->battery_low = (ws_block->data & OREGON2_FLAG_BAT_LOW) ? 1 : 0;
  148. }
  149. uint16_t bcd_decode_short(uint32_t data) {
  150. return (data & 0xF) * 10 + ((data >> 4) & 0xF);
  151. }
  152. static float ws_oregon2_decode_temp(uint32_t data) {
  153. int32_t temp_val;
  154. temp_val = bcd_decode_short(data >> 4);
  155. temp_val *= 10;
  156. temp_val += (data >> 12) & 0xF;
  157. if(data & 0xF) temp_val = -temp_val;
  158. return (float)temp_val / 10.0;
  159. }
  160. static void ws_oregon2_decode_var_data(WSBlockGeneric* ws_b, uint16_t sensor_id, uint32_t data) {
  161. switch(sensor_id) {
  162. case ID_THR228N:
  163. case ID_RTHN129_1:
  164. case ID_RTHN129_2:
  165. case ID_RTHN129_3:
  166. case ID_RTHN129_4:
  167. case ID_RTHN129_5:
  168. ws_b->temp = ws_oregon2_decode_temp(data);
  169. ws_b->humidity = WS_NO_HUMIDITY;
  170. return;
  171. case ID_THGR122N:
  172. ws_b->humidity = bcd_decode_short(data);
  173. ws_b->temp = ws_oregon2_decode_temp(data >> 8);
  174. return;
  175. default:
  176. break;
  177. }
  178. }
  179. void ws_protocol_decoder_oregon2_feed(void* context, bool level, uint32_t duration) {
  180. furi_assert(context);
  181. WSProtocolDecoderOregon2* instance = context;
  182. // oregon v2.1 signal is inverted
  183. ManchesterEvent event = level_and_duration_to_event(!level, duration);
  184. bool data;
  185. // low-level bit sequence decoding
  186. if(event == ManchesterEventReset) {
  187. instance->decoder.parser_step = Oregon2DecoderStepReset;
  188. instance->have_bit = false;
  189. instance->decoder.decode_data = 0UL;
  190. instance->decoder.decode_count_bit = 0;
  191. }
  192. if(manchester_advance(instance->manchester_state, event, &instance->manchester_state, &data)) {
  193. if(instance->have_bit) {
  194. if(!instance->prev_bit && data) {
  195. subghz_protocol_blocks_add_bit(&instance->decoder, 1);
  196. } else if(instance->prev_bit && !data) {
  197. subghz_protocol_blocks_add_bit(&instance->decoder, 0);
  198. } else {
  199. ws_protocol_decoder_oregon2_reset(context);
  200. }
  201. instance->have_bit = false;
  202. } else {
  203. instance->prev_bit = data;
  204. instance->have_bit = true;
  205. }
  206. }
  207. switch(instance->decoder.parser_step) {
  208. case Oregon2DecoderStepReset:
  209. // waiting for fixed oregon2 preamble
  210. if(instance->decoder.decode_count_bit >= OREGON2_PREAMBLE_BITS &&
  211. ((instance->decoder.decode_data & OREGON2_PREAMBLE_MASK) == OREGON2_PREAMBLE)) {
  212. instance->decoder.parser_step = Oregon2DecoderStepFoundPreamble;
  213. instance->decoder.decode_count_bit = 0;
  214. instance->decoder.decode_data = 0UL;
  215. }
  216. break;
  217. case Oregon2DecoderStepFoundPreamble:
  218. // waiting for fixed oregon2 data
  219. if(instance->decoder.decode_count_bit == 32) {
  220. instance->generic.data = instance->decoder.decode_data;
  221. instance->generic.data_count_bit = instance->decoder.decode_count_bit;
  222. instance->decoder.decode_data = 0UL;
  223. instance->decoder.decode_count_bit = 0;
  224. // reverse nibbles in decoded data
  225. instance->generic.data = (instance->generic.data & 0x55555555) << 1 |
  226. (instance->generic.data & 0xAAAAAAAA) >> 1;
  227. instance->generic.data = (instance->generic.data & 0x33333333) << 2 |
  228. (instance->generic.data & 0xCCCCCCCC) >> 2;
  229. ws_oregon2_decode_const_data(&instance->generic);
  230. instance->var_bits =
  231. oregon2_sensor_id_var_bits(OREGON2_SENSOR_ID(instance->generic.data));
  232. if(!instance->var_bits) {
  233. // sensor is not supported, stop decoding, but showing the decoded fixed part
  234. instance->decoder.parser_step = Oregon2DecoderStepReset;
  235. if(instance->base.callback)
  236. instance->base.callback(&instance->base, instance->base.context);
  237. } else {
  238. instance->decoder.parser_step = Oregon2DecoderStepVarData;
  239. }
  240. }
  241. break;
  242. case Oregon2DecoderStepVarData:
  243. // waiting for variable (sensor-specific data)
  244. if(instance->decoder.decode_count_bit == instance->var_bits + OREGON2_CHECKSUM_BITS) {
  245. instance->var_data = instance->decoder.decode_data & 0xFFFFFFFF;
  246. // reverse nibbles in var data
  247. instance->var_data = (instance->var_data & 0x55555555) << 1 |
  248. (instance->var_data & 0xAAAAAAAA) >> 1;
  249. instance->var_data = (instance->var_data & 0x33333333) << 2 |
  250. (instance->var_data & 0xCCCCCCCC) >> 2;
  251. ws_oregon2_decode_var_data(
  252. &instance->generic,
  253. OREGON2_SENSOR_ID(instance->generic.data),
  254. instance->var_data >> OREGON2_CHECKSUM_BITS);
  255. instance->decoder.parser_step = Oregon2DecoderStepReset;
  256. if(instance->base.callback)
  257. instance->base.callback(&instance->base, instance->base.context);
  258. }
  259. break;
  260. }
  261. }
  262. uint8_t ws_protocol_decoder_oregon2_get_hash_data(void* context) {
  263. furi_assert(context);
  264. WSProtocolDecoderOregon2* instance = context;
  265. return subghz_protocol_blocks_get_hash_data(
  266. &instance->decoder, (instance->decoder.decode_count_bit / 8) + 1);
  267. }
  268. SubGhzProtocolStatus ws_protocol_decoder_oregon2_serialize(
  269. void* context,
  270. FlipperFormat* flipper_format,
  271. SubGhzRadioPreset* preset) {
  272. furi_assert(context);
  273. WSProtocolDecoderOregon2* instance = context;
  274. SubGhzProtocolStatus ret = SubGhzProtocolStatusError;
  275. ret = ws_block_generic_serialize(&instance->generic, flipper_format, preset);
  276. if(ret != SubGhzProtocolStatusOk) return ret;
  277. uint32_t temp = instance->var_bits;
  278. if(!flipper_format_write_uint32(flipper_format, "VarBits", &temp, 1)) {
  279. FURI_LOG_E(TAG, "Error adding VarBits");
  280. return SubGhzProtocolStatusErrorParserOthers;
  281. }
  282. if(!flipper_format_write_hex(
  283. flipper_format,
  284. "VarData",
  285. (const uint8_t*)&instance->var_data,
  286. sizeof(instance->var_data))) {
  287. FURI_LOG_E(TAG, "Error adding VarData");
  288. return SubGhzProtocolStatusErrorParserOthers;
  289. }
  290. return ret;
  291. }
  292. SubGhzProtocolStatus
  293. ws_protocol_decoder_oregon2_deserialize(void* context, FlipperFormat* flipper_format) {
  294. furi_assert(context);
  295. WSProtocolDecoderOregon2* instance = context;
  296. uint32_t temp_data;
  297. SubGhzProtocolStatus ret = SubGhzProtocolStatusError;
  298. do {
  299. ret = ws_block_generic_deserialize(&instance->generic, flipper_format);
  300. if(ret != SubGhzProtocolStatusOk) {
  301. break;
  302. }
  303. if(!flipper_format_read_uint32(flipper_format, "VarBits", &temp_data, 1)) {
  304. FURI_LOG_E(TAG, "Missing VarLen");
  305. ret = SubGhzProtocolStatusErrorParserOthers;
  306. break;
  307. }
  308. instance->var_bits = (uint8_t)temp_data;
  309. if(!flipper_format_read_hex(
  310. flipper_format,
  311. "VarData",
  312. (uint8_t*)&instance->var_data,
  313. sizeof(instance->var_data))) { //-V1051
  314. FURI_LOG_E(TAG, "Missing VarData");
  315. ret = SubGhzProtocolStatusErrorParserOthers;
  316. break;
  317. }
  318. if(instance->generic.data_count_bit != ws_oregon2_const.min_count_bit_for_found) {
  319. FURI_LOG_E(TAG, "Wrong number of bits in key: %d", instance->generic.data_count_bit);
  320. ret = SubGhzProtocolStatusErrorValueBitCount;
  321. break;
  322. }
  323. } while(false);
  324. return ret;
  325. }
  326. static void oregon2_append_check_sum(uint32_t fix_data, uint32_t var_data, FuriString* output) {
  327. uint8_t sum = fix_data & 0xF;
  328. uint8_t ref_sum = var_data & 0xFF;
  329. var_data >>= 8;
  330. for(uint8_t i = 1; i < 8; i++) {
  331. fix_data >>= 4;
  332. var_data >>= 4;
  333. sum += (fix_data & 0xF) + (var_data & 0xF);
  334. }
  335. // swap calculated sum nibbles
  336. sum = (((sum >> 4) & 0xF) | (sum << 4)) & 0xFF;
  337. if(sum == ref_sum)
  338. furi_string_cat_printf(output, "Sum ok: 0x%hhX", ref_sum);
  339. else
  340. furi_string_cat_printf(output, "Sum err: 0x%hhX vs 0x%hhX", ref_sum, sum);
  341. }
  342. void ws_protocol_decoder_oregon2_get_string(void* context, FuriString* output) {
  343. furi_assert(context);
  344. WSProtocolDecoderOregon2* instance = context;
  345. furi_string_cat_printf(
  346. output,
  347. "%s\r\n"
  348. "ID: 0x%04lX, ch: %d, bat: %d, rc: 0x%02lX\r\n",
  349. instance->generic.protocol_name,
  350. instance->generic.id,
  351. instance->generic.channel,
  352. instance->generic.battery_low,
  353. (uint32_t)(instance->generic.data >> 4) & 0xFF);
  354. if(instance->var_bits > 0) {
  355. furi_string_cat_printf(
  356. output,
  357. "Temp:%d.%d C Hum:%d%%",
  358. (int16_t)instance->generic.temp,
  359. abs(
  360. ((int16_t)(instance->generic.temp * 10) -
  361. (((int16_t)instance->generic.temp) * 10))),
  362. instance->generic.humidity);
  363. oregon2_append_check_sum((uint32_t)instance->generic.data, instance->var_data, output);
  364. }
  365. }
  366. const SubGhzProtocolDecoder ws_protocol_oregon2_decoder = {
  367. .alloc = ws_protocol_decoder_oregon2_alloc,
  368. .free = ws_protocol_decoder_oregon2_free,
  369. .feed = ws_protocol_decoder_oregon2_feed,
  370. .reset = ws_protocol_decoder_oregon2_reset,
  371. .get_hash_data = ws_protocol_decoder_oregon2_get_hash_data,
  372. .serialize = ws_protocol_decoder_oregon2_serialize,
  373. .deserialize = ws_protocol_decoder_oregon2_deserialize,
  374. .get_string = ws_protocol_decoder_oregon2_get_string,
  375. };
  376. const SubGhzProtocol ws_protocol_oregon2 = {
  377. .name = WS_PROTOCOL_OREGON2_NAME,
  378. .type = SubGhzProtocolWeatherStation,
  379. .flag = SubGhzProtocolFlag_433 | SubGhzProtocolFlag_AM | SubGhzProtocolFlag_Decodable,
  380. .decoder = &ws_protocol_oregon2_decoder,
  381. };