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 Jul 28 09:52:04 2017 -0700
Revision:
25:a35763780a87
Parent:
24:4096249decf1
Child:
26:0dae84ecce27
1.1.20

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 17:774695cb8554 1209 {
AzureIoTClient 17:774695cb8554 1210 break;
AzureIoTClient 17:774695cb8554 1211 }
AzureIoTClient 12:658ca6865de2 1212 }
AzureIoTClient 12:658ca6865de2 1213 }
AzureIoTClient 12:658ca6865de2 1214 }
AzureIoTClient 12:658ca6865de2 1215
AzureIoTClient 25:a35763780a87 1216 static void DisconnectFromClient(PMQTTTRANSPORT_HANDLE_DATA transport_data)
AzureIoTClient 25:a35763780a87 1217 {
AzureIoTClient 25:a35763780a87 1218 (void)mqtt_client_disconnect(transport_data->mqttClient);
AzureIoTClient 25:a35763780a87 1219 xio_destroy(transport_data->xioTransport);
AzureIoTClient 25:a35763780a87 1220 transport_data->xioTransport = NULL;
AzureIoTClient 25:a35763780a87 1221
AzureIoTClient 25:a35763780a87 1222 transport_data->mqttClientStatus = MQTT_CLIENT_STATUS_NOT_CONNECTED;
AzureIoTClient 25:a35763780a87 1223 transport_data->currPacketState = DISCONNECT_TYPE;
AzureIoTClient 25:a35763780a87 1224 }
AzureIoTClient 25:a35763780a87 1225
AzureIoTClient 12:658ca6865de2 1226 static void mqtt_error_callback(MQTT_CLIENT_HANDLE handle, MQTT_CLIENT_EVENT_ERROR error, void* callbackCtx)
AzureIoTClient 12:658ca6865de2 1227 {
AzureIoTClient 12:658ca6865de2 1228 (void)handle;
AzureIoTClient 12:658ca6865de2 1229 if (callbackCtx != NULL)
AzureIoTClient 12:658ca6865de2 1230 {
AzureIoTClient 12:658ca6865de2 1231 PMQTTTRANSPORT_HANDLE_DATA transport_data = (PMQTTTRANSPORT_HANDLE_DATA)callbackCtx;
AzureIoTClient 12:658ca6865de2 1232 switch (error)
AzureIoTClient 12:658ca6865de2 1233 {
AzureIoTClient 12:658ca6865de2 1234 case MQTT_CLIENT_CONNECTION_ERROR:
AzureIoTClient 11:31ebaeb51e1e 1235 {
AzureIoTClient 19:f87dfe76bc70 1236 IoTHubClient_LL_ConnectionStatusCallBack(transport_data->llClientHandle, IOTHUB_CLIENT_CONNECTION_UNAUTHENTICATED, IOTHUB_CLIENT_CONNECTION_NO_NETWORK);
AzureIoTClient 12:658ca6865de2 1237 break;
AzureIoTClient 12:658ca6865de2 1238 }
AzureIoTClient 20:594780be216d 1239 case MQTT_CLIENT_COMMUNICATION_ERROR:
AzureIoTClient 20:594780be216d 1240 {
AzureIoTClient 20:594780be216d 1241 IoTHubClient_LL_ConnectionStatusCallBack(transport_data->llClientHandle, IOTHUB_CLIENT_CONNECTION_UNAUTHENTICATED, IOTHUB_CLIENT_CONNECTION_COMMUNICATION_ERROR);
AzureIoTClient 20:594780be216d 1242 break;
AzureIoTClient 20:594780be216d 1243 }
AzureIoTClient 12:658ca6865de2 1244 case MQTT_CLIENT_NO_PING_RESPONSE:
AzureIoTClient 12:658ca6865de2 1245 {
AzureIoTClient 12:658ca6865de2 1246 LogError("Mqtt Ping Response was not encountered. Reconnecting device...");
AzureIoTClient 25:a35763780a87 1247 DisconnectFromClient(transport_data);
AzureIoTClient 12:658ca6865de2 1248 break;
AzureIoTClient 11:31ebaeb51e1e 1249 }
AzureIoTClient 17:774695cb8554 1250 case MQTT_CLIENT_PARSE_ERROR:
AzureIoTClient 17:774695cb8554 1251 case MQTT_CLIENT_MEMORY_ERROR:
AzureIoTClient 17:774695cb8554 1252 case MQTT_CLIENT_UNKNOWN_ERROR:
AzureIoTClient 17:774695cb8554 1253 {
AzureIoTClient 17:774695cb8554 1254 LogError("INTERNAL ERROR: unexpected error value received %s", ENUM_TO_STRING(MQTT_CLIENT_EVENT_ERROR, error));
AzureIoTClient 20:594780be216d 1255 break;
AzureIoTClient 17:774695cb8554 1256 }
AzureIoTClient 11:31ebaeb51e1e 1257 }
AzureIoTClient 20:594780be216d 1258 transport_data->mqttClientStatus = MQTT_CLIENT_STATUS_NOT_CONNECTED;
AzureIoTClient 12:658ca6865de2 1259 transport_data->currPacketState = PACKET_TYPE_ERROR;
Azure.IoT.Build 13:606465879c57 1260 transport_data->device_twin_get_sent = false;
AzureIoTClient 12:658ca6865de2 1261 if (transport_data->topic_MqttMessage != NULL)
AzureIoTClient 12:658ca6865de2 1262 {
Azure.IoT.Build 13:606465879c57 1263 transport_data->topics_ToSubscribe |= SUBSCRIBE_TELEMETRY_TOPIC;
Azure.IoT.Build 13:606465879c57 1264 }
Azure.IoT.Build 13:606465879c57 1265 if (transport_data->topic_GetState != NULL)
Azure.IoT.Build 13:606465879c57 1266 {
Azure.IoT.Build 13:606465879c57 1267 transport_data->topics_ToSubscribe |= SUBSCRIBE_GET_REPORTED_STATE_TOPIC;
AzureIoTClient 12:658ca6865de2 1268 }
Azure.IoT.Build 13:606465879c57 1269 if (transport_data->topic_NotifyState != NULL)
Azure.IoT.Build 13:606465879c57 1270 {
Azure.IoT.Build 13:606465879c57 1271 transport_data->topics_ToSubscribe |= SUBSCRIBE_NOTIFICATION_STATE_TOPIC;
Azure.IoT.Build 13:606465879c57 1272 }
Azure.IoT.Build 13:606465879c57 1273 if (transport_data->topic_DeviceMethods != NULL)
Azure.IoT.Build 13:606465879c57 1274 {
Azure.IoT.Build 13:606465879c57 1275 transport_data->topics_ToSubscribe |= SUBSCRIBE_DEVICE_METHOD_TOPIC;
Azure.IoT.Build 13:606465879c57 1276 }
Azure.IoT.Build 13:606465879c57 1277 }
AzureIoTClient 12:658ca6865de2 1278 else
AzureIoTClient 12:658ca6865de2 1279 {
AzureIoTClient 12:658ca6865de2 1280 LogError("Failure: mqtt called back with null context.");
AzureIoTClient 11:31ebaeb51e1e 1281 }
AzureIoTClient 11:31ebaeb51e1e 1282 }
AzureIoTClient 11:31ebaeb51e1e 1283
AzureIoTClient 11:31ebaeb51e1e 1284 static void SubscribeToMqttProtocol(PMQTTTRANSPORT_HANDLE_DATA transport_data)
AzureIoTClient 11:31ebaeb51e1e 1285 {
AzureIoTClient 11:31ebaeb51e1e 1286 if (transport_data->topics_ToSubscribe != UNSUBSCRIBE_FROM_TOPIC)
AzureIoTClient 11:31ebaeb51e1e 1287 {
AzureIoTClient 11:31ebaeb51e1e 1288 uint32_t topic_subscription = 0;
AzureIoTClient 11:31ebaeb51e1e 1289 size_t subscribe_count = 0;
AzureIoTClient 11:31ebaeb51e1e 1290 SUBSCRIBE_PAYLOAD subscribe[SUBSCRIBE_TOPIC_COUNT];
AzureIoTClient 11:31ebaeb51e1e 1291 if ((transport_data->topic_MqttMessage != NULL) && (SUBSCRIBE_TELEMETRY_TOPIC & transport_data->topics_ToSubscribe))
AzureIoTClient 11:31ebaeb51e1e 1292 {
AzureIoTClient 11:31ebaeb51e1e 1293 subscribe[subscribe_count].subscribeTopic = STRING_c_str(transport_data->topic_MqttMessage);
AzureIoTClient 11:31ebaeb51e1e 1294 subscribe[subscribe_count].qosReturn = DELIVER_AT_LEAST_ONCE;
AzureIoTClient 11:31ebaeb51e1e 1295 topic_subscription |= SUBSCRIBE_TELEMETRY_TOPIC;
AzureIoTClient 11:31ebaeb51e1e 1296 subscribe_count++;
AzureIoTClient 11:31ebaeb51e1e 1297 }
AzureIoTClient 12:658ca6865de2 1298 if ((transport_data->topic_GetState != NULL) && (SUBSCRIBE_GET_REPORTED_STATE_TOPIC & transport_data->topics_ToSubscribe))
AzureIoTClient 12:658ca6865de2 1299 {
AzureIoTClient 12:658ca6865de2 1300 subscribe[subscribe_count].subscribeTopic = STRING_c_str(transport_data->topic_GetState);
AzureIoTClient 12:658ca6865de2 1301 subscribe[subscribe_count].qosReturn = DELIVER_AT_MOST_ONCE;
AzureIoTClient 12:658ca6865de2 1302 topic_subscription |= SUBSCRIBE_GET_REPORTED_STATE_TOPIC;
AzureIoTClient 12:658ca6865de2 1303 subscribe_count++;
AzureIoTClient 12:658ca6865de2 1304 }
AzureIoTClient 12:658ca6865de2 1305 if ((transport_data->topic_NotifyState != NULL) && (SUBSCRIBE_NOTIFICATION_STATE_TOPIC & transport_data->topics_ToSubscribe))
AzureIoTClient 12:658ca6865de2 1306 {
AzureIoTClient 12:658ca6865de2 1307 subscribe[subscribe_count].subscribeTopic = STRING_c_str(transport_data->topic_NotifyState);
AzureIoTClient 12:658ca6865de2 1308 subscribe[subscribe_count].qosReturn = DELIVER_AT_MOST_ONCE;
AzureIoTClient 12:658ca6865de2 1309 topic_subscription |= SUBSCRIBE_NOTIFICATION_STATE_TOPIC;
AzureIoTClient 12:658ca6865de2 1310 subscribe_count++;
AzureIoTClient 12:658ca6865de2 1311 }
AzureIoTClient 12:658ca6865de2 1312 if ((transport_data->topic_DeviceMethods != NULL) && (SUBSCRIBE_DEVICE_METHOD_TOPIC & transport_data->topics_ToSubscribe))
AzureIoTClient 12:658ca6865de2 1313 {
AzureIoTClient 12:658ca6865de2 1314 subscribe[subscribe_count].subscribeTopic = STRING_c_str(transport_data->topic_DeviceMethods);
AzureIoTClient 12:658ca6865de2 1315 subscribe[subscribe_count].qosReturn = DELIVER_AT_MOST_ONCE;
AzureIoTClient 12:658ca6865de2 1316 topic_subscription |= SUBSCRIBE_DEVICE_METHOD_TOPIC;
AzureIoTClient 12:658ca6865de2 1317 subscribe_count++;
AzureIoTClient 12:658ca6865de2 1318 }
AzureIoTClient 11:31ebaeb51e1e 1319
AzureIoTClient 11:31ebaeb51e1e 1320 if (subscribe_count != 0)
AzureIoTClient 11:31ebaeb51e1e 1321 {
AzureIoTClient 12:658ca6865de2 1322 /* 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 1323 if (mqtt_client_subscribe(transport_data->mqttClient, get_next_packet_id(transport_data), subscribe, subscribe_count) != 0)
AzureIoTClient 11:31ebaeb51e1e 1324 {
AzureIoTClient 12:658ca6865de2 1325 /* Codes_SRS_IOTHUB_MQTT_TRANSPORT_07_017: [Upon failure IoTHubTransport_MQTT_Common_Subscribe shall return a non-zero value.] */
AzureIoTClient 11:31ebaeb51e1e 1326 LogError("Failure: mqtt_client_subscribe returned error.");
AzureIoTClient 11:31ebaeb51e1e 1327 }
AzureIoTClient 11:31ebaeb51e1e 1328 else
AzureIoTClient 11:31ebaeb51e1e 1329 {
AzureIoTClient 12:658ca6865de2 1330 /* Codes_SRS_IOTHUB_MQTT_TRANSPORT_07_018: [On success IoTHubTransport_MQTT_Common_Subscribe shall return 0.] */
AzureIoTClient 11:31ebaeb51e1e 1331 transport_data->topics_ToSubscribe &= ~topic_subscription;
AzureIoTClient 11:31ebaeb51e1e 1332 transport_data->currPacketState = SUBSCRIBE_TYPE;
AzureIoTClient 11:31ebaeb51e1e 1333 }
AzureIoTClient 11:31ebaeb51e1e 1334 }
AzureIoTClient 11:31ebaeb51e1e 1335 else
AzureIoTClient 11:31ebaeb51e1e 1336 {
AzureIoTClient 12:658ca6865de2 1337 /* Codes_SRS_IOTHUB_MQTT_TRANSPORT_07_017: [Upon failure IoTHubTransport_MQTT_Common_Subscribe shall return a non-zero value.] */
AzureIoTClient 11:31ebaeb51e1e 1338 LogError("Failure: subscribe_topic is empty.");
AzureIoTClient 11:31ebaeb51e1e 1339 }
AzureIoTClient 11:31ebaeb51e1e 1340 transport_data->currPacketState = SUBSCRIBE_TYPE;
AzureIoTClient 11:31ebaeb51e1e 1341 }
AzureIoTClient 11:31ebaeb51e1e 1342 else
AzureIoTClient 11:31ebaeb51e1e 1343 {
AzureIoTClient 11:31ebaeb51e1e 1344 transport_data->currPacketState = PUBLISH_TYPE;
AzureIoTClient 11:31ebaeb51e1e 1345 }
AzureIoTClient 11:31ebaeb51e1e 1346 }
AzureIoTClient 11:31ebaeb51e1e 1347
AzureIoTClient 11:31ebaeb51e1e 1348 static const unsigned char* RetrieveMessagePayload(IOTHUB_MESSAGE_HANDLE messageHandle, size_t* length)
AzureIoTClient 11:31ebaeb51e1e 1349 {
AzureIoTClient 11:31ebaeb51e1e 1350 const unsigned char* result;
AzureIoTClient 11:31ebaeb51e1e 1351
AzureIoTClient 11:31ebaeb51e1e 1352 IOTHUBMESSAGE_CONTENT_TYPE contentType = IoTHubMessage_GetContentType(messageHandle);
AzureIoTClient 11:31ebaeb51e1e 1353 if (contentType == IOTHUBMESSAGE_BYTEARRAY)
AzureIoTClient 11:31ebaeb51e1e 1354 {
AzureIoTClient 11:31ebaeb51e1e 1355 if (IoTHubMessage_GetByteArray(messageHandle, &result, length) != IOTHUB_MESSAGE_OK)
AzureIoTClient 11:31ebaeb51e1e 1356 {
AzureIoTClient 11:31ebaeb51e1e 1357 LogError("Failure result from IoTHubMessage_GetByteArray");
AzureIoTClient 11:31ebaeb51e1e 1358 result = NULL;
AzureIoTClient 11:31ebaeb51e1e 1359 *length = 0;
AzureIoTClient 11:31ebaeb51e1e 1360 }
AzureIoTClient 11:31ebaeb51e1e 1361 }
AzureIoTClient 11:31ebaeb51e1e 1362 else if (contentType == IOTHUBMESSAGE_STRING)
AzureIoTClient 11:31ebaeb51e1e 1363 {
AzureIoTClient 11:31ebaeb51e1e 1364 result = (const unsigned char*)IoTHubMessage_GetString(messageHandle);
AzureIoTClient 11:31ebaeb51e1e 1365 if (result == NULL)
AzureIoTClient 11:31ebaeb51e1e 1366 {
AzureIoTClient 11:31ebaeb51e1e 1367 LogError("Failure result from IoTHubMessage_GetString");
AzureIoTClient 11:31ebaeb51e1e 1368 result = NULL;
AzureIoTClient 11:31ebaeb51e1e 1369 *length = 0;
AzureIoTClient 11:31ebaeb51e1e 1370 }
AzureIoTClient 11:31ebaeb51e1e 1371 else
AzureIoTClient 11:31ebaeb51e1e 1372 {
AzureIoTClient 11:31ebaeb51e1e 1373 *length = strlen((const char*)result);
AzureIoTClient 11:31ebaeb51e1e 1374 }
AzureIoTClient 11:31ebaeb51e1e 1375 }
AzureIoTClient 11:31ebaeb51e1e 1376 else
AzureIoTClient 11:31ebaeb51e1e 1377 {
AzureIoTClient 11:31ebaeb51e1e 1378 result = NULL;
AzureIoTClient 11:31ebaeb51e1e 1379 *length = 0;
AzureIoTClient 11:31ebaeb51e1e 1380 }
AzureIoTClient 11:31ebaeb51e1e 1381 return result;
AzureIoTClient 11:31ebaeb51e1e 1382 }
AzureIoTClient 11:31ebaeb51e1e 1383
AzureIoTClient 11:31ebaeb51e1e 1384 static int GetTransportProviderIfNecessary(PMQTTTRANSPORT_HANDLE_DATA transport_data)
AzureIoTClient 11:31ebaeb51e1e 1385 {
AzureIoTClient 11:31ebaeb51e1e 1386 int result;
AzureIoTClient 11:31ebaeb51e1e 1387
AzureIoTClient 11:31ebaeb51e1e 1388 if (transport_data->xioTransport == NULL)
AzureIoTClient 11:31ebaeb51e1e 1389 {
AzureIoTClient 11:31ebaeb51e1e 1390 // construct address
AzureIoTClient 11:31ebaeb51e1e 1391 const char* hostAddress = STRING_c_str(transport_data->hostAddress);
AzureIoTClient 20:594780be216d 1392 MQTT_TRANSPORT_PROXY_OPTIONS mqtt_proxy_options;
AzureIoTClient 20:594780be216d 1393
AzureIoTClient 20:594780be216d 1394 /* 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 1395 mqtt_proxy_options.host_address = transport_data->http_proxy_hostname;
AzureIoTClient 20:594780be216d 1396 mqtt_proxy_options.port = transport_data->http_proxy_port;
AzureIoTClient 20:594780be216d 1397 mqtt_proxy_options.username = transport_data->http_proxy_username;
AzureIoTClient 20:594780be216d 1398 mqtt_proxy_options.password = transport_data->http_proxy_password;
AzureIoTClient 20:594780be216d 1399
AzureIoTClient 20:594780be216d 1400 /* 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 1401 transport_data->xioTransport = transport_data->get_io_transport(hostAddress, (transport_data->http_proxy_hostname == NULL) ? NULL : &mqtt_proxy_options);
AzureIoTClient 11:31ebaeb51e1e 1402 if (transport_data->xioTransport == NULL)
AzureIoTClient 11:31ebaeb51e1e 1403 {
AzureIoTClient 11:31ebaeb51e1e 1404 LogError("Unable to create the lower level TLS layer.");
AzureIoTClient 18:ec8e5e97c6a4 1405 result = __FAILURE__;
AzureIoTClient 11:31ebaeb51e1e 1406 }
AzureIoTClient 11:31ebaeb51e1e 1407 else
AzureIoTClient 11:31ebaeb51e1e 1408 {
AzureIoTClient 11:31ebaeb51e1e 1409 result = 0;
AzureIoTClient 11:31ebaeb51e1e 1410 }
AzureIoTClient 11:31ebaeb51e1e 1411 }
AzureIoTClient 11:31ebaeb51e1e 1412 else
AzureIoTClient 11:31ebaeb51e1e 1413 {
AzureIoTClient 11:31ebaeb51e1e 1414 result = 0;
AzureIoTClient 11:31ebaeb51e1e 1415 }
AzureIoTClient 11:31ebaeb51e1e 1416 return result;
AzureIoTClient 11:31ebaeb51e1e 1417 }
AzureIoTClient 11:31ebaeb51e1e 1418
AzureIoTClient 11:31ebaeb51e1e 1419 static int SendMqttConnectMsg(PMQTTTRANSPORT_HANDLE_DATA transport_data)
AzureIoTClient 11:31ebaeb51e1e 1420 {
AzureIoTClient 11:31ebaeb51e1e 1421 int result;
AzureIoTClient 11:31ebaeb51e1e 1422
AzureIoTClient 21:167f8fcf6fb1 1423 char* sasToken = NULL;
AzureIoTClient 21:167f8fcf6fb1 1424 result = 0;
AzureIoTClient 21:167f8fcf6fb1 1425
AzureIoTClient 21:167f8fcf6fb1 1426 IOTHUB_CREDENTIAL_TYPE cred_type = IoTHubClient_Auth_Get_Credential_Type(transport_data->authorization_module);
AzureIoTClient 21:167f8fcf6fb1 1427 if (cred_type == IOTHUB_CREDENTIAL_TYPE_DEVICE_KEY)
AzureIoTClient 11:31ebaeb51e1e 1428 {
AzureIoTClient 21:167f8fcf6fb1 1429 size_t secSinceEpoch = (size_t)(difftime(get_time(NULL), EPOCH_TIME_T_VALUE) + 0);
AzureIoTClient 21:167f8fcf6fb1 1430 size_t expiryTime = secSinceEpoch + SAS_TOKEN_DEFAULT_LIFETIME;
AzureIoTClient 21:167f8fcf6fb1 1431 sasToken = IoTHubClient_Auth_Get_SasToken(transport_data->authorization_module, STRING_c_str(transport_data->devicesPath), expiryTime);
AzureIoTClient 21:167f8fcf6fb1 1432 if (sasToken == NULL)
AzureIoTClient 21:167f8fcf6fb1 1433 {
AzureIoTClient 21:167f8fcf6fb1 1434 LogError("failure getting sas Token.");
AzureIoTClient 21:167f8fcf6fb1 1435 result = __FAILURE__;
AzureIoTClient 21:167f8fcf6fb1 1436 }
AzureIoTClient 11:31ebaeb51e1e 1437 }
AzureIoTClient 21:167f8fcf6fb1 1438 else if (cred_type == IOTHUB_CREDENTIAL_TYPE_SAS_TOKEN)
AzureIoTClient 11:31ebaeb51e1e 1439 {
AzureIoTClient 21:167f8fcf6fb1 1440 SAS_TOKEN_STATUS token_status = IoTHubClient_Auth_Is_SasToken_Valid(transport_data->authorization_module);
AzureIoTClient 21:167f8fcf6fb1 1441 if (token_status == SAS_TOKEN_STATUS_INVALID)
AzureIoTClient 21:167f8fcf6fb1 1442 {
AzureIoTClient 21:167f8fcf6fb1 1443 IoTHubClient_LL_ConnectionStatusCallBack(transport_data->llClientHandle, IOTHUB_CLIENT_CONNECTION_UNAUTHENTICATED, IOTHUB_CLIENT_CONNECTION_EXPIRED_SAS_TOKEN);
AzureIoTClient 21:167f8fcf6fb1 1444 result = __FAILURE__;
AzureIoTClient 21:167f8fcf6fb1 1445 }
AzureIoTClient 21:167f8fcf6fb1 1446 else if (token_status == SAS_TOKEN_STATUS_FAILED)
AzureIoTClient 11:31ebaeb51e1e 1447 {
AzureIoTClient 21:167f8fcf6fb1 1448 IoTHubClient_LL_ConnectionStatusCallBack(transport_data->llClientHandle, IOTHUB_CLIENT_CONNECTION_UNAUTHENTICATED, IOTHUB_CLIENT_CONNECTION_BAD_CREDENTIAL);
AzureIoTClient 21:167f8fcf6fb1 1449 result = __FAILURE__;
AzureIoTClient 21:167f8fcf6fb1 1450 }
AzureIoTClient 21:167f8fcf6fb1 1451 else
AzureIoTClient 21:167f8fcf6fb1 1452 {
AzureIoTClient 21:167f8fcf6fb1 1453 sasToken = IoTHubClient_Auth_Get_SasToken(transport_data->authorization_module, NULL, 0);
AzureIoTClient 21:167f8fcf6fb1 1454 if (sasToken == NULL)
AzureIoTClient 11:31ebaeb51e1e 1455 {
AzureIoTClient 21:167f8fcf6fb1 1456 LogError("failure getting sas Token.");
AzureIoTClient 18:ec8e5e97c6a4 1457 result = __FAILURE__;
AzureIoTClient 12:658ca6865de2 1458 }
AzureIoTClient 21:167f8fcf6fb1 1459 }
AzureIoTClient 21:167f8fcf6fb1 1460 }
AzureIoTClient 11:31ebaeb51e1e 1461
AzureIoTClient 21:167f8fcf6fb1 1462 if (result == 0)
AzureIoTClient 21:167f8fcf6fb1 1463 {
AzureIoTClient 24:4096249decf1 1464 if (!transport_data->isProductInfoSet)
AzureIoTClient 22:07fec4d325b6 1465 {
AzureIoTClient 24:4096249decf1 1466 // This requires the iothubClientHandle, which sadly the MQTT transport only gets on DoWork, so this code still needs to remain here.
AzureIoTClient 24:4096249decf1 1467 // The correct place for this would be in the Create method, but we don't get the client handle there.
AzureIoTClient 24:4096249decf1 1468 // Also, when device multiplexing is used, the customer creates the transport directly and explicitly, when the client is still not created.
AzureIoTClient 24:4096249decf1 1469 // This will be a major hurdle when we add device multiplexing to MQTT transport.
AzureIoTClient 24:4096249decf1 1470
AzureIoTClient 24:4096249decf1 1471 void* product_info;
AzureIoTClient 24:4096249decf1 1472 STRING_HANDLE clone;
AzureIoTClient 24:4096249decf1 1473 if ((IoTHubClient_LL_GetOption(transport_data->llClientHandle, OPTION_PRODUCT_INFO, &product_info) == IOTHUB_CLIENT_ERROR) || (product_info == NULL))
AzureIoTClient 24:4096249decf1 1474 {
AzureIoTClient 24:4096249decf1 1475 clone = STRING_construct_sprintf("%s%%2F%s", CLIENT_DEVICE_TYPE_PREFIX, IOTHUB_SDK_VERSION);
AzureIoTClient 24:4096249decf1 1476 }
AzureIoTClient 24:4096249decf1 1477 else
AzureIoTClient 24:4096249decf1 1478 {
AzureIoTClient 24:4096249decf1 1479 clone = URL_Encode(product_info);
AzureIoTClient 24:4096249decf1 1480 }
AzureIoTClient 24:4096249decf1 1481
AzureIoTClient 24:4096249decf1 1482 if (clone == NULL)
AzureIoTClient 24:4096249decf1 1483 {
AzureIoTClient 24:4096249decf1 1484 LogError("Failed obtaining the product info");
AzureIoTClient 24:4096249decf1 1485 }
AzureIoTClient 24:4096249decf1 1486 else
AzureIoTClient 24:4096249decf1 1487 {
AzureIoTClient 24:4096249decf1 1488 if (STRING_concat_with_STRING(transport_data->configPassedThroughUsername, clone) != 0)
AzureIoTClient 24:4096249decf1 1489 {
AzureIoTClient 24:4096249decf1 1490 LogError("Failed concatenating the product info");
AzureIoTClient 24:4096249decf1 1491 }
AzureIoTClient 24:4096249decf1 1492 else
AzureIoTClient 24:4096249decf1 1493 {
AzureIoTClient 24:4096249decf1 1494 transport_data->isProductInfoSet = true;
AzureIoTClient 24:4096249decf1 1495 }
AzureIoTClient 24:4096249decf1 1496
AzureIoTClient 24:4096249decf1 1497 STRING_delete(clone);
AzureIoTClient 24:4096249decf1 1498 }
AzureIoTClient 22:07fec4d325b6 1499 }
AzureIoTClient 22:07fec4d325b6 1500
AzureIoTClient 21:167f8fcf6fb1 1501 MQTT_CLIENT_OPTIONS options = { 0 };
AzureIoTClient 21:167f8fcf6fb1 1502 options.clientId = (char*)STRING_c_str(transport_data->device_id);
AzureIoTClient 21:167f8fcf6fb1 1503 options.willMessage = NULL;
AzureIoTClient 21:167f8fcf6fb1 1504 options.username = (char*)STRING_c_str(transport_data->configPassedThroughUsername);
AzureIoTClient 21:167f8fcf6fb1 1505 if (sasToken != NULL)
AzureIoTClient 11:31ebaeb51e1e 1506 {
AzureIoTClient 21:167f8fcf6fb1 1507 options.password = sasToken;
AzureIoTClient 21:167f8fcf6fb1 1508 }
AzureIoTClient 21:167f8fcf6fb1 1509 options.keepAliveInterval = transport_data->keepAliveValue;
AzureIoTClient 21:167f8fcf6fb1 1510 options.useCleanSession = false;
AzureIoTClient 21:167f8fcf6fb1 1511 options.qualityOfServiceValue = DELIVER_AT_LEAST_ONCE;
AzureIoTClient 11:31ebaeb51e1e 1512
AzureIoTClient 21:167f8fcf6fb1 1513 if (GetTransportProviderIfNecessary(transport_data) == 0)
AzureIoTClient 21:167f8fcf6fb1 1514 {
AzureIoTClient 21:167f8fcf6fb1 1515 if (mqtt_client_connect(transport_data->mqttClient, transport_data->xioTransport, &options) != 0)
AzureIoTClient 11:31ebaeb51e1e 1516 {
AzureIoTClient 24:4096249decf1 1517 LogError("failure connecting to address %s.", STRING_c_str(transport_data->hostAddress));
AzureIoTClient 21:167f8fcf6fb1 1518 result = __FAILURE__;
AzureIoTClient 11:31ebaeb51e1e 1519 }
AzureIoTClient 11:31ebaeb51e1e 1520 else
AzureIoTClient 11:31ebaeb51e1e 1521 {
AzureIoTClient 21:167f8fcf6fb1 1522 (void)tickcounter_get_current_ms(transport_data->msgTickCounter, &transport_data->mqtt_connect_time);
AzureIoTClient 21:167f8fcf6fb1 1523 result = 0;
AzureIoTClient 11:31ebaeb51e1e 1524 }
AzureIoTClient 21:167f8fcf6fb1 1525 }
AzureIoTClient 21:167f8fcf6fb1 1526 else
AzureIoTClient 21:167f8fcf6fb1 1527 {
AzureIoTClient 21:167f8fcf6fb1 1528 result = __FAILURE__;
AzureIoTClient 21:167f8fcf6fb1 1529 }
AzureIoTClient 12:658ca6865de2 1530
AzureIoTClient 21:167f8fcf6fb1 1531 if (sasToken != NULL)
AzureIoTClient 21:167f8fcf6fb1 1532 {
AzureIoTClient 21:167f8fcf6fb1 1533 free(sasToken);
AzureIoTClient 11:31ebaeb51e1e 1534 }
AzureIoTClient 11:31ebaeb51e1e 1535 }
AzureIoTClient 11:31ebaeb51e1e 1536 return result;
AzureIoTClient 11:31ebaeb51e1e 1537 }
AzureIoTClient 11:31ebaeb51e1e 1538
AzureIoTClient 11:31ebaeb51e1e 1539 static int InitializeConnection(PMQTTTRANSPORT_HANDLE_DATA transport_data)
AzureIoTClient 11:31ebaeb51e1e 1540 {
AzureIoTClient 11:31ebaeb51e1e 1541 int result = 0;
AzureIoTClient 11:31ebaeb51e1e 1542
AzureIoTClient 11:31ebaeb51e1e 1543 // Make sure we're not destroying the object
AzureIoTClient 11:31ebaeb51e1e 1544 if (!transport_data->isDestroyCalled)
AzureIoTClient 11:31ebaeb51e1e 1545 {
AzureIoTClient 25:a35763780a87 1546 RETRY_ACTION retry_action = RETRY_ACTION_RETRY_LATER;
AzureIoTClient 25:a35763780a87 1547
AzureIoTClient 25:a35763780a87 1548 // 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 1549 if (transport_data->mqttClientStatus == MQTT_CLIENT_STATUS_NOT_CONNECTED && transport_data->isRecoverableError &&
AzureIoTClient 25:a35763780a87 1550 (retry_control_should_retry(transport_data->retry_control_handle, &retry_action) != 0 || retry_action == RETRY_ACTION_RETRY_NOW))
AzureIoTClient 11:31ebaeb51e1e 1551 {
AzureIoTClient 25:a35763780a87 1552 // Note: in case retry_control_should_retry fails, the reconnection shall be attempted anyway (defaulting to policy IOTHUB_CLIENT_RETRY_IMMEDIATE).
AzureIoTClient 25:a35763780a87 1553
Azure.IoT.Build 13:606465879c57 1554 if (tickcounter_get_current_ms(transport_data->msgTickCounter, &transport_data->connectTick) != 0)
AzureIoTClient 11:31ebaeb51e1e 1555 {
AzureIoTClient 12:658ca6865de2 1556 transport_data->connectFailCount++;
AzureIoTClient 18:ec8e5e97c6a4 1557 result = __FAILURE__;
AzureIoTClient 11:31ebaeb51e1e 1558 }
AzureIoTClient 12:658ca6865de2 1559 else
AzureIoTClient 11:31ebaeb51e1e 1560 {
AzureIoTClient 12:658ca6865de2 1561 if (SendMqttConnectMsg(transport_data) != 0)
AzureIoTClient 11:31ebaeb51e1e 1562 {
AzureIoTClient 11:31ebaeb51e1e 1563 transport_data->connectFailCount++;
AzureIoTClient 18:ec8e5e97c6a4 1564 result = __FAILURE__;
AzureIoTClient 11:31ebaeb51e1e 1565 }
AzureIoTClient 11:31ebaeb51e1e 1566 else
AzureIoTClient 11:31ebaeb51e1e 1567 {
AzureIoTClient 20:594780be216d 1568 transport_data->mqttClientStatus = MQTT_CLIENT_STATUS_CONNECTING;
AzureIoTClient 12:658ca6865de2 1569 transport_data->connectFailCount = 0;
AzureIoTClient 12:658ca6865de2 1570 result = 0;
AzureIoTClient 11:31ebaeb51e1e 1571 }
AzureIoTClient 11:31ebaeb51e1e 1572 }
AzureIoTClient 11:31ebaeb51e1e 1573 }
AzureIoTClient 21:167f8fcf6fb1 1574 // 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 1575 else if (transport_data->mqttClientStatus == MQTT_CLIENT_STATUS_CONNECTING)
AzureIoTClient 11:31ebaeb51e1e 1576 {
AzureIoTClient 20:594780be216d 1577 tickcounter_ms_t current_time;
AzureIoTClient 20:594780be216d 1578 if (tickcounter_get_current_ms(transport_data->msgTickCounter, &current_time) != 0)
AzureIoTClient 20:594780be216d 1579 {
AzureIoTClient 20:594780be216d 1580 LogError("failed verifying MQTT_CLIENT_STATUS_CONNECTING timeout");
AzureIoTClient 20:594780be216d 1581 result = __FAILURE__;
AzureIoTClient 20:594780be216d 1582 }
AzureIoTClient 20:594780be216d 1583 else if ((current_time - transport_data->mqtt_connect_time) / 1000 > transport_data->keepAliveValue)
AzureIoTClient 20:594780be216d 1584 {
AzureIoTClient 20:594780be216d 1585 LogError("mqtt_client timed out waiting for CONNACK");
AzureIoTClient 20:594780be216d 1586 DisconnectFromClient(transport_data);
AzureIoTClient 20:594780be216d 1587 result = 0;
AzureIoTClient 20:594780be216d 1588 }
AzureIoTClient 20:594780be216d 1589 }
AzureIoTClient 20:594780be216d 1590 else if (transport_data->mqttClientStatus == MQTT_CLIENT_STATUS_CONNECTED)
AzureIoTClient 20:594780be216d 1591 {
AzureIoTClient 20:594780be216d 1592 // We are connected and not being closed, so does SAS need to reconnect?
Azure.IoT.Build 13:606465879c57 1593 tickcounter_ms_t current_time;
Azure.IoT.Build 13:606465879c57 1594 if (tickcounter_get_current_ms(transport_data->msgTickCounter, &current_time) != 0)
AzureIoTClient 11:31ebaeb51e1e 1595 {
AzureIoTClient 11:31ebaeb51e1e 1596 transport_data->connectFailCount++;
AzureIoTClient 18:ec8e5e97c6a4 1597 result = __FAILURE__;
AzureIoTClient 11:31ebaeb51e1e 1598 }
AzureIoTClient 11:31ebaeb51e1e 1599 else
AzureIoTClient 11:31ebaeb51e1e 1600 {
AzureIoTClient 11:31ebaeb51e1e 1601 if ((current_time - transport_data->mqtt_connect_time) / 1000 > (SAS_TOKEN_DEFAULT_LIFETIME*SAS_REFRESH_MULTIPLIER))
AzureIoTClient 11:31ebaeb51e1e 1602 {
AzureIoTClient 11:31ebaeb51e1e 1603 (void)mqtt_client_disconnect(transport_data->mqttClient);
AzureIoTClient 19:f87dfe76bc70 1604 IoTHubClient_LL_ConnectionStatusCallBack(transport_data->llClientHandle, IOTHUB_CLIENT_CONNECTION_UNAUTHENTICATED, IOTHUB_CLIENT_CONNECTION_EXPIRED_SAS_TOKEN);
AzureIoTClient 20:594780be216d 1605 transport_data->mqttClientStatus = MQTT_CLIENT_STATUS_NOT_CONNECTED;
AzureIoTClient 11:31ebaeb51e1e 1606 transport_data->currPacketState = UNKNOWN_TYPE;
AzureIoTClient 12:658ca6865de2 1607 transport_data->device_twin_get_sent = false;
AzureIoTClient 11:31ebaeb51e1e 1608 if (transport_data->topic_MqttMessage != NULL)
AzureIoTClient 11:31ebaeb51e1e 1609 {
AzureIoTClient 11:31ebaeb51e1e 1610 transport_data->topics_ToSubscribe |= SUBSCRIBE_TELEMETRY_TOPIC;
AzureIoTClient 11:31ebaeb51e1e 1611 }
AzureIoTClient 12:658ca6865de2 1612 if (transport_data->topic_GetState != NULL)
AzureIoTClient 12:658ca6865de2 1613 {
AzureIoTClient 12:658ca6865de2 1614 transport_data->topics_ToSubscribe |= SUBSCRIBE_GET_REPORTED_STATE_TOPIC;
AzureIoTClient 12:658ca6865de2 1615 }
AzureIoTClient 12:658ca6865de2 1616 if (transport_data->topic_NotifyState != NULL)
AzureIoTClient 12:658ca6865de2 1617 {
AzureIoTClient 12:658ca6865de2 1618 transport_data->topics_ToSubscribe |= SUBSCRIBE_NOTIFICATION_STATE_TOPIC;
AzureIoTClient 12:658ca6865de2 1619 }
AzureIoTClient 12:658ca6865de2 1620 if (transport_data->topic_DeviceMethods != NULL)
AzureIoTClient 12:658ca6865de2 1621 {
AzureIoTClient 12:658ca6865de2 1622 transport_data->topics_ToSubscribe |= SUBSCRIBE_DEVICE_METHOD_TOPIC;
AzureIoTClient 12:658ca6865de2 1623 }
AzureIoTClient 11:31ebaeb51e1e 1624 }
AzureIoTClient 11:31ebaeb51e1e 1625 }
AzureIoTClient 11:31ebaeb51e1e 1626 }
AzureIoTClient 11:31ebaeb51e1e 1627 }
AzureIoTClient 11:31ebaeb51e1e 1628 return result;
AzureIoTClient 11:31ebaeb51e1e 1629 }
AzureIoTClient 11:31ebaeb51e1e 1630
AzureIoTClient 11:31ebaeb51e1e 1631 static STRING_HANDLE buildConfigForUsername(const IOTHUB_CLIENT_CONFIG* upperConfig)
AzureIoTClient 11:31ebaeb51e1e 1632 {
AzureIoTClient 22:07fec4d325b6 1633 return STRING_construct_sprintf("%s.%s/%s/api-version=%s&DeviceClientType=", upperConfig->iotHubName, upperConfig->iotHubSuffix, upperConfig->deviceId, IOTHUB_API_VERSION);
AzureIoTClient 11:31ebaeb51e1e 1634 }
AzureIoTClient 11:31ebaeb51e1e 1635
AzureIoTClient 21:167f8fcf6fb1 1636 static PMQTTTRANSPORT_HANDLE_DATA InitializeTransportHandleData(const IOTHUB_CLIENT_CONFIG* upperConfig, PDLIST_ENTRY waitingToSend, IOTHUB_AUTHORIZATION_HANDLE auth_module)
AzureIoTClient 11:31ebaeb51e1e 1637 {
AzureIoTClient 11:31ebaeb51e1e 1638 PMQTTTRANSPORT_HANDLE_DATA state = (PMQTTTRANSPORT_HANDLE_DATA)malloc(sizeof(MQTTTRANSPORT_HANDLE_DATA));
AzureIoTClient 11:31ebaeb51e1e 1639 if (state == NULL)
AzureIoTClient 11:31ebaeb51e1e 1640 {
AzureIoTClient 11:31ebaeb51e1e 1641 LogError("Could not create MQTT transport state. Memory allocation failed.");
AzureIoTClient 11:31ebaeb51e1e 1642 }
AzureIoTClient 11:31ebaeb51e1e 1643 else
AzureIoTClient 11:31ebaeb51e1e 1644 {
AzureIoTClient 21:167f8fcf6fb1 1645 memset(state, 0, sizeof(MQTTTRANSPORT_HANDLE_DATA) );
AzureIoTClient 21:167f8fcf6fb1 1646 if ((state->msgTickCounter = tickcounter_create()) == NULL)
AzureIoTClient 11:31ebaeb51e1e 1647 {
AzureIoTClient 21:167f8fcf6fb1 1648 LogError("Invalid Argument: iotHubName is empty");
AzureIoTClient 21:167f8fcf6fb1 1649 free(state);
AzureIoTClient 21:167f8fcf6fb1 1650 state = NULL;
AzureIoTClient 21:167f8fcf6fb1 1651 }
AzureIoTClient 25:a35763780a87 1652 // Codes_SRS_IOTHUB_TRANSPORT_MQTT_COMMON_09_005: [ MQTT transport shall use EXPONENTIAL_WITH_BACK_OFF as default retry policy ]
AzureIoTClient 25:a35763780a87 1653 else if ((state->retry_control_handle = retry_control_create(DEFAULT_RETRY_POLICY, DEFAULT_RETRY_TIMEOUT_IN_SECONDS)) == NULL)
AzureIoTClient 25:a35763780a87 1654 {
AzureIoTClient 25:a35763780a87 1655 LogError("Failed creating default retry control");
AzureIoTClient 25:a35763780a87 1656 tickcounter_destroy(state->msgTickCounter);
AzureIoTClient 25:a35763780a87 1657 free(state);
AzureIoTClient 25:a35763780a87 1658 state = NULL;
AzureIoTClient 25:a35763780a87 1659 }
AzureIoTClient 21:167f8fcf6fb1 1660 else if ((state->device_id = STRING_construct(upperConfig->deviceId)) == NULL)
AzureIoTClient 21:167f8fcf6fb1 1661 {
AzureIoTClient 21:167f8fcf6fb1 1662 LogError("failure constructing device_id.");
AzureIoTClient 25:a35763780a87 1663 retry_control_destroy(state->retry_control_handle);
Azure.IoT.Build 13:606465879c57 1664 tickcounter_destroy(state->msgTickCounter);
AzureIoTClient 11:31ebaeb51e1e 1665 free(state);
AzureIoTClient 11:31ebaeb51e1e 1666 state = NULL;
AzureIoTClient 11:31ebaeb51e1e 1667 }
AzureIoTClient 21:167f8fcf6fb1 1668 else if ((state->devicesPath = STRING_construct_sprintf("%s.%s/devices/%s", upperConfig->iotHubName, upperConfig->iotHubSuffix, upperConfig->deviceId)) == NULL)
AzureIoTClient 11:31ebaeb51e1e 1669 {
AzureIoTClient 25:a35763780a87 1670 STRING_delete(state->device_id);
AzureIoTClient 25:a35763780a87 1671 retry_control_destroy(state->retry_control_handle);
Azure.IoT.Build 13:606465879c57 1672 tickcounter_destroy(state->msgTickCounter);
AzureIoTClient 11:31ebaeb51e1e 1673 free(state);
AzureIoTClient 11:31ebaeb51e1e 1674 state = NULL;
AzureIoTClient 11:31ebaeb51e1e 1675 }
AzureIoTClient 11:31ebaeb51e1e 1676 else
AzureIoTClient 11:31ebaeb51e1e 1677 {
AzureIoTClient 21:167f8fcf6fb1 1678 if ( (state->topic_MqttEvent = STRING_construct_sprintf(TOPIC_DEVICE_DEVICE, upperConfig->deviceId) ) == NULL)
AzureIoTClient 11:31ebaeb51e1e 1679 {
AzureIoTClient 21:167f8fcf6fb1 1680 LogError("Could not create topic_MqttEvent for MQTT");
AzureIoTClient 11:31ebaeb51e1e 1681 STRING_delete(state->devicesPath);
AzureIoTClient 11:31ebaeb51e1e 1682 STRING_delete(state->device_id);
AzureIoTClient 25:a35763780a87 1683 retry_control_destroy(state->retry_control_handle);
Azure.IoT.Build 13:606465879c57 1684 tickcounter_destroy(state->msgTickCounter);
AzureIoTClient 11:31ebaeb51e1e 1685 free(state);
AzureIoTClient 11:31ebaeb51e1e 1686 state = NULL;
AzureIoTClient 11:31ebaeb51e1e 1687 }
AzureIoTClient 11:31ebaeb51e1e 1688 else
AzureIoTClient 11:31ebaeb51e1e 1689 {
AzureIoTClient 21:167f8fcf6fb1 1690 state->mqttClient = mqtt_client_init(mqtt_notification_callback, mqtt_operation_complete_callback, state, mqtt_error_callback, state);
AzureIoTClient 21:167f8fcf6fb1 1691 if (state->mqttClient == NULL)
AzureIoTClient 11:31ebaeb51e1e 1692 {
AzureIoTClient 21:167f8fcf6fb1 1693 LogError("failure initializing mqtt client.");
AzureIoTClient 11:31ebaeb51e1e 1694 STRING_delete(state->devicesPath);
AzureIoTClient 11:31ebaeb51e1e 1695 STRING_delete(state->topic_MqttEvent);
AzureIoTClient 11:31ebaeb51e1e 1696 STRING_delete(state->device_id);
AzureIoTClient 25:a35763780a87 1697 retry_control_destroy(state->retry_control_handle);
Azure.IoT.Build 13:606465879c57 1698 tickcounter_destroy(state->msgTickCounter);
AzureIoTClient 11:31ebaeb51e1e 1699 free(state);
AzureIoTClient 11:31ebaeb51e1e 1700 state = NULL;
AzureIoTClient 11:31ebaeb51e1e 1701 }
AzureIoTClient 11:31ebaeb51e1e 1702 else
AzureIoTClient 11:31ebaeb51e1e 1703 {
AzureIoTClient 21:167f8fcf6fb1 1704 /* 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 1705 if (upperConfig->protocolGatewayHostName == NULL)
AzureIoTClient 21:167f8fcf6fb1 1706 {
AzureIoTClient 21:167f8fcf6fb1 1707 state->hostAddress = STRING_construct_sprintf("%s.%s", upperConfig->iotHubName, upperConfig->iotHubSuffix);
AzureIoTClient 21:167f8fcf6fb1 1708 }
AzureIoTClient 21:167f8fcf6fb1 1709 else
AzureIoTClient 21:167f8fcf6fb1 1710 {
AzureIoTClient 21:167f8fcf6fb1 1711 state->hostAddress = STRING_construct(upperConfig->protocolGatewayHostName);
AzureIoTClient 21:167f8fcf6fb1 1712 }
AzureIoTClient 21:167f8fcf6fb1 1713
AzureIoTClient 21:167f8fcf6fb1 1714 if (state->hostAddress == NULL)
AzureIoTClient 21:167f8fcf6fb1 1715 {
AzureIoTClient 21:167f8fcf6fb1 1716 LogError("failure constructing host address.");
AzureIoTClient 25:a35763780a87 1717 mqtt_client_deinit(state->mqttClient);
AzureIoTClient 21:167f8fcf6fb1 1718 STRING_delete(state->devicesPath);
AzureIoTClient 21:167f8fcf6fb1 1719 STRING_delete(state->topic_MqttEvent);
AzureIoTClient 21:167f8fcf6fb1 1720 STRING_delete(state->device_id);
AzureIoTClient 25:a35763780a87 1721 retry_control_destroy(state->retry_control_handle);
AzureIoTClient 21:167f8fcf6fb1 1722 tickcounter_destroy(state->msgTickCounter);
AzureIoTClient 21:167f8fcf6fb1 1723 free(state);
AzureIoTClient 21:167f8fcf6fb1 1724 state = NULL;
AzureIoTClient 21:167f8fcf6fb1 1725 }
AzureIoTClient 21:167f8fcf6fb1 1726 else if ((state->configPassedThroughUsername = buildConfigForUsername(upperConfig)) == NULL)
AzureIoTClient 21:167f8fcf6fb1 1727 {
AzureIoTClient 25:a35763780a87 1728 STRING_delete(state->hostAddress);
AzureIoTClient 21:167f8fcf6fb1 1729 mqtt_client_deinit(state->mqttClient);
AzureIoTClient 25:a35763780a87 1730 STRING_delete(state->devicesPath);
AzureIoTClient 21:167f8fcf6fb1 1731 STRING_delete(state->topic_MqttEvent);
AzureIoTClient 21:167f8fcf6fb1 1732 STRING_delete(state->device_id);
AzureIoTClient 25:a35763780a87 1733 retry_control_destroy(state->retry_control_handle);
AzureIoTClient 21:167f8fcf6fb1 1734 tickcounter_destroy(state->msgTickCounter);
AzureIoTClient 21:167f8fcf6fb1 1735 free(state);
AzureIoTClient 21:167f8fcf6fb1 1736 state = NULL;
AzureIoTClient 21:167f8fcf6fb1 1737 }
AzureIoTClient 21:167f8fcf6fb1 1738 else
AzureIoTClient 21:167f8fcf6fb1 1739 {
AzureIoTClient 21:167f8fcf6fb1 1740 /* 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 1741 DList_InitializeListHead(&(state->telemetry_waitingForAck));
AzureIoTClient 21:167f8fcf6fb1 1742 DList_InitializeListHead(&(state->ack_waiting_queue));
AzureIoTClient 21:167f8fcf6fb1 1743 state->isDestroyCalled = false;
AzureIoTClient 21:167f8fcf6fb1 1744 state->isRegistered = false;
AzureIoTClient 21:167f8fcf6fb1 1745 state->mqttClientStatus = MQTT_CLIENT_STATUS_NOT_CONNECTED;
AzureIoTClient 21:167f8fcf6fb1 1746 state->device_twin_get_sent = false;
AzureIoTClient 21:167f8fcf6fb1 1747 state->isRecoverableError = true;
AzureIoTClient 21:167f8fcf6fb1 1748 state->packetId = 1;
AzureIoTClient 21:167f8fcf6fb1 1749 state->llClientHandle = NULL;
AzureIoTClient 21:167f8fcf6fb1 1750 state->xioTransport = NULL;
AzureIoTClient 21:167f8fcf6fb1 1751 state->portNum = 0;
AzureIoTClient 21:167f8fcf6fb1 1752 state->waitingToSend = waitingToSend;
AzureIoTClient 21:167f8fcf6fb1 1753 state->currPacketState = CONNECT_TYPE;
AzureIoTClient 21:167f8fcf6fb1 1754 state->keepAliveValue = DEFAULT_MQTT_KEEPALIVE;
AzureIoTClient 21:167f8fcf6fb1 1755 state->connectFailCount = 0;
AzureIoTClient 21:167f8fcf6fb1 1756 state->connectTick = 0;
AzureIoTClient 21:167f8fcf6fb1 1757 state->topic_MqttMessage = NULL;
AzureIoTClient 21:167f8fcf6fb1 1758 state->topic_GetState = NULL;
AzureIoTClient 21:167f8fcf6fb1 1759 state->topic_NotifyState = NULL;
AzureIoTClient 21:167f8fcf6fb1 1760 state->topics_ToSubscribe = UNSUBSCRIBE_FROM_TOPIC;
AzureIoTClient 21:167f8fcf6fb1 1761 state->topic_DeviceMethods = NULL;
AzureIoTClient 21:167f8fcf6fb1 1762 state->log_trace = state->raw_trace = false;
AzureIoTClient 21:167f8fcf6fb1 1763 srand((unsigned int)get_time(NULL));
AzureIoTClient 21:167f8fcf6fb1 1764 state->authorization_module = auth_module;
AzureIoTClient 24:4096249decf1 1765 state->isProductInfoSet = false;
AzureIoTClient 21:167f8fcf6fb1 1766 }
AzureIoTClient 11:31ebaeb51e1e 1767 }
AzureIoTClient 11:31ebaeb51e1e 1768 }
AzureIoTClient 11:31ebaeb51e1e 1769 }
AzureIoTClient 11:31ebaeb51e1e 1770 }
AzureIoTClient 11:31ebaeb51e1e 1771 return state;
AzureIoTClient 11:31ebaeb51e1e 1772 }
AzureIoTClient 11:31ebaeb51e1e 1773
AzureIoTClient 11:31ebaeb51e1e 1774 TRANSPORT_LL_HANDLE IoTHubTransport_MQTT_Common_Create(const IOTHUBTRANSPORT_CONFIG* config, MQTT_GET_IO_TRANSPORT get_io_transport)
AzureIoTClient 11:31ebaeb51e1e 1775 {
AzureIoTClient 11:31ebaeb51e1e 1776 PMQTTTRANSPORT_HANDLE_DATA result;
AzureIoTClient 11:31ebaeb51e1e 1777 size_t deviceIdSize;
AzureIoTClient 11:31ebaeb51e1e 1778
AzureIoTClient 12:658ca6865de2 1779 /* Codes_SRS_IOTHUB_MQTT_TRANSPORT_07_001: [If parameter config is NULL then IoTHubTransport_MQTT_Common_Create shall return NULL.] */
AzureIoTClient 11:31ebaeb51e1e 1780 /* 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 1781 if (config == NULL || get_io_transport == NULL)
AzureIoTClient 11:31ebaeb51e1e 1782 {
AzureIoTClient 11:31ebaeb51e1e 1783 LogError("Invalid Argument: Config Parameter is NULL.");
AzureIoTClient 11:31ebaeb51e1e 1784 result = NULL;
AzureIoTClient 11:31ebaeb51e1e 1785 }
AzureIoTClient 12:658ca6865de2 1786 /* 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 1787 /* 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 1788 /* 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 1789 else if (config->upperConfig == NULL ||
AzureIoTClient 11:31ebaeb51e1e 1790 config->upperConfig->protocol == NULL ||
AzureIoTClient 11:31ebaeb51e1e 1791 config->upperConfig->deviceId == NULL ||
AzureIoTClient 11:31ebaeb51e1e 1792 ((config->upperConfig->deviceKey != NULL) && (config->upperConfig->deviceSasToken != NULL)) ||
AzureIoTClient 11:31ebaeb51e1e 1793 config->upperConfig->iotHubName == NULL ||
AzureIoTClient 11:31ebaeb51e1e 1794 config->upperConfig->iotHubSuffix == NULL)
AzureIoTClient 11:31ebaeb51e1e 1795 {
AzureIoTClient 11:31ebaeb51e1e 1796 LogError("Invalid Argument: upperConfig structure contains an invalid parameter");
AzureIoTClient 11:31ebaeb51e1e 1797 result = NULL;
AzureIoTClient 11:31ebaeb51e1e 1798 }
AzureIoTClient 21:167f8fcf6fb1 1799 /* 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 1800 else if (config->waitingToSend == NULL)
AzureIoTClient 11:31ebaeb51e1e 1801 {
AzureIoTClient 11:31ebaeb51e1e 1802 LogError("Invalid Argument: waitingToSend is NULL)");
AzureIoTClient 11:31ebaeb51e1e 1803 result = NULL;
AzureIoTClient 11:31ebaeb51e1e 1804 }
AzureIoTClient 21:167f8fcf6fb1 1805 /* 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 1806 else if (config->auth_module_handle == NULL)
AzureIoTClient 21:167f8fcf6fb1 1807 {
AzureIoTClient 21:167f8fcf6fb1 1808 LogError("Invalid Argument: auth_module_handle is NULL)");
AzureIoTClient 21:167f8fcf6fb1 1809 result = NULL;
AzureIoTClient 21:167f8fcf6fb1 1810 }
AzureIoTClient 12:658ca6865de2 1811 /* 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 1812 else if ( ( (deviceIdSize = strlen(config->upperConfig->deviceId)) > 128U) || (deviceIdSize == 0) )
AzureIoTClient 11:31ebaeb51e1e 1813 {
AzureIoTClient 11:31ebaeb51e1e 1814 LogError("Invalid Argument: DeviceId is of an invalid size");
AzureIoTClient 11:31ebaeb51e1e 1815 result = NULL;
AzureIoTClient 11:31ebaeb51e1e 1816 }
AzureIoTClient 12:658ca6865de2 1817 /* 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 1818 else if ((config->upperConfig->deviceKey != NULL) && (strlen(config->upperConfig->deviceKey) == 0))
AzureIoTClient 11:31ebaeb51e1e 1819 {
AzureIoTClient 11:31ebaeb51e1e 1820 LogError("Invalid Argument: deviceKey is empty");
AzureIoTClient 11:31ebaeb51e1e 1821 result = NULL;
AzureIoTClient 11:31ebaeb51e1e 1822 }
AzureIoTClient 12:658ca6865de2 1823 /* 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 1824 else if ((config->upperConfig->deviceSasToken != NULL) && (strlen(config->upperConfig->deviceSasToken) == 0))
AzureIoTClient 11:31ebaeb51e1e 1825 {
AzureIoTClient 11:31ebaeb51e1e 1826 LogError("Invalid Argument: deviceSasToken is empty");
AzureIoTClient 11:31ebaeb51e1e 1827 result = NULL;
AzureIoTClient 11:31ebaeb51e1e 1828 }
AzureIoTClient 12:658ca6865de2 1829 /* 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 1830 else if (strlen(config->upperConfig->iotHubName) == 0)
AzureIoTClient 11:31ebaeb51e1e 1831 {
AzureIoTClient 11:31ebaeb51e1e 1832 LogError("Invalid Argument: iotHubName is empty");
AzureIoTClient 11:31ebaeb51e1e 1833 result = NULL;
AzureIoTClient 11:31ebaeb51e1e 1834 }
AzureIoTClient 11:31ebaeb51e1e 1835 else
AzureIoTClient 11:31ebaeb51e1e 1836 {
AzureIoTClient 21:167f8fcf6fb1 1837 result = InitializeTransportHandleData(config->upperConfig, config->waitingToSend, config->auth_module_handle);
Azure.IoT.Build 13:606465879c57 1838 if (result != NULL)
AzureIoTClient 11:31ebaeb51e1e 1839 {
AzureIoTClient 11:31ebaeb51e1e 1840 result->get_io_transport = get_io_transport;
AzureIoTClient 20:594780be216d 1841 result->http_proxy_hostname = NULL;
AzureIoTClient 20:594780be216d 1842 result->http_proxy_username = NULL;
AzureIoTClient 20:594780be216d 1843 result->http_proxy_password = NULL;
AzureIoTClient 11:31ebaeb51e1e 1844 }
AzureIoTClient 11:31ebaeb51e1e 1845 }
AzureIoTClient 12:658ca6865de2 1846 /* Codes_SRS_IOTHUB_MQTT_TRANSPORT_07_009: [If any error is encountered then IoTHubTransport_MQTT_Common_Create shall return NULL.] */
AzureIoTClient 12:658ca6865de2 1847 /* Codes_SRS_IOTHUB_MQTT_TRANSPORT_07_011: [On Success IoTHubTransport_MQTT_Common_Create shall return a non-NULL value.] */
AzureIoTClient 11:31ebaeb51e1e 1848 return result;
AzureIoTClient 11:31ebaeb51e1e 1849 }
AzureIoTClient 11:31ebaeb51e1e 1850
AzureIoTClient 11:31ebaeb51e1e 1851 void IoTHubTransport_MQTT_Common_Destroy(TRANSPORT_LL_HANDLE handle)
AzureIoTClient 11:31ebaeb51e1e 1852 {
AzureIoTClient 12:658ca6865de2 1853 /* Codes_SRS_IOTHUB_MQTT_TRANSPORT_07_012: [IoTHubTransport_MQTT_Common_Destroy shall do nothing if parameter handle is NULL.] */
AzureIoTClient 11:31ebaeb51e1e 1854 PMQTTTRANSPORT_HANDLE_DATA transport_data = (PMQTTTRANSPORT_HANDLE_DATA)handle;
AzureIoTClient 11:31ebaeb51e1e 1855 if (transport_data != NULL)
AzureIoTClient 11:31ebaeb51e1e 1856 {
AzureIoTClient 11:31ebaeb51e1e 1857 transport_data->isDestroyCalled = true;
AzureIoTClient 11:31ebaeb51e1e 1858
AzureIoTClient 11:31ebaeb51e1e 1859 DisconnectFromClient(transport_data);
AzureIoTClient 11:31ebaeb51e1e 1860
AzureIoTClient 11:31ebaeb51e1e 1861 //Empty the Waiting for Ack Messages.
AzureIoTClient 11:31ebaeb51e1e 1862 while (!DList_IsListEmpty(&transport_data->telemetry_waitingForAck))
AzureIoTClient 11:31ebaeb51e1e 1863 {
AzureIoTClient 11:31ebaeb51e1e 1864 PDLIST_ENTRY currentEntry = DList_RemoveHeadList(&transport_data->telemetry_waitingForAck);
AzureIoTClient 11:31ebaeb51e1e 1865 MQTT_MESSAGE_DETAILS_LIST* mqttMsgEntry = containingRecord(currentEntry, MQTT_MESSAGE_DETAILS_LIST, entry);
AzureIoTClient 11:31ebaeb51e1e 1866 sendMsgComplete(mqttMsgEntry->iotHubMessageEntry, transport_data, IOTHUB_CLIENT_CONFIRMATION_BECAUSE_DESTROY);
AzureIoTClient 11:31ebaeb51e1e 1867 free(mqttMsgEntry);
AzureIoTClient 11:31ebaeb51e1e 1868 }
AzureIoTClient 12:658ca6865de2 1869 while (!DList_IsListEmpty(&transport_data->ack_waiting_queue))
AzureIoTClient 12:658ca6865de2 1870 {
AzureIoTClient 12:658ca6865de2 1871 PDLIST_ENTRY currentEntry = DList_RemoveHeadList(&transport_data->ack_waiting_queue);
AzureIoTClient 12:658ca6865de2 1872 MQTT_DEVICE_TWIN_ITEM* mqtt_device_twin = containingRecord(currentEntry, MQTT_DEVICE_TWIN_ITEM, entry);
AzureIoTClient 12:658ca6865de2 1873 IoTHubClient_LL_ReportedStateComplete(transport_data->llClientHandle, mqtt_device_twin->iothub_msg_id, STATUS_CODE_TIMEOUT_VALUE);
AzureIoTClient 12:658ca6865de2 1874 free(mqtt_device_twin);
AzureIoTClient 12:658ca6865de2 1875 }
AzureIoTClient 11:31ebaeb51e1e 1876
AzureIoTClient 21:167f8fcf6fb1 1877 STRING_delete(transport_data->devicesPath);
AzureIoTClient 11:31ebaeb51e1e 1878
AzureIoTClient 12:658ca6865de2 1879 /* Codes_SRS_IOTHUB_MQTT_TRANSPORT_07_014: [IoTHubTransport_MQTT_Common_Destroy shall free all the resources currently in use.] */
AzureIoTClient 11:31ebaeb51e1e 1880 mqtt_client_deinit(transport_data->mqttClient);
AzureIoTClient 25:a35763780a87 1881 retry_control_destroy(transport_data->retry_control_handle);
AzureIoTClient 11:31ebaeb51e1e 1882 STRING_delete(transport_data->topic_MqttEvent);
AzureIoTClient 11:31ebaeb51e1e 1883 STRING_delete(transport_data->topic_MqttMessage);
AzureIoTClient 11:31ebaeb51e1e 1884 STRING_delete(transport_data->device_id);
AzureIoTClient 11:31ebaeb51e1e 1885 STRING_delete(transport_data->hostAddress);
AzureIoTClient 11:31ebaeb51e1e 1886 STRING_delete(transport_data->configPassedThroughUsername);
AzureIoTClient 12:658ca6865de2 1887 STRING_delete(transport_data->topic_GetState);
AzureIoTClient 12:658ca6865de2 1888 STRING_delete(transport_data->topic_NotifyState);
AzureIoTClient 12:658ca6865de2 1889 STRING_delete(transport_data->topic_DeviceMethods);
AzureIoTClient 12:658ca6865de2 1890
Azure.IoT.Build 13:606465879c57 1891 tickcounter_destroy(transport_data->msgTickCounter);
AzureIoTClient 20:594780be216d 1892 /* Codes_SRS_IOTHUB_TRANSPORT_MQTT_COMMON_01_012: [ `IoTHubTransport_MQTT_Common_Destroy` shall free the stored proxy options. ]*/
AzureIoTClient 20:594780be216d 1893 free_proxy_data(transport_data);
AzureIoTClient 11:31ebaeb51e1e 1894 free(transport_data);
AzureIoTClient 11:31ebaeb51e1e 1895 }
AzureIoTClient 11:31ebaeb51e1e 1896 }
AzureIoTClient 11:31ebaeb51e1e 1897
AzureIoTClient 12:658ca6865de2 1898 int IoTHubTransport_MQTT_Common_Subscribe_DeviceTwin(IOTHUB_DEVICE_HANDLE handle)
AzureIoTClient 12:658ca6865de2 1899 {
AzureIoTClient 12:658ca6865de2 1900 int result;
AzureIoTClient 12:658ca6865de2 1901 PMQTTTRANSPORT_HANDLE_DATA transport_data = (PMQTTTRANSPORT_HANDLE_DATA)handle;
AzureIoTClient 12:658ca6865de2 1902 if (transport_data == NULL)
AzureIoTClient 12:658ca6865de2 1903 {
AzureIoTClient 12:658ca6865de2 1904 /* 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 1905 LogError("Invalid handle parameter. NULL.");
AzureIoTClient 18:ec8e5e97c6a4 1906 result = __FAILURE__;
AzureIoTClient 12:658ca6865de2 1907 }
AzureIoTClient 12:658ca6865de2 1908 else
AzureIoTClient 12:658ca6865de2 1909 {
AzureIoTClient 12:658ca6865de2 1910 if (transport_data->topic_GetState == NULL)
AzureIoTClient 12:658ca6865de2 1911 {
AzureIoTClient 12:658ca6865de2 1912 /* 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 1913 transport_data->topic_GetState = STRING_construct(TOPIC_GET_DESIRED_STATE);
AzureIoTClient 12:658ca6865de2 1914 if (transport_data->topic_GetState == NULL)
AzureIoTClient 12:658ca6865de2 1915 {
AzureIoTClient 12:658ca6865de2 1916 /* Codes_SRS_IOTHUB_MQTT_TRANSPORT_07_046: [Upon failure IoTHubTransport_MQTT_Common_Subscribe_DeviceTwin shall return a non-zero value.] */
AzureIoTClient 12:658ca6865de2 1917 LogError("Failure: unable constructing reported state topic");
AzureIoTClient 18:ec8e5e97c6a4 1918 result = __FAILURE__;
AzureIoTClient 12:658ca6865de2 1919 }
AzureIoTClient 12:658ca6865de2 1920 else
AzureIoTClient 12:658ca6865de2 1921 {
AzureIoTClient 12:658ca6865de2 1922 /* Codes_SRS_IOTHUB_MQTT_TRANSPORT_07_047: [On success IoTHubTransport_MQTT_Common_Subscribe_DeviceTwin shall return 0.] */
AzureIoTClient 12:658ca6865de2 1923 transport_data->topics_ToSubscribe |= SUBSCRIBE_GET_REPORTED_STATE_TOPIC;
AzureIoTClient 12:658ca6865de2 1924 result = 0;
AzureIoTClient 12:658ca6865de2 1925 }
AzureIoTClient 12:658ca6865de2 1926 }
AzureIoTClient 12:658ca6865de2 1927 else
AzureIoTClient 12:658ca6865de2 1928 {
AzureIoTClient 12:658ca6865de2 1929 result = 0;
AzureIoTClient 12:658ca6865de2 1930 }
AzureIoTClient 12:658ca6865de2 1931 if (result == 0 && transport_data->topic_NotifyState == NULL)
AzureIoTClient 12:658ca6865de2 1932 {
AzureIoTClient 12:658ca6865de2 1933 /* 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 1934 transport_data->topic_NotifyState = STRING_construct(TOPIC_NOTIFICATION_STATE);
AzureIoTClient 12:658ca6865de2 1935 if (transport_data->topic_NotifyState == NULL)
AzureIoTClient 12:658ca6865de2 1936 {
AzureIoTClient 12:658ca6865de2 1937 LogError("Failure: unable constructing notify state topic");
AzureIoTClient 18:ec8e5e97c6a4 1938 result = __FAILURE__;
AzureIoTClient 12:658ca6865de2 1939 }
AzureIoTClient 12:658ca6865de2 1940 else
AzureIoTClient 12:658ca6865de2 1941 {
AzureIoTClient 12:658ca6865de2 1942 /* Codes_SRS_IOTHUB_MQTT_TRANSPORT_07_047: [On success IoTHubTransport_MQTT_Common_Subscribe_DeviceTwin shall return 0.] */
AzureIoTClient 12:658ca6865de2 1943 transport_data->topics_ToSubscribe |= SUBSCRIBE_NOTIFICATION_STATE_TOPIC;
AzureIoTClient 12:658ca6865de2 1944 result = 0;
AzureIoTClient 12:658ca6865de2 1945 }
AzureIoTClient 12:658ca6865de2 1946 }
AzureIoTClient 12:658ca6865de2 1947 if (result == 0)
AzureIoTClient 12:658ca6865de2 1948 {
AzureIoTClient 12:658ca6865de2 1949 if (transport_data->currPacketState != CONNACK_TYPE &&
AzureIoTClient 12:658ca6865de2 1950 transport_data->currPacketState != CONNECT_TYPE &&
AzureIoTClient 12:658ca6865de2 1951 transport_data->currPacketState != DISCONNECT_TYPE &&
AzureIoTClient 12:658ca6865de2 1952 transport_data->currPacketState != PACKET_TYPE_ERROR)
AzureIoTClient 12:658ca6865de2 1953 {
AzureIoTClient 12:658ca6865de2 1954 transport_data->currPacketState = SUBSCRIBE_TYPE;
AzureIoTClient 12:658ca6865de2 1955 }
AzureIoTClient 12:658ca6865de2 1956 }
AzureIoTClient 12:658ca6865de2 1957 }
AzureIoTClient 12:658ca6865de2 1958 return result;
AzureIoTClient 12:658ca6865de2 1959 }
AzureIoTClient 12:658ca6865de2 1960
AzureIoTClient 12:658ca6865de2 1961 void IoTHubTransport_MQTT_Common_Unsubscribe_DeviceTwin(IOTHUB_DEVICE_HANDLE handle)
AzureIoTClient 12:658ca6865de2 1962 {
AzureIoTClient 12:658ca6865de2 1963 PMQTTTRANSPORT_HANDLE_DATA transport_data = (PMQTTTRANSPORT_HANDLE_DATA)handle;
AzureIoTClient 12:658ca6865de2 1964 /* 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 1965 if (transport_data != NULL)
AzureIoTClient 12:658ca6865de2 1966 {
AzureIoTClient 12:658ca6865de2 1967 if (transport_data->topic_GetState != NULL)
AzureIoTClient 12:658ca6865de2 1968 {
AzureIoTClient 12:658ca6865de2 1969 /* 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 1970 transport_data->topics_ToSubscribe &= ~SUBSCRIBE_GET_REPORTED_STATE_TOPIC;
AzureIoTClient 12:658ca6865de2 1971 STRING_delete(transport_data->topic_GetState);
AzureIoTClient 12:658ca6865de2 1972 transport_data->topic_GetState = NULL;
AzureIoTClient 12:658ca6865de2 1973 }
AzureIoTClient 12:658ca6865de2 1974 if (transport_data->topic_NotifyState != NULL)
AzureIoTClient 12:658ca6865de2 1975 {
AzureIoTClient 12:658ca6865de2 1976 /* 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 1977 transport_data->topics_ToSubscribe &= ~SUBSCRIBE_NOTIFICATION_STATE_TOPIC;
AzureIoTClient 12:658ca6865de2 1978 STRING_delete(transport_data->topic_NotifyState);
AzureIoTClient 12:658ca6865de2 1979 transport_data->topic_NotifyState = NULL;
AzureIoTClient 12:658ca6865de2 1980 }
AzureIoTClient 12:658ca6865de2 1981 }
AzureIoTClient 12:658ca6865de2 1982 else
AzureIoTClient 12:658ca6865de2 1983 {
AzureIoTClient 12:658ca6865de2 1984 LogError("Invalid argument to unsubscribe (handle is NULL).");
AzureIoTClient 12:658ca6865de2 1985 }
AzureIoTClient 12:658ca6865de2 1986 }
AzureIoTClient 12:658ca6865de2 1987
AzureIoTClient 12:658ca6865de2 1988 int IoTHubTransport_MQTT_Common_Subscribe_DeviceMethod(IOTHUB_DEVICE_HANDLE handle)
AzureIoTClient 12:658ca6865de2 1989 {
AzureIoTClient 12:658ca6865de2 1990 int result;
AzureIoTClient 12:658ca6865de2 1991 PMQTTTRANSPORT_HANDLE_DATA transport_data = (PMQTTTRANSPORT_HANDLE_DATA)handle;
AzureIoTClient 12:658ca6865de2 1992
AzureIoTClient 12:658ca6865de2 1993 if (transport_data == NULL)
AzureIoTClient 12:658ca6865de2 1994 {
AzureIoTClient 12:658ca6865de2 1995 /*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 1996 LogError("Invalid handle parameter. NULL.");
AzureIoTClient 18:ec8e5e97c6a4 1997 result = __FAILURE__;
AzureIoTClient 12:658ca6865de2 1998 }
AzureIoTClient 12:658ca6865de2 1999 else
AzureIoTClient 12:658ca6865de2 2000 {
AzureIoTClient 12:658ca6865de2 2001 if (transport_data->topic_DeviceMethods == NULL)
AzureIoTClient 12:658ca6865de2 2002 {
AzureIoTClient 12:658ca6865de2 2003 /*Codes_SRS_IOTHUB_MQTT_TRANSPORT_12_004 : [IoTHubTransport_MQTT_Common_Subscribe_DeviceMethod shall construct the DEVICE_METHOD topic string for subscribe.]*/
AzureIoTClient 12:658ca6865de2 2004 transport_data->topic_DeviceMethods = STRING_construct(TOPIC_DEVICE_METHOD_SUBSCRIBE);
AzureIoTClient 12:658ca6865de2 2005 if (transport_data->topic_DeviceMethods == NULL)
AzureIoTClient 12:658ca6865de2 2006 {
AzureIoTClient 12:658ca6865de2 2007 /*Codes_SRS_IOTHUB_MQTT_TRANSPORT_12_006 : [Upon failure IoTHubTransport_MQTT_Common_Subscribe_DeviceMethod shall return a non - zero value.]*/
AzureIoTClient 12:658ca6865de2 2008 LogError("Failure: unable constructing device method subscribe topic");
AzureIoTClient 18:ec8e5e97c6a4 2009 result = __FAILURE__;
AzureIoTClient 12:658ca6865de2 2010 }
AzureIoTClient 12:658ca6865de2 2011 else
AzureIoTClient 12:658ca6865de2 2012 {
AzureIoTClient 12:658ca6865de2 2013 /*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 2014 transport_data->topics_ToSubscribe |= SUBSCRIBE_DEVICE_METHOD_TOPIC;
AzureIoTClient 12:658ca6865de2 2015 result = 0;
AzureIoTClient 12:658ca6865de2 2016 }
AzureIoTClient 12:658ca6865de2 2017 }
AzureIoTClient 12:658ca6865de2 2018 else
AzureIoTClient 12:658ca6865de2 2019 {
AzureIoTClient 12:658ca6865de2 2020 /*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 2021 result = 0;
AzureIoTClient 12:658ca6865de2 2022 }
AzureIoTClient 12:658ca6865de2 2023
AzureIoTClient 12:658ca6865de2 2024 if (result == 0)
AzureIoTClient 12:658ca6865de2 2025 {
AzureIoTClient 12:658ca6865de2 2026 /*Codes_SRS_IOTHUB_MQTT_TRANSPORT_12_005 : [IoTHubTransport_MQTT_Common_Subscribe_DeviceMethod shall schedule the send of the subscription.]*/
AzureIoTClient 12:658ca6865de2 2027 /*Codes_SRS_IOTHUB_MQTT_TRANSPORT_12_007 : [On success IoTHubTransport_MQTT_Common_Subscribe_DeviceMethod shall return 0.]*/
AzureIoTClient 12:658ca6865de2 2028 if (transport_data->currPacketState != CONNACK_TYPE &&
AzureIoTClient 12:658ca6865de2 2029 transport_data->currPacketState != CONNECT_TYPE &&
AzureIoTClient 12:658ca6865de2 2030 transport_data->currPacketState != DISCONNECT_TYPE &&
AzureIoTClient 12:658ca6865de2 2031 transport_data->currPacketState != PACKET_TYPE_ERROR)
AzureIoTClient 12:658ca6865de2 2032 {
AzureIoTClient 12:658ca6865de2 2033 transport_data->currPacketState = SUBSCRIBE_TYPE;
AzureIoTClient 12:658ca6865de2 2034 }
AzureIoTClient 12:658ca6865de2 2035 }
AzureIoTClient 12:658ca6865de2 2036 }
AzureIoTClient 12:658ca6865de2 2037 return result;
AzureIoTClient 12:658ca6865de2 2038 }
AzureIoTClient 12:658ca6865de2 2039
AzureIoTClient 12:658ca6865de2 2040 void IoTHubTransport_MQTT_Common_Unsubscribe_DeviceMethod(IOTHUB_DEVICE_HANDLE handle)
AzureIoTClient 12:658ca6865de2 2041 {
AzureIoTClient 12:658ca6865de2 2042 PMQTTTRANSPORT_HANDLE_DATA transport_data = (PMQTTTRANSPORT_HANDLE_DATA)handle;
AzureIoTClient 12:658ca6865de2 2043 /*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 2044 if (transport_data != NULL)
AzureIoTClient 12:658ca6865de2 2045 {
AzureIoTClient 12:658ca6865de2 2046 /*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 2047 if (transport_data->topic_DeviceMethods != NULL)
AzureIoTClient 12:658ca6865de2 2048 {
AzureIoTClient 12:658ca6865de2 2049 /*Codes_SRS_IOTHUB_MQTT_TRANSPORT_12_010 : [IoTHubTransport_MQTT_Common_Unsubscribe_DeviceMethod shall construct the DEVICE_METHOD topic string for unsubscribe.]*/
AzureIoTClient 12:658ca6865de2 2050 const char* unsubscribe[1];
AzureIoTClient 12:658ca6865de2 2051 unsubscribe[0] = STRING_c_str(transport_data->topic_DeviceMethods);
AzureIoTClient 12:658ca6865de2 2052
AzureIoTClient 12:658ca6865de2 2053 /*Codes_SRS_IOTHUB_MQTT_TRANSPORT_12_011 : [IoTHubTransport_MQTT_Common_Unsubscribe_DeviceMethod shall send the unsubscribe.]*/
AzureIoTClient 12:658ca6865de2 2054 if (mqtt_client_unsubscribe(transport_data->mqttClient, get_next_packet_id(transport_data), unsubscribe, 1) != 0)
AzureIoTClient 12:658ca6865de2 2055 {
AzureIoTClient 12:658ca6865de2 2056 LogError("Failure calling mqtt_client_unsubscribe");
AzureIoTClient 12:658ca6865de2 2057 }
AzureIoTClient 12:658ca6865de2 2058
AzureIoTClient 12:658ca6865de2 2059 /*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 2060 STRING_delete(transport_data->topic_DeviceMethods);
AzureIoTClient 12:658ca6865de2 2061 transport_data->topic_DeviceMethods = NULL;
AzureIoTClient 12:658ca6865de2 2062 transport_data->topics_ToSubscribe &= ~SUBSCRIBE_DEVICE_METHOD_TOPIC;
AzureIoTClient 12:658ca6865de2 2063 }
AzureIoTClient 12:658ca6865de2 2064 }
AzureIoTClient 12:658ca6865de2 2065 else
AzureIoTClient 12:658ca6865de2 2066 {
AzureIoTClient 12:658ca6865de2 2067 LogError("Invalid argument to unsubscribe (NULL).");
AzureIoTClient 12:658ca6865de2 2068 }
AzureIoTClient 12:658ca6865de2 2069 }
AzureIoTClient 12:658ca6865de2 2070
AzureIoTClient 14:4dc2b011be33 2071 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 2072 {
AzureIoTClient 14:4dc2b011be33 2073 int result;
AzureIoTClient 14:4dc2b011be33 2074 MQTTTRANSPORT_HANDLE_DATA* transport_data = (MQTTTRANSPORT_HANDLE_DATA*)handle;
AzureIoTClient 14:4dc2b011be33 2075 if (transport_data != NULL)
AzureIoTClient 14:4dc2b011be33 2076 {
AzureIoTClient 14:4dc2b011be33 2077 /* 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 2078 DEVICE_METHOD_INFO* dev_method_info = (DEVICE_METHOD_INFO*)methodId;
AzureIoTClient 14:4dc2b011be33 2079 if (dev_method_info == NULL)
AzureIoTClient 14:4dc2b011be33 2080 {
AzureIoTClient 14:4dc2b011be33 2081 LogError("Failure: DEVICE_METHOD_INFO was NULL");
AzureIoTClient 18:ec8e5e97c6a4 2082 result = __FAILURE__;
AzureIoTClient 14:4dc2b011be33 2083 }
AzureIoTClient 14:4dc2b011be33 2084 else
AzureIoTClient 14:4dc2b011be33 2085 {
AzureIoTClient 14:4dc2b011be33 2086 if (publish_device_method_message(transport_data, status, dev_method_info->request_id, response, respSize) != 0)
AzureIoTClient 14:4dc2b011be33 2087 {
AzureIoTClient 14:4dc2b011be33 2088 /* 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 2089 LogError("Failure: publishing device method response");
AzureIoTClient 18:ec8e5e97c6a4 2090 result = __FAILURE__;
AzureIoTClient 14:4dc2b011be33 2091 }
AzureIoTClient 14:4dc2b011be33 2092 else
AzureIoTClient 14:4dc2b011be33 2093 {
AzureIoTClient 14:4dc2b011be33 2094 result = 0;
AzureIoTClient 14:4dc2b011be33 2095 }
AzureIoTClient 14:4dc2b011be33 2096 STRING_delete(dev_method_info->request_id);
AzureIoTClient 14:4dc2b011be33 2097 free(dev_method_info);
AzureIoTClient 14:4dc2b011be33 2098 }
AzureIoTClient 14:4dc2b011be33 2099 }
AzureIoTClient 14:4dc2b011be33 2100 else
AzureIoTClient 14:4dc2b011be33 2101 {
AzureIoTClient 14:4dc2b011be33 2102 /* 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 2103 result = __FAILURE__;
AzureIoTClient 14:4dc2b011be33 2104 LogError("Failure: invalid IOTHUB_DEVICE_HANDLE parameter specified");
AzureIoTClient 14:4dc2b011be33 2105 }
AzureIoTClient 14:4dc2b011be33 2106 return result;
AzureIoTClient 14:4dc2b011be33 2107 }
AzureIoTClient 14:4dc2b011be33 2108
AzureIoTClient 11:31ebaeb51e1e 2109 int IoTHubTransport_MQTT_Common_Subscribe(IOTHUB_DEVICE_HANDLE handle)
AzureIoTClient 11:31ebaeb51e1e 2110 {
AzureIoTClient 11:31ebaeb51e1e 2111 int result;
AzureIoTClient 11:31ebaeb51e1e 2112 PMQTTTRANSPORT_HANDLE_DATA transport_data = (PMQTTTRANSPORT_HANDLE_DATA)handle;
AzureIoTClient 11:31ebaeb51e1e 2113 if (transport_data == NULL)
AzureIoTClient 11:31ebaeb51e1e 2114 {
AzureIoTClient 12:658ca6865de2 2115 /* 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 2116 LogError("Invalid handle parameter. NULL.");
AzureIoTClient 18:ec8e5e97c6a4 2117 result = __FAILURE__;
AzureIoTClient 11:31ebaeb51e1e 2118 }
AzureIoTClient 11:31ebaeb51e1e 2119 else
AzureIoTClient 11:31ebaeb51e1e 2120 {
AzureIoTClient 12:658ca6865de2 2121 /* 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 2122 transport_data->topic_MqttMessage = STRING_construct_sprintf(TOPIC_DEVICE_MSG, STRING_c_str(transport_data->device_id) );
AzureIoTClient 11:31ebaeb51e1e 2123 if (transport_data->topic_MqttMessage == NULL)
AzureIoTClient 11:31ebaeb51e1e 2124 {
AzureIoTClient 11:31ebaeb51e1e 2125 LogError("Failure constructing Message Topic");
AzureIoTClient 18:ec8e5e97c6a4 2126 result = __FAILURE__;
AzureIoTClient 11:31ebaeb51e1e 2127 }
AzureIoTClient 11:31ebaeb51e1e 2128 else
AzureIoTClient 11:31ebaeb51e1e 2129 {
AzureIoTClient 11:31ebaeb51e1e 2130 transport_data->topics_ToSubscribe |= SUBSCRIBE_TELEMETRY_TOPIC;
AzureIoTClient 12:658ca6865de2 2131 /* 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 2132 if (transport_data->currPacketState != CONNACK_TYPE &&
AzureIoTClient 11:31ebaeb51e1e 2133 transport_data->currPacketState != CONNECT_TYPE &&
AzureIoTClient 11:31ebaeb51e1e 2134 transport_data->currPacketState != DISCONNECT_TYPE &&
AzureIoTClient 11:31ebaeb51e1e 2135 transport_data->currPacketState != PACKET_TYPE_ERROR)
AzureIoTClient 11:31ebaeb51e1e 2136 {
AzureIoTClient 11:31ebaeb51e1e 2137 transport_data->currPacketState = SUBSCRIBE_TYPE;
AzureIoTClient 11:31ebaeb51e1e 2138 }
AzureIoTClient 11:31ebaeb51e1e 2139 result = 0;
AzureIoTClient 11:31ebaeb51e1e 2140 }
AzureIoTClient 11:31ebaeb51e1e 2141 }
AzureIoTClient 11:31ebaeb51e1e 2142 return result;
AzureIoTClient 11:31ebaeb51e1e 2143 }
AzureIoTClient 11:31ebaeb51e1e 2144
AzureIoTClient 11:31ebaeb51e1e 2145 void IoTHubTransport_MQTT_Common_Unsubscribe(IOTHUB_DEVICE_HANDLE handle)
AzureIoTClient 11:31ebaeb51e1e 2146 {
AzureIoTClient 11:31ebaeb51e1e 2147 PMQTTTRANSPORT_HANDLE_DATA transport_data = (PMQTTTRANSPORT_HANDLE_DATA)handle;
AzureIoTClient 12:658ca6865de2 2148 /* Codes_SRS_IOTHUB_MQTT_TRANSPORT_07_019: [If parameter handle is NULL then IoTHubTransport_MQTT_Common_Unsubscribe shall do nothing.] */
AzureIoTClient 11:31ebaeb51e1e 2149 if (transport_data != NULL)
AzureIoTClient 11:31ebaeb51e1e 2150 {
AzureIoTClient 12:658ca6865de2 2151 /* 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 2152 const char* unsubscribe[1];
AzureIoTClient 11:31ebaeb51e1e 2153 unsubscribe[0] = STRING_c_str(transport_data->topic_MqttMessage);
AzureIoTClient 11:31ebaeb51e1e 2154 if (mqtt_client_unsubscribe(transport_data->mqttClient, get_next_packet_id(transport_data), unsubscribe, 1) != 0)
AzureIoTClient 11:31ebaeb51e1e 2155 {
AzureIoTClient 11:31ebaeb51e1e 2156 LogError("Failure calling mqtt_client_unsubscribe");
AzureIoTClient 11:31ebaeb51e1e 2157 }
AzureIoTClient 11:31ebaeb51e1e 2158 STRING_delete(transport_data->topic_MqttMessage);
AzureIoTClient 11:31ebaeb51e1e 2159 transport_data->topic_MqttMessage = NULL;
AzureIoTClient 11:31ebaeb51e1e 2160 transport_data->topics_ToSubscribe &= ~SUBSCRIBE_TELEMETRY_TOPIC;
AzureIoTClient 11:31ebaeb51e1e 2161 }
AzureIoTClient 11:31ebaeb51e1e 2162 else
AzureIoTClient 11:31ebaeb51e1e 2163 {
AzureIoTClient 11:31ebaeb51e1e 2164 LogError("Invalid argument to unsubscribe (NULL).");
AzureIoTClient 11:31ebaeb51e1e 2165 }
AzureIoTClient 11:31ebaeb51e1e 2166 }
AzureIoTClient 11:31ebaeb51e1e 2167
AzureIoTClient 12:658ca6865de2 2168 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 2169 {
AzureIoTClient 12:658ca6865de2 2170 IOTHUB_PROCESS_ITEM_RESULT result;
AzureIoTClient 12:658ca6865de2 2171 /* 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 2172 if (handle == NULL || iothub_item == NULL)
AzureIoTClient 12:658ca6865de2 2173 {
AzureIoTClient 12:658ca6865de2 2174 LogError("Invalid handle parameter iothub_item=%p", iothub_item);
AzureIoTClient 12:658ca6865de2 2175 result = IOTHUB_PROCESS_ERROR;
AzureIoTClient 12:658ca6865de2 2176 }
AzureIoTClient 12:658ca6865de2 2177 else
AzureIoTClient 12:658ca6865de2 2178 {
AzureIoTClient 12:658ca6865de2 2179 PMQTTTRANSPORT_HANDLE_DATA transport_data = (PMQTTTRANSPORT_HANDLE_DATA)handle;
AzureIoTClient 12:658ca6865de2 2180
AzureIoTClient 12:658ca6865de2 2181 if (transport_data->currPacketState == PUBLISH_TYPE)
AzureIoTClient 12:658ca6865de2 2182 {
AzureIoTClient 12:658ca6865de2 2183 if (item_type == IOTHUB_TYPE_DEVICE_TWIN)
AzureIoTClient 12:658ca6865de2 2184 {
AzureIoTClient 12:658ca6865de2 2185 MQTT_DEVICE_TWIN_ITEM* mqtt_info = (MQTT_DEVICE_TWIN_ITEM*)malloc(sizeof(MQTT_DEVICE_TWIN_ITEM));
AzureIoTClient 12:658ca6865de2 2186 if (mqtt_info == NULL)
AzureIoTClient 12:658ca6865de2 2187 {
AzureIoTClient 12:658ca6865de2 2188 /* Codes_SRS_IOTHUBCLIENT_LL_07_004: [ If any errors are encountered IoTHubTransport_MQTT_Common_ProcessItem shall return IOTHUB_PROCESS_ERROR. ]*/
AzureIoTClient 12:658ca6865de2 2189 result = IOTHUB_PROCESS_ERROR;
AzureIoTClient 12:658ca6865de2 2190 }
AzureIoTClient 12:658ca6865de2 2191 else
AzureIoTClient 12:658ca6865de2 2192 {
AzureIoTClient 12:658ca6865de2 2193 /*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 2194 mqtt_info->iothub_type = item_type;
AzureIoTClient 12:658ca6865de2 2195 mqtt_info->iothub_msg_id = iothub_item->device_twin->item_id;
AzureIoTClient 12:658ca6865de2 2196 mqtt_info->retryCount = 0;
AzureIoTClient 12:658ca6865de2 2197
AzureIoTClient 12:658ca6865de2 2198 /* Codes_SRS_IOTHUBCLIENT_LL_07_005: [ If successful IoTHubTransport_MQTT_Common_ProcessItem shall add mqtt info structure acknowledgement queue. ] */
AzureIoTClient 12:658ca6865de2 2199 DList_InsertTailList(&transport_data->ack_waiting_queue, &mqtt_info->entry);
AzureIoTClient 12:658ca6865de2 2200
AzureIoTClient 12:658ca6865de2 2201 if (publish_device_twin_message(transport_data, iothub_item->device_twin, mqtt_info) != 0)
AzureIoTClient 12:658ca6865de2 2202 {
AzureIoTClient 12:658ca6865de2 2203 DList_RemoveEntryList(&mqtt_info->entry);
AzureIoTClient 12:658ca6865de2 2204
AzureIoTClient 12:658ca6865de2 2205 free(mqtt_info);
AzureIoTClient 12:658ca6865de2 2206 /* Codes_SRS_IOTHUBCLIENT_LL_07_004: [ If any errors are encountered IoTHubTransport_MQTT_Common_ProcessItem shall return IOTHUB_PROCESS_ERROR. ]*/
AzureIoTClient 12:658ca6865de2 2207 result = IOTHUB_PROCESS_ERROR;
AzureIoTClient 12:658ca6865de2 2208 }
AzureIoTClient 12:658ca6865de2 2209 else
AzureIoTClient 12:658ca6865de2 2210 {
AzureIoTClient 12:658ca6865de2 2211 result = IOTHUB_PROCESS_OK;
AzureIoTClient 12:658ca6865de2 2212 }
AzureIoTClient 12:658ca6865de2 2213 }
AzureIoTClient 12:658ca6865de2 2214 }
AzureIoTClient 12:658ca6865de2 2215 else
AzureIoTClient 12:658ca6865de2 2216 {
AzureIoTClient 12:658ca6865de2 2217 /* 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 2218 result = IOTHUB_PROCESS_CONTINUE;
AzureIoTClient 12:658ca6865de2 2219 }
AzureIoTClient 12:658ca6865de2 2220 }
AzureIoTClient 12:658ca6865de2 2221 else
AzureIoTClient 12:658ca6865de2 2222 {
AzureIoTClient 12:658ca6865de2 2223 /* 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 2224 result = IOTHUB_PROCESS_NOT_CONNECTED;
AzureIoTClient 12:658ca6865de2 2225 }
AzureIoTClient 12:658ca6865de2 2226 }
AzureIoTClient 12:658ca6865de2 2227 return result;
AzureIoTClient 12:658ca6865de2 2228 }
AzureIoTClient 12:658ca6865de2 2229
AzureIoTClient 12:658ca6865de2 2230 /* 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 2231 void IoTHubTransport_MQTT_Common_DoWork(TRANSPORT_LL_HANDLE handle, IOTHUB_CLIENT_LL_HANDLE iotHubClientHandle)
AzureIoTClient 11:31ebaeb51e1e 2232 {
AzureIoTClient 12:658ca6865de2 2233 /* 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 2234 PMQTTTRANSPORT_HANDLE_DATA transport_data = (PMQTTTRANSPORT_HANDLE_DATA)handle;
AzureIoTClient 11:31ebaeb51e1e 2235 if (transport_data != NULL && iotHubClientHandle != NULL)
AzureIoTClient 11:31ebaeb51e1e 2236 {
AzureIoTClient 11:31ebaeb51e1e 2237 transport_data->llClientHandle = iotHubClientHandle;
AzureIoTClient 11:31ebaeb51e1e 2238
AzureIoTClient 11:31ebaeb51e1e 2239 if (InitializeConnection(transport_data) != 0)
AzureIoTClient 11:31ebaeb51e1e 2240 {
AzureIoTClient 11:31ebaeb51e1e 2241 // Don't want to flood the logs with failures here
AzureIoTClient 11:31ebaeb51e1e 2242 }
AzureIoTClient 11:31ebaeb51e1e 2243 else
AzureIoTClient 11:31ebaeb51e1e 2244 {
AzureIoTClient 11:31ebaeb51e1e 2245 if (transport_data->currPacketState == CONNACK_TYPE || transport_data->currPacketState == SUBSCRIBE_TYPE)
AzureIoTClient 11:31ebaeb51e1e 2246 {
AzureIoTClient 11:31ebaeb51e1e 2247 SubscribeToMqttProtocol(transport_data);
AzureIoTClient 11:31ebaeb51e1e 2248 }
AzureIoTClient 11:31ebaeb51e1e 2249 else if (transport_data->currPacketState == SUBACK_TYPE)
AzureIoTClient 11:31ebaeb51e1e 2250 {
AzureIoTClient 12:658ca6865de2 2251 if ((transport_data->topic_NotifyState != NULL || transport_data->topic_GetState != NULL) &&
AzureIoTClient 12:658ca6865de2 2252 !transport_data->device_twin_get_sent)
AzureIoTClient 12:658ca6865de2 2253 {
AzureIoTClient 12:658ca6865de2 2254 /* 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 2255 if (publish_device_twin_get_message(transport_data) == 0)
AzureIoTClient 12:658ca6865de2 2256 {
AzureIoTClient 12:658ca6865de2 2257 transport_data->device_twin_get_sent = true;
AzureIoTClient 12:658ca6865de2 2258 }
AzureIoTClient 12:658ca6865de2 2259 else
AzureIoTClient 12:658ca6865de2 2260 {
AzureIoTClient 12:658ca6865de2 2261 LogError("Failure: sending device twin get property command.");
AzureIoTClient 12:658ca6865de2 2262 }
AzureIoTClient 12:658ca6865de2 2263 }
AzureIoTClient 11:31ebaeb51e1e 2264 // Publish can be called now
AzureIoTClient 11:31ebaeb51e1e 2265 transport_data->currPacketState = PUBLISH_TYPE;
AzureIoTClient 11:31ebaeb51e1e 2266 }
AzureIoTClient 11:31ebaeb51e1e 2267 else if (transport_data->currPacketState == PUBLISH_TYPE)
AzureIoTClient 11:31ebaeb51e1e 2268 {
AzureIoTClient 11:31ebaeb51e1e 2269 PDLIST_ENTRY currentListEntry = transport_data->telemetry_waitingForAck.Flink;
AzureIoTClient 11:31ebaeb51e1e 2270 while (currentListEntry != &transport_data->telemetry_waitingForAck)
AzureIoTClient 11:31ebaeb51e1e 2271 {
AzureIoTClient 24:4096249decf1 2272 tickcounter_ms_t current_ms;
AzureIoTClient 11:31ebaeb51e1e 2273 MQTT_MESSAGE_DETAILS_LIST* mqttMsgEntry = containingRecord(currentListEntry, MQTT_MESSAGE_DETAILS_LIST, entry);
AzureIoTClient 11:31ebaeb51e1e 2274 DLIST_ENTRY nextListEntry;
AzureIoTClient 11:31ebaeb51e1e 2275 nextListEntry.Flink = currentListEntry->Flink;
AzureIoTClient 11:31ebaeb51e1e 2276
Azure.IoT.Build 13:606465879c57 2277 (void)tickcounter_get_current_ms(transport_data->msgTickCounter, &current_ms);
AzureIoTClient 12:658ca6865de2 2278 /* 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 2279 if (((current_ms - mqttMsgEntry->msgPublishTime) / 1000) > RESEND_TIMEOUT_VALUE_MIN)
AzureIoTClient 11:31ebaeb51e1e 2280 {
AzureIoTClient 24:4096249decf1 2281 /* 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 2282 if (mqttMsgEntry->retryCount >= MAX_SEND_RECOUNT_LIMIT)
AzureIoTClient 11:31ebaeb51e1e 2283 {
AzureIoTClient 24:4096249decf1 2284 PDLIST_ENTRY current_entry;
AzureIoTClient 11:31ebaeb51e1e 2285 (void)DList_RemoveEntryList(currentListEntry);
AzureIoTClient 11:31ebaeb51e1e 2286 sendMsgComplete(mqttMsgEntry->iotHubMessageEntry, transport_data, IOTHUB_CLIENT_CONFIRMATION_MESSAGE_TIMEOUT);
AzureIoTClient 11:31ebaeb51e1e 2287 free(mqttMsgEntry);
AzureIoTClient 24:4096249decf1 2288
AzureIoTClient 24:4096249decf1 2289 transport_data->currPacketState = PACKET_TYPE_ERROR;
AzureIoTClient 24:4096249decf1 2290 transport_data->device_twin_get_sent = false;
AzureIoTClient 24:4096249decf1 2291 DisconnectFromClient(transport_data);
AzureIoTClient 24:4096249decf1 2292
AzureIoTClient 24:4096249decf1 2293 /* 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 2294 current_entry = transport_data->telemetry_waitingForAck.Flink;
AzureIoTClient 24:4096249decf1 2295 while (current_entry != &transport_data->telemetry_waitingForAck)
AzureIoTClient 24:4096249decf1 2296 {
AzureIoTClient 24:4096249decf1 2297 MQTT_MESSAGE_DETAILS_LIST* msg_reset_entry;
AzureIoTClient 24:4096249decf1 2298 msg_reset_entry = containingRecord(current_entry, MQTT_MESSAGE_DETAILS_LIST, entry);
AzureIoTClient 24:4096249decf1 2299 msg_reset_entry->retryCount = 0;
AzureIoTClient 24:4096249decf1 2300 current_entry = current_entry->Flink;
AzureIoTClient 24:4096249decf1 2301 }
AzureIoTClient 11:31ebaeb51e1e 2302 }
AzureIoTClient 11:31ebaeb51e1e 2303 else
AzureIoTClient 11:31ebaeb51e1e 2304 {
AzureIoTClient 11:31ebaeb51e1e 2305 size_t messageLength;
AzureIoTClient 11:31ebaeb51e1e 2306 const unsigned char* messagePayload = RetrieveMessagePayload(mqttMsgEntry->iotHubMessageEntry->messageHandle, &messageLength);
AzureIoTClient 11:31ebaeb51e1e 2307 if (messageLength == 0 || messagePayload == NULL)
AzureIoTClient 11:31ebaeb51e1e 2308 {
AzureIoTClient 11:31ebaeb51e1e 2309 LogError("Failure from creating Message IoTHubMessage_GetData");
AzureIoTClient 11:31ebaeb51e1e 2310 }
AzureIoTClient 11:31ebaeb51e1e 2311 else
AzureIoTClient 11:31ebaeb51e1e 2312 {
AzureIoTClient 11:31ebaeb51e1e 2313 if (publish_mqtt_telemetry_msg(transport_data, mqttMsgEntry, messagePayload, messageLength) != 0)
AzureIoTClient 11:31ebaeb51e1e 2314 {
AzureIoTClient 11:31ebaeb51e1e 2315 (void)DList_RemoveEntryList(currentListEntry);
AzureIoTClient 11:31ebaeb51e1e 2316 sendMsgComplete(mqttMsgEntry->iotHubMessageEntry, transport_data, IOTHUB_CLIENT_CONFIRMATION_ERROR);
AzureIoTClient 11:31ebaeb51e1e 2317 free(mqttMsgEntry);
AzureIoTClient 11:31ebaeb51e1e 2318 }
AzureIoTClient 11:31ebaeb51e1e 2319 }
AzureIoTClient 11:31ebaeb51e1e 2320 }
AzureIoTClient 11:31ebaeb51e1e 2321 }
AzureIoTClient 11:31ebaeb51e1e 2322 currentListEntry = nextListEntry.Flink;
AzureIoTClient 11:31ebaeb51e1e 2323 }
AzureIoTClient 11:31ebaeb51e1e 2324
AzureIoTClient 11:31ebaeb51e1e 2325 currentListEntry = transport_data->waitingToSend->Flink;
AzureIoTClient 12:658ca6865de2 2326 /* Codes_SRS_IOTHUB_MQTT_TRANSPORT_07_027: [IoTHubTransport_MQTT_Common_DoWork shall inspect the "waitingToSend" DLIST passed in config structure.] */
AzureIoTClient 11:31ebaeb51e1e 2327 while (currentListEntry != transport_data->waitingToSend)
AzureIoTClient 11:31ebaeb51e1e 2328 {
AzureIoTClient 11:31ebaeb51e1e 2329 IOTHUB_MESSAGE_LIST* iothubMsgList = containingRecord(currentListEntry, IOTHUB_MESSAGE_LIST, entry);
AzureIoTClient 11:31ebaeb51e1e 2330 DLIST_ENTRY savedFromCurrentListEntry;
AzureIoTClient 11:31ebaeb51e1e 2331 savedFromCurrentListEntry.Flink = currentListEntry->Flink;
AzureIoTClient 11:31ebaeb51e1e 2332
AzureIoTClient 12:658ca6865de2 2333 /* Codes_SRS_IOTHUB_MQTT_TRANSPORT_07_027: [IoTHubTransport_MQTT_Common_DoWork shall inspect the "waitingToSend" DLIST passed in config structure.] */
AzureIoTClient 11:31ebaeb51e1e 2334 size_t messageLength;
AzureIoTClient 11:31ebaeb51e1e 2335 const unsigned char* messagePayload = RetrieveMessagePayload(iothubMsgList->messageHandle, &messageLength);
AzureIoTClient 11:31ebaeb51e1e 2336 if (messageLength == 0 || messagePayload == NULL)
AzureIoTClient 11:31ebaeb51e1e 2337 {
AzureIoTClient 11:31ebaeb51e1e 2338 LogError("Failure result from IoTHubMessage_GetData");
AzureIoTClient 11:31ebaeb51e1e 2339 }
AzureIoTClient 11:31ebaeb51e1e 2340 else
AzureIoTClient 11:31ebaeb51e1e 2341 {
AzureIoTClient 12:658ca6865de2 2342 /* 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 2343 MQTT_MESSAGE_DETAILS_LIST* mqttMsgEntry = (MQTT_MESSAGE_DETAILS_LIST*)malloc(sizeof(MQTT_MESSAGE_DETAILS_LIST));
AzureIoTClient 11:31ebaeb51e1e 2344 if (mqttMsgEntry == NULL)
AzureIoTClient 11:31ebaeb51e1e 2345 {
AzureIoTClient 11:31ebaeb51e1e 2346 LogError("Allocation Error: Failure allocating MQTT Message Detail List.");
AzureIoTClient 11:31ebaeb51e1e 2347 }
AzureIoTClient 11:31ebaeb51e1e 2348 else
AzureIoTClient 11:31ebaeb51e1e 2349 {
AzureIoTClient 11:31ebaeb51e1e 2350 mqttMsgEntry->retryCount = 0;
AzureIoTClient 11:31ebaeb51e1e 2351 mqttMsgEntry->iotHubMessageEntry = iothubMsgList;
AzureIoTClient 11:31ebaeb51e1e 2352 mqttMsgEntry->packet_id = get_next_packet_id(transport_data);
AzureIoTClient 11:31ebaeb51e1e 2353 if (publish_mqtt_telemetry_msg(transport_data, mqttMsgEntry, messagePayload, messageLength) != 0)
AzureIoTClient 11:31ebaeb51e1e 2354 {
AzureIoTClient 11:31ebaeb51e1e 2355 (void)(DList_RemoveEntryList(currentListEntry));
AzureIoTClient 11:31ebaeb51e1e 2356 sendMsgComplete(iothubMsgList, transport_data, IOTHUB_CLIENT_CONFIRMATION_ERROR);
AzureIoTClient 11:31ebaeb51e1e 2357 free(mqttMsgEntry);
AzureIoTClient 11:31ebaeb51e1e 2358 }
AzureIoTClient 11:31ebaeb51e1e 2359 else
AzureIoTClient 11:31ebaeb51e1e 2360 {
AzureIoTClient 11:31ebaeb51e1e 2361 (void)(DList_RemoveEntryList(currentListEntry));
AzureIoTClient 11:31ebaeb51e1e 2362 DList_InsertTailList(&(transport_data->telemetry_waitingForAck), &(mqttMsgEntry->entry));
AzureIoTClient 11:31ebaeb51e1e 2363 }
AzureIoTClient 11:31ebaeb51e1e 2364 }
AzureIoTClient 11:31ebaeb51e1e 2365 }
AzureIoTClient 11:31ebaeb51e1e 2366 currentListEntry = savedFromCurrentListEntry.Flink;
AzureIoTClient 11:31ebaeb51e1e 2367 }
AzureIoTClient 11:31ebaeb51e1e 2368 }
AzureIoTClient 20:594780be216d 2369 /* 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 2370 mqtt_client_dowork(transport_data->mqttClient);
AzureIoTClient 11:31ebaeb51e1e 2371 }
AzureIoTClient 11:31ebaeb51e1e 2372 }
AzureIoTClient 11:31ebaeb51e1e 2373 }
AzureIoTClient 11:31ebaeb51e1e 2374
AzureIoTClient 11:31ebaeb51e1e 2375 IOTHUB_CLIENT_RESULT IoTHubTransport_MQTT_Common_GetSendStatus(IOTHUB_DEVICE_HANDLE handle, IOTHUB_CLIENT_STATUS *iotHubClientStatus)
AzureIoTClient 11:31ebaeb51e1e 2376 {
AzureIoTClient 11:31ebaeb51e1e 2377 IOTHUB_CLIENT_RESULT result;
AzureIoTClient 11:31ebaeb51e1e 2378
AzureIoTClient 11:31ebaeb51e1e 2379 if (handle == NULL || iotHubClientStatus == NULL)
AzureIoTClient 11:31ebaeb51e1e 2380 {
AzureIoTClient 12:658ca6865de2 2381 /* 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 2382 LogError("invalid arument.");
AzureIoTClient 11:31ebaeb51e1e 2383 result = IOTHUB_CLIENT_INVALID_ARG;
AzureIoTClient 11:31ebaeb51e1e 2384 }
AzureIoTClient 11:31ebaeb51e1e 2385 else
AzureIoTClient 11:31ebaeb51e1e 2386 {
AzureIoTClient 11:31ebaeb51e1e 2387 MQTTTRANSPORT_HANDLE_DATA* handleData = (MQTTTRANSPORT_HANDLE_DATA*)handle;
AzureIoTClient 11:31ebaeb51e1e 2388 if (!DList_IsListEmpty(handleData->waitingToSend) || !DList_IsListEmpty(&(handleData->telemetry_waitingForAck)))
AzureIoTClient 11:31ebaeb51e1e 2389 {
AzureIoTClient 12:658ca6865de2 2390 /* 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 2391 *iotHubClientStatus = IOTHUB_CLIENT_SEND_STATUS_BUSY;
AzureIoTClient 11:31ebaeb51e1e 2392 }
AzureIoTClient 11:31ebaeb51e1e 2393 else
AzureIoTClient 11:31ebaeb51e1e 2394 {
AzureIoTClient 12:658ca6865de2 2395 /* 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 2396 *iotHubClientStatus = IOTHUB_CLIENT_SEND_STATUS_IDLE;
AzureIoTClient 11:31ebaeb51e1e 2397 }
AzureIoTClient 11:31ebaeb51e1e 2398 result = IOTHUB_CLIENT_OK;
AzureIoTClient 11:31ebaeb51e1e 2399 }
AzureIoTClient 11:31ebaeb51e1e 2400 return result;
AzureIoTClient 11:31ebaeb51e1e 2401 }
AzureIoTClient 11:31ebaeb51e1e 2402
AzureIoTClient 11:31ebaeb51e1e 2403 IOTHUB_CLIENT_RESULT IoTHubTransport_MQTT_Common_SetOption(TRANSPORT_LL_HANDLE handle, const char* option, const void* value)
AzureIoTClient 11:31ebaeb51e1e 2404 {
AzureIoTClient 12:658ca6865de2 2405 /* 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 2406 IOTHUB_CLIENT_RESULT result;
AzureIoTClient 11:31ebaeb51e1e 2407 if (
AzureIoTClient 11:31ebaeb51e1e 2408 (handle == NULL) ||
AzureIoTClient 11:31ebaeb51e1e 2409 (option == NULL) ||
AzureIoTClient 11:31ebaeb51e1e 2410 (value == NULL)
AzureIoTClient 11:31ebaeb51e1e 2411 )
AzureIoTClient 11:31ebaeb51e1e 2412 {
AzureIoTClient 11:31ebaeb51e1e 2413 result = IOTHUB_CLIENT_INVALID_ARG;
AzureIoTClient 20:594780be216d 2414 LogError("invalid parameter (NULL) passed to IoTHubTransport_MQTT_Common_SetOption.");
AzureIoTClient 11:31ebaeb51e1e 2415 }
AzureIoTClient 11:31ebaeb51e1e 2416 else
AzureIoTClient 11:31ebaeb51e1e 2417 {
AzureIoTClient 11:31ebaeb51e1e 2418 MQTTTRANSPORT_HANDLE_DATA* transport_data = (MQTTTRANSPORT_HANDLE_DATA*)handle;
AzureIoTClient 21:167f8fcf6fb1 2419
AzureIoTClient 21:167f8fcf6fb1 2420 IOTHUB_CREDENTIAL_TYPE cred_type = IoTHubClient_Auth_Get_Credential_Type(transport_data->authorization_module);
AzureIoTClient 21:167f8fcf6fb1 2421
AzureIoTClient 11:31ebaeb51e1e 2422 /* 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 2423 if (strcmp(OPTION_LOG_TRACE, option) == 0)
AzureIoTClient 11:31ebaeb51e1e 2424 {
AzureIoTClient 11:31ebaeb51e1e 2425 transport_data->log_trace = *((bool*)value);
AzureIoTClient 11:31ebaeb51e1e 2426 mqtt_client_set_trace(transport_data->mqttClient, transport_data->log_trace, transport_data->raw_trace);
AzureIoTClient 11:31ebaeb51e1e 2427 result = IOTHUB_CLIENT_OK;
AzureIoTClient 11:31ebaeb51e1e 2428 }
AzureIoTClient 11:31ebaeb51e1e 2429 else if (strcmp("rawlogtrace", option) == 0)
AzureIoTClient 11:31ebaeb51e1e 2430 {
AzureIoTClient 11:31ebaeb51e1e 2431 transport_data->raw_trace = *((bool*)value);
AzureIoTClient 11:31ebaeb51e1e 2432 mqtt_client_set_trace(transport_data->mqttClient, transport_data->log_trace, transport_data->raw_trace);
AzureIoTClient 11:31ebaeb51e1e 2433 result = IOTHUB_CLIENT_OK;
AzureIoTClient 11:31ebaeb51e1e 2434 }
AzureIoTClient 11:31ebaeb51e1e 2435 else if (strcmp(OPTION_KEEP_ALIVE, option) == 0)
AzureIoTClient 11:31ebaeb51e1e 2436 {
AzureIoTClient 11:31ebaeb51e1e 2437 /* 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 2438 int* keepAliveOption = (int*)value;
AzureIoTClient 12:658ca6865de2 2439 /* 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 2440 if (*keepAliveOption != transport_data->keepAliveValue)
AzureIoTClient 11:31ebaeb51e1e 2441 {
AzureIoTClient 11:31ebaeb51e1e 2442 transport_data->keepAliveValue = (uint16_t)(*keepAliveOption);
AzureIoTClient 20:594780be216d 2443 if (transport_data->mqttClientStatus != MQTT_CLIENT_STATUS_NOT_CONNECTED)
AzureIoTClient 11:31ebaeb51e1e 2444 {
AzureIoTClient 20:594780be216d 2445 /* 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 2446 DisconnectFromClient(transport_data);
AzureIoTClient 11:31ebaeb51e1e 2447 }
AzureIoTClient 11:31ebaeb51e1e 2448 }
AzureIoTClient 11:31ebaeb51e1e 2449 result = IOTHUB_CLIENT_OK;
AzureIoTClient 11:31ebaeb51e1e 2450 }
AzureIoTClient 11:31ebaeb51e1e 2451 /* 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 2452 else if ((strcmp(OPTION_X509_CERT, option) == 0) && (cred_type != IOTHUB_CREDENTIAL_TYPE_X509 && cred_type != IOTHUB_CREDENTIAL_TYPE_UNKNOWN))
AzureIoTClient 11:31ebaeb51e1e 2453 {
AzureIoTClient 11:31ebaeb51e1e 2454 LogError("x509certificate specified, but authentication method is not x509");
AzureIoTClient 11:31ebaeb51e1e 2455 result = IOTHUB_CLIENT_INVALID_ARG;
AzureIoTClient 11:31ebaeb51e1e 2456 }
AzureIoTClient 11:31ebaeb51e1e 2457 /* 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 2458 else if ((strcmp(OPTION_X509_PRIVATE_KEY, option) == 0) && (cred_type != IOTHUB_CREDENTIAL_TYPE_X509 && cred_type != IOTHUB_CREDENTIAL_TYPE_UNKNOWN))
AzureIoTClient 11:31ebaeb51e1e 2459 {
AzureIoTClient 11:31ebaeb51e1e 2460 LogError("x509privatekey specified, but authentication method is not x509");
AzureIoTClient 11:31ebaeb51e1e 2461 result = IOTHUB_CLIENT_INVALID_ARG;
AzureIoTClient 11:31ebaeb51e1e 2462 }
AzureIoTClient 20:594780be216d 2463 else if (strcmp(OPTION_HTTP_PROXY, option) == 0)
AzureIoTClient 20:594780be216d 2464 {
AzureIoTClient 20:594780be216d 2465 /* 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 2466 HTTP_PROXY_OPTIONS* proxy_options = (HTTP_PROXY_OPTIONS*)value;
AzureIoTClient 20:594780be216d 2467
AzureIoTClient 20:594780be216d 2468 if (transport_data->xioTransport != NULL)
AzureIoTClient 20:594780be216d 2469 {
AzureIoTClient 20:594780be216d 2470 /* 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 2471 LogError("Cannot set proxy option once the underlying IO is created");
AzureIoTClient 20:594780be216d 2472 result = IOTHUB_CLIENT_ERROR;
AzureIoTClient 20:594780be216d 2473 }
AzureIoTClient 20:594780be216d 2474 else if (proxy_options->host_address == NULL)
AzureIoTClient 20:594780be216d 2475 {
AzureIoTClient 20:594780be216d 2476 /* 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 2477 LogError("NULL host_address in proxy options");
AzureIoTClient 20:594780be216d 2478 result = IOTHUB_CLIENT_INVALID_ARG;
AzureIoTClient 20:594780be216d 2479 }
AzureIoTClient 20:594780be216d 2480 /* 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 2481 else if (((proxy_options->username == NULL) || (proxy_options->password == NULL)) &&
AzureIoTClient 20:594780be216d 2482 (proxy_options->username != proxy_options->password))
AzureIoTClient 20:594780be216d 2483 {
AzureIoTClient 20:594780be216d 2484 LogError("Only one of username and password for proxy settings was NULL");
AzureIoTClient 20:594780be216d 2485 result = IOTHUB_CLIENT_INVALID_ARG;
AzureIoTClient 20:594780be216d 2486 }
AzureIoTClient 20:594780be216d 2487 else
AzureIoTClient 20:594780be216d 2488 {
AzureIoTClient 20:594780be216d 2489 char* copied_proxy_hostname = NULL;
AzureIoTClient 20:594780be216d 2490 char* copied_proxy_username = NULL;
AzureIoTClient 20:594780be216d 2491 char* copied_proxy_password = NULL;
AzureIoTClient 20:594780be216d 2492
AzureIoTClient 20:594780be216d 2493 /* 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 2494 transport_data->http_proxy_port = proxy_options->port;
AzureIoTClient 20:594780be216d 2495 if (mallocAndStrcpy_s(&copied_proxy_hostname, proxy_options->host_address) != 0)
AzureIoTClient 20:594780be216d 2496 {
AzureIoTClient 20:594780be216d 2497 /* 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 2498 LogError("Cannot copy HTTP proxy hostname");
AzureIoTClient 20:594780be216d 2499 result = IOTHUB_CLIENT_ERROR;
AzureIoTClient 20:594780be216d 2500 }
AzureIoTClient 20:594780be216d 2501 /* Codes_SRS_IOTHUB_TRANSPORT_MQTT_COMMON_01_005: [ `username` and `password` shall be allowed to be NULL. ]*/
AzureIoTClient 20:594780be216d 2502 else if ((proxy_options->username != NULL) && (mallocAndStrcpy_s(&copied_proxy_username, proxy_options->username) != 0))
AzureIoTClient 20:594780be216d 2503 {
AzureIoTClient 20:594780be216d 2504 /* 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 2505 free(copied_proxy_hostname);
AzureIoTClient 20:594780be216d 2506 LogError("Cannot copy HTTP proxy username");
AzureIoTClient 20:594780be216d 2507 result = IOTHUB_CLIENT_ERROR;
AzureIoTClient 20:594780be216d 2508 }
AzureIoTClient 20:594780be216d 2509 /* Codes_SRS_IOTHUB_TRANSPORT_MQTT_COMMON_01_005: [ `username` and `password` shall be allowed to be NULL. ]*/
AzureIoTClient 20:594780be216d 2510 else if ((proxy_options->password != NULL) && (mallocAndStrcpy_s(&copied_proxy_password, proxy_options->password) != 0))
AzureIoTClient 20:594780be216d 2511 {
AzureIoTClient 20:594780be216d 2512 /* 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 2513 if (copied_proxy_username != NULL)
AzureIoTClient 20:594780be216d 2514 {
AzureIoTClient 20:594780be216d 2515 free(copied_proxy_username);
AzureIoTClient 20:594780be216d 2516 }
AzureIoTClient 20:594780be216d 2517 free(copied_proxy_hostname);
AzureIoTClient 20:594780be216d 2518 LogError("Cannot copy HTTP proxy password");
AzureIoTClient 20:594780be216d 2519 result = IOTHUB_CLIENT_ERROR;
AzureIoTClient 20:594780be216d 2520 }
AzureIoTClient 20:594780be216d 2521 else
AzureIoTClient 20:594780be216d 2522 {
AzureIoTClient 20:594780be216d 2523 /* 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 2524 free_proxy_data(transport_data);
AzureIoTClient 20:594780be216d 2525
AzureIoTClient 20:594780be216d 2526 transport_data->http_proxy_hostname = copied_proxy_hostname;
AzureIoTClient 20:594780be216d 2527 transport_data->http_proxy_username = copied_proxy_username;
AzureIoTClient 20:594780be216d 2528 transport_data->http_proxy_password = copied_proxy_password;
AzureIoTClient 20:594780be216d 2529
AzureIoTClient 20:594780be216d 2530 /* 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 2531 result = IOTHUB_CLIENT_OK;
AzureIoTClient 20:594780be216d 2532 }
AzureIoTClient 20:594780be216d 2533 }
AzureIoTClient 20:594780be216d 2534 }
AzureIoTClient 11:31ebaeb51e1e 2535 else
AzureIoTClient 11:31ebaeb51e1e 2536 {
AzureIoTClient 21:167f8fcf6fb1 2537 /* 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 2538 if ( ((strcmp(OPTION_X509_CERT, option) == 0) || (strcmp(OPTION_X509_PRIVATE_KEY, option) == 0)) && (cred_type != IOTHUB_CREDENTIAL_TYPE_X509) )
AzureIoTClient 21:167f8fcf6fb1 2539 {
AzureIoTClient 21:167f8fcf6fb1 2540 IoTHubClient_Auth_Set_x509_Type(transport_data->authorization_module, true);
AzureIoTClient 21:167f8fcf6fb1 2541 }
AzureIoTClient 21:167f8fcf6fb1 2542
AzureIoTClient 12:658ca6865de2 2543 /* 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 2544 if (GetTransportProviderIfNecessary(transport_data) == 0)
AzureIoTClient 11:31ebaeb51e1e 2545 {
AzureIoTClient 11:31ebaeb51e1e 2546 if (xio_setoption(transport_data->xioTransport, option, value) == 0)
AzureIoTClient 11:31ebaeb51e1e 2547 {
AzureIoTClient 11:31ebaeb51e1e 2548 result = IOTHUB_CLIENT_OK;
AzureIoTClient 11:31ebaeb51e1e 2549 }
AzureIoTClient 11:31ebaeb51e1e 2550 else
AzureIoTClient 11:31ebaeb51e1e 2551 {
AzureIoTClient 12:658ca6865de2 2552 /* Codes_SRS_IOTHUB_MQTT_TRANSPORT_07_132: [IoTHubTransport_MQTT_Common_SetOption shall return IOTHUB_CLIENT_INVALID_ARG xio_setoption fails] */
AzureIoTClient 11:31ebaeb51e1e 2553 result = IOTHUB_CLIENT_INVALID_ARG;
AzureIoTClient 11:31ebaeb51e1e 2554 }
AzureIoTClient 11:31ebaeb51e1e 2555 }
AzureIoTClient 11:31ebaeb51e1e 2556 else
AzureIoTClient 11:31ebaeb51e1e 2557 {
AzureIoTClient 11:31ebaeb51e1e 2558 result = IOTHUB_CLIENT_ERROR;
AzureIoTClient 11:31ebaeb51e1e 2559 }
AzureIoTClient 11:31ebaeb51e1e 2560 }
AzureIoTClient 11:31ebaeb51e1e 2561 }
AzureIoTClient 11:31ebaeb51e1e 2562 return result;
AzureIoTClient 11:31ebaeb51e1e 2563 }
AzureIoTClient 11:31ebaeb51e1e 2564
AzureIoTClient 11:31ebaeb51e1e 2565 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 2566 {
AzureIoTClient 11:31ebaeb51e1e 2567 IOTHUB_DEVICE_HANDLE result = NULL;
AzureIoTClient 11:31ebaeb51e1e 2568 (void)iotHubClientHandle;
AzureIoTClient 11:31ebaeb51e1e 2569
AzureIoTClient 12:658ca6865de2 2570 // Codes_SRS_IOTHUB_MQTT_TRANSPORT_17_001: [ IoTHubTransport_MQTT_Common_Register shall return NULL if the TRANSPORT_LL_HANDLE is NULL.]
AzureIoTClient 12:658ca6865de2 2571 // Codes_SRS_IOTHUB_MQTT_TRANSPORT_17_002: [ IoTHubTransport_MQTT_Common_Register shall return NULL if device or waitingToSend are NULL.]
AzureIoTClient 11:31ebaeb51e1e 2572 if ((handle == NULL) || (device == NULL) || (waitingToSend == NULL))
AzureIoTClient 11:31ebaeb51e1e 2573 {
AzureIoTClient 12:658ca6865de2 2574 LogError("IoTHubTransport_MQTT_Common_Register: handle, device or waitingToSend is NULL.");
AzureIoTClient 11:31ebaeb51e1e 2575 result = NULL;
AzureIoTClient 11:31ebaeb51e1e 2576 }
AzureIoTClient 11:31ebaeb51e1e 2577 else
AzureIoTClient 11:31ebaeb51e1e 2578 {
AzureIoTClient 11:31ebaeb51e1e 2579 MQTTTRANSPORT_HANDLE_DATA* transport_data = (MQTTTRANSPORT_HANDLE_DATA*)handle;
AzureIoTClient 11:31ebaeb51e1e 2580
AzureIoTClient 12:658ca6865de2 2581 // 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 2582 if (device->deviceId == NULL)
AzureIoTClient 11:31ebaeb51e1e 2583 {
AzureIoTClient 12:658ca6865de2 2584 LogError("IoTHubTransport_MQTT_Common_Register: deviceId is NULL.");
AzureIoTClient 11:31ebaeb51e1e 2585 result = NULL;
AzureIoTClient 11:31ebaeb51e1e 2586 }
AzureIoTClient 12:658ca6865de2 2587 // Codes_SRS_IOTHUB_MQTT_TRANSPORT_03_002: [ IoTHubTransport_MQTT_Common_Register shall return NULL if both deviceKey and deviceSasToken are provided.]
AzureIoTClient 11:31ebaeb51e1e 2588 else if ((device->deviceKey != NULL) && (device->deviceSasToken != NULL))
AzureIoTClient 11:31ebaeb51e1e 2589 {
AzureIoTClient 12:658ca6865de2 2590 LogError("IoTHubTransport_MQTT_Common_Register: Both deviceKey and deviceSasToken are defined. Only one can be used.");
AzureIoTClient 11:31ebaeb51e1e 2591 result = NULL;
AzureIoTClient 11:31ebaeb51e1e 2592 }
AzureIoTClient 11:31ebaeb51e1e 2593 else
AzureIoTClient 11:31ebaeb51e1e 2594 {
AzureIoTClient 12:658ca6865de2 2595 // 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 2596 if (strcmp(STRING_c_str(transport_data->device_id), device->deviceId) != 0)
AzureIoTClient 11:31ebaeb51e1e 2597 {
AzureIoTClient 12:658ca6865de2 2598 LogError("IoTHubTransport_MQTT_Common_Register: deviceId does not match.");
AzureIoTClient 11:31ebaeb51e1e 2599 result = NULL;
AzureIoTClient 11:31ebaeb51e1e 2600 }
AzureIoTClient 21:167f8fcf6fb1 2601 else if (IoTHubClient_Auth_Get_Credential_Type(transport_data->authorization_module) == IOTHUB_CREDENTIAL_TYPE_DEVICE_KEY &&
AzureIoTClient 21:167f8fcf6fb1 2602 (strcmp(IoTHubClient_Auth_Get_DeviceKey(transport_data->authorization_module), device->deviceKey) != 0))
AzureIoTClient 11:31ebaeb51e1e 2603 {
AzureIoTClient 12:658ca6865de2 2604 LogError("IoTHubTransport_MQTT_Common_Register: deviceKey does not match.");
AzureIoTClient 11:31ebaeb51e1e 2605 result = NULL;
AzureIoTClient 11:31ebaeb51e1e 2606 }
AzureIoTClient 11:31ebaeb51e1e 2607 else
AzureIoTClient 11:31ebaeb51e1e 2608 {
AzureIoTClient 11:31ebaeb51e1e 2609 if (transport_data->isRegistered == true)
AzureIoTClient 11:31ebaeb51e1e 2610 {
AzureIoTClient 11:31ebaeb51e1e 2611 LogError("Transport already has device registered by id: [%s]", device->deviceId);
AzureIoTClient 11:31ebaeb51e1e 2612 result = NULL;
AzureIoTClient 11:31ebaeb51e1e 2613 }
AzureIoTClient 11:31ebaeb51e1e 2614 else
AzureIoTClient 11:31ebaeb51e1e 2615 {
AzureIoTClient 11:31ebaeb51e1e 2616 transport_data->isRegistered = true;
AzureIoTClient 12:658ca6865de2 2617 // 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 2618 result = (IOTHUB_DEVICE_HANDLE)handle;
AzureIoTClient 11:31ebaeb51e1e 2619 }
AzureIoTClient 11:31ebaeb51e1e 2620 }
AzureIoTClient 11:31ebaeb51e1e 2621 }
AzureIoTClient 11:31ebaeb51e1e 2622 }
AzureIoTClient 11:31ebaeb51e1e 2623
AzureIoTClient 11:31ebaeb51e1e 2624 return result;
AzureIoTClient 11:31ebaeb51e1e 2625 }
AzureIoTClient 11:31ebaeb51e1e 2626
AzureIoTClient 11:31ebaeb51e1e 2627 void IoTHubTransport_MQTT_Common_Unregister(IOTHUB_DEVICE_HANDLE deviceHandle)
AzureIoTClient 11:31ebaeb51e1e 2628 {
AzureIoTClient 14:4dc2b011be33 2629 // Codes_SRS_IOTHUB_MQTT_TRANSPORT_17_005: [ If deviceHandle is NULL `IoTHubTransport_MQTT_Common_Unregister` shall do nothing. ]
AzureIoTClient 11:31ebaeb51e1e 2630 if (deviceHandle != NULL)
AzureIoTClient 11:31ebaeb51e1e 2631 {
AzureIoTClient 11:31ebaeb51e1e 2632 MQTTTRANSPORT_HANDLE_DATA* transport_data = (MQTTTRANSPORT_HANDLE_DATA*)deviceHandle;
AzureIoTClient 11:31ebaeb51e1e 2633
AzureIoTClient 11:31ebaeb51e1e 2634 transport_data->isRegistered = false;
AzureIoTClient 11:31ebaeb51e1e 2635 }
AzureIoTClient 11:31ebaeb51e1e 2636 }
AzureIoTClient 11:31ebaeb51e1e 2637
AzureIoTClient 11:31ebaeb51e1e 2638 STRING_HANDLE IoTHubTransport_MQTT_Common_GetHostname(TRANSPORT_LL_HANDLE handle)
AzureIoTClient 11:31ebaeb51e1e 2639 {
AzureIoTClient 11:31ebaeb51e1e 2640 STRING_HANDLE result;
AzureIoTClient 12:658ca6865de2 2641 /*Codes_SRS_IOTHUB_MQTT_TRANSPORT_02_001: [ If handle is NULL then IoTHubTransport_MQTT_Common_GetHostname shall fail and return NULL. ]*/
AzureIoTClient 11:31ebaeb51e1e 2642 if (handle == NULL)
AzureIoTClient 11:31ebaeb51e1e 2643 {
AzureIoTClient 11:31ebaeb51e1e 2644 result = NULL;
AzureIoTClient 11:31ebaeb51e1e 2645 }
AzureIoTClient 22:07fec4d325b6 2646 /*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 2647 else if ((result = STRING_clone(((MQTTTRANSPORT_HANDLE_DATA*)(handle))->hostAddress)) == NULL)
AzureIoTClient 11:31ebaeb51e1e 2648 {
AzureIoTClient 22:07fec4d325b6 2649 LogError("Cannot provide the target host name (STRING_clone failed).");
AzureIoTClient 11:31ebaeb51e1e 2650 }
AzureIoTClient 22:07fec4d325b6 2651
AzureIoTClient 11:31ebaeb51e1e 2652 return result;
AzureIoTClient 11:31ebaeb51e1e 2653 }
AzureIoTClient 19:f87dfe76bc70 2654
AzureIoTClient 19:f87dfe76bc70 2655 IOTHUB_CLIENT_RESULT IoTHubTransport_MQTT_Common_SendMessageDisposition(MESSAGE_CALLBACK_INFO* message_data, IOTHUBMESSAGE_DISPOSITION_RESULT disposition)
AzureIoTClient 19:f87dfe76bc70 2656 {
AzureIoTClient 19:f87dfe76bc70 2657 (void)disposition;
AzureIoTClient 19:f87dfe76bc70 2658
AzureIoTClient 19:f87dfe76bc70 2659 IOTHUB_CLIENT_RESULT result;
AzureIoTClient 19:f87dfe76bc70 2660 if (message_data)
AzureIoTClient 19:f87dfe76bc70 2661 {
AzureIoTClient 19:f87dfe76bc70 2662 if (message_data->messageHandle)
AzureIoTClient 19:f87dfe76bc70 2663 {
AzureIoTClient 19:f87dfe76bc70 2664 IoTHubMessage_Destroy(message_data->messageHandle);
AzureIoTClient 19:f87dfe76bc70 2665 result = IOTHUB_CLIENT_OK;
AzureIoTClient 19:f87dfe76bc70 2666 }
AzureIoTClient 19:f87dfe76bc70 2667 else
AzureIoTClient 19:f87dfe76bc70 2668 {
AzureIoTClient 19:f87dfe76bc70 2669 /*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 2670 LogError("message handle is NULL");
AzureIoTClient 19:f87dfe76bc70 2671 result = IOTHUB_CLIENT_ERROR;
AzureIoTClient 19:f87dfe76bc70 2672 }
AzureIoTClient 19:f87dfe76bc70 2673 free(message_data);
AzureIoTClient 19:f87dfe76bc70 2674 }
AzureIoTClient 19:f87dfe76bc70 2675 else
AzureIoTClient 19:f87dfe76bc70 2676 {
AzureIoTClient 19:f87dfe76bc70 2677 /*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 2678 LogError("message_data is NULL");
AzureIoTClient 19:f87dfe76bc70 2679 result = IOTHUB_CLIENT_ERROR;
AzureIoTClient 19:f87dfe76bc70 2680 }
AzureIoTClient 19:f87dfe76bc70 2681 return result;
AzureIoTClient 19:f87dfe76bc70 2682 }
AzureIoTClient 19:f87dfe76bc70 2683