mirror of https://github.com/ARMmbed/mbed-os.git
				
				
				
			Merge pull request #10656 from jarvte/fix_cellularctx_destr
Cellular: AT_CellularContext disconnect in non-blocking modepull/10739/head
						commit
						523ad23a30
					
				| 
						 | 
					@ -603,15 +603,22 @@ TEST_F(TestAT_CellularContext, connect_disconnect_async)
 | 
				
			||||||
    ASSERT_EQ(network_cb_count, 5);
 | 
					    ASSERT_EQ(network_cb_count, 5);
 | 
				
			||||||
    ASSERT_EQ(ctx1.connect(), NSAPI_ERROR_IS_CONNECTED);
 | 
					    ASSERT_EQ(ctx1.connect(), NSAPI_ERROR_IS_CONNECTED);
 | 
				
			||||||
    EXPECT_TRUE(ctx1.is_connected() == true);
 | 
					    EXPECT_TRUE(ctx1.is_connected() == true);
 | 
				
			||||||
 | 
					    ASSERT_EQ(ctx1.disconnect(), NSAPI_ERROR_NO_MEMORY);
 | 
				
			||||||
 | 
					    EXPECT_TRUE(ctx1.is_connected() == true);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    struct equeue_event ptr;
 | 
				
			||||||
 | 
					    equeue_stub.void_ptr = &ptr;
 | 
				
			||||||
 | 
					    equeue_stub.call_cb_immediately = true;
 | 
				
			||||||
    ASSERT_EQ(ctx1.disconnect(), NSAPI_ERROR_OK);
 | 
					    ASSERT_EQ(ctx1.disconnect(), NSAPI_ERROR_OK);
 | 
				
			||||||
    EXPECT_TRUE(ctx1.is_connected() == false);
 | 
					    EXPECT_TRUE(ctx1.is_connected() == false);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    // sdet CellularDevice_stub::connect_counter = 0 so device is already attached and will return NSAPI_ERROR_ALREADY to context when calling connect
 | 
					    // sdet CellularDevice_stub::connect_counter = 0 so device is already attached and will return NSAPI_ERROR_ALREADY to context when calling connect
 | 
				
			||||||
 | 
					    equeue_stub.void_ptr = &ptr;
 | 
				
			||||||
 | 
					    equeue_stub.call_cb_immediately = false;
 | 
				
			||||||
    CellularDevice_stub::connect_counter = 0;
 | 
					    CellularDevice_stub::connect_counter = 0;
 | 
				
			||||||
    // queue can't allocate so return NSAPI_ERROR_NO_MEMORY
 | 
					    // queue can't allocate so return NSAPI_ERROR_NO_MEMORY
 | 
				
			||||||
    ASSERT_EQ(ctx1.connect(), NSAPI_ERROR_NO_MEMORY);
 | 
					    ASSERT_EQ(ctx1.connect(), NSAPI_ERROR_NO_MEMORY);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    struct equeue_event ptr;
 | 
					 | 
				
			||||||
    equeue_stub.void_ptr = &ptr;
 | 
					    equeue_stub.void_ptr = &ptr;
 | 
				
			||||||
    equeue_stub.call_cb_immediately = true;
 | 
					    equeue_stub.call_cb_immediately = true;
 | 
				
			||||||
    ASSERT_EQ(ctx1.connect(), NSAPI_ERROR_OK);
 | 
					    ASSERT_EQ(ctx1.connect(), NSAPI_ERROR_OK);
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -59,6 +59,7 @@ AT_CellularContext::~AT_CellularContext()
 | 
				
			||||||
{
 | 
					{
 | 
				
			||||||
    tr_info("Delete CellularContext with apn: [%s] (%p)", _apn ? _apn : "", this);
 | 
					    tr_info("Delete CellularContext with apn: [%s] (%p)", _apn ? _apn : "", this);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    _is_blocking = true;
 | 
				
			||||||
    (void)disconnect();
 | 
					    (void)disconnect();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    if (_nw) {
 | 
					    if (_nw) {
 | 
				
			||||||
| 
						 | 
					@ -659,15 +660,14 @@ void AT_CellularContext::ppp_disconnected()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
#endif //#if NSAPI_PPP_AVAILABLE
 | 
					#endif //#if NSAPI_PPP_AVAILABLE
 | 
				
			||||||
 | 
					
 | 
				
			||||||
nsapi_error_t AT_CellularContext::disconnect()
 | 
					void AT_CellularContext::do_disconnect()
 | 
				
			||||||
{
 | 
					{
 | 
				
			||||||
    tr_info("CellularContext disconnect()");
 | 
					 | 
				
			||||||
    if (!_nw || !_is_connected) {
 | 
					    if (!_nw || !_is_connected) {
 | 
				
			||||||
        if (_new_context_set) {
 | 
					        if (_new_context_set) {
 | 
				
			||||||
            delete_current_context();
 | 
					            delete_current_context();
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
        _cid = -1;
 | 
					        _cid = -1;
 | 
				
			||||||
        return NSAPI_ERROR_NO_CONNECTION;
 | 
					        _cb_data.error = NSAPI_ERROR_NO_CONNECTION;
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    // set false here so callbacks know that we are not connected and so should not send DISCONNECTED
 | 
					    // 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()
 | 
				
			||||||
        delete_current_context();
 | 
					        delete_current_context();
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
    _cid = -1;
 | 
					    _cid = -1;
 | 
				
			||||||
 | 
					    _cb_data.error = _at.unlock_return_error();
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    return _at.unlock_return_error();
 | 
					nsapi_error_t AT_CellularContext::disconnect()
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
					    tr_info("CellularContext disconnect()");
 | 
				
			||||||
 | 
					    if (_is_blocking) {
 | 
				
			||||||
 | 
					        do_disconnect();
 | 
				
			||||||
 | 
					        return _cb_data.error;
 | 
				
			||||||
 | 
					    } else {
 | 
				
			||||||
 | 
					        int event_id = _device->get_queue()->call_in(0, this, &AT_CellularContext::do_disconnect);
 | 
				
			||||||
 | 
					        if (event_id == 0) {
 | 
				
			||||||
 | 
					            return NSAPI_ERROR_NO_MEMORY;
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					        return NSAPI_ERROR_OK;
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
void AT_CellularContext::deactivate_ip_context()
 | 
					void AT_CellularContext::deactivate_ip_context()
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -118,6 +118,7 @@ private:
 | 
				
			||||||
    nsapi_error_t check_operation(nsapi_error_t err, ContextOperation op);
 | 
					    nsapi_error_t check_operation(nsapi_error_t err, ContextOperation op);
 | 
				
			||||||
    void ciot_opt_cb(mbed::CellularNetwork::CIoT_Supported_Opt ciot_opt);
 | 
					    void ciot_opt_cb(mbed::CellularNetwork::CIoT_Supported_Opt ciot_opt);
 | 
				
			||||||
    virtual void do_connect_with_retry();
 | 
					    virtual void do_connect_with_retry();
 | 
				
			||||||
 | 
					    void do_disconnect();
 | 
				
			||||||
private:
 | 
					private:
 | 
				
			||||||
    bool _is_connected;
 | 
					    bool _is_connected;
 | 
				
			||||||
    ContextOperation  _current_op;
 | 
					    ContextOperation  _current_op;
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
		Reference in New Issue