guowenxue
2020-08-21 02f4d9518378031c63df7a36c49d8b2eabdaab90
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
/*********************************************************************************
 *      Copyright:  (C) Guo Wenxue<guowenxue@gmail.com>
 *                  All rights reserved.
 *
 *       Filename:  zigd.c
 *    Description:  This is the RWME(Radiation Monitor Works Engine) zigd applet entry point
 *                 
 *        Version:  1.0.0(01/13/2013~)
 *         Author:  Guo Wenxue <guowenxue@gmail.com>
 *      ChangeLog:  1, Release initial version on "01/13/2013 02:40:36 PM"
 *                 
 ********************************************************************************/
#include "zigd.h"
 
static void usage_zigd(const char *progname)
{
    banner(progname);
 
    printf("Usage: %s [OPTION]...\n", progname); 
    printf(" %s is a daemon program running on the device to locate the device\n", progname); 
    
    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 init_zigd_context(ZIGD_CONTEX *ctx)
{
    log_info("Initialize zigd context.\n");
 
    ctx->pwr_status = POWER_OFF;
 
    /*  Initialize the zig dataport pointer */
    ctx->comport = comport_init(ZIGD_DATAPORT, 9600, "8N1N");
    if(NULL == ctx->comport)
        return -1;
    
    return 0;
}
 
 
int zigd_main(int argc, char **argv)
{
    int            opt; 
    int            rv = 0; 
    int            debug = 0;
    char           pid_file[64] = { 0 }; /* The file used to record the PID */
    const char     *progname=NULL;
    char           *log_file = ZIGD_DEF_LOG_FILE;
    int            log_level = ZIGD_DEF_LOG_LEVEL;
    cp_logger      *logger = NULL;
    ZIGD_CONTEX    zigd_ctx;
 
    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} 
    };
 
    progname = basename(argv[0]);
 
    /* Parser the command line parameters */
    while ((opt = getopt_long(argc, argv, "dl:vh", long_options, NULL)) != -1) 
    { 
        switch (opt) 
        { 
            case 'd': /* Set debug running */ 
                debug = 1; 
                log_file = DBG_LOG_FILE;
                break; 
 
            case 'l': /* Set the log level */
                log_level = atoi(optarg); 
                break; 
 
            case 'v':  /* Get software version */ 
                banner(progname); /* Defined in version.h */ 
                return EXIT_SUCCESS; 
            
            case 'h':  /* Get help information */ 
                usage_zigd(progname); 
                return 0; 
 
            default: 
                break; 
        } /*  end of "switch(opt)" */ 
    }
 
    if( !debug )
    {
        snprintf(pid_file, sizeof(pid_file), "%s/%s.pid", PID_FILE_PATH, progname);
        if( check_daemon_running(pid_file) ) 
        { 
            printf("Programe already running, exit now.\n"); 
            return -1; 
        } 
    }
 
    if ( !(logger=cp_log_init(NULL, log_file, log_level, ZIGD_LOG_FILE_SIZE)) )
    {
        printf("Logger initialize failure with file: %s\n", logger->file);
        return -1;
    }
 
    if ( cp_log_open() )
    {
        printf("Logger initialize failure with file: %s\n", logger->file);
        return -1;
    }
 
    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();
 
    if( init_zigd_context(&zigd_ctx) < 0 )
    {
        log_fatal("Initialise zigd work context failure, rv=%d\n", rv); 
        goto CleanUp; 
    }
 
#if 0
    /*  Start the zig thread to locate the device */ 
    if( (rv=start_thread_zigd(&(zigd_ctx))) < 0)
    { 
        log_fatal("Start PPP thread failure, rv=%d\n", rv); 
        goto CleanUp; 
    } 
    
    ctrl_thread_start( &zigd_ctx);
#endif
 
CleanUp: 
    log_nrml("Terminate program \"%s\".\n", progname);
    unlink(pid_file); 
    cp_log_term(); 
    return rv;
}