Просмотр исходного кода

[parseopt] Add parse_timeout()

Parsing a timeout value (specified in milliseconds) into an internal
timeout value measured in timer ticks is a common operation.  Provide
a parse_timeout() value to carry out this conversion automatically.

Signed-off-by: Michael Brown <mcb30@ipxe.org>
tags/v1.20.1
Michael Brown 10 лет назад
Родитель
Сommit
5e1fa5cd40

+ 22
- 0
src/core/parseopt.c Просмотреть файл

@@ -30,6 +30,7 @@ FILE_LICENCE ( GPL2_OR_LATER );
30 30
 #include <ipxe/menu.h>
31 31
 #include <ipxe/settings.h>
32 32
 #include <ipxe/params.h>
33
+#include <ipxe/timer.h>
33 34
 #include <ipxe/parseopt.h>
34 35
 
35 36
 /** @file
@@ -95,6 +96,27 @@ int parse_integer ( char *text, unsigned int *value ) {
95 96
 	return 0;
96 97
 }
97 98
 
99
+/**
100
+ * Parse timeout value (in ms)
101
+ *
102
+ * @v text		Text
103
+ * @ret value		Integer value
104
+ * @ret rc		Return status code
105
+ */
106
+int parse_timeout ( char *text, unsigned long *value ) {
107
+	unsigned int value_ms;
108
+	int rc;
109
+
110
+	/* Parse raw integer value */
111
+	if ( ( rc = parse_integer ( text, &value_ms ) ) != 0 )
112
+		return rc;
113
+
114
+	/* Convert to a number of timer ticks */
115
+	*value = ( ( value_ms * TICKS_PER_SEC ) / 1000 );
116
+
117
+	return 0;
118
+}
119
+
98 120
 /**
99 121
  * Parse network device name
100 122
  *

+ 2
- 2
src/hci/commands/menu_cmd.c Просмотреть файл

@@ -194,7 +194,7 @@ struct choose_options {
194 194
 	/** Menu name */
195 195
 	char *menu;
196 196
 	/** Timeout */
197
-	unsigned int timeout;
197
+	unsigned long timeout;
198 198
 	/** Default selection */
199 199
 	char *select;
200 200
 	/** Keep menu */
@@ -208,7 +208,7 @@ static struct option_descriptor choose_opts[] = {
208 208
 	OPTION_DESC ( "default", 'd', required_argument,
209 209
 		      struct choose_options, select, parse_string ),
210 210
 	OPTION_DESC ( "timeout", 't', required_argument,
211
-		      struct choose_options, timeout, parse_integer ),
211
+		      struct choose_options, timeout, parse_timeout ),
212 212
 	OPTION_DESC ( "keep", 'k', no_argument,
213 213
 		      struct choose_options, keep, parse_flag ),
214 214
 };

+ 4
- 3
src/hci/commands/ping_cmd.c Просмотреть файл

@@ -27,6 +27,7 @@ FILE_LICENCE ( GPL2_OR_LATER );
27 27
 #include <getopt.h>
28 28
 #include <ipxe/command.h>
29 29
 #include <ipxe/parseopt.h>
30
+#include <ipxe/timer.h>
30 31
 #include <usr/pingmgmt.h>
31 32
 
32 33
 /** @file
@@ -39,14 +40,14 @@ FILE_LICENCE ( GPL2_OR_LATER );
39 40
 #define PING_DEFAULT_SIZE 64
40 41
 
41 42
 /** Default timeout */
42
-#define PING_DEFAULT_TIMEOUT 1000
43
+#define PING_DEFAULT_TIMEOUT TICKS_PER_SEC
43 44
 
44 45
 /** "ping" options */
45 46
 struct ping_options {
46 47
 	/** Payload length */
47 48
 	unsigned int size;
48 49
 	/** Timeout (in ms) */
49
-	unsigned int timeout;
50
+	unsigned long timeout;
50 51
 };
51 52
 
52 53
 /** "ping" option list */
@@ -54,7 +55,7 @@ static struct option_descriptor ping_opts[] = {
54 55
 	OPTION_DESC ( "size", 's', required_argument,
55 56
 		      struct ping_options, size, parse_integer ),
56 57
 	OPTION_DESC ( "timeout", 't', required_argument,
57
-		      struct ping_options, timeout, parse_integer ),
58
+		      struct ping_options, timeout, parse_timeout ),
58 59
 };
59 60
 
60 61
 /** "ping" command descriptor */

+ 3
- 6
src/hci/commands/sync_cmd.c Просмотреть файл

@@ -24,7 +24,6 @@ FILE_LICENCE ( GPL2_OR_LATER );
24 24
 #include <getopt.h>
25 25
 #include <ipxe/command.h>
26 26
 #include <ipxe/parseopt.h>
27
-#include <ipxe/timer.h>
28 27
 #include <ipxe/pending.h>
29 28
 
30 29
 /** @file
@@ -36,13 +35,13 @@ FILE_LICENCE ( GPL2_OR_LATER );
36 35
 /** "sync" options */
37 36
 struct sync_options {
38 37
 	/** Timeout */
39
-	unsigned int timeout;
38
+	unsigned long timeout;
40 39
 };
41 40
 
42 41
 /** "sync" option list */
43 42
 static struct option_descriptor sync_opts[] = {
44 43
 	OPTION_DESC ( "timeout", 't', required_argument,
45
-		      struct sync_options, timeout, parse_integer ),
44
+		      struct sync_options, timeout, parse_timeout ),
46 45
 };
47 46
 
48 47
 /** "sync" command descriptor */
@@ -59,7 +58,6 @@ static struct command_descriptor sync_cmd =
59 58
  */
60 59
 static int sync_exec ( int argc, char **argv ) {
61 60
 	struct sync_options opts;
62
-	unsigned long timeout;
63 61
 	int rc;
64 62
 
65 63
 	/* Parse options */
@@ -67,8 +65,7 @@ static int sync_exec ( int argc, char **argv ) {
67 65
 		return rc;
68 66
 
69 67
 	/* Wait for pending operations to complete */
70
-	timeout = ( ( opts.timeout * TICKS_PER_SEC ) / 1000 );
71
-	if ( ( rc = pending_wait ( timeout ) ) != 0 ) {
68
+	if ( ( rc = pending_wait ( opts.timeout ) ) != 0 ) {
72 69
 		printf ( "Operations did not complete: %s\n", strerror ( rc ) );
73 70
 		return rc;
74 71
 	}

+ 3
- 3
src/hci/tui/menu_ui.c Просмотреть файл

@@ -303,11 +303,11 @@ static int menu_loop ( struct menu_ui *ui, struct menu_item **selected ) {
303 303
  * Show menu
304 304
  *
305 305
  * @v menu		Menu
306
- * @v wait_ms		Time to wait, in milliseconds (0=indefinite)
306
+ * @v timeout		Timeout period, in ticks (0=indefinite)
307 307
  * @ret selected	Selected item
308 308
  * @ret rc		Return status code
309 309
  */
310
-int show_menu ( struct menu *menu, unsigned int timeout_ms,
310
+int show_menu ( struct menu *menu, unsigned long timeout,
311 311
 		const char *select, struct menu_item **selected ) {
312 312
 	struct menu_item *item;
313 313
 	struct menu_ui ui;
@@ -318,7 +318,7 @@ int show_menu ( struct menu *menu, unsigned int timeout_ms,
318 318
 	/* Initialise UI */
319 319
 	memset ( &ui, 0, sizeof ( ui ) );
320 320
 	ui.menu = menu;
321
-	ui.timeout = ( ( timeout_ms * TICKS_PER_SEC ) / 1000 );
321
+	ui.timeout = timeout;
322 322
 	list_for_each_entry ( item, &menu->items, list ) {
323 323
 		if ( item->label ) {
324 324
 			if ( ! labelled_count )

+ 2
- 2
src/image/script.c Просмотреть файл

@@ -361,7 +361,7 @@ struct prompt_options {
361 361
 	/** Key to wait for */
362 362
 	unsigned int key;
363 363
 	/** Timeout */
364
-	unsigned int timeout;
364
+	unsigned long timeout;
365 365
 };
366 366
 
367 367
 /** "prompt" option list */
@@ -369,7 +369,7 @@ static struct option_descriptor prompt_opts[] = {
369 369
 	OPTION_DESC ( "key", 'k', required_argument,
370 370
 		      struct prompt_options, key, parse_key ),
371 371
 	OPTION_DESC ( "timeout", 't', required_argument,
372
-		      struct prompt_options, timeout, parse_integer ),
372
+		      struct prompt_options, timeout, parse_timeout ),
373 373
 };
374 374
 
375 375
 /** "prompt" command descriptor */

+ 1
- 1
src/include/ipxe/menu.h Просмотреть файл

@@ -43,7 +43,7 @@ extern struct menu_item * add_menu_item ( struct menu *menu, const char *label,
43 43
 					  int is_default );
44 44
 extern void destroy_menu ( struct menu *menu );
45 45
 extern struct menu * find_menu ( const char *name );
46
-extern int show_menu ( struct menu *menu, unsigned int timeout_ms,
46
+extern int show_menu ( struct menu *menu, unsigned long timeout,
47 47
 		       const char *select, struct menu_item **selected );
48 48
 
49 49
 #endif /* _IPXE_MENU_H */

+ 1
- 0
src/include/ipxe/parseopt.h Просмотреть файл

@@ -126,6 +126,7 @@ struct named_setting {
126 126
 
127 127
 extern int parse_string ( char *text, char **value );
128 128
 extern int parse_integer ( char *text, unsigned int *value );
129
+extern int parse_timeout ( char *text, unsigned long *value );
129 130
 extern int parse_netdev ( char *text, struct net_device **netdev );
130 131
 extern int parse_menu ( char *text, struct menu **menu );
131 132
 extern int parse_flag ( char *text __unused, int *flag );

+ 1
- 1
src/include/usr/pingmgmt.h Просмотреть файл

@@ -11,6 +11,6 @@ FILE_LICENCE ( GPL2_OR_LATER );
11 11
 
12 12
 #include <stdint.h>
13 13
 
14
-extern int ping ( const char *hostname, unsigned long timeout_ms, size_t len );
14
+extern int ping ( const char *hostname, unsigned long timeout, size_t len );
15 15
 
16 16
 #endif /* _USR_PINGMGMT_H */

+ 1
- 1
src/include/usr/prompt.h Просмотреть файл

@@ -9,6 +9,6 @@
9 9
 
10 10
 FILE_LICENCE ( GPL2_OR_LATER );
11 11
 
12
-extern int prompt ( const char *text, unsigned int wait_ms, int key );
12
+extern int prompt ( const char *text, unsigned long timeout, int key );
13 13
 
14 14
 #endif /* _USR_PROMPT_H */

+ 3
- 4
src/usr/pingmgmt.c Просмотреть файл

@@ -56,16 +56,15 @@ static void ping_callback ( struct sockaddr *peer, unsigned int sequence,
56 56
  * Ping a host
57 57
  *
58 58
  * @v hostname		Hostname
59
- * @v timeout_ms	Timeout between pings, in ms
59
+ * @v timeout		Timeout between pings, in ticks
60 60
  * @v len		Payload length
61 61
  * @ret rc		Return status code
62 62
  */
63
-int ping ( const char *hostname, unsigned long timeout_ms, size_t len ) {
63
+int ping ( const char *hostname, unsigned long timeout, size_t len ) {
64 64
 	int rc;
65 65
 
66 66
 	/* Create pinger */
67
-	if ( ( rc = create_pinger ( &monojob, hostname,
68
-				    ( ( timeout_ms * TICKS_PER_SEC ) / 1000 ),
67
+	if ( ( rc = create_pinger ( &monojob, hostname, timeout,
69 68
 				    len, ping_callback ) ) != 0 ) {
70 69
 		printf ( "Could not start ping: %s\n", strerror ( rc ) );
71 70
 		return rc;

+ 3
- 4
src/usr/prompt.c Просмотреть файл

@@ -28,28 +28,27 @@ FILE_LICENCE ( GPL2_OR_LATER );
28 28
 #include <errno.h>
29 29
 #include <stdio.h>
30 30
 #include <ipxe/console.h>
31
-#include <ipxe/timer.h>
32 31
 #include <usr/prompt.h>
33 32
 
34 33
 /**
35 34
  * Prompt for keypress
36 35
  *
37 36
  * @v text		Prompt string
38
- * @v wait_ms		Time to wait, in milliseconds (0=indefinite)
37
+ * @v timeout		Timeout period, in ticks (0=indefinite)
39 38
  * @v key		Key to wait for (0=any key)
40 39
  * @ret rc		Return status code
41 40
  *
42 41
  * Returns success if the specified key was pressed within the
43 42
  * specified timeout period.
44 43
  */
45
-int prompt ( const char *text, unsigned int wait_ms, int key ) {
44
+int prompt ( const char *text, unsigned long timeout, int key ) {
46 45
 	int key_pressed;
47 46
 
48 47
 	/* Display prompt */
49 48
 	printf ( "%s", text );
50 49
 
51 50
 	/* Wait for key */
52
-	key_pressed = getkey ( ( wait_ms * TICKS_PER_SEC ) / 1000 );
51
+	key_pressed = getkey ( timeout );
53 52
 
54 53
 	/* Clear the prompt line */
55 54
 	while ( *(text++) )

Загрузка…
Отмена
Сохранить