diff --git a/app.h b/app.h index 40b579ff695..7698d432585 100644 --- a/app.h +++ b/app.h @@ -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); diff --git a/protocols/keeloq.c b/protocols/keeloq.c index 6ea73a7d305..db59be230d5 100644 --- a/protocols/keeloq.c +++ b/protocols/keeloq.c @@ -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. */ diff --git a/protocols/oregon2.c b/protocols/oregon2.c index 3aa57c72d00..aa734661903 100644 --- a/protocols/oregon2.c +++ b/protocols/oregon2.c @@ -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}; @@ -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) { diff --git a/protocols/tpms/citroen.c b/protocols/tpms/citroen.c index 992b7f4b0a8..89d31ab591b 100644 --- a/protocols/tpms/citroen.c +++ b/protocols/tpms/citroen.c @@ -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]; @@ -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; diff --git a/protocols/tpms/ford.c b/protocols/tpms/ford.c index a9c687075d0..e897d9d1f68 100644 --- a/protocols/tpms/ford.c +++ b/protocols/tpms/ford.c @@ -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]; @@ -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 diff --git a/protocols/tpms/renault.c b/protocols/tpms/renault.c index 4bbe55e42a3..b8dd46ce8fa 100644 --- a/protocols/tpms/renault.c +++ b/protocols/tpms/renault.c @@ -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); } @@ -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]; @@ -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; diff --git a/protocols/tpms/schrader.c b/protocols/tpms/schrader.c index ab65a92d39f..efcc2138054 100644 --- a/protocols/tpms/schrader.c +++ b/protocols/tpms/schrader.c @@ -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); } @@ -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. */ @@ -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; diff --git a/protocols/tpms/schrader_eg53ma4.c b/protocols/tpms/schrader_eg53ma4.c index 6fce40d64e2..29701e6ca30 100644 --- a/protocols/tpms/schrader_eg53ma4.c +++ b/protocols/tpms/schrader_eg53ma4.c @@ -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]; @@ -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. */ diff --git a/protocols/tpms/toyota.c b/protocols/tpms/toyota.c index b2735373957..c029baf11b0 100644 --- a/protocols/tpms/toyota.c +++ b/protocols/tpms/toyota.c @@ -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; } @@ -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; @@ -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; } diff --git a/signal.c b/signal.c index bb823f7f468..1ea195245ca 100644 --- a/signal.c +++ b/signal.c @@ -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++; } } @@ -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);