From 9f03afbcbcae2de8a68e20f088b4c38426fceaa9 Mon Sep 17 00:00:00 2001 From: Alex Tereschenko Date: Sun, 16 Oct 2016 16:57:10 +0200 Subject: [PATCH] mock: implement UART functionality Signed-off-by: Alex Tereschenko Signed-off-by: Brendan Le Foll --- docs/mock.md | 4 + include/mock/mock_board.h | 2 +- include/mock/mock_board_uart.h | 68 +++++++++++++++ src/CMakeLists.txt | 1 + src/mock/mock_board.c | 32 +++++++ src/mock/mock_board_uart.c | 100 ++++++++++++++++++++++ src/uart/uart.c | 2 +- tests/mock/CMakeLists.txt | 19 ++++ tests/mock/platform_checks.py | 2 +- tests/mock/uart_checks_data_available.py | 43 ++++++++++ tests/mock/uart_checks_flush.py | 43 ++++++++++ tests/mock/uart_checks_read.py | 52 +++++++++++ tests/mock/uart_checks_set_baudrate.py | 50 +++++++++++ tests/mock/uart_checks_set_flowcontrol.py | 43 ++++++++++ tests/mock/uart_checks_set_mode.py | 43 ++++++++++ tests/mock/uart_checks_set_nonblocking.py | 43 ++++++++++ tests/mock/uart_checks_set_timeout.py | 43 ++++++++++ tests/mock/uart_checks_shared.py | 27 ++++++ tests/mock/uart_checks_write.py | 52 +++++++++++ 19 files changed, 666 insertions(+), 3 deletions(-) create mode 100644 include/mock/mock_board_uart.h create mode 100644 src/mock/mock_board_uart.c create mode 100644 tests/mock/uart_checks_data_available.py create mode 100644 tests/mock/uart_checks_flush.py create mode 100644 tests/mock/uart_checks_read.py create mode 100644 tests/mock/uart_checks_set_baudrate.py create mode 100644 tests/mock/uart_checks_set_flowcontrol.py create mode 100644 tests/mock/uart_checks_set_mode.py create mode 100644 tests/mock/uart_checks_set_nonblocking.py create mode 100644 tests/mock/uart_checks_set_timeout.py create mode 100644 tests/mock/uart_checks_shared.py create mode 100644 tests/mock/uart_checks_write.py diff --git a/docs/mock.md b/docs/mock.md index 6c71af9..37c9847 100644 --- a/docs/mock.md +++ b/docs/mock.md @@ -23,6 +23,8 @@ registers or 5 single-word ones or a mix thereof. data items (words or bytes) are calculated from the sent ones using `sent_byte (or word) XOR constant` formula. See [SPI mock header](../include/mock/mock_board_spi.h#L38-L39) for constant values. +* Single UART port. All functions are supported, but many are simple stubs. Write +always succeeds, read returns 'Z' symbol as many times as `read()` requested. We plan to develop it further and all [contributions](../CONTRIBUTING.md) are more than welcome. @@ -38,6 +40,8 @@ See the table below for pin layout and features | 5 | SPI0MOSI | MOSI pin for SPI0 bus | | 6 | SPI0MISO | MISO pin for SPI0 bus | | 7 | SPI0SCLK | SCLK pin for SPI0 bus | +| 8 | UART0RX | RX pin for UART0 port | +| 9 | UART0TX | TX pin for UART0 port | Building -------- diff --git a/include/mock/mock_board.h b/include/mock/mock_board.h index b6e8a14..41b0fe3 100644 --- a/include/mock/mock_board.h +++ b/include/mock/mock_board.h @@ -30,7 +30,7 @@ extern "C" { #include "mraa_internal.h" -#define MRAA_MOCK_PINCOUNT 8 +#define MRAA_MOCK_PINCOUNT 10 mraa_board_t* mraa_mock_board(); diff --git a/include/mock/mock_board_uart.h b/include/mock/mock_board_uart.h new file mode 100644 index 0000000..0ebefa0 --- /dev/null +++ b/include/mock/mock_board_uart.h @@ -0,0 +1,68 @@ +/* + * Author: Alex Tereschenko + * Copyright (c) 2016 Alex Tereschenko. + * + * 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. + */ + +#pragma once + +#ifdef __cplusplus +extern "C" { +#endif + +#include "mraa_internal.h" + +// ASCII code for "Z", used as a basis for our mock reads +#define MOCK_UART_DATA_BYTE 0x5A + +mraa_result_t +mraa_mock_uart_set_baudrate_replace(mraa_uart_context dev, unsigned int baud); + +mraa_result_t +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_set_flowcontrol_replace(mraa_uart_context dev, mraa_boolean_t xonxoff, mraa_boolean_t rtscts); + +mraa_result_t +mraa_mock_uart_set_mode_replace(mraa_uart_context dev, int bytesize, mraa_uart_parity_t parity, int stopbits); + +mraa_result_t +mraa_mock_uart_set_non_blocking_replace(mraa_uart_context dev, mraa_boolean_t nonblock); + +mraa_result_t +mraa_mock_uart_set_timeout_replace(mraa_uart_context dev, int read, int write, int interchar); + +mraa_boolean_t +mraa_mock_uart_data_available_replace(mraa_uart_context dev, unsigned int millis); + +int +mraa_mock_uart_write_replace(mraa_uart_context dev, const char* buf, size_t len); + +int +mraa_mock_uart_read_replace(mraa_uart_context dev, char* buf, size_t len); + +#ifdef __cplusplus +} +#endif diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 0d320f1..a505ca9 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -91,6 +91,7 @@ set (mraa_LIB_MOCK_SRCS_NOAUTO ${PROJECT_SOURCE_DIR}/src/mock/mock_board_aio.c ${PROJECT_SOURCE_DIR}/src/mock/mock_board_i2c.c ${PROJECT_SOURCE_DIR}/src/mock/mock_board_spi.c + ${PROJECT_SOURCE_DIR}/src/mock/mock_board_uart.c ) if (JSONPLAT) diff --git a/src/mock/mock_board.c b/src/mock/mock_board.c index 56f4008..91cfa47 100644 --- a/src/mock/mock_board.c +++ b/src/mock/mock_board.c @@ -31,8 +31,10 @@ #include "mock/mock_board_aio.h" #include "mock/mock_board_i2c.h" #include "mock/mock_board_spi.h" +#include "mock/mock_board_uart.h" #define PLATFORM_NAME "MRAA mock platform" +#define UART_DEV_PATH "dummy" mraa_board_t* mraa_mock_board() @@ -69,6 +71,12 @@ mraa_mock_board() b->pwm_max_period = 0; b->pwm_min_period = 0; + b->uart_dev_count = 1; + b->def_uart_dev = 0; + b->uart_dev[0].rx = 8; + b->uart_dev[0].tx = 9; + b->uart_dev[0].device_path = UART_DEV_PATH; + b->pins = (mraa_pininfo_t*) malloc(sizeof(mraa_pininfo_t) * MRAA_MOCK_PINCOUNT); if (b->pins == NULL) { goto error; @@ -117,6 +125,16 @@ mraa_mock_board() b->adv_func->spi_write_word_replace = &mraa_mock_spi_write_word_replace; b->adv_func->spi_transfer_buf_replace = &mraa_mock_spi_transfer_buf_replace; b->adv_func->spi_transfer_buf_word_replace = &mraa_mock_spi_transfer_buf_word_replace; + 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_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; + b->adv_func->uart_set_timeout_replace = &mraa_mock_uart_set_timeout_replace; + b->adv_func->uart_data_available_replace = &mraa_mock_uart_data_available_replace; + b->adv_func->uart_write_replace = &mraa_mock_uart_write_replace; + b->adv_func->uart_read_replace = &mraa_mock_uart_read_replace; // Pin definitions int pos = 0; @@ -169,6 +187,20 @@ mraa_mock_board() b->pins[pos].spi.pinmap = 0; pos++; + strncpy(b->pins[pos].name, "UART0RX", 8); + b->pins[pos].capabilities = (mraa_pincapabilities_t){ 1, 0, 0, 0, 0, 0, 0, 1 }; + b->pins[pos].uart.pinmap = 0; + b->pins[pos].uart.parent_id = 0; + b->pins[pos].uart.mux_total = 0; + pos++; + + strncpy(b->pins[pos].name, "UART0TX", 8); + b->pins[pos].capabilities = (mraa_pincapabilities_t){ 1, 0, 0, 0, 0, 0, 0, 1 }; + b->pins[pos].uart.pinmap = 0; + b->pins[pos].uart.parent_id = 0; + b->pins[pos].uart.mux_total = 0; + pos++; + return b; error: diff --git a/src/mock/mock_board_uart.c b/src/mock/mock_board_uart.c new file mode 100644 index 0000000..eb39ec3 --- /dev/null +++ b/src/mock/mock_board_uart.c @@ -0,0 +1,100 @@ +/* + * Author: Alex Tereschenko + * Copyright (c) 2016 Alex Tereschenko. + * + * 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. + */ + +#include +#include + +#include "common.h" +#include "mock/mock_board_uart.h" + +mraa_result_t +mraa_mock_uart_set_baudrate_replace(mraa_uart_context dev, unsigned int baud) +{ + // Limits are taken from uart.c::uint2speed(), they don't matter much anyway + if ((baud < 0) || (baud > 4000000)) { + syslog(LOG_ERR, "uart%i: set_baudrate: invalid baudrate: %i", dev->index, baud); + return MRAA_ERROR_INVALID_PARAMETER; + } + + return MRAA_SUCCESS; +} + +mraa_result_t +mraa_mock_uart_init_raw_replace(mraa_uart_context dev, const char* path) +{ + // The only thing we have to do from the original uart_init_raw() + return mraa_uart_set_baudrate(dev, 9600); +} + +mraa_result_t +mraa_mock_uart_flush_replace(mraa_uart_context dev) +{ + return MRAA_SUCCESS; +} + +mraa_result_t +mraa_mock_uart_set_flowcontrol_replace(mraa_uart_context dev, mraa_boolean_t xonxoff, mraa_boolean_t rtscts) +{ + return MRAA_SUCCESS; +} + +mraa_result_t +mraa_mock_uart_set_mode_replace(mraa_uart_context dev, int bytesize, mraa_uart_parity_t parity, int stopbits) +{ + return MRAA_SUCCESS; +} + +mraa_result_t +mraa_mock_uart_set_non_blocking_replace(mraa_uart_context dev, mraa_boolean_t nonblock) +{ + return MRAA_SUCCESS; +} + +mraa_result_t +mraa_mock_uart_set_timeout_replace(mraa_uart_context dev, int read, int write, int interchar) +{ + return MRAA_SUCCESS; +} + +mraa_boolean_t +mraa_mock_uart_data_available_replace(mraa_uart_context dev, unsigned int millis) +{ + // Our mock implementation will always have "incoming" data + return 1; +} + +int +mraa_mock_uart_write_replace(mraa_uart_context dev, const char* buf, size_t len) +{ + // Our mock implementation always succeeds when sending data + return len; +} + +int +mraa_mock_uart_read_replace(mraa_uart_context dev, char* buf, size_t len) +{ + // We'll return MOCK_UART_DATA_BYTE, len times + memset(buf, MOCK_UART_DATA_BYTE, len); + return len; +} diff --git a/src/uart/uart.c b/src/uart/uart.c index 97153e7..d41a7a8 100644 --- a/src/uart/uart.c +++ b/src/uart/uart.c @@ -261,7 +261,7 @@ mraa_uart_init_raw(const char* path) init_raw_cleanup: if (status != MRAA_SUCCESS) { if (dev != NULL) { - if (dev->fd != -1) { + if (dev->fd >= 0) { close(dev->fd); } free(dev); diff --git a/tests/mock/CMakeLists.txt b/tests/mock/CMakeLists.txt index 98dd57c..c82ff11 100644 --- a/tests/mock/CMakeLists.txt +++ b/tests/mock/CMakeLists.txt @@ -30,6 +30,16 @@ add_test (NAME py_spi_checks_write_byte COMMAND ${PYTHON_DEFAULT_EXECUTABLE} ${C add_test (NAME py_spi_checks_write_word COMMAND ${PYTHON_DEFAULT_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/spi_checks_write_word.py) add_test (NAME py_spi_checks_write COMMAND ${PYTHON_DEFAULT_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/spi_checks_write.py) +add_test (NAME py_uart_checks_set_baudrate COMMAND ${PYTHON_DEFAULT_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/uart_checks_set_baudrate.py) +add_test (NAME py_uart_checks_flush COMMAND ${PYTHON_DEFAULT_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/uart_checks_flush.py) +add_test (NAME py_uart_checks_set_flowcontrol COMMAND ${PYTHON_DEFAULT_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/uart_checks_set_flowcontrol.py) +add_test (NAME py_uart_checks_set_mode COMMAND ${PYTHON_DEFAULT_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/uart_checks_set_mode.py) +add_test (NAME py_uart_checks_set_nonblocking COMMAND ${PYTHON_DEFAULT_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/uart_checks_set_nonblocking.py) +add_test (NAME py_uart_checks_set_timeout COMMAND ${PYTHON_DEFAULT_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/uart_checks_set_timeout.py) +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) + set_tests_properties(py_general py_platform py_gpio_basic @@ -57,4 +67,13 @@ set_tests_properties(py_general py_spi_checks_write_byte py_spi_checks_write_word py_spi_checks_write + py_uart_checks_set_baudrate + py_uart_checks_flush + py_uart_checks_set_flowcontrol + py_uart_checks_set_mode + py_uart_checks_set_nonblocking + py_uart_checks_set_timeout + py_uart_checks_data_available + py_uart_checks_write + py_uart_checks_read PROPERTIES ENVIRONMENT "PYTHONPATH=${PYTHON_DEFAULT_PYTHONPATH}") diff --git a/tests/mock/platform_checks.py b/tests/mock/platform_checks.py index dce6742..eceb824 100755 --- a/tests/mock/platform_checks.py +++ b/tests/mock/platform_checks.py @@ -27,7 +27,7 @@ import mraa as m import unittest as u -PLATFORM_PINCOUNT = 8 +PLATFORM_PINCOUNT = 10 PLATFORM_STD_ADC_RES_BITS = 10 PLATFORM_MAX_ADC_RES_BITS = 12 diff --git a/tests/mock/uart_checks_data_available.py b/tests/mock/uart_checks_data_available.py new file mode 100644 index 0000000..444cb47 --- /dev/null +++ b/tests/mock/uart_checks_data_available.py @@ -0,0 +1,43 @@ +#!/usr/bin/env python + +# Author: Alex Tereschenko +# Copyright (c) 2016 Alex Tereschenko. +# +# 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 UartChecksDataAvailable(u.TestCase): + def setUp(self): + self.uart = m.Uart(MRAA_UART_DEV_NUM) + + def tearDown(self): + del self.uart + + def test_uart_data_available(self): + self.assertEqual(self.uart.dataAvailable(10), + True, + "Running UART dataAvailable() did not return True") + +if __name__ == "__main__": + u.main() diff --git a/tests/mock/uart_checks_flush.py b/tests/mock/uart_checks_flush.py new file mode 100644 index 0000000..351e9fb --- /dev/null +++ b/tests/mock/uart_checks_flush.py @@ -0,0 +1,43 @@ +#!/usr/bin/env python + +# Author: Alex Tereschenko +# Copyright (c) 2016 Alex Tereschenko. +# +# 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 UartChecksFlush(u.TestCase): + def setUp(self): + self.uart = m.Uart(MRAA_UART_DEV_NUM) + + def tearDown(self): + del self.uart + + def test_uart_flush(self): + self.assertEqual(self.uart.flush(), + m.SUCCESS, + "Running UART flush() did not return success") + +if __name__ == "__main__": + u.main() diff --git a/tests/mock/uart_checks_read.py b/tests/mock/uart_checks_read.py new file mode 100644 index 0000000..4ca3c9d --- /dev/null +++ b/tests/mock/uart_checks_read.py @@ -0,0 +1,52 @@ +#!/usr/bin/env python + +# Author: Alex Tereschenko +# Copyright (c) 2016 Alex Tereschenko. +# +# 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 UartChecksRead(u.TestCase): + def setUp(self): + self.uart = m.Uart(MRAA_UART_DEV_NUM) + + def tearDown(self): + del self.uart + + def test_uart_read(self): + TEST_DATA_LEN = 10 + EXPECTED_RESULT = bytearray([MOCK_UART_DATA_BYTE for x in range(TEST_DATA_LEN)]) + self.assertEqual(self.uart.read(TEST_DATA_LEN), + EXPECTED_RESULT, + "Running UART read(%d) did not return %s" % (TEST_DATA_LEN, repr(EXPECTED_RESULT))) + + def test_uart_readStr(self): + TEST_DATA_LEN = 10 + EXPECTED_RESULT = chr(MOCK_UART_DATA_BYTE) * TEST_DATA_LEN + self.assertEqual(self.uart.readStr(TEST_DATA_LEN), + EXPECTED_RESULT, + "Running UART readStr(%d) did not return %s" % (TEST_DATA_LEN, EXPECTED_RESULT)) + +if __name__ == "__main__": + u.main() diff --git a/tests/mock/uart_checks_set_baudrate.py b/tests/mock/uart_checks_set_baudrate.py new file mode 100644 index 0000000..a1f220b --- /dev/null +++ b/tests/mock/uart_checks_set_baudrate.py @@ -0,0 +1,50 @@ +#!/usr/bin/env python + +# Author: Alex Tereschenko +# Copyright (c) 2016 Alex Tereschenko. +# +# 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 UartChecksSetBaudrate(u.TestCase): + def setUp(self): + self.uart = m.Uart(MRAA_UART_DEV_NUM) + + def tearDown(self): + del self.uart + + def test_uart_baudrate_115200(self): + TEST_BAUDRATE = 115200 + self.assertEqual(self.uart.setBaudRate(TEST_BAUDRATE), + m.SUCCESS, + "Setting baudrate to %d did not return success" % TEST_BAUDRATE) + + def test_uart_baudrate_invalid_bigger_than_max(self): + TEST_BAUDRATE = 10000000 + self.assertEqual(self.uart.setBaudRate(TEST_BAUDRATE), + m.ERROR_INVALID_PARAMETER, + "Setting baudrate to %d did not return INVALID_PARAMETER" % TEST_BAUDRATE) + +if __name__ == "__main__": + u.main() diff --git a/tests/mock/uart_checks_set_flowcontrol.py b/tests/mock/uart_checks_set_flowcontrol.py new file mode 100644 index 0000000..528e431 --- /dev/null +++ b/tests/mock/uart_checks_set_flowcontrol.py @@ -0,0 +1,43 @@ +#!/usr/bin/env python + +# Author: Alex Tereschenko +# Copyright (c) 2016 Alex Tereschenko. +# +# 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 UartChecksSetFlowControl(u.TestCase): + def setUp(self): + self.uart = m.Uart(MRAA_UART_DEV_NUM) + + def tearDown(self): + del self.uart + + def test_uart_set_flowcontrol(self): + self.assertEqual(self.uart.setFlowcontrol(False, True), + m.SUCCESS, + "Running UART setFlowControl() did not return success") + +if __name__ == "__main__": + u.main() diff --git a/tests/mock/uart_checks_set_mode.py b/tests/mock/uart_checks_set_mode.py new file mode 100644 index 0000000..1f515d2 --- /dev/null +++ b/tests/mock/uart_checks_set_mode.py @@ -0,0 +1,43 @@ +#!/usr/bin/env python + +# Author: Alex Tereschenko +# Copyright (c) 2016 Alex Tereschenko. +# +# 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 UartChecksSetMode(u.TestCase): + def setUp(self): + self.uart = m.Uart(MRAA_UART_DEV_NUM) + + def tearDown(self): + del self.uart + + def test_uart_set_mode(self): + self.assertEqual(self.uart.setMode(8, m.UART_PARITY_NONE, 1), + m.SUCCESS, + "Running UART setMode() did not return success") + +if __name__ == "__main__": + u.main() diff --git a/tests/mock/uart_checks_set_nonblocking.py b/tests/mock/uart_checks_set_nonblocking.py new file mode 100644 index 0000000..0e95b40 --- /dev/null +++ b/tests/mock/uart_checks_set_nonblocking.py @@ -0,0 +1,43 @@ +#!/usr/bin/env python + +# Author: Alex Tereschenko +# Copyright (c) 2016 Alex Tereschenko. +# +# 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 UartChecksSetNonBlocking(u.TestCase): + def setUp(self): + self.uart = m.Uart(MRAA_UART_DEV_NUM) + + def tearDown(self): + del self.uart + + def test_uart_set_nonblocking(self): + self.assertEqual(self.uart.setNonBlocking(True), + m.SUCCESS, + "Running UART setNonBlocking() did not return success") + +if __name__ == "__main__": + u.main() diff --git a/tests/mock/uart_checks_set_timeout.py b/tests/mock/uart_checks_set_timeout.py new file mode 100644 index 0000000..e139e2c --- /dev/null +++ b/tests/mock/uart_checks_set_timeout.py @@ -0,0 +1,43 @@ +#!/usr/bin/env python + +# Author: Alex Tereschenko +# Copyright (c) 2016 Alex Tereschenko. +# +# 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 UartChecksSetTimeout(u.TestCase): + def setUp(self): + self.uart = m.Uart(MRAA_UART_DEV_NUM) + + def tearDown(self): + del self.uart + + def test_uart_set_timeout(self): + self.assertEqual(self.uart.setTimeout(10, 10, 10), + m.SUCCESS, + "Running UART setTimeout() did not return success") + +if __name__ == "__main__": + u.main() diff --git a/tests/mock/uart_checks_shared.py b/tests/mock/uart_checks_shared.py new file mode 100644 index 0000000..e35a39f --- /dev/null +++ b/tests/mock/uart_checks_shared.py @@ -0,0 +1,27 @@ +#!/usr/bin/env python + +# Author: Alex Tereschenko +# Copyright (c) 2016 Alex Tereschenko. +# +# 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. + +MRAA_UART_DEV_NUM = 0 +# This one is defined in mock_board_uart.h +MOCK_UART_DATA_BYTE = 0x5A diff --git a/tests/mock/uart_checks_write.py b/tests/mock/uart_checks_write.py new file mode 100644 index 0000000..0883de8 --- /dev/null +++ b/tests/mock/uart_checks_write.py @@ -0,0 +1,52 @@ +#!/usr/bin/env python + +# Author: Alex Tereschenko +# Copyright (c) 2016 Alex Tereschenko. +# +# 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 UartChecksWrite(u.TestCase): + def setUp(self): + self.uart = m.Uart(MRAA_UART_DEV_NUM) + + def tearDown(self): + del self.uart + + def test_uart_write(self): + TEST_DATA_LEN = 10 + TEST_DATA = bytearray([x for x in range(TEST_DATA_LEN)]) + self.assertEqual(self.uart.write(TEST_DATA), + TEST_DATA_LEN, + "Running UART write(%s) did not return %d" % (repr(TEST_DATA), TEST_DATA_LEN)) + + def test_uart_writeStr(self): + TEST_DATA = "Hello" + TEST_DATA_LEN = len(TEST_DATA) + self.assertEqual(self.uart.writeStr(TEST_DATA), + TEST_DATA_LEN, + "Running UART writeStr(%s) did not return %d" % (TEST_DATA, TEST_DATA_LEN)) + +if __name__ == "__main__": + u.main()