diff --git a/Inc/cobs.h b/Inc/cobs.h index 334ead3f..ee52e904 100644 --- a/Inc/cobs.h +++ b/Inc/cobs.h @@ -55,7 +55,7 @@ const uint8_t *COBSgetFrameExtent( const uint8_t *inputEnc, int len ); bool COBSSimpleDecode( const uint8_t *inputEnc, int len, struct Frame *o ); bool COBSisEOFRAME( const uint8_t *inputEnc ); -void COBSEncode( const uint8_t *frontMsg, int lfront, const uint8_t *inputMsg, int len, struct Frame *o ); +void COBSEncode( const uint8_t *frontMsg, int lfront, const uint8_t *backMsg, int lback, const uint8_t *inputMsg, int lmsg, struct Frame *o ); /* Context free functions */ void COBSPump( struct COBS *t, const uint8_t *incoming, int len, diff --git a/Inc/otag.h b/Inc/otag.h index 923bdc8f..ba7eb806 100644 --- a/Inc/otag.h +++ b/Inc/otag.h @@ -21,8 +21,11 @@ extern "C" { struct OTAGFrame { unsigned int len; /* Received length (after pre-processing) */ - uint8_t tag; /* Tag (packet type) */ - uint64_t tstamp; /* Timestamp for the packet */ + uint8_t tag; /* Tag (packet type) */ + uint8_t sum; /* Checksum byte */ + bool good; /* Is the checksum valid? */ + uint64_t tstamp; /* Timestamp for the packet */ + uint8_t *d; /* ...pointer to the data itself */ }; diff --git a/Src/cobs.c b/Src/cobs.c index ea0034ee..e21db625 100644 --- a/Src/cobs.c +++ b/Src/cobs.c @@ -46,7 +46,7 @@ void COBSDelete( struct COBS *t ) // ==================================================================================================== -void COBSEncode( const uint8_t *frontMsg, int lfront, const uint8_t *inputMsg, int len, struct Frame *o ) +void COBSEncode( const uint8_t *frontMsg, int lfront, const uint8_t *backMsg, int lback, const uint8_t *inputMsg, int lmsg, struct Frame *o ) /* Encode frame and write into provided output Frame buffer */ @@ -54,7 +54,8 @@ void COBSEncode( const uint8_t *frontMsg, int lfront, const uint8_t *inputMsg, i uint8_t *wp = o->d; o->len = 0; - len += lfront; + /* Get overall message length */ + int len = lfront + lmsg + lback; assert( len <= COBS_OVERALL_MAX_PACKET_LEN ); @@ -65,8 +66,8 @@ void COBSEncode( const uint8_t *frontMsg, int lfront, const uint8_t *inputMsg, i for ( int i = 0; len--; i++ ) { - /* Take byte either from frontmatter or main message */ - const uint8_t *rp = ( i < lfront ) ? &frontMsg[i] : &inputMsg[i - lfront]; + /* 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]; if ( COBS_SYNC_CHAR != *rp ) { diff --git a/Src/otag.c b/Src/otag.c index cc0a43d2..ba5f36a2 100644 --- a/Src/otag.c +++ b/Src/otag.c @@ -55,7 +55,15 @@ void OTAGEncode( const uint8_t channel, const uint64_t tstamp, const uint8_t *in { const uint8_t frontMatter[1] = { channel }; - COBSEncode( frontMatter, 1, inputMsg, len, o ); + uint8_t backMatter[1] = { channel }; + + /* Calculate packet sum for last byte */ + for ( int i = 0; i < len; i++ ) + { + backMatter[0] += inputMsg[i]; + } + + COBSEncode( frontMatter, 1, backMatter, 1, inputMsg, len, o ); } // ==================================================================================================== @@ -73,11 +81,22 @@ 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 - 1; /* OTAG frames have the first element representing the tag */ - t->f.tag = p->d[0]; /* First byte of an OTAG frame is the tag */ - t->f.d = &p->d[1]; /* This is the rest of the data */ - /* Timestamp was already set for this cluster */ + 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 */ + + /* 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]; + } + t->f.good = ( sum == t->f.sum ); + + /* Timestamp was already set for this cluster */ ( t->cb )( &t->f, t->param ); } @@ -99,17 +118,6 @@ void OTAGPump( struct OTAG *t, const uint8_t *incoming, int len, // ==================================================================================================== -bool OTAGSimpleDecode( const uint8_t *inputEnc, int len, struct Frame *o ) - -/* Decode frame and write decoded frame into provided Frame buffer */ -/* Returns FALSE if packet did not decode...and store the fragment. */ - -{ - return COBSSimpleDecode( inputEnc, len, o ); -} - -// ==================================================================================================== - const uint8_t *OTAGgetFrameExtent( const uint8_t *inputEnc, int len ) /* Look through memory until an end of frame marker is found, or memory is exhausted. */