forked from espressif/arduino-esp32
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathESP32_NOW_Serial.cpp
279 lines (260 loc) · 6.64 KB
/
ESP32_NOW_Serial.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
#include "ESP32_NOW_Serial.h"
#include <string.h>
#include "esp_now.h"
#include "esp_system.h"
#include "esp32-hal.h"
#include "esp_mac.h"
/*
*
* Serial Port Implementation Class
*
*/
ESP_NOW_Serial_Class::ESP_NOW_Serial_Class(const uint8_t *mac_addr, uint8_t channel, wifi_interface_t iface, const uint8_t *lmk, bool remove_on_fail)
: ESP_NOW_Peer(mac_addr, channel, iface, lmk) {
tx_ring_buf = NULL;
rx_queue = NULL;
tx_sem = NULL;
queued_size = 0;
queued_buff = NULL;
resend_count = 0;
_remove_on_fail = remove_on_fail;
}
ESP_NOW_Serial_Class::~ESP_NOW_Serial_Class() {
end();
}
size_t ESP_NOW_Serial_Class::setTxBufferSize(size_t tx_queue_len) {
if (tx_ring_buf) {
vRingbufferDelete(tx_ring_buf);
tx_ring_buf = NULL;
}
if (!tx_queue_len) {
return 0;
}
tx_ring_buf = xRingbufferCreate(tx_queue_len, RINGBUF_TYPE_BYTEBUF);
if (!tx_ring_buf) {
return 0;
}
return tx_queue_len;
}
size_t ESP_NOW_Serial_Class::setRxBufferSize(size_t rx_queue_len) {
if (rx_queue) {
vQueueDelete(rx_queue);
rx_queue = NULL;
}
if (!rx_queue_len) {
return 0;
}
rx_queue = xQueueCreate(rx_queue_len, sizeof(uint8_t));
if (!rx_queue) {
return 0;
}
return rx_queue_len;
}
bool ESP_NOW_Serial_Class::begin(unsigned long baud) {
if (!ESP_NOW.begin() || !add()) {
return false;
}
if (tx_sem == NULL) {
tx_sem = xSemaphoreCreateBinary();
//xSemaphoreTake(tx_sem, 0);
xSemaphoreGive(tx_sem);
}
setRxBufferSize(1024); //default if not preset
setTxBufferSize(1024); //default if not preset
return true;
}
void ESP_NOW_Serial_Class::end() {
remove();
setRxBufferSize(0);
setTxBufferSize(0);
if (tx_sem != NULL) {
vSemaphoreDelete(tx_sem);
tx_sem = NULL;
}
}
//Stream
int ESP_NOW_Serial_Class::available(void) {
if (rx_queue == NULL) {
return 0;
}
return uxQueueMessagesWaiting(rx_queue);
}
int ESP_NOW_Serial_Class::peek(void) {
if (rx_queue == NULL) {
return -1;
}
uint8_t c;
if (xQueuePeek(rx_queue, &c, 0)) {
return c;
}
return -1;
}
int ESP_NOW_Serial_Class::read(void) {
if (rx_queue == NULL) {
return -1;
}
uint8_t c = 0;
if (xQueueReceive(rx_queue, &c, 0)) {
return c;
}
return -1;
}
size_t ESP_NOW_Serial_Class::read(uint8_t *buffer, size_t size) {
if (rx_queue == NULL) {
return -1;
}
uint8_t c = 0;
size_t count = 0;
while (count < size && xQueueReceive(rx_queue, &c, 0)) {
buffer[count++] = c;
}
return count;
}
void ESP_NOW_Serial_Class::flush() {
if (tx_ring_buf == NULL) {
return;
}
UBaseType_t uxItemsWaiting = 0;
vRingbufferGetInfo(tx_ring_buf, NULL, NULL, NULL, NULL, &uxItemsWaiting);
if (uxItemsWaiting) {
// Now trigger the ISR to read data from the ring buffer.
if (xSemaphoreTake(tx_sem, 0) == pdTRUE) {
checkForTxData();
}
}
while (uxItemsWaiting) {
delay(5);
vRingbufferGetInfo(tx_ring_buf, NULL, NULL, NULL, NULL, &uxItemsWaiting);
}
}
//RX callback
void ESP_NOW_Serial_Class::onReceive(const uint8_t *data, size_t len, bool broadcast) {
if (rx_queue == NULL) {
return;
}
for (uint32_t i = 0; i < len; i++) {
if (!xQueueSend(rx_queue, data + i, 0)) {
log_e("RX Overflow!");
return;
}
}
// Now trigger the ISR to read data from the ring buffer.
if (xSemaphoreTake(tx_sem, 0) == pdTRUE) {
checkForTxData();
}
}
//Print
int ESP_NOW_Serial_Class::availableForWrite() {
//return ESP_NOW_MAX_DATA_LEN;
if (tx_ring_buf == NULL) {
return 0;
}
return xRingbufferGetCurFreeSize(tx_ring_buf);
}
size_t ESP_NOW_Serial_Class::tryToSend() {
//log_d(MACSTR ": %u", MAC2STR(addr()), queued_size);
size_t sent = send(queued_buff, queued_size);
if (!sent) {
//_onSent will not be called anymore
//the data is lost in this case
vRingbufferReturnItem(tx_ring_buf, queued_buff);
queued_buff = NULL;
xSemaphoreGive(tx_sem);
end();
}
return sent;
}
bool ESP_NOW_Serial_Class::checkForTxData() {
//do we have something that failed the last time?
resend_count = 0;
if (queued_buff == NULL) {
queued_buff = (uint8_t *)xRingbufferReceiveUpTo(tx_ring_buf, &queued_size, 0, ESP_NOW_MAX_DATA_LEN);
} else {
log_d(MACSTR " : PREVIOUS", MAC2STR(addr()));
}
if (queued_buff != NULL) {
return tryToSend() > 0;
}
//log_d(MACSTR ": EMPTY", MAC2STR(addr()));
xSemaphoreGive(tx_sem);
return false;
}
size_t ESP_NOW_Serial_Class::write(const uint8_t *buffer, size_t size, uint32_t timeout) {
log_v(MACSTR ", size %u", MAC2STR(addr()), size);
if (tx_sem == NULL || tx_ring_buf == NULL || !added) {
return 0;
}
size_t space = availableForWrite();
size_t left = size;
if (space) {
if (left < space) {
space = left;
}
if (xRingbufferSend(tx_ring_buf, (void *)(buffer), space, 0) == pdTRUE) {
buffer += space;
left -= space;
if (xSemaphoreTake(tx_sem, 0) == pdTRUE) {
if (checkForTxData()) {
if (!left) {
//we are done
return size;
}
} else {
//send failed
return 0;
}
}
} else {
log_e("RingbufferFastSend Failed");
return 0;
}
} else if (xSemaphoreTake(tx_sem, timeout) == pdTRUE) {
checkForTxData();
}
// Blocking method, Sending data to ringbuffer, and handle the data in ISR.
if (xRingbufferSend(tx_ring_buf, (void *)(buffer), left, timeout / portTICK_PERIOD_MS) != pdTRUE) {
log_e("RingbufferSend Failed");
return size - left;
}
// Now trigger the ISR to read data from the ring buffer.
if (xSemaphoreTake(tx_sem, 0) == pdTRUE) {
checkForTxData();
}
return size;
}
//TX Done Callback
void ESP_NOW_Serial_Class::onSent(bool success) {
log_v(MACSTR " : %s", MAC2STR(addr()), success ? "OK" : "FAIL");
if (tx_sem == NULL || tx_ring_buf == NULL || !added) {
return;
}
if (success) {
vRingbufferReturnItem(tx_ring_buf, queued_buff);
queued_buff = NULL;
//send next packet?
//log_d(MACSTR ": NEXT", MAC2STR(addr()));
checkForTxData();
} else {
//send failed
//resend
if (resend_count < 5) {
resend_count++;
//log_d(MACSTR ": RE-SENDING[%u]", MAC2STR(addr()), resend_count);
tryToSend();
} else {
//resend limit reached
//the data is lost in this case
vRingbufferReturnItem(tx_ring_buf, queued_buff);
queued_buff = NULL;
log_e(MACSTR " : RE-SEND_MAX[%u]", MAC2STR(addr()), resend_count);
//if we are not able to send the data and remove_on_fail is set, remove the peer
if (_remove_on_fail) {
xSemaphoreGive(tx_sem);
end();
return;
}
//log_d(MACSTR ": NEXT", MAC2STR(addr()));
checkForTxData();
}
}
}