From 0aa3153f05fe8cd51d76fd5dee9011a7d2296b7d Mon Sep 17 00:00:00 2001
From: guowenxue <guowenxue@gmail.com>
Date: Wed, 26 Jun 2019 00:37:25 +0800
Subject: [PATCH] update mqttd, add command line options parser

---
 mqttd/main.c |   73 ++++++++++++++++++++++++++++++++++--
 1 files changed, 68 insertions(+), 5 deletions(-)

diff --git a/mqttd/main.c b/mqttd/main.c
index 982b34a..df3f10f 100644
--- a/mqttd/main.c
+++ b/mqttd/main.c
@@ -13,6 +13,8 @@
 #include <stdio.h>
 #include <time.h>
 #include <unistd.h>
+#include <getopt.h>
+#include <libgen.h>
 
 #include <mosquitto.h>
 
@@ -23,25 +25,84 @@
 
 #include "conf.h"
 
+#define PROG_VERSION               "v1.0.0"
 #define DAEMON_PIDFILE             "/tmp/.mqtt.pid"
-
 
 int check_set_program_running(int daemon);
 void *mqtt_sub_worker(void *args);
 void *mqtt_pub_worker(void *args);
+
+static void program_usage(char *progname)
+{
+    
+    printf("Usage: %s [OPTION]...\n", progname);
+    printf(" %s is LingYun studio MQTT daemon program running on RaspberryPi\n", progname);
+
+    printf("\nMandatory arguments to long options are mandatory for short options too:\n");
+    printf(" -d[debug   ]  Running in debug mode\n");
+    printf(" -c[conf    ]  Specify configure file\n");
+    printf(" -h[help    ]  Display this help information\n");
+    printf(" -v[version ]  Display the program version\n");
+
+    printf("\n%s version %s\n", progname, PROG_VERSION);
+    return;
+}
 
 
 int main (int argc, char **argv)
 {
     float              temp;
     float              rh;
-    int                daemon = 0;
+    int                daemon = 1;
     pthread_t          tid;
     mqtt_ctx_t         ctx;
-    char               *conf="./etc/mqttd.conf";
-    int                debug = 1;
+    char               *conf_file=NULL;
+    int                debug = 0;
+    int                opt;
+    char              *progname=NULL;
 
-    if( mqttd_parser_conf(conf, &ctx, debug)<0 )
+    struct option long_options[] = {
+        {"conf", required_argument, NULL, 'c'},
+        {"debug", no_argument, NULL, 'd'},
+        {"version", no_argument, NULL, 'v'},
+        {"help", no_argument, NULL, 'h'},
+        {NULL, 0, NULL, 0}
+    };
+
+    progname = (char *)basename(argv[0]);
+
+    /* Parser the command line parameters */
+    while ((opt = getopt_long(argc, argv, "c:dvh", long_options, NULL)) != -1)
+    {
+        switch (opt)
+        {
+            case 'c': /* Set configure file */
+                conf_file = optarg;
+                break;
+
+            case 'd': /* Set debug running */
+                daemon = 0;
+                debug = 1;
+                break;
+
+            case 'v':  /* Get software version */
+                printf("%s version %s\n", progname, PROG_VERSION);
+                return 0;
+
+            case 'h':  /* Get help information */
+                program_usage(progname);
+                return 0;
+        
+            default:
+                break;
+        }
+    
+    }
+
+    if( !conf_file )
+        debug = 1;
+
+    if( mqttd_parser_conf(conf_file, &ctx, debug)<0 )
     {
         fprintf(stderr, "Parser mqtted configure file failure\n");
         return -2;
@@ -61,6 +122,8 @@
     if( check_set_program_running(daemon) < 0 ) 
         goto OUT;
 
+    if( !debug )
+
     mosquitto_lib_init();
 
     if( thread_start(&tid, mqtt_sub_worker, &ctx ) < 0 )

--
Gitblit v1.9.1