From ade8d98df470f68367243feda8fcf43ed4ae3a93 Mon Sep 17 00:00:00 2001 From: Dave Marples Date: Tue, 6 Aug 2024 00:21:16 +0100 Subject: [PATCH] Respect checksum in OTAG frames --- Inc/otag.h | 2 +- Src/cobs.c | 8 ++++---- Src/itmfifos.c | 13 +++++++++--- Src/orbcat.c | 13 +++++++++--- Src/orbdump.c | 13 +++++++++--- Src/orblcd.c | 13 +++++++++--- Src/orbmortem.c | 15 ++++++++++---- Src/orbprofile.c | 11 ++++++++-- Src/orbstat.c | 13 +++++++++--- Src/orbtop.c | 13 +++++++++--- Src/orbuculum.c | 53 +++++++++++++++++++++++++++++------------------- Src/orbzmq.c | 14 +++++++++---- Src/otag.c | 18 +++++++++------- 13 files changed, 138 insertions(+), 61 deletions(-) diff --git a/Inc/otag.h b/Inc/otag.h index ba7eb806..5b66f9b5 100644 --- a/Inc/otag.h +++ b/Inc/otag.h @@ -40,7 +40,7 @@ struct OTAG void *param; }; -#define OTAG_MAX_PACKET_LEN (COBS_MAX_PACKET_LEN) +#define OTAG_MAX_PACKET_LEN (COBS_MAX_PACKET_LEN-2) #define OTAG_MAX_ENC_PACKET_LEN (COBS_MAX_ENC_PACKET_LEN) #define OTAG_EOP_LEN (COBS_EOP_LEN) #define OTAG_TS_RESOLUTION (1000000000L) diff --git a/Src/cobs.c b/Src/cobs.c index e21db625..be1c2930 100644 --- a/Src/cobs.c +++ b/Src/cobs.c @@ -67,7 +67,7 @@ void COBSEncode( const uint8_t *frontMsg, int lfront, const uint8_t *backMsg, in for ( int i = 0; len--; i++ ) { /* Take byte from frontMatter, main message or backMatter depending on where we are in transmission */ - const uint8_t *rp = ( i < lfront ) ? &frontMsg[i] : ( i < ( lfront + lmsg ) ) ? &inputMsg[i - lfront] : &backMsg[i - lfront - lmsg]; + const uint8_t *rp = ( i < lfront ) ? &frontMsg[i] : ( i < ( lfront + lmsg ) ) ? &inputMsg[i - lfront] : &backMsg[i - ( lfront + lmsg )]; if ( COBS_SYNC_CHAR != *rp ) { @@ -89,10 +89,10 @@ void COBSEncode( const uint8_t *frontMsg, int lfront, const uint8_t *backMsg, in } *cp = seglen; - } - /* Packet must end with a sync to define EOP */ - *wp++ = COBS_SYNC_CHAR; + /* Packet must end with a sync to define EOP */ + *wp++ = COBS_SYNC_CHAR; + } o->len = ( wp - o->d ); } diff --git a/Src/itmfifos.c b/Src/itmfifos.c index 194eecdc..86aebb42 100644 --- a/Src/itmfifos.c +++ b/Src/itmfifos.c @@ -558,11 +558,18 @@ static void _OTAGpacketRxed ( struct OTAGFrame *p, void *param ) { struct itmfifosHandle *f = ( struct itmfifosHandle * )param; - if ( p->tag == f->tag ) + if ( !p->good ) { - for ( int i = 0; i < p->len; i++ ) + genericsReport( V_WARN, "Bad packet received" EOL ); + } + else + { + if ( p->tag == f->tag ) { - _itmPumpProcess( f, p->d[i] ); + for ( int i = 0; i < p->len; i++ ) + { + _itmPumpProcess( f, p->d[i] ); + } } } } diff --git a/Src/orbcat.c b/Src/orbcat.c index 72ddb271..0b921a19 100644 --- a/Src/orbcat.c +++ b/Src/orbcat.c @@ -956,11 +956,18 @@ static struct Stream *_tryOpenStream( void ) static void _OTAGpacketRxed ( struct OTAGFrame *p, void *param ) { - if ( p->tag == options.tag ) + if ( !p->good ) { - for ( int i = 0; i < p->len; i++ ) + genericsReport( V_WARN, "Bad packet received" EOL ); + } + else + { + if ( p->tag == options.tag ) { - _itmPumpProcess( p->d[i] ); + for ( int i = 0; i < p->len; i++ ) + { + _itmPumpProcess( p->d[i] ); + } } } } diff --git a/Src/orbdump.c b/Src/orbdump.c index 11ff5cbc..a5e36935 100644 --- a/Src/orbdump.c +++ b/Src/orbdump.c @@ -380,11 +380,18 @@ bool _processOptions( int argc, char *argv[] ) static void _OTAGpacketRxed ( struct OTAGFrame *p, void *param ) { - if ( p->tag == options.tag ) + if ( !p->good ) { - for ( int i = 0; i < p->len; i++ ) + genericsReport( V_WARN, "Bad packet received" EOL ); + } + else + { + if ( p->tag == options.tag ) { - ITMPump( &_r.i, p->d[i] ); + for ( int i = 0; i < p->len; i++ ) + { + ITMPump( &_r.i, p->d[i] ); + } } } } diff --git a/Src/orblcd.c b/Src/orblcd.c index 2dbf6507..883b0a47 100644 --- a/Src/orblcd.c +++ b/Src/orblcd.c @@ -378,11 +378,18 @@ static void _OTAGpacketRxed ( struct OTAGFrame *p, void *param ) { struct RunTime *r = ( struct RunTime * )param; - if ( p->tag == r->options->tag ) + if ( !p->good ) { - for ( int i = 0; i < p->len; i++ ) + genericsReport( V_WARN, "Bad packet received" EOL ); + } + else + { + if ( p->tag == r->options->tag ) { - _itmPumpProcess( p->d[i], r ); + for ( int i = 0; i < p->len; i++ ) + { + _itmPumpProcess( p->d[i], r ); + } } } } diff --git a/Src/orbmortem.c b/Src/orbmortem.c index e3a9a05f..1f2ef438 100644 --- a/Src/orbmortem.c +++ b/Src/orbmortem.c @@ -617,13 +617,20 @@ static void _OTAGpacketRxed ( struct OTAGFrame *p, void *param ) { struct RunTime *r = ( struct RunTime * )param; - if ( p->tag == r->options->tag ) + if ( !p->good ) { - for ( int i = 0; i < p->len; i++ ) + genericsReport( V_WARN, "Bad packet received" EOL ); + } + else + { + if ( p->tag == r->options->tag ) { - if ( _rxAdd( r, p->d[i] ) ) + for ( int i = 0; i < p->len; i++ ) { - return; + if ( _rxAdd( r, p->d[i] ) ) + { + return; + } } } } diff --git a/Src/orbprofile.c b/Src/orbprofile.c index ea1516ce..285ad0ee 100644 --- a/Src/orbprofile.c +++ b/Src/orbprofile.c @@ -769,9 +769,16 @@ static void _intHandler( int sig ) static void _OTAGpacketRxed ( struct OTAGFrame *p, void *param ) { - if ( p->tag == _r.options->tag ) + if ( !p->good ) { - TRACEDecoderPump( &_r.i, p->d, p->len, _traceCB, &_r ); + genericsReport( V_WARN, "Bad packet received" EOL ); + } + else + { + if ( p->tag == _r.options->tag ) + { + TRACEDecoderPump( &_r.i, p->d, p->len, _traceCB, &_r ); + } } } // ==================================================================================================== diff --git a/Src/orbstat.c b/Src/orbstat.c index 1182cff8..f49b6291 100644 --- a/Src/orbstat.c +++ b/Src/orbstat.c @@ -772,11 +772,18 @@ static void _OTAGpacketRxed ( struct OTAGFrame *p, void *param ) { struct RunTime *r = ( struct RunTime * )param; - if ( p->tag == r->options->tag ) + if ( !p->good ) { - for ( int i = 0; i < p->len; i++ ) + genericsReport( V_WARN, "Bad packet received" EOL ); + } + else + { + if ( p->tag == r->options->tag ) { - _itmPumpProcess( r, p->d[i] ); + for ( int i = 0; i < p->len; i++ ) + { + _itmPumpProcess( r, p->d[i] ); + } } } } diff --git a/Src/orbtop.c b/Src/orbtop.c index fae96db1..44542931 100644 --- a/Src/orbtop.c +++ b/Src/orbtop.c @@ -1250,11 +1250,18 @@ bool _processOptions( int argc, char *argv[] ) static void _OTAGpacketRxed ( struct OTAGFrame *p, void *param ) { - if ( p->tag == options.tag ) + if ( !p->good ) { - for ( int i = 0; i < p->len; i++ ) + genericsReport( V_WARN, "Bad packet received" EOL ); + } + else + { + if ( p->tag == options.tag ) { - _itmPumpProcess( p->d[i] ); + for ( int i = 0; i < p->len; i++ ) + { + _itmPumpProcess( p->d[i] ); + } } } } diff --git a/Src/orbuculum.c b/Src/orbuculum.c index 0251558f..4d96e53d 100644 --- a/Src/orbuculum.c +++ b/Src/orbuculum.c @@ -879,33 +879,40 @@ static void _OTAGpacketRxed( struct OTAGFrame *p, void *param ) struct RunTime *r = ( struct RunTime * )param; struct handlers *h = _r.handler; - /* Account for this reception */ - r->tagCount[p->tag].totalData += p->len; - r->tagCount[p->tag].intervalData += p->len; - - /* Search for channel */ - for ( chIndex = 0; chIndex < r->numHandlers; chIndex++ ) + if ( !p->good ) { - if ( h->channel == p->tag ) - { - break; - } - - h++; + genericsReport( V_WARN, "Bad packet received" EOL ); } - - if ( ( chIndex != r->numHandlers ) && ( h ) ) + else { - /* We must have found a match for this at some point, so add it to the queue */ - for ( int i = 0; i < p->len; i++ ) + /* Account for this reception */ + r->tagCount[p->tag].totalData += p->len; + r->tagCount[p->tag].intervalData += p->len; + + /* Search for channel */ + for ( chIndex = 0; chIndex < r->numHandlers; chIndex++ ) { - h->strippedBlock->buffer[h->strippedBlock->fillLevel++] = p->d[i]; + if ( h->channel == p->tag ) + { + break; + } + + h++; + } - if ( h->strippedBlock->fillLevel == sizeof( h->strippedBlock->buffer ) ) + if ( ( chIndex != r->numHandlers ) && ( h ) ) + { + /* We must have found a match for this at some point, so add it to the queue */ + for ( int i = 0; i < p->len; i++ ) { - /* We filled this block...better send it right now */ - nwclientSend( h->n, h->strippedBlock->fillLevel, h->strippedBlock->buffer ); - h->strippedBlock->fillLevel = 0; + h->strippedBlock->buffer[h->strippedBlock->fillLevel++] = p->d[i]; + + if ( h->strippedBlock->fillLevel == sizeof( h->strippedBlock->buffer ) ) + { + /* We filled this block...better send it right now */ + nwclientSend( h->n, h->strippedBlock->fillLevel, h->strippedBlock->buffer ); + h->strippedBlock->fillLevel = 0; + } } } } @@ -1141,6 +1148,10 @@ static int _usbFeeder( struct RunTime *r ) /* We only attempt to write the file header on the first run through */ firstRunThrough = false; } + else + { + genericsReport( V_INFO, "Orbtrace supports legacy protocol" EOL ); + } genericsReport( V_DEBUG, "USB Interface claimed, ready for data" EOL ); diff --git a/Src/orbzmq.c b/Src/orbzmq.c index 8de551d7..1cfd63b7 100644 --- a/Src/orbzmq.c +++ b/Src/orbzmq.c @@ -885,12 +885,18 @@ static struct Stream *_tryOpenStream() static void _OTAGpacketRxed ( struct OTAGFrame *p, void *param ) { - - if ( p->tag == options.tag ) + if ( !p->good ) + { + genericsReport( V_WARN, "Bad packet received" EOL ); + } + else { - for ( int i = 0; i < p->len; i++ ) + if ( p->tag == options.tag ) { - _itmPumpProcess( p->d[i] ); + for ( int i = 0; i < p->len; i++ ) + { + _itmPumpProcess( p->d[i] ); + } } } } diff --git a/Src/otag.c b/Src/otag.c index ba5f36a2..e4ea92d7 100644 --- a/Src/otag.c +++ b/Src/otag.c @@ -55,14 +55,17 @@ void OTAGEncode( const uint8_t channel, const uint64_t tstamp, const uint8_t *in { const uint8_t frontMatter[1] = { channel }; - uint8_t backMatter[1] = { channel }; + uint8_t sum = channel; /* Calculate packet sum for last byte */ for ( int i = 0; i < len; i++ ) { - backMatter[0] += inputMsg[i]; + sum += inputMsg[i]; } + /* Ensure total sums to 0 */ + uint8_t backMatter[1] = { 256 - sum }; + COBSEncode( frontMatter, 1, backMatter, 1, inputMsg, len, o ); } @@ -81,20 +84,21 @@ static void _pumpcb( struct Frame *p, void *param ) /* Callback function when a COBS packet is complete */ struct OTAG *t = ( struct OTAG * )param; - t->f.len = p->len - 2; /* OTAG frames have the first element representing the tag and last element the checksum */ - t->f.tag = p->d[0]; /* First byte of an OTAG frame is the tag */ + t->f.len = p->len - 2; /* OTAG frames have the first element representing the tag and last element the checksum */ + t->f.tag = p->d[0]; /* First byte of an OTAG frame is the tag */ t->f.sum = p->d[p->len - 1]; /* Last byte of an OTAG frame is the sum */ - t->f.d = &p->d[1]; /* This is the rest of the data */ + t->f.d = &p->d[1]; /* This is the rest of the data */ /* Calculate received packet sum and insert good status into packet */ uint8_t sum = t->f.tag; for ( int i = 0; i < t->f.len; i++ ) { - sum += p->d[i]; + sum += t->f.d[i]; } - t->f.good = ( sum == t->f.sum ); + sum += t->f.sum; + t->f.good = ( sum == 0 ); /* Timestamp was already set for this cluster */ ( t->cb )( &t->f, t->param );