/* * Main.c * * Created on: 2019年8月6日 * Author: 7564 */ #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include /*標準輸入輸出定義*/ #include /*標準函數庫定義*/ #include /*Unix 標準函數定義*/ #include /*檔控制定義*/ #include /*PPSIX 終端控制定義*/ #include /*錯誤號定義*/ #include #include #include #include #include #include #include #include "../../define.h" typedef unsigned char byte; #define PASS 1 #define FAIL -1 #define ARRAY_SIZE(A) (sizeof(A) / sizeof(A[0])) struct SysConfigAndInfo *ShmSysConfigAndInfo; struct StatusCodeData *ShmStatusCodeData; struct PrimaryMcuData *ShmPrimaryMcuData; struct CHAdeMOData *ShmCHAdeMOData; struct CcsData *ShmCcsData; struct FanModuleData *ShmFanModuleData; struct RelayModuleData *ShmRelayModuleData; struct ChargingInfoData *_chargingData[CHAdeMO_QUANTITY + CCS_QUANTITY + GB_QUANTITY]; char *msg = "state : get gun state (index) \n" "card : scanning card (x) : \n" "gun : get gun plugit state (index) \n" "lock : get gun locked state (index) \n" "self : self test state (x) \n" "ver : ver of board (407 or index or rb or fan) \n" "ac : get ac relay state (x) \n"; 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; } int InitShareMemory() { int result = PASS; int MeterSMId; //initial ShmSysConfigAndInfo if ((MeterSMId = shmget(ShmSysConfigAndInfoKey, sizeof(struct SysConfigAndInfo), 0777)) < 0) { result = FAIL; } else if ((ShmSysConfigAndInfo = shmat(MeterSMId, NULL, 0)) == (void *) -1) { result = FAIL; } else {} //initial ShmStatusCodeData if ((MeterSMId = shmget(ShmStatusCodeKey, sizeof(struct StatusCodeData), 0777)) < 0) { result = FAIL; } else if ((ShmStatusCodeData = shmat(MeterSMId, NULL, 0)) == (void *) -1) { result = FAIL; } else {} if(CHAdeMO_QUANTITY > 0) { if ((MeterSMId = shmget(ShmCHAdeMOCommKey, sizeof(struct CHAdeMOData), IPC_CREAT | 0777)) < 0) { result = FAIL; } else if ((ShmCHAdeMOData = shmat(MeterSMId, NULL, 0)) == (void *) -1) { result = FAIL; } else { } } if(CCS_QUANTITY > 0) { if ((MeterSMId = shmget(ShmCcsCommKey, sizeof(struct CcsData), IPC_CREAT | 0777)) < 0) { result = FAIL; } else if ((ShmCcsData = shmat(MeterSMId, NULL, 0)) == (void *) -1) { result = FAIL; } else { } } if ((MeterSMId = shmget(ShmPrimaryMcuKey, sizeof(struct PrimaryMcuData), IPC_CREAT | 0777)) < 0) { result = FAIL; } else if ((ShmPrimaryMcuData = shmat(MeterSMId, NULL, 0)) == (void *) -1) { result = FAIL; } if ((MeterSMId = shmget(ShmFanBdKey, sizeof(struct FanModuleData), IPC_CREAT | 0777)) < 0) { result = FAIL; } else if ((ShmFanModuleData = shmat(MeterSMId, NULL, 0)) == (void *) -1) { result = FAIL; } if ((MeterSMId = shmget(ShmRelayBdKey, sizeof(struct RelayModuleData), IPC_CREAT | 0777)) < 0) { result = FAIL; } else if ((ShmRelayModuleData = shmat(MeterSMId, NULL, 0)) == (void *) -1) { result = FAIL; } return result; } void RunStatusProc(char *v1, char *v2) { int _index = atoi(v1); if (!FindChargingInfoData(_index, &_chargingData[0])) { printf ("FindChargingInfoData error\n"); return; } if (strcmp(v2, "-1") == 0 || strcmp(v2, "") == 0) { // get printf ("index = %x, status = %x\n", _index, _chargingData[_index]->SystemStatus); } else { // set _chargingData[_index]->SystemStatus = atoi(v2); } } void RunCardProc(char *v1, char *v2) { if (ShmSysConfigAndInfo->SysInfo.WaitForPlugit) { ShmSysConfigAndInfo->SysInfo.WaitForPlugit = 0x00; printf ("SysInfo.WaitForPlugit = %x \n", ShmSysConfigAndInfo->SysInfo.WaitForPlugit); } else { ShmSysConfigAndInfo->SysInfo.WaitForPlugit = 0x01; printf ("SysInfo.WaitForPlugit = %x \n", ShmSysConfigAndInfo->SysInfo.WaitForPlugit); } } void RunGunPlugitProc(char *v1, char *v2) { int _index = atoi(v1); if (!FindChargingInfoData(_index, &_chargingData[0])) { printf("FindChargingInfoData error\n"); return; } if (strcmp(v2, "-1") == 0 || strcmp(v2, "") == 0) { // get printf("index = %x, plug it = %x\n", _index, _chargingData[_index]->ConnectorPlugIn); } else { // set _chargingData[_index]->ConnectorPlugIn = atoi(v2); } } void GetGunLockStatusProc(char *v1, char *v2) { if (strcmp(v1, "0") == 0) { printf("Gun Locked Status = %x \n", _chargingData[0]->GunLocked); } if (strcmp(v1, "1") == 0) { printf("Gun Locked Status = %x \n", _chargingData[1]->GunLocked); } } void RunSelfProc() { printf("self test status = %x\n", ShmSysConfigAndInfo->SysInfo.SelfTestSeq); } void GetFwVerProc(char *v1) { if (strcmp(v1, "407") == 0) { printf("407 FW Version = %s \n", ShmPrimaryMcuData->version); } else if (strcmp(v1, "0") == 0) { printf("Ev board 1 FW Version = %s \n", ShmCHAdeMOData->evse[0].version); printf("Ev board 1 FW Version = %s \n", ShmCcsData->V2GMessage_DIN70121->version); } else if (strcmp(v1, "1") == 0) { printf("Ev board 2 FW Version = %s \n", ShmCHAdeMOData->evse[1].version); printf("Ev board 2 FW Version = %s \n", ShmCcsData->V2GMessage_DIN70121->version); } else if (strcmp(v1, "rb") == 0) { printf("RB Version = %s \n", ShmSysConfigAndInfo->SysInfo.RelayModuleFwRev); } else if (strcmp(v1, "fan") == 0) { printf("FAN Version = %s \n", ShmSysConfigAndInfo->SysInfo.FanModuleFwRev); } } void FwUpdateFlagProc() { ShmSysConfigAndInfo->SysInfo.FirmwareUpdate = 0x01; } void CheckAcStatus(char *v1) { if (strcmp(v1, "-1") == 0|| strcmp(v1, "") == 0) { printf("AC Status = %d \n", ShmSysConfigAndInfo->SysInfo.AcContactorStatus); } } void SetCableChkStatus(char *v1, char *v2) { int _index = atoi(v1); if (!FindChargingInfoData(_index, &_chargingData[0])) { printf ("FindChargingInfoData error\n"); return; } _chargingData[_index]->GroundFaultStatus = atoi(v2); } int main(void) { if(InitShareMemory() == FAIL) { printf ("InitShareMemory = FAIL \n"); if(ShmStatusCodeData != NULL) { ShmStatusCodeData->AlarmCode.AlarmEvents.bits.FailToCreateShareMemory=1; } sleep(5); return 0; } for(;;) { char word[128]; char newString[3][10]; int i,j,ctr; fgets(word, sizeof(word), stdin); j=0; ctr=0; strcpy(newString[1], "-1"); strcpy(newString[2], "-1"); for (i = 0; i <= (strlen(word)); i++) { if (word[i] == ' ' || word[i] == '\0' || word[i] == 10) { newString[ctr][j] = '\0'; ctr++; j = 0; } else { newString[ctr][j] = word[i]; j++; } } if(strcmp(newString[0], "state") == 0) { if (strcmp(newString[1], "-1") == 0 || strcmp(newString[1], "") == 0) continue; // 槍狀態 RunStatusProc(newString[1], newString[2]); continue; } else if(strcmp(newString[0], "card") == 0) { // 刷卡狀態 RunCardProc(newString[1], newString[2]); continue; } else if(strcmp(newString[0], "gun") == 0) { if (strcmp(newString[1], "-1") == 0 || strcmp(newString[1], "") == 0) continue; // 插槍狀態 RunGunPlugitProc(newString[1], newString[2]); continue; } else if(strcmp(newString[0], "lock") == 0) { if (strcmp(newString[1], "-1") == 0 || strcmp(newString[1], "") == 0) continue; // 插槍狀態 GetGunLockStatusProc(newString[1], newString[2]); continue; } else if(strcmp(newString[0], "self") == 0) { // CSU 自我檢測狀態 RunSelfProc(newString[1]); continue; } else if(strcmp(newString[0], "ver") == 0) { if (strcmp(newString[1], "-1") == 0 || strcmp(newString[1], "") == 0) continue; // 取 FW 版號 GetFwVerProc(newString[1]); continue; } else if (strcmp(newString[0], "update") == 0) { // 更新 FwUpdateFlagProc(newString[1]); continue; } else if (strcmp(newString[0], "ac") == 0) { // AC contactor 狀態 CheckAcStatus(newString[1]); continue; } else if (strcmp(newString[0], "cable") == 0) { if (strcmp(newString[1], "-1") == 0 || strcmp(newString[1], "") == 0) continue; // cable check pass SetCableChkStatus(newString[1], newString[2]); continue; } printf ("%s\n", msg); usleep(100000); } return 0; }