Files

573 lines
13 KiB
C

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <time.h>
#include <string.h>
#include <sys/prctl.h>
#include <dirent.h>
#include <sys/types.h>
#include <sys/dir.h>
#include <errno.h>
#include <fcntl.h>
#include <time.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdarg.h>
#include <signal.h>
#include "../inc/type.h"
#include "../inc/isys_com.h"
#include "../inc/ipara_cfg.h"
#include "../inc/eeprom.h"
#include "../inc/i2c_drv.h"
#define SYNC_PARA_TO_BKP_FILE "sh /aw/anywave/sync_to_bkp_file.sh"
#define LOAD_PARA_TO_CFG_FILE "sh /aw/anywave/load_to_cfg_file.sh"
extern int executeCMD(const char *cmd, char *result, u16 result_size);
typedef void (*sighandler_t)(int);
int read_file_data(char *file_name,char *buff, int len)
{
int fd;
int ret = 0;
fd = open(file_name,O_RDONLY);
if(fd == -1) {
log_printf(LOG_ERR,I_NOUSED," open %s failed\n",file_name);
return -1;
}
if(read(fd,buff,len)==-1) {
log_printf(LOG_ERR,I_NOUSED,"read %d bytes from %s failed\n", len, file_name);
close(fd);
return -1;
} else {
close(fd);
return 0;
}
}
int check_file_exist(char *filename)
{
if(filename == NULL) {
log_printf(LOG_ERR,I_NOUSED,"filename is null. \n");
return -1;
}
int ret;
if(!access(filename, F_OK))
return 0;
else
return -1;
}
#if 1//verify_sys_com
unsigned long get_sys_tick(void) // 10ms
{
struct timeval tv;
struct timezone tz;
gettimeofday(&tv, &tz);
return (unsigned long)(tv.tv_sec*1000+tv.tv_usec/1000);
//return ((unsigned long) time( NULL)); // us
}
int get_sys_time(S_SYS_TIME *ptime)
{
if(!ptime)
return -1;
/*ptime->hour = date_time.hour;
ptime->min = date_time.min;
ptime->sec = date_time.sec;
ptime->date = date_time.date;
ptime->month= date_time.month;
ptime->year = date_time.year;*/
struct tm *tim;
struct timeval t;
time_t b_time;
b_time = time(NULL);
tim = localtime(&b_time);
// gettimeofday(&t, NULL);
ptime->hour = tim->tm_hour;
ptime->min = tim->tm_min;
ptime->sec = tim->tm_sec;
ptime->date = tim->tm_mday;
ptime->month= tim->tm_mon + 1;
ptime->year = tim->tm_year + 1900;
//printf("%d-%d-%d %d:%d:%d\n",ptime->year,ptime->month, ptime->date, ptime->hour,ptime->min,ptime->sec );
return ((int)tim->tm_wday);
}
void utc2localtime(time_t tUtcTime, struct tm *ptNow, int iZoneOffset)
{
int month[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
if (tUtcTime <= 0 || ptNow == NULL) {
return;
}
gmtime_r(&tUtcTime, ptNow);
ptNow->tm_year += 1900;
ptNow->tm_mon += 1;
ptNow->tm_hour += iZoneOffset;
if (ptNow->tm_hour >= 24) {
ptNow->tm_hour -= 24;
ptNow->tm_mday += 1;
} else if (ptNow->tm_hour < 0) {
ptNow->tm_hour += 24;
ptNow->tm_mday -= 1;
}
int current_month_max = month[ptNow->tm_mon];
if (ptNow->tm_mon == 2) {
if ((ptNow->tm_year % 400 == 0) || (ptNow->tm_year % 4 == 0 && ptNow->tm_year % 100 != 0)) {
current_month_max = 29;
}
}
if (ptNow->tm_mday > current_month_max) {
ptNow->tm_mday = 1;
ptNow->tm_mon += 1;
} else if (ptNow->tm_mday < 1) {
ptNow->tm_mon -= 1;
if (ptNow->tm_mon < 1) {
ptNow->tm_mon = 12;
ptNow->tm_year -= 1;
}
int prev_month = ptNow->tm_mon;
int prev_month_max = month[prev_month];
if (prev_month == 2) {
if ((ptNow->tm_year % 400 == 0) || (ptNow->tm_year % 4 == 0 && ptNow->tm_year % 100 != 0)) {
prev_month_max = 29;
}
}
ptNow->tm_mday = prev_month_max;
}
if (ptNow->tm_mon > 12) {
ptNow->tm_mon = 1;
ptNow->tm_year += 1;
} else if (ptNow->tm_mon < 1) {
ptNow->tm_mon = 12;
ptNow->tm_year -= 1;
}
}
void get_beijing_time(struct tm *ptmBJ)
{
time_t time_cur = 0;
int BeiJing_zone_dffset = 8; //Beijing +8
memset(ptmBJ, 0, sizeof(struct tm));
time_cur = time(NULL);
utc2localtime(time_cur, ptmBJ, BeiJing_zone_dffset);
}
void set_sys_time(S_SYS_TIME *p)
{
#if 1
struct tm tptr;
struct timeval tv;
if(!p) {
return;
}
tptr.tm_year = p->year - 1900;
tptr.tm_mon = p->month - 1;
tptr.tm_mday = p->date;
tptr.tm_hour = p->hour;
tptr.tm_min = p->min;
tptr.tm_sec = p->sec;
tv.tv_sec = mktime(&tptr);
tv.tv_usec = 0;
settimeofday(&tv, NULL);
#else
RTC_DateTypeDef date;
RTC_TimeTypeDef time;
if(!p) {
return;
}
date.RTC_Year = p->year-2000;
date.RTC_Date = p->date;
date.RTC_Month= p->month;
time.RTC_Hours = p->hour;
time.RTC_Minutes= p->min;
time.RTC_Seconds= p->sec;
rtc_set_date_time(&date,&time);
#endif
}
void usr_print_current_time(void)
{
struct timeval tv;
gettimeofday(&tv, NULL);
log_printf(LOG_DEBUG,I_NOUSED,"time: %d.%d\n",tv.tv_sec,tv.tv_usec/1000);
}
uint32 usr_get_cur_time_ms(void)
{
struct timeval tv;
gettimeofday(&tv, NULL);
return (tv.tv_sec*1000+tv.tv_usec/1000);
// printf("time: %d.%d\n",tv.tv_sec,tv.tv_usec/1000);
}
void thread_setnamev(const char* pname)
{
prctl(PR_SET_NAME,pname,NULL,NULL,NULL);
}
#if 1
#include <stdarg.h>
static struct tm bkp_time={0};
int check_sec_change(void)
{
int ret=-1;
struct tm *tim;
struct tm *bk_tim;
static struct tm bkp_time={0};
time_t t_time;
t_time = time(NULL);
tim = localtime(&t_time);
if(tim->tm_sec != bkp_time.tm_sec){
bkp_time.tm_sec = tim->tm_sec;
//printf("sec=%d\n",tim->tm_sec);
ret = 1;
}
return ret;
}
void printf_wrapperV(const char* format, va_list args_list)
{
vprintf(format, args_list);
}
void usr_sec_printf(const char* format, ...)
{
va_list marker;
if(check_sec_change()>0)
{
va_start(marker, format);
printf_wrapperV(format, marker);
va_end(marker);
}
}
#endif
int pox_system(const char *cmd_line)
{
int ret = 0;
sighandler_t old_handler;
old_handler = signal(SIGCHLD, SIG_DFL);
ret = system(cmd_line);
signal(SIGCHLD, old_handler);
return ret;
}
int usr_set_sys_rtc_time(S_SYS_TIME *psys_time)
{
char cmd[100] = {0};
struct tm usr_tm;
struct tm *p_utc_tm;
int zone_offset = 0; // beijing +8
usr_tm.tm_year = psys_time->year;// 2022-1900
usr_tm.tm_mon = psys_time->month-1;
usr_tm.tm_mday = psys_time->date;
usr_tm.tm_hour = psys_time->hour;
usr_tm.tm_min = psys_time->min;
usr_tm.tm_sec = psys_time->sec;
time_t timestamp = mktime(&usr_tm) - (zone_offset*60*60);
p_utc_tm = localtime(&timestamp);
if(p_utc_tm==NULL)
return -1;
memset(cmd, 0, sizeof(cmd));
sprintf(cmd, "date -s '%d-%02d-%02d %02d:%02d:%02d'",
p_utc_tm->tm_year+1900,
p_utc_tm->tm_mon+1,
p_utc_tm->tm_mday,
p_utc_tm->tm_hour,
p_utc_tm->tm_min,
p_utc_tm->tm_sec);
log_printf(LOG_DEBUG,I_NOUSED,"cmd=%s\n", cmd);
system(cmd);
system("hwclock -w");
}
void usr_get_sys_rtc_time(S_SYS_TIME *psys_time,S_ADVANCE_DT_PARA *pdt_time)
{
time_t time_cur = 0;
struct tm tmBJ;
int BeiJing_zone_dffset = 8; //Beijing +8
memset(&tmBJ, 0, sizeof(struct tm));
time_cur = time(NULL);
utc2localtime(time_cur, &tmBJ, BeiJing_zone_dffset);
if(pdt_time!=NULL){
pdt_time->dd.RTC_Year = tmBJ.tm_year-1900;
pdt_time->dd.RTC_Month = tmBJ.tm_mon;
pdt_time->dd.RTC_Date = tmBJ.tm_mday;
pdt_time->tt.RTC_Hours = tmBJ.tm_hour;
pdt_time->tt.RTC_Minutes = tmBJ.tm_min;
pdt_time->tt.RTC_Seconds = tmBJ.tm_sec;
pdt_time->tt.RTC_H12 = 0;// 0-AM24h 1-PM12h*/
}
if(psys_time!=NULL){
psys_time->year = tmBJ.tm_year-1900;
psys_time->month= tmBJ.tm_mon;
psys_time->date = tmBJ.tm_mday;
psys_time->hour = tmBJ.tm_hour;
psys_time->min = tmBJ.tm_min;
psys_time->sec = tmBJ.tm_sec;
}
}
#if 1
extern char release_ver[30];
enum
{
RUN_INFO_LOG_STEP_START=0,
RUN_INFO_LOG_TOTAL_CPU_STEP,
RUN_INFO_LOG_TOTAL_MEM_STEP,
RUN_INFO_LOG_TOTAL_DISK_STEP,
RUN_INFO_LOG_RUNTIME_STEP,
RUN_INFO_LOG_APP_CPU_RAM_STEP,
RUN_INFO_LOG_STEP_END
};
static void hardware_log_info(unsigned char log_step)
{
int ret=0;
char shellCmd[300];
if(log_step >= RUN_INFO_LOG_STEP_END)
{
return;
}
switch(log_step)
{
case RUN_INFO_LOG_STEP_START:
//version
//usr_normal_printf("log imx-app-ver:%s\n",release_ver);
log_printf(LOG_INFO, I_RUN_STATUS, "log imx-app-ver:%s\n",release_ver);
break;
case RUN_INFO_LOG_TOTAL_CPU_STEP:
//cpu
memset(shellCmd,0,sizeof(shellCmd));
sprintf(shellCmd,"top -n 1 -b | grep CPU | head -n 1 >&1");//len=54
ret = pox_system(shellCmd);
if(ret<0)
log_printf(LOG_ERR, I_RUN_STATUS, "Err.shell cmd get total CPU err.\n");
break;
case RUN_INFO_LOG_TOTAL_MEM_STEP:
//mem
memset(shellCmd,0,sizeof(shellCmd));
sprintf(shellCmd,"top -n 1 -b | grep Mem | head -n 1 >&1");
ret = pox_system(shellCmd);
if(ret<0)
log_printf(LOG_ERR, I_RUN_STATUS, "Err.shell cmd get total Mem err.\n");
break;
case RUN_INFO_LOG_TOTAL_DISK_STEP:
//disk
memset(shellCmd,0,sizeof(shellCmd));
sprintf(shellCmd,"df -h | grep Size >&1");
ret = pox_system(shellCmd);
if(ret<0)
log_printf(LOG_ERR, I_RUN_STATUS, "shell cmd df err.\n");
memset(shellCmd,0,sizeof(shellCmd));
sprintf(shellCmd,"df -h | grep / | head -n 1 >&1");
ret = pox_system(shellCmd);
if(ret<0)
log_printf(LOG_ERR, I_RUN_STATUS, "Err.shell cmd df err.\n");
//usr_normal_printf("Err.shell cmd df err.\n");
break;
case RUN_INFO_LOG_RUNTIME_STEP:
//runtime
memset(shellCmd,0,sizeof(shellCmd));
sprintf(shellCmd,"uptime >&1");
ret = pox_system(shellCmd);
if(ret<0)
log_printf(LOG_ERR, I_RUN_STATUS, "Err.shell cmd uptime err.\n");
break;
case RUN_INFO_LOG_APP_CPU_RAM_STEP:
//app cpu/ram
memset(shellCmd,0,sizeof(shellCmd));
sprintf(shellCmd,"top -n 1 -b | grep VSZ | head -n 1 >&1");
ret = pox_system(shellCmd);
if(ret<0)
log_printf(LOG_ERR, I_RUN_STATUS, "Err.shell cmd uptime err.\n");
memset(shellCmd,0,sizeof(shellCmd));
sprintf(shellCmd,"top -n 1 -b | grep imx_atsc_app | head -n 1 >&1");
ret = pox_system(shellCmd);
if(ret<0)
log_printf(LOG_ERR, I_RUN_STATUS, "Err.shell cmd uptime err.\n");
memset(shellCmd,0,sizeof(shellCmd));
sprintf(shellCmd,"top -n 1 -b | grep http | head -n 1 >&1");
ret = pox_system(shellCmd);
if(ret<0)
log_printf(LOG_ERR, I_RUN_STATUS, "Err.shell cmd uptime err.\n");
break;
default:break;
}
}
void check_and_load_bkppara_to_cfgfile()
{
u8 ret;
char shellCmd[100] = {0};
//cfg
ret = check_file_exist(F_EXC_CFG_PARA_FILE);
if(ret==0)
log_printf(LOG_INFO, I_RUN_STATUS, "cfg_file exist\n");
else
log_printf(LOG_INFO, I_RUN_STATUS, "cfg_file is non-exist\n");
//bkp
ret = check_file_exist(F_EXC_CFG_PARA_BKP_FILE);
if(ret==0){
//memset(shellCmd,0,sizeof(shellCmd));
//sprintf(shellCmd,"%s");//len=54
ret = pox_system(LOAD_PARA_TO_CFG_FILE);
//ret = executeCMD(LOAD_PARA_TO_CFG_FILE, msg);
if(ret==0)
log_printf(LOG_INFO, I_RUN_STATUS, "bkp_file is load to cfg_file\n");
else
log_printf(LOG_ERR, I_RUN_STATUS, "bkp_file failed load to cfg_file\n");
}else{
log_printf(LOG_WARN, I_RUN_STATUS, "bkp_file is non-existent\n");
}
}
static void sync_cfgpara_to_bkpfile()
{
u8 ret;
char msg[20] = {0};
ret = check_file_exist(F_EXC_CFG_PARA_FILE);
if(ret==0 && (E2PROM_MAGIC_WORD==c256_byteread(EEPROM_DEFAULT_PARAM_ADDR))){
ret = pox_system(SYNC_PARA_TO_BKP_FILE);
//ret = executeCMD(SYNC_PARA_TO_BKP_FILE, msg);
if(ret==0)
log_printf(LOG_INFO, I_RUN_STATUS, "cfg file is sync to bkp file\n");
else
log_printf(LOG_ERR, I_RUN_STATUS, "cfg file failed sync to bkp file\n");
}else{
log_printf(LOG_WARN, I_RUN_STATUS, "cfg file is non-existent\n");
}
}
void syslog_thr(void * pvParameters)
{
static int sec_cnt=0;
static char log_record_flg=0;
static char log_step=RUN_INFO_LOG_STEP_START;
static char boot_flg=1;
log_printf(LOG_INFO, I_NOUSED, "===syslog_thr start===\n");
while(1)
{
sec_cnt++;
// 2hour || boot 2min
if( (sec_cnt%(2*60*60)==0) || (sec_cnt>=120 && boot_flg) ){
boot_flg=0;
log_record_flg=1;
}
if(log_record_flg)
{
if(log_step >= RUN_INFO_LOG_STEP_END){
log_record_flg = 0;
log_step = RUN_INFO_LOG_STEP_START;
//usr_normal_printf("Sta.finish record run info to log.\n");
log_printf(LOG_INFO, I_RUN_STATUS, "Sta.finish record run info to log.\n");
sync_cfgpara_to_bkpfile();
}
else{
hardware_log_info(log_step);
log_step++;
}
}
sleep(1);
}
}
#endif
#endif//verify_sys_com