[LinuxC] Dynamic(7Daul) - 1.SystemInit() - 전체 코드

박남호·2022년 11월 7일
0
int main(void)
{
    while(1)
    {
        system_init();                                                                                                                                                                 
        system_run();
        system_exit();
    }
    return 0;
}

일단 main은 이렇다. system.c 파일의 system_init() 함수가 어떤 기능을 하는지 하나씩 분석해 보도록 하겠다.

int system_init()
{
	int ret;
	time_t rawtime;
	struct tm* timeinfo;
	char log_path[256];

	signal(SIGPIPE, SIG_IGN);
	// kernel panic debugging
	(void) signal (SIGSEGV, calltrace);

	LOG("Sensonia Dynamic Detector System Start! \n");
	time(&rawtime);
	timeinfo = localtime(&rawtime);

	memset(log_path, 0, sizeof(log_path));
	sprintf(log_path, "/root/log/log_%04d%02d%02d%02d%02d%02d.txt", timeinfo->tm_year+1900, timeinfo->tm_mon+1, timeinfo->tm_mday, timeinfo->tm_hour, timeinfo->tm_min, timeinfo->tm_sec);

	detector_system = (struct system_struct *)malloc(sizeof(struct system_struct));

	detector_system->state = DETECTOR_ERROR;
	detector_system->send_sub_enable = 0;
	seq_abort = 0;
	seq_exit = 0;
	seq_heartbeat = 0;
	seq_pend_main = 0;
	seq_pend_sub = 0;

	SvLd_AppINI(&g_AppPar, LOAD);

	if(open_fpga_device() < 0)
	{
		memset(log_buff, 0x00, sizeof(log_buff));
		snprintf(log_buff, 1024, "Error! open_fpga_device");
		write_logfile(log_buff);
		return -1;
	}
	LOG("Success open_fpga_device\n");
	
	if(setup_spi() < 0)
	{
		memset(log_buff, 0x00, sizeof(log_buff));
		snprintf(log_buff, 1024, "Error! setup_spi");
		write_logfile(log_buff);
		return -1;
	}
	LOG("Success setup_spi\n");

	int ms_Cnt = 0;
	while (check_fpga_config_done() < 0)
	{
		usleep(100000); // 100 ms
		ms_Cnt += 100;
		if (ms_Cnt > 10000)
		{
			ERROR("Timeout : Waiting for FPGA configuration done\n");
			return -1;
		}
	}
	LOG("FPGA configuration done");

	if (access(fpga_fname, F_OK) == 0)
	{
		// File Exist
		SvLd_FPGAParam(LOAD);
		LOG("load file");
	}
	else
	{
		// File dose not exist
		GetFPGAParam();
		SvLd_FPGAParam(SAVE);
		LOG("make file");
	}
	SetFPGAParam();

	if (EnableTempSensor() < 0)
		ERROR("can't enable temperature sensor device");

	LOG("complete! init_seq");

	memset(log_buff, 0x00, sizeof(log_buff));
	snprintf(log_buff, 1024, "Connection");
	write_logfile(log_buff);
	sync();

	// pthread_mutex_init
	ret = 0;
	ret += pthread_mutex_init(&mutx_quit, NULL);
	ret += pthread_mutex_init(&mutx_pend, NULL);
	ret += pthread_mutex_init(&mutx_readout, NULL);
	ret += pthread_mutex_init(&mutx_fill_cash_queue, NULL);
	ret += pthread_mutex_init(&mutx_empty_cash_queue, NULL);
	for (int i=0; i<COPY_THREAD_COUNT; i++)
		ret += pthread_mutex_init(&mutx_copy_thread[i], NULL);

	// pthread_cond_init
	for (int i=0; i<COPY_THREAD_COUNT; i++)
		ret += pthread_cond_init(&copy_thread_cond[i], NULL);

	if (ret)
	{
		memset(log_buff, 0x00, sizeof(log_buff));
		snprintf(log_buff, 1024, "system_init() error\n");
		write_logfile(log_buff);
		return -1;
	}

	// pthread_create
	pthread_create(&detector_system->ctrl_task_thread, NULL, CtrlTask, NULL);
	pthread_create(&detector_system->send_task_main_thread, NULL, SendTaskMain, NULL);
	pthread_create(&detector_system->send_task_sub_thread, NULL, SendTaskSub, NULL);
	pthread_create(&detector_system->consoleproc_thread, NULL, ConsoleProc, NULL);
	pthread_create(&detector_system->readout_proc_thread, NULL, ReadoutProc, NULL);
	pthread_create(&detector_system->heartbeat_thread, NULL, HeartbeatTask, NULL);

	struct thread_struct var[COPY_THREAD_COUNT];
	for (int i=0; i<COPY_THREAD_COUNT; i++)
	{
		var[i].thread_num = i;
		pthread_create(&detector_system->copy_thread[i], NULL, CopyTask, (void *)(&var[i]));
	}

	return 0;
}
profile
NamoPark

0개의 댓글