Browse Source

[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 11 years ago
parent
commit
5e1fa5cd40

+ 22
- 0
src/core/parseopt.c View File

30
 #include <ipxe/menu.h>
30
 #include <ipxe/menu.h>
31
 #include <ipxe/settings.h>
31
 #include <ipxe/settings.h>
32
 #include <ipxe/params.h>
32
 #include <ipxe/params.h>
33
+#include <ipxe/timer.h>
33
 #include <ipxe/parseopt.h>
34
 #include <ipxe/parseopt.h>
34
 
35
 
35
 /** @file
36
 /** @file
95
 	return 0;
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
  * Parse network device name
121
  * Parse network device name
100
  *
122
  *

+ 2
- 2
src/hci/commands/menu_cmd.c View File

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

+ 4
- 3
src/hci/commands/ping_cmd.c View File

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

+ 3
- 6
src/hci/commands/sync_cmd.c View File

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

+ 3
- 3
src/hci/tui/menu_ui.c View File

303
  * Show menu
303
  * Show menu
304
  *
304
  *
305
  * @v menu		Menu
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
  * @ret selected	Selected item
307
  * @ret selected	Selected item
308
  * @ret rc		Return status code
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
 		const char *select, struct menu_item **selected ) {
311
 		const char *select, struct menu_item **selected ) {
312
 	struct menu_item *item;
312
 	struct menu_item *item;
313
 	struct menu_ui ui;
313
 	struct menu_ui ui;
318
 	/* Initialise UI */
318
 	/* Initialise UI */
319
 	memset ( &ui, 0, sizeof ( ui ) );
319
 	memset ( &ui, 0, sizeof ( ui ) );
320
 	ui.menu = menu;
320
 	ui.menu = menu;
321
-	ui.timeout = ( ( timeout_ms * TICKS_PER_SEC ) / 1000 );
321
+	ui.timeout = timeout;
322
 	list_for_each_entry ( item, &menu->items, list ) {
322
 	list_for_each_entry ( item, &menu->items, list ) {
323
 		if ( item->label ) {
323
 		if ( item->label ) {
324
 			if ( ! labelled_count )
324
 			if ( ! labelled_count )

+ 2
- 2
src/image/script.c View File

361
 	/** Key to wait for */
361
 	/** Key to wait for */
362
 	unsigned int key;
362
 	unsigned int key;
363
 	/** Timeout */
363
 	/** Timeout */
364
-	unsigned int timeout;
364
+	unsigned long timeout;
365
 };
365
 };
366
 
366
 
367
 /** "prompt" option list */
367
 /** "prompt" option list */
369
 	OPTION_DESC ( "key", 'k', required_argument,
369
 	OPTION_DESC ( "key", 'k', required_argument,
370
 		      struct prompt_options, key, parse_key ),
370
 		      struct prompt_options, key, parse_key ),
371
 	OPTION_DESC ( "timeout", 't', required_argument,
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
 /** "prompt" command descriptor */
375
 /** "prompt" command descriptor */

+ 1
- 1
src/include/ipxe/menu.h View File

43
 					  int is_default );
43
 					  int is_default );
44
 extern void destroy_menu ( struct menu *menu );
44
 extern void destroy_menu ( struct menu *menu );
45
 extern struct menu * find_menu ( const char *name );
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
 		       const char *select, struct menu_item **selected );
47
 		       const char *select, struct menu_item **selected );
48
 
48
 
49
 #endif /* _IPXE_MENU_H */
49
 #endif /* _IPXE_MENU_H */

+ 1
- 0
src/include/ipxe/parseopt.h View File

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

+ 1
- 1
src/include/usr/pingmgmt.h View File

11
 
11
 
12
 #include <stdint.h>
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
 #endif /* _USR_PINGMGMT_H */
16
 #endif /* _USR_PINGMGMT_H */

+ 1
- 1
src/include/usr/prompt.h View File

9
 
9
 
10
 FILE_LICENCE ( GPL2_OR_LATER );
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
 #endif /* _USR_PROMPT_H */
14
 #endif /* _USR_PROMPT_H */

+ 3
- 4
src/usr/pingmgmt.c View File

56
  * Ping a host
56
  * Ping a host
57
  *
57
  *
58
  * @v hostname		Hostname
58
  * @v hostname		Hostname
59
- * @v timeout_ms	Timeout between pings, in ms
59
+ * @v timeout		Timeout between pings, in ticks
60
  * @v len		Payload length
60
  * @v len		Payload length
61
  * @ret rc		Return status code
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
 	int rc;
64
 	int rc;
65
 
65
 
66
 	/* Create pinger */
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
 				    len, ping_callback ) ) != 0 ) {
68
 				    len, ping_callback ) ) != 0 ) {
70
 		printf ( "Could not start ping: %s\n", strerror ( rc ) );
69
 		printf ( "Could not start ping: %s\n", strerror ( rc ) );
71
 		return rc;
70
 		return rc;

+ 3
- 4
src/usr/prompt.c View File

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

Loading…
Cancel
Save