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  *  @file
0005  *
0006  *  This file contains a test fixture termios device driver.
0007  */
0008 
0009 /*
0010  *  COPYRIGHT (c) 1989-2012.
0011  *  On-Line Applications Research Corporation (OAR).
0012  *
0013  * Redistribution and use in source and binary forms, with or without
0014  * modification, are permitted provided that the following conditions
0015  * are met:
0016  * 1. Redistributions of source code must retain the above copyright
0017  *    notice, this list of conditions and the following disclaimer.
0018  * 2. Redistributions in binary form must reproduce the above copyright
0019  *    notice, this list of conditions and the following disclaimer in the
0020  *    documentation and/or other materials provided with the distribution.
0021  *
0022  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
0023  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
0024  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
0025  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
0026  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
0027  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
0028  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
0029  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
0030  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
0031  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
0032  * POSSIBILITY OF SUCH DAMAGE.
0033  */
0034 
0035 #ifdef HAVE_CONFIG_H
0036 #include "config.h"
0037 #endif
0038 
0039 #include "tmacros.h"
0040 #include <rtems/libio.h>
0041 #include <stdlib.h>
0042 #include <termios.h>
0043 #include <rtems/termiostypes.h>
0044 #include <rtems/libcsupport.h>
0045 #include <rtems/malloc.h>
0046 #include "termios_testdriver.h"
0047 
0048 /* forward declarations to avoid warnings */
0049 int termios_test_driver_inbyte_nonblocking(int port);
0050 void termios_test_driver_outbyte_polled(int port, char ch);
0051 ssize_t termios_test_driver_write_support(
0052   int         minor,
0053   const char *buf,
0054   size_t      len
0055 );
0056 int termios_test_driver_set_attributes(
0057   int                   minor,
0058   const struct termios *t
0059 );
0060 
0061 int termios_test_driver_inbyte_nonblocking( int port )
0062 {
0063   return -1;
0064 }
0065 
0066 void termios_test_driver_outbyte_polled(
0067   int  port,
0068   char ch
0069 )
0070 {
0071 }
0072 
0073 ssize_t termios_test_driver_write_support(
0074   int         minor,
0075   const char *buf,
0076   size_t      len
0077 )
0078 {
0079   size_t nwrite = 0;
0080 
0081   while (nwrite < len) {
0082 #if (TERMIOS_TEST_DRIVER_USE_INTERRUPTS)
0083     termios_test_driver_outbyte_interrupt( minor, *buf++ );
0084 #else
0085     termios_test_driver_outbyte_polled( minor, *buf++ );
0086 #endif
0087     nwrite++;
0088   }
0089   return nwrite;
0090 }
0091 
0092 
0093 /*
0094  *  Set Attributes Handler
0095  */
0096 int termios_test_driver_set_attributes(
0097   int                   minor,
0098   const struct termios *t
0099 )
0100 {
0101   int                    baud_requested;
0102   int                    number;
0103   const char            *parity = "NONE";
0104   const char            *char_size = "5";
0105   const char            *stop = "NONE";
0106 
0107   baud_requested = t->c_ispeed;
0108 
0109   number = rtems_termios_baud_to_number( baud_requested );
0110 
0111   /*
0112    *  Parity
0113    */
0114   if (t->c_cflag & PARENB) {
0115     parity = "EVEN";
0116     if (!(t->c_cflag & PARODD))
0117       parity = "ODD";
0118   }
0119 
0120   /*
0121    *  Character Size
0122    */
0123   if (t->c_cflag & CSIZE) {
0124     switch (t->c_cflag & CSIZE) {
0125       case CS5:  char_size = "5"; break;
0126       case CS6:  char_size = "6"; break;
0127       case CS7:  char_size = "7"; break;
0128       case CS8:  char_size = "8"; break;
0129     }
0130   }
0131 
0132   /*
0133    *  Stop Bits
0134    */
0135   if (t->c_cflag & CSTOPB)
0136     stop = "2";
0137   else
0138     stop = "1";
0139 
0140   printf(
0141     "set_attributes - B%d %s-%s-%s\n",
0142     number,
0143     char_size,
0144     parity,
0145     stop
0146   );
0147   return 0;
0148 }
0149 
0150 /*
0151  *  Test Device Driver Entry Points
0152  */
0153 rtems_device_driver termios_test_driver_initialize(
0154   rtems_device_major_number  major,
0155   rtems_device_minor_number  minor,
0156   void                      *arg
0157 )
0158 {
0159   rtems_status_code sc;
0160 
0161   rtems_termios_initialize();
0162 
0163   /*
0164    *  Register Device Names
0165    */
0166   puts(
0167     "Termios_test_driver - rtems_io_register "
0168       TERMIOS_TEST_DRIVER_DEVICE_NAME " - OK"
0169   );
0170   sc = rtems_io_register_name( TERMIOS_TEST_DRIVER_DEVICE_NAME, major, 0 );
0171   directive_failed( sc, "rtems_io_register_name" );
0172 
0173   return RTEMS_SUCCESSFUL;
0174 }
0175 
0176 rtems_device_driver termios_test_driver_open(
0177   rtems_device_major_number major,
0178   rtems_device_minor_number minor,
0179   void                    * arg
0180 )
0181 {
0182   rtems_status_code sc;
0183   int               rc;
0184   rtems_libio_open_close_args_t *args = arg;
0185   static bool firstCall = true;
0186 
0187   static const rtems_termios_callbacks Callbacks = {
0188     NULL,                                    /* firstOpen */
0189     NULL,                                    /* lastClose */
0190     termios_test_driver_inbyte_nonblocking,  /* pollRead */
0191     termios_test_driver_write_support,       /* write */
0192     termios_test_driver_set_attributes,      /* setAttributes */
0193     NULL,                                    /* stopRemoteTx */
0194     NULL,                                    /* startRemoteTx */
0195     TERMIOS_POLLED                           /* outputUsesInterrupts */
0196   };
0197 
0198   if ( minor > 2 ) {
0199     puts( "ERROR - Termios_testdriver - only 1 minor supported" );
0200     rtems_test_exit(0);
0201   }
0202 
0203   if( firstCall ) {
0204     static const uintptr_t allocSizes [] = {
0205       sizeof( struct rtems_termios_tty ),
0206       128,
0207       64,
0208       256
0209     };
0210 
0211     size_t i;
0212 
0213     firstCall = false;
0214 
0215     for (i = 0; i < sizeof( allocSizes ) / sizeof( allocSizes [0] ); ++i) {
0216       void *opaque = rtems_heap_greedy_allocate( allocSizes, i );
0217 
0218       sc = rtems_termios_open( major, minor, arg, &Callbacks );
0219       rtems_test_assert( sc == RTEMS_NO_MEMORY );
0220 
0221       rtems_heap_greedy_free( opaque );
0222     }
0223   }
0224   
0225   sc = rtems_termios_open (major, minor, arg, &Callbacks);
0226   directive_failed( sc, "rtems_termios_open" );
0227 
0228   puts( "Termios_test_driver - rtems_set_initial_baud - bad baud - OK" );
0229   rc = rtems_termios_set_initial_baud( args->iop->data1, 5000 );
0230   if ( rc != -1 ) {
0231     printf( "ERROR - return %d\n", rc );
0232     rtems_test_exit(0);
0233   }
0234 
0235   puts( "Termios_test_driver - rtems_set_initial_baud - 38400 - OK" );
0236   rc = rtems_termios_set_initial_baud( args->iop->data1, 38400 );
0237   if ( rc ) {
0238     printf( "ERROR - return %d\n", rc );
0239     rtems_test_exit(0);
0240   }
0241 
0242   return RTEMS_SUCCESSFUL;
0243 }
0244 
0245 rtems_device_driver termios_test_driver_close(
0246   rtems_device_major_number major,
0247   rtems_device_minor_number minor,
0248   void                    * arg
0249 )
0250 {
0251   return rtems_termios_close (arg);
0252 }
0253 
0254 rtems_device_driver termios_test_driver_read(
0255   rtems_device_major_number major,
0256   rtems_device_minor_number minor,
0257   void                    * arg
0258 )
0259 {
0260   return rtems_termios_read (arg);
0261 }
0262 
0263 rtems_device_driver termios_test_driver_write(
0264   rtems_device_major_number major,
0265   rtems_device_minor_number minor,
0266   void                    * arg
0267 )
0268 {
0269   return rtems_termios_write (arg);
0270 }
0271 
0272 rtems_device_driver termios_test_driver_control(
0273   rtems_device_major_number major,
0274   rtems_device_minor_number minor,
0275   void                    * arg
0276 )
0277 {
0278   return rtems_termios_ioctl (arg);
0279 }