123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809 |
- #include <sys/time.h>
- #include <sys/timeb.h>
- #include <sys/types.h>
- #include <sys/stat.h>
- #include <sys/types.h>
- #include <sys/ioctl.h>
- #include <sys/socket.h>
- #include <sys/ipc.h>
- #include <sys/shm.h>
- #include <sys/mman.h>
- #include <linux/wireless.h>
- #include <arpa/inet.h>
- #include <netinet/in.h>
- #include <unistd.h>
- #include <stdarg.h>
- #include <stdio.h> /*標準輸入輸出定義*/
- #include <stdlib.h> /*標準函數庫定義*/
- #include <unistd.h> /*Unix 標準函數定義*/
- #include <fcntl.h> /*檔控制定義*/
- #include <termios.h> /*PPSIX 終端控制定義*/
- #include <errno.h> /*錯誤號定義*/
- #include <string.h>
- #include <time.h>
- #include <ctype.h>
- #include <ifaddrs.h>
- #include <math.h>
- #include "../../define.h"
- #include "PrimaryComm.h"
- #include <stdbool.h>
- #define ARRAY_SIZE(A) (sizeof(A) / sizeof(A[0]))
- #define PASS 1
- #define FAIL -1
- #define YES 1
- #define NO 0
- #define EQUAL 0
- #define NORMAL 0
- #define ABNORMAL 1
- #define TILTSENSORDET 2
- typedef unsigned char byte;
- struct SysConfigAndInfo *ShmSysConfigAndInfo;
- struct StatusCodeData *ShmStatusCodeData;
- struct PrimaryMcuData *ShmPrimaryMcuData;
- struct MeterInformation *ShmCsuMeterData;
- struct DcCommonInformation *ShmDcCommonData;
- void trim(char *s);
- int mystrcmp(char *p1,char *p2);
- void substr(char *dest, const char* src, unsigned int start, unsigned int cnt);
- void split(char **arr, char *str, const char *del);
- int Uart1Fd;
- char *priPortName = "/dev/ttyS1";
- Ver ver;
- Gpio_in gpio_in;
- Rtc rtc;
- struct timespec _flash_time;
- byte flash = NO;
- struct timespec _tilt_time;
- byte tilt = NO;
- byte gun_count;
- byte _acSwitch;
- bool _isNeedChkTilt;
- byte _curDeviceStatus[3] = {0, 0, 0};
- byte _reCheckCount[3] = {0, 0, 0};
- struct ChargingInfoData *_chargingData[CHAdeMO_QUANTITY + CCS_QUANTITY + GB_QUANTITY];
- void PRINTF_FUNC(char *string, ...);
- int StoreLogMsg(const char *fmt, ...);
- #define DEBUG_INFO(format, args...) StoreLogMsg("[%s:%d][%s][Info] "format, __FILE__, __LINE__, __FUNCTION__, ##args)
- #define DEBUG_WARN(format, args...) StoreLogMsg("[%s:%d][%s][Warn] "format, __FILE__, __LINE__, __FUNCTION__, ##args)
- #define DEBUG_ERROR(format, args...) StoreLogMsg("[%s:%d][%s][Error] "format, __FILE__, __LINE__, __FUNCTION__, ##args)
- int StoreLogMsg(const char *fmt, ...)
- {
- char Buf[4096+256];
- char buffer[4096];
- va_list args;
- struct timeb SeqEndTime;
- struct tm *tm;
- va_start(args, fmt);
- int rc = vsnprintf(buffer, sizeof(buffer), fmt, args);
- va_end(args);
- memset(Buf,0,sizeof(Buf));
- ftime(&SeqEndTime);
- SeqEndTime.time = time(NULL);
- tm=localtime(&SeqEndTime.time);
- if (ShmSysConfigAndInfo->SysConfig.SwitchDebugFlag == YES)
- {
- sprintf(Buf,"%02d:%02d:%02d:%03d - %s",
- tm->tm_hour,tm->tm_min,tm->tm_sec,SeqEndTime.millitm, buffer);
- printf("%s \n", Buf);
- }
- else
- {
- sprintf(Buf,"echo \"%04d-%02d-%02d %02d:%02d:%02d:%03d - %s\" >> /Storage/SystemLog/[%04d.%02d]SystemLog_%s_Log",
- tm->tm_year+1900,tm->tm_mon+1,tm->tm_mday,tm->tm_hour,tm->tm_min,tm->tm_sec,SeqEndTime.millitm,
- buffer,
- tm->tm_year+1900,tm->tm_mon+1,
- ShmSysConfigAndInfo->SysConfig.SerialNumber);
- system(Buf);
- }
- return rc;
- }
- int DiffTimeb(struct timeb ST, struct timeb ET)
- {
- //return milli-second
- unsigned int StartTime,StopTime;
- StartTime=(unsigned int)ST.time;
- StopTime=(unsigned int)ET.time;
- return (StopTime-StartTime)*1000+ET.millitm-ST.millitm;
- }
- void PRINTF_FUNC(char *string, ...)
- {
- va_list args;
- char buffer[4096];
- va_start(args, string);
- vsnprintf(buffer, sizeof(buffer), string, args);
- va_end(args);
- DEBUG_INFO("%s ", buffer);
- }
- //=================================
- // Common routine
- //=================================
- char* getTimeString(void)
- {
- char *result=malloc(21);
- time_t timep;
- struct tm *p;
- time(&timep);
- p=gmtime(&timep);
- sprintf(result, "[%04d-%02d-%02d %02d:%02d:%02d]", (1900+p->tm_year), (1+p->tm_mon), p->tm_mday, p->tm_hour, p->tm_hour, p->tm_sec);
- return result;
- }
- void trim(char *s)
- {
- int i=0, j, k, l=0;
- while((s[i]==' ')||(s[i]=='\t')||(s[i]=='\n'))
- i++;
- j = strlen(s)-1;
- while((s[j]==' ')||(s[j]=='\t')||(s[j]=='\n'))
- j--;
- if(i==0 && j==strlen(s)-1) { }
- else if(i==0) s[j+1] = '\0';
- else {
- for(k=i; k<=j; k++) s[l++] = s[k];
- s[l] = '\0';
- }
- }
- int mystrcmp(char *p1,char *p2)
- {
- while(*p1==*p2)
- {
- if(*p1=='\0' || *p2=='\0')
- break;
- p1++;
- p2++;
- }
- if(*p1=='\0' && *p2=='\0')
- return(PASS);
- else
- return(FAIL);
- }
- void substr(char *dest, const char* src, unsigned int start, unsigned int cnt)
- {
- strncpy(dest, src + start, cnt);
- dest[cnt] = 0;
- }
- void split(char **arr, char *str, const char *del)
- {
- char *s = strtok(str, del);
- while(s != NULL)
- {
- *arr++ = s;
- s = strtok(NULL, del);
- }
- }
- //==========================================
- // Init all share memory
- //==========================================
- int InitShareMemory()
- {
- int result = PASS;
- int MeterSMId;
- //creat ShmSysConfigAndInfo
- if ((MeterSMId = shmget(ShmSysConfigAndInfoKey, sizeof(struct SysConfigAndInfo), 0777)) < 0)
- {
- #ifdef SystemLogMessage
- DEBUG_ERROR("shmget ShmSysConfigAndInfo NG\n");
- #endif
- result = FAIL;
- }
- else if ((ShmSysConfigAndInfo = shmat(MeterSMId, NULL, 0)) == (void *) -1)
- {
- #ifdef SystemLogMessage
- DEBUG_ERROR("shmat ShmSysConfigAndInfo NG\n");
- #endif
- result = FAIL;
- }
- //creat ShmStatusCodeData
- if ((MeterSMId = shmget(ShmStatusCodeKey, sizeof(struct StatusCodeData), 0777)) < 0)
- {
- #ifdef SystemLogMessage
- DEBUG_ERROR("shmget ShmStatusCodeData NG\n");
- #endif
- result = FAIL;
- }
- else if ((ShmStatusCodeData = shmat(MeterSMId, NULL, 0)) == (void *) -1)
- {
- #ifdef SystemLogMessage
- DEBUG_ERROR("shmat ShmStatusCodeData NG\n");
- #endif
- result = FAIL;
- }
- //creat ShmStatusCodeData
- if ((MeterSMId = shmget(ShmPrimaryMcuKey, sizeof(struct PrimaryMcuData), 0777)) < 0)
- {
- #ifdef SystemLogMessage
- DEBUG_ERROR("shmget ShmPrimaryMcuData NG\n");
- #endif
- result = FAIL;
- }
- else if ((ShmPrimaryMcuData = shmat(MeterSMId, NULL, 0)) == (void *) -1)
- {
- #ifdef ShmPrimaryMcuData
- DEBUG_ERROR("shmat ShmPrimaryMcuData NG\n");
- #endif
- result = FAIL;
- }
- if ((MeterSMId = shmget(ShmCsuMeterKey, sizeof(struct MeterInformation), IPC_CREAT | 0777)) < 0)
- {
- #ifdef SystemLogMessage
- DEBUG_ERROR("shmget ShmCsuMeterKey NG \n");
- #endif
- result = FAIL;
- }
- else if ((ShmCsuMeterData = shmat(MeterSMId, NULL, 0)) == (void *) -1)
- {
- #ifdef SystemLogMessage
- DEBUG_ERROR("shmat ShmCsuMeterData NG \n");
- #endif
- result = FAIL;
- }
- if ((MeterSMId = shmget(ShmCommonKey, sizeof(struct DcCommonInformation), IPC_CREAT | 0777)) < 0)
- {
- #ifdef SystemLogMessage
- DEBUG_ERROR("shmget ShmCommonKey NG \n");
- #endif
- result = FAIL;
- }
- else if ((ShmDcCommonData = shmat(MeterSMId, NULL, 0)) == (void *) -1)
- {
- #ifdef SystemLogMessage
- DEBUG_ERROR("shmat ShmCommonKey NG \n");
- #endif
- result = FAIL;
- }
- return result;
- }
- //================================================
- // Function
- //================================================
- void GetFwAndHwVersion()
- {
- if(Query_FW_Ver(Uart1Fd, Addr.IoExtend, &ver) == PASS)
- {
- //PRINTF_FUNC("s1 = %s \n", ver.Version_FW);
- strcpy((char *)ShmPrimaryMcuData->version, ver.Version_FW);
- strcpy((char *) ShmSysConfigAndInfo->SysInfo.CsuPrimFwRev, ver.Version_FW);
- }
- if (Query_HW_Ver(Uart1Fd, Addr.IoExtend, &ver) == PASS)
- {
- //PRINTF_FUNC("s2 = %s \n", ver.Version_HW);
- }
- }
- void GetInputGpioStatus()
- {
- if (Query_Gpio_Input(Uart1Fd, Addr.IoExtend, &gpio_in) == PASS)
- {
- if (ShmDcCommonData->_tiltSensorStep == _TILT_SENSOR_STEP_NONE)
- {
- if (_curDeviceStatus[_PRIMARY_CHECK_TAG_AC_CONTACT] != gpio_in.AC_Connector ||
- _curDeviceStatus[_PRIMARY_CHECK_TAG_AC_CONTACT] != ShmDcCommonData->psuKeepCommunication)
- {
- if (_reCheckCount[_PRIMARY_CHECK_TAG_AC_CONTACT] >= 3)
- {
- if (ShmPrimaryMcuData->InputDet.bits.EmergencyButton == NORMAL &&
- ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuFailureAlarm == NORMAL &&
- ShmStatusCodeData->AlarmCode.AlarmEvents.bits.PsuComminicationErrWithCSU == NORMAL &&
- ShmPrimaryMcuData->InputDet.bits.DoorOpen == NORMAL &&
- (ShmDcCommonData->psuKeepCommunication == YES ||
- gpio_in.AC_Connector == YES))
- {
- ShmSysConfigAndInfo->SysInfo.AcContactorStatus =
- ShmPrimaryMcuData->InputDet.bits.AcContactorDetec = YES;
- _curDeviceStatus[_PRIMARY_CHECK_TAG_AC_CONTACT] = YES;
- }
- else
- {
- ShmSysConfigAndInfo->SysInfo.AcContactorStatus =
- ShmPrimaryMcuData->InputDet.bits.AcContactorDetec = _acSwitch = NO;
- _curDeviceStatus[_PRIMARY_CHECK_TAG_AC_CONTACT] = NO;
- }
- }
- else
- _reCheckCount[_PRIMARY_CHECK_TAG_AC_CONTACT]++;
- }
- else
- _reCheckCount[_PRIMARY_CHECK_TAG_AC_CONTACT] = NO;
- }
- if (_reCheckCount[_PRIMARY_CHECK_TAG_AC_CONTACT] >= 3 &&
- ShmDcCommonData->psuKeepCommunication == YES &&
- gpio_in.AC_Connector == NO)
- {
- ShmStatusCodeData->AlarmCode.AlarmEvents.bits.AcContactStestFail = YES;
- }
- else
- {
- ShmStatusCodeData->AlarmCode.AlarmEvents.bits.AcContactStestFail = NO;
- }
- // -------------------------------------------------------------------
- if (_curDeviceStatus[_PRIMARY_CHECK_TAG_MAIN_BREAKER] != gpio_in.AC_MainBreaker)
- {
- if (_reCheckCount[_PRIMARY_CHECK_TAG_MAIN_BREAKER] >= 3)
- {
- _curDeviceStatus[_PRIMARY_CHECK_TAG_MAIN_BREAKER] = gpio_in.AC_MainBreaker;
- ShmPrimaryMcuData->InputDet.bits.AcMainBreakerDetec = gpio_in.AC_MainBreaker;
- }
- else
- _reCheckCount[_PRIMARY_CHECK_TAG_MAIN_BREAKER]++;
- }
- else
- _reCheckCount[_PRIMARY_CHECK_TAG_MAIN_BREAKER] = 0;
- ShmPrimaryMcuData->InputDet.bits.SpdDetec = gpio_in.SPD;
- ShmPrimaryMcuData->InputDet.bits.DoorOpen = gpio_in.Door_Open;
- ShmPrimaryMcuData->InputDet.bits.Button1 = gpio_in.Button[0];
- ShmPrimaryMcuData->InputDet.bits.Button2 = gpio_in.Button[1];
- ShmPrimaryMcuData->InputDet.bits.EmergencyButton = gpio_in.Emergency_Btn;
- //PRINTF_FUNC("left = %d \n", ShmPrimaryMcuData->InputDet.bits.Button1);
- //PRINTF_FUNC("right = %d \n", ShmPrimaryMcuData->InputDet.bits.Button2);
- //PRINTF_FUNC("ShmSysConfigAndInfo->SysInfo.AcContactorStatus = %d \n", ShmSysConfigAndInfo->SysInfo.AcContactorStatus);
- if (_reCheckCount[_PRIMARY_CHECK_TAG_MAIN_BREAKER] == 3 &&
- ShmPrimaryMcuData->InputDet.bits.AcMainBreakerDetec == YES)
- DEBUG_ERROR("AC Mainbreaker occur. \n");
- }
- }
- void GetMeterConsumption(byte index)
- {
- if (Query_Meter_value(Uart1Fd, Addr.IoExtend, &ShmCsuMeterData->_meter[index], &ShmCsuMeterData->isWorking, index) == PASS)
- {
- // meter value (Resolution) : 10
- if (!ShmCsuMeterData->isWorking)
- {
- //ShmStatusCodeData->AlarmCode.AlarmEvents.bits.MeterCommTimeout = YES;
- return;
- }
- if (ShmCsuMeterData->_meter[index].isCalculation == YES)
- {
- // printf("Meter%d, Comsumption = %.1f, curMeterValue = %.1f \n",
- // index,
- // ShmCsuMeterData->_meter[index].newMeterValue,
- // ShmCsuMeterData->_meter[index].curMeterValue);
- if (ShmCsuMeterData->_meter[index].curMeterValue == 0)
- ShmCsuMeterData->_meter[index].curMeterValue = ShmCsuMeterData->_meter[index].newMeterValue;
- else
- {
- // 最大充時,兩個都會累加,但只輸出給一槍使用
- ShmCsuMeterData->_meter[index]._chargingValue += ShmCsuMeterData->_meter[index].newMeterValue - ShmCsuMeterData->_meter[index].curMeterValue;
- if (ShmSysConfigAndInfo->SysInfo.MainChargingMode == _MAIN_CHARGING_MODE_NORMAL)
- {
- for (byte subIndex = 0; subIndex < gun_count; subIndex++)
- {
- if (_chargingData[subIndex]->SystemStatus == SYS_MODE_CHARGING)
- {
- ShmCsuMeterData->_meter[subIndex]._curTotalCharging += ShmCsuMeterData->_meter[index].newMeterValue - ShmCsuMeterData->_meter[index].curMeterValue;
- break;
- }
- }
- }
- else
- {
- ShmCsuMeterData->_meter[index]._curTotalCharging += ShmCsuMeterData->_meter[index].newMeterValue - ShmCsuMeterData->_meter[index].curMeterValue;
- }
- ShmCsuMeterData->_meter[index].curMeterValue = ShmCsuMeterData->_meter[index].newMeterValue;
- }
- }
- }
- }
- void SetOutputGpio(byte flash, byte tilt)
- {
- Gpio_out gpio;
- // 藍燈
- gpio.Button_LED[0] = flash;
- // 綠燈
- gpio.Button_LED[1] = flash;
- //gpio.Button_LED[1] = 0x00;
- gpio.System_LED[0] = 0x00;
- gpio.System_LED[1] = 0x00;
- gpio.System_LED[2] = tilt;
- gpio.System_LED[3] = tilt;
- gpio.AC_Connector = 0x00;
- gpio.AC_Breaker = 0x00;
- if (Config_Gpio_Output(Uart1Fd, Addr.IoExtend, &gpio) == PASS)
- {
- //PRINTF_FUNC("SetOutputGpio sucessfully. %d \n", flash);
- }
- else
- {
- //PRINTF_FUNC("SetOutputGpio fail. \n");
- }
- }
- void SetRtcData()
- {
- struct timeb csuTime;
- struct tm *tmCSU;
- ftime(&csuTime);
- tmCSU = localtime(&csuTime.time);
- // PRINTF_FUNC("Time : %04d-%02d-%02d %02d:%02d:%02d \n", tmCSU->tm_year + 1900,
- // tmCSU->tm_mon + 1, tmCSU->tm_mday, tmCSU->tm_hour, tmCSU->tm_min,
- // tmCSU->tm_sec);
- rtc.RtcData[0] = '0' + (tmCSU->tm_year + 1900) / 1000 % 10;
- rtc.RtcData[1] = '0' + (tmCSU->tm_year + 1900) / 100 % 10;
- rtc.RtcData[2] = '0' + (tmCSU->tm_year + 1900) / 10 % 10;
- rtc.RtcData[3] = '0' + (tmCSU->tm_year + 1900) / 1 % 10;
- rtc.RtcData[4] = '0' + (tmCSU->tm_mon + 1) / 10 % 10;
- rtc.RtcData[5] = '0' + (tmCSU->tm_mon + 1) / 1 % 10;
- rtc.RtcData[6] = '0' + (tmCSU->tm_mday) / 10 % 10;
- rtc.RtcData[7] = '0' + (tmCSU->tm_mday) / 1 % 10;
- rtc.RtcData[8] = '0' + (tmCSU->tm_hour) / 10 % 10;
- rtc.RtcData[9] = '0' + (tmCSU->tm_hour) / 1 % 10;
- rtc.RtcData[10] = '0' + (tmCSU->tm_min) / 10 % 10;
- rtc.RtcData[11] = '0' + (tmCSU->tm_min) / 1 % 10;
- rtc.RtcData[12] = '0' + (tmCSU->tm_sec) / 10 % 10;
- rtc.RtcData[13] = '0' + (tmCSU->tm_sec) / 1 % 10;
- if (Config_Rtc_Data(Uart1Fd, Addr.IoExtend, &rtc) == PASS)
- {
- //PRINTF_FUNC("SetRtc sucessfully. \n");
- }
- else
- {
- //PRINTF_FUNC("SetRtc fail. \n");
- }
- }
- void SetModelName()
- {
- if (Config_Model_Name(Uart1Fd, Addr.IoExtend, ShmSysConfigAndInfo->SysConfig.ModelName) == PASS)
- {}
- }
- //================================================
- // Main process
- //================================================
- int InitComPort()
- {
- int fd;
- struct termios tios;
- fd = open(priPortName, O_RDWR);
- if(fd<=0)
- {
- #ifdef SystemLogMessage
- DEBUG_ERROR("open 407 Communication port NG \n");
- #endif
- return -1;
- }
- ioctl (fd, TCGETS, &tios);
- tios.c_cflag = B115200| CS8 | CLOCAL | CREAD;
- tios.c_lflag = 0;
- tios.c_iflag = 0;
- tios.c_oflag = 0;
- tios.c_cc[VMIN]=0;
- tios.c_cc[VTIME]=(unsigned char)1;
- tios.c_lflag=0;
- tcflush(fd, TCIFLUSH);
- ioctl (fd, TCSETS, &tios);
- return fd;
- }
- int GetTimeoutValue(struct timespec *startTime)
- {
- struct timespec endTime;
- clock_gettime(CLOCK_MONOTONIC_COARSE, &endTime);
- return endTime.tv_sec - startTime->tv_sec;
- }
- void GetTimespecFunc(struct timespec *time)
- {
- clock_gettime(CLOCK_MONOTONIC_COARSE, time);
- }
- //================================================
- //================================================
- // CANBUS receive task
- //================================================
- //================================================
- bool FindChargingInfoData(byte target, struct ChargingInfoData **chargingData)
- {
- for (byte index = 0; index < CHAdeMO_QUANTITY; index++)
- {
- if (ShmSysConfigAndInfo->SysInfo.ChademoChargingData[index].Index == target)
- {
- chargingData[target] = &ShmSysConfigAndInfo->SysInfo.ChademoChargingData[index];
- return true;
- }
- }
- for (byte index = 0; index < CCS_QUANTITY; index++)
- {
- if (ShmSysConfigAndInfo->SysInfo.CcsChargingData[index].Index == target)
- {
- chargingData[target] = &ShmSysConfigAndInfo->SysInfo.CcsChargingData[index];
- return true;
- }
- }
- for (byte index = 0; index < GB_QUANTITY; index++)
- {
- if (ShmSysConfigAndInfo->SysInfo.GbChargingData[index].Index == target)
- {
- chargingData[target] = &ShmSysConfigAndInfo->SysInfo.GbChargingData[index];
- return true;
- }
- }
- return false;
- }
- void Initialization()
- {
- bool isPass = false;
- while(!isPass)
- {
- isPass = true;
- for (byte _index = 0; _index < gun_count; _index++)
- {
- if (!FindChargingInfoData(_index, &_chargingData[0]))
- {
- DEBUG_ERROR("EvComm (main) : FindChargingInfoData false \n");
- isPass = false;
- break;
- }
- }
- sleep(1);
- }
- }
- bool CheckCustomer()
- {
- bool result = false;
- char _buf[3] = {0};
- memcpy(_buf, &ShmSysConfigAndInfo->SysConfig.ModelName[12], 2);
- if (strcmp(_buf, "OL") == EQUAL)
- result = true;
- return result;
- }
- void TiltSensorSwitch(bool _switch)
- {
- if (tilt != _switch)
- tilt = _switch;
- SetOutputGpio(flash, tilt);
- }
- void ChkTiltSensor()
- {
- if (ShmPrimaryMcuData->InputDet.bits.AcContactorDetec == YES)
- {
- if (_acSwitch == NO)
- {
- if (ShmDcCommonData->_tiltSensorStep == _TILT_SENSOR_STEP_NONE)
- {
- PRINTF_FUNC("---- Tilt Sensor : _TILT_SENSOR_STEP_PIN_ON ---- \n");
- ShmDcCommonData->_tiltSensorStep = _TILT_SENSOR_STEP_PIN_ON;
- _acSwitch = YES;
- }
- }
- else
- {
- switch (ShmDcCommonData->_tiltSensorStep)
- {
- case _TILT_SENSOR_STEP_PIN_ON:
- {
- TiltSensorSwitch(YES);
- GetTimespecFunc(&_tilt_time);
- PRINTF_FUNC("---- Tilt Sensor : _TILT_SENSOR_STEP_PIN_WAIT ---- \n");
- ShmDcCommonData->_tiltSensorStep = _TILT_SENSOR_STEP_PIN_WAIT;
- }
- break;
- case _TILT_SENSOR_STEP_PIN_WAIT:
- {
- int _time = GetTimeoutValue(&_tilt_time);
- if (_time < 0)
- GetTimespecFunc(&_tilt_time);
- else if (_time > 1)
- {
- if (ShmPrimaryMcuData->InputDet.bits.DoorOpen == NO)
- ShmStatusCodeData->AlarmCode.AlarmEvents.bits.TiltSensorStestFail = YES;
- else
- ShmStatusCodeData->AlarmCode.AlarmEvents.bits.TiltSensorStestFail = NO;
- if (_time > TILTSENSORDET)
- {
- GetTimespecFunc(&_tilt_time);
- PRINTF_FUNC("---- Tilt Sensor : _TILT_SENSOR_STEP_PIN_OFF ---- \n");
- ShmDcCommonData->_tiltSensorStep = _TILT_SENSOR_STEP_PIN_OFF;
- }
- }
- }
- break;
- case _TILT_SENSOR_STEP_PIN_OFF:
- {
- TiltSensorSwitch(NO);
- GetTimespecFunc(&_tilt_time);
- PRINTF_FUNC("---- Tilt Sensor : _TILT_SENSOR_STEP_PIN_FINISH ---- \n");
- ShmDcCommonData->_tiltSensorStep = _TILT_SENSOR_STEP_PIN_FINISH;
- }
- break;
- case _TILT_SENSOR_STEP_PIN_FINISH :
- {
- int _time = GetTimeoutValue(&_tilt_time);
- if (_time < 0)
- GetTimespecFunc(&_tilt_time);
- else if (_time > TILTSENSORDET)
- {
- PRINTF_FUNC("---- Tilt Sensor : _TILT_SENSOR_STEP_NONE ---- \n");
- ShmDcCommonData->_tiltSensorStep = _TILT_SENSOR_STEP_NONE;
- }
- }
- break;
- }
- }
- }
- else
- {
- TiltSensorSwitch(NO);
- ShmDcCommonData->_tiltSensorStep = _TILT_SENSOR_STEP_NONE;
- _acSwitch = NO;
- }
- }
- int main(void)
- {
- if(InitShareMemory() == FAIL)
- {
- #ifdef SystemLogMessage
- DEBUG_ERROR("InitShareMemory NG\n");
- #endif
- if(ShmStatusCodeData!=NULL)
- {
- ShmStatusCodeData->AlarmCode.AlarmEvents.bits.FailToCreateShareMemory = 1;
- }
- sleep(5);
- return 0;
- }
- Uart1Fd = InitComPort();
- if(Uart1Fd < 0)
- {
- #ifdef SystemLogMessage
- DEBUG_ERROR("InitComPort (Uart1 : AM3352 - STM32) NG");
- #endif
- if (ShmStatusCodeData != NULL)
- {
- ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CsuInitFailed = 1;
- }
- sleep(5);
- return 0;
- }
- SetRtcData();
- SetModelName();
- GetTimespecFunc(&_flash_time);
- gun_count = ShmSysConfigAndInfo->SysConfig.TotalConnectorCount;
- Initialization();
- byte _count = 0;
- _acSwitch = NO;
- sleep(1);
- // 需要判斷這個的客戶碼 : OL
- _isNeedChkTilt = CheckCustomer();
- for(;;)
- {
- if (strcmp((char *)ShmSysConfigAndInfo->SysInfo.LcmHwRev, " ") == EQUAL)
- {
- if (GetTimeoutValue(&_flash_time) > 1)
- {
- if (flash == NO)
- flash = YES;
- else
- flash = NO;
- SetOutputGpio(flash, tilt);
- GetTimespecFunc(&_flash_time);
- }
- }
- else
- {
- if (GetTimeoutValue(&_flash_time) > 5)
- {
- if (flash == NO)
- flash = YES;
- SetOutputGpio(flash, tilt);
- GetTimespecFunc(&_flash_time);
- }
- }
- // 程序開始之前~ 必須先確定 FW 版本與硬體版本,確認後!!~ 該模組才算是真正的 Initial Comp.
- // 模組更新 FW 後,需重新做
- if(ShmPrimaryMcuData->SelfTest_Comp != PASS)
- {
- //PRINTF_FUNC("(407) Get Fw and Hw Ver. \n");
- GetFwAndHwVersion();
- sleep(1);
- ShmPrimaryMcuData->SelfTest_Comp = PASS;
- }
- else
- {
- GetInputGpioStatus();
- if(ShmSysConfigAndInfo->SysConfig.ModelName[3] == 'M' ||
- ShmSysConfigAndInfo->SysConfig.ModelName[3] == 'L')
- {
- GetMeterConsumption(_count);
- _count++;
- if (_count >= gun_count)
- _count = 0;
- }
- // if (_isNeedChkTilt)
- // {
- // ChkTiltSensor();
- // }
- }
- usleep(100000);
- }
- return FAIL;
- }
|