/*****************************************************************************
 * file    lib/process/process.c  
 * author  YuLiang
 * version 1.0.0
 * date    26-Sep-2021
 * brief   This file provides all the process related operation functions.
 ******************************************************************************
 * Attention
 *
 * <h2><center>&copy; COPYRIGHT(c) 2021 LandPower</center></h2>
 *
 * Redistribution and use in source and binary forms, with or without modification,
 * are permitted provided that the following conditions are met:
 *   1. Redistributions of source code must retain the above copyright notice,
 *      this list of conditions and the following disclaimer.
 *   2. Redistributions in binary form must reproduce the above copyright notice,
 *      this list of conditions and the following disclaimer in the documentation
 *      and/or other materials provided with the distribution.
 *   3. Neither the name of LandPower nor the names of its contributors may be used to 
 *      endorse or promote products derived from this software without specific
 *      prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 ******************************************************************************/

/* Includes ------------------------------------------------------------------*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

/* 标准C库头文件. */
#include <locale.h>
#include <stdlib.h>
#include <signal.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <linux/watchdog.h>

#include "cmd.h"
#include "mtimer.h"
#include "process.h"
#include "hwgpio.h"
#include "fifo.h"
#include "ca_csg.h"
#include "ca_dbg.h"
#include "ca_cau.h"
#include "ca_param.h"
#include "ca_network.h"
#include "ca_mqtt.h"
#include "debug.h"

/* Private typedef -----------------------------------------------------------*/

/* Private define ------------------------------------------------------------*/

/* Private macro -------------------------------------------------------------*/

/* Private variables ---------------------------------------------------------*/
int32_t recv_qid;
uint16_t version_hex;
uint32_t start_time;
sys_ctrl_t sysctrl = {0};

/* Private function prototypes -----------------------------------------------*/

/* Internal functions --------------------------------------------------------*/
/* 信号处理函数 */
void _signal_handler(int sig)
{
    if (SIGSEGV == sig)
    {
        log_backtrace(LOG_LVL_ERR);
    }
    //else if(SIGINT == sig
    //    || SIGTSTP == sig)
    //{
        /* 屏蔽信号 */
    //    return;
    //}

    exit(-1);
}

/* Interface functions -------------------------------------------------------*/
/* description: 数据处理初始化.
   param:
   return:  (E_NONE)成功,(其他)失败 */
int32_t process_init(void)
{
    LOG("\n");

    LD_E_RETURN(DBG_M_CA_ERR, cau_handle_init());
    LD_E_RETURN(DBG_M_CA_ERR, network_handle_init());
    LD_E_RETURN(DBG_M_CA_ERR, csg_handle_init());
    LD_E_RETURN(DBG_M_CA_ERR, debug_handle_init());
    LD_E_RETURN(DBG_M_CA_ERR, mqtt_handle_init());
    return E_NONE;
}

/* description: 程序入口函数.
   param:
   return: */
int32_t main(int32_t argc, char **argv)
{
    struct sigaction act;
    uint32_t cnt = 0;
    int wdt_Cnt = 0;
    
    /* 设置本地化信息为默认值. */
    setlocale(LC_ALL, "");

    /* 必须最前面, 初始化内存管理模块的基本参数. */
    mtype_init_befor();

    /* log初始化 */
    log_open();
    /* 初始化时定量清除 log */
    log_clean();
    log_out(LOG_DEFAULT, LOG_LVL_WARN, "System start!");
    
    /* 设置信号处理的回调函数 */
    act.sa_handler = _signal_handler;
    sigemptyset(&act.sa_mask);
    sigaction(SIGINT, &act, NULL);
    sigaction(SIGSEGV, &act, NULL);
    sigaction(SIGTSTP, &act, NULL);

    if (param_load() < 0)
    {
        LOG("param_load failure...");
        return 0;
    }
    
    /* 初始化cli等公共模块. */
    cmd_init();
    thread_m_init();
    mtype_init();
    dbg_init();
    mtimer_init();
    vtysh_init();
    vtycmd_init();
    gpio_init();
    
    //GPIO_USB_4G_PWR(OFF);
    fifo_init();
    log_handle_init();

    /* 预初始化 */
    //process_init_befor();

    /* 主处理函数初始化 */
    process_init();
    
    /* 配置恢复命令行启动 */
    vtysh_config_recovery();

    /* 后初始化 */
    //process_init_after

    /* 启动命令行 */
    vtysh_shell_init();
    vtycmd_cmd_init();

    /* 初始化完成 */
    is_system_init = TRUE;
    sysctrl.dev_hex_id = dev_id_2_hex(host.name);
    //LOG("dev_hex_id=0x%x", sysctrl.dev_hex_id);
    
    /* 主循环, 点灯喂狗. */
    sysctrl.start_time = time(NULL);
    for(;;)
    {
        /* 点 RUN 灯. */
        GPIO_RUN_LED(cnt & 0x1);
        
        start_time++;
        cnt++;

        sleep(1);
        if(wdt_Cnt++ > 10)
        {
            wdt_Cnt = 0;
            GPIO_WDT_CTRL(1);
            usleep(100*1000);
            GPIO_WDT_CTRL(0);
        }
    }
    
    return 0;
}
/************************ (C) COPYRIGHT LandPower ***** END OF FILE ****/
