@@ -498,7 +498,6 @@ nsapi_error_t AT_CellularContext::activate_context()
498
498
if (err != NSAPI_ERROR_OK) {
499
499
_at.unlock ();
500
500
tr_error (" Failed to activate network context! (%d)" , err);
501
- call_network_cb (NSAPI_STATUS_DISCONNECTED);
502
501
return err;
503
502
}
504
503
@@ -551,16 +550,18 @@ void AT_CellularContext::do_connect()
551
550
{
552
551
if (!_is_context_active) {
553
552
_cb_data.error = do_activate_context ();
553
+ } else {
554
+ _cb_data.error = NSAPI_ERROR_OK;
555
+ }
556
+
554
557
#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);
560
561
}
562
+ #endif // !NSAPI_PPP_AVAILABLE
561
563
562
564
if (_cb_data.error != NSAPI_ERROR_OK) {
563
- call_network_cb (NSAPI_STATUS_DISCONNECTED);
564
565
_is_connected = false ;
565
566
return ;
566
567
}
@@ -630,16 +631,23 @@ void AT_CellularContext::ppp_status_cb(nsapi_event_t ev, intptr_t ptr)
630
631
tr_debug (" ppp_status_cb: event %d, ptr %d" , ev, ptr);
631
632
if (ev == NSAPI_EVENT_CONNECTION_STATUS_CHANGE && ptr == NSAPI_STATUS_GLOBAL_UP) {
632
633
_is_connected = true ;
633
- } else if (ev == NSAPI_EVENT_CONNECTION_STATUS_CHANGE && ptr == NSAPI_STATUS_DISCONNECTED) {
634
- ppp_disconnected ();
635
634
} else {
636
635
_is_connected = false ;
637
636
}
638
637
639
638
_connect_status = (nsapi_connection_status_t )ptr;
640
639
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
+
641
649
// 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 );
643
651
}
644
652
645
653
void AT_CellularContext::ppp_disconnected ()
@@ -660,10 +668,13 @@ void AT_CellularContext::ppp_disconnected()
660
668
661
669
nsapi_error_t AT_CellularContext::disconnect ()
662
670
{
663
- tr_info (" CellularContext disconnect" );
671
+ tr_info (" CellularContext disconnect() " );
664
672
if (!_nw || !_is_connected) {
665
673
return NSAPI_ERROR_NO_CONNECTION;
666
674
}
675
+
676
+ // set false here so callbacks know that we are not connected and so should not send DISCONNECTED
677
+ _is_connected = false ;
667
678
#if NSAPI_PPP_AVAILABLE
668
679
nsapi_error_t err = nsapi_ppp_disconnect (_at.get_file_handle ());
669
680
if (err != NSAPI_ERROR_OK) {
@@ -681,11 +692,12 @@ nsapi_error_t AT_CellularContext::disconnect()
681
692
} else {
682
693
deactivate_ip_context ();
683
694
}
684
- } else {
685
- call_network_cb (NSAPI_STATUS_DISCONNECTED);
686
695
}
696
+ _is_context_active = false ;
697
+ _connect_status = NSAPI_STATUS_DISCONNECTED;
687
698
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 );
689
701
690
702
return _at.unlock_return_error ();
691
703
}
@@ -928,27 +940,32 @@ void AT_CellularContext::cellular_callback(nsapi_event_t ev, intptr_t ptr)
928
940
if (_is_blocking) {
929
941
if (data->error != NSAPI_ERROR_OK) {
930
942
// operation failed, release semaphore
943
+ _current_op = OP_INVALID;
931
944
_semaphore.release ();
932
945
} else {
933
946
if ((st == CellularDeviceReady && _current_op == OP_DEVICE_READY) ||
934
947
(st == CellularSIMStatusChanged && _current_op == OP_SIM_READY &&
935
948
data->status_data == CellularDevice::SimStateReady)) {
936
949
// target reached, release semaphore
950
+ _current_op = OP_INVALID;
937
951
_semaphore.release ();
938
952
} else if (st == CellularRegistrationStatusChanged && (data->status_data == CellularNetwork::RegisteredHomeNetwork ||
939
953
data->status_data == CellularNetwork::RegisteredRoaming || data->status_data == CellularNetwork::AlreadyRegistered) && _current_op == OP_REGISTER) {
940
954
// target reached, release semaphore
955
+ _current_op = OP_INVALID;
941
956
_semaphore.release ();
942
957
} else if (st == CellularAttachNetwork && (_current_op == OP_ATTACH || _current_op == OP_CONNECT) &&
943
958
data->status_data == CellularNetwork::Attached) {
944
959
// target reached, release semaphore
960
+ _current_op = OP_INVALID;
945
961
_semaphore.release ();
946
962
}
947
963
}
948
964
} else {
949
965
// non blocking
950
966
if (st == CellularAttachNetwork && _current_op == OP_CONNECT && data->error == NSAPI_ERROR_OK &&
951
967
data->status_data == CellularNetwork::Attached) {
968
+ _current_op = OP_INVALID;
952
969
// forward to application
953
970
if (_status_cb) {
954
971
_status_cb (ev, ptr);
@@ -963,14 +980,18 @@ void AT_CellularContext::cellular_callback(nsapi_event_t ev, intptr_t ptr)
963
980
if (ev == NSAPI_EVENT_CONNECTION_STATUS_CHANGE && ptr == NSAPI_STATUS_GLOBAL_UP) {
964
981
tr_info (" CellularContext IP %s" , get_ip_address ());
965
982
_cb_data.error = NSAPI_ERROR_OK;
966
- _semaphore.release ();
967
983
} else if (ev == NSAPI_EVENT_CONNECTION_STATUS_CHANGE && ptr == NSAPI_STATUS_DISCONNECTED) {
968
984
tr_info (" PPP disconnected" );
969
985
_cb_data.error = NSAPI_ERROR_NO_CONNECTION;
970
- _semaphore.release ();
971
986
}
972
987
}
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
974
995
}
975
996
976
997
// forward to application
@@ -1044,8 +1065,7 @@ void AT_CellularContext::ciot_opt_cb(mbed::CellularNetwork::CIoT_Supported_Opt
1044
1065
1045
1066
void AT_CellularContext::set_disconnect ()
1046
1067
{
1068
+ tr_debug (" AT_CellularContext::set_disconnect()" );
1047
1069
_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 );
1051
1071
}
0 commit comments