guowenxue
2019-06-25 0c6d37d6eb4eae9a91c7433adc7e76ba54c67c0f
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
/*********************************************************************************
 *      Copyright:  (C) 2019 LingYun IoT System Studio
 *                  All rights reserved.
 *
 *       Filename:  main.c
 *    Description:  This file 
 *                 
 *        Version:  1.0.0(29/01/19)
 *         Author:  Guo Wenxue <guowenxue@gmail.com>
 *      ChangeLog:  1, Release initial version on "29/01/19 15:34:41"
 *                 
 ********************************************************************************/
#include <stdio.h>
#include <time.h>
#include <unistd.h>
 
#include "logger.h"
#include "proc.h"
#include "hal.h"
 
#define DAEMON_PIDFILE             "/tmp/.mqtt.pid"
 
 
typedef struct mqtt_ctx_s 
{
    int        conf;
} mqtt_ctx_t;  
 
 
int check_set_program_running(int daemon);
void *mqtt_sub_worker(void *args);
void *mqtt_pub_worker(void *args);
 
 
int main (int argc, char **argv)
{
    float              temp;
    float              rh;
    int                daemon = 0;
    pthread_t          tid;
    mqtt_ctx_t         ctx;
 
    //if( logger_init("mqttd.log", LOG_LEVEL_DEBUG, 1024) < 0 ) 
    if( logger_init(DBG_LOG_FILE, LOG_LEVEL_DEBUG, 1024) < 0 ) 
    {
        fprintf(stderr, "Logger system initialise failure\n");
        return -2;
    }
    log_dbg("Logger system initialise ok\n");
 
    install_proc_signal();
 
    if( check_set_program_running(daemon) < 0 ) 
    {
        goto OUT;
    }
 
    if( thread_start(&tid, mqtt_sub_worker, &ctx ) < 0 )
    {
        log_fatal("Start MQTT subscibe worker thread failure\n");
        goto OUT;
    }
 
    if( thread_start(&tid, mqtt_pub_worker, &ctx) < 0 )
    {
        log_fatal("Start MQTT publish worker thread failure\n");
        goto OUT;
    }
 
 
#if 0
    if( hal_init() < 0 )
    {
        log_err("Initialise hardware failure\n");
        return -1;
    }
    log_nrml("HAL initialise ok\n");
#endif
    
 
    while( ! g_signal.stop )
    {
        msleep(1000);
        log_nrml("Main control thread continue running\n");
    }
 
 
OUT:
    logger_term();
    
    return 0;
} /* ----- End of main() ----- */
 
 
int check_set_program_running(int daemon)
{
    if( check_daemon_running(DAEMON_PIDFILE) ) 
    {
        log_err("Program already running, process exit now");
        return -1;
    }
 
    if( daemon ) 
    {
        if( set_daemon_running(DAEMON_PIDFILE) < 0 )
        {
            log_err("set program running as daemon failure\n");
            return -2;
        }
    }
    else
    {
        if( record_daemon_pid(DAEMON_PIDFILE) < 0 )
        {
            log_err("record program running PID failure\n");
            return -3;
        }
    }
 
    return 0;
}
 
void *mqtt_pub_worker(void *args)
{
    mqtt_ctx_t           *ctx = (mqtt_ctx_t *)args;
 
    while( !g_signal.stop )
    {
        msleep(1000);
        log_nrml("MQTT publish thread running\n");
    }
 
    return NULL;
}
 
void *mqtt_sub_worker(void *args)
{
    mqtt_ctx_t           *ctx = (mqtt_ctx_t *)args;
 
    while( !g_signal.stop )
    {
        msleep(1000);
        log_nrml("MQTT subscibe thread running\n");
    }
 
    return NULL;
}