Skip to content

Commit

Permalink
Respect checksum in OTAG frames
Browse files Browse the repository at this point in the history
  • Loading branch information
mubes committed Aug 5, 2024
1 parent 5068415 commit ade8d98
Show file tree
Hide file tree
Showing 13 changed files with 138 additions and 61 deletions.
2 changes: 1 addition & 1 deletion Inc/otag.h
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
8 changes: 4 additions & 4 deletions Src/cobs.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 )
{
Expand All @@ -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 );
}
Expand Down
13 changes: 10 additions & 3 deletions Src/itmfifos.c
Original file line number Diff line number Diff line change
Expand Up @@ -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] );
}
}
}
}
Expand Down
13 changes: 10 additions & 3 deletions Src/orbcat.c
Original file line number Diff line number Diff line change
Expand Up @@ -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] );
}
}
}
}
Expand Down
13 changes: 10 additions & 3 deletions Src/orbdump.c
Original file line number Diff line number Diff line change
Expand Up @@ -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] );
}
}
}
}
Expand Down
13 changes: 10 additions & 3 deletions Src/orblcd.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
}
}
}
}
Expand Down
15 changes: 11 additions & 4 deletions Src/orbmortem.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
}
}
Expand Down
11 changes: 9 additions & 2 deletions Src/orbprofile.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
}
}
}
// ====================================================================================================
Expand Down
13 changes: 10 additions & 3 deletions Src/orbstat.c
Original file line number Diff line number Diff line change
Expand Up @@ -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] );
}
}
}
}
Expand Down
13 changes: 10 additions & 3 deletions Src/orbtop.c
Original file line number Diff line number Diff line change
Expand Up @@ -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] );
}
}
}
}
Expand Down
53 changes: 32 additions & 21 deletions Src/orbuculum.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
}
}
Expand Down Expand Up @@ -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 );

Expand Down
14 changes: 10 additions & 4 deletions Src/orbzmq.c
Original file line number Diff line number Diff line change
Expand Up @@ -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] );
}
}
}
}
Expand Down
18 changes: 11 additions & 7 deletions Src/otag.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
}

Expand All @@ -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 );
Expand Down

0 comments on commit ade8d98

Please sign in to comment.