/*********************************************************************************
|
* Copyright: (C) 2020 LingYun IoT System Studio
|
* All rights reserved.
|
*
|
* Filename: tlv_server.c
|
* Description: This is a socket server program to receive RPi's temperature
|
* from socket client and save it in sqlite database.
|
*
|
* Version: 1.0.0(2020年04月14日)
|
* Author: Guo Wenxue <guowenxue@gmail.com>
|
* ChangeLog: 1, Release initial version on "2020年04月14日 00时52分56秒"
|
*
|
********************************************************************************/
|
#include <stdio.h>
|
#include <unistd.h>
|
#include <string.h>
|
#include <getopt.h>
|
#include <libgen.h>
|
|
#include "logger.h"
|
#include "proc.h"
|
|
#define PROG_VERSION "1.0.0"
|
|
#define DEF_LOG_FILE "tlv_server.log"
|
|
static void banner(void)
|
{
|
printf("Version %s build on %s %s\n", PROG_VERSION, __DATE__, __TIME__);
|
printf("Copyright (C) 2020 LingYun IoT System Studio.\n\n");
|
|
return ;
|
}
|
|
static void program_usage(const char *progname)
|
{
|
printf("Usage: %s [OPTION]...\n", progname);
|
printf("This program receive RPi's temperature from socket client and save it in sqlite database.\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");
|
printf("\n");
|
|
banner();
|
return ;
|
}
|
|
int main (int argc, char *argv[])
|
{
|
int opt;
|
int i = 0;
|
//int rv = 0;
|
int debug = 0;
|
|
char pid_file[64] = { 0 }; /* The file used to record the PID */
|
const char *progname=NULL;
|
|
int log_level = LOG_LEVEL_NRML;
|
char *log_file = DEF_LOG_FILE;
|
logger_t logger;
|
|
struct option long_options[] = {
|
{"debug", no_argument, NULL, 'd'},
|
{"level", required_argument, NULL, 'l'},
|
{"version", no_argument, NULL, 'v'},
|
{"help", no_argument, NULL, 'h'},
|
{NULL, 0, NULL, 0}
|
};
|
|
memset(&logger, 0, sizeof(logger));
|
|
progname = basename(argv[0]); /* get program name */
|
|
/* parser the command line parameters */
|
while ((opt = getopt_long(argc, argv, "c:dl:vh", long_options, NULL)) != -1)
|
{
|
switch (opt)
|
{
|
|
case 'd': /* set debug running */
|
debug = 1;
|
log_level= LOG_LEVEL_DEBUG;
|
log_file = NULL; /* use standard output */
|
break;
|
|
case 'l': /* set the log level */
|
i = atoi(optarg);
|
log_level = i>LOG_LEVEL_MAX ? LOG_LEVEL_MAX-1 : i;
|
logger.flag |= FLAG_LOGGER_LEVEL_OPT;
|
break;
|
|
case 'v': /* print software version */
|
banner();
|
return EXIT_SUCCESS;
|
|
case 'h': /* print help information */
|
program_usage(progname);
|
return 0;
|
|
default:
|
break;
|
}
|
}
|
|
/* check program already running or not, if already running then exit, or set running as daemon */
|
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;
|
}
|
}
|
|
/* initial and open logger system */
|
if( logger_init(&logger, log_file, log_level, 512)<0 || logger_open()<0 )
|
{
|
printf("ERROR: Initialise logger system failure\n");
|
return -1;
|
}
|
|
/* install signal proc handler */
|
install_proc_signal();
|
|
log_nrml("Program start running\n");
|
|
/* g_signal.stop defined in proc.c, and will be set when catch stop signal */
|
while( !g_signal.stop )
|
{
|
log_dbg("Program still running\n");
|
sleep(3);
|
}
|
|
logger_term();
|
|
return 0;
|
}
|