Skip to content

Commit

Permalink
More WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
mubes committed Oct 31, 2023
1 parent 9c6a251 commit 40194b0
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 21 deletions.
8 changes: 5 additions & 3 deletions Inc/cobs.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,10 @@
extern "C" {
#endif

#define COBS_FRONTMATTER (10)
#define COBS_MAX_PACKET_LEN (4096)
#define COBS_MAX_ENC_PACKET_LEN (COBS_MAX_PACKET_LEN + COBS_MAX_PACKET_LEN / 254)
#define COBS_OVERALL_MAX_PACKET_LEN (COBS_MAX_PACKET_LEN+COBS_FRONTMATTER)
#define COBS_MAX_ENC_PACKET_LEN (COBS_OVERALL_MAX_PACKET_LEN + COBS_OVERALL_MAX_PACKET_LEN / 254)

enum COBSPumpState
{
Expand Down Expand Up @@ -52,8 +54,8 @@ const uint8_t *COBSgetFrameExtent( const uint8_t *inputEnc, int len );
const uint8_t *COBSsimpleDecode( const uint8_t *inputEnc, int len, struct Frame *o );
bool COBSisEOFRAME( const uint8_t *inputEnc );

void COBSEncode( const uint8_t *inputMsg, int len, struct Frame *o );

void COBSEncode( const uint8_t *frontMsg, int lfront, const uint8_t *inputMsg, int len, struct Frame *o );
/* Context free functions */
void COBSPump( struct COBS *t, uint8_t *incoming, int len,
void ( *packetRxed )( struct Frame *p, void *param ),
Expand Down
2 changes: 1 addition & 1 deletion Inc/orbtraceIf.h
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ struct OrbtraceIfDevice
const struct OrbtraceInterfaceType *type;
};

struct dataBlock
struct __attribute__((packed)) dataBlock
{
ssize_t fillLevel; /* How full this block is */
uint8_t buffer[USB_TRANSFER_SIZE]; /* Block buffer */
Expand Down
22 changes: 14 additions & 8 deletions Src/cobs.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

#include <string.h>
#include <stdlib.h>
#include <assert.h>
#include "cobs.h"

#define COBS_SYNC_CHAR (0)
Expand Down Expand Up @@ -45,24 +46,28 @@ void COBSDelete( struct COBS *t )

// ====================================================================================================

void COBSEncode( const uint8_t *inputMsg, int len, struct Frame *o )
void COBSEncode( const uint8_t *frontMsg, int lfront, const uint8_t *inputMsg, int len, struct Frame *o )

/* Encode frame and write into provided output Frame buffer */

{
uint8_t *wp = o->d;
o->len = 0;

/* We always start a frame with a sync so the receivers got a chance */
*wp++ = COBS_SYNC_CHAR;
len+=lfront;

assert(len<=COBS_OVERALL_MAX_PACKET_LEN);

if ( len )
{
uint8_t *cp = wp++;
int seglen = 1;

for ( const uint8_t *rp = inputMsg; len--; rp++ )
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];

if ( COBS_SYNC_CHAR != *rp )
{
*wp++ = *rp;
Expand All @@ -84,7 +89,8 @@ void COBSEncode( const uint8_t *inputMsg, int len, struct Frame *o )

*cp = seglen;
}


*wp++ = COBS_SYNC_CHAR;

o->len = ( wp - o->d );
}
Expand Down Expand Up @@ -387,11 +393,11 @@ int main( int argc, void **argv )
for ( int i = 0; i < sizeof( testSet ) / sizeof( struct test ); i++ )
{
fprintf( stderr, "%d: ", i + 1 );
COBSEncode( testSet[i].dec.d, testSet[i].dec.len, &o );
COBSEncode( NULL, 0, testSet[i].dec.d, testSet[i].dec.len, &o );

if ( o.len != COBSgetFrameExtent( o.d, o.len ) - o.d )
if ( o.len != COBSgetFrameExtent( o.d, o.len ) - o.d + 1 )
{
fprintf( stderr, "Static framelen assessment failed %d vs expected %d\n", o.len, COBSgetFrameExtent( o.d, o.len ) - o.d );
fprintf( stderr, "Static framelen assessment failed %d vs expected %d\n", o.len, COBSgetFrameExtent( o.d, o.len ) - o.d + 1 );
continue;
}

Expand Down
57 changes: 48 additions & 9 deletions Src/orbuculum.c
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,9 @@
#define ORBTRACE "orbtrace"
#define ORBTRACEENVNAME "ORBTRACE"

// Define this to use original clean ORBTRACE code
#define LEGACY_ORBTRACE 1

/* Record for options, either defaults or from command line */
struct Options
{
Expand Down Expand Up @@ -105,7 +108,7 @@ struct RunTime
struct COBS c; /* COBS instance, in case we need it */

struct Frame cobsPart; /* COBS part frame for maintaining continuity across packets */

struct Frame cobsOtg; /* Outgoing COBS frame for legacy use */
struct OrbtraceIf *o; /* For accessing ORBTrace devices + BMPs */

uint64_t intervalRawBytes; /* Number of bytes transferred in current interval */
Expand Down Expand Up @@ -402,7 +405,7 @@ bool _processOptions( int argc, char *argv[], struct RunTime *r )
int c, optionIndex = 0;
#define DELIMITER ','

while ( ( c = getopt_long ( argc, argv, "a:Ef:hHVl:m:Mn:o:O:p:s:T:t:v:", _longOptions, &optionIndex ) ) != -1 )
while ( ( c = getopt_long ( argc, argv, "a:Ef:hHVl:m:Mn:o:O:p:s:Tt:v:", _longOptions, &optionIndex ) ) != -1 )
switch ( c )
{
// ------------------------------------
Expand Down Expand Up @@ -679,6 +682,7 @@ void *_checkInterval( void *params )

genericsPrintf( " Waste:%3d%% ", 100 - ( ( totalDat * 100 ) / r->intervalRawBytes ) );
}
#ifndef LEGACY_ORBTRACE
else
{
/* Either raw ITM or COBs frames */
Expand All @@ -688,6 +692,7 @@ void *_checkInterval( void *params )
genericsPrintf( " Waste:%01d.%01d%%", w / 10, w % 10 );
}
}
#endif

r->cobsDataLenRxed = 0;
r->intervalRawBytes = 0;
Expand Down Expand Up @@ -720,9 +725,23 @@ static void _purgeBlock( struct RunTime *r )
{
nwclientSend( h->n, h->strippedBlock->fillLevel, h->strippedBlock->buffer );
h->intervalBytes += h->strippedBlock->fillLevel;

/* The COBs encoded version goes out on the combined COBs channel, with a specific channel header */
int j=h->strippedBlock->fillLevel;
const uint8_t frontMatter[1] = { h->channel };
const uint8_t* b = h->strippedBlock->buffer;

while ( j )
{
COBSEncode( frontMatter, 1,
b,(j<COBS_MAX_PACKET_LEN)?j:COBS_MAX_PACKET_LEN,
&r->cobsOtg );
nwclientSend( _r.cobsHandler, r->cobsOtg.len,r->cobsOtg.d );
b+= (j<COBS_MAX_PACKET_LEN)?j:COBS_MAX_PACKET_LEN;
j-= (j<COBS_MAX_PACKET_LEN)?j:COBS_MAX_PACKET_LEN;
}
h->strippedBlock->fillLevel = 0;
}

h++;
}
}
Expand Down Expand Up @@ -788,7 +807,7 @@ static void _TPIUpacketRxed( enum TPIUPumpEvent e, struct TPIUPacket *p, void *p
}
// ====================================================================================================

static void _cobsDispatch( struct RunTime *r, const uint8_t *buffer, int length )
static void _cobsIncoming( struct RunTime *r, const uint8_t *buffer, int length )

{
const uint8_t *flen;
Expand Down Expand Up @@ -868,6 +887,7 @@ static void _cobsDispatch( struct RunTime *r, const uint8_t *buffer, int length
static void _processBlock( struct RunTime *r, ssize_t fillLevel, uint8_t *buffer )

{
static const uint8_t frontMatter[1] = { DEFAULT_ITM_STREAM };
genericsReport( V_DEBUG, "RXED Packet of %d bytes%s" EOL, fillLevel, ( r->options->intervalReportTime ) ? EOL : "" );

if ( fillLevel )
Expand All @@ -886,13 +906,26 @@ static void _processBlock( struct RunTime *r, ssize_t fillLevel, uint8_t *buffer
if ( r-> options->useTPIU )
{
/* Strip the TPIU framing from this input */
TPIUPump2( &r->t, r->rawBlock[r->rp].buffer, r->rawBlock[r->rp].fillLevel, _TPIUpacketRxed, r );
TPIUPump2( &r->t, buffer, fillLevel, _TPIUpacketRxed, r );
_purgeBlock( r );
}
else
{
/* Do it the old fashioned way and send out the unfettered block */
nwclientSend( _r.n, r->rawBlock[r->rp].fillLevel, r->rawBlock[r->rp].buffer );
nwclientSend( _r.n, fillLevel, buffer );

/* The COBs encoded version goes out on the default COBs channel */
uint8_t* b = buffer;
while ( fillLevel )
{
COBSEncode( frontMatter, 1,
b,
(fillLevel<COBS_MAX_PACKET_LEN)?fillLevel:COBS_MAX_PACKET_LEN,
&r->cobsOtg );
nwclientSend( _r.cobsHandler, r->cobsOtg.len,r->cobsOtg.d );
b+=(fillLevel<COBS_MAX_PACKET_LEN)?fillLevel:COBS_MAX_PACKET_LEN;
fillLevel-=(fillLevel<COBS_MAX_PACKET_LEN)?fillLevel:COBS_MAX_PACKET_LEN;
}
}
}
}
Expand Down Expand Up @@ -975,8 +1008,6 @@ static void _usb_callback( struct libusb_transfer *t )
/* Whatever the status that comes back, there may be data... */
if ( t->actual_length > 0 )
{
_r.intervalRawBytes += t->actual_length;

if ( _r.opFileHandle )
{
if ( write( _r.opFileHandle, t->buffer, t->actual_length ) < 0 )
Expand All @@ -985,16 +1016,20 @@ static void _usb_callback( struct libusb_transfer *t )
}
}

#ifndef LEGACY_ORBTRACE
if ( OrbtraceIsOrbtrace( _r.o ) )
{
_cobsDispatch( &_r, t->buffer, t->actual_length );
_cobsIncoming( &_r, t->buffer, t->actual_length );
/* We need to decode this so it can go through the 'normal' output channels too */
COBSPump( &_r.c, t->buffer, t->actual_length, _COBSpacketRxed, &_r );
_r.intervalRawBytes += t->actual_length;
_purgeBlock( &_r );
}
else
#endif
{
_processBlock( &_r, t->actual_length, t->buffer );
_r.intervalRawBytes += t->actual_length;
}

if ( ( t->status != LIBUSB_TRANSFER_COMPLETED ) &&
Expand Down Expand Up @@ -1466,7 +1501,11 @@ int main( int argc, char *argv[] )

#endif

#ifndef LEGACY_ORBTRACE
if ( _r.options->channelList )
#else
if ( ( _r.options->channelList ) && ( _r.options->useTPIU ))
#endif
{
/* Channel list is only needed for legacy ports that we are re-exporting (i.e. clean unencapsulated flows) */
char *c = _r.options->channelList;
Expand Down

0 comments on commit 40194b0

Please sign in to comment.