Microsoft Azure IoTHub client MQTT transport

Dependents:   STM32F746_iothub_client_sample_mqtt FXOS8700CQ_To_Azure_IoT f767zi_mqtt FXOS8700CQ_To_Azure_IoT ... more

Committer:
AzureIoTClient
Date:
Fri Sep 09 13:37:32 2016 -0700
Revision:
9:50744bd8239e
Parent:
8:418a3b812584
Child:
10:c61c16bb63e6
1.0.10

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Azure.IoT Build 0:5e72a75c31b8 1 // Copyright (c) Microsoft. All rights reserved.
Azure.IoT Build 0:5e72a75c31b8 2 // Licensed under the MIT license. See LICENSE file in the project root for full license information.
Azure.IoT Build 0:5e72a75c31b8 3
Azure.IoT Build 0:5e72a75c31b8 4 #include <stdlib.h>
Azure.IoT Build 0:5e72a75c31b8 5 #ifdef _CRTDBG_MAP_ALLOC
Azure.IoT Build 0:5e72a75c31b8 6 #include <crtdbg.h>
Azure.IoT Build 0:5e72a75c31b8 7 #endif
Azure.IoT Build 1:f2e563755d91 8 #include "azure_c_shared_utility/gballoc.h"
Azure.IoT Build 0:5e72a75c31b8 9
Azure.IoT Build 5:73603e7a6542 10 #include "azure_c_shared_utility/xlogging.h"
Azure.IoT Build 1:f2e563755d91 11 #include "azure_c_shared_utility/strings.h"
Azure.IoT Build 1:f2e563755d91 12 #include "azure_c_shared_utility/doublylinkedlist.h"
Azure.IoT Build 1:f2e563755d91 13 #include "azure_c_shared_utility/crt_abstractions.h"
Azure.IoT Build 0:5e72a75c31b8 14
Azure.IoT Build 0:5e72a75c31b8 15 #include "iothub_client_ll.h"
AzureIoTClient 8:418a3b812584 16 #include "iothub_client_options.h"
Azure.IoT Build 0:5e72a75c31b8 17 #include "iothub_client_private.h"
Azure.IoT Build 0:5e72a75c31b8 18 #include "iothubtransportmqtt.h"
Azure.IoT Build 1:f2e563755d91 19 #include "azure_umqtt_c/mqtt_client.h"
Azure.IoT Build 1:f2e563755d91 20 #include "azure_c_shared_utility/sastoken.h"
Azure.IoT Build 1:f2e563755d91 21 #include "azure_c_shared_utility/tickcounter.h"
Azure.IoT Build 0:5e72a75c31b8 22
Azure.IoT Build 1:f2e563755d91 23 #include "azure_c_shared_utility/tlsio.h"
Azure.IoT Build 1:f2e563755d91 24 #include "azure_c_shared_utility/platform.h"
Azure.IoT Build 0:5e72a75c31b8 25
AzureIoTClient 2:9db4da50abaa 26 #include "azure_c_shared_utility/string_tokenizer.h"
Azure.IoT Build 0:5e72a75c31b8 27 #include "iothub_client_version.h"
Azure.IoT Build 0:5e72a75c31b8 28
Azure.IoT Build 0:5e72a75c31b8 29 #include <stdarg.h>
Azure.IoT Build 0:5e72a75c31b8 30 #include <stdio.h>
Azure.IoT Build 0:5e72a75c31b8 31
AzureIoTClient 6:16875b609849 32 #include <limits.h>
AzureIoTClient 6:16875b609849 33
Azure.IoT Build 0:5e72a75c31b8 34 #define SAS_TOKEN_DEFAULT_LIFETIME 3600
AzureIoTClient 2:9db4da50abaa 35 #define SAS_REFRESH_MULTIPLIER .8
Azure.IoT Build 0:5e72a75c31b8 36 #define EPOCH_TIME_T_VALUE 0
Azure.IoT Build 1:f2e563755d91 37 #define DEFAULT_MQTT_KEEPALIVE 4*60 // 4 min
Azure.IoT Build 0:5e72a75c31b8 38 #define DEFAULT_PORT_NUMBER 8883
Azure.IoT Build 0:5e72a75c31b8 39 #define DEFAULT_TEMP_STRING_LEN 256
Azure.IoT Build 0:5e72a75c31b8 40 #define BUILD_CONFIG_USERNAME 24
Azure.IoT Build 0:5e72a75c31b8 41 #define EVENT_TOPIC_DEFAULT_LEN 27
Azure.IoT Build 0:5e72a75c31b8 42 #define SAS_TOKEN_DEFAULT_LEN 10
Azure.IoT Build 0:5e72a75c31b8 43 #define RESEND_TIMEOUT_VALUE_MIN 1*60
Azure.IoT Build 0:5e72a75c31b8 44 #define MAX_SEND_RECOUNT_LIMIT 2
AzureIoTClient 2:9db4da50abaa 45 #define DEFAULT_CONNECTION_INTERVAL 30
AzureIoTClient 2:9db4da50abaa 46 #define FAILED_CONN_BACKOFF_VALUE 5
AzureIoTClient 2:9db4da50abaa 47
AzureIoTClient 2:9db4da50abaa 48 static const char* DEVICE_MSG_TOPIC = "devices/%s/messages/devicebound/#";
AzureIoTClient 2:9db4da50abaa 49 static const char* DEVICE_DEVICE_TOPIC = "devices/%s/messages/events/";
AzureIoTClient 2:9db4da50abaa 50 static const char* PROPERTY_SEPARATOR = "&";
AzureIoTClient 2:9db4da50abaa 51
AzureIoTClient 2:9db4da50abaa 52 typedef struct SYSTEM_PROPERTY_INFO_TAG
AzureIoTClient 2:9db4da50abaa 53 {
AzureIoTClient 4:e472f5ce3473 54 const char* propName;
AzureIoTClient 4:e472f5ce3473 55 size_t propLength;
AzureIoTClient 2:9db4da50abaa 56 } SYSTEM_PROPERTY_INFO;
AzureIoTClient 2:9db4da50abaa 57
AzureIoTClient 2:9db4da50abaa 58 static SYSTEM_PROPERTY_INFO sysPropList[] = {
AzureIoTClient 4:e472f5ce3473 59 { "%24.exp", 7 },
AzureIoTClient 4:e472f5ce3473 60 { "%24.mid", 7 },
AzureIoTClient 4:e472f5ce3473 61 { "%24.uid", 7 },
AzureIoTClient 4:e472f5ce3473 62 { "%24.to", 6 },
AzureIoTClient 4:e472f5ce3473 63 { "%24.cid", 7 },
AzureIoTClient 4:e472f5ce3473 64 { "devices/", 8 },
AzureIoTClient 4:e472f5ce3473 65 { "iothub-operation", 16 },
AzureIoTClient 4:e472f5ce3473 66 { "iothub-ack", 10 }
AzureIoTClient 2:9db4da50abaa 67 };
Azure.IoT Build 0:5e72a75c31b8 68
AzureIoTClient 4:e472f5ce3473 69 static TICK_COUNTER_HANDLE g_msgTickCounter;
Azure.IoT Build 0:5e72a75c31b8 70
AzureIoTClient 7:7fdd306e6224 71 typedef enum MQTT_TRANSPORT_CREDENTIAL_TYPE_TAG
AzureIoTClient 7:7fdd306e6224 72 {
AzureIoTClient 7:7fdd306e6224 73 CREDENTIAL_NOT_BUILD,
AzureIoTClient 7:7fdd306e6224 74 X509,
AzureIoTClient 7:7fdd306e6224 75 SAS_TOKEN_FROM_USER,
AzureIoTClient 7:7fdd306e6224 76 DEVICE_KEY,
AzureIoTClient 7:7fdd306e6224 77 } MQTT_TRANSPORT_CREDENTIAL_TYPE;
AzureIoTClient 7:7fdd306e6224 78
AzureIoTClient 7:7fdd306e6224 79 typedef struct MQTT_TRANSPORT_CREDENTIALS_TAG
AzureIoTClient 7:7fdd306e6224 80 {
AzureIoTClient 7:7fdd306e6224 81 MQTT_TRANSPORT_CREDENTIAL_TYPE credential_type;
AzureIoTClient 7:7fdd306e6224 82 union
AzureIoTClient 7:7fdd306e6224 83 {
AzureIoTClient 7:7fdd306e6224 84 // Key associated to the device to be used.
AzureIoTClient 7:7fdd306e6224 85 STRING_HANDLE deviceKey;
AzureIoTClient 7:7fdd306e6224 86
AzureIoTClient 7:7fdd306e6224 87 // SAS associated to the device to be used.
AzureIoTClient 7:7fdd306e6224 88 STRING_HANDLE deviceSasToken;
AzureIoTClient 7:7fdd306e6224 89
AzureIoTClient 7:7fdd306e6224 90 } CREDENTIAL_VALUE;
AzureIoTClient 7:7fdd306e6224 91 } MQTT_TRANSPORT_CREDENTIALS;
AzureIoTClient 7:7fdd306e6224 92
Azure.IoT Build 0:5e72a75c31b8 93 typedef struct MQTTTRANSPORT_HANDLE_DATA_TAG
Azure.IoT Build 0:5e72a75c31b8 94 {
AzureIoTClient 4:e472f5ce3473 95 STRING_HANDLE device_id;
AzureIoTClient 7:7fdd306e6224 96 STRING_HANDLE devicesPath;
AzureIoTClient 7:7fdd306e6224 97
AzureIoTClient 7:7fdd306e6224 98 MQTT_TRANSPORT_CREDENTIALS transport_creds;
AzureIoTClient 7:7fdd306e6224 99
AzureIoTClient 4:e472f5ce3473 100 STRING_HANDLE mqttEventTopic;
AzureIoTClient 4:e472f5ce3473 101 STRING_HANDLE mqttMessageTopic;
AzureIoTClient 4:e472f5ce3473 102 STRING_HANDLE hostAddress;
AzureIoTClient 4:e472f5ce3473 103 // 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 4:e472f5ce3473 104 // passed through the username portion of the connection frame.
AzureIoTClient 4:e472f5ce3473 105 // The second segment will contain the device id. The two segments are delemited by a "/".
AzureIoTClient 4:e472f5ce3473 106 // The first segment can be a maximum 256 characters.
AzureIoTClient 4:e472f5ce3473 107 // The second segment can be a maximum 128 characters.
AzureIoTClient 4:e472f5ce3473 108 // With the / delimeter you have 384 chars (Plus a terminator of 0).
AzureIoTClient 4:e472f5ce3473 109 STRING_HANDLE configPassedThroughUsername;
AzureIoTClient 4:e472f5ce3473 110 int portNum;
AzureIoTClient 4:e472f5ce3473 111 MQTT_CLIENT_HANDLE mqttClient;
AzureIoTClient 4:e472f5ce3473 112 uint16_t packetId;
AzureIoTClient 4:e472f5ce3473 113 bool isRegistered;
AzureIoTClient 4:e472f5ce3473 114 bool connected;
AzureIoTClient 4:e472f5ce3473 115 bool subscribed;
AzureIoTClient 4:e472f5ce3473 116 bool receiveMessages;
AzureIoTClient 4:e472f5ce3473 117 bool destroyCalled;
AzureIoTClient 4:e472f5ce3473 118 DLIST_ENTRY waitingForAck;
AzureIoTClient 4:e472f5ce3473 119 PDLIST_ENTRY waitingToSend;
AzureIoTClient 4:e472f5ce3473 120 IOTHUB_CLIENT_LL_HANDLE llClientHandle;
AzureIoTClient 4:e472f5ce3473 121 CONTROL_PACKET_TYPE currPacketState;
AzureIoTClient 4:e472f5ce3473 122 XIO_HANDLE xioTransport;
AzureIoTClient 6:16875b609849 123 uint16_t keepAliveValue;
AzureIoTClient 4:e472f5ce3473 124 uint64_t mqtt_connect_time;
AzureIoTClient 4:e472f5ce3473 125 size_t connectFailCount;
AzureIoTClient 4:e472f5ce3473 126 uint64_t connectTick;
Azure.IoT Build 0:5e72a75c31b8 127 } MQTTTRANSPORT_HANDLE_DATA, *PMQTTTRANSPORT_HANDLE_DATA;
Azure.IoT Build 0:5e72a75c31b8 128
Azure.IoT Build 0:5e72a75c31b8 129 typedef struct MQTT_MESSAGE_DETAILS_LIST_TAG
Azure.IoT Build 0:5e72a75c31b8 130 {
AzureIoTClient 4:e472f5ce3473 131 uint64_t msgPublishTime;
AzureIoTClient 4:e472f5ce3473 132 size_t retryCount;
AzureIoTClient 4:e472f5ce3473 133 IOTHUB_MESSAGE_LIST* iotHubMessageEntry;
AzureIoTClient 4:e472f5ce3473 134 void* context;
AzureIoTClient 4:e472f5ce3473 135 uint16_t msgPacketId;
AzureIoTClient 4:e472f5ce3473 136 DLIST_ENTRY entry;
Azure.IoT Build 0:5e72a75c31b8 137 } MQTT_MESSAGE_DETAILS_LIST, *PMQTT_MESSAGE_DETAILS_LIST;
Azure.IoT Build 0:5e72a75c31b8 138
AzureIoTClient 6:16875b609849 139 static uint16_t get_next_packet_id(PMQTTTRANSPORT_HANDLE_DATA transportState)
AzureIoTClient 6:16875b609849 140 {
AzureIoTClient 6:16875b609849 141 if (transportState->packetId+1 >= USHRT_MAX)
AzureIoTClient 6:16875b609849 142 {
AzureIoTClient 6:16875b609849 143 transportState->packetId = 1;
AzureIoTClient 6:16875b609849 144 }
AzureIoTClient 6:16875b609849 145 else
AzureIoTClient 6:16875b609849 146 {
AzureIoTClient 6:16875b609849 147 transportState->packetId++;
AzureIoTClient 6:16875b609849 148 }
AzureIoTClient 6:16875b609849 149 return transportState->packetId;
AzureIoTClient 6:16875b609849 150 }
AzureIoTClient 6:16875b609849 151
Azure.IoT Build 5:73603e7a6542 152 static void sendMsgComplete(IOTHUB_MESSAGE_LIST* iothubMsgList, PMQTTTRANSPORT_HANDLE_DATA transportState, IOTHUB_CLIENT_CONFIRMATION_RESULT confirmResult)
Azure.IoT Build 0:5e72a75c31b8 153 {
AzureIoTClient 4:e472f5ce3473 154 DLIST_ENTRY messageCompleted;
AzureIoTClient 4:e472f5ce3473 155 DList_InitializeListHead(&messageCompleted);
AzureIoTClient 4:e472f5ce3473 156 DList_InsertTailList(&messageCompleted, &(iothubMsgList->entry));
Azure.IoT Build 5:73603e7a6542 157 IoTHubClient_LL_SendComplete(transportState->llClientHandle, &messageCompleted, confirmResult);
Azure.IoT Build 0:5e72a75c31b8 158 }
Azure.IoT Build 0:5e72a75c31b8 159
AzureIoTClient 2:9db4da50abaa 160 static STRING_HANDLE addPropertiesTouMqttMessage(IOTHUB_MESSAGE_HANDLE iothub_message_handle, const char* eventTopic)
AzureIoTClient 2:9db4da50abaa 161 {
AzureIoTClient 4:e472f5ce3473 162 STRING_HANDLE result = STRING_construct(eventTopic);
AzureIoTClient 4:e472f5ce3473 163 const char* const* propertyKeys;
AzureIoTClient 4:e472f5ce3473 164 const char* const* propertyValues;
AzureIoTClient 4:e472f5ce3473 165 size_t propertyCount;
AzureIoTClient 2:9db4da50abaa 166
AzureIoTClient 4:e472f5ce3473 167 // Construct Properties
AzureIoTClient 4:e472f5ce3473 168 MAP_HANDLE properties_map = IoTHubMessage_Properties(iothub_message_handle);
AzureIoTClient 4:e472f5ce3473 169 if (properties_map != NULL)
AzureIoTClient 4:e472f5ce3473 170 {
AzureIoTClient 4:e472f5ce3473 171 if (Map_GetInternals(properties_map, &propertyKeys, &propertyValues, &propertyCount) != MAP_OK)
AzureIoTClient 4:e472f5ce3473 172 {
AzureIoTClient 4:e472f5ce3473 173 LogError("Failed to get the internals of the property map.");
AzureIoTClient 4:e472f5ce3473 174 STRING_delete(result);
AzureIoTClient 4:e472f5ce3473 175 result = NULL;
AzureIoTClient 4:e472f5ce3473 176 }
AzureIoTClient 4:e472f5ce3473 177 else
AzureIoTClient 4:e472f5ce3473 178 {
AzureIoTClient 4:e472f5ce3473 179 if (propertyCount != 0)
AzureIoTClient 4:e472f5ce3473 180 {
AzureIoTClient 4:e472f5ce3473 181 for (size_t index = 0; index < propertyCount && result != NULL; index++)
AzureIoTClient 4:e472f5ce3473 182 {
AzureIoTClient 4:e472f5ce3473 183 size_t len = strlen(propertyKeys[index]) + strlen(propertyValues[index]) + 2;
AzureIoTClient 4:e472f5ce3473 184 char* propValues = malloc(len+1);
AzureIoTClient 4:e472f5ce3473 185 if (propValues == NULL)
AzureIoTClient 4:e472f5ce3473 186 {
AzureIoTClient 4:e472f5ce3473 187 STRING_delete(result);
AzureIoTClient 4:e472f5ce3473 188 result = NULL;
AzureIoTClient 4:e472f5ce3473 189 }
AzureIoTClient 4:e472f5ce3473 190 else
AzureIoTClient 4:e472f5ce3473 191 {
AzureIoTClient 4:e472f5ce3473 192 sprintf(propValues, "%s=%s%s", propertyKeys[index], propertyValues[index], propertyCount - 1 == index ? "" : "&");
AzureIoTClient 4:e472f5ce3473 193 if (STRING_concat(result, propValues) != 0)
AzureIoTClient 4:e472f5ce3473 194 {
AzureIoTClient 4:e472f5ce3473 195 STRING_delete(result);
AzureIoTClient 4:e472f5ce3473 196 result = NULL;
AzureIoTClient 4:e472f5ce3473 197 }
AzureIoTClient 4:e472f5ce3473 198 free(propValues);
AzureIoTClient 4:e472f5ce3473 199 }
AzureIoTClient 4:e472f5ce3473 200 }
AzureIoTClient 4:e472f5ce3473 201 }
AzureIoTClient 4:e472f5ce3473 202 }
AzureIoTClient 4:e472f5ce3473 203 }
AzureIoTClient 4:e472f5ce3473 204 return result;
AzureIoTClient 2:9db4da50abaa 205 }
AzureIoTClient 2:9db4da50abaa 206
Azure.IoT Build 0:5e72a75c31b8 207 static int publishMqttMessage(PMQTTTRANSPORT_HANDLE_DATA transportState, MQTT_MESSAGE_DETAILS_LIST* mqttMsgEntry, const unsigned char* payload, size_t len)
Azure.IoT Build 0:5e72a75c31b8 208 {
AzureIoTClient 4:e472f5ce3473 209 int result;
AzureIoTClient 6:16875b609849 210 mqttMsgEntry->msgPacketId = get_next_packet_id(transportState);
AzureIoTClient 4:e472f5ce3473 211 STRING_HANDLE msgTopic = addPropertiesTouMqttMessage(mqttMsgEntry->iotHubMessageEntry->messageHandle, STRING_c_str(transportState->mqttEventTopic));
AzureIoTClient 4:e472f5ce3473 212 if (msgTopic == NULL)
AzureIoTClient 4:e472f5ce3473 213 {
AzureIoTClient 4:e472f5ce3473 214 result = __LINE__;
AzureIoTClient 4:e472f5ce3473 215 }
AzureIoTClient 4:e472f5ce3473 216 else
AzureIoTClient 4:e472f5ce3473 217 {
AzureIoTClient 6:16875b609849 218 MQTT_MESSAGE_HANDLE mqttMsg = mqttmessage_create(mqttMsgEntry->msgPacketId, STRING_c_str(msgTopic), DELIVER_AT_LEAST_ONCE, payload, len);
AzureIoTClient 4:e472f5ce3473 219 if (mqttMsg == NULL)
AzureIoTClient 4:e472f5ce3473 220 {
AzureIoTClient 4:e472f5ce3473 221 result = __LINE__;
AzureIoTClient 4:e472f5ce3473 222 }
AzureIoTClient 4:e472f5ce3473 223 else
AzureIoTClient 4:e472f5ce3473 224 {
AzureIoTClient 4:e472f5ce3473 225 if (mqtt_client_publish(transportState->mqttClient, mqttMsg) != 0)
AzureIoTClient 4:e472f5ce3473 226 {
AzureIoTClient 4:e472f5ce3473 227 result = __LINE__;
AzureIoTClient 4:e472f5ce3473 228 }
AzureIoTClient 4:e472f5ce3473 229 else
AzureIoTClient 4:e472f5ce3473 230 {
AzureIoTClient 4:e472f5ce3473 231 mqttMsgEntry->retryCount++;
AzureIoTClient 4:e472f5ce3473 232 (void)tickcounter_get_current_ms(g_msgTickCounter, &mqttMsgEntry->msgPublishTime);
AzureIoTClient 4:e472f5ce3473 233 result = 0;
AzureIoTClient 4:e472f5ce3473 234 }
AzureIoTClient 4:e472f5ce3473 235 mqttmessage_destroy(mqttMsg);
AzureIoTClient 4:e472f5ce3473 236 }
AzureIoTClient 4:e472f5ce3473 237 STRING_delete(msgTopic);
AzureIoTClient 4:e472f5ce3473 238 }
AzureIoTClient 4:e472f5ce3473 239 return result;
AzureIoTClient 3:40f482ed0be8 240 }
AzureIoTClient 3:40f482ed0be8 241
AzureIoTClient 3:40f482ed0be8 242 static bool isSystemProperty(const char* tokenData)
AzureIoTClient 3:40f482ed0be8 243 {
AzureIoTClient 4:e472f5ce3473 244 bool result = false;
AzureIoTClient 4:e472f5ce3473 245 size_t propCount = sizeof(sysPropList)/sizeof(sysPropList[0]);
AzureIoTClient 4:e472f5ce3473 246 for (size_t index = 0; index < propCount; index++)
AzureIoTClient 4:e472f5ce3473 247 {
AzureIoTClient 4:e472f5ce3473 248 if (memcmp(tokenData, sysPropList[index].propName, sysPropList[index].propLength) == 0)
AzureIoTClient 4:e472f5ce3473 249 {
AzureIoTClient 4:e472f5ce3473 250 result = true;
AzureIoTClient 4:e472f5ce3473 251 break;
AzureIoTClient 4:e472f5ce3473 252 }
AzureIoTClient 4:e472f5ce3473 253 }
AzureIoTClient 4:e472f5ce3473 254 return result;
AzureIoTClient 3:40f482ed0be8 255 }
AzureIoTClient 3:40f482ed0be8 256
AzureIoTClient 3:40f482ed0be8 257 static int extractMqttProperties(IOTHUB_MESSAGE_HANDLE IoTHubMessage, MQTT_MESSAGE_HANDLE msgHandle)
AzureIoTClient 3:40f482ed0be8 258 {
AzureIoTClient 4:e472f5ce3473 259 int result;
AzureIoTClient 4:e472f5ce3473 260 STRING_HANDLE mqttTopic = STRING_construct(mqttmessage_getTopicName(msgHandle));
AzureIoTClient 3:40f482ed0be8 261
AzureIoTClient 4:e472f5ce3473 262 STRING_TOKENIZER_HANDLE token = STRING_TOKENIZER_create(mqttTopic);
AzureIoTClient 4:e472f5ce3473 263 if (token != NULL)
AzureIoTClient 4:e472f5ce3473 264 {
AzureIoTClient 4:e472f5ce3473 265 MAP_HANDLE propertyMap = IoTHubMessage_Properties(IoTHubMessage);
AzureIoTClient 4:e472f5ce3473 266 if (propertyMap == NULL)
AzureIoTClient 4:e472f5ce3473 267 {
AzureIoTClient 4:e472f5ce3473 268 LogError("Failure to retrieve IoTHubMessage_properties.");
AzureIoTClient 4:e472f5ce3473 269 result = __LINE__;
AzureIoTClient 4:e472f5ce3473 270 }
AzureIoTClient 4:e472f5ce3473 271 else
AzureIoTClient 4:e472f5ce3473 272 {
AzureIoTClient 4:e472f5ce3473 273 STRING_HANDLE output = STRING_new();
AzureIoTClient 4:e472f5ce3473 274 if (output == NULL)
AzureIoTClient 4:e472f5ce3473 275 {
AzureIoTClient 4:e472f5ce3473 276 LogError("Failure to allocate STRING_new.");
AzureIoTClient 4:e472f5ce3473 277 result = __LINE__;
AzureIoTClient 4:e472f5ce3473 278 }
AzureIoTClient 4:e472f5ce3473 279 else
AzureIoTClient 4:e472f5ce3473 280 {
AzureIoTClient 4:e472f5ce3473 281 result = 0;
AzureIoTClient 4:e472f5ce3473 282 while (STRING_TOKENIZER_get_next_token(token, output, PROPERTY_SEPARATOR) == 0 && result == 0)
AzureIoTClient 4:e472f5ce3473 283 {
AzureIoTClient 4:e472f5ce3473 284 const char* tokenData = STRING_c_str(output);
AzureIoTClient 4:e472f5ce3473 285 size_t tokenLen = strlen(tokenData);
AzureIoTClient 4:e472f5ce3473 286 if (tokenData == NULL || tokenLen == 0)
AzureIoTClient 4:e472f5ce3473 287 {
AzureIoTClient 4:e472f5ce3473 288 break;
AzureIoTClient 4:e472f5ce3473 289 }
AzureIoTClient 4:e472f5ce3473 290 else
AzureIoTClient 4:e472f5ce3473 291 {
AzureIoTClient 4:e472f5ce3473 292 if (!isSystemProperty(tokenData) )
AzureIoTClient 4:e472f5ce3473 293 {
AzureIoTClient 4:e472f5ce3473 294 const char* iterator = tokenData;
AzureIoTClient 4:e472f5ce3473 295 while (iterator != NULL && *iterator != '\0' && result == 0)
AzureIoTClient 4:e472f5ce3473 296 {
AzureIoTClient 4:e472f5ce3473 297 if (*iterator == '=')
AzureIoTClient 4:e472f5ce3473 298 {
AzureIoTClient 4:e472f5ce3473 299 size_t nameLen = iterator - tokenData;
AzureIoTClient 4:e472f5ce3473 300 char* propName = malloc(nameLen + 1);
AzureIoTClient 3:40f482ed0be8 301
AzureIoTClient 4:e472f5ce3473 302 size_t valLen = tokenLen - (nameLen + 1) + 1;
AzureIoTClient 4:e472f5ce3473 303 char* propValue = malloc(valLen + 1);
AzureIoTClient 3:40f482ed0be8 304
AzureIoTClient 4:e472f5ce3473 305 if (propName == NULL || propValue == NULL)
AzureIoTClient 4:e472f5ce3473 306 {
AzureIoTClient 4:e472f5ce3473 307 result = __LINE__;
AzureIoTClient 4:e472f5ce3473 308 }
AzureIoTClient 4:e472f5ce3473 309 else
AzureIoTClient 4:e472f5ce3473 310 {
AzureIoTClient 4:e472f5ce3473 311 strncpy(propName, tokenData, nameLen);
AzureIoTClient 4:e472f5ce3473 312 propName[nameLen] = '\0';
AzureIoTClient 3:40f482ed0be8 313
AzureIoTClient 4:e472f5ce3473 314 strncpy(propValue, iterator + 1, valLen);
AzureIoTClient 4:e472f5ce3473 315 propValue[valLen] = '\0';
AzureIoTClient 3:40f482ed0be8 316
AzureIoTClient 4:e472f5ce3473 317 if (Map_AddOrUpdate(propertyMap, propName, propValue) != MAP_OK)
AzureIoTClient 4:e472f5ce3473 318 {
AzureIoTClient 4:e472f5ce3473 319 LogError("Map_AddOrUpdate failed.");
AzureIoTClient 4:e472f5ce3473 320 result = __LINE__;
AzureIoTClient 4:e472f5ce3473 321 }
AzureIoTClient 4:e472f5ce3473 322 }
AzureIoTClient 4:e472f5ce3473 323 free(propName);
AzureIoTClient 4:e472f5ce3473 324 free(propValue);
AzureIoTClient 3:40f482ed0be8 325
AzureIoTClient 4:e472f5ce3473 326 break;
AzureIoTClient 4:e472f5ce3473 327 }
AzureIoTClient 4:e472f5ce3473 328 iterator++;
AzureIoTClient 4:e472f5ce3473 329 }
AzureIoTClient 4:e472f5ce3473 330 }
AzureIoTClient 4:e472f5ce3473 331 }
AzureIoTClient 4:e472f5ce3473 332 }
AzureIoTClient 4:e472f5ce3473 333 STRING_delete(output);
AzureIoTClient 4:e472f5ce3473 334 }
AzureIoTClient 4:e472f5ce3473 335 }
AzureIoTClient 4:e472f5ce3473 336 STRING_TOKENIZER_destroy(token);
AzureIoTClient 4:e472f5ce3473 337 }
AzureIoTClient 4:e472f5ce3473 338 else
AzureIoTClient 4:e472f5ce3473 339 {
AzureIoTClient 4:e472f5ce3473 340 LogError("Unable to create Tokenizer object.");
AzureIoTClient 4:e472f5ce3473 341 result = __LINE__;
AzureIoTClient 4:e472f5ce3473 342 }
AzureIoTClient 4:e472f5ce3473 343 STRING_delete(mqttTopic);
AzureIoTClient 3:40f482ed0be8 344
AzureIoTClient 4:e472f5ce3473 345 return result;
AzureIoTClient 3:40f482ed0be8 346 }
AzureIoTClient 3:40f482ed0be8 347
AzureIoTClient 3:40f482ed0be8 348 static void MqttRecvCallback(MQTT_MESSAGE_HANDLE msgHandle, void* callbackCtx)
AzureIoTClient 3:40f482ed0be8 349 {
AzureIoTClient 4:e472f5ce3473 350 if (msgHandle != NULL && callbackCtx != NULL)
AzureIoTClient 4:e472f5ce3473 351 {
AzureIoTClient 4:e472f5ce3473 352 const APP_PAYLOAD* appPayload = mqttmessage_getApplicationMsg(msgHandle);
AzureIoTClient 4:e472f5ce3473 353 IOTHUB_MESSAGE_HANDLE IoTHubMessage = IoTHubMessage_CreateFromByteArray(appPayload->message, appPayload->length);
AzureIoTClient 4:e472f5ce3473 354 if (IoTHubMessage == NULL)
AzureIoTClient 4:e472f5ce3473 355 {
AzureIoTClient 4:e472f5ce3473 356 LogError("IotHub Message creation has failed.");
AzureIoTClient 4:e472f5ce3473 357 }
AzureIoTClient 4:e472f5ce3473 358 else
AzureIoTClient 4:e472f5ce3473 359 {
AzureIoTClient 4:e472f5ce3473 360 // Will need to update this when the service has messages that can be rejected
AzureIoTClient 4:e472f5ce3473 361 (void)extractMqttProperties(IoTHubMessage, msgHandle);
AzureIoTClient 4:e472f5ce3473 362 PMQTTTRANSPORT_HANDLE_DATA transportData = (PMQTTTRANSPORT_HANDLE_DATA)callbackCtx;
AzureIoTClient 4:e472f5ce3473 363 if (IoTHubClient_LL_MessageCallback(transportData->llClientHandle, IoTHubMessage) != IOTHUBMESSAGE_ACCEPTED)
AzureIoTClient 4:e472f5ce3473 364 {
AzureIoTClient 4:e472f5ce3473 365 LogError("Event not accepted by our client.");
AzureIoTClient 4:e472f5ce3473 366 }
AzureIoTClient 4:e472f5ce3473 367 IoTHubMessage_Destroy(IoTHubMessage);
AzureIoTClient 4:e472f5ce3473 368 }
AzureIoTClient 4:e472f5ce3473 369 }
AzureIoTClient 3:40f482ed0be8 370 }
AzureIoTClient 3:40f482ed0be8 371
AzureIoTClient 3:40f482ed0be8 372 static void MqttOpCompleteCallback(MQTT_CLIENT_HANDLE handle, MQTT_CLIENT_EVENT_RESULT actionResult, const void* msgInfo, void* callbackCtx)
AzureIoTClient 3:40f482ed0be8 373 {
AzureIoTClient 4:e472f5ce3473 374 (void)handle;
AzureIoTClient 4:e472f5ce3473 375 if (callbackCtx != NULL)
AzureIoTClient 4:e472f5ce3473 376 {
AzureIoTClient 4:e472f5ce3473 377 PMQTTTRANSPORT_HANDLE_DATA transportData = (PMQTTTRANSPORT_HANDLE_DATA)callbackCtx;
AzureIoTClient 3:40f482ed0be8 378
AzureIoTClient 4:e472f5ce3473 379 switch (actionResult)
AzureIoTClient 4:e472f5ce3473 380 {
AzureIoTClient 4:e472f5ce3473 381 case MQTT_CLIENT_ON_PUBLISH_ACK:
AzureIoTClient 4:e472f5ce3473 382 case MQTT_CLIENT_ON_PUBLISH_COMP:
AzureIoTClient 4:e472f5ce3473 383 {
AzureIoTClient 4:e472f5ce3473 384 const PUBLISH_ACK* puback = (const PUBLISH_ACK*)msgInfo;
AzureIoTClient 4:e472f5ce3473 385 if (puback != NULL)
AzureIoTClient 4:e472f5ce3473 386 {
AzureIoTClient 4:e472f5ce3473 387 PDLIST_ENTRY currentListEntry = transportData->waitingForAck.Flink;
AzureIoTClient 4:e472f5ce3473 388 while (currentListEntry != &transportData->waitingForAck)
AzureIoTClient 4:e472f5ce3473 389 {
AzureIoTClient 4:e472f5ce3473 390 MQTT_MESSAGE_DETAILS_LIST* mqttMsgEntry = containingRecord(currentListEntry, MQTT_MESSAGE_DETAILS_LIST, entry);
AzureIoTClient 4:e472f5ce3473 391 DLIST_ENTRY saveListEntry;
AzureIoTClient 4:e472f5ce3473 392 saveListEntry.Flink = currentListEntry->Flink;
AzureIoTClient 3:40f482ed0be8 393
AzureIoTClient 4:e472f5ce3473 394 if (puback->packetId == mqttMsgEntry->msgPacketId)
AzureIoTClient 4:e472f5ce3473 395 {
AzureIoTClient 4:e472f5ce3473 396 (void)DList_RemoveEntryList(currentListEntry); //First remove the item from Waiting for Ack List.
Azure.IoT Build 5:73603e7a6542 397 sendMsgComplete(mqttMsgEntry->iotHubMessageEntry, transportData, IOTHUB_CLIENT_CONFIRMATION_OK);
AzureIoTClient 4:e472f5ce3473 398 free(mqttMsgEntry);
AzureIoTClient 4:e472f5ce3473 399 }
AzureIoTClient 4:e472f5ce3473 400 currentListEntry = saveListEntry.Flink;
AzureIoTClient 4:e472f5ce3473 401 }
AzureIoTClient 4:e472f5ce3473 402 }
AzureIoTClient 4:e472f5ce3473 403 break;
AzureIoTClient 4:e472f5ce3473 404 }
AzureIoTClient 4:e472f5ce3473 405 case MQTT_CLIENT_ON_CONNACK:
AzureIoTClient 4:e472f5ce3473 406 {
AzureIoTClient 4:e472f5ce3473 407 const CONNECT_ACK* connack = (const CONNECT_ACK*)msgInfo;
AzureIoTClient 4:e472f5ce3473 408 if (connack != NULL)
AzureIoTClient 4:e472f5ce3473 409 {
AzureIoTClient 4:e472f5ce3473 410 if (connack->returnCode == CONNECTION_ACCEPTED)
AzureIoTClient 4:e472f5ce3473 411 {
AzureIoTClient 4:e472f5ce3473 412 // The connect packet has been acked
AzureIoTClient 4:e472f5ce3473 413 transportData->currPacketState = CONNACK_TYPE;
AzureIoTClient 4:e472f5ce3473 414 }
AzureIoTClient 4:e472f5ce3473 415 else
AzureIoTClient 4:e472f5ce3473 416 {
AzureIoTClient 4:e472f5ce3473 417 LogError("Connection not accepted, return code: %d.", connack->returnCode);
AzureIoTClient 4:e472f5ce3473 418 (void)mqtt_client_disconnect(transportData->mqttClient);
AzureIoTClient 4:e472f5ce3473 419 transportData->connected = false;
AzureIoTClient 4:e472f5ce3473 420 transportData->currPacketState = PACKET_TYPE_ERROR;
AzureIoTClient 4:e472f5ce3473 421 }
AzureIoTClient 4:e472f5ce3473 422 }
AzureIoTClient 4:e472f5ce3473 423 else
AzureIoTClient 4:e472f5ce3473 424 {
AzureIoTClient 4:e472f5ce3473 425 LogError("MQTT_CLIENT_ON_CONNACK CONNACK parameter is NULL.");
AzureIoTClient 4:e472f5ce3473 426 }
AzureIoTClient 4:e472f5ce3473 427 break;
AzureIoTClient 4:e472f5ce3473 428 }
AzureIoTClient 4:e472f5ce3473 429 case MQTT_CLIENT_ON_SUBSCRIBE_ACK:
AzureIoTClient 4:e472f5ce3473 430 {
AzureIoTClient 4:e472f5ce3473 431 const SUBSCRIBE_ACK* suback = (const SUBSCRIBE_ACK*)msgInfo;
AzureIoTClient 4:e472f5ce3473 432 if (suback != NULL)
AzureIoTClient 4:e472f5ce3473 433 {
AzureIoTClient 4:e472f5ce3473 434 if (suback->qosCount == 1)
AzureIoTClient 4:e472f5ce3473 435 {
AzureIoTClient 4:e472f5ce3473 436 // The connect packet has been acked
AzureIoTClient 4:e472f5ce3473 437 transportData->currPacketState = SUBACK_TYPE;
AzureIoTClient 4:e472f5ce3473 438 }
AzureIoTClient 4:e472f5ce3473 439 else
AzureIoTClient 4:e472f5ce3473 440 {
AzureIoTClient 4:e472f5ce3473 441 LogError("QOS count was not expected: %d.", (int)suback->qosCount);
AzureIoTClient 4:e472f5ce3473 442 }
AzureIoTClient 4:e472f5ce3473 443 }
AzureIoTClient 4:e472f5ce3473 444 break;
AzureIoTClient 4:e472f5ce3473 445 }
AzureIoTClient 4:e472f5ce3473 446 case MQTT_CLIENT_ON_PUBLISH_RECV:
AzureIoTClient 4:e472f5ce3473 447 case MQTT_CLIENT_ON_PUBLISH_REL:
AzureIoTClient 4:e472f5ce3473 448 {
AzureIoTClient 4:e472f5ce3473 449 // Currently not used
AzureIoTClient 4:e472f5ce3473 450 break;
AzureIoTClient 4:e472f5ce3473 451 }
AzureIoTClient 4:e472f5ce3473 452 case MQTT_CLIENT_ON_DISCONNECT:
AzureIoTClient 4:e472f5ce3473 453 {
AzureIoTClient 4:e472f5ce3473 454 // Close the client so we can reconnect again
AzureIoTClient 4:e472f5ce3473 455 transportData->connected = false;
AzureIoTClient 4:e472f5ce3473 456 transportData->currPacketState = DISCONNECT_TYPE;
AzureIoTClient 4:e472f5ce3473 457 break;
AzureIoTClient 4:e472f5ce3473 458 }
AzureIoTClient 4:e472f5ce3473 459 case MQTT_CLIENT_NO_PING_RESPONSE:
AzureIoTClient 4:e472f5ce3473 460 LogError("Mqtt Ping Response was not encountered. Reconnecting device...");
AzureIoTClient 4:e472f5ce3473 461 case MQTT_CLIENT_ON_ERROR:
AzureIoTClient 4:e472f5ce3473 462 {
AzureIoTClient 4:e472f5ce3473 463 xio_close(transportData->xioTransport, NULL, NULL);
AzureIoTClient 4:e472f5ce3473 464 transportData->connected = false;
AzureIoTClient 4:e472f5ce3473 465 transportData->subscribed = false;
AzureIoTClient 4:e472f5ce3473 466 transportData->currPacketState = PACKET_TYPE_ERROR;
AzureIoTClient 4:e472f5ce3473 467 }
AzureIoTClient 4:e472f5ce3473 468 }
AzureIoTClient 4:e472f5ce3473 469 }
AzureIoTClient 3:40f482ed0be8 470 }
AzureIoTClient 3:40f482ed0be8 471
AzureIoTClient 3:40f482ed0be8 472 const XIO_HANDLE getIoTransportProvider(const char* fqdn, int port)
AzureIoTClient 3:40f482ed0be8 473 {
AzureIoTClient 6:16875b609849 474 TLSIO_CONFIG tls_io_config;
AzureIoTClient 4:e472f5ce3473 475 const IO_INTERFACE_DESCRIPTION* io_interface_description = platform_get_default_tlsio();
AzureIoTClient 6:16875b609849 476 tls_io_config.hostname = fqdn;
AzureIoTClient 6:16875b609849 477 tls_io_config.port = port;
AzureIoTClient 6:16875b609849 478 return xio_create(io_interface_description, &tls_io_config);
AzureIoTClient 3:40f482ed0be8 479 }
AzureIoTClient 3:40f482ed0be8 480
AzureIoTClient 3:40f482ed0be8 481 static int SubscribeToMqttProtocol(PMQTTTRANSPORT_HANDLE_DATA transportState)
AzureIoTClient 3:40f482ed0be8 482 {
AzureIoTClient 4:e472f5ce3473 483 int result;
AzureIoTClient 3:40f482ed0be8 484
AzureIoTClient 4:e472f5ce3473 485 if (transportState->receiveMessages && !transportState->subscribed)
AzureIoTClient 4:e472f5ce3473 486 {
AzureIoTClient 6:16875b609849 487 SUBSCRIBE_PAYLOAD subscribe[1];
AzureIoTClient 6:16875b609849 488
AzureIoTClient 6:16875b609849 489 subscribe[0].subscribeTopic = STRING_c_str(transportState->mqttMessageTopic);
AzureIoTClient 6:16875b609849 490 subscribe[0].qosReturn = DELIVER_AT_LEAST_ONCE;
AzureIoTClient 6:16875b609849 491
AzureIoTClient 4:e472f5ce3473 492 /* Codes_SRS_IOTHUB_MQTT_TRANSPORT_07_016: [IoTHubTransportMqtt_Subscribe shall call mqtt_client_subscribe to subscribe to the Message Topic.] */
AzureIoTClient 6:16875b609849 493 if (mqtt_client_subscribe(transportState->mqttClient, get_next_packet_id(transportState), subscribe, 1) != 0)
AzureIoTClient 4:e472f5ce3473 494 {
AzureIoTClient 4:e472f5ce3473 495 /* Codes_SRS_IOTHUB_MQTT_TRANSPORT_07_017: [Upon failure IoTHubTransportMqtt_Subscribe shall return a non-zero value.] */
AzureIoTClient 4:e472f5ce3473 496 result = __LINE__;
AzureIoTClient 4:e472f5ce3473 497 }
AzureIoTClient 4:e472f5ce3473 498 else
AzureIoTClient 4:e472f5ce3473 499 {
AzureIoTClient 4:e472f5ce3473 500 /* Codes_SRS_IOTHUB_MQTT_TRANSPORT_07_018: [On success IoTHubTransportMqtt_Subscribe shall return 0.] */
AzureIoTClient 4:e472f5ce3473 501 transportState->subscribed = true;
AzureIoTClient 4:e472f5ce3473 502 transportState->currPacketState = SUBSCRIBE_TYPE;
AzureIoTClient 4:e472f5ce3473 503 result = 0;
AzureIoTClient 4:e472f5ce3473 504 }
AzureIoTClient 4:e472f5ce3473 505 }
AzureIoTClient 4:e472f5ce3473 506 else
AzureIoTClient 4:e472f5ce3473 507 {
AzureIoTClient 4:e472f5ce3473 508 if (transportState->receiveMessages)
AzureIoTClient 4:e472f5ce3473 509 {
AzureIoTClient 4:e472f5ce3473 510 transportState->currPacketState = SUBSCRIBE_TYPE;
AzureIoTClient 4:e472f5ce3473 511 }
AzureIoTClient 4:e472f5ce3473 512 else
AzureIoTClient 4:e472f5ce3473 513 {
AzureIoTClient 4:e472f5ce3473 514 transportState->currPacketState = PUBLISH_TYPE;
AzureIoTClient 4:e472f5ce3473 515 }
AzureIoTClient 6:16875b609849 516 result = 0;
AzureIoTClient 4:e472f5ce3473 517 }
AzureIoTClient 4:e472f5ce3473 518 return result;
AzureIoTClient 3:40f482ed0be8 519 }
AzureIoTClient 3:40f482ed0be8 520
AzureIoTClient 3:40f482ed0be8 521 static const unsigned char* RetrieveMessagePayload(IOTHUB_MESSAGE_HANDLE messageHandle, size_t* length)
AzureIoTClient 3:40f482ed0be8 522 {
AzureIoTClient 4:e472f5ce3473 523 const unsigned char* result;
AzureIoTClient 3:40f482ed0be8 524
AzureIoTClient 4:e472f5ce3473 525 IOTHUBMESSAGE_CONTENT_TYPE contentType = IoTHubMessage_GetContentType(messageHandle);
AzureIoTClient 4:e472f5ce3473 526 if (contentType == IOTHUBMESSAGE_BYTEARRAY)
AzureIoTClient 4:e472f5ce3473 527 {
AzureIoTClient 4:e472f5ce3473 528 if (IoTHubMessage_GetByteArray(messageHandle, &result, length) != IOTHUB_MESSAGE_OK)
AzureIoTClient 4:e472f5ce3473 529 {
AzureIoTClient 4:e472f5ce3473 530 LogError("Failure result from IoTHubMessage_GetByteArray");
AzureIoTClient 4:e472f5ce3473 531 result = NULL;
AzureIoTClient 4:e472f5ce3473 532 *length = 0;
AzureIoTClient 4:e472f5ce3473 533 }
AzureIoTClient 4:e472f5ce3473 534 }
AzureIoTClient 4:e472f5ce3473 535 else if (contentType == IOTHUBMESSAGE_STRING)
AzureIoTClient 4:e472f5ce3473 536 {
AzureIoTClient 4:e472f5ce3473 537 result = (const unsigned char*)IoTHubMessage_GetString(messageHandle);
AzureIoTClient 4:e472f5ce3473 538 if (result == NULL)
AzureIoTClient 4:e472f5ce3473 539 {
AzureIoTClient 4:e472f5ce3473 540 LogError("Failure result from IoTHubMessage_GetString");
AzureIoTClient 4:e472f5ce3473 541 result = NULL;
AzureIoTClient 4:e472f5ce3473 542 *length = 0;
AzureIoTClient 4:e472f5ce3473 543 }
AzureIoTClient 4:e472f5ce3473 544 else
AzureIoTClient 4:e472f5ce3473 545 {
AzureIoTClient 4:e472f5ce3473 546 *length = strlen((const char*)result);
AzureIoTClient 4:e472f5ce3473 547 }
AzureIoTClient 4:e472f5ce3473 548 }
AzureIoTClient 4:e472f5ce3473 549 else
AzureIoTClient 4:e472f5ce3473 550 {
AzureIoTClient 4:e472f5ce3473 551 result = NULL;
AzureIoTClient 4:e472f5ce3473 552 *length = 0;
AzureIoTClient 4:e472f5ce3473 553 }
AzureIoTClient 4:e472f5ce3473 554 return result;
AzureIoTClient 3:40f482ed0be8 555 }
AzureIoTClient 3:40f482ed0be8 556
AzureIoTClient 3:40f482ed0be8 557 static STRING_HANDLE ConstructSasToken(const char* iothubName, const char* iotHubSuffix, const char* deviceId)
AzureIoTClient 3:40f482ed0be8 558 {
AzureIoTClient 4:e472f5ce3473 559 STRING_HANDLE result;
AzureIoTClient 4:e472f5ce3473 560 size_t len = strlen(iothubName);
AzureIoTClient 4:e472f5ce3473 561 len += strlen(iotHubSuffix);
AzureIoTClient 4:e472f5ce3473 562 len += strlen(deviceId);
AzureIoTClient 3:40f482ed0be8 563
AzureIoTClient 4:e472f5ce3473 564 char* sasToken = malloc(len + SAS_TOKEN_DEFAULT_LEN + 1);
AzureIoTClient 4:e472f5ce3473 565 if (sasToken == NULL)
AzureIoTClient 4:e472f5ce3473 566 {
AzureIoTClient 4:e472f5ce3473 567 result = NULL;
AzureIoTClient 4:e472f5ce3473 568 }
AzureIoTClient 4:e472f5ce3473 569 else
AzureIoTClient 4:e472f5ce3473 570 {
AzureIoTClient 4:e472f5ce3473 571 (void)sprintf(sasToken, "%s.%s/devices/%s", iothubName, iotHubSuffix, deviceId);
AzureIoTClient 4:e472f5ce3473 572 result = STRING_construct(sasToken);
AzureIoTClient 4:e472f5ce3473 573 free(sasToken);
AzureIoTClient 4:e472f5ce3473 574 }
AzureIoTClient 4:e472f5ce3473 575 return result;
AzureIoTClient 3:40f482ed0be8 576 }
AzureIoTClient 3:40f482ed0be8 577
AzureIoTClient 3:40f482ed0be8 578 static STRING_HANDLE ConstructEventTopic(const char* deviceId)
AzureIoTClient 3:40f482ed0be8 579 {
AzureIoTClient 4:e472f5ce3473 580 STRING_HANDLE result;
AzureIoTClient 4:e472f5ce3473 581 size_t len = strlen(deviceId);
AzureIoTClient 3:40f482ed0be8 582
AzureIoTClient 4:e472f5ce3473 583 char* eventTopic = malloc(len + EVENT_TOPIC_DEFAULT_LEN + 1);
AzureIoTClient 4:e472f5ce3473 584 if (eventTopic == NULL)
AzureIoTClient 4:e472f5ce3473 585 {
AzureIoTClient 4:e472f5ce3473 586 result = NULL;
AzureIoTClient 4:e472f5ce3473 587 }
AzureIoTClient 4:e472f5ce3473 588 else
AzureIoTClient 4:e472f5ce3473 589 {
AzureIoTClient 4:e472f5ce3473 590 (void)sprintf(eventTopic, DEVICE_DEVICE_TOPIC, deviceId);
AzureIoTClient 4:e472f5ce3473 591 result = STRING_construct(eventTopic);
AzureIoTClient 4:e472f5ce3473 592 free(eventTopic);
AzureIoTClient 4:e472f5ce3473 593 }
AzureIoTClient 4:e472f5ce3473 594 return result;
AzureIoTClient 3:40f482ed0be8 595 }
AzureIoTClient 3:40f482ed0be8 596
AzureIoTClient 3:40f482ed0be8 597 static STRING_HANDLE ConstructMessageTopic(const char* deviceId)
AzureIoTClient 3:40f482ed0be8 598 {
AzureIoTClient 4:e472f5ce3473 599 STRING_HANDLE result;
AzureIoTClient 4:e472f5ce3473 600 size_t len = strlen(deviceId);
AzureIoTClient 3:40f482ed0be8 601
AzureIoTClient 4:e472f5ce3473 602 char* messageTopic = malloc(len + 32 + 1);
AzureIoTClient 4:e472f5ce3473 603 if (messageTopic == NULL)
AzureIoTClient 4:e472f5ce3473 604 {
AzureIoTClient 4:e472f5ce3473 605 result = NULL;
AzureIoTClient 4:e472f5ce3473 606 }
AzureIoTClient 4:e472f5ce3473 607 else
AzureIoTClient 4:e472f5ce3473 608 {
AzureIoTClient 4:e472f5ce3473 609 (void)sprintf(messageTopic, DEVICE_MSG_TOPIC, deviceId);
AzureIoTClient 4:e472f5ce3473 610 result = STRING_construct(messageTopic);
AzureIoTClient 4:e472f5ce3473 611 free(messageTopic);
AzureIoTClient 4:e472f5ce3473 612 }
AzureIoTClient 4:e472f5ce3473 613 return result;
AzureIoTClient 3:40f482ed0be8 614 }
AzureIoTClient 3:40f482ed0be8 615
AzureIoTClient 3:40f482ed0be8 616 static int GetTransportProviderIfNecessary(PMQTTTRANSPORT_HANDLE_DATA transportState)
AzureIoTClient 3:40f482ed0be8 617 {
AzureIoTClient 4:e472f5ce3473 618 int result;
AzureIoTClient 3:40f482ed0be8 619
AzureIoTClient 4:e472f5ce3473 620 if (transportState->xioTransport == NULL)
AzureIoTClient 4:e472f5ce3473 621 {
AzureIoTClient 4:e472f5ce3473 622 // construct address
AzureIoTClient 4:e472f5ce3473 623 const char* hostAddress = STRING_c_str(transportState->hostAddress);
AzureIoTClient 4:e472f5ce3473 624 transportState->xioTransport = getIoTransportProvider(hostAddress, transportState->portNum);
AzureIoTClient 4:e472f5ce3473 625 if (transportState->xioTransport == NULL)
AzureIoTClient 4:e472f5ce3473 626 {
AzureIoTClient 4:e472f5ce3473 627 LogError("Unable to create the lower level TLS layer.");
AzureIoTClient 4:e472f5ce3473 628 result = __LINE__;
AzureIoTClient 4:e472f5ce3473 629 }
AzureIoTClient 4:e472f5ce3473 630 else
AzureIoTClient 4:e472f5ce3473 631 {
AzureIoTClient 4:e472f5ce3473 632 result = 0;
AzureIoTClient 4:e472f5ce3473 633 }
AzureIoTClient 4:e472f5ce3473 634 }
AzureIoTClient 4:e472f5ce3473 635 else
AzureIoTClient 4:e472f5ce3473 636 {
AzureIoTClient 4:e472f5ce3473 637 result = 0;
AzureIoTClient 4:e472f5ce3473 638 }
AzureIoTClient 4:e472f5ce3473 639 return result;
AzureIoTClient 3:40f482ed0be8 640 }
AzureIoTClient 3:40f482ed0be8 641
AzureIoTClient 3:40f482ed0be8 642 static int SendMqttConnectMsg(PMQTTTRANSPORT_HANDLE_DATA transportState)
AzureIoTClient 3:40f482ed0be8 643 {
AzureIoTClient 4:e472f5ce3473 644 int result;
AzureIoTClient 3:40f482ed0be8 645
AzureIoTClient 4:e472f5ce3473 646 // Not checking the success of this variable, if fail it will fail in the SASToken creation and return false;
AzureIoTClient 4:e472f5ce3473 647 STRING_HANDLE emptyKeyName = STRING_new();
AzureIoTClient 3:40f482ed0be8 648 if (emptyKeyName == NULL)
Azure.IoT Build 0:5e72a75c31b8 649 {
Azure.IoT Build 0:5e72a75c31b8 650 result = __LINE__;
Azure.IoT Build 0:5e72a75c31b8 651 }
Azure.IoT Build 0:5e72a75c31b8 652 else
Azure.IoT Build 0:5e72a75c31b8 653 {
AzureIoTClient 3:40f482ed0be8 654 STRING_HANDLE sasToken = NULL;
AzureIoTClient 7:7fdd306e6224 655
AzureIoTClient 7:7fdd306e6224 656 switch (transportState->transport_creds.credential_type)
AzureIoTClient 3:40f482ed0be8 657 {
AzureIoTClient 7:7fdd306e6224 658 case SAS_TOKEN_FROM_USER:
AzureIoTClient 7:7fdd306e6224 659 sasToken = STRING_clone(transportState->transport_creds.CREDENTIAL_VALUE.deviceSasToken);
AzureIoTClient 7:7fdd306e6224 660 break;
AzureIoTClient 7:7fdd306e6224 661 case DEVICE_KEY:
AzureIoTClient 7:7fdd306e6224 662 {
AzureIoTClient 7:7fdd306e6224 663 // Construct SAS token
AzureIoTClient 7:7fdd306e6224 664 size_t secSinceEpoch = (size_t)(difftime(get_time(NULL), EPOCH_TIME_T_VALUE) + 0);
AzureIoTClient 7:7fdd306e6224 665 size_t expiryTime = secSinceEpoch + SAS_TOKEN_DEFAULT_LIFETIME;
AzureIoTClient 7:7fdd306e6224 666
AzureIoTClient 7:7fdd306e6224 667 sasToken = SASToken_Create(transportState->transport_creds.CREDENTIAL_VALUE.deviceKey, transportState->devicesPath, emptyKeyName, expiryTime);
AzureIoTClient 7:7fdd306e6224 668 break;
AzureIoTClient 7:7fdd306e6224 669 }
AzureIoTClient 7:7fdd306e6224 670 case X509:
AzureIoTClient 7:7fdd306e6224 671 default:
AzureIoTClient 7:7fdd306e6224 672 // The assumption here is that x509 is in place, if not setup
AzureIoTClient 7:7fdd306e6224 673 // correctly the connection will be rejected.
AzureIoTClient 7:7fdd306e6224 674 sasToken = NULL;
AzureIoTClient 7:7fdd306e6224 675 break;
AzureIoTClient 3:40f482ed0be8 676 }
AzureIoTClient 3:40f482ed0be8 677
AzureIoTClient 7:7fdd306e6224 678 MQTT_CLIENT_OPTIONS options = { 0 };
AzureIoTClient 7:7fdd306e6224 679 options.clientId = (char*)STRING_c_str(transportState->device_id);
AzureIoTClient 7:7fdd306e6224 680 options.willMessage = NULL;
AzureIoTClient 7:7fdd306e6224 681 options.username = (char*)STRING_c_str(transportState->configPassedThroughUsername);
AzureIoTClient 7:7fdd306e6224 682 if (sasToken != NULL)
Azure.IoT Build 0:5e72a75c31b8 683 {
AzureIoTClient 7:7fdd306e6224 684 options.password = (char*)STRING_c_str(sasToken);
AzureIoTClient 7:7fdd306e6224 685 }
AzureIoTClient 7:7fdd306e6224 686 options.keepAliveInterval = transportState->keepAliveValue;
AzureIoTClient 7:7fdd306e6224 687 options.useCleanSession = false;
AzureIoTClient 7:7fdd306e6224 688 options.qualityOfServiceValue = DELIVER_AT_LEAST_ONCE;
AzureIoTClient 7:7fdd306e6224 689
AzureIoTClient 7:7fdd306e6224 690 if (GetTransportProviderIfNecessary(transportState) == 0)
AzureIoTClient 7:7fdd306e6224 691 {
AzureIoTClient 7:7fdd306e6224 692 if (mqtt_client_connect(transportState->mqttClient, transportState->xioTransport, &options) != 0)
AzureIoTClient 7:7fdd306e6224 693 {
AzureIoTClient 7:7fdd306e6224 694 LogError("failure connecting to address %s:%d.", STRING_c_str(transportState->hostAddress), transportState->portNum);
AzureIoTClient 7:7fdd306e6224 695 result = __LINE__;
AzureIoTClient 7:7fdd306e6224 696 }
AzureIoTClient 7:7fdd306e6224 697 else
AzureIoTClient 7:7fdd306e6224 698 {
AzureIoTClient 7:7fdd306e6224 699 (void)tickcounter_get_current_ms(g_msgTickCounter, &transportState->mqtt_connect_time);
AzureIoTClient 7:7fdd306e6224 700 result = 0;
AzureIoTClient 7:7fdd306e6224 701 }
Azure.IoT Build 0:5e72a75c31b8 702 }
Azure.IoT Build 0:5e72a75c31b8 703 else
Azure.IoT Build 0:5e72a75c31b8 704 {
AzureIoTClient 7:7fdd306e6224 705 result = __LINE__;
Azure.IoT Build 0:5e72a75c31b8 706 }
AzureIoTClient 3:40f482ed0be8 707 STRING_delete(emptyKeyName);
AzureIoTClient 3:40f482ed0be8 708 STRING_delete(sasToken);
AzureIoTClient 2:9db4da50abaa 709 }
AzureIoTClient 4:e472f5ce3473 710 return result;
Azure.IoT Build 0:5e72a75c31b8 711 }
Azure.IoT Build 0:5e72a75c31b8 712
Azure.IoT Build 0:5e72a75c31b8 713 static int InitializeConnection(PMQTTTRANSPORT_HANDLE_DATA transportState)
Azure.IoT Build 0:5e72a75c31b8 714 {
AzureIoTClient 4:e472f5ce3473 715 int result = 0;
AzureIoTClient 2:9db4da50abaa 716
AzureIoTClient 4:e472f5ce3473 717 // Make sure we're not destroying the object
AzureIoTClient 4:e472f5ce3473 718 if (!transportState->destroyCalled)
AzureIoTClient 4:e472f5ce3473 719 {
AzureIoTClient 4:e472f5ce3473 720 // If we are not connected then check to see if we need
AzureIoTClient 4:e472f5ce3473 721 // to back off the connecting to the server
AzureIoTClient 4:e472f5ce3473 722 if (!transportState->connected)
AzureIoTClient 4:e472f5ce3473 723 {
AzureIoTClient 4:e472f5ce3473 724 // Default makeConnection as true if something goes wrong we'll make the connection
AzureIoTClient 4:e472f5ce3473 725 bool makeConnection = true;
AzureIoTClient 4:e472f5ce3473 726 // If we've failed for FAILED_CONN_BACKOFF_VALUE straight times them let's slow down connection
AzureIoTClient 4:e472f5ce3473 727 // to the service
AzureIoTClient 4:e472f5ce3473 728 if (transportState->connectFailCount > FAILED_CONN_BACKOFF_VALUE)
AzureIoTClient 4:e472f5ce3473 729 {
AzureIoTClient 4:e472f5ce3473 730 uint64_t currentTick;
AzureIoTClient 4:e472f5ce3473 731 if (tickcounter_get_current_ms(g_msgTickCounter, &currentTick) == 0)
AzureIoTClient 4:e472f5ce3473 732 {
AzureIoTClient 4:e472f5ce3473 733 if ( ((currentTick - transportState->connectTick)/1000) <= DEFAULT_CONNECTION_INTERVAL)
AzureIoTClient 4:e472f5ce3473 734 {
AzureIoTClient 4:e472f5ce3473 735 result = __LINE__;
AzureIoTClient 4:e472f5ce3473 736 makeConnection = false;
AzureIoTClient 4:e472f5ce3473 737 }
AzureIoTClient 4:e472f5ce3473 738 }
AzureIoTClient 4:e472f5ce3473 739 }
AzureIoTClient 2:9db4da50abaa 740
AzureIoTClient 4:e472f5ce3473 741 if (makeConnection)
AzureIoTClient 4:e472f5ce3473 742 {
AzureIoTClient 4:e472f5ce3473 743 (void)tickcounter_get_current_ms(g_msgTickCounter, &transportState->connectTick);
AzureIoTClient 4:e472f5ce3473 744 if (SendMqttConnectMsg(transportState) != 0)
AzureIoTClient 4:e472f5ce3473 745 {
AzureIoTClient 4:e472f5ce3473 746 transportState->connectFailCount++;
AzureIoTClient 4:e472f5ce3473 747 result = __LINE__;
AzureIoTClient 4:e472f5ce3473 748 }
AzureIoTClient 4:e472f5ce3473 749 else
AzureIoTClient 4:e472f5ce3473 750 {
AzureIoTClient 4:e472f5ce3473 751 transportState->connectFailCount = 0;
AzureIoTClient 4:e472f5ce3473 752 transportState->connected = true;
AzureIoTClient 4:e472f5ce3473 753 result = 0;
AzureIoTClient 4:e472f5ce3473 754 }
AzureIoTClient 4:e472f5ce3473 755 }
AzureIoTClient 4:e472f5ce3473 756 }
AzureIoTClient 2:9db4da50abaa 757
AzureIoTClient 4:e472f5ce3473 758 if (transportState->connected)
AzureIoTClient 4:e472f5ce3473 759 {
AzureIoTClient 4:e472f5ce3473 760 // We are connected and not being closed, so does SAS need to reconnect?
AzureIoTClient 4:e472f5ce3473 761 uint64_t current_time;
AzureIoTClient 4:e472f5ce3473 762 (void)tickcounter_get_current_ms(g_msgTickCounter, &current_time);
AzureIoTClient 4:e472f5ce3473 763 if ((current_time - transportState->mqtt_connect_time) / 1000 > (SAS_TOKEN_DEFAULT_LIFETIME*SAS_REFRESH_MULTIPLIER))
AzureIoTClient 4:e472f5ce3473 764 {
AzureIoTClient 4:e472f5ce3473 765 (void)mqtt_client_disconnect(transportState->mqttClient);
AzureIoTClient 4:e472f5ce3473 766 transportState->subscribed = false;
AzureIoTClient 4:e472f5ce3473 767 transportState->connected = false;
AzureIoTClient 4:e472f5ce3473 768 transportState->currPacketState = UNKNOWN_TYPE;
AzureIoTClient 4:e472f5ce3473 769 }
AzureIoTClient 4:e472f5ce3473 770 }
AzureIoTClient 4:e472f5ce3473 771 }
AzureIoTClient 4:e472f5ce3473 772 return result;
Azure.IoT Build 0:5e72a75c31b8 773 }
Azure.IoT Build 0:5e72a75c31b8 774
Azure.IoT Build 0:5e72a75c31b8 775 static STRING_HANDLE buildConfigForUsername(const IOTHUB_CLIENT_CONFIG* upperConfig)
Azure.IoT Build 0:5e72a75c31b8 776 {
AzureIoTClient 4:e472f5ce3473 777 STRING_HANDLE result;
Azure.IoT Build 0:5e72a75c31b8 778
AzureIoTClient 4:e472f5ce3473 779 size_t len = strlen(upperConfig->iotHubName)+strlen(upperConfig->iotHubSuffix)+strlen(upperConfig->deviceId)+strlen(CLIENT_DEVICE_TYPE_PREFIX)+strlen(IOTHUB_SDK_VERSION);
AzureIoTClient 4:e472f5ce3473 780 char* eventTopic = malloc(len + BUILD_CONFIG_USERNAME + 1);
AzureIoTClient 4:e472f5ce3473 781 if (eventTopic == NULL)
AzureIoTClient 4:e472f5ce3473 782 {
AzureIoTClient 4:e472f5ce3473 783 result = NULL;
AzureIoTClient 4:e472f5ce3473 784 }
AzureIoTClient 4:e472f5ce3473 785 else
AzureIoTClient 4:e472f5ce3473 786 {
AzureIoTClient 4:e472f5ce3473 787 (void)sprintf(eventTopic, "%s.%s/%s/DeviceClientType=%s%%2F%s", upperConfig->iotHubName, upperConfig->iotHubSuffix, upperConfig->deviceId, CLIENT_DEVICE_TYPE_PREFIX, IOTHUB_SDK_VERSION);
AzureIoTClient 4:e472f5ce3473 788 result = STRING_construct(eventTopic);
AzureIoTClient 4:e472f5ce3473 789 free(eventTopic);
AzureIoTClient 4:e472f5ce3473 790 }
AzureIoTClient 4:e472f5ce3473 791 return result;
Azure.IoT Build 0:5e72a75c31b8 792 }
Azure.IoT Build 0:5e72a75c31b8 793
AzureIoTClient 7:7fdd306e6224 794 static int construct_credential_information(const IOTHUB_CLIENT_CONFIG* upperConfig, PMQTTTRANSPORT_HANDLE_DATA transportState)
AzureIoTClient 7:7fdd306e6224 795 {
AzureIoTClient 7:7fdd306e6224 796 int result;
AzureIoTClient 7:7fdd306e6224 797 if (upperConfig->deviceKey != NULL)
AzureIoTClient 7:7fdd306e6224 798 {
AzureIoTClient 7:7fdd306e6224 799 transportState->transport_creds.CREDENTIAL_VALUE.deviceKey = STRING_construct(upperConfig->deviceKey);
AzureIoTClient 7:7fdd306e6224 800 if (transportState->transport_creds.CREDENTIAL_VALUE.deviceKey == NULL)
AzureIoTClient 7:7fdd306e6224 801 {
AzureIoTClient 7:7fdd306e6224 802 LogError("Could not create device key for MQTT");
AzureIoTClient 7:7fdd306e6224 803 result = __LINE__;
AzureIoTClient 7:7fdd306e6224 804 }
AzureIoTClient 7:7fdd306e6224 805 else if ( (transportState->devicesPath = ConstructSasToken(upperConfig->iotHubName, upperConfig->iotHubSuffix, upperConfig->deviceId)) == NULL)
AzureIoTClient 7:7fdd306e6224 806 {
AzureIoTClient 7:7fdd306e6224 807 STRING_delete(transportState->transport_creds.CREDENTIAL_VALUE.deviceKey);
AzureIoTClient 7:7fdd306e6224 808 result = __LINE__;
AzureIoTClient 7:7fdd306e6224 809 }
AzureIoTClient 7:7fdd306e6224 810 else
AzureIoTClient 7:7fdd306e6224 811 {
AzureIoTClient 7:7fdd306e6224 812 transportState->transport_creds.credential_type = DEVICE_KEY;
AzureIoTClient 7:7fdd306e6224 813 result = 0;
AzureIoTClient 7:7fdd306e6224 814 }
AzureIoTClient 7:7fdd306e6224 815 }
AzureIoTClient 7:7fdd306e6224 816 else if (upperConfig->deviceSasToken != NULL)
AzureIoTClient 7:7fdd306e6224 817 {
AzureIoTClient 7:7fdd306e6224 818 transportState->transport_creds.CREDENTIAL_VALUE.deviceSasToken = STRING_construct(upperConfig->deviceSasToken);
AzureIoTClient 7:7fdd306e6224 819 if (transportState->transport_creds.CREDENTIAL_VALUE.deviceSasToken == NULL)
AzureIoTClient 7:7fdd306e6224 820 {
AzureIoTClient 7:7fdd306e6224 821 result = __LINE__;
AzureIoTClient 7:7fdd306e6224 822 }
AzureIoTClient 7:7fdd306e6224 823 else
AzureIoTClient 7:7fdd306e6224 824 {
AzureIoTClient 7:7fdd306e6224 825 transportState->transport_creds.credential_type = SAS_TOKEN_FROM_USER;
AzureIoTClient 7:7fdd306e6224 826 transportState->devicesPath = NULL;
AzureIoTClient 7:7fdd306e6224 827 result = 0;
AzureIoTClient 7:7fdd306e6224 828 }
AzureIoTClient 7:7fdd306e6224 829 }
AzureIoTClient 7:7fdd306e6224 830 else
AzureIoTClient 7:7fdd306e6224 831 {
AzureIoTClient 7:7fdd306e6224 832 /* Codes_SRS_IOTHUB_MQTT_TRANSPORT_07_041: [If both deviceKey and deviceSasToken fields are NULL then IoTHubTransportMqtt_Create shall assume a x509 authentication.] */
AzureIoTClient 7:7fdd306e6224 833 transportState->transport_creds.credential_type = X509;
AzureIoTClient 7:7fdd306e6224 834 transportState->devicesPath = NULL;
AzureIoTClient 7:7fdd306e6224 835 result = 0;
AzureIoTClient 7:7fdd306e6224 836 }
AzureIoTClient 7:7fdd306e6224 837 return result;
AzureIoTClient 7:7fdd306e6224 838 }
AzureIoTClient 7:7fdd306e6224 839
Azure.IoT Build 0:5e72a75c31b8 840 static PMQTTTRANSPORT_HANDLE_DATA InitializeTransportHandleData(const IOTHUB_CLIENT_CONFIG* upperConfig, PDLIST_ENTRY waitingToSend)
Azure.IoT Build 0:5e72a75c31b8 841 {
AzureIoTClient 4:e472f5ce3473 842 PMQTTTRANSPORT_HANDLE_DATA state = (PMQTTTRANSPORT_HANDLE_DATA)malloc(sizeof(MQTTTRANSPORT_HANDLE_DATA));
AzureIoTClient 4:e472f5ce3473 843 if (state == NULL)
AzureIoTClient 4:e472f5ce3473 844 {
AzureIoTClient 4:e472f5ce3473 845 LogError("Could not create MQTT transport state. Memory allocation failed.");
AzureIoTClient 4:e472f5ce3473 846 }
AzureIoTClient 4:e472f5ce3473 847 else if ((state->device_id = STRING_construct(upperConfig->deviceId)) == NULL)
AzureIoTClient 4:e472f5ce3473 848 {
AzureIoTClient 7:7fdd306e6224 849 LogError("failure constructing device_id.");
AzureIoTClient 4:e472f5ce3473 850 free(state);
AzureIoTClient 4:e472f5ce3473 851 state = NULL;
AzureIoTClient 4:e472f5ce3473 852 }
Azure.IoT Build 0:5e72a75c31b8 853 else
Azure.IoT Build 0:5e72a75c31b8 854 {
AzureIoTClient 7:7fdd306e6224 855 if (construct_credential_information(upperConfig, state) != 0)
AzureIoTClient 3:40f482ed0be8 856 {
AzureIoTClient 3:40f482ed0be8 857 STRING_delete(state->device_id);
AzureIoTClient 3:40f482ed0be8 858 free(state);
AzureIoTClient 3:40f482ed0be8 859 state = NULL;
AzureIoTClient 3:40f482ed0be8 860 }
AzureIoTClient 3:40f482ed0be8 861 else if ((state->mqttEventTopic = ConstructEventTopic(upperConfig->deviceId)) == NULL)
AzureIoTClient 3:40f482ed0be8 862 {
AzureIoTClient 3:40f482ed0be8 863 LogError("Could not create mqttEventTopic for MQTT");
AzureIoTClient 7:7fdd306e6224 864 STRING_delete(state->devicesPath);
AzureIoTClient 7:7fdd306e6224 865 if (state->transport_creds.credential_type == DEVICE_KEY)
AzureIoTClient 7:7fdd306e6224 866 {
AzureIoTClient 7:7fdd306e6224 867 STRING_delete(state->transport_creds.CREDENTIAL_VALUE.deviceKey);
AzureIoTClient 7:7fdd306e6224 868 }
AzureIoTClient 7:7fdd306e6224 869 else if (state->transport_creds.credential_type == SAS_TOKEN_FROM_USER)
AzureIoTClient 7:7fdd306e6224 870 {
AzureIoTClient 7:7fdd306e6224 871 STRING_delete(state->transport_creds.CREDENTIAL_VALUE.deviceSasToken);
AzureIoTClient 7:7fdd306e6224 872 }
AzureIoTClient 3:40f482ed0be8 873 STRING_delete(state->device_id);
AzureIoTClient 3:40f482ed0be8 874 free(state);
AzureIoTClient 3:40f482ed0be8 875 state = NULL;
AzureIoTClient 3:40f482ed0be8 876 }
AzureIoTClient 3:40f482ed0be8 877 else if ((state->mqttMessageTopic = ConstructMessageTopic(upperConfig->deviceId)) == NULL)
AzureIoTClient 3:40f482ed0be8 878 {
AzureIoTClient 3:40f482ed0be8 879 LogError("Could not create mqttMessageTopic for MQTT");
AzureIoTClient 7:7fdd306e6224 880 STRING_delete(state->devicesPath);
AzureIoTClient 7:7fdd306e6224 881 if (state->transport_creds.credential_type == DEVICE_KEY)
AzureIoTClient 7:7fdd306e6224 882 {
AzureIoTClient 7:7fdd306e6224 883 STRING_delete(state->transport_creds.CREDENTIAL_VALUE.deviceKey);
AzureIoTClient 7:7fdd306e6224 884 }
AzureIoTClient 7:7fdd306e6224 885 else if (state->transport_creds.credential_type == SAS_TOKEN_FROM_USER)
AzureIoTClient 7:7fdd306e6224 886 {
AzureIoTClient 7:7fdd306e6224 887 STRING_delete(state->transport_creds.CREDENTIAL_VALUE.deviceSasToken);
AzureIoTClient 7:7fdd306e6224 888 }
Azure.IoT Build 0:5e72a75c31b8 889 STRING_delete(state->mqttEventTopic);
Azure.IoT Build 0:5e72a75c31b8 890 STRING_delete(state->device_id);
Azure.IoT Build 0:5e72a75c31b8 891 free(state);
Azure.IoT Build 0:5e72a75c31b8 892 state = NULL;
Azure.IoT Build 0:5e72a75c31b8 893 }
Azure.IoT Build 0:5e72a75c31b8 894 else
Azure.IoT Build 0:5e72a75c31b8 895 {
Azure.IoT Build 5:73603e7a6542 896 state->mqttClient = mqtt_client_init(MqttRecvCallback, MqttOpCompleteCallback, state);
AzureIoTClient 3:40f482ed0be8 897 if (state->mqttClient == NULL)
Azure.IoT Build 0:5e72a75c31b8 898 {
AzureIoTClient 7:7fdd306e6224 899 LogError("failure initializing mqtt client.");
AzureIoTClient 7:7fdd306e6224 900 STRING_delete(state->devicesPath);
AzureIoTClient 7:7fdd306e6224 901 if (state->transport_creds.credential_type == DEVICE_KEY)
AzureIoTClient 7:7fdd306e6224 902 {
AzureIoTClient 7:7fdd306e6224 903 STRING_delete(state->transport_creds.CREDENTIAL_VALUE.deviceKey);
AzureIoTClient 7:7fdd306e6224 904 }
AzureIoTClient 7:7fdd306e6224 905 else if (state->transport_creds.credential_type == SAS_TOKEN_FROM_USER)
AzureIoTClient 7:7fdd306e6224 906 {
AzureIoTClient 7:7fdd306e6224 907 STRING_delete(state->transport_creds.CREDENTIAL_VALUE.deviceSasToken);
AzureIoTClient 7:7fdd306e6224 908 }
Azure.IoT Build 0:5e72a75c31b8 909 STRING_delete(state->mqttEventTopic);
Azure.IoT Build 0:5e72a75c31b8 910 STRING_delete(state->mqttMessageTopic);
Azure.IoT Build 0:5e72a75c31b8 911 STRING_delete(state->device_id);
Azure.IoT Build 0:5e72a75c31b8 912 free(state);
Azure.IoT Build 0:5e72a75c31b8 913 state = NULL;
Azure.IoT Build 0:5e72a75c31b8 914 }
Azure.IoT Build 0:5e72a75c31b8 915 else
Azure.IoT Build 0:5e72a75c31b8 916 {
AzureIoTClient 3:40f482ed0be8 917 /* Codes_SRS_IOTHUB_MQTT_TRANSPORT_07_008: [The hostname shall be constructed using the iothubname and iothubSuffix.] */
AzureIoTClient 3:40f482ed0be8 918 char tempAddress[DEFAULT_TEMP_STRING_LEN];
AzureIoTClient 3:40f482ed0be8 919 (void)snprintf(tempAddress, DEFAULT_TEMP_STRING_LEN, "%s.%s", upperConfig->iotHubName, upperConfig->iotHubSuffix);
AzureIoTClient 3:40f482ed0be8 920 if ((state->hostAddress = STRING_construct(tempAddress)) == NULL)
AzureIoTClient 3:40f482ed0be8 921 {
AzureIoTClient 7:7fdd306e6224 922 LogError("failure constructing host address.");
AzureIoTClient 7:7fdd306e6224 923 STRING_delete(state->devicesPath);
AzureIoTClient 7:7fdd306e6224 924 if (state->transport_creds.credential_type == DEVICE_KEY)
AzureIoTClient 7:7fdd306e6224 925 {
AzureIoTClient 7:7fdd306e6224 926 STRING_delete(state->transport_creds.CREDENTIAL_VALUE.deviceKey);
AzureIoTClient 7:7fdd306e6224 927 }
AzureIoTClient 7:7fdd306e6224 928 else if (state->transport_creds.credential_type == SAS_TOKEN_FROM_USER)
AzureIoTClient 7:7fdd306e6224 929 {
AzureIoTClient 7:7fdd306e6224 930 STRING_delete(state->transport_creds.CREDENTIAL_VALUE.deviceSasToken);
AzureIoTClient 7:7fdd306e6224 931 }
AzureIoTClient 3:40f482ed0be8 932 STRING_delete(state->mqttEventTopic);
AzureIoTClient 3:40f482ed0be8 933 STRING_delete(state->mqttMessageTopic);
AzureIoTClient 3:40f482ed0be8 934 STRING_delete(state->device_id);
AzureIoTClient 3:40f482ed0be8 935 free(state);
AzureIoTClient 3:40f482ed0be8 936 state = NULL;
AzureIoTClient 3:40f482ed0be8 937 }
AzureIoTClient 3:40f482ed0be8 938 else if ((state->configPassedThroughUsername = buildConfigForUsername(upperConfig)) == NULL)
AzureIoTClient 3:40f482ed0be8 939 {
AzureIoTClient 7:7fdd306e6224 940 STRING_delete(state->devicesPath);
AzureIoTClient 7:7fdd306e6224 941 if (state->transport_creds.credential_type == DEVICE_KEY)
AzureIoTClient 7:7fdd306e6224 942 {
AzureIoTClient 7:7fdd306e6224 943 STRING_delete(state->transport_creds.CREDENTIAL_VALUE.deviceKey);
AzureIoTClient 7:7fdd306e6224 944 }
AzureIoTClient 7:7fdd306e6224 945 else if (state->transport_creds.credential_type == SAS_TOKEN_FROM_USER)
AzureIoTClient 7:7fdd306e6224 946 {
AzureIoTClient 7:7fdd306e6224 947 STRING_delete(state->transport_creds.CREDENTIAL_VALUE.deviceSasToken);
AzureIoTClient 7:7fdd306e6224 948 }
AzureIoTClient 3:40f482ed0be8 949 STRING_delete(state->hostAddress);
AzureIoTClient 3:40f482ed0be8 950 STRING_delete(state->mqttEventTopic);
AzureIoTClient 3:40f482ed0be8 951 STRING_delete(state->mqttMessageTopic);
AzureIoTClient 3:40f482ed0be8 952 STRING_delete(state->device_id);
AzureIoTClient 3:40f482ed0be8 953 free(state);
AzureIoTClient 3:40f482ed0be8 954 state = NULL;
AzureIoTClient 3:40f482ed0be8 955 }
AzureIoTClient 3:40f482ed0be8 956 else
AzureIoTClient 3:40f482ed0be8 957 {
AzureIoTClient 3:40f482ed0be8 958 /* Codes_SRS_IOTHUB_MQTT_TRANSPORT_07_010: [IoTHubTransportMqtt_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 3:40f482ed0be8 959 DList_InitializeListHead(&(state->waitingForAck));
AzureIoTClient 3:40f482ed0be8 960 state->destroyCalled = false;
AzureIoTClient 3:40f482ed0be8 961 state->isRegistered = false;
AzureIoTClient 3:40f482ed0be8 962 state->subscribed = false;
AzureIoTClient 3:40f482ed0be8 963 state->connected = false;
AzureIoTClient 3:40f482ed0be8 964 state->receiveMessages = false;
AzureIoTClient 3:40f482ed0be8 965 state->packetId = 1;
AzureIoTClient 3:40f482ed0be8 966 state->llClientHandle = NULL;
AzureIoTClient 3:40f482ed0be8 967 state->xioTransport = NULL;
AzureIoTClient 3:40f482ed0be8 968 state->portNum = DEFAULT_PORT_NUMBER;
AzureIoTClient 3:40f482ed0be8 969 state->waitingToSend = waitingToSend;
AzureIoTClient 3:40f482ed0be8 970 state->currPacketState = CONNECT_TYPE;
AzureIoTClient 3:40f482ed0be8 971 state->keepAliveValue = DEFAULT_MQTT_KEEPALIVE;
AzureIoTClient 3:40f482ed0be8 972 state->connectFailCount = 0;
AzureIoTClient 3:40f482ed0be8 973 state->connectTick = 0;
AzureIoTClient 3:40f482ed0be8 974 }
Azure.IoT Build 0:5e72a75c31b8 975 }
Azure.IoT Build 0:5e72a75c31b8 976 }
Azure.IoT Build 0:5e72a75c31b8 977 }
AzureIoTClient 4:e472f5ce3473 978 return state;
Azure.IoT Build 0:5e72a75c31b8 979 }
Azure.IoT Build 0:5e72a75c31b8 980
AzureIoTClient 4:e472f5ce3473 981 static TRANSPORT_LL_HANDLE IoTHubTransportMqtt_Create(const IOTHUBTRANSPORT_CONFIG* config)
Azure.IoT Build 0:5e72a75c31b8 982 {
AzureIoTClient 4:e472f5ce3473 983 PMQTTTRANSPORT_HANDLE_DATA result;
AzureIoTClient 4:e472f5ce3473 984 size_t deviceIdSize;
AzureIoTClient 3:40f482ed0be8 985
AzureIoTClient 4:e472f5ce3473 986 /* Codes_SRS_IOTHUB_MQTT_TRANSPORT_07_001: [If parameter config is NULL then IoTHubTransportMqtt_Create shall return NULL.] */
AzureIoTClient 4:e472f5ce3473 987 if (config == NULL)
AzureIoTClient 4:e472f5ce3473 988 {
AzureIoTClient 4:e472f5ce3473 989 LogError("Invalid Argument: Config Parameter is NULL.");
AzureIoTClient 4:e472f5ce3473 990 result = NULL;
AzureIoTClient 4:e472f5ce3473 991 }
AzureIoTClient 4:e472f5ce3473 992 /* Codes_SRS_IOTHUB_MQTT_TRANSPORT_07_002: [If the parameter config's variables upperConfig or waitingToSend are NULL then IoTHubTransportMqtt_Create shall return NULL.] */
AzureIoTClient 4:e472f5ce3473 993 /* Codes_SRS_IOTHUB_MQTT_TRANSPORT_07_003: [If the upperConfig's variables deviceId, both deviceKey and deviceSasToken, iotHubName, protocol, or iotHubSuffix are NULL then IoTHubTransportMqtt_Create shall return NULL.] */
AzureIoTClient 7:7fdd306e6224 994 /* Codes_SRS_IOTHUB_MQTT_TRANSPORT_03_003: [If both deviceKey & deviceSasToken fields are NOT NULL then IoTHubTransportMqtt_Create shall return NULL.] */
AzureIoTClient 7:7fdd306e6224 995 else if (config->upperConfig == NULL ||
AzureIoTClient 7:7fdd306e6224 996 config->upperConfig->protocol == NULL ||
AzureIoTClient 7:7fdd306e6224 997 config->upperConfig->deviceId == NULL ||
AzureIoTClient 7:7fdd306e6224 998 ((config->upperConfig->deviceKey != NULL) && (config->upperConfig->deviceSasToken != NULL)) ||
AzureIoTClient 7:7fdd306e6224 999 config->upperConfig->iotHubName == NULL ||
AzureIoTClient 7:7fdd306e6224 1000 config->upperConfig->iotHubSuffix == NULL)
AzureIoTClient 4:e472f5ce3473 1001 {
AzureIoTClient 4:e472f5ce3473 1002 LogError("Invalid Argument: upperConfig structure contains an invalid parameter");
AzureIoTClient 4:e472f5ce3473 1003 result = NULL;
AzureIoTClient 4:e472f5ce3473 1004 }
AzureIoTClient 4:e472f5ce3473 1005 /* Codes_SRS_IOTHUB_MQTT_TRANSPORT_07_002: [If the parameter config's variables upperConfig or waitingToSend are NULL then IoTHubTransportMqtt_Create shall return NULL.] */
AzureIoTClient 4:e472f5ce3473 1006 else if (config->waitingToSend == NULL)
AzureIoTClient 4:e472f5ce3473 1007 {
AzureIoTClient 4:e472f5ce3473 1008 LogError("Invalid Argument: waitingToSend is NULL)");
AzureIoTClient 4:e472f5ce3473 1009 result = NULL;
AzureIoTClient 4:e472f5ce3473 1010 }
AzureIoTClient 4:e472f5ce3473 1011 /* Codes_SRS_IOTHUB_MQTT_TRANSPORT_07_006: [If the upperConfig's variables deviceId is an empty strings or length is greater then 128 then IoTHubTransportMqtt_Create shall return NULL.] */
AzureIoTClient 4:e472f5ce3473 1012 else if ( ( (deviceIdSize = strlen(config->upperConfig->deviceId)) > 128U) || (deviceIdSize == 0) )
AzureIoTClient 4:e472f5ce3473 1013 {
AzureIoTClient 4:e472f5ce3473 1014 LogError("Invalid Argument: DeviceId is of an invalid size");
AzureIoTClient 4:e472f5ce3473 1015 result = NULL;
AzureIoTClient 4:e472f5ce3473 1016 }
AzureIoTClient 4:e472f5ce3473 1017 /* Codes_SRS_IOTHUB_MQTT_TRANSPORT_07_003: [If the upperConfig's variables deviceId, both deviceKey and deviceSasToken, iotHubName, protocol, or iotHubSuffix are NULL then IoTHubTransportMqtt_Create shall return NULL.] */
AzureIoTClient 4:e472f5ce3473 1018 else if ((config->upperConfig->deviceKey != NULL) && (strlen(config->upperConfig->deviceKey) == 0))
AzureIoTClient 4:e472f5ce3473 1019 {
AzureIoTClient 4:e472f5ce3473 1020 LogError("Invalid Argument: deviceKey is empty");
AzureIoTClient 4:e472f5ce3473 1021 result = NULL;
AzureIoTClient 4:e472f5ce3473 1022 }
AzureIoTClient 4:e472f5ce3473 1023 /* Codes_SRS_IOTHUB_MQTT_TRANSPORT_07_003: [If the upperConfig's variables deviceId, both deviceKey and deviceSasToken, iotHubName, protocol, or iotHubSuffix are NULL then IoTHubTransportMqtt_Create shall return NULL.] */
AzureIoTClient 4:e472f5ce3473 1024 else if ((config->upperConfig->deviceSasToken != NULL) && (strlen(config->upperConfig->deviceSasToken) == 0))
AzureIoTClient 4:e472f5ce3473 1025 {
AzureIoTClient 4:e472f5ce3473 1026 LogError("Invalid Argument: deviceSasToken is empty");
AzureIoTClient 4:e472f5ce3473 1027 result = NULL;
AzureIoTClient 4:e472f5ce3473 1028 }
AzureIoTClient 7:7fdd306e6224 1029 /* Codes_SRS_IOTHUB_MQTT_TRANSPORT_07_003: [If the upperConfig's variables deviceId, deviceKey, iotHubName, protocol, or iotHubSuffix are NULL then IoTHubTransportMqtt_Create shall return NULL.] */
AzureIoTClient 7:7fdd306e6224 1030 else if (strlen(config->upperConfig->iotHubName) == 0)
Azure.IoT Build 0:5e72a75c31b8 1031 {
AzureIoTClient 7:7fdd306e6224 1032 LogError("Invalid Argument: iotHubName is empty");
Azure.IoT Build 0:5e72a75c31b8 1033 result = NULL;
Azure.IoT Build 0:5e72a75c31b8 1034 }
AzureIoTClient 7:7fdd306e6224 1035 else if ((g_msgTickCounter = tickcounter_create()) == NULL)
AzureIoTClient 4:e472f5ce3473 1036 {
AzureIoTClient 4:e472f5ce3473 1037 LogError("Invalid Argument: iotHubName is empty");
AzureIoTClient 4:e472f5ce3473 1038 result = NULL;
AzureIoTClient 4:e472f5ce3473 1039 }
AzureIoTClient 4:e472f5ce3473 1040 else
AzureIoTClient 4:e472f5ce3473 1041 {
AzureIoTClient 4:e472f5ce3473 1042 result = InitializeTransportHandleData(config->upperConfig, config->waitingToSend);
AzureIoTClient 7:7fdd306e6224 1043 if (result == NULL)
AzureIoTClient 4:e472f5ce3473 1044 {
AzureIoTClient 7:7fdd306e6224 1045 tickcounter_destroy(g_msgTickCounter);
AzureIoTClient 4:e472f5ce3473 1046 }
AzureIoTClient 4:e472f5ce3473 1047 }
AzureIoTClient 4:e472f5ce3473 1048 /* Codes_SRS_IOTHUB_MQTT_TRANSPORT_07_009: [If any error is encountered then IoTHubTransportMqtt_Create shall return NULL.] */
AzureIoTClient 4:e472f5ce3473 1049 /* Codes_SRS_IOTHUB_MQTT_TRANSPORT_07_011: [On Success IoTHubTransportMqtt_Create shall return a non-NULL value.] */
AzureIoTClient 4:e472f5ce3473 1050 return result;
Azure.IoT Build 0:5e72a75c31b8 1051 }
Azure.IoT Build 0:5e72a75c31b8 1052
Azure.IoT Build 0:5e72a75c31b8 1053 static void DisconnectFromClient(PMQTTTRANSPORT_HANDLE_DATA transportState)
Azure.IoT Build 0:5e72a75c31b8 1054 {
AzureIoTClient 4:e472f5ce3473 1055 (void)mqtt_client_disconnect(transportState->mqttClient);
AzureIoTClient 4:e472f5ce3473 1056 xio_destroy(transportState->xioTransport);
AzureIoTClient 4:e472f5ce3473 1057 transportState->xioTransport = NULL;
Azure.IoT Build 0:5e72a75c31b8 1058
AzureIoTClient 4:e472f5ce3473 1059 transportState->connected = false;
AzureIoTClient 4:e472f5ce3473 1060 transportState->currPacketState = DISCONNECT_TYPE;
Azure.IoT Build 0:5e72a75c31b8 1061 }
Azure.IoT Build 0:5e72a75c31b8 1062
AzureIoTClient 4:e472f5ce3473 1063 static void IoTHubTransportMqtt_Destroy(TRANSPORT_LL_HANDLE handle)
Azure.IoT Build 0:5e72a75c31b8 1064 {
AzureIoTClient 4:e472f5ce3473 1065 /* Codes_SRS_IOTHUB_MQTT_TRANSPORT_07_012: [IoTHubTransportMqtt_Destroy shall do nothing if parameter handle is NULL.] */
AzureIoTClient 4:e472f5ce3473 1066 PMQTTTRANSPORT_HANDLE_DATA transportState = (PMQTTTRANSPORT_HANDLE_DATA)handle;
AzureIoTClient 4:e472f5ce3473 1067 if (transportState != NULL)
AzureIoTClient 4:e472f5ce3473 1068 {
AzureIoTClient 4:e472f5ce3473 1069 transportState->destroyCalled = true;
Azure.IoT Build 0:5e72a75c31b8 1070
AzureIoTClient 4:e472f5ce3473 1071 DisconnectFromClient(transportState);
Azure.IoT Build 0:5e72a75c31b8 1072
AzureIoTClient 4:e472f5ce3473 1073 //Empty the Waiting for Ack Messages.
AzureIoTClient 4:e472f5ce3473 1074 while (!DList_IsListEmpty(&transportState->waitingForAck))
AzureIoTClient 4:e472f5ce3473 1075 {
AzureIoTClient 4:e472f5ce3473 1076 PDLIST_ENTRY currentEntry = DList_RemoveHeadList(&transportState->waitingForAck);
AzureIoTClient 4:e472f5ce3473 1077 MQTT_MESSAGE_DETAILS_LIST* mqttMsgEntry = containingRecord(currentEntry, MQTT_MESSAGE_DETAILS_LIST, entry);
Azure.IoT Build 5:73603e7a6542 1078 sendMsgComplete(mqttMsgEntry->iotHubMessageEntry, transportState, IOTHUB_CLIENT_CONFIRMATION_BECAUSE_DESTROY);
AzureIoTClient 4:e472f5ce3473 1079 free(mqttMsgEntry);
AzureIoTClient 4:e472f5ce3473 1080 }
Azure.IoT Build 0:5e72a75c31b8 1081
AzureIoTClient 7:7fdd306e6224 1082 switch (transportState->transport_creds.credential_type)
AzureIoTClient 7:7fdd306e6224 1083 {
AzureIoTClient 7:7fdd306e6224 1084 case SAS_TOKEN_FROM_USER:
AzureIoTClient 7:7fdd306e6224 1085 STRING_delete(transportState->transport_creds.CREDENTIAL_VALUE.deviceSasToken);
AzureIoTClient 7:7fdd306e6224 1086 break;
AzureIoTClient 7:7fdd306e6224 1087 case DEVICE_KEY:
AzureIoTClient 7:7fdd306e6224 1088 STRING_delete(transportState->transport_creds.CREDENTIAL_VALUE.deviceKey);
AzureIoTClient 7:7fdd306e6224 1089 STRING_delete(transportState->devicesPath);
AzureIoTClient 7:7fdd306e6224 1090 break;
AzureIoTClient 7:7fdd306e6224 1091 case X509:
AzureIoTClient 7:7fdd306e6224 1092 default:
AzureIoTClient 7:7fdd306e6224 1093 break;
AzureIoTClient 7:7fdd306e6224 1094 }
AzureIoTClient 7:7fdd306e6224 1095
AzureIoTClient 4:e472f5ce3473 1096 /* Codes_SRS_IOTHUB_MQTT_TRANSPORT_07_014: [IoTHubTransportMqtt_Destroy shall free all the resources currently in use.] */
AzureIoTClient 4:e472f5ce3473 1097 mqtt_client_deinit(transportState->mqttClient);
AzureIoTClient 4:e472f5ce3473 1098 STRING_delete(transportState->mqttEventTopic);
AzureIoTClient 4:e472f5ce3473 1099 STRING_delete(transportState->mqttMessageTopic);
AzureIoTClient 4:e472f5ce3473 1100 STRING_delete(transportState->device_id);
AzureIoTClient 4:e472f5ce3473 1101 STRING_delete(transportState->hostAddress);
AzureIoTClient 4:e472f5ce3473 1102 STRING_delete(transportState->configPassedThroughUsername);
AzureIoTClient 4:e472f5ce3473 1103 tickcounter_destroy(g_msgTickCounter);
AzureIoTClient 4:e472f5ce3473 1104 free(transportState);
AzureIoTClient 4:e472f5ce3473 1105 }
Azure.IoT Build 0:5e72a75c31b8 1106 }
Azure.IoT Build 0:5e72a75c31b8 1107
AzureIoTClient 4:e472f5ce3473 1108 static int IoTHubTransportMqtt_Subscribe(IOTHUB_DEVICE_HANDLE handle)
Azure.IoT Build 0:5e72a75c31b8 1109 {
AzureIoTClient 4:e472f5ce3473 1110 int result;
AzureIoTClient 4:e472f5ce3473 1111 PMQTTTRANSPORT_HANDLE_DATA transportState = (PMQTTTRANSPORT_HANDLE_DATA)handle;
AzureIoTClient 4:e472f5ce3473 1112 if (transportState == NULL)
AzureIoTClient 4:e472f5ce3473 1113 {
AzureIoTClient 4:e472f5ce3473 1114 /* Codes_SRS_IOTHUB_MQTT_TRANSPORT_07_015: [If parameter handle is NULL than IoTHubTransportMqtt_Subscribe shall return a non-zero value.] */
AzureIoTClient 4:e472f5ce3473 1115 LogError("Invalid handle parameter. NULL.");
AzureIoTClient 4:e472f5ce3473 1116 result = __LINE__;
AzureIoTClient 4:e472f5ce3473 1117 }
AzureIoTClient 4:e472f5ce3473 1118 else
AzureIoTClient 4:e472f5ce3473 1119 {
AzureIoTClient 4:e472f5ce3473 1120 /* Code_SRS_IOTHUB_MQTT_TRANSPORT_07_016: [IoTHubTransportMqtt_Subscribe shall set a flag to enable mqtt_client_subscribe to be called to subscribe to the Message Topic.] */
AzureIoTClient 4:e472f5ce3473 1121 transportState->receiveMessages = true;
AzureIoTClient 4:e472f5ce3473 1122 /* Code_SRS_IOTHUB_MQTT_TRANSPORT_07_035: [If current packet state is not CONNACT, DISCONNECT_TYPE, or PACKET_TYPE_ERROR then IoTHubTransportMqtt_Subscribe shall set the packet state to SUBSCRIBE_TYPE.]*/
AzureIoTClient 4:e472f5ce3473 1123 if (transportState->currPacketState != CONNACK_TYPE &&
AzureIoTClient 4:e472f5ce3473 1124 transportState->currPacketState != CONNECT_TYPE &&
AzureIoTClient 4:e472f5ce3473 1125 transportState->currPacketState != DISCONNECT_TYPE &&
AzureIoTClient 4:e472f5ce3473 1126 transportState->currPacketState != PACKET_TYPE_ERROR)
AzureIoTClient 4:e472f5ce3473 1127 {
AzureIoTClient 4:e472f5ce3473 1128 transportState->currPacketState = SUBSCRIBE_TYPE;
AzureIoTClient 4:e472f5ce3473 1129 }
AzureIoTClient 4:e472f5ce3473 1130 result = 0;
AzureIoTClient 4:e472f5ce3473 1131 }
AzureIoTClient 4:e472f5ce3473 1132 return result;
Azure.IoT Build 0:5e72a75c31b8 1133 }
Azure.IoT Build 0:5e72a75c31b8 1134
AzureIoTClient 4:e472f5ce3473 1135 static void IoTHubTransportMqtt_Unsubscribe(IOTHUB_DEVICE_HANDLE handle)
Azure.IoT Build 0:5e72a75c31b8 1136 {
AzureIoTClient 4:e472f5ce3473 1137 PMQTTTRANSPORT_HANDLE_DATA transportState = (PMQTTTRANSPORT_HANDLE_DATA)handle;
AzureIoTClient 4:e472f5ce3473 1138 /* Codes_SRS_IOTHUB_MQTT_TRANSPORT_07_019: [If parameter handle is NULL then IoTHubTransportMqtt_Unsubscribe shall do nothing.] */
AzureIoTClient 9:50744bd8239e 1139 if (transportState != NULL)
AzureIoTClient 4:e472f5ce3473 1140 {
AzureIoTClient 4:e472f5ce3473 1141 /* Codes_SRS_IOTHUB_MQTT_TRANSPORT_07_020: [IoTHubTransportMqtt_Unsubscribe shall call mqtt_client_unsubscribe to unsubscribe the mqtt message topic.] */
AzureIoTClient 6:16875b609849 1142 const char* unsubscribe[1];
AzureIoTClient 6:16875b609849 1143 unsubscribe[0] = STRING_c_str(transportState->mqttMessageTopic);
AzureIoTClient 6:16875b609849 1144 (void)mqtt_client_unsubscribe(transportState->mqttClient, get_next_packet_id(transportState), unsubscribe, 1);
AzureIoTClient 4:e472f5ce3473 1145 transportState->subscribed = false;
AzureIoTClient 4:e472f5ce3473 1146 transportState->receiveMessages = false;
AzureIoTClient 4:e472f5ce3473 1147 }
AzureIoTClient 4:e472f5ce3473 1148 else
AzureIoTClient 4:e472f5ce3473 1149 {
AzureIoTClient 4:e472f5ce3473 1150 LogError("Invalid argument to unsubscribe (NULL).");
AzureIoTClient 4:e472f5ce3473 1151 }
Azure.IoT Build 0:5e72a75c31b8 1152 }
Azure.IoT Build 0:5e72a75c31b8 1153
AzureIoTClient 4:e472f5ce3473 1154 static void IoTHubTransportMqtt_DoWork(TRANSPORT_LL_HANDLE handle, IOTHUB_CLIENT_LL_HANDLE iotHubClientHandle)
Azure.IoT Build 0:5e72a75c31b8 1155 {
AzureIoTClient 4:e472f5ce3473 1156 /* Codes_SRS_IOTHUB_MQTT_TRANSPORT_07_026: [IoTHubTransportMqtt_DoWork shall do nothing if parameter handle and/or iotHubClientHandle is NULL.] */
AzureIoTClient 4:e472f5ce3473 1157 PMQTTTRANSPORT_HANDLE_DATA transportState = (PMQTTTRANSPORT_HANDLE_DATA)handle;
AzureIoTClient 4:e472f5ce3473 1158 if (transportState != NULL && iotHubClientHandle != NULL)
AzureIoTClient 4:e472f5ce3473 1159 {
AzureIoTClient 4:e472f5ce3473 1160 transportState->llClientHandle = iotHubClientHandle;
Azure.IoT Build 0:5e72a75c31b8 1161
AzureIoTClient 4:e472f5ce3473 1162 if (InitializeConnection(transportState) != 0)
AzureIoTClient 4:e472f5ce3473 1163 {
AzureIoTClient 4:e472f5ce3473 1164 // Don't want to flood the logs with failures here
AzureIoTClient 4:e472f5ce3473 1165 }
AzureIoTClient 4:e472f5ce3473 1166 else
AzureIoTClient 4:e472f5ce3473 1167 {
AzureIoTClient 4:e472f5ce3473 1168 if (transportState->currPacketState == CONNACK_TYPE || transportState->currPacketState == SUBSCRIBE_TYPE)
AzureIoTClient 4:e472f5ce3473 1169 {
AzureIoTClient 4:e472f5ce3473 1170 (void)SubscribeToMqttProtocol(transportState);
AzureIoTClient 4:e472f5ce3473 1171 }
AzureIoTClient 4:e472f5ce3473 1172 else if (transportState->currPacketState == SUBACK_TYPE)
AzureIoTClient 4:e472f5ce3473 1173 {
AzureIoTClient 4:e472f5ce3473 1174 // Publish can be called now
AzureIoTClient 4:e472f5ce3473 1175 transportState->currPacketState = PUBLISH_TYPE;
AzureIoTClient 4:e472f5ce3473 1176 }
AzureIoTClient 4:e472f5ce3473 1177 else if (transportState->currPacketState == PUBLISH_TYPE)
AzureIoTClient 4:e472f5ce3473 1178 {
AzureIoTClient 4:e472f5ce3473 1179 PDLIST_ENTRY currentListEntry = transportState->waitingForAck.Flink;
AzureIoTClient 4:e472f5ce3473 1180 while (currentListEntry != &transportState->waitingForAck)
AzureIoTClient 4:e472f5ce3473 1181 {
AzureIoTClient 4:e472f5ce3473 1182 MQTT_MESSAGE_DETAILS_LIST* mqttMsgEntry = containingRecord(currentListEntry, MQTT_MESSAGE_DETAILS_LIST, entry);
AzureIoTClient 4:e472f5ce3473 1183 DLIST_ENTRY nextListEntry;
AzureIoTClient 4:e472f5ce3473 1184 nextListEntry.Flink = currentListEntry->Flink;
Azure.IoT Build 0:5e72a75c31b8 1185
AzureIoTClient 4:e472f5ce3473 1186 uint64_t current_ms;
AzureIoTClient 4:e472f5ce3473 1187 (void)tickcounter_get_current_ms(g_msgTickCounter, &current_ms);
AzureIoTClient 4:e472f5ce3473 1188 /* Codes_SRS_IOTHUB_MQTT_TRANSPORT_07_033: [IoTHubTransportMqtt_DoWork shall iterate through the Waiting Acknowledge messages looking for any message that has been waiting longer than 2 min.]*/
AzureIoTClient 4:e472f5ce3473 1189 if (((current_ms - mqttMsgEntry->msgPublishTime) / 1000) > RESEND_TIMEOUT_VALUE_MIN)
AzureIoTClient 4:e472f5ce3473 1190 {
AzureIoTClient 4:e472f5ce3473 1191 /* Codes_SRS_IOTHUB_MQTT_TRANSPORT_07_034: [If IoTHubTransportMqtt_DoWork has resent the message two times then it shall fail the message] */
AzureIoTClient 4:e472f5ce3473 1192 if (mqttMsgEntry->retryCount >= MAX_SEND_RECOUNT_LIMIT)
AzureIoTClient 4:e472f5ce3473 1193 {
AzureIoTClient 4:e472f5ce3473 1194 (void)DList_RemoveEntryList(currentListEntry);
Azure.IoT Build 5:73603e7a6542 1195 sendMsgComplete(mqttMsgEntry->iotHubMessageEntry, transportState, IOTHUB_CLIENT_CONFIRMATION_MESSAGE_TIMEOUT);
AzureIoTClient 4:e472f5ce3473 1196 free(mqttMsgEntry);
AzureIoTClient 4:e472f5ce3473 1197 }
AzureIoTClient 4:e472f5ce3473 1198 else
AzureIoTClient 4:e472f5ce3473 1199 {
AzureIoTClient 4:e472f5ce3473 1200 size_t messageLength;
AzureIoTClient 4:e472f5ce3473 1201 const unsigned char* messagePayload = RetrieveMessagePayload(mqttMsgEntry->iotHubMessageEntry->messageHandle, &messageLength);
AzureIoTClient 4:e472f5ce3473 1202 if (messageLength == 0 || messagePayload == NULL)
AzureIoTClient 4:e472f5ce3473 1203 {
AzureIoTClient 4:e472f5ce3473 1204 LogError("Failure from creating Message IoTHubMessage_GetData");
AzureIoTClient 4:e472f5ce3473 1205 }
AzureIoTClient 4:e472f5ce3473 1206 else
AzureIoTClient 4:e472f5ce3473 1207 {
AzureIoTClient 4:e472f5ce3473 1208 if (publishMqttMessage(transportState, mqttMsgEntry, messagePayload, messageLength) != 0)
AzureIoTClient 4:e472f5ce3473 1209 {
AzureIoTClient 4:e472f5ce3473 1210 (void)DList_RemoveEntryList(currentListEntry);
Azure.IoT Build 5:73603e7a6542 1211 sendMsgComplete(mqttMsgEntry->iotHubMessageEntry, transportState, IOTHUB_CLIENT_CONFIRMATION_ERROR);
AzureIoTClient 4:e472f5ce3473 1212 free(mqttMsgEntry);
AzureIoTClient 4:e472f5ce3473 1213 }
AzureIoTClient 4:e472f5ce3473 1214 }
AzureIoTClient 4:e472f5ce3473 1215 }
AzureIoTClient 4:e472f5ce3473 1216 }
AzureIoTClient 4:e472f5ce3473 1217 currentListEntry = nextListEntry.Flink;
AzureIoTClient 4:e472f5ce3473 1218 }
Azure.IoT Build 0:5e72a75c31b8 1219
AzureIoTClient 4:e472f5ce3473 1220 currentListEntry = transportState->waitingToSend->Flink;
AzureIoTClient 4:e472f5ce3473 1221 /* Codes_SRS_IOTHUB_MQTT_TRANSPORT_07_027: [IoTHubTransportMqtt_DoWork shall inspect the “waitingToSend” DLIST passed in config structure.] */
AzureIoTClient 4:e472f5ce3473 1222 while (currentListEntry != transportState->waitingToSend)
AzureIoTClient 4:e472f5ce3473 1223 {
AzureIoTClient 4:e472f5ce3473 1224 IOTHUB_MESSAGE_LIST* iothubMsgList = containingRecord(currentListEntry, IOTHUB_MESSAGE_LIST, entry);
AzureIoTClient 4:e472f5ce3473 1225 DLIST_ENTRY savedFromCurrentListEntry;
AzureIoTClient 4:e472f5ce3473 1226 savedFromCurrentListEntry.Flink = currentListEntry->Flink;
Azure.IoT Build 0:5e72a75c31b8 1227
AzureIoTClient 4:e472f5ce3473 1228 /* Codes_SRS_IOTHUB_MQTT_TRANSPORT_07_027: [IoTHubTransportMqtt_DoWork shall inspect the “waitingToSend” DLIST passed in config structure.] */
AzureIoTClient 4:e472f5ce3473 1229 size_t messageLength;
AzureIoTClient 4:e472f5ce3473 1230 const unsigned char* messagePayload = RetrieveMessagePayload(iothubMsgList->messageHandle, &messageLength);
AzureIoTClient 4:e472f5ce3473 1231 if (messageLength == 0 || messagePayload == NULL)
AzureIoTClient 4:e472f5ce3473 1232 {
AzureIoTClient 4:e472f5ce3473 1233 LogError("Failure result from IoTHubMessage_GetData");
AzureIoTClient 4:e472f5ce3473 1234 }
AzureIoTClient 4:e472f5ce3473 1235 else
AzureIoTClient 4:e472f5ce3473 1236 {
AzureIoTClient 4:e472f5ce3473 1237 /* Codes_SRS_IOTHUB_MQTT_TRANSPORT_07_029: [IoTHubTransportMqtt_DoWork shall create a MQTT_MESSAGE_HANDLE and pass this to a call to mqtt_client_publish.] */
AzureIoTClient 4:e472f5ce3473 1238 MQTT_MESSAGE_DETAILS_LIST* mqttMsgEntry = (MQTT_MESSAGE_DETAILS_LIST*)malloc(sizeof(MQTT_MESSAGE_DETAILS_LIST));
AzureIoTClient 4:e472f5ce3473 1239 if (mqttMsgEntry == NULL)
AzureIoTClient 4:e472f5ce3473 1240 {
AzureIoTClient 4:e472f5ce3473 1241 LogError("Allocation Error: Failure allocating MQTT Message Detail List.");
AzureIoTClient 4:e472f5ce3473 1242 }
AzureIoTClient 4:e472f5ce3473 1243 else
AzureIoTClient 4:e472f5ce3473 1244 {
AzureIoTClient 4:e472f5ce3473 1245 mqttMsgEntry->retryCount = 0;
AzureIoTClient 4:e472f5ce3473 1246 mqttMsgEntry->iotHubMessageEntry = iothubMsgList;
AzureIoTClient 4:e472f5ce3473 1247 if (publishMqttMessage(transportState, mqttMsgEntry, messagePayload, messageLength) != 0)
AzureIoTClient 4:e472f5ce3473 1248 {
AzureIoTClient 4:e472f5ce3473 1249 (void)(DList_RemoveEntryList(currentListEntry));
Azure.IoT Build 5:73603e7a6542 1250 sendMsgComplete(iothubMsgList, transportState, IOTHUB_CLIENT_CONFIRMATION_ERROR);
AzureIoTClient 4:e472f5ce3473 1251 free(mqttMsgEntry);
AzureIoTClient 4:e472f5ce3473 1252 }
AzureIoTClient 4:e472f5ce3473 1253 else
AzureIoTClient 4:e472f5ce3473 1254 {
AzureIoTClient 4:e472f5ce3473 1255 (void)(DList_RemoveEntryList(currentListEntry));
AzureIoTClient 4:e472f5ce3473 1256 DList_InsertTailList(&(transportState->waitingForAck), &(mqttMsgEntry->entry));
AzureIoTClient 4:e472f5ce3473 1257 }
AzureIoTClient 4:e472f5ce3473 1258 }
AzureIoTClient 4:e472f5ce3473 1259 }
AzureIoTClient 4:e472f5ce3473 1260 currentListEntry = savedFromCurrentListEntry.Flink;
AzureIoTClient 4:e472f5ce3473 1261 }
AzureIoTClient 4:e472f5ce3473 1262 }
AzureIoTClient 4:e472f5ce3473 1263 /* Codes_SRS_IOTHUB_MQTT_TRANSPORT_07_030: [IoTHubTransportMqtt_DoWork shall call mqtt_client_dowork everytime it is called if it is connected.] */
AzureIoTClient 4:e472f5ce3473 1264 mqtt_client_dowork(transportState->mqttClient);
AzureIoTClient 4:e472f5ce3473 1265 }
AzureIoTClient 4:e472f5ce3473 1266 }
Azure.IoT Build 0:5e72a75c31b8 1267 }
Azure.IoT Build 0:5e72a75c31b8 1268
AzureIoTClient 4:e472f5ce3473 1269 static IOTHUB_CLIENT_RESULT IoTHubTransportMqtt_GetSendStatus(IOTHUB_DEVICE_HANDLE handle, IOTHUB_CLIENT_STATUS *iotHubClientStatus)
Azure.IoT Build 0:5e72a75c31b8 1270 {
AzureIoTClient 4:e472f5ce3473 1271 IOTHUB_CLIENT_RESULT result;
Azure.IoT Build 0:5e72a75c31b8 1272
AzureIoTClient 4:e472f5ce3473 1273 if (handle == NULL || iotHubClientStatus == NULL)
AzureIoTClient 4:e472f5ce3473 1274 {
AzureIoTClient 4:e472f5ce3473 1275 /* Codes_SRS_IOTHUB_MQTT_TRANSPORT_07_023: [IoTHubTransportMqtt_GetSendStatus shall return IOTHUB_CLIENT_INVALID_ARG if called with NULL parameter.] */
AzureIoTClient 4:e472f5ce3473 1276 LogError("invalid arument.");
AzureIoTClient 4:e472f5ce3473 1277 result = IOTHUB_CLIENT_INVALID_ARG;
AzureIoTClient 4:e472f5ce3473 1278 }
AzureIoTClient 4:e472f5ce3473 1279 else
AzureIoTClient 4:e472f5ce3473 1280 {
AzureIoTClient 4:e472f5ce3473 1281 MQTTTRANSPORT_HANDLE_DATA* handleData = (MQTTTRANSPORT_HANDLE_DATA*)handle;
AzureIoTClient 4:e472f5ce3473 1282 if (!DList_IsListEmpty(handleData->waitingToSend) || !DList_IsListEmpty(&(handleData->waitingForAck)))
AzureIoTClient 4:e472f5ce3473 1283 {
AzureIoTClient 4:e472f5ce3473 1284 /* Codes_SRS_IOTHUB_MQTT_TRANSPORT_07_025: [IoTHubTransportMqtt_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 4:e472f5ce3473 1285 *iotHubClientStatus = IOTHUB_CLIENT_SEND_STATUS_BUSY;
AzureIoTClient 4:e472f5ce3473 1286 }
AzureIoTClient 4:e472f5ce3473 1287 else
AzureIoTClient 4:e472f5ce3473 1288 {
AzureIoTClient 4:e472f5ce3473 1289 /* Codes_SRS_IOTHUB_MQTT_TRANSPORT_07_024: [IoTHubTransportMqtt_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 4:e472f5ce3473 1290 *iotHubClientStatus = IOTHUB_CLIENT_SEND_STATUS_IDLE;
AzureIoTClient 4:e472f5ce3473 1291 }
AzureIoTClient 4:e472f5ce3473 1292 result = IOTHUB_CLIENT_OK;
AzureIoTClient 4:e472f5ce3473 1293 }
AzureIoTClient 4:e472f5ce3473 1294 return result;
Azure.IoT Build 0:5e72a75c31b8 1295 }
Azure.IoT Build 0:5e72a75c31b8 1296
AzureIoTClient 4:e472f5ce3473 1297 static IOTHUB_CLIENT_RESULT IoTHubTransportMqtt_SetOption(TRANSPORT_LL_HANDLE handle, const char* option, const void* value)
Azure.IoT Build 0:5e72a75c31b8 1298 {
AzureIoTClient 4:e472f5ce3473 1299 /* Codes_SRS_IOTHUB_MQTT_TRANSPORT_07_021: [If any parameter is NULL then IoTHubTransportMqtt_SetOption shall return IOTHUB_CLIENT_INVALID_ARG.] */
AzureIoTClient 4:e472f5ce3473 1300 IOTHUB_CLIENT_RESULT result;
AzureIoTClient 4:e472f5ce3473 1301 if (
AzureIoTClient 4:e472f5ce3473 1302 (handle == NULL) ||
AzureIoTClient 4:e472f5ce3473 1303 (option == NULL) ||
AzureIoTClient 4:e472f5ce3473 1304 (value == NULL)
AzureIoTClient 4:e472f5ce3473 1305 )
AzureIoTClient 4:e472f5ce3473 1306 {
AzureIoTClient 4:e472f5ce3473 1307 result = IOTHUB_CLIENT_INVALID_ARG;
AzureIoTClient 4:e472f5ce3473 1308 LogError("invalid parameter (NULL) passed to clientTransportAMQP_SetOption.");
AzureIoTClient 4:e472f5ce3473 1309 }
AzureIoTClient 4:e472f5ce3473 1310 else
AzureIoTClient 4:e472f5ce3473 1311 {
AzureIoTClient 4:e472f5ce3473 1312 MQTTTRANSPORT_HANDLE_DATA* transportState = (MQTTTRANSPORT_HANDLE_DATA*)handle;
AzureIoTClient 4:e472f5ce3473 1313 /* 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 8:418a3b812584 1314 if (strcmp(OPTION_LOG_TRACE, option) == 0)
AzureIoTClient 4:e472f5ce3473 1315 {
AzureIoTClient 4:e472f5ce3473 1316 bool* traceVal = (bool*)value;
AzureIoTClient 4:e472f5ce3473 1317 mqtt_client_set_trace(transportState->mqttClient, *traceVal, *traceVal);
AzureIoTClient 4:e472f5ce3473 1318 result = IOTHUB_CLIENT_OK;
AzureIoTClient 4:e472f5ce3473 1319 }
AzureIoTClient 8:418a3b812584 1320 else if (strcmp(OPTION_KEEP_ALIVE, option) == 0)
AzureIoTClient 4:e472f5ce3473 1321 {
AzureIoTClient 4:e472f5ce3473 1322 /* 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 4:e472f5ce3473 1323 int* keepAliveOption = (int*)value;
AzureIoTClient 4:e472f5ce3473 1324 /* 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 IoTHubTransportMqtt_SetOption shall do nothing.] */
AzureIoTClient 4:e472f5ce3473 1325 if (*keepAliveOption != transportState->keepAliveValue)
AzureIoTClient 4:e472f5ce3473 1326 {
AzureIoTClient 6:16875b609849 1327 transportState->keepAliveValue = (uint16_t)(*keepAliveOption);
AzureIoTClient 4:e472f5ce3473 1328 if (transportState->connected)
AzureIoTClient 4:e472f5ce3473 1329 {
AzureIoTClient 4:e472f5ce3473 1330 /* Codes_SRS_IOTHUB_MQTT_TRANSPORT_07_038: [If the client is connected when the keepalive is set then IoTHubTransportMqtt_SetOption shall disconnect and reconnect with the specified keepalive value.] */
AzureIoTClient 4:e472f5ce3473 1331 DisconnectFromClient(transportState);
AzureIoTClient 4:e472f5ce3473 1332 }
AzureIoTClient 4:e472f5ce3473 1333 }
AzureIoTClient 4:e472f5ce3473 1334 result = IOTHUB_CLIENT_OK;
AzureIoTClient 4:e472f5ce3473 1335 }
AzureIoTClient 7:7fdd306e6224 1336 /* 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 8:418a3b812584 1337 else if ((strcmp(OPTION_X509_CERT, option) == 0) && (transportState->transport_creds.credential_type != X509))
AzureIoTClient 7:7fdd306e6224 1338 {
AzureIoTClient 7:7fdd306e6224 1339 LogError("x509certificate specified, but authentication method is not x509");
AzureIoTClient 7:7fdd306e6224 1340 result = IOTHUB_CLIENT_INVALID_ARG;
AzureIoTClient 7:7fdd306e6224 1341 }
AzureIoTClient 7:7fdd306e6224 1342 /* 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 8:418a3b812584 1343 else if ((strcmp(OPTION_X509_PRIVATE_KEY, option) == 0) && (transportState->transport_creds.credential_type != X509))
AzureIoTClient 7:7fdd306e6224 1344 {
AzureIoTClient 7:7fdd306e6224 1345 LogError("x509privatekey specified, but authentication method is not x509");
AzureIoTClient 7:7fdd306e6224 1346 result = IOTHUB_CLIENT_INVALID_ARG;
AzureIoTClient 7:7fdd306e6224 1347 }
AzureIoTClient 4:e472f5ce3473 1348 else
AzureIoTClient 4:e472f5ce3473 1349 {
AzureIoTClient 4:e472f5ce3473 1350 /* Codes_SRS_IOTHUB_MQTT_TRANSPORT_07_032: [IoTHubTransportMqtt_SetOption shall pass down the option to xio_setoption if the option parameter is not a known option string for the MQTT transport.] */
AzureIoTClient 4:e472f5ce3473 1351 if (GetTransportProviderIfNecessary(transportState) == 0)
AzureIoTClient 4:e472f5ce3473 1352 {
AzureIoTClient 4:e472f5ce3473 1353 if (xio_setoption(transportState->xioTransport, option, value) == 0)
AzureIoTClient 4:e472f5ce3473 1354 {
AzureIoTClient 4:e472f5ce3473 1355 result = IOTHUB_CLIENT_OK;
AzureIoTClient 4:e472f5ce3473 1356 }
AzureIoTClient 4:e472f5ce3473 1357 else
AzureIoTClient 4:e472f5ce3473 1358 {
AzureIoTClient 4:e472f5ce3473 1359 /* Codes_SRS_IOTHUB_MQTT_TRANSPORT_07_132: [IoTHubTransportMqtt_SetOption shall return IOTHUB_CLIENT_INVALID_ARG xio_setoption fails] */
AzureIoTClient 4:e472f5ce3473 1360 result = IOTHUB_CLIENT_INVALID_ARG;
AzureIoTClient 4:e472f5ce3473 1361 }
AzureIoTClient 4:e472f5ce3473 1362 }
AzureIoTClient 4:e472f5ce3473 1363 else
AzureIoTClient 4:e472f5ce3473 1364 {
AzureIoTClient 4:e472f5ce3473 1365 result = IOTHUB_CLIENT_ERROR;
AzureIoTClient 4:e472f5ce3473 1366 }
AzureIoTClient 4:e472f5ce3473 1367 }
AzureIoTClient 4:e472f5ce3473 1368 }
AzureIoTClient 4:e472f5ce3473 1369 return result;
Azure.IoT Build 0:5e72a75c31b8 1370 }
Azure.IoT Build 0:5e72a75c31b8 1371
AzureIoTClient 4:e472f5ce3473 1372 static IOTHUB_DEVICE_HANDLE IoTHubTransportMqtt_Register(TRANSPORT_LL_HANDLE handle, const IOTHUB_DEVICE_CONFIG* device, IOTHUB_CLIENT_LL_HANDLE iotHubClientHandle, PDLIST_ENTRY waitingToSend)
Azure.IoT Build 0:5e72a75c31b8 1373 {
AzureIoTClient 7:7fdd306e6224 1374 IOTHUB_DEVICE_HANDLE result = NULL;
AzureIoTClient 6:16875b609849 1375 (void)iotHubClientHandle;
AzureIoTClient 6:16875b609849 1376
AzureIoTClient 4:e472f5ce3473 1377 // Codes_SRS_IOTHUB_MQTT_TRANSPORT_17_001: [ IoTHubTransportMqtt_Register shall return NULL if the TRANSPORT_LL_HANDLE is NULL.]
AzureIoTClient 4:e472f5ce3473 1378 // Codes_SRS_IOTHUB_MQTT_TRANSPORT_17_002: [ IoTHubTransportMqtt_Register shall return NULL if device or waitingToSend are NULL.]
AzureIoTClient 4:e472f5ce3473 1379 if ((handle == NULL) || (device == NULL) || (waitingToSend == NULL))
AzureIoTClient 4:e472f5ce3473 1380 {
AzureIoTClient 3:40f482ed0be8 1381 LogError("IoTHubTransportMqtt_Register: handle, device or waitingToSend is NULL.");
Azure.IoT Build 0:5e72a75c31b8 1382 result = NULL;
AzureIoTClient 4:e472f5ce3473 1383 }
AzureIoTClient 4:e472f5ce3473 1384 else
AzureIoTClient 4:e472f5ce3473 1385 {
AzureIoTClient 4:e472f5ce3473 1386 MQTTTRANSPORT_HANDLE_DATA* transportState = (MQTTTRANSPORT_HANDLE_DATA*)handle;
Azure.IoT Build 0:5e72a75c31b8 1387
AzureIoTClient 4:e472f5ce3473 1388 // Codes_SRS_IOTHUB_MQTT_TRANSPORT_03_001: [ IoTHubTransportMqtt_Register shall return NULL if deviceId, or both deviceKey and deviceSasToken are NULL.]
AzureIoTClient 4:e472f5ce3473 1389 if (device->deviceId == NULL)
AzureIoTClient 4:e472f5ce3473 1390 {
AzureIoTClient 3:40f482ed0be8 1391 LogError("IoTHubTransportMqtt_Register: deviceId is NULL.");
AzureIoTClient 3:40f482ed0be8 1392 result = NULL;
AzureIoTClient 4:e472f5ce3473 1393 }
AzureIoTClient 4:e472f5ce3473 1394 // Codes_SRS_IOTHUB_MQTT_TRANSPORT_03_002: [ IoTHubTransportMqtt_Register shall return NULL if both deviceKey and deviceSasToken are provided.]
AzureIoTClient 4:e472f5ce3473 1395 else if ((device->deviceKey != NULL) && (device->deviceSasToken != NULL))
AzureIoTClient 4:e472f5ce3473 1396 {
AzureIoTClient 3:40f482ed0be8 1397 LogError("IoTHubTransportMqtt_Register: Both deviceKey and deviceSasToken are defined. Only one can be used.");
Azure.IoT Build 0:5e72a75c31b8 1398 result = NULL;
AzureIoTClient 4:e472f5ce3473 1399 }
AzureIoTClient 4:e472f5ce3473 1400 else
AzureIoTClient 4:e472f5ce3473 1401 {
AzureIoTClient 4:e472f5ce3473 1402 // Codes_SRS_IOTHUB_MQTT_TRANSPORT_17_003: [ IoTHubTransportMqtt_Register shall return NULL if deviceId or deviceKey do not match the deviceId and deviceKey passed in during IoTHubTransportMqtt_Create.]
AzureIoTClient 4:e472f5ce3473 1403 if (strcmp(STRING_c_str(transportState->device_id), device->deviceId) != 0)
AzureIoTClient 4:e472f5ce3473 1404 {
AzureIoTClient 3:40f482ed0be8 1405 LogError("IoTHubTransportMqtt_Register: deviceId does not match.");
AzureIoTClient 3:40f482ed0be8 1406 result = NULL;
AzureIoTClient 4:e472f5ce3473 1407 }
AzureIoTClient 7:7fdd306e6224 1408 else if ( (transportState->transport_creds.credential_type == DEVICE_KEY) && (strcmp(STRING_c_str(transportState->transport_creds.CREDENTIAL_VALUE.deviceKey), device->deviceKey) != 0))
AzureIoTClient 4:e472f5ce3473 1409 {
AzureIoTClient 3:40f482ed0be8 1410 LogError("IoTHubTransportMqtt_Register: deviceKey does not match.");
Azure.IoT Build 0:5e72a75c31b8 1411 result = NULL;
AzureIoTClient 4:e472f5ce3473 1412 }
AzureIoTClient 4:e472f5ce3473 1413 else
AzureIoTClient 4:e472f5ce3473 1414 {
AzureIoTClient 4:e472f5ce3473 1415 if (transportState->isRegistered == true)
AzureIoTClient 4:e472f5ce3473 1416 {
AzureIoTClient 4:e472f5ce3473 1417 LogError("Transport already has device registered by id: [%s]", device->deviceId);
AzureIoTClient 4:e472f5ce3473 1418 result = NULL;
AzureIoTClient 4:e472f5ce3473 1419 }
AzureIoTClient 4:e472f5ce3473 1420 else
AzureIoTClient 4:e472f5ce3473 1421 {
AzureIoTClient 4:e472f5ce3473 1422 transportState->isRegistered = true;
AzureIoTClient 4:e472f5ce3473 1423 // Codes_SRS_IOTHUB_MQTT_TRANSPORT_17_004: [ IoTHubTransportMqtt_Register shall return the TRANSPORT_LL_HANDLE as the IOTHUB_DEVICE_HANDLE. ]
AzureIoTClient 4:e472f5ce3473 1424 result = (IOTHUB_DEVICE_HANDLE)handle;
AzureIoTClient 4:e472f5ce3473 1425 }
AzureIoTClient 4:e472f5ce3473 1426 }
AzureIoTClient 4:e472f5ce3473 1427 }
AzureIoTClient 4:e472f5ce3473 1428 }
Azure.IoT Build 0:5e72a75c31b8 1429
AzureIoTClient 4:e472f5ce3473 1430 return result;
Azure.IoT Build 0:5e72a75c31b8 1431 }
Azure.IoT Build 0:5e72a75c31b8 1432
Azure.IoT Build 0:5e72a75c31b8 1433 // Codes_SRS_IOTHUB_MQTT_TRANSPORT_17_005: [ IoTHubTransportMqtt_Unregister shall return. ]
AzureIoTClient 4:e472f5ce3473 1434 static void IoTHubTransportMqtt_Unregister(IOTHUB_DEVICE_HANDLE deviceHandle)
Azure.IoT Build 0:5e72a75c31b8 1435 {
AzureIoTClient 4:e472f5ce3473 1436 if (deviceHandle != NULL)
AzureIoTClient 4:e472f5ce3473 1437 {
AzureIoTClient 4:e472f5ce3473 1438 MQTTTRANSPORT_HANDLE_DATA* transportState = (MQTTTRANSPORT_HANDLE_DATA*)deviceHandle;
Azure.IoT Build 0:5e72a75c31b8 1439
AzureIoTClient 4:e472f5ce3473 1440 transportState->isRegistered = false;
AzureIoTClient 4:e472f5ce3473 1441 }
Azure.IoT Build 0:5e72a75c31b8 1442 }
Azure.IoT Build 0:5e72a75c31b8 1443
AzureIoTClient 4:e472f5ce3473 1444 static STRING_HANDLE IoTHubTransportMqtt_GetHostname(TRANSPORT_LL_HANDLE handle)
AzureIoTClient 4:e472f5ce3473 1445 {
AzureIoTClient 4:e472f5ce3473 1446 STRING_HANDLE result;
AzureIoTClient 4:e472f5ce3473 1447 /*Codes_SRS_IOTHUB_MQTT_TRANSPORT_02_001: [ If handle is NULL then IoTHubTransportMqtt_GetHostname shall fail and return NULL. ]*/
AzureIoTClient 4:e472f5ce3473 1448 if (handle == NULL)
AzureIoTClient 4:e472f5ce3473 1449 {
AzureIoTClient 4:e472f5ce3473 1450 result = NULL;
AzureIoTClient 4:e472f5ce3473 1451 }
AzureIoTClient 4:e472f5ce3473 1452 else
AzureIoTClient 4:e472f5ce3473 1453 {
AzureIoTClient 4:e472f5ce3473 1454 /*Codes_SRS_IOTHUB_MQTT_TRANSPORT_02_002: [ Otherwise IoTHubTransportMqtt_GetHostname shall return a non-NULL STRING_HANDLE containg the hostname. ]*/
AzureIoTClient 4:e472f5ce3473 1455 result = ((MQTTTRANSPORT_HANDLE_DATA*)handle)->hostAddress;
AzureIoTClient 4:e472f5ce3473 1456 }
AzureIoTClient 4:e472f5ce3473 1457 return result;
AzureIoTClient 4:e472f5ce3473 1458 }
AzureIoTClient 4:e472f5ce3473 1459
AzureIoTClient 4:e472f5ce3473 1460 static TRANSPORT_PROVIDER myfunc = {
AzureIoTClient 4:e472f5ce3473 1461 IoTHubTransportMqtt_GetHostname,
AzureIoTClient 4:e472f5ce3473 1462 IoTHubTransportMqtt_SetOption,
AzureIoTClient 4:e472f5ce3473 1463 IoTHubTransportMqtt_Create,
AzureIoTClient 4:e472f5ce3473 1464 IoTHubTransportMqtt_Destroy,
AzureIoTClient 4:e472f5ce3473 1465 IoTHubTransportMqtt_Register,
AzureIoTClient 4:e472f5ce3473 1466 IoTHubTransportMqtt_Unregister,
AzureIoTClient 4:e472f5ce3473 1467 IoTHubTransportMqtt_Subscribe,
AzureIoTClient 4:e472f5ce3473 1468 IoTHubTransportMqtt_Unsubscribe,
AzureIoTClient 4:e472f5ce3473 1469 IoTHubTransportMqtt_DoWork,
AzureIoTClient 4:e472f5ce3473 1470 IoTHubTransportMqtt_GetSendStatus
Azure.IoT Build 0:5e72a75c31b8 1471 };
Azure.IoT Build 0:5e72a75c31b8 1472
Azure.IoT Build 0:5e72a75c31b8 1473 /* Codes_SRS_IOTHUB_MQTT_TRANSPORT_07_022: [This function shall return a pointer to a structure of type TRANSPORT_PROVIDER having the following values for it’s fields: IoTHubTransport_Create = IoTHubTransportMqtt_Create
Azure.IoT Build 0:5e72a75c31b8 1474 IoTHubTransport_Destroy = IoTHubTransportMqtt_Destroy
Azure.IoT Build 0:5e72a75c31b8 1475 IoTHubTransport_Subscribe = IoTHubTransportMqtt_Subscribe
Azure.IoT Build 0:5e72a75c31b8 1476 IoTHubTransport_Unsubscribe = IoTHubTransportMqtt_Unsubscribe
Azure.IoT Build 0:5e72a75c31b8 1477 IoTHubTransport_DoWork = IoTHubTransportMqtt_DoWork
Azure.IoT Build 0:5e72a75c31b8 1478 IoTHubTransport_SetOption = IoTHubTransportMqtt_SetOption] */
AzureIoTClient 4:e472f5ce3473 1479 extern const TRANSPORT_PROVIDER* MQTT_Protocol(void)
Azure.IoT Build 0:5e72a75c31b8 1480 {
AzureIoTClient 4:e472f5ce3473 1481 return &myfunc;
Azure.IoT Build 0:5e72a75c31b8 1482 }