Microsoft Azure IoTHub client MQTT transport

Dependents:   STM32F746_iothub_client_sample_mqtt FXOS8700CQ_To_Azure_IoT f767zi_mqtt FXOS8700CQ_To_Azure_IoT ... more

Committer:
AzureIoTClient
Date:
Thu Oct 20 17:07:21 2016 -0700
Revision:
11:31ebaeb51e1e
Parent:
10:c61c16bb63e6
Child:
12:658ca6865de2
1.0.10

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Azure.IoT Build 0:5e72a75c31b8 1 // Copyright (c) Microsoft. All rights reserved.
Azure.IoT Build 0:5e72a75c31b8 2 // Licensed under the MIT license. See LICENSE file in the project root for full license information.
Azure.IoT Build 0:5e72a75c31b8 3
Azure.IoT Build 0:5e72a75c31b8 4 #include <stdlib.h>
Azure.IoT Build 0:5e72a75c31b8 5 #ifdef _CRTDBG_MAP_ALLOC
Azure.IoT Build 0:5e72a75c31b8 6 #include <crtdbg.h>
Azure.IoT Build 0:5e72a75c31b8 7 #endif
Azure.IoT Build 0:5e72a75c31b8 8
Azure.IoT Build 0:5e72a75c31b8 9 #include "iothubtransportmqtt.h"
AzureIoTClient 11:31ebaeb51e1e 10 #include "azure_c_shared_utility/xio.h"
Azure.IoT Build 1:f2e563755d91 11 #include "azure_c_shared_utility/tlsio.h"
Azure.IoT Build 1:f2e563755d91 12 #include "azure_c_shared_utility/platform.h"
AzureIoTClient 11:31ebaeb51e1e 13 #include "iothubtransport_mqtt_common.h"
AzureIoTClient 7:7fdd306e6224 14
AzureIoTClient 11:31ebaeb51e1e 15 static XIO_HANDLE getIoTransportProvider(const char* fqdn)
AzureIoTClient 6:16875b609849 16 {
AzureIoTClient 11:31ebaeb51e1e 17 XIO_HANDLE result;
AzureIoTClient 11:31ebaeb51e1e 18 const IO_INTERFACE_DESCRIPTION* io_interface_description = platform_get_default_tlsio();
AzureIoTClient 11:31ebaeb51e1e 19 if (io_interface_description == NULL)
AzureIoTClient 4:e472f5ce3473 20 {
AzureIoTClient 11:31ebaeb51e1e 21 /* Codes_SRS_IOTHUB_MQTT_TRANSPORT_07_013: [ If platform_get_default_tlsio returns NULL getIoTransportProvider shall return NULL. ] */
AzureIoTClient 11:31ebaeb51e1e 22 LogError("Failure constructing the provider interface");
AzureIoTClient 4:e472f5ce3473 23 result = NULL;
AzureIoTClient 4:e472f5ce3473 24 }
AzureIoTClient 4:e472f5ce3473 25 else
AzureIoTClient 4:e472f5ce3473 26 {
AzureIoTClient 11:31ebaeb51e1e 27 TLSIO_CONFIG tls_io_config;
AzureIoTClient 11:31ebaeb51e1e 28 tls_io_config.hostname = fqdn;
AzureIoTClient 11:31ebaeb51e1e 29 tls_io_config.port = 8883;
AzureIoTClient 11:31ebaeb51e1e 30 /* Codes_SRS_IOTHUB_MQTT_TRANSPORT_07_012: [ getIoTransportProvider shall return the XIO_HANDLE returns by xio_create. ] */
AzureIoTClient 11:31ebaeb51e1e 31 result = xio_create(io_interface_description, &tls_io_config);
AzureIoTClient 2:9db4da50abaa 32 }
AzureIoTClient 4:e472f5ce3473 33 return result;
Azure.IoT Build 0:5e72a75c31b8 34 }
Azure.IoT Build 0:5e72a75c31b8 35
AzureIoTClient 4:e472f5ce3473 36 static TRANSPORT_LL_HANDLE IoTHubTransportMqtt_Create(const IOTHUBTRANSPORT_CONFIG* config)
Azure.IoT Build 0:5e72a75c31b8 37 {
AzureIoTClient 11:31ebaeb51e1e 38 /* Codes_SRS_IOTHUB_MQTT_TRANSPORT_07_001: [IoTHubTransportMqtt_Create shall create a TRANSPORT_LL_HANDLE by calling into the IoTHubMqttAbstract_Create function.] */
AzureIoTClient 11:31ebaeb51e1e 39 return IoTHubTransport_MQTT_Common_Create(config, getIoTransportProvider);
Azure.IoT Build 0:5e72a75c31b8 40 }
Azure.IoT Build 0:5e72a75c31b8 41
AzureIoTClient 4:e472f5ce3473 42 static void IoTHubTransportMqtt_Destroy(TRANSPORT_LL_HANDLE handle)
Azure.IoT Build 0:5e72a75c31b8 43 {
AzureIoTClient 11:31ebaeb51e1e 44 /* Codes_SRS_IOTHUB_MQTT_TRANSPORT_07_002: [ IoTHubTransportMqtt_Destroy shall destroy the TRANSPORT_LL_HANDLE by calling into the IoTHubMqttAbstract_Destroy function. ] */
AzureIoTClient 11:31ebaeb51e1e 45 IoTHubTransport_MQTT_Common_Destroy(handle);
Azure.IoT Build 0:5e72a75c31b8 46 }
Azure.IoT Build 0:5e72a75c31b8 47
AzureIoTClient 4:e472f5ce3473 48 static int IoTHubTransportMqtt_Subscribe(IOTHUB_DEVICE_HANDLE handle)
Azure.IoT Build 0:5e72a75c31b8 49 {
AzureIoTClient 11:31ebaeb51e1e 50 /* Codes_SRS_IOTHUB_MQTT_TRANSPORT_07_005: [ IoTHubTransportMqtt_Subscribe shall subscribe the TRANSPORT_LL_HANDLE by calling into the IoTHubMqttAbstract_Subscribe function. ] */
AzureIoTClient 11:31ebaeb51e1e 51 return IoTHubTransport_MQTT_Common_Subscribe(handle);
Azure.IoT Build 0:5e72a75c31b8 52 }
Azure.IoT Build 0:5e72a75c31b8 53
AzureIoTClient 4:e472f5ce3473 54 static void IoTHubTransportMqtt_Unsubscribe(IOTHUB_DEVICE_HANDLE handle)
Azure.IoT Build 0:5e72a75c31b8 55 {
AzureIoTClient 11:31ebaeb51e1e 56 /* Codes_SRS_IOTHUB_MQTT_TRANSPORT_07_006: [ IoTHubTransportMqtt_Unsubscribe shall unsubscribe the TRANSPORT_LL_HANDLE by calling into the IoTHubMqttAbstract_Unsubscribe function. ] */
AzureIoTClient 11:31ebaeb51e1e 57 IoTHubTransport_MQTT_Common_Unsubscribe(handle);
Azure.IoT Build 0:5e72a75c31b8 58 }
Azure.IoT Build 0:5e72a75c31b8 59
AzureIoTClient 4:e472f5ce3473 60 static void IoTHubTransportMqtt_DoWork(TRANSPORT_LL_HANDLE handle, IOTHUB_CLIENT_LL_HANDLE iotHubClientHandle)
Azure.IoT Build 0:5e72a75c31b8 61 {
AzureIoTClient 11:31ebaeb51e1e 62 /* Codes_SRS_IOTHUB_MQTT_TRANSPORT_07_007: [ IoTHubTransportMqtt_DoWork shall call into the IoTHubMqttAbstract_DoWork function. ] */
AzureIoTClient 11:31ebaeb51e1e 63 IoTHubTransport_MQTT_Common_DoWork(handle, iotHubClientHandle);
Azure.IoT Build 0:5e72a75c31b8 64 }
Azure.IoT Build 0:5e72a75c31b8 65
AzureIoTClient 4:e472f5ce3473 66 static IOTHUB_CLIENT_RESULT IoTHubTransportMqtt_GetSendStatus(IOTHUB_DEVICE_HANDLE handle, IOTHUB_CLIENT_STATUS *iotHubClientStatus)
Azure.IoT Build 0:5e72a75c31b8 67 {
AzureIoTClient 11:31ebaeb51e1e 68 /* Codes_SRS_IOTHUB_MQTT_TRANSPORT_07_008: [ IoTHubTransportMqtt_GetSendStatus shall get the send status by calling into the IoTHubMqttAbstract_GetSendStatus function. ] */
AzureIoTClient 11:31ebaeb51e1e 69 return IoTHubTransport_MQTT_Common_GetSendStatus(handle, iotHubClientStatus);
Azure.IoT Build 0:5e72a75c31b8 70 }
Azure.IoT Build 0:5e72a75c31b8 71
AzureIoTClient 4:e472f5ce3473 72 static IOTHUB_CLIENT_RESULT IoTHubTransportMqtt_SetOption(TRANSPORT_LL_HANDLE handle, const char* option, const void* value)
Azure.IoT Build 0:5e72a75c31b8 73 {
AzureIoTClient 11:31ebaeb51e1e 74 /* Codes_SRS_IOTHUB_MQTT_TRANSPORT_07_009: [ IoTHubTransportMqtt_SetOption shall set the options by calling into the IoTHubMqttAbstract_SetOption function. ] */
AzureIoTClient 11:31ebaeb51e1e 75 return IoTHubTransport_MQTT_Common_SetOption(handle, option, value);
Azure.IoT Build 0:5e72a75c31b8 76 }
Azure.IoT Build 0:5e72a75c31b8 77
AzureIoTClient 4:e472f5ce3473 78 static IOTHUB_DEVICE_HANDLE IoTHubTransportMqtt_Register(TRANSPORT_LL_HANDLE handle, const IOTHUB_DEVICE_CONFIG* device, IOTHUB_CLIENT_LL_HANDLE iotHubClientHandle, PDLIST_ENTRY waitingToSend)
Azure.IoT Build 0:5e72a75c31b8 79 {
AzureIoTClient 11:31ebaeb51e1e 80 /* Codes_SRS_IOTHUB_MQTT_TRANSPORT_07_003: [ IoTHubTransportMqtt_Register shall register the TRANSPORT_LL_HANDLE by calling into the IoTHubMqttAbstract_Register function. ] */
AzureIoTClient 11:31ebaeb51e1e 81 return IoTHubTransport_MQTT_Common_Register(handle, device, iotHubClientHandle, waitingToSend);
Azure.IoT Build 0:5e72a75c31b8 82 }
Azure.IoT Build 0:5e72a75c31b8 83
AzureIoTClient 4:e472f5ce3473 84 static void IoTHubTransportMqtt_Unregister(IOTHUB_DEVICE_HANDLE deviceHandle)
Azure.IoT Build 0:5e72a75c31b8 85 {
AzureIoTClient 11:31ebaeb51e1e 86 /* Codes_SRS_IOTHUB_MQTT_TRANSPORT_07_004: [ IoTHubTransportMqtt_Unregister shall register the TRANSPORT_LL_HANDLE by calling into the IoTHubMqttAbstract_Unregister function. ] */
AzureIoTClient 11:31ebaeb51e1e 87 IoTHubTransport_MQTT_Common_Unregister(deviceHandle);
Azure.IoT Build 0:5e72a75c31b8 88 }
Azure.IoT Build 0:5e72a75c31b8 89
AzureIoTClient 4:e472f5ce3473 90 static STRING_HANDLE IoTHubTransportMqtt_GetHostname(TRANSPORT_LL_HANDLE handle)
AzureIoTClient 4:e472f5ce3473 91 {
AzureIoTClient 11:31ebaeb51e1e 92 /* Codes_SRS_IOTHUB_MQTT_TRANSPORT_07_010: [ IoTHubTransportMqtt_GetHostname shall get the hostname by calling into the IoTHubMqttAbstract_GetHostname function. ] */
AzureIoTClient 11:31ebaeb51e1e 93 return IoTHubTransport_MQTT_Common_GetHostname(handle);
AzureIoTClient 4:e472f5ce3473 94 }
AzureIoTClient 4:e472f5ce3473 95
AzureIoTClient 4:e472f5ce3473 96 static TRANSPORT_PROVIDER myfunc = {
AzureIoTClient 4:e472f5ce3473 97 IoTHubTransportMqtt_GetHostname,
AzureIoTClient 4:e472f5ce3473 98 IoTHubTransportMqtt_SetOption,
AzureIoTClient 4:e472f5ce3473 99 IoTHubTransportMqtt_Create,
AzureIoTClient 4:e472f5ce3473 100 IoTHubTransportMqtt_Destroy,
AzureIoTClient 4:e472f5ce3473 101 IoTHubTransportMqtt_Register,
AzureIoTClient 4:e472f5ce3473 102 IoTHubTransportMqtt_Unregister,
AzureIoTClient 4:e472f5ce3473 103 IoTHubTransportMqtt_Subscribe,
AzureIoTClient 4:e472f5ce3473 104 IoTHubTransportMqtt_Unsubscribe,
AzureIoTClient 4:e472f5ce3473 105 IoTHubTransportMqtt_DoWork,
AzureIoTClient 4:e472f5ce3473 106 IoTHubTransportMqtt_GetSendStatus
Azure.IoT Build 0:5e72a75c31b8 107 };
Azure.IoT Build 0:5e72a75c31b8 108
AzureIoTClient 11:31ebaeb51e1e 109 /* Codes_SRS_IOTHUB_MQTT_TRANSPORT_07_011: [ This function shall return a pointer to a structure of type TRANSPORT_PROVIDER having the following values for its fields:
AzureIoTClient 11:31ebaeb51e1e 110 IoTHubTransport_GetHostname = IoTHubTransportMqtt_GetHostname
AzureIoTClient 11:31ebaeb51e1e 111 IoTHubTransport_Create = IoTHubTransportMqtt_Create
Azure.IoT Build 0:5e72a75c31b8 112 IoTHubTransport_Destroy = IoTHubTransportMqtt_Destroy
Azure.IoT Build 0:5e72a75c31b8 113 IoTHubTransport_Subscribe = IoTHubTransportMqtt_Subscribe
Azure.IoT Build 0:5e72a75c31b8 114 IoTHubTransport_Unsubscribe = IoTHubTransportMqtt_Unsubscribe
Azure.IoT Build 0:5e72a75c31b8 115 IoTHubTransport_DoWork = IoTHubTransportMqtt_DoWork
AzureIoTClient 11:31ebaeb51e1e 116 IoTHubTransport_SetOption = IoTHubTransportMqtt_SetOption ] */
AzureIoTClient 11:31ebaeb51e1e 117 const TRANSPORT_PROVIDER* MQTT_Protocol(void)
Azure.IoT Build 0:5e72a75c31b8 118 {
AzureIoTClient 4:e472f5ce3473 119 return &myfunc;
Azure.IoT Build 0:5e72a75c31b8 120 }