Skip to content

Commit 7b95100

Browse files
authored
Merge pull request #767 from pennam/gsm_debug_retry
GSM: Add cellular state machine events reporting
2 parents 17873e0 + 8b85c6b commit 7b95100

10 files changed

+651
-48
lines changed

libraries/GSM/keywords.txt

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
#######################################
2+
# Syntax Coloring Map For GSM
3+
#######################################
4+
5+
#######################################
6+
# Class (KEYWORD1)
7+
#######################################
8+
9+
GSM KEYWORD1
10+
GSMClient KEYWORD1
11+
GSMSSLClient KEYWORD1
12+
GSMUDP KEYWORD1
13+
14+
#######################################
15+
# Methods and Functions (KEYWORD2)
16+
#######################################
17+
18+
begin
19+
disconnect
20+
end
21+
getTime
22+
getLocalTime
23+
setTime
24+
enableCmux
25+
isCmuxEnable
26+
trace
27+
setTraceLevel
28+
ping
29+
isConnected
30+
getNetwork
31+
32+
#######################################
33+
# Constants (LITERAL1)
34+
#######################################
35+

libraries/GSM/src/GSM.cpp

+58-41
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,6 @@
2626
#include "CellularInterface.h"
2727
#include "GEMALTO_CINTERION_CellularStack.h"
2828

29-
#define MAXRETRY 3
30-
3129
arduino::CMUXClass *arduino::CMUXClass::get_default_instance()
3230
{
3331
static mbed::UnbufferedSerial serial(MBED_CONF_GEMALTO_CINTERION_TX, MBED_CONF_GEMALTO_CINTERION_RX, 115200);
@@ -47,72 +45,61 @@ mbed::CellularDevice *mbed::CellularDevice::get_default_instance()
4745

4846
int arduino::GSMClass::begin(const char* pin, const char* apn, const char* username, const char* password, RadioAccessTechnologyType rat, uint32_t band, bool restart) {
4947

50-
if(restart || isCmuxEnable()) {
51-
pinMode(MBED_CONF_GEMALTO_CINTERION_RST, OUTPUT);
52-
digitalWrite(MBED_CONF_GEMALTO_CINTERION_RST, HIGH);
53-
delay(800);
54-
digitalWrite(MBED_CONF_GEMALTO_CINTERION_RST, LOW);
55-
pinMode(MBED_CONF_GEMALTO_CINTERION_ON, OUTPUT);
56-
digitalWrite(MBED_CONF_GEMALTO_CINTERION_ON, LOW);
57-
delay(1);
58-
digitalWrite(MBED_CONF_GEMALTO_CINTERION_ON, HIGH);
59-
delay(1);
60-
// this timer is to make sure that at boottime and when the CMUX is used,
61-
// ^SYSTART is received in time to avoid stranger behaviour
62-
// from HW serial
63-
delay(2000);
48+
if (restart || isCmuxEnable()) {
49+
reset();
6450
}
6551

6652
_context = mbed::CellularContext::get_default_instance();
6753

6854
if (_context == nullptr) {
69-
printf("Invalid context\n");
55+
DEBUG_ERROR("Invalid mbed::CellularContext");
7056
return 0;
7157
}
58+
7259
pinMode(MBED_CONF_GEMALTO_CINTERION_ON, INPUT_PULLDOWN);
7360

7461
static mbed::DigitalOut rts(MBED_CONF_GEMALTO_CINTERION_RTS, 0);
7562

7663
_device = _context->get_device();
64+
_device->modem_debug_on(_at_debug);
7765

78-
_device->set_cmux_status_flag(_cmuxGSMenable);
79-
80-
_context->set_sim_pin(pin);
66+
if (!isReady()) {
67+
DEBUG_ERROR("Cellular device not ready");
68+
return 0;
69+
}
8170

71+
_device->set_cmux_status_flag(_cmuxGSMenable);
72+
_device->set_retry_timeout_array(_retry_timeout, sizeof(_retry_timeout) / sizeof(_retry_timeout[0]));
73+
#if GSM_DEBUG_ENABLE
74+
_device->attach(mbed::callback(this, &GSMClass::onStatusChange));
75+
#endif
8276
_device->init();
8377

84-
_context->set_authentication_type((mbed::CellularContext::AuthenticationType)1);
85-
8678
_pin = pin;
8779
_apn = apn;
8880
_username = username;
8981
_password = password;
9082
_rat = rat;
9183
_band = (FrequencyBand) band;
92-
_context->set_credentials(apn, username, password);
9384

94-
_context->set_access_technology(rat);
85+
_context->set_sim_pin(pin);
86+
_context->set_authentication_type(mbed::CellularContext::AuthenticationType::PAP);
87+
_context->set_credentials(_apn, _username, _password);
88+
_context->set_access_technology(_rat);
9589
_context->set_band(_band);
9690

9791
int connect_status = NSAPI_ERROR_AUTH_FAILURE;
98-
uint8_t retryCount = 0;
99-
while(connect_status != NSAPI_ERROR_OK && retryCount < MAXRETRY) {
100-
101-
connect_status = _context->connect(pin, apn, username, password);
102-
retryCount++;
103-
104-
if (connect_status == NSAPI_ERROR_AUTH_FAILURE) {
105-
tr_info("Authentication Failure. Exiting application.\n");
106-
} else if (connect_status == NSAPI_ERROR_OK || connect_status == NSAPI_ERROR_IS_CONNECTED) {
107-
connect_status = NSAPI_ERROR_OK;
108-
tr_info("Connection Established.\n");
109-
} else if (retryCount > 2) {
110-
tr_info("Fatal connection failure: %d\n", connect_status);
111-
} else {
112-
tr_info("Couldn't connect, will retry...\n");
113-
continue;
114-
}
11592

93+
DEBUG_INFO("Connecting...");
94+
connect_status = _context->connect(pin, apn, username, password);
95+
96+
if (connect_status == NSAPI_ERROR_AUTH_FAILURE) {
97+
DEBUG_ERROR("Authentication Failure. Exiting application.");
98+
} else if (connect_status == NSAPI_ERROR_OK || connect_status == NSAPI_ERROR_IS_CONNECTED) {
99+
connect_status = NSAPI_ERROR_OK;
100+
DEBUG_INFO("Connection Established.");
101+
} else {
102+
DEBUG_ERROR("Couldn't connect.");
116103
}
117104

118105
return connect_status == NSAPI_ERROR_OK ? 1 : 0;
@@ -164,4 +151,34 @@ NetworkInterface* arduino::GSMClass::getNetwork() {
164151
return _context;
165152
}
166153

154+
void arduino::GSMClass::reset() {
155+
pinMode(MBED_CONF_GEMALTO_CINTERION_RST, OUTPUT);
156+
digitalWrite(MBED_CONF_GEMALTO_CINTERION_RST, HIGH);
157+
delay(800);
158+
digitalWrite(MBED_CONF_GEMALTO_CINTERION_RST, LOW);
159+
pinMode(MBED_CONF_GEMALTO_CINTERION_ON, OUTPUT);
160+
digitalWrite(MBED_CONF_GEMALTO_CINTERION_ON, LOW);
161+
delay(1);
162+
digitalWrite(MBED_CONF_GEMALTO_CINTERION_ON, HIGH);
163+
delay(1);
164+
}
165+
166+
bool arduino::GSMClass::isReady(const int timeout) {
167+
if (!_device) {
168+
DEBUG_ERROR("No device found");
169+
return false;
170+
}
171+
172+
const unsigned int start = millis();
173+
while (_device->is_ready() != NSAPI_ERROR_OK) {
174+
175+
if (millis() - start > timeout) {
176+
DEBUG_WARNING("Timeout waiting device ready");
177+
return false;
178+
}
179+
delay(100);
180+
}
181+
return true;
182+
}
183+
167184
arduino::GSMClass GSM;

libraries/GSM/src/GSM.h

+45-3
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,6 @@
5555
#error Gemalto Cinterion cellular connectivity not supported
5656
#endif
5757

58-
#define MBED_CONF_APP_SOCK_TYPE 1
59-
6058
#if defined __has_include
6159
#if __has_include ("GPS.h")
6260
# define _CMUX_ENABLE 1
@@ -65,6 +63,27 @@
6563
#endif
6664
#endif
6765

66+
#if defined __has_include
67+
#if __has_include ("Arduino_DebugUtils.h")
68+
#include "Arduino_DebugUtils.h"
69+
#define GSM_DEBUG_ENABLE 1
70+
#else
71+
#define DEBUG_ERROR(fmt, ...)
72+
#define DEBUG_WARNING(fmt, ...)
73+
#define DEBUG_INFO(fmt, ...)
74+
#define DEBUG_DEBUG(fmt, ...)
75+
#define DEBUG_VERBOSE(fmt, ...)
76+
#define GSM_DEBUG_ENABLE 0
77+
#endif
78+
#else
79+
#define DEBUG_ERROR(fmt, ...)
80+
#define DEBUG_WARNING(fmt, ...)
81+
#define DEBUG_INFO(fmt, ...)
82+
#define DEBUG_DEBUG(fmt, ...)
83+
#define DEBUG_VERBOSE(fmt, ...)
84+
#define GSM_DEBUG_ENABLE 0
85+
#endif
86+
6887
namespace arduino {
6988

7089
typedef void* (*voidPrtFuncPtr)(void);
@@ -110,7 +129,7 @@ class GSMClass : public MbedSocketClass {
110129
bool isCmuxEnable();
111130
#if MBED_CONF_MBED_TRACE_ENABLE
112131
void trace(Stream& stream);
113-
void setTraceLevel(int trace_level, bool timestamp = false);
132+
void setTraceLevel(int trace_level, bool timestamp = false, bool at_trace = false);
114133
#endif
115134
int ping(const char* hostname, uint8_t ttl = 128);
116135
int ping(const String& hostname, uint8_t ttl = 128);
@@ -133,6 +152,29 @@ class GSMClass : public MbedSocketClass {
133152
NetworkInterface* gsm_if = nullptr;
134153
mbed::CellularContext* _context = nullptr;
135154
mbed::CellularDevice* _device = nullptr;
155+
bool _at_debug = false;
156+
157+
/* Internal cellular state machine retries. Values are in seconds.
158+
* This array also defines the maximum number of retries to 6
159+
*/
160+
const uint16_t _retry_timeout[6] = {1, 2, 4, 8, 16, 32};
161+
162+
#if GSM_DEBUG_ENABLE
163+
static constexpr int RSSI_UNKNOWN = 99;
164+
static const char * const sim_state_str[];
165+
static const char * const reg_type_str[];
166+
static const char * const rat_str[];
167+
static const char * const state_str[];
168+
static const char * const event_str[];
169+
static const char * getRATString(const mbed::CellularNetwork::RadioAccessTechnology rat);
170+
static const char * getStateString(const mbed::CellularStateMachine::CellularState state);
171+
static const char * getEventString(const cellular_event_status event);
172+
static const char * getSIMStateString(const mbed::CellularDevice::SimState state);
173+
static const char * getRegistrationStateString(const mbed::CellularNetwork::RegistrationStatus state);
174+
void onStatusChange(nsapi_event_t ev, intptr_t in);
175+
#endif
176+
void reset();
177+
bool isReady(const int timeout = 5000);
136178
};
137179

138180
}

0 commit comments

Comments
 (0)