Skip to content

Cellular: improved observing of disconnect for callbacks. #7341

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
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
43 changes: 43 additions & 0 deletions features/cellular/framework/AT/AT_CellularNetwork.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ AT_CellularNetwork::~AT_CellularNetwork()
}

_at.remove_urc_handler("NO CARRIER", callback(this, &AT_CellularNetwork::urc_no_carrier));
_at.remove_urc_handler("+CGEV:", callback(this, &AT_CellularNetwork::urc_cgev));

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should also be removed in disconnect()

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

free_credentials();
}

Expand Down Expand Up @@ -105,6 +106,36 @@ void AT_CellularNetwork::urc_no_carrier()
call_network_cb(NSAPI_STATUS_DISCONNECTED);
}

void AT_CellularNetwork::urc_cgev()
{
char buf[13];
if (_at.read_string(buf, 13) < 8) { // smallest string length we wan't to compare is 8
return;
}
tr_debug("urc_cgev: %s", buf);

bool call_cb = false;
// NOTE! If in future there will be 2 or more active contexts we might wan't to read context id also but not for now.

if (memcmp(buf, "NW DETACH", 9) == 0) { // The network has forced a PS detach
call_cb = true;
} else if (memcmp(buf, "ME DETACH", 9) == 0) {// The mobile termination has forced a PS detach.
call_cb = true;
} else if (memcmp(buf, "NW DEACT", 8) == 0) {// The network has forced a context deactivation
call_cb = true;
} else if (memcmp(buf, "ME DEACT", 8) == 0) {// The mobile termination has forced a context deactivation
call_cb = true;
} else if (memcmp(buf, "NW PDN DEACT", 12) == 0) {// The network has deactivated a context
call_cb = true;
} else if (memcmp(buf, "ME PDN DEACT", 12) == 0) {// The mobile termination has deactivated a context.
call_cb = true;
}

if (call_cb) {
call_network_cb(NSAPI_STATUS_DISCONNECTED);
}
}

void AT_CellularNetwork::read_reg_params_and_compare(RegistrationType type)
{
RegistrationStatus reg_status = NotRegistered;
Expand Down Expand Up @@ -329,6 +360,17 @@ nsapi_error_t AT_CellularNetwork::connect()
return err;
}
#else
// additional urc to get better disconnect info for application. Not critical so not returning an error in case of failure
err = _at.set_urc_handler("+CGEV:", callback(this, &AT_CellularNetwork::urc_cgev));
if (err == NSAPI_ERROR_OK) {
_at.lock();
_at.cmd_start("AT+CGEREP=1");
_at.cmd_stop();
_at.resp_start();
_at.resp_stop();
_at.unlock();
}

call_network_cb(NSAPI_STATUS_GLOBAL_UP);
#endif

Expand Down Expand Up @@ -386,6 +428,7 @@ nsapi_error_t AT_CellularNetwork::disconnect()
_at.resp_stop();
_at.restore_at_timeout();

_at.remove_urc_handler("+CGEV:", callback(this, &AT_CellularNetwork::urc_cgev));
call_network_cb(NSAPI_STATUS_DISCONNECTED);

return _at.unlock_return_error();
Expand Down
1 change: 1 addition & 0 deletions features/cellular/framework/AT/AT_CellularNetwork.h
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,7 @@ class AT_CellularNetwork : public CellularNetwork, public AT_CellularBase
void urc_creg();
void urc_cereg();
void urc_cgreg();
void urc_cgev();

nsapi_ip_stack_t string_to_stack_type(const char* pdp_type);

Expand Down