1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009 |
- /*===========================================================================
- Combined Charging System (CCS): SECC
- main.c
- initiated by Vern, Joseph
- (since 2019/07/19)
- =============================================================================*/
- #include <sys/types.h>
- #include <sys/stat.h>
- #include <fcntl.h>
- #include <linux/termios.h>
- #include <stdio.h>
- #include <string.h>
- #include <time.h>
- #include <stdlib.h>
- #include <sys/ipc.h>
- #include <sys/shm.h>
- #include <sys/mman.h>
- #include <linux/sockios.h>
- #include <linux/socket.h>
- #include <sys/socket.h>
- #include <netinet/in.h>
- #include <sys/time.h>
- #include <sys/timeb.h>
- #include <math.h>//for pow
- #include <unistd.h>
- #include "define.h"
- #include "main.h"
- struct SysConfigAndInfo *ShmSysConfigAndInfo;
- struct StatusCodeData *ShmStatusCodeData;
- struct CcsData *ShmCcsData;
- struct InternalComm *ShmInternalComm;
- //struct InternalCommAC *ShmInternalCommAC;
- unsigned char buf_log_main[SIZE_OF_LOG_BUFFER];
- #if SAVE_SYS_LOG_MSG_MAIN_SWITCH == ENABLE
- int StoreLogMsg(unsigned char *DataString);
- #endif
- void System_Init();
- void CreateShareMemory_Init();
- void GPIO_Init();
- void PWM_Init();
- void Confinguration_Init();
- double DiffTimeb(struct timeb ST, struct timeb ET);
- int CreateShareMemory();
- int LoadSysConfigAndInfo(struct SysConfigData *ptr);
- int SpawnTask();
- int StoreUsrConfigData(struct SysConfigData *UsrData);
- unsigned int isKernelSupportNAT()
- {
- unsigned int result = 0;
- unsigned int version = 0;
- FILE *fp;
- char cmd[256];
- char buf[512];
- // Get IP address & net mask
- strcpy(cmd, "uname -v");
- fp = popen(cmd, "r");
- if(fp != NULL)
- {
- if(fgets(buf, sizeof(buf), fp) != NULL)
- {
- sscanf(buf, "#%d", &version);
- // DEBUG_INFO("Kernel version: %d\n", version);
- if(version >= 30)
- result = 1;
- }
- }
- pclose(fp);
- return result;
- }
- float ReadAdcVolt(unsigned char AdcChannel)
- {
- //AIN0=CCS GUN Temp 1
- //AIN1=CCS GUN Temp 2
- //AIN2=CCS_Proximity/2
- //AIN3=pilot voltage
- if(AdcChannel == 3)
- {
- int fd,count,AvgTimes;
- unsigned char SampleBuf[4];
- float TmpVolt, MinSample, AvgSample = 0;
- fd = open("/sys/bus/iio/devices/iio\:device0/in_voltage3_raw", O_RDONLY);
- if(fd > 0)
- {
- //system("echo 1 > /sys/class/gpio/gpio89/value"); //for test
- for(AvgTimes = 0; AvgTimes < 3; AvgTimes++) //period = 60~91 ms(renice -10, , CurrentDemand()) /*+++ 20200909, vern, extend detection time for interference ---*/
- {
- count = 0;
- MinSample = 2306;
- //system("echo 1 > /sys/class/gpio/gpio89/value"); //for test
- while(count < 40) //period = 21~42ms (renice -10, CurrentDemand())/*+++ 20200909, vern, extend detection time for interference ---*/
- {
- //re-sampling period = 3~13ms (renice -10, SLAC())
- //system("echo 1 > /sys/class/gpio/gpio89/value"); //for test
- read(fd, SampleBuf, 4); //period = 3.2~10ms (renice -10, SLAC())
- //system("echo 0 > /sys/class/gpio/gpio89/value"); //for test
- TmpVolt = atoi(SampleBuf);
-
- if((TmpVolt < 2306) && (TmpVolt > 0))//positive voltage
- {
- if(TmpVolt < MinSample)
- {
- MinSample = TmpVolt;
- }
- count++;
- }
- lseek(fd, 0, SEEK_SET);
- }
- //system("echo 0 > /sys/class/gpio/gpio89/value"); //for test
- AvgSample += MinSample;
- }
- AvgSample /= AvgTimes;
- close(fd);
- //system("echo 0 > /sys/class/gpio/gpio89/value"); //for test
- return ((0.954-(1.8*AvgSample/4095))/0.06);
- }
- else
- {
- return -1;
- }
- }
- else
- {
- FILE *fp;
- unsigned char str[64];
- unsigned char AdcValue[8];
- if(AdcChannel > 7)
- {
- return -1;
- }
- memset(str,0,sizeof(str));
- memset(AdcValue,0,sizeof(AdcValue));
- sprintf(str, "cat /sys/bus/iio/devices/iio\\:device0/in_voltage%d_raw", AdcChannel);
- fp=popen(str, "r");
- if(fgets(AdcValue,sizeof(AdcValue),fp) == NULL)
- {
- pclose(fp);
- return -1;
- }
- pclose(fp);
- //Vin = Vref *D / (2^n - 1)
- //printf("ADC[%d]=%d\n",AdcChannel,atoi(AdcValue));
- return ((float)1.8*(float)atoi(AdcValue))/4095;
- }
- }
- /*===========================================================================
- FUNCTION: main
- DESCRIPTION:
- 1. This main routine will be executed by the "rcS" when linux is booting up.
- /opt/ti-processor-sdk-linux-am335x-evm-04.02.00.09/EVSE/rootfs/etc/init.d/rcS
- PRE-CONDITION:
- INPUT:
- OUTPUT:
- GLOBAL VARIABLES:
- =============================================================================*/
- int main(int argc, char *argv[])
- {
- #ifdef CONNECTOR_TEMP_DETECTION
- float CcsConnectorTemp1;
- int CcsConnectorTemp;
- #endif
- SAVE_SYS_LOG_MSG_MAIN("ccs: on");
- SAVE_SYS_LOG_MSG_MAIN("---------------------------------------------");
- SAVE_SYS_LOG_MSG_MAIN("-- MAIN --");
- SAVE_SYS_LOG_MSG_MAIN("---------------------------------------------\n");
- //int Rtn = 0;
- //unsigned int StartTime;
- //struct timeb StartChargingTime, CurrentChargingTime, ChargingLoopTime;
- //float ChargingPower, ChargingEnergy;
- //CSU Initialization
- System_Init();
- //task spawn
- SpawnTask();
- sleep(2);
- system("killall ntpd");
- /**************************************************************/
- /************** main Loop********************************/
- /*****************************************************************/
- while(1)
- {
- //Sleep to reduce CPU power comsumption
- sleep(5);
- #ifdef CONNECTOR_TEMP_DETECTION
- CcsConnectorTemp=0;
- for(unsigned char i=0;i<=1;i++)
- {
- CcsConnectorTemp1 = ReadAdcVolt(i);
- if((CcsConnectorTemp1<=0.9)&&(CcsConnectorTemp1>=0.8))//0 ~ -40
- CcsConnectorTemp1=(CcsConnectorTemp1-0.9)*500;
- else if((CcsConnectorTemp1<=1.07)&&(CcsConnectorTemp1>0.9))
- CcsConnectorTemp1=(CcsConnectorTemp1-0.9)*705.88;
- else
- CcsConnectorTemp1=195;//not available
- CcsConnectorTemp|=((unsigned int)(CcsConnectorTemp1+60)&0xFF)<<(i*8); //0x00(-60)~0xFE(194)
- //printf("CcsConnectorTemp1[%d]=%f\n ",i,CcsConnectorTemp1);
- }
- ShmSysConfigAndInfo->SysInfo.CcsConnectorTemp=CcsConnectorTemp;
- //printf("CcsConnectorTemp=0x%x\n ",CcsConnectorTemp);
- #endif
- }//main while
- }
- /*===========================================================================
- FUNCTION: Eth0_PortSetting
- DESCRIPTION:
- PRE-CONDITION:
- INPUT:
- OUTPUT:
- GLOBAL VARIABLES:
- =============================================================================*/
- int Eth0_PortSetting_Base(unsigned char addr)
- {
- SAVE_SYS_LOG_MSG_MAIN("[Eth0_PortSetting]start");
- unsigned char cmd[128];
- if ((addr >= 20) && (addr <= (255-20-1))) //preserved for dynamic IP and QCA7000
- {
- //Step 0: shutdown eth0
- #if 0
- SAVE_SYS_LOG_MSG_MAIN("[Eth0_PortSetting][eth0]down");
- system("/sbin/ifconfig eth0 down");
- sleep(1);
- #endif
- //Step 1: eth0 setting
- sprintf(buf_log_main,
- "[Eth0_PortSetting][eth0]IP >> 192.168.0.%d(up)",
- addr);
- SAVE_SYS_LOG_MSG_MAIN(buf_log_main);
- sprintf(cmd, "/sbin/ifconfig eth0 192.168.0.%d netmask 255.255.255.0 up", addr);
- system(cmd);
- memset(cmd, 0, sizeof(cmd));
- sleep(1);
- //Step 2: add route
- system("/sbin/route add default gw 192.168.0.1");
- //Step 3: done
- sprintf(buf_log_main,
- "[Eth0_PortSetting]done");
- SAVE_SYS_LOG_MSG_MAIN(buf_log_main);
- return PASS;
- }
- else
- {
- sprintf(buf_log_main,
- "[Eth0_PortSetting][Error]addr(%d) is out of range(20~234)",
- addr);
- SAVE_SYS_LOG_MSG_MAIN(buf_log_main);
- return FAIL;
- }
- }
- /*===========================================================================
- FUNCTION: System_Init
- DESCRIPTION:
- PRE-CONDITION:
- 1. Pin Assignment Configuration
- board-support/linux-4.9.59+gitAUTOINC+a75d8e9305-ga75d8e9305/
- arch/arm/boot/dts/[CCS]am335x-evm.dts
- INPUT:
- OUTPUT:
- GLOBAL VARIABLES:
- =============================================================================*/
- void System_Init()
- {
- //RTC Update
- time_t rtc_new;
- rtc_new = RTC_DEFAULT_TIME; //Epoch time
- stime(&rtc_new);
- system("date");
- sprintf(buf_log_main, "[RTC]%d (DEC)\n", rtc_new);
- SAVE_SYS_LOG_MSG_MAIN(buf_log_main);
- //-----------------------------------
- //Print Linux Kernel Version
- sprintf(buf_log_main, "\nLinux Image SHA1: %s\n", LINUX_IMAGE_VERSION);
- SAVE_SYS_LOG_MSG_MAIN(buf_log_main);
- //Print Hardware Version
- sprintf(buf_log_main, "HW: %s\n", HARDWARE_VERSION);
- SAVE_SYS_LOG_MSG_MAIN(buf_log_main);
- //Print Firmware Version
- sprintf(buf_log_main, "FW: %s\n", FIRMWARE_VERSION);
- SAVE_SYS_LOG_MSG_MAIN(buf_log_main);
- #if (FIRMWARE_VERSION_COMPILE_SETTING_RELEASE_MODE == ENABLE)
- {
- //Print Linux Kernel Version
- printf("\nLinux Image SHA1: %s\n", LINUX_IMAGE_VERSION);
- //Print Hardware Version
- printf("HW: %s\n", HARDWARE_VERSION);
- //Print Firmware Version
- printf("FW: %s\n", FIRMWARE_VERSION);
- }
- #endif
- //Init Eth0
- Eth0_PortSetting_Base(ETH0_PORT_BASE); //192.168.0.20
- // ---------- Hardware Init ---------
- if(isKernelSupportNAT() == 1)
- system("/sbin/insmod /lib/qcaspi_nat.ko");
- else
- system("/sbin/insmod /lib/qcaspi.ko");
- // system("/sbin/insmod /lib/qcaspi.ko");
- sleep(2);
- system("/sbin/ifconfig eth1 192.168.0.11 netmask 255.255.255.0 up");
- sleep(1);
- printf("Starting SSH : \n");
- system("/sbin/dropbear &");
- printf("Starting FTPD : \n");
- system("/usr/bin/tcpsvd -vE 0.0.0.0 21 ftpd -w -t 30 / &");
- //printf("Starting LIGHTTPD : \n");
- //system("/sbin/lighttpd -f /etc/lighttpd/lighttpd.conf -m /lib");
- //Init GPIO
- GPIO_Init();
- //Init ecap0
- PWM_Init();
- // ---------- Software Init ----------
- //Create all share memory
- CreateShareMemory_Init();
- //Init System Confinguration
- //Confinguration_Init();
- SAVE_SYS_LOG_MSG_MAIN("--------------------------");
- SAVE_SYS_LOG_MSG_MAIN("System_Init(): DONE");
- }
- /*===========================================================================
- FUNCTION: CreateShareMemory_Init
- DESCRIPTION:
- PRE-CONDITION:
- INPUT:
- OUTPUT:
- GLOBAL VARIABLES:
- =============================================================================*/
- void CreateShareMemory_Init()
- {
- if(CreateShareMemory() == 0)
- {
- SAVE_SYS_LOG_MSG_MAIN("CreateShareMemory_Init(): NG");
- if(ShmStatusCodeData != NULL)
- {
- ShmStatusCodeData->AlarmCode.AlarmEvents.bits.FailToCreateShareMemory = 1;
- }
- sleep(5);
- system("reboot -f");
- sleep(5);
- system("reboot -f");
- }
- else
- {
- SAVE_SYS_LOG_MSG_MAIN("CreateShareMemory_Init(): DONE");
- }
- }
- /**************************************************************************************/
- /**************************Create all share memory *********************************/
- /**************************************************************************************/
- /*===========================================================================
- FUNCTION: CreateShareMemory
- DESCRIPTION:
- There are 4 share memory created here, which are listed as below.
- 1) ShmSysConfigAndInfo
- 2) ShmStatusCodeData
- 3) ShmCcsData
- 4) ShmInternalComm
- PRE-CONDITION:
- INPUT:
- OUTPUT:
- GLOBAL VARIABLES:
- =============================================================================*/
- int CreateShareMemory()
- {
- int MeterSMId;
- //create ShmSysConfigAndInfo
- if((MeterSMId = shmget(ShmSysConfigAndInfoKey, sizeof(struct SysConfigAndInfo), IPC_CREAT | 0777)) < 0)
- {
- SAVE_SYS_LOG_MSG_MAIN("CreateShareMemory:shmget ShmSysConfigAndInfo NG");
- return 0;
- }
- else if((ShmSysConfigAndInfo = shmat(MeterSMId, NULL, 0)) == (void *)-1)
- {
- SAVE_SYS_LOG_MSG_MAIN("CreateShareMemory:shmat ShmSysConfigAndInfo NG");
- return 0;
- }
- memset(ShmSysConfigAndInfo, 0, sizeof(struct SysConfigAndInfo));
- //create ShmStatusCodeData
- if((MeterSMId = shmget(ShmStatusCodeKey, sizeof(struct StatusCodeData), IPC_CREAT | 0777)) < 0)
- {
- SAVE_SYS_LOG_MSG_MAIN("CreateShareMemory:shmget ShmStatusCodeData NG");
- return 0;
- }
- else if((ShmStatusCodeData = shmat(MeterSMId, NULL, 0)) == (void *)-1)
- {
- SAVE_SYS_LOG_MSG_MAIN("CreateShareMemory:shmat ShmStatusCodeData NG");
- return 0;
- }
- memset(ShmStatusCodeData, 0, sizeof(struct StatusCodeData));
- //create ShmCcsData
- if((MeterSMId = shmget(ShmCcsCommKey, sizeof(struct CcsData), IPC_CREAT | 0777)) < 0)
- {
- SAVE_SYS_LOG_MSG_MAIN("CreateShareMemory:shmget ShmCcsData NG");
- return 0;
- }
- else if((ShmCcsData = shmat(MeterSMId, NULL, 0)) == (void *)-1)
- {
- SAVE_SYS_LOG_MSG_MAIN("CreateShareMemory:shmat ShmCcsData NG");
- return 0;
- }
- memset(ShmCcsData, 0, sizeof(struct CcsData));
- //create ShmInternalComm
- if((MeterSMId = shmget(ShmInternalCommKey, sizeof(struct InternalComm), IPC_CREAT | 0777)) < 0)
- {
- SAVE_SYS_LOG_MSG_MAIN("CreateShareMemory:shmget ShmInternalComm NG");
- return 0;
- }
- else if((ShmInternalComm = shmat(MeterSMId, NULL, 0)) == (void *)-1)
- {
- SAVE_SYS_LOG_MSG_MAIN("CreateShareMemory:shmat ShmInternalComm NG");
- return 0;
- }
- memset(ShmInternalComm, 0, sizeof(struct InternalComm));
- #if 0
- //create ShmInternalCommAC
- if((MeterSMId = shmget(ShmInternalCommACKey, sizeof(struct InternalCommAC), IPC_CREAT | 0777)) < 0)
- {
- SAVE_SYS_LOG_MSG_MAIN("CreateShareMemory:shmget ShmInternalCommAC NG");
- return 0;
- }
- else if((ShmInternalCommAC = shmat(MeterSMId, NULL, 0)) == (void *)-1)
- {
- SAVE_SYS_LOG_MSG_MAIN("CreateShareMemory:shmat ShmInternalCommAC NG");
- return 0;
- }
- //[To-Do] The reset here should be removed, since AW-CCS CSU will reset it in its main.c.
- memset(ShmInternalCommAC, 0, sizeof(struct InternalCommAC));
- #endif
- return 1;
- }
- /*===========================================================================
- FUNCTION: GPIO_Init
- DESCRIPTION:
- 1. GPIO (input) x 1
- * gpio2_0(gpio64): QCA7000 interrupt
- * gpio2_23 (gpio87): CCS Board ID detection
- 2. GPIO (output) x 4
- * gpio2_25 (gpio89): reserved (default = 0)
- * gpio2_24 (gpio88): power reset QCA7000 (default = 0)
- * gpio2_22 (gpio86): Pilot Status E control (0V) (default = 0)
- PRE-CONDITION:
- INPUT:
- OUTPUT:
- GLOBAL VARIABLES:
- =============================================================================*/
- void GPIO_Init()
- {
- /*****************0~3, 4 bank, bank x 32+ num*********************/
- /*****************************************************************/
- /*************** INPUT PIN ***************************************/
- /*****************************************************************/
- /*GPIO2_0: QCA7000 interrupt */
- system("echo 64 > /sys/class/gpio/export");
- system("echo \"in\" > /sys/class/gpio/gpio64/direction");
- //IO port for CCS Board ID detection
- /*GPIO2_23: AM_IO_1, H: ?; L: ?*/
- system("echo 87 > /sys/class/gpio/export");
- system("echo \"in\" > /sys/class/gpio/gpio87/direction");
- /***************************************************************/
- /*************** OUTPUT PIN ************************************/
- /***************************************************************/
- //IO port for Communication Board.(reserved for emergency shutdown PSU, i.e. PSKILL)
- /*GPIO2_25: AM_IO_2, H: ?; L: ?*/
- system("echo 89 > /sys/class/gpio/export");
- system("echo \"out\" > /sys/class/gpio/gpio89/direction");
- system("echo 0 > /sys/class/gpio/gpio89/value");
- //Power Reset QCA7000
- /*GPIO2_24: AM_QCA_PWR_RST, H: reset, L: noraml*/
- system("echo 88 > /sys/class/gpio/export");
- system("echo \"out\" > /sys/class/gpio/gpio88/direction");
- system("echo 0 > /sys/class/gpio/gpio88/value");
- //Pilot Status E control (0V)
- /*GPIO2_22: Pilot_state E, H: state E, L: normal*/
- system("echo 86 > /sys/class/gpio/export");
- system("echo \"out\" > /sys/class/gpio/gpio86/direction");
- system("echo 0 > /sys/class/gpio/gpio86/value");
- SAVE_SYS_LOG_MSG_MAIN("GPIO_Init(): DONE");
- }
- /*===========================================================================
- FUNCTION: PWM_Init
- DESCRIPTION:
- - Initialize the PWM channel by the following configuration.
- * Frequency = 1KHz
- * Duty = 100%
- - The default voltage should be as below.
- * 12V: unplugged
- * 9V: plugged
- - Check Mechanism (to-do)
- * Read CP Voltage and PWM by AIN3:
- // cat /sys/bus/iio/devices/iio\\:device0/in_voltage3_raw;
- PRE-CONDITION:
- INPUT:
- OUTPUT:
- - PWM = 100% at 1KHz
- GLOBAL VARIABLES:
- =============================================================================*/
- void PWM_Init()
- {
- //unsigned char str[256];
- //init ecap0
- //memset(str, 0, sizeof(str));
- system("echo 0 > /sys/class/pwm/pwmchip0/export");
- system("echo 1000000 > /sys/class/pwm/pwmchip0/pwm0/period");// nano seconds =>1k Hz
- system("echo 1000000 > /sys/class/pwm/pwmchip0/pwm0/duty_cycle"); //default 100%
- system("echo 1 > /sys/class/pwm/pwmchip0/pwm0/enable");
- //system("echo 1 > /sys /class/pwm/pwmchip0/pwm0/polarity");
- //PWM_Init_Check(); //Checking if the PWM analog signal is well-initialized
- //as 1KHz,100%, and 12V/9V.
- SAVE_SYS_LOG_MSG_MAIN("PWM_Init(): DONE");
- }
- /*===========================================================================
- FUNCTION:
- DESCRIPTION:
- PRE-CONDITION:
- INPUT:
- OUTPUT:
- GLOBAL VARIABLES:
- =============================================================================*/
- void Confinguration_Init()
- {
- //Load System Confinguration
- LoadSysConfigAndInfo(&ShmSysConfigAndInfo->SysConfig);
- SAVE_SYS_LOG_MSG_MAIN("Confinguration_Init(): DONE");
- }
- /*===========================================================================
- FUNCTION: StoreLogMsg
- DESCRIPTION:
- PRE-CONDITION:
- INPUT:
- OUTPUT:
- GLOBAL VARIABLES:
- =============================================================================*/
- #if SAVE_SYS_LOG_MSG_MAIN_SWITCH == ENABLE
- int StoreLogMsg(unsigned char *DataString)
- {
- static unsigned char Buf[1024];
- static time_t CurrentTime;
- static struct tm *tm;
- static struct timeval tv;
- memset(Buf, 0, sizeof(Buf));
- CurrentTime = time(NULL);
- tm = localtime(&CurrentTime);
- gettimeofday(&tv, NULL); // get microseconds, 10^-6
- sprintf(Buf, "echo \"[%04d%02d%02d: %02d:%02d:%02d.%06d][main]%s\" >> /Storage/SystemLog/[%04d.%02d]SystemLog",
- tm->tm_year + 1900,
- tm->tm_mon + 1,
- tm->tm_mday,
- tm->tm_hour,
- tm->tm_min,
- tm->tm_sec,
- tv.tv_usec,
- DataString,
- tm->tm_year + 1900,
- tm->tm_mon + 1);
- system(Buf);
- DEBUG_PRINTF_MAIN_SYSTEM_LOG("[%02d:%02d:%02d.%06d][main]%s \n",
- tm->tm_hour,
- tm->tm_min,
- tm->tm_sec,
- tv.tv_usec,
- DataString);
- //Reset the buf_log_main Buffer, i.e. DataString
- memset(buf_log_main, 0, SIZE_OF_LOG_BUFFER);
- }
- #endif
- /*===========================================================================
- FUNCTION: DiffTimeb
- DESCRIPTION:
- Caculating the time difference in ms precision (milli-second).
- PRE-CONDITION:
- INPUT:
- OUTPUT:
- GLOBAL VARIABLES:
- =============================================================================*/
- double DiffTimeb(struct timeb ST, struct timeb ET)
- {
- //return milli-second
- double StartTime, EndTime;
- double t_diff;
- StartTime = ((double)ST.time)*1000 + (double)ST.millitm;
- EndTime = ((double)ET.time)*1000 + (double)ET.millitm;
- t_diff = EndTime - StartTime;
- //printf("%.02lf - %.02lf = %.02lf\n", EndTime, StartTime, t_diff);
- if (t_diff < 0)
- {
- #if 0
- if (t_diff < -1000) //1000ms
- {
- sprintf(buf_log_main,
- "[Warning]StartTime(%.02lf) > EndTime(%.02lf), d(%.02lf)",
- StartTime,
- EndTime,
- t_diff);
- SAVE_SYS_LOG_MSG_MAIN(buf_log_main);
- }
- #endif
- return -1;
- }
- return t_diff;
- }
- /**************************************************************************************/
- /****************Following functions are CSU initialization***************************/
- /**************************************************************************************/
- /*===========================================================================
- FUNCTION:
- DESCRIPTION:
- PRE-CONDITION:
- INPUT:
- OUTPUT:
- GLOBAL VARIABLES:
- =============================================================================*/
- int SpawnTask()
- {
- //system("/root/EventLogging &");
- system("/root/CsuComm &");
-
- usleep(50000); //50ms
-
- //system("echo 50000 > /sys/class/pwm/pwmchip0/pwm0/duty_cycle"); //5%, for test.
- system("/root/SeccComm &");
- }
- /*===========================================================================
- FUNCTION:
- DESCRIPTION:
- PRE-CONDITION:
- INPUT:
- OUTPUT:
- GLOBAL VARIABLES:
- =============================================================================*/
- int StoreUsrConfigData(struct SysConfigData *UsrData)
- {
- int fd, wrd;
- unsigned int i, Chk;
- unsigned char *ptr;
- Chk = 0;
- ptr = (unsigned char *)UsrData;
- for(i = 0; i < sizeof(struct SysConfigData) - 4; i++)
- {
- Chk += *(ptr + i);
- }
- UsrData->Checksum = Chk;
- fd = open("/dev/mtdblock10", O_RDWR);
- if(fd < 0)
- {
- SAVE_SYS_LOG_MSG_MAIN("StoreUsrConfigData: open /dev/mtdblock10 NG");
- return 0;
- }
- wrd = write(fd, UsrData, sizeof(struct SysConfigData));
- close(fd);
- if(wrd != (sizeof(struct SysConfigData)))
- {
- SAVE_SYS_LOG_MSG_MAIN("StoreUsrConfigData: write /dev/mtdblock10 NG");
- return 0;
- }
- fd = open("/dev/mtdblock11", O_RDWR);
- if(fd < 0)
- {
- SAVE_SYS_LOG_MSG_MAIN("StoreUsrConfigData: open /dev/mtdblock11(backup) NG");
- return 0;
- }
- wrd = write(fd, UsrData, sizeof(struct SysConfigData));
- close(fd);
- if(wrd != (sizeof(struct SysConfigData)))
- {
- SAVE_SYS_LOG_MSG_MAIN("StoreUsrConfigData: write /dev/mtdblock11(backup) NG");
- return 0;
- }
- return 1;
- }
- /*===========================================================================
- FUNCTION: LoadSysConfigAndInfo
- DESCRIPTION:
- PRE-CONDITION:
- INPUT:
- OUTPUT:
- GLOBAL VARIABLES:
- =============================================================================*/
- int LoadSysConfigAndInfo(struct SysConfigData *ptr)
- {
- int fd, wrd;
- struct SysConfigData *buf;
- unsigned char *PtrBuf;
- unsigned int ChkSum, ChkSumOrg;
- if((buf = malloc(sizeof(struct SysConfigData))) == NULL)
- {
- SAVE_SYS_LOG_MSG_MAIN("LoadSysConfigAndInfo(): malloc buffer NG,rebooting..");
- if(ShmStatusCodeData != NULL)
- {
- ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CsuInitFailed = 1;
- }
- sleep(5);
- system("reboot -f");
- sleep(5);
- system("reboot -f");
- }
- memset(buf, 0, sizeof(struct SysConfigData));
- fd = open("/dev/mtdblock10", O_RDWR);
- if(fd < 0)
- {
- free(buf);
- SAVE_SYS_LOG_MSG_MAIN("LoadSysConfigAndInfo():open mtdblock10 NG,rebooting..");
- if(ShmStatusCodeData != NULL)
- {
- ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CsuInitFailed = 1;
- }
- sleep(5);
- system("reboot -f");
- sleep(5);
- system("reboot -f");
- }
- wrd = read(fd, buf, sizeof(struct SysConfigData));
- close(fd);
- if(wrd != (sizeof(struct SysConfigData)))
- {
- free(buf);
- SAVE_SYS_LOG_MSG_MAIN("LoadSysConfigAndInfo(): read SysConfigData data NG,rebooting..");
- if(ShmStatusCodeData != NULL)
- {
- ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CsuInitFailed = 1;
- }
- sleep(5);
- system("reboot -f");
- sleep(5);
- system("reboot -f");
- }
- PtrBuf = (unsigned char *)buf;
- ChkSum = 0;
- for(wrd = 0; wrd < (sizeof(struct SysConfigData) - 4); wrd++)
- {
- ChkSum += PtrBuf[wrd];
- }
- ChkSumOrg = buf->Checksum;
- if(ChkSum != ChkSumOrg)
- {
- SAVE_SYS_LOG_MSG_MAIN("LoadSysConfigAndInfo(): Primary SysConfigData checksum NG, read backup");
- fd = open("/dev/mtdblock11", O_RDWR);
- if(fd < 0)
- {
- free(buf);
- SAVE_SYS_LOG_MSG_MAIN("LoadSysConfigAndInfo(): open mtdblock11 (backup) NG,rebooting..");
- if(ShmStatusCodeData != NULL)
- {
- ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CsuInitFailed = 1;
- }
- sleep(5);
- system("reboot -f");
- sleep(5);
- system("reboot -f");
- }
- memset(buf, 0, sizeof(struct SysConfigData));
- wrd = read(fd, buf, sizeof(struct SysConfigData));
- close(fd);
- if(wrd != sizeof(struct SysConfigData))
- {
- free(buf);
- SAVE_SYS_LOG_MSG_MAIN("LoadSysConfigAndInfo(): read backup SysConfigData data NG,rebooting..");
- if(ShmStatusCodeData != NULL)
- {
- ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CsuInitFailed = 1;
- }
- sleep(5);
- system("reboot -f");
- sleep(5);
- system("reboot -f");
- }
- PtrBuf = (unsigned char *)buf;
- ChkSum = 0;
- for(wrd = 0; wrd < (sizeof(struct SysConfigData) - 4); wrd++)
- {
- ChkSum += PtrBuf[wrd];
- }
- ChkSumOrg = buf->Checksum;
- if(ChkSum != ChkSumOrg)
- {
- SAVE_SYS_LOG_MSG_MAIN("LoadSysConfigAndInfo(): backup SysConfigData checksum NG, read Factory default");
- fd = open("/dev/mtdblock12", O_RDWR);
- if(fd < 0)
- {
- free(buf);
- SAVE_SYS_LOG_MSG_MAIN("LoadSysConfigAndInfo(): open mtdblock12 (Factory default) NG,rebooting..");
- if(ShmStatusCodeData != NULL)
- {
- ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CsuInitFailed = 1;
- }
- sleep(5);
- system("reboot -f");
- sleep(5);
- system("reboot -f");
- }
- memset(buf, 0, sizeof(struct SysConfigData));
- wrd = read(fd, buf, sizeof(struct SysConfigData));
- close(fd);
- if(wrd != sizeof(struct SysConfigData))
- {
- free(buf);
- SAVE_SYS_LOG_MSG_MAIN("LoadSysConfigAndInfo(): read factory default SysConfigData data NG,rebooting..");
- if(ShmStatusCodeData != NULL)
- {
- ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CsuInitFailed = 1;
- }
- sleep(5);
- system("reboot -f");
- sleep(5);
- system("reboot -f");
- }
- PtrBuf = (unsigned char *)buf;
- ChkSum = 0;
- for(wrd = 0; wrd < (sizeof(struct SysConfigData) - 4); wrd++)
- {
- ChkSum += PtrBuf[wrd];
- }
- ChkSumOrg = buf->Checksum;
- if(ChkSum != ChkSumOrg)
- {
- SAVE_SYS_LOG_MSG_MAIN("LoadSysConfigAndInfo(): factory default SysConfigData checksum NG, restore factory default");
- goto DefaultShm;
- }
- }
- }
- //load OK
- memcpy((struct SysConfigData *)ptr, (struct SysConfigData *)buf, sizeof(struct SysConfigData));
- free(buf);
- SAVE_SYS_LOG_MSG_MAIN("LoadSysConfigAndInfo(): Load SysConfigData OK");
- return 1;
- DefaultShm:
- system("cd /root;./FactoryConfig");
- #if (FIRMWARE_VERSION_COMPILE_SETTING_RELEASE_MODE == DISABLE)
- {
- SAVE_SYS_LOG_MSG_MAIN("sync...");
- system("sync");
- }
- #endif
- sleep(5);
- system("reboot -f");
- sleep(5);
- system("reboot -f");
- }
|