guowenxue
2019-06-25 0c6d37d6eb4eae9a91c7433adc7e76ba54c67c0f
mqttd/main.c
@@ -14,79 +14,135 @@
#include <time.h>
#include <unistd.h>
#include "led.h"
#include "ds18b20.h"
#include "sht20.h"
#include "logger.h"
#include "proc.h"
#include "hal.h"
int hal_init(void);
#define DAEMON_PIDFILE             "/tmp/.mqtt.pid"
/********************************************************************************
 *  Description:
 *   Input Args:
 *  Output Args:
 * Return Value:
 ********************************************************************************/
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 )
    {
        printf("Initialise hardware failure\n");
        log_err("Initialise hardware failure\n");
        return -1;
    }
    log_nrml("HAL initialise ok\n");
#endif
    
    while(1)
    while( ! g_signal.stop )
    {
        turn_led(LED_R, ON);
        sleep(1);
        turn_led(LED_R, OFF);
        sleep(1);
        turn_led(LED_G, ON);
        sleep(1);
        turn_led(LED_G, OFF);
        sleep(1);
        turn_led(LED_B, ON);
        sleep(1);
        turn_led(LED_B, OFF);
        sleep(1);
        if(sht2x_get_temp_humidity(&temp, &rh) < 0)
        {
            printf("SHT2X get temperature and relative humidity failure\n");
        }
        else
        {
            printf("SHT2X Temperature=%lf ℃ relative humidity=%lf%\n", temp, rh);
        }
        if( ds18b20_get_temperature(&temp) < 0)
        {
            printf("DS18B20 get temperature failure\n");
        }
        else
        {
            printf("DS18B20 get temperature=%lf ℃ \n", temp);
        }
        msleep(1000);
        log_nrml("Main control thread continue running\n");
    }
OUT:
    logger_term();
    return 0;
} /* ----- End of main() ----- */
int hal_init(void)
int check_set_program_running(int daemon)
{
    init_led();
    if( sht2x_init() < 0 )
    if( check_daemon_running(DAEMON_PIDFILE) )
    {
        printf("Initialise SHT20 failure\n");
        return -2;
        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;
}