DoComm.c 75 KB

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