@@ -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
@@ -511,26 +522,11 @@ nsapi_error_t AT_CellularContext::activate_context()
511522
512523 _is_context_active = false ;
513524 _is_context_activated = false ;
514- _at.cmd_start (" AT+CGACT?" );
515- _at.cmd_stop ();
516- _at.resp_start (" +CGACT:" );
517- while (_at.info_resp ()) {
518- int context_id = _at.read_int ();
519- int context_activation_state = _at.read_int ();
520- if (context_id == _cid && context_activation_state == 1 ) {
521- _is_context_active = true ;
522- }
523- }
524- _at.resp_stop ();
525+
526+ _is_context_active = _nw->is_active_context (NULL , _cid);
525527
526528 if (!_is_context_active) {
527- tr_info (" Activate PDP context %d" , _cid);
528- _at.cmd_start (" AT+CGACT=1," );
529- _at.write_int (_cid);
530- _at.cmd_stop_read_resp ();
531- if (_at.get_last_error () == NSAPI_ERROR_OK) {
532- _is_context_activated = true ;
533- }
529+ activate_context ();
534530 }
535531
536532 err = (_at.get_last_error () == NSAPI_ERROR_OK) ? NSAPI_ERROR_OK : NSAPI_ERROR_NO_CONNECTION;
@@ -692,34 +688,27 @@ nsapi_error_t AT_CellularContext::disconnect()
692688
693689void AT_CellularContext::deactivate_ip_context ()
694690{
695- deactivate_context ();
691+ check_and_deactivate_context ();
696692}
697693
698694void AT_CellularContext::deactivate_non_ip_context ()
699695{
700- deactivate_context ();
696+ check_and_deactivate_context ();
701697}
702698
703699void AT_CellularContext::deactivate_context ()
700+ {
701+ _at.cmd_start (" AT+CGACT=0," );
702+ _at.write_int (_cid);
703+ _at.cmd_stop_read_resp ();
704+ }
705+
706+ void AT_CellularContext::check_and_deactivate_context ()
704707{
705708 // CGACT and CGATT commands might take up to 3 minutes to respond.
706709 _at.set_at_timeout (180 * 1000 );
707- _is_context_active = false ;
708- size_t active_contexts_count = 0 ;
709- _at.cmd_start (" AT+CGACT?" );
710- _at.cmd_stop ();
711- _at.resp_start (" +CGACT:" );
712- while (_at.info_resp ()) {
713- int context_id = _at.read_int ();
714- int context_activation_state = _at.read_int ();
715- if (context_activation_state == 1 ) {
716- active_contexts_count++;
717- if (context_id == _cid) {
718- _is_context_active = true ;
719- }
720- }
721- }
722- _at.resp_stop ();
710+ int active_contexts_count = 0 ;
711+ _is_context_active = _nw->is_active_context (&active_contexts_count, _cid);
723712
724713 CellularNetwork::RadioAccessTechnology rat = CellularNetwork::RAT_GSM;
725714 // always return NSAPI_ERROR_OK
@@ -730,9 +719,7 @@ void AT_CellularContext::deactivate_context()
730719 // For EPS, if an attempt is made to disconnect the last PDN connection, then the MT responds with ERROR
731720 if (_is_context_active && (rat < CellularNetwork::RAT_E_UTRAN || active_contexts_count > 1 )) {
732721 _at.clear_error ();
733- _at.cmd_start (" AT+CGACT=0," );
734- _at.write_int (_cid);
735- _at.cmd_stop_read_resp ();
722+ deactivate_context ();
736723 }
737724
738725 if (_new_context_set) {
0 commit comments