diff options
Diffstat (limited to 'testsuites/libtests/i2c01/init.c')
-rw-r--r-- | testsuites/libtests/i2c01/init.c | 152 |
1 files changed, 142 insertions, 10 deletions
diff --git a/testsuites/libtests/i2c01/init.c b/testsuites/libtests/i2c01/init.c index 83027ed548..452bea4ac1 100644 --- a/testsuites/libtests/i2c01/init.c +++ b/testsuites/libtests/i2c01/init.c @@ -1,15 +1,28 @@ +/* SPDX-License-Identifier: BSD-2-Clause */ + /* - * Copyright (c) 2014 embedded brains GmbH. All rights reserved. + * Copyright (c) 2014 embedded brains GmbH & Co. KG * - * embedded brains GmbH - * Dornierstr. 4 - * 82178 Puchheim - * Germany - * <rtems@embedded-brains.de> + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. * - * The license and distribution terms for this file may be - * found in the file LICENSE in this distribution or at - * http://www.rtems.org/license/LICENSE. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. */ #ifdef HAVE_CONFIG_H @@ -336,6 +349,124 @@ static void test_simple_read_write(test_bus *bus, int fd) rtems_test_assert(memcmp(&buf[0], &abc[0], sizeof(buf)) == 0); } +typedef struct { + rtems_id caller; + int fd; +} bus_obtainer_ctx; + +static void bus_obtainer_task(rtems_task_argument arg) +{ + rtems_event_set e = 0; + bus_obtainer_ctx *c = (bus_obtainer_ctx *) arg; + int rv; + rtems_status_code sc; + + rv = ioctl(c->fd, I2C_BUS_OBTAIN); + rtems_test_assert(rv == 0); + + sc = rtems_event_send(c->caller, RTEMS_EVENT_1); + rtems_test_assert(sc == RTEMS_SUCCESSFUL); + sc = rtems_event_receive( + RTEMS_EVENT_1, + RTEMS_EVENT_ANY | RTEMS_WAIT, + 100, + &e + ); + rtems_test_assert(sc == RTEMS_SUCCESSFUL); + + rv = ioctl(c->fd, I2C_BUS_RELEASE); + rtems_test_assert(rv == 0); + + sc = rtems_event_send(c->caller, RTEMS_EVENT_1); + rtems_test_assert(sc == RTEMS_SUCCESSFUL); +} + +static void test_nonblock_read_write(test_bus *bus, int fd) +{ + int flags; + int rv; + char buf[3]; + ssize_t n; + i2c_msg msgs[] = {{ + .addr = 1, + .flags = I2C_M_STOP, + .len = sizeof(buf), + .buf = (uint8_t *) buf, + }}; + struct i2c_rdwr_ioctl_data payload = { + .msgs = msgs, + .nmsgs = sizeof(msgs)/sizeof(msgs[0]), + }; + rtems_id id; + rtems_event_set e = 0; + bus_obtainer_ctx ctx = { + .caller = rtems_task_self(), + .fd = fd, + }; + rtems_status_code sc; + + flags = fcntl(fd, F_GETFL, 0); + rtems_test_assert(flags > 0); + + rv = fcntl(fd, F_SETFL, flags | O_NONBLOCK); + rtems_test_assert(rv != -1); + + sc = rtems_task_create( + rtems_build_name('O', 'B', 'T', 'A'), + 2, + RTEMS_MINIMUM_STACK_SIZE, + RTEMS_DEFAULT_MODES, + RTEMS_DEFAULT_ATTRIBUTES, + &id + ); + rtems_test_assert(sc == RTEMS_SUCCESSFUL); + + sc = rtems_task_start( + id, + bus_obtainer_task, + (rtems_task_argument) &ctx + ); + rtems_test_assert(sc == RTEMS_SUCCESSFUL); + + sc = rtems_event_receive( + RTEMS_EVENT_1, + RTEMS_EVENT_ANY | RTEMS_WAIT, + 100, + &e + ); + rtems_test_assert(sc == RTEMS_SUCCESSFUL); + + errno = 0; + n = read(fd, &buf[0], sizeof(buf)); + rtems_test_assert(n == -1); + rtems_test_assert(errno == EAGAIN); + + errno = 0; + n = write(fd, &buf[0], sizeof(buf)); + rtems_test_assert(n == -1); + rtems_test_assert(errno == EAGAIN); + + errno = 0; + rv = ioctl(fd, I2C_RDWR, &payload); + rtems_test_assert(rv == -1); + rtems_test_assert(errno == EAGAIN); + + sc = rtems_event_send(id, RTEMS_EVENT_1); + rtems_test_assert(sc == RTEMS_SUCCESSFUL); + sc = rtems_event_receive( + RTEMS_EVENT_1, + RTEMS_EVENT_ANY | RTEMS_WAIT, + 100, + &e + ); + rtems_test_assert(sc == RTEMS_SUCCESSFUL); + sc = rtems_task_delete(id); + rtems_test_assert(sc == RTEMS_SUCCESSFUL); + + rv = fcntl(fd, F_SETFL, flags); + rtems_test_assert(rv != -1); +} + static void test_gpio_nxp_pca9535(void) { int rv; @@ -627,6 +758,7 @@ static void test(void) rtems_test_assert(bus->base.timeout == 0); test_simple_read_write(bus, fd); + test_nonblock_read_write(bus, fd); test_eeprom(bus); test_gpio_nxp_pca9535(); test_switch_nxp_pca9548a(); @@ -657,7 +789,7 @@ static void Init(rtems_task_argument arg) #define CONFIGURE_MAXIMUM_FILE_DESCRIPTORS 7 -#define CONFIGURE_MAXIMUM_TASKS 1 +#define CONFIGURE_MAXIMUM_TASKS 2 #define CONFIGURE_MAXIMUM_SEMAPHORES 1 |