LingYun IoT Studio NB-IoT research project
guowenxue
2018-11-20 995ca76581e3f4134b1e9f3b2d9b4c3efb0855d4
Update nbiot all makefile and add nbiot parser code
10 files modified
1 files added
319 ■■■■ changed files
src/cp_library/cp_logger.c 12 ●●●● patch | view | raw | blame | history
src/cp_library/cp_logger.h 11 ●●●● patch | view | raw | blame | history
src/cp_library/makefile 1 ●●●● patch | view | raw | blame | history
src/cp_library/test/makefile 17 ●●●●● patch | view | raw | blame | history
src/nbiotd/core/iotd_conf.c 171 ●●●●● patch | view | raw | blame | history
src/nbiotd/core/iotd_conf.h 68 ●●●●● patch | view | raw | blame | history
src/nbiotd/core/makefile 1 ●●●● patch | view | raw | blame | history
src/nbiotd/iotd.c 35 ●●●● patch | view | raw | blame | history
src/nbiotd/lora/makefile 1 ●●●● patch | view | raw | blame | history
src/nbiotd/mqtt/makefile 1 ●●●● patch | view | raw | blame | history
src/nbiotd/nbiot/makefile 1 ●●●● patch | view | raw | blame | history
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; 
    }
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);
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)
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}
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;
}
src/nbiotd/core/iotd_conf.h
New file
@@ -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_  ----- */
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)
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 )
    {
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)
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)
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)