Skip to content

Commit 9cbe31b

Browse files
committed
MCU8MASS-1169 Add timeout for publish
1 parent 095c0e2 commit 9cbe31b

File tree

2 files changed

+20
-9
lines changed

2 files changed

+20
-9
lines changed

src/mqtt_client.cpp

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -536,10 +536,10 @@ bool MqttClientClass::end(void) {
536536
delay(10);
537537
SequansController.clearReceiveBuffer();
538538

539-
connected_to_broker = false;
540-
541539
internalDisconnectCallback(NULL);
542540

541+
connected_to_broker = false;
542+
543543
return true;
544544
}
545545

@@ -559,7 +559,8 @@ bool MqttClientClass::isConnected(void) { return connected_to_broker; }
559559
bool MqttClientClass::publish(const char* topic,
560560
const uint8_t* buffer,
561561
const uint32_t buffer_size,
562-
const MqttQoS quality_of_service) {
562+
const MqttQoS quality_of_service,
563+
const uint32_t timeout_ms) {
563564

564565
if (!isConnected()) {
565566
Log.error("Attempted publish without being connected to a broker");
@@ -599,8 +600,12 @@ bool MqttClientClass::publish(const char* topic,
599600
// termination
600601
char status_code_buffer[3] = "";
601602

602-
if (!SequansController.waitForURC(MQTT_ON_PUBLISH_URC, urc, sizeof(urc))) {
603-
Log.warn("Timed out waiting for publish confirmation\r\n");
603+
if (!SequansController.waitForURC(MQTT_ON_PUBLISH_URC,
604+
urc,
605+
sizeof(urc),
606+
timeout_ms)) {
607+
Log.warn("Timed out waiting for publish confirmation. Consider "
608+
"increasing timeout for publishing\r\n");
604609
return false;
605610
}
606611

@@ -639,11 +644,13 @@ bool MqttClientClass::publish(const char* topic,
639644

640645
bool MqttClientClass::publish(const char* topic,
641646
const char* message,
642-
const MqttQoS quality_of_service) {
647+
const MqttQoS quality_of_service,
648+
const uint32_t timeout_ms) {
643649
return publish(topic,
644650
(uint8_t*)message,
645651
strlen(message),
646-
quality_of_service);
652+
quality_of_service,
653+
timeout_ms);
647654
}
648655

649656
bool MqttClientClass::subscribe(const char* topic,

src/mqtt_client.h

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,26 +90,30 @@ class MqttClientClass {
9090
* @param buffer Data to publish.
9191
* @param buffer_size Has to be in range 1-65535.
9292
* @param quality_of_service MQTT protocol QoS.
93+
* @param timeout_ms Timeout waiting for publish confirmation.
9394
*
9495
* @return true if publish was successful.
9596
*/
9697
bool publish(const char* topic,
9798
const uint8_t* buffer,
9899
const uint32_t buffer_size,
99-
const MqttQoS quality_of_service = AT_LEAST_ONCE);
100+
const MqttQoS quality_of_service = AT_LEAST_ONCE,
101+
const uint32_t timeout_ms = 30000);
100102

101103
/**
102104
* @brief Publishes the contents of the message to the given topic.
103105
*
104106
* @param topic Topic to publish to.
105107
* @param message String to publish, has to be null terminated.
106108
* @param quality_of_service MQTT protocol QoS.
109+
* @param timeout_ms Timeout waiting for publish confirmation.
107110
*
108111
* @return true if publish was successful.
109112
*/
110113
bool publish(const char* topic,
111114
const char* message,
112-
const MqttQoS quality_of_service = AT_LEAST_ONCE);
115+
const MqttQoS quality_of_service = AT_LEAST_ONCE,
116+
const uint32_t timeout_ms = 30000);
113117

114118
/**
115119
* @brief Subscribes to a given topic.

0 commit comments

Comments
 (0)