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:
Fri Aug 25 11:22:29 2017 -0700
Revision:
27:04de3c0bf1db
Parent:
26:0dae84ecce27
Child:
28:0cd355c3294e
1.1.22

Who changed what in which revision?

UserRevisionLine numberNew contents of line
AzureIoTClient 11:31ebaeb51e1e 1 // Copyright (c) Microsoft. All rights reserved.
AzureIoTClient 11:31ebaeb51e1e 2 // Licensed under the MIT license. See LICENSE file in the project root for full license information.
AzureIoTClient 11:31ebaeb51e1e 3
AzureIoTClient 11:31ebaeb51e1e 4 #include <stdlib.h>
AzureIoTClient 12:658ca6865de2 5 #include <ctype.h>
AzureIoTClient 18:ec8e5e97c6a4 6 #include "azure_c_shared_utility/optimize_size.h"
AzureIoTClient 11:31ebaeb51e1e 7 #include "azure_c_shared_utility/gballoc.h"
AzureIoTClient 11:31ebaeb51e1e 8
AzureIoTClient 11:31ebaeb51e1e 9 #include "azure_c_shared_utility/xlogging.h"
AzureIoTClient 11:31ebaeb51e1e 10 #include "azure_c_shared_utility/strings.h"
AzureIoTClient 11:31ebaeb51e1e 11 #include "azure_c_shared_utility/doublylinkedlist.h"
AzureIoTClient 11:31ebaeb51e1e 12 #include "azure_c_shared_utility/crt_abstractions.h"
AzureIoTClient 11:31ebaeb51e1e 13
AzureIoTClient 11:31ebaeb51e1e 14 #include "iothub_client_ll.h"
AzureIoTClient 11:31ebaeb51e1e 15 #include "iothub_client_options.h"
AzureIoTClient 11:31ebaeb51e1e 16 #include "iothub_client_private.h"
AzureIoTClient 11:31ebaeb51e1e 17 #include "azure_umqtt_c/mqtt_client.h"
AzureIoTClient 11:31ebaeb51e1e 18 #include "azure_c_shared_utility/sastoken.h"
AzureIoTClient 11:31ebaeb51e1e 19 #include "azure_c_shared_utility/tickcounter.h"
AzureIoTClient 11:31ebaeb51e1e 20
AzureIoTClient 11:31ebaeb51e1e 21 #include "azure_c_shared_utility/tlsio.h"
AzureIoTClient 11:31ebaeb51e1e 22 #include "azure_c_shared_utility/platform.h"
AzureIoTClient 11:31ebaeb51e1e 23
AzureIoTClient 11:31ebaeb51e1e 24 #include "azure_c_shared_utility/string_tokenizer.h"
AzureIoTClient 20:594780be216d 25 #include "azure_c_shared_utility/shared_util_options.h"
AzureIoTClient 22:07fec4d325b6 26 #include "azure_c_shared_utility/urlencode.h"
AzureIoTClient 11:31ebaeb51e1e 27 #include "iothub_client_version.h"
AzureIoTClient 25:a35763780a87 28 #include "iothub_client_retry_control.h"
AzureIoTClient 11:31ebaeb51e1e 29
AzureIoTClient 11:31ebaeb51e1e 30 #include "iothubtransport_mqtt_common.h"
AzureIoTClient 11:31ebaeb51e1e 31
AzureIoTClient 11:31ebaeb51e1e 32 #include <stdarg.h>
AzureIoTClient 11:31ebaeb51e1e 33 #include <stdio.h>
AzureIoTClient 11:31ebaeb51e1e 34
AzureIoTClient 11:31ebaeb51e1e 35 #include <limits.h>
AzureIoTClient 12:658ca6865de2 36 #include <inttypes.h>
AzureIoTClient 11:31ebaeb51e1e 37
AzureIoTClient 25:a35763780a87 38 #define SAS_TOKEN_DEFAULT_LIFETIME 3600
AzureIoTClient 25:a35763780a87 39 #define SAS_REFRESH_MULTIPLIER .8
AzureIoTClient 25:a35763780a87 40 #define EPOCH_TIME_T_VALUE 0
AzureIoTClient 25:a35763780a87 41 #define DEFAULT_MQTT_KEEPALIVE 4*60 // 4 min
AzureIoTClient 25:a35763780a87 42 #define BUILD_CONFIG_USERNAME 24
AzureIoTClient 25:a35763780a87 43 #define SAS_TOKEN_DEFAULT_LEN 10
AzureIoTClient 25:a35763780a87 44 #define RESEND_TIMEOUT_VALUE_MIN 1*60
AzureIoTClient 25:a35763780a87 45 #define MAX_SEND_RECOUNT_LIMIT 2
AzureIoTClient 25:a35763780a87 46 #define DEFAULT_CONNECTION_INTERVAL 30
AzureIoTClient 25:a35763780a87 47 #define FAILED_CONN_BACKOFF_VALUE 5
AzureIoTClient 25:a35763780a87 48 #define STATUS_CODE_FAILURE_VALUE 500
AzureIoTClient 25:a35763780a87 49 #define STATUS_CODE_TIMEOUT_VALUE 408
AzureIoTClient 25:a35763780a87 50
AzureIoTClient 25:a35763780a87 51 #define DEFAULT_RETRY_POLICY IOTHUB_CLIENT_RETRY_EXPONENTIAL_BACKOFF_WITH_JITTER
AzureIoTClient 25:a35763780a87 52 #define DEFAULT_RETRY_TIMEOUT_IN_SECONDS 0
AzureIoTClient 12:658ca6865de2 53
AzureIoTClient 12:658ca6865de2 54 static const char TOPIC_DEVICE_TWIN_PREFIX[] = "$iothub/twin";
AzureIoTClient 12:658ca6865de2 55 static const char TOPIC_DEVICE_METHOD_PREFIX[] = "$iothub/methods";
AzureIoTClient 12:658ca6865de2 56
AzureIoTClient 12:658ca6865de2 57 static const char* TOPIC_GET_DESIRED_STATE = "$iothub/twin/res/#";
AzureIoTClient 12:658ca6865de2 58 static const char* TOPIC_NOTIFICATION_STATE = "$iothub/twin/PATCH/properties/desired/#";
AzureIoTClient 11:31ebaeb51e1e 59
AzureIoTClient 11:31ebaeb51e1e 60 static const char* TOPIC_DEVICE_MSG = "devices/%s/messages/devicebound/#";
AzureIoTClient 11:31ebaeb51e1e 61 static const char* TOPIC_DEVICE_DEVICE = "devices/%s/messages/events/";
AzureIoTClient 12:658ca6865de2 62
AzureIoTClient 12:658ca6865de2 63 static const char* TOPIC_DEVICE_METHOD_SUBSCRIBE = "$iothub/methods/POST/#";
AzureIoTClient 12:658ca6865de2 64 static const char* TOPIC_DEVICE_METHOD_RESPONSE = "$iothub/methods/res";
AzureIoTClient 12:658ca6865de2 65
AzureIoTClient 12:658ca6865de2 66 static const char* IOTHUB_API_VERSION = "2016-11-14";
AzureIoTClient 12:658ca6865de2 67
AzureIoTClient 11:31ebaeb51e1e 68 static const char* PROPERTY_SEPARATOR = "&";
AzureIoTClient 12:658ca6865de2 69 static const char* REPORTED_PROPERTIES_TOPIC = "$iothub/twin/PATCH/properties/reported/?$rid=%"PRIu16;
AzureIoTClient 12:658ca6865de2 70 static const char* GET_PROPERTIES_TOPIC = "$iothub/twin/GET/?$rid=%"PRIu16;
AzureIoTClient 14:4dc2b011be33 71 static const char* DEVICE_METHOD_RESPONSE_TOPIC = "$iothub/methods/res/%d/?$rid=%s";
AzureIoTClient 12:658ca6865de2 72
AzureIoTClient 12:658ca6865de2 73 static const char* REQUEST_ID_PROPERTY = "?$rid=";
AzureIoTClient 11:31ebaeb51e1e 74
AzureIoTClient 18:ec8e5e97c6a4 75 static const char* MESSAGE_ID_PROPERTY = "mid";
AzureIoTClient 18:ec8e5e97c6a4 76 static const char* CORRELATION_ID_PROPERTY = "cid";
AzureIoTClient 27:04de3c0bf1db 77 static const char* CONTENT_TYPE_PROPERTY = "ct";
AzureIoTClient 27:04de3c0bf1db 78 static const char* CONTENT_ENCODING_PROPERTY = "ce";
AzureIoTClient 18:ec8e5e97c6a4 79
AzureIoTClient 11:31ebaeb51e1e 80 #define UNSUBSCRIBE_FROM_TOPIC 0x0000
AzureIoTClient 12:658ca6865de2 81 #define SUBSCRIBE_GET_REPORTED_STATE_TOPIC 0x0001
AzureIoTClient 12:658ca6865de2 82 #define SUBSCRIBE_NOTIFICATION_STATE_TOPIC 0x0002
AzureIoTClient 11:31ebaeb51e1e 83 #define SUBSCRIBE_TELEMETRY_TOPIC 0x0004
AzureIoTClient 12:658ca6865de2 84 #define SUBSCRIBE_METHODS_TOPIC 0x0008
AzureIoTClient 12:658ca6865de2 85 #define SUBSCRIBE_DEVICE_METHOD_TOPIC 0x0010
AzureIoTClient 12:658ca6865de2 86 #define SUBSCRIBE_TOPIC_COUNT 4
AzureIoTClient 11:31ebaeb51e1e 87
AzureIoTClient 17:774695cb8554 88 DEFINE_ENUM_STRINGS(MQTT_CLIENT_EVENT_ERROR, MQTT_CLIENT_EVENT_ERROR_VALUES)
AzureIoTClient 17:774695cb8554 89
AzureIoTClient 11:31ebaeb51e1e 90 typedef struct SYSTEM_PROPERTY_INFO_TAG
AzureIoTClient 11:31ebaeb51e1e 91 {
AzureIoTClient 11:31ebaeb51e1e 92 const char* propName;
AzureIoTClient 11:31ebaeb51e1e 93 size_t propLength;
AzureIoTClient 11:31ebaeb51e1e 94 } SYSTEM_PROPERTY_INFO;
AzureIoTClient 11:31ebaeb51e1e 95
AzureIoTClient 11:31ebaeb51e1e 96 static SYSTEM_PROPERTY_INFO sysPropList[] = {
AzureIoTClient 11:31ebaeb51e1e 97 { "%24.exp", 7 },
AzureIoTClient 11:31ebaeb51e1e 98 { "%24.mid", 7 },
AzureIoTClient 11:31ebaeb51e1e 99 { "%24.uid", 7 },
AzureIoTClient 11:31ebaeb51e1e 100 { "%24.to", 6 },
AzureIoTClient 11:31ebaeb51e1e 101 { "%24.cid", 7 },
AzureIoTClient 27:04de3c0bf1db 102 { "%24.ct", 6 },
AzureIoTClient 27:04de3c0bf1db 103 { "%24.ce", 6 },
AzureIoTClient 11:31ebaeb51e1e 104 { "devices/", 8 },
AzureIoTClient 11:31ebaeb51e1e 105 { "iothub-operation", 16 },
AzureIoTClient 11:31ebaeb51e1e 106 { "iothub-ack", 10 }
AzureIoTClient 11:31ebaeb51e1e 107 };
AzureIoTClient 11:31ebaeb51e1e 108
AzureIoTClient 12:658ca6865de2 109 typedef enum DEVICE_TWIN_MSG_TYPE_TAG
AzureIoTClient 12:658ca6865de2 110 {
AzureIoTClient 12:658ca6865de2 111 REPORTED_STATE,
AzureIoTClient 12:658ca6865de2 112 RETRIEVE_PROPERTIES
AzureIoTClient 12:658ca6865de2 113 } DEVICE_TWIN_MSG_TYPE;
AzureIoTClient 12:658ca6865de2 114
AzureIoTClient 11:31ebaeb51e1e 115 typedef enum MQTT_TRANSPORT_CREDENTIAL_TYPE_TAG
AzureIoTClient 11:31ebaeb51e1e 116 {
AzureIoTClient 11:31ebaeb51e1e 117 CREDENTIAL_NOT_BUILD,
AzureIoTClient 11:31ebaeb51e1e 118 X509,
AzureIoTClient 11:31ebaeb51e1e 119 SAS_TOKEN_FROM_USER,
AzureIoTClient 11:31ebaeb51e1e 120 DEVICE_KEY,
AzureIoTClient 11:31ebaeb51e1e 121 } MQTT_TRANSPORT_CREDENTIAL_TYPE;
AzureIoTClient 11:31ebaeb51e1e 122
AzureIoTClient 20:594780be216d 123 typedef enum MQTT_CLIENT_STATUS_TAG
AzureIoTClient 20:594780be216d 124 {
AzureIoTClient 21:167f8fcf6fb1 125 MQTT_CLIENT_STATUS_NOT_CONNECTED,
AzureIoTClient 21:167f8fcf6fb1 126 MQTT_CLIENT_STATUS_CONNECTING,
AzureIoTClient 21:167f8fcf6fb1 127 MQTT_CLIENT_STATUS_CONNECTED
AzureIoTClient 20:594780be216d 128 } MQTT_CLIENT_STATUS;
AzureIoTClient 20:594780be216d 129
AzureIoTClient 11:31ebaeb51e1e 130 typedef struct MQTTTRANSPORT_HANDLE_DATA_TAG
AzureIoTClient 11:31ebaeb51e1e 131 {
AzureIoTClient 11:31ebaeb51e1e 132 // Topic control
AzureIoTClient 11:31ebaeb51e1e 133 STRING_HANDLE topic_MqttEvent;
AzureIoTClient 11:31ebaeb51e1e 134 STRING_HANDLE topic_MqttMessage;
AzureIoTClient 12:658ca6865de2 135 STRING_HANDLE topic_GetState;
AzureIoTClient 12:658ca6865de2 136 STRING_HANDLE topic_NotifyState;
AzureIoTClient 12:658ca6865de2 137
AzureIoTClient 12:658ca6865de2 138 STRING_HANDLE topic_DeviceMethods;
AzureIoTClient 11:31ebaeb51e1e 139
AzureIoTClient 11:31ebaeb51e1e 140 uint32_t topics_ToSubscribe;
AzureIoTClient 12:658ca6865de2 141
AzureIoTClient 11:31ebaeb51e1e 142 // Connection related constants
AzureIoTClient 11:31ebaeb51e1e 143 STRING_HANDLE hostAddress;
AzureIoTClient 11:31ebaeb51e1e 144 STRING_HANDLE device_id;
AzureIoTClient 11:31ebaeb51e1e 145 STRING_HANDLE devicesPath;
AzureIoTClient 11:31ebaeb51e1e 146 int portNum;
AzureIoTClient 11:31ebaeb51e1e 147
AzureIoTClient 11:31ebaeb51e1e 148 MQTT_GET_IO_TRANSPORT get_io_transport;
AzureIoTClient 11:31ebaeb51e1e 149
AzureIoTClient 11:31ebaeb51e1e 150 // The current mqtt iothub implementation requires that the hub name and the domain suffix be passed as the first of a series of segments
AzureIoTClient 11:31ebaeb51e1e 151 // passed through the username portion of the connection frame.
AzureIoTClient 11:31ebaeb51e1e 152 // The second segment will contain the device id. The two segments are delemited by a "/".
AzureIoTClient 11:31ebaeb51e1e 153 // The first segment can be a maximum 256 characters.
AzureIoTClient 11:31ebaeb51e1e 154 // The second segment can be a maximum 128 characters.
AzureIoTClient 11:31ebaeb51e1e 155 // With the / delimeter you have 384 chars (Plus a terminator of 0).
AzureIoTClient 11:31ebaeb51e1e 156 STRING_HANDLE configPassedThroughUsername;
AzureIoTClient 11:31ebaeb51e1e 157
AzureIoTClient 11:31ebaeb51e1e 158 // Upper layer
AzureIoTClient 11:31ebaeb51e1e 159 IOTHUB_CLIENT_LL_HANDLE llClientHandle;
AzureIoTClient 11:31ebaeb51e1e 160
AzureIoTClient 11:31ebaeb51e1e 161 // Protocol
AzureIoTClient 11:31ebaeb51e1e 162 MQTT_CLIENT_HANDLE mqttClient;
AzureIoTClient 11:31ebaeb51e1e 163 XIO_HANDLE xioTransport;
AzureIoTClient 11:31ebaeb51e1e 164
AzureIoTClient 11:31ebaeb51e1e 165 // Session - connection
AzureIoTClient 11:31ebaeb51e1e 166 uint16_t packetId;
AzureIoTClient 11:31ebaeb51e1e 167
AzureIoTClient 11:31ebaeb51e1e 168 // Connection state control
AzureIoTClient 11:31ebaeb51e1e 169 bool isRegistered;
AzureIoTClient 20:594780be216d 170 MQTT_CLIENT_STATUS mqttClientStatus;
AzureIoTClient 11:31ebaeb51e1e 171 bool isDestroyCalled;
AzureIoTClient 12:658ca6865de2 172 bool device_twin_get_sent;
AzureIoTClient 12:658ca6865de2 173 bool isRecoverableError;
AzureIoTClient 11:31ebaeb51e1e 174 uint16_t keepAliveValue;
Azure.IoT.Build 13:606465879c57 175 tickcounter_ms_t mqtt_connect_time;
AzureIoTClient 11:31ebaeb51e1e 176 size_t connectFailCount;
Azure.IoT.Build 13:606465879c57 177 tickcounter_ms_t connectTick;
AzureIoTClient 11:31ebaeb51e1e 178 bool log_trace;
AzureIoTClient 11:31ebaeb51e1e 179 bool raw_trace;
Azure.IoT.Build 13:606465879c57 180 TICK_COUNTER_HANDLE msgTickCounter;
AzureIoTClient 11:31ebaeb51e1e 181
AzureIoTClient 11:31ebaeb51e1e 182 // Internal lists for message tracking
AzureIoTClient 11:31ebaeb51e1e 183 PDLIST_ENTRY waitingToSend;
AzureIoTClient 12:658ca6865de2 184 DLIST_ENTRY ack_waiting_queue;
AzureIoTClient 12:658ca6865de2 185
AzureIoTClient 11:31ebaeb51e1e 186 // Message tracking
AzureIoTClient 11:31ebaeb51e1e 187 CONTROL_PACKET_TYPE currPacketState;
AzureIoTClient 11:31ebaeb51e1e 188
AzureIoTClient 11:31ebaeb51e1e 189 // Telemetry specific
AzureIoTClient 11:31ebaeb51e1e 190 DLIST_ENTRY telemetry_waitingForAck;
AzureIoTClient 12:658ca6865de2 191
AzureIoTClient 25:a35763780a87 192 // Controls frequency of reconnection logic.
AzureIoTClient 25:a35763780a87 193 RETRY_CONTROL_HANDLE retry_control_handle;
AzureIoTClient 20:594780be216d 194
AzureIoTClient 21:167f8fcf6fb1 195 // Auth module used to generating handle authorization
AzureIoTClient 21:167f8fcf6fb1 196 // with either SAS Token, x509 Certs, and Device SAS Token
AzureIoTClient 21:167f8fcf6fb1 197 IOTHUB_AUTHORIZATION_HANDLE authorization_module;
AzureIoTClient 21:167f8fcf6fb1 198
AzureIoTClient 20:594780be216d 199 char* http_proxy_hostname;
AzureIoTClient 20:594780be216d 200 int http_proxy_port;
AzureIoTClient 20:594780be216d 201 char* http_proxy_username;
AzureIoTClient 20:594780be216d 202 char* http_proxy_password;
AzureIoTClient 24:4096249decf1 203 bool isProductInfoSet;
AzureIoTClient 11:31ebaeb51e1e 204 } MQTTTRANSPORT_HANDLE_DATA, *PMQTTTRANSPORT_HANDLE_DATA;
AzureIoTClient 11:31ebaeb51e1e 205
AzureIoTClient 12:658ca6865de2 206 typedef struct MQTT_DEVICE_TWIN_ITEM_TAG
AzureIoTClient 12:658ca6865de2 207 {
Azure.IoT.Build 13:606465879c57 208 tickcounter_ms_t msgPublishTime;
AzureIoTClient 12:658ca6865de2 209 size_t retryCount;
AzureIoTClient 12:658ca6865de2 210 IOTHUB_IDENTITY_TYPE iothub_type;
AzureIoTClient 12:658ca6865de2 211 uint16_t packet_id;
AzureIoTClient 12:658ca6865de2 212 uint32_t iothub_msg_id;
AzureIoTClient 12:658ca6865de2 213 IOTHUB_DEVICE_TWIN* device_twin_data;
AzureIoTClient 12:658ca6865de2 214 DEVICE_TWIN_MSG_TYPE device_twin_msg_type;
AzureIoTClient 12:658ca6865de2 215 DLIST_ENTRY entry;
AzureIoTClient 12:658ca6865de2 216 } MQTT_DEVICE_TWIN_ITEM;
AzureIoTClient 12:658ca6865de2 217
AzureIoTClient 11:31ebaeb51e1e 218 typedef struct MQTT_MESSAGE_DETAILS_LIST_TAG
AzureIoTClient 11:31ebaeb51e1e 219 {
Azure.IoT.Build 13:606465879c57 220 tickcounter_ms_t msgPublishTime;
AzureIoTClient 11:31ebaeb51e1e 221 size_t retryCount;
AzureIoTClient 11:31ebaeb51e1e 222 IOTHUB_MESSAGE_LIST* iotHubMessageEntry;
AzureIoTClient 11:31ebaeb51e1e 223 void* context;
AzureIoTClient 11:31ebaeb51e1e 224 uint16_t packet_id;
AzureIoTClient 11:31ebaeb51e1e 225 DLIST_ENTRY entry;
AzureIoTClient 11:31ebaeb51e1e 226 } MQTT_MESSAGE_DETAILS_LIST, *PMQTT_MESSAGE_DETAILS_LIST;
AzureIoTClient 11:31ebaeb51e1e 227
AzureIoTClient 14:4dc2b011be33 228 typedef struct DEVICE_METHOD_INFO_TAG
AzureIoTClient 14:4dc2b011be33 229 {
AzureIoTClient 14:4dc2b011be33 230 STRING_HANDLE request_id;
AzureIoTClient 14:4dc2b011be33 231 } DEVICE_METHOD_INFO;
AzureIoTClient 14:4dc2b011be33 232
AzureIoTClient 20:594780be216d 233 static void free_proxy_data(MQTTTRANSPORT_HANDLE_DATA* mqtt_transport_instance)
AzureIoTClient 20:594780be216d 234 {
AzureIoTClient 20:594780be216d 235 if (mqtt_transport_instance->http_proxy_hostname != NULL)
AzureIoTClient 20:594780be216d 236 {
AzureIoTClient 20:594780be216d 237 free(mqtt_transport_instance->http_proxy_hostname);
AzureIoTClient 20:594780be216d 238 mqtt_transport_instance->http_proxy_hostname = NULL;
AzureIoTClient 20:594780be216d 239 }
AzureIoTClient 20:594780be216d 240
AzureIoTClient 20:594780be216d 241 if (mqtt_transport_instance->http_proxy_username != NULL)
AzureIoTClient 20:594780be216d 242 {
AzureIoTClient 20:594780be216d 243 free(mqtt_transport_instance->http_proxy_username);
AzureIoTClient 20:594780be216d 244 mqtt_transport_instance->http_proxy_username = NULL;
AzureIoTClient 20:594780be216d 245 }
AzureIoTClient 20:594780be216d 246
AzureIoTClient 20:594780be216d 247 if (mqtt_transport_instance->http_proxy_password != NULL)
AzureIoTClient 20:594780be216d 248 {
AzureIoTClient 20:594780be216d 249 free(mqtt_transport_instance->http_proxy_password);
AzureIoTClient 20:594780be216d 250 mqtt_transport_instance->http_proxy_password = NULL;
AzureIoTClient 20:594780be216d 251 }
AzureIoTClient 20:594780be216d 252 }
AzureIoTClient 20:594780be216d 253
AzureIoTClient 25:a35763780a87 254 int IoTHubTransport_MQTT_Common_SetRetryPolicy(TRANSPORT_LL_HANDLE handle, IOTHUB_CLIENT_RETRY_POLICY retryPolicy, size_t retryTimeoutLimitInSeconds)
AzureIoTClient 12:658ca6865de2 255 {
AzureIoTClient 12:658ca6865de2 256 int result;
AzureIoTClient 12:658ca6865de2 257
AzureIoTClient 25:a35763780a87 258 if (handle == NULL)
AzureIoTClient 12:658ca6865de2 259 {
AzureIoTClient 12:658ca6865de2 260 /* Codes_SRS_IOTHUB_TRANSPORT_MQTT_COMMON_25_041: [**If any handle is NULL then IoTHubTransport_MQTT_Common_SetRetryPolicy shall return resultant line.] */
AzureIoTClient 12:658ca6865de2 261 LogError("Invalid handle parameter. NULL.");
AzureIoTClient 18:ec8e5e97c6a4 262 result = __FAILURE__;
AzureIoTClient 12:658ca6865de2 263 }
AzureIoTClient 12:658ca6865de2 264 else
AzureIoTClient 12:658ca6865de2 265 {
AzureIoTClient 25:a35763780a87 266 RETRY_CONTROL_HANDLE new_retry_control_handle;
AzureIoTClient 25:a35763780a87 267
AzureIoTClient 25:a35763780a87 268 // Codes_SRS_IOTHUB_TRANSPORT_MQTT_COMMON_09_006: [ IoTHubTransport_MQTT_Common_SetRetryPolicy shall set the retry logic by calling retry_control_create() with retry policy and retryTimeout as parameters]
AzureIoTClient 25:a35763780a87 269 // Codes_SRS_IOTHUB_TRANSPORT_MQTT_COMMON_09_009: [ If retry_control_create() fails then IoTHubTransport_MQTT_Common_SetRetryPolicy shall revert to previous retry policy and return non-zero value ]
AzureIoTClient 25:a35763780a87 270 if ((new_retry_control_handle = retry_control_create(retryPolicy, (unsigned int)retryTimeoutLimitInSeconds)) == NULL)
AzureIoTClient 12:658ca6865de2 271 {
AzureIoTClient 25:a35763780a87 272 LogError("Failed creating new retry control handle");
AzureIoTClient 25:a35763780a87 273 result = __FAILURE__;
AzureIoTClient 12:658ca6865de2 274 }
AzureIoTClient 12:658ca6865de2 275 else
AzureIoTClient 12:658ca6865de2 276 {
AzureIoTClient 25:a35763780a87 277 PMQTTTRANSPORT_HANDLE_DATA transport_data = (PMQTTTRANSPORT_HANDLE_DATA)handle;
AzureIoTClient 25:a35763780a87 278 RETRY_CONTROL_HANDLE previous_retry_control_handle = transport_data->retry_control_handle;
AzureIoTClient 25:a35763780a87 279
AzureIoTClient 25:a35763780a87 280 transport_data->retry_control_handle = new_retry_control_handle;
AzureIoTClient 25:a35763780a87 281 retry_control_destroy(previous_retry_control_handle);
AzureIoTClient 25:a35763780a87 282
AzureIoTClient 25:a35763780a87 283 /*Codes_SRS_IOTHUB_TRANSPORT_MQTT_COMMON_25_045: [**If retry logic for specified parameters of retry policy and retryTimeoutLimitInSeconds is created successfully then IoTHubTransport_MQTT_Common_SetRetryPolicy shall return 0]*/
AzureIoTClient 25:a35763780a87 284 result = 0;
AzureIoTClient 12:658ca6865de2 285 }
AzureIoTClient 12:658ca6865de2 286 }
AzureIoTClient 25:a35763780a87 287
AzureIoTClient 12:658ca6865de2 288 return result;
AzureIoTClient 12:658ca6865de2 289 }
AzureIoTClient 12:658ca6865de2 290
AzureIoTClient 11:31ebaeb51e1e 291 static uint16_t get_next_packet_id(PMQTTTRANSPORT_HANDLE_DATA transport_data)
AzureIoTClient 11:31ebaeb51e1e 292 {
AzureIoTClient 11:31ebaeb51e1e 293 if (transport_data->packetId+1 >= USHRT_MAX)
AzureIoTClient 11:31ebaeb51e1e 294 {
AzureIoTClient 11:31ebaeb51e1e 295 transport_data->packetId = 1;
AzureIoTClient 11:31ebaeb51e1e 296 }
AzureIoTClient 11:31ebaeb51e1e 297 else
AzureIoTClient 11:31ebaeb51e1e 298 {
AzureIoTClient 11:31ebaeb51e1e 299 transport_data->packetId++;
AzureIoTClient 11:31ebaeb51e1e 300 }
AzureIoTClient 11:31ebaeb51e1e 301 return transport_data->packetId;
AzureIoTClient 11:31ebaeb51e1e 302 }
AzureIoTClient 11:31ebaeb51e1e 303
AzureIoTClient 11:31ebaeb51e1e 304 static const char* retrieve_mqtt_return_codes(CONNECT_RETURN_CODE rtn_code)
AzureIoTClient 11:31ebaeb51e1e 305 {
AzureIoTClient 11:31ebaeb51e1e 306 switch (rtn_code)
AzureIoTClient 11:31ebaeb51e1e 307 {
AzureIoTClient 11:31ebaeb51e1e 308 case CONNECTION_ACCEPTED:
AzureIoTClient 11:31ebaeb51e1e 309 return "Accepted";
AzureIoTClient 11:31ebaeb51e1e 310 case CONN_REFUSED_UNACCEPTABLE_VERSION:
AzureIoTClient 11:31ebaeb51e1e 311 return "Unacceptable Version";
AzureIoTClient 11:31ebaeb51e1e 312 case CONN_REFUSED_ID_REJECTED:
AzureIoTClient 11:31ebaeb51e1e 313 return "Id Rejected";
AzureIoTClient 11:31ebaeb51e1e 314 case CONN_REFUSED_SERVER_UNAVAIL:
AzureIoTClient 11:31ebaeb51e1e 315 return "Server Unavailable";
AzureIoTClient 11:31ebaeb51e1e 316 case CONN_REFUSED_BAD_USERNAME_PASSWORD:
AzureIoTClient 11:31ebaeb51e1e 317 return "Bad Username/Password";
AzureIoTClient 11:31ebaeb51e1e 318 case CONN_REFUSED_NOT_AUTHORIZED:
AzureIoTClient 11:31ebaeb51e1e 319 return "Not Authorized";
AzureIoTClient 11:31ebaeb51e1e 320 case CONN_REFUSED_UNKNOWN:
AzureIoTClient 11:31ebaeb51e1e 321 default:
AzureIoTClient 11:31ebaeb51e1e 322 return "Unknown";
AzureIoTClient 11:31ebaeb51e1e 323 }
AzureIoTClient 11:31ebaeb51e1e 324 }
AzureIoTClient 11:31ebaeb51e1e 325
AzureIoTClient 14:4dc2b011be33 326 static int retrieve_device_method_rid_info(const char* resp_topic, STRING_HANDLE method_name, STRING_HANDLE request_id)
AzureIoTClient 12:658ca6865de2 327 {
AzureIoTClient 12:658ca6865de2 328 int result;
AzureIoTClient 12:658ca6865de2 329 STRING_TOKENIZER_HANDLE token_handle = STRING_TOKENIZER_create_from_char(resp_topic);
AzureIoTClient 12:658ca6865de2 330 if (token_handle == NULL)
AzureIoTClient 12:658ca6865de2 331 {
AzureIoTClient 12:658ca6865de2 332 LogError("Failed creating token from device twin topic.");
AzureIoTClient 18:ec8e5e97c6a4 333 result = __FAILURE__;
AzureIoTClient 12:658ca6865de2 334 }
AzureIoTClient 12:658ca6865de2 335 else
AzureIoTClient 12:658ca6865de2 336 {
AzureIoTClient 12:658ca6865de2 337 STRING_HANDLE token_value;
AzureIoTClient 12:658ca6865de2 338 if ((token_value = STRING_new()) == NULL)
AzureIoTClient 12:658ca6865de2 339 {
AzureIoTClient 12:658ca6865de2 340 LogError("Failed allocating new string .");
AzureIoTClient 18:ec8e5e97c6a4 341 result = __FAILURE__;
AzureIoTClient 12:658ca6865de2 342 }
AzureIoTClient 12:658ca6865de2 343 else
AzureIoTClient 12:658ca6865de2 344 {
AzureIoTClient 12:658ca6865de2 345 size_t token_index = 0;
AzureIoTClient 12:658ca6865de2 346 size_t request_id_length = strlen(REQUEST_ID_PROPERTY);
AzureIoTClient 18:ec8e5e97c6a4 347 result = __FAILURE__;
AzureIoTClient 12:658ca6865de2 348 while (STRING_TOKENIZER_get_next_token(token_handle, token_value, "/") == 0)
AzureIoTClient 12:658ca6865de2 349 {
AzureIoTClient 12:658ca6865de2 350 if (token_index == 3)
AzureIoTClient 12:658ca6865de2 351 {
AzureIoTClient 12:658ca6865de2 352 if (STRING_concat_with_STRING(method_name, token_value) != 0)
AzureIoTClient 12:658ca6865de2 353 {
AzureIoTClient 14:4dc2b011be33 354 LogError("Failed STRING_concat_with_STRING.");
AzureIoTClient 18:ec8e5e97c6a4 355 result = __FAILURE__;
AzureIoTClient 12:658ca6865de2 356 break;
AzureIoTClient 12:658ca6865de2 357 }
AzureIoTClient 12:658ca6865de2 358 }
AzureIoTClient 12:658ca6865de2 359 else if (token_index == 4)
AzureIoTClient 12:658ca6865de2 360 {
AzureIoTClient 12:658ca6865de2 361 if (STRING_length(token_value) >= request_id_length)
AzureIoTClient 12:658ca6865de2 362 {
AzureIoTClient 12:658ca6865de2 363 const char* request_id_value = STRING_c_str(token_value);
AzureIoTClient 12:658ca6865de2 364 if (memcmp(request_id_value, REQUEST_ID_PROPERTY, request_id_length) == 0)
AzureIoTClient 12:658ca6865de2 365 {
AzureIoTClient 14:4dc2b011be33 366 if (STRING_concat(request_id, request_id_value+request_id_length) != 0)
AzureIoTClient 14:4dc2b011be33 367 {
AzureIoTClient 14:4dc2b011be33 368 LogError("Failed STRING_concat failed.");
AzureIoTClient 18:ec8e5e97c6a4 369 result = __FAILURE__;
AzureIoTClient 14:4dc2b011be33 370 }
AzureIoTClient 14:4dc2b011be33 371 else
AzureIoTClient 14:4dc2b011be33 372 {
AzureIoTClient 14:4dc2b011be33 373 result = 0;
AzureIoTClient 14:4dc2b011be33 374 }
AzureIoTClient 12:658ca6865de2 375 break;
AzureIoTClient 12:658ca6865de2 376 }
AzureIoTClient 12:658ca6865de2 377 }
AzureIoTClient 12:658ca6865de2 378 }
AzureIoTClient 12:658ca6865de2 379 token_index++;
AzureIoTClient 12:658ca6865de2 380 }
AzureIoTClient 12:658ca6865de2 381 STRING_delete(token_value);
AzureIoTClient 12:658ca6865de2 382 }
AzureIoTClient 12:658ca6865de2 383 STRING_TOKENIZER_destroy(token_handle);
AzureIoTClient 12:658ca6865de2 384 }
AzureIoTClient 12:658ca6865de2 385 return result;
AzureIoTClient 12:658ca6865de2 386 }
AzureIoTClient 12:658ca6865de2 387
AzureIoTClient 12:658ca6865de2 388 static int parse_device_twin_topic_info(const char* resp_topic, bool* patch_msg, size_t* request_id, int* status_code)
AzureIoTClient 12:658ca6865de2 389 {
AzureIoTClient 12:658ca6865de2 390 int result;
AzureIoTClient 12:658ca6865de2 391 STRING_TOKENIZER_HANDLE token_handle = STRING_TOKENIZER_create_from_char(resp_topic);
AzureIoTClient 12:658ca6865de2 392 if (token_handle == NULL)
AzureIoTClient 12:658ca6865de2 393 {
AzureIoTClient 12:658ca6865de2 394 LogError("Failed creating token from device twin topic.");
AzureIoTClient 18:ec8e5e97c6a4 395 result = __FAILURE__;
AzureIoTClient 12:658ca6865de2 396 *status_code = 0;
AzureIoTClient 12:658ca6865de2 397 *request_id = 0;
AzureIoTClient 12:658ca6865de2 398 *patch_msg = false;
AzureIoTClient 12:658ca6865de2 399 }
AzureIoTClient 12:658ca6865de2 400 else
AzureIoTClient 12:658ca6865de2 401 {
AzureIoTClient 12:658ca6865de2 402 STRING_HANDLE token_value;
AzureIoTClient 12:658ca6865de2 403 if ((token_value = STRING_new()) == NULL)
AzureIoTClient 12:658ca6865de2 404 {
AzureIoTClient 12:658ca6865de2 405 LogError("Failed allocating new string .");
AzureIoTClient 18:ec8e5e97c6a4 406 result = __FAILURE__;
AzureIoTClient 12:658ca6865de2 407 *status_code = 0;
AzureIoTClient 12:658ca6865de2 408 *request_id = 0;
AzureIoTClient 12:658ca6865de2 409 *patch_msg = false;
AzureIoTClient 12:658ca6865de2 410 }
AzureIoTClient 12:658ca6865de2 411 else
AzureIoTClient 12:658ca6865de2 412 {
AzureIoTClient 18:ec8e5e97c6a4 413 result = __FAILURE__;
AzureIoTClient 12:658ca6865de2 414 size_t token_count = 0;
AzureIoTClient 12:658ca6865de2 415 while (STRING_TOKENIZER_get_next_token(token_handle, token_value, "/") == 0)
AzureIoTClient 12:658ca6865de2 416 {
AzureIoTClient 12:658ca6865de2 417 if (token_count == 2)
AzureIoTClient 12:658ca6865de2 418 {
AzureIoTClient 12:658ca6865de2 419 if (strcmp(STRING_c_str(token_value), "PATCH") == 0)
AzureIoTClient 12:658ca6865de2 420 {
AzureIoTClient 12:658ca6865de2 421 *patch_msg = true;
AzureIoTClient 12:658ca6865de2 422 *status_code = 0;
AzureIoTClient 12:658ca6865de2 423 *request_id = 0;
AzureIoTClient 12:658ca6865de2 424 result = 0;
AzureIoTClient 12:658ca6865de2 425 break;
AzureIoTClient 12:658ca6865de2 426 }
AzureIoTClient 12:658ca6865de2 427 else
AzureIoTClient 12:658ca6865de2 428 {
AzureIoTClient 12:658ca6865de2 429 *patch_msg = false;
AzureIoTClient 12:658ca6865de2 430 }
AzureIoTClient 12:658ca6865de2 431 }
AzureIoTClient 12:658ca6865de2 432 else if (token_count == 3)
AzureIoTClient 12:658ca6865de2 433 {
AzureIoTClient 12:658ca6865de2 434 *status_code = atol(STRING_c_str(token_value));
AzureIoTClient 12:658ca6865de2 435 if (STRING_TOKENIZER_get_next_token(token_handle, token_value, "/?$rid=") == 0)
AzureIoTClient 12:658ca6865de2 436 {
AzureIoTClient 12:658ca6865de2 437 *request_id = (size_t)atol(STRING_c_str(token_value));
AzureIoTClient 12:658ca6865de2 438 }
AzureIoTClient 12:658ca6865de2 439 *patch_msg = false;
AzureIoTClient 12:658ca6865de2 440 result = 0;
AzureIoTClient 12:658ca6865de2 441 break;
AzureIoTClient 12:658ca6865de2 442 }
AzureIoTClient 12:658ca6865de2 443 token_count++;
AzureIoTClient 12:658ca6865de2 444 }
AzureIoTClient 12:658ca6865de2 445 STRING_delete(token_value);
AzureIoTClient 12:658ca6865de2 446 }
AzureIoTClient 12:658ca6865de2 447 STRING_TOKENIZER_destroy(token_handle);
AzureIoTClient 12:658ca6865de2 448 }
AzureIoTClient 12:658ca6865de2 449 return result;
AzureIoTClient 12:658ca6865de2 450 }
AzureIoTClient 12:658ca6865de2 451
AzureIoTClient 12:658ca6865de2 452 static IOTHUB_IDENTITY_TYPE retrieve_topic_type(const char* topic_resp)
AzureIoTClient 12:658ca6865de2 453 {
AzureIoTClient 12:658ca6865de2 454 IOTHUB_IDENTITY_TYPE type;
AzureIoTClient 12:658ca6865de2 455 size_t dev_twin_topic_len = sizeof(TOPIC_DEVICE_TWIN_PREFIX)-1;
AzureIoTClient 12:658ca6865de2 456 size_t dev_method_topic_len = sizeof(TOPIC_DEVICE_METHOD_PREFIX)-1;
AzureIoTClient 12:658ca6865de2 457 size_t topic_len = strlen(topic_resp);
AzureIoTClient 12:658ca6865de2 458 size_t len_to_use = (dev_twin_topic_len > dev_method_topic_len) ? dev_twin_topic_len : dev_method_topic_len;
AzureIoTClient 12:658ca6865de2 459
AzureIoTClient 12:658ca6865de2 460 bool search_device_twin = true;
AzureIoTClient 12:658ca6865de2 461 bool search_device_method = true;
AzureIoTClient 12:658ca6865de2 462 if (topic_len < len_to_use)
AzureIoTClient 12:658ca6865de2 463 {
AzureIoTClient 12:658ca6865de2 464 search_device_twin = false;
AzureIoTClient 12:658ca6865de2 465 search_device_method = false;
AzureIoTClient 12:658ca6865de2 466 }
AzureIoTClient 12:658ca6865de2 467 else
AzureIoTClient 12:658ca6865de2 468 {
AzureIoTClient 16:14640ee83e99 469 size_t index = 0;
AzureIoTClient 16:14640ee83e99 470 for (index = 0; index < len_to_use && (search_device_twin || search_device_method); index++)
AzureIoTClient 12:658ca6865de2 471 {
AzureIoTClient 12:658ca6865de2 472 if (search_device_twin)
AzureIoTClient 12:658ca6865de2 473 {
AzureIoTClient 12:658ca6865de2 474 if (index > dev_twin_topic_len)
AzureIoTClient 12:658ca6865de2 475 {
AzureIoTClient 12:658ca6865de2 476 search_device_twin = false;
AzureIoTClient 12:658ca6865de2 477 }
AzureIoTClient 12:658ca6865de2 478 else
AzureIoTClient 12:658ca6865de2 479 {
AzureIoTClient 12:658ca6865de2 480 if (index == dev_twin_topic_len)
AzureIoTClient 12:658ca6865de2 481 {
AzureIoTClient 12:658ca6865de2 482 break;
AzureIoTClient 12:658ca6865de2 483 }
AzureIoTClient 14:4dc2b011be33 484 else if (TOUPPER(TOPIC_DEVICE_TWIN_PREFIX[index]) != TOUPPER(topic_resp[index]))
AzureIoTClient 12:658ca6865de2 485 {
AzureIoTClient 12:658ca6865de2 486 search_device_twin = false;
AzureIoTClient 12:658ca6865de2 487 }
AzureIoTClient 12:658ca6865de2 488 }
AzureIoTClient 12:658ca6865de2 489 }
AzureIoTClient 12:658ca6865de2 490 if (search_device_method)
AzureIoTClient 12:658ca6865de2 491 {
AzureIoTClient 12:658ca6865de2 492 if (index > dev_method_topic_len)
AzureIoTClient 12:658ca6865de2 493 {
AzureIoTClient 12:658ca6865de2 494 search_device_method = false;
AzureIoTClient 12:658ca6865de2 495 }
AzureIoTClient 12:658ca6865de2 496 else
AzureIoTClient 12:658ca6865de2 497 {
AzureIoTClient 12:658ca6865de2 498 if (index == dev_method_topic_len)
AzureIoTClient 12:658ca6865de2 499 {
AzureIoTClient 12:658ca6865de2 500 break;
AzureIoTClient 12:658ca6865de2 501 }
AzureIoTClient 14:4dc2b011be33 502 else if (TOUPPER(TOPIC_DEVICE_METHOD_PREFIX[index]) != TOUPPER(topic_resp[index]))
AzureIoTClient 12:658ca6865de2 503 {
AzureIoTClient 12:658ca6865de2 504 search_device_method = false;
AzureIoTClient 12:658ca6865de2 505 }
AzureIoTClient 12:658ca6865de2 506 }
AzureIoTClient 12:658ca6865de2 507 }
AzureIoTClient 12:658ca6865de2 508 }
AzureIoTClient 12:658ca6865de2 509 }
AzureIoTClient 12:658ca6865de2 510 if (search_device_twin)
AzureIoTClient 12:658ca6865de2 511 {
AzureIoTClient 12:658ca6865de2 512 type = IOTHUB_TYPE_DEVICE_TWIN;
AzureIoTClient 12:658ca6865de2 513 }
AzureIoTClient 12:658ca6865de2 514 else if (search_device_method)
AzureIoTClient 12:658ca6865de2 515 {
AzureIoTClient 12:658ca6865de2 516 type = IOTHUB_TYPE_DEVICE_METHODS;
AzureIoTClient 12:658ca6865de2 517 }
AzureIoTClient 12:658ca6865de2 518 else
AzureIoTClient 12:658ca6865de2 519 {
AzureIoTClient 12:658ca6865de2 520 type = IOTHUB_TYPE_TELEMETRY;
AzureIoTClient 12:658ca6865de2 521 }
AzureIoTClient 12:658ca6865de2 522 return type;
AzureIoTClient 12:658ca6865de2 523 }
AzureIoTClient 12:658ca6865de2 524
AzureIoTClient 11:31ebaeb51e1e 525 static void sendMsgComplete(IOTHUB_MESSAGE_LIST* iothubMsgList, PMQTTTRANSPORT_HANDLE_DATA transport_data, IOTHUB_CLIENT_CONFIRMATION_RESULT confirmResult)
AzureIoTClient 11:31ebaeb51e1e 526 {
AzureIoTClient 11:31ebaeb51e1e 527 DLIST_ENTRY messageCompleted;
AzureIoTClient 11:31ebaeb51e1e 528 DList_InitializeListHead(&messageCompleted);
AzureIoTClient 11:31ebaeb51e1e 529 DList_InsertTailList(&messageCompleted, &(iothubMsgList->entry));
AzureIoTClient 11:31ebaeb51e1e 530 IoTHubClient_LL_SendComplete(transport_data->llClientHandle, &messageCompleted, confirmResult);
AzureIoTClient 11:31ebaeb51e1e 531 }
AzureIoTClient 11:31ebaeb51e1e 532
AzureIoTClient 11:31ebaeb51e1e 533 static STRING_HANDLE addPropertiesTouMqttMessage(IOTHUB_MESSAGE_HANDLE iothub_message_handle, const char* eventTopic)
AzureIoTClient 11:31ebaeb51e1e 534 {
AzureIoTClient 11:31ebaeb51e1e 535 STRING_HANDLE result = STRING_construct(eventTopic);
AzureIoTClient 11:31ebaeb51e1e 536 const char* const* propertyKeys;
AzureIoTClient 11:31ebaeb51e1e 537 const char* const* propertyValues;
AzureIoTClient 11:31ebaeb51e1e 538 size_t propertyCount;
AzureIoTClient 23:84f4c36da8c1 539 size_t index = 0;
AzureIoTClient 11:31ebaeb51e1e 540
AzureIoTClient 11:31ebaeb51e1e 541 // Construct Properties
AzureIoTClient 11:31ebaeb51e1e 542 MAP_HANDLE properties_map = IoTHubMessage_Properties(iothub_message_handle);
AzureIoTClient 11:31ebaeb51e1e 543 if (properties_map != NULL)
AzureIoTClient 11:31ebaeb51e1e 544 {
AzureIoTClient 11:31ebaeb51e1e 545 if (Map_GetInternals(properties_map, &propertyKeys, &propertyValues, &propertyCount) != MAP_OK)
AzureIoTClient 11:31ebaeb51e1e 546 {
AzureIoTClient 11:31ebaeb51e1e 547 LogError("Failed to get the internals of the property map.");
AzureIoTClient 11:31ebaeb51e1e 548 STRING_delete(result);
AzureIoTClient 11:31ebaeb51e1e 549 result = NULL;
AzureIoTClient 11:31ebaeb51e1e 550 }
AzureIoTClient 11:31ebaeb51e1e 551 else
AzureIoTClient 11:31ebaeb51e1e 552 {
AzureIoTClient 11:31ebaeb51e1e 553 if (propertyCount != 0)
AzureIoTClient 11:31ebaeb51e1e 554 {
AzureIoTClient 16:14640ee83e99 555 for (index = 0; index < propertyCount && result != NULL; index++)
AzureIoTClient 11:31ebaeb51e1e 556 {
AzureIoTClient 11:31ebaeb51e1e 557 if (STRING_sprintf(result, "%s=%s%s", propertyKeys[index], propertyValues[index], propertyCount - 1 == index ? "" : PROPERTY_SEPARATOR) != 0)
AzureIoTClient 11:31ebaeb51e1e 558 {
AzureIoTClient 23:84f4c36da8c1 559 LogError("Failed construting property string.");
AzureIoTClient 11:31ebaeb51e1e 560 STRING_delete(result);
AzureIoTClient 11:31ebaeb51e1e 561 result = NULL;
AzureIoTClient 11:31ebaeb51e1e 562 }
AzureIoTClient 11:31ebaeb51e1e 563 }
AzureIoTClient 11:31ebaeb51e1e 564 }
AzureIoTClient 11:31ebaeb51e1e 565 }
AzureIoTClient 11:31ebaeb51e1e 566 }
AzureIoTClient 23:84f4c36da8c1 567
AzureIoTClient 23:84f4c36da8c1 568 /* Codes_SRS_IOTHUB_TRANSPORT_MQTT_COMMON_07_052: [ IoTHubTransport_MQTT_Common_DoWork shall check for the CorrelationId property and if found add the value as a system property in the format of $.cid=<id> ] */
AzureIoTClient 23:84f4c36da8c1 569 if (result != NULL)
AzureIoTClient 23:84f4c36da8c1 570 {
AzureIoTClient 23:84f4c36da8c1 571 const char* correlation_id = IoTHubMessage_GetCorrelationId(iothub_message_handle);
AzureIoTClient 23:84f4c36da8c1 572 if (correlation_id != NULL)
AzureIoTClient 23:84f4c36da8c1 573 {
AzureIoTClient 23:84f4c36da8c1 574 if (STRING_sprintf(result, "%s%%24.cid=%s", index == 0 ? "" : PROPERTY_SEPARATOR, correlation_id) != 0)
AzureIoTClient 23:84f4c36da8c1 575 {
AzureIoTClient 27:04de3c0bf1db 576 LogError("Failed setting correlation id.");
AzureIoTClient 23:84f4c36da8c1 577 STRING_delete(result);
AzureIoTClient 23:84f4c36da8c1 578 result = NULL;
AzureIoTClient 23:84f4c36da8c1 579 }
AzureIoTClient 23:84f4c36da8c1 580 index++;
AzureIoTClient 23:84f4c36da8c1 581 }
AzureIoTClient 23:84f4c36da8c1 582 }
AzureIoTClient 23:84f4c36da8c1 583
AzureIoTClient 23:84f4c36da8c1 584 /* Codes_SRS_IOTHUB_TRANSPORT_MQTT_COMMON_07_053: [ IoTHubTransport_MQTT_Common_DoWork shall check for the MessageId property and if found add the value as a system property in the format of $.mid=<id> ] */
AzureIoTClient 23:84f4c36da8c1 585 if (result != NULL)
AzureIoTClient 23:84f4c36da8c1 586 {
AzureIoTClient 23:84f4c36da8c1 587 const char* msg_id = IoTHubMessage_GetMessageId(iothub_message_handle);
AzureIoTClient 23:84f4c36da8c1 588 if (msg_id != NULL)
AzureIoTClient 23:84f4c36da8c1 589 {
AzureIoTClient 23:84f4c36da8c1 590 if (STRING_sprintf(result, "%s%%24.mid=%s", index == 0 ? "" : PROPERTY_SEPARATOR, msg_id) != 0)
AzureIoTClient 23:84f4c36da8c1 591 {
AzureIoTClient 27:04de3c0bf1db 592 LogError("Failed setting message id.");
AzureIoTClient 27:04de3c0bf1db 593 STRING_delete(result);
AzureIoTClient 27:04de3c0bf1db 594 result = NULL;
AzureIoTClient 27:04de3c0bf1db 595 }
AzureIoTClient 27:04de3c0bf1db 596 index++;
AzureIoTClient 27:04de3c0bf1db 597 }
AzureIoTClient 27:04de3c0bf1db 598 }
AzureIoTClient 27:04de3c0bf1db 599
AzureIoTClient 27:04de3c0bf1db 600 // Codes_SRS_IOTHUB_TRANSPORT_MQTT_COMMON_09_010: [ `IoTHubTransport_MQTT_Common_DoWork` shall check for the ContentType property and if found add the `value` as a system property in the format of `$.ct=<value>` ]
AzureIoTClient 27:04de3c0bf1db 601 if (result != NULL)
AzureIoTClient 27:04de3c0bf1db 602 {
AzureIoTClient 27:04de3c0bf1db 603 const char* content_type = IoTHubMessage_GetContentTypeSystemProperty(iothub_message_handle);
AzureIoTClient 27:04de3c0bf1db 604 if (content_type != NULL)
AzureIoTClient 27:04de3c0bf1db 605 {
AzureIoTClient 27:04de3c0bf1db 606 if (STRING_sprintf(result, "%s%%24.%s=%s", index == 0 ? "" : PROPERTY_SEPARATOR, CONTENT_TYPE_PROPERTY, content_type) != 0)
AzureIoTClient 27:04de3c0bf1db 607 {
AzureIoTClient 27:04de3c0bf1db 608 LogError("Failed setting content type.");
AzureIoTClient 23:84f4c36da8c1 609 STRING_delete(result);
AzureIoTClient 23:84f4c36da8c1 610 result = NULL;
AzureIoTClient 23:84f4c36da8c1 611 }
AzureIoTClient 27:04de3c0bf1db 612 index++;
AzureIoTClient 23:84f4c36da8c1 613 }
AzureIoTClient 23:84f4c36da8c1 614 }
AzureIoTClient 27:04de3c0bf1db 615
AzureIoTClient 27:04de3c0bf1db 616 // Codes_SRS_IOTHUB_TRANSPORT_MQTT_COMMON_09_011: [ `IoTHubTransport_MQTT_Common_DoWork` shall check for the ContentEncoding property and if found add the `value` as a system property in the format of `$.ce=<value>` ]
AzureIoTClient 27:04de3c0bf1db 617 if (result != NULL)
AzureIoTClient 27:04de3c0bf1db 618 {
AzureIoTClient 27:04de3c0bf1db 619 const char* content_encoding = IoTHubMessage_GetContentEncodingSystemProperty(iothub_message_handle);
AzureIoTClient 27:04de3c0bf1db 620 if (content_encoding != NULL)
AzureIoTClient 27:04de3c0bf1db 621 {
AzureIoTClient 27:04de3c0bf1db 622 if (STRING_sprintf(result, "%s%%24.%s=%s", index == 0 ? "" : PROPERTY_SEPARATOR, CONTENT_ENCODING_PROPERTY, content_encoding) != 0)
AzureIoTClient 27:04de3c0bf1db 623 {
AzureIoTClient 27:04de3c0bf1db 624 LogError("Failed setting content encoding.");
AzureIoTClient 27:04de3c0bf1db 625 STRING_delete(result);
AzureIoTClient 27:04de3c0bf1db 626 result = NULL;
AzureIoTClient 27:04de3c0bf1db 627 }
AzureIoTClient 27:04de3c0bf1db 628 index++;
AzureIoTClient 27:04de3c0bf1db 629 }
AzureIoTClient 27:04de3c0bf1db 630 }
AzureIoTClient 27:04de3c0bf1db 631
AzureIoTClient 11:31ebaeb51e1e 632 return result;
AzureIoTClient 11:31ebaeb51e1e 633 }
AzureIoTClient 11:31ebaeb51e1e 634
AzureIoTClient 11:31ebaeb51e1e 635 static int publish_mqtt_telemetry_msg(PMQTTTRANSPORT_HANDLE_DATA transport_data, MQTT_MESSAGE_DETAILS_LIST* mqttMsgEntry, const unsigned char* payload, size_t len)
AzureIoTClient 11:31ebaeb51e1e 636 {
AzureIoTClient 11:31ebaeb51e1e 637 int result;
AzureIoTClient 11:31ebaeb51e1e 638 STRING_HANDLE msgTopic = addPropertiesTouMqttMessage(mqttMsgEntry->iotHubMessageEntry->messageHandle, STRING_c_str(transport_data->topic_MqttEvent));
AzureIoTClient 11:31ebaeb51e1e 639 if (msgTopic == NULL)
AzureIoTClient 11:31ebaeb51e1e 640 {
AzureIoTClient 27:04de3c0bf1db 641 LogError("Failed adding properties to mqtt message");
AzureIoTClient 18:ec8e5e97c6a4 642 result = __FAILURE__;
AzureIoTClient 11:31ebaeb51e1e 643 }
AzureIoTClient 11:31ebaeb51e1e 644 else
AzureIoTClient 11:31ebaeb51e1e 645 {
AzureIoTClient 11:31ebaeb51e1e 646 MQTT_MESSAGE_HANDLE mqttMsg = mqttmessage_create(mqttMsgEntry->packet_id, STRING_c_str(msgTopic), DELIVER_AT_LEAST_ONCE, payload, len);
AzureIoTClient 11:31ebaeb51e1e 647 if (mqttMsg == NULL)
AzureIoTClient 11:31ebaeb51e1e 648 {
AzureIoTClient 27:04de3c0bf1db 649 LogError("Failed creating mqtt message");
AzureIoTClient 18:ec8e5e97c6a4 650 result = __FAILURE__;
AzureIoTClient 11:31ebaeb51e1e 651 }
AzureIoTClient 11:31ebaeb51e1e 652 else
AzureIoTClient 11:31ebaeb51e1e 653 {
Azure.IoT.Build 13:606465879c57 654 if (tickcounter_get_current_ms(transport_data->msgTickCounter, &mqttMsgEntry->msgPublishTime) != 0)
AzureIoTClient 11:31ebaeb51e1e 655 {
AzureIoTClient 11:31ebaeb51e1e 656 LogError("Failed retrieving tickcounter info");
AzureIoTClient 18:ec8e5e97c6a4 657 result = __FAILURE__;
AzureIoTClient 11:31ebaeb51e1e 658 }
AzureIoTClient 11:31ebaeb51e1e 659 else
AzureIoTClient 11:31ebaeb51e1e 660 {
AzureIoTClient 11:31ebaeb51e1e 661 if (mqtt_client_publish(transport_data->mqttClient, mqttMsg) != 0)
AzureIoTClient 11:31ebaeb51e1e 662 {
AzureIoTClient 27:04de3c0bf1db 663 LogError("Failed attempting to publish mqtt message");
AzureIoTClient 18:ec8e5e97c6a4 664 result = __FAILURE__;
AzureIoTClient 11:31ebaeb51e1e 665 }
AzureIoTClient 11:31ebaeb51e1e 666 else
AzureIoTClient 11:31ebaeb51e1e 667 {
AzureIoTClient 11:31ebaeb51e1e 668 mqttMsgEntry->retryCount++;
AzureIoTClient 11:31ebaeb51e1e 669 result = 0;
AzureIoTClient 11:31ebaeb51e1e 670 }
AzureIoTClient 11:31ebaeb51e1e 671 }
AzureIoTClient 11:31ebaeb51e1e 672 mqttmessage_destroy(mqttMsg);
AzureIoTClient 11:31ebaeb51e1e 673 }
AzureIoTClient 11:31ebaeb51e1e 674 STRING_delete(msgTopic);
AzureIoTClient 11:31ebaeb51e1e 675 }
AzureIoTClient 11:31ebaeb51e1e 676 return result;
AzureIoTClient 11:31ebaeb51e1e 677 }
AzureIoTClient 11:31ebaeb51e1e 678
AzureIoTClient 14:4dc2b011be33 679 static int publish_device_method_message(MQTTTRANSPORT_HANDLE_DATA* transport_data, int status_code, STRING_HANDLE request_id, const unsigned char* response, size_t response_size)
AzureIoTClient 12:658ca6865de2 680 {
AzureIoTClient 12:658ca6865de2 681 int result;
AzureIoTClient 12:658ca6865de2 682 uint16_t packet_id = get_next_packet_id(transport_data);
AzureIoTClient 12:658ca6865de2 683
AzureIoTClient 14:4dc2b011be33 684 STRING_HANDLE msg_topic = STRING_construct_sprintf(DEVICE_METHOD_RESPONSE_TOPIC, status_code, STRING_c_str(request_id) );
AzureIoTClient 12:658ca6865de2 685 if (msg_topic == NULL)
AzureIoTClient 12:658ca6865de2 686 {
AzureIoTClient 12:658ca6865de2 687 LogError("Failed constructing message topic.");
AzureIoTClient 18:ec8e5e97c6a4 688 result = __FAILURE__;
AzureIoTClient 12:658ca6865de2 689 }
AzureIoTClient 12:658ca6865de2 690 else
AzureIoTClient 12:658ca6865de2 691 {
AzureIoTClient 14:4dc2b011be33 692 MQTT_MESSAGE_HANDLE mqtt_get_msg = mqttmessage_create(packet_id, STRING_c_str(msg_topic), DELIVER_AT_MOST_ONCE, response, response_size);
AzureIoTClient 12:658ca6865de2 693 if (mqtt_get_msg == NULL)
AzureIoTClient 12:658ca6865de2 694 {
AzureIoTClient 12:658ca6865de2 695 LogError("Failed constructing mqtt message.");
AzureIoTClient 18:ec8e5e97c6a4 696 result = __FAILURE__;
AzureIoTClient 12:658ca6865de2 697 }
AzureIoTClient 12:658ca6865de2 698 else
AzureIoTClient 12:658ca6865de2 699 {
AzureIoTClient 12:658ca6865de2 700 if (mqtt_client_publish(transport_data->mqttClient, mqtt_get_msg) != 0)
AzureIoTClient 12:658ca6865de2 701 {
AzureIoTClient 12:658ca6865de2 702 LogError("Failed publishing to mqtt client.");
AzureIoTClient 18:ec8e5e97c6a4 703 result = __FAILURE__;
AzureIoTClient 12:658ca6865de2 704 }
AzureIoTClient 12:658ca6865de2 705 else
AzureIoTClient 12:658ca6865de2 706 {
AzureIoTClient 12:658ca6865de2 707 result = 0;
AzureIoTClient 12:658ca6865de2 708 }
AzureIoTClient 12:658ca6865de2 709 mqttmessage_destroy(mqtt_get_msg);
AzureIoTClient 12:658ca6865de2 710 }
AzureIoTClient 12:658ca6865de2 711 STRING_delete(msg_topic);
AzureIoTClient 12:658ca6865de2 712 }
AzureIoTClient 12:658ca6865de2 713 return result;
AzureIoTClient 12:658ca6865de2 714 }
AzureIoTClient 12:658ca6865de2 715
AzureIoTClient 12:658ca6865de2 716 static int publish_device_twin_get_message(MQTTTRANSPORT_HANDLE_DATA* transport_data)
AzureIoTClient 12:658ca6865de2 717 {
AzureIoTClient 12:658ca6865de2 718 int result;
AzureIoTClient 12:658ca6865de2 719 MQTT_DEVICE_TWIN_ITEM* mqtt_info = (MQTT_DEVICE_TWIN_ITEM*)malloc(sizeof(MQTT_DEVICE_TWIN_ITEM));
AzureIoTClient 12:658ca6865de2 720 if (mqtt_info == NULL)
AzureIoTClient 12:658ca6865de2 721 {
AzureIoTClient 12:658ca6865de2 722 LogError("Failed allocating device twin data.");
AzureIoTClient 18:ec8e5e97c6a4 723 result = __FAILURE__;
AzureIoTClient 12:658ca6865de2 724 }
AzureIoTClient 12:658ca6865de2 725 else
AzureIoTClient 12:658ca6865de2 726 {
AzureIoTClient 12:658ca6865de2 727 mqtt_info->packet_id = get_next_packet_id(transport_data);
AzureIoTClient 12:658ca6865de2 728 mqtt_info->iothub_msg_id = 0;
AzureIoTClient 12:658ca6865de2 729 mqtt_info->device_twin_msg_type = RETRIEVE_PROPERTIES;
AzureIoTClient 12:658ca6865de2 730 mqtt_info->retryCount = 0;
AzureIoTClient 12:658ca6865de2 731 mqtt_info->msgPublishTime = 0;
AzureIoTClient 12:658ca6865de2 732 mqtt_info->iothub_type = IOTHUB_TYPE_DEVICE_TWIN;
AzureIoTClient 12:658ca6865de2 733 mqtt_info->device_twin_data = NULL;
AzureIoTClient 12:658ca6865de2 734 STRING_HANDLE msg_topic = STRING_construct_sprintf(GET_PROPERTIES_TOPIC, mqtt_info->packet_id);
AzureIoTClient 12:658ca6865de2 735 if (msg_topic == NULL)
AzureIoTClient 12:658ca6865de2 736 {
AzureIoTClient 12:658ca6865de2 737 LogError("Failed constructing get Prop topic.");
AzureIoTClient 12:658ca6865de2 738 free(mqtt_info);
AzureIoTClient 18:ec8e5e97c6a4 739 result = __FAILURE__;
AzureIoTClient 12:658ca6865de2 740 }
AzureIoTClient 12:658ca6865de2 741 else
AzureIoTClient 12:658ca6865de2 742 {
AzureIoTClient 12:658ca6865de2 743 MQTT_MESSAGE_HANDLE mqtt_get_msg = mqttmessage_create(mqtt_info->packet_id, STRING_c_str(msg_topic), DELIVER_AT_MOST_ONCE, NULL, 0);
AzureIoTClient 12:658ca6865de2 744 if (mqtt_get_msg == NULL)
AzureIoTClient 12:658ca6865de2 745 {
AzureIoTClient 12:658ca6865de2 746 LogError("Failed constructing mqtt message.");
AzureIoTClient 12:658ca6865de2 747 free(mqtt_info);
AzureIoTClient 18:ec8e5e97c6a4 748 result = __FAILURE__;
AzureIoTClient 12:658ca6865de2 749 }
AzureIoTClient 12:658ca6865de2 750 else
AzureIoTClient 12:658ca6865de2 751 {
AzureIoTClient 12:658ca6865de2 752 if (mqtt_client_publish(transport_data->mqttClient, mqtt_get_msg) != 0)
AzureIoTClient 12:658ca6865de2 753 {
AzureIoTClient 12:658ca6865de2 754 LogError("Failed publishing to mqtt client.");
AzureIoTClient 12:658ca6865de2 755 free(mqtt_info);
AzureIoTClient 18:ec8e5e97c6a4 756 result = __FAILURE__;
AzureIoTClient 12:658ca6865de2 757 }
AzureIoTClient 12:658ca6865de2 758 else
AzureIoTClient 12:658ca6865de2 759 {
AzureIoTClient 12:658ca6865de2 760 DList_InsertTailList(&transport_data->ack_waiting_queue, &mqtt_info->entry);
AzureIoTClient 12:658ca6865de2 761 result = 0;
AzureIoTClient 12:658ca6865de2 762 }
AzureIoTClient 12:658ca6865de2 763 mqttmessage_destroy(mqtt_get_msg);
AzureIoTClient 12:658ca6865de2 764 }
AzureIoTClient 12:658ca6865de2 765 STRING_delete(msg_topic);
AzureIoTClient 12:658ca6865de2 766 }
AzureIoTClient 12:658ca6865de2 767 }
AzureIoTClient 12:658ca6865de2 768 return result;
AzureIoTClient 12:658ca6865de2 769 }
AzureIoTClient 12:658ca6865de2 770
AzureIoTClient 12:658ca6865de2 771 static int publish_device_twin_message(MQTTTRANSPORT_HANDLE_DATA* transport_data, IOTHUB_DEVICE_TWIN* device_twin_info, MQTT_DEVICE_TWIN_ITEM* mqtt_info)
AzureIoTClient 12:658ca6865de2 772 {
AzureIoTClient 12:658ca6865de2 773 int result;
AzureIoTClient 12:658ca6865de2 774 mqtt_info->packet_id = get_next_packet_id(transport_data);
AzureIoTClient 12:658ca6865de2 775 mqtt_info->device_twin_msg_type = REPORTED_STATE;
AzureIoTClient 12:658ca6865de2 776 STRING_HANDLE msgTopic = STRING_construct_sprintf(REPORTED_PROPERTIES_TOPIC, mqtt_info->packet_id);
AzureIoTClient 12:658ca6865de2 777 if (msgTopic == NULL)
AzureIoTClient 12:658ca6865de2 778 {
AzureIoTClient 12:658ca6865de2 779 LogError("Failed constructing reported prop topic.");
AzureIoTClient 18:ec8e5e97c6a4 780 result = __FAILURE__;
AzureIoTClient 12:658ca6865de2 781 }
AzureIoTClient 12:658ca6865de2 782 else
AzureIoTClient 12:658ca6865de2 783 {
AzureIoTClient 12:658ca6865de2 784 const CONSTBUFFER* data_buff = CONSTBUFFER_GetContent(device_twin_info->report_data_handle);
AzureIoTClient 12:658ca6865de2 785 MQTT_MESSAGE_HANDLE mqtt_rpt_msg = mqttmessage_create(mqtt_info->packet_id, STRING_c_str(msgTopic), DELIVER_AT_MOST_ONCE, data_buff->buffer, data_buff->size);
AzureIoTClient 12:658ca6865de2 786 if (mqtt_rpt_msg == NULL)
AzureIoTClient 12:658ca6865de2 787 {
AzureIoTClient 12:658ca6865de2 788 LogError("Failed creating mqtt message");
AzureIoTClient 18:ec8e5e97c6a4 789 result = __FAILURE__;
AzureIoTClient 12:658ca6865de2 790 }
AzureIoTClient 12:658ca6865de2 791 else
AzureIoTClient 12:658ca6865de2 792 {
Azure.IoT.Build 13:606465879c57 793 if (tickcounter_get_current_ms(transport_data->msgTickCounter, &mqtt_info->msgPublishTime) != 0)
AzureIoTClient 12:658ca6865de2 794 {
AzureIoTClient 12:658ca6865de2 795 LogError("Failed retrieving tickcounter info");
AzureIoTClient 18:ec8e5e97c6a4 796 result = __FAILURE__;
AzureIoTClient 12:658ca6865de2 797 }
AzureIoTClient 12:658ca6865de2 798 else
AzureIoTClient 12:658ca6865de2 799 {
AzureIoTClient 12:658ca6865de2 800 if (mqtt_client_publish(transport_data->mqttClient, mqtt_rpt_msg) != 0)
AzureIoTClient 12:658ca6865de2 801 {
AzureIoTClient 12:658ca6865de2 802 LogError("Failed publishing mqtt message");
AzureIoTClient 18:ec8e5e97c6a4 803 result = __FAILURE__;
AzureIoTClient 12:658ca6865de2 804 }
AzureIoTClient 12:658ca6865de2 805 else
AzureIoTClient 12:658ca6865de2 806 {
AzureIoTClient 12:658ca6865de2 807 mqtt_info->retryCount++;
AzureIoTClient 12:658ca6865de2 808 result = 0;
AzureIoTClient 12:658ca6865de2 809 }
AzureIoTClient 12:658ca6865de2 810 }
AzureIoTClient 12:658ca6865de2 811 mqttmessage_destroy(mqtt_rpt_msg);
AzureIoTClient 12:658ca6865de2 812 }
AzureIoTClient 12:658ca6865de2 813 STRING_delete(msgTopic);
AzureIoTClient 12:658ca6865de2 814 }
AzureIoTClient 12:658ca6865de2 815 return result;
AzureIoTClient 12:658ca6865de2 816 }
AzureIoTClient 12:658ca6865de2 817
AzureIoTClient 11:31ebaeb51e1e 818 static bool isSystemProperty(const char* tokenData)
AzureIoTClient 11:31ebaeb51e1e 819 {
AzureIoTClient 11:31ebaeb51e1e 820 bool result = false;
AzureIoTClient 11:31ebaeb51e1e 821 size_t propCount = sizeof(sysPropList)/sizeof(sysPropList[0]);
AzureIoTClient 16:14640ee83e99 822 size_t index = 0;
AzureIoTClient 16:14640ee83e99 823 for (index = 0; index < propCount; index++)
AzureIoTClient 11:31ebaeb51e1e 824 {
AzureIoTClient 11:31ebaeb51e1e 825 if (memcmp(tokenData, sysPropList[index].propName, sysPropList[index].propLength) == 0)
AzureIoTClient 11:31ebaeb51e1e 826 {
AzureIoTClient 11:31ebaeb51e1e 827 result = true;
AzureIoTClient 11:31ebaeb51e1e 828 break;
AzureIoTClient 11:31ebaeb51e1e 829 }
AzureIoTClient 11:31ebaeb51e1e 830 }
AzureIoTClient 11:31ebaeb51e1e 831 return result;
AzureIoTClient 11:31ebaeb51e1e 832 }
AzureIoTClient 11:31ebaeb51e1e 833
AzureIoTClient 11:31ebaeb51e1e 834 static int extractMqttProperties(IOTHUB_MESSAGE_HANDLE IoTHubMessage, const char* topic_name)
AzureIoTClient 11:31ebaeb51e1e 835 {
AzureIoTClient 11:31ebaeb51e1e 836 int result;
AzureIoTClient 11:31ebaeb51e1e 837 STRING_HANDLE mqttTopic = STRING_construct(topic_name);
AzureIoTClient 11:31ebaeb51e1e 838 if (mqttTopic == NULL)
AzureIoTClient 11:31ebaeb51e1e 839 {
AzureIoTClient 11:31ebaeb51e1e 840 LogError("Failure constructing string topic name.");
AzureIoTClient 18:ec8e5e97c6a4 841 result = __FAILURE__;
AzureIoTClient 11:31ebaeb51e1e 842 }
AzureIoTClient 11:31ebaeb51e1e 843 else
AzureIoTClient 11:31ebaeb51e1e 844 {
AzureIoTClient 11:31ebaeb51e1e 845 STRING_TOKENIZER_HANDLE token = STRING_TOKENIZER_create(mqttTopic);
AzureIoTClient 11:31ebaeb51e1e 846 if (token != NULL)
AzureIoTClient 11:31ebaeb51e1e 847 {
AzureIoTClient 11:31ebaeb51e1e 848 MAP_HANDLE propertyMap = IoTHubMessage_Properties(IoTHubMessage);
AzureIoTClient 11:31ebaeb51e1e 849 if (propertyMap == NULL)
AzureIoTClient 11:31ebaeb51e1e 850 {
AzureIoTClient 11:31ebaeb51e1e 851 LogError("Failure to retrieve IoTHubMessage_properties.");
AzureIoTClient 18:ec8e5e97c6a4 852 result = __FAILURE__;
AzureIoTClient 11:31ebaeb51e1e 853 }
AzureIoTClient 11:31ebaeb51e1e 854 else
AzureIoTClient 11:31ebaeb51e1e 855 {
AzureIoTClient 11:31ebaeb51e1e 856 STRING_HANDLE output = STRING_new();
AzureIoTClient 11:31ebaeb51e1e 857 if (output == NULL)
AzureIoTClient 11:31ebaeb51e1e 858 {
AzureIoTClient 11:31ebaeb51e1e 859 LogError("Failure to allocate STRING_new.");
AzureIoTClient 18:ec8e5e97c6a4 860 result = __FAILURE__;
AzureIoTClient 11:31ebaeb51e1e 861 }
AzureIoTClient 11:31ebaeb51e1e 862 else
AzureIoTClient 11:31ebaeb51e1e 863 {
AzureIoTClient 11:31ebaeb51e1e 864 result = 0;
AzureIoTClient 18:ec8e5e97c6a4 865
AzureIoTClient 11:31ebaeb51e1e 866 while (STRING_TOKENIZER_get_next_token(token, output, PROPERTY_SEPARATOR) == 0 && result == 0)
AzureIoTClient 11:31ebaeb51e1e 867 {
AzureIoTClient 11:31ebaeb51e1e 868 const char* tokenData = STRING_c_str(output);
AzureIoTClient 11:31ebaeb51e1e 869 size_t tokenLen = strlen(tokenData);
AzureIoTClient 11:31ebaeb51e1e 870 if (tokenData == NULL || tokenLen == 0)
AzureIoTClient 11:31ebaeb51e1e 871 {
AzureIoTClient 11:31ebaeb51e1e 872 break;
AzureIoTClient 11:31ebaeb51e1e 873 }
AzureIoTClient 11:31ebaeb51e1e 874 else
AzureIoTClient 11:31ebaeb51e1e 875 {
AzureIoTClient 18:ec8e5e97c6a4 876 if (isSystemProperty(tokenData))
AzureIoTClient 11:31ebaeb51e1e 877 {
AzureIoTClient 11:31ebaeb51e1e 878 const char* iterator = tokenData;
AzureIoTClient 11:31ebaeb51e1e 879 while (iterator != NULL && *iterator != '\0' && result == 0)
AzureIoTClient 11:31ebaeb51e1e 880 {
AzureIoTClient 11:31ebaeb51e1e 881 if (*iterator == '=')
AzureIoTClient 11:31ebaeb51e1e 882 {
AzureIoTClient 11:31ebaeb51e1e 883 size_t nameLen = iterator - tokenData;
AzureIoTClient 11:31ebaeb51e1e 884 char* propName = malloc(nameLen + 1);
AzureIoTClient 11:31ebaeb51e1e 885
AzureIoTClient 11:31ebaeb51e1e 886 size_t valLen = tokenLen - (nameLen + 1) + 1;
AzureIoTClient 11:31ebaeb51e1e 887 char* propValue = malloc(valLen + 1);
AzureIoTClient 11:31ebaeb51e1e 888
AzureIoTClient 11:31ebaeb51e1e 889 if (propName == NULL || propValue == NULL)
AzureIoTClient 11:31ebaeb51e1e 890 {
AzureIoTClient 27:04de3c0bf1db 891 LogError("Failed allocating property name (%p) and/or value (%p)", propName, propValue);
AzureIoTClient 18:ec8e5e97c6a4 892 result = __FAILURE__;
AzureIoTClient 18:ec8e5e97c6a4 893 }
AzureIoTClient 18:ec8e5e97c6a4 894 else
AzureIoTClient 18:ec8e5e97c6a4 895 {
AzureIoTClient 18:ec8e5e97c6a4 896 strncpy(propName, tokenData, nameLen);
AzureIoTClient 18:ec8e5e97c6a4 897 propName[nameLen] = '\0';
AzureIoTClient 18:ec8e5e97c6a4 898
AzureIoTClient 18:ec8e5e97c6a4 899 strncpy(propValue, iterator + 1, valLen);
AzureIoTClient 18:ec8e5e97c6a4 900 propValue[valLen] = '\0';
AzureIoTClient 18:ec8e5e97c6a4 901
AzureIoTClient 27:04de3c0bf1db 902 if (nameLen > 2)
AzureIoTClient 18:ec8e5e97c6a4 903 {
AzureIoTClient 27:04de3c0bf1db 904 if (nameLen > 3)
AzureIoTClient 18:ec8e5e97c6a4 905 {
AzureIoTClient 27:04de3c0bf1db 906 if (strcmp((const char*)&propName[nameLen - 3], MESSAGE_ID_PROPERTY) == 0)
AzureIoTClient 27:04de3c0bf1db 907 {
AzureIoTClient 27:04de3c0bf1db 908 if (IoTHubMessage_SetMessageId(IoTHubMessage, propValue) != IOTHUB_MESSAGE_OK)
AzureIoTClient 27:04de3c0bf1db 909 {
AzureIoTClient 27:04de3c0bf1db 910 LogError("Failed to set IOTHUB_MESSAGE_HANDLE 'messageId' property.");
AzureIoTClient 27:04de3c0bf1db 911 result = __FAILURE__;
AzureIoTClient 27:04de3c0bf1db 912 }
AzureIoTClient 27:04de3c0bf1db 913 }
AzureIoTClient 27:04de3c0bf1db 914 else if (strcmp((const char*)&propName[nameLen - 3], CORRELATION_ID_PROPERTY) == 0)
AzureIoTClient 18:ec8e5e97c6a4 915 {
AzureIoTClient 27:04de3c0bf1db 916 if (IoTHubMessage_SetCorrelationId(IoTHubMessage, propValue) != IOTHUB_MESSAGE_OK)
AzureIoTClient 27:04de3c0bf1db 917 {
AzureIoTClient 27:04de3c0bf1db 918 LogError("Failed to set IOTHUB_MESSAGE_HANDLE 'correlationId' property.");
AzureIoTClient 27:04de3c0bf1db 919 result = __FAILURE__;
AzureIoTClient 27:04de3c0bf1db 920 }
AzureIoTClient 27:04de3c0bf1db 921 }
AzureIoTClient 27:04de3c0bf1db 922 }
AzureIoTClient 27:04de3c0bf1db 923
AzureIoTClient 27:04de3c0bf1db 924 // Codes_SRS_IOTHUB_TRANSPORT_MQTT_COMMON_09_012: [ If type is IOTHUB_TYPE_TELEMETRY and the system property `$.ct` is defined, its value shall be set on the IOTHUB_MESSAGE_HANDLE's ContentType property ]
AzureIoTClient 27:04de3c0bf1db 925 if (strcmp((const char*)&propName[nameLen - 2], CONTENT_TYPE_PROPERTY) == 0)
AzureIoTClient 27:04de3c0bf1db 926 {
AzureIoTClient 27:04de3c0bf1db 927 if (IoTHubMessage_SetContentTypeSystemProperty(IoTHubMessage, propValue) != IOTHUB_MESSAGE_OK)
AzureIoTClient 27:04de3c0bf1db 928 {
AzureIoTClient 27:04de3c0bf1db 929 LogError("Failed to set IOTHUB_MESSAGE_HANDLE 'customContentType' property.");
AzureIoTClient 18:ec8e5e97c6a4 930 result = __FAILURE__;
AzureIoTClient 18:ec8e5e97c6a4 931 }
AzureIoTClient 18:ec8e5e97c6a4 932 }
AzureIoTClient 27:04de3c0bf1db 933 // Codes_SRS_IOTHUB_TRANSPORT_MQTT_COMMON_09_013: [ If type is IOTHUB_TYPE_TELEMETRY and the system property `$.ce` is defined, its value shall be set on the IOTHUB_MESSAGE_HANDLE's ContentEncoding property ]
AzureIoTClient 27:04de3c0bf1db 934 else if (strcmp((const char*)&propName[nameLen - 2], CONTENT_ENCODING_PROPERTY) == 0)
AzureIoTClient 18:ec8e5e97c6a4 935 {
AzureIoTClient 27:04de3c0bf1db 936 if (IoTHubMessage_SetContentEncodingSystemProperty(IoTHubMessage, propValue) != IOTHUB_MESSAGE_OK)
AzureIoTClient 18:ec8e5e97c6a4 937 {
AzureIoTClient 27:04de3c0bf1db 938 LogError("Failed to set IOTHUB_MESSAGE_HANDLE 'contentEncoding' property.");
AzureIoTClient 18:ec8e5e97c6a4 939 result = __FAILURE__;
AzureIoTClient 18:ec8e5e97c6a4 940 }
AzureIoTClient 18:ec8e5e97c6a4 941 }
AzureIoTClient 18:ec8e5e97c6a4 942 }
AzureIoTClient 18:ec8e5e97c6a4 943 }
AzureIoTClient 18:ec8e5e97c6a4 944 free(propName);
AzureIoTClient 18:ec8e5e97c6a4 945 free(propValue);
AzureIoTClient 18:ec8e5e97c6a4 946
AzureIoTClient 18:ec8e5e97c6a4 947 break;
AzureIoTClient 18:ec8e5e97c6a4 948 }
AzureIoTClient 18:ec8e5e97c6a4 949 iterator++;
AzureIoTClient 18:ec8e5e97c6a4 950 }
AzureIoTClient 18:ec8e5e97c6a4 951 }
AzureIoTClient 18:ec8e5e97c6a4 952 else
AzureIoTClient 18:ec8e5e97c6a4 953 {
AzureIoTClient 18:ec8e5e97c6a4 954 const char* iterator = tokenData;
AzureIoTClient 18:ec8e5e97c6a4 955 while (iterator != NULL && *iterator != '\0' && result == 0)
AzureIoTClient 18:ec8e5e97c6a4 956 {
AzureIoTClient 18:ec8e5e97c6a4 957 if (*iterator == '=')
AzureIoTClient 18:ec8e5e97c6a4 958 {
AzureIoTClient 18:ec8e5e97c6a4 959 size_t nameLen = iterator - tokenData;
AzureIoTClient 18:ec8e5e97c6a4 960 char* propName = malloc(nameLen + 1);
AzureIoTClient 18:ec8e5e97c6a4 961
AzureIoTClient 18:ec8e5e97c6a4 962 size_t valLen = tokenLen - (nameLen + 1) + 1;
AzureIoTClient 18:ec8e5e97c6a4 963 char* propValue = malloc(valLen + 1);
AzureIoTClient 18:ec8e5e97c6a4 964
AzureIoTClient 18:ec8e5e97c6a4 965 if (propName == NULL || propValue == NULL)
AzureIoTClient 18:ec8e5e97c6a4 966 {
AzureIoTClient 18:ec8e5e97c6a4 967 result = __FAILURE__;
AzureIoTClient 11:31ebaeb51e1e 968 }
AzureIoTClient 11:31ebaeb51e1e 969 else
AzureIoTClient 11:31ebaeb51e1e 970 {
AzureIoTClient 11:31ebaeb51e1e 971 strncpy(propName, tokenData, nameLen);
AzureIoTClient 11:31ebaeb51e1e 972 propName[nameLen] = '\0';
AzureIoTClient 11:31ebaeb51e1e 973
AzureIoTClient 11:31ebaeb51e1e 974 strncpy(propValue, iterator + 1, valLen);
AzureIoTClient 11:31ebaeb51e1e 975 propValue[valLen] = '\0';
AzureIoTClient 11:31ebaeb51e1e 976
AzureIoTClient 11:31ebaeb51e1e 977 if (Map_AddOrUpdate(propertyMap, propName, propValue) != MAP_OK)
AzureIoTClient 11:31ebaeb51e1e 978 {
AzureIoTClient 11:31ebaeb51e1e 979 LogError("Map_AddOrUpdate failed.");
AzureIoTClient 18:ec8e5e97c6a4 980 result = __FAILURE__;
AzureIoTClient 11:31ebaeb51e1e 981 }
AzureIoTClient 11:31ebaeb51e1e 982 }
AzureIoTClient 11:31ebaeb51e1e 983 free(propName);
AzureIoTClient 11:31ebaeb51e1e 984 free(propValue);
AzureIoTClient 11:31ebaeb51e1e 985
AzureIoTClient 11:31ebaeb51e1e 986 break;
AzureIoTClient 11:31ebaeb51e1e 987 }
AzureIoTClient 11:31ebaeb51e1e 988 iterator++;
AzureIoTClient 11:31ebaeb51e1e 989 }
AzureIoTClient 11:31ebaeb51e1e 990 }
AzureIoTClient 11:31ebaeb51e1e 991 }
AzureIoTClient 11:31ebaeb51e1e 992 }
AzureIoTClient 11:31ebaeb51e1e 993 STRING_delete(output);
AzureIoTClient 11:31ebaeb51e1e 994 }
AzureIoTClient 11:31ebaeb51e1e 995 }
AzureIoTClient 11:31ebaeb51e1e 996 STRING_TOKENIZER_destroy(token);
AzureIoTClient 11:31ebaeb51e1e 997 }
AzureIoTClient 11:31ebaeb51e1e 998 else
AzureIoTClient 11:31ebaeb51e1e 999 {
AzureIoTClient 11:31ebaeb51e1e 1000 LogError("Unable to create Tokenizer object.");
AzureIoTClient 18:ec8e5e97c6a4 1001 result = __FAILURE__;
AzureIoTClient 11:31ebaeb51e1e 1002 }
AzureIoTClient 11:31ebaeb51e1e 1003 STRING_delete(mqttTopic);
AzureIoTClient 11:31ebaeb51e1e 1004 }
AzureIoTClient 11:31ebaeb51e1e 1005 return result;
AzureIoTClient 11:31ebaeb51e1e 1006 }
AzureIoTClient 11:31ebaeb51e1e 1007
AzureIoTClient 11:31ebaeb51e1e 1008 static void mqtt_notification_callback(MQTT_MESSAGE_HANDLE msgHandle, void* callbackCtx)
AzureIoTClient 11:31ebaeb51e1e 1009 {
AzureIoTClient 14:4dc2b011be33 1010 /* Tests_SRS_IOTHUB_MQTT_TRANSPORT_07_051: [ If msgHandle or callbackCtx is NULL, mqtt_notification_callback shall do nothing. ] */
AzureIoTClient 11:31ebaeb51e1e 1011 if (msgHandle != NULL && callbackCtx != NULL)
AzureIoTClient 11:31ebaeb51e1e 1012 {
AzureIoTClient 14:4dc2b011be33 1013 /* Tests_SRS_IOTHUB_MQTT_TRANSPORT_07_052: [ mqtt_notification_callback shall extract the topic Name from the MQTT_MESSAGE_HANDLE. ] */
AzureIoTClient 11:31ebaeb51e1e 1014 const char* topic_resp = mqttmessage_getTopicName(msgHandle);
AzureIoTClient 11:31ebaeb51e1e 1015 if (topic_resp == NULL)
AzureIoTClient 11:31ebaeb51e1e 1016 {
AzureIoTClient 11:31ebaeb51e1e 1017 LogError("Failure: NULL topic name encountered");
AzureIoTClient 11:31ebaeb51e1e 1018 }
AzureIoTClient 11:31ebaeb51e1e 1019 else
AzureIoTClient 11:31ebaeb51e1e 1020 {
AzureIoTClient 11:31ebaeb51e1e 1021 PMQTTTRANSPORT_HANDLE_DATA transportData = (PMQTTTRANSPORT_HANDLE_DATA)callbackCtx;
AzureIoTClient 11:31ebaeb51e1e 1022
AzureIoTClient 12:658ca6865de2 1023 IOTHUB_IDENTITY_TYPE type = retrieve_topic_type(topic_resp);
AzureIoTClient 12:658ca6865de2 1024 if (type == IOTHUB_TYPE_DEVICE_TWIN)
AzureIoTClient 11:31ebaeb51e1e 1025 {
AzureIoTClient 12:658ca6865de2 1026 size_t request_id;
AzureIoTClient 12:658ca6865de2 1027 int status_code;
AzureIoTClient 12:658ca6865de2 1028 bool notification_msg;
AzureIoTClient 12:658ca6865de2 1029 if (parse_device_twin_topic_info(topic_resp, &notification_msg, &request_id, &status_code) != 0)
AzureIoTClient 12:658ca6865de2 1030 {
AzureIoTClient 12:658ca6865de2 1031 LogError("Failure: parsing device topic info");
AzureIoTClient 12:658ca6865de2 1032 }
AzureIoTClient 12:658ca6865de2 1033 else
AzureIoTClient 12:658ca6865de2 1034 {
AzureIoTClient 12:658ca6865de2 1035 const APP_PAYLOAD* payload = mqttmessage_getApplicationMsg(msgHandle);
AzureIoTClient 12:658ca6865de2 1036 if (notification_msg)
AzureIoTClient 12:658ca6865de2 1037 {
AzureIoTClient 12:658ca6865de2 1038 IoTHubClient_LL_RetrievePropertyComplete(transportData->llClientHandle, DEVICE_TWIN_UPDATE_PARTIAL, payload->message, payload->length);
AzureIoTClient 12:658ca6865de2 1039 }
AzureIoTClient 12:658ca6865de2 1040 else
AzureIoTClient 12:658ca6865de2 1041 {
AzureIoTClient 12:658ca6865de2 1042 PDLIST_ENTRY dev_twin_item = transportData->ack_waiting_queue.Flink;
AzureIoTClient 12:658ca6865de2 1043 while (dev_twin_item != &transportData->ack_waiting_queue)
AzureIoTClient 12:658ca6865de2 1044 {
AzureIoTClient 12:658ca6865de2 1045 DLIST_ENTRY saveListEntry;
AzureIoTClient 12:658ca6865de2 1046 saveListEntry.Flink = dev_twin_item->Flink;
AzureIoTClient 12:658ca6865de2 1047 MQTT_DEVICE_TWIN_ITEM* msg_entry = containingRecord(dev_twin_item, MQTT_DEVICE_TWIN_ITEM, entry);
AzureIoTClient 12:658ca6865de2 1048 if (request_id == msg_entry->packet_id)
AzureIoTClient 12:658ca6865de2 1049 {
AzureIoTClient 12:658ca6865de2 1050 (void)DList_RemoveEntryList(dev_twin_item);
AzureIoTClient 12:658ca6865de2 1051 if (msg_entry->device_twin_msg_type == RETRIEVE_PROPERTIES)
AzureIoTClient 12:658ca6865de2 1052 {
AzureIoTClient 14:4dc2b011be33 1053 /* Codes_SRS_IOTHUB_MQTT_TRANSPORT_07_054: [ If type is IOTHUB_TYPE_DEVICE_TWIN, then on success if msg_type is RETRIEVE_PROPERTIES then mqtt_notification_callback shall call IoTHubClient_LL_RetrievePropertyComplete... ] */
AzureIoTClient 12:658ca6865de2 1054 IoTHubClient_LL_RetrievePropertyComplete(transportData->llClientHandle, DEVICE_TWIN_UPDATE_COMPLETE, payload->message, payload->length);
AzureIoTClient 12:658ca6865de2 1055 }
AzureIoTClient 12:658ca6865de2 1056 else
AzureIoTClient 12:658ca6865de2 1057 {
AzureIoTClient 14:4dc2b011be33 1058 /* Codes_SRS_IOTHUB_MQTT_TRANSPORT_07_055: [ if device_twin_msg_type is not RETRIEVE_PROPERTIES then mqtt_notification_callback shall call IoTHubClient_LL_ReportedStateComplete ] */
AzureIoTClient 12:658ca6865de2 1059 IoTHubClient_LL_ReportedStateComplete(transportData->llClientHandle, msg_entry->iothub_msg_id, status_code);
AzureIoTClient 12:658ca6865de2 1060 }
AzureIoTClient 12:658ca6865de2 1061 free(msg_entry);
AzureIoTClient 12:658ca6865de2 1062 break;
AzureIoTClient 12:658ca6865de2 1063 }
AzureIoTClient 12:658ca6865de2 1064 dev_twin_item = saveListEntry.Flink;
AzureIoTClient 12:658ca6865de2 1065 }
AzureIoTClient 12:658ca6865de2 1066 }
AzureIoTClient 12:658ca6865de2 1067 }
AzureIoTClient 12:658ca6865de2 1068 }
AzureIoTClient 12:658ca6865de2 1069 else if (type == IOTHUB_TYPE_DEVICE_METHODS)
AzureIoTClient 12:658ca6865de2 1070 {
AzureIoTClient 12:658ca6865de2 1071 STRING_HANDLE method_name = STRING_new();
AzureIoTClient 12:658ca6865de2 1072 if (method_name == NULL)
AzureIoTClient 12:658ca6865de2 1073 {
AzureIoTClient 12:658ca6865de2 1074 LogError("Failure: allocating method_name string value");
AzureIoTClient 12:658ca6865de2 1075 }
AzureIoTClient 12:658ca6865de2 1076 else
AzureIoTClient 12:658ca6865de2 1077 {
AzureIoTClient 14:4dc2b011be33 1078 DEVICE_METHOD_INFO* dev_method_info = malloc(sizeof(DEVICE_METHOD_INFO) );
AzureIoTClient 14:4dc2b011be33 1079 if (dev_method_info == NULL)
AzureIoTClient 12:658ca6865de2 1080 {
AzureIoTClient 14:4dc2b011be33 1081 LogError("Failure: allocating DEVICE_METHOD_INFO object");
AzureIoTClient 12:658ca6865de2 1082 }
AzureIoTClient 12:658ca6865de2 1083 else
AzureIoTClient 12:658ca6865de2 1084 {
AzureIoTClient 14:4dc2b011be33 1085 dev_method_info->request_id = STRING_new();
AzureIoTClient 14:4dc2b011be33 1086 if (dev_method_info->request_id == NULL)
AzureIoTClient 14:4dc2b011be33 1087 {
AzureIoTClient 14:4dc2b011be33 1088 LogError("Failure constructing request_id string");
AzureIoTClient 14:4dc2b011be33 1089 free(dev_method_info);
AzureIoTClient 14:4dc2b011be33 1090 }
AzureIoTClient 14:4dc2b011be33 1091 else if (retrieve_device_method_rid_info(topic_resp, method_name, dev_method_info->request_id) != 0)
AzureIoTClient 12:658ca6865de2 1092 {
AzureIoTClient 14:4dc2b011be33 1093 LogError("Failure: retrieve device topic info");
AzureIoTClient 14:4dc2b011be33 1094 STRING_delete(dev_method_info->request_id);
AzureIoTClient 14:4dc2b011be33 1095 free(dev_method_info);
AzureIoTClient 12:658ca6865de2 1096 }
AzureIoTClient 14:4dc2b011be33 1097 else
AzureIoTClient 14:4dc2b011be33 1098 {
AzureIoTClient 14:4dc2b011be33 1099 /* CodesSRS_IOTHUB_MQTT_TRANSPORT_07_053: [ If type is IOTHUB_TYPE_DEVICE_METHODS, then on success mqtt_notification_callback shall call IoTHubClient_LL_DeviceMethodComplete. ] */
AzureIoTClient 14:4dc2b011be33 1100 const APP_PAYLOAD* payload = mqttmessage_getApplicationMsg(msgHandle);
AzureIoTClient 14:4dc2b011be33 1101 if (IoTHubClient_LL_DeviceMethodComplete(transportData->llClientHandle, STRING_c_str(method_name), payload->message, payload->length, (void*)dev_method_info) != 0)
AzureIoTClient 14:4dc2b011be33 1102 {
AzureIoTClient 14:4dc2b011be33 1103 LogError("Failure: IoTHubClient_LL_DeviceMethodComplete");
AzureIoTClient 14:4dc2b011be33 1104 STRING_delete(dev_method_info->request_id);
AzureIoTClient 14:4dc2b011be33 1105 free(dev_method_info);
AzureIoTClient 14:4dc2b011be33 1106 }
AzureIoTClient 14:4dc2b011be33 1107 }
AzureIoTClient 12:658ca6865de2 1108 }
AzureIoTClient 12:658ca6865de2 1109 STRING_delete(method_name);
AzureIoTClient 12:658ca6865de2 1110 }
AzureIoTClient 11:31ebaeb51e1e 1111 }
AzureIoTClient 11:31ebaeb51e1e 1112 else
AzureIoTClient 11:31ebaeb51e1e 1113 {
AzureIoTClient 12:658ca6865de2 1114 const APP_PAYLOAD* appPayload = mqttmessage_getApplicationMsg(msgHandle);
AzureIoTClient 12:658ca6865de2 1115 IOTHUB_MESSAGE_HANDLE IoTHubMessage = IoTHubMessage_CreateFromByteArray(appPayload->message, appPayload->length);
AzureIoTClient 12:658ca6865de2 1116 if (IoTHubMessage == NULL)
AzureIoTClient 11:31ebaeb51e1e 1117 {
AzureIoTClient 12:658ca6865de2 1118 LogError("Failure: IotHub Message creation has failed.");
AzureIoTClient 11:31ebaeb51e1e 1119 }
AzureIoTClient 11:31ebaeb51e1e 1120 else
AzureIoTClient 11:31ebaeb51e1e 1121 {
AzureIoTClient 12:658ca6865de2 1122 // Will need to update this when the service has messages that can be rejected
AzureIoTClient 12:658ca6865de2 1123 if (extractMqttProperties(IoTHubMessage, topic_resp) != 0)
AzureIoTClient 12:658ca6865de2 1124 {
AzureIoTClient 12:658ca6865de2 1125 LogError("failure extracting mqtt properties.");
AzureIoTClient 12:658ca6865de2 1126 }
AzureIoTClient 12:658ca6865de2 1127 else
AzureIoTClient 11:31ebaeb51e1e 1128 {
AzureIoTClient 19:f87dfe76bc70 1129 MESSAGE_CALLBACK_INFO* messageData = (MESSAGE_CALLBACK_INFO*)malloc(sizeof(MESSAGE_CALLBACK_INFO));
AzureIoTClient 19:f87dfe76bc70 1130 if (messageData == NULL)
AzureIoTClient 19:f87dfe76bc70 1131 {
AzureIoTClient 19:f87dfe76bc70 1132 LogError("malloc failed");
AzureIoTClient 19:f87dfe76bc70 1133 }
AzureIoTClient 19:f87dfe76bc70 1134 else
AzureIoTClient 12:658ca6865de2 1135 {
AzureIoTClient 19:f87dfe76bc70 1136 messageData->messageHandle = IoTHubMessage;
AzureIoTClient 19:f87dfe76bc70 1137 messageData->transportContext = NULL;
AzureIoTClient 19:f87dfe76bc70 1138
AzureIoTClient 19:f87dfe76bc70 1139 /* Codes_SRS_IOTHUB_MQTT_TRANSPORT_07_056: [ If type is IOTHUB_TYPE_TELEMETRY, then on success mqtt_notification_callback shall call IoTHubClient_LL_MessageCallback. ] */
AzureIoTClient 19:f87dfe76bc70 1140 if (!IoTHubClient_LL_MessageCallback(transportData->llClientHandle, messageData))
AzureIoTClient 19:f87dfe76bc70 1141 {
AzureIoTClient 21:167f8fcf6fb1 1142 LogError("IoTHubClient_LL_MessageCallback returned false");
AzureIoTClient 19:f87dfe76bc70 1143
AzureIoTClient 19:f87dfe76bc70 1144 IoTHubMessage_Destroy(IoTHubMessage);
AzureIoTClient 19:f87dfe76bc70 1145 free(messageData);
AzureIoTClient 19:f87dfe76bc70 1146 }
AzureIoTClient 12:658ca6865de2 1147 }
AzureIoTClient 11:31ebaeb51e1e 1148 }
AzureIoTClient 11:31ebaeb51e1e 1149 }
AzureIoTClient 11:31ebaeb51e1e 1150 }
AzureIoTClient 11:31ebaeb51e1e 1151 }
AzureIoTClient 11:31ebaeb51e1e 1152 }
AzureIoTClient 11:31ebaeb51e1e 1153 }
AzureIoTClient 11:31ebaeb51e1e 1154
AzureIoTClient 11:31ebaeb51e1e 1155 static void mqtt_operation_complete_callback(MQTT_CLIENT_HANDLE handle, MQTT_CLIENT_EVENT_RESULT actionResult, const void* msgInfo, void* callbackCtx)
AzureIoTClient 11:31ebaeb51e1e 1156 {
AzureIoTClient 11:31ebaeb51e1e 1157 (void)handle;
AzureIoTClient 11:31ebaeb51e1e 1158 if (callbackCtx != NULL)
AzureIoTClient 11:31ebaeb51e1e 1159 {
AzureIoTClient 11:31ebaeb51e1e 1160 PMQTTTRANSPORT_HANDLE_DATA transport_data = (PMQTTTRANSPORT_HANDLE_DATA)callbackCtx;
AzureIoTClient 11:31ebaeb51e1e 1161
AzureIoTClient 11:31ebaeb51e1e 1162 switch (actionResult)
AzureIoTClient 11:31ebaeb51e1e 1163 {
AzureIoTClient 11:31ebaeb51e1e 1164 case MQTT_CLIENT_ON_PUBLISH_ACK:
AzureIoTClient 11:31ebaeb51e1e 1165 case MQTT_CLIENT_ON_PUBLISH_COMP:
AzureIoTClient 11:31ebaeb51e1e 1166 {
AzureIoTClient 11:31ebaeb51e1e 1167 const PUBLISH_ACK* puback = (const PUBLISH_ACK*)msgInfo;
AzureIoTClient 11:31ebaeb51e1e 1168 if (puback != NULL)
AzureIoTClient 11:31ebaeb51e1e 1169 {
AzureIoTClient 11:31ebaeb51e1e 1170 PDLIST_ENTRY currentListEntry = transport_data->telemetry_waitingForAck.Flink;
AzureIoTClient 11:31ebaeb51e1e 1171 while (currentListEntry != &transport_data->telemetry_waitingForAck)
AzureIoTClient 11:31ebaeb51e1e 1172 {
AzureIoTClient 11:31ebaeb51e1e 1173 MQTT_MESSAGE_DETAILS_LIST* mqttMsgEntry = containingRecord(currentListEntry, MQTT_MESSAGE_DETAILS_LIST, entry);
AzureIoTClient 11:31ebaeb51e1e 1174 DLIST_ENTRY saveListEntry;
AzureIoTClient 11:31ebaeb51e1e 1175 saveListEntry.Flink = currentListEntry->Flink;
AzureIoTClient 11:31ebaeb51e1e 1176
AzureIoTClient 11:31ebaeb51e1e 1177 if (puback->packetId == mqttMsgEntry->packet_id)
AzureIoTClient 11:31ebaeb51e1e 1178 {
AzureIoTClient 11:31ebaeb51e1e 1179 (void)DList_RemoveEntryList(currentListEntry); //First remove the item from Waiting for Ack List.
AzureIoTClient 11:31ebaeb51e1e 1180 sendMsgComplete(mqttMsgEntry->iotHubMessageEntry, transport_data, IOTHUB_CLIENT_CONFIRMATION_OK);
AzureIoTClient 11:31ebaeb51e1e 1181 free(mqttMsgEntry);
AzureIoTClient 11:31ebaeb51e1e 1182 }
AzureIoTClient 11:31ebaeb51e1e 1183 currentListEntry = saveListEntry.Flink;
AzureIoTClient 11:31ebaeb51e1e 1184 }
AzureIoTClient 11:31ebaeb51e1e 1185 }
AzureIoTClient 11:31ebaeb51e1e 1186 else
AzureIoTClient 11:31ebaeb51e1e 1187 {
AzureIoTClient 11:31ebaeb51e1e 1188 LogError("Failure: MQTT_CLIENT_ON_PUBLISH_ACK publish_ack structure NULL.");
AzureIoTClient 11:31ebaeb51e1e 1189 }
AzureIoTClient 11:31ebaeb51e1e 1190 break;
AzureIoTClient 11:31ebaeb51e1e 1191 }
AzureIoTClient 11:31ebaeb51e1e 1192 case MQTT_CLIENT_ON_CONNACK:
AzureIoTClient 11:31ebaeb51e1e 1193 {
AzureIoTClient 11:31ebaeb51e1e 1194 const CONNECT_ACK* connack = (const CONNECT_ACK*)msgInfo;
AzureIoTClient 11:31ebaeb51e1e 1195 if (connack != NULL)
AzureIoTClient 11:31ebaeb51e1e 1196 {
AzureIoTClient 11:31ebaeb51e1e 1197 if (connack->returnCode == CONNECTION_ACCEPTED)
AzureIoTClient 11:31ebaeb51e1e 1198 {
AzureIoTClient 11:31ebaeb51e1e 1199 // The connect packet has been acked
AzureIoTClient 11:31ebaeb51e1e 1200 transport_data->currPacketState = CONNACK_TYPE;
AzureIoTClient 12:658ca6865de2 1201 transport_data->isRecoverableError = true;
AzureIoTClient 20:594780be216d 1202 transport_data->mqttClientStatus = MQTT_CLIENT_STATUS_CONNECTED;
AzureIoTClient 25:a35763780a87 1203
AzureIoTClient 25:a35763780a87 1204 // Codes_SRS_IOTHUB_TRANSPORT_MQTT_COMMON_09_008: [ Upon successful connection the retry control shall be reset using retry_control_reset() ]
AzureIoTClient 25:a35763780a87 1205 retry_control_reset(transport_data->retry_control_handle);
AzureIoTClient 25:a35763780a87 1206
AzureIoTClient 19:f87dfe76bc70 1207 IoTHubClient_LL_ConnectionStatusCallBack(transport_data->llClientHandle, IOTHUB_CLIENT_CONNECTION_AUTHENTICATED, IOTHUB_CLIENT_CONNECTION_OK);
AzureIoTClient 11:31ebaeb51e1e 1208 }
AzureIoTClient 11:31ebaeb51e1e 1209 else
AzureIoTClient 11:31ebaeb51e1e 1210 {
AzureIoTClient 12:658ca6865de2 1211 if (connack->returnCode == CONN_REFUSED_BAD_USERNAME_PASSWORD)
AzureIoTClient 12:658ca6865de2 1212 {
AzureIoTClient 12:658ca6865de2 1213 transport_data->isRecoverableError = false;
AzureIoTClient 19:f87dfe76bc70 1214 IoTHubClient_LL_ConnectionStatusCallBack(transport_data->llClientHandle, IOTHUB_CLIENT_CONNECTION_UNAUTHENTICATED, IOTHUB_CLIENT_CONNECTION_BAD_CREDENTIAL);
AzureIoTClient 12:658ca6865de2 1215 }
AzureIoTClient 12:658ca6865de2 1216 else if (connack->returnCode == CONN_REFUSED_NOT_AUTHORIZED)
AzureIoTClient 12:658ca6865de2 1217 {
AzureIoTClient 19:f87dfe76bc70 1218 IoTHubClient_LL_ConnectionStatusCallBack(transport_data->llClientHandle, IOTHUB_CLIENT_CONNECTION_UNAUTHENTICATED, IOTHUB_CLIENT_CONNECTION_DEVICE_DISABLED);
AzureIoTClient 12:658ca6865de2 1219 }
AzureIoTClient 12:658ca6865de2 1220 else if (connack->returnCode == CONN_REFUSED_UNACCEPTABLE_VERSION)
AzureIoTClient 12:658ca6865de2 1221 {
AzureIoTClient 12:658ca6865de2 1222 transport_data->isRecoverableError = false;
AzureIoTClient 12:658ca6865de2 1223 }
AzureIoTClient 11:31ebaeb51e1e 1224 LogError("Connection Not Accepted: 0x%x: %s", connack->returnCode, retrieve_mqtt_return_codes(connack->returnCode) );
AzureIoTClient 11:31ebaeb51e1e 1225 (void)mqtt_client_disconnect(transport_data->mqttClient);
AzureIoTClient 20:594780be216d 1226 transport_data->mqttClientStatus = MQTT_CLIENT_STATUS_NOT_CONNECTED;
AzureIoTClient 11:31ebaeb51e1e 1227 transport_data->currPacketState = PACKET_TYPE_ERROR;
AzureIoTClient 11:31ebaeb51e1e 1228 }
AzureIoTClient 11:31ebaeb51e1e 1229 }
AzureIoTClient 11:31ebaeb51e1e 1230 else
AzureIoTClient 11:31ebaeb51e1e 1231 {
AzureIoTClient 11:31ebaeb51e1e 1232 LogError("MQTT_CLIENT_ON_CONNACK CONNACK parameter is NULL.");
AzureIoTClient 11:31ebaeb51e1e 1233 }
AzureIoTClient 11:31ebaeb51e1e 1234 break;
AzureIoTClient 11:31ebaeb51e1e 1235 }
AzureIoTClient 11:31ebaeb51e1e 1236 case MQTT_CLIENT_ON_SUBSCRIBE_ACK:
AzureIoTClient 11:31ebaeb51e1e 1237 {
AzureIoTClient 11:31ebaeb51e1e 1238 const SUBSCRIBE_ACK* suback = (const SUBSCRIBE_ACK*)msgInfo;
AzureIoTClient 11:31ebaeb51e1e 1239 if (suback != NULL)
AzureIoTClient 11:31ebaeb51e1e 1240 {
AzureIoTClient 16:14640ee83e99 1241 size_t index = 0;
AzureIoTClient 16:14640ee83e99 1242 for (index = 0; index < suback->qosCount; index++)
AzureIoTClient 11:31ebaeb51e1e 1243 {
AzureIoTClient 11:31ebaeb51e1e 1244 if (suback->qosReturn[index] == DELIVER_FAILURE)
AzureIoTClient 11:31ebaeb51e1e 1245 {
AzureIoTClient 11:31ebaeb51e1e 1246 LogError("Subscribe delivery failure of subscribe %zu", index);
AzureIoTClient 11:31ebaeb51e1e 1247 }
AzureIoTClient 11:31ebaeb51e1e 1248 }
AzureIoTClient 11:31ebaeb51e1e 1249 // The connect packet has been acked
AzureIoTClient 11:31ebaeb51e1e 1250 transport_data->currPacketState = SUBACK_TYPE;
AzureIoTClient 11:31ebaeb51e1e 1251 }
AzureIoTClient 11:31ebaeb51e1e 1252 else
AzureIoTClient 11:31ebaeb51e1e 1253 {
AzureIoTClient 11:31ebaeb51e1e 1254 LogError("Failure: MQTT_CLIENT_ON_SUBSCRIBE_ACK SUBSCRIBE_ACK parameter is NULL.");
AzureIoTClient 11:31ebaeb51e1e 1255 }
AzureIoTClient 11:31ebaeb51e1e 1256 break;
AzureIoTClient 11:31ebaeb51e1e 1257 }
AzureIoTClient 11:31ebaeb51e1e 1258 case MQTT_CLIENT_ON_PUBLISH_RECV:
AzureIoTClient 11:31ebaeb51e1e 1259 case MQTT_CLIENT_ON_PUBLISH_REL:
AzureIoTClient 11:31ebaeb51e1e 1260 {
AzureIoTClient 11:31ebaeb51e1e 1261 // Currently not used
AzureIoTClient 11:31ebaeb51e1e 1262 break;
AzureIoTClient 11:31ebaeb51e1e 1263 }
AzureIoTClient 11:31ebaeb51e1e 1264 case MQTT_CLIENT_ON_DISCONNECT:
AzureIoTClient 11:31ebaeb51e1e 1265 {
AzureIoTClient 11:31ebaeb51e1e 1266 // Close the client so we can reconnect again
AzureIoTClient 20:594780be216d 1267 transport_data->mqttClientStatus = MQTT_CLIENT_STATUS_NOT_CONNECTED;
AzureIoTClient 11:31ebaeb51e1e 1268 transport_data->currPacketState = DISCONNECT_TYPE;
AzureIoTClient 11:31ebaeb51e1e 1269 break;
AzureIoTClient 11:31ebaeb51e1e 1270 }
AzureIoTClient 17:774695cb8554 1271 case MQTT_CLIENT_ON_UNSUBSCRIBE_ACK:
AzureIoTClient 26:0dae84ecce27 1272 case MQTT_CLIENT_ON_PING_RESPONSE:
AzureIoTClient 26:0dae84ecce27 1273 default:
AzureIoTClient 17:774695cb8554 1274 {
AzureIoTClient 17:774695cb8554 1275 break;
AzureIoTClient 26:0dae84ecce27 1276 }
AzureIoTClient 12:658ca6865de2 1277 }
AzureIoTClient 12:658ca6865de2 1278 }
AzureIoTClient 12:658ca6865de2 1279 }
AzureIoTClient 12:658ca6865de2 1280
AzureIoTClient 25:a35763780a87 1281 static void DisconnectFromClient(PMQTTTRANSPORT_HANDLE_DATA transport_data)
AzureIoTClient 25:a35763780a87 1282 {
AzureIoTClient 25:a35763780a87 1283 (void)mqtt_client_disconnect(transport_data->mqttClient);
AzureIoTClient 25:a35763780a87 1284 xio_destroy(transport_data->xioTransport);
AzureIoTClient 25:a35763780a87 1285 transport_data->xioTransport = NULL;
AzureIoTClient 25:a35763780a87 1286
AzureIoTClient 25:a35763780a87 1287 transport_data->mqttClientStatus = MQTT_CLIENT_STATUS_NOT_CONNECTED;
AzureIoTClient 25:a35763780a87 1288 transport_data->currPacketState = DISCONNECT_TYPE;
AzureIoTClient 25:a35763780a87 1289 }
AzureIoTClient 25:a35763780a87 1290
AzureIoTClient 12:658ca6865de2 1291 static void mqtt_error_callback(MQTT_CLIENT_HANDLE handle, MQTT_CLIENT_EVENT_ERROR error, void* callbackCtx)
AzureIoTClient 12:658ca6865de2 1292 {
AzureIoTClient 12:658ca6865de2 1293 (void)handle;
AzureIoTClient 12:658ca6865de2 1294 if (callbackCtx != NULL)
AzureIoTClient 12:658ca6865de2 1295 {
AzureIoTClient 12:658ca6865de2 1296 PMQTTTRANSPORT_HANDLE_DATA transport_data = (PMQTTTRANSPORT_HANDLE_DATA)callbackCtx;
AzureIoTClient 12:658ca6865de2 1297 switch (error)
AzureIoTClient 12:658ca6865de2 1298 {
AzureIoTClient 12:658ca6865de2 1299 case MQTT_CLIENT_CONNECTION_ERROR:
AzureIoTClient 11:31ebaeb51e1e 1300 {
AzureIoTClient 19:f87dfe76bc70 1301 IoTHubClient_LL_ConnectionStatusCallBack(transport_data->llClientHandle, IOTHUB_CLIENT_CONNECTION_UNAUTHENTICATED, IOTHUB_CLIENT_CONNECTION_NO_NETWORK);
AzureIoTClient 12:658ca6865de2 1302 break;
AzureIoTClient 12:658ca6865de2 1303 }
AzureIoTClient 20:594780be216d 1304 case MQTT_CLIENT_COMMUNICATION_ERROR:
AzureIoTClient 20:594780be216d 1305 {
AzureIoTClient 20:594780be216d 1306 IoTHubClient_LL_ConnectionStatusCallBack(transport_data->llClientHandle, IOTHUB_CLIENT_CONNECTION_UNAUTHENTICATED, IOTHUB_CLIENT_CONNECTION_COMMUNICATION_ERROR);
AzureIoTClient 20:594780be216d 1307 break;
AzureIoTClient 20:594780be216d 1308 }
AzureIoTClient 12:658ca6865de2 1309 case MQTT_CLIENT_NO_PING_RESPONSE:
AzureIoTClient 12:658ca6865de2 1310 {
AzureIoTClient 12:658ca6865de2 1311 LogError("Mqtt Ping Response was not encountered. Reconnecting device...");
AzureIoTClient 25:a35763780a87 1312 DisconnectFromClient(transport_data);
AzureIoTClient 12:658ca6865de2 1313 break;
AzureIoTClient 11:31ebaeb51e1e 1314 }
AzureIoTClient 17:774695cb8554 1315 case MQTT_CLIENT_PARSE_ERROR:
AzureIoTClient 17:774695cb8554 1316 case MQTT_CLIENT_MEMORY_ERROR:
AzureIoTClient 17:774695cb8554 1317 case MQTT_CLIENT_UNKNOWN_ERROR:
AzureIoTClient 17:774695cb8554 1318 {
AzureIoTClient 17:774695cb8554 1319 LogError("INTERNAL ERROR: unexpected error value received %s", ENUM_TO_STRING(MQTT_CLIENT_EVENT_ERROR, error));
AzureIoTClient 20:594780be216d 1320 break;
AzureIoTClient 17:774695cb8554 1321 }
AzureIoTClient 11:31ebaeb51e1e 1322 }
AzureIoTClient 20:594780be216d 1323 transport_data->mqttClientStatus = MQTT_CLIENT_STATUS_NOT_CONNECTED;
AzureIoTClient 12:658ca6865de2 1324 transport_data->currPacketState = PACKET_TYPE_ERROR;
Azure.IoT.Build 13:606465879c57 1325 transport_data->device_twin_get_sent = false;
AzureIoTClient 12:658ca6865de2 1326 if (transport_data->topic_MqttMessage != NULL)
AzureIoTClient 12:658ca6865de2 1327 {
Azure.IoT.Build 13:606465879c57 1328 transport_data->topics_ToSubscribe |= SUBSCRIBE_TELEMETRY_TOPIC;
Azure.IoT.Build 13:606465879c57 1329 }
Azure.IoT.Build 13:606465879c57 1330 if (transport_data->topic_GetState != NULL)
Azure.IoT.Build 13:606465879c57 1331 {
Azure.IoT.Build 13:606465879c57 1332 transport_data->topics_ToSubscribe |= SUBSCRIBE_GET_REPORTED_STATE_TOPIC;
AzureIoTClient 12:658ca6865de2 1333 }
Azure.IoT.Build 13:606465879c57 1334 if (transport_data->topic_NotifyState != NULL)
Azure.IoT.Build 13:606465879c57 1335 {
Azure.IoT.Build 13:606465879c57 1336 transport_data->topics_ToSubscribe |= SUBSCRIBE_NOTIFICATION_STATE_TOPIC;
Azure.IoT.Build 13:606465879c57 1337 }
Azure.IoT.Build 13:606465879c57 1338 if (transport_data->topic_DeviceMethods != NULL)
Azure.IoT.Build 13:606465879c57 1339 {
Azure.IoT.Build 13:606465879c57 1340 transport_data->topics_ToSubscribe |= SUBSCRIBE_DEVICE_METHOD_TOPIC;
Azure.IoT.Build 13:606465879c57 1341 }
Azure.IoT.Build 13:606465879c57 1342 }
AzureIoTClient 12:658ca6865de2 1343 else
AzureIoTClient 12:658ca6865de2 1344 {
AzureIoTClient 12:658ca6865de2 1345 LogError("Failure: mqtt called back with null context.");
AzureIoTClient 11:31ebaeb51e1e 1346 }
AzureIoTClient 11:31ebaeb51e1e 1347 }
AzureIoTClient 11:31ebaeb51e1e 1348
AzureIoTClient 11:31ebaeb51e1e 1349 static void SubscribeToMqttProtocol(PMQTTTRANSPORT_HANDLE_DATA transport_data)
AzureIoTClient 11:31ebaeb51e1e 1350 {
AzureIoTClient 11:31ebaeb51e1e 1351 if (transport_data->topics_ToSubscribe != UNSUBSCRIBE_FROM_TOPIC)
AzureIoTClient 11:31ebaeb51e1e 1352 {
AzureIoTClient 11:31ebaeb51e1e 1353 uint32_t topic_subscription = 0;
AzureIoTClient 11:31ebaeb51e1e 1354 size_t subscribe_count = 0;
AzureIoTClient 11:31ebaeb51e1e 1355 SUBSCRIBE_PAYLOAD subscribe[SUBSCRIBE_TOPIC_COUNT];
AzureIoTClient 11:31ebaeb51e1e 1356 if ((transport_data->topic_MqttMessage != NULL) && (SUBSCRIBE_TELEMETRY_TOPIC & transport_data->topics_ToSubscribe))
AzureIoTClient 11:31ebaeb51e1e 1357 {
AzureIoTClient 11:31ebaeb51e1e 1358 subscribe[subscribe_count].subscribeTopic = STRING_c_str(transport_data->topic_MqttMessage);
AzureIoTClient 11:31ebaeb51e1e 1359 subscribe[subscribe_count].qosReturn = DELIVER_AT_LEAST_ONCE;
AzureIoTClient 11:31ebaeb51e1e 1360 topic_subscription |= SUBSCRIBE_TELEMETRY_TOPIC;
AzureIoTClient 11:31ebaeb51e1e 1361 subscribe_count++;
AzureIoTClient 11:31ebaeb51e1e 1362 }
AzureIoTClient 12:658ca6865de2 1363 if ((transport_data->topic_GetState != NULL) && (SUBSCRIBE_GET_REPORTED_STATE_TOPIC & transport_data->topics_ToSubscribe))
AzureIoTClient 12:658ca6865de2 1364 {
AzureIoTClient 12:658ca6865de2 1365 subscribe[subscribe_count].subscribeTopic = STRING_c_str(transport_data->topic_GetState);
AzureIoTClient 12:658ca6865de2 1366 subscribe[subscribe_count].qosReturn = DELIVER_AT_MOST_ONCE;
AzureIoTClient 12:658ca6865de2 1367 topic_subscription |= SUBSCRIBE_GET_REPORTED_STATE_TOPIC;
AzureIoTClient 12:658ca6865de2 1368 subscribe_count++;
AzureIoTClient 12:658ca6865de2 1369 }
AzureIoTClient 12:658ca6865de2 1370 if ((transport_data->topic_NotifyState != NULL) && (SUBSCRIBE_NOTIFICATION_STATE_TOPIC & transport_data->topics_ToSubscribe))
AzureIoTClient 12:658ca6865de2 1371 {
AzureIoTClient 12:658ca6865de2 1372 subscribe[subscribe_count].subscribeTopic = STRING_c_str(transport_data->topic_NotifyState);
AzureIoTClient 12:658ca6865de2 1373 subscribe[subscribe_count].qosReturn = DELIVER_AT_MOST_ONCE;
AzureIoTClient 12:658ca6865de2 1374 topic_subscription |= SUBSCRIBE_NOTIFICATION_STATE_TOPIC;
AzureIoTClient 12:658ca6865de2 1375 subscribe_count++;
AzureIoTClient 12:658ca6865de2 1376 }
AzureIoTClient 12:658ca6865de2 1377 if ((transport_data->topic_DeviceMethods != NULL) && (SUBSCRIBE_DEVICE_METHOD_TOPIC & transport_data->topics_ToSubscribe))
AzureIoTClient 12:658ca6865de2 1378 {
AzureIoTClient 12:658ca6865de2 1379 subscribe[subscribe_count].subscribeTopic = STRING_c_str(transport_data->topic_DeviceMethods);
AzureIoTClient 12:658ca6865de2 1380 subscribe[subscribe_count].qosReturn = DELIVER_AT_MOST_ONCE;
AzureIoTClient 12:658ca6865de2 1381 topic_subscription |= SUBSCRIBE_DEVICE_METHOD_TOPIC;
AzureIoTClient 12:658ca6865de2 1382 subscribe_count++;
AzureIoTClient 12:658ca6865de2 1383 }
AzureIoTClient 11:31ebaeb51e1e 1384
AzureIoTClient 11:31ebaeb51e1e 1385 if (subscribe_count != 0)
AzureIoTClient 11:31ebaeb51e1e 1386 {
AzureIoTClient 12:658ca6865de2 1387 /* Codes_SRS_IOTHUB_MQTT_TRANSPORT_07_016: [IoTHubTransport_MQTT_Common_Subscribe shall call mqtt_client_subscribe to subscribe to the Message Topic.] */
AzureIoTClient 11:31ebaeb51e1e 1388 if (mqtt_client_subscribe(transport_data->mqttClient, get_next_packet_id(transport_data), subscribe, subscribe_count) != 0)
AzureIoTClient 11:31ebaeb51e1e 1389 {
AzureIoTClient 12:658ca6865de2 1390 /* Codes_SRS_IOTHUB_MQTT_TRANSPORT_07_017: [Upon failure IoTHubTransport_MQTT_Common_Subscribe shall return a non-zero value.] */
AzureIoTClient 11:31ebaeb51e1e 1391 LogError("Failure: mqtt_client_subscribe returned error.");
AzureIoTClient 11:31ebaeb51e1e 1392 }
AzureIoTClient 11:31ebaeb51e1e 1393 else
AzureIoTClient 11:31ebaeb51e1e 1394 {
AzureIoTClient 12:658ca6865de2 1395 /* Codes_SRS_IOTHUB_MQTT_TRANSPORT_07_018: [On success IoTHubTransport_MQTT_Common_Subscribe shall return 0.] */
AzureIoTClient 11:31ebaeb51e1e 1396 transport_data->topics_ToSubscribe &= ~topic_subscription;
AzureIoTClient 11:31ebaeb51e1e 1397 transport_data->currPacketState = SUBSCRIBE_TYPE;
AzureIoTClient 11:31ebaeb51e1e 1398 }
AzureIoTClient 11:31ebaeb51e1e 1399 }
AzureIoTClient 11:31ebaeb51e1e 1400 else
AzureIoTClient 11:31ebaeb51e1e 1401 {
AzureIoTClient 12:658ca6865de2 1402 /* Codes_SRS_IOTHUB_MQTT_TRANSPORT_07_017: [Upon failure IoTHubTransport_MQTT_Common_Subscribe shall return a non-zero value.] */
AzureIoTClient 11:31ebaeb51e1e 1403 LogError("Failure: subscribe_topic is empty.");
AzureIoTClient 11:31ebaeb51e1e 1404 }
AzureIoTClient 11:31ebaeb51e1e 1405 transport_data->currPacketState = SUBSCRIBE_TYPE;
AzureIoTClient 11:31ebaeb51e1e 1406 }
AzureIoTClient 11:31ebaeb51e1e 1407 else
AzureIoTClient 11:31ebaeb51e1e 1408 {
AzureIoTClient 11:31ebaeb51e1e 1409 transport_data->currPacketState = PUBLISH_TYPE;
AzureIoTClient 11:31ebaeb51e1e 1410 }
AzureIoTClient 11:31ebaeb51e1e 1411 }
AzureIoTClient 11:31ebaeb51e1e 1412
AzureIoTClient 11:31ebaeb51e1e 1413 static const unsigned char* RetrieveMessagePayload(IOTHUB_MESSAGE_HANDLE messageHandle, size_t* length)
AzureIoTClient 11:31ebaeb51e1e 1414 {
AzureIoTClient 11:31ebaeb51e1e 1415 const unsigned char* result;
AzureIoTClient 11:31ebaeb51e1e 1416
AzureIoTClient 11:31ebaeb51e1e 1417 IOTHUBMESSAGE_CONTENT_TYPE contentType = IoTHubMessage_GetContentType(messageHandle);
AzureIoTClient 11:31ebaeb51e1e 1418 if (contentType == IOTHUBMESSAGE_BYTEARRAY)
AzureIoTClient 11:31ebaeb51e1e 1419 {
AzureIoTClient 11:31ebaeb51e1e 1420 if (IoTHubMessage_GetByteArray(messageHandle, &result, length) != IOTHUB_MESSAGE_OK)
AzureIoTClient 11:31ebaeb51e1e 1421 {
AzureIoTClient 11:31ebaeb51e1e 1422 LogError("Failure result from IoTHubMessage_GetByteArray");
AzureIoTClient 11:31ebaeb51e1e 1423 result = NULL;
AzureIoTClient 11:31ebaeb51e1e 1424 *length = 0;
AzureIoTClient 11:31ebaeb51e1e 1425 }
AzureIoTClient 11:31ebaeb51e1e 1426 }
AzureIoTClient 11:31ebaeb51e1e 1427 else if (contentType == IOTHUBMESSAGE_STRING)
AzureIoTClient 11:31ebaeb51e1e 1428 {
AzureIoTClient 11:31ebaeb51e1e 1429 result = (const unsigned char*)IoTHubMessage_GetString(messageHandle);
AzureIoTClient 11:31ebaeb51e1e 1430 if (result == NULL)
AzureIoTClient 11:31ebaeb51e1e 1431 {
AzureIoTClient 11:31ebaeb51e1e 1432 LogError("Failure result from IoTHubMessage_GetString");
AzureIoTClient 11:31ebaeb51e1e 1433 result = NULL;
AzureIoTClient 11:31ebaeb51e1e 1434 *length = 0;
AzureIoTClient 11:31ebaeb51e1e 1435 }
AzureIoTClient 11:31ebaeb51e1e 1436 else
AzureIoTClient 11:31ebaeb51e1e 1437 {
AzureIoTClient 11:31ebaeb51e1e 1438 *length = strlen((const char*)result);
AzureIoTClient 11:31ebaeb51e1e 1439 }
AzureIoTClient 11:31ebaeb51e1e 1440 }
AzureIoTClient 11:31ebaeb51e1e 1441 else
AzureIoTClient 11:31ebaeb51e1e 1442 {
AzureIoTClient 11:31ebaeb51e1e 1443 result = NULL;
AzureIoTClient 11:31ebaeb51e1e 1444 *length = 0;
AzureIoTClient 11:31ebaeb51e1e 1445 }
AzureIoTClient 11:31ebaeb51e1e 1446 return result;
AzureIoTClient 11:31ebaeb51e1e 1447 }
AzureIoTClient 11:31ebaeb51e1e 1448
AzureIoTClient 11:31ebaeb51e1e 1449 static int GetTransportProviderIfNecessary(PMQTTTRANSPORT_HANDLE_DATA transport_data)
AzureIoTClient 11:31ebaeb51e1e 1450 {
AzureIoTClient 11:31ebaeb51e1e 1451 int result;
AzureIoTClient 11:31ebaeb51e1e 1452
AzureIoTClient 11:31ebaeb51e1e 1453 if (transport_data->xioTransport == NULL)
AzureIoTClient 11:31ebaeb51e1e 1454 {
AzureIoTClient 11:31ebaeb51e1e 1455 // construct address
AzureIoTClient 11:31ebaeb51e1e 1456 const char* hostAddress = STRING_c_str(transport_data->hostAddress);
AzureIoTClient 20:594780be216d 1457 MQTT_TRANSPORT_PROXY_OPTIONS mqtt_proxy_options;
AzureIoTClient 20:594780be216d 1458
AzureIoTClient 20:594780be216d 1459 /* Codes_SRS_IOTHUB_TRANSPORT_MQTT_COMMON_01_011: [ If no `proxy_data` option has been set, NULL shall be passed as the argument `mqtt_transport_proxy_options` when calling the function `get_io_transport` passed in `IoTHubTransport_MQTT_Common__Create`. ]*/
AzureIoTClient 20:594780be216d 1460 mqtt_proxy_options.host_address = transport_data->http_proxy_hostname;
AzureIoTClient 20:594780be216d 1461 mqtt_proxy_options.port = transport_data->http_proxy_port;
AzureIoTClient 20:594780be216d 1462 mqtt_proxy_options.username = transport_data->http_proxy_username;
AzureIoTClient 20:594780be216d 1463 mqtt_proxy_options.password = transport_data->http_proxy_password;
AzureIoTClient 20:594780be216d 1464
AzureIoTClient 20:594780be216d 1465 /* Codes_SRS_IOTHUB_TRANSPORT_MQTT_COMMON_01_010: [ If the `proxy_data` option has been set, the proxy options shall be filled in the argument `mqtt_transport_proxy_options` when calling the function `get_io_transport` passed in `IoTHubTransport_MQTT_Common__Create` to obtain the underlying IO handle. ]*/
AzureIoTClient 20:594780be216d 1466 transport_data->xioTransport = transport_data->get_io_transport(hostAddress, (transport_data->http_proxy_hostname == NULL) ? NULL : &mqtt_proxy_options);
AzureIoTClient 11:31ebaeb51e1e 1467 if (transport_data->xioTransport == NULL)
AzureIoTClient 11:31ebaeb51e1e 1468 {
AzureIoTClient 11:31ebaeb51e1e 1469 LogError("Unable to create the lower level TLS layer.");
AzureIoTClient 18:ec8e5e97c6a4 1470 result = __FAILURE__;
AzureIoTClient 11:31ebaeb51e1e 1471 }
AzureIoTClient 11:31ebaeb51e1e 1472 else
AzureIoTClient 11:31ebaeb51e1e 1473 {
AzureIoTClient 11:31ebaeb51e1e 1474 result = 0;
AzureIoTClient 11:31ebaeb51e1e 1475 }
AzureIoTClient 11:31ebaeb51e1e 1476 }
AzureIoTClient 11:31ebaeb51e1e 1477 else
AzureIoTClient 11:31ebaeb51e1e 1478 {
AzureIoTClient 11:31ebaeb51e1e 1479 result = 0;
AzureIoTClient 11:31ebaeb51e1e 1480 }
AzureIoTClient 11:31ebaeb51e1e 1481 return result;
AzureIoTClient 11:31ebaeb51e1e 1482 }
AzureIoTClient 11:31ebaeb51e1e 1483
AzureIoTClient 11:31ebaeb51e1e 1484 static int SendMqttConnectMsg(PMQTTTRANSPORT_HANDLE_DATA transport_data)
AzureIoTClient 11:31ebaeb51e1e 1485 {
AzureIoTClient 11:31ebaeb51e1e 1486 int result;
AzureIoTClient 11:31ebaeb51e1e 1487
AzureIoTClient 21:167f8fcf6fb1 1488 char* sasToken = NULL;
AzureIoTClient 21:167f8fcf6fb1 1489 result = 0;
AzureIoTClient 21:167f8fcf6fb1 1490
AzureIoTClient 21:167f8fcf6fb1 1491 IOTHUB_CREDENTIAL_TYPE cred_type = IoTHubClient_Auth_Get_Credential_Type(transport_data->authorization_module);
AzureIoTClient 21:167f8fcf6fb1 1492 if (cred_type == IOTHUB_CREDENTIAL_TYPE_DEVICE_KEY)
AzureIoTClient 11:31ebaeb51e1e 1493 {
AzureIoTClient 21:167f8fcf6fb1 1494 size_t secSinceEpoch = (size_t)(difftime(get_time(NULL), EPOCH_TIME_T_VALUE) + 0);
AzureIoTClient 21:167f8fcf6fb1 1495 size_t expiryTime = secSinceEpoch + SAS_TOKEN_DEFAULT_LIFETIME;
AzureIoTClient 21:167f8fcf6fb1 1496 sasToken = IoTHubClient_Auth_Get_SasToken(transport_data->authorization_module, STRING_c_str(transport_data->devicesPath), expiryTime);
AzureIoTClient 21:167f8fcf6fb1 1497 if (sasToken == NULL)
AzureIoTClient 21:167f8fcf6fb1 1498 {
AzureIoTClient 21:167f8fcf6fb1 1499 LogError("failure getting sas Token.");
AzureIoTClient 21:167f8fcf6fb1 1500 result = __FAILURE__;
AzureIoTClient 21:167f8fcf6fb1 1501 }
AzureIoTClient 11:31ebaeb51e1e 1502 }
AzureIoTClient 21:167f8fcf6fb1 1503 else if (cred_type == IOTHUB_CREDENTIAL_TYPE_SAS_TOKEN)
AzureIoTClient 11:31ebaeb51e1e 1504 {
AzureIoTClient 21:167f8fcf6fb1 1505 SAS_TOKEN_STATUS token_status = IoTHubClient_Auth_Is_SasToken_Valid(transport_data->authorization_module);
AzureIoTClient 21:167f8fcf6fb1 1506 if (token_status == SAS_TOKEN_STATUS_INVALID)
AzureIoTClient 21:167f8fcf6fb1 1507 {
AzureIoTClient 21:167f8fcf6fb1 1508 IoTHubClient_LL_ConnectionStatusCallBack(transport_data->llClientHandle, IOTHUB_CLIENT_CONNECTION_UNAUTHENTICATED, IOTHUB_CLIENT_CONNECTION_EXPIRED_SAS_TOKEN);
AzureIoTClient 21:167f8fcf6fb1 1509 result = __FAILURE__;
AzureIoTClient 21:167f8fcf6fb1 1510 }
AzureIoTClient 21:167f8fcf6fb1 1511 else if (token_status == SAS_TOKEN_STATUS_FAILED)
AzureIoTClient 11:31ebaeb51e1e 1512 {
AzureIoTClient 21:167f8fcf6fb1 1513 IoTHubClient_LL_ConnectionStatusCallBack(transport_data->llClientHandle, IOTHUB_CLIENT_CONNECTION_UNAUTHENTICATED, IOTHUB_CLIENT_CONNECTION_BAD_CREDENTIAL);
AzureIoTClient 21:167f8fcf6fb1 1514 result = __FAILURE__;
AzureIoTClient 21:167f8fcf6fb1 1515 }
AzureIoTClient 21:167f8fcf6fb1 1516 else
AzureIoTClient 21:167f8fcf6fb1 1517 {
AzureIoTClient 21:167f8fcf6fb1 1518 sasToken = IoTHubClient_Auth_Get_SasToken(transport_data->authorization_module, NULL, 0);
AzureIoTClient 21:167f8fcf6fb1 1519 if (sasToken == NULL)
AzureIoTClient 11:31ebaeb51e1e 1520 {
AzureIoTClient 21:167f8fcf6fb1 1521 LogError("failure getting sas Token.");
AzureIoTClient 18:ec8e5e97c6a4 1522 result = __FAILURE__;
AzureIoTClient 12:658ca6865de2 1523 }
AzureIoTClient 21:167f8fcf6fb1 1524 }
AzureIoTClient 21:167f8fcf6fb1 1525 }
AzureIoTClient 11:31ebaeb51e1e 1526
AzureIoTClient 21:167f8fcf6fb1 1527 if (result == 0)
AzureIoTClient 21:167f8fcf6fb1 1528 {
AzureIoTClient 24:4096249decf1 1529 if (!transport_data->isProductInfoSet)
AzureIoTClient 22:07fec4d325b6 1530 {
AzureIoTClient 24:4096249decf1 1531 // This requires the iothubClientHandle, which sadly the MQTT transport only gets on DoWork, so this code still needs to remain here.
AzureIoTClient 24:4096249decf1 1532 // The correct place for this would be in the Create method, but we don't get the client handle there.
AzureIoTClient 24:4096249decf1 1533 // Also, when device multiplexing is used, the customer creates the transport directly and explicitly, when the client is still not created.
AzureIoTClient 24:4096249decf1 1534 // This will be a major hurdle when we add device multiplexing to MQTT transport.
AzureIoTClient 24:4096249decf1 1535
AzureIoTClient 24:4096249decf1 1536 void* product_info;
AzureIoTClient 24:4096249decf1 1537 STRING_HANDLE clone;
AzureIoTClient 24:4096249decf1 1538 if ((IoTHubClient_LL_GetOption(transport_data->llClientHandle, OPTION_PRODUCT_INFO, &product_info) == IOTHUB_CLIENT_ERROR) || (product_info == NULL))
AzureIoTClient 24:4096249decf1 1539 {
AzureIoTClient 24:4096249decf1 1540 clone = STRING_construct_sprintf("%s%%2F%s", CLIENT_DEVICE_TYPE_PREFIX, IOTHUB_SDK_VERSION);
AzureIoTClient 24:4096249decf1 1541 }
AzureIoTClient 24:4096249decf1 1542 else
AzureIoTClient 24:4096249decf1 1543 {
AzureIoTClient 24:4096249decf1 1544 clone = URL_Encode(product_info);
AzureIoTClient 24:4096249decf1 1545 }
AzureIoTClient 24:4096249decf1 1546
AzureIoTClient 24:4096249decf1 1547 if (clone == NULL)
AzureIoTClient 24:4096249decf1 1548 {
AzureIoTClient 24:4096249decf1 1549 LogError("Failed obtaining the product info");
AzureIoTClient 24:4096249decf1 1550 }
AzureIoTClient 24:4096249decf1 1551 else
AzureIoTClient 24:4096249decf1 1552 {
AzureIoTClient 24:4096249decf1 1553 if (STRING_concat_with_STRING(transport_data->configPassedThroughUsername, clone) != 0)
AzureIoTClient 24:4096249decf1 1554 {
AzureIoTClient 24:4096249decf1 1555 LogError("Failed concatenating the product info");
AzureIoTClient 24:4096249decf1 1556 }
AzureIoTClient 24:4096249decf1 1557 else
AzureIoTClient 24:4096249decf1 1558 {
AzureIoTClient 24:4096249decf1 1559 transport_data->isProductInfoSet = true;
AzureIoTClient 24:4096249decf1 1560 }
AzureIoTClient 24:4096249decf1 1561
AzureIoTClient 24:4096249decf1 1562 STRING_delete(clone);
AzureIoTClient 24:4096249decf1 1563 }
AzureIoTClient 22:07fec4d325b6 1564 }
AzureIoTClient 22:07fec4d325b6 1565
AzureIoTClient 21:167f8fcf6fb1 1566 MQTT_CLIENT_OPTIONS options = { 0 };
AzureIoTClient 21:167f8fcf6fb1 1567 options.clientId = (char*)STRING_c_str(transport_data->device_id);
AzureIoTClient 21:167f8fcf6fb1 1568 options.willMessage = NULL;
AzureIoTClient 21:167f8fcf6fb1 1569 options.username = (char*)STRING_c_str(transport_data->configPassedThroughUsername);
AzureIoTClient 21:167f8fcf6fb1 1570 if (sasToken != NULL)
AzureIoTClient 11:31ebaeb51e1e 1571 {
AzureIoTClient 21:167f8fcf6fb1 1572 options.password = sasToken;
AzureIoTClient 21:167f8fcf6fb1 1573 }
AzureIoTClient 21:167f8fcf6fb1 1574 options.keepAliveInterval = transport_data->keepAliveValue;
AzureIoTClient 21:167f8fcf6fb1 1575 options.useCleanSession = false;
AzureIoTClient 21:167f8fcf6fb1 1576 options.qualityOfServiceValue = DELIVER_AT_LEAST_ONCE;
AzureIoTClient 11:31ebaeb51e1e 1577
AzureIoTClient 21:167f8fcf6fb1 1578 if (GetTransportProviderIfNecessary(transport_data) == 0)
AzureIoTClient 21:167f8fcf6fb1 1579 {
AzureIoTClient 21:167f8fcf6fb1 1580 if (mqtt_client_connect(transport_data->mqttClient, transport_data->xioTransport, &options) != 0)
AzureIoTClient 11:31ebaeb51e1e 1581 {
AzureIoTClient 24:4096249decf1 1582 LogError("failure connecting to address %s.", STRING_c_str(transport_data->hostAddress));
AzureIoTClient 21:167f8fcf6fb1 1583 result = __FAILURE__;
AzureIoTClient 11:31ebaeb51e1e 1584 }
AzureIoTClient 11:31ebaeb51e1e 1585 else
AzureIoTClient 11:31ebaeb51e1e 1586 {
AzureIoTClient 21:167f8fcf6fb1 1587 (void)tickcounter_get_current_ms(transport_data->msgTickCounter, &transport_data->mqtt_connect_time);
AzureIoTClient 21:167f8fcf6fb1 1588 result = 0;
AzureIoTClient 11:31ebaeb51e1e 1589 }
AzureIoTClient 21:167f8fcf6fb1 1590 }
AzureIoTClient 21:167f8fcf6fb1 1591 else
AzureIoTClient 21:167f8fcf6fb1 1592 {
AzureIoTClient 21:167f8fcf6fb1 1593 result = __FAILURE__;
AzureIoTClient 21:167f8fcf6fb1 1594 }
AzureIoTClient 12:658ca6865de2 1595
AzureIoTClient 21:167f8fcf6fb1 1596 if (sasToken != NULL)
AzureIoTClient 21:167f8fcf6fb1 1597 {
AzureIoTClient 21:167f8fcf6fb1 1598 free(sasToken);
AzureIoTClient 11:31ebaeb51e1e 1599 }
AzureIoTClient 11:31ebaeb51e1e 1600 }
AzureIoTClient 11:31ebaeb51e1e 1601 return result;
AzureIoTClient 11:31ebaeb51e1e 1602 }
AzureIoTClient 11:31ebaeb51e1e 1603
AzureIoTClient 11:31ebaeb51e1e 1604 static int InitializeConnection(PMQTTTRANSPORT_HANDLE_DATA transport_data)
AzureIoTClient 11:31ebaeb51e1e 1605 {
AzureIoTClient 11:31ebaeb51e1e 1606 int result = 0;
AzureIoTClient 11:31ebaeb51e1e 1607
AzureIoTClient 11:31ebaeb51e1e 1608 // Make sure we're not destroying the object
AzureIoTClient 11:31ebaeb51e1e 1609 if (!transport_data->isDestroyCalled)
AzureIoTClient 11:31ebaeb51e1e 1610 {
AzureIoTClient 25:a35763780a87 1611 RETRY_ACTION retry_action = RETRY_ACTION_RETRY_LATER;
AzureIoTClient 25:a35763780a87 1612
AzureIoTClient 25:a35763780a87 1613 // Codes_SRS_IOTHUB_TRANSPORT_MQTT_COMMON_09_007: [ IoTHubTransport_MQTT_Common_DoWork shall try to reconnect according to the current retry policy set ]
AzureIoTClient 25:a35763780a87 1614 if (transport_data->mqttClientStatus == MQTT_CLIENT_STATUS_NOT_CONNECTED && transport_data->isRecoverableError &&
AzureIoTClient 25:a35763780a87 1615 (retry_control_should_retry(transport_data->retry_control_handle, &retry_action) != 0 || retry_action == RETRY_ACTION_RETRY_NOW))
AzureIoTClient 11:31ebaeb51e1e 1616 {
AzureIoTClient 25:a35763780a87 1617 // Note: in case retry_control_should_retry fails, the reconnection shall be attempted anyway (defaulting to policy IOTHUB_CLIENT_RETRY_IMMEDIATE).
AzureIoTClient 25:a35763780a87 1618
Azure.IoT.Build 13:606465879c57 1619 if (tickcounter_get_current_ms(transport_data->msgTickCounter, &transport_data->connectTick) != 0)
AzureIoTClient 11:31ebaeb51e1e 1620 {
AzureIoTClient 12:658ca6865de2 1621 transport_data->connectFailCount++;
AzureIoTClient 18:ec8e5e97c6a4 1622 result = __FAILURE__;
AzureIoTClient 11:31ebaeb51e1e 1623 }
AzureIoTClient 12:658ca6865de2 1624 else
AzureIoTClient 11:31ebaeb51e1e 1625 {
AzureIoTClient 12:658ca6865de2 1626 if (SendMqttConnectMsg(transport_data) != 0)
AzureIoTClient 11:31ebaeb51e1e 1627 {
AzureIoTClient 11:31ebaeb51e1e 1628 transport_data->connectFailCount++;
AzureIoTClient 18:ec8e5e97c6a4 1629 result = __FAILURE__;
AzureIoTClient 11:31ebaeb51e1e 1630 }
AzureIoTClient 11:31ebaeb51e1e 1631 else
AzureIoTClient 11:31ebaeb51e1e 1632 {
AzureIoTClient 20:594780be216d 1633 transport_data->mqttClientStatus = MQTT_CLIENT_STATUS_CONNECTING;
AzureIoTClient 12:658ca6865de2 1634 transport_data->connectFailCount = 0;
AzureIoTClient 12:658ca6865de2 1635 result = 0;
AzureIoTClient 11:31ebaeb51e1e 1636 }
AzureIoTClient 11:31ebaeb51e1e 1637 }
AzureIoTClient 11:31ebaeb51e1e 1638 }
AzureIoTClient 21:167f8fcf6fb1 1639 // Codes_SRS_IOTHUB_TRANSPORT_MQTT_COMMON_09_001: [ IoTHubTransport_MQTT_Common_DoWork shall trigger reconnection if the mqtt_client_connect does not complete within `keepalive` seconds]
AzureIoTClient 20:594780be216d 1640 else if (transport_data->mqttClientStatus == MQTT_CLIENT_STATUS_CONNECTING)
AzureIoTClient 11:31ebaeb51e1e 1641 {
AzureIoTClient 20:594780be216d 1642 tickcounter_ms_t current_time;
AzureIoTClient 20:594780be216d 1643 if (tickcounter_get_current_ms(transport_data->msgTickCounter, &current_time) != 0)
AzureIoTClient 20:594780be216d 1644 {
AzureIoTClient 20:594780be216d 1645 LogError("failed verifying MQTT_CLIENT_STATUS_CONNECTING timeout");
AzureIoTClient 20:594780be216d 1646 result = __FAILURE__;
AzureIoTClient 20:594780be216d 1647 }
AzureIoTClient 20:594780be216d 1648 else if ((current_time - transport_data->mqtt_connect_time) / 1000 > transport_data->keepAliveValue)
AzureIoTClient 20:594780be216d 1649 {
AzureIoTClient 20:594780be216d 1650 LogError("mqtt_client timed out waiting for CONNACK");
AzureIoTClient 20:594780be216d 1651 DisconnectFromClient(transport_data);
AzureIoTClient 20:594780be216d 1652 result = 0;
AzureIoTClient 20:594780be216d 1653 }
AzureIoTClient 20:594780be216d 1654 }
AzureIoTClient 20:594780be216d 1655 else if (transport_data->mqttClientStatus == MQTT_CLIENT_STATUS_CONNECTED)
AzureIoTClient 20:594780be216d 1656 {
AzureIoTClient 20:594780be216d 1657 // We are connected and not being closed, so does SAS need to reconnect?
Azure.IoT.Build 13:606465879c57 1658 tickcounter_ms_t current_time;
Azure.IoT.Build 13:606465879c57 1659 if (tickcounter_get_current_ms(transport_data->msgTickCounter, &current_time) != 0)
AzureIoTClient 11:31ebaeb51e1e 1660 {
AzureIoTClient 11:31ebaeb51e1e 1661 transport_data->connectFailCount++;
AzureIoTClient 18:ec8e5e97c6a4 1662 result = __FAILURE__;
AzureIoTClient 11:31ebaeb51e1e 1663 }
AzureIoTClient 11:31ebaeb51e1e 1664 else
AzureIoTClient 11:31ebaeb51e1e 1665 {
AzureIoTClient 11:31ebaeb51e1e 1666 if ((current_time - transport_data->mqtt_connect_time) / 1000 > (SAS_TOKEN_DEFAULT_LIFETIME*SAS_REFRESH_MULTIPLIER))
AzureIoTClient 11:31ebaeb51e1e 1667 {
AzureIoTClient 27:04de3c0bf1db 1668 /* Codes_SRS_IOTHUB_TRANSPORT_MQTT_COMMON_07_058: [ If the sas token has timed out IoTHubTransport_MQTT_Common_DoWork shall disconnect from the mqtt client and destroy the transport information and wait for reconnect. ] */
AzureIoTClient 11:31ebaeb51e1e 1669 (void)mqtt_client_disconnect(transport_data->mqttClient);
AzureIoTClient 27:04de3c0bf1db 1670 xio_destroy(transport_data->xioTransport);
AzureIoTClient 27:04de3c0bf1db 1671 transport_data->xioTransport = NULL;
AzureIoTClient 27:04de3c0bf1db 1672
AzureIoTClient 19:f87dfe76bc70 1673 IoTHubClient_LL_ConnectionStatusCallBack(transport_data->llClientHandle, IOTHUB_CLIENT_CONNECTION_UNAUTHENTICATED, IOTHUB_CLIENT_CONNECTION_EXPIRED_SAS_TOKEN);
AzureIoTClient 20:594780be216d 1674 transport_data->mqttClientStatus = MQTT_CLIENT_STATUS_NOT_CONNECTED;
AzureIoTClient 11:31ebaeb51e1e 1675 transport_data->currPacketState = UNKNOWN_TYPE;
AzureIoTClient 12:658ca6865de2 1676 transport_data->device_twin_get_sent = false;
AzureIoTClient 11:31ebaeb51e1e 1677 if (transport_data->topic_MqttMessage != NULL)
AzureIoTClient 11:31ebaeb51e1e 1678 {
AzureIoTClient 11:31ebaeb51e1e 1679 transport_data->topics_ToSubscribe |= SUBSCRIBE_TELEMETRY_TOPIC;
AzureIoTClient 11:31ebaeb51e1e 1680 }
AzureIoTClient 12:658ca6865de2 1681 if (transport_data->topic_GetState != NULL)
AzureIoTClient 12:658ca6865de2 1682 {
AzureIoTClient 12:658ca6865de2 1683 transport_data->topics_ToSubscribe |= SUBSCRIBE_GET_REPORTED_STATE_TOPIC;
AzureIoTClient 12:658ca6865de2 1684 }
AzureIoTClient 12:658ca6865de2 1685 if (transport_data->topic_NotifyState != NULL)
AzureIoTClient 12:658ca6865de2 1686 {
AzureIoTClient 12:658ca6865de2 1687 transport_data->topics_ToSubscribe |= SUBSCRIBE_NOTIFICATION_STATE_TOPIC;
AzureIoTClient 12:658ca6865de2 1688 }
AzureIoTClient 12:658ca6865de2 1689 if (transport_data->topic_DeviceMethods != NULL)
AzureIoTClient 12:658ca6865de2 1690 {
AzureIoTClient 12:658ca6865de2 1691 transport_data->topics_ToSubscribe |= SUBSCRIBE_DEVICE_METHOD_TOPIC;
AzureIoTClient 12:658ca6865de2 1692 }
AzureIoTClient 11:31ebaeb51e1e 1693 }
AzureIoTClient 11:31ebaeb51e1e 1694 }
AzureIoTClient 11:31ebaeb51e1e 1695 }
AzureIoTClient 11:31ebaeb51e1e 1696 }
AzureIoTClient 11:31ebaeb51e1e 1697 return result;
AzureIoTClient 11:31ebaeb51e1e 1698 }
AzureIoTClient 11:31ebaeb51e1e 1699
AzureIoTClient 11:31ebaeb51e1e 1700 static STRING_HANDLE buildConfigForUsername(const IOTHUB_CLIENT_CONFIG* upperConfig)
AzureIoTClient 11:31ebaeb51e1e 1701 {
AzureIoTClient 22:07fec4d325b6 1702 return STRING_construct_sprintf("%s.%s/%s/api-version=%s&DeviceClientType=", upperConfig->iotHubName, upperConfig->iotHubSuffix, upperConfig->deviceId, IOTHUB_API_VERSION);
AzureIoTClient 11:31ebaeb51e1e 1703 }
AzureIoTClient 11:31ebaeb51e1e 1704
AzureIoTClient 21:167f8fcf6fb1 1705 static PMQTTTRANSPORT_HANDLE_DATA InitializeTransportHandleData(const IOTHUB_CLIENT_CONFIG* upperConfig, PDLIST_ENTRY waitingToSend, IOTHUB_AUTHORIZATION_HANDLE auth_module)
AzureIoTClient 11:31ebaeb51e1e 1706 {
AzureIoTClient 11:31ebaeb51e1e 1707 PMQTTTRANSPORT_HANDLE_DATA state = (PMQTTTRANSPORT_HANDLE_DATA)malloc(sizeof(MQTTTRANSPORT_HANDLE_DATA));
AzureIoTClient 11:31ebaeb51e1e 1708 if (state == NULL)
AzureIoTClient 11:31ebaeb51e1e 1709 {
AzureIoTClient 11:31ebaeb51e1e 1710 LogError("Could not create MQTT transport state. Memory allocation failed.");
AzureIoTClient 11:31ebaeb51e1e 1711 }
AzureIoTClient 11:31ebaeb51e1e 1712 else
AzureIoTClient 11:31ebaeb51e1e 1713 {
AzureIoTClient 21:167f8fcf6fb1 1714 memset(state, 0, sizeof(MQTTTRANSPORT_HANDLE_DATA) );
AzureIoTClient 21:167f8fcf6fb1 1715 if ((state->msgTickCounter = tickcounter_create()) == NULL)
AzureIoTClient 11:31ebaeb51e1e 1716 {
AzureIoTClient 21:167f8fcf6fb1 1717 LogError("Invalid Argument: iotHubName is empty");
AzureIoTClient 21:167f8fcf6fb1 1718 free(state);
AzureIoTClient 21:167f8fcf6fb1 1719 state = NULL;
AzureIoTClient 21:167f8fcf6fb1 1720 }
AzureIoTClient 25:a35763780a87 1721 // Codes_SRS_IOTHUB_TRANSPORT_MQTT_COMMON_09_005: [ MQTT transport shall use EXPONENTIAL_WITH_BACK_OFF as default retry policy ]
AzureIoTClient 25:a35763780a87 1722 else if ((state->retry_control_handle = retry_control_create(DEFAULT_RETRY_POLICY, DEFAULT_RETRY_TIMEOUT_IN_SECONDS)) == NULL)
AzureIoTClient 25:a35763780a87 1723 {
AzureIoTClient 25:a35763780a87 1724 LogError("Failed creating default retry control");
AzureIoTClient 25:a35763780a87 1725 tickcounter_destroy(state->msgTickCounter);
AzureIoTClient 25:a35763780a87 1726 free(state);
AzureIoTClient 25:a35763780a87 1727 state = NULL;
AzureIoTClient 25:a35763780a87 1728 }
AzureIoTClient 21:167f8fcf6fb1 1729 else if ((state->device_id = STRING_construct(upperConfig->deviceId)) == NULL)
AzureIoTClient 21:167f8fcf6fb1 1730 {
AzureIoTClient 21:167f8fcf6fb1 1731 LogError("failure constructing device_id.");
AzureIoTClient 25:a35763780a87 1732 retry_control_destroy(state->retry_control_handle);
Azure.IoT.Build 13:606465879c57 1733 tickcounter_destroy(state->msgTickCounter);
AzureIoTClient 11:31ebaeb51e1e 1734 free(state);
AzureIoTClient 11:31ebaeb51e1e 1735 state = NULL;
AzureIoTClient 11:31ebaeb51e1e 1736 }
AzureIoTClient 21:167f8fcf6fb1 1737 else if ((state->devicesPath = STRING_construct_sprintf("%s.%s/devices/%s", upperConfig->iotHubName, upperConfig->iotHubSuffix, upperConfig->deviceId)) == NULL)
AzureIoTClient 11:31ebaeb51e1e 1738 {
AzureIoTClient 25:a35763780a87 1739 STRING_delete(state->device_id);
AzureIoTClient 25:a35763780a87 1740 retry_control_destroy(state->retry_control_handle);
Azure.IoT.Build 13:606465879c57 1741 tickcounter_destroy(state->msgTickCounter);
AzureIoTClient 11:31ebaeb51e1e 1742 free(state);
AzureIoTClient 11:31ebaeb51e1e 1743 state = NULL;
AzureIoTClient 11:31ebaeb51e1e 1744 }
AzureIoTClient 11:31ebaeb51e1e 1745 else
AzureIoTClient 11:31ebaeb51e1e 1746 {
AzureIoTClient 21:167f8fcf6fb1 1747 if ( (state->topic_MqttEvent = STRING_construct_sprintf(TOPIC_DEVICE_DEVICE, upperConfig->deviceId) ) == NULL)
AzureIoTClient 11:31ebaeb51e1e 1748 {
AzureIoTClient 21:167f8fcf6fb1 1749 LogError("Could not create topic_MqttEvent for MQTT");
AzureIoTClient 11:31ebaeb51e1e 1750 STRING_delete(state->devicesPath);
AzureIoTClient 11:31ebaeb51e1e 1751 STRING_delete(state->device_id);
AzureIoTClient 25:a35763780a87 1752 retry_control_destroy(state->retry_control_handle);
Azure.IoT.Build 13:606465879c57 1753 tickcounter_destroy(state->msgTickCounter);
AzureIoTClient 11:31ebaeb51e1e 1754 free(state);
AzureIoTClient 11:31ebaeb51e1e 1755 state = NULL;
AzureIoTClient 11:31ebaeb51e1e 1756 }
AzureIoTClient 11:31ebaeb51e1e 1757 else
AzureIoTClient 11:31ebaeb51e1e 1758 {
AzureIoTClient 21:167f8fcf6fb1 1759 state->mqttClient = mqtt_client_init(mqtt_notification_callback, mqtt_operation_complete_callback, state, mqtt_error_callback, state);
AzureIoTClient 21:167f8fcf6fb1 1760 if (state->mqttClient == NULL)
AzureIoTClient 11:31ebaeb51e1e 1761 {
AzureIoTClient 21:167f8fcf6fb1 1762 LogError("failure initializing mqtt client.");
AzureIoTClient 11:31ebaeb51e1e 1763 STRING_delete(state->devicesPath);
AzureIoTClient 11:31ebaeb51e1e 1764 STRING_delete(state->topic_MqttEvent);
AzureIoTClient 11:31ebaeb51e1e 1765 STRING_delete(state->device_id);
AzureIoTClient 25:a35763780a87 1766 retry_control_destroy(state->retry_control_handle);
Azure.IoT.Build 13:606465879c57 1767 tickcounter_destroy(state->msgTickCounter);
AzureIoTClient 11:31ebaeb51e1e 1768 free(state);
AzureIoTClient 11:31ebaeb51e1e 1769 state = NULL;
AzureIoTClient 11:31ebaeb51e1e 1770 }
AzureIoTClient 11:31ebaeb51e1e 1771 else
AzureIoTClient 11:31ebaeb51e1e 1772 {
AzureIoTClient 21:167f8fcf6fb1 1773 /* Codes_SRS_IOTHUB_MQTT_TRANSPORT_07_008: [If the upperConfig contains a valid protocolGatewayHostName value the this shall be used for the hostname, otherwise the hostname shall be constructed using the iothubname and iothubSuffix.] */
AzureIoTClient 21:167f8fcf6fb1 1774 if (upperConfig->protocolGatewayHostName == NULL)
AzureIoTClient 21:167f8fcf6fb1 1775 {
AzureIoTClient 21:167f8fcf6fb1 1776 state->hostAddress = STRING_construct_sprintf("%s.%s", upperConfig->iotHubName, upperConfig->iotHubSuffix);
AzureIoTClient 21:167f8fcf6fb1 1777 }
AzureIoTClient 21:167f8fcf6fb1 1778 else
AzureIoTClient 21:167f8fcf6fb1 1779 {
AzureIoTClient 21:167f8fcf6fb1 1780 state->hostAddress = STRING_construct(upperConfig->protocolGatewayHostName);
AzureIoTClient 21:167f8fcf6fb1 1781 }
AzureIoTClient 21:167f8fcf6fb1 1782
AzureIoTClient 21:167f8fcf6fb1 1783 if (state->hostAddress == NULL)
AzureIoTClient 21:167f8fcf6fb1 1784 {
AzureIoTClient 21:167f8fcf6fb1 1785 LogError("failure constructing host address.");
AzureIoTClient 25:a35763780a87 1786 mqtt_client_deinit(state->mqttClient);
AzureIoTClient 21:167f8fcf6fb1 1787 STRING_delete(state->devicesPath);
AzureIoTClient 21:167f8fcf6fb1 1788 STRING_delete(state->topic_MqttEvent);
AzureIoTClient 21:167f8fcf6fb1 1789 STRING_delete(state->device_id);
AzureIoTClient 25:a35763780a87 1790 retry_control_destroy(state->retry_control_handle);
AzureIoTClient 21:167f8fcf6fb1 1791 tickcounter_destroy(state->msgTickCounter);
AzureIoTClient 21:167f8fcf6fb1 1792 free(state);
AzureIoTClient 21:167f8fcf6fb1 1793 state = NULL;
AzureIoTClient 21:167f8fcf6fb1 1794 }
AzureIoTClient 21:167f8fcf6fb1 1795 else if ((state->configPassedThroughUsername = buildConfigForUsername(upperConfig)) == NULL)
AzureIoTClient 21:167f8fcf6fb1 1796 {
AzureIoTClient 25:a35763780a87 1797 STRING_delete(state->hostAddress);
AzureIoTClient 21:167f8fcf6fb1 1798 mqtt_client_deinit(state->mqttClient);
AzureIoTClient 25:a35763780a87 1799 STRING_delete(state->devicesPath);
AzureIoTClient 21:167f8fcf6fb1 1800 STRING_delete(state->topic_MqttEvent);
AzureIoTClient 21:167f8fcf6fb1 1801 STRING_delete(state->device_id);
AzureIoTClient 25:a35763780a87 1802 retry_control_destroy(state->retry_control_handle);
AzureIoTClient 21:167f8fcf6fb1 1803 tickcounter_destroy(state->msgTickCounter);
AzureIoTClient 21:167f8fcf6fb1 1804 free(state);
AzureIoTClient 21:167f8fcf6fb1 1805 state = NULL;
AzureIoTClient 21:167f8fcf6fb1 1806 }
AzureIoTClient 21:167f8fcf6fb1 1807 else
AzureIoTClient 21:167f8fcf6fb1 1808 {
AzureIoTClient 21:167f8fcf6fb1 1809 /* Codes_SRS_IOTHUB_MQTT_TRANSPORT_07_010: [IoTHubTransport_MQTT_Common_Create shall allocate memory to save its internal state where all topics, hostname, device_id, device_key, sasTokenSr and client handle shall be saved.] */
AzureIoTClient 21:167f8fcf6fb1 1810 DList_InitializeListHead(&(state->telemetry_waitingForAck));
AzureIoTClient 21:167f8fcf6fb1 1811 DList_InitializeListHead(&(state->ack_waiting_queue));
AzureIoTClient 21:167f8fcf6fb1 1812 state->isDestroyCalled = false;
AzureIoTClient 21:167f8fcf6fb1 1813 state->isRegistered = false;
AzureIoTClient 21:167f8fcf6fb1 1814 state->mqttClientStatus = MQTT_CLIENT_STATUS_NOT_CONNECTED;
AzureIoTClient 21:167f8fcf6fb1 1815 state->device_twin_get_sent = false;
AzureIoTClient 21:167f8fcf6fb1 1816 state->isRecoverableError = true;
AzureIoTClient 21:167f8fcf6fb1 1817 state->packetId = 1;
AzureIoTClient 21:167f8fcf6fb1 1818 state->llClientHandle = NULL;
AzureIoTClient 21:167f8fcf6fb1 1819 state->xioTransport = NULL;
AzureIoTClient 21:167f8fcf6fb1 1820 state->portNum = 0;
AzureIoTClient 21:167f8fcf6fb1 1821 state->waitingToSend = waitingToSend;
AzureIoTClient 21:167f8fcf6fb1 1822 state->currPacketState = CONNECT_TYPE;
AzureIoTClient 21:167f8fcf6fb1 1823 state->keepAliveValue = DEFAULT_MQTT_KEEPALIVE;
AzureIoTClient 21:167f8fcf6fb1 1824 state->connectFailCount = 0;
AzureIoTClient 21:167f8fcf6fb1 1825 state->connectTick = 0;
AzureIoTClient 21:167f8fcf6fb1 1826 state->topic_MqttMessage = NULL;
AzureIoTClient 21:167f8fcf6fb1 1827 state->topic_GetState = NULL;
AzureIoTClient 21:167f8fcf6fb1 1828 state->topic_NotifyState = NULL;
AzureIoTClient 21:167f8fcf6fb1 1829 state->topics_ToSubscribe = UNSUBSCRIBE_FROM_TOPIC;
AzureIoTClient 21:167f8fcf6fb1 1830 state->topic_DeviceMethods = NULL;
AzureIoTClient 21:167f8fcf6fb1 1831 state->log_trace = state->raw_trace = false;
AzureIoTClient 21:167f8fcf6fb1 1832 srand((unsigned int)get_time(NULL));
AzureIoTClient 21:167f8fcf6fb1 1833 state->authorization_module = auth_module;
AzureIoTClient 24:4096249decf1 1834 state->isProductInfoSet = false;
AzureIoTClient 21:167f8fcf6fb1 1835 }
AzureIoTClient 11:31ebaeb51e1e 1836 }
AzureIoTClient 11:31ebaeb51e1e 1837 }
AzureIoTClient 11:31ebaeb51e1e 1838 }
AzureIoTClient 11:31ebaeb51e1e 1839 }
AzureIoTClient 11:31ebaeb51e1e 1840 return state;
AzureIoTClient 11:31ebaeb51e1e 1841 }
AzureIoTClient 11:31ebaeb51e1e 1842
AzureIoTClient 11:31ebaeb51e1e 1843 TRANSPORT_LL_HANDLE IoTHubTransport_MQTT_Common_Create(const IOTHUBTRANSPORT_CONFIG* config, MQTT_GET_IO_TRANSPORT get_io_transport)
AzureIoTClient 11:31ebaeb51e1e 1844 {
AzureIoTClient 11:31ebaeb51e1e 1845 PMQTTTRANSPORT_HANDLE_DATA result;
AzureIoTClient 11:31ebaeb51e1e 1846 size_t deviceIdSize;
AzureIoTClient 11:31ebaeb51e1e 1847
AzureIoTClient 12:658ca6865de2 1848 /* Codes_SRS_IOTHUB_MQTT_TRANSPORT_07_001: [If parameter config is NULL then IoTHubTransport_MQTT_Common_Create shall return NULL.] */
AzureIoTClient 11:31ebaeb51e1e 1849 /* Codes_SRS_IOTHUB_TRANSPORT_MQTT_COMMON_07_041: [ if get_io_transport is NULL then IoTHubTransport_MQTT_Common_Create shall return NULL. ] */
AzureIoTClient 11:31ebaeb51e1e 1850 if (config == NULL || get_io_transport == NULL)
AzureIoTClient 11:31ebaeb51e1e 1851 {
AzureIoTClient 11:31ebaeb51e1e 1852 LogError("Invalid Argument: Config Parameter is NULL.");
AzureIoTClient 11:31ebaeb51e1e 1853 result = NULL;
AzureIoTClient 11:31ebaeb51e1e 1854 }
AzureIoTClient 12:658ca6865de2 1855 /* Codes_SRS_IOTHUB_MQTT_TRANSPORT_07_002: [If the parameter config's variables upperConfig or waitingToSend are NULL then IoTHubTransport_MQTT_Common_Create shall return NULL.] */
AzureIoTClient 12:658ca6865de2 1856 /* Codes_SRS_IOTHUB_MQTT_TRANSPORT_07_003: [If the upperConfig's variables deviceId, both deviceKey and deviceSasToken, iotHubName, protocol, or iotHubSuffix are NULL then IoTHubTransport_MQTT_Common_Create shall return NULL.] */
AzureIoTClient 12:658ca6865de2 1857 /* Codes_SRS_IOTHUB_MQTT_TRANSPORT_03_003: [If both deviceKey & deviceSasToken fields are NOT NULL then IoTHubTransport_MQTT_Common_Create shall return NULL.] */
AzureIoTClient 11:31ebaeb51e1e 1858 else if (config->upperConfig == NULL ||
AzureIoTClient 11:31ebaeb51e1e 1859 config->upperConfig->protocol == NULL ||
AzureIoTClient 11:31ebaeb51e1e 1860 config->upperConfig->deviceId == NULL ||
AzureIoTClient 11:31ebaeb51e1e 1861 ((config->upperConfig->deviceKey != NULL) && (config->upperConfig->deviceSasToken != NULL)) ||
AzureIoTClient 11:31ebaeb51e1e 1862 config->upperConfig->iotHubName == NULL ||
AzureIoTClient 11:31ebaeb51e1e 1863 config->upperConfig->iotHubSuffix == NULL)
AzureIoTClient 11:31ebaeb51e1e 1864 {
AzureIoTClient 11:31ebaeb51e1e 1865 LogError("Invalid Argument: upperConfig structure contains an invalid parameter");
AzureIoTClient 11:31ebaeb51e1e 1866 result = NULL;
AzureIoTClient 11:31ebaeb51e1e 1867 }
AzureIoTClient 21:167f8fcf6fb1 1868 /* Codes_SRS_IOTHUB_MQTT_TRANSPORT_07_002: [If the parameter config's variables upperConfig, auth_module_handle or waitingToSend are NULL then IoTHubTransport_MQTT_Common_Create shall return NULL.] */
AzureIoTClient 11:31ebaeb51e1e 1869 else if (config->waitingToSend == NULL)
AzureIoTClient 11:31ebaeb51e1e 1870 {
AzureIoTClient 11:31ebaeb51e1e 1871 LogError("Invalid Argument: waitingToSend is NULL)");
AzureIoTClient 11:31ebaeb51e1e 1872 result = NULL;
AzureIoTClient 11:31ebaeb51e1e 1873 }
AzureIoTClient 21:167f8fcf6fb1 1874 /* Codes_SRS_IOTHUB_MQTT_TRANSPORT_07_002: [If the parameter config's variables upperConfig, auth_module_handle or waitingToSend are NULL then IoTHubTransport_MQTT_Common_Create shall return NULL.] */
AzureIoTClient 21:167f8fcf6fb1 1875 else if (config->auth_module_handle == NULL)
AzureIoTClient 21:167f8fcf6fb1 1876 {
AzureIoTClient 21:167f8fcf6fb1 1877 LogError("Invalid Argument: auth_module_handle is NULL)");
AzureIoTClient 21:167f8fcf6fb1 1878 result = NULL;
AzureIoTClient 21:167f8fcf6fb1 1879 }
AzureIoTClient 12:658ca6865de2 1880 /* Codes_SRS_IOTHUB_MQTT_TRANSPORT_07_006: [If the upperConfig's variables deviceId is an empty strings or length is greater then 128 then IoTHubTransport_MQTT_Common_Create shall return NULL.] */
AzureIoTClient 11:31ebaeb51e1e 1881 else if ( ( (deviceIdSize = strlen(config->upperConfig->deviceId)) > 128U) || (deviceIdSize == 0) )
AzureIoTClient 11:31ebaeb51e1e 1882 {
AzureIoTClient 11:31ebaeb51e1e 1883 LogError("Invalid Argument: DeviceId is of an invalid size");
AzureIoTClient 11:31ebaeb51e1e 1884 result = NULL;
AzureIoTClient 11:31ebaeb51e1e 1885 }
AzureIoTClient 12:658ca6865de2 1886 /* Codes_SRS_IOTHUB_MQTT_TRANSPORT_07_003: [If the upperConfig's variables deviceId, both deviceKey and deviceSasToken, iotHubName, protocol, or iotHubSuffix are NULL then IoTHubTransport_MQTT_Common_Create shall return NULL.] */
AzureIoTClient 11:31ebaeb51e1e 1887 else if ((config->upperConfig->deviceKey != NULL) && (strlen(config->upperConfig->deviceKey) == 0))
AzureIoTClient 11:31ebaeb51e1e 1888 {
AzureIoTClient 11:31ebaeb51e1e 1889 LogError("Invalid Argument: deviceKey is empty");
AzureIoTClient 11:31ebaeb51e1e 1890 result = NULL;
AzureIoTClient 11:31ebaeb51e1e 1891 }
AzureIoTClient 12:658ca6865de2 1892 /* Codes_SRS_IOTHUB_MQTT_TRANSPORT_07_003: [If the upperConfig's variables deviceId, both deviceKey and deviceSasToken, iotHubName, protocol, or iotHubSuffix are NULL then IoTHubTransport_MQTT_Common_Create shall return NULL.] */
AzureIoTClient 11:31ebaeb51e1e 1893 else if ((config->upperConfig->deviceSasToken != NULL) && (strlen(config->upperConfig->deviceSasToken) == 0))
AzureIoTClient 11:31ebaeb51e1e 1894 {
AzureIoTClient 11:31ebaeb51e1e 1895 LogError("Invalid Argument: deviceSasToken is empty");
AzureIoTClient 11:31ebaeb51e1e 1896 result = NULL;
AzureIoTClient 11:31ebaeb51e1e 1897 }
AzureIoTClient 12:658ca6865de2 1898 /* Codes_SRS_IOTHUB_MQTT_TRANSPORT_07_003: [If the upperConfig's variables deviceId, deviceKey, iotHubName, protocol, or iotHubSuffix are NULL then IoTHubTransport_MQTT_Common_Create shall return NULL.] */
AzureIoTClient 11:31ebaeb51e1e 1899 else if (strlen(config->upperConfig->iotHubName) == 0)
AzureIoTClient 11:31ebaeb51e1e 1900 {
AzureIoTClient 11:31ebaeb51e1e 1901 LogError("Invalid Argument: iotHubName is empty");
AzureIoTClient 11:31ebaeb51e1e 1902 result = NULL;
AzureIoTClient 11:31ebaeb51e1e 1903 }
AzureIoTClient 11:31ebaeb51e1e 1904 else
AzureIoTClient 11:31ebaeb51e1e 1905 {
AzureIoTClient 21:167f8fcf6fb1 1906 result = InitializeTransportHandleData(config->upperConfig, config->waitingToSend, config->auth_module_handle);
Azure.IoT.Build 13:606465879c57 1907 if (result != NULL)
AzureIoTClient 11:31ebaeb51e1e 1908 {
AzureIoTClient 11:31ebaeb51e1e 1909 result->get_io_transport = get_io_transport;
AzureIoTClient 20:594780be216d 1910 result->http_proxy_hostname = NULL;
AzureIoTClient 20:594780be216d 1911 result->http_proxy_username = NULL;
AzureIoTClient 20:594780be216d 1912 result->http_proxy_password = NULL;
AzureIoTClient 11:31ebaeb51e1e 1913 }
AzureIoTClient 11:31ebaeb51e1e 1914 }
AzureIoTClient 12:658ca6865de2 1915 /* Codes_SRS_IOTHUB_MQTT_TRANSPORT_07_009: [If any error is encountered then IoTHubTransport_MQTT_Common_Create shall return NULL.] */
AzureIoTClient 12:658ca6865de2 1916 /* Codes_SRS_IOTHUB_MQTT_TRANSPORT_07_011: [On Success IoTHubTransport_MQTT_Common_Create shall return a non-NULL value.] */
AzureIoTClient 11:31ebaeb51e1e 1917 return result;
AzureIoTClient 11:31ebaeb51e1e 1918 }
AzureIoTClient 11:31ebaeb51e1e 1919
AzureIoTClient 11:31ebaeb51e1e 1920 void IoTHubTransport_MQTT_Common_Destroy(TRANSPORT_LL_HANDLE handle)
AzureIoTClient 11:31ebaeb51e1e 1921 {
AzureIoTClient 12:658ca6865de2 1922 /* Codes_SRS_IOTHUB_MQTT_TRANSPORT_07_012: [IoTHubTransport_MQTT_Common_Destroy shall do nothing if parameter handle is NULL.] */
AzureIoTClient 11:31ebaeb51e1e 1923 PMQTTTRANSPORT_HANDLE_DATA transport_data = (PMQTTTRANSPORT_HANDLE_DATA)handle;
AzureIoTClient 11:31ebaeb51e1e 1924 if (transport_data != NULL)
AzureIoTClient 11:31ebaeb51e1e 1925 {
AzureIoTClient 11:31ebaeb51e1e 1926 transport_data->isDestroyCalled = true;
AzureIoTClient 11:31ebaeb51e1e 1927
AzureIoTClient 11:31ebaeb51e1e 1928 DisconnectFromClient(transport_data);
AzureIoTClient 11:31ebaeb51e1e 1929
AzureIoTClient 11:31ebaeb51e1e 1930 //Empty the Waiting for Ack Messages.
AzureIoTClient 11:31ebaeb51e1e 1931 while (!DList_IsListEmpty(&transport_data->telemetry_waitingForAck))
AzureIoTClient 11:31ebaeb51e1e 1932 {
AzureIoTClient 11:31ebaeb51e1e 1933 PDLIST_ENTRY currentEntry = DList_RemoveHeadList(&transport_data->telemetry_waitingForAck);
AzureIoTClient 11:31ebaeb51e1e 1934 MQTT_MESSAGE_DETAILS_LIST* mqttMsgEntry = containingRecord(currentEntry, MQTT_MESSAGE_DETAILS_LIST, entry);
AzureIoTClient 11:31ebaeb51e1e 1935 sendMsgComplete(mqttMsgEntry->iotHubMessageEntry, transport_data, IOTHUB_CLIENT_CONFIRMATION_BECAUSE_DESTROY);
AzureIoTClient 11:31ebaeb51e1e 1936 free(mqttMsgEntry);
AzureIoTClient 11:31ebaeb51e1e 1937 }
AzureIoTClient 12:658ca6865de2 1938 while (!DList_IsListEmpty(&transport_data->ack_waiting_queue))
AzureIoTClient 12:658ca6865de2 1939 {
AzureIoTClient 12:658ca6865de2 1940 PDLIST_ENTRY currentEntry = DList_RemoveHeadList(&transport_data->ack_waiting_queue);
AzureIoTClient 12:658ca6865de2 1941 MQTT_DEVICE_TWIN_ITEM* mqtt_device_twin = containingRecord(currentEntry, MQTT_DEVICE_TWIN_ITEM, entry);
AzureIoTClient 12:658ca6865de2 1942 IoTHubClient_LL_ReportedStateComplete(transport_data->llClientHandle, mqtt_device_twin->iothub_msg_id, STATUS_CODE_TIMEOUT_VALUE);
AzureIoTClient 12:658ca6865de2 1943 free(mqtt_device_twin);
AzureIoTClient 12:658ca6865de2 1944 }
AzureIoTClient 11:31ebaeb51e1e 1945
AzureIoTClient 21:167f8fcf6fb1 1946 STRING_delete(transport_data->devicesPath);
AzureIoTClient 11:31ebaeb51e1e 1947
AzureIoTClient 12:658ca6865de2 1948 /* Codes_SRS_IOTHUB_MQTT_TRANSPORT_07_014: [IoTHubTransport_MQTT_Common_Destroy shall free all the resources currently in use.] */
AzureIoTClient 11:31ebaeb51e1e 1949 mqtt_client_deinit(transport_data->mqttClient);
AzureIoTClient 25:a35763780a87 1950 retry_control_destroy(transport_data->retry_control_handle);
AzureIoTClient 11:31ebaeb51e1e 1951 STRING_delete(transport_data->topic_MqttEvent);
AzureIoTClient 11:31ebaeb51e1e 1952 STRING_delete(transport_data->topic_MqttMessage);
AzureIoTClient 11:31ebaeb51e1e 1953 STRING_delete(transport_data->device_id);
AzureIoTClient 11:31ebaeb51e1e 1954 STRING_delete(transport_data->hostAddress);
AzureIoTClient 11:31ebaeb51e1e 1955 STRING_delete(transport_data->configPassedThroughUsername);
AzureIoTClient 12:658ca6865de2 1956 STRING_delete(transport_data->topic_GetState);
AzureIoTClient 12:658ca6865de2 1957 STRING_delete(transport_data->topic_NotifyState);
AzureIoTClient 12:658ca6865de2 1958 STRING_delete(transport_data->topic_DeviceMethods);
AzureIoTClient 12:658ca6865de2 1959
Azure.IoT.Build 13:606465879c57 1960 tickcounter_destroy(transport_data->msgTickCounter);
AzureIoTClient 20:594780be216d 1961 /* Codes_SRS_IOTHUB_TRANSPORT_MQTT_COMMON_01_012: [ `IoTHubTransport_MQTT_Common_Destroy` shall free the stored proxy options. ]*/
AzureIoTClient 20:594780be216d 1962 free_proxy_data(transport_data);
AzureIoTClient 11:31ebaeb51e1e 1963 free(transport_data);
AzureIoTClient 11:31ebaeb51e1e 1964 }
AzureIoTClient 11:31ebaeb51e1e 1965 }
AzureIoTClient 11:31ebaeb51e1e 1966
AzureIoTClient 12:658ca6865de2 1967 int IoTHubTransport_MQTT_Common_Subscribe_DeviceTwin(IOTHUB_DEVICE_HANDLE handle)
AzureIoTClient 12:658ca6865de2 1968 {
AzureIoTClient 12:658ca6865de2 1969 int result;
AzureIoTClient 12:658ca6865de2 1970 PMQTTTRANSPORT_HANDLE_DATA transport_data = (PMQTTTRANSPORT_HANDLE_DATA)handle;
AzureIoTClient 12:658ca6865de2 1971 if (transport_data == NULL)
AzureIoTClient 12:658ca6865de2 1972 {
AzureIoTClient 12:658ca6865de2 1973 /* Codes_SRS_IOTHUB_MQTT_TRANSPORT_07_042: [If the parameter handle is NULL than IoTHubTransport_MQTT_Common_Subscribe shall return a non-zero value.] */
AzureIoTClient 12:658ca6865de2 1974 LogError("Invalid handle parameter. NULL.");
AzureIoTClient 18:ec8e5e97c6a4 1975 result = __FAILURE__;
AzureIoTClient 12:658ca6865de2 1976 }
AzureIoTClient 12:658ca6865de2 1977 else
AzureIoTClient 12:658ca6865de2 1978 {
AzureIoTClient 12:658ca6865de2 1979 if (transport_data->topic_GetState == NULL)
AzureIoTClient 12:658ca6865de2 1980 {
AzureIoTClient 12:658ca6865de2 1981 /* Codes_SRS_IOTHUB_MQTT_TRANSPORT_07_044: [IoTHubTransport_MQTT_Common_Subscribe_DeviceTwin shall construct the get state topic string and the notify state topic string.] */
AzureIoTClient 12:658ca6865de2 1982 transport_data->topic_GetState = STRING_construct(TOPIC_GET_DESIRED_STATE);
AzureIoTClient 12:658ca6865de2 1983 if (transport_data->topic_GetState == NULL)
AzureIoTClient 12:658ca6865de2 1984 {
AzureIoTClient 12:658ca6865de2 1985 /* Codes_SRS_IOTHUB_MQTT_TRANSPORT_07_046: [Upon failure IoTHubTransport_MQTT_Common_Subscribe_DeviceTwin shall return a non-zero value.] */
AzureIoTClient 12:658ca6865de2 1986 LogError("Failure: unable constructing reported state topic");
AzureIoTClient 18:ec8e5e97c6a4 1987 result = __FAILURE__;
AzureIoTClient 12:658ca6865de2 1988 }
AzureIoTClient 12:658ca6865de2 1989 else
AzureIoTClient 12:658ca6865de2 1990 {
AzureIoTClient 12:658ca6865de2 1991 /* Codes_SRS_IOTHUB_MQTT_TRANSPORT_07_047: [On success IoTHubTransport_MQTT_Common_Subscribe_DeviceTwin shall return 0.] */
AzureIoTClient 12:658ca6865de2 1992 transport_data->topics_ToSubscribe |= SUBSCRIBE_GET_REPORTED_STATE_TOPIC;
AzureIoTClient 12:658ca6865de2 1993 result = 0;
AzureIoTClient 12:658ca6865de2 1994 }
AzureIoTClient 12:658ca6865de2 1995 }
AzureIoTClient 12:658ca6865de2 1996 else
AzureIoTClient 12:658ca6865de2 1997 {
AzureIoTClient 12:658ca6865de2 1998 result = 0;
AzureIoTClient 12:658ca6865de2 1999 }
AzureIoTClient 12:658ca6865de2 2000 if (result == 0 && transport_data->topic_NotifyState == NULL)
AzureIoTClient 12:658ca6865de2 2001 {
AzureIoTClient 12:658ca6865de2 2002 /* Codes_SRS_IOTHUB_MQTT_TRANSPORT_07_044: [`IoTHubTransport_MQTT_Common_Subscribe_DeviceTwin` shall construct the get state topic string and the notify state topic string.] */
AzureIoTClient 12:658ca6865de2 2003 transport_data->topic_NotifyState = STRING_construct(TOPIC_NOTIFICATION_STATE);
AzureIoTClient 12:658ca6865de2 2004 if (transport_data->topic_NotifyState == NULL)
AzureIoTClient 12:658ca6865de2 2005 {
AzureIoTClient 12:658ca6865de2 2006 LogError("Failure: unable constructing notify state topic");
AzureIoTClient 18:ec8e5e97c6a4 2007 result = __FAILURE__;
AzureIoTClient 12:658ca6865de2 2008 }
AzureIoTClient 12:658ca6865de2 2009 else
AzureIoTClient 12:658ca6865de2 2010 {
AzureIoTClient 12:658ca6865de2 2011 /* Codes_SRS_IOTHUB_MQTT_TRANSPORT_07_047: [On success IoTHubTransport_MQTT_Common_Subscribe_DeviceTwin shall return 0.] */
AzureIoTClient 12:658ca6865de2 2012 transport_data->topics_ToSubscribe |= SUBSCRIBE_NOTIFICATION_STATE_TOPIC;
AzureIoTClient 12:658ca6865de2 2013 result = 0;
AzureIoTClient 12:658ca6865de2 2014 }
AzureIoTClient 12:658ca6865de2 2015 }
AzureIoTClient 12:658ca6865de2 2016 if (result == 0)
AzureIoTClient 12:658ca6865de2 2017 {
AzureIoTClient 12:658ca6865de2 2018 if (transport_data->currPacketState != CONNACK_TYPE &&
AzureIoTClient 12:658ca6865de2 2019 transport_data->currPacketState != CONNECT_TYPE &&
AzureIoTClient 12:658ca6865de2 2020 transport_data->currPacketState != DISCONNECT_TYPE &&
AzureIoTClient 12:658ca6865de2 2021 transport_data->currPacketState != PACKET_TYPE_ERROR)
AzureIoTClient 12:658ca6865de2 2022 {
AzureIoTClient 12:658ca6865de2 2023 transport_data->currPacketState = SUBSCRIBE_TYPE;
AzureIoTClient 12:658ca6865de2 2024 }
AzureIoTClient 12:658ca6865de2 2025 }
AzureIoTClient 12:658ca6865de2 2026 }
AzureIoTClient 12:658ca6865de2 2027 return result;
AzureIoTClient 12:658ca6865de2 2028 }
AzureIoTClient 12:658ca6865de2 2029
AzureIoTClient 12:658ca6865de2 2030 void IoTHubTransport_MQTT_Common_Unsubscribe_DeviceTwin(IOTHUB_DEVICE_HANDLE handle)
AzureIoTClient 12:658ca6865de2 2031 {
AzureIoTClient 12:658ca6865de2 2032 PMQTTTRANSPORT_HANDLE_DATA transport_data = (PMQTTTRANSPORT_HANDLE_DATA)handle;
AzureIoTClient 12:658ca6865de2 2033 /* Codes_SRS_IOTHUB_MQTT_TRANSPORT_07_048: [If the parameter handle is NULL than IoTHubTransport_MQTT_Common_Unsubscribe_DeviceTwin shall do nothing.] */
AzureIoTClient 12:658ca6865de2 2034 if (transport_data != NULL)
AzureIoTClient 12:658ca6865de2 2035 {
AzureIoTClient 12:658ca6865de2 2036 if (transport_data->topic_GetState != NULL)
AzureIoTClient 12:658ca6865de2 2037 {
AzureIoTClient 12:658ca6865de2 2038 /* Codes_SRS_IOTHUB_MQTT_TRANSPORT_07_049: [If subscribe_state is set to IOTHUB_DEVICE_TWIN_DESIRED_STATE then IoTHubTransport_MQTT_Common_Unsubscribe_DeviceTwin shall unsubscribe from the topic_GetState to the mqtt client.] */
AzureIoTClient 12:658ca6865de2 2039 transport_data->topics_ToSubscribe &= ~SUBSCRIBE_GET_REPORTED_STATE_TOPIC;
AzureIoTClient 12:658ca6865de2 2040 STRING_delete(transport_data->topic_GetState);
AzureIoTClient 12:658ca6865de2 2041 transport_data->topic_GetState = NULL;
AzureIoTClient 12:658ca6865de2 2042 }
AzureIoTClient 12:658ca6865de2 2043 if (transport_data->topic_NotifyState != NULL)
AzureIoTClient 12:658ca6865de2 2044 {
AzureIoTClient 12:658ca6865de2 2045 /* Codes_SRS_IOTHUB_MQTT_TRANSPORT_07_050: [If subscribe_state is set to IOTHUB_DEVICE_TWIN_NOTIFICATION_STATE then IoTHubTransport_MQTT_Common_Unsubscribe_DeviceTwin shall unsubscribe from the topic_NotifyState to the mqtt client.] */
AzureIoTClient 12:658ca6865de2 2046 transport_data->topics_ToSubscribe &= ~SUBSCRIBE_NOTIFICATION_STATE_TOPIC;
AzureIoTClient 12:658ca6865de2 2047 STRING_delete(transport_data->topic_NotifyState);
AzureIoTClient 12:658ca6865de2 2048 transport_data->topic_NotifyState = NULL;
AzureIoTClient 12:658ca6865de2 2049 }
AzureIoTClient 12:658ca6865de2 2050 }
AzureIoTClient 12:658ca6865de2 2051 else
AzureIoTClient 12:658ca6865de2 2052 {
AzureIoTClient 12:658ca6865de2 2053 LogError("Invalid argument to unsubscribe (handle is NULL).");
AzureIoTClient 12:658ca6865de2 2054 }
AzureIoTClient 12:658ca6865de2 2055 }
AzureIoTClient 12:658ca6865de2 2056
AzureIoTClient 12:658ca6865de2 2057 int IoTHubTransport_MQTT_Common_Subscribe_DeviceMethod(IOTHUB_DEVICE_HANDLE handle)
AzureIoTClient 12:658ca6865de2 2058 {
AzureIoTClient 12:658ca6865de2 2059 int result;
AzureIoTClient 12:658ca6865de2 2060 PMQTTTRANSPORT_HANDLE_DATA transport_data = (PMQTTTRANSPORT_HANDLE_DATA)handle;
AzureIoTClient 12:658ca6865de2 2061
AzureIoTClient 12:658ca6865de2 2062 if (transport_data == NULL)
AzureIoTClient 12:658ca6865de2 2063 {
AzureIoTClient 12:658ca6865de2 2064 /*Codes_SRS_IOTHUB_MQTT_TRANSPORT_12_001 : [If the parameter handle is NULL than IoTHubTransport_MQTT_Common_Subscribe_DeviceMethod shall return a non - zero value.]*/
AzureIoTClient 12:658ca6865de2 2065 LogError("Invalid handle parameter. NULL.");
AzureIoTClient 18:ec8e5e97c6a4 2066 result = __FAILURE__;
AzureIoTClient 12:658ca6865de2 2067 }
AzureIoTClient 12:658ca6865de2 2068 else
AzureIoTClient 12:658ca6865de2 2069 {
AzureIoTClient 12:658ca6865de2 2070 if (transport_data->topic_DeviceMethods == NULL)
AzureIoTClient 12:658ca6865de2 2071 {
AzureIoTClient 12:658ca6865de2 2072 /*Codes_SRS_IOTHUB_MQTT_TRANSPORT_12_004 : [IoTHubTransport_MQTT_Common_Subscribe_DeviceMethod shall construct the DEVICE_METHOD topic string for subscribe.]*/
AzureIoTClient 12:658ca6865de2 2073 transport_data->topic_DeviceMethods = STRING_construct(TOPIC_DEVICE_METHOD_SUBSCRIBE);
AzureIoTClient 12:658ca6865de2 2074 if (transport_data->topic_DeviceMethods == NULL)
AzureIoTClient 12:658ca6865de2 2075 {
AzureIoTClient 12:658ca6865de2 2076 /*Codes_SRS_IOTHUB_MQTT_TRANSPORT_12_006 : [Upon failure IoTHubTransport_MQTT_Common_Subscribe_DeviceMethod shall return a non - zero value.]*/
AzureIoTClient 12:658ca6865de2 2077 LogError("Failure: unable constructing device method subscribe topic");
AzureIoTClient 18:ec8e5e97c6a4 2078 result = __FAILURE__;
AzureIoTClient 12:658ca6865de2 2079 }
AzureIoTClient 12:658ca6865de2 2080 else
AzureIoTClient 12:658ca6865de2 2081 {
AzureIoTClient 12:658ca6865de2 2082 /*Codes_SRS_IOTHUB_MQTT_TRANSPORT_12_003 : [IoTHubTransport_MQTT_Common_Subscribe_DeviceMethod shall set the signaling flag for DEVICE_METHOD topic for the receiver's topic list. ]*/
AzureIoTClient 12:658ca6865de2 2083 transport_data->topics_ToSubscribe |= SUBSCRIBE_DEVICE_METHOD_TOPIC;
AzureIoTClient 12:658ca6865de2 2084 result = 0;
AzureIoTClient 12:658ca6865de2 2085 }
AzureIoTClient 12:658ca6865de2 2086 }
AzureIoTClient 12:658ca6865de2 2087 else
AzureIoTClient 12:658ca6865de2 2088 {
AzureIoTClient 12:658ca6865de2 2089 /*Codes_SRS_IOTHUB_MQTT_TRANSPORT_12_002 : [If the MQTT transport has been previously subscribed to DEVICE_METHOD topic IoTHubTransport_MQTT_Common_Subscribe_DeviceMethod shall do nothing and return 0.]*/
AzureIoTClient 12:658ca6865de2 2090 result = 0;
AzureIoTClient 12:658ca6865de2 2091 }
AzureIoTClient 12:658ca6865de2 2092
AzureIoTClient 12:658ca6865de2 2093 if (result == 0)
AzureIoTClient 12:658ca6865de2 2094 {
AzureIoTClient 12:658ca6865de2 2095 /*Codes_SRS_IOTHUB_MQTT_TRANSPORT_12_005 : [IoTHubTransport_MQTT_Common_Subscribe_DeviceMethod shall schedule the send of the subscription.]*/
AzureIoTClient 12:658ca6865de2 2096 /*Codes_SRS_IOTHUB_MQTT_TRANSPORT_12_007 : [On success IoTHubTransport_MQTT_Common_Subscribe_DeviceMethod shall return 0.]*/
AzureIoTClient 12:658ca6865de2 2097 if (transport_data->currPacketState != CONNACK_TYPE &&
AzureIoTClient 12:658ca6865de2 2098 transport_data->currPacketState != CONNECT_TYPE &&
AzureIoTClient 12:658ca6865de2 2099 transport_data->currPacketState != DISCONNECT_TYPE &&
AzureIoTClient 12:658ca6865de2 2100 transport_data->currPacketState != PACKET_TYPE_ERROR)
AzureIoTClient 12:658ca6865de2 2101 {
AzureIoTClient 12:658ca6865de2 2102 transport_data->currPacketState = SUBSCRIBE_TYPE;
AzureIoTClient 12:658ca6865de2 2103 }
AzureIoTClient 12:658ca6865de2 2104 }
AzureIoTClient 12:658ca6865de2 2105 }
AzureIoTClient 12:658ca6865de2 2106 return result;
AzureIoTClient 12:658ca6865de2 2107 }
AzureIoTClient 12:658ca6865de2 2108
AzureIoTClient 12:658ca6865de2 2109 void IoTHubTransport_MQTT_Common_Unsubscribe_DeviceMethod(IOTHUB_DEVICE_HANDLE handle)
AzureIoTClient 12:658ca6865de2 2110 {
AzureIoTClient 12:658ca6865de2 2111 PMQTTTRANSPORT_HANDLE_DATA transport_data = (PMQTTTRANSPORT_HANDLE_DATA)handle;
AzureIoTClient 12:658ca6865de2 2112 /*Codes_SRS_IOTHUB_MQTT_TRANSPORT_12_008 : [If the parameter handle is NULL than IoTHubTransport_MQTT_Common_Unsubscribe_DeviceMethod shall do nothing and return.]*/
AzureIoTClient 12:658ca6865de2 2113 if (transport_data != NULL)
AzureIoTClient 12:658ca6865de2 2114 {
AzureIoTClient 12:658ca6865de2 2115 /*Codes_SRS_IOTHUB_MQTT_TRANSPORT_12_009 : [If the MQTT transport has not been subscribed to DEVICE_METHOD topic IoTHubTransport_MQTT_Common_Unsubscribe_DeviceMethod shall do nothing and return.]*/
AzureIoTClient 12:658ca6865de2 2116 if (transport_data->topic_DeviceMethods != NULL)
AzureIoTClient 12:658ca6865de2 2117 {
AzureIoTClient 12:658ca6865de2 2118 /*Codes_SRS_IOTHUB_MQTT_TRANSPORT_12_010 : [IoTHubTransport_MQTT_Common_Unsubscribe_DeviceMethod shall construct the DEVICE_METHOD topic string for unsubscribe.]*/
AzureIoTClient 12:658ca6865de2 2119 const char* unsubscribe[1];
AzureIoTClient 12:658ca6865de2 2120 unsubscribe[0] = STRING_c_str(transport_data->topic_DeviceMethods);
AzureIoTClient 12:658ca6865de2 2121
AzureIoTClient 12:658ca6865de2 2122 /*Codes_SRS_IOTHUB_MQTT_TRANSPORT_12_011 : [IoTHubTransport_MQTT_Common_Unsubscribe_DeviceMethod shall send the unsubscribe.]*/
AzureIoTClient 12:658ca6865de2 2123 if (mqtt_client_unsubscribe(transport_data->mqttClient, get_next_packet_id(transport_data), unsubscribe, 1) != 0)
AzureIoTClient 12:658ca6865de2 2124 {
AzureIoTClient 12:658ca6865de2 2125 LogError("Failure calling mqtt_client_unsubscribe");
AzureIoTClient 12:658ca6865de2 2126 }
AzureIoTClient 12:658ca6865de2 2127
AzureIoTClient 12:658ca6865de2 2128 /*Codes_SRS_IOTHUB_MQTT_TRANSPORT_12_012 : [IoTHubTransport_MQTT_Common_Unsubscribe_DeviceMethod shall removes the signaling flag for DEVICE_METHOD topic from the receiver's topic list. ]*/
AzureIoTClient 12:658ca6865de2 2129 STRING_delete(transport_data->topic_DeviceMethods);
AzureIoTClient 12:658ca6865de2 2130 transport_data->topic_DeviceMethods = NULL;
AzureIoTClient 12:658ca6865de2 2131 transport_data->topics_ToSubscribe &= ~SUBSCRIBE_DEVICE_METHOD_TOPIC;
AzureIoTClient 12:658ca6865de2 2132 }
AzureIoTClient 12:658ca6865de2 2133 }
AzureIoTClient 12:658ca6865de2 2134 else
AzureIoTClient 12:658ca6865de2 2135 {
AzureIoTClient 12:658ca6865de2 2136 LogError("Invalid argument to unsubscribe (NULL).");
AzureIoTClient 12:658ca6865de2 2137 }
AzureIoTClient 12:658ca6865de2 2138 }
AzureIoTClient 12:658ca6865de2 2139
AzureIoTClient 14:4dc2b011be33 2140 int IoTHubTransport_MQTT_Common_DeviceMethod_Response(IOTHUB_DEVICE_HANDLE handle, METHOD_HANDLE methodId, const unsigned char* response, size_t respSize, int status)
AzureIoTClient 14:4dc2b011be33 2141 {
AzureIoTClient 14:4dc2b011be33 2142 int result;
AzureIoTClient 14:4dc2b011be33 2143 MQTTTRANSPORT_HANDLE_DATA* transport_data = (MQTTTRANSPORT_HANDLE_DATA*)handle;
AzureIoTClient 14:4dc2b011be33 2144 if (transport_data != NULL)
AzureIoTClient 14:4dc2b011be33 2145 {
AzureIoTClient 14:4dc2b011be33 2146 /* Codes_SRS_IOTHUB_TRANSPORT_MQTT_COMMON_07_042: [ IoTHubTransport_MQTT_Common_DeviceMethod_Response shall publish an mqtt message for the device method response. ] */
AzureIoTClient 14:4dc2b011be33 2147 DEVICE_METHOD_INFO* dev_method_info = (DEVICE_METHOD_INFO*)methodId;
AzureIoTClient 14:4dc2b011be33 2148 if (dev_method_info == NULL)
AzureIoTClient 14:4dc2b011be33 2149 {
AzureIoTClient 14:4dc2b011be33 2150 LogError("Failure: DEVICE_METHOD_INFO was NULL");
AzureIoTClient 18:ec8e5e97c6a4 2151 result = __FAILURE__;
AzureIoTClient 14:4dc2b011be33 2152 }
AzureIoTClient 14:4dc2b011be33 2153 else
AzureIoTClient 14:4dc2b011be33 2154 {
AzureIoTClient 14:4dc2b011be33 2155 if (publish_device_method_message(transport_data, status, dev_method_info->request_id, response, respSize) != 0)
AzureIoTClient 14:4dc2b011be33 2156 {
AzureIoTClient 14:4dc2b011be33 2157 /* Codes_SRS_IOTHUB_TRANSPORT_MQTT_COMMON_07_051: [ If any error is encountered, IoTHubTransport_MQTT_Common_DeviceMethod_Response shall return a non-zero value. ] */
AzureIoTClient 14:4dc2b011be33 2158 LogError("Failure: publishing device method response");
AzureIoTClient 18:ec8e5e97c6a4 2159 result = __FAILURE__;
AzureIoTClient 14:4dc2b011be33 2160 }
AzureIoTClient 14:4dc2b011be33 2161 else
AzureIoTClient 14:4dc2b011be33 2162 {
AzureIoTClient 14:4dc2b011be33 2163 result = 0;
AzureIoTClient 14:4dc2b011be33 2164 }
AzureIoTClient 14:4dc2b011be33 2165 STRING_delete(dev_method_info->request_id);
AzureIoTClient 14:4dc2b011be33 2166 free(dev_method_info);
AzureIoTClient 14:4dc2b011be33 2167 }
AzureIoTClient 14:4dc2b011be33 2168 }
AzureIoTClient 14:4dc2b011be33 2169 else
AzureIoTClient 14:4dc2b011be33 2170 {
AzureIoTClient 14:4dc2b011be33 2171 /* Codes_SRS_IOTHUB_TRANSPORT_MQTT_COMMON_07_041: [ If the parameter handle is NULL than IoTHubTransport_MQTT_Common_DeviceMethod_Response shall return a non-zero value. ] */
AzureIoTClient 18:ec8e5e97c6a4 2172 result = __FAILURE__;
AzureIoTClient 14:4dc2b011be33 2173 LogError("Failure: invalid IOTHUB_DEVICE_HANDLE parameter specified");
AzureIoTClient 14:4dc2b011be33 2174 }
AzureIoTClient 14:4dc2b011be33 2175 return result;
AzureIoTClient 14:4dc2b011be33 2176 }
AzureIoTClient 14:4dc2b011be33 2177
AzureIoTClient 11:31ebaeb51e1e 2178 int IoTHubTransport_MQTT_Common_Subscribe(IOTHUB_DEVICE_HANDLE handle)
AzureIoTClient 11:31ebaeb51e1e 2179 {
AzureIoTClient 11:31ebaeb51e1e 2180 int result;
AzureIoTClient 11:31ebaeb51e1e 2181 PMQTTTRANSPORT_HANDLE_DATA transport_data = (PMQTTTRANSPORT_HANDLE_DATA)handle;
AzureIoTClient 11:31ebaeb51e1e 2182 if (transport_data == NULL)
AzureIoTClient 11:31ebaeb51e1e 2183 {
AzureIoTClient 12:658ca6865de2 2184 /* Codes_SRS_IOTHUB_MQTT_TRANSPORT_07_015: [If parameter handle is NULL than IoTHubTransport_MQTT_Common_Subscribe shall return a non-zero value.] */
AzureIoTClient 11:31ebaeb51e1e 2185 LogError("Invalid handle parameter. NULL.");
AzureIoTClient 18:ec8e5e97c6a4 2186 result = __FAILURE__;
AzureIoTClient 11:31ebaeb51e1e 2187 }
AzureIoTClient 11:31ebaeb51e1e 2188 else
AzureIoTClient 11:31ebaeb51e1e 2189 {
AzureIoTClient 12:658ca6865de2 2190 /* Code_SRS_IOTHUB_MQTT_TRANSPORT_07_016: [IoTHubTransport_MQTT_Common_Subscribe shall set a flag to enable mqtt_client_subscribe to be called to subscribe to the Message Topic.] */
AzureIoTClient 11:31ebaeb51e1e 2191 transport_data->topic_MqttMessage = STRING_construct_sprintf(TOPIC_DEVICE_MSG, STRING_c_str(transport_data->device_id) );
AzureIoTClient 11:31ebaeb51e1e 2192 if (transport_data->topic_MqttMessage == NULL)
AzureIoTClient 11:31ebaeb51e1e 2193 {
AzureIoTClient 11:31ebaeb51e1e 2194 LogError("Failure constructing Message Topic");
AzureIoTClient 18:ec8e5e97c6a4 2195 result = __FAILURE__;
AzureIoTClient 11:31ebaeb51e1e 2196 }
AzureIoTClient 11:31ebaeb51e1e 2197 else
AzureIoTClient 11:31ebaeb51e1e 2198 {
AzureIoTClient 11:31ebaeb51e1e 2199 transport_data->topics_ToSubscribe |= SUBSCRIBE_TELEMETRY_TOPIC;
AzureIoTClient 12:658ca6865de2 2200 /* Code_SRS_IOTHUB_MQTT_TRANSPORT_07_035: [If current packet state is not CONNACT, DISCONNECT_TYPE, or PACKET_TYPE_ERROR then IoTHubTransport_MQTT_Common_Subscribe shall set the packet state to SUBSCRIBE_TYPE.]*/
AzureIoTClient 11:31ebaeb51e1e 2201 if (transport_data->currPacketState != CONNACK_TYPE &&
AzureIoTClient 11:31ebaeb51e1e 2202 transport_data->currPacketState != CONNECT_TYPE &&
AzureIoTClient 11:31ebaeb51e1e 2203 transport_data->currPacketState != DISCONNECT_TYPE &&
AzureIoTClient 11:31ebaeb51e1e 2204 transport_data->currPacketState != PACKET_TYPE_ERROR)
AzureIoTClient 11:31ebaeb51e1e 2205 {
AzureIoTClient 11:31ebaeb51e1e 2206 transport_data->currPacketState = SUBSCRIBE_TYPE;
AzureIoTClient 11:31ebaeb51e1e 2207 }
AzureIoTClient 11:31ebaeb51e1e 2208 result = 0;
AzureIoTClient 11:31ebaeb51e1e 2209 }
AzureIoTClient 11:31ebaeb51e1e 2210 }
AzureIoTClient 11:31ebaeb51e1e 2211 return result;
AzureIoTClient 11:31ebaeb51e1e 2212 }
AzureIoTClient 11:31ebaeb51e1e 2213
AzureIoTClient 11:31ebaeb51e1e 2214 void IoTHubTransport_MQTT_Common_Unsubscribe(IOTHUB_DEVICE_HANDLE handle)
AzureIoTClient 11:31ebaeb51e1e 2215 {
AzureIoTClient 11:31ebaeb51e1e 2216 PMQTTTRANSPORT_HANDLE_DATA transport_data = (PMQTTTRANSPORT_HANDLE_DATA)handle;
AzureIoTClient 12:658ca6865de2 2217 /* Codes_SRS_IOTHUB_MQTT_TRANSPORT_07_019: [If parameter handle is NULL then IoTHubTransport_MQTT_Common_Unsubscribe shall do nothing.] */
AzureIoTClient 11:31ebaeb51e1e 2218 if (transport_data != NULL)
AzureIoTClient 11:31ebaeb51e1e 2219 {
AzureIoTClient 12:658ca6865de2 2220 /* Codes_SRS_IOTHUB_MQTT_TRANSPORT_07_020: [IoTHubTransport_MQTT_Common_Unsubscribe shall call mqtt_client_unsubscribe to unsubscribe the mqtt message topic.] */
AzureIoTClient 11:31ebaeb51e1e 2221 const char* unsubscribe[1];
AzureIoTClient 11:31ebaeb51e1e 2222 unsubscribe[0] = STRING_c_str(transport_data->topic_MqttMessage);
AzureIoTClient 11:31ebaeb51e1e 2223 if (mqtt_client_unsubscribe(transport_data->mqttClient, get_next_packet_id(transport_data), unsubscribe, 1) != 0)
AzureIoTClient 11:31ebaeb51e1e 2224 {
AzureIoTClient 11:31ebaeb51e1e 2225 LogError("Failure calling mqtt_client_unsubscribe");
AzureIoTClient 11:31ebaeb51e1e 2226 }
AzureIoTClient 11:31ebaeb51e1e 2227 STRING_delete(transport_data->topic_MqttMessage);
AzureIoTClient 11:31ebaeb51e1e 2228 transport_data->topic_MqttMessage = NULL;
AzureIoTClient 11:31ebaeb51e1e 2229 transport_data->topics_ToSubscribe &= ~SUBSCRIBE_TELEMETRY_TOPIC;
AzureIoTClient 11:31ebaeb51e1e 2230 }
AzureIoTClient 11:31ebaeb51e1e 2231 else
AzureIoTClient 11:31ebaeb51e1e 2232 {
AzureIoTClient 11:31ebaeb51e1e 2233 LogError("Invalid argument to unsubscribe (NULL).");
AzureIoTClient 11:31ebaeb51e1e 2234 }
AzureIoTClient 11:31ebaeb51e1e 2235 }
AzureIoTClient 11:31ebaeb51e1e 2236
AzureIoTClient 12:658ca6865de2 2237 IOTHUB_PROCESS_ITEM_RESULT IoTHubTransport_MQTT_Common_ProcessItem(TRANSPORT_LL_HANDLE handle, IOTHUB_IDENTITY_TYPE item_type, IOTHUB_IDENTITY_INFO* iothub_item)
AzureIoTClient 12:658ca6865de2 2238 {
AzureIoTClient 12:658ca6865de2 2239 IOTHUB_PROCESS_ITEM_RESULT result;
AzureIoTClient 12:658ca6865de2 2240 /* Codes_SRS_IOTHUBCLIENT_LL_07_001: [ If handle or iothub_item are NULL then IoTHubTransport_MQTT_Common_ProcessItem shall return IOTHUB_PROCESS_ERROR. ]*/
AzureIoTClient 12:658ca6865de2 2241 if (handle == NULL || iothub_item == NULL)
AzureIoTClient 12:658ca6865de2 2242 {
AzureIoTClient 12:658ca6865de2 2243 LogError("Invalid handle parameter iothub_item=%p", iothub_item);
AzureIoTClient 12:658ca6865de2 2244 result = IOTHUB_PROCESS_ERROR;
AzureIoTClient 12:658ca6865de2 2245 }
AzureIoTClient 12:658ca6865de2 2246 else
AzureIoTClient 12:658ca6865de2 2247 {
AzureIoTClient 12:658ca6865de2 2248 PMQTTTRANSPORT_HANDLE_DATA transport_data = (PMQTTTRANSPORT_HANDLE_DATA)handle;
AzureIoTClient 12:658ca6865de2 2249
AzureIoTClient 12:658ca6865de2 2250 if (transport_data->currPacketState == PUBLISH_TYPE)
AzureIoTClient 12:658ca6865de2 2251 {
AzureIoTClient 12:658ca6865de2 2252 if (item_type == IOTHUB_TYPE_DEVICE_TWIN)
AzureIoTClient 12:658ca6865de2 2253 {
AzureIoTClient 12:658ca6865de2 2254 MQTT_DEVICE_TWIN_ITEM* mqtt_info = (MQTT_DEVICE_TWIN_ITEM*)malloc(sizeof(MQTT_DEVICE_TWIN_ITEM));
AzureIoTClient 12:658ca6865de2 2255 if (mqtt_info == NULL)
AzureIoTClient 12:658ca6865de2 2256 {
AzureIoTClient 12:658ca6865de2 2257 /* Codes_SRS_IOTHUBCLIENT_LL_07_004: [ If any errors are encountered IoTHubTransport_MQTT_Common_ProcessItem shall return IOTHUB_PROCESS_ERROR. ]*/
AzureIoTClient 12:658ca6865de2 2258 result = IOTHUB_PROCESS_ERROR;
AzureIoTClient 12:658ca6865de2 2259 }
AzureIoTClient 12:658ca6865de2 2260 else
AzureIoTClient 12:658ca6865de2 2261 {
AzureIoTClient 12:658ca6865de2 2262 /*Codes_SRS_IOTHUBCLIENT_LL_07_003: [ IoTHubTransport_MQTT_Common_ProcessItem shall publish a message to the mqtt protocol with the message topic for the message type.]*/
AzureIoTClient 12:658ca6865de2 2263 mqtt_info->iothub_type = item_type;
AzureIoTClient 12:658ca6865de2 2264 mqtt_info->iothub_msg_id = iothub_item->device_twin->item_id;
AzureIoTClient 12:658ca6865de2 2265 mqtt_info->retryCount = 0;
AzureIoTClient 12:658ca6865de2 2266
AzureIoTClient 12:658ca6865de2 2267 /* Codes_SRS_IOTHUBCLIENT_LL_07_005: [ If successful IoTHubTransport_MQTT_Common_ProcessItem shall add mqtt info structure acknowledgement queue. ] */
AzureIoTClient 12:658ca6865de2 2268 DList_InsertTailList(&transport_data->ack_waiting_queue, &mqtt_info->entry);
AzureIoTClient 12:658ca6865de2 2269
AzureIoTClient 12:658ca6865de2 2270 if (publish_device_twin_message(transport_data, iothub_item->device_twin, mqtt_info) != 0)
AzureIoTClient 12:658ca6865de2 2271 {
AzureIoTClient 12:658ca6865de2 2272 DList_RemoveEntryList(&mqtt_info->entry);
AzureIoTClient 12:658ca6865de2 2273
AzureIoTClient 12:658ca6865de2 2274 free(mqtt_info);
AzureIoTClient 12:658ca6865de2 2275 /* Codes_SRS_IOTHUBCLIENT_LL_07_004: [ If any errors are encountered IoTHubTransport_MQTT_Common_ProcessItem shall return IOTHUB_PROCESS_ERROR. ]*/
AzureIoTClient 12:658ca6865de2 2276 result = IOTHUB_PROCESS_ERROR;
AzureIoTClient 12:658ca6865de2 2277 }
AzureIoTClient 12:658ca6865de2 2278 else
AzureIoTClient 12:658ca6865de2 2279 {
AzureIoTClient 12:658ca6865de2 2280 result = IOTHUB_PROCESS_OK;
AzureIoTClient 12:658ca6865de2 2281 }
AzureIoTClient 12:658ca6865de2 2282 }
AzureIoTClient 12:658ca6865de2 2283 }
AzureIoTClient 12:658ca6865de2 2284 else
AzureIoTClient 12:658ca6865de2 2285 {
AzureIoTClient 12:658ca6865de2 2286 /* Codes_SRS_IOTHUBCLIENT_LL_07_006: [ If the item_type is not a supported type IoTHubTransport_MQTT_Common_ProcessItem shall return IOTHUB_PROCESS_CONTINUE. ]*/
AzureIoTClient 12:658ca6865de2 2287 result = IOTHUB_PROCESS_CONTINUE;
AzureIoTClient 12:658ca6865de2 2288 }
AzureIoTClient 12:658ca6865de2 2289 }
AzureIoTClient 12:658ca6865de2 2290 else
AzureIoTClient 12:658ca6865de2 2291 {
AzureIoTClient 12:658ca6865de2 2292 /* Codes_SRS_IOTHUBCLIENT_LL_07_002: [ If the mqtt is not ready to publish messages IoTHubTransport_MQTT_Common_ProcessItem shall return IOTHUB_PROCESS_NOT_CONNECTED. ] */
AzureIoTClient 12:658ca6865de2 2293 result = IOTHUB_PROCESS_NOT_CONNECTED;
AzureIoTClient 12:658ca6865de2 2294 }
AzureIoTClient 12:658ca6865de2 2295 }
AzureIoTClient 12:658ca6865de2 2296 return result;
AzureIoTClient 12:658ca6865de2 2297 }
AzureIoTClient 12:658ca6865de2 2298
AzureIoTClient 12:658ca6865de2 2299 /* Codes_SRS_IOTHUB_MQTT_TRANSPORT_07_054: [ IoTHubTransport_MQTT_Common_DoWork shall subscribe to the Notification and get_state Topics if they are defined. ] */
AzureIoTClient 11:31ebaeb51e1e 2300 void IoTHubTransport_MQTT_Common_DoWork(TRANSPORT_LL_HANDLE handle, IOTHUB_CLIENT_LL_HANDLE iotHubClientHandle)
AzureIoTClient 11:31ebaeb51e1e 2301 {
AzureIoTClient 12:658ca6865de2 2302 /* Codes_SRS_IOTHUB_MQTT_TRANSPORT_07_026: [IoTHubTransport_MQTT_Common_DoWork shall do nothing if parameter handle and/or iotHubClientHandle is NULL.] */
AzureIoTClient 11:31ebaeb51e1e 2303 PMQTTTRANSPORT_HANDLE_DATA transport_data = (PMQTTTRANSPORT_HANDLE_DATA)handle;
AzureIoTClient 11:31ebaeb51e1e 2304 if (transport_data != NULL && iotHubClientHandle != NULL)
AzureIoTClient 11:31ebaeb51e1e 2305 {
AzureIoTClient 11:31ebaeb51e1e 2306 transport_data->llClientHandle = iotHubClientHandle;
AzureIoTClient 11:31ebaeb51e1e 2307
AzureIoTClient 11:31ebaeb51e1e 2308 if (InitializeConnection(transport_data) != 0)
AzureIoTClient 11:31ebaeb51e1e 2309 {
AzureIoTClient 11:31ebaeb51e1e 2310 // Don't want to flood the logs with failures here
AzureIoTClient 11:31ebaeb51e1e 2311 }
AzureIoTClient 11:31ebaeb51e1e 2312 else
AzureIoTClient 11:31ebaeb51e1e 2313 {
AzureIoTClient 11:31ebaeb51e1e 2314 if (transport_data->currPacketState == CONNACK_TYPE || transport_data->currPacketState == SUBSCRIBE_TYPE)
AzureIoTClient 11:31ebaeb51e1e 2315 {
AzureIoTClient 11:31ebaeb51e1e 2316 SubscribeToMqttProtocol(transport_data);
AzureIoTClient 11:31ebaeb51e1e 2317 }
AzureIoTClient 11:31ebaeb51e1e 2318 else if (transport_data->currPacketState == SUBACK_TYPE)
AzureIoTClient 11:31ebaeb51e1e 2319 {
AzureIoTClient 12:658ca6865de2 2320 if ((transport_data->topic_NotifyState != NULL || transport_data->topic_GetState != NULL) &&
AzureIoTClient 12:658ca6865de2 2321 !transport_data->device_twin_get_sent)
AzureIoTClient 12:658ca6865de2 2322 {
AzureIoTClient 12:658ca6865de2 2323 /* Codes_SRS_IOTHUB_MQTT_TRANSPORT_07_055: [ IoTHubTransport_MQTT_Common_DoWork shall send a device twin get property message upon successfully retrieving a SUBACK on device twin topics. ] */
AzureIoTClient 12:658ca6865de2 2324 if (publish_device_twin_get_message(transport_data) == 0)
AzureIoTClient 12:658ca6865de2 2325 {
AzureIoTClient 12:658ca6865de2 2326 transport_data->device_twin_get_sent = true;
AzureIoTClient 12:658ca6865de2 2327 }
AzureIoTClient 12:658ca6865de2 2328 else
AzureIoTClient 12:658ca6865de2 2329 {
AzureIoTClient 12:658ca6865de2 2330 LogError("Failure: sending device twin get property command.");
AzureIoTClient 12:658ca6865de2 2331 }
AzureIoTClient 12:658ca6865de2 2332 }
AzureIoTClient 11:31ebaeb51e1e 2333 // Publish can be called now
AzureIoTClient 11:31ebaeb51e1e 2334 transport_data->currPacketState = PUBLISH_TYPE;
AzureIoTClient 11:31ebaeb51e1e 2335 }
AzureIoTClient 11:31ebaeb51e1e 2336 else if (transport_data->currPacketState == PUBLISH_TYPE)
AzureIoTClient 11:31ebaeb51e1e 2337 {
AzureIoTClient 11:31ebaeb51e1e 2338 PDLIST_ENTRY currentListEntry = transport_data->telemetry_waitingForAck.Flink;
AzureIoTClient 11:31ebaeb51e1e 2339 while (currentListEntry != &transport_data->telemetry_waitingForAck)
AzureIoTClient 11:31ebaeb51e1e 2340 {
AzureIoTClient 24:4096249decf1 2341 tickcounter_ms_t current_ms;
AzureIoTClient 11:31ebaeb51e1e 2342 MQTT_MESSAGE_DETAILS_LIST* mqttMsgEntry = containingRecord(currentListEntry, MQTT_MESSAGE_DETAILS_LIST, entry);
AzureIoTClient 11:31ebaeb51e1e 2343 DLIST_ENTRY nextListEntry;
AzureIoTClient 11:31ebaeb51e1e 2344 nextListEntry.Flink = currentListEntry->Flink;
AzureIoTClient 11:31ebaeb51e1e 2345
Azure.IoT.Build 13:606465879c57 2346 (void)tickcounter_get_current_ms(transport_data->msgTickCounter, &current_ms);
AzureIoTClient 12:658ca6865de2 2347 /* Codes_SRS_IOTHUB_MQTT_TRANSPORT_07_033: [IoTHubTransport_MQTT_Common_DoWork shall iterate through the Waiting Acknowledge messages looking for any message that has been waiting longer than 2 min.]*/
AzureIoTClient 11:31ebaeb51e1e 2348 if (((current_ms - mqttMsgEntry->msgPublishTime) / 1000) > RESEND_TIMEOUT_VALUE_MIN)
AzureIoTClient 11:31ebaeb51e1e 2349 {
AzureIoTClient 24:4096249decf1 2350 /* Codes_SRS_IOTHUB_MQTT_TRANSPORT_07_034: [If IoTHubTransport_MQTT_Common_DoWork has resent the message two times then it shall fail the message and reconnect to IoTHub ... ] */
AzureIoTClient 11:31ebaeb51e1e 2351 if (mqttMsgEntry->retryCount >= MAX_SEND_RECOUNT_LIMIT)
AzureIoTClient 11:31ebaeb51e1e 2352 {
AzureIoTClient 24:4096249decf1 2353 PDLIST_ENTRY current_entry;
AzureIoTClient 11:31ebaeb51e1e 2354 (void)DList_RemoveEntryList(currentListEntry);
AzureIoTClient 11:31ebaeb51e1e 2355 sendMsgComplete(mqttMsgEntry->iotHubMessageEntry, transport_data, IOTHUB_CLIENT_CONFIRMATION_MESSAGE_TIMEOUT);
AzureIoTClient 11:31ebaeb51e1e 2356 free(mqttMsgEntry);
AzureIoTClient 24:4096249decf1 2357
AzureIoTClient 24:4096249decf1 2358 transport_data->currPacketState = PACKET_TYPE_ERROR;
AzureIoTClient 24:4096249decf1 2359 transport_data->device_twin_get_sent = false;
AzureIoTClient 24:4096249decf1 2360 DisconnectFromClient(transport_data);
AzureIoTClient 24:4096249decf1 2361
AzureIoTClient 24:4096249decf1 2362 /* Codes_SRS_IOTHUB_TRANSPORT_MQTT_COMMON_07_057: [ ... then go through all the rest of the waiting messages and reset the retryCount on the message. ]*/
AzureIoTClient 24:4096249decf1 2363 current_entry = transport_data->telemetry_waitingForAck.Flink;
AzureIoTClient 24:4096249decf1 2364 while (current_entry != &transport_data->telemetry_waitingForAck)
AzureIoTClient 24:4096249decf1 2365 {
AzureIoTClient 24:4096249decf1 2366 MQTT_MESSAGE_DETAILS_LIST* msg_reset_entry;
AzureIoTClient 24:4096249decf1 2367 msg_reset_entry = containingRecord(current_entry, MQTT_MESSAGE_DETAILS_LIST, entry);
AzureIoTClient 24:4096249decf1 2368 msg_reset_entry->retryCount = 0;
AzureIoTClient 24:4096249decf1 2369 current_entry = current_entry->Flink;
AzureIoTClient 24:4096249decf1 2370 }
AzureIoTClient 11:31ebaeb51e1e 2371 }
AzureIoTClient 11:31ebaeb51e1e 2372 else
AzureIoTClient 11:31ebaeb51e1e 2373 {
AzureIoTClient 11:31ebaeb51e1e 2374 size_t messageLength;
AzureIoTClient 11:31ebaeb51e1e 2375 const unsigned char* messagePayload = RetrieveMessagePayload(mqttMsgEntry->iotHubMessageEntry->messageHandle, &messageLength);
AzureIoTClient 11:31ebaeb51e1e 2376 if (messageLength == 0 || messagePayload == NULL)
AzureIoTClient 11:31ebaeb51e1e 2377 {
AzureIoTClient 11:31ebaeb51e1e 2378 LogError("Failure from creating Message IoTHubMessage_GetData");
AzureIoTClient 11:31ebaeb51e1e 2379 }
AzureIoTClient 11:31ebaeb51e1e 2380 else
AzureIoTClient 11:31ebaeb51e1e 2381 {
AzureIoTClient 11:31ebaeb51e1e 2382 if (publish_mqtt_telemetry_msg(transport_data, mqttMsgEntry, messagePayload, messageLength) != 0)
AzureIoTClient 11:31ebaeb51e1e 2383 {
AzureIoTClient 11:31ebaeb51e1e 2384 (void)DList_RemoveEntryList(currentListEntry);
AzureIoTClient 11:31ebaeb51e1e 2385 sendMsgComplete(mqttMsgEntry->iotHubMessageEntry, transport_data, IOTHUB_CLIENT_CONFIRMATION_ERROR);
AzureIoTClient 11:31ebaeb51e1e 2386 free(mqttMsgEntry);
AzureIoTClient 11:31ebaeb51e1e 2387 }
AzureIoTClient 11:31ebaeb51e1e 2388 }
AzureIoTClient 11:31ebaeb51e1e 2389 }
AzureIoTClient 11:31ebaeb51e1e 2390 }
AzureIoTClient 11:31ebaeb51e1e 2391 currentListEntry = nextListEntry.Flink;
AzureIoTClient 11:31ebaeb51e1e 2392 }
AzureIoTClient 11:31ebaeb51e1e 2393
AzureIoTClient 11:31ebaeb51e1e 2394 currentListEntry = transport_data->waitingToSend->Flink;
AzureIoTClient 12:658ca6865de2 2395 /* Codes_SRS_IOTHUB_MQTT_TRANSPORT_07_027: [IoTHubTransport_MQTT_Common_DoWork shall inspect the "waitingToSend" DLIST passed in config structure.] */
AzureIoTClient 11:31ebaeb51e1e 2396 while (currentListEntry != transport_data->waitingToSend)
AzureIoTClient 11:31ebaeb51e1e 2397 {
AzureIoTClient 11:31ebaeb51e1e 2398 IOTHUB_MESSAGE_LIST* iothubMsgList = containingRecord(currentListEntry, IOTHUB_MESSAGE_LIST, entry);
AzureIoTClient 11:31ebaeb51e1e 2399 DLIST_ENTRY savedFromCurrentListEntry;
AzureIoTClient 11:31ebaeb51e1e 2400 savedFromCurrentListEntry.Flink = currentListEntry->Flink;
AzureIoTClient 11:31ebaeb51e1e 2401
AzureIoTClient 12:658ca6865de2 2402 /* Codes_SRS_IOTHUB_MQTT_TRANSPORT_07_027: [IoTHubTransport_MQTT_Common_DoWork shall inspect the "waitingToSend" DLIST passed in config structure.] */
AzureIoTClient 11:31ebaeb51e1e 2403 size_t messageLength;
AzureIoTClient 11:31ebaeb51e1e 2404 const unsigned char* messagePayload = RetrieveMessagePayload(iothubMsgList->messageHandle, &messageLength);
AzureIoTClient 11:31ebaeb51e1e 2405 if (messageLength == 0 || messagePayload == NULL)
AzureIoTClient 11:31ebaeb51e1e 2406 {
AzureIoTClient 11:31ebaeb51e1e 2407 LogError("Failure result from IoTHubMessage_GetData");
AzureIoTClient 11:31ebaeb51e1e 2408 }
AzureIoTClient 11:31ebaeb51e1e 2409 else
AzureIoTClient 11:31ebaeb51e1e 2410 {
AzureIoTClient 12:658ca6865de2 2411 /* Codes_SRS_IOTHUB_MQTT_TRANSPORT_07_029: [IoTHubTransport_MQTT_Common_DoWork shall create a MQTT_MESSAGE_HANDLE and pass this to a call to mqtt_client_publish.] */
AzureIoTClient 11:31ebaeb51e1e 2412 MQTT_MESSAGE_DETAILS_LIST* mqttMsgEntry = (MQTT_MESSAGE_DETAILS_LIST*)malloc(sizeof(MQTT_MESSAGE_DETAILS_LIST));
AzureIoTClient 11:31ebaeb51e1e 2413 if (mqttMsgEntry == NULL)
AzureIoTClient 11:31ebaeb51e1e 2414 {
AzureIoTClient 11:31ebaeb51e1e 2415 LogError("Allocation Error: Failure allocating MQTT Message Detail List.");
AzureIoTClient 11:31ebaeb51e1e 2416 }
AzureIoTClient 11:31ebaeb51e1e 2417 else
AzureIoTClient 11:31ebaeb51e1e 2418 {
AzureIoTClient 11:31ebaeb51e1e 2419 mqttMsgEntry->retryCount = 0;
AzureIoTClient 11:31ebaeb51e1e 2420 mqttMsgEntry->iotHubMessageEntry = iothubMsgList;
AzureIoTClient 11:31ebaeb51e1e 2421 mqttMsgEntry->packet_id = get_next_packet_id(transport_data);
AzureIoTClient 11:31ebaeb51e1e 2422 if (publish_mqtt_telemetry_msg(transport_data, mqttMsgEntry, messagePayload, messageLength) != 0)
AzureIoTClient 11:31ebaeb51e1e 2423 {
AzureIoTClient 11:31ebaeb51e1e 2424 (void)(DList_RemoveEntryList(currentListEntry));
AzureIoTClient 11:31ebaeb51e1e 2425 sendMsgComplete(iothubMsgList, transport_data, IOTHUB_CLIENT_CONFIRMATION_ERROR);
AzureIoTClient 11:31ebaeb51e1e 2426 free(mqttMsgEntry);
AzureIoTClient 11:31ebaeb51e1e 2427 }
AzureIoTClient 11:31ebaeb51e1e 2428 else
AzureIoTClient 11:31ebaeb51e1e 2429 {
AzureIoTClient 11:31ebaeb51e1e 2430 (void)(DList_RemoveEntryList(currentListEntry));
AzureIoTClient 11:31ebaeb51e1e 2431 DList_InsertTailList(&(transport_data->telemetry_waitingForAck), &(mqttMsgEntry->entry));
AzureIoTClient 11:31ebaeb51e1e 2432 }
AzureIoTClient 11:31ebaeb51e1e 2433 }
AzureIoTClient 11:31ebaeb51e1e 2434 }
AzureIoTClient 11:31ebaeb51e1e 2435 currentListEntry = savedFromCurrentListEntry.Flink;
AzureIoTClient 11:31ebaeb51e1e 2436 }
AzureIoTClient 11:31ebaeb51e1e 2437 }
AzureIoTClient 20:594780be216d 2438 /* Codes_SRS_IOTHUB_MQTT_TRANSPORT_07_030: [IoTHubTransport_MQTT_Common_DoWork shall call mqtt_client_dowork everytime it is called if it is connected.] */
AzureIoTClient 11:31ebaeb51e1e 2439 mqtt_client_dowork(transport_data->mqttClient);
AzureIoTClient 11:31ebaeb51e1e 2440 }
AzureIoTClient 11:31ebaeb51e1e 2441 }
AzureIoTClient 11:31ebaeb51e1e 2442 }
AzureIoTClient 11:31ebaeb51e1e 2443
AzureIoTClient 11:31ebaeb51e1e 2444 IOTHUB_CLIENT_RESULT IoTHubTransport_MQTT_Common_GetSendStatus(IOTHUB_DEVICE_HANDLE handle, IOTHUB_CLIENT_STATUS *iotHubClientStatus)
AzureIoTClient 11:31ebaeb51e1e 2445 {
AzureIoTClient 11:31ebaeb51e1e 2446 IOTHUB_CLIENT_RESULT result;
AzureIoTClient 11:31ebaeb51e1e 2447
AzureIoTClient 11:31ebaeb51e1e 2448 if (handle == NULL || iotHubClientStatus == NULL)
AzureIoTClient 11:31ebaeb51e1e 2449 {
AzureIoTClient 12:658ca6865de2 2450 /* Codes_SRS_IOTHUB_MQTT_TRANSPORT_07_023: [IoTHubTransport_MQTT_Common_GetSendStatus shall return IOTHUB_CLIENT_INVALID_ARG if called with NULL parameter.] */
AzureIoTClient 11:31ebaeb51e1e 2451 LogError("invalid arument.");
AzureIoTClient 11:31ebaeb51e1e 2452 result = IOTHUB_CLIENT_INVALID_ARG;
AzureIoTClient 11:31ebaeb51e1e 2453 }
AzureIoTClient 11:31ebaeb51e1e 2454 else
AzureIoTClient 11:31ebaeb51e1e 2455 {
AzureIoTClient 11:31ebaeb51e1e 2456 MQTTTRANSPORT_HANDLE_DATA* handleData = (MQTTTRANSPORT_HANDLE_DATA*)handle;
AzureIoTClient 11:31ebaeb51e1e 2457 if (!DList_IsListEmpty(handleData->waitingToSend) || !DList_IsListEmpty(&(handleData->telemetry_waitingForAck)))
AzureIoTClient 11:31ebaeb51e1e 2458 {
AzureIoTClient 12:658ca6865de2 2459 /* Codes_SRS_IOTHUB_MQTT_TRANSPORT_07_025: [IoTHubTransport_MQTT_Common_GetSendStatus shall return IOTHUB_CLIENT_OK and status IOTHUB_CLIENT_SEND_STATUS_BUSY if there are currently event items to be sent or being sent.] */
AzureIoTClient 11:31ebaeb51e1e 2460 *iotHubClientStatus = IOTHUB_CLIENT_SEND_STATUS_BUSY;
AzureIoTClient 11:31ebaeb51e1e 2461 }
AzureIoTClient 11:31ebaeb51e1e 2462 else
AzureIoTClient 11:31ebaeb51e1e 2463 {
AzureIoTClient 12:658ca6865de2 2464 /* Codes_SRS_IOTHUB_MQTT_TRANSPORT_07_024: [IoTHubTransport_MQTT_Common_GetSendStatus shall return IOTHUB_CLIENT_OK and status IOTHUB_CLIENT_SEND_STATUS_IDLE if there are currently no event items to be sent or being sent.] */
AzureIoTClient 11:31ebaeb51e1e 2465 *iotHubClientStatus = IOTHUB_CLIENT_SEND_STATUS_IDLE;
AzureIoTClient 11:31ebaeb51e1e 2466 }
AzureIoTClient 11:31ebaeb51e1e 2467 result = IOTHUB_CLIENT_OK;
AzureIoTClient 11:31ebaeb51e1e 2468 }
AzureIoTClient 11:31ebaeb51e1e 2469 return result;
AzureIoTClient 11:31ebaeb51e1e 2470 }
AzureIoTClient 11:31ebaeb51e1e 2471
AzureIoTClient 11:31ebaeb51e1e 2472 IOTHUB_CLIENT_RESULT IoTHubTransport_MQTT_Common_SetOption(TRANSPORT_LL_HANDLE handle, const char* option, const void* value)
AzureIoTClient 11:31ebaeb51e1e 2473 {
AzureIoTClient 12:658ca6865de2 2474 /* Codes_SRS_IOTHUB_MQTT_TRANSPORT_07_021: [If any parameter is NULL then IoTHubTransport_MQTT_Common_SetOption shall return IOTHUB_CLIENT_INVALID_ARG.] */
AzureIoTClient 11:31ebaeb51e1e 2475 IOTHUB_CLIENT_RESULT result;
AzureIoTClient 11:31ebaeb51e1e 2476 if (
AzureIoTClient 11:31ebaeb51e1e 2477 (handle == NULL) ||
AzureIoTClient 11:31ebaeb51e1e 2478 (option == NULL) ||
AzureIoTClient 11:31ebaeb51e1e 2479 (value == NULL)
AzureIoTClient 11:31ebaeb51e1e 2480 )
AzureIoTClient 11:31ebaeb51e1e 2481 {
AzureIoTClient 11:31ebaeb51e1e 2482 result = IOTHUB_CLIENT_INVALID_ARG;
AzureIoTClient 20:594780be216d 2483 LogError("invalid parameter (NULL) passed to IoTHubTransport_MQTT_Common_SetOption.");
AzureIoTClient 11:31ebaeb51e1e 2484 }
AzureIoTClient 11:31ebaeb51e1e 2485 else
AzureIoTClient 11:31ebaeb51e1e 2486 {
AzureIoTClient 11:31ebaeb51e1e 2487 MQTTTRANSPORT_HANDLE_DATA* transport_data = (MQTTTRANSPORT_HANDLE_DATA*)handle;
AzureIoTClient 21:167f8fcf6fb1 2488
AzureIoTClient 21:167f8fcf6fb1 2489 IOTHUB_CREDENTIAL_TYPE cred_type = IoTHubClient_Auth_Get_Credential_Type(transport_data->authorization_module);
AzureIoTClient 21:167f8fcf6fb1 2490
AzureIoTClient 11:31ebaeb51e1e 2491 /* Codes_SRS_IOTHUB_MQTT_TRANSPORT_07_031: [If the option parameter is set to "logtrace" then the value shall be a bool_ptr and the value will determine if the mqtt client log is on or off.] */
AzureIoTClient 11:31ebaeb51e1e 2492 if (strcmp(OPTION_LOG_TRACE, option) == 0)
AzureIoTClient 11:31ebaeb51e1e 2493 {
AzureIoTClient 11:31ebaeb51e1e 2494 transport_data->log_trace = *((bool*)value);
AzureIoTClient 11:31ebaeb51e1e 2495 mqtt_client_set_trace(transport_data->mqttClient, transport_data->log_trace, transport_data->raw_trace);
AzureIoTClient 11:31ebaeb51e1e 2496 result = IOTHUB_CLIENT_OK;
AzureIoTClient 11:31ebaeb51e1e 2497 }
AzureIoTClient 11:31ebaeb51e1e 2498 else if (strcmp("rawlogtrace", option) == 0)
AzureIoTClient 11:31ebaeb51e1e 2499 {
AzureIoTClient 11:31ebaeb51e1e 2500 transport_data->raw_trace = *((bool*)value);
AzureIoTClient 11:31ebaeb51e1e 2501 mqtt_client_set_trace(transport_data->mqttClient, transport_data->log_trace, transport_data->raw_trace);
AzureIoTClient 11:31ebaeb51e1e 2502 result = IOTHUB_CLIENT_OK;
AzureIoTClient 11:31ebaeb51e1e 2503 }
AzureIoTClient 11:31ebaeb51e1e 2504 else if (strcmp(OPTION_KEEP_ALIVE, option) == 0)
AzureIoTClient 11:31ebaeb51e1e 2505 {
AzureIoTClient 11:31ebaeb51e1e 2506 /* Codes_SRS_IOTHUB_MQTT_TRANSPORT_07_036: [If the option parameter is set to "keepalive" then the value shall be a int_ptr and the value will determine the mqtt keepalive time that is set for pings.] */
AzureIoTClient 11:31ebaeb51e1e 2507 int* keepAliveOption = (int*)value;
AzureIoTClient 12:658ca6865de2 2508 /* Codes_SRS_IOTHUB_MQTT_TRANSPORT_07_037 : [If the option parameter is set to supplied int_ptr keepalive is the same value as the existing keepalive then IoTHubTransport_MQTT_Common_SetOption shall do nothing.] */
AzureIoTClient 11:31ebaeb51e1e 2509 if (*keepAliveOption != transport_data->keepAliveValue)
AzureIoTClient 11:31ebaeb51e1e 2510 {
AzureIoTClient 11:31ebaeb51e1e 2511 transport_data->keepAliveValue = (uint16_t)(*keepAliveOption);
AzureIoTClient 20:594780be216d 2512 if (transport_data->mqttClientStatus != MQTT_CLIENT_STATUS_NOT_CONNECTED)
AzureIoTClient 11:31ebaeb51e1e 2513 {
AzureIoTClient 20:594780be216d 2514 /* Codes_SRS_IOTHUB_MQTT_TRANSPORT_07_038: [If the client is connected when the keepalive is set then IoTHubTransport_MQTT_Common_SetOption shall disconnect and reconnect with the specified keepalive value.] */
AzureIoTClient 11:31ebaeb51e1e 2515 DisconnectFromClient(transport_data);
AzureIoTClient 11:31ebaeb51e1e 2516 }
AzureIoTClient 11:31ebaeb51e1e 2517 }
AzureIoTClient 11:31ebaeb51e1e 2518 result = IOTHUB_CLIENT_OK;
AzureIoTClient 11:31ebaeb51e1e 2519 }
AzureIoTClient 11:31ebaeb51e1e 2520 /* Codes_SRS_IOTHUB_MQTT_TRANSPORT_07_039: [If the option parameter is set to "x509certificate" then the value shall be a const char of the certificate to be used for x509.] */
AzureIoTClient 21:167f8fcf6fb1 2521 else if ((strcmp(OPTION_X509_CERT, option) == 0) && (cred_type != IOTHUB_CREDENTIAL_TYPE_X509 && cred_type != IOTHUB_CREDENTIAL_TYPE_UNKNOWN))
AzureIoTClient 11:31ebaeb51e1e 2522 {
AzureIoTClient 11:31ebaeb51e1e 2523 LogError("x509certificate specified, but authentication method is not x509");
AzureIoTClient 11:31ebaeb51e1e 2524 result = IOTHUB_CLIENT_INVALID_ARG;
AzureIoTClient 11:31ebaeb51e1e 2525 }
AzureIoTClient 11:31ebaeb51e1e 2526 /* Codes_SRS_IOTHUB_MQTT_TRANSPORT_07_040: [If the option parameter is set to "x509privatekey" then the value shall be a const char of the RSA Private Key to be used for x509.] */
AzureIoTClient 21:167f8fcf6fb1 2527 else if ((strcmp(OPTION_X509_PRIVATE_KEY, option) == 0) && (cred_type != IOTHUB_CREDENTIAL_TYPE_X509 && cred_type != IOTHUB_CREDENTIAL_TYPE_UNKNOWN))
AzureIoTClient 11:31ebaeb51e1e 2528 {
AzureIoTClient 11:31ebaeb51e1e 2529 LogError("x509privatekey specified, but authentication method is not x509");
AzureIoTClient 11:31ebaeb51e1e 2530 result = IOTHUB_CLIENT_INVALID_ARG;
AzureIoTClient 11:31ebaeb51e1e 2531 }
AzureIoTClient 20:594780be216d 2532 else if (strcmp(OPTION_HTTP_PROXY, option) == 0)
AzureIoTClient 20:594780be216d 2533 {
AzureIoTClient 20:594780be216d 2534 /* Codes_SRS_IOTHUB_TRANSPORT_MQTT_COMMON_01_001: [ If `option` is `proxy_data`, `value` shall be used as an `HTTP_PROXY_OPTIONS*`. ]*/
AzureIoTClient 20:594780be216d 2535 HTTP_PROXY_OPTIONS* proxy_options = (HTTP_PROXY_OPTIONS*)value;
AzureIoTClient 20:594780be216d 2536
AzureIoTClient 20:594780be216d 2537 if (transport_data->xioTransport != NULL)
AzureIoTClient 20:594780be216d 2538 {
AzureIoTClient 20:594780be216d 2539 /* Codes_SRS_IOTHUB_TRANSPORT_MQTT_COMMON_01_007: [ If the underlying IO has already been created, then `IoTHubTransport_MQTT_Common_SetOption` shall fail and return `IOTHUB_CLIENT_ERROR`. ]*/
AzureIoTClient 20:594780be216d 2540 LogError("Cannot set proxy option once the underlying IO is created");
AzureIoTClient 20:594780be216d 2541 result = IOTHUB_CLIENT_ERROR;
AzureIoTClient 20:594780be216d 2542 }
AzureIoTClient 20:594780be216d 2543 else if (proxy_options->host_address == NULL)
AzureIoTClient 20:594780be216d 2544 {
AzureIoTClient 20:594780be216d 2545 /* Codes_SRS_IOTHUB_TRANSPORT_MQTT_COMMON_01_003: [ If `host_address` is NULL, `IoTHubTransport_MQTT_Common_SetOption` shall fail and return `IOTHUB_CLIENT_INVALID_ARG`. ]*/
AzureIoTClient 20:594780be216d 2546 LogError("NULL host_address in proxy options");
AzureIoTClient 20:594780be216d 2547 result = IOTHUB_CLIENT_INVALID_ARG;
AzureIoTClient 20:594780be216d 2548 }
AzureIoTClient 20:594780be216d 2549 /* Codes_SRS_IOTHUB_TRANSPORT_MQTT_COMMON_01_006: [ If only one of `username` and `password` is NULL, `IoTHubTransport_MQTT_Common_SetOption` shall fail and return `IOTHUB_CLIENT_INVALID_ARG`. ]*/
AzureIoTClient 20:594780be216d 2550 else if (((proxy_options->username == NULL) || (proxy_options->password == NULL)) &&
AzureIoTClient 20:594780be216d 2551 (proxy_options->username != proxy_options->password))
AzureIoTClient 20:594780be216d 2552 {
AzureIoTClient 20:594780be216d 2553 LogError("Only one of username and password for proxy settings was NULL");
AzureIoTClient 20:594780be216d 2554 result = IOTHUB_CLIENT_INVALID_ARG;
AzureIoTClient 20:594780be216d 2555 }
AzureIoTClient 20:594780be216d 2556 else
AzureIoTClient 20:594780be216d 2557 {
AzureIoTClient 20:594780be216d 2558 char* copied_proxy_hostname = NULL;
AzureIoTClient 20:594780be216d 2559 char* copied_proxy_username = NULL;
AzureIoTClient 20:594780be216d 2560 char* copied_proxy_password = NULL;
AzureIoTClient 20:594780be216d 2561
AzureIoTClient 20:594780be216d 2562 /* Codes_SRS_IOTHUB_TRANSPORT_MQTT_COMMON_01_002: [ The fields `host_address`, `port`, `username` and `password` shall be saved for later used (needed when creating the underlying IO to be used by the transport). ]*/
AzureIoTClient 20:594780be216d 2563 transport_data->http_proxy_port = proxy_options->port;
AzureIoTClient 20:594780be216d 2564 if (mallocAndStrcpy_s(&copied_proxy_hostname, proxy_options->host_address) != 0)
AzureIoTClient 20:594780be216d 2565 {
AzureIoTClient 20:594780be216d 2566 /* Codes_SRS_IOTHUB_TRANSPORT_MQTT_COMMON_01_004: [ If copying `host_address`, `username` or `password` fails, `IoTHubTransport_MQTT_Common_SetOption` shall fail and return `IOTHUB_CLIENT_ERROR`. ]*/
AzureIoTClient 20:594780be216d 2567 LogError("Cannot copy HTTP proxy hostname");
AzureIoTClient 20:594780be216d 2568 result = IOTHUB_CLIENT_ERROR;
AzureIoTClient 20:594780be216d 2569 }
AzureIoTClient 20:594780be216d 2570 /* Codes_SRS_IOTHUB_TRANSPORT_MQTT_COMMON_01_005: [ `username` and `password` shall be allowed to be NULL. ]*/
AzureIoTClient 20:594780be216d 2571 else if ((proxy_options->username != NULL) && (mallocAndStrcpy_s(&copied_proxy_username, proxy_options->username) != 0))
AzureIoTClient 20:594780be216d 2572 {
AzureIoTClient 20:594780be216d 2573 /* Codes_SRS_IOTHUB_TRANSPORT_MQTT_COMMON_01_004: [ If copying `host_address`, `username` or `password` fails, `IoTHubTransport_MQTT_Common_SetOption` shall fail and return `IOTHUB_CLIENT_ERROR`. ]*/
AzureIoTClient 20:594780be216d 2574 free(copied_proxy_hostname);
AzureIoTClient 20:594780be216d 2575 LogError("Cannot copy HTTP proxy username");
AzureIoTClient 20:594780be216d 2576 result = IOTHUB_CLIENT_ERROR;
AzureIoTClient 20:594780be216d 2577 }
AzureIoTClient 20:594780be216d 2578 /* Codes_SRS_IOTHUB_TRANSPORT_MQTT_COMMON_01_005: [ `username` and `password` shall be allowed to be NULL. ]*/
AzureIoTClient 20:594780be216d 2579 else if ((proxy_options->password != NULL) && (mallocAndStrcpy_s(&copied_proxy_password, proxy_options->password) != 0))
AzureIoTClient 20:594780be216d 2580 {
AzureIoTClient 20:594780be216d 2581 /* Codes_SRS_IOTHUB_TRANSPORT_MQTT_COMMON_01_004: [ If copying `host_address`, `username` or `password` fails, `IoTHubTransport_MQTT_Common_SetOption` shall fail and return `IOTHUB_CLIENT_ERROR`. ]*/
AzureIoTClient 20:594780be216d 2582 if (copied_proxy_username != NULL)
AzureIoTClient 20:594780be216d 2583 {
AzureIoTClient 20:594780be216d 2584 free(copied_proxy_username);
AzureIoTClient 20:594780be216d 2585 }
AzureIoTClient 20:594780be216d 2586 free(copied_proxy_hostname);
AzureIoTClient 20:594780be216d 2587 LogError("Cannot copy HTTP proxy password");
AzureIoTClient 20:594780be216d 2588 result = IOTHUB_CLIENT_ERROR;
AzureIoTClient 20:594780be216d 2589 }
AzureIoTClient 20:594780be216d 2590 else
AzureIoTClient 20:594780be216d 2591 {
AzureIoTClient 20:594780be216d 2592 /* Codes_SRS_IOTHUB_TRANSPORT_MQTT_COMMON_01_009: [ When setting the proxy options succeeds any previously saved proxy options shall be freed. ]*/
AzureIoTClient 20:594780be216d 2593 free_proxy_data(transport_data);
AzureIoTClient 20:594780be216d 2594
AzureIoTClient 20:594780be216d 2595 transport_data->http_proxy_hostname = copied_proxy_hostname;
AzureIoTClient 20:594780be216d 2596 transport_data->http_proxy_username = copied_proxy_username;
AzureIoTClient 20:594780be216d 2597 transport_data->http_proxy_password = copied_proxy_password;
AzureIoTClient 20:594780be216d 2598
AzureIoTClient 20:594780be216d 2599 /* Codes_SRS_IOTHUB_TRANSPORT_MQTT_COMMON_01_008: [ If setting the `proxy_data` option suceeds, `IoTHubTransport_MQTT_Common_SetOption` shall return `IOTHUB_CLIENT_OK` ]*/
AzureIoTClient 20:594780be216d 2600 result = IOTHUB_CLIENT_OK;
AzureIoTClient 20:594780be216d 2601 }
AzureIoTClient 20:594780be216d 2602 }
AzureIoTClient 20:594780be216d 2603 }
AzureIoTClient 11:31ebaeb51e1e 2604 else
AzureIoTClient 11:31ebaeb51e1e 2605 {
AzureIoTClient 21:167f8fcf6fb1 2606 /* Codes_SRS_IOTHUB_MQTT_TRANSPORT_07_039: [If the option parameter is set to "x509certificate" then the value shall be a const char of the certificate to be used for x509.] */
AzureIoTClient 21:167f8fcf6fb1 2607 if ( ((strcmp(OPTION_X509_CERT, option) == 0) || (strcmp(OPTION_X509_PRIVATE_KEY, option) == 0)) && (cred_type != IOTHUB_CREDENTIAL_TYPE_X509) )
AzureIoTClient 21:167f8fcf6fb1 2608 {
AzureIoTClient 21:167f8fcf6fb1 2609 IoTHubClient_Auth_Set_x509_Type(transport_data->authorization_module, true);
AzureIoTClient 21:167f8fcf6fb1 2610 }
AzureIoTClient 21:167f8fcf6fb1 2611
AzureIoTClient 12:658ca6865de2 2612 /* Codes_SRS_IOTHUB_MQTT_TRANSPORT_07_032: [IoTHubTransport_MQTT_Common_SetOption shall pass down the option to xio_setoption if the option parameter is not a known option string for the MQTT transport.] */
AzureIoTClient 11:31ebaeb51e1e 2613 if (GetTransportProviderIfNecessary(transport_data) == 0)
AzureIoTClient 11:31ebaeb51e1e 2614 {
AzureIoTClient 11:31ebaeb51e1e 2615 if (xio_setoption(transport_data->xioTransport, option, value) == 0)
AzureIoTClient 11:31ebaeb51e1e 2616 {
AzureIoTClient 11:31ebaeb51e1e 2617 result = IOTHUB_CLIENT_OK;
AzureIoTClient 11:31ebaeb51e1e 2618 }
AzureIoTClient 11:31ebaeb51e1e 2619 else
AzureIoTClient 11:31ebaeb51e1e 2620 {
AzureIoTClient 12:658ca6865de2 2621 /* Codes_SRS_IOTHUB_MQTT_TRANSPORT_07_132: [IoTHubTransport_MQTT_Common_SetOption shall return IOTHUB_CLIENT_INVALID_ARG xio_setoption fails] */
AzureIoTClient 11:31ebaeb51e1e 2622 result = IOTHUB_CLIENT_INVALID_ARG;
AzureIoTClient 11:31ebaeb51e1e 2623 }
AzureIoTClient 11:31ebaeb51e1e 2624 }
AzureIoTClient 11:31ebaeb51e1e 2625 else
AzureIoTClient 11:31ebaeb51e1e 2626 {
AzureIoTClient 11:31ebaeb51e1e 2627 result = IOTHUB_CLIENT_ERROR;
AzureIoTClient 11:31ebaeb51e1e 2628 }
AzureIoTClient 11:31ebaeb51e1e 2629 }
AzureIoTClient 11:31ebaeb51e1e 2630 }
AzureIoTClient 11:31ebaeb51e1e 2631 return result;
AzureIoTClient 11:31ebaeb51e1e 2632 }
AzureIoTClient 11:31ebaeb51e1e 2633
AzureIoTClient 11:31ebaeb51e1e 2634 IOTHUB_DEVICE_HANDLE IoTHubTransport_MQTT_Common_Register(TRANSPORT_LL_HANDLE handle, const IOTHUB_DEVICE_CONFIG* device, IOTHUB_CLIENT_LL_HANDLE iotHubClientHandle, PDLIST_ENTRY waitingToSend)
AzureIoTClient 11:31ebaeb51e1e 2635 {
AzureIoTClient 11:31ebaeb51e1e 2636 IOTHUB_DEVICE_HANDLE result = NULL;
AzureIoTClient 11:31ebaeb51e1e 2637 (void)iotHubClientHandle;
AzureIoTClient 11:31ebaeb51e1e 2638
AzureIoTClient 12:658ca6865de2 2639 // Codes_SRS_IOTHUB_MQTT_TRANSPORT_17_001: [ IoTHubTransport_MQTT_Common_Register shall return NULL if the TRANSPORT_LL_HANDLE is NULL.]
AzureIoTClient 12:658ca6865de2 2640 // Codes_SRS_IOTHUB_MQTT_TRANSPORT_17_002: [ IoTHubTransport_MQTT_Common_Register shall return NULL if device or waitingToSend are NULL.]
AzureIoTClient 11:31ebaeb51e1e 2641 if ((handle == NULL) || (device == NULL) || (waitingToSend == NULL))
AzureIoTClient 11:31ebaeb51e1e 2642 {
AzureIoTClient 12:658ca6865de2 2643 LogError("IoTHubTransport_MQTT_Common_Register: handle, device or waitingToSend is NULL.");
AzureIoTClient 11:31ebaeb51e1e 2644 result = NULL;
AzureIoTClient 11:31ebaeb51e1e 2645 }
AzureIoTClient 11:31ebaeb51e1e 2646 else
AzureIoTClient 11:31ebaeb51e1e 2647 {
AzureIoTClient 11:31ebaeb51e1e 2648 MQTTTRANSPORT_HANDLE_DATA* transport_data = (MQTTTRANSPORT_HANDLE_DATA*)handle;
AzureIoTClient 11:31ebaeb51e1e 2649
AzureIoTClient 12:658ca6865de2 2650 // Codes_SRS_IOTHUB_MQTT_TRANSPORT_03_001: [ IoTHubTransport_MQTT_Common_Register shall return NULL if deviceId, or both deviceKey and deviceSasToken are NULL.]
AzureIoTClient 11:31ebaeb51e1e 2651 if (device->deviceId == NULL)
AzureIoTClient 11:31ebaeb51e1e 2652 {
AzureIoTClient 12:658ca6865de2 2653 LogError("IoTHubTransport_MQTT_Common_Register: deviceId is NULL.");
AzureIoTClient 11:31ebaeb51e1e 2654 result = NULL;
AzureIoTClient 11:31ebaeb51e1e 2655 }
AzureIoTClient 12:658ca6865de2 2656 // Codes_SRS_IOTHUB_MQTT_TRANSPORT_03_002: [ IoTHubTransport_MQTT_Common_Register shall return NULL if both deviceKey and deviceSasToken are provided.]
AzureIoTClient 11:31ebaeb51e1e 2657 else if ((device->deviceKey != NULL) && (device->deviceSasToken != NULL))
AzureIoTClient 11:31ebaeb51e1e 2658 {
AzureIoTClient 12:658ca6865de2 2659 LogError("IoTHubTransport_MQTT_Common_Register: Both deviceKey and deviceSasToken are defined. Only one can be used.");
AzureIoTClient 11:31ebaeb51e1e 2660 result = NULL;
AzureIoTClient 11:31ebaeb51e1e 2661 }
AzureIoTClient 11:31ebaeb51e1e 2662 else
AzureIoTClient 11:31ebaeb51e1e 2663 {
AzureIoTClient 12:658ca6865de2 2664 // Codes_SRS_IOTHUB_MQTT_TRANSPORT_17_003: [ IoTHubTransport_MQTT_Common_Register shall return NULL if deviceId or deviceKey do not match the deviceId and deviceKey passed in during IoTHubTransport_MQTT_Common_Create.]
AzureIoTClient 11:31ebaeb51e1e 2665 if (strcmp(STRING_c_str(transport_data->device_id), device->deviceId) != 0)
AzureIoTClient 11:31ebaeb51e1e 2666 {
AzureIoTClient 12:658ca6865de2 2667 LogError("IoTHubTransport_MQTT_Common_Register: deviceId does not match.");
AzureIoTClient 11:31ebaeb51e1e 2668 result = NULL;
AzureIoTClient 11:31ebaeb51e1e 2669 }
AzureIoTClient 21:167f8fcf6fb1 2670 else if (IoTHubClient_Auth_Get_Credential_Type(transport_data->authorization_module) == IOTHUB_CREDENTIAL_TYPE_DEVICE_KEY &&
AzureIoTClient 21:167f8fcf6fb1 2671 (strcmp(IoTHubClient_Auth_Get_DeviceKey(transport_data->authorization_module), device->deviceKey) != 0))
AzureIoTClient 11:31ebaeb51e1e 2672 {
AzureIoTClient 12:658ca6865de2 2673 LogError("IoTHubTransport_MQTT_Common_Register: deviceKey does not match.");
AzureIoTClient 11:31ebaeb51e1e 2674 result = NULL;
AzureIoTClient 11:31ebaeb51e1e 2675 }
AzureIoTClient 11:31ebaeb51e1e 2676 else
AzureIoTClient 11:31ebaeb51e1e 2677 {
AzureIoTClient 11:31ebaeb51e1e 2678 if (transport_data->isRegistered == true)
AzureIoTClient 11:31ebaeb51e1e 2679 {
AzureIoTClient 11:31ebaeb51e1e 2680 LogError("Transport already has device registered by id: [%s]", device->deviceId);
AzureIoTClient 11:31ebaeb51e1e 2681 result = NULL;
AzureIoTClient 11:31ebaeb51e1e 2682 }
AzureIoTClient 11:31ebaeb51e1e 2683 else
AzureIoTClient 11:31ebaeb51e1e 2684 {
AzureIoTClient 11:31ebaeb51e1e 2685 transport_data->isRegistered = true;
AzureIoTClient 12:658ca6865de2 2686 // Codes_SRS_IOTHUB_MQTT_TRANSPORT_17_004: [ IoTHubTransport_MQTT_Common_Register shall return the TRANSPORT_LL_HANDLE as the IOTHUB_DEVICE_HANDLE. ]
AzureIoTClient 11:31ebaeb51e1e 2687 result = (IOTHUB_DEVICE_HANDLE)handle;
AzureIoTClient 11:31ebaeb51e1e 2688 }
AzureIoTClient 11:31ebaeb51e1e 2689 }
AzureIoTClient 11:31ebaeb51e1e 2690 }
AzureIoTClient 11:31ebaeb51e1e 2691 }
AzureIoTClient 11:31ebaeb51e1e 2692
AzureIoTClient 11:31ebaeb51e1e 2693 return result;
AzureIoTClient 11:31ebaeb51e1e 2694 }
AzureIoTClient 11:31ebaeb51e1e 2695
AzureIoTClient 11:31ebaeb51e1e 2696 void IoTHubTransport_MQTT_Common_Unregister(IOTHUB_DEVICE_HANDLE deviceHandle)
AzureIoTClient 11:31ebaeb51e1e 2697 {
AzureIoTClient 14:4dc2b011be33 2698 // Codes_SRS_IOTHUB_MQTT_TRANSPORT_17_005: [ If deviceHandle is NULL `IoTHubTransport_MQTT_Common_Unregister` shall do nothing. ]
AzureIoTClient 11:31ebaeb51e1e 2699 if (deviceHandle != NULL)
AzureIoTClient 11:31ebaeb51e1e 2700 {
AzureIoTClient 11:31ebaeb51e1e 2701 MQTTTRANSPORT_HANDLE_DATA* transport_data = (MQTTTRANSPORT_HANDLE_DATA*)deviceHandle;
AzureIoTClient 11:31ebaeb51e1e 2702
AzureIoTClient 11:31ebaeb51e1e 2703 transport_data->isRegistered = false;
AzureIoTClient 11:31ebaeb51e1e 2704 }
AzureIoTClient 11:31ebaeb51e1e 2705 }
AzureIoTClient 11:31ebaeb51e1e 2706
AzureIoTClient 11:31ebaeb51e1e 2707 STRING_HANDLE IoTHubTransport_MQTT_Common_GetHostname(TRANSPORT_LL_HANDLE handle)
AzureIoTClient 11:31ebaeb51e1e 2708 {
AzureIoTClient 11:31ebaeb51e1e 2709 STRING_HANDLE result;
AzureIoTClient 12:658ca6865de2 2710 /*Codes_SRS_IOTHUB_MQTT_TRANSPORT_02_001: [ If handle is NULL then IoTHubTransport_MQTT_Common_GetHostname shall fail and return NULL. ]*/
AzureIoTClient 11:31ebaeb51e1e 2711 if (handle == NULL)
AzureIoTClient 11:31ebaeb51e1e 2712 {
AzureIoTClient 11:31ebaeb51e1e 2713 result = NULL;
AzureIoTClient 11:31ebaeb51e1e 2714 }
AzureIoTClient 22:07fec4d325b6 2715 /*Codes_SRS_IOTHUB_MQTT_TRANSPORT_02_002: [ Otherwise IoTHubTransport_MQTT_Common_GetHostname shall return a non-NULL STRING_HANDLE containg the hostname. ]*/
AzureIoTClient 22:07fec4d325b6 2716 else if ((result = STRING_clone(((MQTTTRANSPORT_HANDLE_DATA*)(handle))->hostAddress)) == NULL)
AzureIoTClient 11:31ebaeb51e1e 2717 {
AzureIoTClient 22:07fec4d325b6 2718 LogError("Cannot provide the target host name (STRING_clone failed).");
AzureIoTClient 11:31ebaeb51e1e 2719 }
AzureIoTClient 22:07fec4d325b6 2720
AzureIoTClient 11:31ebaeb51e1e 2721 return result;
AzureIoTClient 11:31ebaeb51e1e 2722 }
AzureIoTClient 19:f87dfe76bc70 2723
AzureIoTClient 19:f87dfe76bc70 2724 IOTHUB_CLIENT_RESULT IoTHubTransport_MQTT_Common_SendMessageDisposition(MESSAGE_CALLBACK_INFO* message_data, IOTHUBMESSAGE_DISPOSITION_RESULT disposition)
AzureIoTClient 19:f87dfe76bc70 2725 {
AzureIoTClient 19:f87dfe76bc70 2726 (void)disposition;
AzureIoTClient 19:f87dfe76bc70 2727
AzureIoTClient 19:f87dfe76bc70 2728 IOTHUB_CLIENT_RESULT result;
AzureIoTClient 19:f87dfe76bc70 2729 if (message_data)
AzureIoTClient 19:f87dfe76bc70 2730 {
AzureIoTClient 19:f87dfe76bc70 2731 if (message_data->messageHandle)
AzureIoTClient 19:f87dfe76bc70 2732 {
AzureIoTClient 19:f87dfe76bc70 2733 IoTHubMessage_Destroy(message_data->messageHandle);
AzureIoTClient 19:f87dfe76bc70 2734 result = IOTHUB_CLIENT_OK;
AzureIoTClient 19:f87dfe76bc70 2735 }
AzureIoTClient 19:f87dfe76bc70 2736 else
AzureIoTClient 19:f87dfe76bc70 2737 {
AzureIoTClient 19:f87dfe76bc70 2738 /*Codes_SRS_IOTHUB_MQTT_TRANSPORT_10_002: [If any of the messageData fields are NULL, IoTHubTransport_MQTT_Common_SendMessageDisposition shall fail and return IOTHUB_CLIENT_ERROR. ]*/
AzureIoTClient 19:f87dfe76bc70 2739 LogError("message handle is NULL");
AzureIoTClient 19:f87dfe76bc70 2740 result = IOTHUB_CLIENT_ERROR;
AzureIoTClient 19:f87dfe76bc70 2741 }
AzureIoTClient 19:f87dfe76bc70 2742 free(message_data);
AzureIoTClient 19:f87dfe76bc70 2743 }
AzureIoTClient 19:f87dfe76bc70 2744 else
AzureIoTClient 19:f87dfe76bc70 2745 {
AzureIoTClient 19:f87dfe76bc70 2746 /*Codes_SRS_IOTHUB_MQTT_TRANSPORT_10_001: [If messageData is NULL, IoTHubTransport_MQTT_Common_SendMessageDisposition shall fail and return IOTHUB_CLIENT_ERROR. ]*/
AzureIoTClient 19:f87dfe76bc70 2747 LogError("message_data is NULL");
AzureIoTClient 19:f87dfe76bc70 2748 result = IOTHUB_CLIENT_ERROR;
AzureIoTClient 19:f87dfe76bc70 2749 }
AzureIoTClient 19:f87dfe76bc70 2750 return result;
AzureIoTClient 19:f87dfe76bc70 2751 }
AzureIoTClient 19:f87dfe76bc70 2752