LingYun IoT Studio NB-IoT research project
guowenxue
2018-11-19 3509b6dfaad073c2b7887031c917c12b8ed5caa1
Add iotd program
11 files added
785 ■■■■■ changed files
src/nbiotd/core/iotd_conf.c patch | view | raw | blame | history
src/nbiotd/core/makefile 82 ●●●●● patch | view | raw | blame | history
src/nbiotd/etc/iotd.conf 32 ●●●●● patch | view | raw | blame | history
src/nbiotd/iotd.c 249 ●●●●● patch | view | raw | blame | history
src/nbiotd/lora/lora_api.c patch | view | raw | blame | history
src/nbiotd/lora/makefile 82 ●●●●● patch | view | raw | blame | history
src/nbiotd/makefile 176 ●●●●● patch | view | raw | blame | history
src/nbiotd/mqtt/makefile 82 ●●●●● patch | view | raw | blame | history
src/nbiotd/mqtt/mqtt_api.c patch | view | raw | blame | history
src/nbiotd/nbiot/makefile 82 ●●●●● patch | view | raw | blame | history
src/nbiotd/nbiot/nbiot_api.c patch | view | raw | blame | history
src/nbiotd/core/iotd_conf.c
src/nbiotd/core/makefile
New file
@@ -0,0 +1,82 @@
#*********************************************************************************
#      Copyright:  (C) 2012 CoherentPlus Sdn. Bhd.
#                  All rights reserved.
#
#       Filename:  Makefile
#    Description:  This is the common subdir Makefile which to compile all the C
#                  source code to object files and then generate the shared or
#                  static library named lib$(FOLDER_NAME).a orlib $(FOLDER_NAME).so,
#                  which depends on the variable $LINK_MODE.
#
#        Version:  1.0.0(10/08/2011~)
#                  Author:  Guo Wenxue <guowenxue@gmail.com>
#      ChangeLog:  1, Release initial version on "10/08/2011 01:29:33 AM"
#
#********************************************************************************/
PWD=$(shell pwd)
#TOP_COMPILE=YES
#If wanna compile in the subdir, not called by top makefile, uncomment it
ifneq (${TOP_COMPILE}, YES)
LOCAL_COMPILE=YES
endif
LIBNAME=$(shell basename ${PWD})
STALIB=lib${LIBNAME}.a
DYNLIB=lib${LIBNAME}.so
VPATH= .
SRCS = $(wildcard ${VPATH}/*.c)
OBJS = $(patsubst %.c,%.o,$(SRCS))
#======================================================
#  ---> Doesn't call by top makefile, compile by local
#======================================================
CROSS_COMPILE?=/opt/rpi/arm-bcm2708/arm-linux-gnueabihf/bin/arm-linux-gnueabihf-
PRJDIR?=$(shell dirname ${PWD})
CFLAGS+=-I${PRJDIR}
CC = ${CROSS_COMPILE}gcc
AR = ${CROSS_COMPILE}ar
ifeq ("${LINK_MODE}", "STATIC")
    LIBS = ${STALIB}
else
    LIBS=${DYNLIB}
endif
all: entry ${LIBS} install
entry:
    @echo " ";
    @echo " =========================================================";
    @echo " **     Compile subdir ${LIBNAME} for ${ARCH}             ";
    @echo " =========================================================";
#$(LD) -g --relocatable $(OBJS) -o lib${LIBNAME}.o
${STALIB}:    $(OBJS)
    $(AR) -rcu $@ $(OBJS)
${DYNLIB}:   $(OBJS)
    $(CC) -fPIC -shared -o $@ $(OBJS)
%.o : %.c
    $(CC) -c $< $(CFLAGS)
install:
    @if [ ! -z "${LIBS_PATH}" ] ; then \
        mkdir -p ${LIBS_PATH} ; \
        cp ${LIBS} ${LIBS_PATH}; \
    fi;
clean:
    @rm -f *.o
    @rm -rf *.gdb *.a *.so
distclean: clean
    @rm -f  tags cscope*
.PHONY: clean entry
src/nbiotd/etc/iotd.conf
New file
@@ -0,0 +1,32 @@
# uited program configure file
[log]
file=/var/log/nbiotd.log
#Loglevel: 0:Disable 1:Fatal 2:ERROR 3:warnning 4:Normal 5:Debug 6:Infor 7:Trace
level=5
#Unit Kib
size=1024
[nbiot]
enable=1
comport="/dev/ttyUSB0"
baudrate=9600
#Settings format is: Databit[8/7],Parity[O/E/S/N],StopBit[1/0],FlowControl[S/H/N]
settings="8N1N"
[lora]
enable=0
comport="/dev/ttyS1"
baudrate=115200
#Settings format is: Databit[8/7],Parity[O/E/S/N],StopBit[1/0],FlowControl[S/H/N]
settings="8N1N"
[mqtt]
enable=1
hostname=master.iot-yun.com
port=8107
src/nbiotd/iotd.c
New file
@@ -0,0 +1,249 @@
/*********************************************************************************
 * Copyright:  (C) 2018 LingYun IoT Studio
 *                  All rights reserved.
 *       Filename:  iotd_main.c
 *    Description:  This file is iotd (Internet of Things daemon) program main() entry point,
 *
 *        Version:  1.0.0(2018/11/19)
 *         Author:  Guo Wenxue <guowenxue@aliyun.com>
 *      ChangeLog:  1, Release initial version on "2018/11/19 22:05:56 CST"
 *
 ********************************************************************************/
#include <stdio.h>
#include <getopt.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <signal.h>
#include <linux/input.h>
#include <linux/kd.h>
#include <linux/keyboard.h>
#include <linux/watchdog.h>
#include <libgen.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include "cp_logger.h"
#include "cp_proc.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
{
    iotd_conf_t     conf;
} iotd_ctx_t;
void banner(const char *program)
{
    printf("Copyright (C) 2018 LingYun IoT Studio.\n");
    printf("%s Version 1.0.0 Build on @ %s %s\n", program, __DATE__, __TIME__);
}
static void program_usage(const char *progname)
{
    banner(progname);
    printf("Usage: %s [OPTION]...\n", progname);
    printf(" %s is a daemon program running on the device, which used to \n", progname);
    printf("receive date from serial and transfer the data to remote server by socket\n");
    printf("\nMandatory arguments to long options are mandatory for short options too:\n");
    printf(" -d[debug   ]  Running in debug mode\n");
    printf(" -l[level   ]  Set the log level as [0..%d]\n", LOG_LEVEL_MAX-1);
    printf(" -h[help    ]  Display this help information\n");
    printf(" -v[version ]  Display the program version\n");
    return;
}
int main(int argc, char **argv)
{
    int                   opt;
    int                   i = 0;
    int                   rv = 0;
    int                   debug = 0;
    const char           *progname=NULL;
    char                  pid_file[64] = { 0 }; /*  The file used to record the PID */
    char                 *conf_file = "iotd.conf";
    char                 *log_file = DEF_LOG_FILE;
    int                   log_level = DEF_LOG_LEVEL;
    iotd_ctx_t            iotd_ctx;
    pthread_t             tid;
    iotd_conf_t          *conf;
    cp_logger            *logger;
    memset(&iotd_ctx, 0, sizeof(iotd_ctx));
    conf = &iotd_ctx.conf;
    logger = &conf->logger;
    struct option long_options[] = {
        {"conf", required_argument, NULL, 'c'},
        {"debug", no_argument, NULL, 'd'},
        {"level", required_argument, NULL, 'l'},
        {"version", no_argument, NULL, 'v'},
        {"help", no_argument, NULL, 'h'},
        {NULL, 0, NULL, 0}
    };
    progname = basename(argv[0]);
    /*  Parser the command line parameters */
    while ((opt = getopt_long(argc, argv, "c:dl:vh", long_options, NULL)) != -1)
    {
        switch (opt)
        {
            case 'c':  /*  Set configure file */
                conf_file = optarg;
                break;
            case 'd': /*  Set debug running */
                debug = 1;
                log_file = DBG_LOG_FILE;
                break;
            case 'l': /*  Set the log level */
                i = atoi(optarg);
                log_level = i>LOG_LEVEL_MAX ? LOG_LEVEL_MAX-1 : i;
                logger->flag |= CP_LOGGER_LEVEL_OPT;
                break;
            case 'v':  /*  Get software version */
                banner(progname); /*  Defined in version.h */
                return EXIT_SUCCESS;
            case 'h':  /*  Get help information */
                program_usage(progname);
                return 0;
            default:
                break;
        } /*   end of "switch(opt)" */
    }
    snprintf(pid_file, sizeof(pid_file), "/var/run/%s.pid", progname);
    if( !debug )
    {
        if( check_daemon_running(pid_file) )
        {
            printf("Programe already running, exit now.\n");
            return -1;
        }
    }
    if ( !cp_log_init(logger, log_file, log_level, 1024/*KB*/) || cp_log_open() )
    {
        printf("Logger initialize failure with file: %s\n", logger->file);
        return -1;
    }
    /*  Parser configure file and start the initialize log */
    rv = parser_iotd_conf(conf_file, conf);
    if(rv < 0)
    {
        log_fatal("Parse configure file %s failure, rv=%d\n", conf_file, rv);
        goto CleanUp;
    }
    /* If configure file logger is not same as default one, then reopen the logger system */
    if(  !debug && strncmp(logger->file, DEF_LOG_FILE, strlen(DEF_LOG_FILE)) )
    {
        log_warn("Reinitiase logger to [%s]\n", logger->file);
        cp_log_close();
        if( cp_log_open() )
        {
            printf("Logger reopen failure with file: %s\n", logger->file);
            return -1;
        }
        printf("Open logger ok\n");
    }
    if( !debug )
    {
        if( set_daemon_running(pid_file) )
        {
            log_fatal("Set program \"%s\" running as daemon failure.\n", progname);
            goto CleanUp;
        }
    }
    cp_install_proc_signal();
    /* start watchdog thread worker  */
    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 );
    /* Start NB-IoT thread worker */
    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 );
    while( !g_cp_signal.stop )
    {
        sleep(1);
    }
CleanUp:
    log_warn("Terminate main program \"%s\".\n", progname);
    unlink(pid_file);
    cp_log_term();
    return rv;
}
#define WTD_DEV          "/dev/watchdog"
void *watchdog_worker(void *arg)
{
    int                      wtd_fd = -1;
    int                      timeout = 120; /* 2 minutes */
    int                      times = 0;
    log_nrml("start watchdog thread\n");
    if( (wtd_fd=open(WTD_DEV, O_RDWR)) < 0 )
    {
        log_err("Open watchdog '%s' failure: %s\n", WTD_DEV, strerror(errno));
        return NULL;
    }
    ioctl(wtd_fd, WDIOC_SETOPTIONS, WDIOS_ENABLECARD);
    ioctl(wtd_fd, WDIOC_SETTIMEOUT, &timeout);
    log_nrml("Start watchdog on timeout %d seconds\n", timeout);
    while( !g_cp_signal.stop )
    {
        if( times++ > 20 )
        {
            log_dbg("start feed watchdog now\n");
            ioctl(wtd_fd, WDIOC_KEEPALIVE, NULL);
            times = 0;
        }
        sleep(1);
    }
    close(wtd_fd);
    printf("watchdog thread worker exit\n");
    return NULL;
}
src/nbiotd/lora/lora_api.c
src/nbiotd/lora/makefile
New file
@@ -0,0 +1,82 @@
#*********************************************************************************
#      Copyright:  (C) 2012 CoherentPlus Sdn. Bhd.
#                  All rights reserved.
#
#       Filename:  Makefile
#    Description:  This is the common subdir Makefile which to compile all the C
#                  source code to object files and then generate the shared or
#                  static library named lib$(FOLDER_NAME).a orlib $(FOLDER_NAME).so,
#                  which depends on the variable $LINK_MODE.
#
#        Version:  1.0.0(10/08/2011~)
#                  Author:  Guo Wenxue <guowenxue@gmail.com>
#      ChangeLog:  1, Release initial version on "10/08/2011 01:29:33 AM"
#
#********************************************************************************/
PWD=$(shell pwd)
#TOP_COMPILE=YES
#If wanna compile in the subdir, not called by top makefile, uncomment it
ifneq (${TOP_COMPILE}, YES)
LOCAL_COMPILE=YES
endif
LIBNAME=$(shell basename ${PWD})
STALIB=lib${LIBNAME}.a
DYNLIB=lib${LIBNAME}.so
VPATH= .
SRCS = $(wildcard ${VPATH}/*.c)
OBJS = $(patsubst %.c,%.o,$(SRCS))
#======================================================
#  ---> Doesn't call by top makefile, compile by local
#======================================================
CROSS_COMPILE?=/opt/rpi/arm-bcm2708/arm-linux-gnueabihf/bin/arm-linux-gnueabihf-
PRJDIR?=$(shell dirname ${PWD})
CFLAGS+=-I${PRJDIR}
CC = ${CROSS_COMPILE}gcc
AR = ${CROSS_COMPILE}ar
ifeq ("${LINK_MODE}", "STATIC")
    LIBS = ${STALIB}
else
    LIBS=${DYNLIB}
endif
all: entry ${LIBS} install
entry:
    @echo " ";
    @echo " =========================================================";
    @echo " **     Compile subdir ${LIBNAME} for ${ARCH}             ";
    @echo " =========================================================";
#$(LD) -g --relocatable $(OBJS) -o lib${LIBNAME}.o
${STALIB}:    $(OBJS)
    $(AR) -rcu $@ $(OBJS)
${DYNLIB}:   $(OBJS)
    $(CC) -fPIC -shared -o $@ $(OBJS)
%.o : %.c
    $(CC) -c $< $(CFLAGS)
install:
    @if [ ! -z "${LIBS_PATH}" ] ; then \
        mkdir -p ${LIBS_PATH} ; \
        cp ${LIBS} ${LIBS_PATH}; \
    fi;
clean:
    @rm -f *.o
    @rm -rf *.gdb *.a *.so
distclean: clean
    @rm -f  tags cscope*
.PHONY: clean entry
src/nbiotd/makefile
New file
@@ -0,0 +1,176 @@
#*********************************************************************************
#      Copyright:  (C) 2012 CoherentPlus Sdn. Bhd.
#                  All rights reserved.
#
#       Filename:  Makefile
#    Description:  This is the common top Makefile which used to call function to
#                  Cross compile the subdir source code and link all the libs to
#                  excutable binary file.
#
#        Version:  1.0.0(10/08/2011~)
#         Author:  Guo Wenxue <guowenxue@gmail.com>
#      ChangeLog:  1, Release initial version on "10/08/2011 01:29:33 AM"
#
#********************************************************************************/
APP_BINARY_NAME=iotd
INST_PATH=/tftp
TOP_PATH=$(shell pwd)
LIBS_PATH=${TOP_PATH}/.libs
#Excutable binary file runs on platform
ARCH?=arm
#ARCH?=i386
#Static linked or Dynamic link
LINK_MODE=STATIC
CFLAGS+=-Werror -Wall
ifeq ("${LINK_MODE}", "STATIC")
    CFLAGS+=--static
    LDFLAGS+=-static
else
    LDFLAGS+=-ldl
endif
# ==================================
# **   Cross Compiler Setting     **
# ==================================
export CROSS_COMPILE=/opt/rpi/arm-bcm2708/arm-linux-gnueabihf/bin/arm-linux-gnueabihf-
# Get alll the source code folder, all the source code in the subdir will be compiled and
# archive to a static library, named lib${subdir_name}.a
OBJFILES = $(patsubst %.c,%,$(wildcard *.c))
SUBSRCS = $(shell find . -follow  -maxdepth 1 -type d|sed -n 's/.\///p'|grep -v 'libs' \
          |grep -v 'etc' | grep -v '.svn'|grep -v '.git'|grep -v 'include'|grep -v 'bin')
# Add the subdir compiled static library into LDFLAGS
#LDFLAGS+=$(patsubst %,-L%,$(SUBSRCS))
#LDFLAGS+=$(patsubst %,-l%,$(SUBSRCS))
LDFLAGS+=-L${LIBS_PATH}
LDFLAGS+=$(patsubst %,-l%,$(SUBSRCS))
LIBS=$(patsubst %,${LIBS_PATH}/lib%.a,$(SUBSRCS))
LDFLAGS+=-Xlinker "-(" $(LIBS) -Xlinker "-)"
CFLAGS+=$(patsubst %,-I${TOP_PATH}/%,$(SUBSRCS))
CFLAGS+=-I$(TOP_PATH)
LDFLAGS+=-lpthread
export CC=${CROSS_COMPILE}gcc
export CXX=${CROSS_COMPILE}gcc
export AR=${CROSS_COMPILE}ar
export AS=${CROSS_COMPILE}as
export LD=${CROSS_COMPILE}ld
export RANLIB=${CROSS_COMPILE}ranlib
export STRIP=${CROSS_COMPILE}strip
export CFLAGS
export LDFLAGS
export ARCH
export LINK_MODE
export LIBS_PATH
#A function used to goes into the sub-folder to compile
MAKEFUNC = @MakeSubDir() \
    { \
        for DIR in ${SUBSRCS}; do \
            if [ -d $${DIR} ] ; then \
                cd $${DIR}; \
                MakeSubDir; \
                if [ -f makefile -o -f Makefile ]; then \
                    pwd; \
                    make $(1); \
                    if [ "$$?" != "0" ]; then \
                        exit 1; \
                    fi; \
                fi; \
                cd ..; \
            fi; \
        done; \
        if [ -f makefile -o -f Makefile ]; then \
            make $(1); \
            if [ "$$?" != "0" ]; then \
                exit 1; \
            fi; \
        fi; \
    }; \
MAKEME = cd $(2); MakeSubDir $(1); cd ..;
LOOPMAKEFUNC = $(MAKEFUNC) $(foreach dir,$(SUBSRCS),$(call MAKEME,$(1),$(dir)))
.PHONY: all
all: entry version prepare binary install
prepare:
    @if [ ! -L cp_library  ] ; then \
        ln -s ../cp_library; \
    fi
entry:
    @echo $(SUBSRCS)
    @echo " ";
    @echo " =========================================================";
    @echo " ** Come into compile ${APP_BINARY_NAME} for ARCH $(ARCH) ";
    @echo " =========================================================";
    @echo " ";
    @make clean
version:
    @echo "/* Generated by makefile, don't Edit it by hand */" > version.h
    @echo '#define DATE "$(COMPILE_DATE)"' >> version.h
    @echo "#define MAJOR 1" >>version.h
    @echo "#define MINOR 0" >>version.h
    @echo "#define REVER 0" >>version.h
    @if [ -f .svn/entries ] ; then \
        echo "#define SVNVER `sed -n -e 4p .svn/entries`" >>version.h; \
    else \
        echo "#define SVNVER 0" >>version.h; \
    fi;
    @echo "" >> version.h
    @echo '#define version(progname) printf("%s Version %d.%d.%d Build @%05d (%s)\\n", progname, MAJOR, MINOR, REVER,SVNVER, DATE)' >> version.h
    @echo '#define copyright() printf("Copyright (C) 2012 CoherentPlus Sdn. Bhd.\\n")' >>version.h
    @echo '#define banner(progname) {version(progname); copyright(); printf("\\n");}' >>version.h
    @echo "" >> version.h
subdir:
    @$(call LOOPMAKEFUNC,all)
    @echo " ";
    @echo " =========================================================";
    @echo " ** Compile and linking ${APP_BINARY_NAME} for ${ARCH} now";
    @echo " =========================================================";
%: %.c
    $(CC) $< $(CFLAGS) -c -o $@.o
binary: subdir $(OBJFILES)
    $(CC) $(CFLAGS) -o $(APP_BINARY_NAME) *.o $(LDFLAGS)
    $(STRIP) $(APP_BINARY_NAME)
tag:
    @cscope -Rbq
    @ctags --c-kinds=+defglmnstuvx --langmap=c:.c.h.ho.hem.het.hec.hev.him.hit.hic.hiv -R .
install:
    cp $(APP_BINARY_NAME) ${INST_PATH}
uninstall:
    rm -f ${INST_PATH}/$(APP_BINARY_NAME)
clean:
    @$(call LOOPMAKEFUNC,clean)
    @rm -rf ${LIBS_PATH}
    @rm -f version.h
    @rm -f *.o $(APP_BINARY_NAME)
    @rm -f *.elf*
    @rm -f *.gdb
    @rm -f cp_library
distclean: clean
    @$(call LOOPMAKEFUNC,distclean)
clear: distclean
    @rm -f tags  cscope*
src/nbiotd/mqtt/makefile
New file
@@ -0,0 +1,82 @@
#*********************************************************************************
#      Copyright:  (C) 2012 CoherentPlus Sdn. Bhd.
#                  All rights reserved.
#
#       Filename:  Makefile
#    Description:  This is the common subdir Makefile which to compile all the C
#                  source code to object files and then generate the shared or
#                  static library named lib$(FOLDER_NAME).a orlib $(FOLDER_NAME).so,
#                  which depends on the variable $LINK_MODE.
#
#        Version:  1.0.0(10/08/2011~)
#                  Author:  Guo Wenxue <guowenxue@gmail.com>
#      ChangeLog:  1, Release initial version on "10/08/2011 01:29:33 AM"
#
#********************************************************************************/
PWD=$(shell pwd)
#TOP_COMPILE=YES
#If wanna compile in the subdir, not called by top makefile, uncomment it
ifneq (${TOP_COMPILE}, YES)
LOCAL_COMPILE=YES
endif
LIBNAME=$(shell basename ${PWD})
STALIB=lib${LIBNAME}.a
DYNLIB=lib${LIBNAME}.so
VPATH= .
SRCS = $(wildcard ${VPATH}/*.c)
OBJS = $(patsubst %.c,%.o,$(SRCS))
#======================================================
#  ---> Doesn't call by top makefile, compile by local
#======================================================
CROSS_COMPILE?=/opt/rpi/arm-bcm2708/arm-linux-gnueabihf/bin/arm-linux-gnueabihf-
PRJDIR?=$(shell dirname ${PWD})
CFLAGS+=-I${PRJDIR}
CC = ${CROSS_COMPILE}gcc
AR = ${CROSS_COMPILE}ar
ifeq ("${LINK_MODE}", "STATIC")
    LIBS = ${STALIB}
else
    LIBS=${DYNLIB}
endif
all: entry ${LIBS} install
entry:
    @echo " ";
    @echo " =========================================================";
    @echo " **     Compile subdir ${LIBNAME} for ${ARCH}             ";
    @echo " =========================================================";
#$(LD) -g --relocatable $(OBJS) -o lib${LIBNAME}.o
${STALIB}:    $(OBJS)
    $(AR) -rcu $@ $(OBJS)
${DYNLIB}:   $(OBJS)
    $(CC) -fPIC -shared -o $@ $(OBJS)
%.o : %.c
    $(CC) -c $< $(CFLAGS)
install:
    @if [ ! -z "${LIBS_PATH}" ] ; then \
        mkdir -p ${LIBS_PATH} ; \
        cp ${LIBS} ${LIBS_PATH}; \
    fi;
clean:
    @rm -f *.o
    @rm -rf *.gdb *.a *.so
distclean: clean
    @rm -f  tags cscope*
.PHONY: clean entry
src/nbiotd/mqtt/mqtt_api.c
src/nbiotd/nbiot/makefile
New file
@@ -0,0 +1,82 @@
#*********************************************************************************
#      Copyright:  (C) 2012 CoherentPlus Sdn. Bhd.
#                  All rights reserved.
#
#       Filename:  Makefile
#    Description:  This is the common subdir Makefile which to compile all the C
#                  source code to object files and then generate the shared or
#                  static library named lib$(FOLDER_NAME).a orlib $(FOLDER_NAME).so,
#                  which depends on the variable $LINK_MODE.
#
#        Version:  1.0.0(10/08/2011~)
#                  Author:  Guo Wenxue <guowenxue@gmail.com>
#      ChangeLog:  1, Release initial version on "10/08/2011 01:29:33 AM"
#
#********************************************************************************/
PWD=$(shell pwd)
#TOP_COMPILE=YES
#If wanna compile in the subdir, not called by top makefile, uncomment it
ifneq (${TOP_COMPILE}, YES)
LOCAL_COMPILE=YES
endif
LIBNAME=$(shell basename ${PWD})
STALIB=lib${LIBNAME}.a
DYNLIB=lib${LIBNAME}.so
VPATH= .
SRCS = $(wildcard ${VPATH}/*.c)
OBJS = $(patsubst %.c,%.o,$(SRCS))
#======================================================
#  ---> Doesn't call by top makefile, compile by local
#======================================================
CROSS_COMPILE?=/opt/rpi/arm-bcm2708/arm-linux-gnueabihf/bin/arm-linux-gnueabihf-
PRJDIR?=$(shell dirname ${PWD})
CFLAGS+=-I${PRJDIR}
CC = ${CROSS_COMPILE}gcc
AR = ${CROSS_COMPILE}ar
ifeq ("${LINK_MODE}", "STATIC")
    LIBS = ${STALIB}
else
    LIBS=${DYNLIB}
endif
all: entry ${LIBS} install
entry:
    @echo " ";
    @echo " =========================================================";
    @echo " **     Compile subdir ${LIBNAME} for ${ARCH}             ";
    @echo " =========================================================";
#$(LD) -g --relocatable $(OBJS) -o lib${LIBNAME}.o
${STALIB}:    $(OBJS)
    $(AR) -rcu $@ $(OBJS)
${DYNLIB}:   $(OBJS)
    $(CC) -fPIC -shared -o $@ $(OBJS)
%.o : %.c
    $(CC) -c $< $(CFLAGS)
install:
    @if [ ! -z "${LIBS_PATH}" ] ; then \
        mkdir -p ${LIBS_PATH} ; \
        cp ${LIBS} ${LIBS_PATH}; \
    fi;
clean:
    @rm -f *.o
    @rm -rf *.gdb *.a *.so
distclean: clean
    @rm -f  tags cscope*
.PHONY: clean entry
src/nbiotd/nbiot/nbiot_api.c