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