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/dumpbuf.h>
0045 #include "termios_testdriver_polled.h"
0046 
0047 /* forward declarations to avoid warnings */
0048 int termios_test_driver_inbyte_nonblocking(int port);
0049 void termios_test_driver_outbyte_polled(int port, char ch);
0050 ssize_t termios_test_driver_write_support(
0051   int         minor,
0052   const char *buf,
0053   size_t      len
0054 );
0055 int termios_test_driver_set_attributes(
0056   int                   minor,
0057   const struct termios *t
0058 );
0059 
0060 #define TX_MAX 1024
0061 uint8_t Tx_Buffer[TX_MAX];
0062 int     Tx_Index = 0;
0063 
0064 void termios_test_driver_dump_tx(const char *c)
0065 {
0066   printf( "%s %d characters\n", c, Tx_Index );
0067   rtems_print_buffer( Tx_Buffer, Tx_Index );
0068   Tx_Index = 0;
0069 }
0070   
0071 const uint8_t *Rx_Buffer;
0072 int            Rx_Index;
0073 int            Rx_Length;
0074 bool           Rx_FirstTime = true;
0075 
0076 void termios_test_driver_set_rx(
0077   const void *p,
0078   size_t      len
0079 )
0080 {
0081   Rx_Buffer = p;
0082   Rx_Length = len;
0083   Rx_Index  = 0;
0084 }
0085 
0086 int termios_test_driver_inbyte_nonblocking( int port )
0087 {
0088   if ( Rx_FirstTime == true ) {
0089     Rx_FirstTime = false;
0090     return -1;
0091   }
0092   if ( Rx_Index >= Rx_Length )
0093     return -1;
0094   return Rx_Buffer[ Rx_Index++ ];
0095 }
0096 
0097 void termios_test_driver_outbyte_polled(
0098   int  port,
0099   char ch
0100 )
0101 {
0102   Tx_Buffer[Tx_Index++] = (uint8_t) ch;
0103 }
0104 
0105 ssize_t termios_test_driver_write_support (int minor, const char *buf, size_t len)
0106 {
0107   size_t nwrite = 0;
0108 
0109   while (nwrite < len) {
0110     termios_test_driver_outbyte_polled( minor, *buf++ );
0111     nwrite++;
0112   }
0113   return nwrite;
0114 }
0115 
0116 
0117 /*
0118  *  Set Attributes Handler
0119  */
0120 int termios_test_driver_set_attributes(
0121   int                   minor,
0122   const struct termios *t
0123 )
0124 {
0125   return 0;
0126 }
0127 
0128 /*
0129  *  Test Device Driver Entry Points
0130  */
0131 rtems_device_driver termios_test_driver_initialize(
0132   rtems_device_major_number  major,
0133   rtems_device_minor_number  minor,
0134   void                      *arg
0135 )
0136 {
0137   rtems_termios_initialize();
0138 
0139   /*
0140    *  Register Device Names
0141    */
0142   (void) rtems_io_register_name( TERMIOS_TEST_DRIVER_DEVICE_NAME, major, 0 );
0143 
0144   return RTEMS_SUCCESSFUL;
0145 }
0146 
0147 rtems_device_driver termios_test_driver_open(
0148   rtems_device_major_number major,
0149   rtems_device_minor_number minor,
0150   void                    * arg
0151 )
0152 {
0153   rtems_status_code sc;
0154   static const rtems_termios_callbacks Callbacks = {
0155     NULL,                                    /* firstOpen */
0156     NULL,                                    /* lastClose */
0157     termios_test_driver_inbyte_nonblocking,  /* pollRead */
0158     termios_test_driver_write_support,       /* write */
0159     termios_test_driver_set_attributes,      /* setAttributes */
0160     NULL,                                    /* stopRemoteTx */
0161     NULL,                                    /* startRemoteTx */
0162     TERMIOS_POLLED                           /* outputUsesInterrupts */
0163   };
0164 
0165   if ( minor > 2 ) {
0166     puts( "ERROR - Termios_testdriver - only 1 minor supported" );
0167     rtems_test_exit(0);
0168   }
0169 
0170   sc = rtems_termios_open (major, minor, arg, &Callbacks);
0171   directive_failed( sc, "termios open" ); 
0172 
0173   return RTEMS_SUCCESSFUL;
0174 }
0175 
0176 rtems_device_driver termios_test_driver_close(
0177   rtems_device_major_number major,
0178   rtems_device_minor_number minor,
0179   void                    * arg
0180 )
0181 {
0182   return rtems_termios_close (arg);
0183 }
0184 
0185 rtems_device_driver termios_test_driver_read(
0186   rtems_device_major_number major,
0187   rtems_device_minor_number minor,
0188   void                    * arg
0189 )
0190 {
0191   return rtems_termios_read (arg);
0192 }
0193 
0194 rtems_device_driver termios_test_driver_write(
0195   rtems_device_major_number major,
0196   rtems_device_minor_number minor,
0197   void                    * arg
0198 )
0199 {
0200   return rtems_termios_write (arg);
0201 }
0202 
0203 rtems_device_driver termios_test_driver_control(
0204   rtems_device_major_number major,
0205   rtems_device_minor_number minor,
0206   void                    * arg
0207 )
0208 {
0209   return rtems_termios_ioctl (arg);
0210 }