Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .github/scripts/check-header.py
Original file line number Diff line number Diff line change
Expand Up @@ -369,7 +369,7 @@ def configArgParser():

FREERTOS_HEADER = [
'/*\n',
' * FreeRTOS V202011.00\n',
' * FreeRTOS V202012.00\n',
' * Copyright (C) 2020 Amazon.com, Inc. or its affiliates. All Rights Reserved.\n',
' *\n',
' * Permission is hereby granted, free of charge, to any person obtaining a copy of\n',
Expand Down Expand Up @@ -422,7 +422,7 @@ def configArgParser():

KERNEL_HEADER = [
'/*\n',
' * FreeRTOS Kernel V10.4.2\n',
' * FreeRTOS Kernel V10.4.3\n',
' * Copyright (C) 2020 Amazon.com, Inc. or its affiliates. All Rights Reserved.\n',
' *\n',
' * Permission is hereby granted, free of charge, to any person obtaining a copy of\n',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,25 +28,27 @@
* Demo for showing how to use the Device Defender library's APIs. The Device
* Defender library provides macros and helper functions for assembling MQTT
* topics strings, and for determining whether an incoming MQTT message is
* related to device defender. The Device Defender library does not depend on
* related to Device Defender. The Device Defender library does not depend on
* any particular MQTT library, therefore the code for MQTT operations is
* placed in another file (mqtt_demo_helpers.c). This demo uses the coreMQTT
* library. If needed, mqtt_demo_helpers.c can be modified to replace coreMQTT
* with another MQTT library. This demo requires using the AWS IoT broker as
* Device Defender is an AWS service.
*
* This demo connects to the AWS IoT broker and subscribes to the device
* defender topics. It then collects metrics for the open ports and sockets on
* the device using FreeRTOS+TCP, and generates a device defender report. The
* This demo subscribes to the Device Defender topics. It then collects metrics
* for the open ports and sockets on the device using FreeRTOS+TCP. Additonally
* the stack high water mark and task IDs are collected for custom metrics.
* These metrics are used to generate a Device Defender report. The
* report is then published, and the demo waits for a response from the device
* defender service. Upon receiving the response or timing out, the demo
* finishes.
* defender service. Upon receiving an accepted response, the demo finishes.
* If the demo receives a rejected response or times out, the demo repeats up to
* a maximum of DEFENDER_MAX_DEMO_LOOP_COUNT times.
*
* 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
* Thing. We recommend using an increasing unique ID such as the current
* timestamp.
*/

Expand Down Expand Up @@ -97,7 +99,7 @@
#define DEFENDER_RESPONSE_WAIT_SECONDS ( 2 )

/**
* @brief Name of the report id field in the response from the AWS IoT Device
* @brief Name of the report ID field in the response from the AWS IoT Device
* Defender service.
*/
#define DEFENDER_RESPONSE_REPORT_ID_FIELD "reportId"
Expand All @@ -124,7 +126,7 @@
#define DELAY_BETWEEN_DEMO_RETRY_ITERATIONS_TICKS ( pdMS_TO_TICKS( 5000U ) )

/**
* @brief Status values of the device defender report.
* @brief Status values of the Device Defender report.
*/
typedef enum
{
Expand All @@ -133,8 +135,8 @@ typedef enum
ReportStatusRejected
} ReportStatus_t;

/**
* @brief Each compilation unit that consumes the NetworkContext must define it.
/**
* @brief Each compilation unit that consumes the NetworkContext must define it.
* It should contain a single pointer to the type of your desired transport.
* When using multiple transports in the same compilation unit, define this pointer as void *.
*
Expand Down Expand Up @@ -196,7 +198,17 @@ static uint16_t pusOpenUdpPorts[ democonfigOPEN_UDP_PORTS_ARRAY_SIZE ];
static Connection_t pxEstablishedConnections[ democonfigESTABLISHED_CONNECTIONS_ARRAY_SIZE ];

/**
* @brief All the metrics sent in the device defender report.
* @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.
*/
static ReportMetrics_t xDeviceMetrics;

Expand All @@ -206,14 +218,15 @@ static ReportMetrics_t xDeviceMetrics;
static ReportStatus_t xReportStatus;

/**
* @brief Buffer for generating the device defender report.
* @brief Buffer for generating the Device Defender report.
*/
static char pcDeviceMetricsJsonReport[ democonfigDEVICE_METRICS_REPORT_BUFFER_SIZE ];

/**
* @brief Report Id sent in the defender report.
* @brief Report ID sent in the defender report.
*/
static uint32_t ulReportId = 0UL;

/*-----------------------------------------------------------*/

/**
Expand All @@ -228,43 +241,43 @@ static void prvPublishCallback( MQTTContext_t * pxMqttContext,
MQTTDeserializedInfo_t * pxDeserializedInfo );

/**
* @brief Collect all the metrics to be sent in the device defender report.
* @brief Collect all the metrics to be sent in the Device Defender report.
*
* @return true if all the metrics are successfully collected;
* false otherwise.
*/
static bool prvCollectDeviceMetrics( void );

/**
* @brief Generate the device defender report.
* @brief Generate the Device Defender report.
*
* @param[out] pulOutReportLength Length of the device defender report.
* @param[out] pulOutReportLength Length of the Device Defender report.
*
* @return true if the report is generated successfully;
* false otherwise.
*/
static bool prvGenerateDeviceMetricsReport( uint32_t * pulOutReportLength );

/**
* @brief Subscribe to the device defender topics.
* @brief Subscribe to the Device Defender topics.
*
* @return true if the subscribe is successful;
* false otherwise.
*/
static bool prvSubscribeToDefenderTopics( void );

/**
* @brief Unsubscribe from the device defender topics.
* @brief Unsubscribe from the Device Defender topics.
*
* @return true if the unsubscribe is successful;
* false otherwise.
*/
static bool prvUnsubscribeFromDefenderTopics( void );

/**
* @brief Publish the generated device defender report.
* @brief Publish the generated Device Defender report.
*
* @param[in] ulReportLength Length of the device defender report.
* @param[in] ulReportLength Length of the Device Defender report.
*
* @return true if the report is published successfully;
* false otherwise.
Expand Down Expand Up @@ -398,7 +411,7 @@ static void prvPublishCallback( MQTTContext_t * pxMqttContext,

pxPublishInfo = pxDeserializedInfo->pPublishInfo;

/* Verify that the publish is for device defender, and if so get which
/* Verify that the publish is for Device Defender, and if so get which
* defender API it is for */
xStatus = Defender_MatchTopic( pxPublishInfo->pTopicName,
pxPublishInfo->topicNameLength,
Expand Down Expand Up @@ -462,62 +475,94 @@ static void prvPublishCallback( MQTTContext_t * pxMqttContext,
static bool prvCollectDeviceMetrics( void )
{
bool xStatus = false;
eMetricsCollectorStatus eMetricsCollectorStatus;
uint32_t ulNumOpenTcpPorts = 0UL, ulNumOpenUdpPorts = 0UL, ulNumEstablishedConnections = 0UL;
eMetricsCollectorStatus eStatus;
uint32_t ulNumOpenTcpPorts = 0UL, ulNumOpenUdpPorts = 0UL, ulNumEstablishedConnections = 0UL, i;
UBaseType_t uxTasksWritten = { 0 };
TaskStatus_t pxTaskStatus = { 0 };

/* Collect bytes and packets sent and received. */
eMetricsCollectorStatus = eGetNetworkStats( &( xNetworkStats ) );
eStatus = eGetNetworkStats( &( xNetworkStats ) );

if( eMetricsCollectorStatus != eMetricsCollectorSuccess )
if( eStatus != eMetricsCollectorSuccess )
{
LogError( ( "xGetNetworkStats failed. Status: %d.",
eMetricsCollectorStatus ) );
eStatus ) );
}

/* Collect a list of open TCP ports. */
if( eMetricsCollectorStatus == eMetricsCollectorSuccess )
if( eStatus == eMetricsCollectorSuccess )
{
eMetricsCollectorStatus = eGetOpenTcpPorts( &( pusOpenTcpPorts[ 0 ] ),
eStatus = eGetOpenTcpPorts( &( pusOpenTcpPorts[ 0 ] ),
democonfigOPEN_TCP_PORTS_ARRAY_SIZE,
&( ulNumOpenTcpPorts ) );

if( eMetricsCollectorStatus != eMetricsCollectorSuccess )
if( eStatus != eMetricsCollectorSuccess )
{
LogError( ( "xGetOpenTcpPorts failed. Status: %d.",
eMetricsCollectorStatus ) );
eStatus ) );
}
}

/* Collect a list of open UDP ports. */
if( eMetricsCollectorStatus == eMetricsCollectorSuccess )
if( eStatus == eMetricsCollectorSuccess )
{
eMetricsCollectorStatus = eGetOpenUdpPorts( &( pusOpenUdpPorts[ 0 ] ),
eStatus = eGetOpenUdpPorts( &( pusOpenUdpPorts[ 0 ] ),
democonfigOPEN_UDP_PORTS_ARRAY_SIZE,
&( ulNumOpenUdpPorts ) );

if( eMetricsCollectorStatus != eMetricsCollectorSuccess )
if( eStatus != eMetricsCollectorSuccess )
{
LogError( ( "xGetOpenUdpPorts failed. Status: %d.",
eMetricsCollectorStatus ) );
eStatus ) );
}
}

/* Collect a list of established connections. */
if( eMetricsCollectorStatus == eMetricsCollectorSuccess )
if( eStatus == eMetricsCollectorSuccess )
{
eMetricsCollectorStatus = eGetEstablishedConnections( &( pxEstablishedConnections[ 0 ] ),
eStatus = eGetEstablishedConnections( &( pxEstablishedConnections[ 0 ] ),
democonfigESTABLISHED_CONNECTIONS_ARRAY_SIZE,
&( ulNumEstablishedConnections ) );

if( eMetricsCollectorStatus != eMetricsCollectorSuccess )
if( eStatus != eMetricsCollectorSuccess )
{
LogError( ( "GetEstablishedConnections failed. Status: %d.",
eMetricsCollectorStatus ) );
eStatus ) );
}
}

/* 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 );

if( uxTasksWritten == 0 )
{
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. */
if( eMetricsCollectorStatus == eMetricsCollectorSuccess )
if( eStatus == eMetricsCollectorSuccess )
{
xStatus = true;
xDeviceMetrics.pxNetworkStats = &( xNetworkStats );
Expand All @@ -527,6 +572,9 @@ static bool prvCollectDeviceMetrics( void )
xDeviceMetrics.ulOpenUdpPortsArrayLength = ulNumOpenUdpPorts;
xDeviceMetrics.pxEstablishedConnectionsArray = &( pxEstablishedConnections[ 0 ] );
xDeviceMetrics.ulEstablishedConnectionsArrayLength = ulNumEstablishedConnections;
xDeviceMetrics.ulStackHighWaterMark = pxTaskStatus.usStackHighWaterMark;
xDeviceMetrics.pulTaskIdArray = pulCustomMetricsTaskNumbers;
xDeviceMetrics.ulTaskIdArrayLength = uxTasksWritten;
}

return xStatus;
Expand Down Expand Up @@ -671,14 +719,14 @@ void prvDefenderDemoTask( void * pvParameters )
* DEFENDER_MAX_DEMO_LOOP_COUNT times. */
do
{
/* Set a report Id to be used.
/* 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
* a given Thing. We recommend using an increasing unique ID such as the
* current timestamp. */
ulReportId = ( uint32_t ) xTaskGetTickCount();

Expand Down Expand Up @@ -772,12 +820,12 @@ void prvDefenderDemoTask( void * pvParameters )
*/
if( xStatus == true )
{
LogInfo( ( "Generating device defender report..." ) );
LogInfo( ( "Generating Device Defender report..." ) );
xStatus = prvGenerateDeviceMetricsReport( &( ulReportLength ) );

if( xStatus != true )
{
LogError( ( "Failed to generate device defender report." ) );
LogError( ( "Failed to generate Device Defender report." ) );
}
}

Expand All @@ -790,12 +838,12 @@ void prvDefenderDemoTask( void * pvParameters )
* run time */
if( xStatus == true )
{
LogInfo( ( "Publishing device defender report..." ) );
LogInfo( ( "Publishing Device Defender report..." ) );
xStatus = prvPublishDeviceMetricsReport( ulReportLength );

if( xStatus != true )
{
LogError( ( "Failed to publish device defender report." ) );
LogError( ( "Failed to publish Device Defender report." ) );
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,8 @@
#define configMINIMAL_STACK_SIZE ( ( unsigned short ) 60 ) /* In this simulated case, the stack only has to hold one small structure as the real stack is part of the Win32 thread. */
#define configTOTAL_HEAP_SIZE ( ( size_t ) ( 2048U * 1024U ) )
#define configMAX_TASK_NAME_LEN ( 15 )
#define configUSE_TRACE_FACILITY 0
#define configUSE_TRACE_FACILITY 1
#define configUSE_STATS_FORMATTING_FUNCTIONS 0
#define configUSE_16_BIT_TICKS 0
#define configIDLE_SHOULD_YIELD 1
#define configUSE_CO_ROUTINES 0
Expand Down Expand Up @@ -103,15 +104,6 @@
#define INCLUDE_xTimerPendFunctionCall 1
#define INCLUDE_pcTaskGetTaskName 1

/* This demo makes use of one or more example stats formatting functions. These
* format the raw data provided by the uxTaskGetSystemState() function in to human
* readable ASCII form. See the notes in the implementation of vTaskList() within
* FreeRTOS/Source/tasks.c for limitations. configUSE_STATS_FORMATTING_FUNCTIONS
* is set to 2 so the formatting functions are included without the stdio.h being
* included in tasks.c. That is because this project defines its own sprintf()
* functions. */
#define configUSE_STATS_FORMATTING_FUNCTIONS 1

/* Assert call defined for debug builds. */
#ifdef _DEBUG
extern void vAssertCalled( const char * pcFile,
Expand Down
Loading