@@ -106,34 +106,6 @@ static nbus_ret_t stream_receive_expect(Nbus *self, uint8_t *buf, size_t size) {
106
106
}
107
107
108
108
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
-
137
109
/**
138
110
* @brief Wait for any gap in the input stream
139
111
*
@@ -144,7 +116,7 @@ static nbus_ret_t stream_eot_expect(Nbus *self) {
144
116
static nbus_ret_t stream_wait_for_eot (Nbus * self ) {
145
117
uint8_t buf [8 ];
146
118
//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 ) {
148
120
;
149
121
}
150
122
return NBUS_RET_OK ;
@@ -301,7 +273,6 @@ static nbus_ret_t nbus_pbuf_dispatch(Nbus *self, struct nbus_pbuf *pbuf) {
301
273
/* Not ours, not interested. */
302
274
continue ;
303
275
}
304
-
305
276
}
306
277
307
278
if (xQueueSend (self -> sockets [i ].rx_queue , & pbuf , 0 ) != pdTRUE ) {
@@ -367,47 +338,34 @@ static void nbus_mac_task(void *p) {
367
338
while (true) {
368
339
struct nbus_pbuf * pbuf = nbus_pbuf_allocate (self );
369
340
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. */
371
343
stream_wait_for_eot (self );
372
344
u_log (system_log , LOG_TYPE_WARN , U_LOG_MODULE_PREFIX ("buffer allocation error" ));
373
345
continue ;
374
346
}
375
347
376
348
nbus_ret_t ret = nbus_pbuf_receive_header (pbuf , self );
377
349
if (ret == NBUS_RET_OK ) {
350
+ marker (GPIOE , GPIO11 , false);
378
351
ret = nbus_pbuf_receive_data (pbuf , self );
379
352
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
391
355
* if there is no suitable socket bound. Give up the pbuf ownership. */
392
356
nbus_pbuf_dispatch (self , pbuf );
393
357
}
394
358
}
395
- marker (GPIOE , GPIO11 , false);
396
-
359
+ /* Do not do 'else'! This must catch wrong status 'ret' from both inner conditions. */
397
360
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 );
400
361
nbus_pbuf_release (self , pbuf );
401
- continue ;
402
362
}
403
363
404
364
pbuf = nbus_pbuf_collect_one (self );
405
365
if (pbuf != NULL ) {
366
+ /* Transmit and consume echo until EOT, we are not interested in the echo now. */
406
367
nbus_pbuf_transmit (pbuf , self );
407
-
408
- /* Consume echo until EOT. */
409
368
stream_wait_for_eot (self );
410
-
411
369
nbus_pbuf_release (self , pbuf );
412
370
}
413
371
}
@@ -675,7 +633,7 @@ nbus_ret_t nbus_pbuf_get_destination(struct nbus_pbuf *self, uint8_t id[4], uint
675
633
676
634
nbus_ret_t nbus_pbuf_get_source (struct nbus_pbuf * self , uint8_t id [4 ], uint8_t * ep ) {
677
635
memcpy (id , & (self -> buf [20 ]), 4 );
678
- * ep = self -> buf [14 ] >> 0x0f ;
636
+ * ep = self -> buf [14 ] & 0x0f ;
679
637
680
638
return NBUS_RET_OK ;
681
639
}
0 commit comments