oregon2.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  1. #include "oregon2.h"
  2. #include "../blocks/const.h"
  3. #include "../blocks/decoder.h"
  4. #include "../blocks/generic.h"
  5. #include "../blocks/math.h"
  6. #include <lib/toolbox/manchester_decoder.h>
  7. #include <lib/flipper_format/flipper_format_i.h>
  8. #define TAG "SubGhzProtocolOregon2"
  9. static const SubGhzBlockConst oregon2_const = {
  10. .te_long = 1000,
  11. .te_short = 500,
  12. .te_delta = 200,
  13. .min_count_bit_for_found = 32,
  14. };
  15. #define OREGON2_PREAMBLE_BITS 19
  16. #define OREGON2_PREAMBLE_MASK ((1 << (OREGON2_PREAMBLE_BITS + 1)) - 1)
  17. #define OREGON2_SENSOR_ID(d) (((d) >> 16) & 0xFFFF)
  18. #define OREGON2_CHECKSUM_BITS 8
  19. // 15 ones + 0101 (inverted A)
  20. #define OREGON2_PREAMBLE 0b1111111111111110101
  21. // bit indicating the low battery
  22. #define OREGON2_FLAG_BAT_LOW 0x4
  23. struct SubGhzProtocolDecoderOregon2 {
  24. SubGhzProtocolDecoderBase base;
  25. SubGhzBlockDecoder decoder;
  26. SubGhzBlockGeneric generic;
  27. ManchesterState manchester_state;
  28. bool prev_bit;
  29. bool have_bit;
  30. uint8_t var_bits;
  31. uint32_t var_data;
  32. };
  33. typedef struct SubGhzProtocolDecoderOregon2 SubGhzProtocolDecoderOregon2;
  34. typedef enum {
  35. Oregon2DecoderStepReset = 0,
  36. Oregon2DecoderStepFoundPreamble,
  37. Oregon2DecoderStepVarData,
  38. } Oregon2DecoderStep;
  39. void* subghz_protocol_decoder_oregon2_alloc(SubGhzEnvironment* environment) {
  40. UNUSED(environment);
  41. SubGhzProtocolDecoderOregon2* instance = malloc(sizeof(SubGhzProtocolDecoderOregon2));
  42. instance->base.protocol = &subghz_protocol_oregon2;
  43. instance->generic.protocol_name = instance->base.protocol->name;
  44. return instance;
  45. }
  46. void subghz_protocol_decoder_oregon2_free(void* context) {
  47. furi_assert(context);
  48. SubGhzProtocolDecoderOregon2* instance = context;
  49. free(instance);
  50. }
  51. void subghz_protocol_decoder_oregon2_reset(void* context) {
  52. furi_assert(context);
  53. SubGhzProtocolDecoderOregon2* instance = context;
  54. instance->decoder.parser_step = Oregon2DecoderStepReset;
  55. instance->decoder.decode_data = 0UL;
  56. instance->decoder.decode_count_bit = 0;
  57. manchester_advance(
  58. instance->manchester_state, ManchesterEventReset, &instance->manchester_state, NULL);
  59. instance->have_bit = false;
  60. instance->var_data = 0;
  61. instance->var_bits = 0;
  62. }
  63. static ManchesterEvent level_and_duration_to_event(bool level, uint32_t duration) {
  64. bool is_long = false;
  65. if(DURATION_DIFF(duration, oregon2_const.te_long) < oregon2_const.te_delta) {
  66. is_long = true;
  67. } else if(DURATION_DIFF(duration, oregon2_const.te_short) < oregon2_const.te_delta) {
  68. is_long = false;
  69. } else {
  70. return ManchesterEventReset;
  71. }
  72. if(level)
  73. return is_long ? ManchesterEventLongHigh : ManchesterEventShortHigh;
  74. else
  75. return is_long ? ManchesterEventLongLow : ManchesterEventShortLow;
  76. }
  77. // From sensor id code return amount of bits in variable section
  78. static uint8_t oregon2_sensor_id_var_bits(uint16_t sensor_id) {
  79. if(sensor_id == 0xEC40) return 16;
  80. return 0;
  81. }
  82. void subghz_protocol_decoder_oregon2_feed(void* context, bool level, uint32_t duration) {
  83. furi_assert(context);
  84. SubGhzProtocolDecoderOregon2* instance = context;
  85. // oregon v2.1 signal is inverted
  86. ManchesterEvent event = level_and_duration_to_event(!level, duration);
  87. bool data;
  88. // low-level bit sequence decoding
  89. if(event == ManchesterEventReset) {
  90. instance->decoder.parser_step = Oregon2DecoderStepReset;
  91. instance->have_bit = false;
  92. instance->decoder.decode_data = 0UL;
  93. instance->decoder.decode_count_bit = 0;
  94. }
  95. if(manchester_advance(instance->manchester_state, event, &instance->manchester_state, &data)) {
  96. if(instance->have_bit) {
  97. if(!instance->prev_bit && data) {
  98. subghz_protocol_blocks_add_bit(&instance->decoder, 1);
  99. } else if(instance->prev_bit && !data) {
  100. subghz_protocol_blocks_add_bit(&instance->decoder, 0);
  101. } else {
  102. subghz_protocol_decoder_oregon2_reset(context);
  103. }
  104. instance->have_bit = false;
  105. } else {
  106. instance->prev_bit = data;
  107. instance->have_bit = true;
  108. }
  109. }
  110. switch(instance->decoder.parser_step) {
  111. case Oregon2DecoderStepReset:
  112. // waiting for fixed oregon2 preamble
  113. if(instance->decoder.decode_count_bit >= OREGON2_PREAMBLE_BITS &&
  114. ((instance->decoder.decode_data & OREGON2_PREAMBLE_MASK) == OREGON2_PREAMBLE)) {
  115. instance->decoder.parser_step = Oregon2DecoderStepFoundPreamble;
  116. instance->decoder.decode_count_bit = 0;
  117. instance->decoder.decode_data = 0UL;
  118. }
  119. break;
  120. case Oregon2DecoderStepFoundPreamble:
  121. // waiting for fixed oregon2 data
  122. if(instance->decoder.decode_count_bit == 32) {
  123. instance->generic.data = instance->decoder.decode_data;
  124. instance->generic.data_count_bit = instance->decoder.decode_count_bit;
  125. instance->decoder.decode_data = 0UL;
  126. instance->decoder.decode_count_bit = 0;
  127. // reverse nibbles in decoded data
  128. instance->generic.data = (instance->generic.data & 0x55555555) << 1 |
  129. (instance->generic.data & 0xAAAAAAAA) >> 1;
  130. instance->generic.data = (instance->generic.data & 0x33333333) << 2 |
  131. (instance->generic.data & 0xCCCCCCCC) >> 2;
  132. instance->var_bits =
  133. oregon2_sensor_id_var_bits(OREGON2_SENSOR_ID(instance->generic.data));
  134. if(!instance->var_bits) {
  135. // sensor is not supported, stop decoding, but showing the decoded fixed part
  136. instance->decoder.parser_step = Oregon2DecoderStepReset;
  137. if(instance->base.callback)
  138. instance->base.callback(&instance->base, instance->base.context);
  139. } else {
  140. instance->decoder.parser_step = Oregon2DecoderStepVarData;
  141. }
  142. }
  143. break;
  144. case Oregon2DecoderStepVarData:
  145. // waiting for variable (sensor-specific data)
  146. if(instance->decoder.decode_count_bit == instance->var_bits + OREGON2_CHECKSUM_BITS) {
  147. instance->var_data = instance->decoder.decode_data & 0xFFFFFFFF;
  148. // reverse nibbles in var data
  149. instance->var_data = (instance->var_data & 0x55555555) << 1 |
  150. (instance->var_data & 0xAAAAAAAA) >> 1;
  151. instance->var_data = (instance->var_data & 0x33333333) << 2 |
  152. (instance->var_data & 0xCCCCCCCC) >> 2;
  153. instance->decoder.parser_step = Oregon2DecoderStepReset;
  154. if(instance->base.callback)
  155. instance->base.callback(&instance->base, instance->base.context);
  156. }
  157. break;
  158. }
  159. }
  160. uint8_t subghz_protocol_decoder_oregon2_get_hash_data(void* context) {
  161. furi_assert(context);
  162. SubGhzProtocolDecoderOregon2* instance = context;
  163. return subghz_protocol_blocks_get_hash_data(
  164. &instance->decoder, (instance->decoder.decode_count_bit / 8) + 1);
  165. }
  166. bool subghz_protocol_decoder_oregon2_serialize(
  167. void* context,
  168. FlipperFormat* flipper_format,
  169. SubGhzPresetDefinition* preset) {
  170. furi_assert(context);
  171. SubGhzProtocolDecoderOregon2* instance = context;
  172. if(!subghz_block_generic_serialize(&instance->generic, flipper_format, preset)) return false;
  173. uint32_t temp = instance->var_bits;
  174. if(!flipper_format_write_uint32(flipper_format, "VarBits", &temp, 1)) {
  175. FURI_LOG_E(TAG, "Error adding VarBits");
  176. return false;
  177. }
  178. if(!flipper_format_write_hex(
  179. flipper_format,
  180. "VarData",
  181. (const uint8_t*)&instance->var_data,
  182. sizeof(instance->var_data))) {
  183. FURI_LOG_E(TAG, "Error adding VarData");
  184. return false;
  185. }
  186. return true;
  187. }
  188. bool subghz_protocol_decoder_oregon2_deserialize(void* context, FlipperFormat* flipper_format) {
  189. furi_assert(context);
  190. SubGhzProtocolDecoderOregon2* instance = context;
  191. bool ret = false;
  192. uint32_t temp_data;
  193. do {
  194. if(!subghz_block_generic_deserialize(&instance->generic, flipper_format)) {
  195. break;
  196. }
  197. if(!flipper_format_read_uint32(flipper_format, "VarBits", &temp_data, 1)) {
  198. FURI_LOG_E(TAG, "Missing VarLen");
  199. break;
  200. }
  201. instance->var_bits = (uint8_t)temp_data;
  202. if(!flipper_format_read_hex(
  203. flipper_format,
  204. "VarData",
  205. (uint8_t*)&instance->var_data,
  206. sizeof(instance->var_data))) {
  207. FURI_LOG_E(TAG, "Missing VarData");
  208. break;
  209. }
  210. if(instance->generic.data_count_bit != oregon2_const.min_count_bit_for_found) {
  211. FURI_LOG_E(TAG, "Wrong number of bits in key: %d", instance->generic.data_count_bit);
  212. break;
  213. }
  214. ret = true;
  215. } while(false);
  216. return ret;
  217. }
  218. // append string of the variable data
  219. static void
  220. oregon2_var_data_append_string(uint16_t sensor_id, uint32_t var_data, FuriString* output) {
  221. uint32_t val;
  222. if(sensor_id == 0xEC40) {
  223. val = ((var_data >> 4) & 0xF) * 10 + ((var_data >> 8) & 0xF);
  224. furi_string_cat_printf(
  225. output,
  226. "Temp: %s%ld.%ld C\r\n",
  227. (var_data & 0xF) ? "-" : "+",
  228. val,
  229. (uint32_t)(var_data >> 12) & 0xF);
  230. }
  231. }
  232. static void oregon2_append_check_sum(uint32_t fix_data, uint32_t var_data, FuriString* output) {
  233. uint8_t sum = fix_data & 0xF;
  234. uint8_t ref_sum = var_data & 0xFF;
  235. var_data >>= 8;
  236. for(uint8_t i = 1; i < 8; i++) {
  237. fix_data >>= 4;
  238. var_data >>= 4;
  239. sum += (fix_data & 0xF) + (var_data & 0xF);
  240. }
  241. // swap calculated sum nibbles
  242. sum = (((sum >> 4) & 0xF) | (sum << 4)) & 0xFF;
  243. if(sum == ref_sum)
  244. furi_string_cat_printf(output, "Sum ok: 0x%hhX", ref_sum);
  245. else
  246. furi_string_cat_printf(output, "Sum err: 0x%hhX vs 0x%hhX", ref_sum, sum);
  247. }
  248. void subghz_protocol_decoder_oregon2_get_string(void* context, FuriString* output) {
  249. furi_assert(context);
  250. SubGhzProtocolDecoderOregon2* instance = context;
  251. uint16_t sensor_id = OREGON2_SENSOR_ID(instance->generic.data);
  252. furi_string_cat_printf(
  253. output,
  254. "%s\r\n"
  255. "ID: 0x%04lX, ch: %ld%s, rc: 0x%02lX\r\n",
  256. instance->generic.protocol_name,
  257. (uint32_t)sensor_id,
  258. (uint32_t)(instance->generic.data >> 12) & 0xF,
  259. ((instance->generic.data & OREGON2_FLAG_BAT_LOW) ? ", low bat" : ""),
  260. (uint32_t)(instance->generic.data >> 4) & 0xFF);
  261. if(instance->var_bits > 0) {
  262. oregon2_var_data_append_string(
  263. sensor_id, instance->var_data >> OREGON2_CHECKSUM_BITS, output);
  264. oregon2_append_check_sum((uint32_t)instance->generic.data, instance->var_data, output);
  265. }
  266. }
  267. const SubGhzProtocolDecoder subghz_protocol_oregon2_decoder = {
  268. .alloc = subghz_protocol_decoder_oregon2_alloc,
  269. .free = subghz_protocol_decoder_oregon2_free,
  270. .feed = subghz_protocol_decoder_oregon2_feed,
  271. .reset = subghz_protocol_decoder_oregon2_reset,
  272. .get_hash_data = subghz_protocol_decoder_oregon2_get_hash_data,
  273. .serialize = subghz_protocol_decoder_oregon2_serialize,
  274. .deserialize = subghz_protocol_decoder_oregon2_deserialize,
  275. .get_string = subghz_protocol_decoder_oregon2_get_string,
  276. };
  277. const SubGhzProtocol subghz_protocol_oregon2 = {
  278. .name = SUBGHZ_PROTOCOL_OREGON2_NAME,
  279. .type = SubGhzProtocolTypeStatic,
  280. .flag = SubGhzProtocolFlag_433 | SubGhzProtocolFlag_AM | SubGhzProtocolFlag_Decodable |
  281. SubGhzProtocolFlag_Load | SubGhzProtocolFlag_Save,
  282. .decoder = &subghz_protocol_oregon2_decoder,
  283. };