|
| 1 | +/** @file |
| 2 | + Oil tank monitor using manchester encoded FSK protocol with CRC. |
| 3 | +
|
| 4 | + Copyright (C) 2022 Christian W. Zuckschwerdt <[email protected]> |
| 5 | + Device analysis by StarMonkey1 |
| 6 | +
|
| 7 | + This program is free software; you can redistribute it and/or modify |
| 8 | + it under the terms of the GNU General Public License as published by |
| 9 | + the Free Software Foundation; either version 2 of the License, or |
| 10 | + (at your option) any later version. |
| 11 | +*/ |
| 12 | + |
| 13 | +#include "decoder.h" |
| 14 | + |
| 15 | +/** |
| 16 | +Oil tank monitor using manchester encoded FSK protocol with CRC. |
| 17 | +
|
| 18 | +Tested devices: |
| 19 | +- Apollo Ultrasonic Smart liquid monitor (FSK, 433.92M) Issue #2244 |
| 20 | +
|
| 21 | +Should apply to similar Watchman, Beckett, and Apollo devices too. |
| 22 | +
|
| 23 | +There is a preamble plus de-sync of 555558, then MC coded an inner preamble of 5558 (raw 9999996a). |
| 24 | +End of frame is the last half-bit repeated additional 2 times, then 4 times mark. |
| 25 | +
|
| 26 | +The sensor sends a single packet once every half hour or twice a second |
| 27 | +for 5 minutes when in pairing/test mode. |
| 28 | +
|
| 29 | +Depth reading is in cm, lowest reading is unknown, highest is unknown, invalid is unknown. |
| 30 | +
|
| 31 | +Data Format: |
| 32 | +
|
| 33 | + PRE?16h ID?16h FLAGS?16h CM:8d CRC:8h |
| 34 | +
|
| 35 | +Data Layout: |
| 36 | +
|
| 37 | + PP PP II II FF CC DD XX |
| 38 | +
|
| 39 | +- P: 16 bit Preamble of 0x5558 |
| 40 | +- I: 16 bit Sensor ID |
| 41 | +- F: 8 bit Flags maybe |
| 42 | +- C: 8 bit Counter maybe |
| 43 | +- D: 8 bit Depth in cm, could have a MSB somewhere? |
| 44 | +- X: 8 bit CRC-8, poly 0x31 init 0x00, bit reflected |
| 45 | +
|
| 46 | +example packets are: |
| 47 | +
|
| 48 | +- raw: {158}555558 9999 996a 6559aaa99996a55696a9a5963c |
| 49 | +- aligned: {134}9999996a 6559aaa999969aa6aa9a6995 fc |
| 50 | +- decoded: 5558 bd01 5642 0497 |
| 51 | +
|
| 52 | +TODO: this is not confirmed |
| 53 | +Start of frame full preamble is depending on first data bit either |
| 54 | +
|
| 55 | + 0101 0101 0101 0101 0101 0111 01 |
| 56 | + 0101 0101 0101 0101 0101 1000 10 |
| 57 | +*/ |
| 58 | +static int oil_smart_decode(r_device *decoder, bitbuffer_t *bitbuffer, unsigned row, unsigned bitpos) |
| 59 | +{ |
| 60 | + bitbuffer_t databits = {0}; |
| 61 | + bitbuffer_manchester_decode(bitbuffer, row, bitpos, &databits, 64); |
| 62 | + |
| 63 | + if (databits.bits_per_row[0] < 64) { |
| 64 | + return 0; // DECODE_ABORT_LENGTH; // TODO: fix calling code to handle negative return values |
| 65 | + } |
| 66 | + |
| 67 | + uint8_t *b = databits.bb[0]; |
| 68 | + |
| 69 | + if (b[0] != 0x55 || b[1] != 0x58) { |
| 70 | + decoder_log(decoder, 2, __func__, "Couldn't find preamble"); |
| 71 | + return 0; // DECODE_FAIL_SANITY; // TODO: fix calling code to handle negative return values |
| 72 | + } |
| 73 | + |
| 74 | + if (crc8le(b, 8, 0x31, 0x00)) { |
| 75 | + decoder_log(decoder, 2, __func__, "CRC8 fail"); |
| 76 | + return 0; // DECODE_FAIL_MIC; // TODO: fix calling code to handle negative return values |
| 77 | + } |
| 78 | + |
| 79 | + // We do not know if the unit ID changes when you rebind |
| 80 | + // by holding a magnet to the sensor for long enough? |
| 81 | + uint16_t unit_id = (b[2] << 8) | b[3]; |
| 82 | + |
| 83 | + // TODO: none of these are identified. |
| 84 | + uint8_t unknown1 = b[4]; // idle seems to be 0x17 |
| 85 | + uint8_t unknown2 = b[5]; // appears to change variously to: 0c 0e 10 12 |
| 86 | + |
| 87 | + // TODO: the value for a bad reading has not been found? |
| 88 | + // TODO: there could be more (MSB) bits to this? |
| 89 | + uint16_t depth = b[6]; |
| 90 | + |
| 91 | + /* clang-format off */ |
| 92 | + data_t *data = data_make( |
| 93 | + "model", "", DATA_STRING, "Oil-Ultrasonic", |
| 94 | + "id", "", DATA_FORMAT, "%04x", DATA_INT, unit_id, |
| 95 | + "depth_cm", "", DATA_INT, depth, |
| 96 | + "unknown1", "", DATA_FORMAT, "%02x", DATA_INT, unknown1, |
| 97 | + "unknown2", "", DATA_FORMAT, "%02x", DATA_INT, unknown2, |
| 98 | + NULL); |
| 99 | + /* clang-format on */ |
| 100 | + |
| 101 | + decoder_output_data(decoder, data); |
| 102 | + return 1; |
| 103 | +} |
| 104 | + |
| 105 | +/** |
| 106 | +Oil tank monitor using manchester encoded FSK protocol with CRC. |
| 107 | +@sa oil_smart_decode() |
| 108 | +*/ |
| 109 | +static int oil_smart_callback(r_device *decoder, bitbuffer_t *bitbuffer) |
| 110 | +{ |
| 111 | + uint8_t const preamble_pattern[2] = {0x55, 0x58}; |
| 112 | + // End of frame is the last half-bit repeated additional 2 times, then 4 times mark. |
| 113 | + |
| 114 | + unsigned bitpos = 0; |
| 115 | + int events = 0; |
| 116 | + |
| 117 | + // Find a preamble with enough bits after it that it could be a complete packet |
| 118 | + while ((bitpos = bitbuffer_search(bitbuffer, 0, bitpos, preamble_pattern, 16)) + 128 <= |
| 119 | + bitbuffer->bits_per_row[0]) { |
| 120 | + events += oil_smart_decode(decoder, bitbuffer, 0, bitpos + 16); |
| 121 | + bitpos += 2; |
| 122 | + } |
| 123 | + |
| 124 | + return events; |
| 125 | +} |
| 126 | + |
| 127 | +static char *output_fields[] = { |
| 128 | + "model", |
| 129 | + "id", |
| 130 | + "depth_cm", |
| 131 | + "unknown1", |
| 132 | + "unknown2", |
| 133 | + NULL, |
| 134 | +}; |
| 135 | + |
| 136 | +r_device const oil_smart = { |
| 137 | + .name = "Oil Ultrasonic SMART FSK", |
| 138 | + .modulation = FSK_PULSE_PCM, |
| 139 | + .short_width = 500, |
| 140 | + .long_width = 500, |
| 141 | + .reset_limit = 2000, |
| 142 | + .decode_fn = &oil_smart_callback, |
| 143 | + .fields = output_fields, |
| 144 | +}; |
0 commit comments