Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -653,20 +653,6 @@ TEST_F(TestAT_CellularNetwork, test_AT_CellularNetwork_attach)
cn.attach(&network_cb);
}

TEST_F(TestAT_CellularNetwork, test_get_connection_status)
{
EventQueue que;
FileHandle_stub fh1;
ATHandler at(&fh1, que, 0, ",");

AT_CellularNetwork cn(at);

ATHandler_stub::nsapi_error_value = NSAPI_ERROR_OK;
network_cb_count = 0;
cn.attach(&network_cb);
EXPECT_TRUE(NSAPI_STATUS_DISCONNECTED == cn.get_connection_status());
}

TEST_F(TestAT_CellularNetwork, test_AT_CellularNetwork_set_receive_period)
{
EventQueue que;
Expand Down
5 changes: 0 additions & 5 deletions UNITTESTS/stubs/AT_CellularNetwork_stub.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,6 @@ void AT_CellularNetwork::attach(Callback<void(nsapi_event_t, intptr_t)> status_c
{
}

nsapi_connection_status_t AT_CellularNetwork::get_connection_status() const
{
return NSAPI_STATUS_LOCAL_UP;
}

nsapi_error_t AT_CellularNetwork::set_registration_urc(RegistrationType type, bool urc_on)
{
if (AT_CellularNetwork_stub::set_registration_urc_fail_counter) {
Expand Down
2 changes: 1 addition & 1 deletion UNITTESTS/stubs/CellularDevice_stub.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,6 @@ nsapi_error_t CellularDevice::shutdown()
return NSAPI_ERROR_OK;
}

void CellularDevice::cellular_callback(nsapi_event_t ev, intptr_t ptr)
void CellularDevice::cellular_callback(nsapi_event_t ev, intptr_t ptr, CellularContext *ctx)
{
}
2 changes: 2 additions & 0 deletions features/cellular/framework/API/CellularContext.h
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,8 @@ class CellularContext : public CellularInterface {
* The parameters on the callback are the event type and event type dependent reason parameter.
*
* @remark deleting CellularDevice/CellularContext in callback is not allowed.
* @remark Allocating/adding lots of traces not recommended as callback is called mostly from State machines thread which
* is now 2048. You can change to main thread for example via EventQueue.
*
* @param status_cb The callback for status changes.
*/
Expand Down
8 changes: 4 additions & 4 deletions features/cellular/framework/API/CellularDevice.h
Original file line number Diff line number Diff line change
Expand Up @@ -283,8 +283,8 @@ class CellularDevice {
* The parameters on the callback are the event type and event-type dependent reason parameter.
*
* @remark deleting CellularDevice/CellularContext in callback not allowed.
* @remark application should not attach to this function if using CellularContext::attach as it will contain the
* same information.
* @remark Allocating/adding lots of traces not recommended as callback is called mostly from State machines thread which
* is now 2048. You can change to main thread for example via EventQueue.
*
* @param status_cb The callback for status changes.
*/
Expand Down Expand Up @@ -420,8 +420,8 @@ class CellularDevice {
* This method will broadcast to every interested classes:
* CellularContext (might be many) and CellularStateMachine if available.
*/
void cellular_callback(nsapi_event_t ev, intptr_t ptr);

void cellular_callback(nsapi_event_t ev, intptr_t ptr, CellularContext *ctx = NULL);
void stm_callback(nsapi_event_t ev, intptr_t ptr);
int _network_ref_count;
int _sms_ref_count;
int _info_ref_count;
Expand Down
10 changes: 4 additions & 6 deletions features/cellular/framework/API/CellularNetwork.h
Original file line number Diff line number Diff line change
Expand Up @@ -319,16 +319,14 @@ class CellularNetwork {
* on the network. The parameters on the callback are the event type and
* event-type dependent reason parameter.
*
* @remark Application should not call attach if using CellularContext class. Call instead CellularContext::attach
* as CellularDevice is dependent of this attach if CellularContext/CellularDevice is used to get
* device/sim ready, registered, attached, connected.
*
* @param status_cb The callback for status changes
*/
virtual void attach(mbed::Callback<void(nsapi_event_t, intptr_t)> status_cb) = 0;

/** Get the connection status
*
* @return The connection status according to ConnectionStatusType
*/
virtual nsapi_connection_status_t get_connection_status() const = 0;

/** Read operator names
*
* @param op_names on successful return contains linked list of operator names.
Expand Down
13 changes: 9 additions & 4 deletions features/cellular/framework/AT/ATHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1256,22 +1256,27 @@ void ATHandler::debug_print(const char *p, int len)
bool ATHandler::sync(int timeout_ms)
{
tr_debug("AT sync");
lock();
uint32_t timeout = _at_timeout;
_at_timeout = timeout_ms;
// poll for 10 seconds
for (int i = 0; i < 10; i++) {
lock();
set_at_timeout(timeout_ms, false);
// For sync use an AT command that is supported by all modems and likely not used frequently,
// especially a common response like OK could be response to previous request.
clear_error();
_start_time = rtos::Kernel::get_ms_count();
cmd_start("AT+CMEE?");
cmd_stop();
resp_start("+CMEE:");
resp_stop();
restore_at_timeout();
unlock();
if (!_last_err) {
_at_timeout = timeout;
unlock();
return true;
}
}
tr_error("AT sync failed");
_at_timeout = timeout;
unlock();
return false;
}
60 changes: 40 additions & 20 deletions features/cellular/framework/AT/AT_CellularContext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -498,7 +498,6 @@ nsapi_error_t AT_CellularContext::activate_context()
if (err != NSAPI_ERROR_OK) {
_at.unlock();
tr_error("Failed to activate network context! (%d)", err);
call_network_cb(NSAPI_STATUS_DISCONNECTED);
return err;
}

Expand Down Expand Up @@ -551,16 +550,18 @@ void AT_CellularContext::do_connect()
{
if (!_is_context_active) {
_cb_data.error = do_activate_context();
} else {
_cb_data.error = NSAPI_ERROR_OK;
}

#if !NSAPI_PPP_AVAILABLE
// in PPP mode we did not activate any context, just searched the correct _cid
if (_status_cb) {
_status_cb((nsapi_event_t)CellularActivatePDPContext, (intptr_t)&_cb_data);
}
#endif // !NSAPI_PPP_AVAILABLE
// in PPP mode we did not activate any context, just searched the correct _cid
if (_status_cb) {
_status_cb((nsapi_event_t)CellularActivatePDPContext, (intptr_t)&_cb_data);
}
#endif // !NSAPI_PPP_AVAILABLE

if (_cb_data.error != NSAPI_ERROR_OK) {
call_network_cb(NSAPI_STATUS_DISCONNECTED);
_is_connected = false;
return;
}
Expand Down Expand Up @@ -630,16 +631,23 @@ void AT_CellularContext::ppp_status_cb(nsapi_event_t ev, intptr_t ptr)
tr_debug("ppp_status_cb: event %d, ptr %d", ev, ptr);
if (ev == NSAPI_EVENT_CONNECTION_STATUS_CHANGE && ptr == NSAPI_STATUS_GLOBAL_UP) {
_is_connected = true;
} else if (ev == NSAPI_EVENT_CONNECTION_STATUS_CHANGE && ptr == NSAPI_STATUS_DISCONNECTED) {
ppp_disconnected();
} else {
_is_connected = false;
}

_connect_status = (nsapi_connection_status_t)ptr;

// catch all NSAPI_STATUS_DISCONNECTED events but send to device only when we did not ask for disconnect.
if (ev == NSAPI_EVENT_CONNECTION_STATUS_CHANGE && ptr == NSAPI_STATUS_DISCONNECTED) {
if (_is_connected) {
ppp_disconnected();
_device->cellular_callback(ev, ptr, this);
}
return;
}

// call device's callback, it will broadcast this to here (cellular_callback)
_device->cellular_callback(ev, ptr);
_device->cellular_callback(ev, ptr, this);
}

void AT_CellularContext::ppp_disconnected()
Expand All @@ -660,10 +668,13 @@ void AT_CellularContext::ppp_disconnected()

nsapi_error_t AT_CellularContext::disconnect()
{
tr_info("CellularContext disconnect");
tr_info("CellularContext disconnect()");
if (!_nw || !_is_connected) {
return NSAPI_ERROR_NO_CONNECTION;
}

// set false here so callbacks know that we are not connected and so should not send DISCONNECTED
_is_connected = false;
#if NSAPI_PPP_AVAILABLE
nsapi_error_t err = nsapi_ppp_disconnect(_at.get_file_handle());
if (err != NSAPI_ERROR_OK) {
Expand All @@ -681,11 +692,12 @@ nsapi_error_t AT_CellularContext::disconnect()
} else {
deactivate_ip_context();
}
} else {
call_network_cb(NSAPI_STATUS_DISCONNECTED);
}
_is_context_active = false;
_connect_status = NSAPI_STATUS_DISCONNECTED;

_is_connected = false;
// call device's callback, it will broadcast this to here (cellular_callback)
_device->cellular_callback(NSAPI_EVENT_CONNECTION_STATUS_CHANGE, NSAPI_STATUS_DISCONNECTED, this);

return _at.unlock_return_error();
}
Expand Down Expand Up @@ -928,27 +940,32 @@ void AT_CellularContext::cellular_callback(nsapi_event_t ev, intptr_t ptr)
if (_is_blocking) {
if (data->error != NSAPI_ERROR_OK) {
// operation failed, release semaphore
_current_op = OP_INVALID;
_semaphore.release();
} else {
if ((st == CellularDeviceReady && _current_op == OP_DEVICE_READY) ||
(st == CellularSIMStatusChanged && _current_op == OP_SIM_READY &&
data->status_data == CellularDevice::SimStateReady)) {
// target reached, release semaphore
_current_op = OP_INVALID;
_semaphore.release();
} else if (st == CellularRegistrationStatusChanged && (data->status_data == CellularNetwork::RegisteredHomeNetwork ||
data->status_data == CellularNetwork::RegisteredRoaming || data->status_data == CellularNetwork::AlreadyRegistered) && _current_op == OP_REGISTER) {
// target reached, release semaphore
_current_op = OP_INVALID;
_semaphore.release();
} else if (st == CellularAttachNetwork && (_current_op == OP_ATTACH || _current_op == OP_CONNECT) &&
data->status_data == CellularNetwork::Attached) {
// target reached, release semaphore
_current_op = OP_INVALID;
_semaphore.release();
}
}
} else {
// non blocking
if (st == CellularAttachNetwork && _current_op == OP_CONNECT && data->error == NSAPI_ERROR_OK &&
data->status_data == CellularNetwork::Attached) {
_current_op = OP_INVALID;
// forward to application
if (_status_cb) {
_status_cb(ev, ptr);
Expand All @@ -963,14 +980,18 @@ void AT_CellularContext::cellular_callback(nsapi_event_t ev, intptr_t ptr)
if (ev == NSAPI_EVENT_CONNECTION_STATUS_CHANGE && ptr == NSAPI_STATUS_GLOBAL_UP) {
tr_info("CellularContext IP %s", get_ip_address());
_cb_data.error = NSAPI_ERROR_OK;
_semaphore.release();
} else if (ev == NSAPI_EVENT_CONNECTION_STATUS_CHANGE && ptr == NSAPI_STATUS_DISCONNECTED) {
tr_info("PPP disconnected");
_cb_data.error = NSAPI_ERROR_NO_CONNECTION;
_semaphore.release();
}
}
#endif
#else
#if MBED_CONF_MBED_TRACE_ENABLE
if (ev == NSAPI_EVENT_CONNECTION_STATUS_CHANGE && ptr == NSAPI_STATUS_DISCONNECTED) {
tr_info("cb: CellularContext disconnected");
}
#endif // MBED_CONF_MBED_TRACE_ENABLE
#endif // NSAPI_PPP_AVAILABLE
}

// forward to application
Expand Down Expand Up @@ -1044,8 +1065,7 @@ void AT_CellularContext::ciot_opt_cb(mbed::CellularNetwork::CIoT_Supported_Opt

void AT_CellularContext::set_disconnect()
{
tr_debug("AT_CellularContext::set_disconnect()");
_is_connected = false;
cell_callback_data_t data;
data.error = NSAPI_STATUS_DISCONNECTED;
_device->cellular_callback(NSAPI_EVENT_CONNECTION_STATUS_CHANGE, (intptr_t)&data);
_device->cellular_callback(NSAPI_EVENT_CONNECTION_STATUS_CHANGE, NSAPI_STATUS_DISCONNECTED, this);
}
14 changes: 3 additions & 11 deletions features/cellular/framework/AT/AT_CellularNetwork.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ void AT_CellularNetwork::read_reg_params_and_compare(RegistrationType type)
reg_params._status == RegisteredRoaming)) {
if (previous_registration_status == RegisteredHomeNetwork ||
previous_registration_status == RegisteredRoaming) {
_connection_status_cb(NSAPI_EVENT_CONNECTION_STATUS_CHANGE, NSAPI_STATUS_DISCONNECTED);
call_network_cb(NSAPI_STATUS_DISCONNECTED);
}
}
}
Expand Down Expand Up @@ -200,11 +200,8 @@ void AT_CellularNetwork::urc_cgreg()

void AT_CellularNetwork::call_network_cb(nsapi_connection_status_t status)
{
if (_connect_status != status) {
_connect_status = status;
if (_connection_status_cb) {
_connection_status_cb(NSAPI_EVENT_CONNECTION_STATUS_CHANGE, _connect_status);
}
if (_connection_status_cb) {
_connection_status_cb(NSAPI_EVENT_CONNECTION_STATUS_CHANGE, _connect_status);
}
}

Expand All @@ -213,11 +210,6 @@ void AT_CellularNetwork::attach(Callback<void(nsapi_event_t, intptr_t)> status_c
_connection_status_cb = status_cb;
}

nsapi_connection_status_t AT_CellularNetwork::get_connection_status() const
{
return _connect_status;
}

nsapi_error_t AT_CellularNetwork::set_registration_urc(RegistrationType type, bool urc_on)
{
int index = (int)type;
Expand Down
2 changes: 0 additions & 2 deletions features/cellular/framework/AT/AT_CellularNetwork.h
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,6 @@ class AT_CellularNetwork : public CellularNetwork, public AT_CellularBase {

virtual void attach(Callback<void(nsapi_event_t, intptr_t)> status_cb);

virtual nsapi_connection_status_t get_connection_status() const;

virtual nsapi_error_t set_access_technology(RadioAccessTechnology rat);

virtual nsapi_error_t scan_plmn(operList_t &operators, int &ops_count);
Expand Down
Loading