Microsoft Azure IoTHub client MQTT transport

Dependents:   STM32F746_iothub_client_sample_mqtt FXOS8700CQ_To_Azure_IoT f767zi_mqtt FXOS8700CQ_To_Azure_IoT ... more

Committer:
AzureIoTClient
Date:
Mon Jul 18 16:44:47 2016 -0700
Revision:
6:16875b609849
Parent:
5:73603e7a6542
Child:
7:7fdd306e6224
1.0.11

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