Skip to content

Commit

Permalink
jobs: use compile time length helper
Browse files Browse the repository at this point in the history
  • Loading branch information
BogdanTheGeek committed Dec 15, 2023
1 parent 532789d commit ec1ae74
Showing 1 changed file with 24 additions and 17 deletions.
41 changes: 24 additions & 17 deletions source/jobs.c
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,13 @@

/** @cond DO_NOT_DOCUMENT */

/**
* @brief Get the length of a string literal.
*/
#ifdef CONST_STRLEN
#undef CONST_STRLEN
#endif
#define CONST_STRLEN( x ) ( sizeof( ( x ) ) - 1U )

/**
* @brief Table of topic API strings in JobsTopic_t order.
Expand Down Expand Up @@ -282,7 +289,7 @@ JobsStatus_t Jobs_GetTopic( char * buffer,
if( api >= JobsDescribeSuccess )
{
( void ) strnAppend( buffer, &start, length,
"+/", ( sizeof( "+/" ) - 1U ) );
"+/", ( CONST_STRLEN( "+/" ) ) );
}

ret = strnAppend( buffer, &start, length,
Expand Down Expand Up @@ -717,7 +724,7 @@ size_t Jobs_StartNextMsg( const char * clientToken,
{
( void ) strnAppend( buffer, &start, bufferSize, JOBS_API_CLIENTTOKEN, JOBS_API_CLIENTTOKEN_LENGTH );
( void ) strnAppend( buffer, &start, bufferSize, clientToken, clientTokenLength );
( void ) strnAppend( buffer, &start, bufferSize, "\"}", sizeof( "\"}" ) - 1U );
( void ) strnAppend( buffer, &start, bufferSize, "\"}", ( CONST_STRLEN( "\"}" ) ) );
}

return start;
Expand Down Expand Up @@ -748,7 +755,7 @@ JobsStatus_t Jobs_Describe( char * buffer,
( void ) strnAppend( buffer, &start, length,
jobId, jobIdLength );
( void ) strnAppend( buffer, &start, length,
"/", ( sizeof( "/" ) - 1U ) );
"/", ( CONST_STRLEN( "/" ) ) );
ret = strnAppend( buffer, &start, length,
JOBS_API_DESCRIBE, JOBS_API_DESCRIBE_LENGTH );

Expand Down Expand Up @@ -788,7 +795,7 @@ JobsStatus_t Jobs_Update( char * buffer,
( void ) strnAppend( buffer, &start, length,
jobId, jobIdLength );
( void ) strnAppend( buffer, &start, length,
"/", ( sizeof( "/" ) - 1U ) );
"/", ( CONST_STRLEN( "/" ) ) );
ret = strnAppend( buffer, &start, length,
JOBS_API_UPDATE, JOBS_API_UPDATE_LENGTH );

Expand Down Expand Up @@ -821,11 +828,11 @@ size_t Jobs_UpdateMsg( JobCurrentStatus_t status,

static const size_t jobStatusStringLengths[ 5U ] =
{
sizeof( "QUEUED" ) - 1U,
sizeof( "IN_PROGRESS" ) - 1U,
sizeof( "FAILED" ) - 1U,
sizeof( "SUCCEEDED" ) - 1U,
sizeof( "REJECTED" ) - 1U
CONST_STRLEN( "QUEUED" ),
CONST_STRLEN( "IN_PROGRESS" ),
CONST_STRLEN( "FAILED" ),
CONST_STRLEN( "SUCCEEDED" ),
CONST_STRLEN( "REJECTED" )
};

size_t start = 0U;
Expand All @@ -838,7 +845,7 @@ size_t Jobs_UpdateMsg( JobCurrentStatus_t status,
( void ) strnAppend( buffer, &start, bufferSize, jobStatusString[ status ], jobStatusStringLengths[ status ] );
( void ) strnAppend( buffer, &start, bufferSize, JOBS_API_EXPECTED_VERSION, JOBS_API_EXPECTED_VERSION_LENGTH );
( void ) strnAppend( buffer, &start, bufferSize, expectedVersion, expectedVersionLength );
( void ) strnAppend( buffer, &start, bufferSize, "\"}", sizeof( "\"}" ) - 1U );
( void ) strnAppend( buffer, &start, bufferSize, "\"}", ( CONST_STRLEN( "\"}" ) ) );
}

return start;
Expand Down Expand Up @@ -868,17 +875,17 @@ bool Jobs_IsJobUpdateStatus( const char * topic,

static const size_t jobUpdateStatusStringLengths[ 2U ] =
{
sizeof( "accepted" ) - 1U,
sizeof( "rejected" ) - 1U
CONST_STRLEN( "accepted" ),
CONST_STRLEN( "rejected" )
};

/* Max suffix size = max topic size - "$aws/<thingname>" prefix */
size_t suffixBufferLength = ( TOPIC_BUFFER_SIZE - sizeof( "$aws/<thingname>" ) - 1U );
char suffixBuffer[ TOPIC_BUFFER_SIZE - sizeof( "$aws/<thingname>" ) - 1U ] = { '\0' };
size_t suffixBufferLength = ( TOPIC_BUFFER_SIZE - CONST_STRLEN( "$aws/<thingname>" ) );
char suffixBuffer[ TOPIC_BUFFER_SIZE - CONST_STRLEN( "$aws/<thingname>" ) ] = { '\0' };
size_t start = 0U;

( void ) strnAppend( suffixBuffer, &start, suffixBufferLength, jobId, jobIdLength );
( void ) strnAppend( suffixBuffer, &start, suffixBufferLength, "/update/", sizeof( "/update/" ) - 1U );
( void ) strnAppend( suffixBuffer, &start, suffixBufferLength, "/update/", ( CONST_STRLEN( "/update/" ) ) );
( void ) strnAppend( suffixBuffer, &start, suffixBufferLength, jobUpdateStatusString[ expectedStatus ], jobUpdateStatusStringLengths[ expectedStatus ] );

return isThingnameTopicMatch( topic, topicLength, suffixBuffer, strnlen( suffixBuffer, suffixBufferLength ), thingName, thingNameLength );
Expand All @@ -898,7 +905,7 @@ size_t Jobs_GetJobId( const char * message,
jsonResult = JSON_SearchConst( message,
messageLength,
"execution.jobId",
sizeof( "execution.jobId" ) - 1U,
CONST_STRLEN( "execution.jobId" ),
jobId,
&jobIdLength,
NULL );
Expand All @@ -921,7 +928,7 @@ size_t Jobs_GetJobDocument( const char * message,
jsonResult = JSON_SearchConst( message,
messageLength,
"execution.jobDocument",
sizeof( "execution.jobDocument" ) - 1U,
CONST_STRLEN( "execution.jobDocument" ),
jobDoc,
&jobDocLength,
NULL );
Expand Down

0 comments on commit ec1ae74

Please sign in to comment.