Microsoft Azure IoTHub client MQTT transport

Dependents:   STM32F746_iothub_client_sample_mqtt FXOS8700CQ_To_Azure_IoT f767zi_mqtt FXOS8700CQ_To_Azure_IoT ... more

Committer:
AzureIoTClient
Date:
Tue Sep 11 11:12:42 2018 -0700
Revision:
41:410450f16a9f
Parent:
40:cb03d6a6f46d
1.2.9

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