diff --git a/features/cellular/easy_cellular/CellularConnectionFSM.cpp b/features/cellular/easy_cellular/CellularConnectionFSM.cpp index 388fab3cd4..e5f1638b2c 100644 --- a/features/cellular/easy_cellular/CellularConnectionFSM.cpp +++ b/features/cellular/easy_cellular/CellularConnectionFSM.cpp @@ -79,6 +79,10 @@ void CellularConnectionFSM::stop() if (_cellularDevice) { _cellularDevice->close_power(); _cellularDevice->close_network(); + _cellularDevice->close_sim(); + _power = NULL; + _network = NULL; + _sim = NULL; } if (_queue_thread) { _queue_thread->terminate(); @@ -289,7 +293,7 @@ void CellularConnectionFSM::report_failure(const char* msg) const char* CellularConnectionFSM::get_state_string(CellularState state) { - static const char *strings[] = { "Init", "Power", "Device ready", "SIM pin", "Registering network", "Attaching network", "Connecting network", "Connected"}; + static const char *strings[] = { "Init", "Power", "Device ready", "SIM pin", "Registering network", "Attaching network", "Activating PDP Context", "Connecting network", "Connected"}; return strings[state]; } @@ -446,7 +450,7 @@ void CellularConnectionFSM::state_attaching() CellularNetwork::AttachStatus attach_status; if (get_attach_network(attach_status)) { if (attach_status == CellularNetwork::Attached) { - enter_to_state(STATE_CONNECTING_NETWORK); + enter_to_state(STATE_ACTIVATING_PDP_CONTEXT); } else { set_attach_network(); retry_state_or_fail(); @@ -456,6 +460,18 @@ void CellularConnectionFSM::state_attaching() } } +void CellularConnectionFSM::state_activating_pdp_context() +{ + _cellularDevice->set_timeout(TIMEOUT_CONNECT); + tr_info("Activate PDP Context (timeout %d ms)", TIMEOUT_CONNECT); + if (_network->activate_context() == NSAPI_ERROR_OK) { + // when using modems stack connect is synchronous + _next_state = STATE_CONNECTING_NETWORK; + } else { + retry_state_or_fail(); + } +} + void CellularConnectionFSM::state_connect_to_network() { _cellularDevice->set_timeout(TIMEOUT_CONNECT); @@ -501,6 +517,9 @@ void CellularConnectionFSM::event() case STATE_ATTACHING_NETWORK: state_attaching(); break; + case STATE_ACTIVATING_PDP_CONTEXT: + state_activating_pdp_context(); + break; case STATE_CONNECTING_NETWORK: state_connect_to_network(); break; diff --git a/features/cellular/easy_cellular/CellularConnectionFSM.h b/features/cellular/easy_cellular/CellularConnectionFSM.h index 92e62099d3..37bbe98f3b 100644 --- a/features/cellular/easy_cellular/CellularConnectionFSM.h +++ b/features/cellular/easy_cellular/CellularConnectionFSM.h @@ -59,6 +59,7 @@ public: STATE_SIM_PIN, STATE_REGISTERING_NETWORK, STATE_ATTACHING_NETWORK, + STATE_ACTIVATING_PDP_CONTEXT, STATE_CONNECTING_NETWORK, STATE_CONNECTED }; @@ -120,10 +121,10 @@ public: CellularSIM* get_sim(); /** Change cellular connection to the target state - * @param state to continue + * @param state to continue. Default is to connect. * @return see nsapi_error_t, 0 on success */ - nsapi_error_t continue_to_state(CellularState state); + nsapi_error_t continue_to_state(CellularState state = STATE_CONNECTED); /** Set cellular device SIM PIN code * @param sim_pin PIN code @@ -157,6 +158,7 @@ private: void state_sim_pin(); void state_registering(); void state_attaching(); + void state_activating_pdp_context(); void state_connect_to_network(); void state_connected(); void enter_to_state(CellularState state); diff --git a/features/cellular/framework/AT/AT_CellularNetwork.cpp b/features/cellular/framework/AT/AT_CellularNetwork.cpp index 59bddab888..b912c3bb57 100644 --- a/features/cellular/framework/AT/AT_CellularNetwork.cpp +++ b/features/cellular/framework/AT/AT_CellularNetwork.cpp @@ -259,6 +259,8 @@ nsapi_error_t AT_CellularNetwork::activate_context() // If new PDP context was created and failed to activate, delete it if (err != NSAPI_ERROR_OK && _new_context_set) { delete_current_context(); + } else if (err == NSAPI_ERROR_OK) { + _is_context_active = true; } _at.unlock();