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 Jun 26 19:14:02 2018 -0700
Revision:
40:cb03d6a6f46d
Parent:
39:6231984e0179
Child:
41:410450f16a9f
1.2.6

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