in source/demo-tasks/defender_demo.c [776:916]
void prvDefenderDemoTask( void * pvParameters )
{
bool xStatus = false;
uint32_t ulReportLength = 0UL;
uint32_t ulNotificationValue;
/* Remove compiler warnings about unused parameters. */
( void ) pvParameters;
/* Start with report not received. */
xReportStatus = ReportStatusNotReceived;
/******************** Subscribe to Defender topics. *******************/
/* Attempt to subscribe to the AWS IoT Device Defender topics.
* Since this demo is using JSON, in prvSubscribeToDefenderTopics() we
* subscribe to the topics to which accepted and rejected responses are
* received from after publishing a JSON report.
*
* This demo uses a constant #democonfigCLIENT_IDENTIFIER known at compile time
* therefore we use macros to assemble defender topic strings.
* If the thing name is known at run time, then we could use the API
* #Defender_GetTopic instead.
*
* For example, for the JSON accepted responses topic:
*
* #define TOPIC_BUFFER_LENGTH ( 256U )
*
* // Every device should have a unique thing name registered with AWS IoT Core.
* // This example assumes that the device has a unique serial number which is
* // registered as the thing name with AWS IoT Core.
* const char * pThingName = GetDeviceSerialNumber();
* uint16_t thingNameLength = ( uint16_t )strlen( pThingname );
* char topicBuffer[ TOPIC_BUFFER_LENGTH ] = { 0 };
* uint16_t topicLength = 0;
* DefenderStatus_t status = DefenderSuccess;
*
* status = Defender_GetTopic( &( topicBuffer[ 0 ] ),
* TOPIC_BUFFER_LENGTH,
* pThingName,
* thingNameLength,
* DefenderJsonReportAccepted,
* &( topicLength ) );
*/
LogInfo( ( "Subscribing to defender topics..." ) );
xStatus = prvSubscribeToDefenderTopics();
if( xStatus == true )
{
for( ; ; )
{
xStatus = true;
/* Set a report Id to be used.
*
* !!!NOTE!!!
* This demo sets the report ID to xTaskGetTickCount(), which may collide
* if the device is reset. Reports for a Thing with a previously used
* report ID will be assumed to be duplicates and discarded by the Device
* Defender service. The report ID needs to be unique per report sent with
* a given Thing. We recommend using an increasing unique id such as the
* current timestamp. */
ulReportId = ( uint32_t ) xTaskGetTickCount();
xReportStatus = ReportStatusNotReceived;
/*********************** Collect device metrics. **********************/
/* We then need to collect the metrics that will be sent to the AWS IoT
* Device Defender service. This demo uses the functions declared in
* in metrics_collector.h to collect network metrics. For this demo, the
* implementation of these functions are in metrics_collector.c and
* collects metrics using tcp_netstat utility for FreeRTOS+TCP. */
if( xStatus == true )
{
LogInfo( ( "Collecting device metrics..." ) );
xStatus = prvCollectDeviceMetrics();
if( xStatus != true )
{
LogError( ( "Failed to collect device metrics." ) );
}
}
/********************** Generate defender report. *********************/
/* The data needs to be incorporated into a JSON formatted report,
* which follows the format expected by the Device Defender service.
* This format is documented here:
* https://docs.aws.amazon.com/iot/latest/developerguide/detect-device-side-metrics.html
*/
if( xStatus == true )
{
LogInfo( ( "Generating device defender report..." ) );
xStatus = prvGenerateDeviceMetricsReport( &( ulReportLength ) );
if( xStatus != true )
{
LogError( ( "Failed to generate device defender report." ) );
}
}
/********************** Publish defender report. **********************/
/* The report is then published to the Device Defender service. This report
* is published to the MQTT topic for publishing JSON reports. As before,
* we use the defender library macros to create the topic string, though
* #Defender_GetTopic could be used if the Thing name is acquired at
* run time */
if( xStatus == true )
{
LogInfo( ( "Publishing device defender report..." ) );
xStatus = prvPublishDeviceMetricsReport( ulReportLength );
if( xStatus != true )
{
LogError( ( "Failed to publish device defender report." ) );
}
}
/* Wait for the response to our report. */
ulNotificationValue = ulTaskNotifyTake( pdFALSE, pdMS_TO_TICKS( defenderexampleMS_TO_WAIT_FOR_NOTIFICATION ) );
if( ulNotificationValue == 0 )
{
LogInfo( ( "Failed to receive defender report receipt notification." ) );
}
else
{
if( xReportStatus == ReportStatusNotReceived )
{
LogError( ( "Failed to receive response from AWS IoT Device Defender Service." ) );
xStatus = false;
}
}
LogDebug( ( "Sleeping until next report." ) );
vTaskDelay( pdMS_TO_TICKS( defenderexampleMS_BETWEEN_REPORTS ) );
}
}
}