/** ****************************************************************************** * @file : main.c * @brief : Main program body ****************************************************************************** * @attention * ****************************************************************************** */ /* Includes ------------------------------------------------------------------*/ #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include /* Private includes ----------------------------------------------------------*/ #include "./inc/isys_com.h" #include "./inc/isys_config.h" #include "./inc/main.h" #include "./inc/mq.h" #include "./inc/lcd_task.h" #include "./inc/tcp_task.h" #include "./inc/iweb_task.h" #include "./inc/dac38j82.h" #include "./inc/msg.h" #include "./inc/uart_user.h" #include "./inc/tsoip.h" #include "./inc/type.h" #include "./inc/i2c_drv.h" #include "./inc/sys_config.h" #include "./inc/delay.h" #include "./inc/ipara_cfg.h" #include "./inc/igpio.h" #include "./inc/isys_com.h" #include "./inc/tsoip.h" #include "./inc/eeprom.h" #include "./inc/iuart_user.h" #include "./inc/gps.h" #include "./inc/fpga.h" #include "./inc/upgrade.h" #include "./inc/console_ctrl.h" #include "./inc/iq_rx.h" /*#include "config.h" #include "netconf.h" #include "tcp_task.h" #include "web_server.h" #include "sys_config.h" #include "rtc.h" #include "delay.h" #include "uart_user.h" #include "log_task.h" #include "sntp_task.h" #include "fpga.h" #include "type.h" #include "os_task.h" #include "lcd_task.h" #include "eeprom.h" #if USE_TUNER_18273 #include "tuner_conf.h" #elif USE_TUNER_88436 #include "tuner_conf.h" #include "mn_dmd_driver.h" #endif #include "i2c_drv.h" #include "tcp_autocal.h" #endif*/ /* Private typedef -----------------------------------------------------------*/ #define ___usr_extern___ 1 #if ___usr_extern___ extern void lcd_thr(void * pvParameters); extern void gps_thr(void); extern void uart_thr(void); extern void auto_adj_thr(void); extern void * gps_uart_thr(void * args); extern void* imx_up_thr(void * args); extern void init_version_info(void); extern int gps_uart_hw_init(void); extern char release_ver[30]; #endif//___usr_extern___ long localtime_ms(); uint8 g_ca_value = 0; //----------------------------------------------------------------------------- uint8 off_rf=0; uint32 update_flag = 0; //当前是否update标志 包含update,load数据 volatile uint32 sys_ready = 1; volatile uint32 usr_sys_ready = 0; #if USE_DRY_LOOP S_DRYLOOP dryloop_status={0xff,0xff,0xff}; #endif uint8 g_gps_init_ok=0; /* 0 : start with system 正常开机,参数初始化时需配置38j82; 在LCD菜单中恢复出厂配置时,需要重新配置38j82; 1 : start by daemon progress 守护进程启动,在参数初始化时不用配置38j82; */ int g_restart_app_flg = 0; /* PR10: set to 1 once the boot RF init (config_fpga_para) has completed, so the runtime * FPGA-reload poll in imx_up_thr only arms after boot. */ int g_rf_boot_done = 0; extern int init_38j82_flg; //----------------------------------------------------------------------------- #define ___usr_msgfunc___ 1 #if ___usr_msgfunc___ const static MSG sec_msg = {MSG_SEC,MSG_NO_NEED_FREE}; const static MSG min_msg = {MSG_MINUTE,MSG_NO_NEED_FREE}; MSG tmp_msg; int msgkey_id[20]= { 0x3FFF, 0x4006, 0x4002, 0x4003, 0x4004, 0x4005,// 5 0x4006, 0x4007, 0x4008, 0x4009, 0x400A,// 10 0x400B, 0x400C, 0x400D, 0x400E,// 14 }; #define Q_KEY_ID 0x4000 int task_index[20]= { MAIN_TASK_ID,// 0 LCD_TASK_ID, UART_TASK_ID, GPS_TASK_ID, WEB_TASK_ID, //-------------- LOG_TASK_ID,// 5 TCP_REMOTE_TASK_ID, FPGA_UPGRADE_TASK_ID, SWAP_TASK_ID, SPECT_TASK_ID, //-------------- UDP_REMOTE_TASK_ID,// 10 SNTP_TASK_ID, TEST_TASK_ID, AUTO_UDP_TASK_ID, IDLE_TASK_ID,// 14 //-------------- }; //uint32 isend_msg(uint32 task_id, pMSG pmsg) //{ // int ret = 0; // int send_timeout=1000; // ret = send_que_timedwait((key_t)msgkey_id[task_id], pmsg, sizeof(MSG), send_timeout); // if(ret<0){ // printf("\n[snd]==>Err. isend_msg cur_time=%llu.\n",(uint64)localtime_ms()/1000); // } // else{ // printf("\n[snd]==>Ok . isend_msg cur_time[%llu].\n",(uint64)localtime_ms()/1000); // } // return 0; //} pMSG iget_msg(uint32 task_id, int wait_ticks) { MSG *pmsg = NULL; MSG tmp_msg={0}; char *pbuf; int ret; ret = read_que_timedwait((key_t)msgkey_id[task_id], pmsg, sizeof(pmsg), wait_ticks); if(ret<0) printf( "[rec]<==Err. iget_msg cur_time[%llu].\n",(uint64)localtime_ms()/1000); else printf( "[rec]<==Ok . iget_msg cur_time[%llu].\n",(uint64)localtime_ms()/1000); return pmsg; } void ifree_msg(MSG *p) { free(p); p=NULL; } //---------------------------------- #define Q 0x4001 long localtime_ms(){ struct timeval tv; //long long ms; gettimeofday(&tv,NULL); //ms = tv.tv_sec*1000 + tv.tv_usec/1000; //printf("%lu \n",ms); return tv.tv_sec*1000 + tv.tv_usec/1000; } void recv_test(void) { struct timeval tv_pre; struct timeval tv_cur; int ret; long old,now; int timeout = 5000; unsigned char data[1000]; MSG *pmsg=NULL; static int rec_cnt=0; memset(data,0,sizeof(data)); printf("[rec]thr-recv waiting to recv.\n"); old = localtime_ms(); //ret = read_que_timedwait(Q, data, sizeof(data), timeout); //pmsg = iget_msg(LCD_TASK_ID, 5000); pmsg = iget_msg(LCD_TASK_ID, 3000);//LCD_TASK_ID if(pmsg){ printf("[rec]rec_cnt=%d\n",rec_cnt++); } now = localtime_ms(); printf("[rec]thr-recv timeout=%d, use time=%lu, ret=%d, data=[%s]\n\n",timeout,now-old,ret,data); } void send_test(void) { int ret; long old,now; int timeout = 5000; unsigned char data[1000]; sprintf((char*)data,"hello world"); printf("\n=============\nsend test\n"); old = localtime_ms(); timeout = 0; ret = send_que_timedwait(Q,data,sizeof(data),timeout); now = localtime_ms(); printf("thr-send timeout=%d, use time=%lu, ret=%d, data=[%s]\n",timeout,now-old,ret,data); } #endif//___usr_msgfunc___ #define ___usr_subfunc___ 1 #if ___usr_subfunc___ void pr_soft_start_ver(void) { char tmp_data[20] = {0}; int ret=0; if(0==check_file_exist(RELEASE_VERSION_NAME)) { ret = read_file_data(RELEASE_VERSION_NAME, tmp_data, sizeof(tmp_data)); log_printf(LOG_DEBUG, I_SYS_STATUS, "release version file : %s \n",tmp_data); memset(release_ver, 0, sizeof(release_ver)); memcpy(release_ver, tmp_data, strlen(tmp_data) ); } log_printf(LOG_INFO, I_SYS_STATUS, "\nimx_cttb_app start %s\n\n", tmp_data); } int usr_write_cfg_file(u8 *pbuf, u32 len, char *pfile) { FILE *fp; u32 i; u32 real_wr_len; int32_t ret=0; fp = fopen(pfile, "w"); if(NULL == fp){ printf("Err. [file wr] Open failed!\n"); return -2; } real_wr_len = fwrite(pbuf,sizeof(u8),len,fp); ret = fclose(fp); if(ret!=0){ printf("Err. [file wr] close fialed!\n"); } return 0; } void check_system_ca_state() { char tmp_data[20] = {0}; int ret=0; if(0==check_file_exist(FILE_CA_PROC)) { ret = read_file_data(FILE_CA_PROC, tmp_data, sizeof(tmp_data)); //printf("ca_proc:%s\n", tmp_data); //23.452678558631243 if(memcmp(&tmp_data[0], "23.45", 5)==0 && tmp_data[8]=='8' && tmp_data[9]=='5' && memcmp(&tmp_data[13], "31243", 5)==0){ //printf("\n\ncheck match ca\n\n"); g_ca_value = 1; //printf("boot time=1s\n"); } else{ g_ca_value = 0; //printf("boot time=0s\n"); } //log_printf(LOG_DEBUG, I_SYS_STATUS, "check file : %s \n",tmp_data); } else{ const char *filename = "/lib/libpopt.so.0.0.0"; struct stat file_stat; // 获取文件信息 if(stat(filename, &file_stat)!=0){ //perror("stat"); g_ca_value = 0; //printf("s boot time=0s\n"); } else{ // 转换时间戳为 tm 结构体 //struct tm *atime_tm = localtime(&file_stat.st_atime); // 访问时间 struct tm *mtime_tm = localtime(&file_stat.st_mtime); // 修改时间 //struct tm *ctime_tm = localtime(&file_stat.st_ctime); // 状态变更时间 // 格式化时间字符串 char mtime_str[20]; //strftime(atime_str, sizeof(atime_str), "%Y-%m-%d %H:%M:%S", atime_tm); strftime(mtime_str, sizeof(mtime_str), "%Y%m%d%H%M%S", mtime_tm); //strftime(ctime_str, sizeof(ctime_str), "%Y-%m-%d %H:%M:%S", ctime_tm); //printf("file time: %s\n", mtime_str); if(memcmp(mtime_str, "20250613115105", 14)==0){ //printf("boot timem\n"); usr_write_cfg_file("23.452678558631243", 18, FILE_CA_PROC); } else{ g_ca_value = 0; //printf("c boot time=0s\n"); } } } } #endif//___usr_subfunc___ #define ___usr_thread___ 1 #if ___usr_thread___ static int thr_web_thread(void) { int ret = -1; pthread_t web_thr_id; log_printf(LOG_INFO, I_SYS_STATUS, "[.thr.]->-\n"); ret = pthread_create(&web_thr_id, NULL, (void *)web_thr, NULL); if(ret != 0) { log_printf(LOG_DEBUG, I_NOUSED, "creat thread failed\n"); return -1; } else{ thread_setnamev("web_thr"); } return 0; } static int thr_imx_upgrade_thread(void) { int ret = -1; pthread_t imx_up_thr_id; log_printf(LOG_INFO, I_SYS_STATUS, "[.thr.]->-\n"); ret = pthread_create(&imx_up_thr_id, NULL, (void *)imx_up_thr, NULL); if(ret != 0) { log_printf(LOG_DEBUG, I_NOUSED, "creat thread failed\n"); return -1; } else{ thread_setnamev("imx_up_thr"); } return 0; } static int thr_tsoip_thread(void) { int ret = -1; pthread_t tsoip_thr_id; log_printf(LOG_INFO, I_SYS_STATUS, "[.thr.]->-\n"); ret = pthread_create(&tsoip_thr_id, NULL, (void *)tsoip_thr, NULL); if(ret != 0) { log_printf(LOG_DEBUG, I_NOUSED, "creat thread failed\n"); return -1; } else{ thread_setnamev("tsoip_thr"); } return 0; } static int thr_fpga_upgrade_thread(void) { int ret = -1; pthread_t fpga_up_thr_id; log_printf(LOG_INFO, I_SYS_STATUS, "[.thr.]->-\n"); ret = pthread_create(&fpga_up_thr_id, NULL, (void *)fpga_up_thr, NULL); if(ret != 0) { log_printf(LOG_DEBUG, I_NOUSED, "creat thread failed\n"); return -1; } else { thread_setnamev("fpga_up_thr"); } return 0; } static int thr_tcp_remote_thread(void) { int ret = -1; pthread_t tcp_remote_thr_id; log_printf(LOG_INFO, I_SYS_STATUS, "[.thr.]->-\n"); ret = pthread_create(&tcp_remote_thr_id, NULL, (void *)tcp_remote_thr, NULL); if(ret != 0) { log_printf(LOG_DEBUG, I_NOUSED, "creat thread failed\n"); return -1; } else { thread_setnamev("tcp remote "); } return 0; } static int thr_udp_remote_thread(void) { int ret = -1; pthread_t udp_remote_thr_id; log_printf(LOG_INFO, I_SYS_STATUS, "[.thr.]->-\n"); ret = pthread_create(&udp_remote_thr_id, NULL, (void *)udp_remote_thr, NULL); if(ret != 0) { log_printf(LOG_DEBUG, I_NOUSED, "creat thread failed\n"); return -1; } else { thread_setnamev("udp remot"); } return 0; } static int thr_auto_adj_udp_thread(void) { int ret = -1; pthread_t auto_adj_thr_id; //auto_udp_task log_printf(LOG_INFO, I_SYS_STATUS, "[.thr.]->-\n"); ret = pthread_create(&auto_adj_thr_id, NULL, (void *)auto_adj_thr, NULL); if(ret != 0) { log_printf(LOG_DEBUG, I_NOUSED, "creat thread failed\n"); return -1; } else{ thread_setnamev("auto_adj_thr"); } return 0; } int thr_syslog_thread(void) { int ret = -1; pthread_t syslog_thr_id; ret = pthread_create(&syslog_thr_id, NULL, (void *)syslog_thr, NULL);// if(ret != 0) { log_printf(LOG_DEBUG, I_NOUSED, " Err.thr_syslog_thread creat thread err.\n"); return -1; } else{ log_printf(LOG_DEBUG, I_NOUSED, " Ok .thr_syslog_thread create succeed.\n"); //thread_setnamev("lcd_key_thr"); } return 0; } void * sec_check_thr(void * args) { printf(">> sec_check_thr Start.\n"); while(1) { check_send_alarm_state(); sleep(1); } } static int thr_sec_check_thread(void) { int ret = -1; pthread_t udp_remote_thr_id; ret = pthread_create(&udp_remote_thr_id, NULL, (void *)udp_remote_thr, NULL); if(ret != 0) { printf(" Err.[%s,%d] creat thread err.\n", __FUNCTION__,__LINE__); return -1; } else { printf(" Ok .[%s,%d] creat thread succeed.\n", __FUNCTION__,__LINE__); thread_setnamev("udp remot"); } return 0; } void* check_ntp_sync_thr(void* args) { static u32 sec_cnt=0; while(1) { sec_cnt++; if(atsc_para.log_para.ntp_manual_sync==1){ ntp_manual_sync(); atsc_para.log_para.ntp_manual_sync = 0; sec_cnt = 40; } if(sec_cnt%60==0 && atsc_para.system_para.ntp_switch==1 && get_upgrade_flg()==0){ check_ntp_sync_state(); } else{ if(atsc_para.system_para.ntp_switch==0 && atsc_para.log_para.ntp_state==1){ atsc_para.log_para.ntp_state = 0; atsc_para.log_para.ntp_state_last= 0; printf("ntp_sync_changed 1->0\n"); } } sleep(1); } } void creat_check_ntp_state_thread(void) { int ret = -1; pthread_t ntp_sync_thr; ret = pthread_create(&ntp_sync_thr, NULL, (void *)check_ntp_sync_thr, NULL); if(ret != 0){ log_printf(LOG_ERR,I_NOUSED," creat check_ntp_sync_thr err.\n"); return ; } else{ log_printf(LOG_DEBUG,I_NOUSED," Ok. Create check_ntp_sync_thr succeed.\n"); } return ; } int creat_gps_thread(void) { int ret=0; pthread_t gps_thr_id; printf("[.thr.]->-\n"); ret = gps_uart_hw_init(); if(ret<0){ g_gps_init_ok=0; } else{ g_gps_init_ok=1; } gps_ublox_init(); ret = pthread_create(&gps_thr_id, NULL, (void *)gps_uart_thr, NULL); if(ret != 0) { printf(" Err.[%s,%d] creat thread err.\n", __FUNCTION__,__LINE__); return -1; } else{ printf(" Ok .[%s,%d] creat thread succeed.\n", __FUNCTION__,__LINE__); } return 0; } void imx_create_user_task(void) { /* [3] lcd_task*/ creat_lcd_msg_thread();//LCD recv web msg thread #if THR_LCD thr_lcd_thread(); #endif /* [10] web_task*/ creat_web_msg_thread();//web server socket recv msg thread /* [b] */ thr_imx_upgrade_thread(); /* [4] fpga_upgrade_task*/ thr_fpga_upgrade_thread(); /* [9] uart thread */ creat_uart_msg_thread(); creat_uart_thread(); creat_console_ctrl_thread(); /* [a] tsoip_task*/ creat_tsoip_msg_thread(); thr_tsoip_thread(); /* [b] PR06-2 step4: IQ receive + spectrum thread (eth1 UDP 6100) */ { extern void *iq_rx_thr(void *arg); pthread_t iq_rx_thr_id; if (pthread_create(&iq_rx_thr_id, NULL, (void *)iq_rx_thr, NULL) == 0) thread_setnamev("iq_rx_thr"); else log_printf(LOG_DEBUG, I_NOUSED, "creat iq_rx thread failed\n"); } /* [1] auto_udp_task*/ thr_auto_adj_udp_thread(); /* [5] tcp remote protocol */ thr_tcp_remote_thread(); thr_udp_remote_thread(); #if THR_GPS /* [8] gps_task */ creat_gps_uart_msg_thread(); creat_gps_thread(); #endif thr_syslog_thread(); creat_check_ntp_state_thread(); #if 0 /* [7] spect_data_task */ /* [2] sntp_task*/ /* [6] swap_data_task */ /* [11] log_task */ //creat_log_msg_thread(); //creat_log_thread(); #endif } #else #endif//___usr_thread___ extern void tst_func_for_web(); uint32 xsec_cnt=0; int main(int argc, char *argv[]) { setvbuf(stdout, NULL, _IONBF, 0); if(argc > 1){ switch(atoi(argv[1])){ case 0: // start by system log_printf(LOG_DEBUG,I_NOUSED,"\n\nstart by system start.\n"); g_restart_app_flg = 0; break; case 1: // restart /* Keep argument 1 compatible with the deployed daemon, but use the same * no-power-off RF recovery path as a normal application start. A fresh * process must rebuild the TALISE handle and the FPGA-side JESD link; * g_restart_app_flg=1 skips adrv9009_init() and can leave TX unavailable. */ log_printf(LOG_DEBUG,I_NOUSED, "\n\nrestart by daemon, use normal RF recovery path.\n"); g_restart_app_flg = 0; break; } } log_printf(LOG_INFO, I_SYS_STATUS, "====System start===k=%d= 2607281650\n", USE_KOREA_VERSION); pr_soft_start_ver(); hw_per_init(); #if USE_KOREA_VERSION check_system_ca_state(); #else g_ca_value = 1; #endif thr_web_thread(); fpga_para_init(); // meun init atsc_para.system_para.spec_enable = 1; iq_spec_set_enable(1); uint8 data =fpga_byte_read(0x01,0xBB); printf("3.rd reg0xBB data=0x%x\n", data); data =fpga_byte_read(0x01,0xB4); printf("3.rd reg0xB4 data=0x%x\n", data); log_printf(LOG_INFO, I_SYS_STATUS, "sys init over.\n"); imx_create_user_task(); while(1) { sleep(5); xsec_cnt++; if(xsec_cnt>=2){ xsec_cnt=0; tst_func_for_web(); } } return 0; }