diff --git a/.gitmodules b/.gitmodules index 8b4c2d07450..db22a1ca84e 100644 --- a/.gitmodules +++ b/.gitmodules @@ -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 diff --git a/demos/coreMQTT/mqtt_demo_mutual_auth.c b/demos/coreMQTT/mqtt_demo_mutual_auth.c index a665f826e47..2bbc83f23b6 100644 --- a/demos/coreMQTT/mqtt_demo_mutual_auth.c +++ b/demos/coreMQTT/mqtt_demo_mutual_auth.c @@ -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. @@ -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 -------------------------*/ @@ -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 /** @@ -159,6 +161,8 @@ */ #define mqttexampleCONNACK_RECV_TIMEOUT_MS ( 1000U ) +#define democonfigAZURE_IOT_DEVICE_NAME "" + /** * @brief The topic to subscribe and publish to in the example. * @@ -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. @@ -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. ******************************/ @@ -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 ); } @@ -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. */ @@ -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, @@ -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; @@ -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; diff --git a/demos/include/aws_clientcredential.h b/demos/include/aws_clientcredential.h index c3bda361fda..93c21deee1c 100644 --- a/demos/include/aws_clientcredential.h +++ b/demos/include/aws_clientcredential.h @@ -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 "" /* * @brief Host name. @@ -60,13 +60,13 @@ * * @todo If you are using Wi-Fi, set this to your network name. */ -#define clientcredentialWIFI_SSID "" +#define clientcredentialWIFI_SSID "" /* * @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 "" /* * @brief Wi-Fi network security type. diff --git a/demos/include/aws_clientcredential_keys.h b/demos/include/aws_clientcredential_keys.h index ac861f23e68..b3c6fcf3ab8 100644 --- a/demos/include/aws_clientcredential_keys.h +++ b/demos/include/aws_clientcredential_keys.h @@ -48,7 +48,7 @@ * "...base64 data...\n"\ * "-----END CERTIFICATE-----\n" */ -#define keyCLIENT_CERTIFICATE_PEM NULL +#define keyCLIENT_CERTIFICATE_PEM "" /* * @brief PEM-encoded issuer certificate for AWS IoT Just In Time Registration (JITR). @@ -88,6 +88,6 @@ * "...base64 data...\n"\ * "-----END RSA PRIVATE KEY-----\n" */ -#define keyCLIENT_PRIVATE_KEY_PEM NULL +#define keyCLIENT_PRIVATE_KEY_PEM "" #endif /* AWS_CLIENT_CREDENTIAL_KEYS_H */ diff --git a/libraries/azure-iot-middleware-freertos b/libraries/azure-iot-middleware-freertos new file mode 160000 index 00000000000..d99a55d3499 --- /dev/null +++ b/libraries/azure-iot-middleware-freertos @@ -0,0 +1 @@ +Subproject commit d99a55d3499486b04fba048c9c186032849157fd diff --git a/vendors/st/boards/stm32l475_discovery/aws_demos/config_files/aws_demo_config.h b/vendors/st/boards/stm32l475_discovery/aws_demos/config_files/aws_demo_config.h index b5fda9921aa..185ab2d78cf 100644 --- a/vendors/st/boards/stm32l475_discovery/aws_demos/config_files/aws_demo_config.h +++ b/vendors/st/boards/stm32l475_discovery/aws_demos/config_files/aws_demo_config.h @@ -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 ) diff --git a/vendors/st/boards/stm32l475_discovery/aws_demos/config_files/azure_iot_config.h b/vendors/st/boards/stm32l475_discovery/aws_demos/config_files/azure_iot_config.h new file mode 100644 index 00000000000..6672f39b750 --- /dev/null +++ b/vendors/st/boards/stm32l475_discovery/aws_demos/config_files/azure_iot_config.h @@ -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 */ diff --git a/vendors/st/boards/stm32l475_discovery/aws_demos/config_files/mqtt_demo_mutual_auth_config.h b/vendors/st/boards/stm32l475_discovery/aws_demos/config_files/mqtt_demo_mutual_auth_config.h index ac78d0b5bb0..8d5b1e3c7b9 100644 --- a/vendors/st/boards/stm32l475_discovery/aws_demos/config_files/mqtt_demo_mutual_auth_config.h +++ b/vendors/st/boards/stm32l475_discovery/aws_demos/config_files/mqtt_demo_mutual_auth_config.h @@ -112,11 +112,32 @@ * * #define democonfigROOT_CA_PEM "...insert here..." */ +#define democonfigROOT_CA_PEM "-----BEGIN CERTIFICATE-----\n" \ +"M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r51XgdIGn9w/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