2626#include " CellularInterface.h"
2727#include " GEMALTO_CINTERION_CellularStack.h"
2828
29- #define MAXRETRY 3
30-
3129arduino::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
4846int 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+
167184arduino::GSMClass GSM;
0 commit comments