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 Jun 30 10:40:56 2017 -0700
Revision:
24:4096249decf1
Parent:
23:84f4c36da8c1
Child:
25:a35763780a87
1.1.18

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