Skip to content

Commit

Permalink
Fix start/len reporting in all the decoders.
Browse files Browse the repository at this point in the history
  • Loading branch information
antirez committed Jan 18, 2023
1 parent 8fe8d93 commit 0f8ef0a
Show file tree
Hide file tree
Showing 10 changed files with 43 additions and 12 deletions.
2 changes: 1 addition & 1 deletion app.h
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ void scan_for_signal(ProtoViewApp *app);
bool bitmap_get(uint8_t *b, uint32_t blen, uint32_t bitpos);
void bitmap_set(uint8_t *b, uint32_t blen, uint32_t bitpos, bool val);
void bitmap_copy(uint8_t *d, uint32_t dlen, uint32_t doff, uint8_t *s, uint32_t slen, uint32_t soff, uint32_t count);
void bitmap_set_pattern(uint8_t *b, uint32_t blen, const char *pat);
void bitmap_set_pattern(uint8_t *b, uint32_t blen, uint32_t off, const char *pat);
void bitmap_reverse_bytes(uint8_t *p, uint32_t len);
bool bitmap_match_bits(uint8_t *b, uint32_t blen, uint32_t bitpos, const char *bits);
uint32_t bitmap_seek_bits(uint8_t *b, uint32_t blen, uint32_t startpos, uint32_t maxbits, const char *bits);
Expand Down
4 changes: 1 addition & 3 deletions protocols/keeloq.c
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,9 @@ static bool decode(uint8_t *bits, uint32_t numbytes, uint32_t numbits, ProtoView
convert_from_line_code(raw,sizeof(raw),bits,numbytes,off,
"110","100"); /* Pulse width modulation. */
FURI_LOG_E(TAG, "Keeloq decoded bits: %lu", decoded);

if (decoded < 66) return false; /* Require the full 66 bits. */

off += 66*3; // Seek end to compute total length.
info->pulses_count = off - info->start_off;
info->pulses_count = (off+66*3) - info->start_off;

bitmap_reverse_bytes(raw,sizeof(raw)); /* Keeloq is LSB first. */

Expand Down
2 changes: 2 additions & 0 deletions protocols/oregon2.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ static bool decode(uint8_t *bits, uint32_t numbytes, uint32_t numbits, ProtoView
if (off == BITMAP_SEEK_NOT_FOUND) return false;
FURI_LOG_E(TAG, "Oregon2 preamble+sync found");

info->start_off = off;
off += 32; /* Skip preamble. */

uint8_t buffer[8], raw[8] = {0};
Expand All @@ -21,6 +22,7 @@ static bool decode(uint8_t *bits, uint32_t numbytes, uint32_t numbits, ProtoView
FURI_LOG_E(TAG, "Oregon2 decoded bits: %lu", decoded);

if (decoded < 11*4) return false; /* Minimum len to extract some data. */
info->pulses_count = (off+11*4*4) - info->start_off;

char temp[3] = {0}, deviceid[2] = {0}, hum[2] = {0};
for (int j = 0; j < 64; j += 4) {
Expand Down
3 changes: 3 additions & 0 deletions protocols/tpms/citroen.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ static bool decode(uint8_t *bits, uint32_t numbytes, uint32_t numbits, ProtoView
if (off == BITMAP_SEEK_NOT_FOUND) return false;
FURI_LOG_E(TAG, "Renault TPMS preamble+sync found");

info->start_off = off;
off += sync_len; /* Skip preamble + sync. */

uint8_t raw[10];
Expand All @@ -37,6 +38,8 @@ static bool decode(uint8_t *bits, uint32_t numbytes, uint32_t numbits, ProtoView
for (int j = 1; j < 10; j++) crc ^= raw[j];
if (crc != 0) return false; /* Require sane checksum. */

info->pulses_count = (off+8*10*2) - info->start_off;

int repeat = raw[5] & 0xf;
float kpa = (float)raw[6]*1.364;
int temp = raw[7]-50;
Expand Down
3 changes: 3 additions & 0 deletions protocols/tpms/ford.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ static bool decode(uint8_t *bits, uint32_t numbytes, uint32_t numbits, ProtoView
if (off == BITMAP_SEEK_NOT_FOUND) return false;
FURI_LOG_E(TAG, "Fort TPMS preamble+sync found");

info->start_off = off;
off += sync_len; /* Skip preamble and sync. */

uint8_t raw[8];
Expand All @@ -35,6 +36,8 @@ static bool decode(uint8_t *bits, uint32_t numbytes, uint32_t numbits, ProtoView
for (int j = 0; j < 7; j++) crc += raw[j];
if (crc != raw[7]) return false; /* Require sane CRC. */

info->pulses_count = (off+8*8*2) - info->start_off;

float psi = 0.25 * (((raw[6]&0x20)<<3)|raw[4]);

/* Temperature apperas to be valid only if the most significant
Expand Down
5 changes: 4 additions & 1 deletion protocols/tpms/renault.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ static const char *test_vector =
static bool decode(uint8_t *bits, uint32_t numbytes, uint32_t numbits, ProtoViewMsgInfo *info) {

if (USE_TEST_VECTOR) { /* Test vector to check that decoding works. */
bitmap_set_pattern(bits,numbytes,test_vector);
bitmap_set_pattern(bits,numbytes,0,test_vector);
numbits = strlen(test_vector);
}

Expand All @@ -36,6 +36,7 @@ static bool decode(uint8_t *bits, uint32_t numbytes, uint32_t numbits, ProtoView
if (off == BITMAP_SEEK_NOT_FOUND) return false;
FURI_LOG_E(TAG, "Renault TPMS preamble+sync found");

info->start_off = off;
off += 20; /* Skip preamble. */

uint8_t raw[9];
Expand All @@ -47,6 +48,8 @@ static bool decode(uint8_t *bits, uint32_t numbytes, uint32_t numbits, ProtoView
if (decoded < 8*9) return false; /* Require the full 9 bytes. */
if (crc8(raw,8,0,7) != raw[8]) return false; /* Require sane CRC. */

info->pulses_count = (off+8*9*2) - info->start_off;

float kpa = 0.75 *((uint32_t)((raw[0]&3)<<8) | raw[1]);
int temp = raw[2]-30;

Expand Down
5 changes: 4 additions & 1 deletion protocols/tpms/schrader.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ static const char *test_vector = "0000001111010101010110100101100101101010010101
static bool decode(uint8_t *bits, uint32_t numbytes, uint32_t numbits, ProtoViewMsgInfo *info) {

if (USE_TEST_VECTOR) { /* Test vector to check that decoding works. */
bitmap_set_pattern(bits,numbytes,test_vector);
bitmap_set_pattern(bits,numbytes,0,test_vector);
numbits = strlen(test_vector);
}

Expand All @@ -27,6 +27,7 @@ static bool decode(uint8_t *bits, uint32_t numbytes, uint32_t numbits, ProtoView
if (off == BITMAP_SEEK_NOT_FOUND) return false;
FURI_LOG_E(TAG, "Schrader TPMS gap+preamble found");

info->start_off = off;
off += 10; /* Skip just the long pulse and the first 3 bits of sync, so
that we have the first byte of data with the sync nibble
0011 = 0x3. */
Expand All @@ -46,6 +47,8 @@ static bool decode(uint8_t *bits, uint32_t numbytes, uint32_t numbits, ProtoView
return false;
}

info->pulses_count = (off+8*8*2) - info->start_off;

float kpa = (float)raw[5]*2.5;
int temp = raw[6]-50;

Expand Down
3 changes: 3 additions & 0 deletions protocols/tpms/schrader_eg53ma4.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ static bool decode(uint8_t *bits, uint32_t numbytes, uint32_t numbits, ProtoView
if (off == BITMAP_SEEK_NOT_FOUND) return false;
FURI_LOG_E(TAG, "Schrader EG53MA4 TPMS preamble+sync found");

info->start_off = off;
off += sync_len-8; /* Skip preamble, not sync that is part of the data. */

uint8_t raw[10];
Expand All @@ -40,6 +41,8 @@ static bool decode(uint8_t *bits, uint32_t numbytes, uint32_t numbits, ProtoView
for (int j = 0; j < 9; j++) crc += raw[j];
if (crc != raw[9]) return false; /* Require sane CRC. */

info->pulses_count = (off+10*8*2) - info->start_off;

/* To convert the raw pressure to kPa, RTL433 uses 2.5, but is likely
* wrong. Searching on Google for users experimenting with the value
* reported, the value appears to be 2.75. */
Expand Down
18 changes: 16 additions & 2 deletions protocols/tpms/toyota.c
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ static bool decode(uint8_t *bits, uint32_t numbytes, uint32_t numbits, ProtoView
for (j = 0; sync[j]; j++) {
off = bitmap_seek_bits(bits,numbytes,0,numbits,sync[j]);
if (off != BITMAP_SEEK_NOT_FOUND) {
info->start_off = off;
off += strlen(sync[j])-2;
break;
}
Expand All @@ -58,6 +59,19 @@ static bool decode(uint8_t *bits, uint32_t numbytes, uint32_t numbits, ProtoView
if (decoded < 8*9) return false; /* Require the full 8 bytes. */
if (crc8(raw,8,0x80,7) != raw[8]) return false; /* Require sane CRC. */

/* We detected a valid signal. However now info->start_off is actually
* pointing to the sync part, not the preamble of alternating 0 and 1.
* Protoview decoders get called with some space to the left, in order
* for the decoder itself to fix the signal if neeeded, so that its
* logical representation will be more accurate and better to save
* and retransmit. */
if (info->start_off >= 12) {
info->start_off -= 12;
bitmap_set_pattern(bits,numbytes,info->start_off,"010101010101");
}

info->pulses_count = (off+8*9*2) - info->start_off;

float kpa = (float)((raw[4]&0x7f)<<1 | raw[5]>>7) * 0.25 - 7;
int temp = ((raw[5]&0x7f)<<1 | raw[6]>>7) - 40;

Expand All @@ -67,8 +81,8 @@ static bool decode(uint8_t *bits, uint32_t numbytes, uint32_t numbits, ProtoView
raw[6],raw[7],raw[8]);
snprintf(info->info1,sizeof(info->info1),"Tire ID %02X%02X%02X%02X",
raw[0],raw[1],raw[2],raw[3]);
snprintf(info->info1,sizeof(info->info1),"Pressure %.2f psi", (double)kpa);
snprintf(info->info2,sizeof(info->info2),"Temperature %d C", temp);
snprintf(info->info2,sizeof(info->info2),"Pressure %.2f psi", (double)kpa);
snprintf(info->info3,sizeof(info->info3),"Temperature %d C", temp);
return true;
}

Expand Down
10 changes: 6 additions & 4 deletions signal.c
Original file line number Diff line number Diff line change
Expand Up @@ -363,15 +363,17 @@ uint32_t bitmap_seek_bits(uint8_t *b, uint32_t blen, uint32_t startpos, uint32_t
return BITMAP_SEEK_NOT_FOUND;
}

/* Set the pattern 'pat' into the bitmap 'b' of max length 'blen' bytes.
/* Set the pattern 'pat' into the bitmap 'b' of max length 'blen' bytes,
* starting from the specified offset.
*
* The pattern is given as a string of 0s and 1s characters, like "01101001".
* This function is useful in order to set the test vectors in the protocol
* decoders, to see if the decoding works regardless of the fact we are able
* to actually receive a given signal. */
void bitmap_set_pattern(uint8_t *b, uint32_t blen, const char *pat) {
void bitmap_set_pattern(uint8_t *b, uint32_t blen, uint32_t off, const char *pat) {
uint32_t i = 0;
while(pat[i]) {
bitmap_set(b,blen,i,pat[i] == '1');
bitmap_set(b,blen,i+off,pat[i] == '1');
i++;
}
}
Expand Down Expand Up @@ -537,7 +539,7 @@ bool decode_signal(RawSamplesBuffer *s, uint64_t len, ProtoViewMsgInfo *info) {

/* We call the decoders with an offset a few samples before the actual
* signal detected and for a len of a few bits after its end. */
uint32_t before_samples = 20;
uint32_t before_samples = 32;
uint32_t after_samples = 100;

uint8_t *bitmap = malloc(bitmap_size);
Expand Down

0 comments on commit 0f8ef0a

Please sign in to comment.