From cae63bbd52885b253a81a576df44ab0d959ae3c5 Mon Sep 17 00:00:00 2001 From: Shiran Ben-Melech Date: Thu, 3 Mar 2016 16:12:48 +0200 Subject: [PATCH] firmata: Added option to set uart non blocking state Signed-off-by: Shiran Ben-Melech Signed-off-by: Brendan Le Foll --- api/mraa/uart.h | 9 +++++++++ api/mraa/uart.hpp | 13 +++++++++++++ src/uart/uart.c | 22 ++++++++++++++++++++++ 3 files changed, 44 insertions(+) diff --git a/api/mraa/uart.h b/api/mraa/uart.h index 3ca196f..c6f5a7a 100644 --- a/api/mraa/uart.h +++ b/api/mraa/uart.h @@ -118,6 +118,15 @@ mraa_result_t mraa_uart_set_flowcontrol(mraa_uart_context dev, mraa_boolean_t xo */ mraa_result_t mraa_uart_set_timeout(mraa_uart_context dev, int read, int write, int interchar); +/** + * Set the blocking state for write operations + * + * @param dev The UART context + * @param nonblock new nonblocking state + * @return Result of operation + */ +mraa_result_t mraa_uart_set_non_blocking(mraa_uart_context dev, mraa_boolean_t nonblock); + /** * Get Char pointer with tty device path within Linux * For example. Could point to "/dev/ttyS0" diff --git a/api/mraa/uart.hpp b/api/mraa/uart.hpp index cc485a3..73154ce 100644 --- a/api/mraa/uart.hpp +++ b/api/mraa/uart.hpp @@ -236,6 +236,19 @@ class Uart return (Result) mraa_uart_set_timeout(m_uart, read, write, interchar); } + /** + * Set the blocking state for write operations + * + * @param dev The UART context + * @param nonblock new nonblocking state + * @return Result of operation + */ + Result + SetNonBlocking(bool nonblock) + { + return (Result) mraa_uart_set_non_blocking(m_uart, nonblock); + } + private: mraa_uart_context m_uart; }; diff --git a/src/uart/uart.c b/src/uart/uart.c index 0f8a2e3..1b0f435 100644 --- a/src/uart/uart.c +++ b/src/uart/uart.c @@ -451,6 +451,28 @@ mraa_uart_set_timeout(mraa_uart_context dev, int read, int write, int interchar) return MRAA_SUCCESS; } +mraa_result_t +mraa_uart_set_non_blocking(mraa_uart_context dev, mraa_boolean_t nonblock) +{ + // get current flags + int flags = fcntl(dev->fd, F_GETFL); + + // update flags with new blocking state according to nonblock bool + if (nonblock) { + flags |= O_NONBLOCK; + } else { + flags &= ~O_NONBLOCK; + } + + // set new flags + if (fcntl(dev->fd, F_SETFL, flags) < 0) { + syslog(LOG_ERR, "failed changing fd blocking state"); + return MRAA_ERROR_UNSPECIFIED; + } + + return MRAA_SUCCESS; +} + const char* mraa_uart_get_dev_path(mraa_uart_context dev) {