Microsoft Azure IoTHub client MQTT transport

Dependents:   STM32F746_iothub_client_sample_mqtt FXOS8700CQ_To_Azure_IoT f767zi_mqtt FXOS8700CQ_To_Azure_IoT ... more

Committer:
AzureIoTClient
Date:
Fri Nov 03 13:17:53 2017 -0700
Revision:
31:d6198e67d1eb
Parent:
30:52ff609606c8
Child:
32:103a46ed8822
1.1.27

Who changed what in which revision?

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