|
| 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 | +FIXME: confirm this |
| 27 | +The sensor sends a single packet once every hour or twice a second |
| 28 | +for 11 minutes when in pairing/test mode (pairing needs 35 sec). |
| 29 | +depth reading is in cm, lowest reading is ~3, highest is ~305, 0 is invalid |
| 30 | +
|
| 31 | +Data Format: |
| 32 | +
|
| 33 | + PRE?16h ID?16h CTR?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 6559aaa999969aa6aa9a6995fc |
| 50 | +
|
| 51 | +FIXME: this is not confirmed |
| 52 | +Start of frame full preamble is depending on first data bit either |
| 53 | +
|
| 54 | + 0101 0101 0101 0101 0101 0111 01 |
| 55 | + 0101 0101 0101 0101 0101 1000 10 |
| 56 | +*/ |
| 57 | +static int oil_smart_decode(r_device *decoder, bitbuffer_t *bitbuffer, unsigned row, unsigned bitpos) |
| 58 | +{ |
| 59 | + bitbuffer_t databits = {0}; |
| 60 | + bitbuffer_manchester_decode(bitbuffer, row, bitpos, &databits, 64); |
| 61 | + |
| 62 | + if (databits.bits_per_row[0] < 64) { |
| 63 | + return 0; // DECODE_ABORT_LENGTH; // TODO: fix calling code to handle negative return values |
| 64 | + } |
| 65 | + |
| 66 | + uint8_t *b = databits.bb[0]; |
| 67 | + |
| 68 | + if (b[0] != 0x55 || b[1] != 0x58) { |
| 69 | + decoder_log(decoder, 2, __func__, "Couldn't find preamble"); |
| 70 | + return 0; // DECODE_FAIL_SANITY; // TODO: fix calling code to handle negative return values |
| 71 | + } |
| 72 | + |
| 73 | + if (crc8le(b, 8, 0x31, 0x00)) { |
| 74 | + decoder_log(decoder, 2, __func__, "CRC8 fail"); |
| 75 | + return 0; // DECODE_FAIL_MIC; // TODO: fix calling code to handle negative return values |
| 76 | + } |
| 77 | + |
| 78 | + // FIXME: does not seem to be the case here |
| 79 | + // The unit ID changes when you rebind by holding a magnet to the |
| 80 | + // sensor for long enough. |
| 81 | + uint16_t unit_id = (b[2] << 8) | b[3]; |
| 82 | + |
| 83 | + // FIXME: none of these are confirmed. |
| 84 | + // 0x01: Rebinding (magnet held to sensor) |
| 85 | + // 0x02: High-bit for depth |
| 86 | + // 0x04: (always zero?) |
| 87 | + // 0x08: Leak/theft alarm |
| 88 | + // 0x10: (unknown toggle) |
| 89 | + // 0x20: (unknown toggle) |
| 90 | + // 0x40: (unknown toggle) |
| 91 | + // 0x80: (always zero?) |
| 92 | + uint8_t flags = b[4]; |
| 93 | + uint8_t alarm = b[5]; |
| 94 | + |
| 95 | + // FIXME: confirm there is a binding counter |
| 96 | + uint16_t depth = 0; |
| 97 | + uint16_t binding_countdown = 0; |
| 98 | + if (flags & 1) { |
| 99 | + // When binding, the countdown counts up from 0x40 to 0x4a |
| 100 | + // (as long as you hold the magnet to it for long enough) |
| 101 | + // before the device ID changes. The receiver unit needs |
| 102 | + // to receive this *strongly* in order to change its |
| 103 | + // allegiance. |
| 104 | + binding_countdown = b[6]; |
| 105 | + } |
| 106 | + else { |
| 107 | + // A depth reading of zero indicates no reading. |
| 108 | + //depth = ((b[5] & 0x02) << 7) | b[6]; |
| 109 | + depth = b[6]; |
| 110 | + } |
| 111 | + |
| 112 | + /* clang-format off */ |
| 113 | + data_t *data = data_make( |
| 114 | + "model", "", DATA_STRING, "Oil-SonicSmart", |
| 115 | + "id", "", DATA_FORMAT, "%04x", DATA_INT, unit_id, |
| 116 | + "flags", "", DATA_FORMAT, "%02x", DATA_INT, flags, |
| 117 | + "alarm", "", DATA_FORMAT, "%02x", DATA_INT, alarm, |
| 118 | + "binding_countdown", "", DATA_INT, binding_countdown, |
| 119 | + "depth_cm", "", DATA_INT, depth, |
| 120 | + NULL); |
| 121 | + /* clang-format on */ |
| 122 | + |
| 123 | + decoder_output_data(decoder, data); |
| 124 | + return 1; |
| 125 | +} |
| 126 | + |
| 127 | +/** |
| 128 | +Oil tank monitor using manchester encoded FSK protocol with CRC. |
| 129 | +@sa oil_smart_decode() |
| 130 | +*/ |
| 131 | +static int oil_smart_callback(r_device *decoder, bitbuffer_t *bitbuffer) |
| 132 | +{ |
| 133 | + uint8_t const preamble_pattern[2] = {0x55, 0x58}; |
| 134 | + // End of frame is the last half-bit repeated additional 2 times, then 4 times mark. |
| 135 | + |
| 136 | + unsigned bitpos = 0; |
| 137 | + int events = 0; |
| 138 | + |
| 139 | + // Find a preamble with enough bits after it that it could be a complete packet |
| 140 | + while ((bitpos = bitbuffer_search(bitbuffer, 0, bitpos, preamble_pattern, 16)) + 128 <= |
| 141 | + bitbuffer->bits_per_row[0]) { |
| 142 | + events += oil_smart_decode(decoder, bitbuffer, 0, bitpos + 16); |
| 143 | + bitpos += 2; |
| 144 | + } |
| 145 | + |
| 146 | + return events; |
| 147 | +} |
| 148 | + |
| 149 | +static char *output_fields[] = { |
| 150 | + "model", |
| 151 | + "id", |
| 152 | + "flags", |
| 153 | + "alarm", |
| 154 | + "binding_countdown", |
| 155 | + "depth_cm", |
| 156 | + NULL, |
| 157 | +}; |
| 158 | + |
| 159 | +r_device oil_smart = { |
| 160 | + .name = "Oil Ultrasonic SMART FSK", |
| 161 | + .modulation = FSK_PULSE_PCM, |
| 162 | + .short_width = 500, |
| 163 | + .long_width = 500, |
| 164 | + .reset_limit = 2000, |
| 165 | + .decode_fn = &oil_smart_callback, |
| 166 | + .fields = output_fields, |
| 167 | +}; |
0 commit comments