Skip to content

Commit b22a16f

Browse files
author
Teppo Järvelin
committed
Cellular: Added dynamic alloc and destruction to easycellular.
Now application can call connect and disconnect multiple times and resources are freed and constructed properly. Also whole easycellular can be deleted and constructed again.
1 parent d08c819 commit b22a16f

File tree

6 files changed

+74
-34
lines changed

6 files changed

+74
-34
lines changed

features/cellular/easy_cellular/CellularConnectionFSM.cpp

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -70,18 +70,28 @@ CellularConnectionFSM::CellularConnectionFSM() :
7070
CellularConnectionFSM::~CellularConnectionFSM()
7171
{
7272
stop();
73-
delete _cellularDevice;
7473
}
7574

7675
void CellularConnectionFSM::stop()
7776
{
78-
tr_info("CellularConnectionUtil::stop");
77+
_queue.cancel(_event_id);
78+
_queue.break_dispatch();
7979

8080
if (_queue_thread) {
8181
_queue_thread->terminate();
8282
delete _queue_thread;
8383
_queue_thread = NULL;
8484
}
85+
86+
delete _cellularDevice;
87+
_cellularDevice = NULL;
88+
// _cellularDevice closes all interfaces in destructor
89+
_power = NULL;
90+
_network = NULL;
91+
_sim = NULL;
92+
93+
_state = STATE_INIT;
94+
_next_state = _state;
8595
}
8696

8797
nsapi_error_t CellularConnectionFSM::init()
@@ -98,6 +108,7 @@ nsapi_error_t CellularConnectionFSM::init()
98108
stop();
99109
return NSAPI_ERROR_NO_MEMORY;
100110
}
111+
101112
_network = _cellularDevice->open_network(_serial);
102113
if (!_network) {
103114
stop();
@@ -360,6 +371,7 @@ nsapi_error_t CellularConnectionFSM::continue_from_state(CellularState state)
360371

361372
nsapi_error_t CellularConnectionFSM::continue_to_state(CellularState state)
362373
{
374+
MBED_ASSERT(_cellularDevice);
363375
_retry_count = 0;
364376
if (state < _state) {
365377
_state = state;
@@ -647,8 +659,13 @@ void CellularConnectionFSM::set_callback(mbed::Callback<bool(int, int)> status_c
647659

648660
void CellularConnectionFSM::attach(mbed::Callback<void(nsapi_event_t, intptr_t)> status_cb)
649661
{
662+
MBED_ASSERT(_network);
650663
_event_status_cb = status_cb;
651-
_network->attach(callback(this, &CellularConnectionFSM::network_callback));
664+
if (status_cb) {
665+
_network->attach(callback(this, &CellularConnectionFSM::network_callback));
666+
} else {
667+
_network->attach(NULL);
668+
}
652669
}
653670

654671
void CellularConnectionFSM::network_callback(nsapi_event_t ev, intptr_t ptr)

features/cellular/easy_cellular/CellularConnectionFSM.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,8 @@ class CellularConnectionFSM
102102
*/
103103
nsapi_error_t start_dispatch();
104104

105-
/** Stop event queue dispatching and close cellular interfaces
105+
/** Stop event queue dispatching and close cellular interfaces. After calling stop(), init() must be called
106+
* before any other methods.
106107
*/
107108
void stop();
108109

@@ -153,6 +154,7 @@ class CellularConnectionFSM
153154
* @return string format of the given state
154155
*/
155156
const char* get_state_string(CellularState state);
157+
156158
private:
157159
bool power_on();
158160
bool open_sim();

features/cellular/easy_cellular/EasyCellularConnection.cpp

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,8 @@ EasyCellularConnection::EasyCellularConnection(bool debug) :
7676

7777
EasyCellularConnection::~EasyCellularConnection()
7878
{
79-
_cellularConnectionFSM.stop();
79+
_cellularConnectionFSM.set_callback(NULL);
80+
_cellularConnectionFSM.attach(NULL);
8081
}
8182

8283
nsapi_error_t EasyCellularConnection::init()
@@ -118,7 +119,7 @@ void EasyCellularConnection::set_credentials(const char *apn, const char *uname,
118119
}
119120
#endif // #if USE_APN_LOOKUP
120121
} else {
121-
//if get_network() returns NULL it means there was not enough memory for
122+
//if get_network() returns NULL it means there was not enough memory for
122123
//an AT_CellularNetwork element during CellularConnectionFSM initialization
123124
tr_error("There was not enough memory during CellularConnectionFSM initialization");
124125
}
@@ -224,13 +225,19 @@ nsapi_error_t EasyCellularConnection::disconnect()
224225
{
225226
_credentials_err = NSAPI_ERROR_OK;
226227
_is_connected = false;
228+
_is_initialized = false;
227229
#if USE_APN_LOOKUP
228230
_credentials_set = false;
229231
#endif // #if USE_APN_LOOKUP
230-
if (!_cellularConnectionFSM.get_network()) {
231-
return NSAPI_ERROR_NO_CONNECTION;
232+
233+
nsapi_error_t err = NSAPI_ERROR_OK;
234+
if (_cellularConnectionFSM.get_network()) {
235+
err = _cellularConnectionFSM.get_network()->disconnect();
232236
}
233-
return _cellularConnectionFSM.get_network()->disconnect();
237+
238+
_cellularConnectionFSM.stop();
239+
240+
return err;
234241
}
235242

236243
bool EasyCellularConnection::is_connected()

features/cellular/framework/AT/ATHandler.cpp

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,6 @@ static const uint8_t map_3gpp_errors[][2] = {
6363

6464
ATHandler::ATHandler(FileHandle *fh, EventQueue &queue, int timeout, const char *output_delimiter, uint16_t send_delay) :
6565
_nextATHandler(0),
66-
_fileHandle(fh),
6766
_queue(queue),
6867
_last_err(NSAPI_ERROR_OK),
6968
_last_3gpp_error(0),
@@ -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::enable_debug(bool enable)
@@ -153,7 +150,20 @@ FileHandle *ATHandler::get_file_handle()
153150

154151
void ATHandler::set_file_handle(FileHandle *fh)
155152
{
153+
_fh_sigio_set = false;
156154
_fileHandle = fh;
155+
_fileHandle->set_blocking(false);
156+
set_filehandle_sigio();
157+
}
158+
159+
void ATHandler::set_filehandle_sigio()
160+
{
161+
if (_fh_sigio_set) {
162+
return;
163+
}
164+
165+
_fileHandle->sigio(mbed::Callback<void()>(this, &ATHandler::event));
166+
_fh_sigio_set = true;
157167
}
158168

159169
nsapi_error_t ATHandler::set_urc_handler(const char *prefix, mbed::Callback<void()> callback)
@@ -310,15 +320,6 @@ void ATHandler::process_oob()
310320
unlock();
311321
}
312322

313-
void ATHandler::set_filehandle_sigio()
314-
{
315-
if (_fh_sigio_set) {
316-
return;
317-
}
318-
_fileHandle->sigio(mbed::Callback<void()>(this, &ATHandler::event));
319-
_fh_sigio_set = true;
320-
}
321-
322323
void ATHandler::reset_buffer()
323324
{
324325
tr_debug("%s", __func__);
@@ -1023,8 +1024,8 @@ void ATHandler::cmd_start(const char* cmd)
10231024
if (time_difference < (uint64_t)_at_send_delay) {
10241025
wait_ms((uint64_t)_at_send_delay - time_difference);
10251026
tr_debug("AT wait %llu %llu", current_time, _last_response_stop);
1026-
}
1027-
}
1027+
}
1028+
}
10281029

10291030
at_debug("AT cmd %s (err %d)\n", cmd, _last_err);
10301031

@@ -1088,7 +1089,7 @@ void ATHandler::cmd_stop()
10881089
size_t ATHandler::write_bytes(const uint8_t *data, size_t len)
10891090
{
10901091
at_debug("AT write bytes %d (err %d)\n", len, _last_err);
1091-
1092+
10921093
if (_last_err != NSAPI_ERROR_OK) {
10931094
return 0;
10941095
}

features/cellular/framework/AT/ATHandler.h

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -86,12 +86,6 @@ class ATHandler
8686
*/
8787
FileHandle *get_file_handle();
8888

89-
/** Set file handle, which is used for reading AT responses and writing AT commands
90-
*
91-
* @param fh file handle used for reading AT responses and writing AT commands
92-
*/
93-
void set_file_handle(FileHandle *fh);
94-
9589
/** Locks the mutex for file handle if AT_HANDLER_MUTEX is defined.
9690
*/
9791
void lock();
@@ -165,6 +159,11 @@ class ATHandler
165159
*/
166160
void clear_error();
167161

162+
/**
163+
* Flushes the underlying stream
164+
*/
165+
void flush();
166+
168167
/** Tries to find oob's from the AT response. Call the urc callback if one is found.
169168
*/
170169
void process_oob();
@@ -173,10 +172,11 @@ class ATHandler
173172
*/
174173
void set_filehandle_sigio();
175174

176-
/**
177-
* Flushes the underlying stream
175+
/** Set file handle, which is used for reading AT responses and writing AT commands
176+
*
177+
* @param fh file handle used for reading AT responses and writing AT commands
178178
*/
179-
void flush();
179+
void set_file_handle(FileHandle *fh);
180180

181181
protected:
182182
void event();

features/cellular/framework/AT/AT_CellularNetwork.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,19 @@ AT_CellularNetwork::AT_CellularNetwork(ATHandler &atHandler) : AT_CellularBase(a
4848

4949
AT_CellularNetwork::~AT_CellularNetwork()
5050
{
51+
#if NSAPI_PPP_AVAILABLE
52+
(void)disconnect();
53+
#else
54+
delete _stack;
55+
#endif // NSAPI_PPP_AVAILABLE
56+
57+
for (int type = 0; type < CellularNetwork::C_MAX; type++) {
58+
if (has_registration((RegistrationType)type)) {
59+
_at.remove_urc_handler(at_reg[type].urc_prefix, _urc_funcs[type]);
60+
}
61+
}
62+
63+
_at.remove_urc_handler("NO CARRIER", callback(this, &AT_CellularNetwork::urc_no_carrier));
5164
free_credentials();
5265
}
5366

0 commit comments

Comments
 (0)