A small footprint MQTT library

Dependents:   STM32F746_iothub_client_sample_mqtt FXOS8700CQ_To_Azure_IoT f767zi_mqtt FXOS8700CQ_To_Azure_IoT ... more

Committer:
AzureIoTClient
Date:
Sat Jan 28 09:34:52 2017 -0800
Revision:
13:3c202001e4ba
Parent:
12:30b08cda82fd
Child:
14:4b5b4dccfc8b
1.1.6

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Azure.IoT Build 0:ef4901974abc 1 // Copyright (c) Microsoft. All rights reserved.
Azure.IoT Build 0:ef4901974abc 2 // Licensed under the MIT license. See LICENSE file in the project root for full license information.
Azure.IoT Build 0:ef4901974abc 3
Azure.IoT Build 0:ef4901974abc 4 #include <stdlib.h>
Azure.IoT Build 0:ef4901974abc 5 #include "azure_c_shared_utility/gballoc.h"
Azure.IoT Build 0:ef4901974abc 6 #include "azure_c_shared_utility/platform.h"
Azure.IoT Build 0:ef4901974abc 7 #include "azure_c_shared_utility/tickcounter.h"
Azure.IoT Build 0:ef4901974abc 8 #include "azure_c_shared_utility/crt_abstractions.h"
Azure.IoT Build 4:e7167dabd6e4 9 #include "azure_c_shared_utility/xlogging.h"
AzureIoTClient 8:83bb166aba73 10 #include "azure_c_shared_utility/strings.h"
AzureIoTClient 8:83bb166aba73 11 #include "azure_c_shared_utility/agenttime.h"
AzureIoTClient 9:37d14c31ff6e 12 #include "azure_c_shared_utility/threadapi.h"
Azure.IoT Build 0:ef4901974abc 13
Azure.IoT Build 0:ef4901974abc 14 #include "azure_umqtt_c/mqtt_client.h"
Azure.IoT Build 0:ef4901974abc 15 #include "azure_umqtt_c/mqtt_codec.h"
AzureIoTClient 8:83bb166aba73 16 #include <inttypes.h>
Azure.IoT Build 0:ef4901974abc 17
Azure.IoT Build 0:ef4901974abc 18 #define KEEP_ALIVE_BUFFER_SEC 10
Azure.IoT Build 0:ef4901974abc 19 #define VARIABLE_HEADER_OFFSET 2
Azure.IoT Build 0:ef4901974abc 20 #define RETAIN_FLAG_MASK 0x1
Azure.IoT Build 0:ef4901974abc 21 #define QOS_LEAST_ONCE_FLAG_MASK 0x2
Azure.IoT Build 0:ef4901974abc 22 #define QOS_EXACTLY_ONCE_FLAG_MASK 0x4
Azure.IoT Build 0:ef4901974abc 23 #define DUPLICATE_FLAG_MASK 0x8
Azure.IoT Build 0:ef4901974abc 24 #define CONNECT_PACKET_MASK 0xf0
AzureIoTClient 1:8dba42ff9701 25 #define TIME_MAX_BUFFER 16
AzureIoTClient 8:83bb166aba73 26 #define DEFAULT_MAX_PING_RESPONSE_TIME 80 // % of time to send pings
AzureIoTClient 9:37d14c31ff6e 27 #define MAX_CLOSE_RETRIES 10
Azure.IoT Build 0:ef4901974abc 28
AzureIoTClient 8:83bb166aba73 29 static const char* TRUE_CONST = "true";
AzureIoTClient 8:83bb166aba73 30 static const char* FALSE_CONST = "false";
AzureIoTClient 8:83bb166aba73 31
AzureIoTClient 8:83bb166aba73 32 DEFINE_ENUM_STRINGS(QOS_VALUE, QOS_VALUE_VALUES);
Azure.IoT Build 0:ef4901974abc 33
Azure.IoT Build 0:ef4901974abc 34 typedef struct MQTT_CLIENT_TAG
Azure.IoT Build 0:ef4901974abc 35 {
Azure.IoT Build 0:ef4901974abc 36 XIO_HANDLE xioHandle;
Azure.IoT Build 0:ef4901974abc 37 MQTTCODEC_HANDLE codec_handle;
Azure.IoT Build 0:ef4901974abc 38 CONTROL_PACKET_TYPE packetState;
Azure.IoT Build 0:ef4901974abc 39 TICK_COUNTER_HANDLE packetTickCntr;
Azure.IoT.Build 10:2ab268507775 40 tickcounter_ms_t packetSendTimeMs;
Azure.IoT Build 0:ef4901974abc 41 ON_MQTT_OPERATION_CALLBACK fnOperationCallback;
Azure.IoT Build 0:ef4901974abc 42 ON_MQTT_MESSAGE_RECV_CALLBACK fnMessageRecv;
Azure.IoT Build 0:ef4901974abc 43 void* ctx;
AzureIoTClient 9:37d14c31ff6e 44 ON_MQTT_ERROR_CALLBACK fnOnErrorCallBack;
AzureIoTClient 9:37d14c31ff6e 45 void* errorCBCtx;
Azure.IoT Build 0:ef4901974abc 46 QOS_VALUE qosValue;
Azure.IoT Build 0:ef4901974abc 47 uint16_t keepAliveInterval;
Azure.IoT Build 0:ef4901974abc 48 MQTT_CLIENT_OPTIONS mqttOptions;
Azure.IoT Build 0:ef4901974abc 49 bool clientConnected;
Azure.IoT Build 0:ef4901974abc 50 bool socketConnected;
Azure.IoT Build 0:ef4901974abc 51 bool logTrace;
Azure.IoT Build 0:ef4901974abc 52 bool rawBytesTrace;
Azure.IoT.Build 10:2ab268507775 53 tickcounter_ms_t timeSincePing;
AzureIoTClient 3:9b4e7158ca0d 54 uint16_t maxPingRespTime;
Azure.IoT Build 0:ef4901974abc 55 } MQTT_CLIENT;
Azure.IoT Build 0:ef4901974abc 56
AzureIoTClient 9:37d14c31ff6e 57 static void on_connection_closed(void* context)
AzureIoTClient 9:37d14c31ff6e 58 {
AzureIoTClient 9:37d14c31ff6e 59 size_t* close_complete = (size_t*)context;
AzureIoTClient 9:37d14c31ff6e 60 *close_complete = 1;
AzureIoTClient 9:37d14c31ff6e 61 }
AzureIoTClient 9:37d14c31ff6e 62
AzureIoTClient 9:37d14c31ff6e 63 static void close_connection(MQTT_CLIENT* mqtt_client)
AzureIoTClient 9:37d14c31ff6e 64 {
AzureIoTClient 9:37d14c31ff6e 65 size_t close_complete = 0;
AzureIoTClient 9:37d14c31ff6e 66 (void)xio_close(mqtt_client->xioHandle, on_connection_closed, &close_complete);
AzureIoTClient 9:37d14c31ff6e 67
AzureIoTClient 9:37d14c31ff6e 68 size_t counter = 0;
AzureIoTClient 9:37d14c31ff6e 69 do
AzureIoTClient 9:37d14c31ff6e 70 {
AzureIoTClient 9:37d14c31ff6e 71 xio_dowork(mqtt_client->xioHandle);
AzureIoTClient 9:37d14c31ff6e 72 counter++;
AzureIoTClient 9:37d14c31ff6e 73 ThreadAPI_Sleep(10);
AzureIoTClient 9:37d14c31ff6e 74 } while (close_complete == 0 && counter < MAX_CLOSE_RETRIES);
AzureIoTClient 9:37d14c31ff6e 75 mqtt_client->socketConnected = false;
AzureIoTClient 9:37d14c31ff6e 76 mqtt_client->clientConnected = false;
AzureIoTClient 9:37d14c31ff6e 77 }
AzureIoTClient 9:37d14c31ff6e 78
AzureIoTClient 9:37d14c31ff6e 79 static void set_error_callback(MQTT_CLIENT* mqtt_client, MQTT_CLIENT_EVENT_ERROR error_type)
AzureIoTClient 9:37d14c31ff6e 80 {
AzureIoTClient 9:37d14c31ff6e 81 if (mqtt_client->fnOnErrorCallBack)
AzureIoTClient 9:37d14c31ff6e 82 {
AzureIoTClient 9:37d14c31ff6e 83 mqtt_client->fnOnErrorCallBack(mqtt_client, error_type, mqtt_client->errorCBCtx);
AzureIoTClient 9:37d14c31ff6e 84 }
AzureIoTClient 9:37d14c31ff6e 85 close_connection(mqtt_client);
AzureIoTClient 9:37d14c31ff6e 86 }
AzureIoTClient 9:37d14c31ff6e 87
AzureIoTClient 8:83bb166aba73 88 static STRING_HANDLE construct_trace_log_handle(MQTT_CLIENT* mqtt_client)
AzureIoTClient 8:83bb166aba73 89 {
AzureIoTClient 8:83bb166aba73 90 STRING_HANDLE trace_log;
AzureIoTClient 8:83bb166aba73 91 if (mqtt_client->logTrace)
AzureIoTClient 8:83bb166aba73 92 {
AzureIoTClient 8:83bb166aba73 93 trace_log = STRING_new();
AzureIoTClient 8:83bb166aba73 94 }
AzureIoTClient 8:83bb166aba73 95 else
AzureIoTClient 8:83bb166aba73 96 {
AzureIoTClient 8:83bb166aba73 97 trace_log = NULL;
AzureIoTClient 8:83bb166aba73 98 }
AzureIoTClient 8:83bb166aba73 99 return trace_log;
AzureIoTClient 8:83bb166aba73 100 }
AzureIoTClient 8:83bb166aba73 101
AzureIoTClient 8:83bb166aba73 102 static uint16_t byteutil_read_uint16(uint8_t** buffer, size_t len)
Azure.IoT Build 0:ef4901974abc 103 {
Azure.IoT Build 0:ef4901974abc 104 uint16_t result = 0;
AzureIoTClient 8:83bb166aba73 105 if (buffer != NULL && *buffer != NULL && len >= 2)
Azure.IoT Build 0:ef4901974abc 106 {
AzureIoTClient 8:83bb166aba73 107 result = 256 * (**buffer) + (*(*buffer + 1));
Azure.IoT Build 0:ef4901974abc 108 *buffer += 2; // Move the ptr
Azure.IoT Build 0:ef4901974abc 109 }
AzureIoTClient 8:83bb166aba73 110 else
AzureIoTClient 8:83bb166aba73 111 {
AzureIoTClient 11:a0a6a4cf7812 112 LOG(AZ_LOG_ERROR, LOG_LINE, "byteutil_read_uint16 == NULL or less than 2");
AzureIoTClient 8:83bb166aba73 113 }
Azure.IoT Build 0:ef4901974abc 114 return result;
Azure.IoT Build 0:ef4901974abc 115 }
Azure.IoT Build 0:ef4901974abc 116
Azure.IoT Build 0:ef4901974abc 117 static char* byteutil_readUTF(uint8_t** buffer, size_t* byteLen)
Azure.IoT Build 0:ef4901974abc 118 {
Azure.IoT Build 0:ef4901974abc 119 char* result = NULL;
Azure.IoT Build 0:ef4901974abc 120 if (buffer != NULL)
Azure.IoT Build 0:ef4901974abc 121 {
Azure.IoT Build 0:ef4901974abc 122 // Get the length of the string
AzureIoTClient 8:83bb166aba73 123 uint16_t len = byteutil_read_uint16(buffer, *byteLen);
Azure.IoT Build 0:ef4901974abc 124 if (len > 0)
Azure.IoT Build 0:ef4901974abc 125 {
Azure.IoT Build 0:ef4901974abc 126 result = (char*)malloc(len + 1);
Azure.IoT Build 0:ef4901974abc 127 if (result != NULL)
Azure.IoT Build 0:ef4901974abc 128 {
Azure.IoT Build 0:ef4901974abc 129 (void)memcpy(result, *buffer, len);
Azure.IoT Build 0:ef4901974abc 130 result[len] = '\0';
Azure.IoT Build 0:ef4901974abc 131 *buffer += len;
Azure.IoT Build 0:ef4901974abc 132 if (byteLen != NULL)
Azure.IoT Build 0:ef4901974abc 133 {
Azure.IoT Build 0:ef4901974abc 134 *byteLen = len;
Azure.IoT Build 0:ef4901974abc 135 }
Azure.IoT Build 0:ef4901974abc 136 }
Azure.IoT Build 0:ef4901974abc 137 }
Azure.IoT Build 0:ef4901974abc 138 }
AzureIoTClient 8:83bb166aba73 139 else
AzureIoTClient 8:83bb166aba73 140 {
AzureIoTClient 11:a0a6a4cf7812 141 LOG(AZ_LOG_ERROR, LOG_LINE, "readByte buffer == NULL.");
AzureIoTClient 8:83bb166aba73 142 }
Azure.IoT Build 0:ef4901974abc 143 return result;
Azure.IoT Build 0:ef4901974abc 144 }
Azure.IoT Build 0:ef4901974abc 145
Azure.IoT Build 0:ef4901974abc 146 static uint8_t byteutil_readByte(uint8_t** buffer)
Azure.IoT Build 0:ef4901974abc 147 {
Azure.IoT Build 0:ef4901974abc 148 uint8_t result = 0;
Azure.IoT Build 0:ef4901974abc 149 if (buffer != NULL)
Azure.IoT Build 0:ef4901974abc 150 {
Azure.IoT Build 0:ef4901974abc 151 result = **buffer;
Azure.IoT Build 0:ef4901974abc 152 (*buffer)++;
Azure.IoT Build 0:ef4901974abc 153 }
AzureIoTClient 8:83bb166aba73 154 else
AzureIoTClient 8:83bb166aba73 155 {
AzureIoTClient 11:a0a6a4cf7812 156 LOG(AZ_LOG_ERROR, LOG_LINE, "readByte buffer == NULL.");
AzureIoTClient 8:83bb166aba73 157 }
Azure.IoT Build 0:ef4901974abc 158 return result;
Azure.IoT Build 0:ef4901974abc 159 }
Azure.IoT Build 0:ef4901974abc 160
Azure.IoT Build 0:ef4901974abc 161 static void sendComplete(void* context, IO_SEND_RESULT send_result)
Azure.IoT Build 0:ef4901974abc 162 {
AzureIoTClient 8:83bb166aba73 163 MQTT_CLIENT* mqtt_client = (MQTT_CLIENT*)context;
AzureIoTClient 9:37d14c31ff6e 164 if (mqtt_client != NULL)
Azure.IoT Build 0:ef4901974abc 165 {
AzureIoTClient 9:37d14c31ff6e 166 if (send_result == IO_SEND_OK)
Azure.IoT Build 0:ef4901974abc 167 {
AzureIoTClient 9:37d14c31ff6e 168 if (mqtt_client->packetState == DISCONNECT_TYPE)
AzureIoTClient 5:34779607059c 169 {
AzureIoTClient 9:37d14c31ff6e 170 /*Codes_SRS_MQTT_CLIENT_07_032: [If the actionResult parameter is of type MQTT_CLIENT_ON_DISCONNECT the the msgInfo value shall be NULL.]*/
AzureIoTClient 9:37d14c31ff6e 171 if (mqtt_client->fnOperationCallback != NULL)
AzureIoTClient 9:37d14c31ff6e 172 {
AzureIoTClient 9:37d14c31ff6e 173 mqtt_client->fnOperationCallback(mqtt_client, MQTT_CLIENT_ON_DISCONNECT, NULL, mqtt_client->ctx);
AzureIoTClient 9:37d14c31ff6e 174 }
AzureIoTClient 9:37d14c31ff6e 175 // close the xio
AzureIoTClient 9:37d14c31ff6e 176 close_connection(mqtt_client);
AzureIoTClient 5:34779607059c 177 }
AzureIoTClient 9:37d14c31ff6e 178 }
AzureIoTClient 9:37d14c31ff6e 179 else if (send_result == IO_SEND_ERROR)
AzureIoTClient 9:37d14c31ff6e 180 {
AzureIoTClient 11:a0a6a4cf7812 181 LOG(AZ_LOG_ERROR, LOG_LINE, "MQTT Send Complete Failure send_result: %d", (int)send_result);
AzureIoTClient 9:37d14c31ff6e 182 set_error_callback(mqtt_client, MQTT_CLIENT_COMMUNICATION_ERROR);
Azure.IoT Build 0:ef4901974abc 183 }
Azure.IoT Build 0:ef4901974abc 184 }
AzureIoTClient 5:34779607059c 185 else
AzureIoTClient 5:34779607059c 186 {
AzureIoTClient 11:a0a6a4cf7812 187 LOG(AZ_LOG_ERROR, LOG_LINE, "MQTT Send Complete Failure with NULL mqtt_client");
AzureIoTClient 5:34779607059c 188 }
Azure.IoT Build 0:ef4901974abc 189 }
Azure.IoT Build 0:ef4901974abc 190
Azure.IoT Build 0:ef4901974abc 191 static const char* retrievePacketType(CONTROL_PACKET_TYPE packet)
Azure.IoT Build 0:ef4901974abc 192 {
Azure.IoT Build 0:ef4901974abc 193 switch (packet&CONNECT_PACKET_MASK)
Azure.IoT Build 0:ef4901974abc 194 {
Azure.IoT Build 0:ef4901974abc 195 case CONNECT_TYPE: return "CONNECT";
Azure.IoT Build 0:ef4901974abc 196 case CONNACK_TYPE: return "CONNACK";
Azure.IoT Build 0:ef4901974abc 197 case PUBLISH_TYPE: return "PUBLISH";
Azure.IoT Build 0:ef4901974abc 198 case PUBACK_TYPE: return "PUBACK";
Azure.IoT Build 0:ef4901974abc 199 case PUBREC_TYPE: return "PUBREC";
Azure.IoT Build 0:ef4901974abc 200 case PUBREL_TYPE: return "PUBREL";
Azure.IoT Build 0:ef4901974abc 201 case SUBSCRIBE_TYPE: return "SUBSCRIBE";
Azure.IoT Build 0:ef4901974abc 202 case SUBACK_TYPE: return "SUBACK";
Azure.IoT Build 0:ef4901974abc 203 case UNSUBSCRIBE_TYPE: return "UNSUBSCRIBE";
Azure.IoT Build 0:ef4901974abc 204 case UNSUBACK_TYPE: return "UNSUBACK";
Azure.IoT Build 0:ef4901974abc 205 case PINGREQ_TYPE: return "PINGREQ";
Azure.IoT Build 0:ef4901974abc 206 case PINGRESP_TYPE: return "PINGRESP";
Azure.IoT Build 0:ef4901974abc 207 case DISCONNECT_TYPE: return "DISCONNECT";
Azure.IoT Build 0:ef4901974abc 208 default:
Azure.IoT Build 0:ef4901974abc 209 case PACKET_TYPE_ERROR:
Azure.IoT Build 0:ef4901974abc 210 case UNKNOWN_TYPE:
Azure.IoT Build 0:ef4901974abc 211 return "UNKNOWN";
Azure.IoT Build 0:ef4901974abc 212 }
Azure.IoT Build 0:ef4901974abc 213 }
Azure.IoT Build 0:ef4901974abc 214
AzureIoTClient 1:8dba42ff9701 215 static void getLogTime(char* timeResult, size_t len)
AzureIoTClient 1:8dba42ff9701 216 {
AzureIoTClient 1:8dba42ff9701 217 if (timeResult != NULL)
AzureIoTClient 1:8dba42ff9701 218 {
AzureIoTClient 8:83bb166aba73 219 time_t agent_time = get_time(NULL);
AzureIoTClient 8:83bb166aba73 220 if (agent_time == (time_t)-1)
AzureIoTClient 1:8dba42ff9701 221 {
AzureIoTClient 1:8dba42ff9701 222 timeResult[0] = '\0';
AzureIoTClient 1:8dba42ff9701 223 }
AzureIoTClient 8:83bb166aba73 224 else
AzureIoTClient 8:83bb166aba73 225 {
AzureIoTClient 8:83bb166aba73 226 struct tm* tmInfo = localtime(&agent_time);
AzureIoTClient 8:83bb166aba73 227 if (tmInfo == NULL)
AzureIoTClient 8:83bb166aba73 228 {
AzureIoTClient 8:83bb166aba73 229 timeResult[0] = '\0';
AzureIoTClient 8:83bb166aba73 230 }
AzureIoTClient 8:83bb166aba73 231 else
AzureIoTClient 8:83bb166aba73 232 {
AzureIoTClient 8:83bb166aba73 233 if (strftime(timeResult, len, "%H:%M:%S", tmInfo) == 0)
AzureIoTClient 8:83bb166aba73 234 {
AzureIoTClient 8:83bb166aba73 235 timeResult[0] = '\0';
AzureIoTClient 8:83bb166aba73 236 }
AzureIoTClient 8:83bb166aba73 237 }
AzureIoTClient 8:83bb166aba73 238 }
AzureIoTClient 1:8dba42ff9701 239 }
AzureIoTClient 1:8dba42ff9701 240 }
AzureIoTClient 1:8dba42ff9701 241
AzureIoTClient 8:83bb166aba73 242 static void log_outgoing_trace(MQTT_CLIENT* mqtt_client, STRING_HANDLE trace_log)
Azure.IoT Build 0:ef4901974abc 243 {
AzureIoTClient 8:83bb166aba73 244 if (mqtt_client != NULL && mqtt_client->logTrace && trace_log != NULL)
AzureIoTClient 8:83bb166aba73 245 {
AzureIoTClient 8:83bb166aba73 246 char tmBuffer[TIME_MAX_BUFFER];
AzureIoTClient 8:83bb166aba73 247 getLogTime(tmBuffer, TIME_MAX_BUFFER);
AzureIoTClient 11:a0a6a4cf7812 248 LOG(AZ_LOG_TRACE, LOG_LINE, "-> %s %s", tmBuffer, STRING_c_str(trace_log));
AzureIoTClient 8:83bb166aba73 249 }
AzureIoTClient 8:83bb166aba73 250 }
AzureIoTClient 8:83bb166aba73 251
AzureIoTClient 8:83bb166aba73 252 static void logOutgoingingRawTrace(MQTT_CLIENT* mqtt_client, const uint8_t* data, size_t length)
AzureIoTClient 8:83bb166aba73 253 {
AzureIoTClient 8:83bb166aba73 254 if (mqtt_client != NULL && data != NULL && length > 0 && mqtt_client->rawBytesTrace)
Azure.IoT Build 0:ef4901974abc 255 {
AzureIoTClient 1:8dba42ff9701 256 char tmBuffer[TIME_MAX_BUFFER];
AzureIoTClient 1:8dba42ff9701 257 getLogTime(tmBuffer, TIME_MAX_BUFFER);
AzureIoTClient 1:8dba42ff9701 258
AzureIoTClient 11:a0a6a4cf7812 259 LOG(AZ_LOG_TRACE, 0, "-> %s %s: ", tmBuffer, retrievePacketType((unsigned char)data[0]));
AzureIoTClient 12:30b08cda82fd 260 size_t index = 0;
AzureIoTClient 12:30b08cda82fd 261 for (index = 0; index < length; index++)
Azure.IoT Build 0:ef4901974abc 262 {
AzureIoTClient 11:a0a6a4cf7812 263 LOG(AZ_LOG_TRACE, 0, "0x%02x ", data[index]);
Azure.IoT Build 0:ef4901974abc 264 }
AzureIoTClient 11:a0a6a4cf7812 265 LOG(AZ_LOG_TRACE, LOG_LINE, "");
Azure.IoT Build 0:ef4901974abc 266 }
Azure.IoT Build 0:ef4901974abc 267 }
Azure.IoT Build 0:ef4901974abc 268
AzureIoTClient 8:83bb166aba73 269 static void log_incoming_trace(MQTT_CLIENT* mqtt_client, STRING_HANDLE trace_log)
AzureIoTClient 8:83bb166aba73 270 {
AzureIoTClient 8:83bb166aba73 271 if (mqtt_client != NULL && mqtt_client->logTrace && trace_log != NULL)
AzureIoTClient 8:83bb166aba73 272 {
AzureIoTClient 8:83bb166aba73 273 char tmBuffer[TIME_MAX_BUFFER];
AzureIoTClient 8:83bb166aba73 274 getLogTime(tmBuffer, TIME_MAX_BUFFER);
AzureIoTClient 11:a0a6a4cf7812 275 LOG(AZ_LOG_TRACE, LOG_LINE, "<- %s %s", tmBuffer, STRING_c_str(trace_log) );
AzureIoTClient 8:83bb166aba73 276 }
AzureIoTClient 8:83bb166aba73 277 }
AzureIoTClient 8:83bb166aba73 278
AzureIoTClient 8:83bb166aba73 279 static void logIncomingRawTrace(MQTT_CLIENT* mqtt_client, CONTROL_PACKET_TYPE packet, uint8_t flags, const uint8_t* data, size_t length)
Azure.IoT Build 0:ef4901974abc 280 {
AzureIoTClient 6:d9c4702f91ca 281 #ifdef NO_LOGGING
AzureIoTClient 8:83bb166aba73 282 UNUSED(flags);
AzureIoTClient 6:d9c4702f91ca 283 #endif
AzureIoTClient 8:83bb166aba73 284 if (mqtt_client != NULL && mqtt_client->rawBytesTrace)
Azure.IoT Build 0:ef4901974abc 285 {
AzureIoTClient 3:9b4e7158ca0d 286 if (data != NULL && length > 0)
AzureIoTClient 3:9b4e7158ca0d 287 {
AzureIoTClient 3:9b4e7158ca0d 288 char tmBuffer[TIME_MAX_BUFFER];
AzureIoTClient 3:9b4e7158ca0d 289 getLogTime(tmBuffer, TIME_MAX_BUFFER);
AzureIoTClient 1:8dba42ff9701 290
AzureIoTClient 11:a0a6a4cf7812 291 LOG(AZ_LOG_TRACE, 0, "<- %s %s: 0x%02x 0x%02x ", tmBuffer, retrievePacketType((CONTROL_PACKET_TYPE)packet), (unsigned char)(packet | flags), length);
AzureIoTClient 12:30b08cda82fd 292 size_t index = 0;
AzureIoTClient 12:30b08cda82fd 293 for (index = 0; index < length; index++)
AzureIoTClient 3:9b4e7158ca0d 294 {
AzureIoTClient 11:a0a6a4cf7812 295 LOG(AZ_LOG_TRACE, 0, "0x%02x ", data[index]);
AzureIoTClient 3:9b4e7158ca0d 296 }
AzureIoTClient 11:a0a6a4cf7812 297 LOG(AZ_LOG_TRACE, LOG_LINE, "");
AzureIoTClient 3:9b4e7158ca0d 298 }
AzureIoTClient 3:9b4e7158ca0d 299 else if (packet == PINGRESP_TYPE)
Azure.IoT Build 0:ef4901974abc 300 {
AzureIoTClient 3:9b4e7158ca0d 301 char tmBuffer[TIME_MAX_BUFFER];
AzureIoTClient 3:9b4e7158ca0d 302 getLogTime(tmBuffer, TIME_MAX_BUFFER);
AzureIoTClient 11:a0a6a4cf7812 303 LOG(AZ_LOG_TRACE, LOG_LINE, "<- %s %s: 0x%02x 0x%02x ", tmBuffer, retrievePacketType((CONTROL_PACKET_TYPE)packet), (unsigned char)(packet | flags), length);
Azure.IoT Build 0:ef4901974abc 304 }
Azure.IoT Build 0:ef4901974abc 305 }
Azure.IoT Build 0:ef4901974abc 306 }
Azure.IoT Build 0:ef4901974abc 307
AzureIoTClient 8:83bb166aba73 308 static int sendPacketItem(MQTT_CLIENT* mqtt_client, const unsigned char* data, size_t length)
Azure.IoT Build 0:ef4901974abc 309 {
Azure.IoT Build 0:ef4901974abc 310 int result;
Azure.IoT Build 0:ef4901974abc 311
AzureIoTClient 8:83bb166aba73 312 if (tickcounter_get_current_ms(mqtt_client->packetTickCntr, &mqtt_client->packetSendTimeMs) != 0)
Azure.IoT Build 0:ef4901974abc 313 {
AzureIoTClient 11:a0a6a4cf7812 314 LOG(AZ_LOG_ERROR, LOG_LINE, "Failure getting current ms tickcounter");
Azure.IoT Build 0:ef4901974abc 315 result = __LINE__;
Azure.IoT Build 0:ef4901974abc 316 }
Azure.IoT Build 0:ef4901974abc 317 else
Azure.IoT Build 0:ef4901974abc 318 {
AzureIoTClient 8:83bb166aba73 319 result = xio_send(mqtt_client->xioHandle, (const void*)data, length, sendComplete, mqtt_client);
Azure.IoT Build 0:ef4901974abc 320 if (result != 0)
Azure.IoT Build 0:ef4901974abc 321 {
AzureIoTClient 11:a0a6a4cf7812 322 LOG(AZ_LOG_ERROR, LOG_LINE, "%d: Failure sending control packet data", result);
Azure.IoT Build 0:ef4901974abc 323 result = __LINE__;
Azure.IoT Build 0:ef4901974abc 324 }
Azure.IoT Build 4:e7167dabd6e4 325 else
Azure.IoT Build 4:e7167dabd6e4 326 {
AzureIoTClient 8:83bb166aba73 327 logOutgoingingRawTrace(mqtt_client, (const uint8_t*)data, length);
Azure.IoT Build 4:e7167dabd6e4 328 }
Azure.IoT Build 0:ef4901974abc 329 }
Azure.IoT Build 0:ef4901974abc 330 return result;
Azure.IoT Build 0:ef4901974abc 331 }
Azure.IoT Build 0:ef4901974abc 332
Azure.IoT Build 0:ef4901974abc 333 static void onOpenComplete(void* context, IO_OPEN_RESULT open_result)
Azure.IoT Build 0:ef4901974abc 334 {
AzureIoTClient 8:83bb166aba73 335 MQTT_CLIENT* mqtt_client = (MQTT_CLIENT*)context;
AzureIoTClient 8:83bb166aba73 336 if (mqtt_client != NULL)
Azure.IoT Build 0:ef4901974abc 337 {
AzureIoTClient 8:83bb166aba73 338 if (open_result == IO_OPEN_OK && !mqtt_client->socketConnected)
Azure.IoT Build 0:ef4901974abc 339 {
AzureIoTClient 8:83bb166aba73 340 mqtt_client->packetState = CONNECT_TYPE;
AzureIoTClient 8:83bb166aba73 341 mqtt_client->socketConnected = true;
AzureIoTClient 8:83bb166aba73 342
AzureIoTClient 8:83bb166aba73 343 STRING_HANDLE trace_log = construct_trace_log_handle(mqtt_client);
AzureIoTClient 8:83bb166aba73 344
Azure.IoT Build 0:ef4901974abc 345 // Send the Connect packet
AzureIoTClient 8:83bb166aba73 346 BUFFER_HANDLE connPacket = mqtt_codec_connect(&mqtt_client->mqttOptions, trace_log);
Azure.IoT Build 0:ef4901974abc 347 if (connPacket == NULL)
Azure.IoT Build 0:ef4901974abc 348 {
AzureIoTClient 11:a0a6a4cf7812 349 LOG(AZ_LOG_ERROR, LOG_LINE, "Error: mqtt_codec_connect failed");
Azure.IoT Build 0:ef4901974abc 350 }
Azure.IoT Build 0:ef4901974abc 351 else
Azure.IoT Build 0:ef4901974abc 352 {
AzureIoTClient 13:3c202001e4ba 353 size_t size = BUFFER_length(connPacket);
Azure.IoT Build 0:ef4901974abc 354 /*Codes_SRS_MQTT_CLIENT_07_009: [On success mqtt_client_connect shall send the MQTT CONNECT to the endpoint.]*/
AzureIoTClient 13:3c202001e4ba 355 if (sendPacketItem(mqtt_client, BUFFER_u_char(connPacket), size) != 0)
Azure.IoT Build 0:ef4901974abc 356 {
AzureIoTClient 11:a0a6a4cf7812 357 LOG(AZ_LOG_ERROR, LOG_LINE, "Error: mqtt_codec_connect failed");
Azure.IoT Build 0:ef4901974abc 358 }
AzureIoTClient 8:83bb166aba73 359 else
AzureIoTClient 8:83bb166aba73 360 {
AzureIoTClient 8:83bb166aba73 361 log_outgoing_trace(mqtt_client, trace_log);
AzureIoTClient 8:83bb166aba73 362 }
Azure.IoT Build 0:ef4901974abc 363 BUFFER_delete(connPacket);
Azure.IoT Build 0:ef4901974abc 364 }
AzureIoTClient 8:83bb166aba73 365 if (trace_log != NULL)
AzureIoTClient 8:83bb166aba73 366 {
AzureIoTClient 8:83bb166aba73 367 STRING_delete(trace_log);
AzureIoTClient 8:83bb166aba73 368 }
Azure.IoT Build 0:ef4901974abc 369 }
AzureIoTClient 8:83bb166aba73 370 else
Azure.IoT Build 0:ef4901974abc 371 {
AzureIoTClient 9:37d14c31ff6e 372 if (mqtt_client->socketConnected == false && mqtt_client->fnOnErrorCallBack)
AzureIoTClient 8:83bb166aba73 373 {
AzureIoTClient 9:37d14c31ff6e 374 mqtt_client->fnOnErrorCallBack(mqtt_client, MQTT_CLIENT_CONNECTION_ERROR, mqtt_client->errorCBCtx);
AzureIoTClient 8:83bb166aba73 375 }
AzureIoTClient 9:37d14c31ff6e 376 close_connection(mqtt_client);
Azure.IoT Build 0:ef4901974abc 377 }
Azure.IoT Build 0:ef4901974abc 378 }
AzureIoTClient 8:83bb166aba73 379 else
AzureIoTClient 8:83bb166aba73 380 {
AzureIoTClient 11:a0a6a4cf7812 381 LOG(AZ_LOG_ERROR, LOG_LINE, "Error: mqtt_client is NULL");
AzureIoTClient 8:83bb166aba73 382 }
Azure.IoT Build 0:ef4901974abc 383 }
Azure.IoT Build 0:ef4901974abc 384
Azure.IoT Build 0:ef4901974abc 385 static void onBytesReceived(void* context, const unsigned char* buffer, size_t size)
Azure.IoT Build 0:ef4901974abc 386 {
AzureIoTClient 8:83bb166aba73 387 MQTT_CLIENT* mqtt_client = (MQTT_CLIENT*)context;
AzureIoTClient 8:83bb166aba73 388 if (mqtt_client != NULL)
Azure.IoT Build 0:ef4901974abc 389 {
AzureIoTClient 8:83bb166aba73 390 if (mqtt_codec_bytesReceived(mqtt_client->codec_handle, buffer, size) != 0)
Azure.IoT Build 0:ef4901974abc 391 {
AzureIoTClient 9:37d14c31ff6e 392 set_error_callback(mqtt_client, MQTT_CLIENT_PARSE_ERROR);
Azure.IoT Build 0:ef4901974abc 393 }
Azure.IoT Build 0:ef4901974abc 394 }
AzureIoTClient 8:83bb166aba73 395 else
AzureIoTClient 8:83bb166aba73 396 {
AzureIoTClient 11:a0a6a4cf7812 397 LOG(AZ_LOG_ERROR, LOG_LINE, "Error: mqtt_client is NULL");
AzureIoTClient 8:83bb166aba73 398 }
Azure.IoT Build 0:ef4901974abc 399 }
Azure.IoT Build 0:ef4901974abc 400
Azure.IoT Build 0:ef4901974abc 401 static void onIoError(void* context)
Azure.IoT Build 0:ef4901974abc 402 {
AzureIoTClient 8:83bb166aba73 403 MQTT_CLIENT* mqtt_client = (MQTT_CLIENT*)context;
AzureIoTClient 8:83bb166aba73 404 if (mqtt_client != NULL && mqtt_client->fnOperationCallback)
Azure.IoT Build 0:ef4901974abc 405 {
AzureIoTClient 9:37d14c31ff6e 406 /*Codes_SRS_MQTT_CLIENT_07_032: [If the actionResult parameter is of type MQTT_CLIENT_ON_DISCONNECT the the msgInfo value shall be NULL.]*/
AzureIoTClient 8:83bb166aba73 407 /* Codes_SRS_MQTT_CLIENT_07_036: [ If an error is encountered by the ioHandle the mqtt_client shall call xio_close. ] */
AzureIoTClient 9:37d14c31ff6e 408 set_error_callback(mqtt_client, MQTT_CLIENT_CONNECTION_ERROR);
AzureIoTClient 8:83bb166aba73 409 }
AzureIoTClient 8:83bb166aba73 410 else
AzureIoTClient 8:83bb166aba73 411 {
AzureIoTClient 11:a0a6a4cf7812 412 LOG(AZ_LOG_ERROR, LOG_LINE, "Error invalid parameter: mqtt_client: %p", mqtt_client);
Azure.IoT Build 0:ef4901974abc 413 }
Azure.IoT Build 0:ef4901974abc 414 }
Azure.IoT Build 0:ef4901974abc 415
AzureIoTClient 8:83bb166aba73 416 static int cloneMqttOptions(MQTT_CLIENT* mqtt_client, const MQTT_CLIENT_OPTIONS* mqttOptions)
Azure.IoT Build 0:ef4901974abc 417 {
Azure.IoT Build 0:ef4901974abc 418 int result = 0;
Azure.IoT Build 0:ef4901974abc 419 if (mqttOptions->clientId != NULL)
Azure.IoT Build 0:ef4901974abc 420 {
AzureIoTClient 8:83bb166aba73 421 if (mallocAndStrcpy_s(&mqtt_client->mqttOptions.clientId, mqttOptions->clientId) != 0)
Azure.IoT Build 0:ef4901974abc 422 {
Azure.IoT Build 0:ef4901974abc 423 result = __LINE__;
AzureIoTClient 11:a0a6a4cf7812 424 LOG(AZ_LOG_ERROR, LOG_LINE, "mallocAndStrcpy_s clientId");
Azure.IoT Build 0:ef4901974abc 425 }
Azure.IoT Build 0:ef4901974abc 426 }
Azure.IoT Build 0:ef4901974abc 427 if (result == 0 && mqttOptions->willTopic != NULL)
Azure.IoT Build 0:ef4901974abc 428 {
AzureIoTClient 8:83bb166aba73 429 if (mallocAndStrcpy_s(&mqtt_client->mqttOptions.willTopic, mqttOptions->willTopic) != 0)
Azure.IoT Build 0:ef4901974abc 430 {
Azure.IoT Build 0:ef4901974abc 431 result = __LINE__;
AzureIoTClient 11:a0a6a4cf7812 432 LOG(AZ_LOG_ERROR, LOG_LINE, "mallocAndStrcpy_s willTopic");
Azure.IoT Build 0:ef4901974abc 433 }
Azure.IoT Build 0:ef4901974abc 434 }
Azure.IoT Build 0:ef4901974abc 435 if (result == 0 && mqttOptions->willMessage != NULL)
Azure.IoT Build 0:ef4901974abc 436 {
AzureIoTClient 8:83bb166aba73 437 if (mallocAndStrcpy_s(&mqtt_client->mqttOptions.willMessage, mqttOptions->willMessage) != 0)
Azure.IoT Build 0:ef4901974abc 438 {
AzureIoTClient 11:a0a6a4cf7812 439 LOG(AZ_LOG_ERROR, LOG_LINE, "mallocAndStrcpy_s willMessage");
Azure.IoT Build 0:ef4901974abc 440 result = __LINE__;
Azure.IoT Build 0:ef4901974abc 441 }
Azure.IoT Build 0:ef4901974abc 442 }
Azure.IoT Build 0:ef4901974abc 443 if (result == 0 && mqttOptions->username != NULL)
Azure.IoT Build 0:ef4901974abc 444 {
AzureIoTClient 8:83bb166aba73 445 if (mallocAndStrcpy_s(&mqtt_client->mqttOptions.username, mqttOptions->username) != 0)
Azure.IoT Build 0:ef4901974abc 446 {
AzureIoTClient 11:a0a6a4cf7812 447 LOG(AZ_LOG_ERROR, LOG_LINE, "mallocAndStrcpy_s username");
Azure.IoT Build 0:ef4901974abc 448 result = __LINE__;
Azure.IoT Build 0:ef4901974abc 449 }
Azure.IoT Build 0:ef4901974abc 450 }
Azure.IoT Build 0:ef4901974abc 451 if (result == 0 && mqttOptions->password != NULL)
Azure.IoT Build 0:ef4901974abc 452 {
AzureIoTClient 8:83bb166aba73 453 if (mallocAndStrcpy_s(&mqtt_client->mqttOptions.password, mqttOptions->password) != 0)
Azure.IoT Build 0:ef4901974abc 454 {
AzureIoTClient 11:a0a6a4cf7812 455 LOG(AZ_LOG_ERROR, LOG_LINE, "mallocAndStrcpy_s password");
Azure.IoT Build 0:ef4901974abc 456 result = __LINE__;
Azure.IoT Build 0:ef4901974abc 457 }
Azure.IoT Build 0:ef4901974abc 458 }
Azure.IoT Build 0:ef4901974abc 459 if (result == 0)
Azure.IoT Build 0:ef4901974abc 460 {
AzureIoTClient 8:83bb166aba73 461 mqtt_client->mqttOptions.keepAliveInterval = mqttOptions->keepAliveInterval;
AzureIoTClient 8:83bb166aba73 462 mqtt_client->mqttOptions.messageRetain = mqttOptions->messageRetain;
AzureIoTClient 8:83bb166aba73 463 mqtt_client->mqttOptions.useCleanSession = mqttOptions->useCleanSession;
AzureIoTClient 8:83bb166aba73 464 mqtt_client->mqttOptions.qualityOfServiceValue = mqttOptions->qualityOfServiceValue;
Azure.IoT Build 0:ef4901974abc 465 }
Azure.IoT Build 0:ef4901974abc 466 else
Azure.IoT Build 0:ef4901974abc 467 {
AzureIoTClient 8:83bb166aba73 468 free(mqtt_client->mqttOptions.clientId);
AzureIoTClient 8:83bb166aba73 469 free(mqtt_client->mqttOptions.willTopic);
AzureIoTClient 8:83bb166aba73 470 free(mqtt_client->mqttOptions.willMessage);
AzureIoTClient 8:83bb166aba73 471 free(mqtt_client->mqttOptions.username);
AzureIoTClient 8:83bb166aba73 472 free(mqtt_client->mqttOptions.password);
Azure.IoT Build 0:ef4901974abc 473 }
Azure.IoT Build 0:ef4901974abc 474 return result;
Azure.IoT Build 0:ef4901974abc 475 }
Azure.IoT Build 0:ef4901974abc 476
Azure.IoT Build 0:ef4901974abc 477 static void recvCompleteCallback(void* context, CONTROL_PACKET_TYPE packet, int flags, BUFFER_HANDLE headerData)
Azure.IoT Build 0:ef4901974abc 478 {
AzureIoTClient 8:83bb166aba73 479 MQTT_CLIENT* mqtt_client = (MQTT_CLIENT*)context;
AzureIoTClient 8:83bb166aba73 480 if ((mqtt_client != NULL && headerData != NULL) || packet == PINGRESP_TYPE)
Azure.IoT Build 0:ef4901974abc 481 {
Azure.IoT Build 0:ef4901974abc 482 size_t len = BUFFER_length(headerData);
Azure.IoT Build 0:ef4901974abc 483 uint8_t* iterator = BUFFER_u_char(headerData);
Azure.IoT Build 0:ef4901974abc 484
AzureIoTClient 8:83bb166aba73 485 logIncomingRawTrace(mqtt_client, packet, (uint8_t)flags, iterator, len);
Azure.IoT Build 0:ef4901974abc 486
AzureIoTClient 3:9b4e7158ca0d 487 if ((iterator != NULL && len > 0) || packet == PINGRESP_TYPE)
Azure.IoT Build 0:ef4901974abc 488 {
Azure.IoT Build 0:ef4901974abc 489 switch (packet)
Azure.IoT Build 0:ef4901974abc 490 {
AzureIoTClient 3:9b4e7158ca0d 491 case CONNACK_TYPE:
Azure.IoT Build 0:ef4901974abc 492 {
AzureIoTClient 8:83bb166aba73 493 if (mqtt_client->fnOperationCallback != NULL)
AzureIoTClient 3:9b4e7158ca0d 494 {
AzureIoTClient 8:83bb166aba73 495 STRING_HANDLE trace_log = NULL;
AzureIoTClient 8:83bb166aba73 496
AzureIoTClient 3:9b4e7158ca0d 497 /*Codes_SRS_MQTT_CLIENT_07_028: [If the actionResult parameter is of type CONNECT_ACK then the msgInfo value shall be a CONNECT_ACK structure.]*/
AzureIoTClient 3:9b4e7158ca0d 498 CONNECT_ACK connack = { 0 };
AzureIoTClient 3:9b4e7158ca0d 499 connack.isSessionPresent = (byteutil_readByte(&iterator) == 0x1) ? true : false;
AzureIoTClient 3:9b4e7158ca0d 500 connack.returnCode = byteutil_readByte(&iterator);
Azure.IoT Build 0:ef4901974abc 501
AzureIoTClient 8:83bb166aba73 502 if (mqtt_client->logTrace)
AzureIoTClient 8:83bb166aba73 503 {
AzureIoTClient 8:83bb166aba73 504 trace_log = STRING_construct_sprintf("CONNACK | SESSION_PRESENT: %s | RETURN_CODE: 0x%x", connack.isSessionPresent ? TRUE_CONST : FALSE_CONST, connack.returnCode);
AzureIoTClient 8:83bb166aba73 505 log_incoming_trace(mqtt_client, trace_log);
AzureIoTClient 8:83bb166aba73 506 STRING_delete(trace_log);
AzureIoTClient 8:83bb166aba73 507 }
AzureIoTClient 8:83bb166aba73 508 mqtt_client->fnOperationCallback(mqtt_client, MQTT_CLIENT_ON_CONNACK, (void*)&connack, mqtt_client->ctx);
AzureIoTClient 3:9b4e7158ca0d 509
AzureIoTClient 3:9b4e7158ca0d 510 if (connack.returnCode == CONNECTION_ACCEPTED)
AzureIoTClient 3:9b4e7158ca0d 511 {
AzureIoTClient 8:83bb166aba73 512 mqtt_client->clientConnected = true;
AzureIoTClient 3:9b4e7158ca0d 513 }
Azure.IoT Build 0:ef4901974abc 514 }
AzureIoTClient 8:83bb166aba73 515 else
AzureIoTClient 8:83bb166aba73 516 {
AzureIoTClient 11:a0a6a4cf7812 517 LOG(AZ_LOG_ERROR, LOG_LINE, "fnOperationCallback NULL");
AzureIoTClient 8:83bb166aba73 518 }
AzureIoTClient 3:9b4e7158ca0d 519 break;
Azure.IoT Build 0:ef4901974abc 520 }
AzureIoTClient 3:9b4e7158ca0d 521 case PUBLISH_TYPE:
Azure.IoT Build 0:ef4901974abc 522 {
AzureIoTClient 8:83bb166aba73 523 if (mqtt_client->fnMessageRecv != NULL)
AzureIoTClient 3:9b4e7158ca0d 524 {
AzureIoTClient 8:83bb166aba73 525 STRING_HANDLE trace_log = NULL;
AzureIoTClient 8:83bb166aba73 526
AzureIoTClient 3:9b4e7158ca0d 527 bool isDuplicateMsg = (flags & DUPLICATE_FLAG_MASK) ? true : false;
AzureIoTClient 3:9b4e7158ca0d 528 bool isRetainMsg = (flags & RETAIN_FLAG_MASK) ? true : false;
AzureIoTClient 3:9b4e7158ca0d 529 QOS_VALUE qosValue = (flags == 0) ? DELIVER_AT_MOST_ONCE : (flags & QOS_LEAST_ONCE_FLAG_MASK) ? DELIVER_AT_LEAST_ONCE : DELIVER_EXACTLY_ONCE;
AzureIoTClient 3:9b4e7158ca0d 530
AzureIoTClient 8:83bb166aba73 531 if (mqtt_client->logTrace)
AzureIoTClient 8:83bb166aba73 532 {
AzureIoTClient 8:83bb166aba73 533 trace_log = STRING_construct_sprintf("PUBLISH | IS_DUP: %s | RETAIN: %d | QOS: %s", isDuplicateMsg ? TRUE_CONST : FALSE_CONST,
AzureIoTClient 8:83bb166aba73 534 isRetainMsg ? 1 : 0, ENUM_TO_STRING(QOS_VALUE, qosValue) );
AzureIoTClient 8:83bb166aba73 535 }
AzureIoTClient 8:83bb166aba73 536
AzureIoTClient 3:9b4e7158ca0d 537 uint8_t* initialPos = iterator;
AzureIoTClient 8:83bb166aba73 538 size_t length = len - (iterator - initialPos);
AzureIoTClient 8:83bb166aba73 539 char* topicName = byteutil_readUTF(&iterator, &length);
AzureIoTClient 5:34779607059c 540 if (topicName == NULL)
AzureIoTClient 3:9b4e7158ca0d 541 {
AzureIoTClient 11:a0a6a4cf7812 542 LOG(AZ_LOG_ERROR, LOG_LINE, "Publish MSG: failure reading topic name");
AzureIoTClient 9:37d14c31ff6e 543 set_error_callback(mqtt_client, MQTT_CLIENT_PARSE_ERROR);
AzureIoTClient 8:83bb166aba73 544 if (trace_log != NULL)
AzureIoTClient 8:83bb166aba73 545 {
AzureIoTClient 8:83bb166aba73 546 STRING_delete(trace_log);
AzureIoTClient 5:34779607059c 547 }
AzureIoTClient 3:9b4e7158ca0d 548 }
AzureIoTClient 3:9b4e7158ca0d 549 else
AzureIoTClient 3:9b4e7158ca0d 550 {
AzureIoTClient 8:83bb166aba73 551 if (mqtt_client->logTrace)
AzureIoTClient 8:83bb166aba73 552 {
AzureIoTClient 8:83bb166aba73 553 STRING_sprintf(trace_log, " | TOPIC_NAME: %s", topicName);
AzureIoTClient 8:83bb166aba73 554 }
AzureIoTClient 5:34779607059c 555 uint16_t packetId = 0;
AzureIoTClient 8:83bb166aba73 556 length = len - (iterator - initialPos);
AzureIoTClient 5:34779607059c 557 if (qosValue != DELIVER_AT_MOST_ONCE)
AzureIoTClient 5:34779607059c 558 {
AzureIoTClient 8:83bb166aba73 559 packetId = byteutil_read_uint16(&iterator, length);
AzureIoTClient 8:83bb166aba73 560 if (mqtt_client->logTrace)
AzureIoTClient 8:83bb166aba73 561 {
AzureIoTClient 8:83bb166aba73 562 STRING_sprintf(trace_log, " | PACKET_ID: %"PRIu16, packetId);
AzureIoTClient 8:83bb166aba73 563 }
AzureIoTClient 5:34779607059c 564 }
AzureIoTClient 8:83bb166aba73 565 length = len - (iterator - initialPos);
AzureIoTClient 3:9b4e7158ca0d 566
AzureIoTClient 5:34779607059c 567 MQTT_MESSAGE_HANDLE msgHandle = mqttmessage_create(packetId, topicName, qosValue, iterator, length);
AzureIoTClient 5:34779607059c 568 if (msgHandle == NULL)
AzureIoTClient 3:9b4e7158ca0d 569 {
AzureIoTClient 11:a0a6a4cf7812 570 LOG(AZ_LOG_ERROR, LOG_LINE, "failure in mqttmessage_create");
AzureIoTClient 9:37d14c31ff6e 571 set_error_callback(mqtt_client, MQTT_CLIENT_MEMORY_ERROR);
AzureIoTClient 8:83bb166aba73 572 if (trace_log != NULL) {
AzureIoTClient 8:83bb166aba73 573 STRING_delete(trace_log);
AzureIoTClient 5:34779607059c 574 }
AzureIoTClient 3:9b4e7158ca0d 575 }
AzureIoTClient 5:34779607059c 576 else
AzureIoTClient 3:9b4e7158ca0d 577 {
AzureIoTClient 5:34779607059c 578 if (mqttmessage_setIsDuplicateMsg(msgHandle, isDuplicateMsg) != 0 ||
AzureIoTClient 5:34779607059c 579 mqttmessage_setIsRetained(msgHandle, isRetainMsg) != 0)
AzureIoTClient 5:34779607059c 580 {
AzureIoTClient 11:a0a6a4cf7812 581 LOG(AZ_LOG_ERROR, LOG_LINE, "failure setting mqtt message property");
AzureIoTClient 9:37d14c31ff6e 582 set_error_callback(mqtt_client, MQTT_CLIENT_MEMORY_ERROR);
AzureIoTClient 8:83bb166aba73 583 if (trace_log != NULL) {
AzureIoTClient 8:83bb166aba73 584 STRING_delete(trace_log);
AzureIoTClient 5:34779607059c 585 }
AzureIoTClient 5:34779607059c 586 }
AzureIoTClient 5:34779607059c 587 else
AzureIoTClient 5:34779607059c 588 {
AzureIoTClient 8:83bb166aba73 589 if (mqtt_client->logTrace)
AzureIoTClient 8:83bb166aba73 590 {
AzureIoTClient 8:83bb166aba73 591 STRING_sprintf(trace_log, " | PAYLOAD_LEN: %zu", length);
AzureIoTClient 8:83bb166aba73 592 log_incoming_trace(mqtt_client, trace_log);
AzureIoTClient 8:83bb166aba73 593 STRING_delete(trace_log);
AzureIoTClient 8:83bb166aba73 594 }
AzureIoTClient 8:83bb166aba73 595
AzureIoTClient 8:83bb166aba73 596 mqtt_client->fnMessageRecv(msgHandle, mqtt_client->ctx);
AzureIoTClient 5:34779607059c 597
AzureIoTClient 5:34779607059c 598 BUFFER_HANDLE pubRel = NULL;
AzureIoTClient 5:34779607059c 599 if (qosValue == DELIVER_EXACTLY_ONCE)
AzureIoTClient 5:34779607059c 600 {
AzureIoTClient 5:34779607059c 601 pubRel = mqtt_codec_publishReceived(packetId);
AzureIoTClient 5:34779607059c 602 if (pubRel == NULL)
AzureIoTClient 5:34779607059c 603 {
AzureIoTClient 11:a0a6a4cf7812 604 LOG(AZ_LOG_ERROR, LOG_LINE, "Failed to allocate publish receive message.");
AzureIoTClient 9:37d14c31ff6e 605 set_error_callback(mqtt_client, MQTT_CLIENT_MEMORY_ERROR);
AzureIoTClient 5:34779607059c 606 }
AzureIoTClient 5:34779607059c 607 }
AzureIoTClient 5:34779607059c 608 else if (qosValue == DELIVER_AT_LEAST_ONCE)
AzureIoTClient 5:34779607059c 609 {
AzureIoTClient 5:34779607059c 610 pubRel = mqtt_codec_publishAck(packetId);
AzureIoTClient 5:34779607059c 611 if (pubRel == NULL)
AzureIoTClient 5:34779607059c 612 {
AzureIoTClient 11:a0a6a4cf7812 613 LOG(AZ_LOG_ERROR, LOG_LINE, "Failed to allocate publish ack message.");
AzureIoTClient 9:37d14c31ff6e 614 set_error_callback(mqtt_client, MQTT_CLIENT_MEMORY_ERROR);
AzureIoTClient 5:34779607059c 615 }
AzureIoTClient 5:34779607059c 616 }
AzureIoTClient 5:34779607059c 617 if (pubRel != NULL)
AzureIoTClient 5:34779607059c 618 {
AzureIoTClient 13:3c202001e4ba 619 size_t size = BUFFER_length(pubRel);
AzureIoTClient 13:3c202001e4ba 620 (void)sendPacketItem(mqtt_client, BUFFER_u_char(pubRel), size);
AzureIoTClient 5:34779607059c 621 BUFFER_delete(pubRel);
AzureIoTClient 5:34779607059c 622 }
AzureIoTClient 5:34779607059c 623 }
AzureIoTClient 5:34779607059c 624 mqttmessage_destroy(msgHandle);
AzureIoTClient 3:9b4e7158ca0d 625 }
AzureIoTClient 3:9b4e7158ca0d 626 free(topicName);
AzureIoTClient 3:9b4e7158ca0d 627 }
Azure.IoT Build 0:ef4901974abc 628 }
AzureIoTClient 3:9b4e7158ca0d 629 break;
AzureIoTClient 3:9b4e7158ca0d 630 }
AzureIoTClient 3:9b4e7158ca0d 631 case PUBACK_TYPE:
AzureIoTClient 3:9b4e7158ca0d 632 case PUBREC_TYPE:
AzureIoTClient 3:9b4e7158ca0d 633 case PUBREL_TYPE:
AzureIoTClient 3:9b4e7158ca0d 634 case PUBCOMP_TYPE:
AzureIoTClient 3:9b4e7158ca0d 635 {
AzureIoTClient 8:83bb166aba73 636 if (mqtt_client->fnOperationCallback)
Azure.IoT Build 0:ef4901974abc 637 {
AzureIoTClient 8:83bb166aba73 638 STRING_HANDLE trace_log = NULL;
AzureIoTClient 8:83bb166aba73 639
AzureIoTClient 3:9b4e7158ca0d 640 /*Codes_SRS_MQTT_CLIENT_07_029: [If the actionResult parameter are of types PUBACK_TYPE, PUBREC_TYPE, PUBREL_TYPE or PUBCOMP_TYPE then the msgInfo value shall be a PUBLISH_ACK structure.]*/
AzureIoTClient 3:9b4e7158ca0d 641 MQTT_CLIENT_EVENT_RESULT action = (packet == PUBACK_TYPE) ? MQTT_CLIENT_ON_PUBLISH_ACK :
AzureIoTClient 3:9b4e7158ca0d 642 (packet == PUBREC_TYPE) ? MQTT_CLIENT_ON_PUBLISH_RECV :
AzureIoTClient 3:9b4e7158ca0d 643 (packet == PUBREL_TYPE) ? MQTT_CLIENT_ON_PUBLISH_REL : MQTT_CLIENT_ON_PUBLISH_COMP;
AzureIoTClient 3:9b4e7158ca0d 644
AzureIoTClient 3:9b4e7158ca0d 645 PUBLISH_ACK publish_ack = { 0 };
AzureIoTClient 8:83bb166aba73 646 publish_ack.packetId = byteutil_read_uint16(&iterator, len);
AzureIoTClient 8:83bb166aba73 647
AzureIoTClient 8:83bb166aba73 648 if (mqtt_client->logTrace)
AzureIoTClient 8:83bb166aba73 649 {
AzureIoTClient 8:83bb166aba73 650 trace_log = STRING_construct_sprintf("%s | PACKET_ID: %"PRIu16, packet == PUBACK_TYPE ? "PUBACK" : (packet == PUBREC_TYPE) ? "PUBREC" : (packet == PUBREL_TYPE) ? "PUBREL" : "PUBCOMP",
AzureIoTClient 8:83bb166aba73 651 publish_ack.packetId);
AzureIoTClient 8:83bb166aba73 652
AzureIoTClient 8:83bb166aba73 653 log_incoming_trace(mqtt_client, trace_log);
AzureIoTClient 8:83bb166aba73 654 STRING_delete(trace_log);
AzureIoTClient 8:83bb166aba73 655 }
Azure.IoT Build 0:ef4901974abc 656
Azure.IoT Build 0:ef4901974abc 657 BUFFER_HANDLE pubRel = NULL;
AzureIoTClient 8:83bb166aba73 658 mqtt_client->fnOperationCallback(mqtt_client, action, (void*)&publish_ack, mqtt_client->ctx);
AzureIoTClient 3:9b4e7158ca0d 659 if (packet == PUBREC_TYPE)
Azure.IoT Build 0:ef4901974abc 660 {
AzureIoTClient 3:9b4e7158ca0d 661 pubRel = mqtt_codec_publishRelease(publish_ack.packetId);
AzureIoTClient 5:34779607059c 662 if (pubRel == NULL)
AzureIoTClient 5:34779607059c 663 {
AzureIoTClient 11:a0a6a4cf7812 664 LOG(AZ_LOG_ERROR, LOG_LINE, "Failed to allocate publish release message.");
AzureIoTClient 9:37d14c31ff6e 665 set_error_callback(mqtt_client, MQTT_CLIENT_MEMORY_ERROR);
AzureIoTClient 5:34779607059c 666 }
Azure.IoT Build 0:ef4901974abc 667 }
AzureIoTClient 3:9b4e7158ca0d 668 else if (packet == PUBREL_TYPE)
Azure.IoT Build 0:ef4901974abc 669 {
AzureIoTClient 3:9b4e7158ca0d 670 pubRel = mqtt_codec_publishComplete(publish_ack.packetId);
AzureIoTClient 5:34779607059c 671 if (pubRel == NULL)
AzureIoTClient 5:34779607059c 672 {
AzureIoTClient 11:a0a6a4cf7812 673 LOG(AZ_LOG_ERROR, LOG_LINE, "Failed to allocate publish complete message.");
AzureIoTClient 9:37d14c31ff6e 674 set_error_callback(mqtt_client, MQTT_CLIENT_MEMORY_ERROR);
AzureIoTClient 5:34779607059c 675 }
Azure.IoT Build 0:ef4901974abc 676 }
Azure.IoT Build 0:ef4901974abc 677 if (pubRel != NULL)
Azure.IoT Build 0:ef4901974abc 678 {
AzureIoTClient 13:3c202001e4ba 679 size_t size = BUFFER_length(pubRel);
AzureIoTClient 13:3c202001e4ba 680 (void)sendPacketItem(mqtt_client, BUFFER_u_char(pubRel), size);
Azure.IoT Build 0:ef4901974abc 681 BUFFER_delete(pubRel);
Azure.IoT Build 0:ef4901974abc 682 }
Azure.IoT Build 0:ef4901974abc 683 }
AzureIoTClient 3:9b4e7158ca0d 684 break;
Azure.IoT Build 0:ef4901974abc 685 }
AzureIoTClient 3:9b4e7158ca0d 686 case SUBACK_TYPE:
Azure.IoT Build 0:ef4901974abc 687 {
AzureIoTClient 8:83bb166aba73 688 if (mqtt_client->fnOperationCallback)
AzureIoTClient 3:9b4e7158ca0d 689 {
AzureIoTClient 8:83bb166aba73 690 STRING_HANDLE trace_log = NULL;
AzureIoTClient 8:83bb166aba73 691
AzureIoTClient 3:9b4e7158ca0d 692 /*Codes_SRS_MQTT_CLIENT_07_030: [If the actionResult parameter is of type SUBACK_TYPE then the msgInfo value shall be a SUBSCRIBE_ACK structure.]*/
AzureIoTClient 3:9b4e7158ca0d 693 SUBSCRIBE_ACK suback = { 0 };
AzureIoTClient 3:9b4e7158ca0d 694
AzureIoTClient 3:9b4e7158ca0d 695 size_t remainLen = len;
AzureIoTClient 8:83bb166aba73 696 suback.packetId = byteutil_read_uint16(&iterator, len);
AzureIoTClient 3:9b4e7158ca0d 697 remainLen -= 2;
Azure.IoT Build 0:ef4901974abc 698
AzureIoTClient 8:83bb166aba73 699 if (mqtt_client->logTrace)
AzureIoTClient 8:83bb166aba73 700 {
AzureIoTClient 8:83bb166aba73 701 trace_log = STRING_construct_sprintf("SUBACK | PACKET_ID: %"PRIu16, suback.packetId);
AzureIoTClient 8:83bb166aba73 702 }
AzureIoTClient 8:83bb166aba73 703
AzureIoTClient 3:9b4e7158ca0d 704 // Allocate the remaining len
AzureIoTClient 3:9b4e7158ca0d 705 suback.qosReturn = (QOS_VALUE*)malloc(sizeof(QOS_VALUE)*remainLen);
AzureIoTClient 3:9b4e7158ca0d 706 if (suback.qosReturn != NULL)
Azure.IoT Build 0:ef4901974abc 707 {
AzureIoTClient 3:9b4e7158ca0d 708 while (remainLen > 0)
AzureIoTClient 3:9b4e7158ca0d 709 {
AzureIoTClient 3:9b4e7158ca0d 710 suback.qosReturn[suback.qosCount++] = byteutil_readByte(&iterator);
AzureIoTClient 3:9b4e7158ca0d 711 remainLen--;
AzureIoTClient 8:83bb166aba73 712 if (mqtt_client->logTrace)
AzureIoTClient 8:83bb166aba73 713 {
AzureIoTClient 8:83bb166aba73 714 STRING_sprintf(trace_log, " | RETURN_CODE: %"PRIu16, suback.qosReturn[suback.qosCount-1]);
AzureIoTClient 8:83bb166aba73 715 }
AzureIoTClient 3:9b4e7158ca0d 716 }
AzureIoTClient 8:83bb166aba73 717
AzureIoTClient 8:83bb166aba73 718 if (mqtt_client->logTrace)
AzureIoTClient 8:83bb166aba73 719 {
AzureIoTClient 8:83bb166aba73 720 log_incoming_trace(mqtt_client, trace_log);
AzureIoTClient 8:83bb166aba73 721 STRING_delete(trace_log);
AzureIoTClient 8:83bb166aba73 722 }
AzureIoTClient 8:83bb166aba73 723 mqtt_client->fnOperationCallback(mqtt_client, MQTT_CLIENT_ON_SUBSCRIBE_ACK, (void*)&suback, mqtt_client->ctx);
AzureIoTClient 3:9b4e7158ca0d 724 free(suback.qosReturn);
Azure.IoT Build 0:ef4901974abc 725 }
AzureIoTClient 5:34779607059c 726 else
AzureIoTClient 5:34779607059c 727 {
AzureIoTClient 11:a0a6a4cf7812 728 LOG(AZ_LOG_ERROR, LOG_LINE, "allocation of quality of service value failed.");
AzureIoTClient 9:37d14c31ff6e 729 set_error_callback(mqtt_client, MQTT_CLIENT_MEMORY_ERROR);
AzureIoTClient 5:34779607059c 730 }
Azure.IoT Build 0:ef4901974abc 731 }
AzureIoTClient 3:9b4e7158ca0d 732 break;
Azure.IoT Build 0:ef4901974abc 733 }
AzureIoTClient 3:9b4e7158ca0d 734 case UNSUBACK_TYPE:
Azure.IoT Build 0:ef4901974abc 735 {
AzureIoTClient 8:83bb166aba73 736 if (mqtt_client->fnOperationCallback)
AzureIoTClient 3:9b4e7158ca0d 737 {
AzureIoTClient 8:83bb166aba73 738 STRING_HANDLE trace_log = NULL;
AzureIoTClient 8:83bb166aba73 739
AzureIoTClient 3:9b4e7158ca0d 740 /*Codes_SRS_MQTT_CLIENT_07_031: [If the actionResult parameter is of type UNSUBACK_TYPE then the msgInfo value shall be a UNSUBSCRIBE_ACK structure.]*/
AzureIoTClient 3:9b4e7158ca0d 741 UNSUBSCRIBE_ACK unsuback = { 0 };
AzureIoTClient 3:9b4e7158ca0d 742 iterator += VARIABLE_HEADER_OFFSET;
AzureIoTClient 8:83bb166aba73 743 unsuback.packetId = byteutil_read_uint16(&iterator, len);
Azure.IoT Build 0:ef4901974abc 744
AzureIoTClient 8:83bb166aba73 745 if (mqtt_client->logTrace)
AzureIoTClient 8:83bb166aba73 746 {
AzureIoTClient 8:83bb166aba73 747 trace_log = STRING_construct_sprintf("UNSUBACK | PACKET_ID: %"PRIu16, unsuback.packetId);
AzureIoTClient 8:83bb166aba73 748 log_incoming_trace(mqtt_client, trace_log);
AzureIoTClient 8:83bb166aba73 749 STRING_delete(trace_log);
AzureIoTClient 8:83bb166aba73 750 }
AzureIoTClient 8:83bb166aba73 751 mqtt_client->fnOperationCallback(mqtt_client, MQTT_CLIENT_ON_UNSUBSCRIBE_ACK, (void*)&unsuback, mqtt_client->ctx);
AzureIoTClient 3:9b4e7158ca0d 752 }
AzureIoTClient 3:9b4e7158ca0d 753 break;
Azure.IoT Build 0:ef4901974abc 754 }
AzureIoTClient 3:9b4e7158ca0d 755 case PINGRESP_TYPE:
AzureIoTClient 8:83bb166aba73 756 mqtt_client->timeSincePing = 0;
AzureIoTClient 3:9b4e7158ca0d 757 // Ping responses do not get forwarded
AzureIoTClient 8:83bb166aba73 758 if (mqtt_client->logTrace)
AzureIoTClient 8:83bb166aba73 759 {
AzureIoTClient 8:83bb166aba73 760 STRING_HANDLE trace_log = STRING_construct_sprintf("PINGRESP");
AzureIoTClient 8:83bb166aba73 761 log_incoming_trace(mqtt_client, trace_log);
AzureIoTClient 8:83bb166aba73 762 STRING_delete(trace_log);
AzureIoTClient 8:83bb166aba73 763 }
AzureIoTClient 3:9b4e7158ca0d 764 break;
AzureIoTClient 3:9b4e7158ca0d 765 default:
AzureIoTClient 3:9b4e7158ca0d 766 break;
Azure.IoT Build 0:ef4901974abc 767 }
Azure.IoT Build 0:ef4901974abc 768 }
Azure.IoT Build 0:ef4901974abc 769 }
Azure.IoT Build 0:ef4901974abc 770 }
Azure.IoT Build 0:ef4901974abc 771
AzureIoTClient 9:37d14c31ff6e 772 MQTT_CLIENT_HANDLE mqtt_client_init(ON_MQTT_MESSAGE_RECV_CALLBACK msgRecv, ON_MQTT_OPERATION_CALLBACK opCallback, void* opCallbackCtx, ON_MQTT_ERROR_CALLBACK onErrorCallBack, void* errorCBCtx)
Azure.IoT Build 0:ef4901974abc 773 {
Azure.IoT Build 0:ef4901974abc 774 MQTT_CLIENT* result;
Azure.IoT Build 0:ef4901974abc 775 /*Codes_SRS_MQTT_CLIENT_07_001: [If the parameters ON_MQTT_MESSAGE_RECV_CALLBACK is NULL then mqttclient_init shall return NULL.]*/
Azure.IoT Build 0:ef4901974abc 776 if (msgRecv == NULL)
Azure.IoT Build 0:ef4901974abc 777 {
Azure.IoT Build 0:ef4901974abc 778 result = NULL;
Azure.IoT Build 0:ef4901974abc 779 }
Azure.IoT Build 0:ef4901974abc 780 else
Azure.IoT Build 0:ef4901974abc 781 {
Azure.IoT Build 0:ef4901974abc 782 result = malloc(sizeof(MQTT_CLIENT));
Azure.IoT Build 0:ef4901974abc 783 if (result == NULL)
Azure.IoT Build 0:ef4901974abc 784 {
Azure.IoT Build 0:ef4901974abc 785 /*Codes_SRS_MQTT_CLIENT_07_002: [If any failure is encountered then mqttclient_init shall return NULL.]*/
AzureIoTClient 11:a0a6a4cf7812 786 LOG(AZ_LOG_ERROR, LOG_LINE, "mqtt_client_init failure: Allocation Failure");
Azure.IoT Build 0:ef4901974abc 787 }
Azure.IoT Build 0:ef4901974abc 788 else
Azure.IoT Build 0:ef4901974abc 789 {
Azure.IoT Build 0:ef4901974abc 790 /*Codes_SRS_MQTT_CLIENT_07_003: [mqttclient_init shall allocate MQTTCLIENT_DATA_INSTANCE and return the MQTTCLIENT_HANDLE on success.]*/
Azure.IoT Build 0:ef4901974abc 791 result->xioHandle = NULL;
Azure.IoT Build 0:ef4901974abc 792 result->packetState = UNKNOWN_TYPE;
Azure.IoT Build 0:ef4901974abc 793 result->packetSendTimeMs = 0;
Azure.IoT Build 0:ef4901974abc 794 result->fnOperationCallback = opCallback;
AzureIoTClient 9:37d14c31ff6e 795 result->ctx = opCallbackCtx;
Azure.IoT Build 0:ef4901974abc 796 result->fnMessageRecv = msgRecv;
AzureIoTClient 9:37d14c31ff6e 797 result->fnOnErrorCallBack = onErrorCallBack;
AzureIoTClient 9:37d14c31ff6e 798 result->errorCBCtx = errorCBCtx;
Azure.IoT Build 0:ef4901974abc 799 result->qosValue = DELIVER_AT_MOST_ONCE;
Azure.IoT Build 0:ef4901974abc 800 result->keepAliveInterval = 0;
Azure.IoT Build 0:ef4901974abc 801 result->packetTickCntr = tickcounter_create();
Azure.IoT Build 0:ef4901974abc 802 result->mqttOptions.clientId = NULL;
Azure.IoT Build 0:ef4901974abc 803 result->mqttOptions.willTopic = NULL;
Azure.IoT Build 0:ef4901974abc 804 result->mqttOptions.willMessage = NULL;
Azure.IoT Build 0:ef4901974abc 805 result->mqttOptions.username = NULL;
Azure.IoT Build 0:ef4901974abc 806 result->mqttOptions.password = NULL;
Azure.IoT Build 0:ef4901974abc 807 result->socketConnected = false;
Azure.IoT Build 0:ef4901974abc 808 result->clientConnected = false;
Azure.IoT Build 0:ef4901974abc 809 result->logTrace = false;
Azure.IoT Build 0:ef4901974abc 810 result->rawBytesTrace = false;
AzureIoTClient 3:9b4e7158ca0d 811 result->timeSincePing = 0;
AzureIoTClient 3:9b4e7158ca0d 812 result->maxPingRespTime = DEFAULT_MAX_PING_RESPONSE_TIME;
Azure.IoT Build 0:ef4901974abc 813 if (result->packetTickCntr == NULL)
Azure.IoT Build 0:ef4901974abc 814 {
Azure.IoT Build 0:ef4901974abc 815 /*Codes_SRS_MQTT_CLIENT_07_002: [If any failure is encountered then mqttclient_init shall return NULL.]*/
AzureIoTClient 11:a0a6a4cf7812 816 LOG(AZ_LOG_ERROR, LOG_LINE, "mqtt_client_init failure: tickcounter_create failure");
Azure.IoT Build 0:ef4901974abc 817 free(result);
Azure.IoT Build 0:ef4901974abc 818 result = NULL;
Azure.IoT Build 0:ef4901974abc 819 }
Azure.IoT Build 0:ef4901974abc 820 else
Azure.IoT Build 0:ef4901974abc 821 {
Azure.IoT Build 0:ef4901974abc 822 result->codec_handle = mqtt_codec_create(recvCompleteCallback, result);
Azure.IoT Build 0:ef4901974abc 823 if (result->codec_handle == NULL)
Azure.IoT Build 0:ef4901974abc 824 {
Azure.IoT Build 0:ef4901974abc 825 /*Codes_SRS_MQTT_CLIENT_07_002: [If any failure is encountered then mqttclient_init shall return NULL.]*/
AzureIoTClient 11:a0a6a4cf7812 826 LOG(AZ_LOG_ERROR, LOG_LINE, "mqtt_client_init failure: mqtt_codec_create failure");
Azure.IoT Build 0:ef4901974abc 827 tickcounter_destroy(result->packetTickCntr);
Azure.IoT Build 0:ef4901974abc 828 free(result);
Azure.IoT Build 0:ef4901974abc 829 result = NULL;
Azure.IoT Build 0:ef4901974abc 830 }
Azure.IoT Build 0:ef4901974abc 831 }
Azure.IoT Build 0:ef4901974abc 832 }
Azure.IoT Build 0:ef4901974abc 833 }
Azure.IoT Build 0:ef4901974abc 834 return result;
Azure.IoT Build 0:ef4901974abc 835 }
Azure.IoT Build 0:ef4901974abc 836
Azure.IoT Build 0:ef4901974abc 837 void mqtt_client_deinit(MQTT_CLIENT_HANDLE handle)
Azure.IoT Build 0:ef4901974abc 838 {
Azure.IoT Build 0:ef4901974abc 839 /*Codes_SRS_MQTT_CLIENT_07_004: [If the parameter handle is NULL then function mqtt_client_deinit shall do nothing.]*/
Azure.IoT Build 0:ef4901974abc 840 if (handle != NULL)
Azure.IoT Build 0:ef4901974abc 841 {
Azure.IoT Build 0:ef4901974abc 842 /*Codes_SRS_MQTT_CLIENT_07_005: [mqtt_client_deinit shall deallocate all memory allocated in this unit.]*/
AzureIoTClient 8:83bb166aba73 843 MQTT_CLIENT* mqtt_client = (MQTT_CLIENT*)handle;
AzureIoTClient 8:83bb166aba73 844 tickcounter_destroy(mqtt_client->packetTickCntr);
AzureIoTClient 8:83bb166aba73 845 mqtt_codec_destroy(mqtt_client->codec_handle);
AzureIoTClient 8:83bb166aba73 846 free(mqtt_client);
Azure.IoT Build 0:ef4901974abc 847 }
Azure.IoT Build 0:ef4901974abc 848 }
Azure.IoT Build 0:ef4901974abc 849
Azure.IoT Build 0:ef4901974abc 850 int mqtt_client_connect(MQTT_CLIENT_HANDLE handle, XIO_HANDLE xioHandle, MQTT_CLIENT_OPTIONS* mqttOptions)
Azure.IoT Build 0:ef4901974abc 851 {
Azure.IoT Build 0:ef4901974abc 852 int result;
Azure.IoT Build 0:ef4901974abc 853 /*SRS_MQTT_CLIENT_07_006: [If any of the parameters handle, ioHandle, or mqttOptions are NULL then mqtt_client_connect shall return a non-zero value.]*/
Azure.IoT Build 0:ef4901974abc 854 if (handle == NULL || mqttOptions == NULL)
Azure.IoT Build 0:ef4901974abc 855 {
AzureIoTClient 11:a0a6a4cf7812 856 LOG(AZ_LOG_ERROR, LOG_LINE, "mqtt_client_connect: NULL argument (handle = %p, mqttOptions = %p)", handle, mqttOptions);
Azure.IoT Build 0:ef4901974abc 857 result = __LINE__;
Azure.IoT Build 0:ef4901974abc 858 }
Azure.IoT Build 0:ef4901974abc 859 else
Azure.IoT Build 0:ef4901974abc 860 {
AzureIoTClient 8:83bb166aba73 861 MQTT_CLIENT* mqtt_client = (MQTT_CLIENT*)handle;
Azure.IoT Build 0:ef4901974abc 862 if (xioHandle == NULL)
Azure.IoT Build 0:ef4901974abc 863 {
Azure.IoT Build 0:ef4901974abc 864 /*Codes_SRS_MQTT_CLIENT_07_007: [If any failure is encountered then mqtt_client_connect shall return a non-zero value.]*/
AzureIoTClient 11:a0a6a4cf7812 865 LOG(AZ_LOG_ERROR, LOG_LINE, "Error: mqttcodec_connect failed");
Azure.IoT Build 0:ef4901974abc 866 result = __LINE__;
Azure.IoT Build 0:ef4901974abc 867 }
Azure.IoT Build 0:ef4901974abc 868 else
Azure.IoT Build 0:ef4901974abc 869 {
AzureIoTClient 8:83bb166aba73 870 mqtt_client->xioHandle = xioHandle;
AzureIoTClient 8:83bb166aba73 871 mqtt_client->packetState = UNKNOWN_TYPE;
AzureIoTClient 8:83bb166aba73 872 mqtt_client->qosValue = mqttOptions->qualityOfServiceValue;
AzureIoTClient 8:83bb166aba73 873 mqtt_client->keepAliveInterval = mqttOptions->keepAliveInterval;
AzureIoTClient 8:83bb166aba73 874 mqtt_client->maxPingRespTime = (DEFAULT_MAX_PING_RESPONSE_TIME < mqttOptions->keepAliveInterval/2) ? DEFAULT_MAX_PING_RESPONSE_TIME : mqttOptions->keepAliveInterval/2;
AzureIoTClient 8:83bb166aba73 875 if (cloneMqttOptions(mqtt_client, mqttOptions) != 0)
Azure.IoT Build 0:ef4901974abc 876 {
AzureIoTClient 11:a0a6a4cf7812 877 LOG(AZ_LOG_ERROR, LOG_LINE, "Error: Clone Mqtt Options failed");
Azure.IoT Build 0:ef4901974abc 878 result = __LINE__;
Azure.IoT Build 0:ef4901974abc 879 }
Azure.IoT Build 0:ef4901974abc 880 /*Codes_SRS_MQTT_CLIENT_07_008: [mqtt_client_connect shall open the XIO_HANDLE by calling into the xio_open interface.]*/
AzureIoTClient 8:83bb166aba73 881 else if (xio_open(xioHandle, onOpenComplete, mqtt_client, onBytesReceived, mqtt_client, onIoError, mqtt_client) != 0)
Azure.IoT Build 0:ef4901974abc 882 {
Azure.IoT Build 0:ef4901974abc 883 /*Codes_SRS_MQTT_CLIENT_07_007: [If any failure is encountered then mqtt_client_connect shall return a non-zero value.]*/
AzureIoTClient 11:a0a6a4cf7812 884 LOG(AZ_LOG_ERROR, LOG_LINE, "Error: io_open failed");
Azure.IoT Build 0:ef4901974abc 885 result = __LINE__;
Azure.IoT Build 0:ef4901974abc 886 }
Azure.IoT Build 0:ef4901974abc 887 else
Azure.IoT Build 0:ef4901974abc 888 {
Azure.IoT Build 0:ef4901974abc 889 result = 0;
Azure.IoT Build 0:ef4901974abc 890 }
Azure.IoT Build 0:ef4901974abc 891 }
Azure.IoT Build 0:ef4901974abc 892 }
Azure.IoT Build 0:ef4901974abc 893 return result;
Azure.IoT Build 0:ef4901974abc 894 }
Azure.IoT Build 0:ef4901974abc 895
Azure.IoT Build 0:ef4901974abc 896 int mqtt_client_publish(MQTT_CLIENT_HANDLE handle, MQTT_MESSAGE_HANDLE msgHandle)
Azure.IoT Build 0:ef4901974abc 897 {
Azure.IoT Build 0:ef4901974abc 898 int result;
AzureIoTClient 8:83bb166aba73 899 MQTT_CLIENT* mqtt_client = (MQTT_CLIENT*)handle;
AzureIoTClient 8:83bb166aba73 900 if (mqtt_client == NULL || msgHandle == NULL)
Azure.IoT Build 0:ef4901974abc 901 {
Azure.IoT Build 0:ef4901974abc 902 /*Codes_SRS_MQTT_CLIENT_07_019: [If one of the parameters handle or msgHandle is NULL then mqtt_client_publish shall return a non-zero value.]*/
Azure.IoT Build 0:ef4901974abc 903 result = __LINE__;
Azure.IoT Build 0:ef4901974abc 904 }
Azure.IoT Build 0:ef4901974abc 905 else
Azure.IoT Build 0:ef4901974abc 906 {
Azure.IoT Build 0:ef4901974abc 907 /*Codes_SRS_MQTT_CLIENT_07_021: [mqtt_client_publish shall get the message information from the MQTT_MESSAGE_HANDLE.]*/
Azure.IoT Build 0:ef4901974abc 908 const APP_PAYLOAD* payload = mqttmessage_getApplicationMsg(msgHandle);
Azure.IoT Build 0:ef4901974abc 909 if (payload == NULL)
Azure.IoT Build 0:ef4901974abc 910 {
Azure.IoT Build 0:ef4901974abc 911 /*Codes_SRS_MQTT_CLIENT_07_020: [If any failure is encountered then mqtt_client_unsubscribe shall return a non-zero value.]*/
AzureIoTClient 11:a0a6a4cf7812 912 LOG(AZ_LOG_ERROR, LOG_LINE, "Error: mqttmessage_getApplicationMsg failed");
Azure.IoT Build 0:ef4901974abc 913 result = __LINE__;
Azure.IoT Build 0:ef4901974abc 914 }
Azure.IoT Build 0:ef4901974abc 915 else
Azure.IoT Build 0:ef4901974abc 916 {
AzureIoTClient 8:83bb166aba73 917 STRING_HANDLE trace_log = construct_trace_log_handle(mqtt_client);
AzureIoTClient 8:83bb166aba73 918
AzureIoTClient 13:3c202001e4ba 919 QOS_VALUE qos = mqttmessage_getQosType(msgHandle);
AzureIoTClient 13:3c202001e4ba 920 bool isDuplicate = mqttmessage_getIsDuplicateMsg(msgHandle);
AzureIoTClient 13:3c202001e4ba 921 bool isRetained = mqttmessage_getIsRetained(msgHandle);
AzureIoTClient 13:3c202001e4ba 922 uint16_t packetId = mqttmessage_getPacketId(msgHandle);
AzureIoTClient 13:3c202001e4ba 923 const char* topicName = mqttmessage_getTopicName(msgHandle);
AzureIoTClient 13:3c202001e4ba 924 BUFFER_HANDLE publishPacket = mqtt_codec_publish(qos, isDuplicate, isRetained, packetId, topicName, payload->message, payload->length, trace_log);
Azure.IoT Build 0:ef4901974abc 925 if (publishPacket == NULL)
Azure.IoT Build 0:ef4901974abc 926 {
Azure.IoT Build 0:ef4901974abc 927 /*Codes_SRS_MQTT_CLIENT_07_020: [If any failure is encountered then mqtt_client_unsubscribe shall return a non-zero value.]*/
AzureIoTClient 11:a0a6a4cf7812 928 LOG(AZ_LOG_ERROR, LOG_LINE, "Error: mqtt_codec_publish failed");
Azure.IoT Build 0:ef4901974abc 929 result = __LINE__;
Azure.IoT Build 0:ef4901974abc 930 }
Azure.IoT Build 0:ef4901974abc 931 else
Azure.IoT Build 0:ef4901974abc 932 {
AzureIoTClient 8:83bb166aba73 933 mqtt_client->packetState = PUBLISH_TYPE;
Azure.IoT Build 0:ef4901974abc 934
Azure.IoT Build 0:ef4901974abc 935 /*Codes_SRS_MQTT_CLIENT_07_022: [On success mqtt_client_publish shall send the MQTT SUBCRIBE packet to the endpoint.]*/
AzureIoTClient 13:3c202001e4ba 936 size_t size = BUFFER_length(publishPacket);
AzureIoTClient 13:3c202001e4ba 937 if (sendPacketItem(mqtt_client, BUFFER_u_char(publishPacket), size) != 0)
Azure.IoT Build 0:ef4901974abc 938 {
Azure.IoT Build 0:ef4901974abc 939 /*Codes_SRS_MQTT_CLIENT_07_020: [If any failure is encountered then mqtt_client_unsubscribe shall return a non-zero value.]*/
AzureIoTClient 11:a0a6a4cf7812 940 LOG(AZ_LOG_ERROR, LOG_LINE, "Error: mqtt_client_publish send failed");
Azure.IoT Build 0:ef4901974abc 941 result = __LINE__;
Azure.IoT Build 0:ef4901974abc 942 }
Azure.IoT Build 0:ef4901974abc 943 else
Azure.IoT Build 0:ef4901974abc 944 {
AzureIoTClient 8:83bb166aba73 945 log_outgoing_trace(mqtt_client, trace_log);
Azure.IoT Build 0:ef4901974abc 946 result = 0;
Azure.IoT Build 0:ef4901974abc 947 }
Azure.IoT Build 0:ef4901974abc 948 BUFFER_delete(publishPacket);
Azure.IoT Build 0:ef4901974abc 949 }
AzureIoTClient 8:83bb166aba73 950 if (trace_log != NULL)
AzureIoTClient 8:83bb166aba73 951 {
AzureIoTClient 8:83bb166aba73 952 STRING_delete(trace_log);
AzureIoTClient 8:83bb166aba73 953 }
Azure.IoT Build 0:ef4901974abc 954 }
Azure.IoT Build 0:ef4901974abc 955 }
Azure.IoT Build 0:ef4901974abc 956 return result;
Azure.IoT Build 0:ef4901974abc 957 }
Azure.IoT Build 0:ef4901974abc 958
Azure.IoT Build 0:ef4901974abc 959 int mqtt_client_subscribe(MQTT_CLIENT_HANDLE handle, uint16_t packetId, SUBSCRIBE_PAYLOAD* subscribeList, size_t count)
Azure.IoT Build 0:ef4901974abc 960 {
Azure.IoT Build 0:ef4901974abc 961 int result;
AzureIoTClient 8:83bb166aba73 962 MQTT_CLIENT* mqtt_client = (MQTT_CLIENT*)handle;
AzureIoTClient 8:83bb166aba73 963 if (mqtt_client == NULL || subscribeList == NULL || count == 0)
Azure.IoT Build 0:ef4901974abc 964 {
Azure.IoT Build 0:ef4901974abc 965 /*Codes_SRS_MQTT_CLIENT_07_013: [If any of the parameters handle, subscribeList is NULL or count is 0 then mqtt_client_subscribe shall return a non-zero value.]*/
Azure.IoT Build 0:ef4901974abc 966 result = __LINE__;
Azure.IoT Build 0:ef4901974abc 967 }
Azure.IoT Build 0:ef4901974abc 968 else
Azure.IoT Build 0:ef4901974abc 969 {
AzureIoTClient 8:83bb166aba73 970 STRING_HANDLE trace_log = construct_trace_log_handle(mqtt_client);
AzureIoTClient 8:83bb166aba73 971
AzureIoTClient 8:83bb166aba73 972 BUFFER_HANDLE subPacket = mqtt_codec_subscribe(packetId, subscribeList, count, trace_log);
Azure.IoT Build 0:ef4901974abc 973 if (subPacket == NULL)
Azure.IoT Build 0:ef4901974abc 974 {
Azure.IoT Build 0:ef4901974abc 975 /*Codes_SRS_MQTT_CLIENT_07_014: [If any failure is encountered then mqtt_client_subscribe shall return a non-zero value.]*/
AzureIoTClient 11:a0a6a4cf7812 976 LOG(AZ_LOG_ERROR, LOG_LINE, "Error: mqtt_codec_subscribe failed");
Azure.IoT Build 0:ef4901974abc 977 result = __LINE__;
Azure.IoT Build 0:ef4901974abc 978 }
Azure.IoT Build 0:ef4901974abc 979 else
Azure.IoT Build 0:ef4901974abc 980 {
AzureIoTClient 8:83bb166aba73 981 mqtt_client->packetState = SUBSCRIBE_TYPE;
Azure.IoT Build 0:ef4901974abc 982
AzureIoTClient 13:3c202001e4ba 983 size_t size = BUFFER_length(subPacket);
Azure.IoT Build 0:ef4901974abc 984 /*Codes_SRS_MQTT_CLIENT_07_015: [On success mqtt_client_subscribe shall send the MQTT SUBCRIBE packet to the endpoint.]*/
AzureIoTClient 13:3c202001e4ba 985 if (sendPacketItem(mqtt_client, BUFFER_u_char(subPacket), size) != 0)
Azure.IoT Build 0:ef4901974abc 986 {
Azure.IoT Build 0:ef4901974abc 987 /*Codes_SRS_MQTT_CLIENT_07_014: [If any failure is encountered then mqtt_client_subscribe shall return a non-zero value.]*/
AzureIoTClient 11:a0a6a4cf7812 988 LOG(AZ_LOG_ERROR, LOG_LINE, "Error: mqtt_client_subscribe send failed");
Azure.IoT Build 0:ef4901974abc 989 result = __LINE__;
Azure.IoT Build 0:ef4901974abc 990 }
Azure.IoT Build 0:ef4901974abc 991 else
Azure.IoT Build 0:ef4901974abc 992 {
AzureIoTClient 8:83bb166aba73 993 log_outgoing_trace(mqtt_client, trace_log);
Azure.IoT Build 0:ef4901974abc 994 result = 0;
Azure.IoT Build 0:ef4901974abc 995 }
Azure.IoT Build 0:ef4901974abc 996 BUFFER_delete(subPacket);
Azure.IoT Build 0:ef4901974abc 997 }
AzureIoTClient 8:83bb166aba73 998 if (trace_log != NULL)
AzureIoTClient 8:83bb166aba73 999 {
AzureIoTClient 8:83bb166aba73 1000 STRING_delete(trace_log);
AzureIoTClient 8:83bb166aba73 1001 }
Azure.IoT Build 0:ef4901974abc 1002 }
Azure.IoT Build 0:ef4901974abc 1003 return result;
Azure.IoT Build 0:ef4901974abc 1004 }
Azure.IoT Build 0:ef4901974abc 1005
Azure.IoT Build 0:ef4901974abc 1006 int mqtt_client_unsubscribe(MQTT_CLIENT_HANDLE handle, uint16_t packetId, const char** unsubscribeList, size_t count)
Azure.IoT Build 0:ef4901974abc 1007 {
Azure.IoT Build 0:ef4901974abc 1008 int result;
AzureIoTClient 8:83bb166aba73 1009 MQTT_CLIENT* mqtt_client = (MQTT_CLIENT*)handle;
AzureIoTClient 8:83bb166aba73 1010 if (mqtt_client == NULL || unsubscribeList == NULL || count == 0)
Azure.IoT Build 0:ef4901974abc 1011 {
Azure.IoT Build 0:ef4901974abc 1012 /*Codes_SRS_MQTT_CLIENT_07_016: [If any of the parameters handle, unsubscribeList is NULL or count is 0 then mqtt_client_unsubscribe shall return a non-zero value.]*/
Azure.IoT Build 0:ef4901974abc 1013 result = __LINE__;
Azure.IoT Build 0:ef4901974abc 1014 }
Azure.IoT Build 0:ef4901974abc 1015 else
Azure.IoT Build 0:ef4901974abc 1016 {
AzureIoTClient 8:83bb166aba73 1017 STRING_HANDLE trace_log = construct_trace_log_handle(mqtt_client);
AzureIoTClient 8:83bb166aba73 1018
AzureIoTClient 8:83bb166aba73 1019 BUFFER_HANDLE unsubPacket = mqtt_codec_unsubscribe(packetId, unsubscribeList, count, trace_log);
Azure.IoT Build 0:ef4901974abc 1020 if (unsubPacket == NULL)
Azure.IoT Build 0:ef4901974abc 1021 {
Azure.IoT Build 0:ef4901974abc 1022 /*Codes_SRS_MQTT_CLIENT_07_017: [If any failure is encountered then mqtt_client_unsubscribe shall return a non-zero value.]*/
AzureIoTClient 11:a0a6a4cf7812 1023 LOG(AZ_LOG_ERROR, LOG_LINE, "Error: mqtt_codec_unsubscribe failed");
Azure.IoT Build 0:ef4901974abc 1024 result = __LINE__;
Azure.IoT Build 0:ef4901974abc 1025 }
Azure.IoT Build 0:ef4901974abc 1026 else
Azure.IoT Build 0:ef4901974abc 1027 {
AzureIoTClient 8:83bb166aba73 1028 mqtt_client->packetState = UNSUBSCRIBE_TYPE;
Azure.IoT Build 0:ef4901974abc 1029
AzureIoTClient 13:3c202001e4ba 1030 size_t size = BUFFER_length(unsubPacket);
Azure.IoT Build 0:ef4901974abc 1031 /*Codes_SRS_MQTT_CLIENT_07_018: [On success mqtt_client_unsubscribe shall send the MQTT SUBCRIBE packet to the endpoint.]*/
AzureIoTClient 13:3c202001e4ba 1032 if (sendPacketItem(mqtt_client, BUFFER_u_char(unsubPacket), size) != 0)
Azure.IoT Build 0:ef4901974abc 1033 {
Azure.IoT Build 0:ef4901974abc 1034 /*Codes_SRS_MQTT_CLIENT_07_017: [If any failure is encountered then mqtt_client_unsubscribe shall return a non-zero value.].]*/
AzureIoTClient 11:a0a6a4cf7812 1035 LOG(AZ_LOG_ERROR, LOG_LINE, "Error: mqtt_client_unsubscribe send failed");
Azure.IoT Build 0:ef4901974abc 1036 result = __LINE__;
Azure.IoT Build 0:ef4901974abc 1037 }
Azure.IoT Build 0:ef4901974abc 1038 else
Azure.IoT Build 0:ef4901974abc 1039 {
AzureIoTClient 8:83bb166aba73 1040 log_outgoing_trace(mqtt_client, trace_log);
Azure.IoT Build 0:ef4901974abc 1041 result = 0;
Azure.IoT Build 0:ef4901974abc 1042 }
Azure.IoT Build 0:ef4901974abc 1043 BUFFER_delete(unsubPacket);
Azure.IoT Build 0:ef4901974abc 1044 }
AzureIoTClient 8:83bb166aba73 1045 if (trace_log != NULL)
AzureIoTClient 8:83bb166aba73 1046 {
AzureIoTClient 8:83bb166aba73 1047 STRING_delete(trace_log);
AzureIoTClient 8:83bb166aba73 1048 }
Azure.IoT Build 0:ef4901974abc 1049 }
Azure.IoT Build 0:ef4901974abc 1050 return result;
Azure.IoT Build 0:ef4901974abc 1051 }
Azure.IoT Build 0:ef4901974abc 1052
Azure.IoT Build 0:ef4901974abc 1053 int mqtt_client_disconnect(MQTT_CLIENT_HANDLE handle)
Azure.IoT Build 0:ef4901974abc 1054 {
Azure.IoT Build 0:ef4901974abc 1055 int result;
AzureIoTClient 8:83bb166aba73 1056 MQTT_CLIENT* mqtt_client = (MQTT_CLIENT*)handle;
AzureIoTClient 8:83bb166aba73 1057 if (mqtt_client == NULL)
Azure.IoT Build 0:ef4901974abc 1058 {
Azure.IoT Build 0:ef4901974abc 1059 /*Codes_SRS_MQTT_CLIENT_07_010: [If the parameters handle is NULL then mqtt_client_disconnect shall return a non-zero value.]*/
Azure.IoT Build 0:ef4901974abc 1060 result = __LINE__;
Azure.IoT Build 0:ef4901974abc 1061 }
Azure.IoT Build 0:ef4901974abc 1062 else
Azure.IoT Build 0:ef4901974abc 1063 {
Azure.IoT Build 0:ef4901974abc 1064 BUFFER_HANDLE disconnectPacket = mqtt_codec_disconnect();
Azure.IoT Build 0:ef4901974abc 1065 if (disconnectPacket == NULL)
Azure.IoT Build 0:ef4901974abc 1066 {
Azure.IoT Build 0:ef4901974abc 1067 /*Codes_SRS_MQTT_CLIENT_07_011: [If any failure is encountered then mqtt_client_disconnect shall return a non-zero value.]*/
AzureIoTClient 11:a0a6a4cf7812 1068 LOG(AZ_LOG_ERROR, LOG_LINE, "Error: mqtt_client_disconnect failed");
AzureIoTClient 8:83bb166aba73 1069 mqtt_client->packetState = PACKET_TYPE_ERROR;
Azure.IoT Build 0:ef4901974abc 1070 result = __LINE__;
Azure.IoT Build 0:ef4901974abc 1071 }
Azure.IoT Build 0:ef4901974abc 1072 else
Azure.IoT Build 0:ef4901974abc 1073 {
AzureIoTClient 8:83bb166aba73 1074 mqtt_client->packetState = DISCONNECT_TYPE;
Azure.IoT Build 0:ef4901974abc 1075
AzureIoTClient 13:3c202001e4ba 1076 size_t size = BUFFER_length(disconnectPacket);
Azure.IoT Build 0:ef4901974abc 1077 /*Codes_SRS_MQTT_CLIENT_07_012: [On success mqtt_client_disconnect shall send the MQTT DISCONNECT packet to the endpoint.]*/
AzureIoTClient 13:3c202001e4ba 1078 if (sendPacketItem(mqtt_client, BUFFER_u_char(disconnectPacket), size) != 0)
Azure.IoT Build 0:ef4901974abc 1079 {
Azure.IoT Build 0:ef4901974abc 1080 /*Codes_SRS_MQTT_CLIENT_07_011: [If any failure is encountered then mqtt_client_disconnect shall return a non-zero value.]*/
AzureIoTClient 11:a0a6a4cf7812 1081 LOG(AZ_LOG_ERROR, LOG_LINE, "Error: mqtt_client_disconnect send failed");
Azure.IoT Build 0:ef4901974abc 1082 result = __LINE__;
Azure.IoT Build 0:ef4901974abc 1083 }
Azure.IoT Build 0:ef4901974abc 1084 else
Azure.IoT Build 0:ef4901974abc 1085 {
AzureIoTClient 8:83bb166aba73 1086 if (mqtt_client->logTrace)
AzureIoTClient 8:83bb166aba73 1087 {
AzureIoTClient 8:83bb166aba73 1088 STRING_HANDLE trace_log = STRING_construct("DISCONNECT");
AzureIoTClient 8:83bb166aba73 1089 log_outgoing_trace(mqtt_client, trace_log);
AzureIoTClient 8:83bb166aba73 1090 STRING_delete(trace_log);
AzureIoTClient 8:83bb166aba73 1091 }
Azure.IoT Build 0:ef4901974abc 1092 result = 0;
Azure.IoT Build 0:ef4901974abc 1093 }
Azure.IoT Build 0:ef4901974abc 1094 BUFFER_delete(disconnectPacket);
AzureIoTClient 13:3c202001e4ba 1095 free(mqtt_client->mqttOptions.clientId);
AzureIoTClient 13:3c202001e4ba 1096 free(mqtt_client->mqttOptions.willTopic);
AzureIoTClient 13:3c202001e4ba 1097 free(mqtt_client->mqttOptions.willMessage);
AzureIoTClient 13:3c202001e4ba 1098 free(mqtt_client->mqttOptions.username);
AzureIoTClient 13:3c202001e4ba 1099 free(mqtt_client->mqttOptions.password);
Azure.IoT Build 0:ef4901974abc 1100 }
Azure.IoT Build 0:ef4901974abc 1101 }
Azure.IoT Build 0:ef4901974abc 1102 return result;
Azure.IoT Build 0:ef4901974abc 1103 }
Azure.IoT Build 0:ef4901974abc 1104
Azure.IoT Build 0:ef4901974abc 1105 void mqtt_client_dowork(MQTT_CLIENT_HANDLE handle)
Azure.IoT Build 0:ef4901974abc 1106 {
AzureIoTClient 8:83bb166aba73 1107 MQTT_CLIENT* mqtt_client = (MQTT_CLIENT*)handle;
Azure.IoT Build 0:ef4901974abc 1108 /*Codes_SRS_MQTT_CLIENT_07_023: [If the parameter handle is NULL then mqtt_client_dowork shall do nothing.]*/
AzureIoTClient 8:83bb166aba73 1109 if (mqtt_client != NULL)
Azure.IoT Build 0:ef4901974abc 1110 {
Azure.IoT Build 0:ef4901974abc 1111 /*Codes_SRS_MQTT_CLIENT_07_024: [mqtt_client_dowork shall call the xio_dowork function to complete operations.]*/
AzureIoTClient 8:83bb166aba73 1112 xio_dowork(mqtt_client->xioHandle);
Azure.IoT Build 0:ef4901974abc 1113
Azure.IoT Build 0:ef4901974abc 1114 /*Codes_SRS_MQTT_CLIENT_07_025: [mqtt_client_dowork shall retrieve the the last packet send value and ...]*/
AzureIoTClient 8:83bb166aba73 1115 if (mqtt_client->socketConnected && mqtt_client->clientConnected && mqtt_client->keepAliveInterval > 0)
Azure.IoT Build 0:ef4901974abc 1116 {
Azure.IoT.Build 10:2ab268507775 1117 tickcounter_ms_t current_ms;
AzureIoTClient 8:83bb166aba73 1118 if (tickcounter_get_current_ms(mqtt_client->packetTickCntr, &current_ms) != 0)
Azure.IoT Build 0:ef4901974abc 1119 {
AzureIoTClient 11:a0a6a4cf7812 1120 LOG(AZ_LOG_ERROR, LOG_LINE, "Error: tickcounter_get_current_ms failed");
Azure.IoT Build 0:ef4901974abc 1121 }
Azure.IoT Build 0:ef4901974abc 1122 else
Azure.IoT Build 0:ef4901974abc 1123 {
AzureIoTClient 9:37d14c31ff6e 1124 /* Codes_SRS_MQTT_CLIENT_07_035: [If the timeSincePing has expired past the maxPingRespTime then mqtt_client_dowork shall call the Error Callback function with the message MQTT_CLIENT_NO_PING_RESPONSE] */
AzureIoTClient 8:83bb166aba73 1125 if (mqtt_client->timeSincePing > 0 && ((current_ms - mqtt_client->timeSincePing)/1000) > mqtt_client->maxPingRespTime)
AzureIoTClient 3:9b4e7158ca0d 1126 {
AzureIoTClient 3:9b4e7158ca0d 1127 // We haven't gotten a ping response in the alloted time
AzureIoTClient 9:37d14c31ff6e 1128 set_error_callback(mqtt_client, MQTT_CLIENT_NO_PING_RESPONSE);
AzureIoTClient 8:83bb166aba73 1129 mqtt_client->timeSincePing = 0;
AzureIoTClient 8:83bb166aba73 1130 mqtt_client->packetSendTimeMs = 0;
AzureIoTClient 8:83bb166aba73 1131 mqtt_client->packetState = UNKNOWN_TYPE;
AzureIoTClient 3:9b4e7158ca0d 1132 }
AzureIoTClient 8:83bb166aba73 1133 else if ((((current_ms - mqtt_client->packetSendTimeMs) / 1000) + KEEP_ALIVE_BUFFER_SEC) > mqtt_client->keepAliveInterval)
Azure.IoT Build 0:ef4901974abc 1134 {
Azure.IoT Build 0:ef4901974abc 1135 /*Codes_SRS_MQTT_CLIENT_07_026: [if keepAliveInternal is > 0 and the send time is greater than the MQTT KeepAliveInterval then it shall construct an MQTT PINGREQ packet.]*/
Azure.IoT Build 0:ef4901974abc 1136 BUFFER_HANDLE pingPacket = mqtt_codec_ping();
Azure.IoT Build 0:ef4901974abc 1137 if (pingPacket != NULL)
Azure.IoT Build 0:ef4901974abc 1138 {
AzureIoTClient 13:3c202001e4ba 1139 size_t size = BUFFER_length(pingPacket);
AzureIoTClient 13:3c202001e4ba 1140 (void)sendPacketItem(mqtt_client, BUFFER_u_char(pingPacket), size);
Azure.IoT Build 0:ef4901974abc 1141 BUFFER_delete(pingPacket);
AzureIoTClient 8:83bb166aba73 1142 (void)tickcounter_get_current_ms(mqtt_client->packetTickCntr, &mqtt_client->timeSincePing);
AzureIoTClient 8:83bb166aba73 1143
AzureIoTClient 8:83bb166aba73 1144 if (mqtt_client->logTrace)
AzureIoTClient 8:83bb166aba73 1145 {
AzureIoTClient 8:83bb166aba73 1146 STRING_HANDLE trace_log = STRING_construct("PINGREQ");
AzureIoTClient 8:83bb166aba73 1147 log_outgoing_trace(mqtt_client, trace_log);
AzureIoTClient 8:83bb166aba73 1148 STRING_delete(trace_log);
AzureIoTClient 8:83bb166aba73 1149 }
Azure.IoT Build 0:ef4901974abc 1150 }
Azure.IoT Build 0:ef4901974abc 1151 }
Azure.IoT Build 0:ef4901974abc 1152 }
Azure.IoT Build 0:ef4901974abc 1153 }
Azure.IoT Build 0:ef4901974abc 1154 }
Azure.IoT Build 0:ef4901974abc 1155 }
Azure.IoT Build 0:ef4901974abc 1156
Azure.IoT Build 0:ef4901974abc 1157 void mqtt_client_set_trace(MQTT_CLIENT_HANDLE handle, bool traceOn, bool rawBytesOn)
Azure.IoT Build 0:ef4901974abc 1158 {
AzureIoTClient 8:83bb166aba73 1159 MQTT_CLIENT* mqtt_client = (MQTT_CLIENT*)handle;
AzureIoTClient 8:83bb166aba73 1160 if (mqtt_client != NULL)
Azure.IoT Build 0:ef4901974abc 1161 {
AzureIoTClient 8:83bb166aba73 1162 mqtt_client->logTrace = traceOn;
AzureIoTClient 8:83bb166aba73 1163 mqtt_client->rawBytesTrace = rawBytesOn;
Azure.IoT Build 0:ef4901974abc 1164 }
Azure.IoT Build 0:ef4901974abc 1165 }