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:
Mon Sep 25 13:37:36 2017 -0700
Revision:
29:923be0c3998a
Parent:
28:0cd355c3294e
Child:
30:52ff609606c8
1.1.24

Who changed what in which revision?

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