A small footprint MQTT library

Dependents:   STM32F746_iothub_client_sample_mqtt FXOS8700CQ_To_Azure_IoT f767zi_mqtt FXOS8700CQ_To_Azure_IoT ... more

Revision:
11:a0a6a4cf7812
Parent:
10:2ab268507775
Child:
12:30b08cda82fd
--- a/mqtt_client.c	Wed Dec 14 16:00:23 2016 -0800
+++ b/mqtt_client.c	Fri Jan 13 18:40:50 2017 -0800
@@ -109,7 +109,7 @@
     }
     else
     {
-        LOG(LOG_ERROR, LOG_LINE, "byteutil_read_uint16 == NULL or less than 2");
+        LOG(AZ_LOG_ERROR, LOG_LINE, "byteutil_read_uint16 == NULL or less than 2");
     }
     return result;
 }
@@ -138,7 +138,7 @@
     }
     else
     {
-        LOG(LOG_ERROR, LOG_LINE, "readByte buffer == NULL.");
+        LOG(AZ_LOG_ERROR, LOG_LINE, "readByte buffer == NULL.");
     }
     return result;
 }
@@ -153,7 +153,7 @@
     }
     else
     {
-        LOG(LOG_ERROR, LOG_LINE, "readByte buffer == NULL.");
+        LOG(AZ_LOG_ERROR, LOG_LINE, "readByte buffer == NULL.");
     }
     return result;
 }
@@ -178,13 +178,13 @@
         }
         else if (send_result == IO_SEND_ERROR)
         {
-            LOG(LOG_ERROR, LOG_LINE, "MQTT Send Complete Failure send_result: %d", (int)send_result);
+            LOG(AZ_LOG_ERROR, LOG_LINE, "MQTT Send Complete Failure send_result: %d", (int)send_result);
             set_error_callback(mqtt_client, MQTT_CLIENT_COMMUNICATION_ERROR);
         }
     }
     else
     {
-        LOG(LOG_ERROR, LOG_LINE, "MQTT Send Complete Failure with NULL mqtt_client");
+        LOG(AZ_LOG_ERROR, LOG_LINE, "MQTT Send Complete Failure with NULL mqtt_client");
     }
 }
 
@@ -245,7 +245,7 @@
     {
         char tmBuffer[TIME_MAX_BUFFER];
         getLogTime(tmBuffer, TIME_MAX_BUFFER);
-        LOG(LOG_TRACE, LOG_LINE, "-> %s %s", tmBuffer, STRING_c_str(trace_log));
+        LOG(AZ_LOG_TRACE, LOG_LINE, "-> %s %s", tmBuffer, STRING_c_str(trace_log));
     }
 }
 
@@ -256,12 +256,12 @@
         char tmBuffer[TIME_MAX_BUFFER];
         getLogTime(tmBuffer, TIME_MAX_BUFFER);
 
-        LOG(LOG_TRACE, 0, "-> %s %s: ", tmBuffer, retrievePacketType((unsigned char)data[0]));
+        LOG(AZ_LOG_TRACE, 0, "-> %s %s: ", tmBuffer, retrievePacketType((unsigned char)data[0]));
         for (size_t index = 0; index < length; index++)
         {
-            LOG(LOG_TRACE, 0, "0x%02x ", data[index]);
+            LOG(AZ_LOG_TRACE, 0, "0x%02x ", data[index]);
         }
-        LOG(LOG_TRACE, LOG_LINE, "");
+        LOG(AZ_LOG_TRACE, LOG_LINE, "");
     }
 }
 
@@ -271,7 +271,7 @@
     {
         char tmBuffer[TIME_MAX_BUFFER];
         getLogTime(tmBuffer, TIME_MAX_BUFFER);
-        LOG(LOG_TRACE, LOG_LINE, "<- %s %s", tmBuffer, STRING_c_str(trace_log) );
+        LOG(AZ_LOG_TRACE, LOG_LINE, "<- %s %s", tmBuffer, STRING_c_str(trace_log) );
     }
 }
 
@@ -287,18 +287,18 @@
             char tmBuffer[TIME_MAX_BUFFER];
             getLogTime(tmBuffer, TIME_MAX_BUFFER);
 
-            LOG(LOG_TRACE, 0, "<- %s %s: 0x%02x 0x%02x ", tmBuffer, retrievePacketType((CONTROL_PACKET_TYPE)packet), (unsigned char)(packet | flags), length);
+            LOG(AZ_LOG_TRACE, 0, "<- %s %s: 0x%02x 0x%02x ", tmBuffer, retrievePacketType((CONTROL_PACKET_TYPE)packet), (unsigned char)(packet | flags), length);
             for (size_t index = 0; index < length; index++)
             {
-                LOG(LOG_TRACE, 0, "0x%02x ", data[index]);
+                LOG(AZ_LOG_TRACE, 0, "0x%02x ", data[index]);
             }
-            LOG(LOG_TRACE, LOG_LINE, "");
+            LOG(AZ_LOG_TRACE, LOG_LINE, "");
         }
         else if (packet == PINGRESP_TYPE)
         {
             char tmBuffer[TIME_MAX_BUFFER];
             getLogTime(tmBuffer, TIME_MAX_BUFFER);
-            LOG(LOG_TRACE, LOG_LINE, "<- %s %s: 0x%02x 0x%02x ", tmBuffer, retrievePacketType((CONTROL_PACKET_TYPE)packet), (unsigned char)(packet | flags), length);
+            LOG(AZ_LOG_TRACE, LOG_LINE, "<- %s %s: 0x%02x 0x%02x ", tmBuffer, retrievePacketType((CONTROL_PACKET_TYPE)packet), (unsigned char)(packet | flags), length);
         }
     }
 }
@@ -309,7 +309,7 @@
 
     if (tickcounter_get_current_ms(mqtt_client->packetTickCntr, &mqtt_client->packetSendTimeMs) != 0)
     {
-        LOG(LOG_ERROR, LOG_LINE, "Failure getting current ms tickcounter");
+        LOG(AZ_LOG_ERROR, LOG_LINE, "Failure getting current ms tickcounter");
         result = __LINE__;
     }
     else
@@ -317,7 +317,7 @@
         result = xio_send(mqtt_client->xioHandle, (const void*)data, length, sendComplete, mqtt_client);
         if (result != 0)
         {
-            LOG(LOG_ERROR, LOG_LINE, "%d: Failure sending control packet data", result);
+            LOG(AZ_LOG_ERROR, LOG_LINE, "%d: Failure sending control packet data", result);
             result = __LINE__;
         }
         else
@@ -344,14 +344,14 @@
             BUFFER_HANDLE connPacket = mqtt_codec_connect(&mqtt_client->mqttOptions, trace_log);
             if (connPacket == NULL)
             {
-                LOG(LOG_ERROR, LOG_LINE, "Error: mqtt_codec_connect failed");
+                LOG(AZ_LOG_ERROR, LOG_LINE, "Error: mqtt_codec_connect failed");
             }
             else
             {
                 /*Codes_SRS_MQTT_CLIENT_07_009: [On success mqtt_client_connect shall send the MQTT CONNECT to the endpoint.]*/
                 if (sendPacketItem(mqtt_client, BUFFER_u_char(connPacket), BUFFER_length(connPacket)) != 0)
                 {
-                    LOG(LOG_ERROR, LOG_LINE, "Error: mqtt_codec_connect failed");
+                    LOG(AZ_LOG_ERROR, LOG_LINE, "Error: mqtt_codec_connect failed");
                 }
                 else
                 {
@@ -375,7 +375,7 @@
     }
     else
     {
-        LOG(LOG_ERROR, LOG_LINE, "Error: mqtt_client is NULL");
+        LOG(AZ_LOG_ERROR, LOG_LINE, "Error: mqtt_client is NULL");
     }
 }
 
@@ -391,7 +391,7 @@
     }
     else
     {
-        LOG(LOG_ERROR, LOG_LINE, "Error: mqtt_client is NULL");
+        LOG(AZ_LOG_ERROR, LOG_LINE, "Error: mqtt_client is NULL");
     }
 }
 
@@ -406,7 +406,7 @@
     }
     else
     {
-        LOG(LOG_ERROR, LOG_LINE, "Error invalid parameter: mqtt_client: %p", mqtt_client);
+        LOG(AZ_LOG_ERROR, LOG_LINE, "Error invalid parameter: mqtt_client: %p", mqtt_client);
     }
 }
 
@@ -418,7 +418,7 @@
         if (mallocAndStrcpy_s(&mqtt_client->mqttOptions.clientId, mqttOptions->clientId) != 0)
         {
             result = __LINE__;
-            LOG(LOG_ERROR, LOG_LINE, "mallocAndStrcpy_s clientId");
+            LOG(AZ_LOG_ERROR, LOG_LINE, "mallocAndStrcpy_s clientId");
         }
     }
     if (result == 0 && mqttOptions->willTopic != NULL)
@@ -426,14 +426,14 @@
         if (mallocAndStrcpy_s(&mqtt_client->mqttOptions.willTopic, mqttOptions->willTopic) != 0)
         {
             result = __LINE__;
-            LOG(LOG_ERROR, LOG_LINE, "mallocAndStrcpy_s willTopic");
+            LOG(AZ_LOG_ERROR, LOG_LINE, "mallocAndStrcpy_s willTopic");
         }
     }
     if (result == 0 && mqttOptions->willMessage != NULL)
     {
         if (mallocAndStrcpy_s(&mqtt_client->mqttOptions.willMessage, mqttOptions->willMessage) != 0)
         {
-            LOG(LOG_ERROR, LOG_LINE, "mallocAndStrcpy_s willMessage");
+            LOG(AZ_LOG_ERROR, LOG_LINE, "mallocAndStrcpy_s willMessage");
             result = __LINE__;
         }
     }
@@ -441,7 +441,7 @@
     {
         if (mallocAndStrcpy_s(&mqtt_client->mqttOptions.username, mqttOptions->username) != 0)
         {
-            LOG(LOG_ERROR, LOG_LINE, "mallocAndStrcpy_s username");
+            LOG(AZ_LOG_ERROR, LOG_LINE, "mallocAndStrcpy_s username");
             result = __LINE__;
         }
     }
@@ -449,7 +449,7 @@
     {
         if (mallocAndStrcpy_s(&mqtt_client->mqttOptions.password, mqttOptions->password) != 0)
         {
-            LOG(LOG_ERROR, LOG_LINE, "mallocAndStrcpy_s password");
+            LOG(AZ_LOG_ERROR, LOG_LINE, "mallocAndStrcpy_s password");
             result = __LINE__;
         }
     }
@@ -511,7 +511,7 @@
                     }
                     else
                     {
-                        LOG(LOG_ERROR, LOG_LINE, "fnOperationCallback NULL");
+                        LOG(AZ_LOG_ERROR, LOG_LINE, "fnOperationCallback NULL");
                     }
                     break;
                 }
@@ -536,7 +536,7 @@
                         char* topicName = byteutil_readUTF(&iterator, &length);
                         if (topicName == NULL)
                         {
-                            LOG(LOG_ERROR, LOG_LINE, "Publish MSG: failure reading topic name");
+                            LOG(AZ_LOG_ERROR, LOG_LINE, "Publish MSG: failure reading topic name");
                             set_error_callback(mqtt_client, MQTT_CLIENT_PARSE_ERROR);
                             if (trace_log != NULL)
                             {
@@ -564,7 +564,7 @@
                             MQTT_MESSAGE_HANDLE msgHandle = mqttmessage_create(packetId, topicName, qosValue, iterator, length);
                             if (msgHandle == NULL)
                             {
-                                LOG(LOG_ERROR, LOG_LINE, "failure in mqttmessage_create");
+                                LOG(AZ_LOG_ERROR, LOG_LINE, "failure in mqttmessage_create");
                                 set_error_callback(mqtt_client, MQTT_CLIENT_MEMORY_ERROR);
                                 if (trace_log != NULL) {
                                     STRING_delete(trace_log);
@@ -575,7 +575,7 @@
                                 if (mqttmessage_setIsDuplicateMsg(msgHandle, isDuplicateMsg) != 0 ||
                                     mqttmessage_setIsRetained(msgHandle, isRetainMsg) != 0)
                                 {
-                                    LOG(LOG_ERROR, LOG_LINE, "failure setting mqtt message property");
+                                    LOG(AZ_LOG_ERROR, LOG_LINE, "failure setting mqtt message property");
                                     set_error_callback(mqtt_client, MQTT_CLIENT_MEMORY_ERROR);
                                     if (trace_log != NULL) {
                                         STRING_delete(trace_log);
@@ -598,7 +598,7 @@
                                         pubRel = mqtt_codec_publishReceived(packetId);
                                         if (pubRel == NULL)
                                         {
-                                            LOG(LOG_ERROR, LOG_LINE, "Failed to allocate publish receive message.");
+                                            LOG(AZ_LOG_ERROR, LOG_LINE, "Failed to allocate publish receive message.");
                                             set_error_callback(mqtt_client, MQTT_CLIENT_MEMORY_ERROR);
                                         }
                                     }
@@ -607,7 +607,7 @@
                                         pubRel = mqtt_codec_publishAck(packetId);
                                         if (pubRel == NULL)
                                         {
-                                            LOG(LOG_ERROR, LOG_LINE, "Failed to allocate publish ack message.");
+                                            LOG(AZ_LOG_ERROR, LOG_LINE, "Failed to allocate publish ack message.");
                                             set_error_callback(mqtt_client, MQTT_CLIENT_MEMORY_ERROR);
                                         }
                                     }
@@ -657,7 +657,7 @@
                             pubRel = mqtt_codec_publishRelease(publish_ack.packetId);
                             if (pubRel == NULL)
                             {
-                                LOG(LOG_ERROR, LOG_LINE, "Failed to allocate publish release message.");
+                                LOG(AZ_LOG_ERROR, LOG_LINE, "Failed to allocate publish release message.");
                                 set_error_callback(mqtt_client, MQTT_CLIENT_MEMORY_ERROR);
                             }
                         }
@@ -666,7 +666,7 @@
                             pubRel = mqtt_codec_publishComplete(publish_ack.packetId);
                             if (pubRel == NULL)
                             {
-                                LOG(LOG_ERROR, LOG_LINE, "Failed to allocate publish complete message.");
+                                LOG(AZ_LOG_ERROR, LOG_LINE, "Failed to allocate publish complete message.");
                                 set_error_callback(mqtt_client, MQTT_CLIENT_MEMORY_ERROR);
                             }
                         }
@@ -720,7 +720,7 @@
                         }
                         else
                         {
-                            LOG(LOG_ERROR, LOG_LINE, "allocation of quality of service value failed.");
+                            LOG(AZ_LOG_ERROR, LOG_LINE, "allocation of quality of service value failed.");
                             set_error_callback(mqtt_client, MQTT_CLIENT_MEMORY_ERROR);
                         }
                     }
@@ -778,7 +778,7 @@
         if (result == NULL)
         {
             /*Codes_SRS_MQTT_CLIENT_07_002: [If any failure is encountered then mqttclient_init shall return NULL.]*/
-            LOG(LOG_ERROR, LOG_LINE, "mqtt_client_init failure: Allocation Failure");
+            LOG(AZ_LOG_ERROR, LOG_LINE, "mqtt_client_init failure: Allocation Failure");
         }
         else
         {
@@ -808,7 +808,7 @@
             if (result->packetTickCntr == NULL)
             {
                 /*Codes_SRS_MQTT_CLIENT_07_002: [If any failure is encountered then mqttclient_init shall return NULL.]*/
-                LOG(LOG_ERROR, LOG_LINE, "mqtt_client_init failure: tickcounter_create failure");
+                LOG(AZ_LOG_ERROR, LOG_LINE, "mqtt_client_init failure: tickcounter_create failure");
                 free(result);
                 result = NULL;
             }
@@ -818,7 +818,7 @@
                 if (result->codec_handle == NULL)
                 {
                     /*Codes_SRS_MQTT_CLIENT_07_002: [If any failure is encountered then mqttclient_init shall return NULL.]*/
-                    LOG(LOG_ERROR, LOG_LINE, "mqtt_client_init failure: mqtt_codec_create failure");
+                    LOG(AZ_LOG_ERROR, LOG_LINE, "mqtt_client_init failure: mqtt_codec_create failure");
                     tickcounter_destroy(result->packetTickCntr);
                     free(result);
                     result = NULL;
@@ -853,7 +853,7 @@
     /*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.]*/
     if (handle == NULL || mqttOptions == NULL)
     {
-        LOG(LOG_ERROR, LOG_LINE, "mqtt_client_connect: NULL argument (handle = %p, mqttOptions = %p)", handle, mqttOptions);
+        LOG(AZ_LOG_ERROR, LOG_LINE, "mqtt_client_connect: NULL argument (handle = %p, mqttOptions = %p)", handle, mqttOptions);
         result = __LINE__;
     }
     else
@@ -862,7 +862,7 @@
         if (xioHandle == NULL)
         {
             /*Codes_SRS_MQTT_CLIENT_07_007: [If any failure is encountered then mqtt_client_connect shall return a non-zero value.]*/
-            LOG(LOG_ERROR, LOG_LINE, "Error: mqttcodec_connect failed");
+            LOG(AZ_LOG_ERROR, LOG_LINE, "Error: mqttcodec_connect failed");
             result = __LINE__;
         }
         else
@@ -874,14 +874,14 @@
             mqtt_client->maxPingRespTime = (DEFAULT_MAX_PING_RESPONSE_TIME < mqttOptions->keepAliveInterval/2) ? DEFAULT_MAX_PING_RESPONSE_TIME : mqttOptions->keepAliveInterval/2;
             if (cloneMqttOptions(mqtt_client, mqttOptions) != 0)
             {
-                LOG(LOG_ERROR, LOG_LINE, "Error: Clone Mqtt Options failed");
+                LOG(AZ_LOG_ERROR, LOG_LINE, "Error: Clone Mqtt Options failed");
                 result = __LINE__;
             }
             /*Codes_SRS_MQTT_CLIENT_07_008: [mqtt_client_connect shall open the XIO_HANDLE by calling into the xio_open interface.]*/
             else if (xio_open(xioHandle, onOpenComplete, mqtt_client, onBytesReceived, mqtt_client, onIoError, mqtt_client) != 0)
             {
                 /*Codes_SRS_MQTT_CLIENT_07_007: [If any failure is encountered then mqtt_client_connect shall return a non-zero value.]*/
-                LOG(LOG_ERROR, LOG_LINE, "Error: io_open failed");
+                LOG(AZ_LOG_ERROR, LOG_LINE, "Error: io_open failed");
                 result = __LINE__;
             }
             else
@@ -909,7 +909,7 @@
         if (payload == NULL)
         {
             /*Codes_SRS_MQTT_CLIENT_07_020: [If any failure is encountered then mqtt_client_unsubscribe shall return a non-zero value.]*/
-            LOG(LOG_ERROR, LOG_LINE, "Error: mqttmessage_getApplicationMsg failed");
+            LOG(AZ_LOG_ERROR, LOG_LINE, "Error: mqttmessage_getApplicationMsg failed");
             result = __LINE__;
         }
         else
@@ -921,7 +921,7 @@
             if (publishPacket == NULL)
             {
                 /*Codes_SRS_MQTT_CLIENT_07_020: [If any failure is encountered then mqtt_client_unsubscribe shall return a non-zero value.]*/
-                LOG(LOG_ERROR, LOG_LINE, "Error: mqtt_codec_publish failed");
+                LOG(AZ_LOG_ERROR, LOG_LINE, "Error: mqtt_codec_publish failed");
                 result = __LINE__;
             }
             else
@@ -932,7 +932,7 @@
                 if (sendPacketItem(mqtt_client, BUFFER_u_char(publishPacket), BUFFER_length(publishPacket)) != 0)
                 {
                     /*Codes_SRS_MQTT_CLIENT_07_020: [If any failure is encountered then mqtt_client_unsubscribe shall return a non-zero value.]*/
-                    LOG(LOG_ERROR, LOG_LINE, "Error: mqtt_client_publish send failed");
+                    LOG(AZ_LOG_ERROR, LOG_LINE, "Error: mqtt_client_publish send failed");
                     result = __LINE__;
                 }
                 else
@@ -968,7 +968,7 @@
         if (subPacket == NULL)
         {
             /*Codes_SRS_MQTT_CLIENT_07_014: [If any failure is encountered then mqtt_client_subscribe shall return a non-zero value.]*/
-            LOG(LOG_ERROR, LOG_LINE, "Error: mqtt_codec_subscribe failed");
+            LOG(AZ_LOG_ERROR, LOG_LINE, "Error: mqtt_codec_subscribe failed");
             result = __LINE__;
         }
         else
@@ -979,7 +979,7 @@
             if (sendPacketItem(mqtt_client, BUFFER_u_char(subPacket), BUFFER_length(subPacket)) != 0)
             {
                 /*Codes_SRS_MQTT_CLIENT_07_014: [If any failure is encountered then mqtt_client_subscribe shall return a non-zero value.]*/
-                LOG(LOG_ERROR, LOG_LINE, "Error: mqtt_client_subscribe send failed");
+                LOG(AZ_LOG_ERROR, LOG_LINE, "Error: mqtt_client_subscribe send failed");
                 result = __LINE__;
             }
             else
@@ -1014,7 +1014,7 @@
         if (unsubPacket == NULL)
         {
             /*Codes_SRS_MQTT_CLIENT_07_017: [If any failure is encountered then mqtt_client_unsubscribe shall return a non-zero value.]*/
-            LOG(LOG_ERROR, LOG_LINE, "Error: mqtt_codec_unsubscribe failed");
+            LOG(AZ_LOG_ERROR, LOG_LINE, "Error: mqtt_codec_unsubscribe failed");
             result = __LINE__;
         }
         else
@@ -1025,7 +1025,7 @@
             if (sendPacketItem(mqtt_client, BUFFER_u_char(unsubPacket), BUFFER_length(unsubPacket)) != 0)
             {
                 /*Codes_SRS_MQTT_CLIENT_07_017: [If any failure is encountered then mqtt_client_unsubscribe shall return a non-zero value.].]*/
-                LOG(LOG_ERROR, LOG_LINE, "Error: mqtt_client_unsubscribe send failed");
+                LOG(AZ_LOG_ERROR, LOG_LINE, "Error: mqtt_client_unsubscribe send failed");
                 result = __LINE__;
             }
             else
@@ -1058,7 +1058,7 @@
         if (disconnectPacket == NULL)
         {
             /*Codes_SRS_MQTT_CLIENT_07_011: [If any failure is encountered then mqtt_client_disconnect shall return a non-zero value.]*/
-            LOG(LOG_ERROR, LOG_LINE, "Error: mqtt_client_disconnect failed");
+            LOG(AZ_LOG_ERROR, LOG_LINE, "Error: mqtt_client_disconnect failed");
             mqtt_client->packetState = PACKET_TYPE_ERROR;
             result = __LINE__;
         }
@@ -1070,7 +1070,7 @@
             if (sendPacketItem(mqtt_client, BUFFER_u_char(disconnectPacket), BUFFER_length(disconnectPacket)) != 0)
             {
                 /*Codes_SRS_MQTT_CLIENT_07_011: [If any failure is encountered then mqtt_client_disconnect shall return a non-zero value.]*/
-                LOG(LOG_ERROR, LOG_LINE, "Error: mqtt_client_disconnect send failed");
+                LOG(AZ_LOG_ERROR, LOG_LINE, "Error: mqtt_client_disconnect send failed");
                 result = __LINE__;
             }
             else
@@ -1104,7 +1104,7 @@
             tickcounter_ms_t current_ms;
             if (tickcounter_get_current_ms(mqtt_client->packetTickCntr, &current_ms) != 0)
             {
-                LOG(LOG_ERROR, LOG_LINE, "Error: tickcounter_get_current_ms failed");
+                LOG(AZ_LOG_ERROR, LOG_LINE, "Error: tickcounter_get_current_ms failed");
             }
             else
             {