From 995ca76581e3f4134b1e9f3b2d9b4c3efb0855d4 Mon Sep 17 00:00:00 2001
From: guowenxue <guowenxue@gmail.com>
Date: Tue, 20 Nov 2018 13:28:47 +0800
Subject: [PATCH] Update nbiot all makefile and add nbiot parser code

---
 src/nbiotd/iotd.c            |   35 ++---
 src/nbiotd/mqtt/makefile     |    1 
 src/nbiotd/core/iotd_conf.c  |  171 ++++++++++++++++++++++++++++
 src/nbiotd/core/makefile     |    1 
 src/cp_library/test/makefile |   17 --
 src/cp_library/cp_logger.h   |   11 +
 src/nbiotd/nbiot/makefile    |    1 
 src/nbiotd/lora/makefile     |    1 
 src/cp_library/cp_logger.c   |   12 +-
 src/cp_library/makefile      |    1 
 src/nbiotd/core/iotd_conf.h  |   68 +++++++++++
 11 files changed, 274 insertions(+), 45 deletions(-)

diff --git a/src/cp_library/cp_logger.c b/src/cp_library/cp_logger.c
index 7eb89aa..03ce722 100644
--- a/src/cp_library/cp_logger.c
+++ b/src/cp_library/cp_logger.c
@@ -2,7 +2,7 @@
  *      Copyright:  (C) 2012 Guo Wenxue <guowenxue@gmail.com>
  *                  All rights reserved.
  *
- *       Filename:  cp_log.c
+ *       Filename:  cp_logger.c
  *    Description:  This file is the linux infrastructural logger system library
  *                 
  *        Version:  1.0.0(08/08/2012~)
@@ -18,7 +18,7 @@
 
 static unsigned long log_rollback_size = LOG_ROLLBACK_NONE;
 
-static cp_logger *logger = NULL;
+static logger_t *logger = NULL;
 
 char *log_str[LOG_LEVEL_MAX + 1] = { "", "F", "E", "W", "N", "D", "I", "T", "M" };
 
@@ -84,18 +84,18 @@
     }
 }
 
-cp_logger *cp_log_init(cp_logger *log, char *filename, int level, int log_size)
+logger_t *cp_log_init(logger_t *log, char *filename, int level, int log_size)
 {
     if(NULL == log)
     {
-        logger = malloc(sizeof(cp_logger));
-        memset(logger, 0, sizeof(cp_logger));
+        logger = malloc(sizeof(*logger));
+        memset(logger, 0, sizeof(*logger));
         logger->flag |= CP_LOGGER_MALLOC; 
     }
     else
     {
         logger = log;
-        memset(logger, 0, sizeof(cp_logger));
+        memset(logger, 0, sizeof(*logger));
         logger->flag |= CP_LOGGER_ARGUMENT; 
     }
 
diff --git a/src/cp_library/cp_logger.h b/src/cp_library/cp_logger.h
index ae39c55..ef2d862 100644
--- a/src/cp_library/cp_logger.h
+++ b/src/cp_library/cp_logger.h
@@ -11,8 +11,8 @@
  *                 
  ********************************************************************************/
 
-#ifndef __CP_LOG_H
-#define __CP_LOG_H
+#ifndef __CP_LOGGER_H
+#define __CP_LOGGER_H
 
 #include <stdarg.h>
 #include <stdio.h>
@@ -35,6 +35,7 @@
 
 #define DEFAULT_LOGFILE             "cp_logger.log"
 #define DBG_LOG_FILE                "console"  /*  Debug mode log file is console */ 
+#define DEF_LOG_MAXSIZE             1024 
 
 #define LOG_ROLLBACK_SIZE           512    /* Default rollback log size  */
 #define LOG_ROLLBACK_NONE           0      /* Set rollback size to 0 will not rollback  */
@@ -65,7 +66,7 @@
 #define CP_LOGGER_FILE                0<<1
 
 #define CP_LOGGER_LEVEL_OPT           1<<2 /*  The log level is sepcified by the command option */
-typedef struct _cp_logger
+typedef struct logger_s
 {
     unsigned char      flag;  /* This logger pointer is malloc() or passed by argument */
     char               file[FILENAME_LEN];
@@ -73,7 +74,7 @@
     int                size;
 
     FILE               *fp;
-} cp_logger;
+} logger_t;
 
 extern char *log_str[];
 
@@ -83,7 +84,7 @@
 void cp_log_set_time_format(char *time_format);
 
 
-extern cp_logger *cp_log_init(cp_logger *log, char *filename, int level, int log_size);
+extern logger_t *cp_log_init(logger_t *log, char *filename, int level, int log_size);
 extern int  cp_log_open(void);
 extern int  cp_log_reopen(void);
 extern void cp_log_close(void);
diff --git a/src/cp_library/makefile b/src/cp_library/makefile
index 366a7c0..e8223ca 100644
--- a/src/cp_library/makefile
+++ b/src/cp_library/makefile
@@ -20,6 +20,7 @@
 DYNLIB=lib${LIBNAME}.so
 
 CROSS_COMPILE?=/opt/rpi/arm-bcm2708/arm-linux-gnueabihf/bin/arm-linux-gnueabihf-
+LINK_MODE=STATIC
 
 VPATH= .
 SRCS = $(wildcard ${VPATH}/*.c)
diff --git a/src/cp_library/test/makefile b/src/cp_library/test/makefile
index f1d11c8..697a554 100644
--- a/src/cp_library/test/makefile
+++ b/src/cp_library/test/makefile
@@ -21,31 +21,16 @@
 ARCH?=arm
 
 LINK_MODE=STATIC
-MODE=PRODUCTION
-DEBUG=1
 
 CFLAGS+=-Wall -Werror
 #CFLAGS+=-Wno-unused
-
-ifeq ("${MODE}", "PRODUCTION")
-    CFLAGS+=-DPRODUCTION_MODE
-endif
-ifdef DEBUG
-    CFLAGS+=-g -DDEBUG
-endif
 
 COMPILE_DATE=$(shell date -u +"%Y-%m-%d %H:%M")
 VPATH= .
 SRCS = $(wildcard ${VPATH}/*.c)
 OBJS = $(patsubst %.c,%.o,$(SRCS))
 
-TMP=$(shell echo $(ARCH) | tr "[A-Z]" "[a-z]")
-ifneq (,$(filter i386,$(TMP)))
-    CROSS_COMPILE=
-else
-    CROSS_COMPILE=/opt/rpi/arm-bcm2708/arm-linux-gnueabihf/bin/arm-linux-gnueabihf-
-endif
-
+CROSS_COMPILE?=/opt/rpi/arm-bcm2708/arm-linux-gnueabihf/bin/arm-linux-gnueabihf-
 
 CFLAGS+=-I${LIB_PATH}
 LDFLAGS+=-L${LIB_PATH} -l${LIB_NAME}
diff --git a/src/nbiotd/core/iotd_conf.c b/src/nbiotd/core/iotd_conf.c
index e69de29..036db5f 100644
--- a/src/nbiotd/core/iotd_conf.c
+++ b/src/nbiotd/core/iotd_conf.c
@@ -0,0 +1,171 @@
+/*********************************************************************************
+ *      Copyright:  (C) 2018 LingYun IoT System Studio
+ *                  All rights reserved.
+ *
+ *       Filename:  iotd_conf.c
+ *    Description:  This file is for iotd configure file parser API
+ *                 
+ *        Version:  1.0.0(2018年11月20日)
+ *         Author:  Guo Wenxue <guowenxue@gmail.com>
+ *      ChangeLog:  1, Release initial version on "2018年11月20日 12时11分46秒"
+ *                 
+ ********************************************************************************/
+#include "iotd_conf.h"
+#include "cp_iniparser.h"
+
+int parser_iotd_conf(char *conf_file, iotd_conf_t *iotd_conf)
+{
+    dictionary          *ini;
+    char                *str;
+    int                  data;
+    logger_t            *logger;
+    nbiot_conf_t        *nbiot;
+    lora_conf_t         *lora;
+    mqtt_conf_t         *mqtt;
+
+
+    if(!conf_file || !iotd_conf )
+    {
+        log_err("Invalid input arguments\n");
+        return -1;
+    }
+
+    ini = iniparser_load(conf_file);
+    if (ini==NULL) 
+    {   
+        log_err("cannot parse file: %s\n", conf_file);
+        return -2 ;
+    }   
+
+    memset(iotd_conf, 0, sizeof(*iotd_conf));
+    logger = &iotd_conf->logger;
+    nbiot = &iotd_conf->nbiot_conf;
+    lora = &iotd_conf->lora_conf;
+    mqtt = &iotd_conf->mqtt_conf;
+
+    /*+----------------------------------+
+      |  Program  logger configuration   |
+      +----------------------------------+*/
+
+    str = iniparser_getstring(ini, "log:file", NULL);
+    strncpy(logger->file, (strlen(str)>0 ? str : DEF_LOG_FILE), FILENAME_LEN);
+    log_nrml("Set log file [%s] from configuration.\n", logger->file);
+
+    /* If not set the log level from command line, then use the configure one*/
+    if( !(logger->flag&CP_LOGGER_LEVEL_OPT) )
+    {
+        logger->level = iniparser_getint(ini, "log:level", LOG_LEVEL_NRML);
+        log_nrml("Set log level[%d] from configuration.\n", logger->level);
+    }
+
+    /*  Set the log maximum file size in the configure file */
+    logger->size = iniparser_getint(ini, "log:size", DEF_LOG_MAXSIZE);
+    logger->size = logger->size>DEF_LOG_MAXSIZE ? DEF_LOG_MAXSIZE : logger->size;
+    log_nrml("Set log size [%dKiB] from configuration\n", logger->size);
+
+
+    /*+----------------------------------+
+      |    NB-IoT Module configuration   |
+      +----------------------------------+*/
+
+    data = iniparser_getint(ini, "nbiot:enable", 0);
+    nbiot->enable = data? ENABLE : DISABLE;
+    printf("Confiugre NB-IoT thread [%s]\n", nbiot->enable ? "enable" : "disable");
+
+    str = iniparser_getstring(ini, "nbiot:comport", NULL);
+    if( NULL!=str && strlen(str) > 0 )
+    {
+        strncpy(nbiot->comport, str, sizeof(nbiot->comport));
+    }
+    else
+    {
+        log_err("Configure without NB-IoT comport device, thread will not start\n");
+        nbiot->enable = DISABLE;
+    }
+    log_nrml("Configure NB-IoT comport device as '%s'\n", lora->comport);
+
+    nbiot->baudrate = iniparser_getint(ini, "nbiot:baudrate", 9600);
+    log_nrml("Configure NB-IoT comport baudrate as '%d'\n", nbiot->baudrate);
+
+    str = iniparser_getstring(ini, "nbiot:settings", "8N1N");
+    if( NULL!=str && strlen(str) >= 4 )
+    {
+        strncpy(nbiot->settings, str, 4);
+        log_nrml("Configure NB-IoT comport settings as '%s'\n", nbiot->settings);
+    }
+    else
+    {
+        log_err("Configure Invalid NB-IoT comport settings '%s', thread will not start.\n", str);
+        nbiot->enable = DISABLE;
+    }
+
+
+    /*+----------------------------------+
+      |     LoRa Module configuration    |
+      +----------------------------------+*/
+
+    data = iniparser_getint(ini, "lora:enable", 0);
+    lora->enable = data? ENABLE : DISABLE;
+    printf("Confiugre LoRa thread [%s]\n", lora->enable ? "enable" : "disable");
+
+    str = iniparser_getstring(ini, "lora:comport", NULL);
+    if( NULL!=str && strlen(str) > 0 )
+    {
+        strncpy(lora->comport, str, sizeof(lora->comport));
+    }
+    else
+    {
+        log_err("Configure without LoRa comport device, thread will not start\n");
+        lora->enable = DISABLE;
+    }
+    log_nrml("Configure LoRa comport device as '%s'\n", lora->comport);
+
+    lora->baudrate = iniparser_getint(ini, "lora:baudrate", 115200);
+    log_nrml("Configure LoRa comport baudrate as '%d'\n", lora->baudrate);
+
+    str = iniparser_getstring(ini, "lora:settings", "8N1N");
+    if( NULL!=str && strlen(str) >= 4 )
+    {
+        strncpy(lora->settings, str, 4);
+        log_nrml("Configure LoRa comport settings as '%s'\n", lora->settings);
+    }
+    else
+    {
+        log_err("Configure Invalid LoRa comport settings '%s', thread will not start.\n", str);
+        lora->enable = DISABLE;
+    }
+
+
+    /*+----------------------------------+
+      |     MQTT Server configuration    |
+      +----------------------------------+*/
+
+    data = iniparser_getint(ini, "mqtt:enable", 0);
+    mqtt->enable = data? ENABLE : DISABLE;
+    printf("Confiugre MQTT thread [%s]\n", lora->enable ? "enable" : "disable");
+
+    str = iniparser_getstring(ini, "mqtt:hostname", NULL);
+    if( !str || strlen(str) <= 0 )
+    {
+        log_err("Configure MQTT Server hostname invalid, thread will not start\n");
+        mqtt->enable = DISABLE;
+    }
+    strncpy(mqtt->hostname, str, HOSTN_LEN);
+    log_nrml("Configure MQTT Server hostname: \"%s\" \n", mqtt->hostname);
+
+    data = iniparser_getint(ini, "mqtt:port", 0);
+    if( !data )
+    {
+        log_err("Configure MQTT Server port invalid, thread will not start\n");
+        mqtt->enable = DISABLE;
+    }
+    mqtt->port = data;
+    log_nrml("Configure MQTT Server port: [%d]\n", mqtt->port);
+
+
+
+    iniparser_freedict(ini);
+
+    return 0;
+}
+
diff --git a/src/nbiotd/core/iotd_conf.h b/src/nbiotd/core/iotd_conf.h
new file mode 100644
index 0000000..7445b7e
--- /dev/null
+++ b/src/nbiotd/core/iotd_conf.h
@@ -0,0 +1,68 @@
+/********************************************************************************
+ *      Copyright:  (C) 2018 LingYun IoT System Studio
+ *                  All rights reserved.
+ *
+ *       Filename:  iotd_conf.h
+ *    Description:  This file is for iotd configure file parser API
+ *
+ *        Version:  1.0.0(2018年11月20日)
+ *         Author:  Guo Wenxue <guowenxue@gmail.com>
+ *      ChangeLog:  1, Release initial version on "2018年11月20日 12时28分20秒"
+ *                 
+ ********************************************************************************/
+#ifndef  _IOTD_CONF_H_
+#define  _IOTD_CONF_H_
+
+#include "cp_logger.h"
+
+#define DISABLE                 0
+#define ENABLE                  1
+
+#define HOSTN_LEN              64
+
+#ifndef FILEN_LEN 
+#define FILEN_LEN              64
+#endif
+
+#define DEF_LOG_FILE         "/var/log/iotd.log"
+#define DEF_LOG_LEVEL        LOG_LEVEL_NRML
+
+typedef struct lora_conf_s 
+{
+    int               enable;
+    char              comport[FILEN_LEN];    /*  Comport device name */
+    int               baudrate;              /*  Baudrate, default 9600 */  
+    char              settings[5];           /*  "8N1N", DataBit[8/7],Parity[O/E/N],StopBit[1/0],FlowControl[S/H/N] */ 
+} lora_conf_t;  
+
+
+typedef struct nbiot_conf_s 
+{
+    unsigned char     enable;
+    char              comport[FILEN_LEN];    /*  Comport device name */
+    int               baudrate;              /*  Baudrate, default 9600 */  
+    char              settings[5];           /*  "8N1N", DataBit[8/7],Parity[O/E/N],StopBit[1/0],FlowControl[S/H/N] */ 
+} nbiot_conf_t;  
+
+
+typedef struct mqtt_conf_s 
+{
+    unsigned char      enable;  /* Enable this thread or not */
+    char               hostname[HOSTN_LEN];   /*  Remote MQTT server hostname: IP or Domain name format */
+    unsigned short     port; /* MQTT Server listen port */
+} mqtt_conf_t;  
+
+
+typedef struct iotd_conf_s 
+{
+    logger_t          logger;
+    lora_conf_t       lora_conf;
+    nbiot_conf_t      nbiot_conf;
+    mqtt_conf_t       mqtt_conf;
+} iotd_conf_t;  
+
+
+extern int parser_iotd_conf(char *conf_file, iotd_conf_t *iotd_conf);
+
+#endif   /* ----- #ifndef _IOTD_CONF_H_  ----- */
+
diff --git a/src/nbiotd/core/makefile b/src/nbiotd/core/makefile
index 366a7c0..e8223ca 100644
--- a/src/nbiotd/core/makefile
+++ b/src/nbiotd/core/makefile
@@ -20,6 +20,7 @@
 DYNLIB=lib${LIBNAME}.so
 
 CROSS_COMPILE?=/opt/rpi/arm-bcm2708/arm-linux-gnueabihf/bin/arm-linux-gnueabihf-
+LINK_MODE=STATIC
 
 VPATH= .
 SRCS = $(wildcard ${VPATH}/*.c)
diff --git a/src/nbiotd/iotd.c b/src/nbiotd/iotd.c
index d901ab4..061618d 100644
--- a/src/nbiotd/iotd.c
+++ b/src/nbiotd/iotd.c
@@ -23,21 +23,11 @@
 #include <sys/stat.h>
 #include <fcntl.h>
 
-#include "cp_logger.h"
 #include "cp_proc.h"
+#include "iotd_conf.h"
 
-#define DEF_LOG_FILE         "/var/log/iotd.log"
-#define DEF_LOG_LEVEL        LOG_LEVEL_NRML
 
 void *watchdog_worker(void *arg);
-
-
-
-typedef struct iotd_conf_s 
-{
-    cp_logger       logger;
-    
-} iotd_conf_t;  
 
 
 typedef struct iotd_ctx_s 
@@ -85,7 +75,7 @@
 
     pthread_t             tid;
     iotd_conf_t          *conf; 
-    cp_logger            *logger;
+    logger_t             *logger;
 
     memset(&iotd_ctx, 0, sizeof(iotd_ctx));
     conf = &iotd_ctx.conf;
@@ -187,16 +177,25 @@
     thread_start(&tid, watchdog_worker, NULL);
 
     /* Start MQTT thread worker */
-    log_nrml("start MQTT socket process thread\n");
-    //thread_start(&tid, mqtt_worker, (void *)&iotd_ctx );
+    if( conf->mqtt_conf.enable )
+    {
+        log_nrml("start MQTT publish process thread\n"); 
+        //thread_start(&tid, mqtt_worker, (void *)&iotd_ctx );
+    }
 
     /* Start NB-IoT thread worker */
-    log_nrml("start NB-IoT process thread\n");
-    //thread_start(&tid, nbiot_worker, (void *)&iotd_ctx );
+    if( conf->nbiot_conf.enable )
+    {
+        log_nrml("start NB-IoT process thread\n");
+        //thread_start(&tid, nbiot_worker, (void *)&iotd_ctx );
+    }
 
     /* Start LoRa thread worker */
-    log_nrml("start LoRa AP process thread\n");
-    //thread_start(&tid, nbiot_worker, (void *)&iotd_ctx );
+    if( conf->nbiot_conf.enable )
+    {
+        log_nrml("start LoRa AP process thread\n");
+        //thread_start(&tid, nbiot_worker, (void *)&iotd_ctx );
+    }
 
     while( !g_cp_signal.stop )
     {
diff --git a/src/nbiotd/lora/makefile b/src/nbiotd/lora/makefile
index 366a7c0..e8223ca 100644
--- a/src/nbiotd/lora/makefile
+++ b/src/nbiotd/lora/makefile
@@ -20,6 +20,7 @@
 DYNLIB=lib${LIBNAME}.so
 
 CROSS_COMPILE?=/opt/rpi/arm-bcm2708/arm-linux-gnueabihf/bin/arm-linux-gnueabihf-
+LINK_MODE=STATIC
 
 VPATH= .
 SRCS = $(wildcard ${VPATH}/*.c)
diff --git a/src/nbiotd/mqtt/makefile b/src/nbiotd/mqtt/makefile
index 366a7c0..e8223ca 100644
--- a/src/nbiotd/mqtt/makefile
+++ b/src/nbiotd/mqtt/makefile
@@ -20,6 +20,7 @@
 DYNLIB=lib${LIBNAME}.so
 
 CROSS_COMPILE?=/opt/rpi/arm-bcm2708/arm-linux-gnueabihf/bin/arm-linux-gnueabihf-
+LINK_MODE=STATIC
 
 VPATH= .
 SRCS = $(wildcard ${VPATH}/*.c)
diff --git a/src/nbiotd/nbiot/makefile b/src/nbiotd/nbiot/makefile
index 366a7c0..e8223ca 100644
--- a/src/nbiotd/nbiot/makefile
+++ b/src/nbiotd/nbiot/makefile
@@ -20,6 +20,7 @@
 DYNLIB=lib${LIBNAME}.so
 
 CROSS_COMPILE?=/opt/rpi/arm-bcm2708/arm-linux-gnueabihf/bin/arm-linux-gnueabihf-
+LINK_MODE=STATIC
 
 VPATH= .
 SRCS = $(wildcard ${VPATH}/*.c)

--
Gitblit v1.9.1