DoComm.c 80 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <stdint.h>
  4. #include <stdarg.h>
  5. #include <string.h>
  6. #include <stdbool.h>
  7. #include <sys/time.h>
  8. #include <sys/timeb.h>
  9. #include <sys/socket.h>
  10. #include <sys/types.h>
  11. #include <sys/ipc.h>
  12. #include <sys/shm.h>
  13. #include <arpa/inet.h>
  14. #include <netinet/in.h>
  15. #include <assert.h>
  16. #include <time.h>
  17. #include <errno.h>
  18. #include <unistd.h>
  19. #include <signal.h>
  20. #include "../Log/log.h"
  21. #include "../ShareMemory/shmMem.h"
  22. #include "../Define/define.h"
  23. #include "../Config.h"
  24. #include "../SelectGun/SelectGun.h"
  25. #include "../timeout.h"
  26. #include "DoComm.h"
  27. //------------------------------------------------------------------------------
  28. static DoCommGblData gDoCommGblData = {0};
  29. static struct SysConfigData *pSysConfig = NULL;
  30. static struct SysInfoData *pSysInfo = NULL;
  31. static struct WARNING_CODE_INFO *pSysWarning = NULL;
  32. static struct AlarmCodeData *pAlarmCode = NULL;
  33. static struct PsuData *ShmPsuData = NULL;
  34. static struct OCPP16Data *ShmOCPP16Data = NULL;
  35. static struct PrimaryMcuData *ShmPrimaryMcuData = NULL;
  36. static SelectGunInfo *ShmSelectGunInfo = NULL;
  37. static DcCommonInfo *ShmDcCommonData = NULL;
  38. //static struct ChargingInfoData *ChargingData[CHAdeMO_QUANTITY + CCS_QUANTITY + GB_QUANTITY]
  39. static struct ChargingInfoData *pDcChargingInfo = NULL;
  40. static struct timeb gRegTimeUp[2][MAX_REGISTER_NUM] = {0};
  41. static struct WARNING_CODE_INFO gPreSysWarningInfo = {0};
  42. // Hexdump
  43. char old_Hexdump[10240];
  44. uint8_t psu_num= 0;
  45. //------------------------------------------------------------------------------
  46. static void removeFaultCodeToBuf(uint8_t *Code);
  47. static void addFaultCodeToBuf(uint8_t *Code);
  48. static int readMiscCommand(int fd, uint8_t id);
  49. static int writeCsuModuleVersion(int fd);
  50. static int writeGroundFaultDetection(int fd, uint8_t status, uint8_t id);
  51. //------------------------------------------------------------------------------
  52. //--- Common function ---
  53. //------------------------------------------------------------------------------
  54. void GetClockTime(struct timespec *_now_time, void *null)
  55. {
  56. clock_gettime(CLOCK_MONOTONIC, _now_time);
  57. }
  58. static int DiffTimeb(struct timeb ST, struct timeb ET)
  59. {
  60. //return milli-second
  61. unsigned int StartTime, StopTime;
  62. StartTime = (unsigned int)ST.time;
  63. StopTime = (unsigned int)ET.time;
  64. return (StopTime - StartTime) * 1000 + ET.millitm - ST.millitm;
  65. }
  66. /**
  67. * [hexdump : check data]
  68. * @Author Jerry
  69. * @DateTime 2018-12-21
  70. * @param p [description]
  71. * @param size [description]
  72. */
  73. static void Hexdump(const void *p, size_t size)
  74. {
  75. const uint8_t *c = p;
  76. char message[10240] = {0};
  77. uint32_t i = 0;
  78. uint32_t message_len = 0;
  79. assert(p);
  80. //printf("Dumping %u bytes from %p:\n", size, p);
  81. message_len += sprintf(&message[message_len], "\nDumping %u bytes from %p:\n", (unsigned int)size, p);
  82. while (size > 0) {
  83. for (i = 0; i < 16; i++) {
  84. if (i < size) {
  85. //printf("%02x ", c[i]);
  86. message_len += sprintf(&message[message_len], "%02x ", c[i]);
  87. } else {
  88. //printf(" ");
  89. message_len += sprintf(&message[message_len], " ");
  90. }
  91. }
  92. for (i = 0; i < 16; i++) {
  93. if (i < size) {
  94. //printf("%c", c[i] >= 32 && c[i] < 127 ? c[i] : '.');
  95. message_len += sprintf(&message[message_len], "%c", c[i] >= 32 && c[i] < 127 ? c[i] : '.');
  96. } else {
  97. //printf(" ");
  98. message_len += sprintf(&message[message_len], " ");
  99. }
  100. }
  101. //printf("\n");
  102. message_len += sprintf(&message[message_len], "\n");
  103. c += 16;
  104. if (size <= 16) {
  105. break;
  106. }
  107. size -= 16;
  108. }
  109. //message_len += sprintf(&message[message_len], "\n");
  110. if( strcmp(old_Hexdump,message) != EQUAL ) {
  111. log_info("%s", message);
  112. strcpy(old_Hexdump,message);
  113. }
  114. }
  115. static int string2ByteArray(char *input, uint8_t *output)
  116. {
  117. int loop = 0;
  118. int i = 0;
  119. while (input[loop] != '\0') {
  120. output[i++] = input[loop++];
  121. }
  122. output[loop] = '\0';
  123. return loop + 1;
  124. }
  125. static void unixSocketSigPipeHandle(int sig)
  126. {
  127. log_error("socket packet error %x", sig);
  128. }
  129. static void InitSocketSigPipe(void)
  130. {
  131. struct sigaction action;
  132. action.sa_handler = unixSocketSigPipeHandle;
  133. sigemptyset(&action.sa_mask);
  134. action.sa_flags = 0;
  135. sigaction(SIGPIPE, &action, NULL);
  136. }
  137. //------------------------------------------------------------------------------
  138. static void setTcpStatus(uint8_t setValue)
  139. {
  140. pAlarmCode->AlarmEvents.bits.DisconnectedFromDo = setValue;
  141. }
  142. //------------------------------------------------------------------------------
  143. //--- TCP socket function ---
  144. //------------------------------------------------------------------------------
  145. static int sendTcpSocket(int fd, uint8_t *data, uint16_t dataLen)
  146. {
  147. int size = -1;
  148. size = send(fd, data, dataLen , 0);
  149. if ((size < 0) || (errno == EAGAIN)) {
  150. if (size < 0) {
  151. log_error("Send Socket Size = %d, EAGAIN error %d:%s", size, errno, strerror(errno));
  152. }
  153. }
  154. return size;
  155. }
  156. static int recvTcpSocket(int fd, uint8_t *data, uint16_t dataLen)
  157. {
  158. int size = -1;
  159. uint8_t *pdata = (uint8_t *)data;
  160. size = recv(fd, pdata, dataLen, MSG_WAITALL);
  161. if ((errno == EAGAIN) || (size < 0)) {
  162. log_error("Receive Socket Size = %d, EAGAIN error %d:%s", size, errno, strerror(errno));
  163. }
  164. return size;
  165. }
  166. static int getSO_ERROR(int fd)
  167. {
  168. int err = 1;
  169. socklen_t len = sizeof err;
  170. if (-1 == getsockopt(fd, SOL_SOCKET, SO_ERROR, (char *)&err, &len)) {
  171. log_error("getSO_ERROR");
  172. }
  173. if (err) {
  174. errno = err; // set errno to the socket SO_ERROR
  175. }
  176. return err;
  177. }
  178. static void closeSocket(int fd) // *not* the Windows closesocket()
  179. {
  180. if (fd < 0) {
  181. return;
  182. }
  183. getSO_ERROR(fd); // first clear any errors, which can cause close to fail
  184. if (shutdown(fd, SHUT_RDWR) < 0) { // secondly, terminate the 'reliable' delivery
  185. if (errno != ENOTCONN && errno != EINVAL) { // SGI causes EINVAL
  186. log_info("shutdown");
  187. }
  188. }
  189. if (close(fd) < 0) { // finally call close()
  190. log_info("client socket close");
  191. }
  192. }
  193. static int doCommConnToServer(void)
  194. {
  195. struct sockaddr_in dest;
  196. struct timeval tv;
  197. int flag;
  198. int TcpSock = 0;
  199. //if (TcpSock > 0) {
  200. // close(TcpSock);
  201. //}
  202. if ((TcpSock = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
  203. log_error("Open TCP socket NG");
  204. return -1;
  205. }
  206. //flag = fcntl (TcpSock, F_GETFL, 0);
  207. //if (flag >= 0) {
  208. // flag |= O_NONBLOCK;
  209. // fcntl(TcpSock, F_SETFL, flag );
  210. //}
  211. tv.tv_sec = 3;
  212. tv.tv_usec = 0;
  213. setsockopt(TcpSock, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(struct timeval)); //設定等待3s
  214. setsockopt(TcpSock, SOL_SOCKET, SO_SNDTIMEO, &tv, sizeof(struct timeval)); //設定傳送3s
  215. flag = 1;
  216. setsockopt(TcpSock, SOL_SOCKET, MSG_NOSIGNAL, &flag, sizeof(flag));
  217. memset(&dest, 0, sizeof(dest));
  218. dest.sin_family = AF_INET;
  219. dest.sin_port = htons(DoTcpPort);
  220. inet_aton(DoIPAddress, (struct in_addr *) &dest.sin_addr.s_addr);
  221. if (connect(TcpSock, (struct sockaddr *) &dest, sizeof(dest)) != 0) {
  222. close(TcpSock);
  223. return -1;
  224. }
  225. return TcpSock;
  226. }
  227. //------------------------------------------------------------------------------
  228. //--- Audi select gun ---
  229. //------------------------------------------------------------------------------
  230. static void clearPricesInfo(uint8_t id)
  231. {
  232. memset(&ShmSelectGunInfo->PricesInfo[id], 0, sizeof(PricesInfo));
  233. ShmSelectGunInfo->PricesInfo[id].Balance = FAIL_BALANCE_PRICES;
  234. }
  235. static void ClearAuthorizedFlag(void)
  236. {
  237. ShmOCPP16Data->SpMsg.bits.AuthorizeConf = NO;
  238. pSysInfo->AuthorizeFlag = NO;
  239. }
  240. static void ClearDetectPluginFlag(void)
  241. {
  242. pSysInfo->WaitForPlugit = NO;
  243. }
  244. static bool isDetectPlugin(void)
  245. {
  246. if (pSysInfo->WaitForPlugit == YES) {
  247. return YES;
  248. }
  249. return NO;
  250. }
  251. static void destroySelectGun(uint8_t curGun)
  252. {
  253. uint8_t i = 0;
  254. uint8_t totalGun = pSysConfig->TotalConnectorCount;
  255. if (curGun == DESTROY_ALL_SEL) {
  256. ShmSelectGunInfo->SelGunInfo.RightGun = SEL_GUN_RELEASE;
  257. ShmSelectGunInfo->SelGunInfo.LeftGun = SEL_GUN_RELEASE;
  258. for (i = 0; i < totalGun; i++) {
  259. pDcChargingInfo = (struct ChargingInfoData *)GetDcChargingInfoData(i);
  260. pDcChargingInfo->TimeoutFlag = Timeout_None;
  261. clearPricesInfo(i);
  262. }
  263. pSysInfo->CurGunSelected = 0;
  264. strcpy((char *)pSysConfig->UserId, "");
  265. }
  266. //for charging timeout or complete
  267. if ((curGun == LEFT_GUN_NUM) && (ShmSelectGunInfo->SelGunInfo.LeftGun != SEL_GUN_RELEASE)) {
  268. ShmSelectGunInfo->SelGunInfo.LeftGun = SEL_GUN_RELEASE;
  269. if (ShmOCPP16Data->SpMsg.bits.AuthorizeConf != NO) {
  270. ClearAuthorizedFlag();
  271. }
  272. clearPricesInfo(curGun);
  273. }
  274. if ((curGun == RIGHT_GUN_NUM) && (ShmSelectGunInfo->SelGunInfo.RightGun != SEL_GUN_RELEASE)) {
  275. if (ShmOCPP16Data->SpMsg.bits.AuthorizeConf != NO) {
  276. ClearAuthorizedFlag();
  277. }
  278. clearPricesInfo(curGun);
  279. }
  280. }
  281. static int getConfirmSelectedGun(uint8_t curSel)
  282. {
  283. if (((curSel == LEFT_GUN_NUM) && (ShmSelectGunInfo->SelGunInfo.LeftGun >= SEL_GUN_CONFIRM)) ||
  284. ((curSel == RIGHT_GUN_NUM) && (ShmSelectGunInfo->SelGunInfo.RightGun >= SEL_GUN_CONFIRM))) {
  285. return PASS;
  286. }
  287. return FAIL;
  288. }
  289. static int getSelGunWaitToAuthor(uint8_t curGun)
  290. {
  291. if (curGun == LEFT_GUN_NUM && ShmSelectGunInfo->SelGunInfo.LeftGun == SEL_GUN_ATHOR) {
  292. return FAIL;
  293. }
  294. if (curGun == RIGHT_GUN_NUM && ShmSelectGunInfo->SelGunInfo.RightGun == SEL_GUN_ATHOR) {
  295. return FAIL;
  296. }
  297. return PASS;
  298. }
  299. static void setConfirmSelGun(uint8_t selGun)
  300. {
  301. if (selGun == LEFT_GUN_NUM && ShmSelectGunInfo->SelGunInfo.LeftGun == SEL_GUN_RELEASE) {
  302. ShmSelectGunInfo->SelGunInfo.LeftGun = SEL_GUN_CONFIRM;
  303. //printf("confirmSelGun left");
  304. } else if (selGun == RIGHT_GUN_NUM && ShmSelectGunInfo->SelGunInfo.RightGun == SEL_GUN_RELEASE) {
  305. ShmSelectGunInfo->SelGunInfo.RightGun = SEL_GUN_CONFIRM;
  306. //printf("confirmSelGun right");
  307. }
  308. }
  309. //------------------------------------------------------------------------------
  310. //--- DoComm function ---
  311. //------------------------------------------------------------------------------
  312. static int compareOpcode(uint8_t opCode)
  313. {
  314. if (opCode != OP_WAIT_RESPONSE) {
  315. //log_error("response operative code fail");
  316. return FAIL;
  317. }
  318. return PASS;
  319. }
  320. static int compareResult(uint8_t result)
  321. {
  322. if (result != COMMAND_RESULT_OK) {
  323. //log_error("response result fail");
  324. return FAIL;
  325. }
  326. return PASS;
  327. }
  328. static int compareRegister(uint8_t srcReg, uint8_t destReg)
  329. {
  330. if (srcReg != destReg) {
  331. //log_error("response register fail");
  332. return FAIL;
  333. }
  334. return PASS;
  335. }
  336. static float transPricesUnit(int prices)
  337. {
  338. //printf("prices = %.2f", prices * PRICES_UNIT);
  339. return (prices * PRICES_UNIT);
  340. }
  341. static void clearMiscCommand(void)
  342. {
  343. gDoCommGblData.MiscCmd = 0;
  344. }
  345. static int qrCodeUrlInfoHandle(uint8_t *data)
  346. {
  347. //int len = 0;
  348. //char cmdBuf[128] = {0};
  349. char localTime[128] = {0};
  350. uint16_t timeLen = 0;
  351. struct timeb SeqEndTime;
  352. struct tm *tm;
  353. if ((char *)&data[0] == '\0') {
  354. log_error("QR code date error");
  355. return FAIL;
  356. }
  357. //get local system time
  358. ftime(&SeqEndTime);
  359. SeqEndTime.time = time(NULL);
  360. tm = localtime(&SeqEndTime.time);
  361. timeLen = sprintf(localTime, "%04d-%02d-%02d %02d:%02d",
  362. tm->tm_year + 1900,
  363. tm->tm_mon + 1,
  364. tm->tm_mday,
  365. tm->tm_hour,
  366. tm->tm_min);
  367. //copy QR code string
  368. if (strncmp((char *)localTime, (char *)&data[0], timeLen - 2) != 0) {
  369. memset(pSysConfig->SystemId, '\0', sizeof(pSysConfig->SystemId));
  370. //printf("data = %s", data);
  371. //len =
  372. string2ByteArray((char *)data, (uint8_t *)pSysConfig->SystemId);
  373. //printf("SystemId = %s", pSysConfig->SystemId);
  374. }
  375. //if ((char *)&data[len] == '\0') {
  376. // log_error("power cabinet system date error");
  377. // return FAIL;
  378. //}
  379. //set system date
  380. //if (strncmp((char *)localTime, (char *)&data[len], timeLen) != 0) {
  381. // sprintf(cmdBuf, "date -s \"%s\" >> /dev/null", (char *)&data[len]);
  382. // system(cmdBuf);
  383. // log_info("local time = %s, data time = %s", localTime, (char *)&data[len]);
  384. // ShmOCPP16Data->MsMsg.bits.ResetReq = YES;
  385. // strcpy((char *)ShmOCPP16Data->Reset.Type, "Soft");
  386. // sleep(3);
  387. // //gDoCommGblData.DisConnCount = CHECK_NETWORK_FAIL_COUNT;
  388. //}
  389. return PASS;
  390. }
  391. static int updateFirmwareHandle(uint8_t *imgName)
  392. {
  393. int ret = PASS;
  394. char cmdBuf[1024] = {0};
  395. sprintf(cmdBuf, "/mnt/%s", imgName);
  396. log_info("Program ready to check file %s", cmdBuf);
  397. if ( access(cmdBuf, F_OK) != -1) {
  398. log_info("File '%s' exist.", cmdBuf);
  399. pSysInfo->FirmwareUpdate = YES;
  400. } else {
  401. log_info("File '%s' doesn't exist.", cmdBuf);
  402. ret = FAIL;
  403. }
  404. #if 0
  405. char cmdBuf[1024] = {0};
  406. int status = 0;
  407. system("touch ./tftpUpdate.sh"); //創建shell
  408. sprintf(cmdBuf, "echo tftp -gr %s -l %s/%s %s; > ./tftpUpdate.sh",
  409. imgName,
  410. IMAGE_FILE_PATH,
  411. imgName,
  412. DoIPAddress);
  413. system("chmod +x ./tftpUpdate.sh"); //修改權限
  414. status = system("sh tftpUpdate.sh"); //執行shell
  415. if (-1 == status) {
  416. printf("system error!");
  417. } else {
  418. printf("exit status value = [0x%x]", status);
  419. if (WIFEXITED(status)) {
  420. if (0 == WEXITSTATUS(status)) {
  421. printf("run shell script successfully.");
  422. pSysInfo->FirmwareUpdate = YES;
  423. } else {
  424. printf("run shell script fail, script exit code: %d", WEXITSTATUS(status));
  425. }
  426. } else {
  427. printf("exit status = [%d]", WEXITSTATUS(status));
  428. }
  429. }
  430. system("rm -rf ./tftpUpdate.sh"); //刪除shell
  431. #endif //0
  432. return ret;
  433. }
  434. static int miscCommandHandle(uint8_t dataLen, uint8_t plugNum, uint8_t *data)
  435. {
  436. int ret = PASS;
  437. uint8_t i = 0, j = 0;
  438. uint8_t cmdCount = (dataLen / 6);
  439. uint16_t cmd = 0;
  440. uint32_t value = 0;
  441. float prices = 0;
  442. MiscCommand *pMiscCmd = NULL;
  443. if (cmdCount < 1) {
  444. gDoCommGblData.MiscCmd = 0;
  445. //printf("cmdCount fail = %d, data len = %d", cmdCount, dataLen);
  446. return FAIL;
  447. }
  448. for (i = 0; i < cmdCount; i++) {
  449. pMiscCmd = (MiscCommand *)data + i;
  450. cmd = ntohs(pMiscCmd->CMD);
  451. value = ntohl(*((uint32_t *)&pMiscCmd->Value[0]));
  452. //log_info("misc command = %x, value = %d", cmd, value);
  453. switch (cmd) {
  454. //--- Execute parameter ---
  455. case MISC_CMD_CONNECOTOR_TIMEOUT:
  456. ShmSelectGunInfo->RemoteSetup.ConnectionTimeout = value;
  457. log_info("connection timeout = %d", ShmSelectGunInfo->RemoteSetup.ConnectionTimeout);
  458. clearMiscCommand();
  459. break;
  460. case MISC_CMD_OPERATIVE_STATE:
  461. if ((value > YES) || (value < NO)) {
  462. if (plugNum == 1) {
  463. clearMiscCommand();
  464. }
  465. break;
  466. }
  467. log_info("change availability plugNum = %d, value = %d", plugNum, value);
  468. ShmOCPP16Data->CsMsg.bits[plugNum].ChangeAvailabilityReq = YES;
  469. if (value == YES) {
  470. strcpy((char *)ShmOCPP16Data->ChangeAvailability[plugNum].Type, "Operative");
  471. } else if (value == NO) {
  472. strcpy((char *)ShmOCPP16Data->ChangeAvailability[plugNum].Type, "Inoperative");
  473. }
  474. if (plugNum == 1) {
  475. clearMiscCommand();
  476. }
  477. break;
  478. case MISC_CMD_DEFAULT_PRICES:
  479. prices = transPricesUnit((int)value);
  480. log_info("default prices = %.2f", prices);
  481. //pSysConfig->BillingData.isBilling = YES;
  482. for (j = 0; j < 24; j++) {
  483. pSysConfig->BillingData.Fee[j] = prices;
  484. }
  485. clearMiscCommand();
  486. break;
  487. case MISC_CMD_DEFAULT_CURRENCY:
  488. if (value < 0) {
  489. clearMiscCommand();
  490. return FAIL;
  491. }
  492. //log_info("default currency = %s%c", (uint8_t *)Currency[value]);
  493. log_info("default currency = %s", (uint8_t *)GetCurrency(value));
  494. pSysConfig->BillingData.Currency = value;
  495. clearMiscCommand();
  496. break;
  497. case MISC_CMD_ACCOUNT_BALANCE:
  498. if (pSysInfo->CurGunSelected != (plugNum)) {
  499. clearMiscCommand();
  500. break;
  501. }
  502. #if defined DD360Audi
  503. if (getConfirmSelectedGun(plugNum) == FAIL) {
  504. log_info("Remote start not select gun");
  505. break;
  506. }
  507. #endif //DD360Audi
  508. ShmSelectGunInfo->PricesInfo[plugNum].Balance = transPricesUnit((int)value);
  509. log_info("%d misc balance = %.2f", plugNum, ShmSelectGunInfo->PricesInfo[plugNum].Balance);
  510. clearMiscCommand();
  511. break;
  512. case MISC_CMD_BACKEND_STATUS :
  513. ShmSelectGunInfo->EthDevStatus.Backend = value;
  514. break;
  515. case MISC_CMD_ETHERNET_STATUS :
  516. ShmSelectGunInfo->EthDevStatus.Ethernet = value;
  517. break;
  518. case MISC_CMD_WIFI_STATUS :
  519. ShmSelectGunInfo->EthDevStatus.Wifi = value;
  520. break;
  521. case MISC_CMD_4G_STATUS :
  522. ShmSelectGunInfo->EthDevStatus.FourG = value;
  523. break;
  524. case MISC_CMD_BILLING_INFO:
  525. pSysConfig->BillingData.isBilling = value;
  526. break;
  527. case MISC_CMD_WEB_STOP_CHARGING:
  528. pSysConfig->StopChargingByButton = value;
  529. break;
  530. //--- Control Dispenser ---
  531. case MISC_CMD_HARDWARE_REBOOT:
  532. if (value != YES) {
  533. clearMiscCommand();
  534. break;
  535. }
  536. log_info("Hardware reboot");
  537. ShmOCPP16Data->MsMsg.bits.ResetReq = YES;
  538. strcpy((char *)ShmOCPP16Data->Reset.Type, "Hard");
  539. clearMiscCommand();
  540. break;
  541. case MISC_CMD_SOFTWARE_RESTART:
  542. if (value != YES) {
  543. clearMiscCommand();
  544. break;
  545. }
  546. clearMiscCommand();
  547. log_info("Software reboot");
  548. ShmOCPP16Data->MsMsg.bits.ResetReq = YES;
  549. strcpy((char *)ShmOCPP16Data->Reset.Type, "Soft");
  550. break;
  551. case MISC_CMD_REMOTE_START_CHARGING:
  552. if (value != YES) {
  553. if (plugNum == 1) {
  554. clearMiscCommand();
  555. }
  556. break;
  557. }
  558. log_info("Remote start charging plugNum = %d", plugNum);
  559. if (getSelGunWaitToAuthor(plugNum) == FAIL) {
  560. log_error("Remote start gun already charging");
  561. break;
  562. }
  563. if (pSysInfo->CurGunSelected != (plugNum)) {
  564. if (plugNum == LEFT_GUN_NUM &&
  565. (ShmSelectGunInfo->SelGunInfo.LeftGun == SEL_GUN_CONFIRM ||
  566. ShmSelectGunInfo->SelGunInfo.LeftGun == SEL_GUN_ATHOR)) {
  567. ShmSelectGunInfo->SelGunInfo.LeftGun = SEL_GUN_RELEASE;
  568. strcpy((char *)pSysConfig->UserId, "");
  569. pSysInfo->WaitForPlugit = NO;
  570. sleep(1); //Jerry add
  571. pSysInfo->SystemPage = _LCM_VIEW;
  572. GetClockTime(&pSysInfo->SystemTimeoutTimer, NULL);
  573. pSysInfo->SystemTimeoutFlag = Timeout_None;
  574. }
  575. if (plugNum == RIGHT_GUN_NUM &&
  576. (ShmSelectGunInfo->SelGunInfo.RightGun == SEL_GUN_CONFIRM ||
  577. ShmSelectGunInfo->SelGunInfo.RightGun == SEL_GUN_ATHOR)) {
  578. ShmSelectGunInfo->SelGunInfo.RightGun = SEL_GUN_RELEASE;
  579. strcpy((char *)pSysConfig->UserId, "");
  580. pSysInfo->WaitForPlugit = NO;
  581. sleep(1); //Jerry add
  582. pSysInfo->SystemPage = _LCM_VIEW;
  583. GetClockTime(&pSysInfo->SystemTimeoutTimer, NULL);
  584. pSysInfo->SystemTimeoutFlag = Timeout_None;
  585. }
  586. pSysInfo->CurGunSelected = (plugNum);
  587. pSysInfo->CurGunSelectedByAc = NO_DEFINE;
  588. }
  589. setConfirmSelGun(pSysInfo->CurGunSelected);
  590. ShmOCPP16Data->CsMsg.bits[plugNum].RemoteStartTransactionReq = YES;
  591. ShmSelectGunInfo->PricesInfo[plugNum].Balance = 0.00;
  592. clearMiscCommand();
  593. break;
  594. case MISC_CMD_REMOTE_STOP_CHARGING:
  595. if (value != YES) {
  596. if (plugNum == 1) {
  597. clearMiscCommand();
  598. }
  599. break;
  600. }
  601. strcpy((char *)pSysConfig->UserId, "");
  602. pSysInfo->WaitForPlugit = NO;
  603. pSysInfo->SystemPage = _LCM_VIEW;
  604. GetClockTime(&pSysInfo->SystemTimeoutTimer, NULL);
  605. pSysInfo->SystemTimeoutFlag = Timeout_None;
  606. destroySelectGun(plugNum);
  607. clearMiscCommand();
  608. break;
  609. case MISC_CMD_REMOTE_UNLOCK:
  610. if (value != YES) {
  611. if (plugNum == 1) {
  612. clearMiscCommand();
  613. }
  614. break;
  615. }
  616. if (isDetectPlugin() == YES) {
  617. ClearDetectPluginFlag();
  618. strcpy((char *)pSysConfig->UserId, "");
  619. pSysInfo->WaitForPlugit = NO;
  620. pSysInfo->SystemPage = _LCM_VIEW;
  621. GetClockTime(&pSysInfo->SystemTimeoutTimer, NULL);
  622. pSysInfo->SystemTimeoutFlag = Timeout_None;
  623. destroySelectGun(plugNum);
  624. } else {
  625. pDcChargingInfo = (struct ChargingInfoData *)GetDcChargingInfoData(plugNum);
  626. pDcChargingInfo->StopChargeFlag = POWER_CABINET_STOP_CHARGING;
  627. }
  628. break;
  629. case MISC_CMD_AUTH_DISABLE:
  630. pSysConfig->AuthorisationMode = value;
  631. log_info("Auto Authorize Disable:%d", pSysConfig->AuthorisationMode);
  632. //clearMiscCommand();
  633. break;
  634. case MISC_CMD_EVCCID_ENABLE:
  635. pSysConfig->isAuthrizeByEVCCID = value;
  636. log_info("Authorize By EVCCID:%d",pSysConfig->isAuthrizeByEVCCID);
  637. //clearMiscCommand();
  638. break;
  639. default:
  640. clearMiscCommand();
  641. break;
  642. }
  643. usleep(128);
  644. }
  645. return ret;
  646. }
  647. static int chargingcapabilityHandle(uint8_t *data, uint8_t plugNum)
  648. {
  649. uint8_t addr = 0;
  650. float MaxVolt, MaxCurrent, MaxPower;
  651. static PricesInfo pricesInfo[2] = {0};
  652. CapabilityInfo *pCapabilityInfo = NULL;
  653. AccountInfo *pAccountInfo = NULL;
  654. pDcChargingInfo = (struct ChargingInfoData *)GetDcChargingInfoData(plugNum);
  655. //--- charging capability information --------------------------------------
  656. pCapabilityInfo = (CapabilityInfo *)&data[0];
  657. MaxVolt = (float)(ntohs(pCapabilityInfo->OutputVoltage));
  658. if (MaxVolt >= 0) {
  659. if (MaxVolt > MAX_VOLTAGE) {
  660. MaxVolt = MAX_VOLTAGE;
  661. }
  662. pDcChargingInfo->MaximumChargingVoltage = MaxVolt;
  663. }
  664. MaxCurrent = (float)(ntohs(pCapabilityInfo->OutputCurrent));
  665. if (MaxCurrent >= 0) {
  666. if (MaxCurrent > MAX_CURRENCY) {
  667. MaxCurrent = MAX_CURRENCY;
  668. }
  669. pDcChargingInfo->AvailableChargingCurrent = MaxCurrent;
  670. }
  671. MaxPower = (float)(ntohs(pCapabilityInfo->OutputPower));
  672. if (MaxPower >= 0) {
  673. if (MaxPower > MAX_POWER) {
  674. MaxPower = MAX_POWER;
  675. }
  676. pDcChargingInfo->AvailableChargingPower = MaxPower;
  677. }
  678. //MaxVolt = (float)(data[0] << 8 |data[1]);
  679. //MaxCurrent = (float)(data[2] << 8 | data[3]);
  680. //MaxPower = (float)(data[4] << 8 | data[5]);
  681. //printf("MaxVolt=%f, MaxCurrent=%f, MaxPower=%f,", MaxVolt, MaxCurrent, MaxPower);
  682. //--- user prices information ----------------------------------------------
  683. addr = (sizeof(CapabilityInfo) - 2); //2 byte reserved
  684. pAccountInfo = (AccountInfo *)&data[addr];
  685. pSysConfig->BillingData.Currency = pAccountInfo->Currency;
  686. ShmSelectGunInfo->PricesInfo[plugNum].UserPrices = transPricesUnit(ntohl(pAccountInfo->UserPrices));
  687. pDcChargingInfo->ChargingFee = transPricesUnit(ntohl(pAccountInfo->TotalCost));
  688. ShmSelectGunInfo->PricesInfo[plugNum].Balance = transPricesUnit(ntohl(pAccountInfo->Balance));
  689. if ((pricesInfo[plugNum].UserPrices != ShmSelectGunInfo->PricesInfo[plugNum].UserPrices) ||
  690. (pricesInfo[plugNum].Balance != ShmSelectGunInfo->PricesInfo[plugNum].Balance)) {
  691. pricesInfo[plugNum].UserPrices = ShmSelectGunInfo->PricesInfo[plugNum].UserPrices;
  692. pricesInfo[plugNum].Balance = ShmSelectGunInfo->PricesInfo[plugNum].Balance;
  693. log_info("id = %d, user prices = %.2f, Total cost = %.2f, Account balances = %.2f, currency = %s",
  694. plugNum,
  695. ShmSelectGunInfo->PricesInfo[plugNum].UserPrices,
  696. pDcChargingInfo->ChargingFee,
  697. ShmSelectGunInfo->PricesInfo[plugNum].Balance,
  698. (uint8_t *)GetCurrency(pSysConfig->BillingData.Currency)
  699. );
  700. }
  701. return PASS;
  702. }
  703. static void addFaultCodeToBuf(uint8_t *Code)
  704. {
  705. uint8_t warningCount = pSysWarning->WarningCount;
  706. if (warningCount < 10) {
  707. memcpy(&pSysWarning->WarningCode[warningCount][0],
  708. Code,
  709. strlen((char *)Code));
  710. pSysWarning->WarningCount++;
  711. }
  712. }
  713. static void removeFaultCodeToBuf(uint8_t *Code)
  714. {
  715. uint8_t find = 0x01;
  716. uint8_t i = 0;
  717. char _code[7] = {0};
  718. sprintf(_code, "%s", Code);
  719. // 把相關的錯誤碼一次移除,避免重複顯示
  720. while (find) {
  721. usleep(128);
  722. find = 0x00;
  723. for (i = 0; i < pSysWarning->WarningCount; i++) {
  724. usleep(128);
  725. if (find == 0x00) {
  726. if (memcmp(&pSysWarning->WarningCode[i][0], _code, 7) == 0) {
  727. find = 0x01;
  728. }
  729. } else {
  730. memcpy(&pSysWarning->WarningCode[i - 1][0],
  731. &pSysWarning->WarningCode[i][0], 7);
  732. }
  733. }
  734. if (find) {
  735. pSysWarning->WarningCount--;
  736. }
  737. }
  738. }
  739. bool CompareArrayIsZero(uint8_t *array, unsigned int array_size)
  740. {
  741. uint8_t i;
  742. if (array != NULL) {
  743. for (i = 0; i < array_size; i++) {
  744. if (array[i] != 0) {
  745. return false;
  746. }
  747. }
  748. }
  749. return true;
  750. }
  751. static int powerCabinetStatusProcess(uint8_t dataLen, uint8_t *data)
  752. {
  753. uint8_t ret = 0;
  754. uint8_t i = 0;
  755. uint8_t count = 0;
  756. uint8_t EventCodeTmp[7] = {0};
  757. uint8_t StatusArray[MAX_REGISTER_NUM][WARNING_CODE_SIZE] = {0};
  758. uint8_t remaindLen = 0;
  759. //uint8_t statusCodeError = 0;
  760. if (dataLen > 0) {
  761. Hexdump((uint8_t *)data, dataLen);
  762. remaindLen = dataLen % WARNING_CODE_SIZE;
  763. if (remaindLen != 0) {
  764. log_info("fail status code length = %d", dataLen);
  765. dataLen -= remaindLen;
  766. }
  767. if (dataLen < WARNING_CODE_SIZE) {
  768. log_error("fail status code length = %d", dataLen);
  769. Hexdump(data, dataLen);
  770. if (pSysWarning->WarningCount > 0) {
  771. for (i = 0; i < pSysWarning->WarningCount; i++) {
  772. usleep(128);
  773. if (pSysWarning->WarningCode[i][1] >= '3' || //from backend or power cabinet
  774. (pSysWarning->WarningCode[i][0] >= 'B' &&
  775. pSysWarning->WarningCode[i][1] >= '4')) {
  776. memset(EventCodeTmp, 0, sizeof(EventCodeTmp));
  777. memcpy(EventCodeTmp,
  778. pSysWarning->WarningCode[i],
  779. sizeof(EventCodeTmp));
  780. removeFaultCodeToBuf(EventCodeTmp);
  781. }
  782. }
  783. }
  784. return FAIL;
  785. }
  786. for (count = 0; count < dataLen; count += WARNING_CODE_SIZE) {
  787. usleep(128);
  788. if (strncmp((char *)&data[count + 1], "1", 1) != 0 &&
  789. strncmp((char *)&data[count + 1], "2", 1) != 0 &&
  790. strncmp((char *)&data[count + 1], "3", 1) != 0 &&
  791. (strncmp((char *)&data[count], "B", 1) != 0 &&
  792. strncmp((char *)&data[count + 1], "4", 1) != 0)
  793. ) {
  794. log_error("error status code = %s", (char *)&data[count]);
  795. continue;
  796. }
  797. // misc command status code handle
  798. if (strncmp((char *)&data[count], MISC_ST_MISC_CMD, WARNING_CODE_SIZE) == 0) {
  799. gDoCommGblData.MiscCmd = REG_MISC_CONTROL;
  800. memset((char *)&data[count], 0, WARNING_CODE_SIZE);
  801. continue;
  802. } else if (strncmp((char *)&data[count], MISC_ST_VERSION, WARNING_CODE_SIZE) == 0) {
  803. gDoCommGblData.MiscCmd = REG_REPORT_CSU_VERSION;
  804. memset((char *)&data[count], 0, WARNING_CODE_SIZE);
  805. continue;
  806. }
  807. if (CompareArrayIsZero((uint8_t *)&data[count], WARNING_CODE_SIZE) == true) {
  808. memset(EventCodeTmp, 0, sizeof(EventCodeTmp));
  809. memcpy(EventCodeTmp, (char *)&data[count], sizeof(EventCodeTmp));
  810. log_error("error status = %s", EventCodeTmp);
  811. continue;
  812. }
  813. strncpy((char *)&StatusArray[count / WARNING_CODE_SIZE], (char *)&data[count], WARNING_CODE_SIZE);
  814. ret = 0;
  815. for (i = 0; i < pSysWarning->WarningCount; i++) {
  816. usleep(128);
  817. if (memcmp(&pSysWarning->WarningCode[i][0],
  818. StatusArray[count / WARNING_CODE_SIZE],
  819. WARNING_CODE_SIZE) == 0) {
  820. ret = 1;
  821. break;
  822. }
  823. }
  824. if (ret == 0) {
  825. addFaultCodeToBuf(StatusArray[count / WARNING_CODE_SIZE]);
  826. }
  827. //Rtn=StatusCodeProcessing(StatusArray[count/6]);
  828. }
  829. } else {
  830. if (CompareArrayIsZero(data, WARNING_CODE_SIZE) == false) {
  831. log_error("power cabinet status code data length is zero, but have data");
  832. Hexdump((uint8_t *)data, WARNING_CODE_SIZE);
  833. }
  834. }
  835. for (i = 0; i < pSysWarning->WarningCount; i++) {
  836. usleep(128);
  837. if (pSysWarning->WarningCode[i][1] >= '3' || //from backend or power cabinet 0x33
  838. (pSysWarning->WarningCode[i][0] >= 'B' &&
  839. pSysWarning->WarningCode[i][1] >= '4')) {
  840. ret = 0;
  841. for (count = 0; count < (dataLen / WARNING_CODE_SIZE); count++) {
  842. usleep(128);
  843. if (memcmp(&pSysWarning->WarningCode[i][0],
  844. StatusArray[count],
  845. WARNING_CODE_SIZE) == 0) {
  846. ret = 1;
  847. break;
  848. }
  849. }
  850. if (ret == 0) {
  851. memset(EventCodeTmp, 0, sizeof(EventCodeTmp));
  852. memcpy(EventCodeTmp,
  853. pSysWarning->WarningCode[i],
  854. sizeof(EventCodeTmp));
  855. removeFaultCodeToBuf(EventCodeTmp);
  856. }
  857. }
  858. }
  859. return PASS;
  860. }
  861. void GetCabientDcmRevHandle(uint8_t *data)
  862. {
  863. PCDCMVer *pVersion = (PCDCMVer *)&data[0];
  864. strcpy(ShmDcCommonData->CabinetModelName , pVersion->CabinetModelName);
  865. strcpy(ShmDcCommonData->CabinetBoolLoaderVersion, pVersion->CabinetBoolLoaderVersion);
  866. strcpy(ShmDcCommonData->CabinetKernelVersion , pVersion->CabinetKernelVersion);
  867. strcpy(ShmDcCommonData->CabinetRFSystemVersion , pVersion->CabinetRFSystemVersion);
  868. strcpy(ShmDcCommonData->CabinetPrimaryVersion , pVersion->CabinetPrimaryVersion);
  869. strcpy(ShmDcCommonData->CabinetIPAddr , pVersion->CabinetIPAddr);
  870. }
  871. void GetCabientOtherRevHandle(uint8_t *data)
  872. {
  873. PCOthVer *pVersion = (PCOthVer *)&data[0];
  874. strcpy(ShmDcCommonData->CabinetRelay0Version , pVersion->CabinetRelay0Version);
  875. strcpy(ShmDcCommonData->CabinetRelay1Version , pVersion->CabinetRelay1Version);
  876. strcpy(ShmDcCommonData->CabinetFanVersion , pVersion->CabinetFanVersion);
  877. }
  878. void GetCabientEachPsuRevHandle(uint8_t *data)
  879. {
  880. PCnPsuVer *pVersion = (PCnPsuVer *)&data[0];
  881. strcpy(ShmDcCommonData->PsuVer[pVersion->n_PSU].DCVersion, pVersion->DCVersion);
  882. strcpy(ShmDcCommonData->PsuVer[pVersion->n_PSU].FPCVersion, pVersion->FPCVersion);
  883. }
  884. static int responsePackeHandle(int fd, uint8_t *pResult, uint8_t plugNum, uint8_t reg)
  885. {
  886. int ret = PASS;
  887. uint8_t rawDataLen = 0;
  888. CsuResultPkt *pCsuResult = (CsuResultPkt *)pResult;
  889. SoftwareUpdInfo *pSoftwareUpd = NULL;
  890. PcPsuOutput *pPcPsuOutput = NULL;
  891. //Hexdump((uint8_t *)pCsuResult, sizeof(CmdHead) + pCsuResult->Head.DataLen);
  892. if (compareOpcode(pCsuResult->Head.OP) == FAIL ||
  893. compareResult(pCsuResult->Data.Result) == FAIL ||
  894. compareRegister(pCsuResult->Data.Register, reg) == FAIL) {
  895. if (reg != REG_CHARGING_PERMISSION) {
  896. return FAIL;
  897. }
  898. }
  899. rawDataLen = (pCsuResult->Head.DataLen - 2); //2 byte = register and result byte
  900. switch (pCsuResult->Data.Register) {
  901. case REG_MODEL_NAME:
  902. break;
  903. case REG_CONNECTOR_ID:
  904. gDoCommGblData.ConnectorID[0] = pCsuResult->Data.Data[0];
  905. gDoCommGblData.ConnectorID[1] = pCsuResult->Data.Data[1];
  906. log_info("OK (Left connector ID = %d, Right connector ID = %d)",
  907. pCsuResult->Data.Data[0],
  908. pCsuResult->Data.Data[1]);
  909. break;
  910. case REG_POWER_CABINET_STATUS:
  911. ret = powerCabinetStatusProcess(rawDataLen, pCsuResult->Data.Data);
  912. break;
  913. case REG_DISPENSER_STATUS:
  914. memset(&gPreSysWarningInfo, 0, sizeof(struct WARNING_CODE_INFO));
  915. memcpy((uint8_t *)&gPreSysWarningInfo,
  916. pSysWarning,
  917. sizeof(struct WARNING_CODE_INFO));
  918. break;
  919. case REG_CHARGING_CAP:
  920. chargingcapabilityHandle(pCsuResult->Data.Data, plugNum);
  921. break;
  922. case REG_CHARGING_TARGET:
  923. break;
  924. case REG_SOFTWARE_UPDATE:
  925. pSoftwareUpd = (SoftwareUpdInfo *)&pCsuResult->Data.Data[0];
  926. if ((strcmp((char *)pSoftwareUpd->ImgName, "") == 0) ||
  927. (rawDataLen == 0) ||
  928. pSoftwareUpd->UpdateState != 1) {
  929. ret = FAIL;
  930. break;
  931. }
  932. ret = FAIL;
  933. ret = updateFirmwareHandle(pSoftwareUpd->ImgName);
  934. break;
  935. case REG_PLUG_IN_STATE:
  936. break;
  937. case REG_CONNECTOR_STATE:
  938. break;
  939. case REG_USER_ID:
  940. //log_info("USER ID:%s", pCsuResult->Data.Data[0] == 1 ? "Accept" : "Reject" );
  941. //if (pCsuResult->Data.Data[0] <= 0) {
  942. // return FAIL;
  943. //}
  944. if (pCsuResult->Data.Result == COMMAND_RESULT_NG) {
  945. return COMMAND_RESULT_NG;
  946. }
  947. ShmSelectGunInfo->AuthorStateFromCabinet[plugNum] = pCsuResult->Data.Data[0];
  948. return pCsuResult->Data.Data[0];
  949. break;
  950. case REG_CHARGING_PERMISSION:
  951. //log_info("id = %d, result = %s, action = %s",
  952. // pCsuResult->Head.ID,
  953. // pCsuResult->Data.Result == 1 ? "OK" : "NG",
  954. // pCsuResult->Data.Data[0] == 1 ? "Permitted" : "NOT permitted");
  955. if (pCsuResult->Data.Result == COMMAND_RESULT_NG) {
  956. return COMMAND_RESULT_NG;
  957. }
  958. ShmSelectGunInfo->WaitDoCommPermission[plugNum] = pCsuResult->Data.Data[0];
  959. return pCsuResult->Data.Data[0];
  960. break;
  961. case REG_MISC_CONTROL:
  962. miscCommandHandle(rawDataLen, plugNum, pCsuResult->Data.Data);
  963. break;
  964. case REG_REPORT_CSU_VERSION:
  965. case REG_REPORT_OTHER_VERSION:
  966. break;
  967. case REG_PRESENT_CHARGING_INFO:
  968. pPcPsuOutput = (PcPsuOutput *)&pCsuResult->Data.Data[0];
  969. ShmDcCommonData->PcPsuOutput[plugNum].Voltage = ntohs((uint16_t)pPcPsuOutput->Voltage);
  970. ShmDcCommonData->PcPsuOutput[plugNum].Current = ntohs((uint16_t)pPcPsuOutput->Current);
  971. //log_info("%d power cabinet PSU output vol = %f, cur = %f",
  972. // plugNum,
  973. // (float)ShmDcCommonData->PcPsuOutput[plugNum].Voltage,
  974. // (float)ShmDcCommonData->PcPsuOutput[plugNum].Current);
  975. break;
  976. case REG_QRCODE_URL_INFO:
  977. ret = qrCodeUrlInfoHandle(pCsuResult->Data.Data);
  978. break;
  979. case REG_WAIT_PLUG_IT_STATE:
  980. break;
  981. case REG_Ground_Fault_Detection:
  982. if (pCsuResult->Data.Result == COMMAND_RESULT_NG) {
  983. return COMMAND_RESULT_NG;
  984. }
  985. //集電弓relay 不打開才能進入動作
  986. pDcChargingInfo = (struct ChargingInfoData *)GetDcChargingInfoData(plugNum);
  987. if(pDcChargingInfo->PantographFlag == YES)
  988. {
  989. GroundFaultDetection *gfd = (GroundFaultDetection *)&pCsuResult->Data.Data[0];
  990. if(pDcChargingInfo->GroundFaultStatus != gfd->Status)
  991. {
  992. log_info("id = %d, GFD Result = %d",
  993. pCsuResult->Head.ID,
  994. gfd->Status);
  995. }
  996. pDcChargingInfo->GroundFaultStatus = gfd->Status;
  997. }
  998. break;
  999. case REG_GET_CABINET_DCM_VERSION:
  1000. GetCabientDcmRevHandle(pCsuResult->Data.Data);
  1001. break;
  1002. case REG_GET_CABINET_OTHER_VERSION:
  1003. GetCabientOtherRevHandle(pCsuResult->Data.Data);
  1004. break;
  1005. case REG_GET_CABINET_PSU_NUMBER:
  1006. ShmDcCommonData->PSU_Number = pCsuResult->Data.Data[0];
  1007. log_info("PSU Numbers:%d",ShmDcCommonData->PSU_Number);
  1008. break;
  1009. case REG_GET_CABINET_PSU_VERSION:
  1010. GetCabientEachPsuRevHandle(pCsuResult->Data.Data);
  1011. break;
  1012. default:
  1013. break;
  1014. }
  1015. return ret;
  1016. }
  1017. static int composeSocketData(int fd,
  1018. uint8_t id,
  1019. uint8_t opCode,
  1020. uint8_t reg,
  1021. uint8_t dataLen,
  1022. uint8_t *data)
  1023. {
  1024. int ret = PASS;
  1025. int size = 0;
  1026. int sendPktLen = 0;
  1027. uint8_t i = 0;
  1028. uint8_t plugNum = 0;
  1029. CsuCmdPkt csuCmdPkt = {0};
  1030. CsuResultPkt csuResult = {0};
  1031. csuCmdPkt.Head.SeqNum = gDoCommGblData.SeqNum++;
  1032. csuCmdPkt.Head.ID = id;
  1033. csuCmdPkt.Head.OP = opCode;
  1034. csuCmdPkt.Head.DataLen = dataLen + 1; //+1 for register byte
  1035. csuCmdPkt.Data.Register = reg;
  1036. if ((data != NULL) && (dataLen > 0)) {
  1037. memcpy(csuCmdPkt.Data.Data, data, dataLen);
  1038. }
  1039. sendPktLen = csuCmdPkt.Head.DataLen + sizeof(csuCmdPkt.Head);
  1040. //Hexdump((uint8_t *)&csuCmdPkt, sendPktLen);
  1041. // Not Let ask cabinet version trasmit into FAIL Network
  1042. if (reg == REG_GET_CABINET_DCM_VERSION || reg == REG_GET_CABINET_OTHER_VERSION ||
  1043. reg == REG_GET_CABINET_PSU_NUMBER || reg == REG_GET_CABINET_PSU_VERSION) {
  1044. gDoCommGblData.DisConnCount = 0;
  1045. }
  1046. //send command packet
  1047. if ((size = sendTcpSocket(fd, (uint8_t *)&csuCmdPkt, sendPktLen)) < 0) {
  1048. log_error("TCP socket send packet fail = %d", size);
  1049. gDoCommGblData.DisConnCount++;
  1050. return FAIL;
  1051. }
  1052. //receive result head
  1053. if ((size = recvTcpSocket(fd, (uint8_t *)&csuResult.Head, sizeof(csuResult.Head))) < 0) {
  1054. log_error("TCP socket RX head fail = %d", size);
  1055. gDoCommGblData.DisConnCount++;
  1056. return FAIL;
  1057. }
  1058. //receive result raw data
  1059. if ((size = recvTcpSocket(fd, (uint8_t *)&csuResult.Data, csuResult.Head.DataLen)) < 0) {
  1060. log_error("TCP socket RX data fail = %d", size);
  1061. gDoCommGblData.DisConnCount++;
  1062. return FAIL;
  1063. }
  1064. for (i = 0; i < sizeof(gDoCommGblData.ConnectorID); i++) {
  1065. if (id == gDoCommGblData.ConnectorID[i]) {
  1066. plugNum = i;
  1067. break;
  1068. }
  1069. }
  1070. ret = responsePackeHandle(fd, (uint8_t *)&csuResult, plugNum, csuCmdPkt.Data.Register);
  1071. gDoCommGblData.DisConnCount = 0; //送收資料沒有問題, 清除中斷網路計數
  1072. return ret;
  1073. }
  1074. static int writeWaitPlugItState(int fd, uint8_t id)
  1075. {
  1076. int ret = PASS;
  1077. uint8_t data[2] = {pSysInfo->WaitForPlugit, 0};
  1078. //printf("WaitForPlugit = %d", pSysInfo->WaitForPlugit);
  1079. if ((ret = composeSocketData(fd,
  1080. id,
  1081. OP_WRITE_DATA,
  1082. REG_WAIT_PLUG_IT_STATE,
  1083. 1,
  1084. &data[0])) == FAIL) {
  1085. return ret;
  1086. }
  1087. return ret;
  1088. }
  1089. static int readQRcodeURLAndSystemDate(int fd)
  1090. {
  1091. int ret = PASS;
  1092. ret = composeSocketData(fd,
  1093. ID_REGISTER,
  1094. OP_READ_DATA,
  1095. REG_QRCODE_URL_INFO,
  1096. 0,
  1097. NULL);
  1098. return ret;
  1099. }
  1100. static int writePresentChargingInfo(int fd, uint8_t plugNum, uint8_t id)
  1101. {
  1102. int ret = PASS;
  1103. uint8_t dataBuf[16] = {0};
  1104. PreChargingInfo *pPreChargingInfo = (PreChargingInfo *)dataBuf;
  1105. pDcChargingInfo = (struct ChargingInfoData *)GetDcChargingInfoData(plugNum);
  1106. if(pDcChargingInfo->PantographFlag == NO)
  1107. {
  1108. pPreChargingInfo->PresentChargingVoltage = htons((uint16_t)(pDcChargingInfo->PresentChargingVoltage * 10));
  1109. pPreChargingInfo->PresentChargingCurrent = htons((uint16_t)(pDcChargingInfo->PresentChargingCurrent * 10));
  1110. }
  1111. else
  1112. {
  1113. pPreChargingInfo->PresentChargingVoltage = htons((uint16_t)(ShmDcCommonData->PcPsuOutput[plugNum].Voltage));
  1114. pPreChargingInfo->PresentChargingCurrent = htons((uint16_t)(ShmDcCommonData->PcPsuOutput[plugNum].Current));
  1115. }
  1116. pPreChargingInfo->RemainChargingDuration = htonl(pDcChargingInfo->RemainChargingDuration);
  1117. pPreChargingInfo->EvBatterySoc = pDcChargingInfo->EvBatterySoc;
  1118. ret = composeSocketData(fd,
  1119. id,
  1120. OP_WRITE_DATA,
  1121. REG_PRESENT_CHARGING_INFO,
  1122. sizeof(PreChargingInfo),
  1123. (uint8_t *)pPreChargingInfo);
  1124. return ret;
  1125. }
  1126. static int writeOtherModuleVersion(int fd)
  1127. {
  1128. int ret = PASS;
  1129. uint8_t data[255] = {0};
  1130. uint8_t dataLen = 0;
  1131. //report other module version message
  1132. memcpy(&data[dataLen], pSysInfo->FanModuleFwRev, VERSION_BUF_SIZE);
  1133. dataLen += VERSION_BUF_SIZE;
  1134. memcpy(&data[dataLen], pSysInfo->RelayModuleFwRev, VERSION_BUF_SIZE);
  1135. dataLen += VERSION_BUF_SIZE;
  1136. memcpy(&data[dataLen], pSysInfo->Connector1FwRev, VERSION_BUF_SIZE);
  1137. dataLen += VERSION_BUF_SIZE;
  1138. memcpy(&data[dataLen], pSysInfo->Connector2FwRev, VERSION_BUF_SIZE);
  1139. dataLen += VERSION_BUF_SIZE;
  1140. memcpy(&data[dataLen], pSysInfo->LedModuleFwRev, VERSION_BUF_SIZE);
  1141. dataLen += VERSION_BUF_SIZE;
  1142. ret = composeSocketData(fd,
  1143. ID_REGISTER,
  1144. OP_WRITE_DATA,
  1145. REG_REPORT_OTHER_VERSION,
  1146. dataLen,
  1147. &data[0]);
  1148. return ret;
  1149. }
  1150. static int writeCsuModuleVersion(int fd)
  1151. {
  1152. int ret = PASS;
  1153. uint8_t data[255] = {0};
  1154. uint8_t dataLen = 0;
  1155. //report CSU platform version message
  1156. memcpy(&data[dataLen], pSysInfo->CsuBootLoadFwRev, VERSION_BUF_SIZE);
  1157. dataLen += VERSION_BUF_SIZE;
  1158. memcpy(&data[dataLen], pSysInfo->CsuKernelFwRev, VERSION_BUF_SIZE);
  1159. dataLen += VERSION_BUF_SIZE;
  1160. memcpy(&data[dataLen], pSysInfo->CsuRootFsFwRev, VERSION_BUF_SIZE);
  1161. dataLen += VERSION_BUF_SIZE;
  1162. memcpy(&data[dataLen], pSysInfo->CsuPrimFwRev, VERSION_BUF_SIZE);
  1163. dataLen += VERSION_BUF_SIZE;
  1164. ret = composeSocketData(fd,
  1165. ID_REGISTER,
  1166. OP_WRITE_DATA,
  1167. REG_REPORT_CSU_VERSION,
  1168. dataLen,
  1169. &data[0]);
  1170. return ret;
  1171. }
  1172. static int readMiscCommand(int fd, uint8_t id)
  1173. {
  1174. int ret = PASS;
  1175. ret = composeSocketData(fd,
  1176. id,
  1177. OP_READ_DATA,
  1178. REG_MISC_CONTROL,
  1179. 0,
  1180. NULL);
  1181. return ret;
  1182. }
  1183. static int readChargePermission(int fd, uint8_t id)
  1184. {
  1185. return composeSocketData(fd,
  1186. id,
  1187. OP_READ_DATA,
  1188. REG_CHARGING_PERMISSION,
  1189. 0,
  1190. NULL);
  1191. }
  1192. static int writeGroundFaultDetection(int fd, uint8_t status, uint8_t id)
  1193. {
  1194. int ret = PASS;
  1195. uint8_t dataBuf[1] = {status};
  1196. ret = composeSocketData(fd,
  1197. id,
  1198. OP_WRITE_DATA,
  1199. REG_Ground_Fault_Detection,
  1200. 1,
  1201. &dataBuf[0]);
  1202. return ret;
  1203. }
  1204. static int writeUserID(int fd, uint8_t id, uint8_t *pUserID)
  1205. {
  1206. if ((strlen((char *)pUserID) <= 0) || (pUserID == NULL)) {
  1207. return FAIL;
  1208. }
  1209. return composeSocketData(fd,
  1210. id,
  1211. OP_WRITE_DATA,
  1212. REG_USER_ID,
  1213. strlen((char *)pUserID),
  1214. pUserID);
  1215. }
  1216. static int writeConnectorState(int fd, uint8_t plugNum, uint8_t id)
  1217. {
  1218. uint8_t dataBuf[10] = {'\0'};
  1219. ConnectorState *pConnState = (ConnectorState *)dataBuf;
  1220. static char vendorErrorCodeTmp[2][WARNING_CODE_SIZE] = {0};
  1221. int ret = PASS;
  1222. pDcChargingInfo = (struct ChargingInfoData *)GetDcChargingInfoData(plugNum);
  1223. pConnState->ConnectorTemp = pDcChargingInfo->ConnectorTemp;
  1224. pConnState->ChillerTemp = pDcChargingInfo->ChillerTemp;
  1225. if (pDcChargingInfo->SystemStatus <= S_AUTHORIZING) {
  1226. pConnState->State = CONN_ST_IDLE; //idle
  1227. strncpy((char *)ShmOCPP16Data->StatusNotification[plugNum].VendorErrorCode,
  1228. "",
  1229. sizeof(ShmOCPP16Data->StatusNotification[plugNum].VendorErrorCode));
  1230. strncpy(&vendorErrorCodeTmp[plugNum][0], "", WARNING_CODE_SIZE);
  1231. } else if ((pDcChargingInfo->SystemStatus <= S_PREPARING_FOR_EVSE) ||
  1232. (pDcChargingInfo->SystemStatus == S_CCS_PRECHARGE_ST0) ||
  1233. (pDcChargingInfo->SystemStatus == S_CCS_PRECHARGE_ST1)) {
  1234. pConnState->State = CONN_ST_PREPARING; //preparing
  1235. } else if (pDcChargingInfo->SystemStatus == S_CHARGING) {
  1236. pConnState->State = CONN_ST_CHARGING; //charging
  1237. } else if (pDcChargingInfo->SystemStatus == S_TERMINATING) {
  1238. pConnState->State = CONN_ST_TERMINATING; //terminating
  1239. } else if ((pDcChargingInfo->SystemStatus == S_ALARM) ||
  1240. (pDcChargingInfo->SystemStatus == S_FAULT)) {
  1241. pConnState->State = CONN_ST_ALARM;
  1242. strncpy((char *)pConnState->WarningCode,
  1243. (char *)ShmOCPP16Data->StatusNotification[plugNum].VendorErrorCode,
  1244. WARNING_CODE_SIZE);
  1245. if (strncmp(&vendorErrorCodeTmp[plugNum][0], "", WARNING_CODE_SIZE) == 0) {
  1246. strncpy(&vendorErrorCodeTmp[plugNum][0],
  1247. (char *)ShmOCPP16Data->StatusNotification[plugNum].VendorErrorCode,
  1248. WARNING_CODE_SIZE);
  1249. vendorErrorCodeTmp[plugNum][6] = '\0';
  1250. log_info("1 ID = %d, VendorErrorCode = %s",
  1251. plugNum,
  1252. (char *)ShmOCPP16Data->StatusNotification[plugNum].VendorErrorCode);
  1253. } else {
  1254. if (strncmp(&vendorErrorCodeTmp[plugNum][0],
  1255. (char *)ShmOCPP16Data->StatusNotification[plugNum].VendorErrorCode,
  1256. WARNING_CODE_SIZE) != 0
  1257. ) {
  1258. strncpy(&vendorErrorCodeTmp[plugNum][0],
  1259. (char *)ShmOCPP16Data->StatusNotification[plugNum].VendorErrorCode,
  1260. WARNING_CODE_SIZE);
  1261. vendorErrorCodeTmp[plugNum][6] = '\0';
  1262. log_info("2 ID = %d, VendorErrorCode = %s",
  1263. plugNum,
  1264. (char *)ShmOCPP16Data->StatusNotification[plugNum].VendorErrorCode);
  1265. }
  1266. }
  1267. }
  1268. ret = composeSocketData(fd,
  1269. id,
  1270. OP_WRITE_DATA,
  1271. REG_CONNECTOR_STATE,
  1272. sizeof(dataBuf) - 1,
  1273. &dataBuf[0]);
  1274. return ret;
  1275. }
  1276. static int writePlugInStatus(int fd, uint8_t plugNum, uint8_t id)
  1277. {
  1278. int ret = PASS;
  1279. pDcChargingInfo = (struct ChargingInfoData *)GetDcChargingInfoData(plugNum);
  1280. uint8_t dataBuf[2] = {pDcChargingInfo->ConnectorPlugIn, 0};
  1281. ret = composeSocketData(fd,
  1282. id,
  1283. OP_WRITE_DATA,
  1284. REG_PLUG_IN_STATE,
  1285. 1,
  1286. &dataBuf[0]);
  1287. return ret;
  1288. }
  1289. static int readSoftwareUpdate(int fd, uint8_t gunID)
  1290. {
  1291. int ret = PASS;
  1292. uint8_t dataBuf[2] = {pSysInfo->FirmwareUpdate, 0};
  1293. ret = composeSocketData(fd,
  1294. gunID,
  1295. OP_READ_DATA,
  1296. REG_SOFTWARE_UPDATE,
  1297. 1,
  1298. &dataBuf[0]);
  1299. return ret;
  1300. }
  1301. static int writeChargingTarget(int fd, uint8_t plugNum, uint8_t id)
  1302. {
  1303. int ret = PASS;
  1304. float ChargingVolt, ChargingAmp;
  1305. uint8_t dataBuf[4] = {0};
  1306. pDcChargingInfo = (struct ChargingInfoData *)GetDcChargingInfoData(plugNum);
  1307. ChargingVolt = pDcChargingInfo->EvBatterytargetVoltage;
  1308. ChargingAmp = pDcChargingInfo->EvBatterytargetCurrent;
  1309. ChargingVolt *= 10;
  1310. ChargingAmp *= 10;
  1311. dataBuf[0] = ((uint16_t)ChargingVolt >> 8) & 0xFF;
  1312. dataBuf[1] = ((uint16_t)ChargingVolt) & 0xFF;
  1313. dataBuf[2] = ((uint16_t)ChargingAmp >> 8) & 0xFF;
  1314. dataBuf[3] = ((uint16_t)ChargingAmp) & 0xFF;
  1315. ret = composeSocketData(fd,
  1316. id,
  1317. OP_WRITE_DATA,
  1318. REG_CHARGING_TARGET,
  1319. sizeof(dataBuf),
  1320. &dataBuf[0]);
  1321. return ret;
  1322. }
  1323. static int readChargingCapability(int fd, uint8_t id)
  1324. {
  1325. int ret = PASS;
  1326. ret = composeSocketData(fd,
  1327. id,
  1328. OP_READ_DATA,
  1329. REG_CHARGING_CAP,
  1330. 0,
  1331. NULL);
  1332. return ret;
  1333. }
  1334. static int readCabinetDCMVersion(int fd, uint8_t id)
  1335. {
  1336. int ret = PASS;
  1337. ret = composeSocketData(fd,
  1338. id,
  1339. OP_READ_DATA,
  1340. REG_GET_CABINET_DCM_VERSION,
  1341. 0,
  1342. NULL);
  1343. return ret;
  1344. }
  1345. static int readCabinetOtherVersion(int fd, uint8_t id)
  1346. {
  1347. int ret = PASS;
  1348. ret = composeSocketData(fd,
  1349. id,
  1350. OP_READ_DATA,
  1351. REG_GET_CABINET_OTHER_VERSION,
  1352. 0,
  1353. NULL);
  1354. return ret;
  1355. }
  1356. static int readCabinetPSUNumber(int fd, uint8_t id)
  1357. {
  1358. int ret = PASS;
  1359. ret = composeSocketData(fd,
  1360. id,
  1361. OP_READ_DATA,
  1362. REG_GET_CABINET_PSU_NUMBER,
  1363. 0,
  1364. NULL);
  1365. return ret;
  1366. }
  1367. static int readCabinetEachPSUVersion(int fd, uint8_t id,uint8_t n_psu)
  1368. {
  1369. int ret = PASS;
  1370. uint8_t data_buf[0] = {0};
  1371. data_buf[0] = n_psu;
  1372. ret = composeSocketData(fd,
  1373. id,
  1374. OP_READ_DATA,
  1375. REG_GET_CABINET_PSU_VERSION,
  1376. sizeof(data_buf),
  1377. &data_buf[0]);
  1378. return ret;
  1379. }
  1380. static int writeDispenserStatus(int fd, uint8_t gunID)
  1381. {
  1382. uint8_t warningCount = 0;
  1383. uint8_t count = 0;
  1384. uint8_t CurWarnCodeTmp[10][6] = {0};
  1385. uint8_t PreWarnCodeTmp[10][6] = {0};
  1386. int ret = PASS;
  1387. if ((pSysWarning->WarningCount <= 0) &&
  1388. (gPreSysWarningInfo.WarningCount <= 0)) {
  1389. return FAIL;
  1390. }
  1391. warningCount = pSysWarning->WarningCount;
  1392. for (count = 0; count < warningCount; count++) {
  1393. memcpy(CurWarnCodeTmp[count], pSysWarning->WarningCode[count], WARNING_CODE_SIZE);
  1394. }
  1395. warningCount = gPreSysWarningInfo.WarningCount;
  1396. for (count = 0; count < warningCount; count++) {
  1397. memcpy(PreWarnCodeTmp[count], gPreSysWarningInfo.WarningCode[count], WARNING_CODE_SIZE);
  1398. }
  1399. if (strcmp((char *)&CurWarnCodeTmp[0], (char *)&PreWarnCodeTmp[0]) == 0) {
  1400. return FAIL;
  1401. }
  1402. ret = composeSocketData(fd,
  1403. gunID,
  1404. OP_WRITE_DATA,
  1405. REG_DISPENSER_STATUS,
  1406. strlen((char *)CurWarnCodeTmp),
  1407. &CurWarnCodeTmp[0][0]);
  1408. return ret;
  1409. }
  1410. static int readPowerCabinetStatus(int fd, uint8_t gunID)
  1411. {
  1412. int ret = PASS;
  1413. ret = composeSocketData(fd,
  1414. gunID,
  1415. OP_READ_DATA,
  1416. REG_POWER_CABINET_STATUS,
  1417. 0,
  1418. NULL);
  1419. return ret;
  1420. }
  1421. static int readConnectorID(int fd)
  1422. {
  1423. int ret = PASS;
  1424. ret = composeSocketData(fd,
  1425. ID_REGISTER,
  1426. OP_READ_DATA,
  1427. REG_CONNECTOR_ID,
  1428. 0,
  1429. NULL);
  1430. return ret;
  1431. }
  1432. static int WriteModelName(int fd)
  1433. {
  1434. int ret = PASS;
  1435. uint8_t Tmp = 0;
  1436. uint8_t TmpBuf[255] = {0};
  1437. memcpy(TmpBuf,
  1438. pSysConfig->ModelName,
  1439. strlen((char *)pSysConfig->ModelName));
  1440. Tmp = (uint8_t)(ShmPrimaryMcuData->InputDet.bits.Key2 << 2 |
  1441. ShmPrimaryMcuData->InputDet.bits.Key1 << 1 |
  1442. ShmPrimaryMcuData->InputDet.bits.Key0);
  1443. TmpBuf[strlen((char *)pSysConfig->ModelName)] = Tmp; //Dispenser switch value
  1444. ret = composeSocketData(fd,
  1445. ID_REGISTER,
  1446. OP_WRITE_DATA,
  1447. REG_MODEL_NAME,
  1448. strlen((char *)pSysConfig->ModelName) + 1,
  1449. &TmpBuf[0]);
  1450. return ret;
  1451. }
  1452. static void calDisconnectCount(uint8_t *pValue, uint8_t maxCount)
  1453. {
  1454. sleep(3); //wait 1 second
  1455. if ((*pValue) >= maxCount) {
  1456. setTcpStatus(ABNORMAL);
  1457. } else {
  1458. (*pValue)++;
  1459. }
  1460. }
  1461. static int CheckNetworkStatus(void)
  1462. {
  1463. char *ipAddr = (char *)&pSysConfig->Eth0Interface.EthIpAddress;
  1464. //printf("Check network IP Header = %s\n", ipAddr);
  1465. if (strstr(ipAddr, CMP_ETH_IP_HEAD) != NULL) {
  1466. return 1;
  1467. }
  1468. return 0;
  1469. }
  1470. static void updateFirmwareProcess(int fd, uint8_t gunID, uint8_t totalConnCount)
  1471. {
  1472. bool canUpdateFirmware = true;
  1473. uint8_t plugNum = 0;
  1474. uint8_t ackCount = 5;
  1475. struct timeb updateTime;
  1476. pDcChargingInfo = (struct ChargingInfoData *)GetDcChargingInfoData(plugNum);
  1477. if (pSysWarning->Level != 2) {
  1478. for (plugNum = 0; plugNum < totalConnCount; plugNum++) {
  1479. if (pDcChargingInfo->SystemStatus != S_IDLE &&
  1480. pDcChargingInfo->SystemStatus != S_RESERVATION &&
  1481. pDcChargingInfo->SystemStatus != S_MAINTAIN) {
  1482. canUpdateFirmware = false;
  1483. }
  1484. }
  1485. }
  1486. if (canUpdateFirmware) {
  1487. ftime(&updateTime);
  1488. if (DiffTimeb(gRegTimeUp[0][REG_SOFTWARE_UPDATE], updateTime) > LOOP_RETRY_TIME * 5) {
  1489. readSoftwareUpdate(fd, gunID);
  1490. ftime(&gRegTimeUp[0][REG_SOFTWARE_UPDATE]);
  1491. }
  1492. if (pSysInfo->FirmwareUpdate == YES) {
  1493. while (ackCount != 0) {
  1494. ftime(&updateTime);
  1495. if (DiffTimeb(gRegTimeUp[0][REG_SOFTWARE_UPDATE], updateTime) > LOOP_RETRY_TIME / 2) {
  1496. readSoftwareUpdate(fd, gunID);
  1497. ftime(&gRegTimeUp[0][REG_SOFTWARE_UPDATE]);
  1498. if (pSysInfo->FirmwareUpdate == NO) {
  1499. ackCount--;
  1500. }
  1501. }
  1502. usleep(128);
  1503. }
  1504. }
  1505. }
  1506. }
  1507. static void checkAuthorProcess(int fd, uint8_t plugNum)
  1508. {
  1509. int ret = 0;
  1510. uint8_t gunID = 0;
  1511. struct timeb AuthNowTime;
  1512. #if defined DD360Audi
  1513. gunID = gDoCommGblData.ConnectorID[pSysInfo->CurGunSelected];
  1514. //gunID = gDoCommGblData.ConnectorID[plugNum];
  1515. if (pSysConfig->AutoAuth_Disable) {
  1516. gunID = ID_REGISTER;
  1517. ShmSelectGunInfo->PricesInfo[pSysInfo->CurGunSelected].Balance = 0.0;
  1518. }
  1519. #else
  1520. gunID = ID_REGISTER;
  1521. ShmSelectGunInfo->PricesInfo[pSysInfo->CurGunSelected].Balance = 0.0; //非Audi 不需要等待主櫃回報餘額
  1522. #endif // DD360Audi
  1523. if ((ShmOCPP16Data->SpMsg.bits.AuthorizeReq == YES) ||
  1524. (pSysInfo->AuthorizeFlag == YES)) {
  1525. ftime(&AuthNowTime);
  1526. if (DiffTimeb(gRegTimeUp[plugNum][REG_USER_ID], AuthNowTime) > LOOP_RETRY_TIME) {
  1527. ret = writeUserID(fd,
  1528. gunID,
  1529. pSysConfig->UserId);
  1530. if (ret >= 0) {
  1531. memset(ShmOCPP16Data->Authorize.ResponseIdTagInfo.Status,
  1532. 0,
  1533. sizeof(ShmOCPP16Data->Authorize.ResponseIdTagInfo.Status));
  1534. if (ret == 0) {
  1535. strcpy((char *)ShmOCPP16Data->Authorize.ResponseIdTagInfo.Status, "Invalid");
  1536. } else {
  1537. strcpy((char *)ShmOCPP16Data->Authorize.ResponseIdTagInfo.Status, "Accepted");
  1538. }
  1539. //printf("%d Balance = %.2f, %.2f\n",
  1540. // plugNum,
  1541. // ShmSelectGunInfo->PricesInfo[plugNum].Balance,
  1542. // FAIL_BALANCE_PRICES);
  1543. if (ShmSelectGunInfo->PricesInfo[plugNum].Balance != FAIL_BALANCE_PRICES) {
  1544. ShmOCPP16Data->SpMsg.bits.AuthorizeConf = YES; //isAuthorizedComplete
  1545. ShmOCPP16Data->SpMsg.bits.AuthorizeReq = NO;
  1546. pSysInfo->AuthorizeFlag = NO;
  1547. ShmPsuData->SystemAvailablePower = NO;
  1548. ShmPsuData->SystemPresentPsuQuantity = NO;
  1549. }
  1550. }
  1551. ftime(&gRegTimeUp[plugNum][REG_USER_ID]);
  1552. }
  1553. }
  1554. }
  1555. static int messageTrigger(int fd, uint8_t plugNum, uint8_t gunID, uint8_t curReg)
  1556. {
  1557. int ret = DISPENSER_INIT_SUCC;
  1558. int isContinue = 1;
  1559. struct timeb NowTime;
  1560. while (isContinue) {
  1561. usleep(128);
  1562. ftime(&NowTime);
  1563. switch (curReg) {
  1564. case REG_MODEL_NAME:
  1565. if (WriteModelName(fd) == PASS) {
  1566. gDoCommGblData.DisConnCount = 0;
  1567. curReg = REG_CONNECTOR_ID;
  1568. } else {
  1569. sleep(1);
  1570. }
  1571. break;
  1572. case REG_CONNECTOR_ID:
  1573. if (readConnectorID(fd) == PASS) {
  1574. gDoCommGblData.DisConnCount = 0;
  1575. curReg = REG_REPORT_CSU_VERSION;
  1576. } else {
  1577. sleep(1);
  1578. }
  1579. break;
  1580. case REG_POWER_CABINET_STATUS:
  1581. if (DiffTimeb(gRegTimeUp[plugNum][curReg], NowTime) > LOOP_RETRY_TIME ||
  1582. DiffTimeb(gRegTimeUp[plugNum][curReg], NowTime) < 0) {
  1583. readPowerCabinetStatus(fd, gunID);
  1584. ftime(&gRegTimeUp[plugNum][curReg]);
  1585. }
  1586. curReg = REG_DISPENSER_STATUS;
  1587. break;
  1588. case REG_DISPENSER_STATUS:
  1589. if (DiffTimeb(gRegTimeUp[plugNum][curReg], NowTime) > LOOP_RETRY_TIME ||
  1590. DiffTimeb(gRegTimeUp[plugNum][curReg], NowTime) < 0) {
  1591. writeDispenserStatus(fd, gunID);
  1592. ftime(&gRegTimeUp[plugNum][curReg]);
  1593. }
  1594. curReg = REG_PLUG_IN_STATE;
  1595. break;
  1596. //case REG_CHARGING_CAP:
  1597. // readChargingCapability(fd, plugNum, gunID);
  1598. // break;
  1599. //case REG_CHARGING_TARGET:
  1600. // writeChargingTarget(fd, plugNum, gunID);
  1601. // break;
  1602. //case REG_SOFTWARE_UPDATE:
  1603. // if (ChargingData[plugNum]->SystemStatus == S_IDLE) {
  1604. // if (DiffTimeb(gRegTimeUp[plugNum][curReg], NowTime) > L OOP_RETRY_TIME||
  1605. //DiffTimeb(gRegTimeUp[plugNum][curReg], NowTime) < 0 * 1) {
  1606. // readSoftwareUpdate(fd);
  1607. // ftime(&gRegTimeUp[plugNum][curReg]);
  1608. // }
  1609. // while (pSysInfo->FirmwareUpdate == YES) {
  1610. // ftime(&NowTime);
  1611. // if (DiffTimeb(gRegTimeUp[plugNum][curReg], NowT ime) > LOOP_RETRY_TIME||
  1612. //DiffTimeb(gRegTimeUp[plugNum][curReg], NowTime) < 0) {
  1613. // readSoftwareUpdate(fd);
  1614. // }
  1615. // usleep(128);
  1616. // }
  1617. // }
  1618. // isContinue = 0;
  1619. // break;
  1620. case REG_PLUG_IN_STATE:
  1621. if (DiffTimeb(gRegTimeUp[plugNum][curReg], NowTime) > LOOP_RETRY_TIME ||
  1622. DiffTimeb(gRegTimeUp[plugNum][curReg], NowTime) < 0) {
  1623. writePlugInStatus(fd, plugNum, gunID);
  1624. ftime(&gRegTimeUp[plugNum][curReg]);
  1625. }
  1626. curReg = REG_CONNECTOR_STATE;
  1627. break;
  1628. case REG_CONNECTOR_STATE:
  1629. if (DiffTimeb(gRegTimeUp[plugNum][curReg], NowTime) > LOOP_RETRY_TIME ||
  1630. DiffTimeb(gRegTimeUp[plugNum][curReg], NowTime) < 0) {
  1631. writeConnectorState(fd, plugNum, gunID);
  1632. ftime(&gRegTimeUp[plugNum][curReg]);
  1633. }
  1634. //check misc command from power cabinet
  1635. if (gDoCommGblData.MiscCmd != 0) {
  1636. if (gDoCommGblData.MiscCmd != REG_MISC_CONTROL) {
  1637. log_error("misc command failed = %x", gDoCommGblData.MiscCmd);
  1638. }
  1639. curReg = gDoCommGblData.MiscCmd;
  1640. } else {
  1641. curReg = REG_QRCODE_URL_INFO;
  1642. }
  1643. break;
  1644. //case REG_USER_ID:
  1645. // writeUserID(fd, gunID, uint8_t *pUserID);
  1646. // break;
  1647. //case REG_CHARGING_PERMISSION:
  1648. // readChargePermission(fd, gunID);
  1649. // break;
  1650. case REG_MISC_CONTROL:
  1651. readMiscCommand(fd, gunID);
  1652. isContinue = 0;
  1653. break;
  1654. case REG_REPORT_CSU_VERSION:
  1655. case REG_REPORT_OTHER_VERSION:
  1656. if (gDoCommGblData.MiscCmd != 0) {
  1657. writeCsuModuleVersion(fd);
  1658. writeOtherModuleVersion(fd);
  1659. clearMiscCommand();
  1660. isContinue = 0;
  1661. } else {
  1662. //power up write dispenser version and get power cabinet system ID
  1663. if ((writeCsuModuleVersion(fd) == PASS) &&
  1664. (writeOtherModuleVersion(fd) == PASS) &&
  1665. (readQRcodeURLAndSystemDate(fd) == PASS)
  1666. ) {
  1667. //gDoCommGblData.DisConnCount = 0;
  1668. isContinue = 0;
  1669. }
  1670. }
  1671. break;
  1672. //case REG_PRESENT_CHARGING_INFO:
  1673. // break;
  1674. case REG_QRCODE_URL_INFO:
  1675. if (gunID == 1) {
  1676. if (DiffTimeb(gRegTimeUp[plugNum][curReg], NowTime) > (LOOP_RETRY_TIME * 10) ||
  1677. DiffTimeb(gRegTimeUp[plugNum][curReg], NowTime) < 0
  1678. ) {
  1679. readQRcodeURLAndSystemDate(fd);
  1680. ftime(&gRegTimeUp[plugNum][curReg]);
  1681. }
  1682. }
  1683. isContinue = 0;
  1684. //curReg = REG_SOFTWARE_UPDATE;
  1685. break;
  1686. //case REG_WAIT_PLUG_IT_STATE:
  1687. // if (DiffTimeb(gRegTimeUp[plugNum][curReg], NowTime) > (LOOP_RETRY_TIME / 10)) {
  1688. // writeWaitPlugItState(fd, gunID);
  1689. // ftime(&gRegTimeUp[plugNum][curReg]);
  1690. // }
  1691. // isContinue = 0;
  1692. // break;
  1693. default:
  1694. log_error("error curReg = %x", curReg);
  1695. gDoCommGblData.MiscCmd = 0;
  1696. gDoCommGblData.DisConnCount = CHECK_NETWORK_FAIL_COUNT;
  1697. isContinue = 0;
  1698. break;
  1699. }
  1700. if (gDoCommGblData.DisConnCount >= CHECK_NETWORK_FAIL_COUNT) {
  1701. return DISPENSER_SOCKET_RECONN;
  1702. }
  1703. }//for
  1704. return ret;
  1705. }
  1706. //static bool isDetectPlugin()
  1707. //{
  1708. // if (pSysInfo->WaitForPlugit == YES) {
  1709. // return YES;
  1710. // }
  1711. //
  1712. // return NO;
  1713. //}
  1714. static void systemStatusProcess(int fd, uint8_t totalGun, uint8_t plugNum, uint8_t gunID)
  1715. {
  1716. uint8_t i = 0;
  1717. struct timeb AuthNowTime = {0};
  1718. pDcChargingInfo = (struct ChargingInfoData *)GetDcChargingInfoData(plugNum);
  1719. // Get Power Cabinet Version information
  1720. if (pSysConfig->ShowInformation == YES && pSysInfo->SystemPage == _LCM_VIEW) {
  1721. // Get Power Cabinet DCM Version
  1722. ftime(&AuthNowTime);
  1723. if (DiffTimeb(gRegTimeUp[plugNum][REG_GET_CABINET_DCM_VERSION], AuthNowTime) > LOOP_RETRY_TIME ||
  1724. DiffTimeb(gRegTimeUp[plugNum][REG_GET_CABINET_DCM_VERSION], AuthNowTime) < 0
  1725. ) {
  1726. readCabinetDCMVersion(fd, gunID);
  1727. ftime(&gRegTimeUp[plugNum][REG_GET_CABINET_DCM_VERSION]);
  1728. }
  1729. // Get Power Cabinet Relay and Fan Version
  1730. ftime(&AuthNowTime);
  1731. if (DiffTimeb(gRegTimeUp[plugNum][REG_GET_CABINET_OTHER_VERSION], AuthNowTime) > LOOP_RETRY_TIME ||
  1732. DiffTimeb(gRegTimeUp[plugNum][REG_GET_CABINET_OTHER_VERSION], AuthNowTime) < 0
  1733. ) {
  1734. readCabinetOtherVersion(fd, gunID);
  1735. ftime(&gRegTimeUp[plugNum][REG_GET_CABINET_OTHER_VERSION]);
  1736. }
  1737. // Get Power Cabinet PSU Numbers
  1738. ftime(&AuthNowTime);
  1739. if (DiffTimeb(gRegTimeUp[plugNum][REG_GET_CABINET_PSU_NUMBER], AuthNowTime) > LOOP_RETRY_TIME ||
  1740. DiffTimeb(gRegTimeUp[plugNum][REG_GET_CABINET_PSU_NUMBER], AuthNowTime) < 0
  1741. ) {
  1742. readCabinetPSUNumber(fd, gunID);
  1743. ftime(&gRegTimeUp[plugNum][REG_GET_CABINET_PSU_NUMBER]);
  1744. }
  1745. // Get Power Cabinet each PSU Version
  1746. if (ShmDcCommonData->PSU_Number > 0) {
  1747. for(i = 0 ; i < ShmDcCommonData->PSU_Number ; i++) {
  1748. if (DiffTimeb(gRegTimeUp[plugNum][REG_GET_CABINET_PSU_VERSION], AuthNowTime) > LOOP_RETRY_TIME ||
  1749. DiffTimeb(gRegTimeUp[plugNum][REG_GET_CABINET_PSU_VERSION], AuthNowTime) < 0
  1750. ) {
  1751. readCabinetEachPSUVersion(fd, gunID,i);
  1752. ftime(&gRegTimeUp[plugNum][REG_GET_CABINET_PSU_VERSION]);
  1753. }
  1754. }
  1755. }
  1756. }
  1757. switch (pDcChargingInfo->SystemStatus) {
  1758. case S_IDLE:
  1759. case S_RESERVATION:
  1760. case S_MAINTAIN:
  1761. case S_ALARM:
  1762. case S_AUTHORIZING:
  1763. checkAuthorProcess(fd, plugNum);
  1764. //authorization complete, write wait plug it state
  1765. if (ShmSelectGunInfo->PricesInfo[plugNum].Balance == FAIL_BALANCE_PRICES) {
  1766. break;
  1767. }
  1768. ftime(&AuthNowTime);
  1769. if (DiffTimeb(gRegTimeUp[plugNum][REG_WAIT_PLUG_IT_STATE], AuthNowTime) > (LOOP_RETRY_TIME / 10) ||
  1770. DiffTimeb(gRegTimeUp[plugNum][REG_WAIT_PLUG_IT_STATE], AuthNowTime) < 0
  1771. ) {
  1772. writeWaitPlugItState(fd, gunID);
  1773. ftime(&gRegTimeUp[plugNum][REG_WAIT_PLUG_IT_STATE]);
  1774. }
  1775. /*
  1776. ftime(&AuthNowTime);
  1777. if (DiffTimeb(gRegTimeUp[plugNum][REG_MISC_CONTROL], AuthNowTime) > (LOOP_RETRY_TIME / 10) ||
  1778. DiffTimeb(gRegTimeUp[plugNum][REG_MISC_CONTROL], AuthNowTime) < 0
  1779. ) {
  1780. readMiscCommand(fd, gunID);
  1781. ftime(&gRegTimeUp[plugNum][REG_MISC_CONTROL]);
  1782. }
  1783. */
  1784. ftime(&AuthNowTime);
  1785. if (DiffTimeb(gRegTimeUp[plugNum][REG_CHARGING_CAP], AuthNowTime) > LOOP_RETRY_TIME ||
  1786. DiffTimeb(gRegTimeUp[plugNum][REG_CHARGING_CAP], AuthNowTime) < 0
  1787. ) {
  1788. readChargingCapability(fd, gunID);
  1789. if (pDcChargingInfo->PantographFlag)
  1790. writeGroundFaultDetection(fd, 0, gunID);
  1791. ftime(&gRegTimeUp[plugNum][REG_CHARGING_CAP]);
  1792. }
  1793. break;
  1794. case S_PREPARNING: //get permission
  1795. ftime(&AuthNowTime);
  1796. if (DiffTimeb(gRegTimeUp[plugNum][REG_CONNECTOR_STATE], AuthNowTime) > LOOP_RETRY_TIME ||
  1797. DiffTimeb(gRegTimeUp[plugNum][REG_CONNECTOR_STATE], AuthNowTime) < 0
  1798. ) {
  1799. writeConnectorState(fd, plugNum, gunID);
  1800. ftime(&gRegTimeUp[plugNum][REG_CONNECTOR_STATE]);
  1801. }
  1802. ftime(&AuthNowTime);
  1803. if (DiffTimeb(gRegTimeUp[plugNum][REG_CHARGING_PERMISSION], AuthNowTime) > LOOP_RETRY_TIME ||
  1804. DiffTimeb(gRegTimeUp[plugNum][REG_CHARGING_PERMISSION], AuthNowTime) < 0
  1805. ) {
  1806. if (readChargePermission(fd, gunID) && readChargingCapability(fd, gunID)) {
  1807. for (i = 0; i < totalGun; i++) {
  1808. pDcChargingInfo = (struct ChargingInfoData *)GetDcChargingInfoData(i);
  1809. ShmPsuData->SystemAvailablePower += pDcChargingInfo->AvailableChargingPower;
  1810. }
  1811. ShmPsuData->SystemPresentPsuQuantity = (ShmPsuData->SystemAvailablePower / 30);
  1812. }
  1813. ftime(&gRegTimeUp[plugNum][REG_CHARGING_PERMISSION]);
  1814. }
  1815. ftime(&AuthNowTime);
  1816. if (DiffTimeb(gRegTimeUp[plugNum][REG_PRESENT_CHARGING_INFO], AuthNowTime) > LOOP_RETRY_TIME ||
  1817. DiffTimeb(gRegTimeUp[plugNum][REG_PRESENT_CHARGING_INFO], AuthNowTime) < 0
  1818. ) {
  1819. writePresentChargingInfo(fd, plugNum, gunID);
  1820. if (pDcChargingInfo->PantographFlag)
  1821. writeGroundFaultDetection(fd, 0, gunID);
  1822. ftime(&gRegTimeUp[plugNum][REG_PRESENT_CHARGING_INFO]);
  1823. }
  1824. break;
  1825. case S_PREPARING_FOR_EV://wait connector lock
  1826. ftime(&AuthNowTime);
  1827. if (DiffTimeb(gRegTimeUp[plugNum][REG_CHARGING_CAP], AuthNowTime) > LOOP_RETRY_TIME ||
  1828. DiffTimeb(gRegTimeUp[plugNum][REG_CHARGING_CAP], AuthNowTime) < 0
  1829. ) {
  1830. readChargingCapability(fd, gunID);
  1831. ftime(&gRegTimeUp[plugNum][REG_CHARGING_CAP]);
  1832. }
  1833. ftime(&AuthNowTime);
  1834. if (DiffTimeb(gRegTimeUp[plugNum][REG_CHARGING_PERMISSION], AuthNowTime) > LOOP_RETRY_TIME ||
  1835. DiffTimeb(gRegTimeUp[plugNum][REG_CHARGING_PERMISSION], AuthNowTime) < 0
  1836. ) {
  1837. if (readChargePermission(fd, gunID) == 0) {
  1838. log_info("S_PREPARING_FOR_EV Stop charging by power cabinet's permission = %d, %d",
  1839. plugNum,
  1840. REG_CHARGING_PERMISSION);
  1841. pDcChargingInfo->StopChargeFlag = POWER_CABINET_STOP_CHARGING;
  1842. }
  1843. ftime(&gRegTimeUp[plugNum][REG_CHARGING_PERMISSION]);
  1844. }
  1845. ftime(&AuthNowTime);
  1846. if (DiffTimeb(gRegTimeUp[plugNum][REG_PRESENT_CHARGING_INFO], AuthNowTime) > LOOP_RETRY_TIME ||
  1847. DiffTimeb(gRegTimeUp[plugNum][REG_PRESENT_CHARGING_INFO], AuthNowTime) < 0
  1848. ) {
  1849. writePresentChargingInfo(fd, plugNum, gunID);
  1850. if (pDcChargingInfo->PantographFlag)
  1851. writeGroundFaultDetection(fd, 0, gunID);
  1852. ftime(&gRegTimeUp[plugNum][REG_PRESENT_CHARGING_INFO]);
  1853. }
  1854. break;
  1855. case S_PREPARING_FOR_EVSE: //insaulation test
  1856. case S_CCS_PRECHARGE_ST0:
  1857. case S_CCS_PRECHARGE_ST1:
  1858. writeChargingTarget(fd, plugNum, gunID);
  1859. if (pDcChargingInfo->PantographFlag)
  1860. writeGroundFaultDetection(fd, 1, gunID);
  1861. ftime(&AuthNowTime);
  1862. if (DiffTimeb(gRegTimeUp[plugNum][REG_CHARGING_CAP], AuthNowTime) > LOOP_RETRY_TIME ||
  1863. DiffTimeb(gRegTimeUp[plugNum][REG_CHARGING_CAP], AuthNowTime) < 0
  1864. ) {
  1865. readChargingCapability(fd, gunID);
  1866. ftime(&gRegTimeUp[plugNum][REG_CHARGING_CAP]);
  1867. }
  1868. ftime(&AuthNowTime);
  1869. if (DiffTimeb(gRegTimeUp[plugNum][REG_CHARGING_PERMISSION], AuthNowTime) > LOOP_RETRY_TIME ||
  1870. DiffTimeb(gRegTimeUp[plugNum][REG_CHARGING_PERMISSION], AuthNowTime) < 0
  1871. ) {
  1872. if (readChargePermission(fd, gunID) == 0) {
  1873. log_info("S_PREPARING_FOR_EVSE Stop charging by power cabinet's permission = %d, %d",
  1874. plugNum,
  1875. REG_CHARGING_PERMISSION);
  1876. pDcChargingInfo->StopChargeFlag = POWER_CABINET_STOP_CHARGING;
  1877. }
  1878. ftime(&gRegTimeUp[plugNum][REG_CHARGING_PERMISSION]);
  1879. }
  1880. ftime(&AuthNowTime);
  1881. if (DiffTimeb(gRegTimeUp[plugNum][REG_PRESENT_CHARGING_INFO], AuthNowTime) > LOOP_RETRY_TIME ||
  1882. DiffTimeb(gRegTimeUp[plugNum][REG_PRESENT_CHARGING_INFO], AuthNowTime) < 0
  1883. ) {
  1884. writePresentChargingInfo(fd, plugNum, gunID);
  1885. ftime(&gRegTimeUp[plugNum][REG_PRESENT_CHARGING_INFO]);
  1886. }
  1887. break;
  1888. case S_CHARGING: //charging
  1889. case S_TERMINATING:
  1890. if(pDcChargingInfo->Type == _Type_GB || pDcChargingInfo->Type == _Type_Chademo)
  1891. {
  1892. if (pDcChargingInfo->PantographFlag)
  1893. writeGroundFaultDetection(fd, 0, gunID);
  1894. }
  1895. else
  1896. {
  1897. if (pDcChargingInfo->PantographFlag)
  1898. writeGroundFaultDetection(fd, 1, gunID);
  1899. }
  1900. ftime(&AuthNowTime);
  1901. if (DiffTimeb(gRegTimeUp[plugNum][REG_CHARGING_CAP], AuthNowTime) > LOOP_RETRY_TIME ||
  1902. DiffTimeb(gRegTimeUp[plugNum][REG_CHARGING_CAP], AuthNowTime) < 0
  1903. ) {
  1904. readChargingCapability(fd, gunID);
  1905. ftime(&gRegTimeUp[plugNum][REG_CHARGING_CAP]);
  1906. }
  1907. ftime(&AuthNowTime);
  1908. if (pDcChargingInfo->SystemStatus != S_TERMINATING) {
  1909. if (DiffTimeb(gRegTimeUp[plugNum][REG_CHARGING_PERMISSION], AuthNowTime) > LOOP_RETRY_TIME ||
  1910. DiffTimeb(gRegTimeUp[plugNum][REG_CHARGING_PERMISSION], AuthNowTime) < 0
  1911. ) {
  1912. if (readChargePermission(fd, gunID) == 0) {
  1913. log_info("Stop charging by power cabinet's permission = %d, %d",
  1914. plugNum,
  1915. REG_CHARGING_PERMISSION);
  1916. pDcChargingInfo->StopChargeFlag = POWER_CABINET_STOP_CHARGING;
  1917. //printf("%d StopChargeFlag = %d\n", plugNum, pDcChargingInfo->StopChargeFlag);
  1918. }
  1919. ftime(&gRegTimeUp[plugNum][REG_CHARGING_PERMISSION]);
  1920. }
  1921. }
  1922. writeChargingTarget(fd, plugNum, gunID);
  1923. ftime(&AuthNowTime);
  1924. if (DiffTimeb(gRegTimeUp[plugNum][REG_PRESENT_CHARGING_INFO], AuthNowTime) > LOOP_RETRY_TIME ||
  1925. DiffTimeb(gRegTimeUp[plugNum][REG_PRESENT_CHARGING_INFO], AuthNowTime) < 0
  1926. ) {
  1927. writePresentChargingInfo(fd, plugNum, gunID);
  1928. ftime(&gRegTimeUp[plugNum][REG_PRESENT_CHARGING_INFO]);
  1929. }
  1930. break;
  1931. case S_COMPLETE:
  1932. ftime(&AuthNowTime);
  1933. if (DiffTimeb(gRegTimeUp[plugNum][REG_CHARGING_CAP], AuthNowTime) > LOOP_RETRY_TIME ||
  1934. DiffTimeb(gRegTimeUp[plugNum][REG_CHARGING_CAP], AuthNowTime) < 0
  1935. ) {
  1936. readChargingCapability(fd, gunID);
  1937. if (pDcChargingInfo->PantographFlag)
  1938. writeGroundFaultDetection(fd, 0, gunID);
  1939. ftime(&gRegTimeUp[plugNum][REG_CHARGING_CAP]);
  1940. }
  1941. break;
  1942. default:
  1943. break;
  1944. }
  1945. }
  1946. static int networkCreatePorcess(void)
  1947. {
  1948. int fd = 0;
  1949. uint8_t discount = 0;
  1950. while (pSysInfo->SelfTestSeq != _STEST_COMPLETE) {
  1951. usleep(128);
  1952. }
  1953. setTcpStatus(ABNORMAL);
  1954. /**************** Check Network **********/
  1955. //log_info("Check Dispenser Network Information");
  1956. while (pSysInfo->SelfTestSeq != _STEST_COMPLETE) {
  1957. usleep(125);
  1958. }
  1959. while (!CheckNetworkStatus()) {
  1960. calDisconnectCount(&gDoCommGblData.DisConnCount, CHECK_NETWORK_FAIL_COUNT);
  1961. if( gDoCommGblData.DisConnCount != discount ) {
  1962. discount = gDoCommGblData.DisConnCount;
  1963. log_error("disconnect count = % d", gDoCommGblData.DisConnCount);
  1964. }
  1965. }
  1966. log_info("Dispenser Network Information checked: IP = % s Netmask = % s, Gateway = % s",
  1967. pSysConfig->Eth0Interface.EthIpAddress,
  1968. pSysConfig->Eth0Interface.EthSubmaskAddress,
  1969. pSysConfig->Eth0Interface.EthGatewayAddress);
  1970. gDoCommGblData.DisConnCount = 0;
  1971. discount = 0;
  1972. /**************** Power cabinet connection **********/
  1973. log_info("Connect to Power Cabinet");
  1974. while ((fd = doCommConnToServer()) <= 0) {
  1975. calDisconnectCount(&gDoCommGblData.DisConnCount, CONNECT_SERVER_FAIL_COUNT);
  1976. if( gDoCommGblData.DisConnCount != discount && fd == -1) {
  1977. discount = gDoCommGblData.DisConnCount;
  1978. log_error("disconnect count = %d, fd = %d", gDoCommGblData.DisConnCount, fd);
  1979. }
  1980. }
  1981. log_info("Power cabinet connected(fd = % d): IP = % s Netmask = % s, Gateway = % s",
  1982. fd,
  1983. pSysConfig->Eth0Interface.EthIpAddress,
  1984. pSysConfig->Eth0Interface.EthSubmaskAddress,
  1985. pSysConfig->Eth0Interface.EthGatewayAddress);
  1986. gDoCommGblData.DisConnCount = 0;
  1987. destroySelectGun(DESTROY_ALL_SEL);
  1988. sleep(3);
  1989. setTcpStatus(NORMAL);
  1990. return fd;
  1991. }
  1992. int main(int argc, char *argv[])
  1993. {
  1994. uint8_t plugNum = 0, gunID = 0;
  1995. uint8_t index = 0;
  1996. uint8_t totalConnCount = 0;
  1997. uint8_t initDone = DISPENER_INIT_FAIL;
  1998. char tmpbuf[256] = {0};
  1999. int fd = 0;
  2000. int isContinue = 1;
  2001. InitSocketSigPipe();
  2002. /**************** Initialization **********/
  2003. if (CreateAllCsuShareMemory() == FAIL) {
  2004. log_error("create share memory error");
  2005. return FAIL;
  2006. }
  2007. MappingGunChargingInfo("DoComm Task");
  2008. pSysConfig = (struct SysConfigData *)GetShmSysConfigData();
  2009. pSysInfo = (struct SysInfoData *)GetShmSysInfoData();
  2010. pSysWarning = (struct WARNING_CODE_INFO *)GetShmSysWarningInfo();
  2011. pAlarmCode = (struct AlarmCodeData *)GetShmAlarmCodeData();
  2012. ShmPsuData = (struct PsuData *)GetShmPsuData();
  2013. ShmPrimaryMcuData = (struct PrimaryMcuData *)GetShmPrimaryMcuData();
  2014. ShmOCPP16Data = (struct OCPP16Data *)GetShmOCPP16Data();
  2015. ShmSelectGunInfo = (SelectGunInfo *)GetShmSelectGunInfo();
  2016. ShmDcCommonData = (DcCommonInfo *)GetShmDcCommonData();
  2017. totalConnCount = pSysConfig->TotalConnectorCount;
  2018. for (index = 0; index < totalConnCount; index++) {
  2019. clearPricesInfo(index);
  2020. }
  2021. //initial trigger message timer
  2022. for (index = 0; index < MAX_REGISTER_NUM; index++) {
  2023. ftime(&gRegTimeUp[0][index]);
  2024. usleep(128);
  2025. ftime(&gRegTimeUp[1][index]);
  2026. usleep(50000);
  2027. }
  2028. setTcpStatus(NORMAL);
  2029. while (isContinue) {
  2030. if (initDone == DISPENER_INIT_FAIL) {
  2031. fd = networkCreatePorcess();
  2032. initDone = messageTrigger(fd, 0, 0, REG_MODEL_NAME); //first trigger model name and connector id
  2033. } else {
  2034. plugNum = 0;
  2035. gunID = 0;
  2036. for (plugNum = 0; plugNum < totalConnCount; plugNum++) {
  2037. //checkAuthorProcess(fd, plugNum);
  2038. //plugNum : setup chargingData value for bottom layer
  2039. //gunID : connector Id from power cabinet, 1 = left gun, 2 = right gun,
  2040. gunID = gDoCommGblData.ConnectorID[plugNum];
  2041. systemStatusProcess(fd, totalConnCount, plugNum, gunID);
  2042. initDone = messageTrigger(fd,
  2043. plugNum,
  2044. gunID,
  2045. REG_POWER_CABINET_STATUS);
  2046. if (initDone == DISPENSER_SOCKET_RECONN) {
  2047. break;
  2048. }
  2049. initDone = messageTrigger(fd,
  2050. plugNum,
  2051. gunID,
  2052. REG_MISC_CONTROL);
  2053. if (initDone == DISPENSER_SOCKET_RECONN) {
  2054. break;
  2055. }
  2056. }
  2057. }
  2058. if (initDone != DISPENSER_SOCKET_RECONN) {
  2059. //update dispenser firmware
  2060. updateFirmwareProcess(fd, gDoCommGblData.ConnectorID[0], totalConnCount);
  2061. usleep(100000);
  2062. continue;
  2063. }
  2064. //Ethernet error recovery handle
  2065. log_info("Disconnected from power cabinet...re-connecting");
  2066. setTcpStatus(ABNORMAL);
  2067. if (pSysConfig->Eth0Interface.EthDhcpClient == 0) {
  2068. sprintf(tmpbuf,
  2069. "/sbin/udhcpc -i eth0 -x hostname:CSU3_%s -s /root/dhcp_script/eth0.script > /dev/null &",
  2070. pSysConfig->SystemId);
  2071. system(tmpbuf);
  2072. }
  2073. gDoCommGblData.DisConnCount = 0;
  2074. gDoCommGblData.SeqNum = 0;
  2075. closeSocket(fd);
  2076. fd = 0;
  2077. initDone = DISPENER_INIT_FAIL;
  2078. }
  2079. }