blob: 7e16c3cad52d3d98c41674d79ad6eeb117b4869e [file] [log] [blame]
#include "apparms.h"
#include "apparms_log.h"
#include "apparms_hmac_sha256.h"
#include "apparms_argv.h"
#if defined(ARMS_BOARD_EXAMPLE)
#include "apparms_config_example.h"
#include "apparms_data_example.h"
#endif
static APPARMS g_stARMSAPP;
PAPPARMS GetARMSAppPointer(void)
{
return &g_stARMSAPP;
}
PARMSCFGFUNLIST GetARMSAppCfgFunPointer(void)
{
return g_stARMSAPP.pCfgFunList;
}
PARMSDATAFUNLIST GetARMSAppDataFunPointer(void)
{
return g_stARMSAPP.pDataFunList;
}
#ifdef ARMS_SUPPORT_DM
PARMSDM GetARMSAppDMSTPointer(void)
{
return &g_stARMSAPP.stARMSDM;
}
#endif
#ifdef ARMS_SUPPORT_FOTA
PARMSFOTA GetARMSAppFOTASTPointer(void)
{
return &g_stARMSAPP.stARMSFOTA;
}
#endif
#ifndef ARMS_FOTA_CHECK_INTERVAL_VIA_TIMER
static void arms_signal_user_handle_usr2(int signum)
{
if (signum != SIGUSR2)
return;
}
#endif
#ifndef ARMS_DM_REPORT_INTERVAL_VIA_TIMER
static void arms_signal_user_handle_usr1(int signum)
{
if (signum != SIGUSR1)
return;
}
#endif
static void arms_signal_termsignal_handle(int signum)
{
PAPPARMS pARMS = GetARMSAppPointer();
if ((pARMS->stARMSLOOP.emAppStatus == APP_STATUS_INIT) || (pARMS->stARMSLOOP.emAppStatus == APP_STATUS_CHK_NETWORK))
exit(0);
if (pARMS->stARMSLOOP.exitflag == 1)
return;
#ifdef ARMS_SUPPORT_EXTIF
arms_extif_stop_thread(&pARMS->stARMSEXTIF);
#endif
#ifdef ARMS_SUPPORT_DM
#ifdef ARMS_DM_REPORT_INTERVAL_VIA_TIMER
arms_dm_stop_timer(&pARMS->stARMSDM);
#endif
arms_dm_stop_thread(&pARMS->stARMSDM);
#ifdef ARMS_DM_MQTT
arms_sock_dm_mqtt_stop_thread(&pARMS->stARMSDM.stDMSock);
#endif
#ifdef ARMS_DM_LWM2M
arms_sock_dm_lwm2m_stop_thread(&pARMS->stARMSDM.stDMSock);
#endif
#ifdef ARMS_DM_IPC
arms_dmipc_stop_timer(&pARMS->stARMSDM.stDMIPC, &pARMS->stARMSDM.stDMCfg.stIPCCfg);
arms_dmipc_stop_thread(&pARMS->stARMSDM.stDMIPC, &pARMS->stARMSDM.stDMCfg.stIPCCfg);
#endif
#ifdef ARMS_DM_RPC
arms_dmrpc_stop(&pARMS->stARMSDM.stDMRPC, &pARMS->stARMSDM.stDMCfg.stRPCCfg);
#endif
#endif
#ifdef ARMS_SUPPORT_FOTA
#ifdef ARMS_FOTA_CHECK_INTERVAL_VIA_TIMER
arms_fota_stop_timer(&pARMS->stARMSFOTA);
#endif
arms_fota_stop_thread(&pARMS->stARMSFOTA);
#endif
#ifdef ARMS_SUPPORT_EC2
arms_ec2_stop_thread(&pARMS->stARMSEC2);
#endif
sleep(1);
arms_loop_stop(&pARMS->stARMSLOOP);
}
static void arms_signal_handle_reg(void)
{
/* configure to ignore SIGPIPE, this will cause write to fail on a closed socket (instead of receiving SIGPIPE) */
if (signal(SIGPIPE, SIG_IGN) == SIG_ERR)
{
fprintf(stderr, "can't execute signal \n");
//exit(1);
}
//SIGUSR1 and SIGUSR2 is used for DM and FOTA, ignore here.
#ifndef ARMS_DM_REPORT_INTERVAL_VIA_TIMER
signal(SIGUSR1, arms_signal_user_handle_usr1);
#endif
#ifndef ARMS_FOTA_CHECK_INTERVAL_VIA_TIMER
signal(SIGUSR2, arms_signal_user_handle_usr2);
#endif
signal(SIGTERM, arms_signal_termsignal_handle);
signal(SIGINT, arms_signal_termsignal_handle);
signal(SIGPIPE, SIG_IGN);
}
static int arms_init_phase1_imei(void)
{
int ret;
unsigned char *pDMIMEI = NULL;
char szIMEI[32] = {0};
PAPPARMS pApp = GetARMSAppPointer();
#ifdef ARMS_SUPPORT_DM
pDMIMEI = pApp->stARMSDM.stDMCfg.stDMCommCfg.szIMEI;
#endif
do
{
/*start with customzied platform init*/
ret = arms_config_os_get_imei(pApp->pCfgFunList, pDMIMEI, (unsigned char *)szIMEI);
if (ret >= 0)
{
ARMS_LOG_INFO("%s(%d) IMEI=[%s].\n",__FUNCTION__,__LINE__, szIMEI);
pApp->stARMSLOOP.emAppStatus = APP_STATUS_CHK_NETWORK;
break;
}
sleep(2);
}while(pApp->stARMSLOOP.emAppStatus == APP_STATUS_INIT);
#ifdef ARMS_SUPPORT_FOTA
strcpy((char *)pApp->stARMSFOTA.stFOTACfg.stFOTACommCfg.szIMEI, szIMEI);
#endif
#ifdef ARMS_SUPPORT_EC2
strcpy((char *)pApp->stARMSEC2.stEC2Cfg.stEC2CommCfg.szIMEI, szIMEI);
#endif
return 0;
}
static int arms_init_phase1_get_network(void)
{
int ret;
PAPPARMS pApp = GetARMSAppPointer();
do
{
/*start with customzied platform init*/
ret = arms_config_os_get_networkst(pApp->pCfgFunList, pApp->stARMSLOOP.szIPAddr);
if (ret >= 0)
{
ARMS_LOG_INFO("%s(%d) szIPAddr=[%s].\n",__FUNCTION__,__LINE__, pApp->stARMSLOOP.szIPAddr);
pApp->stARMSLOOP.emAppStatus = APP_STATUS_READY;
break;
}
sleep(2);
}while(pApp->stARMSLOOP.emAppStatus == APP_STATUS_CHK_NETWORK);
return 0;
}
static int arms_init_phase1_cfg(void)
{
PAPPARMS pApp = GetARMSAppPointer();
#ifdef ARMS_SUPPORT_DM
pApp->stARMSDM.pCfgFunList = g_stARMSAPP.pCfgFunList;
pApp->stARMSDM.pDataFunList = g_stARMSAPP.pDataFunList;
arms_config_os_dm_init(pApp->pCfgFunList, &pApp->stARMSDM.stDMCfg);
#endif
#ifdef ARMS_SUPPORT_FOTA
pApp->stARMSFOTA.pCfgFunList = g_stARMSAPP.pCfgFunList;
pApp->stARMSFOTA.pDataFunList = g_stARMSAPP.pDataFunList;
arms_config_os_fota_init(pApp->pCfgFunList, &pApp->stARMSFOTA.stFOTACfg);
#endif
#ifdef ARMS_SUPPORT_EC2
pApp->stARMSEC2.pCfgFunList = g_stARMSAPP.pCfgFunList;
pApp->stARMSEC2.pDataFunList = g_stARMSAPP.pDataFunList;
arms_config_os_ec2_init(pApp->pCfgFunList, &pApp->stARMSEC2.stEC2Cfg);
#endif
return 0;
}
static int arms_init_phase1(void)
{
PAPPARMS pApp = GetARMSAppPointer();
memset(pApp, 0, sizeof(APPARMS));
pApp->stARMSLOOP.emAppStatus = APP_STATUS_INIT;
#if defined(ARMS_BOARD_EXAMPLE)
g_stARMSAPP.pCfgFunList = arms_config_example_get_cfgfun_pointer();
g_stARMSAPP.pDataFunList = arms_data_example_get_cfgfun_pointer();
#endif
arms_config_os_platform_init(g_stARMSAPP.pCfgFunList);
arms_init_phase1_imei();
arms_init_phase1_get_network();
arms_init_phase1_cfg();
arms_loop_init((void*)pApp);
#ifdef ARMS_SUPPORT_DM
arms_dm_init(pApp);
#endif
#ifdef ARMS_SUPPORT_FOTA
arms_fota_init(pApp);
#endif
#ifdef ARMS_SUPPORT_EC2
arms_ec2_init(pApp);
#endif
return 0;
}
static int arms_init_phase2(void)
{
PAPPARMS pApp = GetARMSAppPointer();
#ifdef ARMS_SUPPORT_DM
arms_dm_start_thread(&pApp->stARMSDM);
#ifdef ARMS_DM_REPORT_INTERVAL_VIA_TIMER
arms_dm_start_timer(&pApp->stARMSDM);
#endif
#ifdef ARMS_DM_IPC
arms_dmipc_start_timer(&pApp->stARMSDM.stDMIPC, &pApp->stARMSDM.stDMCfg.stIPCCfg);
#endif
#ifdef ARMS_DM_RPC
arms_dmrpc_start(&pApp->stARMSDM.stDMRPC, &pApp->stARMSDM.stDMCfg.stRPCCfg);
#endif
#endif
#ifdef ARMS_SUPPORT_FOTA
arms_fota_start_thread(&pApp->stARMSFOTA);
#ifdef ARMS_FOTA_CHECK_INTERVAL_VIA_TIMER
arms_fota_start_timer(&pApp->stARMSFOTA);
#endif
#endif
#ifdef ARMS_SUPPORT_EC2
arms_ec2_start_thread(&pApp->stARMSEC2);
#endif
#ifdef ARMS_SUPPORT_EXTIF
arms_extif_start_thread(&pApp->stARMSEXTIF);
#endif
arms_signal_handle_reg();
return 0;
}
static int arms_uninit_phase1(void)
{
PAPPARMS pApp = GetARMSAppPointer();
#ifdef ARMS_SUPPORT_DM
arms_dm_uninit(pApp);
#endif
#ifdef ARMS_SUPPORT_FOTA
arms_fota_uninit(pApp);
#endif
#ifdef ARMS_SUPPORT_EC2
arms_ec2_uninit(pApp);
#endif
arms_loop_uninit(pApp);
arms_config_os_platform_uninit(pApp->pCfgFunList);
return 0;
}
static int arms_uninit_phase2(void)
{
return 0;
}
static int arms_main(int argc, char *argv[])
{
PAPPARMS pApp = GetARMSAppPointer();
arms_init_phase1();
arms_init_phase2();
arms_loop_main((void*)pApp);
arms_uninit_phase2();
arms_uninit_phase1();
return 0;
}
int main(int argc, char *argv[])
{
arms_argv_parse_cmdargs(argc, argv);
arms_log_open();
ARMS_LOG_INFO("%s(%d) apparms v%d.%d.%d.596 started.\n",__FUNCTION__,__LINE__,ARMS_VERSION_MAJOR,ARMS_VERSION_MINOR,ARMS_VERSION_PATCH);
arms_main(argc, argv);
arms_log_close();
return 0;
}