Skip to content

Commit 5ad7c3e

Browse files
committed
Merge branch 'gracefully_disconnect' of https://github.com/jarvte/mbed-os into dev_rollup
2 parents c873dc0 + a655a53 commit 5ad7c3e

File tree

5 files changed

+59
-16
lines changed

5 files changed

+59
-16
lines changed

features/cellular/framework/API/CellularNetwork.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -230,7 +230,7 @@ class CellularNetwork {
230230
*/
231231
virtual nsapi_error_t get_attach(AttachStatus &status) = 0;
232232

233-
/** Request detach from a network.
233+
/** Request detach and deregister from a network.
234234
*
235235
* @return NSAPI_ERROR_OK on success
236236
* NSAPI_ERROR_DEVICE_ERROR on failure

features/cellular/framework/AT/ATHandler.cpp

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,6 @@ ATHandler::ATHandler(FileHandle *fh, EventQueue &queue, int timeout, const char
7272
_previous_at_timeout(timeout),
7373
_at_send_delay(send_delay),
7474
_last_response_stop(0),
75-
_fh_sigio_set(false),
7675
_processing(false),
7776
_ref_count(1),
7877
_is_fh_usable(true),
@@ -109,9 +108,7 @@ ATHandler::ATHandler(FileHandle *fh, EventQueue &queue, int timeout, const char
109108
set_tag(&_info_stop, CRLF);
110109
set_tag(&_elem_stop, ")");
111110

112-
_fileHandle->set_blocking(false);
113-
114-
set_filehandle_sigio();
111+
set_file_handle(fh);
115112
}
116113

117114
void ATHandler::set_debug(bool debug_on)
@@ -154,6 +151,8 @@ FileHandle *ATHandler::get_file_handle()
154151
void ATHandler::set_file_handle(FileHandle *fh)
155152
{
156153
_fileHandle = fh;
154+
_fileHandle->set_blocking(false);
155+
set_filehandle_sigio();
157156
}
158157

159158
void ATHandler::set_is_filehandle_usable(bool usable)
@@ -312,11 +311,7 @@ void ATHandler::process_oob()
312311

313312
void ATHandler::set_filehandle_sigio()
314313
{
315-
if (_fh_sigio_set) {
316-
return;
317-
}
318314
_fileHandle->sigio(mbed::Callback<void()>(this, &ATHandler::event));
319-
_fh_sigio_set = true;
320315
}
321316

322317
void ATHandler::reset_buffer()
@@ -1230,3 +1225,26 @@ void ATHandler::debug_print(const char *p, int len)
12301225
}
12311226
#endif // MBED_CONF_CELLULAR_DEBUG_AT
12321227
}
1228+
1229+
bool ATHandler::sync(int timeout_ms)
1230+
{
1231+
tr_debug("AT sync");
1232+
// poll for 10 seconds
1233+
for (int i = 0; i < 10; i++) {
1234+
lock();
1235+
set_at_timeout(timeout_ms, false);
1236+
// For sync use an AT command that is supported by all modems and likely not used frequently,
1237+
// especially a common response like OK could be response to previous request.
1238+
cmd_start("AT+CMEE?");
1239+
cmd_stop();
1240+
resp_start("+CMEE:");
1241+
resp_stop();
1242+
restore_at_timeout();
1243+
unlock();
1244+
if (!_last_err) {
1245+
return true;
1246+
}
1247+
}
1248+
tr_error("AT sync failed");
1249+
return false;
1250+
}

features/cellular/framework/AT/ATHandler.h

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,13 @@ class ATHandler {
181181
*/
182182
void set_is_filehandle_usable(bool usable);
183183

184+
/** Synchronize AT command and response handling to modem.
185+
*
186+
* @param timeout_ms ATHandler timeout when trying to sync. Will be restored when function returns.
187+
* @return true is synchronization was successful, false in case of failure
188+
*/
189+
bool sync(int timeout_ms);
190+
184191
protected:
185192
void event();
186193
#ifdef AT_HANDLER_MUTEX
@@ -211,8 +218,6 @@ class ATHandler {
211218
uint16_t _at_send_delay;
212219
uint64_t _last_response_stop;
213220

214-
bool _fh_sigio_set;
215-
216221
bool _processing;
217222
int32_t _ref_count;
218223
bool _is_fh_usable;

features/cellular/framework/AT/AT_CellularContext.cpp

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
#define DEVICE_TIMEOUT 5 * 60 * 1000 // 5 minutes
2929

3030
#if NSAPI_PPP_AVAILABLE
31+
#define AT_SYNC_TIMEOUT 1000 // 1 second timeout
3132
#include "nsapi_ppp.h"
3233
#endif
3334

@@ -557,6 +558,8 @@ void AT_CellularContext::do_connect()
557558
tr_error("Failed to open data channel!");
558559
call_network_cb(NSAPI_STATUS_DISCONNECTED);
559560
_is_connected = false;
561+
} else {
562+
_is_context_activated = true;
560563
}
561564
}
562565
#else
@@ -630,13 +633,17 @@ nsapi_error_t AT_CellularContext::disconnect()
630633
_at.lock();
631634
_at.set_file_handle(_at.get_file_handle());
632635
_at.set_is_filehandle_usable(true);
633-
//_at.sync(); // consume extra characters after ppp disconnect, also it may take a while until modem listens AT commands
636+
if (!_at.sync(AT_SYNC_TIMEOUT)) { // consume extra characters after ppp disconnect, also it may take a while until modem listens AT commands
637+
tr_error("AT sync failed after PPP Disconnect");
638+
}
634639
_at.unlock();
635640
#endif // NSAPI_PPP_AVAILABLE
636641
_at.lock();
637642

638643
// deactivate a context only if we have activated
639644
if (_is_context_activated) {
645+
// CGACT and CGATT commands might take up to 3 minutes to respond.
646+
_at.set_at_timeout(180 * 1000);
640647
_is_context_active = false;
641648
size_t active_contexts_count = 0;
642649
_at.cmd_start("AT+CGACT?");
@@ -662,17 +669,27 @@ nsapi_error_t AT_CellularContext::disconnect()
662669
// 3GPP TS 27.007:
663670
// For EPS, if an attempt is made to disconnect the last PDN connection, then the MT responds with ERROR
664671
if (_is_context_active && (rat < CellularNetwork::RAT_E_UTRAN || active_contexts_count > 1)) {
672+
_at.clear_error();
665673
_at.cmd_start("AT+CGACT=0,");
666674
_at.write_int(_cid);
667675
_at.cmd_stop_read_resp();
668676
}
669-
}
670677

671-
if (!_at.get_last_error()) {
672-
_is_connected = false;
673-
call_network_cb(NSAPI_STATUS_DISCONNECTED);
678+
if (_new_context_set) {
679+
_at.clear_error();
680+
_at.cmd_start("AT+CGDCONT=");
681+
_at.write_int(_cid);
682+
_at.cmd_stop_read_resp();
683+
}
684+
_at.clear_error();
685+
_at.cmd_start("AT+CGATT=0");
686+
_at.cmd_stop_read_resp();
687+
_at.restore_at_timeout();
674688
}
675689

690+
_is_connected = false;
691+
call_network_cb(NSAPI_STATUS_DISCONNECTED);
692+
676693
return _at.unlock_return_error();
677694
}
678695

features/cellular/framework/AT/AT_CellularNetwork.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -361,6 +361,9 @@ nsapi_error_t AT_CellularNetwork::detach()
361361
_at.cmd_start("AT+CGATT=0");
362362
_at.cmd_stop_read_resp();
363363

364+
_at.cmd_start("AT+COPS=2");
365+
_at.cmd_stop_read_resp();
366+
364367
call_network_cb(NSAPI_STATUS_DISCONNECTED);
365368

366369
return _at.unlock_return_error();

0 commit comments

Comments
 (0)