Skip to content

Commit

Permalink
Merge pull request #9890 from bergzand/pr/ethos/sync
Browse files Browse the repository at this point in the history
ethos: Only accept frame if previous frame is read
  • Loading branch information
kaspar030 authored Nov 14, 2018
2 parents a98edd3 + c4608ca commit 836fe3d
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 4 deletions.
18 changes: 14 additions & 4 deletions drivers/ethos/ethos.c
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ void ethos_setup(ethos_t *dev, const ethos_params_t *params)
dev->framesize = 0;
dev->frametype = 0;
dev->last_framesize = 0;
dev->accept_new = true;

tsrb_init(&dev->inbuf, (char*)params->buf, params->bufsize);
mutex_init(&dev->out_mutex);
Expand All @@ -82,6 +83,7 @@ static void _reset_state(ethos_t *dev)
dev->state = WAIT_FRAMESTART;
dev->frametype = 0;
dev->framesize = 0;
dev->accept_new = true;
}

static void _handle_char(ethos_t *dev, char c)
Expand All @@ -90,9 +92,12 @@ static void _handle_char(ethos_t *dev, char c)
case ETHOS_FRAME_TYPE_DATA:
case ETHOS_FRAME_TYPE_HELLO:
case ETHOS_FRAME_TYPE_HELLO_REPLY:
if (tsrb_add_one(&dev->inbuf, c) == 0) {
dev->framesize++;
} else {
if (dev->accept_new) {
if (tsrb_add_one(&dev->inbuf, c) == 0) {
dev->framesize++;
}
}
else {
//puts("lost frame");
dev->inbuf.reads = 0;
dev->inbuf.writes = 0;
Expand All @@ -112,6 +117,7 @@ static void _end_of_frame(ethos_t *dev)
switch(dev->frametype) {
case ETHOS_FRAME_TYPE_DATA:
if (dev->framesize) {
assert(dev->last_framesize == 0);
dev->last_framesize = dev->framesize;
dev->netdev.event_callback((netdev_t*) dev, NETDEV_EVENT_ISR);
}
Expand All @@ -137,6 +143,9 @@ static void ethos_isr(void *arg, uint8_t c)
case WAIT_FRAMESTART:
if (c == ETHOS_FRAME_DELIMITER) {
_reset_state(dev);
if (dev->last_framesize) {
dev->accept_new = false;
}
dev->state = IN_FRAME;
}
break;
Expand Down Expand Up @@ -304,13 +313,14 @@ static int _recv(netdev_t *netdev, void *buf, size_t len, void* info)
}

len = dev->last_framesize;
dev->last_framesize = 0;

if ((tsrb_get(&dev->inbuf, buf, len) != (int)len)) {
DEBUG("ethos _recv(): inbuf doesn't contain enough bytes.\n");
dev->last_framesize = 0;
return -1;
}

dev->last_framesize = 0;
return (int)len;
}
else {
Expand Down
3 changes: 3 additions & 0 deletions drivers/include/ethos.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
#ifndef ETHOS_H
#define ETHOS_H

#include <stdbool.h>

#include "kernel_types.h"
#include "periph/uart.h"
#include "net/netdev.h"
Expand Down Expand Up @@ -78,6 +80,7 @@ typedef struct {
unsigned frametype; /**< type of currently incoming frame */
size_t last_framesize; /**< size of last completed frame */
mutex_t out_mutex; /**< mutex used for locking concurrent sends */
bool accept_new; /**< incoming frame can be stored or not */
} ethos_t;

/**
Expand Down

0 comments on commit 836fe3d

Please sign in to comment.