Skip to content

Commit 5ccfd9f

Browse files
author
Teppo Järvelin
committed
Cellular: improved observing of disconnect for callbacks.
1 parent 65abff9 commit 5ccfd9f

File tree

2 files changed

+44
-0
lines changed

2 files changed

+44
-0
lines changed

features/cellular/framework/AT/AT_CellularNetwork.cpp

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ AT_CellularNetwork::~AT_CellularNetwork()
6161
}
6262

6363
_at.remove_urc_handler("NO CARRIER", callback(this, &AT_CellularNetwork::urc_no_carrier));
64+
_at.remove_urc_handler("+CGEV:", callback(this, &AT_CellularNetwork::urc_cgev));
6465
free_credentials();
6566
}
6667

@@ -105,6 +106,36 @@ void AT_CellularNetwork::urc_no_carrier()
105106
call_network_cb(NSAPI_STATUS_DISCONNECTED);
106107
}
107108

109+
void AT_CellularNetwork::urc_cgev()
110+
{
111+
char buf[13];
112+
if (_at.read_string(buf, 13) < 8) { // smallest string length we wan't to compare is 8
113+
return;
114+
}
115+
tr_debug("urc_cgev: %s", buf);
116+
117+
bool call_cb = false;
118+
// 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.
119+
120+
if (memcmp(buf, "NW DETACH", 9) == 0) { // The network has forced a PS detach
121+
call_cb = true;
122+
} else if (memcmp(buf, "ME DETACH", 9) == 0) {// The mobile termination has forced a PS detach.
123+
call_cb = true;
124+
} else if (memcmp(buf, "NW DEACT", 8) == 0) {// The network has forced a context deactivation
125+
call_cb = true;
126+
} else if (memcmp(buf, "ME DEACT", 8) == 0) {// The mobile termination has forced a context deactivation
127+
call_cb = true;
128+
} else if (memcmp(buf, "NW PDN DEACT", 12) == 0) {// The network has deactivated a context
129+
call_cb = true;
130+
} else if (memcmp(buf, "ME PDN DEACT", 12) == 0) {// The mobile termination has deactivated a context.
131+
call_cb = true;
132+
}
133+
134+
if (call_cb) {
135+
call_network_cb(NSAPI_STATUS_DISCONNECTED);
136+
}
137+
}
138+
108139
void AT_CellularNetwork::read_reg_params_and_compare(RegistrationType type)
109140
{
110141
RegistrationStatus reg_status = NotRegistered;
@@ -329,6 +360,17 @@ nsapi_error_t AT_CellularNetwork::connect()
329360
return err;
330361
}
331362
#else
363+
// additional urc to get better disconnect info for application. Not critical so not returning an error in case of failure
364+
err = _at.set_urc_handler("+CGEV:", callback(this, &AT_CellularNetwork::urc_cgev));
365+
if (err == NSAPI_ERROR_OK) {
366+
_at.lock();
367+
_at.cmd_start("AT+CGEREP=1");
368+
_at.cmd_stop();
369+
_at.resp_start();
370+
_at.resp_stop();
371+
_at.unlock();
372+
}
373+
332374
call_network_cb(NSAPI_STATUS_GLOBAL_UP);
333375
#endif
334376

@@ -386,6 +428,7 @@ nsapi_error_t AT_CellularNetwork::disconnect()
386428
_at.resp_stop();
387429
_at.restore_at_timeout();
388430

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

391434
return _at.unlock_return_error();

features/cellular/framework/AT/AT_CellularNetwork.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,7 @@ class AT_CellularNetwork : public CellularNetwork, public AT_CellularBase
149149
void urc_creg();
150150
void urc_cereg();
151151
void urc_cgreg();
152+
void urc_cgev();
152153

153154
nsapi_ip_stack_t string_to_stack_type(const char* pdp_type);
154155

0 commit comments

Comments
 (0)