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 11 14:02:18 2017 -0700
Revision:
26:0dae84ecce27
Parent:
25:a35763780a87
Child:
27:04de3c0bf1db
1.1.21

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