Defender demo cleanup (#651)

pull/656/head
tianmc1 4 years ago committed by GitHub
parent 63d38b846e
commit aa1eed8b4e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -197,16 +197,6 @@ static uint16_t pusOpenUdpPorts[ democonfigOPEN_UDP_PORTS_ARRAY_SIZE ];
*/
static Connection_t pxEstablishedConnections[ democonfigESTABLISHED_CONNECTIONS_ARRAY_SIZE ];
/**
* @brief Array of task statuses, used to generate custom metrics.
*/
static TaskStatus_t pxTaskStatusList[ democonfigCUSTOM_METRICS_TASKS_ARRAY_SIZE ];
/**
* @brief Task numbers custom metric array.
*/
static uint32_t pulCustomMetricsTaskNumbers[ democonfigCUSTOM_METRICS_TASKS_ARRAY_SIZE ];
/**
* @brief All the metrics sent in the Device Defender report.
*/
@ -243,6 +233,8 @@ static void prvPublishCallback( MQTTContext_t * pxMqttContext,
/**
* @brief Collect all the metrics to be sent in the Device Defender report.
*
* On success, caller is responsible for freeing xDeviceMetrics.pxTaskStatusArray.
*
* @return true if all the metrics are successfully collected;
* false otherwise.
*/
@ -251,12 +243,12 @@ static bool prvCollectDeviceMetrics( void );
/**
* @brief Generate the Device Defender report.
*
* @param[out] pulOutReportLength Length of the Device Defender report.
* @param[out] pxOutReportLength Length of the Device Defender report.
*
* @return true if the report is generated successfully;
* false otherwise.
*/
static bool prvGenerateDeviceMetricsReport( uint32_t * pulOutReportLength );
static bool prvGenerateDeviceMetricsReport( size_t * pxOutReportLength );
/**
* @brief Subscribe to the Device Defender topics.
@ -277,12 +269,12 @@ static bool prvUnsubscribeFromDefenderTopics( void );
/**
* @brief Publish the generated Device Defender report.
*
* @param[in] ulReportLength Length of the Device Defender report.
* @param[in] xReportLength Length of the Device Defender report.
*
* @return true if the report is published successfully;
* false otherwise.
*/
static bool prvPublishDeviceMetricsReport( uint32_t ulReportLength );
static bool prvPublishDeviceMetricsReport( size_t xReportLength );
/**
* @brief Validate the response received from the AWS IoT Device Defender Service.
@ -291,13 +283,13 @@ static bool prvPublishDeviceMetricsReport( uint32_t ulReportLength );
* is same as was sent in the published report.
*
* @param[in] pcDefenderResponse The defender response to validate.
* @param[in] ulDefenderResponseLength Length of the defender response.
* @param[in] xDefenderResponseLength Length of the defender response.
*
* @return true if the response is valid;
* false otherwise.
*/
static bool prvValidateDefenderResponse( const char * pcDefenderResponse,
uint32_t ulDefenderResponseLength );
size_t xDefenderResponseLength );
/**
* @brief The task used to demonstrate the Defender API.
@ -316,7 +308,7 @@ static void prvDefenderDemoTask( void * pvParameters );
/*-----------------------------------------------------------*/
static bool prvValidateDefenderResponse( const char * pcDefenderResponse,
uint32_t ulDefenderResponseLength )
size_t xDefenderResponseLength )
{
bool xStatus = false;
JSONStatus_t eJsonResult = JSONSuccess;
@ -327,12 +319,12 @@ static bool prvValidateDefenderResponse( const char * pcDefenderResponse,
configASSERT( pcDefenderResponse != NULL );
/* Is the response a valid JSON? */
eJsonResult = JSON_Validate( pcDefenderResponse, ulDefenderResponseLength );
eJsonResult = JSON_Validate( pcDefenderResponse, xDefenderResponseLength );
if( eJsonResult != JSONSuccess )
{
LogError( ( "Invalid response from AWS IoT Device Defender Service: %.*s.",
( int ) ulDefenderResponseLength,
( int ) xDefenderResponseLength,
pcDefenderResponse ) );
}
@ -340,7 +332,7 @@ static bool prvValidateDefenderResponse( const char * pcDefenderResponse,
{
/* Search the ReportId key in the response. */
eJsonResult = JSON_Search( ( char * ) pcDefenderResponse,
ulDefenderResponseLength,
xDefenderResponseLength,
DEFENDER_RESPONSE_REPORT_ID_FIELD,
DEFENDER_RESPONSE_REPORT_ID_FIELD_LENGTH,
&( ucReportIdString ),
@ -351,7 +343,7 @@ static bool prvValidateDefenderResponse( const char * pcDefenderResponse,
LogError( ( "%s key not found in the response from the"
"AWS IoT Device Defender Service: %.*s.",
DEFENDER_RESPONSE_REPORT_ID_FIELD,
( int ) ulDefenderResponseLength,
( int ) xDefenderResponseLength,
pcDefenderResponse ) );
}
}
@ -376,7 +368,7 @@ static bool prvValidateDefenderResponse( const char * pcDefenderResponse,
DEFENDER_RESPONSE_REPORT_ID_FIELD,
ulReportId,
ulReportIdInResponse,
( int ) ulDefenderResponseLength,
( int ) xDefenderResponseLength,
pcDefenderResponse ) );
}
}
@ -476,9 +468,11 @@ static bool prvCollectDeviceMetrics( void )
{
bool xStatus = false;
eMetricsCollectorStatus eStatus;
uint32_t ulNumOpenTcpPorts = 0UL, ulNumOpenUdpPorts = 0UL, ulNumEstablishedConnections = 0UL, i;
size_t xNumOpenTcpPorts = 0UL, xNumOpenUdpPorts = 0UL, xNumEstablishedConnections = 0UL, i;
UBaseType_t uxTasksWritten = { 0 };
UBaseType_t uxNumTasksRunning;
TaskStatus_t pxTaskStatus = { 0 };
TaskStatus_t * pxTaskStatusArray = NULL;
/* Collect bytes and packets sent and received. */
eStatus = eGetNetworkStats( &( xNetworkStats ) );
@ -494,7 +488,7 @@ static bool prvCollectDeviceMetrics( void )
{
eStatus = eGetOpenTcpPorts( &( pusOpenTcpPorts[ 0 ] ),
democonfigOPEN_TCP_PORTS_ARRAY_SIZE,
&( ulNumOpenTcpPorts ) );
&( xNumOpenTcpPorts ) );
if( eStatus != eMetricsCollectorSuccess )
{
@ -508,7 +502,7 @@ static bool prvCollectDeviceMetrics( void )
{
eStatus = eGetOpenUdpPorts( &( pusOpenUdpPorts[ 0 ] ),
democonfigOPEN_UDP_PORTS_ARRAY_SIZE,
&( ulNumOpenUdpPorts ) );
&( xNumOpenUdpPorts ) );
if( eStatus != eMetricsCollectorSuccess )
{
@ -522,7 +516,7 @@ static bool prvCollectDeviceMetrics( void )
{
eStatus = eGetEstablishedConnections( &( pxEstablishedConnections[ 0 ] ),
democonfigESTABLISHED_CONNECTIONS_ARRAY_SIZE,
&( ulNumEstablishedConnections ) );
&( xNumEstablishedConnections ) );
if( eStatus != eMetricsCollectorSuccess )
{
@ -531,33 +525,18 @@ static bool prvCollectDeviceMetrics( void )
}
}
/* Collect custom metrics. This demo sends this task's stack high water mark
* as a number type custom metric and the current task IDs as a list of
* numbers type custom metric. */
if( eStatus == eMetricsCollectorSuccess )
{
vTaskGetInfo(
/* Query this task. */
NULL,
&pxTaskStatus,
/* Include the stack high water mark value. */
pdTRUE,
/* Don't include the task state in the TaskStatus_t structure. */
0 );
uxTasksWritten = uxTaskGetSystemState( pxTaskStatusList, democonfigCUSTOM_METRICS_TASKS_ARRAY_SIZE, NULL );
/* Get task count */
uxNumTasksRunning = uxTaskGetNumberOfTasks();
if( uxTasksWritten == 0 )
/* Allocate pxTaskStatusArray */
pxTaskStatusArray = pvPortMalloc( uxNumTasksRunning * sizeof( TaskStatus_t ) );
if( pxTaskStatusArray == NULL )
{
LogError( ( "Cannot allocate memory for pxTaskStatusArray: pvPortMalloc() failed." ) );
eStatus = eMetricsCollectorCollectionFailed;
LogError( ( "Failed to collect system state. uxTaskGetSystemState() failed due to insufficient buffer space.",
eStatus ) );
}
else
{
for( i = 0; i < uxTasksWritten; i++ )
{
pulCustomMetricsTaskNumbers[ i ] = pxTaskStatusList[ i ].xTaskNumber;
}
}
}
@ -566,6 +545,9 @@ static bool prvCollectDeviceMetrics( void )
* numbers type custom metric. */
if( eStatus == eMetricsCollectorSuccess )
{
/* Get the current task's status information. The usStackHighWaterMark
* field of the task status will be included in the report as a "number"
* custom metric. */
vTaskGetInfo(
/* Query this task. */
NULL,
@ -574,21 +556,20 @@ static bool prvCollectDeviceMetrics( void )
pdTRUE,
/* Don't include the task state in the TaskStatus_t structure. */
0 );
uxTasksWritten = uxTaskGetSystemState( pxTaskStatusList, democonfigCUSTOM_METRICS_TASKS_ARRAY_SIZE, NULL );
/* Get the task status information for all running tasks. The task IDs
* of each task is then extracted to include in the report as a "list of
* numbers" custom metric */
uxTasksWritten = uxTaskGetSystemState( pxTaskStatusArray, uxNumTasksRunning, NULL );
if( uxTasksWritten == 0 )
{
/* If 0 is returned, the buffer was too small. This line is reached
* when we hit the race condition where tasks have been added since
* we got the result of uxTaskGetNumberOfTasks() */
eStatus = eMetricsCollectorCollectionFailed;
LogError( ( "Failed to collect system state. uxTaskGetSystemState() failed due to insufficient buffer space.",
eStatus ) );
}
else
{
for( i = 0; i < uxTasksWritten; i++ )
{
pulCustomMetricsTaskNumbers[ i ] = pxTaskStatusList[ i ].xTaskNumber;
}
}
}
/* Populate device metrics. */
@ -597,21 +578,30 @@ static bool prvCollectDeviceMetrics( void )
xStatus = true;
xDeviceMetrics.pxNetworkStats = &( xNetworkStats );
xDeviceMetrics.pusOpenTcpPortsArray = &( pusOpenTcpPorts[ 0 ] );
xDeviceMetrics.ulOpenTcpPortsArrayLength = ulNumOpenTcpPorts;
xDeviceMetrics.xOpenTcpPortsArrayLength = xNumOpenTcpPorts;
xDeviceMetrics.pusOpenUdpPortsArray = &( pusOpenUdpPorts[ 0 ] );
xDeviceMetrics.ulOpenUdpPortsArrayLength = ulNumOpenUdpPorts;
xDeviceMetrics.xOpenUdpPortsArrayLength = xNumOpenUdpPorts;
xDeviceMetrics.pxEstablishedConnectionsArray = &( pxEstablishedConnections[ 0 ] );
xDeviceMetrics.ulEstablishedConnectionsArrayLength = ulNumEstablishedConnections;
xDeviceMetrics.xEstablishedConnectionsArrayLength = xNumEstablishedConnections;
xDeviceMetrics.ulStackHighWaterMark = pxTaskStatus.usStackHighWaterMark;
xDeviceMetrics.pulTaskIdArray = pulCustomMetricsTaskNumbers;
xDeviceMetrics.ulTaskIdArrayLength = uxTasksWritten;
xDeviceMetrics.pxTaskStatusArray = pxTaskStatusArray;
xDeviceMetrics.xTaskStatusArrayLength = uxTasksWritten;
}
else
{
/* Free pxTaskStatusArray if we allocated it but did not add it to the
* xDeviceMetrics struct. */
if( pxTaskStatusArray != NULL )
{
vPortFree( pxTaskStatusArray );
}
}
return xStatus;
}
/*-----------------------------------------------------------*/
static bool prvGenerateDeviceMetricsReport( uint32_t * pulOutReportLength )
static bool prvGenerateDeviceMetricsReport( size_t * pxOutReportLength )
{
bool xStatus = false;
eReportBuilderStatus eReportBuilderStatus;
@ -624,7 +614,7 @@ static bool prvGenerateDeviceMetricsReport( uint32_t * pulOutReportLength )
democonfigDEVICE_METRICS_REPORT_MAJOR_VERSION,
democonfigDEVICE_METRICS_REPORT_MINOR_VERSION,
ulReportId,
pulOutReportLength );
pxOutReportLength );
if( eReportBuilderStatus != eReportBuilderSuccess )
{
@ -634,7 +624,7 @@ static bool prvGenerateDeviceMetricsReport( uint32_t * pulOutReportLength )
else
{
LogDebug( ( "Generated Report: %.*s.",
*pulOutReportLength,
*pxOutReportLength,
&( pcDeviceMetricsJsonReport[ 0 ] ) ) );
xStatus = true;
}
@ -699,13 +689,13 @@ static bool prvUnsubscribeFromDefenderTopics( void )
}
/*-----------------------------------------------------------*/
static bool prvPublishDeviceMetricsReport( uint32_t reportLength )
static bool prvPublishDeviceMetricsReport( size_t xReportLength )
{
return xPublishToTopic( &xMqttContext,
DEFENDER_API_JSON_PUBLISH( democonfigTHING_NAME ),
DEFENDER_API_LENGTH_JSON_PUBLISH( THING_NAME_LENGTH ),
&( pcDeviceMetricsJsonReport[ 0 ] ),
reportLength );
xReportLength );
}
/*-----------------------------------------------------------*/
@ -732,7 +722,8 @@ void prvDefenderDemoTask( void * pvParameters )
{
bool xStatus = false;
BaseType_t xExitStatus = EXIT_FAILURE;
uint32_t ulReportLength = 0UL, i, ulMqttSessionEstablished = 0UL;
uint32_t ulReportLength = 0UL, i;
bool xMqttSessionEstablished = false;
UBaseType_t uxDemoRunCount = 0UL;
/* Remove compiler warnings about unused parameters. */
@ -777,7 +768,7 @@ void prvDefenderDemoTask( void * pvParameters )
}
else
{
ulMqttSessionEstablished = 1;
xMqttSessionEstablished = true;
}
/******************** Subscribe to Defender topics. *******************/
@ -853,6 +844,13 @@ void prvDefenderDemoTask( void * pvParameters )
LogInfo( ( "Generating Device Defender report..." ) );
xStatus = prvGenerateDeviceMetricsReport( &( ulReportLength ) );
/* Free the allocated array in xDeviceMetrics struct which is not
* used anymore after prvGenerateDeviceMetricsReport(). This code is
* only reached when prvCollectDeviceMetrics succeeded, so
* xDeviceMetrics.pxTaskStatusArray is a valid allocation that needs
* to be freed. */
vPortFree( xDeviceMetrics.pxTaskStatusArray );
if( xStatus != true )
{
LogError( ( "Failed to generate Device Defender report." ) );
@ -908,7 +906,7 @@ void prvDefenderDemoTask( void * pvParameters )
* protocol spec, it is okay to send UNSUBSCRIBE even if no corresponding
* subscription exists on the broker. Therefore, it is okay to attempt
* unsubscribe even if one more subscribe failed earlier. */
if( ulMqttSessionEstablished == 1 )
if( xMqttSessionEstablished )
{
LogInfo( ( "Unsubscribing from defender topics..." ) );
xStatus = prvUnsubscribeFromDefenderTopics();

@ -75,12 +75,12 @@ eMetricsCollectorStatus eGetNetworkStats( NetworkStats_t * pxOutNetworkStats )
/* Fill our response with values gotten from FreeRTOS+TCP. */
if( eStatus == eMetricsCollectorSuccess )
{
LogDebug( ( "Network stats read. Bytes received: %u, packets received: %u, "
"bytes sent: %u, packets sent: %u.",
( unsigned int ) xMetrics.xInput.uxByteCount,
( unsigned int ) xMetrics.xInput.uxPacketCount,
( unsigned int ) xMetrics.xOutput.uxByteCount,
( unsigned int ) xMetrics.xOutput.uxPacketCount ) );
LogDebug( ( "Network stats read. Bytes received: %lu, packets received: %lu, "
"bytes sent: %lu, packets sent: %lu.",
( unsigned long ) xMetrics.xInput.uxByteCount,
( unsigned long ) xMetrics.xInput.uxPacketCount,
( unsigned long ) xMetrics.xOutput.uxByteCount,
( unsigned long ) xMetrics.xOutput.uxPacketCount ) );
pxOutNetworkStats->ulBytesReceived = xMetrics.xInput.uxByteCount;
pxOutNetworkStats->ulPacketsReceived = xMetrics.xInput.uxPacketCount;
@ -93,17 +93,17 @@ eMetricsCollectorStatus eGetNetworkStats( NetworkStats_t * pxOutNetworkStats )
/*-----------------------------------------------------------*/
eMetricsCollectorStatus eGetOpenTcpPorts( uint16_t * pusOutTcpPortsArray,
uint32_t ulTcpPortsArrayLength,
uint32_t * pulOutNumTcpOpenPorts )
size_t xTcpPortsArrayLength,
size_t * pxOutNumTcpOpenPorts )
{
eMetricsCollectorStatus eStatus = eMetricsCollectorSuccess;
MetricsType_t xMetrics = { 0 };
BaseType_t xMetricsStatus = 0;
uint32_t ulCopyAmount = 0UL;
size_t xCopyAmount = 0UL;
/* pusOutTcpPortsArray can be NULL. */
configASSERT( pulOutNumTcpOpenPorts != NULL );
configASSERT( pxOutNumTcpOpenPorts != NULL );
/* Get metrics from FreeRTOS+TCP tcp_netstat utility. */
xMetricsStatus = vGetMetrics( &xMetrics );
@ -121,24 +121,24 @@ eMetricsCollectorStatus eGetOpenTcpPorts( uint16_t * pusOutTcpPortsArray,
* given array. */
if( pusOutTcpPortsArray != NULL )
{
ulCopyAmount = xMetrics.xTCPPortList.uxCount;
xCopyAmount = xMetrics.xTCPPortList.uxCount;
/* Limit the copied ports to what can fit in the output array. */
if( ulTcpPortsArrayLength < xMetrics.xTCPPortList.uxCount )
if( xTcpPortsArrayLength < xMetrics.xTCPPortList.uxCount )
{
LogWarn( ( "Ports returned truncated due to insufficient buffer size." ) );
ulCopyAmount = ulTcpPortsArrayLength;
xCopyAmount = xTcpPortsArrayLength;
}
memcpy( pusOutTcpPortsArray, &xMetrics.xTCPPortList.usTCPPortList, ulCopyAmount * sizeof( uint16_t ) );
memcpy( pusOutTcpPortsArray, &xMetrics.xTCPPortList.usTCPPortList, xCopyAmount * sizeof( uint16_t ) );
/* Return the number of elements copied to the array. */
*pulOutNumTcpOpenPorts = ulCopyAmount;
*pxOutNumTcpOpenPorts = xCopyAmount;
}
else
{
/* Return the total number of open ports. */
*pulOutNumTcpOpenPorts = xMetrics.xTCPPortList.uxCount;
*pxOutNumTcpOpenPorts = xMetrics.xTCPPortList.uxCount;
}
}
@ -147,17 +147,17 @@ eMetricsCollectorStatus eGetOpenTcpPorts( uint16_t * pusOutTcpPortsArray,
/*-----------------------------------------------------------*/
eMetricsCollectorStatus eGetOpenUdpPorts( uint16_t * pusOutUdpPortsArray,
uint32_t ulUdpPortsArrayLength,
uint32_t * pulOutNumUdpOpenPorts )
size_t xUdpPortsArrayLength,
size_t * pxOutNumUdpOpenPorts )
{
eMetricsCollectorStatus eStatus = eMetricsCollectorSuccess;
MetricsType_t xMetrics = { 0 };
BaseType_t xMetricsStatus = 0;
uint32_t ulCopyAmount = 0UL;
uint32_t xCopyAmount = 0UL;
/* pusOutUdpPortsArray can be NULL. */
configASSERT( pulOutNumUdpOpenPorts != NULL );
configASSERT( pxOutNumUdpOpenPorts != NULL );
/* Get metrics from FreeRTOS+TCP tcp_netstat utility. */
xMetricsStatus = vGetMetrics( &xMetrics );
@ -175,24 +175,24 @@ eMetricsCollectorStatus eGetOpenUdpPorts( uint16_t * pusOutUdpPortsArray,
* given array. */
if( pusOutUdpPortsArray != NULL )
{
ulCopyAmount = xMetrics.xUDPPortList.uxCount;
xCopyAmount = xMetrics.xUDPPortList.uxCount;
/* Limit the copied ports to what can fit in the output array. */
if( ulUdpPortsArrayLength < xMetrics.xUDPPortList.uxCount )
if( xUdpPortsArrayLength < xMetrics.xUDPPortList.uxCount )
{
LogWarn( ( "Ports returned truncated due to insufficient buffer size." ) );
ulCopyAmount = ulUdpPortsArrayLength;
xCopyAmount = xUdpPortsArrayLength;
}
memcpy( pusOutUdpPortsArray, &xMetrics.xUDPPortList.usUDPPortList, ulCopyAmount * sizeof( uint16_t ) );
memcpy( pusOutUdpPortsArray, &xMetrics.xUDPPortList.usUDPPortList, xCopyAmount * sizeof( uint16_t ) );
/* Return the number of elements copied to the array. */
*pulOutNumUdpOpenPorts = ulCopyAmount;
*pxOutNumUdpOpenPorts = xCopyAmount;
}
else
{
/* Return the total number of open ports. */
*pulOutNumUdpOpenPorts = xMetrics.xUDPPortList.uxCount;
*pxOutNumUdpOpenPorts = xMetrics.xUDPPortList.uxCount;
}
}
@ -202,19 +202,19 @@ eMetricsCollectorStatus eGetOpenUdpPorts( uint16_t * pusOutUdpPortsArray,
/*-----------------------------------------------------------*/
eMetricsCollectorStatus eGetEstablishedConnections( Connection_t * pxOutConnectionsArray,
uint32_t ulConnectionsArrayLength,
uint32_t * pulOutNumEstablishedConnections )
size_t xConnectionsArrayLength,
size_t * pxOutNumEstablishedConnections )
{
eMetricsCollectorStatus eStatus = eMetricsCollectorSuccess;
MetricsType_t xMetrics = { 0 };
BaseType_t xMetricsStatus = 0;
uint32_t ulCopyAmount = 0UL;
size_t xCopyAmount = 0UL;
uint32_t ulLocalIp = 0UL;
uint32_t i;
/* pxOutConnectionsArray can be NULL. */
configASSERT( pulOutNumEstablishedConnections != NULL );
configASSERT( pxOutNumEstablishedConnections != NULL );
/* Get metrics from FreeRTOS+TCP tcp_netstat utility. */
xMetricsStatus = vGetMetrics( &xMetrics );
@ -232,19 +232,19 @@ eMetricsCollectorStatus eGetEstablishedConnections( Connection_t * pxOutConnecti
* the given array. */
if( pxOutConnectionsArray != NULL )
{
ulCopyAmount = xMetrics.xTCPSocketList.uxCount;
xCopyAmount = xMetrics.xTCPSocketList.uxCount;
/* Get local IP as the tcp_netstat utility does not give it. */
ulLocalIp = FreeRTOS_GetIPAddress();
/* Limit the outputted connections to what can fit in the output array. */
if( ulConnectionsArrayLength < xMetrics.xTCPSocketList.uxCount )
if( xConnectionsArrayLength < xMetrics.xTCPSocketList.uxCount )
{
LogWarn( ( "Ports returned truncated due to insufficient buffer size." ) );
ulCopyAmount = ulConnectionsArrayLength;
xCopyAmount = xConnectionsArrayLength;
}
for( i = 0; i < ulCopyAmount; i++ )
for( i = 0; i < xCopyAmount; i++ )
{
pxOutConnectionsArray[ i ].ulLocalIp = ulLocalIp;
pxOutConnectionsArray[ i ].usLocalPort =
@ -256,12 +256,12 @@ eMetricsCollectorStatus eGetEstablishedConnections( Connection_t * pxOutConnecti
}
/* Return the number of elements copied to the array. */
*pulOutNumEstablishedConnections = ulCopyAmount;
*pxOutNumEstablishedConnections = xCopyAmount;
}
else
{
/* Return the total number of established connections. */
*pulOutNumEstablishedConnections = xMetrics.xTCPSocketList.uxCount;
*pxOutNumEstablishedConnections = xMetrics.xTCPSocketList.uxCount;
}
}

@ -89,9 +89,9 @@ eMetricsCollectorStatus eGetNetworkStats( NetworkStats_t * pxOutNetworkStats );
*
* @param[out] pusOutTcpPortsArray The array to write the open TCP ports into. This
* can be NULL, if only the number of open ports is needed.
* @param[in] ulTcpPortsArrayLength Length of the pusOutTcpPortsArray, if it is not
* @param[in] xTcpPortsArrayLength Length of the pusOutTcpPortsArray, if it is not
* NULL.
* @param[out] pulOutNumTcpOpenPorts Number of open TCP ports if @p
* @param[out] pxOutNumTcpOpenPorts Number of open TCP ports if @p
* pusOutTcpPortsArray NULL, else number of TCP ports written.
*
* @return #eMetricsCollectorSuccess if open TCP ports are successfully obtained;
@ -99,8 +99,8 @@ eMetricsCollectorStatus eGetNetworkStats( NetworkStats_t * pxOutNetworkStats );
* #eMetricsCollectorCollectionFailed if the collection methods failed.
*/
eMetricsCollectorStatus eGetOpenTcpPorts( uint16_t * pusOutTcpPortsArray,
uint32_t ulTcpPortsArrayLength,
uint32_t * pulOutNumTcpOpenPorts );
size_t xTcpPortsArrayLength,
size_t * pxOutNumTcpOpenPorts );
/**
* @brief Get a list of the open UDP ports.
@ -110,9 +110,9 @@ eMetricsCollectorStatus eGetOpenTcpPorts( uint16_t * pusOutTcpPortsArray,
*
* @param[out] pusOutUdpPortsArray The array to write the open UDP ports into. Can
* be NULL, if only number of open ports is needed.
* @param[in] ulUdpPortsArrayLength Length of the pusOutUdpPortsArray, if it is not
* @param[in] xUdpPortsArrayLength Length of the pusOutUdpPortsArray, if it is not
* NULL.
* @param[out] pulOutNumUdpOpenPorts Number of open UDP ports if @p
* @param[out] pxOutNumUdpOpenPorts Number of open UDP ports if @p
* pusOutUdpPortsArray NULL, else number of UDP ports written.
*
* @return #eMetricsCollectorSuccess if open UDP ports are successfully obtained;
@ -120,8 +120,8 @@ eMetricsCollectorStatus eGetOpenTcpPorts( uint16_t * pusOutTcpPortsArray,
* #eMetricsCollectorCollectionFailed if the collection methods failed.
*/
eMetricsCollectorStatus eGetOpenUdpPorts( uint16_t * pusOutUdpPortsArray,
uint32_t ulUdpPortsArrayLength,
uint32_t * pulOutNumUdpOpenPorts );
size_t xUdpPortsArrayLength,
size_t * pxOutNumUdpOpenPorts );
/**
* @brief Get a list of established connections.
@ -133,17 +133,17 @@ eMetricsCollectorStatus eGetOpenUdpPorts( uint16_t * pusOutUdpPortsArray,
* @param[out] pxOutConnectionsArray The array to write the established connections
* into. This can be NULL, if only the number of established connections is
* needed.
* @param[in] ulConnectionsArrayLength Length of the pxOutConnectionsArray, if it
* @param[in] xConnectionsArrayLength Length of the pxOutConnectionsArray, if it
* is not NULL.
* @param[out] pulOutNumEstablishedConnections Number of established connections if @p
* pusOutNumEstablishedConnections NULL, else number of established connections written.
* @param[out] pxOutNumEstablishedConnections Number of established connections if @p
* pxOutNumEstablishedConnections NULL, else number of established connections written.
*
* @return #eMetricsCollectorSuccess if established connections are successfully obtained;
* #eMetricsCollectorBadParameter if invalid parameters are passed;
* #eMetricsCollectorCollectionFailed if the collection methods failed.
*/
eMetricsCollectorStatus eGetEstablishedConnections( Connection_t * pxOutConnectionsArray,
uint32_t ulConnectionsArrayLength,
uint32_t * pulOutNumEstablishedConnections );
size_t xConnectionsArrayLength,
size_t * pxOutNumEstablishedConnections );
#endif /* ifndef METRICS_COLLECTOR_H_ */

@ -132,19 +132,19 @@
* ]
*
* @param[in] pcBuffer The buffer to write the ports array.
* @param[in] ulBufferLength The length of the buffer.
* @param[in] xBufferLength The length of the buffer.
* @param[in] pusOpenPortsArray The array containing the open ports.
* @param[in] ulOpenPortsArrayLength Length of the pusOpenPortsArray array.
* @param[out] pulOutCharsWritten Number of characters written to the buffer.
* @param[in] xOpenPortsArrayLength Length of the pusOpenPortsArray array.
* @param[out] pxOutCharsWritten Number of characters written to the buffer.
*
* @return #ReportBuilderSuccess if the array is successfully written;
* #ReportBuilderBufferTooSmall if the buffer cannot hold the full array.
*/
static eReportBuilderStatus prvWritePortsArray( char * pcBuffer,
uint32_t ulBufferLength,
size_t xBufferLength,
const uint16_t * pusOpenPortsArray,
uint32_t ulOpenPortsArrayLength,
uint32_t * pulOutCharsWritten );
size_t xOpenPortsArrayLength,
size_t * pxOutCharsWritten );
/**
* @brief Write established connections array to the given buffer in the format
@ -163,59 +163,60 @@ static eReportBuilderStatus prvWritePortsArray( char * pcBuffer,
* ]
*
* @param[in] pcBuffer The buffer to write the connections array.
* @param[in] ulBufferLength The length of the buffer.
* @param[in] xBufferLength The length of the buffer.
* @param[in] pxConnectionsArray The array containing the established connections.
* @param[in] ulConnectionsArrayLength Length of the pxConnectionsArray array.
* @param[out] pulOutCharsWritten Number of characters written to the buffer.
* @param[in] xConnectionsArrayLength Length of the pxConnectionsArray array.
* @param[out] pxOutCharsWritten Number of characters written to the buffer.
*
* @return #ReportBuilderSuccess if the array is successfully written;
* #ReportBuilderBufferTooSmall if the buffer cannot hold the full array.
*/
static eReportBuilderStatus prvWriteConnectionsArray( char * pcBuffer,
uint32_t ulBufferLength,
size_t xBufferLength,
const Connection_t * pxConnectionsArray,
uint32_t ulConnectionsArrayLength,
uint32_t * pulOutCharsWritten );
size_t xConnectionsArrayLength,
size_t * pxOutCharsWritten );
/**
* @brief Write task ID array to the given buffer as a JSON array.
*
* @param[in] pcBuffer The buffer to write the array of task IDs.
* @param[in] ulBufferLength The length of the buffer.
* @param[in] pulTaskIdArray The array containing the task IDs.
* @param[in] pulTaskIdArrayLength Length of the pulTaskIdsArray array.
* @param[out] pulOutCharsWritten Number of characters written to the buffer.
* @param[in] xBufferLength The length of the buffer.
* @param[in] pxTaskStatusArray The array containing the task statuses.
* @param[in] xTaskStatusArrayLength Length of the pxTaskStatusArray array.
* @param[out] pxOutCharsWritten Number of characters written to the buffer.
*
* @return #ReportBuilderSuccess if the array is successfully written;
* #ReportBuilderBufferTooSmall if the buffer cannot hold the full array.
*/
static eReportBuilderStatus prvWriteTaskIdArray( char * pcBuffer,
uint32_t ulBufferLength,
const uint32_t * pulTaskIdArray,
uint32_t pulTaskIdArrayLength,
uint32_t * pulOutCharsWritten );
size_t xBufferLength,
const TaskStatus_t * pxTaskStatusArray,
size_t xTaskStatusArrayLength,
size_t * pxOutCharsWritten );
/*-----------------------------------------------------------*/
static eReportBuilderStatus prvWritePortsArray( char * pcBuffer,
uint32_t ulBufferLength,
uint32_t xBufferLength,
const uint16_t * pusOpenPortsArray,
uint32_t ulOpenPortsArrayLength,
uint32_t * pulOutCharsWritten )
uint32_t xOpenPortsArrayLength,
uint32_t * pxOutCharsWritten )
{
char * pcCurrentWritePos = pcBuffer;
uint32_t i, ulRemainingBufferLength = ulBufferLength;
uint32_t i;
size_t xRemainingBufferLength = xBufferLength;
int32_t lCharactersWritten;
eReportBuilderStatus eStatus = eReportBuilderSuccess;
configASSERT( pcBuffer != NULL );
configASSERT( pusOpenPortsArray != NULL );
configASSERT( pulOutCharsWritten != NULL );
configASSERT( pxOutCharsWritten != NULL );
/* Write the JSON array open marker. */
if( ulRemainingBufferLength > 1 )
if( xRemainingBufferLength > 1 )
{
*pcCurrentWritePos = reportbuilderJSON_ARRAY_OPEN_MARKER;
ulRemainingBufferLength -= 1;
xRemainingBufferLength -= 1;
pcCurrentWritePos += 1;
}
else
@ -224,20 +225,20 @@ static eReportBuilderStatus prvWritePortsArray( char * pcBuffer,
}
/* Write the array elements. */
for( i = 0; ( ( i < ulOpenPortsArrayLength ) && ( eStatus == eReportBuilderSuccess ) ); i++ )
for( i = 0; ( ( i < xOpenPortsArrayLength ) && ( eStatus == eReportBuilderSuccess ) ); i++ )
{
lCharactersWritten = snprintf( pcCurrentWritePos,
ulRemainingBufferLength,
xRemainingBufferLength,
reportbuilderJSON_PORT_OBJECT_FORMAT,
pusOpenPortsArray[ i ] );
if( !reportbuilderSNPRINTF_SUCCESS( lCharactersWritten, ulRemainingBufferLength ) )
if( !reportbuilderSNPRINTF_SUCCESS( lCharactersWritten, xRemainingBufferLength ) )
{
eStatus = eReportBuilderBufferTooSmall;
}
else
{
ulRemainingBufferLength -= ( uint32_t ) lCharactersWritten;
xRemainingBufferLength -= ( uint32_t ) lCharactersWritten;
pcCurrentWritePos += lCharactersWritten;
}
}
@ -245,19 +246,19 @@ static eReportBuilderStatus prvWritePortsArray( char * pcBuffer,
if( eStatus == eReportBuilderSuccess )
{
/* Discard the last comma. */
if( ulOpenPortsArrayLength > 0 )
if( xOpenPortsArrayLength > 0 )
{
pcCurrentWritePos -= 1;
ulRemainingBufferLength += 1;
xRemainingBufferLength += 1;
}
/* Write the JSON array close marker. */
if( ulRemainingBufferLength > 1 )
if( xRemainingBufferLength > 1 )
{
*pcCurrentWritePos = reportbuilderJSON_ARRAY_CLOSE_MARKER;
ulRemainingBufferLength -= 1;
xRemainingBufferLength -= 1;
pcCurrentWritePos += 1;
*pulOutCharsWritten = ulBufferLength - ulRemainingBufferLength;
*pxOutCharsWritten = xBufferLength - xRemainingBufferLength;
}
else
{
@ -270,26 +271,27 @@ static eReportBuilderStatus prvWritePortsArray( char * pcBuffer,
/*-----------------------------------------------------------*/
static eReportBuilderStatus prvWriteConnectionsArray( char * pcBuffer,
uint32_t ulBufferLength,
size_t xBufferLength,
const Connection_t * pxConnectionsArray,
uint32_t ulConnectionsArrayLength,
uint32_t * pulOutCharsWritten )
size_t xConnectionsArrayLength,
size_t * pxOutCharsWritten )
{
char * pcCurrentWritePos = pcBuffer;
uint32_t i, ulRemainingBufferLength = ulBufferLength;
uint32_t i;
size_t xRemainingBufferLength = xBufferLength;
int32_t lCharactersWritten;
eReportBuilderStatus eStatus = eReportBuilderSuccess;
const Connection_t * pxConn;
configASSERT( pcBuffer != NULL );
configASSERT( pxConnectionsArray != NULL );
configASSERT( pulOutCharsWritten != NULL );
configASSERT( pxOutCharsWritten != NULL );
/* Write the JSON array open marker. */
if( ulRemainingBufferLength > 1 )
if( xRemainingBufferLength > 1 )
{
*pcCurrentWritePos = reportbuilderJSON_ARRAY_OPEN_MARKER;
ulRemainingBufferLength -= 1;
xRemainingBufferLength -= 1;
pcCurrentWritePos += 1;
}
else
@ -298,11 +300,11 @@ static eReportBuilderStatus prvWriteConnectionsArray( char * pcBuffer,
}
/* Write the array elements. */
for( i = 0; ( ( i < ulConnectionsArrayLength ) && ( eStatus == eReportBuilderSuccess ) ); i++ )
for( i = 0; ( ( i < xConnectionsArrayLength ) && ( eStatus == eReportBuilderSuccess ) ); i++ )
{
pxConn = &( pxConnectionsArray[ i ] );
lCharactersWritten = snprintf( pcCurrentWritePos,
ulRemainingBufferLength,
xRemainingBufferLength,
reportbuilderJSON_CONNECTION_OBJECT_FORMAT,
pxConn->usLocalPort,
( pxConn->ulRemoteIp >> 24 ) & 0xFF,
@ -311,13 +313,13 @@ static eReportBuilderStatus prvWriteConnectionsArray( char * pcBuffer,
( pxConn->ulRemoteIp ) & 0xFF,
pxConn->usRemotePort );
if( !reportbuilderSNPRINTF_SUCCESS( lCharactersWritten, ulRemainingBufferLength ) )
if( !reportbuilderSNPRINTF_SUCCESS( lCharactersWritten, xRemainingBufferLength ) )
{
eStatus = eReportBuilderBufferTooSmall;
}
else
{
ulRemainingBufferLength -= lCharactersWritten;
xRemainingBufferLength -= lCharactersWritten;
pcCurrentWritePos += lCharactersWritten;
}
}
@ -325,19 +327,19 @@ static eReportBuilderStatus prvWriteConnectionsArray( char * pcBuffer,
if( eStatus == eReportBuilderSuccess )
{
/* Discard the last comma. */
if( ulConnectionsArrayLength > 0 )
if( xConnectionsArrayLength > 0 )
{
pcCurrentWritePos -= 1;
ulRemainingBufferLength += 1;
xRemainingBufferLength += 1;
}
/* Write the JSON array close marker. */
if( ulRemainingBufferLength > 1 )
if( xRemainingBufferLength > 1 )
{
*pcCurrentWritePos = reportbuilderJSON_ARRAY_CLOSE_MARKER;
ulRemainingBufferLength -= 1;
xRemainingBufferLength -= 1;
pcCurrentWritePos += 1;
*pulOutCharsWritten = ulBufferLength - ulRemainingBufferLength;
*pxOutCharsWritten = xBufferLength - xRemainingBufferLength;
}
else
{
@ -350,25 +352,26 @@ static eReportBuilderStatus prvWriteConnectionsArray( char * pcBuffer,
/*-----------------------------------------------------------*/
static eReportBuilderStatus prvWriteTaskIdArray( char * pcBuffer,
uint32_t ulBufferLength,
const uint32_t * pulTaskIdArray,
uint32_t pulTaskIdArrayLength,
uint32_t * pulOutCharsWritten )
size_t xBufferLength,
const TaskStatus_t * pxTaskStatusArray,
size_t xTaskStatusArrayLength,
size_t * pxOutCharsWritten )
{
char * pcCurrentWritePos = pcBuffer;
uint32_t i, ulRemainingBufferLength = ulBufferLength;
uint32_t i;
size_t xRemainingBufferLength = xBufferLength;
int32_t lCharactersWritten;
eReportBuilderStatus eStatus = eReportBuilderSuccess;
configASSERT( pcBuffer != NULL );
configASSERT( pulTaskIdArray != NULL );
configASSERT( pulOutCharsWritten != NULL );
configASSERT( pxTaskStatusArray != NULL );
configASSERT( pxOutCharsWritten != NULL );
/* Write the JSON array open marker. */
if( ulRemainingBufferLength > 1 )
if( xRemainingBufferLength > 1 )
{
*pcCurrentWritePos = reportbuilderJSON_ARRAY_OPEN_MARKER;
ulRemainingBufferLength -= 1;
xRemainingBufferLength -= 1;
pcCurrentWritePos += 1;
}
else
@ -377,20 +380,20 @@ static eReportBuilderStatus prvWriteTaskIdArray( char * pcBuffer,
}
/* Write the array elements. */
for( i = 0; ( ( i < pulTaskIdArrayLength ) && ( eStatus == eReportBuilderSuccess ) ); i++ )
for( i = 0; ( ( i < xTaskStatusArrayLength ) && ( eStatus == eReportBuilderSuccess ) ); i++ )
{
lCharactersWritten = snprintf( pcCurrentWritePos,
ulRemainingBufferLength,
xRemainingBufferLength,
"%u,",
pulTaskIdArray[ i ] );
pxTaskStatusArray[ i ].xTaskNumber );
if( !reportbuilderSNPRINTF_SUCCESS( lCharactersWritten, ulRemainingBufferLength ) )
if( !reportbuilderSNPRINTF_SUCCESS( lCharactersWritten, xRemainingBufferLength ) )
{
eStatus = eReportBuilderBufferTooSmall;
}
else
{
ulRemainingBufferLength -= ( uint32_t ) lCharactersWritten;
xRemainingBufferLength -= ( uint32_t ) lCharactersWritten;
pcCurrentWritePos += lCharactersWritten;
}
}
@ -398,19 +401,19 @@ static eReportBuilderStatus prvWriteTaskIdArray( char * pcBuffer,
if( eStatus == eReportBuilderSuccess )
{
/* Discard the last comma. */
if( pulTaskIdArrayLength > 0 )
if( xTaskStatusArrayLength > 0 )
{
pcCurrentWritePos -= 1;
ulRemainingBufferLength += 1;
xRemainingBufferLength += 1;
}
/* Write the JSON array close marker. */
if( ulRemainingBufferLength > 1 )
if( xRemainingBufferLength > 1 )
{
*pcCurrentWritePos = reportbuilderJSON_ARRAY_CLOSE_MARKER;
ulRemainingBufferLength -= 1;
xRemainingBufferLength -= 1;
pcCurrentWritePos += 1;
*pulOutCharsWritten = ulBufferLength - ulRemainingBufferLength;
*pxOutCharsWritten = xBufferLength - xRemainingBufferLength;
}
else
{
@ -423,35 +426,35 @@ static eReportBuilderStatus prvWriteTaskIdArray( char * pcBuffer,
/*-----------------------------------------------------------*/
eReportBuilderStatus eGenerateJsonReport( char * pcBuffer,
uint32_t ulBufferLength,
size_t xBufferLength,
const ReportMetrics_t * pxMetrics,
uint32_t ulMajorReportVersion,
uint32_t ulMinorReportVersion,
uint32_t ulReportId,
uint32_t * pulOutReportLength )
size_t * pxOutReportLength )
{
char * pcCurrentWritePos = pcBuffer;
uint32_t ulRemainingBufferLength = ulBufferLength;
size_t xRemainingBufferLength = xBufferLength;
uint32_t bufferWritten;
eReportBuilderStatus eStatus = eReportBuilderSuccess;
int32_t lCharactersWritten;
configASSERT( pcBuffer != NULL );
configASSERT( pxMetrics != NULL );
configASSERT( pulOutReportLength != NULL );
configASSERT( ulBufferLength != 0 );
configASSERT( pxOutReportLength != NULL );
configASSERT( xBufferLength != 0 );
if( ( pcBuffer == NULL ) ||
( ulBufferLength == 0 ) ||
( xBufferLength == 0 ) ||
( pxMetrics == NULL ) ||
( pulOutReportLength == NULL ) )
( pxOutReportLength == NULL ) )
{
LogError( ( "Invalid parameters. pcBuffer: %p, ulBufferLength: %u"
LogError( ( "Invalid parameters. pcBuffer: %p, xBufferLength: %u"
" pMetrics: %p, pOutReprotLength: %p.",
pcBuffer,
ulBufferLength,
xBufferLength,
pxMetrics,
pulOutReportLength ) );
pxOutReportLength ) );
eStatus = eReportBuilderBadParameter;
}
@ -459,20 +462,20 @@ eReportBuilderStatus eGenerateJsonReport( char * pcBuffer,
if( eStatus == eReportBuilderSuccess )
{
lCharactersWritten = snprintf( pcCurrentWritePos,
ulRemainingBufferLength,
xRemainingBufferLength,
reportbuilderJSON_REPORT_FORMAT_PART1,
ulReportId,
ulMajorReportVersion,
ulMinorReportVersion );
if( !reportbuilderSNPRINTF_SUCCESS( lCharactersWritten, ulRemainingBufferLength ) )
if( !reportbuilderSNPRINTF_SUCCESS( lCharactersWritten, xRemainingBufferLength ) )
{
LogError( ( "Failed to write part 1." ) );
eStatus = eReportBuilderBufferTooSmall;
}
else
{
ulRemainingBufferLength -= lCharactersWritten;
xRemainingBufferLength -= lCharactersWritten;
pcCurrentWritePos += lCharactersWritten;
}
}
@ -481,15 +484,15 @@ eReportBuilderStatus eGenerateJsonReport( char * pcBuffer,
if( eStatus == eReportBuilderSuccess )
{
eStatus = prvWritePortsArray( pcCurrentWritePos,
ulRemainingBufferLength,
xRemainingBufferLength,
pxMetrics->pusOpenTcpPortsArray,
pxMetrics->ulOpenTcpPortsArrayLength,
pxMetrics->xOpenTcpPortsArrayLength,
&( bufferWritten ) );
if( eStatus == eReportBuilderSuccess )
{
pcCurrentWritePos += bufferWritten;
ulRemainingBufferLength -= bufferWritten;
xRemainingBufferLength -= bufferWritten;
}
else
{
@ -501,18 +504,18 @@ eReportBuilderStatus eGenerateJsonReport( char * pcBuffer,
if( eStatus == eReportBuilderSuccess )
{
lCharactersWritten = snprintf( pcCurrentWritePos,
ulRemainingBufferLength,
xRemainingBufferLength,
reportbuilderJSON_REPORT_FORMAT_PART2,
pxMetrics->ulOpenTcpPortsArrayLength );
pxMetrics->xOpenTcpPortsArrayLength );
if( !reportbuilderSNPRINTF_SUCCESS( lCharactersWritten, ulRemainingBufferLength ) )
if( !reportbuilderSNPRINTF_SUCCESS( lCharactersWritten, xRemainingBufferLength ) )
{
LogError( ( "Failed to write part 2." ) );
eStatus = eReportBuilderBufferTooSmall;
}
else
{
ulRemainingBufferLength -= lCharactersWritten;
xRemainingBufferLength -= lCharactersWritten;
pcCurrentWritePos += lCharactersWritten;
}
}
@ -521,15 +524,15 @@ eReportBuilderStatus eGenerateJsonReport( char * pcBuffer,
if( eStatus == eReportBuilderSuccess )
{
eStatus = prvWritePortsArray( pcCurrentWritePos,
ulRemainingBufferLength,
xRemainingBufferLength,
pxMetrics->pusOpenUdpPortsArray,
pxMetrics->ulOpenUdpPortsArrayLength,
pxMetrics->xOpenUdpPortsArrayLength,
&( bufferWritten ) );
if( eStatus == eReportBuilderSuccess )
{
pcCurrentWritePos += bufferWritten;
ulRemainingBufferLength -= bufferWritten;
xRemainingBufferLength -= bufferWritten;
}
else
{
@ -541,23 +544,23 @@ eReportBuilderStatus eGenerateJsonReport( char * pcBuffer,
if( eStatus == eReportBuilderSuccess )
{
lCharactersWritten = snprintf( pcCurrentWritePos,
ulRemainingBufferLength,
xRemainingBufferLength,
reportbuilderJSON_REPORT_FORMAT_PART3,
pxMetrics->ulOpenUdpPortsArrayLength,
pxMetrics->xOpenUdpPortsArrayLength,
pxMetrics->pxNetworkStats->ulBytesReceived,
pxMetrics->pxNetworkStats->ulBytesSent,
pxMetrics->pxNetworkStats->ulPacketsReceived,
pxMetrics->pxNetworkStats->ulPacketsSent,
DEFENDER_REPORT_ESTABLISHED_CONNECTIONS_KEY );
if( !reportbuilderSNPRINTF_SUCCESS( lCharactersWritten, ulRemainingBufferLength ) )
if( !reportbuilderSNPRINTF_SUCCESS( lCharactersWritten, xRemainingBufferLength ) )
{
LogError( ( "Failed to write part 3." ) );
eStatus = eReportBuilderBufferTooSmall;
}
else
{
ulRemainingBufferLength -= lCharactersWritten;
xRemainingBufferLength -= lCharactersWritten;
pcCurrentWritePos += lCharactersWritten;
}
}
@ -566,15 +569,15 @@ eReportBuilderStatus eGenerateJsonReport( char * pcBuffer,
if( eStatus == eReportBuilderSuccess )
{
eStatus = prvWriteConnectionsArray( pcCurrentWritePos,
ulRemainingBufferLength,
xRemainingBufferLength,
pxMetrics->pxEstablishedConnectionsArray,
pxMetrics->ulEstablishedConnectionsArrayLength,
pxMetrics->xEstablishedConnectionsArrayLength,
&( bufferWritten ) );
if( eStatus == eReportBuilderSuccess )
{
pcCurrentWritePos += bufferWritten;
ulRemainingBufferLength -= bufferWritten;
xRemainingBufferLength -= bufferWritten;
}
else
{
@ -586,19 +589,19 @@ eReportBuilderStatus eGenerateJsonReport( char * pcBuffer,
if( eStatus == eReportBuilderSuccess )
{
lCharactersWritten = snprintf( pcCurrentWritePos,
ulRemainingBufferLength,
xRemainingBufferLength,
reportbuilderJSON_REPORT_FORMAT_PART4,
pxMetrics->ulEstablishedConnectionsArrayLength,
pxMetrics->xEstablishedConnectionsArrayLength,
pxMetrics->ulStackHighWaterMark );
if( !reportbuilderSNPRINTF_SUCCESS( lCharactersWritten, ulRemainingBufferLength ) )
if( !reportbuilderSNPRINTF_SUCCESS( lCharactersWritten, xRemainingBufferLength ) )
{
LogError( ( "Failed to write part 4." ) );
eStatus = eReportBuilderBufferTooSmall;
}
else
{
ulRemainingBufferLength -= lCharactersWritten;
xRemainingBufferLength -= lCharactersWritten;
pcCurrentWritePos += lCharactersWritten;
}
}
@ -607,15 +610,15 @@ eReportBuilderStatus eGenerateJsonReport( char * pcBuffer,
if( eStatus == eReportBuilderSuccess )
{
eStatus = prvWriteTaskIdArray( pcCurrentWritePos,
ulRemainingBufferLength,
pxMetrics->pulTaskIdArray,
pxMetrics->ulTaskIdArrayLength,
&( bufferWritten ) );
xRemainingBufferLength,
pxMetrics->pxTaskStatusArray,
pxMetrics->xTaskStatusArrayLength,
&( bufferWritten ) );
if( eStatus == eReportBuilderSuccess )
{
pcCurrentWritePos += bufferWritten;
ulRemainingBufferLength -= bufferWritten;
xRemainingBufferLength -= bufferWritten;
}
else
{
@ -627,19 +630,19 @@ eReportBuilderStatus eGenerateJsonReport( char * pcBuffer,
if( eStatus == eReportBuilderSuccess )
{
lCharactersWritten = snprintf( pcCurrentWritePos,
ulRemainingBufferLength,
xRemainingBufferLength,
reportbuilderJSON_REPORT_FORMAT_PART5 );
if( !reportbuilderSNPRINTF_SUCCESS( lCharactersWritten, ulRemainingBufferLength ) )
if( !reportbuilderSNPRINTF_SUCCESS( lCharactersWritten, xRemainingBufferLength ) )
{
LogError( ( "Failed to write part 5." ) );
eStatus = eReportBuilderBufferTooSmall;
}
else
{
ulRemainingBufferLength -= lCharactersWritten;
xRemainingBufferLength -= lCharactersWritten;
pcCurrentWritePos += lCharactersWritten;
*pulOutReportLength = ulBufferLength - ulRemainingBufferLength;
*pxOutReportLength = xBufferLength - xRemainingBufferLength;
}
}

@ -53,15 +53,15 @@ typedef struct ReportMetrics
{
NetworkStats_t * pxNetworkStats;
uint16_t * pusOpenTcpPortsArray;
uint32_t ulOpenTcpPortsArrayLength;
size_t xOpenTcpPortsArrayLength;
uint16_t * pusOpenUdpPortsArray;
uint32_t ulOpenUdpPortsArrayLength;
size_t xOpenUdpPortsArrayLength;
Connection_t * pxEstablishedConnectionsArray;
uint32_t ulEstablishedConnectionsArrayLength;
size_t xEstablishedConnectionsArrayLength;
/* Custom metrics */
uint32_t ulStackHighWaterMark;
uint32_t * pulTaskIdArray;
uint32_t ulTaskIdArrayLength;
TaskStatus_t * pxTaskStatusArray;
size_t xTaskStatusArrayLength;
} ReportMetrics_t;
/**
@ -69,23 +69,23 @@ typedef struct ReportMetrics
* Service.
*
* @param[in] pcBuffer The buffer to write the report into.
* @param[in] ulBufferLength The length of the buffer.
* @param[in] xBufferLength The length of the buffer.
* @param[in] pxMetrics Metrics to write in the generated report.
* @param[in] ulMajorReportVersion Major version of the report.
* @param[in] ulMinorReportVersion Minor version of the report.
* @param[in] ulReportId Value to be used as the ulReportId in the generated report.
* @param[out] pulOutReprotLength The length of the generated report.
* @param[out] pxOutReprotLength The length of the generated report.
*
* @return #ReportBuilderSuccess if the report is successfully generated;
* #ReportBuilderBadParameter if invalid parameters are passed;
* #ReportBuilderBufferTooSmall if the buffer cannot hold the full report.
*/
eReportBuilderStatus eGenerateJsonReport( char * pcBuffer,
uint32_t ulBufferLength,
size_t xBufferLength,
const ReportMetrics_t * pxMetrics,
uint32_t ulMajorReportVersion,
uint32_t ulMinorReportVersion,
uint32_t ulReportId,
uint32_t * pulOutReportLength );
size_t * pxOutReportLength );
#endif /* ifndef REPORT_BUILDER_H_ */

@ -1675,6 +1675,7 @@ prvcheckothertasksarestillrunning
prvchecktask
prvchecktaskcounters
prvchecktimercallback
prvcollectdevicemetrics
prvcomtxtimercallback
prvconnectandcreatedemotasks
prvcopycommand
@ -1703,6 +1704,7 @@ prvfirstregtesttask
prvflashcoroutine
prvflashtimercallback
prvformatcommand
prvgeneratedevicemetricsreport
prvgetdisinheritpriorityaftertimeout
prvgetnextdelaytime
prvgettcbfromhandle
@ -1899,11 +1901,8 @@ pulmemchecktaskrunningcounter
pulnotification
pulnotifiedvalue
pulnumber
puloutcharswritten
puloutnumestablishedconnections
puloutnumtcpopenports
puloutnumudpopenports
puloutreportlength
puloutreprotlength
pulstandardperipheralregister
pulsystemperipheralregister
@ -1912,7 +1911,6 @@ pultaskidarraylength
pultaskidsarray
pusername
pusopenportsarray
pusoutnumestablishedconnections
pusouttcpportsarray
pusoutudpportsarray
putc
@ -1969,8 +1967,14 @@ pxnetworkcontext
pxnetworkcredentials
pxnext
pxopenedinterfacehandle
pxoutcharswritten
pxoutconnectionsarray
pxoutnetworkstats
pxoutnumestablishedconnections
pxoutnumtcpopenports
pxoutnumudpopenports
pxoutreportlength
pxoutreprotlength
pxpacketinfo
pxpathlen
pxport
@ -1987,6 +1991,7 @@ pxstaticstreambuffer
pxstreambuffer
pxsubscriptionlist
pxtaskbuffer
pxtaskstatusarray
pxtcb
pxtickstowait
pxtimeout
@ -2532,14 +2537,12 @@ ulbytesreceived
ulbytessent
ulcalculatedvalue
ulcallcount
ulconnectionsarraylength
ulcoursecyclecounter
ulcurrentversion
ulcyclecount
ulcyclecounter
ulcyclecounters
uldata
uldefenderresponselength
uldemosoftwaretimercounter
uldigestlength
uldirection
@ -2590,7 +2593,6 @@ ulnotificationvalue
ulnotifiedvalue
uloffset
ulong
ulopenportsarraylength
ulpacketsreceived
ulpacketssent
ulport
@ -2605,7 +2607,6 @@ ulreloadvalue
ulreloadvalueforonehighresolutiontick
ulremoteipaddress
ulreportid
ulreportlength
ulrestartoffset
ulreturned
ulsecondnotificationvalueconst
@ -2624,14 +2625,12 @@ ultaskendtrace
ultasknotifytake
ultasknotifytakeindexed
ultasknotifyvalueclearindexed
ultcpportsarraylength
ultim
ultimeoutms
ultimer
ultotalframelength
ultrascale
ultx
uludpportsarraylength
ulvaluetosend
ulwantedbaud
umount
@ -2690,6 +2689,7 @@ usport
usportnumber
usremoteport
usstackdepth
usstackhighwatermark
usstacksize
ustaskstacksize
usthingnamelength
@ -2987,6 +2987,7 @@ xcommand
xcommandqueue
xcomporthandle
xconnectedsocket
xconnectionsarraylength
xcontrolmessagebuffer
xcorebmessagebuffers
xcreatedtask
@ -2995,12 +2996,14 @@ xcurbyte
xdatalength
xdatamessagebuffers
xdb
xdefenderresponselength
xdelayticks
xdeleteresponsereceived
xdeletetaskstack
xdeltams
xdemotimer
xdestinationaddress
xdevicemetrics
xdigitcountertimer
xdirectprint
xdolisten
@ -3130,6 +3133,7 @@ xnumbytessenttotal
xnumreqbytes
xoktogivemutex
xoneshottimer
xopenportsarraylength
xoptionlength
xor
xosel
@ -3225,6 +3229,7 @@ xregtesterror
xregteststacksize
xregteststatus
xregulatoroffidletime
xreportlength
xreportstatus
xrequest
xresponsecount
@ -3328,12 +3333,14 @@ xtaskresumeall
xtaskresumeallcallback
xtaskresumeallstub
xtasksocket
xtaskstatusarraylength
xtaskswaitingtoreceive
xtaskswaitingtosend
xtasktodelete
xtasktonotify
xtcbbuffer
xtcp
xtcpportsarraylength
xtcptestechoclientstaskparams
xtea
xtensa
@ -3369,6 +3376,7 @@ xtxdescriptors
xtxhasended
xtxmessages
xtxtcbbuffer
xudpportsarraylength
xupdatedmessagebuffer
xurllen
xurlparser
@ -3392,4 +3400,4 @@ yyyy
yyyymmddhhmmss
zc
zer
zynq
zynq

Loading…
Cancel
Save