Skip to content

Commit

Permalink
Addition of checksum to OTAG packets
Browse files Browse the repository at this point in the history
  • Loading branch information
mubes committed Aug 5, 2024
1 parent 64af329 commit 5068415
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 23 deletions.
2 changes: 1 addition & 1 deletion Inc/cobs.h
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
7 changes: 5 additions & 2 deletions Inc/otag.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
};

Expand Down
9 changes: 5 additions & 4 deletions Src/cobs.c
Original file line number Diff line number Diff line change
Expand Up @@ -46,15 +46,16 @@ 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 */

{
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 );

Expand All @@ -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 )
{
Expand Down
40 changes: 24 additions & 16 deletions Src/otag.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
}

// ====================================================================================================
Expand All @@ -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 );
}

Expand All @@ -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. */
Expand Down

0 comments on commit 5068415

Please sign in to comment.