Changeset ba95e8f in mainline for uspace/app/test_serial/test_serial.c


Ignore:
Timestamp:
2010-05-06T10:31:02Z (14 years ago)
Author:
Lenka Trochtova <trochtova.lenka@…>
Branches:
lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
Children:
ca97cad
Parents:
bb864a0
Message:

fixed some bugs; now it is possible to read from serial port using character interface, this might be tested with the test_serial application

File:
1 edited

Legend:

Unmodified
Added
Removed
  • uspace/app/test_serial/test_serial.c

    rbb864a0 rba95e8f  
    2828
    2929/** @addtogroup test_serial
    30  * @brief       test the serial port driver
     30 * @brief       test the serial port driver - read from the serial port
    3131 * @{
    3232 */
     
    3535 */
    3636
     37#include <errno.h>
     38#include <stdlib.h>
    3739#include <stdio.h>
    3840#include <ipc/ipc.h>
    3941#include <sys/types.h>
    40 #include <atomic.h>
    41 #include <ipc/devmap.h>
    4242#include <async.h>
    4343#include <ipc/services.h>
    44 #include <ipc/serial.h>
    45 #include <as.h>
    46 #include <sysinfo.h>
    47 #include <errno.h>
    48 
    49 
    50 #include <stdlib.h>
    51 #include <unistd.h>
    52 #include <fcntl.h>
    53 #include <sys/stat.h>
    54 
    55 #include <string.h>
    56 
    57 #define NAME            "test serial"
    58 
    59 /*
    60 static int device_get_handle(const char *name, dev_handle_t *handle);
    61 static void print_usage();
    62 static void move_shutters(const char * serial_dev_name, char room, char cmd);
    63 static char getcmd(bool wnd, bool up);
    64 static bool is_com_dev(const char *dev_name);
    65 
    66 static int device_get_handle(const char *name, dev_handle_t *handle)
    67 {
    68         int phone = ipc_connect_me_to(PHONE_NS, SERVICE_DEVMAP, DEVMAP_CLIENT, 0);
    69         if (phone < 0)
    70                 return phone;
    71        
    72         ipc_call_t answer;
    73         aid_t req = async_send_2(phone, DEVMAP_DEVICE_GET_HANDLE, 0, 0, &answer);
    74        
    75         ipcarg_t retval = ipc_data_write_start(phone, name, str_length(name) + 1);
    76 
    77         if (retval != EOK) {
    78                 async_wait_for(req, NULL);
    79                 ipc_hangup(phone);
    80                 return retval;
    81         }
    82 
    83         async_wait_for(req, &retval);
    84 
    85         if (handle != NULL)
    86                 *handle = -1;
    87        
    88         if (retval == EOK) {
    89                 if (handle != NULL)
    90                         *handle = (dev_handle_t) IPC_GET_ARG1(answer);
    91         }
    92        
    93         ipc_hangup(phone);
    94         return retval;
    95 }
    96 
    97 static void print_usage()
    98 {
    99         printf("Usage: \n test_serial comN count \n where 'comN' is a serial port and count is a number of characters to be read\n");   
    100 }
    101 
    102 
    103 // The name of a serial device must be between 'com0' and 'com9'.
    104 static bool is_com_dev(const char *dev_name)
    105 {
    106         if (str_length(dev_name) != 4) {
    107                 return false;
    108         }
    109        
    110         if (str_cmp("com0", dev_name) > 0) {
    111                 return false;
    112         }
    113        
    114         if (str_cmp(dev_name, "com9") > 0) {
    115                 return false;
    116         }
    117        
    118         return true;
    119 }
    120 
    121 
    122 int main(int argc, char *argv[])
    123 {
    124         if (argc != 3) {
    125                 printf(NAME ": incorrect number of arguments.\n");
    126                 print_usage();
    127                 return 0;               
    128         }       
    129 
    130        
    131         const char *serial_dev_name = argv[1];
    132         long int cnt = strtol(argv[2], NULL, 10);
    133        
    134         if (!is_com_dev(serial_dev_name)) {
    135                 printf(NAME ": the first argument is not correct.\n");
    136                 print_usage();
    137                 return 0;       
    138         }
    139        
    140         dev_handle_t serial_dev_handle = -1;
    141        
    142         if (device_get_handle(serial_dev_name, &serial_dev_handle) !=  EOK) {
    143                 printf(NAME ": could not get the handle of %s.\n", serial_dev_name);
    144                 return;
    145         }
    146        
    147         printf(NAME ": got the handle of %s.\n", serial_dev_name);
    148        
    149         int dev_phone = ipc_connect_me_to(PHONE_NS, SERVICE_DEVMAP, DEVMAP_CONNECT_TO_DEVICE, serial_dev_handle);
    150         if(dev_phone < 0) {
    151                 printf(NAME ": could not connect to %s device.\n", serial_dev_name);
    152                 return;
    153         }
    154        
    155         printf("The application will read %d characters from %s: \n", cnt, serial_dev_name);
    156        
    157         int i, c;
    158         for (i = 0; i < cnt; i++) {
    159                 async_req_0_1(dev_phone, SERIAL_GETCHAR, &c);
    160                 printf("%c", (char)c);
    161         }
    162        
    163         return 0;
    164 }*/
    165 
    166 
    16744#include <ipc/devman.h>
    16845#include <devman.h>
    16946#include <device/char.h>
    17047
     48#define NAME            "test serial"
     49
    17150
    17251static void print_usage()
    17352{
    174         printf("Usage: \n test_serial count \n where count is a number of characters to be read\n");   
     53        printf("Usage: \n test_serial count \n where count is the number of characters to be read\n"); 
    17554}
    176 
    17755
    17856int main(int argc, char *argv[])
     
    19573        }
    19674       
    197         printf(NAME ": device handle is %d.\n", handle);       
     75        printf(NAME ": trying to read %d characters from device with handle %d.\n", cnt, handle);       
    19876       
    19977        int phone;
     
    21290        }
    21391       
    214         int read = read_dev(phone, buf, cnt);
    215         if (0 > read) {
    216                 printf(NAME ": failed read from device, errno = %d.\n", -read);
    217                 ipc_hangup(phone);
    218                 devman_hangup_phone(DEVMAN_CLIENT);
    219                 return 4;
     92        int total = 0;
     93        int read = 0;
     94        while (total < cnt) {           
     95                read = read_dev(phone, buf, cnt - total);
     96                if (0 > read) {
     97                        printf(NAME ": failed read from device, errno = %d.\n", -read);
     98                        ipc_hangup(phone);
     99                        devman_hangup_phone(DEVMAN_CLIENT);
     100                        free(buf);
     101                        return 4;
     102                }               
     103                total += read;
     104                if (read > 0) {                 
     105                        buf[read] = 0;
     106                        printf(buf);           
     107                } else {       
     108                        usleep(100000);                 
     109                }       
    220110        }
    221        
    222         buf[cnt+1] = 0;
    223         printf(NAME ": read data: '%s'.", buf);
    224111       
    225112        devman_hangup_phone(DEVMAN_CLIENT);
    226113        ipc_hangup(phone);
     114        free(buf);
    227115       
    228116        return 0;
Note: See TracChangeset for help on using the changeset viewer.