Skip to content

Commit c7269f9

Browse files
author
Veijo Pesonen
committed
Switches from OOB thread to global event queue
1 parent ab1a511 commit c7269f9

File tree

2 files changed

+31
-56
lines changed

2 files changed

+31
-56
lines changed

ESP8266Interface.cpp

Lines changed: 23 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,9 @@
1515
*/
1616

1717
#include <cstring>
18-
#include "Callback.h"
18+
#include "events/EventQueue.h"
19+
#include "events/mbed_shared_queues.h"
20+
#include "platform/Callback.h"
1921
#include "ESP8266.h"
2022
#include "ESP8266Interface.h"
2123
#include "mbed_debug.h"
@@ -51,8 +53,8 @@ ESP8266Interface::ESP8266Interface()
5153
_started(false),
5254
_conn_stat(NSAPI_STATUS_DISCONNECTED),
5355
_conn_stat_cb(NULL),
54-
_oob_thr(NULL),
55-
_oob_thr_sta(NULL)
56+
_geq(NULL),
57+
_oob_event_id(0)
5658
{
5759
memset(_cbs, 0, sizeof(_cbs));
5860
memset(ap_ssid, 0, sizeof(ap_ssid));
@@ -77,9 +79,8 @@ ESP8266Interface::ESP8266Interface(PinName tx, PinName rx, bool debug, PinName r
7779
_started(false),
7880
_conn_stat(NSAPI_STATUS_DISCONNECTED),
7981
_conn_stat_cb(NULL),
80-
_oob_thr(NULL),
81-
_oob_thr_sta(NULL),
82-
_oob_thr_run(false)
82+
_geq(NULL),
83+
_oob_event_id(0)
8384
{
8485
memset(_cbs, 0, sizeof(_cbs));
8586
memset(ap_ssid, 0, sizeof(ap_ssid));
@@ -97,14 +98,8 @@ ESP8266Interface::ESP8266Interface(PinName tx, PinName rx, bool debug, PinName r
9798

9899
ESP8266Interface::~ESP8266Interface()
99100
{
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);
101+
if (_oob_event_id) {
102+
_geq->cancel(_oob_event_id);
108103
}
109104
}
110105

@@ -123,36 +118,14 @@ int ESP8266Interface::connect(const char *ssid, const char *pass, nsapi_security
123118
return connect();
124119
}
125120

126-
void ESP8266Interface::start_bg_oob()
121+
void ESP8266Interface::_oob2geq()
127122
{
128-
static const uint32_t _stack_sz = 1536;
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-
"esp8266_oob");
143-
}
144-
if (!_oob_thr) {
145-
MBED_ERROR(MBED_MAKE_ERROR(MBED_MODULE_DRIVER, MBED_ERROR_CODE_THREAD_CREATE_FAILED), \
146-
"ESP8266::start_bg_oob: thread creation failed");
147-
}
123+
_geq = mbed_event_queue();
124+
_oob_event_id = _geq->call_every(ESP8266_RECV_TIMEOUT, callback(this, &ESP8266Interface::proc_oob_evnt));
148125

149-
if (_oob_thr->get_state() == rtos::Thread::Deleted) {
150-
_oob_thr_run = true;
151-
osStatus status = _oob_thr->start(callback(this, &ESP8266Interface::thr_process_oob));
152-
if (status != osOK) {
153-
MBED_ERROR(MBED_MAKE_ERROR(MBED_MODULE_DRIVER, MBED_ERROR_CODE_THREAD_CREATE_FAILED), \
154-
"ESP8266::start_bg_oob: thread start failed");
155-
}
126+
if (!_oob_event_id) {
127+
MBED_ERROR(MBED_MAKE_ERROR(MBED_MODULE_DRIVER, MBED_ERROR_CODE_ENOMEM), \
128+
"ESP8266::_oob2geq: unable to allocate OOB event");
156129
}
157130
}
158131

@@ -175,7 +148,9 @@ int ESP8266Interface::connect()
175148
return status;
176149
}
177150

178-
start_bg_oob();
151+
if (!_oob_event_id) {
152+
_oob2geq();
153+
}
179154

180155
if(get_ip_address()) {
181156
return NSAPI_ERROR_IS_CONNECTED;
@@ -680,6 +655,8 @@ void ESP8266Interface::update_conn_state_cb()
680655
default:
681656
_started = false;
682657
_initialized = false;
658+
_geq->cancel(_oob_event_id);
659+
_oob_event_id = 0;
683660
_conn_stat = NSAPI_STATUS_DISCONNECTED;
684661
}
685662

@@ -689,12 +666,9 @@ void ESP8266Interface::update_conn_state_cb()
689666
}
690667
}
691668

692-
void ESP8266Interface::thr_process_oob()
669+
void ESP8266Interface::proc_oob_evnt()
693670
{
694-
while (_oob_thr_run) {
695-
if (_initialized) {
696-
_esp.bg_process_oob(ESP8266_RECV_TIMEOUT, true);
697-
}
698-
wait_ms(ESP8266_RECV_TIMEOUT);
671+
if (_initialized) {
672+
_esp.bg_process_oob(ESP8266_RECV_TIMEOUT, true);
699673
}
700674
}

ESP8266Interface.h

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,9 @@
1919

2020
#include "mbed.h"
2121

22-
#include "Thread.h"
23-
#include "Callback.h"
22+
#include "events/EventQueue.h"
23+
#include "events/mbed_shared_queues.h"
24+
#include "platform/Callback.h"
2425
#include "netsocket/nsapi_types.h"
2526
#include "netsocket/NetworkInterface.h"
2627
#include "netsocket/NetworkStack.h"
@@ -369,11 +370,11 @@ class ESP8266Interface : public NetworkStack, public WiFiInterface
369370
Callback<void(nsapi_event_t, intptr_t)> _conn_stat_cb;
370371

371372
// Background OOB processing
372-
rtos::Thread *_oob_thr;
373-
unsigned char *_oob_thr_sta;
374-
bool _oob_thr_run;
375-
void thr_process_oob();
376-
void start_bg_oob();
373+
// Use global EventQueue
374+
events::EventQueue *_geq;
375+
int _oob_event_id;
376+
void proc_oob_evnt();
377+
void _oob2geq();
377378
};
378379

379380
#endif

0 commit comments

Comments
 (0)