Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings
This repository was archived by the owner on Oct 12, 2023. It is now read-only.

Commit 49ec775

Browse files
Sync Arduino libraries with latest Azure IoT SDK 1.0.45
1 parent 5c42d65 commit 49ec775

File tree

4 files changed

+69
-46
lines changed

4 files changed

+69
-46
lines changed

‎library.properties‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name=AzureIoTProtocol_MQTT
2-
version=1.0.44
2+
version=1.0.45
33
author=Microsoft
44
maintainer=Microsoft <iotcert@microsoft.com>
55
sentence=Azure MQTT protocol library for Arduino. For the Arduino MKR1000 or Zero and WiFi Shield 101, Adafruit Huzzah and Feather M0, or SparkFun Thing.

‎src/AzureIoTProtocol_MQTT.h‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,6 @@
66

77
#include "azure_umqtt_c/mqtt_client.h"
88

9-
#define AzureIoTProtocolMQTTVersion "1.0.43"
9+
#define AzureIoTProtocolMQTTVersion "1.0.45"
1010

1111
#endif //AZUREIOTPROTOCOLMQTT_H

‎src/azure_umqtt_c/mqtt_client.c‎

Lines changed: 60 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
#include "azure_umqtt_c/mqtt_codec.h"
1717
#include <inttypes.h>
1818

19-
#define KEEP_ALIVE_BUFFER_SEC 10
2019
#define VARIABLE_HEADER_OFFSET 2
2120
#define RETAIN_FLAG_MASK 0x1
2221
#define QOS_LEAST_ONCE_FLAG_MASK 0x2
@@ -25,10 +24,10 @@
2524
#define CONNECT_PACKET_MASK 0xf0
2625
#define TIME_MAX_BUFFER 16
2726
#define DEFAULT_MAX_PING_RESPONSE_TIME 80 // % of time to send pings
28-
#define MAX_CLOSE_RETRIES 10
27+
#define MAX_CLOSE_RETRIES 2
2928

30-
static const char* TRUE_CONST = "true";
31-
static const char* FALSE_CONST = "false";
29+
static const char* constTRUE_CONST = "true";
30+
static const char* constFALSE_CONST = "false";
3231

3332
DEFINE_ENUM_STRINGS(QOS_VALUE, QOS_VALUE_VALUES);
3433

@@ -73,17 +72,28 @@ static void on_connection_closed(void* context)
7372

7473
static void close_connection(MQTT_CLIENT* mqtt_client)
7574
{
76-
(void)xio_close(mqtt_client->xioHandle, on_connection_closed, mqtt_client);
77-
if (mqtt_client->disconnect_cb == NULL)
75+
if (mqtt_client->socketConnected)
7876
{
79-
size_tcounter=0;
80-
do
77+
(void)xio_close(mqtt_client->xioHandle, on_connection_closed, mqtt_client);
78+
if (mqtt_client->disconnect_cb==NULL)
8179
{
82-
xio_dowork(mqtt_client->xioHandle);
83-
counter++;
84-
ThreadAPI_Sleep(2);
85-
} while (mqtt_client->clientConnected && counter < MAX_CLOSE_RETRIES);
80+
size_t counter = 0;
81+
do
82+
{
83+
xio_dowork(mqtt_client->xioHandle);
84+
counter++;
85+
ThreadAPI_Sleep(2);
86+
} while (mqtt_client->clientConnected && counter < MAX_CLOSE_RETRIES);
87+
}
88+
}
89+
else
90+
{
91+
if (mqtt_client->disconnect_cb)
92+
{
93+
mqtt_client->disconnect_cb(mqtt_client->disconnect_ctx);
94+
}
8695
}
96+
mqtt_client->xioHandle = NULL;
8797
}
8898

8999
static void set_error_callback(MQTT_CLIENT* mqtt_client, MQTT_CLIENT_EVENT_ERROR error_type)
@@ -1153,43 +1163,56 @@ int mqtt_client_disconnect(MQTT_CLIENT_HANDLE handle, ON_MQTT_DISCONNECTED_CALLB
11531163
}
11541164
else
11551165
{
1156-
BUFFER_HANDLE disconnectPacket = mqtt_codec_disconnect();
1157-
if (disconnectPacket == NULL)
1166+
if (mqtt_client->clientConnected)
11581167
{
1159-
/*Codes_SRS_MQTT_CLIENT_07_011: [If any failure is encountered then mqtt_client_disconnect shall return a non-zero value.]*/
1160-
LOG(AZ_LOG_ERROR, LOG_LINE, "Error: mqtt_client_disconnect failed");
1161-
mqtt_client->packetState = PACKET_TYPE_ERROR;
1162-
result = __FAILURE__;
1163-
}
1164-
else
1165-
{
1166-
/* Codes_SRS_MQTT_CLIENT_07_037: [ if callback is not NULL callback shall be called once the mqtt connection has been disconnected ] */
1167-
mqtt_client->disconnect_cb = callback;
1168-
mqtt_client->disconnect_ctx = ctx;
1169-
mqtt_client->packetState = DISCONNECT_TYPE;
1170-
1171-
size_t size = BUFFER_length(disconnectPacket);
1172-
/*Codes_SRS_MQTT_CLIENT_07_012: [On success mqtt_client_disconnect shall send the MQTT DISCONNECT packet to the endpoint.]*/
1173-
if (sendPacketItem(mqtt_client, BUFFER_u_char(disconnectPacket), size) != 0)
1168+
BUFFER_HANDLE disconnectPacket = mqtt_codec_disconnect();
1169+
if (disconnectPacket == NULL)
11741170
{
11751171
/*Codes_SRS_MQTT_CLIENT_07_011: [If any failure is encountered then mqtt_client_disconnect shall return a non-zero value.]*/
1176-
LOG(AZ_LOG_ERROR, LOG_LINE, "Error: mqtt_client_disconnect send failed");
1172+
LOG(AZ_LOG_ERROR, LOG_LINE, "Error: mqtt_client_disconnect failed");
1173+
mqtt_client->packetState = PACKET_TYPE_ERROR;
11771174
result = __FAILURE__;
11781175
}
11791176
else
11801177
{
1181-
if (mqtt_client->logTrace)
1178+
/* Codes_SRS_MQTT_CLIENT_07_037: [ if callback is not NULL callback shall be called once the mqtt connection has been disconnected ] */
1179+
mqtt_client->disconnect_cb = callback;
1180+
mqtt_client->disconnect_ctx = ctx;
1181+
mqtt_client->packetState = DISCONNECT_TYPE;
1182+
1183+
size_t size = BUFFER_length(disconnectPacket);
1184+
/*Codes_SRS_MQTT_CLIENT_07_012: [On success mqtt_client_disconnect shall send the MQTT DISCONNECT packet to the endpoint.]*/
1185+
if (sendPacketItem(mqtt_client, BUFFER_u_char(disconnectPacket), size) != 0)
11821186
{
1183-
STRING_HANDLEtrace_log=STRING_construct("DISCONNECT");
1184-
log_outgoing_trace(mqtt_client, trace_log);
1185-
STRING_delete(trace_log);
1187+
/*Codes_SRS_MQTT_CLIENT_07_011: [If any failure is encountered then mqtt_client_disconnect shall return a non-zero value.]*/
1188+
LOG(AZ_LOG_ERROR, LOG_LINE, "Error: mqtt_client_disconnect send failed");
1189+
result=__FAILURE__;
11861190
}
1187-
result = 0;
1191+
else
1192+
{
1193+
if (mqtt_client->logTrace)
1194+
{
1195+
STRING_HANDLE trace_log = STRING_construct("DISCONNECT");
1196+
log_outgoing_trace(mqtt_client, trace_log);
1197+
STRING_delete(trace_log);
1198+
}
1199+
result = 0;
1200+
}
1201+
BUFFER_delete(disconnectPacket);
11881202
}
1189-
BUFFER_delete(disconnectPacket);
11901203
clear_mqtt_options(mqtt_client);
11911204
mqtt_client->xioHandle = NULL;
11921205
}
1206+
else
1207+
{
1208+
// If the client is not connected then just close the underlying socket
1209+
mqtt_client->disconnect_cb = callback;
1210+
mqtt_client->disconnect_ctx = ctx;
1211+
1212+
close_connection(mqtt_client);
1213+
clear_mqtt_options(mqtt_client);
1214+
result = 0;
1215+
}
11931216
}
11941217
return result;
11951218
}
@@ -1223,7 +1246,7 @@ void mqtt_client_dowork(MQTT_CLIENT_HANDLE handle)
12231246
mqtt_client->packetSendTimeMs = 0;
12241247
mqtt_client->packetState = UNKNOWN_TYPE;
12251248
}
1226-
else if ((((current_ms - mqtt_client->packetSendTimeMs) / 1000) +KEEP_ALIVE_BUFFER_SEC) > mqtt_client->keepAliveInterval)
1249+
else if (((current_ms - mqtt_client->packetSendTimeMs) / 1000) >= mqtt_client->keepAliveInterval)
12271250
{
12281251
/*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.]*/
12291252
BUFFER_HANDLE pingPacket = mqtt_codec_ping();

‎src/azure_umqtt_c/mqtt_codec.c‎

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,8 @@
4444
CODEC_STATE_VAR_HEADER, \
4545
CODEC_STATE_PAYLOAD
4646

47-
static const char* TRUE_CONST = "true";
48-
static const char* FALSE_CONST = "false";
47+
static const char* constTRUE_CONST = "true";
48+
static const char* constFALSE_CONST = "false";
4949

5050
DEFINE_ENUM(CODEC_STATE_RESULT, CODEC_STATE_VALUES);
5151

@@ -84,7 +84,7 @@ static const char* retrieve_qos_value(QOS_VALUE value)
8484
}
8585
}
8686

87-
void byteutil_writeByte(uint8_t** buffer, uint8_t value)
87+
staticvoid byteutil_writeByte(uint8_t** buffer, uint8_t value)
8888
{
8989
if (buffer != NULL)
9090
{
@@ -93,7 +93,7 @@ void byteutil_writeByte(uint8_t** buffer, uint8_t value)
9393
}
9494
}
9595

96-
void byteutil_writeInt(uint8_t** buffer, uint16_t value)
96+
staticvoid byteutil_writeInt(uint8_t** buffer, uint16_t value)
9797
{
9898
if (buffer != NULL)
9999
{
@@ -104,7 +104,7 @@ void byteutil_writeInt(uint8_t** buffer, uint16_t value)
104104
}
105105
}
106106

107-
void byteutil_writeUTF(uint8_t** buffer, const char* stringData, uint16_t len)
107+
staticvoid byteutil_writeUTF(uint8_t** buffer, const char* stringData, uint16_t len)
108108
{
109109
if (buffer != NULL)
110110
{
@@ -114,7 +114,7 @@ void byteutil_writeUTF(uint8_t** buffer, const char* stringData, uint16_t len)
114114
}
115115
}
116116

117-
CONTROL_PACKET_TYPE processControlPacketType(uint8_t pktByte, int* flags)
117+
staticCONTROL_PACKET_TYPE processControlPacketType(uint8_t pktByte, int* flags)
118118
{
119119
CONTROL_PACKET_TYPE result;
120120
result = PACKET_TYPE_BYTE(pktByte);
@@ -483,7 +483,7 @@ static int constructConnPayload(BUFFER_HANDLE ctrlPacket, const MQTT_CLIENT_OPTI
483483
}
484484
packet[CONN_FLAG_BYTE_OFFSET] |= WILL_FLAG_FLAG;
485485
byteutil_writeUTF(&iterator, mqttOptions->willTopic, (uint16_t)willTopicLen);
486-
packet[CONN_FLAG_BYTE_OFFSET] |= mqttOptions->qualityOfServiceValue;
486+
packet[CONN_FLAG_BYTE_OFFSET] |= (mqttOptions->qualityOfServiceValue << 3);
487487
if (mqttOptions->messageRetain)
488488
{
489489
packet[CONN_FLAG_BYTE_OFFSET] |= WILL_RETAIN_FLAG;

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /