26
26
#include " CellularInterface.h"
27
27
#include " GEMALTO_CINTERION_CellularStack.h"
28
28
29
- #define MAXRETRY 3
30
-
31
29
arduino::CMUXClass *arduino::CMUXClass::get_default_instance ()
32
30
{
33
31
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()
47
45
48
46
int arduino::GSMClass::begin (const char * pin, const char * apn, const char * username, const char * password, RadioAccessTechnologyType rat, uint32_t band, bool restart) {
49
47
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 ();
64
50
}
65
51
66
52
_context = mbed::CellularContext::get_default_instance ();
67
53
68
54
if (_context == nullptr ) {
69
- printf (" Invalid context \n " );
55
+ DEBUG_ERROR (" Invalid mbed::CellularContext " );
70
56
return 0 ;
71
57
}
58
+
72
59
pinMode (MBED_CONF_GEMALTO_CINTERION_ON, INPUT_PULLDOWN);
73
60
74
61
static mbed::DigitalOut rts (MBED_CONF_GEMALTO_CINTERION_RTS, 0 );
75
62
76
63
_device = _context->get_device ();
64
+ _device->modem_debug_on (_at_debug);
77
65
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
+ }
81
70
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
82
76
_device->init ();
83
77
84
- _context->set_authentication_type ((mbed::CellularContext::AuthenticationType)1 );
85
-
86
78
_pin = pin;
87
79
_apn = apn;
88
80
_username = username;
89
81
_password = password;
90
82
_rat = rat;
91
83
_band = (FrequencyBand) band;
92
- _context->set_credentials (apn, username, password);
93
84
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);
95
89
_context->set_band (_band);
96
90
97
91
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
- }
115
92
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." );
116
103
}
117
104
118
105
return connect_status == NSAPI_ERROR_OK ? 1 : 0 ;
@@ -164,4 +151,34 @@ NetworkInterface* arduino::GSMClass::getNetwork() {
164
151
return _context;
165
152
}
166
153
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
+
167
184
arduino::GSMClass GSM;
0 commit comments