From cf14939ccad9ba40e239c12b4daf395e5930c05b Mon Sep 17 00:00:00 2001 From: Jon Trulson Date: Wed, 29 Apr 2015 16:08:36 -0600 Subject: [PATCH] uart: add uart device read and write functionality Signed-off-by: Jon Trulson Signed-off-by: Brendan Le Foll --- api/mraa/uart.h | 21 +++++++++++++++++++++ api/mraa/uart.hpp | 26 ++++++++++++++++++++++++++ src/uart/uart.c | 32 ++++++++++++++++++++++++++++++++ 3 files changed, 79 insertions(+) diff --git a/api/mraa/uart.h b/api/mraa/uart.h index 79aa732..e321d6a 100644 --- a/api/mraa/uart.h +++ b/api/mraa/uart.h @@ -93,6 +93,27 @@ mraa_result_t mraa_uart_open_dev(mraa_uart_context dev, speed_t baud); */ mraa_result_t mraa_uart_close_dev(mraa_uart_context dev); +/** + * Read bytes from the device into a buffer + * + * @param dev uart context + * @param buf buffer pointer + * @param len maximum size of buffer + * @return the number of bytes read, or -1 if an error occurred + */ +int mraa_uart_read(mraa_uart_context dev, char *buf, size_t len); + +/** + * Write bytes in buffer to a device + * + * @param dev uart context + * @param buf buffer pointer + * @param len maximum size of buffer + * @return the number of bytes written, or -1 if an error occurred + */ +int mraa_uart_write(mraa_uart_context dev, char *buf, size_t len); + + #ifdef __cplusplus } #endif diff --git a/api/mraa/uart.hpp b/api/mraa/uart.hpp index e976c87..d158d52 100644 --- a/api/mraa/uart.hpp +++ b/api/mraa/uart.hpp @@ -102,6 +102,32 @@ class Uart return mraa_uart_close_dev(m_uart); } + /** + * Read bytes from the device into a buffer + * + * @param buf buffer pointer + * @param len maximum size of buffer + * @return the number of bytes read, or -1 if an error occurred + */ + int + read(char *buf, size_t len) + { + return mraa_uart_read(m_uart, buf, len); + } + + /** + * Write bytes in buffer to a device + * + * @param buf buffer pointer + * @param len maximum size of buffer + * @return the number of bytes written, or -1 if an error occurred + */ + int + write(char *buf, size_t len) + { + return mraa_uart_write(m_uart, buf, len); + } + private: mraa_uart_context m_uart; }; diff --git a/src/uart/uart.c b/src/uart/uart.c index d933f0a..416cf71 100644 --- a/src/uart/uart.c +++ b/src/uart/uart.c @@ -172,3 +172,35 @@ mraa_uart_close_dev(mraa_uart_context dev) dev->fd = -1; return MRAA_SUCCESS; } + +int +mraa_uart_read(mraa_uart_context dev, char *buf, size_t len) +{ + if (!dev) { + syslog(LOG_ERR, "uart: read: context is NULL"); + return MRAA_ERROR_INVALID_HANDLE; + } + + if (dev->fd < 0) { + syslog(LOG_ERR, "uart: port is not open"); + return MRAA_ERROR_INVALID_RESOURCE; + } + + return read(dev->fd, buf, len); +} + +int +mraa_uart_write(mraa_uart_context dev, char *buf, size_t len) +{ + if (!dev) { + syslog(LOG_ERR, "uart: write: context is NULL"); + return MRAA_ERROR_INVALID_HANDLE; + } + + if (dev->fd < 0) { + syslog(LOG_ERR, "uart: port is not open"); + return MRAA_ERROR_INVALID_RESOURCE; + } + + return write(dev->fd, buf, len); +}