Changeset ba95e8f in mainline
- Timestamp:
- 2010-05-06T10:31:02Z (15 years ago)
- Branches:
- lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
- Children:
- ca97cad
- Parents:
- bb864a0
- Location:
- uspace
- Files:
-
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
uspace/app/test_serial/test_serial.c
rbb864a0 rba95e8f 28 28 29 29 /** @addtogroup test_serial 30 * @brief test the serial port driver 30 * @brief test the serial port driver - read from the serial port 31 31 * @{ 32 32 */ … … 35 35 */ 36 36 37 #include <errno.h> 38 #include <stdlib.h> 37 39 #include <stdio.h> 38 40 #include <ipc/ipc.h> 39 41 #include <sys/types.h> 40 #include <atomic.h>41 #include <ipc/devmap.h>42 42 #include <async.h> 43 43 #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 167 44 #include <ipc/devman.h> 168 45 #include <devman.h> 169 46 #include <device/char.h> 170 47 48 #define NAME "test serial" 49 171 50 172 51 static void print_usage() 173 52 { 174 printf("Usage: \n test_serial count \n where count is anumber 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"); 175 54 } 176 177 55 178 56 int main(int argc, char *argv[]) … … 195 73 } 196 74 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); 198 76 199 77 int phone; … … 212 90 } 213 91 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 } 220 110 } 221 222 buf[cnt+1] = 0;223 printf(NAME ": read data: '%s'.", buf);224 111 225 112 devman_hangup_phone(DEVMAN_CLIENT); 226 113 ipc_hangup(phone); 114 free(buf); 227 115 228 116 return 0; -
uspace/lib/libc/generic/device/char.c
rbb864a0 rba95e8f 47 47 async_serialize_start(); 48 48 49 printf("calling interface %d\n", DEV_IFACE_ID(CHAR_DEV_IFACE));50 49 aid_t req = async_send_1(dev_phone, DEV_IFACE_ID(CHAR_DEV_IFACE), CHAR_READ_DEV, &answer); 51 50 52 printf("async_data_read_start \n");53 51 int rc = async_data_read_start(dev_phone, buf, len); 54 52 55 printf("async_data_read_start, rc = %d\n", rc);56 53 if (rc != EOK) { 57 54 ipcarg_t rc_orig; … … 66 63 } 67 64 68 printf("async_wait_for(req, &rc);\n", rc);69 65 async_wait_for(req, &rc); 70 printf("async_serialize_end();\n", rc);71 66 async_serialize_end(); 72 67 … … 75 70 } 76 71 77 printf("IPC_GET_ARG1(answer);\n", rc);78 72 return IPC_GET_ARG1(answer); 79 73 } -
uspace/lib/libdrv/generic/remote_char.c
rbb864a0 rba95e8f 58 58 { 59 59 char_iface_t *char_iface = (char_iface_t *)iface; 60 ipc_callid_t cid; 60 61 61 62 size_t len; 62 if (!async_data_read_receive(&c allid, &len)) {63 if (!async_data_read_receive(&cid, &len)) { 63 64 // TODO handle protocol error 64 65 ipc_answer_0(callid, EINVAL); … … 67 68 68 69 if (!char_iface->read) { 69 async_data_read_finalize(c allid, NULL, 0);70 async_data_read_finalize(cid, NULL, 0); 70 71 ipc_answer_0(callid, ENOTSUP); 71 72 return; … … 80 81 81 82 if (ret < 0) { // some error occured 82 async_data_read_finalize(c allid, buf, 0);83 async_data_read_finalize(cid, buf, 0); 83 84 ipc_answer_0(callid, ret); 84 85 return; 85 86 } 86 87 87 printf("remote_char_read - async_data_read_finalize\n"); 88 async_data_read_finalize(callid, buf, ret); 89 printf("remote_char_read - ipc_answer_0(callid, EOK);\n"); 90 ipc_answer_0(callid, EOK); 88 async_data_read_finalize(cid, buf, ret); 89 ipc_answer_1(callid, EOK, ret); 91 90 } 92 91 … … 117 116 ipc_answer_0(callid, ret); 118 117 } else { 119 ipc_answer_ 0(callid, EOK);118 ipc_answer_1(callid, EOK, ret); 120 119 } 121 120 }
Note:
See TracChangeset
for help on using the changeset viewer.