[457] | 1 | // Copyright (c) Microsoft. All rights reserved.
|
---|
| 2 | // Licensed under the MIT license. See LICENSE file in the project root for full license information.
|
---|
| 3 |
|
---|
| 4 | #include <stdlib.h>
|
---|
| 5 | #include "iothubtransportmqtt.h"
|
---|
| 6 | #include "azure_c_shared_utility/xio.h"
|
---|
| 7 | #include "azure_c_shared_utility/tlsio.h"
|
---|
| 8 | #include "azure_c_shared_utility/platform.h"
|
---|
| 9 | #include "internal/iothubtransport_mqtt_common.h"
|
---|
| 10 | #include "azure_c_shared_utility/xlogging.h"
|
---|
| 11 |
|
---|
| 12 | static XIO_HANDLE getIoTransportProvider(const char* fully_qualified_name, const MQTT_TRANSPORT_PROXY_OPTIONS* mqtt_transport_proxy_options)
|
---|
| 13 | {
|
---|
| 14 | XIO_HANDLE result;
|
---|
| 15 |
|
---|
| 16 | /* Codes_SRS_IOTHUB_MQTT_TRANSPORT_01_001: [ `getIoTransportProvider` shall obtain the TLS IO interface handle by calling `platform_get_default_tlsio`. ]*/
|
---|
| 17 | const IO_INTERFACE_DESCRIPTION* io_interface_description = platform_get_default_tlsio();
|
---|
| 18 | (void)mqtt_transport_proxy_options;
|
---|
| 19 |
|
---|
| 20 | if (io_interface_description == NULL)
|
---|
| 21 | {
|
---|
| 22 | /* Codes_SRS_IOTHUB_MQTT_TRANSPORT_07_013: [ If `platform_get_default_tlsio` returns NULL, `getIoTransportProvider` shall return NULL. ] */
|
---|
| 23 | LogError("Failure constructing the provider interface");
|
---|
| 24 | result = NULL;
|
---|
| 25 | }
|
---|
| 26 | else
|
---|
| 27 | {
|
---|
| 28 | /* Codes_SRS_IOTHUB_MQTT_TRANSPORT_01_002: [ The TLS IO parameters shall be a `TLSIO_CONFIG` structure filled as below: ]*/
|
---|
| 29 | TLSIO_CONFIG tls_io_config;
|
---|
| 30 |
|
---|
| 31 | /* Codes_SRS_IOTHUB_MQTT_TRANSPORT_01_003: [ - `hostname` shall be set to `fully_qualified_name`. ]*/
|
---|
| 32 | tls_io_config.hostname = fully_qualified_name;
|
---|
| 33 | /* Codes_SRS_IOTHUB_MQTT_TRANSPORT_01_004: [ - `port` shall be set to 8883. ]*/
|
---|
| 34 | tls_io_config.port = 8883;
|
---|
| 35 | /* Codes_SRS_IOTHUB_MQTT_TRANSPORT_01_005: [ - `underlying_io_interface` shall be set to NULL. ]*/
|
---|
| 36 | tls_io_config.underlying_io_interface = NULL;
|
---|
| 37 | /* Codes_SRS_IOTHUB_MQTT_TRANSPORT_01_006: [ - `underlying_io_parameters` shall be set to NULL. ]*/
|
---|
| 38 | tls_io_config.underlying_io_parameters = NULL;
|
---|
| 39 |
|
---|
| 40 | /* Codes_SRS_IOTHUB_MQTT_TRANSPORT_07_012: [ `getIoTransportProvider` shall return the `XIO_HANDLE` returned by `xio_create`. ] */
|
---|
| 41 | result = xio_create(io_interface_description, &tls_io_config);
|
---|
| 42 | }
|
---|
| 43 | return result;
|
---|
| 44 | }
|
---|
| 45 |
|
---|
| 46 | static TRANSPORT_LL_HANDLE IoTHubTransportMqtt_Create(const IOTHUBTRANSPORT_CONFIG* config, TRANSPORT_CALLBACKS_INFO* cb_info, void* ctx)
|
---|
| 47 | {
|
---|
| 48 | /* Codes_SRS_IOTHUB_MQTT_TRANSPORT_07_001: [IoTHubTransportMqtt_Create shall create a TRANSPORT_LL_HANDLE by calling into the IoTHubMqttAbstract_Create function.] */
|
---|
| 49 | return IoTHubTransport_MQTT_Common_Create(config, getIoTransportProvider, cb_info, ctx);
|
---|
| 50 | }
|
---|
| 51 |
|
---|
| 52 | static void IoTHubTransportMqtt_Destroy(TRANSPORT_LL_HANDLE handle)
|
---|
| 53 | {
|
---|
| 54 | /* Codes_SRS_IOTHUB_MQTT_TRANSPORT_07_002: [ IoTHubTransportMqtt_Destroy shall destroy the TRANSPORT_LL_HANDLE by calling into the IoTHubMqttAbstract_Destroy function. ] */
|
---|
| 55 | IoTHubTransport_MQTT_Common_Destroy(handle);
|
---|
| 56 | }
|
---|
| 57 |
|
---|
| 58 | static int IoTHubTransportMqtt_Subscribe(IOTHUB_DEVICE_HANDLE handle)
|
---|
| 59 | {
|
---|
| 60 | /* Codes_SRS_IOTHUB_MQTT_TRANSPORT_07_005: [ IoTHubTransportMqtt_Subscribe shall subscribe the TRANSPORT_LL_HANDLE by calling into the IoTHubMqttAbstract_Subscribe function. ] */
|
---|
| 61 | return IoTHubTransport_MQTT_Common_Subscribe(handle);
|
---|
| 62 | }
|
---|
| 63 |
|
---|
| 64 | static void IoTHubTransportMqtt_Unsubscribe(IOTHUB_DEVICE_HANDLE handle)
|
---|
| 65 | {
|
---|
| 66 | /* Codes_SRS_IOTHUB_MQTT_TRANSPORT_07_006: [ IoTHubTransportMqtt_Unsubscribe shall unsubscribe the TRANSPORT_LL_HANDLE by calling into the IoTHubMqttAbstract_Unsubscribe function. ] */
|
---|
| 67 | IoTHubTransport_MQTT_Common_Unsubscribe(handle);
|
---|
| 68 | }
|
---|
| 69 |
|
---|
| 70 | /* Codes_SRS_IOTHUB_MQTT_TRANSPORT_07_026: [ IoTHubTransportMqtt_Subscribe_DeviceMethod shall subscribe the TRANSPORT_LL_HANDLE by calling into the IoTHubMqttAbstract_Subscribe_DeviceMethod function. ] */
|
---|
| 71 | static int IoTHubTransportMqtt_Subscribe_DeviceMethod(IOTHUB_DEVICE_HANDLE handle)
|
---|
| 72 | {
|
---|
| 73 | return IoTHubTransport_MQTT_Common_Subscribe_DeviceMethod(handle);
|
---|
| 74 | }
|
---|
| 75 |
|
---|
| 76 | /* Codes_SRS_IOTHUB_MQTT_TRANSPORT_07_027: [ IoTHubTransportMqtt_Unsubscribe_DeviceMethod shall call into the IoTHubMqttAbstract_Unsubscribe_DeviceMethod function. ] */
|
---|
| 77 | static void IoTHubTransportMqtt_Unsubscribe_DeviceMethod(IOTHUB_DEVICE_HANDLE handle)
|
---|
| 78 | {
|
---|
| 79 | IoTHubTransport_MQTT_Common_Unsubscribe_DeviceMethod(handle);
|
---|
| 80 | }
|
---|
| 81 |
|
---|
| 82 | /* Codes_SRS_IOTHUB_MQTT_TRANSPORT_07_025: [ IoTHubTransportMqtt_Subscribe_DeviceTwin shall call into the IoTHubMqttAbstract_Subscribe_DeviceTwin function. ] */
|
---|
| 83 | static int IoTHubTransportMqtt_Subscribe_DeviceTwin(IOTHUB_DEVICE_HANDLE handle)
|
---|
| 84 | {
|
---|
| 85 | return IoTHubTransport_MQTT_Common_Subscribe_DeviceTwin(handle);
|
---|
| 86 | }
|
---|
| 87 |
|
---|
| 88 | /* Codes_SRS_IOTHUB_MQTT_TRANSPORT_07_024: [ IoTHubTransportMqtt_Unsubscribe_DeviceTwin shall shall call into the IoTHubMqttAbstract_Unsubscribe_DeviceTwin function. ] */
|
---|
| 89 | static void IoTHubTransportMqtt_Unsubscribe_DeviceTwin(IOTHUB_DEVICE_HANDLE handle)
|
---|
| 90 | {
|
---|
| 91 | IoTHubTransport_MQTT_Common_Unsubscribe_DeviceTwin(handle);
|
---|
| 92 | }
|
---|
| 93 |
|
---|
| 94 | static IOTHUB_CLIENT_RESULT IoTHubTransportMqtt_GetTwinAsync(IOTHUB_DEVICE_HANDLE handle, IOTHUB_CLIENT_DEVICE_TWIN_CALLBACK completionCallback, void* callbackContext)
|
---|
| 95 | {
|
---|
| 96 | // Codes_SRS_IOTHUB_MQTT_TRANSPORT_09_001: [ IoTHubTransportMqtt_GetTwinAsync shall call into the IoTHubTransport_MQTT_Common_GetTwinAsync function. ]
|
---|
| 97 | return IoTHubTransport_MQTT_Common_GetTwinAsync(handle, completionCallback, callbackContext);
|
---|
| 98 | }
|
---|
| 99 |
|
---|
| 100 | /* Codes_SRS_IOTHUB_MQTT_TRANSPORT_07_023: [ IoTHubTransportMqtt_DeviceMethod_Response shall call into the IoTHubMqttAbstract_DeviceMethod_Response function. ] */
|
---|
| 101 | static int IoTHubTransportMqtt_DeviceMethod_Response(IOTHUB_DEVICE_HANDLE handle, METHOD_HANDLE methodId, const unsigned char* response, size_t response_size, int status_response)
|
---|
| 102 | {
|
---|
| 103 | return IoTHubTransport_MQTT_Common_DeviceMethod_Response(handle, methodId, response, response_size, status_response);
|
---|
| 104 | }
|
---|
| 105 |
|
---|
| 106 | /* Codes_SRS_IOTHUB_MQTT_TRANSPORT_10_001: [IoTHubTransportMqtt_SendMessageDisposition shall send the message disposition by calling into the IoTHubMqttAbstract_SendMessageDisposition function.] */
|
---|
| 107 | static IOTHUB_CLIENT_RESULT IoTHubTransportMqtt_SendMessageDisposition(MESSAGE_CALLBACK_INFO* message_data, IOTHUBMESSAGE_DISPOSITION_RESULT disposition)
|
---|
| 108 | {
|
---|
| 109 | return IoTHubTransport_MQTT_Common_SendMessageDisposition(message_data, disposition);
|
---|
| 110 | }
|
---|
| 111 |
|
---|
| 112 | static IOTHUB_PROCESS_ITEM_RESULT IoTHubTransportMqtt_ProcessItem(TRANSPORT_LL_HANDLE handle, IOTHUB_IDENTITY_TYPE item_type, IOTHUB_IDENTITY_INFO* iothub_item)
|
---|
| 113 | {
|
---|
| 114 | return IoTHubTransport_MQTT_Common_ProcessItem(handle, item_type, iothub_item);
|
---|
| 115 | }
|
---|
| 116 |
|
---|
| 117 | /* Codes_SRS_IOTHUB_MQTT_TRANSPORT_07_054: [ IoTHubTransportMqtt_DoWork shall subscribe to the Notification and get_state Topics if they are defined. ] */
|
---|
| 118 | static void IoTHubTransportMqtt_DoWork(TRANSPORT_LL_HANDLE handle)
|
---|
| 119 | {
|
---|
| 120 | /* Codes_SRS_IOTHUB_MQTT_TRANSPORT_07_007: [ IoTHubTransportMqtt_DoWork shall call into the IoTHubMqttAbstract_DoWork function. ] */
|
---|
| 121 | IoTHubTransport_MQTT_Common_DoWork(handle);
|
---|
| 122 | }
|
---|
| 123 |
|
---|
| 124 | static int IoTHubTransportMqtt_SetRetryPolicy(TRANSPORT_LL_HANDLE handle, IOTHUB_CLIENT_RETRY_POLICY retryPolicy, size_t retryTimeoutLimitInSeconds)
|
---|
| 125 | {
|
---|
| 126 | /* Codes_SRS_IOTHUB_MQTT_TRANSPORT_25_012: [** IoTHubTransportMqtt_SetRetryPolicy shall call into the IoTHubMqttAbstract_SetRetryPolicy function. ] */
|
---|
| 127 | return IoTHubTransport_MQTT_Common_SetRetryPolicy(handle, retryPolicy, retryTimeoutLimitInSeconds);
|
---|
| 128 | }
|
---|
| 129 |
|
---|
| 130 | static IOTHUB_CLIENT_RESULT IoTHubTransportMqtt_GetSendStatus(IOTHUB_DEVICE_HANDLE handle, IOTHUB_CLIENT_STATUS *iotHubClientStatus)
|
---|
| 131 | {
|
---|
| 132 | /* Codes_SRS_IOTHUB_MQTT_TRANSPORT_07_008: [ IoTHubTransportMqtt_GetSendStatus shall get the send status by calling into the IoTHubMqttAbstract_GetSendStatus function. ] */
|
---|
| 133 | return IoTHubTransport_MQTT_Common_GetSendStatus(handle, iotHubClientStatus);
|
---|
| 134 | }
|
---|
| 135 |
|
---|
| 136 | static IOTHUB_CLIENT_RESULT IoTHubTransportMqtt_SetOption(TRANSPORT_LL_HANDLE handle, const char* option, const void* value)
|
---|
| 137 | {
|
---|
| 138 | /* Codes_SRS_IOTHUB_MQTT_TRANSPORT_07_009: [ IoTHubTransportMqtt_SetOption shall set the options by calling into the IoTHubMqttAbstract_SetOption function. ] */
|
---|
| 139 | return IoTHubTransport_MQTT_Common_SetOption(handle, option, value);
|
---|
| 140 | }
|
---|
| 141 |
|
---|
| 142 | static IOTHUB_DEVICE_HANDLE IoTHubTransportMqtt_Register(TRANSPORT_LL_HANDLE handle, const IOTHUB_DEVICE_CONFIG* device, PDLIST_ENTRY waitingToSend)
|
---|
| 143 | {
|
---|
| 144 | /* Codes_SRS_IOTHUB_MQTT_TRANSPORT_07_003: [ IoTHubTransportMqtt_Register shall register the TRANSPORT_LL_HANDLE by calling into the IoTHubMqttAbstract_Register function. ] */
|
---|
| 145 | return IoTHubTransport_MQTT_Common_Register(handle, device, waitingToSend);
|
---|
| 146 | }
|
---|
| 147 |
|
---|
| 148 | static void IoTHubTransportMqtt_Unregister(IOTHUB_DEVICE_HANDLE deviceHandle)
|
---|
| 149 | {
|
---|
| 150 | /* Codes_SRS_IOTHUB_MQTT_TRANSPORT_07_004: [ IoTHubTransportMqtt_Unregister shall register the TRANSPORT_LL_HANDLE by calling into the IoTHubMqttAbstract_Unregister function. ] */
|
---|
| 151 | IoTHubTransport_MQTT_Common_Unregister(deviceHandle);
|
---|
| 152 | }
|
---|
| 153 |
|
---|
| 154 | static STRING_HANDLE IoTHubTransportMqtt_GetHostname(TRANSPORT_LL_HANDLE handle)
|
---|
| 155 | {
|
---|
| 156 | /* Codes_SRS_IOTHUB_MQTT_TRANSPORT_07_010: [ IoTHubTransportMqtt_GetHostname shall get the hostname by calling into the IoTHubMqttAbstract_GetHostname function. ] */
|
---|
| 157 | return IoTHubTransport_MQTT_Common_GetHostname(handle);
|
---|
| 158 | }
|
---|
| 159 |
|
---|
| 160 | static int IotHubTransportMqtt_Subscribe_InputQueue(IOTHUB_DEVICE_HANDLE handle)
|
---|
| 161 | {
|
---|
| 162 | /* Codes_SRS_IOTHUB_MQTT_TRANSPORT_31_14: [ IoTHubTransportMqtt_Subscribe shall subscribe the TRANSPORT_LL_HANDLE by calling into IoTHubTransport_MQTT_Common_Subscribe_InputQueue. ] */
|
---|
| 163 | return IoTHubTransport_MQTT_Common_Subscribe_InputQueue(handle);
|
---|
| 164 | }
|
---|
| 165 |
|
---|
| 166 | static void IotHubTransportMqtt_Unsubscribe_InputQueue(IOTHUB_DEVICE_HANDLE handle)
|
---|
| 167 | {
|
---|
| 168 | /* Codes_SRS_IOTHUBTRANSPORTAMQP_31_015: [IotHubTransportMQTT_Unsubscribe_InputQueue shall unsubscribe by calling into IoTHubTransport_MQTT_Common_Unsubscribe_InputQueue. ] */
|
---|
| 169 | IoTHubTransport_MQTT_Common_Unsubscribe_InputQueue(handle);
|
---|
| 170 | }
|
---|
| 171 |
|
---|
| 172 | static int IotHubTransportMqtt_SetCallbackContext(TRANSPORT_LL_HANDLE handle, void* ctx)
|
---|
| 173 | {
|
---|
| 174 | return IoTHubTransport_MQTT_SetCallbackContext(handle, ctx);
|
---|
| 175 | }
|
---|
| 176 |
|
---|
| 177 | static int IotHubTransportMqtt_GetSupportedPlatformInfo(TRANSPORT_LL_HANDLE handle, PLATFORM_INFO_OPTION* info)
|
---|
| 178 | {
|
---|
| 179 | return IoTHubTransport_MQTT_GetSupportedPlatformInfo(handle, info);
|
---|
| 180 | }
|
---|
| 181 |
|
---|
| 182 | static TRANSPORT_PROVIDER myfunc =
|
---|
| 183 | {
|
---|
| 184 | IoTHubTransportMqtt_SendMessageDisposition, /*pfIotHubTransport_SendMessageDisposition IoTHubTransport_SendMessageDisposition;*/
|
---|
| 185 | IoTHubTransportMqtt_Subscribe_DeviceMethod, /*pfIoTHubTransport_Subscribe_DeviceMethod IoTHubTransport_Subscribe_DeviceMethod;*/
|
---|
| 186 | IoTHubTransportMqtt_Unsubscribe_DeviceMethod, /*pfIoTHubTransport_Unsubscribe_DeviceMethod IoTHubTransport_Unsubscribe_DeviceMethod;*/
|
---|
| 187 | IoTHubTransportMqtt_DeviceMethod_Response, /*pfIoTHubTransport_DeviceMethod_Response IoTHubTransport_DeviceMethod_Response;*/
|
---|
| 188 | IoTHubTransportMqtt_Subscribe_DeviceTwin, /*pfIoTHubTransport_Subscribe_DeviceTwin IoTHubTransport_Subscribe_DeviceTwin;*/
|
---|
| 189 | IoTHubTransportMqtt_Unsubscribe_DeviceTwin, /*pfIoTHubTransport_Unsubscribe_DeviceTwin IoTHubTransport_Unsubscribe_DeviceTwin;*/
|
---|
| 190 | IoTHubTransportMqtt_ProcessItem, /*pfIoTHubTransport_ProcessItem IoTHubTransport_ProcessItem;*/
|
---|
| 191 | IoTHubTransportMqtt_GetHostname, /*pfIoTHubTransport_GetHostname IoTHubTransport_GetHostname;*/
|
---|
| 192 | IoTHubTransportMqtt_SetOption, /*pfIoTHubTransport_SetOption IoTHubTransport_SetOption;*/
|
---|
| 193 | IoTHubTransportMqtt_Create, /*pfIoTHubTransport_Create IoTHubTransport_Create;*/
|
---|
| 194 | IoTHubTransportMqtt_Destroy, /*pfIoTHubTransport_Destroy IoTHubTransport_Destroy;*/
|
---|
| 195 | IoTHubTransportMqtt_Register, /*pfIotHubTransport_Register IoTHubTransport_Register;*/
|
---|
| 196 | IoTHubTransportMqtt_Unregister, /*pfIotHubTransport_Unregister IoTHubTransport_Unegister;*/
|
---|
| 197 | IoTHubTransportMqtt_Subscribe, /*pfIoTHubTransport_Subscribe IoTHubTransport_Subscribe;*/
|
---|
| 198 | IoTHubTransportMqtt_Unsubscribe, /*pfIoTHubTransport_Unsubscribe IoTHubTransport_Unsubscribe;*/
|
---|
| 199 | IoTHubTransportMqtt_DoWork, /*pfIoTHubTransport_DoWork IoTHubTransport_DoWork;*/
|
---|
| 200 | IoTHubTransportMqtt_SetRetryPolicy, /*pfIoTHubTransport_DoWork IoTHubTransport_SetRetryPolicy;*/
|
---|
| 201 | IoTHubTransportMqtt_GetSendStatus, /*pfIoTHubTransport_GetSendStatus IoTHubTransport_GetSendStatus;*/
|
---|
| 202 | IotHubTransportMqtt_Subscribe_InputQueue, /*pfIoTHubTransport_Subscribe_InputQueue IoTHubTransport_Subscribe_InputQueue; */
|
---|
| 203 | IotHubTransportMqtt_Unsubscribe_InputQueue, /*pfIoTHubTransport_Unsubscribe_InputQueue IoTHubTransport_Unsubscribe_InputQueue; */
|
---|
| 204 | IotHubTransportMqtt_SetCallbackContext, /*pfIoTHubTransport_SetCallbackContext IoTHubTransport_SetCallbackContext; */
|
---|
| 205 | IoTHubTransportMqtt_GetTwinAsync, /*pfIoTHubTransport_GetTwinAsync IoTHubTransport_GetTwinAsync;*/
|
---|
| 206 | IotHubTransportMqtt_GetSupportedPlatformInfo /*pfIoTHubTransport_GetSupportedPlatformInfo IoTHubTransport_GetSupportedPlatformInfo;*/
|
---|
| 207 | };
|
---|
| 208 |
|
---|
| 209 | /* Codes_SRS_IOTHUB_MQTT_TRANSPORT_07_022: [This function shall return a pointer to a structure of type TRANSPORT_PROVIDER */
|
---|
| 210 | extern const TRANSPORT_PROVIDER* MQTT_Protocol(void)
|
---|
| 211 | {
|
---|
| 212 | return &myfunc;
|
---|
| 213 | }
|
---|