/*********************************************************************************
|
* 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;
|
}
|