diff options
author | px4dev <px4@purgatory.org> | 2013-04-28 18:14:46 -0700 |
---|---|---|
committer | px4dev <px4@purgatory.org> | 2013-04-28 18:14:46 -0700 |
commit | e67022f874f0fa091ed7dffd617c70c0c0253b5c (patch) | |
tree | 4308fd72c7dc6f782d464ac5886b226c371fd74d /src/modules/systemlib/hx_stream.c | |
parent | 8f7200e0114d6bd9fcac7ec088b125e54761c2e0 (diff) | |
download | px4-firmware-e67022f874f0fa091ed7dffd617c70c0c0253b5c.tar.gz px4-firmware-e67022f874f0fa091ed7dffd617c70c0c0253b5c.tar.bz2 px4-firmware-e67022f874f0fa091ed7dffd617c70c0c0253b5c.zip |
Serial interface for IOv2
Diffstat (limited to 'src/modules/systemlib/hx_stream.c')
-rw-r--r-- | src/modules/systemlib/hx_stream.c | 42 |
1 files changed, 38 insertions, 4 deletions
diff --git a/src/modules/systemlib/hx_stream.c b/src/modules/systemlib/hx_stream.c index 8d77f14a8..88f7f762c 100644 --- a/src/modules/systemlib/hx_stream.c +++ b/src/modules/systemlib/hx_stream.c @@ -76,6 +76,7 @@ struct hx_stream { static void hx_tx_raw(hx_stream_t stream, uint8_t c); static void hx_tx_raw(hx_stream_t stream, uint8_t c); static int hx_rx_frame(hx_stream_t stream); +static bool hx_rx_char(hx_stream_t stream, uint8_t c); static void hx_tx_raw(hx_stream_t stream, uint8_t c) @@ -224,13 +225,13 @@ hx_stream_send(hx_stream_t stream, return 0; } -void -hx_stream_rx(hx_stream_t stream, uint8_t c) +static bool +hx_rx_char(hx_stream_t stream, uint8_t c) { /* frame end? */ if (c == FBO) { hx_rx_frame(stream); - return; + return true; } /* escaped? */ @@ -241,10 +242,43 @@ hx_stream_rx(hx_stream_t stream, uint8_t c) } else if (c == CEO) { /* now escaped, ignore the byte */ stream->escaped = true; - return; + return false; } /* save for later */ if (stream->frame_bytes < sizeof(stream->buf)) stream->buf[stream->frame_bytes++] = c; + + return false; +} + +void +hx_stream_rx_char(hx_stream_t stream, uint8_t c) +{ + hx_rx_char(stream, c); +} + +int +hx_stream_rx(hx_stream_t stream) +{ + uint16_t buf[16]; + ssize_t len; + + /* read bytes */ + len = read(stream->fd, buf, sizeof(buf)); + if (len <= 0) { + + /* nonblocking stream and no data */ + if (errno == EWOULDBLOCK) + return 0; + + /* error or EOF */ + return -errno; + } + + /* process received characters */ + for (int i = 0; i < len; i++) + hx_rx_char(stream, buf[i]); + + return 0; } |