Skip to content

Commit 779fb6d

Browse files
author
Teppo Järvelin
committed
Cellular: AT_CellularContext disconnect in non-blocking mode
Disconnect was supporting only blocking mode.
1 parent b2abfc3 commit 779fb6d

File tree

3 files changed

+27
-5
lines changed

3 files changed

+27
-5
lines changed

UNITTESTS/features/cellular/framework/AT/at_cellularcontext/at_cellularcontexttest.cpp

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -603,15 +603,22 @@ TEST_F(TestAT_CellularContext, connect_disconnect_async)
603603
ASSERT_EQ(network_cb_count, 5);
604604
ASSERT_EQ(ctx1.connect(), NSAPI_ERROR_IS_CONNECTED);
605605
EXPECT_TRUE(ctx1.is_connected() == true);
606+
ASSERT_EQ(ctx1.disconnect(), NSAPI_ERROR_NO_MEMORY);
607+
EXPECT_TRUE(ctx1.is_connected() == true);
608+
609+
struct equeue_event ptr;
610+
equeue_stub.void_ptr = &ptr;
611+
equeue_stub.call_cb_immediately = true;
606612
ASSERT_EQ(ctx1.disconnect(), NSAPI_ERROR_OK);
607613
EXPECT_TRUE(ctx1.is_connected() == false);
608614

609615
// sdet CellularDevice_stub::connect_counter = 0 so device is already attached and will return NSAPI_ERROR_ALREADY to context when calling connect
616+
equeue_stub.void_ptr = &ptr;
617+
equeue_stub.call_cb_immediately = false;
610618
CellularDevice_stub::connect_counter = 0;
611619
// queue can't allocate so return NSAPI_ERROR_NO_MEMORY
612620
ASSERT_EQ(ctx1.connect(), NSAPI_ERROR_NO_MEMORY);
613621

614-
struct equeue_event ptr;
615622
equeue_stub.void_ptr = &ptr;
616623
equeue_stub.call_cb_immediately = true;
617624
ASSERT_EQ(ctx1.connect(), NSAPI_ERROR_OK);

features/cellular/framework/AT/AT_CellularContext.cpp

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ AT_CellularContext::~AT_CellularContext()
5959
{
6060
tr_info("Delete CellularContext with apn: [%s] (%p)", _apn ? _apn : "", this);
6161

62+
_is_blocking = true;
6263
(void)disconnect();
6364

6465
if (_nw) {
@@ -659,15 +660,14 @@ void AT_CellularContext::ppp_disconnected()
659660

660661
#endif //#if NSAPI_PPP_AVAILABLE
661662

662-
nsapi_error_t AT_CellularContext::disconnect()
663+
void AT_CellularContext::do_disconnect()
663664
{
664-
tr_info("CellularContext disconnect()");
665665
if (!_nw || !_is_connected) {
666666
if (_new_context_set) {
667667
delete_current_context();
668668
}
669669
_cid = -1;
670-
return NSAPI_ERROR_NO_CONNECTION;
670+
_cb_data.error = NSAPI_ERROR_NO_CONNECTION;
671671
}
672672

673673
// set false here so callbacks know that we are not connected and so should not send DISCONNECTED
@@ -703,8 +703,22 @@ nsapi_error_t AT_CellularContext::disconnect()
703703
delete_current_context();
704704
}
705705
_cid = -1;
706+
_cb_data.error = _at.unlock_return_error();
707+
}
706708

707-
return _at.unlock_return_error();
709+
nsapi_error_t AT_CellularContext::disconnect()
710+
{
711+
tr_info("CellularContext disconnect()");
712+
if (_is_blocking) {
713+
do_disconnect();
714+
return _cb_data.error;
715+
} else {
716+
int event_id = _device->get_queue()->call_in(0, this, &AT_CellularContext::do_disconnect);
717+
if (event_id == 0) {
718+
return NSAPI_ERROR_NO_MEMORY;
719+
}
720+
return NSAPI_ERROR_OK;
721+
}
708722
}
709723

710724
void AT_CellularContext::deactivate_ip_context()

features/cellular/framework/AT/AT_CellularContext.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,7 @@ class AT_CellularContext : public CellularContext, public AT_CellularBase {
118118
AT_CellularBase::CellularProperty pdp_type_t_to_cellular_property(pdp_type_t pdp_type);
119119
void ciot_opt_cb(mbed::CellularNetwork::CIoT_Supported_Opt ciot_opt);
120120
virtual void do_connect_with_retry();
121+
void do_disconnect();
121122
private:
122123
bool _is_connected;
123124
ContextOperation _current_op;

0 commit comments

Comments
 (0)