@@ -50,7 +50,9 @@ ESP8266Interface::ESP8266Interface()
50
50
_initialized(false ),
51
51
_started(false ),
52
52
_conn_stat(NSAPI_STATUS_DISCONNECTED),
53
- _conn_stat_cb(NULL )
53
+ _conn_stat_cb(NULL ),
54
+ _oob_thr(NULL ),
55
+ _oob_thr_sta(NULL )
54
56
{
55
57
memset (_cbs, 0 , sizeof (_cbs));
56
58
memset (ap_ssid, 0 , sizeof (ap_ssid));
@@ -74,7 +76,10 @@ ESP8266Interface::ESP8266Interface(PinName tx, PinName rx, bool debug, PinName r
74
76
_initialized(false ),
75
77
_started(false ),
76
78
_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 )
78
83
{
79
84
memset (_cbs, 0 , sizeof (_cbs));
80
85
memset (ap_ssid, 0 , sizeof (ap_ssid));
@@ -90,6 +95,19 @@ ESP8266Interface::ESP8266Interface(PinName tx, PinName rx, bool debug, PinName r
90
95
}
91
96
}
92
97
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
+
93
111
int ESP8266Interface::connect (const char *ssid, const char *pass, nsapi_security_t security,
94
112
uint8_t channel)
95
113
{
@@ -105,6 +123,36 @@ int ESP8266Interface::connect(const char *ssid, const char *pass, nsapi_security
105
123
return connect ();
106
124
}
107
125
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
+
108
156
int ESP8266Interface::connect ()
109
157
{
110
158
nsapi_error_t status;
@@ -124,9 +172,7 @@ int ESP8266Interface::connect()
124
172
return status;
125
173
}
126
174
127
- if (_oob_thread.get_state () == rtos::Thread::Deleted) {
128
- _oob_thread.start (callback (this , &ESP8266Interface::bg_process_oob));
129
- }
175
+ start_bg_oob ();
130
176
131
177
if (get_ip_address ()) {
132
178
return NSAPI_ERROR_IS_CONNECTED;
@@ -632,8 +678,10 @@ void ESP8266Interface::update_conn_state_cb()
632
678
633
679
void ESP8266Interface::bg_process_oob ()
634
680
{
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
+ }
637
685
wait_ms (ESP8266_RECV_TIMEOUT);
638
686
}
639
687
}
0 commit comments