Skip to content
This repository has been archived by the owner on Dec 8, 2022. It is now read-only.

Commit

Permalink
sample changes
Browse files Browse the repository at this point in the history
  • Loading branch information
danewalton committed Sep 16, 2021
1 parent cb09733 commit ea766b4
Show file tree
Hide file tree
Showing 8 changed files with 165 additions and 66 deletions.
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
Expand Up @@ -116,3 +116,6 @@
[submodule "libraries/abstractions/ota_pal_psa"]
path = libraries/abstractions/ota_pal_psa
url = https://github.com/Linaro/freertos-ota-pal-psa.git
[submodule "libraries/azure-iot-middleware-freertos"]
path = libraries/azure-iot-middleware-freertos
url = https://github.com/Azure/azure-iot-middleware-freertos.git
146 changes: 87 additions & 59 deletions demos/coreMQTT/mqtt_demo_mutual_auth.c
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
* memory. It uses QoS1 for sending to and receiving messages from the broker.
*
* A mutually authenticated TLS connection is used to connect to the
* MQTT message broker in this example. Define democonfigMQTT_BROKER_ENDPOINT
* MQTT message broker in this example. Define democonfigIOTHUB_HOSTNAME
* and democonfigROOT_CA_PEM, in mqtt_demo_mutual_auth_config.h, and the client
* private key and certificate, in aws_clientcredential_keys.h, to establish a
* mutually authenticated connection.
Expand Down Expand Up @@ -81,8 +81,10 @@
/* Include header for root CA certificates. */
#include "iot_default_root_certificates.h"

/* Include AWS IoT metrics macros header. */
#include "aws_iot_metrics.h"
/* Azure includes */
#include "azure_iot_hub_client.h"
#include "azure_iot_provisioning_client.h"


/*------------- Demo configurations -------------------------*/

Expand All @@ -99,8 +101,8 @@
/**
* @brief The MQTT broker endpoint used for this demo.
*/
#ifndef democonfigMQTT_BROKER_ENDPOINT
#define democonfigMQTT_BROKER_ENDPOINT clientcredentialMQTT_BROKER_ENDPOINT
#ifndef democonfigIOTHUB_HOSTNAME
#define democonfigIOTHUB_HOSTNAME clientcredentialMQTT_BROKER_ENDPOINT
#endif

/**
Expand Down Expand Up @@ -159,6 +161,8 @@
*/
#define mqttexampleCONNACK_RECV_TIMEOUT_MS ( 1000U )

#define democonfigAZURE_IOT_DEVICE_NAME "<insert device ID here>"

/**
* @brief The topic to subscribe and publish to in the example.
*
Expand Down Expand Up @@ -245,6 +249,17 @@ struct NetworkContext

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

/* Azure variables */

#define mqttexampleAZURE_TELEMETRY_MESSAGE "{\"Test Data\":\"Data\"}"

static AzureIoTHubClient_t xIoTHubClient;
static uint8_t ucIoTHubBuffer[5120];
static uint64_t ulGlobalEntryTime = 1639093301;
uint64_t ullGetUnixTime( void );

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

/**
* @brief Calculate and perform an exponential backoff with jitter delay for
* the next retry attempt of a failed network operation with the server.
Expand Down Expand Up @@ -528,9 +543,22 @@ int RunCoreMqttMutualAuthDemo( bool awsIotMqttMode,

/* Sends an MQTT Connect packet over the already established TLS connection,
* and waits for connection acknowledgment (CONNACK) packet. */
LogInfo( ( "Creating an MQTT connection to %s.", democonfigMQTT_BROKER_ENDPOINT ) );
LogInfo( ( "Creating an MQTT connection to %s.", democonfigIOTHUB_HOSTNAME ) );
xDemoStatus = prvCreateMQTTConnectionWithBroker( &xMQTTContext, &xNetworkContext );
}
vTaskDelay( mqttexampleDELAY_BETWEEN_PUBLISHES_TICKS );

for( ; ; )
{
AzureIoTHubClient_SendTelemetry(&xIoTHubClient,
mqttexampleAZURE_TELEMETRY_MESSAGE,
strlen(mqttexampleAZURE_TELEMETRY_MESSAGE),
NULL,
eAzureIoTHubMessageQoS1,
NULL);
AzureIoTHubClient_ProcessLoop(&xIoTHubClient, 50);
vTaskDelay( mqttexampleDELAY_BETWEEN_PUBLISHES_TICKS );
}

/**************************** Subscribe. ******************************/

Expand Down Expand Up @@ -599,7 +627,7 @@ int RunCoreMqttMutualAuthDemo( bool awsIotMqttMode,
/* Send an MQTT Disconnect packet over the already connected TLS over TCP connection.
* There is no corresponding response for the disconnect packet. After sending
* disconnect, client must close the network connection. */
LogInfo( ( "Disconnecting the MQTT connection with %s.", democonfigMQTT_BROKER_ENDPOINT ) );
LogInfo( ( "Disconnecting the MQTT connection with %s.", democonfigIOTHUB_HOSTNAME ) );
xMQTTStatus = MQTT_Disconnect( &xMQTTContext );
}

Expand Down Expand Up @@ -729,8 +757,8 @@ static BaseType_t prvConnectToServerWithBackoffRetries( NetworkContext_t * pxNet

/* Set the credentials for establishing a TLS connection. */
/* Initializer server information. */
xServerInfo.pHostName = democonfigMQTT_BROKER_ENDPOINT;
xServerInfo.hostNameLength = strlen( democonfigMQTT_BROKER_ENDPOINT );
xServerInfo.pHostName = democonfigIOTHUB_HOSTNAME;
xServerInfo.hostNameLength = strlen( democonfigIOTHUB_HOSTNAME );
xServerInfo.port = democonfigMQTT_BROKER_PORT;

/* Configure credentials for TLS mutual authenticated session. */
Expand All @@ -756,10 +784,10 @@ static BaseType_t prvConnectToServerWithBackoffRetries( NetworkContext_t * pxNet
do
{
/* Establish a TLS session with the MQTT broker. This example connects to
* the MQTT broker as specified in democonfigMQTT_BROKER_ENDPOINT and
* the MQTT broker as specified in democonfigIOTHUB_HOSTNAME and
* democonfigMQTT_BROKER_PORT at the top of this file. */
LogInfo( ( "Creating a TLS connection to %s:%u.",
democonfigMQTT_BROKER_ENDPOINT,
democonfigIOTHUB_HOSTNAME,
democonfigMQTT_BROKER_PORT ) );
/* Attempt to create a mutually authenticated TLS connection. */
xNetworkStatus = SecureSocketsTransport_Connect( pxNetworkContext,
Expand All @@ -785,63 +813,43 @@ static BaseType_t prvConnectToServerWithBackoffRetries( NetworkContext_t * pxNet
static BaseType_t prvCreateMQTTConnectionWithBroker( MQTTContext_t * pxMQTTContext,
NetworkContext_t * pxNetworkContext )
{
MQTTStatus_t xResult;
MQTTConnectInfo_t xConnectInfo;
bool xSessionPresent;
TransportInterface_t xTransport;
AzureIoTTransportInterface_t xTransport;
BaseType_t xStatus = pdFAIL;

/* Fill in Transport Interface send and receive function pointers. */
xTransport.pNetworkContext = pxNetworkContext;
xTransport.send = SecureSocketsTransport_Send;
xTransport.recv = SecureSocketsTransport_Recv;

/* Initialize MQTT library. */
xResult = MQTT_Init( pxMQTTContext, &xTransport, prvGetTimeMs, prvEventCallback, &xBuffer );
configASSERT( xResult == MQTTSuccess );
xTransport.pxNetworkContext = pxNetworkContext;
xTransport.xSend = SecureSocketsTransport_Send;
xTransport.xRecv = SecureSocketsTransport_Recv;

AzureIoTResult_t xIoTResult = AzureIoTHubClient_Init(&xIoTHubClient, (const uint8_t*)democonfigIOTHUB_HOSTNAME,
strlen(democonfigIOTHUB_HOSTNAME),
(const uint8_t*)democonfigAZURE_IOT_DEVICE_NAME,
strlen(democonfigAZURE_IOT_DEVICE_NAME),
NULL,
ucIoTHubBuffer,
sizeof(ucIoTHubBuffer),
ullGetUnixTime,
&xTransport);
if (xIoTResult == eAzureIoTSuccess)
{
LogInfo(("Initialized"));
}
else
{
LogError(("Error during initialization: %d", xIoTResult));
}

/* Some fields are not used in this demo so start with everything at 0. */
( void ) memset( ( void * ) &xConnectInfo, 0x00, sizeof( xConnectInfo ) );

/* Start with a clean session i.e. direct the MQTT broker to discard any
* previous session data. Also, establishing a connection with clean session
* will ensure that the broker does not store any data when this client
* gets disconnected. */
xConnectInfo.cleanSession = true;

/* The client identifier is used to uniquely identify this MQTT client to
* the MQTT broker. In a production device the identifier can be something
* unique, such as a device serial number. */
xConnectInfo.pClientIdentifier = democonfigCLIENT_IDENTIFIER;
xConnectInfo.clientIdentifierLength = ( uint16_t ) strlen( democonfigCLIENT_IDENTIFIER );

/* Use the metrics string as username to report the OS and MQTT client version
* metrics to AWS IoT. */
xConnectInfo.pUserName = AWS_IOT_METRICS_STRING;
xConnectInfo.userNameLength = AWS_IOT_METRICS_STRING_LENGTH;

/* Set MQTT keep-alive period. If the application does not send packets at an interval less than
* the keep-alive period, the MQTT library will send PINGREQ packets. */
xConnectInfo.keepAliveSeconds = mqttexampleKEEP_ALIVE_TIMEOUT_SECONDS;

/* Send MQTT CONNECT packet to broker. LWT is not used in this demo, so it
* is passed as NULL. */
xResult = MQTT_Connect( pxMQTTContext,
&xConnectInfo,
NULL,
mqttexampleCONNACK_RECV_TIMEOUT_MS,
&xSessionPresent );
bool xWasPresent;
xIoTResult = AzureIoTHubClient_Connect(&xIoTHubClient, true, &xWasPresent, 500);

if( xResult != MQTTSuccess )
if(xIoTResult == eAzureIoTSuccess)
{
LogError( ( "Failed to establish MQTT connection: Server=%s, MQTTStatus=%s",
democonfigMQTT_BROKER_ENDPOINT, MQTT_Status_strerror( xResult ) ) );
LogInfo(("Successfully connected!"));
xStatus = pdPASS;
}
else
{
/* Successfully established and MQTT connection with the broker. */
LogInfo( ( "An MQTT connection is established with %s.", democonfigMQTT_BROKER_ENDPOINT ) );
xStatus = pdPASS;
LogError(("Failed to connect"));
}

return xStatus;
Expand Down Expand Up @@ -1155,6 +1163,26 @@ static void prvEventCallback( MQTTContext_t * pxMQTTContext,

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

uint64_t ullGetUnixTime( void )
{
TickType_t xTickCount = 0;
uint64_t ulTime = 0UL;

/* Get the current tick count. */
xTickCount = xTaskGetTickCount();

/* Convert the ticks to milliseconds. */
ulTime = ( uint64_t ) xTickCount / configTICK_RATE_HZ;

/* Reduce ulGlobalEntryTimeMs from obtained time so as to always return the
* elapsed time in the application. */
ulTime = ( uint64_t ) ( ulTime + ulGlobalEntryTime );

return ulTime;
}

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

static uint32_t prvGetTimeMs( void )
{
TickType_t xTickCount = 0;
Expand Down
6 changes: 3 additions & 3 deletions demos/include/aws_clientcredential.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
*
* @todo Set this to the fully-qualified DNS name of your MQTT broker.
*/
#define clientcredentialMQTT_BROKER_ENDPOINT ""
#define clientcredentialMQTT_BROKER_ENDPOINT "<place your iothub here>"

/*
* @brief Host name.
Expand Down Expand Up @@ -60,13 +60,13 @@
*
* @todo If you are using Wi-Fi, set this to your network name.
*/
#define clientcredentialWIFI_SSID ""
#define clientcredentialWIFI_SSID "<place your wifi ssid here>"

/*
* @brief Password needed to join Wi-Fi network.
* @todo If you are using WPA, set this to your network password.
*/
#define clientcredentialWIFI_PASSWORD ""
#define clientcredentialWIFI_PASSWORD "<place your wifi password here>"

/*
* @brief Wi-Fi network security type.
Expand Down
4 changes: 2 additions & 2 deletions demos/include/aws_clientcredential_keys.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@
* "...base64 data...\n"\
* "-----END CERTIFICATE-----\n"
*/
#define keyCLIENT_CERTIFICATE_PEM NULL
#define keyCLIENT_CERTIFICATE_PEM "<place your device cert here>"

/*
* @brief PEM-encoded issuer certificate for AWS IoT Just In Time Registration (JITR).
Expand Down Expand Up @@ -88,6 +88,6 @@
* "...base64 data...\n"\
* "-----END RSA PRIVATE KEY-----\n"
*/
#define keyCLIENT_PRIVATE_KEY_PEM NULL
#define keyCLIENT_PRIVATE_KEY_PEM "<place your private key here>"

#endif /* AWS_CLIENT_CREDENTIAL_KEYS_H */
1 change: 1 addition & 0 deletions libraries/azure-iot-middleware-freertos
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@
#define CONFIG_CORE_MQTT_MUTUAL_AUTH_DEMO_ENABLED

/* Default configuration for all demos. Individual demos can override these below */
#define democonfigDEMO_STACKSIZE ( configMINIMAL_STACK_SIZE * 8 )
#define democonfigDEMO_STACKSIZE ( configMINIMAL_STACK_SIZE * 12 )
#define democonfigDEMO_PRIORITY ( tskIDLE_PRIORITY + 5 )
#define democonfigNETWORK_TYPES ( AWSIOT_NETWORK_TYPE_WIFI )

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/* Copyright (c) Microsoft Corporation.
Licensed under the MIT License. */

#ifndef AZURE_IOT_CONFIG_H
#define AZURE_IOT_CONFIG_H


/**************************************************/
/******* DO NOT CHANGE the following order ********/
/**************************************************/

/* Include logging header files and define logging macros in the following order:
* 1. Include the header file "logging_levels.h".
* 2. Define the LIBRARY_LOG_NAME and LIBRARY_LOG_LEVEL macros depending on
* the logging configuration for AzureIoT middleware.
* 3. Include the header file "logging_stack.h", if logging is enabled for AzureIoT middleware.
*/

#include "logging_levels.h"

/* Logging configuration for the AzureIoT middleware library. */
#ifndef LIBRARY_LOG_NAME
#define LIBRARY_LOG_NAME "AZ IOT"
#endif

#ifndef LIBRARY_LOG_LEVEL
#define LIBRARY_LOG_LEVEL LOG_INFO
#endif

/* Prototype for the function used to print to console on Windows simulator
* of FreeRTOS.
* The function prints to the console before the network is connected;
* then a UDP port after the network has connected. */
extern void vLoggingPrintf( const char * pcFormatString,
... );

/* Map the SdkLog macro to the logging function to enable logging
* on Windows simulator. */
#ifndef SdkLog
#define SdkLog( message ) vLoggingPrintf message
#endif

#include "logging_stack.h"
/************ End of logging configuration ****************/

#endif /* AZURE_IOT_CONFIG_H */
Original file line number Diff line number Diff line change
Expand Up @@ -112,11 +112,32 @@
*
* #define democonfigROOT_CA_PEM "...insert here..."
*/
#define democonfigROOT_CA_PEM "-----BEGIN CERTIFICATE-----\n" \
"MIIDdzCCAl+gAwIBAgIEAgAAuTANBgkqhkiG9w0BAQUFADBaMQswCQYDVQQGEwJJ\n" \
"RTESMBAGA1UEChMJQmFsdGltb3JlMRMwEQYDVQQLEwpDeWJlclRydXN0MSIwIAYD\n" \
"VQQDExlCYWx0aW1vcmUgQ3liZXJUcnVzdCBSb290MB4XDTAwMDUxMjE4NDYwMFoX\n" \
"DTI1MDUxMjIzNTkwMFowWjELMAkGA1UEBhMCSUUxEjAQBgNVBAoTCUJhbHRpbW9y\n" \
"ZTETMBEGA1UECxMKQ3liZXJUcnVzdDEiMCAGA1UEAxMZQmFsdGltb3JlIEN5YmVy\n" \
"VHJ1c3QgUm9vdDCCASIwDQYJKoZIhvcNAQEBBQADggEPADCCAQoCggEBAKMEuyKr\n" \
"mD1X6CZymrV51Cni4eiVgLGw41uOKymaZN+hXe2wCQVt2yguzmKiYv60iNoS6zjr\n" \
"IZ3AQSsBUnuId9Mcj8e6uYi1agnnc+gRQKfRzMpijS3ljwumUNKoUMMo6vWrJYeK\n" \
"mpYcqWe4PwzV9/lSEy/CG9VwcPCPwBLKBsua4dnKM3p31vjsufFoREJIE9LAwqSu\n" \
"XmD+tqYF/LTdB1kC1FkYmGP1pWPgkAx9XbIGevOF6uvUA65ehD5f/xXtabz5OTZy\n" \
"dc93Uk3zyZAsuT3lySNTPx8kmCFcB5kpvcY67Oduhjprl3RjM71oGDHweI12v/ye\n" \
"jl0qhqdNkNwnGjkCAwEAAaNFMEMwHQYDVR0OBBYEFOWdWTCCR1jMrPoIVDaGezq1\n" \
"BE3wMBIGA1UdEwEB/wQIMAYBAf8CAQMwDgYDVR0PAQH/BAQDAgEGMA0GCSqGSIb3\n" \
"DQEBBQUAA4IBAQCFDF2O5G9RaEIFoN27TyclhAO992T9Ldcw46QQF+vaKSm2eT92\n" \
"9hkTI7gQCvlYpNRhcL0EYWoSihfVCr3FvDB81ukMJY2GQE/szKN+OMY3EU/t3Wgx\n" \
"jkzSswF07r51XgdIGn9w/xZchMB5hbgF/X++ZRGjD8ACtPhSNzkE1akxehi/oCr0\n" \
"Epn3o0WC4zxe9Z2etciefC7IpJ5OCBRLbf1wbWsaY71k5h+3zvDyny67G7fyUIhz\n" \
"ksLi4xaNmjICq44Y3ekQEe5+NauQrz4wlHrQMz2nZQ/1/I6eYs9HRCwBXbsdtTLS\n" \
"R9I4LtD+gdwyah617jzV/OeBHRnDJELqYzmp\n" \
"-----END CERTIFICATE-----\n"

/**
* @brief Size of the network buffer for MQTT packets.
*/
#define democonfigNETWORK_BUFFER_SIZE ( 1024U )
#define democonfigNETWORK_BUFFER_SIZE ( 5 * 1024U )

/**
* @brief The maximum number of times to run the subscribe publish loop in the
Expand Down

0 comments on commit ea766b4

Please sign in to comment.