Back to home page

LXR

 
 

    


File indexing completed on 2025-05-11 08:24:34

0001 /* SPDX-License-Identifier: BSD-2-Clause */
0002 
0003 /*
0004  *  COPYRIGHT (c) 1989-2012.
0005  *  On-Line Applications Research Corporation (OAR).
0006  *
0007  * Redistribution and use in source and binary forms, with or without
0008  * modification, are permitted provided that the following conditions
0009  * are met:
0010  * 1. Redistributions of source code must retain the above copyright
0011  *    notice, this list of conditions and the following disclaimer.
0012  * 2. Redistributions in binary form must reproduce the above copyright
0013  *    notice, this list of conditions and the following disclaimer in the
0014  *    documentation and/or other materials provided with the distribution.
0015  *
0016  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
0017  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
0018  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
0019  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
0020  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
0021  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
0022  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
0023  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
0024  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
0025  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
0026  * POSSIBILITY OF SUCH DAMAGE.
0027  */
0028 
0029 #ifdef HAVE_CONFIG_H
0030 #include "config.h"
0031 #endif
0032 
0033 #include <tmacros.h>
0034 #include "test_support.h"
0035 #include "termios_testdriver_polled.h"
0036 
0037 #include <sys/types.h>
0038 #include <sys/stat.h>
0039 #include <fcntl.h>
0040 #include <unistd.h>
0041 #include <termios.h>
0042 #include <rtems/dumpbuf.h>
0043 #include <rtems/termiostypes.h>
0044 
0045 const char rtems_test_name[] = "TERMIOS 3";
0046 
0047 /* forward declarations to avoid warnings */
0048 rtems_task Init(rtems_task_argument argument);
0049 void write_helper(int fd, const char *c);
0050 void read_helper(int fd, const char *expected);
0051 void open_it(void);
0052 void close_it(void);
0053 void change_iflag(const char *desc, int mask, int new);
0054 
0055 void write_helper(
0056   int        fd,
0057   const char *c
0058 )
0059 {
0060   size_t   len;
0061   int      rc;
0062   
0063   len = strlen( c );
0064   printf( "Writing: %s", c );
0065 
0066   rc = write( fd, c, len );
0067   rtems_test_assert( rc == len );
0068 
0069   termios_test_driver_dump_tx("Transmitted");
0070 }
0071 
0072 uint8_t read_helper_buffer[256];
0073 
0074 void read_helper(
0075   int         fd,
0076   const char *expected
0077 )
0078 {
0079   int    rc;
0080   size_t len;
0081 
0082   len = strlen( expected );
0083 
0084   termios_test_driver_set_rx( expected, len );
0085   printf( "\nReading (expected):\n" );
0086   rtems_print_buffer( (unsigned char *)expected, len-1 );
0087 
0088   rc = read( fd, read_helper_buffer, sizeof(read_helper_buffer) );
0089   rtems_test_assert( rc != -1 );
0090 
0091   printf( "Read %d bytes from read(2)\n", rc );
0092   rtems_print_buffer( read_helper_buffer, rc );
0093 
0094   termios_test_driver_dump_tx("Echoed");
0095 }
0096 
0097 int Test_fd;
0098 
0099 void open_it(void)
0100 {
0101   /* open the file */
0102   puts( "open(" TERMIOS_TEST_DRIVER_DEVICE_NAME ") - OK " );
0103   Test_fd = open( TERMIOS_TEST_DRIVER_DEVICE_NAME, O_RDWR );
0104   rtems_test_assert( Test_fd != -1 );
0105 }
0106 
0107 void close_it(void)
0108 {
0109   int rc;
0110 
0111   puts( "close(" TERMIOS_TEST_DRIVER_DEVICE_NAME ") - OK " );
0112   rc = close( Test_fd );
0113   rtems_test_assert( rc == 0 );
0114 }
0115 
0116 void change_iflag( const char *desc, int mask, int new )
0117 {
0118   int            rc;
0119   struct termios attr;
0120 
0121   printf( "Changing c_iflag to: %s\n", desc );
0122   rc = tcgetattr( Test_fd, &attr );
0123   rtems_test_assert( rc == 0 );
0124 
0125   attr.c_iflag &= ~mask;
0126   attr.c_iflag |= new;
0127 
0128   rc = tcsetattr( Test_fd, TCSANOW, &attr );
0129   rtems_test_assert( rc == 0 );
0130 }
0131 
0132 const char ExpectedOutput_1[] = "This is test output.\n";
0133 const char ExpectedInput_1[] = "Test input this is.\n";
0134 const char ExpectedInput_2[] = "1235\b456.\n";
0135 const char ExpectedInput_3[] = "tab\ttab.\n";
0136 const char ExpectedInput_4[] = "cr\r.";
0137 const char ExpectedInput_5[] = "aBcDeFgH.\n";
0138 const char ExpectedInput_6[] = "Testing VERASE\177.\n";
0139 const char ExpectedInput_7[] = "Testing VKILL\025.\n";
0140 const char ExpectedInput_8[] = "\177Testing VERASE in column 1.\n";
0141 const char ExpectedInput_9[] = "\t tab \tTesting VKILL after tab.\025\n";
0142 
0143 rtems_task Init(
0144   rtems_task_argument argument
0145 )
0146 {
0147   TEST_BEGIN();
0148 
0149   open_it();
0150 
0151   /* some basic cases */
0152   write_helper( Test_fd, ExpectedOutput_1 );
0153   read_helper( Test_fd, ExpectedInput_1 );
0154   read_helper( Test_fd, ExpectedInput_2 );
0155   read_helper( Test_fd, ExpectedInput_3 );
0156   read_helper( Test_fd, ExpectedInput_4 );
0157 
0158   /* test to lower case input mapping */
0159   read_helper( Test_fd, ExpectedInput_5 );
0160   change_iflag( "Enable to lower case mapping on input", IUCLC, IUCLC );
0161   read_helper( Test_fd, ExpectedInput_5 );
0162   change_iflag( "Disable to lower case mapping on input", IUCLC, 0 );
0163 
0164   read_helper( Test_fd, ExpectedInput_6 );
0165   read_helper( Test_fd, ExpectedInput_7 );
0166   read_helper( Test_fd, ExpectedInput_8 );
0167   read_helper( Test_fd, ExpectedInput_9 );
0168 
0169   puts( "" );
0170   close_it();
0171 
0172   TEST_END();
0173 
0174   rtems_test_exit(0);
0175 }
0176 
0177 /* configuration information */
0178 
0179 #define CONFIGURE_APPLICATION_NEEDS_SIMPLE_CONSOLE_DRIVER
0180 #define CONFIGURE_APPLICATION_NEEDS_CLOCK_DRIVER
0181 #define CONFIGURE_APPLICATION_EXTRA_DRIVERS \
0182   TERMIOS_TEST_DRIVER_TABLE_ENTRY
0183 
0184 /* we need to be able to open the test device */
0185 #define CONFIGURE_MAXIMUM_FILE_DESCRIPTORS 4
0186 #define CONFIGURE_MAXIMUM_TASKS             1
0187 #define CONFIGURE_INITIAL_EXTENSIONS RTEMS_TEST_INITIAL_EXTENSION
0188 
0189 #define CONFIGURE_RTEMS_INIT_TASKS_TABLE
0190 
0191 #define CONFIGURE_INIT
0192 
0193 #include <rtems/confdefs.h>
0194 /* end of file */