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:
Tue Jan 24 15:23:25 2017 -0800
Revision:
16:14640ee83e99
Parent:
15:81c95c94c8d7
Child:
17:774695cb8554
1.1.5

Who changed what in which revision?

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