Skip to content

Commit c30de0e

Browse files
committed
MCU8MASS-1170 Introduce adjustable timeout for Lte.begin()
1 parent 1f2dc48 commit c30de0e

File tree

2 files changed

+34
-5
lines changed

2 files changed

+34
-5
lines changed

src/lte.cpp

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1+
#include "lte.h"
2+
13
#include "led_ctrl.h"
24
#include "log.h"
3-
#include "lte.h"
45
#include "mqtt_client.h"
56
#include "sequans_controller.h"
67

@@ -97,14 +98,22 @@ static void timezoneCallback(__attribute__((unused)) char* buffer) {
9798
got_timezone = true;
9899
}
99100

100-
bool LteClass::begin(const bool print_messages) {
101+
bool LteClass::begin(const uint32_t timeout_ms, const bool print_messages) {
102+
103+
const uint32_t start = millis();
101104

102105
// If low power is utilized, sequans controller will already been
103106
// initialized, so don't reset it by calling begin again
104107
if (!SequansController.isInitialized()) {
105108
SequansController.begin();
106109
}
107110

111+
// Disconnect before configuration if already connected
112+
SequansController.writeCommand(AT_DISCONNECT);
113+
114+
delay(100);
115+
SequansController.clearReceiveBuffer();
116+
108117
SequansController.registerCallback(TIMEZONE_CALLBACK, timezoneCallback);
109118

110119
SequansController.writeCommand(AT_ENABLE_TIME_ZONE_UPDATE);
@@ -154,7 +163,7 @@ bool LteClass::begin(const bool print_messages) {
154163
Log.infof("Connecting to operator");
155164
}
156165

157-
while (!isConnected()) {
166+
while (!isConnected() && millis() - start < timeout_ms) {
158167
LedCtrl.toggle(Led::CELL, true);
159168
delay(500);
160169

@@ -163,6 +172,20 @@ bool LteClass::begin(const bool print_messages) {
163172
}
164173
}
165174

175+
if (millis() - start >= timeout_ms) {
176+
177+
Log.rawf(" Was not able to connect to the network within the timeout "
178+
"of %d ms. Consider increasing the timeout or checking your "
179+
"cellular coverage.\r\n",
180+
timeout_ms);
181+
182+
SequansController.unregisterCallback(CEREG_CALLBACK);
183+
SequansController.unregisterCallback(TIMEZONE_CALLBACK);
184+
SequansController.writeCommand(AT_DISCONNECT);
185+
186+
return false;
187+
}
188+
166189
if (print_messages) {
167190
Log.rawf(" OK!\r\n");
168191
}
@@ -264,6 +287,7 @@ void LteClass::end(void) {
264287
while (isConnected() && millis() - start < 2000) {}
265288
SequansController.unregisterCallback(CEREG_CALLBACK);
266289

290+
SequansController.clearReceiveBuffer();
267291
SequansController.end();
268292
}
269293

src/lte.h

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
#ifndef LTE_H
66
#define LTE_H
77

8+
#include <Arduino.h>
9+
810
#include <stdint.h>
911

1012
class LteClass {
@@ -20,7 +22,7 @@ class LteClass {
2022
/**
2123
* @brief Singleton instance.
2224
*/
23-
static LteClass &instance(void) {
25+
static LteClass& instance(void) {
2426
static LteClass instance;
2527
return instance;
2628
}
@@ -29,12 +31,15 @@ class LteClass {
2931
* @brief Initializes the LTE module and its controller interface. Connects
3032
* to the network.
3133
*
34+
* @param timeout_ms The amount of time to wait for connection before
35+
* aborting.
3236
* @param print_messages If set to true, the messages related to connection
3337
* will be logged.
3438
*
3539
* @return True if initialization was successful and connection was made.
3640
*/
37-
bool begin(const bool print_messages = true);
41+
bool begin(const uint32_t timeout_ms = 240000,
42+
const bool print_messages = true);
3843

3944
/**
4045
* @brief Disables the interface with the LTE module. Disconnects from

0 commit comments

Comments
 (0)