1515 * limitations under the License.
1616 */
1717
18+ #include " rtos/ThisThread.h"
1819#include " CellularUtil.h"
1920#include " AT_CellularDevice.h"
2021#include " AT_CellularInformation.h"
@@ -202,6 +203,7 @@ nsapi_error_t AT_CellularDevice::get_sim_state(SimState &state)
202203 _at->flush ();
203204 nsapi_error_t error = _at->at_cmd_str (" +CPIN" , " ?" , simstr, sizeof (simstr));
204205 ssize_t len = strlen (simstr);
206+ device_err_t err = _at->get_last_device_error ();
205207 _at->unlock ();
206208
207209 if (len != -1 ) {
@@ -213,7 +215,6 @@ nsapi_error_t AT_CellularDevice::get_sim_state(SimState &state)
213215 state = SimStatePukNeeded;
214216 } else {
215217 simstr[len] = ' \0 ' ;
216- tr_error (" Unknown SIM state %s" , simstr);
217218 state = SimStateUnknown;
218219 }
219220 } else {
@@ -229,7 +230,11 @@ nsapi_error_t AT_CellularDevice::get_sim_state(SimState &state)
229230 tr_error (" SIM PUK required" );
230231 break ;
231232 case SimStateUnknown:
232- tr_warn (" SIM state unknown" );
233+ if (err.errType == DeviceErrorTypeErrorCME && err.errCode == 14 ) {
234+ tr_info (" SIM busy" );
235+ } else {
236+ tr_warn (" SIM state unknown" );
237+ }
233238 break ;
234239 default :
235240 tr_info (" SIM is ready" );
@@ -443,12 +448,18 @@ nsapi_error_t AT_CellularDevice::init()
443448 setup_at_handler ();
444449
445450 _at->lock ();
446- _at->flush ();
447- _at->at_cmd_discard (" E0" , " " );
448-
449- _at->at_cmd_discard (" +CMEE" , " =1" );
450-
451- _at->at_cmd_discard (" +CFUN" , " =1" );
451+ for (int retry = 1 ; retry <= 3 ; retry++) {
452+ _at->clear_error ();
453+ _at->flush ();
454+ _at->at_cmd_discard (" E0" , " " );
455+ _at->at_cmd_discard (" +CMEE" , " =1" );
456+ _at->at_cmd_discard (" +CFUN" , " =1" );
457+ if (_at->get_last_error () == NSAPI_ERROR_OK) {
458+ break ;
459+ }
460+ tr_debug (" Wait 100ms to init modem" );
461+ rtos::ThisThread::sleep_for (100 ); // let modem have time to get ready
462+ }
452463
453464 return _at->unlock_return_error ();
454465}
0 commit comments