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:
Wed Nov 16 21:38:15 2016 -0800
Revision:
9:37d14c31ff6e
Parent:
8:83bb166aba73
Child:
10:2ab268507775
1.0.10

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