15
15
*/
16
16
17
17
#include < cstring>
18
- #include " Callback.h"
18
+ #include " events/EventQueue.h"
19
+ #include " events/mbed_shared_queues.h"
20
+ #include " platform/Callback.h"
19
21
#include " ESP8266.h"
20
22
#include " ESP8266Interface.h"
21
23
#include " mbed_debug.h"
@@ -51,8 +53,8 @@ ESP8266Interface::ESP8266Interface()
51
53
_started(false ),
52
54
_conn_stat(NSAPI_STATUS_DISCONNECTED),
53
55
_conn_stat_cb(NULL ),
54
- _oob_thr (NULL ),
55
- _oob_thr_sta( NULL )
56
+ _geq (NULL ),
57
+ _oob_event_id( 0 )
56
58
{
57
59
memset (_cbs, 0 , sizeof (_cbs));
58
60
memset (ap_ssid, 0 , sizeof (ap_ssid));
@@ -77,9 +79,8 @@ ESP8266Interface::ESP8266Interface(PinName tx, PinName rx, bool debug, PinName r
77
79
_started(false ),
78
80
_conn_stat(NSAPI_STATUS_DISCONNECTED),
79
81
_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 )
83
84
{
84
85
memset (_cbs, 0 , sizeof (_cbs));
85
86
memset (ap_ssid, 0 , sizeof (ap_ssid));
@@ -97,14 +98,8 @@ ESP8266Interface::ESP8266Interface(PinName tx, PinName rx, bool debug, PinName r
97
98
98
99
ESP8266Interface::~ESP8266Interface ()
99
100
{
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);
108
103
}
109
104
}
110
105
@@ -123,36 +118,14 @@ int ESP8266Interface::connect(const char *ssid, const char *pass, nsapi_security
123
118
return connect ();
124
119
}
125
120
126
- void ESP8266Interface::start_bg_oob ()
121
+ void ESP8266Interface::_oob2geq ()
127
122
{
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));
148
125
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" );
156
129
}
157
130
}
158
131
@@ -175,7 +148,9 @@ int ESP8266Interface::connect()
175
148
return status;
176
149
}
177
150
178
- start_bg_oob ();
151
+ if (!_oob_event_id) {
152
+ _oob2geq ();
153
+ }
179
154
180
155
if (get_ip_address ()) {
181
156
return NSAPI_ERROR_IS_CONNECTED;
@@ -680,6 +655,8 @@ void ESP8266Interface::update_conn_state_cb()
680
655
default :
681
656
_started = false ;
682
657
_initialized = false ;
658
+ _geq->cancel (_oob_event_id);
659
+ _oob_event_id = 0 ;
683
660
_conn_stat = NSAPI_STATUS_DISCONNECTED;
684
661
}
685
662
@@ -689,12 +666,9 @@ void ESP8266Interface::update_conn_state_cb()
689
666
}
690
667
}
691
668
692
- void ESP8266Interface::thr_process_oob ()
669
+ void ESP8266Interface::proc_oob_evnt ()
693
670
{
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 );
699
673
}
700
674
}
0 commit comments