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:
Wed Nov 16 21:37:42 2016 -0800
Revision:
12:658ca6865de2
Parent:
11:31ebaeb51e1e
Child:
13:606465879c57
1.0.10

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