Skip to content

Commit 3eaa772

Browse files
authored
Have Fleet Provisoning demo log recieved CBOR payloads (aws#1713)
1 parent f367d0b commit 3eaa772

File tree

4 files changed

+142
-3
lines changed

4 files changed

+142
-3
lines changed

demos/fleet_provisioning/fleet_provisioning_with_csr/fleet_provisioning_serializer.c

+111
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@
2121
* SOFTWARE.
2222
*/
2323

24+
/* Standard includes */
25+
#include <stdarg.h>
26+
2427
/* TinyCBOR library for CBOR encoding and decoding operations. */
2528
#include "cbor.h"
2629

@@ -32,6 +35,35 @@
3235

3336
/* Header include. */
3437
#include "fleet_provisioning_serializer.h"
38+
39+
/*-----------------------------------------------------------*/
40+
41+
/**
42+
* @brief Context passed to tinyCBOR for #cborPrinter. Initial
43+
* state should be zeroed.
44+
*/
45+
typedef struct
46+
{
47+
const char * str;
48+
size_t length;
49+
} CborPrintContext_t;
50+
51+
/*-----------------------------------------------------------*/
52+
53+
/**
54+
* @brief Printing function to pass to tinyCBOR.
55+
*
56+
* cbor_value_to_pretty_stream calls it multiple times to print a textual CBOR
57+
* representation.
58+
*
59+
* @param token Context for the function.
60+
* @param fmt Printf style format string.
61+
* @param ... Printf style args after format string.
62+
*/
63+
static CborError cborPrinter( void * token,
64+
const char * fmt,
65+
... );
66+
3567
/*-----------------------------------------------------------*/
3668

3769
bool generateCsrRequest( uint8_t * pBuffer,
@@ -372,3 +404,82 @@ bool parseRegisterThingResponse( const uint8_t * pResponse,
372404
return( cborRet == CborNoError );
373405
}
374406
/*-----------------------------------------------------------*/
407+
408+
static CborError cborPrinter( void * token,
409+
const char * fmt,
410+
... )
411+
{
412+
int result;
413+
va_list args;
414+
CborPrintContext_t * ctx = ( CborPrintContext_t * ) token;
415+
416+
va_start( args, fmt );
417+
418+
/* Compute length to write. */
419+
result = vsnprintf( NULL, 0, fmt, args );
420+
421+
va_end( args );
422+
423+
if( result < 0 )
424+
{
425+
LogError( ( "Error formatting CBOR string." ) );
426+
}
427+
else
428+
{
429+
size_t newLen = ( unsigned ) result;
430+
size_t oldLen = ctx->length;
431+
char * newPtr;
432+
433+
ctx->length = oldLen + newLen;
434+
newPtr = ( char * ) realloc( ( void * ) ctx->str, ctx->length + 1 );
435+
436+
if( newPtr == NULL )
437+
{
438+
LogError( ( "Failed to reallocate CBOR string." ) );
439+
result = -1;
440+
}
441+
else
442+
{
443+
va_start( args, fmt );
444+
445+
result = vsnprintf( newPtr + oldLen, newLen + 1, fmt, args );
446+
447+
va_end( args );
448+
449+
ctx->str = newPtr;
450+
451+
if( result < 0 )
452+
{
453+
LogError( ( "Error printing CBOR string." ) );
454+
}
455+
}
456+
}
457+
458+
return ( result < 0 ) ? CborErrorIO : CborNoError;
459+
}
460+
/*-----------------------------------------------------------*/
461+
462+
const char * getStringFromCbor( const uint8_t * cbor,
463+
size_t length )
464+
{
465+
CborPrintContext_t printCtx = { 0 };
466+
CborParser parser;
467+
CborValue value;
468+
CborError error;
469+
470+
error = cbor_parser_init( cbor, length, 0, &parser, &value );
471+
472+
if( error == CborNoError )
473+
{
474+
error = cbor_value_to_pretty_stream( cborPrinter, &printCtx, &value, CborPrettyDefaultFlags );
475+
}
476+
477+
if( error != CborNoError )
478+
{
479+
LogError( ( "Error printing CBOR payload." ) );
480+
printCtx.str = "";
481+
}
482+
483+
return printCtx.str;
484+
}
485+
/*-----------------------------------------------------------*/

demos/fleet_provisioning/fleet_provisioning_with_csr/fleet_provisioning_serializer.h

+11
Original file line numberDiff line numberDiff line change
@@ -110,3 +110,14 @@ bool parseRegisterThingResponse( const uint8_t * pResponse,
110110
size_t length,
111111
char * pThingNameBuffer,
112112
size_t * pThingNameBufferLength );
113+
114+
/**
115+
* @brief Converts a CBOR document into a pretty printed string.
116+
*
117+
* @param[in] cbor The CBOR document.
118+
* @param[in] length The length of the CBOR document.
119+
*
120+
* @returns The pretty printed string on success. "" on error.
121+
*/
122+
const char * getStringFromCbor( const uint8_t * cbor,
123+
size_t length );

demos/fleet_provisioning/fleet_provisioning_with_csr/fleet_provisioning_with_csr_demo.c

+18-3
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@
5050

5151
/* Standard includes. */
5252
#include <stdlib.h>
53+
#include <stdio.h>
5354
#include <stdint.h>
5455
#include <stdbool.h>
5556

@@ -60,9 +61,6 @@
6061
/* Demo config. */
6162
#include "demo_config.h"
6263

63-
/* TinyCBOR library for CBOR encoding and decoding operations. */
64-
#include "cbor.h"
65-
6664
/* corePKCS11 includes. */
6765
#include "core_pkcs11.h"
6866
#include "core_pkcs11_config.h"
@@ -233,6 +231,7 @@ static void provisioningPublishCallback( MQTTPublishInfo_t * pPublishInfo,
233231
{
234232
FleetProvisioningStatus_t status;
235233
FleetProvisioningTopic_t api;
234+
const char * cborDump;
236235

237236
/* Silence compiler warnings about unused variables. */
238237
( void ) packetIdentifier;
@@ -252,6 +251,10 @@ static void provisioningPublishCallback( MQTTPublishInfo_t * pPublishInfo,
252251
{
253252
LogInfo( ( "Received accepted response from Fleet Provisioning CreateCertificateFromCsr API." ) );
254253

254+
cborDump = getStringFromCbor( ( const uint8_t * ) pPublishInfo->pPayload, pPublishInfo->payloadLength );
255+
LogDebug( ( "Payload: %s", cborDump ) );
256+
free( ( void * ) cborDump );
257+
255258
responseStatus = ResponseAccepted;
256259

257260
/* Copy the payload from the MQTT library's buffer to #payloadBuffer. */
@@ -265,12 +268,20 @@ static void provisioningPublishCallback( MQTTPublishInfo_t * pPublishInfo,
265268
{
266269
LogError( ( "Received rejected response from Fleet Provisioning CreateCertificateFromCsr API." ) );
267270

271+
cborDump = getStringFromCbor( ( const uint8_t * ) pPublishInfo->pPayload, pPublishInfo->payloadLength );
272+
LogError( ( "Payload: %s", cborDump ) );
273+
free( ( void * ) cborDump );
274+
268275
responseStatus = ResponseRejected;
269276
}
270277
else if( api == FleetProvCborRegisterThingAccepted )
271278
{
272279
LogInfo( ( "Received accepted response from Fleet Provisioning RegisterThing API." ) );
273280

281+
cborDump = getStringFromCbor( ( const uint8_t * ) pPublishInfo->pPayload, pPublishInfo->payloadLength );
282+
LogDebug( ( "Payload: %s", cborDump ) );
283+
free( ( void * ) cborDump );
284+
274285
responseStatus = ResponseAccepted;
275286

276287
/* Copy the payload from the MQTT library's buffer to #payloadBuffer. */
@@ -284,6 +295,10 @@ static void provisioningPublishCallback( MQTTPublishInfo_t * pPublishInfo,
284295
{
285296
LogError( ( "Received rejected response from Fleet Provisioning RegisterThing API." ) );
286297

298+
cborDump = getStringFromCbor( ( const uint8_t * ) pPublishInfo->pPayload, pPublishInfo->payloadLength );
299+
LogError( ( "Payload: %s", cborDump ) );
300+
free( ( void * ) cborDump );
301+
287302
responseStatus = ResponseRejected;
288303
}
289304
else

demos/lexicon.txt

+2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
accel
2+
fmt
23
accesskeyid
34
ack
45
acks
@@ -67,6 +68,7 @@ cas
6768
cb
6869
cbc
6970
cbor
71+
cborprinter
7072
ccm
7173
ccs
7274
cdn

0 commit comments

Comments
 (0)