serial1.c

Go to the documentation of this file.
00001 /*
00002  * Copyright (c) 2009 Lenka Trochtova
00003  * All rights reserved.
00004  *
00005  * Redistribution and use in source and binary forms, with or without
00006  * modification, are permitted provided that the following conditions
00007  * are met:
00008  *
00009  * - Redistributions of source code must retain the above copyright
00010  *   notice, this list of conditions and the following disclaimer.
00011  * - Redistributions in binary form must reproduce the above copyright
00012  *   notice, this list of conditions and the following disclaimer in the
00013  *   documentation and/or other materials provided with the distribution.
00014  * - The name of the author may not be used to endorse or promote products
00015  *   derived from this software without specific prior written permission.
00016  *
00017  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
00018  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
00019  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
00020  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
00021  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
00022  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
00023  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
00024  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00025  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
00026  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00027  */
00028 
00037 #include <inttypes.h>
00038 #include <errno.h>
00039 #include <stdlib.h>
00040 #include <stdio.h>
00041 #include <sys/types.h>
00042 #include <async.h>
00043 #include <ipc/services.h>
00044 #include <ipc/devman.h>
00045 #include <devman.h>
00046 #include <device/char_dev.h>
00047 #include <str.h>
00048 #include <ipc/serial_ctl.h>
00049 #include "../../tester.h"
00050 
00051 #define DEFAULT_COUNT  1024
00052 #define DEFAULT_SLEEP  100000
00053 #define EOT            "####> End of transfer <####\n"
00054 
00055 const char *test_serial1(void)
00056 {
00057         size_t cnt;
00058         
00059         if (test_argc < 1)
00060                 cnt = DEFAULT_COUNT;
00061         else
00062                 switch (str_size_t(test_argv[0], NULL, 0, true, &cnt)) {
00063                 case EOK:
00064                         break;
00065                 case EINVAL:
00066                         return "Invalid argument, unsigned integer expected";
00067                 case EOVERFLOW:
00068                         return "Argument size overflow";
00069                 default:
00070                         return "Unexpected argument error";
00071                 }
00072         
00073         int res = devman_get_phone(DEVMAN_CLIENT, IPC_FLAG_BLOCKING);
00074         
00075         devman_handle_t handle;
00076         res = devman_device_get_handle("/hw/pci0/00:01.0/com1/a", &handle,
00077             IPC_FLAG_BLOCKING);
00078         if (res != EOK)
00079                 return "Could not get serial device handle";
00080         
00081         int phone = devman_device_connect(handle, IPC_FLAG_BLOCKING);
00082         if (phone < 0) {
00083                 devman_hangup_phone(DEVMAN_CLIENT);
00084                 return "Unable to connect to serial device";
00085         }
00086         
00087         char *buf = (char *) malloc(cnt + 1);
00088         if (buf == NULL) {
00089                 async_hangup(phone);
00090                 devman_hangup_phone(DEVMAN_CLIENT);
00091                 return "Failed to allocate input buffer";
00092         }
00093         
00094         sysarg_t old_baud;
00095         sysarg_t old_par;
00096         sysarg_t old_stop;
00097         sysarg_t old_word_size;
00098         
00099         res = async_req_0_4(phone, SERIAL_GET_COM_PROPS, &old_baud,
00100             &old_par, &old_word_size, &old_stop);
00101         if (res != EOK) {
00102                 free(buf);
00103                 async_hangup(phone);
00104                 devman_hangup_phone(DEVMAN_CLIENT);
00105                 return "Failed to get old serial communication parameters";
00106         }
00107         
00108         res = async_req_4_0(phone, SERIAL_SET_COM_PROPS, 1200,
00109             SERIAL_NO_PARITY, 8, 1);
00110         if (EOK != res) {
00111                 free(buf);
00112                 async_hangup(phone);
00113                 devman_hangup_phone(DEVMAN_CLIENT);
00114                 return "Failed to set serial communication parameters";
00115         }
00116         
00117         TPRINTF("Trying to read %zu characters from serial device "
00118             "(handle=%" PRIun ")\n", cnt, handle);
00119         
00120         size_t total = 0;
00121         while (total < cnt) {
00122                 ssize_t read = char_dev_read(phone, buf, cnt - total);
00123                 
00124                 if (read < 0) {
00125                         async_req_4_0(phone, SERIAL_SET_COM_PROPS, old_baud,
00126                             old_par, old_word_size, old_stop);
00127                         free(buf);
00128                         async_hangup(phone);
00129                         devman_hangup_phone(DEVMAN_CLIENT);
00130                         return "Failed read from serial device";
00131                 }
00132                 
00133                 if ((size_t) read > cnt - total) {
00134                         async_req_4_0(phone, SERIAL_SET_COM_PROPS, old_baud,
00135                             old_par, old_word_size, old_stop);
00136                         free(buf);
00137                         async_hangup(phone);
00138                         devman_hangup_phone(DEVMAN_CLIENT);
00139                         return "Read more data than expected";
00140                 }
00141                 
00142                 TPRINTF("Read %zd bytes\n", read);
00143                 
00144                 if (read == 0)
00145                         usleep(DEFAULT_SLEEP);
00146                 else {
00147                         buf[read] = 0;
00148                         
00149                         /*
00150                          * Write data back to the device to test the opposite
00151                          * direction of data transfer.
00152                          */
00153                         ssize_t written = char_dev_write(phone, buf, read);
00154                         
00155                         if (written < 0) {
00156                                 async_req_4_0(phone, SERIAL_SET_COM_PROPS, old_baud,
00157                                     old_par, old_word_size, old_stop);
00158                                 free(buf);
00159                                 async_hangup(phone);
00160                                 devman_hangup_phone(DEVMAN_CLIENT);
00161                                 return "Failed write to serial device";
00162                         }
00163                         
00164                         if (written != read) {
00165                                 async_req_4_0(phone, SERIAL_SET_COM_PROPS, old_baud,
00166                                     old_par, old_word_size, old_stop);
00167                                 free(buf);
00168                                 async_hangup(phone);
00169                                 devman_hangup_phone(DEVMAN_CLIENT);
00170                                 return "Written less data than read from serial device";
00171                         }
00172                         
00173                         TPRINTF("Written %zd bytes\n", written);
00174                 }
00175                 
00176                 total += read;
00177         }
00178         
00179         TPRINTF("Trying to write EOT banner to the serial device\n");
00180         
00181         size_t eot_size = str_size(EOT);
00182         ssize_t written = char_dev_write(phone, (void *) EOT, eot_size);
00183         
00184         async_req_4_0(phone, SERIAL_SET_COM_PROPS, old_baud,
00185             old_par, old_word_size, old_stop);
00186         free(buf);
00187         async_hangup(phone);
00188         devman_hangup_phone(DEVMAN_CLIENT);
00189         
00190         if (written < 0)
00191                 return "Failed to write EOT banner to serial device";
00192         
00193         if ((size_t) written != eot_size)
00194                 return "Written less data than the size of the EOT banner "
00195                     "to serial device";
00196         
00197         return NULL;
00198 }
00199 

Generated on Thu Jun 2 07:45:43 2011 for HelenOS/USB by  doxygen 1.4.7