guowenxue
2019-06-26 0aa3153f05fe8cd51d76fd5dee9011a7d2296b7d
update mqttd, add command line options parser
4 files modified
104 ■■■■ changed files
mqttd/etc/conf.c 28 ●●●● patch | view | raw | blame | history
mqttd/etc/mqttd.conf 2 ●●● patch | view | raw | blame | history
mqttd/main.c 73 ●●●●● patch | view | raw | blame | history
mqttd/makefile 1 ●●●● patch | view | raw | blame | history
mqttd/etc/conf.c
@@ -25,28 +25,42 @@
    if( !conf_file )
    { 
        fprintf(stderr, "\nWARNNING: Use default MQTT configure\n");
        /* logger settings */
        strncpy(ctx->logfile, DBG_LOG_FILE, sizeof(ctx->logfile));
        ctx->loglevel = LOG_LEVEL_DEBUG;
        ctx->logsize = 1024;
        if( logger_init(ctx->logfile, ctx->loglevel, ctx->logsize) < 0 )
        {
            fprintf(stderr, "Logger system initialise failure\n");
            return -2;
        }
        log_nrml("Logger system initialise ok\n");
        log_warn("WARNNING: Use default MQTT configure\n");
        /* Broker settings */
        strncpy(ctx->host, DEF_BORKER_HOSTNAME, sizeof(ctx->host));
        ctx->port = DEF_BROKER_PORT;
        log_nrml("Use default broker server [%s:%d]\n", ctx->host, ctx->port);
        strncpy(ctx->uid, DEF_BROKER_USERNAME, sizeof(ctx->uid));
        strncpy(ctx->pwd, DEF_BROKER_PASSWD, sizeof(ctx->pwd));
        log_nrml("Use default broker author by [%s:%s]\n", ctx->uid, ctx->pwd);
        ctx->keepalive = DEF_BROKER_KEEPALIVE;
        log_nrml("Use default broker keepalive timeout [%d] seconds\n", ctx->keepalive);
        
        /* Publisher settings */
        strncpy(ctx->pubTopic, DEF_PUBTOPIC, sizeof(ctx->pubTopic));
        ctx->pubQos = DEF_PUBQOS;
        ctx->interval = DEF_PUBINTERVAL;
        log_nrml("Use default publisher topic \"%s\" with Qos[%d] interval[%d]\n", ctx->pubTopic, ctx->pubQos, ctx->interval);
        /* Subscriber settings */
        strncpy(ctx->subTopic, DEF_SUBTOPIC, sizeof(ctx->subTopic));
        ctx->subQos = DEF_SUBQOS;
        log_nrml("Use default subscriber topic \"%s\" with Qos[%d]\n", ctx->subTopic, ctx->subQos);
        return 0;
    }
@@ -103,7 +117,7 @@
        return -2;
    }
    ctx->port = val;
    log_nrml("Parser and get broker server [%s:%d]\n", ctx->host, ctx->port);
    log_nrml("Parser broker server [%s:%d]\n", ctx->host, ctx->port);
    str=iniparser_getstring(ini, "broker:username", DEF_BROKER_USERNAME);
    strncpy(ctx->uid, str, sizeof(ctx->uid) );
@@ -111,10 +125,10 @@
    str=iniparser_getstring(ini, "broker:password", DEF_BROKER_PASSWD);
    strncpy(ctx->pwd, str, sizeof(ctx->pwd) );
    log_nrml("Parser and get broker author by [%s:%s]\n", ctx->uid, ctx->pwd);
    log_nrml("Parser broker author by [%s:%s]\n", ctx->uid, ctx->pwd);
    ctx->keepalive = iniparser_getint(ini, "broker:keepalive", DEF_BROKER_KEEPALIVE);
    log_nrml("Parser and get broker keepalive timeout [%d] seconds\n", ctx->keepalive);
    log_nrml("Parser broker keepalive timeout [%d] seconds\n", ctx->keepalive);
    /*+------------------------------------------------------+
     *|             parser publisher settings                |
@@ -122,11 +136,10 @@
    str=iniparser_getstring(ini, "publisher:pubTopic", DEF_PUBTOPIC);
    strncpy(ctx->pubTopic, str, sizeof(ctx->pubTopic) );
    log_nrml("Parser and get publisher topic \"%s\"\n", ctx->pubTopic);
    ctx->pubQos = iniparser_getint(ini, "publisher:pubQos", DEF_PUBQOS);
    ctx->interval = iniparser_getint(ini, "publisher:interval", DEF_PUBINTERVAL);
    log_nrml("Parser and get publisher Qos[%d] and interval [%d] seconds\n", ctx->pubQos, ctx->interval);
    log_nrml("Parser publisher topic \"%s\" with Qos[%d] interval[%d]\n", ctx->pubTopic, ctx->pubQos, ctx->interval);
    /*+------------------------------------------------------+
     *|             parser subscriber settings               |
@@ -134,10 +147,9 @@
    str=iniparser_getstring(ini, "subsciber:subTopic", DEF_SUBTOPIC);
    strncpy(ctx->subTopic, str, sizeof(ctx->subTopic) );
    log_nrml("Parser and get subscriber topic \"%s\"\n", ctx->subTopic);
    ctx->subQos = iniparser_getint(ini, "subsciber:subQos", DEF_SUBQOS);
    log_nrml("Parser and get subscriber Qos[%d]\n", ctx->subQos);
    log_nrml("Parser subscriber topic \"%s\" with Qos[%d]\n", ctx->subTopic, ctx->subQos);
    return 0;
}
mqttd/etc/mqttd.conf
@@ -36,7 +36,7 @@
pubQos=2
# Publisher上报传感器数据的周期
interval=60
interval=5
[subsciber]
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 )
mqttd/makefile
@@ -72,6 +72,7 @@
clean: 
    @make clean -C hal
    @make clean -C etc
    @make clean -C lylib
    @rm -f version.h 
    @rm -f *.o $(IMAGE_NAME)