Repository navigation
Expand file tree
/
Copy pathserial2socket.cpp
More file actions
366 lines (310 loc) · 10.5 KB
/
Copy pathserial2socket.cpp
File metadata and controls
366 lines (310 loc) · 10.5 KB
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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
/*
serial2socket.cpp - serial 2 socket functions class
Copyright (c) 2014 Luc Lebosse. All rights reserved.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
// #define ESP_LOG_FEATURE LOG_OUTPUT_SERIAL0
#include "../../include/esp3d_config.h"
#if defined(ESP3DLIB_ENV) && COMMUNICATION_PROTOCOL == SOCKET_SERIAL
#include <Arduino.h>
#include "freertos/FreeRTOS.h"
#include "freertos/semphr.h"
#include "freertos/queue.h"
#include "../../core/esp3d_commands.h"
#include "../../core/esp3d_message.h"
#include "serial2socket.h"
Serial_2_Socket Serial2Socket;
Serial_2_Socket::Serial_2_Socket() {
_rxBufferMutex = (void*)xSemaphoreCreateMutex();
if (_rxBufferMutex == NULL) {
esp3d_log_e("Serial2Socket: Failed to create RX mutex");
}
_txBufferMutex = (void*)xSemaphoreCreateMutex();
if (_txBufferMutex == NULL) {
esp3d_log_e("Serial2Socket: Failed to create TX mutex");
}
_txQueue = (void*)xQueueCreate(S2S_TXQUEUESIZE, sizeof(ESP3DMessage *));
if (_txQueue == NULL) {
esp3d_log_e("Serial2Socket: Failed to create TX queue");
}
end();
}
Serial_2_Socket::~Serial_2_Socket() {
if (_rxBufferMutex != NULL) {
vSemaphoreDelete((SemaphoreHandle_t)_rxBufferMutex);
_rxBufferMutex = NULL;
}
if (_txBufferMutex != NULL) {
vSemaphoreDelete((SemaphoreHandle_t)_txBufferMutex);
_txBufferMutex = NULL;
}
end();
}
void Serial_2_Socket::begin(long speed) { end(); }
void Serial_2_Socket::enable(bool enable) { _started = enable; }
void Serial_2_Socket::pause(bool state) {
_paused = state;
if (_paused) {
// Protect TX buffer access with mutex
if (_txBufferMutex != NULL && xSemaphoreTake((SemaphoreHandle_t)_txBufferMutex, pdMS_TO_TICKS(100)) == pdTRUE) {
_TXbufferSize = 0;
xSemaphoreGive((SemaphoreHandle_t)_txBufferMutex);
} else {
_TXbufferSize = 0;
}
// Protect RX buffer access with mutex
if (_rxBufferMutex != NULL && xSemaphoreTake((SemaphoreHandle_t)_rxBufferMutex, pdMS_TO_TICKS(100)) == pdTRUE) {
_RXbufferSize = 0;
_RXbufferpos = 0;
xSemaphoreGive((SemaphoreHandle_t)_rxBufferMutex);
} else {
_RXbufferSize = 0;
_RXbufferpos = 0;
}
} else {
_lastflush = millis();
}
}
bool Serial_2_Socket::isPaused() { return _paused; }
void Serial_2_Socket::end() {
// Protect TX buffer access with mutex
if (_txBufferMutex != NULL && xSemaphoreTake((SemaphoreHandle_t)_txBufferMutex, pdMS_TO_TICKS(100)) == pdTRUE) {
_TXbufferSize = 0;
xSemaphoreGive((SemaphoreHandle_t)_txBufferMutex);
} else {
_TXbufferSize = 0;
}
// Protect RX buffer access with mutex
if (_rxBufferMutex != NULL && xSemaphoreTake((SemaphoreHandle_t)_rxBufferMutex, pdMS_TO_TICKS(100)) == pdTRUE) {
_RXbufferSize = 0;
_RXbufferpos = 0;
xSemaphoreGive((SemaphoreHandle_t)_rxBufferMutex);
} else {
_RXbufferSize = 0;
_RXbufferpos = 0;
}
_started = false;
_paused = false;
_lastflush = millis();
#if defined(AUTHENTICATION_FEATURE)
_auth = ESP3DAuthenticationLevel::guest;
#else
_auth = ESP3DAuthenticationLevel::admin;
#endif // AUTHENTICATION_FEATURE
}
long Serial_2_Socket::baudRate() { return 0; }
bool Serial_2_Socket::started() { return _started; }
Serial_2_Socket::operator bool() const { return true; }
int Serial_2_Socket::available() {
if (_paused || !_started) {
return 0;
}
if (_RXbufferSize == 0) {
return 0;
}
// Protect RX buffer access with mutex
if (_rxBufferMutex != NULL && xSemaphoreTake((SemaphoreHandle_t)_rxBufferMutex, pdMS_TO_TICKS(100)) != pdTRUE) {
esp3d_log_e("Serial2Socket: Failed to take mutex for available");
return 0;
}
int size = _RXbufferSize;
if (_rxBufferMutex != NULL) {
xSemaphoreGive((SemaphoreHandle_t)_rxBufferMutex);
}
return size;
}
size_t Serial_2_Socket::write(uint8_t c) {
if (!_started || _paused) {
return 1;
}
esp3d_log_d("Serial2Socket: write one char %c", c);
return write(&c, 1);
}
size_t Serial_2_Socket::write(const uint8_t *buffer, size_t size) {
if (buffer == NULL || size == 0 || !_started || _paused) {
esp3d_log("Serial2Socket: no data, not started or paused");
return size;
}
// Take TX buffer mutex once for the entire function
if (_txBufferMutex != NULL && xSemaphoreTake((SemaphoreHandle_t)_txBufferMutex, pdMS_TO_TICKS(100)) != pdTRUE) {
esp3d_log_e("Serial2Socket: Failed to take mutex for write");
return 0;
}
if (_TXbufferSize == 0) {
_lastflush = millis();
}
// Check if buffer is full and needs flushing
if (_TXbufferSize + size > S2S_TXBUFFERSIZE) {
esp3d_log_d("Serial2Socket: buffer full, flush it");
queueTXbuffer(); // We already have the mutex
}
// Add data to buffer and flush on newline
for (int i = 0; i < size; i++) {
_TXbuffer[_TXbufferSize] = buffer[i];
_TXbufferSize++;
if (buffer[i] == (const uint8_t)'\n') {
esp3d_log_d("S2S: %s TXSize: %d", (const char *)_TXbuffer, _TXbufferSize);
queueTXbuffer(); // We already have the mutex
}
}
// Check if we need to flush based on time
if (_TXbufferSize > 0 && ((millis() - _lastflush) > S2S_FLUSHTIMEOUT)) {
queueTXbuffer(); // We already have the mutex
}
// Release the mutex at the end
if (_txBufferMutex != NULL) {
xSemaphoreGive((SemaphoreHandle_t)_txBufferMutex);
}
return size;
}
int Serial_2_Socket::peek(void) {
esp3d_log_d("Serial2Socket: peek first of %d", _RXbufferSize);
if (_RXbufferSize <= 0 || !_started) {
return -1;
}
// Protect RX buffer access with mutex
if (_rxBufferMutex != NULL && xSemaphoreTake((SemaphoreHandle_t)_rxBufferMutex, pdMS_TO_TICKS(100)) != pdTRUE) {
esp3d_log_e("Serial2Socket: Failed to take mutex for peek");
return -1;
}
uint8_t v = _RXbuffer[_RXbufferpos];
if (_rxBufferMutex != NULL) {
xSemaphoreGive((SemaphoreHandle_t)_rxBufferMutex);
}
return v;
}
// Send data to socket output buffer
bool Serial_2_Socket::push2RX(const uint8_t *buffer, size_t size) {
if (buffer == NULL || size == 0 || !_started || _paused) {
return false;
}
// Protect RX buffer access with mutex
if (_rxBufferMutex != NULL && xSemaphoreTake((SemaphoreHandle_t)_rxBufferMutex, pdMS_TO_TICKS(100)) != pdTRUE) {
esp3d_log_e("Serial2Socket: cannot take mutex for push2RX");
return false;
}
int data_size = size;
bool success = false;
esp3d_log_d("Serial2Socket: pushing %d chars to buffer", data_size);
if ((data_size + _RXbufferSize) <= S2S_RXBUFFERSIZE) {
int current = _RXbufferpos + _RXbufferSize;
if (current > S2S_RXBUFFERSIZE) {
current = current - S2S_RXBUFFERSIZE;
}
for (int i = 0; i < data_size; i++) {
if (current > (S2S_RXBUFFERSIZE - 1)) {
current = 0;
}
_RXbuffer[current] = buffer[i];
current++;
}
_RXbufferSize += size;
if (current < S2S_RXBUFFERSIZE) {
_RXbuffer[current] = 0;
}
success = true;
}
if (_rxBufferMutex != NULL) {
xSemaphoreGive((SemaphoreHandle_t)_rxBufferMutex);
}
return success;
}
int Serial_2_Socket::read(void) {
if (_RXbufferSize <= 0 || !_started || _paused) {
return -1;
}
// Protect RX buffer access with mutex
if (_rxBufferMutex != NULL && xSemaphoreTake((SemaphoreHandle_t)_rxBufferMutex, pdMS_TO_TICKS(100)) != pdTRUE) {
esp3d_log_e("Serial2Socket: Failed to take mutex for read");
return -1;
}
uint8_t v = _RXbuffer[_RXbufferpos];
_RXbufferpos++;
if (_RXbufferpos > (S2S_RXBUFFERSIZE - 1)) {
_RXbufferpos = 0;
}
_RXbufferSize--;
esp3d_log_d("Serial2Socket: read one char %c", v);
if (_rxBufferMutex != NULL) {
xSemaphoreGive((SemaphoreHandle_t)_rxBufferMutex);
}
return v;
}
// Called from the ESP3D task. write() runs in the caller's task (Marlin's
// loop), which must not wait on network clients, so it only queues messages.
void Serial_2_Socket::handle() {
handle_flush();
ESP3DMessage *msg;
while (_txQueue != NULL &&
xQueueReceive((QueueHandle_t)_txQueue, &msg, 0) == pdTRUE) {
esp3d_commands.process(msg);
}
}
void Serial_2_Socket::handle_flush() {
if (_TXbufferSize > 0 && _started && !_paused) {
if ((_TXbufferSize >= S2S_TXBUFFERSIZE) ||
((millis() - _lastflush) > S2S_FLUSHTIMEOUT)) {
esp3d_log("force socket flush");
flush(true);
}
}
}
void Serial_2_Socket::flush(bool useMutex) {
if (useMutex && _txBufferMutex != NULL &&
xSemaphoreTake((SemaphoreHandle_t)_txBufferMutex, pdMS_TO_TICKS(100)) != pdTRUE) {
esp3d_log_e("Serial2Socket: Failed to take mutex for flush");
return;
}
queueTXbuffer();
if (useMutex && _txBufferMutex != NULL) {
xSemaphoreGive((SemaphoreHandle_t)_txBufferMutex);
}
}
// Move the TX buffer into a message for handle() to process.
// The caller holds the TX mutex. If the queue is full, the output is dropped.
void Serial_2_Socket::queueTXbuffer() {
if (_TXbufferSize > 0 && _started && !_paused) {
ESP3DMessage *msg = esp3d_message_manager.newMsg(
ESP3DClientType::socket_serial, ESP3DClientType::all_clients, _TXbuffer,
_TXbufferSize, _auth);
_lastflush = millis();
_TXbufferSize = 0;
if (msg) {
msg->type = ESP3DMessageType::unique;
if (_txQueue == NULL ||
xQueueSend((QueueHandle_t)_txQueue, &msg, 0) != pdTRUE) {
esp3d_message_manager.deleteMsg(msg);
}
} else {
esp3d_log_e("Cannot create message");
}
}
}
bool Serial_2_Socket::dispatch(ESP3DMessage *message) {
if (!message || !_started) {
esp3d_log_e("Serial2Socket: no message or not started");
return false;
}
if (message->size > 0 && message->data) {
esp3d_log_d("Serial2Socket: dispatch message %d", message->size);
if (!push2RX(message->data, message->size)) {
esp3d_log_e("Serial2Socket: cannot push all data");
return false;
}
esp3d_message_manager.deleteMsg(message);
return true;
}
esp3d_log_e("Serial2Socket: no data in message");
return false;
}
#endif // ESP3DLIB_ENV