Microsoft Azure IoTHub client MQTT transport
Dependents: STM32F746_iothub_client_sample_mqtt FXOS8700CQ_To_Azure_IoT f767zi_mqtt FXOS8700CQ_To_Azure_IoT ... more
iothubtransportmqtt.c@14:4dc2b011be33, 2017-01-08 (annotated)
- Committer:
- AzureIoTClient
- Date:
- Sun Jan 08 11:11:42 2017 -0800
- Revision:
- 14:4dc2b011be33
- Parent:
- 12:658ca6865de2
- Child:
- 16:14640ee83e99
1.1.3
Who changed what in which revision?
User | Revision | Line number | New 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 | 14:4dc2b011be33 | 60 | /* Codes_SRS_IOTHUB_MQTT_TRANSPORT_07_026: [ IoTHubTransportMqtt_Subscribe_DeviceMethod shall subscribe the TRANSPORT_LL_HANDLE by calling into the IoTHubMqttAbstract_Subscribe_DeviceMethod function. ] */ |
AzureIoTClient | 12:658ca6865de2 | 61 | static int IoTHubTransportMqtt_Subscribe_DeviceMethod(IOTHUB_DEVICE_HANDLE handle) |
AzureIoTClient | 12:658ca6865de2 | 62 | { |
AzureIoTClient | 12:658ca6865de2 | 63 | return IoTHubTransport_MQTT_Common_Subscribe_DeviceMethod(handle); |
AzureIoTClient | 12:658ca6865de2 | 64 | } |
AzureIoTClient | 12:658ca6865de2 | 65 | |
AzureIoTClient | 14:4dc2b011be33 | 66 | /* Codes_SRS_IOTHUB_MQTT_TRANSPORT_07_027: [ IoTHubTransportMqtt_Unsubscribe_DeviceMethod shall call into the IoTHubMqttAbstract_Unsubscribe_DeviceMethod function. ] */ |
AzureIoTClient | 12:658ca6865de2 | 67 | static void IoTHubTransportMqtt_Unsubscribe_DeviceMethod(IOTHUB_DEVICE_HANDLE handle) |
AzureIoTClient | 12:658ca6865de2 | 68 | { |
AzureIoTClient | 12:658ca6865de2 | 69 | IoTHubTransport_MQTT_Common_Unsubscribe_DeviceMethod(handle); |
AzureIoTClient | 12:658ca6865de2 | 70 | } |
AzureIoTClient | 12:658ca6865de2 | 71 | |
AzureIoTClient | 14:4dc2b011be33 | 72 | /* Codes_SRS_IOTHUB_MQTT_TRANSPORT_07_025: [ IoTHubTransportMqtt_Subscribe_DeviceTwin shall call into the IoTHubMqttAbstract_Subscribe_DeviceTwin function. ] */ |
AzureIoTClient | 12:658ca6865de2 | 73 | static int IoTHubTransportMqtt_Subscribe_DeviceTwin(IOTHUB_DEVICE_HANDLE handle) |
AzureIoTClient | 12:658ca6865de2 | 74 | { |
AzureIoTClient | 12:658ca6865de2 | 75 | return IoTHubTransport_MQTT_Common_Subscribe_DeviceTwin(handle); |
AzureIoTClient | 12:658ca6865de2 | 76 | } |
AzureIoTClient | 12:658ca6865de2 | 77 | |
AzureIoTClient | 14:4dc2b011be33 | 78 | /* Codes_SRS_IOTHUB_MQTT_TRANSPORT_07_024: [ IoTHubTransportMqtt_Unsubscribe_DeviceTwin shall shall call into the IoTHubMqttAbstract_Unsubscribe_DeviceTwin function. ] */ |
AzureIoTClient | 12:658ca6865de2 | 79 | static void IoTHubTransportMqtt_Unsubscribe_DeviceTwin(IOTHUB_DEVICE_HANDLE handle) |
AzureIoTClient | 12:658ca6865de2 | 80 | { |
AzureIoTClient | 12:658ca6865de2 | 81 | IoTHubTransport_MQTT_Common_Unsubscribe_DeviceTwin(handle); |
AzureIoTClient | 12:658ca6865de2 | 82 | } |
AzureIoTClient | 12:658ca6865de2 | 83 | |
AzureIoTClient | 14:4dc2b011be33 | 84 | /* Codes_SRS_IOTHUB_MQTT_TRANSPORT_07_023: [ IoTHubTransportMqtt_DeviceMethod_Response shall call into the IoTHubMqttAbstract_DeviceMethod_Response function. ] */ |
AzureIoTClient | 14:4dc2b011be33 | 85 | static int IoTHubTransportMqtt_DeviceMethod_Response(IOTHUB_DEVICE_HANDLE handle, METHOD_HANDLE methodId, const unsigned char* response, size_t response_size, int status_response) |
AzureIoTClient | 14:4dc2b011be33 | 86 | { |
AzureIoTClient | 14:4dc2b011be33 | 87 | return IoTHubTransport_MQTT_Common_DeviceMethod_Response(handle, methodId, response, response_size, status_response); |
AzureIoTClient | 14:4dc2b011be33 | 88 | } |
AzureIoTClient | 14:4dc2b011be33 | 89 | |
AzureIoTClient | 12:658ca6865de2 | 90 | static IOTHUB_PROCESS_ITEM_RESULT IoTHubTransportMqtt_ProcessItem(TRANSPORT_LL_HANDLE handle, IOTHUB_IDENTITY_TYPE item_type, IOTHUB_IDENTITY_INFO* iothub_item) |
AzureIoTClient | 12:658ca6865de2 | 91 | { |
AzureIoTClient | 12:658ca6865de2 | 92 | return IoTHubTransport_MQTT_Common_ProcessItem(handle, item_type, iothub_item); |
AzureIoTClient | 12:658ca6865de2 | 93 | } |
AzureIoTClient | 12:658ca6865de2 | 94 | |
AzureIoTClient | 12:658ca6865de2 | 95 | /* Codes_SRS_IOTHUB_MQTT_TRANSPORT_07_054: [ IoTHubTransportMqtt_DoWork shall subscribe to the Notification and get_state Topics if they are defined. ] */ |
AzureIoTClient | 4:e472f5ce3473 | 96 | static void IoTHubTransportMqtt_DoWork(TRANSPORT_LL_HANDLE handle, IOTHUB_CLIENT_LL_HANDLE iotHubClientHandle) |
Azure.IoT Build | 0:5e72a75c31b8 | 97 | { |
AzureIoTClient | 11:31ebaeb51e1e | 98 | /* Codes_SRS_IOTHUB_MQTT_TRANSPORT_07_007: [ IoTHubTransportMqtt_DoWork shall call into the IoTHubMqttAbstract_DoWork function. ] */ |
AzureIoTClient | 11:31ebaeb51e1e | 99 | IoTHubTransport_MQTT_Common_DoWork(handle, iotHubClientHandle); |
Azure.IoT Build | 0:5e72a75c31b8 | 100 | } |
Azure.IoT Build | 0:5e72a75c31b8 | 101 | |
AzureIoTClient | 12:658ca6865de2 | 102 | static int IoTHubTransportMqtt_SetRetryPolicy(TRANSPORT_LL_HANDLE handle, IOTHUB_CLIENT_RETRY_POLICY retryPolicy, size_t retryTimeoutLimitInSeconds) |
AzureIoTClient | 12:658ca6865de2 | 103 | { |
AzureIoTClient | 12:658ca6865de2 | 104 | /* Codes_SRS_IOTHUB_MQTT_TRANSPORT_25_012: [** IoTHubTransportMqtt_SetRetryPolicy shall call into the IoTHubMqttAbstract_SetRetryPolicy function. ] */ |
AzureIoTClient | 12:658ca6865de2 | 105 | return IoTHubTransport_MQTT_Common_SetRetryPolicy(handle, retryPolicy, retryTimeoutLimitInSeconds); |
AzureIoTClient | 12:658ca6865de2 | 106 | } |
AzureIoTClient | 12:658ca6865de2 | 107 | |
AzureIoTClient | 4:e472f5ce3473 | 108 | static IOTHUB_CLIENT_RESULT IoTHubTransportMqtt_GetSendStatus(IOTHUB_DEVICE_HANDLE handle, IOTHUB_CLIENT_STATUS *iotHubClientStatus) |
Azure.IoT Build | 0:5e72a75c31b8 | 109 | { |
AzureIoTClient | 11:31ebaeb51e1e | 110 | /* Codes_SRS_IOTHUB_MQTT_TRANSPORT_07_008: [ IoTHubTransportMqtt_GetSendStatus shall get the send status by calling into the IoTHubMqttAbstract_GetSendStatus function. ] */ |
AzureIoTClient | 11:31ebaeb51e1e | 111 | return IoTHubTransport_MQTT_Common_GetSendStatus(handle, iotHubClientStatus); |
Azure.IoT Build | 0:5e72a75c31b8 | 112 | } |
Azure.IoT Build | 0:5e72a75c31b8 | 113 | |
AzureIoTClient | 4:e472f5ce3473 | 114 | static IOTHUB_CLIENT_RESULT IoTHubTransportMqtt_SetOption(TRANSPORT_LL_HANDLE handle, const char* option, const void* value) |
Azure.IoT Build | 0:5e72a75c31b8 | 115 | { |
AzureIoTClient | 11:31ebaeb51e1e | 116 | /* Codes_SRS_IOTHUB_MQTT_TRANSPORT_07_009: [ IoTHubTransportMqtt_SetOption shall set the options by calling into the IoTHubMqttAbstract_SetOption function. ] */ |
AzureIoTClient | 11:31ebaeb51e1e | 117 | return IoTHubTransport_MQTT_Common_SetOption(handle, option, value); |
Azure.IoT Build | 0:5e72a75c31b8 | 118 | } |
Azure.IoT Build | 0:5e72a75c31b8 | 119 | |
AzureIoTClient | 4:e472f5ce3473 | 120 | 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 | 121 | { |
AzureIoTClient | 11:31ebaeb51e1e | 122 | /* 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 | 123 | return IoTHubTransport_MQTT_Common_Register(handle, device, iotHubClientHandle, waitingToSend); |
Azure.IoT Build | 0:5e72a75c31b8 | 124 | } |
Azure.IoT Build | 0:5e72a75c31b8 | 125 | |
AzureIoTClient | 4:e472f5ce3473 | 126 | static void IoTHubTransportMqtt_Unregister(IOTHUB_DEVICE_HANDLE deviceHandle) |
Azure.IoT Build | 0:5e72a75c31b8 | 127 | { |
AzureIoTClient | 11:31ebaeb51e1e | 128 | /* 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 | 129 | IoTHubTransport_MQTT_Common_Unregister(deviceHandle); |
Azure.IoT Build | 0:5e72a75c31b8 | 130 | } |
Azure.IoT Build | 0:5e72a75c31b8 | 131 | |
AzureIoTClient | 4:e472f5ce3473 | 132 | static STRING_HANDLE IoTHubTransportMqtt_GetHostname(TRANSPORT_LL_HANDLE handle) |
AzureIoTClient | 4:e472f5ce3473 | 133 | { |
AzureIoTClient | 11:31ebaeb51e1e | 134 | /* Codes_SRS_IOTHUB_MQTT_TRANSPORT_07_010: [ IoTHubTransportMqtt_GetHostname shall get the hostname by calling into the IoTHubMqttAbstract_GetHostname function. ] */ |
AzureIoTClient | 11:31ebaeb51e1e | 135 | return IoTHubTransport_MQTT_Common_GetHostname(handle); |
AzureIoTClient | 4:e472f5ce3473 | 136 | } |
AzureIoTClient | 4:e472f5ce3473 | 137 | |
AzureIoTClient | 12:658ca6865de2 | 138 | static TRANSPORT_PROVIDER myfunc = |
AzureIoTClient | 12:658ca6865de2 | 139 | { |
AzureIoTClient | 12:658ca6865de2 | 140 | IoTHubTransportMqtt_Subscribe_DeviceMethod, /*pfIoTHubTransport_Subscribe_DeviceMethod IoTHubTransport_Subscribe_DeviceMethod;*/ |
AzureIoTClient | 12:658ca6865de2 | 141 | IoTHubTransportMqtt_Unsubscribe_DeviceMethod, /*pfIoTHubTransport_Unsubscribe_DeviceMethod IoTHubTransport_Unsubscribe_DeviceMethod;*/ |
AzureIoTClient | 14:4dc2b011be33 | 142 | IoTHubTransportMqtt_DeviceMethod_Response, /*pfIoTHubTransport_DeviceMethod_Response IoTHubTransport_DeviceMethod_Response;*/ |
AzureIoTClient | 12:658ca6865de2 | 143 | IoTHubTransportMqtt_Subscribe_DeviceTwin, /*pfIoTHubTransport_Subscribe_DeviceTwin IoTHubTransport_Subscribe_DeviceTwin;*/ |
AzureIoTClient | 12:658ca6865de2 | 144 | IoTHubTransportMqtt_Unsubscribe_DeviceTwin, /*pfIoTHubTransport_Unsubscribe_DeviceTwin IoTHubTransport_Unsubscribe_DeviceTwin;*/ |
AzureIoTClient | 12:658ca6865de2 | 145 | IoTHubTransportMqtt_ProcessItem, /*pfIoTHubTransport_ProcessItem IoTHubTransport_ProcessItem;*/ |
AzureIoTClient | 12:658ca6865de2 | 146 | IoTHubTransportMqtt_GetHostname, /*pfIoTHubTransport_GetHostname IoTHubTransport_GetHostname;*/ |
AzureIoTClient | 12:658ca6865de2 | 147 | IoTHubTransportMqtt_SetOption, /*pfIoTHubTransport_SetOption IoTHubTransport_SetOption;*/ |
AzureIoTClient | 12:658ca6865de2 | 148 | IoTHubTransportMqtt_Create, /*pfIoTHubTransport_Create IoTHubTransport_Create;*/ |
AzureIoTClient | 12:658ca6865de2 | 149 | IoTHubTransportMqtt_Destroy, /*pfIoTHubTransport_Destroy IoTHubTransport_Destroy;*/ |
AzureIoTClient | 12:658ca6865de2 | 150 | IoTHubTransportMqtt_Register, /*pfIotHubTransport_Register IoTHubTransport_Register;*/ |
AzureIoTClient | 12:658ca6865de2 | 151 | IoTHubTransportMqtt_Unregister, /*pfIotHubTransport_Unregister IoTHubTransport_Unegister;*/ |
AzureIoTClient | 12:658ca6865de2 | 152 | IoTHubTransportMqtt_Subscribe, /*pfIoTHubTransport_Subscribe IoTHubTransport_Subscribe;*/ |
AzureIoTClient | 12:658ca6865de2 | 153 | IoTHubTransportMqtt_Unsubscribe, /*pfIoTHubTransport_Unsubscribe IoTHubTransport_Unsubscribe;*/ |
AzureIoTClient | 12:658ca6865de2 | 154 | IoTHubTransportMqtt_DoWork, /*pfIoTHubTransport_DoWork IoTHubTransport_DoWork;*/ |
AzureIoTClient | 12:658ca6865de2 | 155 | IoTHubTransportMqtt_SetRetryPolicy, /*pfIoTHubTransport_DoWork IoTHubTransport_SetRetryPolicy;*/ |
AzureIoTClient | 12:658ca6865de2 | 156 | IoTHubTransportMqtt_GetSendStatus /*pfIoTHubTransport_GetSendStatus IoTHubTransport_GetSendStatus;*/ |
Azure.IoT Build | 0:5e72a75c31b8 | 157 | }; |
Azure.IoT Build | 0:5e72a75c31b8 | 158 | |
AzureIoTClient | 12:658ca6865de2 | 159 | /* Codes_SRS_IOTHUB_MQTT_TRANSPORT_07_022: [This function shall return a pointer to a structure of type TRANSPORT_PROVIDER */ |
AzureIoTClient | 12:658ca6865de2 | 160 | extern const TRANSPORT_PROVIDER* MQTT_Protocol(void) |
Azure.IoT Build | 0:5e72a75c31b8 | 161 | { |
AzureIoTClient | 4:e472f5ce3473 | 162 | return &myfunc; |
Azure.IoT Build | 0:5e72a75c31b8 | 163 | } |