diff --git a/api/mraa/uart.h b/api/mraa/uart.h index c6f5a7a..4b580fe 100644 --- a/api/mraa/uart.h +++ b/api/mraa/uart.h @@ -72,6 +72,18 @@ mraa_uart_context mraa_uart_init_raw(const char* path); */ mraa_result_t mraa_uart_flush(mraa_uart_context dev); +/** + * Send a break to the device. + * Blocks until complete. + * + * @param dev The UART context + * @param duration When 0, send a break lasting at least 250 + * milliseconds, and not more than 500 milliseconds. When non zero, + * the break duration is implementation specific. + * @return Result of operation + */ +mraa_result_t mraa_uart_sendbreak(mraa_uart_context dev, int duration); + /** * Set the baudrate. * Takes an int and will attempt to decide what baudrate is diff --git a/api/mraa/uart.hpp b/api/mraa/uart.hpp index ac3b5b0..8bd5d34 100644 --- a/api/mraa/uart.hpp +++ b/api/mraa/uart.hpp @@ -197,6 +197,21 @@ class Uart return (Result) mraa_uart_flush(m_uart); } + /** + * Send a break to the device. + * Blocks until complete. + * + * @param duration When 0, send a break lasting at least 250 + * milliseconds, and not more than 500 milliseconds. When non zero, + * the break duration is implementation specific. + * @return Result of operation + */ + Result + sendBreak(int duration) + { + return (Result) mraa_uart_sendbreak(m_uart, duration); + } + /** * Set the baudrate. * Takes an int and will attempt to decide what baudrate is diff --git a/include/mock/mock_board_uart.h b/include/mock/mock_board_uart.h index 0ebefa0..fb6da4f 100644 --- a/include/mock/mock_board_uart.h +++ b/include/mock/mock_board_uart.h @@ -42,6 +42,9 @@ mraa_mock_uart_init_raw_replace(mraa_uart_context dev, const char* path); mraa_result_t mraa_mock_uart_flush_replace(mraa_uart_context dev); +mraa_result_t +mraa_mock_uart_sendbreak_replace(mraa_uart_context dev, int duration); + mraa_result_t mraa_mock_uart_set_flowcontrol_replace(mraa_uart_context dev, mraa_boolean_t xonxoff, mraa_boolean_t rtscts); diff --git a/include/mraa_adv_func.h b/include/mraa_adv_func.h index d6b0a56..dbe471d 100644 --- a/include/mraa_adv_func.h +++ b/include/mraa_adv_func.h @@ -113,6 +113,7 @@ typedef struct { mraa_result_t (*uart_init_post) (mraa_uart_context uart); mraa_result_t (*uart_init_raw_replace) (mraa_uart_context dev, const char* path); mraa_result_t (*uart_flush_replace) (mraa_uart_context dev); + mraa_result_t (*uart_sendbreak_replace) (mraa_uart_context dev, int duration); mraa_result_t (*uart_set_baudrate_replace) (mraa_uart_context dev, unsigned int baud); mraa_result_t (*uart_set_mode_replace) (mraa_uart_context dev, int bytesize, mraa_uart_parity_t parity, int stopbits); mraa_result_t (*uart_set_flowcontrol_replace) (mraa_uart_context dev, mraa_boolean_t xonxoff, mraa_boolean_t rtscts); diff --git a/src/mock/mock_board.c b/src/mock/mock_board.c index 91cfa47..9b07681 100644 --- a/src/mock/mock_board.c +++ b/src/mock/mock_board.c @@ -128,6 +128,7 @@ mraa_mock_board() b->adv_func->uart_init_raw_replace = &mraa_mock_uart_init_raw_replace; b->adv_func->uart_set_baudrate_replace = &mraa_mock_uart_set_baudrate_replace; b->adv_func->uart_flush_replace = &mraa_mock_uart_flush_replace; + b->adv_func->uart_sendbreak_replace = &mraa_mock_uart_sendbreak_replace; b->adv_func->uart_set_flowcontrol_replace = &mraa_mock_uart_set_flowcontrol_replace; b->adv_func->uart_set_mode_replace = &mraa_mock_uart_set_mode_replace; b->adv_func->uart_set_non_blocking_replace = &mraa_mock_uart_set_non_blocking_replace; diff --git a/src/mock/mock_board_uart.c b/src/mock/mock_board_uart.c index eb39ec3..9e5f1ea 100644 --- a/src/mock/mock_board_uart.c +++ b/src/mock/mock_board_uart.c @@ -53,6 +53,12 @@ mraa_mock_uart_flush_replace(mraa_uart_context dev) return MRAA_SUCCESS; } +mraa_result_t +mraa_mock_uart_sendbreak_replace(mraa_uart_context dev, int duration) +{ + return MRAA_SUCCESS; +} + mraa_result_t mraa_mock_uart_set_flowcontrol_replace(mraa_uart_context dev, mraa_boolean_t xonxoff, mraa_boolean_t rtscts) { diff --git a/src/peripheralman/peripheralman.c b/src/peripheralman/peripheralman.c index ff37eaf..a75e30d 100644 --- a/src/peripheralman/peripheralman.c +++ b/src/peripheralman/peripheralman.c @@ -137,6 +137,12 @@ mraa_pman_uart_flush_replace(mraa_uart_context dev) return MRAA_ERROR_FEATURE_NOT_IMPLEMENTED; } +static mraa_result_t +mraa_pman_uart_sendbreak_replace(mraa_uart_context dev) +{ + return MRAA_ERROR_FEATURE_NOT_IMPLEMENTED; +} + static int mraa_pman_uart_read_replace(mraa_uart_context dev, char* buf, size_t len) { @@ -854,6 +860,7 @@ mraa_peripheralman_plat_init() b->adv_func->uart_init_raw_replace = &mraa_pman_uart_init_raw_replace; b->adv_func->uart_set_baudrate_replace = &mraa_pman_uart_set_baudrate_replace; b->adv_func->uart_flush_replace = &mraa_pman_uart_flush_replace; + b->adv_func->uart_sendbreak_replace = &mraa_pman_uart_sendbreak_replace; b->adv_func->uart_set_flowcontrol_replace = &mraa_pman_uart_set_flowcontrol_replace; b->adv_func->uart_set_mode_replace = &mraa_pman_uart_set_mode_replace; b->adv_func->uart_set_non_blocking_replace = &mraa_pman_uart_set_non_blocking_replace; diff --git a/src/uart/uart.c b/src/uart/uart.c index 4ce93d6..66bdcbb 100644 --- a/src/uart/uart.c +++ b/src/uart/uart.c @@ -323,6 +323,27 @@ mraa_uart_flush(mraa_uart_context dev) return MRAA_SUCCESS; } +mraa_result_t +mraa_uart_sendbreak(mraa_uart_context dev, int duration) +{ + if (!dev) { + syslog(LOG_ERR, "uart: sendbreak: context is NULL"); + return MRAA_ERROR_INVALID_HANDLE; + } + + if (IS_FUNC_DEFINED(dev, uart_sendbreak_replace)) { + return dev->advance_func->uart_sendbreak_replace(dev, duration); + } + +#if !defined(PERIPHERALMAN) + if (tcsendbreak(dev->fd, duration) == -1) { + return MRAA_ERROR_INVALID_PARAMETER; + } +#endif + + return MRAA_SUCCESS; +} + mraa_result_t mraa_uart_set_baudrate(mraa_uart_context dev, unsigned int baud) { diff --git a/tests/mock/CMakeLists.txt b/tests/mock/CMakeLists.txt index c82ff11..d1192f5 100644 --- a/tests/mock/CMakeLists.txt +++ b/tests/mock/CMakeLists.txt @@ -39,6 +39,7 @@ add_test (NAME py_uart_checks_set_timeout COMMAND ${PYTHON_DEFAULT_EXECUTABLE} $ add_test (NAME py_uart_checks_data_available COMMAND ${PYTHON_DEFAULT_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/uart_checks_data_available.py) add_test (NAME py_uart_checks_write COMMAND ${PYTHON_DEFAULT_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/uart_checks_write.py) add_test (NAME py_uart_checks_read COMMAND ${PYTHON_DEFAULT_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/uart_checks_read.py) +add_test (NAME py_uart_checks_sendbreak COMMAND ${PYTHON_DEFAULT_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/uart_checks_sendbreak.py) set_tests_properties(py_general py_platform @@ -76,4 +77,5 @@ set_tests_properties(py_general py_uart_checks_data_available py_uart_checks_write py_uart_checks_read + py_uart_checks_sendbreak PROPERTIES ENVIRONMENT "PYTHONPATH=${PYTHON_DEFAULT_PYTHONPATH}") diff --git a/tests/mock/uart_checks_sendbreak.py b/tests/mock/uart_checks_sendbreak.py new file mode 100644 index 0000000..0fed9ec --- /dev/null +++ b/tests/mock/uart_checks_sendbreak.py @@ -0,0 +1,43 @@ +#!/usr/bin/env python + +# Author: Jon Trulson +# Copyright (c) 2017 Intel Corporation. +# +# Permission is hereby granted, free of charge, to any person obtaining +# a copy of this software and associated documentation files (the +# "Software"), to deal in the Software without restriction, including +# without limitation the rights to use, copy, modify, merge, publish, +# distribute, sublicense, and/or sell copies of the Software, and to +# permit persons to whom the Software is furnished to do so, subject to +# the following conditions: +# +# The above copyright notice and this permission notice shall be +# included in all copies or substantial portions of the Software. +# +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND +# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE +# LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION +# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION +# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + +import mraa as m +import unittest as u + +from uart_checks_shared import * + +class UartChecksSendbreak(u.TestCase): + def setUp(self): + self.uart = m.Uart(MRAA_UART_DEV_NUM) + + def tearDown(self): + del self.uart + + def test_uart_sendbreak(self): + self.assertEqual(self.uart.sendBreak(0), + m.SUCCESS, + "Running UART sendBreak() did not return success") + +if __name__ == "__main__": + u.main()