@@ -467,15 +467,26 @@ nsapi_error_t AT_CellularContext::do_activate_context()
467467
468468nsapi_error_t AT_CellularContext::activate_ip_context ()
469469{
470- return activate_context ();
470+ return find_and_activate_context ();
471471}
472472
473473nsapi_error_t AT_CellularContext::activate_non_ip_context ()
474474{
475- return activate_context ();
475+ return find_and_activate_context ();
476476}
477477
478- nsapi_error_t AT_CellularContext::activate_context ()
478+ void AT_CellularContext::activate_context ()
479+ {
480+ tr_info (" Activate PDP context %d" , _cid);
481+ _at.cmd_start (" AT+CGACT=1," );
482+ _at.write_int (_cid);
483+ _at.cmd_stop_read_resp ();
484+ if (_at.get_last_error () == NSAPI_ERROR_OK) {
485+ _is_context_activated = true ;
486+ }
487+ }
488+
489+ nsapi_error_t AT_CellularContext::find_and_activate_context ()
479490{
480491 _at.lock ();
481492
@@ -510,26 +521,11 @@ nsapi_error_t AT_CellularContext::activate_context()
510521
511522 _is_context_active = false ;
512523 _is_context_activated = false ;
513- _at.cmd_start (" AT+CGACT?" );
514- _at.cmd_stop ();
515- _at.resp_start (" +CGACT:" );
516- while (_at.info_resp ()) {
517- int context_id = _at.read_int ();
518- int context_activation_state = _at.read_int ();
519- if (context_id == _cid && context_activation_state == 1 ) {
520- _is_context_active = true ;
521- }
522- }
523- _at.resp_stop ();
524+
525+ _is_context_active = _nw->is_active_context (NULL , _cid);
524526
525527 if (!_is_context_active) {
526- tr_info (" Activate PDP context %d" , _cid);
527- _at.cmd_start (" AT+CGACT=1," );
528- _at.write_int (_cid);
529- _at.cmd_stop_read_resp ();
530- if (_at.get_last_error () == NSAPI_ERROR_OK) {
531- _is_context_activated = true ;
532- }
528+ activate_context ();
533529 }
534530
535531 err = (_at.get_last_error () == NSAPI_ERROR_OK) ? NSAPI_ERROR_OK : NSAPI_ERROR_NO_CONNECTION;
@@ -704,34 +700,27 @@ nsapi_error_t AT_CellularContext::disconnect()
704700
705701void AT_CellularContext::deactivate_ip_context ()
706702{
707- deactivate_context ();
703+ check_and_deactivate_context ();
708704}
709705
710706void AT_CellularContext::deactivate_non_ip_context ()
711707{
712- deactivate_context ();
708+ check_and_deactivate_context ();
713709}
714710
715711void AT_CellularContext::deactivate_context ()
712+ {
713+ _at.cmd_start (" AT+CGACT=0," );
714+ _at.write_int (_cid);
715+ _at.cmd_stop_read_resp ();
716+ }
717+
718+ void AT_CellularContext::check_and_deactivate_context ()
716719{
717720 // CGACT and CGATT commands might take up to 3 minutes to respond.
718721 _at.set_at_timeout (180 * 1000 );
719- _is_context_active = false ;
720- size_t active_contexts_count = 0 ;
721- _at.cmd_start (" AT+CGACT?" );
722- _at.cmd_stop ();
723- _at.resp_start (" +CGACT:" );
724- while (_at.info_resp ()) {
725- int context_id = _at.read_int ();
726- int context_activation_state = _at.read_int ();
727- if (context_activation_state == 1 ) {
728- active_contexts_count++;
729- if (context_id == _cid) {
730- _is_context_active = true ;
731- }
732- }
733- }
734- _at.resp_stop ();
722+ int active_contexts_count = 0 ;
723+ _is_context_active = _nw->is_active_context (&active_contexts_count, _cid);
735724
736725 CellularNetwork::RadioAccessTechnology rat = CellularNetwork::RAT_GSM;
737726 // always return NSAPI_ERROR_OK
@@ -742,9 +731,7 @@ void AT_CellularContext::deactivate_context()
742731 // For EPS, if an attempt is made to disconnect the last PDN connection, then the MT responds with ERROR
743732 if (_is_context_active && (rat < CellularNetwork::RAT_E_UTRAN || active_contexts_count > 1 )) {
744733 _at.clear_error ();
745- _at.cmd_start (" AT+CGACT=0," );
746- _at.write_int (_cid);
747- _at.cmd_stop_read_resp ();
734+ deactivate_context ();
748735 }
749736
750737 if (_new_context_set) {
0 commit comments