Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • uspace/app/tester/hw/serial/serial1.c

    r8d2dd7f2 rf300523  
    3535 */
    3636
    37 #include <inttypes.h>
     37#include <async.h>
    3838#include <errno.h>
     39#include <io/chardev.h>
     40#include <io/serial.h>
     41#include <ipc/services.h>
     42#include <loc.h>
    3943#include <stdlib.h>
    4044#include <stdio.h>
    4145#include <stddef.h>
    42 #include <async.h>
    43 #include <thread.h>
    44 #include <ipc/services.h>
    45 #include <loc.h>
    46 #include <char_dev_iface.h>
    4746#include <str.h>
    48 #include <ipc/serial_ctl.h>
    4947#include "../../tester.h"
    5048
     
    5654{
    5755        size_t cnt;
     56        serial_t *serial;
     57        chardev_t *chardev;
     58        int rc;
     59        size_t nread;
     60        size_t nwritten;
    5861       
    5962        if (test_argc < 1)
     
    7982        async_sess_t *sess = loc_service_connect(svc_id, INTERFACE_DDF,
    8083            IPC_FLAG_BLOCKING);
    81         if (!sess)
     84        if (sess == NULL)
    8285                return "Failed connecting to serial device";
     86       
     87        res = chardev_open(sess, &chardev);
     88        if (res != EOK) {
     89                async_hangup(sess);
     90                return "Failed opening serial port";
     91        }
     92       
     93        res = serial_open(sess, &serial);
     94        if (res != EOK) {
     95                chardev_close(chardev);
     96                async_hangup(sess);
     97                return "Failed opening serial port";
     98        }
    8399       
    84100        char *buf = (char *) malloc(cnt + 1);
    85101        if (buf == NULL) {
     102                chardev_close(chardev);
     103                serial_close(serial);
    86104                async_hangup(sess);
    87105                return "Failed allocating input buffer";
    88106        }
    89107       
    90         sysarg_t old_baud;
    91         sysarg_t old_par;
    92         sysarg_t old_stop;
    93         sysarg_t old_word_size;
    94        
    95         async_exch_t *exch = async_exchange_begin(sess);
    96         res = async_req_0_4(exch, SERIAL_GET_COM_PROPS, &old_baud,
    97             &old_par, &old_word_size, &old_stop);
    98         async_exchange_end(exch);
    99        
     108        unsigned old_baud;
     109        serial_parity_t old_par;
     110        unsigned old_stop;
     111        unsigned old_word_size;
     112       
     113        res = serial_get_comm_props(serial, &old_baud, &old_par,
     114            &old_word_size, &old_stop);
    100115        if (res != EOK) {
    101116                free(buf);
     117                chardev_close(chardev);
     118                serial_close(serial);
    102119                async_hangup(sess);
    103120                return "Failed to get old serial communication parameters";
    104121        }
    105122       
    106         exch = async_exchange_begin(sess);
    107         res = async_req_4_0(exch, SERIAL_SET_COM_PROPS, 1200,
    108             SERIAL_NO_PARITY, 8, 1);
    109         async_exchange_end(exch);
    110        
     123        res = serial_set_comm_props(serial, 1200, SERIAL_NO_PARITY, 8, 1);
    111124        if (EOK != res) {
    112125                free(buf);
     126                chardev_close(chardev);
     127                serial_close(serial);
    113128                async_hangup(sess);
    114129                return "Failed setting serial communication parameters";
     
    120135        size_t total = 0;
    121136        while (total < cnt) {
    122                 ssize_t read = char_dev_read(sess, buf, cnt - total);
    123                
    124                 if (read < 0) {
    125                         exch = async_exchange_begin(sess);
    126                         async_req_4_0(exch, SERIAL_SET_COM_PROPS, old_baud,
    127                             old_par, old_word_size, old_stop);
    128                         async_exchange_end(exch);
    129                        
    130                         free(buf);
     137               
     138                rc = chardev_read(chardev, buf, cnt - total, &nread);
     139                if (rc != EOK) {
     140                        (void) serial_set_comm_props(serial, old_baud,
     141                            old_par, old_word_size, old_stop);
     142                       
     143                        free(buf);
     144                        chardev_close(chardev);
     145                        serial_close(serial);
    131146                        async_hangup(sess);
    132147                        return "Failed reading from serial device";
    133148                }
    134149               
    135                 if ((size_t) read > cnt - total) {
    136                         exch = async_exchange_begin(sess);
    137                         async_req_4_0(exch, SERIAL_SET_COM_PROPS, old_baud,
    138                             old_par, old_word_size, old_stop);
    139                         async_exchange_end(exch);
    140                        
    141                         free(buf);
     150                if (nread > cnt - total) {
     151                        (void) serial_set_comm_props(serial, old_baud,
     152                            old_par, old_word_size, old_stop);
     153                       
     154                        free(buf);
     155                        chardev_close(chardev);
     156                        serial_close(serial);
    142157                        async_hangup(sess);
    143158                        return "Read more data than expected";
    144159                }
    145160               
    146                 TPRINTF("Read %zd bytes\n", read);
    147                
    148                 if (read == 0)
    149                         thread_usleep(DEFAULT_SLEEP);
    150                 else {
    151                         buf[read] = 0;
    152                        
    153                         /*
    154                          * Write data back to the device to test the opposite
    155                          * direction of data transfer.
    156                          */
    157                         ssize_t written = char_dev_write(sess, buf, read);
    158                        
    159                         if (written < 0) {
    160                                 exch = async_exchange_begin(sess);
    161                                 async_req_4_0(exch, SERIAL_SET_COM_PROPS, old_baud,
    162                                     old_par, old_word_size, old_stop);
    163                                 async_exchange_end(exch);
    164                                
    165                                 free(buf);
    166                                 async_hangup(sess);
    167                                 return "Failed writing to serial device";
    168                         }
    169                        
    170                         if (written != read) {
    171                                 exch = async_exchange_begin(sess);
    172                                 async_req_4_0(exch, SERIAL_SET_COM_PROPS, old_baud,
    173                                     old_par, old_word_size, old_stop);
    174                                 async_exchange_end(exch);
    175                                
    176                                 free(buf);
    177                                 async_hangup(sess);
    178                                 return "Written less data than read from serial device";
    179                         }
    180                        
    181                         TPRINTF("Written %zd bytes\n", written);
    182                 }
    183                
    184                 total += read;
     161                TPRINTF("Read %zd bytes\n", nread);
     162               
     163                buf[nread] = 0;
     164               
     165                /*
     166                 * Write data back to the device to test the opposite
     167                 * direction of data transfer.
     168                 */
     169                rc = chardev_write(chardev, buf, nread, &nwritten);
     170                if (rc != EOK) {
     171                        (void) serial_set_comm_props(serial, old_baud,
     172                            old_par, old_word_size, old_stop);
     173                       
     174                        free(buf);
     175                        chardev_close(chardev);
     176                        serial_close(serial);
     177                        async_hangup(sess);
     178                        return "Failed writing to serial device";
     179                }
     180               
     181                if (nwritten != nread) {
     182                        (void) serial_set_comm_props(serial, old_baud,
     183                            old_par, old_word_size, old_stop);
     184                       
     185                        free(buf);
     186                        chardev_close(chardev);
     187                        serial_close(serial);
     188                        async_hangup(sess);
     189                        return "Written less data than read from serial device";
     190                }
     191               
     192                TPRINTF("Written %zd bytes\n", nwritten);
     193               
     194                total += nread;
    185195        }
    186196       
     
    188198       
    189199        size_t eot_size = str_size(EOT);
    190         ssize_t written = char_dev_write(sess, (void *) EOT, eot_size);
    191        
    192         exch = async_exchange_begin(sess);
    193         async_req_4_0(exch, SERIAL_SET_COM_PROPS, old_baud,
    194             old_par, old_word_size, old_stop);
    195         async_exchange_end(exch);
     200        rc = chardev_write(chardev, (void *) EOT, eot_size, &nwritten);
     201       
     202        (void) serial_set_comm_props(serial, old_baud, old_par, old_word_size,
     203            old_stop);
    196204       
    197205        free(buf);
     206        chardev_close(chardev);
     207        serial_close(serial);
    198208        async_hangup(sess);
    199209       
    200         if (written < 0)
     210        if (rc != EOK)
    201211                return "Failed to write EOT banner to serial device";
    202212       
    203         if ((size_t) written != eot_size)
     213        if (nwritten != eot_size)
    204214                return "Written less data than the size of the EOT banner "
    205215                    "to serial device";
Note: See TracChangeset for help on using the changeset viewer.