@@ -498,7 +498,6 @@ nsapi_error_t AT_CellularContext::activate_context()
498498 if (err != NSAPI_ERROR_OK) {
499499 _at.unlock ();
500500 tr_error (" Failed to activate network context! (%d)" , err);
501- call_network_cb (NSAPI_STATUS_DISCONNECTED);
502501 return err;
503502 }
504503
@@ -551,16 +550,18 @@ void AT_CellularContext::do_connect()
551550{
552551 if (!_is_context_active) {
553552 _cb_data.error = do_activate_context ();
553+ } else {
554+ _cb_data.error = NSAPI_ERROR_OK;
555+ }
556+
554557#if !NSAPI_PPP_AVAILABLE
555- // in PPP mode we did not activate any context, just searched the correct _cid
556- if (_status_cb) {
557- _status_cb ((nsapi_event_t )CellularActivatePDPContext, (intptr_t )&_cb_data);
558- }
559- #endif // !NSAPI_PPP_AVAILABLE
558+ // in PPP mode we did not activate any context, just searched the correct _cid
559+ if (_status_cb) {
560+ _status_cb ((nsapi_event_t )CellularActivatePDPContext, (intptr_t )&_cb_data);
560561 }
562+ #endif // !NSAPI_PPP_AVAILABLE
561563
562564 if (_cb_data.error != NSAPI_ERROR_OK) {
563- call_network_cb (NSAPI_STATUS_DISCONNECTED);
564565 _is_connected = false ;
565566 return ;
566567 }
@@ -630,16 +631,23 @@ void AT_CellularContext::ppp_status_cb(nsapi_event_t ev, intptr_t ptr)
630631 tr_debug (" ppp_status_cb: event %d, ptr %d" , ev, ptr);
631632 if (ev == NSAPI_EVENT_CONNECTION_STATUS_CHANGE && ptr == NSAPI_STATUS_GLOBAL_UP) {
632633 _is_connected = true ;
633- } else if (ev == NSAPI_EVENT_CONNECTION_STATUS_CHANGE && ptr == NSAPI_STATUS_DISCONNECTED) {
634- ppp_disconnected ();
635634 } else {
636635 _is_connected = false ;
637636 }
638637
639638 _connect_status = (nsapi_connection_status_t )ptr;
640639
640+ // catch all NSAPI_STATUS_DISCONNECTED events but send to device only when we did not ask for disconnect.
641+ if (ev == NSAPI_EVENT_CONNECTION_STATUS_CHANGE && ptr == NSAPI_STATUS_DISCONNECTED) {
642+ if (_is_connected) {
643+ ppp_disconnected ();
644+ _device->cellular_callback (ev, ptr, this );
645+ }
646+ return ;
647+ }
648+
641649 // call device's callback, it will broadcast this to here (cellular_callback)
642- _device->cellular_callback (ev, ptr);
650+ _device->cellular_callback (ev, ptr, this );
643651}
644652
645653void AT_CellularContext::ppp_disconnected ()
@@ -660,10 +668,13 @@ void AT_CellularContext::ppp_disconnected()
660668
661669nsapi_error_t AT_CellularContext::disconnect ()
662670{
663- tr_info (" CellularContext disconnect" );
671+ tr_info (" CellularContext disconnect() " );
664672 if (!_nw || !_is_connected) {
665673 return NSAPI_ERROR_NO_CONNECTION;
666674 }
675+
676+ // set false here so callbacks know that we are not connected and so should not send DISCONNECTED
677+ _is_connected = false ;
667678#if NSAPI_PPP_AVAILABLE
668679 nsapi_error_t err = nsapi_ppp_disconnect (_at.get_file_handle ());
669680 if (err != NSAPI_ERROR_OK) {
@@ -681,11 +692,12 @@ nsapi_error_t AT_CellularContext::disconnect()
681692 } else {
682693 deactivate_ip_context ();
683694 }
684- } else {
685- call_network_cb (NSAPI_STATUS_DISCONNECTED);
686695 }
696+ _is_context_active = false ;
697+ _connect_status = NSAPI_STATUS_DISCONNECTED;
687698
688- _is_connected = false ;
699+ // call device's callback, it will broadcast this to here (cellular_callback)
700+ _device->cellular_callback (NSAPI_EVENT_CONNECTION_STATUS_CHANGE, NSAPI_STATUS_DISCONNECTED, this );
689701
690702 return _at.unlock_return_error ();
691703}
@@ -928,27 +940,32 @@ void AT_CellularContext::cellular_callback(nsapi_event_t ev, intptr_t ptr)
928940 if (_is_blocking) {
929941 if (data->error != NSAPI_ERROR_OK) {
930942 // operation failed, release semaphore
943+ _current_op = OP_INVALID;
931944 _semaphore.release ();
932945 } else {
933946 if ((st == CellularDeviceReady && _current_op == OP_DEVICE_READY) ||
934947 (st == CellularSIMStatusChanged && _current_op == OP_SIM_READY &&
935948 data->status_data == CellularDevice::SimStateReady)) {
936949 // target reached, release semaphore
950+ _current_op = OP_INVALID;
937951 _semaphore.release ();
938952 } else if (st == CellularRegistrationStatusChanged && (data->status_data == CellularNetwork::RegisteredHomeNetwork ||
939953 data->status_data == CellularNetwork::RegisteredRoaming || data->status_data == CellularNetwork::AlreadyRegistered) && _current_op == OP_REGISTER) {
940954 // target reached, release semaphore
955+ _current_op = OP_INVALID;
941956 _semaphore.release ();
942957 } else if (st == CellularAttachNetwork && (_current_op == OP_ATTACH || _current_op == OP_CONNECT) &&
943958 data->status_data == CellularNetwork::Attached) {
944959 // target reached, release semaphore
960+ _current_op = OP_INVALID;
945961 _semaphore.release ();
946962 }
947963 }
948964 } else {
949965 // non blocking
950966 if (st == CellularAttachNetwork && _current_op == OP_CONNECT && data->error == NSAPI_ERROR_OK &&
951967 data->status_data == CellularNetwork::Attached) {
968+ _current_op = OP_INVALID;
952969 // forward to application
953970 if (_status_cb) {
954971 _status_cb (ev, ptr);
@@ -963,14 +980,18 @@ void AT_CellularContext::cellular_callback(nsapi_event_t ev, intptr_t ptr)
963980 if (ev == NSAPI_EVENT_CONNECTION_STATUS_CHANGE && ptr == NSAPI_STATUS_GLOBAL_UP) {
964981 tr_info (" CellularContext IP %s" , get_ip_address ());
965982 _cb_data.error = NSAPI_ERROR_OK;
966- _semaphore.release ();
967983 } else if (ev == NSAPI_EVENT_CONNECTION_STATUS_CHANGE && ptr == NSAPI_STATUS_DISCONNECTED) {
968984 tr_info (" PPP disconnected" );
969985 _cb_data.error = NSAPI_ERROR_NO_CONNECTION;
970- _semaphore.release ();
971986 }
972987 }
973- #endif
988+ #else
989+ #if MBED_CONF_MBED_TRACE_ENABLE
990+ if (ev == NSAPI_EVENT_CONNECTION_STATUS_CHANGE && ptr == NSAPI_STATUS_DISCONNECTED) {
991+ tr_info (" cb: CellularContext disconnected" );
992+ }
993+ #endif // MBED_CONF_MBED_TRACE_ENABLE
994+ #endif // NSAPI_PPP_AVAILABLE
974995 }
975996
976997 // forward to application
@@ -1044,8 +1065,7 @@ void AT_CellularContext::ciot_opt_cb(mbed::CellularNetwork::CIoT_Supported_Opt
10441065
10451066void AT_CellularContext::set_disconnect ()
10461067{
1068+ tr_debug (" AT_CellularContext::set_disconnect()" );
10471069 _is_connected = false ;
1048- cell_callback_data_t data;
1049- data.error = NSAPI_STATUS_DISCONNECTED;
1050- _device->cellular_callback (NSAPI_EVENT_CONNECTION_STATUS_CHANGE, (intptr_t )&data);
1070+ _device->cellular_callback (NSAPI_EVENT_CONNECTION_STATUS_CHANGE, NSAPI_STATUS_DISCONNECTED, this );
10511071}
0 commit comments