Skip to content

Commit 108c80b

Browse files
committed
feat(srv/nbus2): Disable EOT processing and adjust timeouts
1 parent 5208f0c commit 108c80b

File tree

2 files changed

+11
-53
lines changed

2 files changed

+11
-53
lines changed

services/nbus2/nbus2.c

+9-51
Original file line numberDiff line numberDiff line change
@@ -106,34 +106,6 @@ static nbus_ret_t stream_receive_expect(Nbus *self, uint8_t *buf, size_t size) {
106106
}
107107

108108

109-
/**
110-
* @brief Check for EOT at the end of the NBUS packet
111-
*
112-
* This is specific to the underlying implementation of the UART driver. If the number of bytes requested
113-
* during the read is the same as the number of received bytes, EOT is never returned because it is not
114-
* even attempted to be read from the StreamBuffer. It cannot be reasonably implemented because peek()
115-
* is not available.
116-
*
117-
* We are using the fact there is a interpacket delay after EOT so we may safely read one byte from
118-
* the stream if done *immediately* and should receive zero bytes with EOT return value. If anything other
119-
* is received, we may ignore it (actually we must because we lost framing).
120-
*
121-
* @return NBUS_RET_OK if EOT is correctly detected or
122-
* NBUS_RET_FAILED if some data is still left but no EOT was received. Most probably a malformed
123-
* packet, missing interpacket delay, interference, etc.
124-
*/
125-
static nbus_ret_t stream_eot_expect(Nbus *self) {
126-
uint8_t buf = 0;
127-
//u_log(system_log, LOG_TYPE_DEBUG, U_LOG_MODULE_PREFIX("expect EOT"));
128-
if (self->stream->vmt->read_timeout(self->stream, &buf, sizeof(buf), NULL, 0) == STREAM_RET_EOT) {
129-
//u_log(system_log, LOG_TYPE_DEBUG, U_LOG_MODULE_PREFIX("EOT"));
130-
return NBUS_RET_OK;
131-
}
132-
133-
return NBUS_RET_FAILED;
134-
}
135-
136-
137109
/**
138110
* @brief Wait for any gap in the input stream
139111
*
@@ -144,7 +116,7 @@ static nbus_ret_t stream_eot_expect(Nbus *self) {
144116
static nbus_ret_t stream_wait_for_eot(Nbus *self) {
145117
uint8_t buf[8];
146118
//u_log(system_log, LOG_TYPE_DEBUG, U_LOG_MODULE_PREFIX("wait for EOT"));
147-
while (self->stream->vmt->read_timeout(self->stream, buf, sizeof(buf), NULL, 10) == STREAM_RET_OK) {
119+
while (self->stream->vmt->read_timeout(self->stream, buf, sizeof(buf), NULL, 1) == STREAM_RET_OK) {
148120
;
149121
}
150122
return NBUS_RET_OK;
@@ -301,7 +273,6 @@ static nbus_ret_t nbus_pbuf_dispatch(Nbus *self, struct nbus_pbuf *pbuf) {
301273
/* Not ours, not interested. */
302274
continue;
303275
}
304-
305276
}
306277

307278
if (xQueueSend(self->sockets[i].rx_queue, &pbuf, 0) != pdTRUE) {
@@ -367,47 +338,34 @@ static void nbus_mac_task(void *p) {
367338
while (true) {
368339
struct nbus_pbuf *pbuf = nbus_pbuf_allocate(self);
369340
if (pbuf == NULL) {
370-
/* Cannot allocate a buffer, we must drop the packet. Wait at least for EOT. */
341+
/* Cannot allocate a buffer, we must drop the packet. Wait at least for EOT,
342+
* also yielding allowing other tasks to run. */
371343
stream_wait_for_eot(self);
372344
u_log(system_log, LOG_TYPE_WARN, U_LOG_MODULE_PREFIX("buffer allocation error"));
373345
continue;
374346
}
375347

376348
nbus_ret_t ret = nbus_pbuf_receive_header(pbuf, self);
377349
if (ret == NBUS_RET_OK) {
350+
marker(GPIOE, GPIO11, false);
378351
ret = nbus_pbuf_receive_data(pbuf, self);
379352
if (ret == NBUS_RET_OK) {
380-
/* End of packet. No additional data should be in the receive buffer. Check for EOT now
381-
* before a new packet reception starts. The packet is considered valid regardless of any
382-
* discarded additional data. */
383-
if (stream_eot_expect(self) != NBUS_RET_OK) {
384-
/* No EOT, additional data received, wait for one. */
385-
stream_wait_for_eot(self);
386-
}
387-
//print_pbuf(self, pbuf, "received ");
388-
marker(GPIOE, GPIO10, true);
389-
390-
/* Dispatch the received packet to the right socket, optionally discard the packet
353+
/* End of packet. No additional data should be in the receive buffer.
354+
* Dispatch the received packet to the right socket, optionally discard the packet
391355
* if there is no suitable socket bound. Give up the pbuf ownership. */
392356
nbus_pbuf_dispatch(self, pbuf);
393357
}
394358
}
395-
marker(GPIOE, GPIO11, false);
396-
359+
/* Do not do 'else'! This must catch wrong status 'ret' from both inner conditions. */
397360
if (ret != NBUS_RET_OK) {
398-
u_log(system_log, LOG_TYPE_WARN, U_LOG_MODULE_PREFIX("packet data reception error"));
399-
stream_wait_for_eot(self);
400361
nbus_pbuf_release(self, pbuf);
401-
continue;
402362
}
403363

404364
pbuf = nbus_pbuf_collect_one(self);
405365
if (pbuf != NULL) {
366+
/* Transmit and consume echo until EOT, we are not interested in the echo now. */
406367
nbus_pbuf_transmit(pbuf, self);
407-
408-
/* Consume echo until EOT. */
409368
stream_wait_for_eot(self);
410-
411369
nbus_pbuf_release(self, pbuf);
412370
}
413371
}
@@ -675,7 +633,7 @@ nbus_ret_t nbus_pbuf_get_destination(struct nbus_pbuf *self, uint8_t id[4], uint
675633

676634
nbus_ret_t nbus_pbuf_get_source(struct nbus_pbuf *self, uint8_t id[4], uint8_t *ep) {
677635
memcpy(id, &(self->buf[20]), 4);
678-
*ep = self->buf[14] >> 0x0f;
636+
*ep = self->buf[14] & 0x0f;
679637

680638
return NBUS_RET_OK;
681639
}

services/nbus2/nbus2.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@
1515
#include <main.h>
1616

1717

18-
#define NBUS_TIMEOUT_MS 10000
19-
#define NBUS_PBUF_COUNT 4
18+
#define NBUS_TIMEOUT_MS 1
19+
#define NBUS_PBUF_COUNT 16
2020
#define NBUS_PBUF_DATA_SIZE 1050
2121
#define NBUS_SOCKET_COUNT 4
2222
#define NBUS_SOCKET_TX_QUEUE_LEN 2

0 commit comments

Comments
 (0)