Skip to content

Commit

Permalink
Switches from OOB thread to global event queue
Browse files Browse the repository at this point in the history
  • Loading branch information
Veijo Pesonen committed Oct 26, 2018
1 parent ab1a511 commit c7269f9
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 56 deletions.
72 changes: 23 additions & 49 deletions ESP8266Interface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@
*/

#include <cstring>
#include "Callback.h"
#include "events/EventQueue.h"
#include "events/mbed_shared_queues.h"
#include "platform/Callback.h"
#include "ESP8266.h"
#include "ESP8266Interface.h"
#include "mbed_debug.h"
Expand Down Expand Up @@ -51,8 +53,8 @@ ESP8266Interface::ESP8266Interface()
_started(false),
_conn_stat(NSAPI_STATUS_DISCONNECTED),
_conn_stat_cb(NULL),
_oob_thr(NULL),
_oob_thr_sta(NULL)
_geq(NULL),
_oob_event_id(0)
{
memset(_cbs, 0, sizeof(_cbs));
memset(ap_ssid, 0, sizeof(ap_ssid));
Expand All @@ -77,9 +79,8 @@ ESP8266Interface::ESP8266Interface(PinName tx, PinName rx, bool debug, PinName r
_started(false),
_conn_stat(NSAPI_STATUS_DISCONNECTED),
_conn_stat_cb(NULL),
_oob_thr(NULL),
_oob_thr_sta(NULL),
_oob_thr_run(false)
_geq(NULL),
_oob_event_id(0)
{
memset(_cbs, 0, sizeof(_cbs));
memset(ap_ssid, 0, sizeof(ap_ssid));
Expand All @@ -97,14 +98,8 @@ ESP8266Interface::ESP8266Interface(PinName tx, PinName rx, bool debug, PinName r

ESP8266Interface::~ESP8266Interface()
{
_oob_thr_run = false;

if (_oob_thr) {
_oob_thr->join();
delete _oob_thr;
}
if (_oob_thr_sta) {
free (_oob_thr_sta);
if (_oob_event_id) {
_geq->cancel(_oob_event_id);
}
}

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

void ESP8266Interface::start_bg_oob()
void ESP8266Interface::_oob2geq()
{
static const uint32_t _stack_sz = 1536;

if (!_oob_thr_sta) {
_oob_thr_sta = (unsigned char*) (malloc(_stack_sz));
}
if (!_oob_thr_sta) {
MBED_ERROR(MBED_MAKE_ERROR(MBED_MODULE_DRIVER, MBED_ERROR_ENOMEM), \
"ESP8266::start_bg_oob: no memory");
}

if (!_oob_thr) {
_oob_thr = new Thread(osPriorityNormal,
_stack_sz,
_oob_thr_sta,
"esp8266_oob");
}
if (!_oob_thr) {
MBED_ERROR(MBED_MAKE_ERROR(MBED_MODULE_DRIVER, MBED_ERROR_CODE_THREAD_CREATE_FAILED), \
"ESP8266::start_bg_oob: thread creation failed");
}
_geq = mbed_event_queue();
_oob_event_id = _geq->call_every(ESP8266_RECV_TIMEOUT, callback(this, &ESP8266Interface::proc_oob_evnt));

if (_oob_thr->get_state() == rtos::Thread::Deleted) {
_oob_thr_run = true;
osStatus status = _oob_thr->start(callback(this, &ESP8266Interface::thr_process_oob));
if (status != osOK) {
MBED_ERROR(MBED_MAKE_ERROR(MBED_MODULE_DRIVER, MBED_ERROR_CODE_THREAD_CREATE_FAILED), \
"ESP8266::start_bg_oob: thread start failed");
}
if (!_oob_event_id) {
MBED_ERROR(MBED_MAKE_ERROR(MBED_MODULE_DRIVER, MBED_ERROR_CODE_ENOMEM), \
"ESP8266::_oob2geq: unable to allocate OOB event");
}
}

Expand All @@ -175,7 +148,9 @@ int ESP8266Interface::connect()
return status;
}

start_bg_oob();
if (!_oob_event_id) {
_oob2geq();
}

if(get_ip_address()) {
return NSAPI_ERROR_IS_CONNECTED;
Expand Down Expand Up @@ -680,6 +655,8 @@ void ESP8266Interface::update_conn_state_cb()
default:
_started = false;
_initialized = false;
_geq->cancel(_oob_event_id);
_oob_event_id = 0;
_conn_stat = NSAPI_STATUS_DISCONNECTED;
}

Expand All @@ -689,12 +666,9 @@ void ESP8266Interface::update_conn_state_cb()
}
}

void ESP8266Interface::thr_process_oob()
void ESP8266Interface::proc_oob_evnt()
{
while (_oob_thr_run) {
if (_initialized) {
_esp.bg_process_oob(ESP8266_RECV_TIMEOUT, true);
}
wait_ms(ESP8266_RECV_TIMEOUT);
if (_initialized) {
_esp.bg_process_oob(ESP8266_RECV_TIMEOUT, true);
}
}
15 changes: 8 additions & 7 deletions ESP8266Interface.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,9 @@

#include "mbed.h"

#include "Thread.h"
#include "Callback.h"
#include "events/EventQueue.h"
#include "events/mbed_shared_queues.h"
#include "platform/Callback.h"
#include "netsocket/nsapi_types.h"
#include "netsocket/NetworkInterface.h"
#include "netsocket/NetworkStack.h"
Expand Down Expand Up @@ -369,11 +370,11 @@ class ESP8266Interface : public NetworkStack, public WiFiInterface
Callback<void(nsapi_event_t, intptr_t)> _conn_stat_cb;

// Background OOB processing
rtos::Thread *_oob_thr;
unsigned char *_oob_thr_sta;
bool _oob_thr_run;
void thr_process_oob();
void start_bg_oob();
// Use global EventQueue
events::EventQueue *_geq;
int _oob_event_id;
void proc_oob_evnt();
void _oob2geq();
};

#endif

0 comments on commit c7269f9

Please sign in to comment.