Skip to content

Commit f1e0d36

Browse files
author
Veijo Pesonen
committed
Makes OOB bg thread dynamically created
1 parent f7e95aa commit f1e0d36

File tree

2 files changed

+64
-8
lines changed

2 files changed

+64
-8
lines changed

ESP8266Interface.cpp

Lines changed: 55 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,9 @@ ESP8266Interface::ESP8266Interface()
5050
_initialized(false),
5151
_started(false),
5252
_conn_stat(NSAPI_STATUS_DISCONNECTED),
53-
_conn_stat_cb(NULL)
53+
_conn_stat_cb(NULL),
54+
_oob_thr(NULL),
55+
_oob_thr_sta(NULL)
5456
{
5557
memset(_cbs, 0, sizeof(_cbs));
5658
memset(ap_ssid, 0, sizeof(ap_ssid));
@@ -74,7 +76,10 @@ ESP8266Interface::ESP8266Interface(PinName tx, PinName rx, bool debug, PinName r
7476
_initialized(false),
7577
_started(false),
7678
_conn_stat(NSAPI_STATUS_DISCONNECTED),
77-
_conn_stat_cb(NULL)
79+
_conn_stat_cb(NULL),
80+
_oob_thr(NULL),
81+
_oob_thr_sta(NULL),
82+
_oob_thr_run(false)
7883
{
7984
memset(_cbs, 0, sizeof(_cbs));
8085
memset(ap_ssid, 0, sizeof(ap_ssid));
@@ -90,6 +95,19 @@ ESP8266Interface::ESP8266Interface(PinName tx, PinName rx, bool debug, PinName r
9095
}
9196
}
9297

98+
ESP8266Interface::~ESP8266Interface()
99+
{
100+
_oob_thr_run = false;
101+
102+
if (_oob_thr) {
103+
_oob_thr->join();
104+
delete _oob_thr;
105+
}
106+
if (_oob_thr_sta) {
107+
free (_oob_thr_sta);
108+
}
109+
}
110+
93111
int ESP8266Interface::connect(const char *ssid, const char *pass, nsapi_security_t security,
94112
uint8_t channel)
95113
{
@@ -105,6 +123,36 @@ int ESP8266Interface::connect(const char *ssid, const char *pass, nsapi_security
105123
return connect();
106124
}
107125

126+
void ESP8266Interface::start_bg_oob()
127+
{
128+
static const uint32_t _stack_sz = 2048;
129+
130+
if (!_oob_thr_sta) {
131+
_oob_thr_sta = (unsigned char*) (malloc(_stack_sz));
132+
}
133+
if (!_oob_thr_sta) {
134+
MBED_ERROR(MBED_MAKE_ERROR(MBED_MODULE_DRIVER, MBED_ERROR_ENOMEM), \
135+
"ESP8266::start_bg_oob: no memory");
136+
}
137+
138+
if (!_oob_thr) {
139+
_oob_thr = new Thread(osPriorityNormal,
140+
_stack_sz,
141+
_oob_thr_sta,
142+
"oob_proc");
143+
}
144+
145+
if (!_oob_thr) {
146+
MBED_ERROR(MBED_MAKE_ERROR(MBED_MODULE_DRIVER, MBED_ERROR_CODE_THREAD_CREATE_FAILED), \
147+
"ESP8266::start_bg_oob: no thread");
148+
}
149+
150+
if (_oob_thr->get_state() == rtos::Thread::Deleted) {
151+
_oob_thr_run = true;
152+
_oob_thr->start(callback(this, &ESP8266Interface::bg_process_oob));
153+
}
154+
}
155+
108156
int ESP8266Interface::connect()
109157
{
110158
nsapi_error_t status;
@@ -124,9 +172,7 @@ int ESP8266Interface::connect()
124172
return status;
125173
}
126174

127-
if (_oob_thread.get_state() == rtos::Thread::Deleted) {
128-
_oob_thread.start(callback(this, &ESP8266Interface::bg_process_oob));
129-
}
175+
start_bg_oob();
130176

131177
if(get_ip_address()) {
132178
return NSAPI_ERROR_IS_CONNECTED;
@@ -632,8 +678,10 @@ void ESP8266Interface::update_conn_state_cb()
632678

633679
void ESP8266Interface::bg_process_oob()
634680
{
635-
for(;;) {
636-
_esp.bg_process_oob(ESP8266_RECV_TIMEOUT, true);
681+
while (_oob_thr_run) {
682+
if (_initialized) {
683+
_esp.bg_process_oob(ESP8266_RECV_TIMEOUT, true);
684+
}
637685
wait_ms(ESP8266_RECV_TIMEOUT);
638686
}
639687
}

ESP8266Interface.h

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,11 @@ class ESP8266Interface : public NetworkStack, public WiFiInterface
5252
*/
5353
ESP8266Interface(PinName tx, PinName rx, bool debug=false, PinName rts=NC, PinName cts=NC);
5454

55+
/**
56+
* @brief ESP8266Interface default destructor
57+
*/
58+
~ESP8266Interface();
59+
5560
/** Start the interface
5661
*
5762
* Attempts to connect to a WiFi network. Requires ssid and passphrase to be set.
@@ -364,8 +369,11 @@ class ESP8266Interface : public NetworkStack, public WiFiInterface
364369
Callback<void(nsapi_event_t, intptr_t)> _conn_stat_cb;
365370

366371
// Background OOB processing
367-
rtos::Thread _oob_thread;
372+
rtos::Thread *_oob_thr;
373+
unsigned char *_oob_thr_sta;
374+
bool _oob_thr_run;
368375
void bg_process_oob();
376+
void start_bg_oob();
369377
};
370378

371379
#endif

0 commit comments

Comments
 (0)