Kenji Arai / TYBLE16_mbedlized_os5_several_examples_1st

Dependencies:   nRF51_Vdd TextLCD BME280

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers CellularConnectionFSM.cpp Source File

CellularConnectionFSM.cpp

00001 /*
00002  * Copyright (c) 2017, Arm Limited and affiliates.
00003  * SPDX-License-Identifier: Apache-2.0
00004  *
00005  * Licensed under the Apache License, Version 2.0 (the "License");
00006  * you may not use this file except in compliance with the License.
00007  * You may obtain a copy of the License at
00008  *
00009  *     http://www.apache.org/licenses/LICENSE-2.0
00010  *
00011  * Unless required by applicable law or agreed to in writing, software
00012  * distributed under the License is distributed on an "AS IS" BASIS,
00013  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00014  * See the License for the specific language governing permissions and
00015  * limitations under the License.
00016  */
00017 
00018 #include "CellularConnectionFSM.h"
00019 
00020 #ifdef CELLULAR_DEVICE
00021 
00022 #ifndef MBED_TRACE_MAX_LEVEL
00023 #define MBED_TRACE_MAX_LEVEL TRACE_LEVEL_INFO
00024 #endif
00025 #include "CellularLog.h"
00026 #include "CellularCommon.h"
00027 
00028 // timeout to wait for AT responses
00029 #define TIMEOUT_POWER_ON     (1*1000)
00030 #define TIMEOUT_SIM_PIN      (1*1000)
00031 #define TIMEOUT_NETWORK      (10*1000)
00032 #define TIMEOUT_CONNECT      (60*1000)
00033 #define TIMEOUT_REGISTRATION (180*1000)
00034 
00035 // maximum time when retrying network register, attach and connect in seconds ( 20minutes )
00036 #define TIMEOUT_NETWORK_MAX (20*60)
00037 
00038 #define RETRY_COUNT_DEFAULT 3
00039 
00040 namespace mbed {
00041 
00042 CellularConnectionFSM::CellularConnectionFSM() :
00043     _serial(0), _state(STATE_INIT), _next_state(_state), _status_callback(0), _event_status_cb(0), _network(0), _power(0), _sim(0),
00044     _queue(8 * EVENTS_EVENT_SIZE), _queue_thread(0), _cellularDevice(0), _retry_count(0), _event_timeout(-1),
00045     _at_queue(8 * EVENTS_EVENT_SIZE), _event_id(0), _plmn(0), _command_success(false), _plmn_network_found(false)
00046 {
00047     memset(_sim_pin, 0, sizeof(_sim_pin));
00048 #if MBED_CONF_CELLULAR_RANDOM_MAX_START_DELAY == 0
00049     _start_time = 0;
00050 #else
00051     // so that not every device don't start at the exact same time (for example after power outage)
00052     _start_time = rand() % (MBED_CONF_CELLULAR_RANDOM_MAX_START_DELAY);
00053 #endif // MBED_CONF_CELLULAR_RANDOM_MAX_START_DELAY
00054 
00055     // set initial retry values in seconds
00056     _retry_timeout_array[0] = 1; // double time on each retry in order to keep network happy
00057     _retry_timeout_array[1] = 2;
00058     _retry_timeout_array[2] = 4;
00059     _retry_timeout_array[3] = 8;
00060     _retry_timeout_array[4] = 16;
00061     _retry_timeout_array[5] = 32;
00062     _retry_timeout_array[6] = 64;
00063     _retry_timeout_array[7] = 128; // if around two minutes was not enough then let's wait much longer
00064     _retry_timeout_array[8] = 600;
00065     _retry_timeout_array[9] = TIMEOUT_NETWORK_MAX;
00066     _retry_array_length = MAX_RETRY_ARRAY_SIZE;
00067 }
00068 
00069 CellularConnectionFSM::~CellularConnectionFSM()
00070 {
00071     stop();
00072 }
00073 
00074 void CellularConnectionFSM::stop()
00075 {
00076     _queue.cancel(_event_id);
00077     _queue.break_dispatch();
00078 
00079     if (_queue_thread) {
00080         _queue_thread->terminate();
00081         delete _queue_thread;
00082         _queue_thread = NULL;
00083     }
00084 
00085     delete _cellularDevice;
00086     _cellularDevice = NULL;
00087     // _cellularDevice closes all interfaces in destructor
00088     _power = NULL;
00089     _network = NULL;
00090     _sim = NULL;
00091 
00092     _state = STATE_INIT;
00093     _next_state = _state;
00094 }
00095 
00096 nsapi_error_t CellularConnectionFSM::init()
00097 {
00098     tr_info("CELLULAR_DEVICE: %s", CELLULAR_STRINGIFY(CELLULAR_DEVICE));
00099     _cellularDevice = new CELLULAR_DEVICE(_at_queue);
00100     if (!_cellularDevice) {
00101         stop();
00102         return NSAPI_ERROR_NO_MEMORY ;
00103     }
00104 
00105     _power = _cellularDevice->open_power(_serial);
00106     if (!_power) {
00107         stop();
00108         return NSAPI_ERROR_NO_MEMORY ;
00109     }
00110 
00111     _network = _cellularDevice->open_network(_serial);
00112     if (!_network) {
00113         stop();
00114         return NSAPI_ERROR_NO_MEMORY ;
00115     }
00116 
00117     _sim = _cellularDevice->open_sim(_serial);
00118     if (!_sim) {
00119         stop();
00120         return NSAPI_ERROR_NO_MEMORY ;
00121     }
00122 
00123     _at_queue.chain(&_queue);
00124 
00125     _retry_count = 0;
00126     _state = STATE_INIT;
00127     _next_state = STATE_INIT;
00128 
00129     return _network->init();
00130 }
00131 
00132 bool CellularConnectionFSM::power_on()
00133 {
00134     nsapi_error_t err = _power->on();
00135     if (err != NSAPI_ERROR_OK  && err != NSAPI_ERROR_UNSUPPORTED ) {
00136         tr_warn("Cellular start failed. Power off/on.");
00137         err = _power->off();
00138         if (err != NSAPI_ERROR_OK  && err != NSAPI_ERROR_UNSUPPORTED ) {
00139             tr_error("Cellular power down failing after failed power up attempt!");
00140         }
00141         return false;
00142     }
00143     return true;
00144 }
00145 
00146 void CellularConnectionFSM::set_sim_pin(const char *sim_pin)
00147 {
00148     strncpy(_sim_pin, sim_pin, sizeof(_sim_pin));
00149     _sim_pin[sizeof(_sim_pin) - 1] = '\0';
00150 }
00151 
00152 void CellularConnectionFSM::set_plmn(const char *plmn)
00153 {
00154     _plmn = plmn;
00155 }
00156 
00157 bool CellularConnectionFSM::open_sim()
00158 {
00159     CellularSIM::SimState state = CellularSIM::SimStateUnknown;
00160     // wait until SIM is readable
00161     // here you could add wait(secs) if you know start delay of your SIM
00162     if (_sim->get_sim_state(state) != NSAPI_ERROR_OK ) {
00163         tr_info("Waiting for SIM (err while reading)...");
00164         if (_event_status_cb) {
00165             _event_status_cb((nsapi_event_t)CellularSIMStatusChanged, state);
00166         }
00167         return false;
00168     }
00169 
00170     // report current state so callback can set sim pin if needed
00171     if (_event_status_cb) {
00172         _event_status_cb((nsapi_event_t)CellularSIMStatusChanged, state);
00173     }
00174 
00175     if (state == CellularSIM::SimStatePinNeeded) {
00176         if (strlen(_sim_pin)) {
00177             tr_info("SIM pin required, entering pin");
00178             nsapi_error_t err = _sim->set_pin(_sim_pin);
00179             if (err) {
00180                 tr_error("SIM pin set failed with: %d, bailing out...", err);
00181             }
00182         } else {
00183             // No sim pin provided even it's needed, stop state machine
00184             tr_error("PIN required but No SIM pin provided.");
00185             _retry_count = MAX_RETRY_ARRAY_SIZE;
00186             return false;
00187         }
00188     }
00189 
00190     return state == CellularSIM::SimStateReady;
00191 }
00192 
00193 bool CellularConnectionFSM::is_registered()
00194 {
00195     CellularNetwork::RegistrationStatus status;
00196     bool is_registered = false;
00197 
00198     for (int type = 0; type < CellularNetwork::C_MAX; type++) {
00199         if (get_network_registration((CellularNetwork::RegistrationType) type, status, is_registered)) {
00200             tr_debug("get_network_registration: type=%d, status=%d", type, status);
00201             if (is_registered) {
00202                 break;
00203             }
00204         }
00205     }
00206 
00207     return is_registered;
00208 }
00209 
00210 bool CellularConnectionFSM::get_network_registration(CellularNetwork::RegistrationType type,
00211                                                      CellularNetwork::RegistrationStatus &status, bool &is_registered)
00212 {
00213     is_registered = false;
00214     bool is_roaming = false;
00215     nsapi_error_t err = _network->get_registration_status(type, status);
00216     if (err != NSAPI_ERROR_OK ) {
00217         if (err != NSAPI_ERROR_UNSUPPORTED ) {
00218             tr_warn("Get network registration failed (type %d)!", type);
00219         }
00220         return false;
00221     }
00222     switch (status) {
00223         case CellularNetwork::RegisteredRoaming:
00224             is_roaming = true;
00225         // fall-through
00226         case CellularNetwork::RegisteredHomeNetwork:
00227             is_registered = true;
00228             break;
00229         case CellularNetwork::RegisteredSMSOnlyRoaming:
00230             is_roaming = true;
00231         // fall-through
00232         case CellularNetwork::RegisteredSMSOnlyHome:
00233             tr_warn("SMS only network registration!");
00234             break;
00235         case CellularNetwork::RegisteredCSFBNotPreferredRoaming:
00236             is_roaming = true;
00237         // fall-through
00238         case CellularNetwork::RegisteredCSFBNotPreferredHome:
00239             tr_warn("Not preferred network registration!");
00240             break;
00241         case CellularNetwork::AttachedEmergencyOnly:
00242             tr_warn("Emergency only network registration!");
00243             break;
00244         case CellularNetwork::RegistrationDenied:
00245         case CellularNetwork::NotRegistered:
00246         case CellularNetwork::Unknown:
00247         case CellularNetwork::SearchingNetwork:
00248         default:
00249             break;
00250     }
00251 
00252     if (is_roaming) {
00253         tr_warn("Roaming cellular network!");
00254     }
00255 
00256     return true;
00257 }
00258 
00259 void CellularConnectionFSM::report_failure(const char *msg)
00260 {
00261     tr_error("Cellular network failed: %s", msg);
00262     if (_status_callback) {
00263         _status_callback(_state, _next_state);
00264     }
00265 }
00266 
00267 const char *CellularConnectionFSM::get_state_string(CellularState state)
00268 {
00269 #if MBED_CONF_MBED_TRACE_ENABLE
00270     static const char *strings[] = { "Init", "Power", "Device ready", "SIM pin", "Registering network", "Manual registering", "Attaching network", "Activating PDP Context", "Connecting network", "Connected"};
00271     return strings[state];
00272 #else
00273     return NULL;
00274 #endif // #if MBED_CONF_MBED_TRACE_ENABLE
00275 }
00276 
00277 bool CellularConnectionFSM::is_registered_to_plmn()
00278 {
00279     int format;
00280     CellularNetwork::operator_t op;
00281 
00282     nsapi_error_t err = _network->get_operator_params(format, op);
00283     if (err == NSAPI_ERROR_OK ) {
00284         if (format == 2) {
00285             // great, numeric format we can do comparison for that
00286             if (strcmp(op.op_num, _plmn) == 0) {
00287                 return true;
00288             }
00289             return false;
00290         }
00291 
00292         // format was alpha, get operator names to do the comparing
00293         CellularNetwork::operator_names_list names_list;
00294         nsapi_error_t err = _network->get_operator_names(names_list);
00295         if (err == NSAPI_ERROR_OK ) {
00296             CellularNetwork::operator_names_t *op_names = names_list.get_head();
00297             bool found_match = false;
00298             while (op_names) {
00299                 if (format == 0) {
00300                     if (strcmp(op.op_long, op_names->alpha) == 0) {
00301                         found_match = true;
00302                     }
00303                 } else if (format == 1) {
00304                     if (strcmp(op.op_short, op_names->alpha) == 0) {
00305                         found_match = true;
00306                     }
00307                 }
00308 
00309                 if (found_match) {
00310                     if (strcmp(_plmn, op_names->numeric)) {
00311                         names_list.delete_all();
00312                         return true;
00313                     }
00314                     names_list.delete_all();
00315                     return false;
00316                 }
00317             }
00318         }
00319         names_list.delete_all();
00320     }
00321 
00322     return false;
00323 }
00324 
00325 nsapi_error_t CellularConnectionFSM::continue_from_state(CellularState state)
00326 {
00327     tr_info("Continue state from %s to %s", get_state_string((CellularConnectionFSM::CellularState)_state),
00328             get_state_string((CellularConnectionFSM::CellularState)state));
00329     _state = state;
00330     _next_state = state;
00331     _retry_count = 0;
00332     if (!_queue.call_in(0, callback(this, &CellularConnectionFSM::event))) {
00333         stop();
00334         return NSAPI_ERROR_NO_MEMORY ;
00335     }
00336 
00337     return NSAPI_ERROR_OK ;
00338 }
00339 
00340 nsapi_error_t CellularConnectionFSM::continue_to_state(CellularState state)
00341 {
00342     MBED_ASSERT(_cellularDevice);
00343     _retry_count = 0;
00344     if (state < _state) {
00345         _state = state;
00346     } else {
00347         // update next state so that we don't continue from previous state
00348         _state = _next_state;
00349     }
00350     if (!_queue.call_in(0, callback(this, &CellularConnectionFSM::event))) {
00351         stop();
00352         return NSAPI_ERROR_NO_MEMORY ;
00353     }
00354 
00355     return NSAPI_ERROR_OK ;
00356 }
00357 
00358 void CellularConnectionFSM::enter_to_state(CellularState state)
00359 {
00360     _next_state = state;
00361     _retry_count = 0;
00362     _command_success = false;
00363 }
00364 
00365 void CellularConnectionFSM::retry_state_or_fail()
00366 {
00367     if (++_retry_count < MAX_RETRY_ARRAY_SIZE) {
00368         tr_debug("Retry State %s, retry %d/%d", get_state_string(_state), _retry_count, MAX_RETRY_ARRAY_SIZE);
00369         _event_timeout = _retry_timeout_array[_retry_count];
00370     } else {
00371         report_failure(get_state_string(_state));
00372         return;
00373     }
00374 }
00375 
00376 void CellularConnectionFSM::state_init()
00377 {
00378     // we should check that if power is already on then we can jump to device ready state
00379     _cellularDevice->set_timeout(TIMEOUT_POWER_ON);
00380     tr_info("Cellular state init (timeout %d ms)", TIMEOUT_POWER_ON);
00381     nsapi_error_t err = _power->is_device_ready();
00382     if (err != NSAPI_ERROR_OK ) {
00383         _event_timeout = _start_time;
00384         tr_info("Init state, waiting %d ms before POWER state)", _start_time);
00385         enter_to_state(STATE_POWER_ON);
00386     } else {
00387         tr_info("Device was ready to accept commands, jump to device ready");
00388         enter_to_state(STATE_DEVICE_READY);
00389     }
00390 }
00391 
00392 void CellularConnectionFSM::state_power_on()
00393 {
00394     _cellularDevice->set_timeout(TIMEOUT_POWER_ON);
00395     tr_info("Cellular power ON (timeout %d ms)", TIMEOUT_POWER_ON);
00396     if (power_on()) {
00397         enter_to_state(STATE_DEVICE_READY);
00398     } else {
00399         // retry to power on device
00400         retry_state_or_fail();
00401     }
00402 }
00403 
00404 bool CellularConnectionFSM::device_ready()
00405 {
00406     if (_cellularDevice->init_module(_serial) != NSAPI_ERROR_OK ) {
00407         return false;
00408     }
00409     tr_info("Cellular device ready");
00410     if (_event_status_cb) {
00411         _event_status_cb((nsapi_event_t)CellularDeviceReady, 0);
00412     }
00413     _power->remove_device_ready_urc_cb(mbed::callback(this, &CellularConnectionFSM::ready_urc_cb));
00414     _cellularDevice->close_power();
00415     _power = NULL;
00416     return true;
00417 }
00418 
00419 void CellularConnectionFSM::state_device_ready()
00420 {
00421     _cellularDevice->set_timeout(TIMEOUT_POWER_ON);
00422     if (_power->set_at_mode() == NSAPI_ERROR_OK ) {
00423         if (device_ready()) {
00424             enter_to_state(STATE_SIM_PIN);
00425         }
00426     } else {
00427         if (_retry_count == 0) {
00428             (void)_power->set_device_ready_urc_cb(mbed::callback(this, &CellularConnectionFSM::ready_urc_cb));
00429         }
00430         retry_state_or_fail();
00431     }
00432 }
00433 
00434 void CellularConnectionFSM::state_sim_pin()
00435 {
00436     _cellularDevice->set_timeout(TIMEOUT_SIM_PIN);
00437     tr_info("Sim state (timeout %d ms)", TIMEOUT_SIM_PIN);
00438     if (open_sim()) {
00439         bool success = false;
00440         for (int type = 0; type < CellularNetwork::C_MAX; type++) {
00441             if (!_network->set_registration_urc((CellularNetwork::RegistrationType)type, true)) {
00442                 success = true;
00443             }
00444         }
00445         if (!success) {
00446             tr_warn("Failed to set any URC's for registration");
00447             retry_state_or_fail();
00448             return;
00449         }
00450         if (_plmn) {
00451             enter_to_state(STATE_MANUAL_REGISTERING_NETWORK);
00452         } else {
00453             enter_to_state(STATE_REGISTERING_NETWORK);
00454         }
00455     } else {
00456         retry_state_or_fail();
00457     }
00458 }
00459 
00460 void CellularConnectionFSM::state_registering()
00461 {
00462     _cellularDevice->set_timeout(TIMEOUT_NETWORK);
00463     if (is_registered()) {
00464         // we are already registered, go to attach
00465         enter_to_state(STATE_ATTACHING_NETWORK);
00466     } else {
00467         _cellularDevice->set_timeout(TIMEOUT_REGISTRATION);
00468         if (!_command_success) {
00469             _command_success = (_network->set_registration() == NSAPI_ERROR_OK );
00470         }
00471         retry_state_or_fail();
00472     }
00473 }
00474 
00475 // only used when _plmn is set
00476 void CellularConnectionFSM::state_manual_registering_network()
00477 {
00478     _cellularDevice->set_timeout(TIMEOUT_REGISTRATION);
00479     tr_info("state_manual_registering_network");
00480     if (!_plmn_network_found) {
00481         if (is_registered() && is_registered_to_plmn()) {
00482             _plmn_network_found = true;
00483             enter_to_state(STATE_ATTACHING_NETWORK);
00484         } else {
00485             if (!_command_success) {
00486                 _command_success = (_network->set_registration(_plmn) == NSAPI_ERROR_OK );
00487             }
00488             retry_state_or_fail();
00489         }
00490     }
00491 }
00492 
00493 void CellularConnectionFSM::state_attaching()
00494 {
00495     _cellularDevice->set_timeout(TIMEOUT_CONNECT);
00496     if (_network->set_attach() == NSAPI_ERROR_OK ) {
00497         _cellularDevice->close_sim();
00498         _sim = NULL;
00499         enter_to_state(STATE_ACTIVATING_PDP_CONTEXT);
00500     } else {
00501         retry_state_or_fail();
00502     }
00503 }
00504 
00505 void CellularConnectionFSM::state_activating_pdp_context()
00506 {
00507     _cellularDevice->set_timeout(TIMEOUT_CONNECT);
00508     tr_info("Activate PDP Context (timeout %d ms)", TIMEOUT_CONNECT);
00509     if (_network->activate_context() == NSAPI_ERROR_OK ) {
00510         // when using modems stack connect is synchronous
00511         _next_state = STATE_CONNECTING_NETWORK;
00512     } else {
00513         retry_state_or_fail();
00514     }
00515 }
00516 
00517 void CellularConnectionFSM::state_connect_to_network()
00518 {
00519     _cellularDevice->set_timeout(TIMEOUT_CONNECT);
00520     tr_info("Connect to cellular network (timeout %d ms)", TIMEOUT_CONNECT);
00521     if (_network->connect() == NSAPI_ERROR_OK ) {
00522         _cellularDevice->set_timeout(TIMEOUT_NETWORK);
00523         tr_debug("Connected to cellular network, set at timeout (timeout %d ms)", TIMEOUT_NETWORK);
00524         // when using modems stack connect is synchronous
00525         _next_state = STATE_CONNECTED;
00526     } else {
00527         retry_state_or_fail();
00528     }
00529 }
00530 
00531 void CellularConnectionFSM::state_connected()
00532 {
00533     _cellularDevice->set_timeout(TIMEOUT_NETWORK);
00534     tr_debug("Cellular ready! (timeout %d ms)", TIMEOUT_NETWORK);
00535     if (_status_callback) {
00536         _status_callback(_state, _next_state);
00537     }
00538 }
00539 
00540 void CellularConnectionFSM::event()
00541 {
00542     _event_timeout = -1;
00543     switch (_state) {
00544         case STATE_INIT:
00545             state_init();
00546             break;
00547         case STATE_POWER_ON:
00548             state_power_on();
00549             break;
00550         case STATE_DEVICE_READY:
00551             state_device_ready();
00552             break;
00553         case STATE_SIM_PIN:
00554             state_sim_pin();
00555             break;
00556         case STATE_REGISTERING_NETWORK:
00557             state_registering();
00558             break;
00559         case STATE_MANUAL_REGISTERING_NETWORK:
00560             state_manual_registering_network();
00561             break;
00562         case STATE_ATTACHING_NETWORK:
00563             state_attaching();
00564             break;
00565         case STATE_ACTIVATING_PDP_CONTEXT:
00566             state_activating_pdp_context();
00567             break;
00568         case STATE_CONNECTING_NETWORK:
00569             state_connect_to_network();
00570             break;
00571         case STATE_CONNECTED:
00572             state_connected();
00573             break;
00574         default:
00575             MBED_ASSERT(0);
00576             break;
00577     }
00578 
00579     if (_next_state != _state || _event_timeout >= 0) {
00580         if (_next_state != _state) { // state exit condition
00581             tr_info("Cellular state from %s to %s", get_state_string((CellularConnectionFSM::CellularState)_state),
00582                     get_state_string((CellularConnectionFSM::CellularState)_next_state));
00583             if (_status_callback) {
00584                 if (!_status_callback(_state, _next_state)) {
00585                     return;
00586                 }
00587             }
00588         } else {
00589             tr_info("Cellular event in %d seconds", _event_timeout);
00590         }
00591         _state = _next_state;
00592         if (_event_timeout == -1) {
00593             _event_timeout = 0;
00594         }
00595         _event_id = _queue.call_in(_event_timeout * 1000, callback(this, &CellularConnectionFSM::event));
00596         if (!_event_id) {
00597             report_failure("Cellular event failure!");
00598             return;
00599         }
00600     }
00601 }
00602 
00603 nsapi_error_t CellularConnectionFSM::start_dispatch()
00604 {
00605     MBED_ASSERT(!_queue_thread);
00606 
00607     _queue_thread = new rtos::Thread(osPriorityNormal, 2048);
00608     if (!_queue_thread) {
00609         stop();
00610         return NSAPI_ERROR_NO_MEMORY ;
00611     }
00612     if (_queue_thread->start(callback(&_queue, &events::EventQueue::dispatch_forever)) != osOK) {
00613         stop();
00614         return NSAPI_ERROR_NO_MEMORY ;
00615     }
00616 
00617     return NSAPI_ERROR_OK ;
00618 }
00619 
00620 void CellularConnectionFSM::set_serial(UARTSerial *serial)
00621 {
00622     _serial = serial;
00623 }
00624 
00625 void CellularConnectionFSM::set_callback(mbed::Callback<bool(int, int)> status_callback)
00626 {
00627     _status_callback = status_callback;
00628 }
00629 
00630 void CellularConnectionFSM::attach(mbed::Callback<void(nsapi_event_t, intptr_t)> status_cb)
00631 {
00632     MBED_ASSERT(_network);
00633     _event_status_cb = status_cb;
00634     if (status_cb) {
00635         _network->attach(callback(this, &CellularConnectionFSM::network_callback));
00636     } else {
00637         _network->attach(NULL);
00638     }
00639 }
00640 
00641 void CellularConnectionFSM::network_callback(nsapi_event_t ev, intptr_t ptr)
00642 {
00643     tr_info("FSM: network_callback called with event: %d, intptr: %d, _state: %s", ev, ptr, get_state_string(_state));
00644     if ((cellular_connection_status_t)ev == CellularRegistrationStatusChanged &&
00645             (_state == STATE_REGISTERING_NETWORK || _state == STATE_MANUAL_REGISTERING_NETWORK)) {
00646         // expect packet data so only these states are valid
00647         if (ptr == CellularNetwork::RegisteredHomeNetwork || ptr == CellularNetwork::RegisteredRoaming) {
00648             if (_plmn) {
00649                 if (is_registered_to_plmn()) {
00650                     if (!_plmn_network_found) {
00651                         _plmn_network_found = true;
00652                         _queue.cancel(_event_id);
00653                         continue_from_state(STATE_ATTACHING_NETWORK);
00654                     }
00655                 }
00656             } else {
00657                 _queue.cancel(_event_id);
00658                 continue_from_state(STATE_ATTACHING_NETWORK);
00659             }
00660         }
00661     }
00662 
00663     if (_event_status_cb) {
00664         _event_status_cb(ev, ptr);
00665     }
00666 }
00667 
00668 void CellularConnectionFSM::ready_urc_cb()
00669 {
00670     tr_debug("Device ready URC func called");
00671     if (_state == STATE_DEVICE_READY && _power->set_at_mode() == NSAPI_ERROR_OK ) {
00672         tr_debug("State was STATE_DEVICE_READY and at mode ready, cancel state and move to next");
00673         if (device_ready()) {
00674             _queue.cancel(_event_id);
00675             continue_from_state(STATE_SIM_PIN);
00676         }
00677     }
00678 }
00679 
00680 events::EventQueue *CellularConnectionFSM::get_queue()
00681 {
00682     return &_queue;
00683 }
00684 
00685 CellularNetwork *CellularConnectionFSM::get_network()
00686 {
00687     return _network;
00688 }
00689 
00690 CellularDevice *CellularConnectionFSM::get_device()
00691 {
00692     return _cellularDevice;
00693 }
00694 
00695 CellularSIM *CellularConnectionFSM::get_sim()
00696 {
00697     return _sim;
00698 }
00699 
00700 NetworkStack *CellularConnectionFSM::get_stack()
00701 {
00702     return _cellularDevice->get_stack();
00703 }
00704 
00705 void CellularConnectionFSM::set_retry_timeout_array(uint16_t timeout[], int array_len)
00706 {
00707     _retry_array_length = array_len > MAX_RETRY_ARRAY_SIZE ? MAX_RETRY_ARRAY_SIZE : array_len;
00708 
00709     for (int i = 0; i < _retry_array_length; i++) {
00710         _retry_timeout_array[i] = timeout[i];
00711     }
00712 }
00713 
00714 } // namespace
00715 
00716 #endif // CELLULAR_DEVICE