Changeset 3b3c689 in mainline
- Timestamp:
- 2012-03-12T23:17:57Z (13 years ago)
- Branches:
- lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
- Children:
- 8bf672d
- Parents:
- 6428115
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
uspace/app/ping/ping.c
r6428115 r3b3c689 33 33 */ 34 34 35 #include <async.h> 36 #include <bool.h> 35 37 #include <errno.h> 36 38 #include <fibril_synch.h> 37 39 #include <inet/inetping.h> 40 #include <io/console.h> 38 41 #include <stdio.h> 39 42 #include <stdlib.h> … … 42 45 #define NAME "ping" 43 46 47 /** Delay between subsequent ping requests in microseconds */ 48 #define PING_DELAY (1000 * 1000) 49 44 50 /** Ping request timeout in microseconds */ 45 51 #define PING_TIMEOUT (1000 * 1000) 46 52 47 53 static int ping_ev_recv(inetping_sdu_t *); 54 55 static bool done = false; 48 56 static FIBRIL_CONDVAR_INITIALIZE(done_cv); 49 57 static FIBRIL_MUTEX_INITIALIZE(done_lock); … … 53 61 }; 54 62 63 static inet_addr_t src_addr; 64 static inet_addr_t dest_addr; 65 66 static bool ping_repeat = false; 67 55 68 static void print_syntax(void) 56 69 { 57 printf("syntax: " NAME " <addr>\n");70 printf("syntax: " NAME " [-r] <addr>\n"); 58 71 } 59 72 … … 99 112 } 100 113 114 static void ping_signal_done(void) 115 { 116 fibril_mutex_lock(&done_lock); 117 done = true; 118 fibril_mutex_unlock(&done_lock); 119 fibril_condvar_broadcast(&done_cv); 120 } 121 101 122 static int ping_ev_recv(inetping_sdu_t *sdu) 102 123 { … … 116 137 "payload size %zu\n", asrc, adest, sdu->seq_no, sdu->size); 117 138 118 fibril_condvar_broadcast(&done_cv); 139 if (!ping_repeat) { 140 ping_signal_done(); 141 } 119 142 120 143 free(asrc); … … 123 146 } 124 147 148 static int ping_send(uint16_t seq_no) 149 { 150 inetping_sdu_t sdu; 151 int rc; 152 153 sdu.src = src_addr; 154 sdu.dest = dest_addr; 155 sdu.seq_no = seq_no; 156 sdu.data = (void *) "foo"; 157 sdu.size = 3; 158 159 rc = inetping_send(&sdu); 160 if (rc != EOK) { 161 printf(NAME ": Failed sending echo request (%d).\n", rc); 162 return rc; 163 } 164 165 return EOK; 166 } 167 168 static int transmit_fibril(void *arg) 169 { 170 uint16_t seq_no = 0; 171 172 while (true) { 173 fibril_mutex_lock(&done_lock); 174 if (done) { 175 fibril_mutex_unlock(&done_lock); 176 return 0; 177 } 178 fibril_mutex_unlock(&done_lock); 179 180 (void) ping_send(++seq_no); 181 async_usleep(PING_DELAY); 182 } 183 184 return 0; 185 } 186 187 static int input_fibril(void *arg) 188 { 189 console_ctrl_t *con; 190 kbd_event_t ev; 191 192 con = console_init(stdin, stdout); 193 printf("[Press Ctrl-Q to quit]\n"); 194 195 while (true) { 196 if (!console_get_kbd_event(con, &ev)) 197 break; 198 199 if (ev.type == KEY_PRESS && (ev.mods & (KM_ALT | KM_SHIFT)) == 200 0 && (ev.mods & KM_CTRL) != 0) { 201 /* Ctrl+key */ 202 if (ev.key == KC_Q) { 203 ping_signal_done(); 204 return 0; 205 } 206 } 207 } 208 209 return 0; 210 } 211 125 212 int main(int argc, char *argv[]) 126 213 { 127 214 int rc; 128 in etping_sdu_t sdu;215 int argi; 129 216 130 217 rc = inetping_init(&ev_ops); … … 135 222 } 136 223 137 if (argc != 2) { 224 argi = 1; 225 if (argi < argc && str_cmp(argv[argi], "-r") == 0) { 226 ping_repeat = true; 227 ++argi; 228 } else { 229 ping_repeat = false; 230 } 231 232 if (argc - argi != 1) { 138 233 print_syntax(); 139 234 return 1; … … 141 236 142 237 /* Parse destination address */ 143 rc = addr_parse(argv[ 1], &sdu.dest);238 rc = addr_parse(argv[argi], &dest_addr); 144 239 if (rc != EOK) { 145 240 printf(NAME ": Invalid address format.\n"); … … 148 243 } 149 244 150 sdu.seq_no = 1;151 sdu.data = (void *) "foo";152 sdu.size = 3;153 154 245 /* Determine source address */ 155 rc = inetping_get_srcaddr(& sdu.dest, &sdu.src);246 rc = inetping_get_srcaddr(&dest_addr, &src_addr); 156 247 if (rc != EOK) { 157 248 printf(NAME ": Failed determining source address.\n"); … … 159 250 } 160 251 161 rc = inetping_send(&sdu); 162 if (rc != EOK) { 163 printf(NAME ": Failed sending echo request (%d).\n", rc); 164 return 1; 252 fid_t fid; 253 254 if (ping_repeat) { 255 fid = fibril_create(transmit_fibril, NULL); 256 if (fid == 0) { 257 printf(NAME ": Failed creating transmit fibril.\n"); 258 return 1; 259 } 260 261 fibril_add_ready(fid); 262 263 fid = fibril_create(input_fibril, NULL); 264 if (fid == 0) { 265 printf(NAME ": Failed creating input fibril.\n"); 266 return 1; 267 } 268 269 fibril_add_ready(fid); 270 } else { 271 ping_send(1); 165 272 } 166 273 167 274 fibril_mutex_lock(&done_lock); 168 rc = fibril_condvar_wait_timeout(&done_cv, &done_lock, PING_TIMEOUT); 275 rc = EOK; 276 while (!done && rc != ETIMEOUT) { 277 rc = fibril_condvar_wait_timeout(&done_cv, &done_lock, 278 ping_repeat ? 0 : PING_TIMEOUT); 279 } 169 280 fibril_mutex_unlock(&done_lock); 170 281
Note:
See TracChangeset
for help on using the changeset viewer.