#include "rest_proxy.h"
#include "selfcare_proxy.h"
#include  "log.h"

int sys_timer10ms;
static int proxy_run_mode = 0;
// 运行模式
#define RUN_MODE_WITH_ALPOOCS 1  /* alpo_ocs<--restful-->restproxy<--udp-->pps */
#define RUN_MODE_WITH_ERSTFUL 2  /* user<------restful-->restproxy<--udp-->ocs */
#define RUN_MODE_WITH_CRM 	 4  /* user<--CRM--restful-->restproxy<--udp-->ocs, recharge card/tariff in CRM */

extern int dba_main(int argc, char **argv);
extern int smcli_client_thread(void *param);
extern int pstn_client_thread(void *param);
extern void _selfcare_udp_process();
extern void _selfcare_res_fsm(void);
int get_proxy_run_mode()
{
	return proxy_run_mode;
}

int fsm_debug()
{
	char buf[MAX_BUFFER];
	
	if(cli_read_cmd(buf) == 0)
		return 0;
	if(strncasecmp(buf, "log off", 7) == 0)
	{
		g_log_env.log_flag = 0x00;
	}
	else  if(strncasecmp(buf, "log all", 7) == 0)
	{
		g_log_env.log_flag = 0xff;
	}
	return 1;
}

int init_comm(const int log_flag, const char *log_file)
{
    if(log_flag)
    {
        strcpy(g_log_env.log_file, log_file);
    }
	else
	{
		strcpy(g_log_env.log_file, "./log/log");
	}

	int ret = rest_proxy_open_log(0);

	return ret;
}

int init_modes(int run_mode, int argc, char **argv)
{
	int ret = SUCCESS;
 	pthread_t tid;
 
       pthread_create(&tid, NULL, (void *)debug_monitor, NULL);

	if(run_mode == RUN_MODE_WITH_ALPOOCS)
	{
    	/* 初始化rest proxy. 	  alpo_ocs<-----restful_http-->rest_proxy<------udp----->pps */
    		ret = rest_proxy_init();
		if(ret != SUCCESS)
		{
			return ret;;
		}
	}
	
	if(run_mode == RUN_MODE_WITH_ERSTFUL || run_mode == RUN_MODE_WITH_CRM )
	{

		pthread_create(&tid, NULL, (void *)smcli_client_thread, NULL);
		
		pthread_create(&tid, NULL, (void *)pstn_client_thread, NULL);
		
		if(run_mode == RUN_MODE_WITH_CRM)
		{
			dba_main(0, NULL);
		}

#if 1//def ENABLE_CRM_REST_AND_Z1
		/* 初始化selfcare proxy. self_care<-----restful_http----->rest_proxy<-----udp---->ocs */
		ret = selfcare_proxy_init(argc, argv);
		if(ret != SUCCESS)
		{
			return ret;;
		}
#endif
	}

	

	return ret;
}

void run_modes_fsm(int run_mode)
{
	if(run_mode == RUN_MODE_WITH_ALPOOCS)
	{
		rest_proxy_fsm_main();
	}

	fsm_debug();
}
int main(int argc, char **argv)
{

	int ret;
	int err_flag = 0;
	char log_file[128]= {0};
	int run_mode = RUN_MODE_WITH_ALPOOCS;
	int log_flag = 0;
	int daemon_flag = 0;

	struct termios prev_termio;


	while ((ret = getopt(argc, argv, "a:d:f:r:e")) != -1)
	{
		switch (ret) 
		{
			case 'd':
				daemon_flag = 1;
				break;
			case 'f':
				if(optarg != NULL)
				{
					log_flag = 1;
					strcpy(log_file, optarg);
				}
				break;
			case 'r':
				if(optarg != NULL)
				{
					run_mode = atoi(optarg) ;//== RUN_MODE_WITH_ERSTFUL ? RUN_MODE_WITH_ERSTFUL : RUN_MODE_WITH_ALPOOCS;
				}
				break;
			case '?':
				err_flag = 1;
				break;
		}
	}
	if (err_flag) 
	{
		printf("Usage: %s -c restproxy [-f /va/log/sgs_log_file.txt] [-d] [-r 1(with alpo ocs))|2(with selfcare)]\n", argv[0]);
		exit(1);
	}

	proxy_run_mode = run_mode;
	
	if(daemon_flag)
	{
		sysDaemonInit();
	}

	SetTermSignal(&prev_termio, Terminate);
	SigactionSystem();
	SetFSMTimer();

	ret = init_comm(log_flag, log_file);
	if(ret != 0)
	{
		goto INITFAIL;
	}

	ret = init_modes(run_mode, argc, argv);
	if(ret != 0)
	{
		goto INITFAIL;
	}

 	
	sys_timer10ms = 1;
	while(1)
	{  
		if(sys_timer10ms)
		{
			run_modes_fsm(run_mode);
		}
		if(0)
		{
			_selfcare_udp_process();

			_selfcare_res_fsm();
		}
		
		usleep(5000);
	}

INITFAIL:
	rest_proxy_uninit();
    	selfcare_proxy_uninit();
    
    return 0;
}
