Skip to content

Commit 4e16b82

Browse files
committed
Differentiate timeouts and add a function for clearing n messages
1 parent 0fa3882 commit 4e16b82

File tree

2 files changed

+38
-18
lines changed

2 files changed

+38
-18
lines changed

src/mqtt_client.cpp

Lines changed: 33 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,9 @@
106106
#define HCESIGN_DIGEST_LENGTH 64
107107
#define HCESIGN_CTX_ID_LENGTH 5
108108

109-
#define POLL_TIMEOUT_MS 20000
109+
#define SIGN_TIMEOUT_MS 20000
110+
#define MQTT_TIMEOUT_MS 50
111+
#define DISCONNECT_TIMEOUT_MS 20000
110112

111113
// Singleton instance
112114
MqttClientClass MqttClient = MqttClientClass::instance();
@@ -154,7 +156,6 @@ static void internalConnectedCallback(char *urc_data) {
154156

155157
static void internalDisconnectCallback(char *urc_data) {
156158
connected_to_broker = false;
157-
// LedCtrl.off(Led::CON, true);
158159

159160
if (disconnected_callback != NULL) {
160161
disconnected_callback();
@@ -286,7 +287,10 @@ static bool generateSigningCommand(char *data, char *command_buffer) {
286287
return true;
287288
}
288289

289-
bool MqttClientClass::signIncomingRequests(void) {
290+
/**
291+
* @brief Sends the signed message from the ECC back to the Sequans modem
292+
*/
293+
static bool signIncomingRequests(void) {
290294
if (signing_request_flag) {
291295

292296
SequansController.startCriticalSection();
@@ -418,7 +422,7 @@ bool MqttClientClass::begin(const char *client_id,
418422
uint32_t start = millis();
419423

420424
while (signIncomingRequests() == false) {
421-
if (millis() - start > POLL_TIMEOUT_MS) {
425+
if (millis() - start > SIGN_TIMEOUT_MS) {
422426
Log.error("Timed out waiting for pub signing\r\n");
423427
return false;
424428
}
@@ -475,19 +479,24 @@ bool MqttClientClass::publish(const char *topic,
475479
char command[MQTT_PUBLISH_LENGTH_PRE_DATA + digits_in_buffer_size];
476480

477481
// Fill everything besides the data
478-
sprintf(command, MQTT_PUBLISH, topic, quality_of_service, buffer_size);
482+
sprintf(command,
483+
MQTT_PUBLISH,
484+
topic,
485+
quality_of_service,
486+
(unsigned long)buffer_size);
479487
SequansController.writeCommand(command);
480488

481489
// Wait for start character for delivering payload
482-
if (SequansController.waitForByte('>', POLL_TIMEOUT_MS) ==
490+
if (SequansController.waitForByte('>', MQTT_TIMEOUT_MS) ==
483491
SEQUANS_CONTROLLER_READ_BYTE_TIMEOUT) {
484-
Log.error("Timed out waiting for >");
492+
Log.error("Timed out waiting for signal to deliver MQTT payload.");
493+
return false;
485494
}
486495
SequansController.writeBytes(buffer, buffer_size);
487496

488497
// Wait until we receive the first URC which we discard
489498
uint8_t wait_result = SequansController.waitForByte(
490-
URC_IDENTIFIER_START_CHARACTER, POLL_TIMEOUT_MS);
499+
URC_IDENTIFIER_START_CHARACTER, MQTT_TIMEOUT_MS);
491500

492501
if (wait_result != SEQUANS_CONTROLLER_READ_BYTE_OK) {
493502
Log.errorf("Error when waiting for the first URC start character. "
@@ -500,7 +509,7 @@ bool MqttClientClass::publish(const char *topic,
500509

501510
// Wait until we receive the second URC which includes the status code
502511
wait_result = SequansController.waitForByte(URC_IDENTIFIER_START_CHARACTER,
503-
POLL_TIMEOUT_MS);
512+
MQTT_TIMEOUT_MS);
504513
if (wait_result != SEQUANS_CONTROLLER_READ_BYTE_OK) {
505514
Log.errorf("Error when waiting for the second URC start character. "
506515
"Error was %d\r\n",
@@ -583,7 +592,7 @@ bool MqttClientClass::subscribe(const char *topic,
583592

584593
// Now we wait for the URC
585594
if (SequansController.waitForByte(URC_IDENTIFIER_START_CHARACTER,
586-
POLL_TIMEOUT_MS) ==
595+
MQTT_TIMEOUT_MS) ==
587596
SEQUANS_CONTROLLER_READ_BYTE_TIMEOUT) {
588597
Log.error("Timed out waiting for start byte in MQTT subscribe");
589598
return false;
@@ -655,7 +664,7 @@ bool MqttClientClass::readMessage(const char *topic,
655664
// Wait for first byte in receive buffer
656665
uint32_t start = millis();
657666
while (!SequansController.isRxReady()) {
658-
if (millis() - start > POLL_TIMEOUT_MS) {
667+
if (millis() - start > MQTT_TIMEOUT_MS) {
659668
Log.error("Timed out waiting for reading MQTT message");
660669
return SEQUANS_CONTROLLER_READ_BYTE_TIMEOUT;
661670
}
@@ -687,6 +696,18 @@ String MqttClientClass::readMessage(const char *topic, const uint16_t size) {
687696
return buffer;
688697
}
689698

699+
void MqttClientClass::clearMessages(const char *topic,
700+
const uint16_t num_messages) {
701+
702+
SequansController.clearReceiveBuffer();
703+
char command[MQTT_RECEIVE_LENGTH] = "";
704+
sprintf(command, MQTT_RECEIVE, topic);
705+
706+
for (uint16_t i = 0; i < num_messages; i++) {
707+
SequansController.writeCommand(command);
708+
}
709+
}
710+
690711
bool MqttClientClass::disconnect(bool lte_event) {
691712

692713
LedCtrl.off(Led::CON, true);
@@ -716,7 +737,7 @@ bool MqttClientClass::disconnect(bool lte_event) {
716737
// Wait for the broker to disconnect
717738
uint32_t start = millis();
718739
while (connected_to_broker) {
719-
if (millis() - start > 20000) {
740+
if (millis() - start > DISCONNECT_TIMEOUT_MS) {
720741
Log.error("Timed out waiting for broker disconnect URC");
721742
return false;
722743
}

src/mqtt_client.h

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -162,14 +162,13 @@ class MqttClientClass {
162162
String readMessage(const char *topic, const uint16_t size = 256);
163163

164164
/**
165-
* @brief If the MQTT connection is encrypted with the hardware crypto
166-
* engine, we need to regularly check and sign the data going back and
167-
* forward (for example when the unit is pinging the broker).
165+
* @brief Reads @p num_messages MQTT messages from the Sequans modem and
166+
* discards them.
168167
*
169-
* @note This function has to be called regularly, for example in the main
170-
* loop for the MQTT connection to remain active.
168+
* @param topic Topic to clear the messages from.
169+
* @param num_messages Number of messages to discard.
171170
*/
172-
bool signIncomingRequests(void);
171+
void clearMessages(const char *topic, const uint16_t num_messages);
173172

174173
/**
175174
* @brief Disconnects from the MQTT broker.

0 commit comments

Comments
 (0)