From 32f193a7e27658a27e4f0cd6071b936ab75516a3 Mon Sep 17 00:00:00 2001 From: saramonteiro Date: Wed, 16 Sep 2020 17:19:07 -0300 Subject: [PATCH] Added timer device path as a CLI option --- examples/timer/timer_main.c | 35 +++++++++++++++++++++++++++++++---- 1 file changed, 31 insertions(+), 4 deletions(-) diff --git a/examples/timer/timer_main.c b/examples/timer/timer_main.c index a60ca9b20..ee4a23e22 100644 --- a/examples/timer/timer_main.c +++ b/examples/timer/timer_main.c @@ -45,13 +45,15 @@ #include #include #include - +#include #include /**************************************************************************** * Pre-processor Definitions ****************************************************************************/ +#define DEVNAME_SIZE 11 + #ifndef CONFIG_EXAMPLES_TIMER_DEVNAME # define CONFIG_EXAMPLES_TIMER_DEVNAME "/dev/timer0" #endif @@ -72,6 +74,10 @@ # define CONFIG_EXAMPLES_TIMER_SIGNO 17 #endif +/**************************************************************************** + * Private Types + ****************************************************************************/ + /**************************************************************************** * Private Functions ****************************************************************************/ @@ -142,16 +148,37 @@ int main(int argc, FAR char *argv[]) int ret; int fd; int i; + int opt; + char devname[DEVNAME_SIZE]; + strcpy(devname, CONFIG_EXAMPLES_TIMER_DEVNAME); + + while ((opt = getopt(argc, argv, ":d:")) != -1) + { + switch (opt) + { + case 'd': + strcpy(devname, optarg); + break; + case ':': + fprintf(stderr, "ERROR: Option needs a value\n"); + exit(EXIT_FAILURE); + default: /* '?' */ + fprintf(stderr, "Usage: %s [-d /dev/timerx]\n", + argv[0]); + exit(EXIT_FAILURE); + } + } + /* Open the timer device */ - printf("Open %s\n", CONFIG_EXAMPLES_TIMER_DEVNAME); + printf("Open %s\n", devname); - fd = open(CONFIG_EXAMPLES_TIMER_DEVNAME, O_RDONLY); + fd = open(devname, O_RDONLY); if (fd < 0) { fprintf(stderr, "ERROR: Failed to open %s: %d\n", - CONFIG_EXAMPLES_TIMER_DEVNAME, errno); + devname, errno); return EXIT_FAILURE; }