Skip to content

Commit 9a81f86

Browse files
committed
GSM: enable debugging without mbed trace
* Based on mbed attach callback and Arduino_DebugUtils
1 parent f4d7f33 commit 9a81f86

File tree

3 files changed

+319
-5
lines changed

3 files changed

+319
-5
lines changed

libraries/GSM/src/GSM.cpp

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ int arduino::GSMClass::begin(const char* pin, const char* apn, const char* usern
6666
_context = mbed::CellularContext::get_default_instance();
6767

6868
if (_context == nullptr) {
69-
printf("Invalid context\n");
69+
DEBUG_ERROR("Invalid mbed::CellularContext");
7070
return 0;
7171
}
7272
pinMode(MBED_CONF_GEMALTO_CINTERION_ON, INPUT_PULLDOWN);
@@ -81,6 +81,10 @@ int arduino::GSMClass::begin(const char* pin, const char* apn, const char* usern
8181

8282
_context->set_sim_pin(pin);
8383

84+
#if GSM_DEBUG_ENABLE
85+
_device->attach(mbed::callback(this, &GSMClass::onStatusChange));
86+
#endif
87+
8488
_device->init();
8589

8690
_context->set_authentication_type((mbed::CellularContext::AuthenticationType)1);
@@ -104,14 +108,14 @@ int arduino::GSMClass::begin(const char* pin, const char* apn, const char* usern
104108
retryCount++;
105109

106110
if (connect_status == NSAPI_ERROR_AUTH_FAILURE) {
107-
tr_info("Authentication Failure. Exiting application.\n");
111+
DEBUG_ERROR("Authentication Failure. Exiting application.");
108112
} else if (connect_status == NSAPI_ERROR_OK || connect_status == NSAPI_ERROR_IS_CONNECTED) {
109113
connect_status = NSAPI_ERROR_OK;
110-
tr_info("Connection Established.\n");
114+
DEBUG_INFO("Connection Established.");
111115
} else if (retryCount > 2) {
112-
tr_info("Fatal connection failure: %d\n", connect_status);
116+
DEBUG_ERROR("Fatal connection failure: %d", connect_status);
113117
} else {
114-
tr_info("Couldn't connect, will retry...\n");
118+
DEBUG_WARNING("Couldn't connect, will retry...");
115119
continue;
116120
}
117121

libraries/GSM/src/GSM.h

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,27 @@
6363
#endif
6464
#endif
6565

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+
6687
namespace arduino {
6788

6889
typedef void* (*voidPrtFuncPtr)(void);
@@ -132,6 +153,21 @@ class GSMClass : public MbedSocketClass {
132153
mbed::CellularContext* _context = nullptr;
133154
mbed::CellularDevice* _device = nullptr;
134155
bool _at_debug = false;
156+
157+
#if GSM_DEBUG_ENABLE
158+
static constexpr int RSSI_UNKNOWN = 99;
159+
static const char * const sim_state_str[];
160+
static const char * const reg_type_str[];
161+
static const char * const rat_str[];
162+
static const char * const state_str[];
163+
static const char * const event_str[];
164+
static const char * getRATString(const mbed::CellularNetwork::RadioAccessTechnology rat);
165+
static const char * getStateString(const mbed::CellularStateMachine::CellularState state);
166+
static const char * getEventString(const cellular_event_status event);
167+
static const char * getSIMStateString(const mbed::CellularDevice::SimState state);
168+
static const char * getRegistrationStateString(const mbed::CellularNetwork::RegistrationStatus state);
169+
void onStatusChange(nsapi_event_t ev, intptr_t in);
170+
#endif
135171
};
136172

137173
}

libraries/GSM/src/GSMDebug.cpp

Lines changed: 274 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,274 @@
1+
/*
2+
GSMDebug.cpp - Library for GSM on mbed platforms.
3+
Copyright (c) 2011-2023 Arduino LLC. All right reserved.
4+
5+
This library is free software; you can redistribute it and/or
6+
modify it under the terms of the GNU Lesser General Public
7+
License as published by the Free Software Foundation; either
8+
version 2.1 of the License, or (at your option) any later version.
9+
10+
This library is distributed in the hope that it will be useful,
11+
but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13+
Lesser General Public License for more details.
14+
15+
You should have received a copy of the GNU Lesser General Public
16+
License along with this library; if not, write to the Free Software
17+
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18+
*/
19+
20+
#include <GSM.h>
21+
22+
#if GSM_DEBUG_ENABLE
23+
24+
constexpr const char * const arduino::GSMClass::sim_state_str[] = {
25+
"Ready",
26+
"PIN Needed",
27+
"PUK Needed",
28+
"Unknown"
29+
};
30+
31+
constexpr const char * const arduino::GSMClass::reg_type_str[] = {
32+
"Not Registered",
33+
"Registered (Home Network)",
34+
"Searching Network",
35+
"Registration Denied",
36+
"Registration Unknown",
37+
"Registered (Roaming)",
38+
"Registered (SMS Only Home)",
39+
"Registered (SMS Only Roaming)",
40+
"Attached (Emergency Only)",
41+
"Registered (CSFB Not Preferred Home)",
42+
"Registered (CSFB Not Preferred Roaming)",
43+
"Already Registered"
44+
};
45+
46+
constexpr const char * const arduino::GSMClass::rat_str[] = {
47+
"GSM",
48+
"GSM_COMPACT",
49+
"UTRAN",
50+
"EGPRS",
51+
"HSDPA",
52+
"HSUPA",
53+
"HSDPA_HSUPA",
54+
"E_UTRAN",
55+
"CATM1",
56+
"NB1",
57+
"RAT unknown",
58+
};
59+
60+
constexpr const char * const arduino::GSMClass::state_str[] = {
61+
"Init",
62+
"Power On",
63+
"Device ready",
64+
"SIM PIN",
65+
"Signal quality",
66+
"Registering network",
67+
"Attaching network",
68+
"Unknown"
69+
};
70+
71+
constexpr const char * const arduino::GSMClass::event_str[] = {
72+
"Device ready",
73+
"SIM status",
74+
"Registration status",
75+
"Registration type",
76+
"Cell ID",
77+
"RAT",
78+
"Attach network",
79+
"Activate PDP context",
80+
"Signal quality",
81+
"Retry",
82+
"Timeout",
83+
};
84+
85+
const char * arduino::GSMClass::getRATString(const mbed::CellularNetwork::RadioAccessTechnology rat) {
86+
switch (rat) {
87+
case mbed::CellularNetwork::RadioAccessTechnology::RAT_GSM:
88+
case mbed::CellularNetwork::RadioAccessTechnology::RAT_GSM_COMPACT:
89+
case mbed::CellularNetwork::RadioAccessTechnology::RAT_UTRAN:
90+
case mbed::CellularNetwork::RadioAccessTechnology::RAT_EGPRS:
91+
case mbed::CellularNetwork::RadioAccessTechnology::RAT_HSDPA:
92+
case mbed::CellularNetwork::RadioAccessTechnology::RAT_HSUPA:
93+
case mbed::CellularNetwork::RadioAccessTechnology::RAT_HSDPA_HSUPA:
94+
case mbed::CellularNetwork::RadioAccessTechnology::RAT_E_UTRAN:
95+
case mbed::CellularNetwork::RadioAccessTechnology::RAT_CATM1:
96+
case mbed::CellularNetwork::RadioAccessTechnology::RAT_NB1:
97+
return rat_str[rat];
98+
break;
99+
100+
case mbed::CellularNetwork::RadioAccessTechnology::RAT_UNKNOWN:
101+
case mbed::CellularNetwork::RadioAccessTechnology::RAT_MAX:
102+
default:
103+
return rat_str[mbed::CellularNetwork::RadioAccessTechnology::RAT_UNKNOWN];
104+
break;
105+
}
106+
}
107+
108+
const char * arduino::GSMClass::getStateString(const mbed::CellularStateMachine::CellularState state) {
109+
switch (state) {
110+
case mbed::CellularStateMachine::CellularState::STATE_INIT:
111+
case mbed::CellularStateMachine::CellularState::STATE_POWER_ON:
112+
case mbed::CellularStateMachine::CellularState::STATE_DEVICE_READY:
113+
case mbed::CellularStateMachine::CellularState::STATE_SIM_PIN:
114+
case mbed::CellularStateMachine::CellularState::STATE_SIGNAL_QUALITY:
115+
case mbed::CellularStateMachine::CellularState::STATE_REGISTERING_NETWORK:
116+
case mbed::CellularStateMachine::CellularState::STATE_ATTACHING_NETWORK:
117+
return state_str[state];
118+
break;
119+
120+
case mbed::CellularStateMachine::CellularState::STATE_MAX_FSM_STATE:
121+
default:
122+
return state_str[mbed::CellularStateMachine::CellularState::STATE_MAX_FSM_STATE];
123+
break;
124+
}
125+
}
126+
127+
const char * arduino::GSMClass::getEventString(const cellular_event_status event) {
128+
switch (event) {
129+
case cellular_event_status::CellularDeviceReady:
130+
case cellular_event_status::CellularSIMStatusChanged:
131+
case cellular_event_status::CellularRegistrationStatusChanged:
132+
case cellular_event_status::CellularRegistrationTypeChanged:
133+
case cellular_event_status::CellularCellIDChanged:
134+
case cellular_event_status::CellularRadioAccessTechnologyChanged:
135+
case cellular_event_status::CellularAttachNetwork:
136+
case cellular_event_status::CellularActivatePDPContext:
137+
case cellular_event_status::CellularSignalQuality:
138+
case cellular_event_status::CellularStateRetryEvent:
139+
case cellular_event_status::CellularDeviceTimeout:
140+
return event_str[event - NSAPI_EVENT_CELLULAR_STATUS_BASE];
141+
break;
142+
143+
default:
144+
return "Unknown";
145+
break;
146+
}
147+
}
148+
149+
const char * arduino::GSMClass::getSIMStateString(const mbed::CellularDevice::SimState state) {
150+
switch (state) {
151+
case mbed::CellularDevice::SimStateReady:
152+
case mbed::CellularDevice::SimStatePinNeeded:
153+
case mbed::CellularDevice::SimStatePukNeeded:
154+
case mbed::CellularDevice::SimStateUnknown:
155+
return sim_state_str[state];
156+
break;
157+
158+
default:
159+
return sim_state_str[mbed::CellularDevice::SimStateUnknown];
160+
}
161+
}
162+
163+
const char * arduino::GSMClass::getRegistrationStateString(const mbed::CellularNetwork::RegistrationStatus state) {
164+
switch (state) {
165+
case mbed::CellularNetwork::StatusNotAvailable:
166+
case mbed::CellularNetwork::NotRegistered:
167+
case mbed::CellularNetwork::RegisteredHomeNetwork:
168+
case mbed::CellularNetwork::SearchingNetwork:
169+
case mbed::CellularNetwork::RegistrationDenied:
170+
case mbed::CellularNetwork::Unknown:
171+
case mbed::CellularNetwork::RegisteredRoaming:
172+
case mbed::CellularNetwork::RegisteredSMSOnlyHome:
173+
case mbed::CellularNetwork::RegisteredSMSOnlyRoaming:
174+
case mbed::CellularNetwork::AttachedEmergencyOnly:
175+
case mbed::CellularNetwork::RegisteredCSFBNotPreferredHome:
176+
case mbed::CellularNetwork::RegisteredCSFBNotPreferredRoaming:
177+
case mbed::CellularNetwork::AlreadyRegistered:
178+
return reg_type_str[state];
179+
break;
180+
181+
default:
182+
return reg_type_str[mbed::CellularNetwork::Unknown];
183+
}
184+
}
185+
186+
void arduino::GSMClass::onStatusChange(nsapi_event_t ev, intptr_t in) {
187+
188+
const cell_callback_data_t *data = (const cell_callback_data_t *)in;
189+
190+
switch(ev)
191+
{
192+
case CellularDeviceReady:
193+
{
194+
DEBUG_INFO("Modem is powered and ready to receive commands");
195+
}
196+
break;
197+
198+
case CellularSIMStatusChanged:
199+
{
200+
const mbed::CellularDevice::SimState state = static_cast<mbed::CellularDevice::SimState>(data->status_data);
201+
DEBUG_INFO("SIM status: %s", getSIMStateString(state));
202+
}
203+
break;
204+
205+
case CellularRegistrationStatusChanged:
206+
{
207+
const mbed::CellularNetwork::RegistrationStatus state = static_cast<mbed::CellularNetwork::RegistrationStatus>(data->status_data);
208+
DEBUG_INFO("Registration status: %s", getRegistrationStateString(state));
209+
}
210+
break;
211+
212+
case CellularRegistrationTypeChanged:
213+
{
214+
/* Never called from mbed driver */
215+
}
216+
break;
217+
218+
case CellularCellIDChanged:
219+
{
220+
DEBUG_INFO("Cellular ID changed: %d", data->status_data);
221+
}
222+
break;
223+
224+
case CellularRadioAccessTechnologyChanged:
225+
{
226+
const mbed::CellularNetwork::RadioAccessTechnology rat = static_cast <mbed::CellularNetwork::RadioAccessTechnology>(data->status_data);
227+
DEBUG_INFO("RAT changed: %s", getRATString(rat));
228+
}
229+
break;
230+
231+
case CellularAttachNetwork:
232+
{
233+
DEBUG_INFO("Network status: %s", data->status_data ? "Attached" : "Detached");
234+
}
235+
break;
236+
237+
case CellularActivatePDPContext:
238+
{
239+
DEBUG_INFO("Activate PDP context %s", (data->error != NSAPI_ERROR_OK) ? "Failure" : "Success");
240+
}
241+
break;
242+
243+
case CellularSignalQuality:
244+
{
245+
const cell_signal_quality_t * sig = (const cell_signal_quality_t *)data->data;
246+
if((data->error != NSAPI_ERROR_OK) || (sig->rssi == RSSI_UNKNOWN)) {
247+
DEBUG_INFO("RSSI: Unknown");
248+
} else {
249+
DEBUG_INFO("RSSI: %d", sig->rssi);
250+
}
251+
}
252+
break;
253+
254+
case CellularStateRetryEvent:
255+
{
256+
const cell_retry_cb_t * retry_cb_data = (const cell_retry_cb_t *)data->data;
257+
const cellular_event_status event = static_cast<cellular_event_status>(data->status_data);
258+
const mbed::CellularStateMachine::CellularState state = static_cast<mbed::CellularStateMachine::CellularState>(retry_cb_data->state);
259+
DEBUG_WARNING("Cellular event %s timed out. Cellular state %s, retry count %d", getEventString(event), getStateString(state), retry_cb_data->retry_count);
260+
}
261+
break;
262+
263+
case CellularDeviceTimeout:
264+
{
265+
const cell_timeout_cb_t * timeout_cb_data = (const cell_timeout_cb_t *)data->data;
266+
const cellular_event_status event = static_cast<cellular_event_status>(data->status_data);
267+
const mbed::CellularStateMachine::CellularState state = static_cast<mbed::CellularStateMachine::CellularState>(timeout_cb_data->state);
268+
DEBUG_DEBUG("Cellular state: %s, waiting for event %s. Timeout %d", getStateString(state), getEventString(event), timeout_cb_data->timeout);
269+
}
270+
break;
271+
}
272+
}
273+
274+
#endif

0 commit comments

Comments
 (0)