]> rtime.felk.cvut.cz Git - linux-lin.git/commitdiff
Basic argument parsing made by getopt_long.
authorRostislav Lisovy <lisovy@gmail.com>
Thu, 24 Nov 2011 14:50:28 +0000 (15:50 +0100)
committerRostislav Lisovy <lisovy@gmail.com>
Thu, 24 Nov 2011 14:50:28 +0000 (15:50 +0100)
misc/tty_lin_master/main.c

index faa01cebd707a5642a0203fe526f8fce9d6493bd..7da775f9e1825477ab3b009b8bf1605530cd060a 100644 (file)
 #include <unistd.h>
 #include <fcntl.h>
 #include <time.h> /* clock_nanosleep */
 #include <unistd.h>
 #include <fcntl.h>
 #include <time.h> /* clock_nanosleep */
+#include <getopt.h>
 
 #define LIN_HDR_SIZE           2
 #define LIN_PKT_MAX_SIZE       16 /* FIXME */
 
 
 #define LIN_HDR_SIZE           2
 #define LIN_PKT_MAX_SIZE       16 /* FIXME */
 
-const int lin_baudrate = 19200;
+int lin_baudrate = 19200;
 int lin_break_baud = 0;
 
 struct termios tattr_orig;
 int lin_break_baud = 0;
 
 struct termios tattr_orig;
@@ -202,19 +203,70 @@ int read_header(int tty)
        return 0;
 }
 
        return 0;
 }
 
+static void usage(void)
+{
+       printf("Usage: lin_master <parameters>\n\n");
+       printf("Mandatory parameters:\n");
+       printf("  -d, --device <device>   Device name, e.g. /dev/ttyS0\n\n");
+       printf("Optional parameters:\n");
+       printf("  -B, --baud <baud>       LIN bus baudrate. Default value is 19200.\n");
+       printf("                          Recommendet values are 2400, 9600, 19200\n");
+       printf("  -i, --id <num>          LIN frame ID\n");
+       printf("  -r, --response <num>    Values of data fields sent from slave task\n");
+       printf("  -h, --help              Help, i.e. this screen\n");
+}
 
 int main(int argc, char* argv[])
 {
 
 int main(int argc, char* argv[])
 {
-       char dev[32];
+       static struct option long_opts[] = {
+               {"device"  , 1, 0, 'd'},
+               {"baud"    , 1, 0, 'B'},
+               {"id"      , 1, 0, 'i'},
+               {"response", 1, 0, 'r'},
+               {"help"    , 0, 0, 'h'},
+               {0, 0, 0, 0}
+       };
+       int opt;
+       char dev[32] = {'\0'};
        int tty;
 
        int tty;
 
-       if (argc < 2) {
-               fprintf(stderr, "Device is missing\n");
-               fprintf(stderr, "Usage: %s DEVICE\n", argv[0]);
-               return -3;
+       while ((opt = getopt_long(argc, argv, "d:B:i:r:h", &long_opts[0], NULL)) != EOF) {
+               switch (opt) {
+                       case 'd':
+                               strncpy((char*)&dev, optarg, 32);
+                               break;
+
+                       case 'B':
+                               lin_baudrate = atoi(optarg);
+                               break;
+
+                       case 'i':
+                               break;
+
+                       case 'r':
+                               break;
+
+                       case 'h':
+                       default:
+                               usage();
+                               exit(opt == 'h' ? 0 : 1);
+                               break;
+               }
+       }
+
+       if (optind < argc) {
+               usage();
+               //fprintf(stderr, "Expected argument after options\n");
+               exit(EXIT_FAILURE);
+       }
+
+       /* Device name was not set by user */
+       if (strlen(dev) == 0) {
+               usage();
+               exit(EXIT_FAILURE);
        }
 
        }
 
-       strncpy((char*)&dev, argv[1], 32);
+       /* ----------------------------------- */
        tty = open(dev, O_RDWR);
        if (tty < 0) {
                perror("open()");
        tty = open(dev, O_RDWR);
        if (tty < 0) {
                perror("open()");