commit | author | age
|
13d8a8
|
1 |
/********************************************************************************* |
G |
2 |
* Copyright: (C) 2019 LingYun IoT System Studio |
|
3 |
* All rights reserved. |
|
4 |
* |
|
5 |
* Filename: main.c |
|
6 |
* Description: This file |
|
7 |
* |
|
8 |
* Version: 1.0.0(29/01/19) |
|
9 |
* Author: Guo Wenxue <guowenxue@gmail.com> |
|
10 |
* ChangeLog: 1, Release initial version on "29/01/19 15:34:41" |
|
11 |
* |
|
12 |
********************************************************************************/ |
|
13 |
#include <stdio.h> |
|
14 |
#include <stdlib.h> |
|
15 |
#include <unistd.h> |
|
16 |
#include <time.h> |
|
17 |
#include <getopt.h> |
|
18 |
#include <libgen.h> |
|
19 |
#include <string.h> |
|
20 |
#include <errno.h> |
|
21 |
|
|
22 |
#include "logger.h" |
|
23 |
#include "ds18b20.h" |
|
24 |
#include "proc.h" |
|
25 |
|
|
26 |
#define PROG_VERSION "v1.0.0" |
|
27 |
#define DAEMON_PIDFILE "/tmp/.socketd.pid" |
|
28 |
|
|
29 |
static void program_usage(char *progname) |
|
30 |
{ |
|
31 |
|
|
32 |
printf("Usage: %s [OPTION]...\n", progname); |
|
33 |
printf(" %s is LingYun studio temperature socket client program running on RaspberryPi\n", progname); |
|
34 |
|
|
35 |
printf("\nMandatory arguments to long options are mandatory for short options too:\n"); |
|
36 |
printf(" -d[debug ] Running in debug mode\n"); |
|
37 |
printf(" -h[help ] Display this help information\n"); |
|
38 |
printf(" -v[version ] Display the program version\n"); |
|
39 |
|
|
40 |
printf("\n%s version %s\n", progname, PROG_VERSION); |
|
41 |
return; |
|
42 |
} |
|
43 |
|
|
44 |
int main (int argc, char **argv) |
|
45 |
{ |
|
46 |
int daemon = 1; |
|
47 |
int opt; |
|
48 |
char *progname=NULL; |
|
49 |
char *logfile="client.log"; |
|
50 |
int loglevel=LOG_LEVEL_INFO; |
|
51 |
int logsize=10; /* logfile size max to 10K */ |
|
52 |
|
|
53 |
struct option long_options[] = { |
|
54 |
{"debug", no_argument, NULL, 'd'}, |
|
55 |
{"version", no_argument, NULL, 'v'}, |
|
56 |
{"help", no_argument, NULL, 'h'}, |
|
57 |
{NULL, 0, NULL, 0} |
|
58 |
}; |
|
59 |
|
|
60 |
progname = (char *)basename(argv[0]); |
|
61 |
|
|
62 |
/* Parser the command line parameters */ |
|
63 |
while ((opt = getopt_long(argc, argv, "dvh", long_options, NULL)) != -1) |
|
64 |
{ |
|
65 |
switch (opt) |
|
66 |
{ |
|
67 |
case 'd': /* Set debug running */ |
|
68 |
daemon = 0; |
|
69 |
logfile="console"; |
|
70 |
loglevel=LOG_LEVEL_DEBUG; |
|
71 |
break; |
|
72 |
|
|
73 |
case 'v': /* Get software version */ |
|
74 |
printf("%s version %s\n", progname, PROG_VERSION); |
|
75 |
return 0; |
|
76 |
|
|
77 |
case 'h': /* Get help information */ |
|
78 |
program_usage(progname); |
|
79 |
return 0; |
|
80 |
|
|
81 |
default: |
|
82 |
break; |
|
83 |
} |
|
84 |
|
|
85 |
} |
|
86 |
|
|
87 |
if( log_open(logfile, loglevel, logsize, THREAD_LOCK_NONE) < 0 ) |
|
88 |
{ |
|
89 |
fprintf(stderr, "Initial log system failed\n"); |
|
90 |
return 1; |
|
91 |
} |
|
92 |
|
|
93 |
install_default_signal(); |
|
94 |
|
|
95 |
if( check_set_program_running(daemon, DAEMON_PIDFILE) < 0 ) |
|
96 |
goto cleanup; |
|
97 |
|
|
98 |
while( ! g_signal.stop ) |
|
99 |
{ |
|
100 |
msleep(1000); |
|
101 |
} |
|
102 |
|
|
103 |
cleanup: |
|
104 |
log_close(); |
|
105 |
|
|
106 |
return 0; |
|
107 |
} |