Verified Commit a9809f4e authored by Vincent Coubard's avatar Vincent Coubard
Browse files

Keyword: Add Azure support



The user can select the cloud connection to connect the application to.
Either AWS IoT or Azure IoT Hub.

To add Azure support, an Azure device must be created following.
Then the device connection string must be added into credentials.
Finaly, to build with Azure support compile with the flag -e AZURE.

On the technical side, the Azure code and AWS code have been split
with their associated app code into two different files:
- aws_demo.c
- azure_demo.c

The AWS code remains as it is before this addition while the azure code
follows closely the example exposed by the Open IoT SDK.

The CI pipeline has been modified to support and test Azure. While
compiling more examples, it reuse build trees and is therefore faster
than the previous pipeline.

Signed-off-by: Vincent Coubard's avatarVincent Coubard <vincent.coubard@arm.com>
parent 7270975e
......@@ -30,26 +30,11 @@ if(NOT CMAKE_BUILD_TYPE)
set(CMAKE_BUILD_TYPE "Debug" CACHE STRING "The build type" FORCE)
endif()
FetchContent_Declare(iotsdk-toolchains
GIT_REPOSITORY https://git.gitlab.arm.com/iot/open-iot-sdk/toolchain.git
GIT_TAG 126d2092a2325c77fdf36043e931293c62e2ad29
SOURCE_DIR ${CMAKE_BINARY_DIR}/toolchains
)
# Override MCU driver HAL
FetchContent_Declare(
mcu-driver-reference-platforms-for-arm
GIT_REPOSITORY https://git.gitlab.arm.com/iot/open-iot-sdk/mcu-driver-hal/mcu-driver-reference-platforms-for-arm.git
GIT_TAG 9b7696596ca1c0f380e77f3d7c5de150635e340e
GIT_PROGRESS ON
)
# Declare the Open IoT SDK
FetchContent_Declare(
open_iot_sdk
GIT_REPOSITORY https://git.gitlab.arm.com/iot/open-iot-sdk/sdk.git
GIT_TAG 96becfbf25c2fd24f4b18c9e7a9c817b780392c4
GIT_TAG 76aa21b371135ed66e1680717974b43098193348
)
# Declare the Open IoT SDK configuration flags
......@@ -70,7 +55,11 @@ set(TFM_PARTITION_FIRMWARE_UPDATE ON)
set(TFM_LIB_MODEL ON)
set(CONFIG_TFM_ENABLE_FPU ON)
set(IOTSDK_MBEDTLS ON)
set(IOTSDK_AWS_CLIENT ON)
if(${CLOUD_CLIENT} STREQUAL "AWS")
set(IOTSDK_AWS_CLIENT ON)
elseif(${CLOUD_CLIENT} STREQUAL "AZURE")
set(IOTSDK_AZURE_SDK ON)
endif()
set(IOTSDK_LWIP ON)
set(IOTSDK_LWIP_SOCKETS ON)
set(IOTSDK_IP_NETWORK ON)
......@@ -108,8 +97,8 @@ set(ml-embedded-evaluation-kit_LIB_DIR ${ml-embedded-evaluation-kit_BINARY_DIR}/
# Start of the project
project(tfm_ns LANGUAGES C CXX)
# Path to AWS credentials
set(AWS_CONFIG_CREDENTIALS_PATH "${PRJ_DIR}/bsp/default_credentials" CACHE STRING "Path to AWS credentials")
# Path to credentials
set(APP_CONFIG_CREDENTIALS_PATH "${PRJ_DIR}/bsp/default_credentials" CACHE STRING "Path to credentials")
# Include module path from Open IoT SDK
list(APPEND CMAKE_MODULE_PATH ${open_iot_sdk_SOURCE_DIR}/components/TF-M)
......@@ -194,7 +183,9 @@ set(HTTP_CONFIG_PATH ${PRJ_DIR}/bsp/aws_configs)
set(PKCS11_CONFIG_PATH ${PRJ_DIR}/bsp/aws_configs)
set(MBEDTLS_CONFIG_PATH ${PRJ_DIR}/bsp/aws_libraries/3rdparty/mbedtls_config)
add_subdirectory(lib/AWS)
if(${CLOUD_CLIENT} STREQUAL "AWS")
add_subdirectory(lib/AWS)
endif()
add_subdirectory(bsp)
# Setup Target
......
......@@ -26,8 +26,9 @@ Follow these simple steps to build and execute the code example's application wi
* [Launch Arm Virtual Hardware system](#launch-arm-virtual-hardware-instance)
* [Build and execute](#build-and-execute-the-application)
* [Setting up Cloud connectivity](#setting-up-aws-connectivity)
* [Enabling OTA firmware update from the Cloud](#ota-firmware-update)
* [Setting up AWS Cloud connectivity](#setting-up-aws-connectivity)
* [Enabling OTA firmware update from the AWS Cloud](#ota-firmware-update)
* [Setting up Azure Cloud connectivity](#setting-up-azure-connectivity)
* [Terminating Arm Virtual Hardware](#terminate-arm-virtual-hardware-instance)
# Launch Arm Virtual Hardware Instance
......@@ -440,6 +441,42 @@ The instructions below use the keyword spotting name, kws, as an example. Repla
<br>
# Setting up Azure connectivity
The Keyword Detection application can connect to an Azure IoT Hub and report ML inference results through that connection.
To connect to the Azure IoT Hub cloud service you will need to setup a device and then set the device connection string within the Application. You will need to create an [Azure Account](https://azure.microsoft.com/) if you don’t already have one.
## Azure IoT Hub setup
To create a new Azure IoT Hub and one device within it through the web portal follow the [instructions](https://docs.microsoft.com/en-us/azure/iot-hub/iot-hub-create-through-portal) provided by Azure.
## Configure the application to connect to your Azure IoT Hub
Now that you have created a device in your IoT Hub, the application must be configured to connect to the Azure IoT Hub with the credentials of the device created.
Within the application directory that you are using, edit the `bsp/default_credentials/iothub_credentials.h` file.
You must set the define `IOTHUB_DEVICE_CONNECTION_STRING` to the value of the device's `Primary Connection String`. This value can be [retrieved](https://docs.microsoft.com/en-us/azure/iot-hub/iot-hub-create-through-portal#register-a-new-device-in-the-iot-hub) in the portal.
## Build the application to connect to your Azure IoT Hub
The application selects a cloud client (Aws or Azure) at build time. This is achieved by adding the flag `-e <AZURE|AWS>` to the build command line.
To build a version of kws connecting to the Azure cloud on Corstone-300 and using FreeRTOS, use the following command line:
```sh
./ats.sh build kws -e AZURE
```
## Monitoring messages sent to your Azure IoT Hub
The Azure web portal does not offer monitoring facilities to visualize packets received out of the box.
To monitor packets, you can use the [Azure IoT Tools](https://marketplace.visualstudio.com/items?itemName=vsciot-vscode.azure-iot-tools) VS Code extension and follow these [instructions](https://docs.microsoft.com/en-us/azure/iot-hub/iot-hub-vscode-iot-toolkit-cloud-device-messaging#monitor-device-to-cloud-messages).
To monitor activity (connection, disconnection, ...) follow the [reference instructions](https://docs.microsoft.com/en-us/azure/iot-hub/monitor-iot-hub).
# Terminate Arm Virtual Hardware Instance
When you are done using the AMI instance at the end of the day, you need to make sure you shut it down properly or you may be charged for usage you did not actually use. There are 2 ways to do this action (pick one):
......@@ -460,18 +497,18 @@ Execute the following script located in the application repository.
- `bsp`: Arm Corstone-300 subsystem platform code and AWS configurations.
- `lib`: Middleware used by IoT Total Solution.
- `lib/mcuboot`: MCUboot bootloader.
- `lib/tf-m`: Trusted Firmware M: [Platform Security Architecture](PSA) for Armv8-M.
- `lib/mbedcrypto`: Mbed TLS and PSA cryptographic APIs.
- `lib/ml-embedded-evaluation-kit`: Arm ML embedded evaluation kit Ethos NPU. It includes [TensorFlow](https://www.tensorflow.org/)
- `lib/amazon_freertos`: FreeRTOS distribution.
- `libVHT`: Virtual streaming solution for Arm Virtual Hardware.
- `AWS`: OTA and PKCS11 integration with AWS IoT SDK.
- `VHT`: Virtual streaming solution for Arm Virtual Hardware.
- `blinky`: Blinky application.
- `blinky/main_ns.c`: Entry point of the blinky application
- `kws`: Keyword detection application.
- `kws/source/main_ns.c`: Entry point of the kws application.
- `kws/source/aws_demo.c`: AWS IoT specific code of the kws application.
- `kws/source/azure_demo.c`: Azure IoT Hub specific code of the kws application.
- `kws/source/blinky_task.c`: Blinky/UX thread of the application.
- `kws/source/ml_interface.c`: Interface between the virtual streaming solution and tensor flow.
- `kws/source/ethosu_platform_adaptation.c`: RTOS adapatation for the Ethos U55.
- `kws/ota`: Connector between AWS OTA and the application.
- `mlia`: Integration the ML Inference Advisor, using a simple wrapper script.sh to install and run the tool on given models.
# ML Model Replacement
......
......@@ -41,7 +41,7 @@ target_include_directories(${LIB_NAME}
${TFM_PLATFORM_TARGET_DIR}/arm/mps3/an547/services/src
${TFM_PLATFORM_TARGET_DIR}/arm/mps3/common
${PRJ_DIR}/bsp/aws_configs
${AWS_CONFIG_CREDENTIALS_PATH}
${APP_CONFIG_CREDENTIALS_PATH}
${PRJ_DIR}/bsp/aws_libraries/3rdparty/jsmn
${PRJ_DIR}/bsp/aws_libraries/3rdparty/mbedtls_config
${PRJ_DIR}/bsp/aws_libraries/3rdparty/mbedtls_utils
......
......@@ -277,7 +277,7 @@
//#define MBEDTLS_PLATFORM_FPRINTF_ALT
//#define MBEDTLS_PLATFORM_PRINTF_ALT
//#define MBEDTLS_PLATFORM_SNPRINTF_ALT
//#define MBEDTLS_PLATFORM_NV_SEED_ALT
#define MBEDTLS_PLATFORM_NV_SEED_ALT
//#define MBEDTLS_PLATFORM_SETUP_TEARDOWN_ALT
/**
......@@ -305,7 +305,7 @@
*
* Uncomment to get errors on using deprecated functions.
*/
#define MBEDTLS_DEPRECATED_REMOVED
//#define MBEDTLS_DEPRECATED_REMOVED
/**
* \def MBEDTLS_CHECK_PARAMS
......@@ -569,7 +569,7 @@
*
* Uncomment to use your own hardware entropy collector.
*/
#define MBEDTLS_ENTROPY_HARDWARE_ALT
//#define MBEDTLS_ENTROPY_HARDWARE_ALT
/**
* \def MBEDTLS_AES_ROM_TABLES
......@@ -1245,7 +1245,7 @@
* \note The entropy collector will write to the seed file before entropy is
* given to an external source, to update it.
*/
//#define MBEDTLS_ENTROPY_NV_SEED
#define MBEDTLS_ENTROPY_NV_SEED
/**
* \def MBEDTLS_MEMORY_DEBUG
......@@ -1473,7 +1473,7 @@
* configuration of this extension).
*
*/
//#define MBEDTLS_SSL_RENEGOTIATION
#define MBEDTLS_SSL_RENEGOTIATION
/**
* \def MBEDTLS_SSL_SRV_SUPPORT_SSLV2_CLIENT_HELLO
......@@ -2360,7 +2360,7 @@
*
* This module enables mbedtls_strerror().
*/
//#define MBEDTLS_ERROR_C
#define MBEDTLS_ERROR_C
/**
* \def MBEDTLS_GCM_C
......
/* Copyright (c) 2022, Arm Limited and Contributors. All rights reserved.
* SPDX-License-Identifier: Apache-2.0
*/
#ifndef IOTHUB_CREDENTIALS_H
#define IOTHUB_CREDENTIALS_H
#define IOTHUB_SERVER_TLS_CERTIFICATE \
/* Baltimore */ \
"-----BEGIN CERTIFICATE-----\r\n" \
"MIIDdzCCAl+gAwIBAgIEAgAAuTANBgkqhkiG9w0BAQUFADBaMQswCQYDVQQGEwJJ\r\n" \
"RTESMBAGA1UEChMJQmFsdGltb3JlMRMwEQYDVQQLEwpDeWJlclRydXN0MSIwIAYD\r\n" \
"VQQDExlCYWx0aW1vcmUg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H/MA4GA1UdDwEB/wQEAwIBhjAdBgNVHQ4EFgQUTiJUIBiV\r\n" \
"5uNu5g/6+rkS7QYXjzkwDQYJKoZIhvcNAQELBQADggEBAGBnKJRvDkhj6zHd6mcY\r\n" \
"1Yl9PMWLSn/pvtsrF9+wX3N3KjITOYFnQoQj8kVnNeyIv/iPsGEMNKSuIEyExtv4\r\n" \
"NeF22d+mQrvHRAiGfzZ0JFrabA0UWTW98kndth/Jsw1HKj2ZL7tcu7XUIOGZX1NG\r\n" \
"Fdtom/DzMNU+MeKNhJ7jitralj41E6Vf8PlwUHBHQRFXGU7Aj64GxJUTFy8bJZ91\r\n" \
"8rGOmaFvE7FBcf6IKshPECBV1/MUReXgRPTqh5Uykw7+U0b6LJ3/iyK5S9kJRaTe\r\n" \
"pLiaWN0bfVKfjllDiIGknibVb63dDcY3fe0Dkhvld1927jyNxF1WW6LZZm6zNTfl\r\n" \
"MrY=\r\n" \
"-----END CERTIFICATE-----\r\n"
#define IOTHUB_DEVICE_CONNECTION_STRING "Invalid connection string"
#endif /* IOTHUB_CREDENTIALS_H */
......@@ -61,7 +61,7 @@ extern uint32_t SystemCoreClock;
#define configUSE_ALTERNATIVE_API 0 /* Deprecated! */
#define configQUEUE_REGISTRY_SIZE 10
#define configUSE_QUEUE_SETS 0
#define configUSE_TIME_SLICING 0
#define configUSE_TIME_SLICING 1
#define configUSE_NEWLIB_REENTRANT 0
#define configENABLE_BACKWARD_COMPATIBILITY 0
#define configNUM_THREAD_LOCAL_STORAGE_POINTERS 5
......
......@@ -41,29 +41,7 @@ target_compile_definitions(lwipopts
# Bind socket interface to implementation
target_link_libraries(cmsis-sockets-api INTERFACE lwip-sockets)
target_include_directories(coreHTTP-config
INTERFACE
${PRJ_DIR}/bsp/aws_configs
${PRJ_DIR}/bsp/aws_libraries/logging/include/
)
target_include_directories(coreMQTT-config
INTERFACE
${PRJ_DIR}/bsp/aws_configs
${PRJ_DIR}/bsp/aws_libraries/logging/include/
)
target_link_libraries(coreMQTT-config
INTERFACE
cmsis-rtos-api
)
target_link_libraries(coreHTTP-config
INTERFACE
cmsis-rtos-api
)
add_executable(kws
"${PRJ_DIR}/bsp/aws_libraries/abstractions/secure_sockets/lwip/iot_secure_sockets.c"
"${PRJ_DIR}/bsp/aws_libraries/freertos_plus/standard/crypto/src/iot_crypto.c"
"${PRJ_DIR}/bsp/aws_libraries/freertos_plus/standard/tls/src/iot_tls.c"
# application
"${PRJ_DIR}/kws/source/ml_interface.cc"
"${PRJ_DIR}/kws/source/model_config.cc"
......@@ -116,7 +94,6 @@ target_compile_definitions(kws
target_include_directories(kws
PRIVATE
${PRJ_DIR}/bsp/aws_configs
${PRJ_DIR}/kws/source
${PRJ_DIR}/kws/include
......@@ -146,7 +123,6 @@ add_dependencies(kws
# final binary
target_link_libraries(kws
AWS-extra
startup
tfm-ns-interface-cmsis-rtos
cmsis-rtos-implementation
......@@ -154,6 +130,87 @@ target_link_libraries(kws
lib-bsp
)
if(${CLOUD_CLIENT} STREQUAL "AWS")
# Configure AWS libraries
target_include_directories(coreHTTP-config
INTERFACE
${PRJ_DIR}/bsp/aws_configs
${PRJ_DIR}/bsp/aws_libraries/logging/include/
)
target_include_directories(coreMQTT-config
INTERFACE
${PRJ_DIR}/bsp/aws_configs
${PRJ_DIR}/bsp/aws_libraries/logging/include/
)
target_link_libraries(coreMQTT-config
INTERFACE
cmsis-rtos-api
)
target_link_libraries(coreHTTP-config
INTERFACE
cmsis-rtos-api
)
# Complete kws
target_sources(kws
PUBLIC
"${PRJ_DIR}/bsp/aws_libraries/abstractions/secure_sockets/lwip/iot_secure_sockets.c"
"${PRJ_DIR}/bsp/aws_libraries/freertos_plus/standard/crypto/src/iot_crypto.c"
"${PRJ_DIR}/bsp/aws_libraries/freertos_plus/standard/tls/src/iot_tls.c"
source/aws_demo.c
)
target_include_directories(kws
PRIVATE
${PRJ_DIR}/bsp/aws_configs
"${PRJ_DIR}/bsp/aws_libraries/3rdparty/mbedtls_config"
)
target_link_libraries(kws
AWS-extra
)
elseif(${CLOUD_CLIENT} STREQUAL "AZURE")
# Azure SDK config
# Provides NTP implementation of time() function to Azure SDK platform library
target_link_libraries(azure-iot-sdk-platform
PUBLIC
azure-sdk-ntp-time
)
# Provides LwIP socket implementation to Azure SDK platform library
target_link_libraries(azure-iot-sdk-platform
PUBLIC
lwip-sockets
)
target_sources(kws
PUBLIC
source/azure_demo.c
)
target_include_directories(kws
PRIVATE
${APP_CONFIG_CREDENTIALS_PATH}
)
# final binary
target_link_libraries(kws
mbedtls
tfm-ns-interface
lwip-sockets
lwipcore
lwipopts
iotsdk-ip-network-api
cmsis-sockets-api
mcu-driver-bootstrap
mcu-driver-hal
cmsis-rtos-api
azure-iothub-client
)
endif()
target_link_options(kws
PRIVATE
${ml-embedded-evaluation-kit_LIB_DIR}/liblibethos-u-kws.a
......
/* Copyright (c) 2017-2022, Arm Limited and Contributors. All rights reserved.
* SPDX-License-Identifier: Apache-2.0
*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <stdbool.h>
#include "cmsis_os2.h"
#include "iotsdk/ip_network_api.h"
/* includes for TFM */
#include "psa/update.h"
/* includes for IoT Cloud */
#include "iot_logging_task.h"
#include "aws_dev_mode_key_provisioning.h"
#include "ota_provision.h"
#include "version/application_version.h"
#define IOT_LOGGING_QUEUE_SIZE 90
extern void DEMO_RUNNER_RunDemos(void);
psa_key_handle_t xOTACodeVerifyKeyHandle = NULL;
void network_state_callback(network_state_callback_event_t status)
{
if (status == NETWORK_UP) {
printf("[INF] network up, starting demo\r\n");
if (strcmp(clientcredentialMQTT_BROKER_ENDPOINT, "endpointid.amazonaws.com") == 0) {
printf("[ERR] INVALID CREDENTIALS AND ENDPOINT.\r\n");
printf("[ERR] Set the right configuration and credentials in aws_clientcredential.h and "
"aws_clientcredential_keys.h\r\n");
// Start the inference directly
ml_task_inference_start();
} else {
DEMO_RUNNER_RunDemos();
}
} else {
printf("[ERR] network down\r\n");
}
}
void print_version()
{
if (GetImageVersionPSA(FWU_IMAGE_TYPE_NONSECURE) == 0) {
printf("Firmware version: %d.%d.%d\r\n",
xAppFirmwareVersion.u.x.major,
xAppFirmwareVersion.u.x.minor,
xAppFirmwareVersion.u.x.build);
}
}
int endpoint_init(void)
{
BaseType_t log_initialized = xLoggingTaskInitialize(0, osPriorityNormal2, IOT_LOGGING_QUEUE_SIZE);
if (log_initialized != pdTRUE) {
printf("Failed to initialize logging task [%ld]\r\n", log_initialized);
return -1;
}
CK_RV provisionning_status = vDevModeKeyProvisioning();
if (provisionning_status == osOK) {
BaseType_t ret = ota_privision_code_signing_key(&xOTACodeVerifyKeyHandle);
if (ret != PSA_SUCCESS) {
printf("ota_privision_code_signing_key failed [%d]\r\n", ret);
}
}
print_version();
osStatus_t status = start_network_task(network_state_callback, 0);
if (status != osOK) {
printf("Failed to start network task [%d]\r\n", status);
return -1;
}
return 0;
}
/*
* Copyright (c) 2022, Arm Limited and Contributors. All rights reserved.
* SPDX-License-Identifier: Apache-2.0
*/
/* This example shows device-to-cloud messaging with Azure.
*/
#include <stdlib.h>
#include <inttypes.h>
#include <stdarg.h>
#include <stdio.h>
#include <string.h>
#include <stdbool.h>
#include "cmsis_os2.h"
#include "RTOS_config.h"
#include "azure_c_shared_utility/shared_util_options.h"
#include "azure_c_shared_utility/xlogging.h"
#include "iothub_client_options.h"
#include "iothub_device_client_ll.h"
#include "iothub_message.h"
#include "iothub.h"
#include "iothubtransportmqtt.h"
#include "mbedtls/threading.h"
#include "mbedtls/platform.h"
#include "iothub_credentials.h"
#include "ml_interface.h"
/** Azure IoT Hub server TLS certificate - mbedtls specific.
* It's passed as OPTION_TRUSTED_CERT option to IoT Hub client.
*/
const char *certificates = IOTHUB_SERVER_TLS_CERTIFICATE;
const char *connectionString = IOTHUB_DEVICE_CONNECTION_STRING;
extern void azure_task(void *arg);
int endpoint_init(void)
{
static const osThreadAttr_t az_task_attr = {.priority = osPriorityNormal, .name = "AZURE_TASK", .stack_size = 8192};
osThreadId_t demo_thread = osThreadNew(azure_task, NULL, &az_task_attr);
if (!demo_thread) {
printf("Failed to create thread\r\n");
return -1;
}
return 0;
}
typedef enum {
APP_EVENT_IOT_HUB_CONNECTION_UP,
APP_EVENT_IOT_HUB_CONNECTION_DOWN,
APP_EVENT_SEND_MSG,
APP_EVENT_SEND_MSG_OK,
APP_EVENT_SEND_MSG_ERROR,
APP_EVENT_NONE
} app_event_t;
typedef struct {
app_event_t event;
union {
int32_t return_code;
ml_processing_state_t ml_state;
};
} app_msg_t;
static osMessageQueueId_t app_msg_queue = NULL;
/** This example use NV seed as entropy generator.
* Set dummy data as NV seed.
*/
static int mbedtls_platform_example_nv_seed_read(unsigned char *buf, size_t buf_len)
{
if (buf == NULL) {
return (-1);
}
memset(buf, 0xA5, buf_len);
return 0;
}
static int mbedtls_platform_example_nv_seed_write(unsigned char *buf, size_t buf_len)
{
return 0;
}
#ifdef IOTSDK_AZURE_SDK_LOGGING
static void iothub_logging(
LOG_CATEGORY log_category, const char *file, const char *func, int line, unsigned int options, const char *fmt, ...)
{
const char *p, *basename;
/* Extract basename from file */
for (p = basename = file; *p != '\0'; p++) {
if (*p == '/' || *p == '\\')
basename = p + 1;
}
// determine required buffer size
va_list args;
va_start(args, fmt);
int len = vsnprintf(NULL, 0, fmt, args);
va_end(args);
if (len < 0)
return;
// format message
char msg[len + 1];
va_start(args, fmt);
vsnprintf(msg, len + 1, fmt, args);
va_end(args);
switch (log_category) {
case AZ_LOG_ERROR:
printf("Error - %s:%d: %s\r\n", basename, line, msg);
break;
case AZ_LOG_INFO:
printf("Info - %s:%d: %s\r\n", basename, line, msg);
break;
case AZ_LOG_TRACE:
printf("Debug - %s:%d: %s\r\n", basename, line, msg);
break;
default:
break;
}
}
#endif // IOTSDK_AZURE_SDK_LOGGING
/** This callback is called by the Azure IoT Hub client.
* It handles IoT Hub connection status.
*/
static void connection_status_callback(IOTHUB_CLIENT_CONNECTION_STATUS result,
IOTHUB_CLIENT_CONNECTION_STATUS_REASON reason,
void *ignored)
{
((void)ignored);