DoComm.c 75 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317
  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. }
  716. }
  717. static void removeFaultCodeToBuf(uint8_t *Code)
  718. {
  719. uint8_t find = 0x01;
  720. uint8_t i = 0;
  721. char _code[7] = {0};
  722. sprintf(_code, "%s", Code);
  723. // 把相關的錯誤碼一次移除,避免重複顯示
  724. while (find) {
  725. usleep(128);
  726. find = 0x00;
  727. for (i = 0; i < pSysWarning->WarningCount; i++) {
  728. usleep(128);
  729. if (find == 0x00) {
  730. if (memcmp(&pSysWarning->WarningCode[i][0], _code, 7) == 0) {
  731. find = 0x01;
  732. }
  733. } else {
  734. memcpy(&pSysWarning->WarningCode[i - 1][0],
  735. &pSysWarning->WarningCode[i][0], 7);
  736. }
  737. }
  738. if (find) {
  739. pSysWarning->WarningCount--;
  740. }
  741. }
  742. }
  743. bool CompareArrayIsZero(uint8_t *array, unsigned int array_size)
  744. {
  745. uint8_t i;
  746. if (array != NULL) {
  747. for (i = 0; i < array_size; i++) {
  748. if (array[i] != 0) {
  749. return false;
  750. }
  751. }
  752. }
  753. return true;
  754. }
  755. static int powerCabinetStatusProcess(uint8_t dataLen, uint8_t *data)
  756. {
  757. uint8_t ret = 0;
  758. uint8_t i = 0;
  759. uint8_t count = 0;
  760. uint8_t EventCodeTmp[7] = {0};
  761. uint8_t StatusArray[MAX_REGISTER_NUM][WARNING_CODE_SIZE] = {0};
  762. uint8_t remaindLen = 0;
  763. //uint8_t statusCodeError = 0;
  764. if (dataLen > 0) {
  765. Hexdump((uint8_t *)data, dataLen);
  766. remaindLen = dataLen % WARNING_CODE_SIZE;
  767. if (remaindLen != 0) {
  768. log_info("fail status code length = %d", dataLen);
  769. dataLen -= remaindLen;
  770. }
  771. if (dataLen < WARNING_CODE_SIZE) {
  772. log_error("fail status code length = %d", dataLen);
  773. Hexdump(data, dataLen);
  774. if (pSysWarning->WarningCount > 0) {
  775. for (i = 0; i < pSysWarning->WarningCount; i++) {
  776. usleep(128);
  777. if (pSysWarning->WarningCode[i][1] >= '3' || //from backend or power cabinet
  778. (pSysWarning->WarningCode[i][0] >= 'B' &&
  779. pSysWarning->WarningCode[i][1] >= '4')) {
  780. memset(EventCodeTmp, 0, sizeof(EventCodeTmp));
  781. memcpy(EventCodeTmp,
  782. pSysWarning->WarningCode[i],
  783. sizeof(EventCodeTmp));
  784. removeFaultCodeToBuf(EventCodeTmp);
  785. }
  786. }
  787. }
  788. return FAIL;
  789. }
  790. for (count = 0; count < dataLen; count += WARNING_CODE_SIZE) {
  791. usleep(128);
  792. if (strncmp((char *)&data[count + 1], "1", 1) != 0 &&
  793. strncmp((char *)&data[count + 1], "2", 1) != 0 &&
  794. strncmp((char *)&data[count + 1], "3", 1) != 0 &&
  795. (strncmp((char *)&data[count], "B", 1) != 0 &&
  796. strncmp((char *)&data[count + 1], "4", 1) != 0)
  797. ) {
  798. log_error("error status code = %s", (char *)&data[count]);
  799. continue;
  800. }
  801. // misc command status code handle
  802. if (strncmp((char *)&data[count], MISC_ST_MISC_CMD, WARNING_CODE_SIZE) == 0) {
  803. gDoCommGblData.MiscCmd = REG_MISC_CONTROL;
  804. memset((char *)&data[count], 0, WARNING_CODE_SIZE);
  805. continue;
  806. } else if (strncmp((char *)&data[count], MISC_ST_VERSION, WARNING_CODE_SIZE) == 0) {
  807. gDoCommGblData.MiscCmd = REG_REPORT_CSU_VERSION;
  808. memset((char *)&data[count], 0, WARNING_CODE_SIZE);
  809. continue;
  810. }
  811. if (CompareArrayIsZero((uint8_t *)&data[count], WARNING_CODE_SIZE) == true) {
  812. memset(EventCodeTmp, 0, sizeof(EventCodeTmp));
  813. memcpy(EventCodeTmp, (char *)&data[count], sizeof(EventCodeTmp));
  814. log_error("error status = %s", EventCodeTmp);
  815. continue;
  816. }
  817. strncpy((char *)&StatusArray[count / WARNING_CODE_SIZE], (char *)&data[count], WARNING_CODE_SIZE);
  818. ret = 0;
  819. for (i = 0; i < pSysWarning->WarningCount; i++) {
  820. usleep(128);
  821. if (memcmp(&pSysWarning->WarningCode[i][0],
  822. StatusArray[count / WARNING_CODE_SIZE],
  823. WARNING_CODE_SIZE) == 0) {
  824. ret = 1;
  825. break;
  826. }
  827. }
  828. if (ret == 0) {
  829. addFaultCodeToBuf(StatusArray[count / WARNING_CODE_SIZE]);
  830. }
  831. //Rtn=StatusCodeProcessing(StatusArray[count/6]);
  832. }
  833. } else {
  834. if (CompareArrayIsZero(data, WARNING_CODE_SIZE) == false) {
  835. log_error("power cabinet status code data length is zero, but have data");
  836. Hexdump((uint8_t *)data, WARNING_CODE_SIZE);
  837. }
  838. }
  839. for (i = 0; i < pSysWarning->WarningCount; i++) {
  840. usleep(128);
  841. if (pSysWarning->WarningCode[i][1] >= '3' || //from backend or power cabinet 0x33
  842. (pSysWarning->WarningCode[i][0] >= 'B' &&
  843. pSysWarning->WarningCode[i][1] >= '4')) {
  844. ret = 0;
  845. for (count = 0; count < (dataLen / WARNING_CODE_SIZE); count++) {
  846. usleep(128);
  847. if (memcmp(&pSysWarning->WarningCode[i][0],
  848. StatusArray[count],
  849. WARNING_CODE_SIZE) == 0) {
  850. ret = 1;
  851. break;
  852. }
  853. }
  854. if (ret == 0) {
  855. memset(EventCodeTmp, 0, sizeof(EventCodeTmp));
  856. memcpy(EventCodeTmp,
  857. pSysWarning->WarningCode[i],
  858. sizeof(EventCodeTmp));
  859. removeFaultCodeToBuf(EventCodeTmp);
  860. }
  861. }
  862. }
  863. return PASS;
  864. }
  865. static int responsePackeHandle(int fd, uint8_t *pResult, uint8_t plugNum, uint8_t reg)
  866. {
  867. int ret = PASS;
  868. uint8_t rawDataLen = 0;
  869. CsuResultPkt *pCsuResult = (CsuResultPkt *)pResult;
  870. SoftwareUpdInfo *pSoftwareUpd = NULL;
  871. PcPsuOutput *pPcPsuOutput = NULL;
  872. //Hexdump((uint8_t *)pCsuResult, sizeof(CmdHead) + pCsuResult->Head.DataLen);
  873. if (compareOpcode(pCsuResult->Head.OP) == FAIL ||
  874. compareResult(pCsuResult->Data.Result) == FAIL ||
  875. compareRegister(pCsuResult->Data.Register, reg) == FAIL) {
  876. if (reg != REG_CHARGING_PERMISSION) {
  877. return FAIL;
  878. }
  879. }
  880. rawDataLen = (pCsuResult->Head.DataLen - 2); //2 byte = register and result byte
  881. switch (pCsuResult->Data.Register) {
  882. case REG_MODEL_NAME:
  883. break;
  884. case REG_CONNECTOR_ID:
  885. gDoCommGblData.ConnectorID[0] = pCsuResult->Data.Data[0];
  886. gDoCommGblData.ConnectorID[1] = pCsuResult->Data.Data[1];
  887. log_info("OK (Left connector ID = %d, Right connector ID = %d)",
  888. pCsuResult->Data.Data[0],
  889. pCsuResult->Data.Data[1]);
  890. break;
  891. case REG_POWER_CABINET_STATUS:
  892. ret = powerCabinetStatusProcess(rawDataLen, pCsuResult->Data.Data);
  893. break;
  894. case REG_DISPENSER_STATUS:
  895. memset(&gPreSysWarningInfo, 0, sizeof(struct WARNING_CODE_INFO));
  896. memcpy((uint8_t *)&gPreSysWarningInfo,
  897. pSysWarning,
  898. sizeof(struct WARNING_CODE_INFO));
  899. break;
  900. case REG_CHARGING_CAP:
  901. chargingcapabilityHandle(pCsuResult->Data.Data, plugNum);
  902. break;
  903. case REG_CHARGING_TARGET:
  904. break;
  905. case REG_SOFTWARE_UPDATE:
  906. pSoftwareUpd = (SoftwareUpdInfo *)&pCsuResult->Data.Data[0];
  907. if ((strcmp((char *)pSoftwareUpd->ImgName, "") == 0) ||
  908. (rawDataLen == 0) ||
  909. pSoftwareUpd->UpdateState != 1) {
  910. ret = FAIL;
  911. break;
  912. }
  913. ret = FAIL;
  914. ret = updateFirmwareHandle(pSoftwareUpd->ImgName);
  915. break;
  916. case REG_PLUG_IN_STATE:
  917. break;
  918. case REG_CONNECTOR_STATE:
  919. break;
  920. case REG_USER_ID:
  921. //log_info("USER ID:%s", pCsuResult->Data.Data[0] == 1 ? "Accept" : "Reject" );
  922. //if (pCsuResult->Data.Data[0] <= 0) {
  923. // return FAIL;
  924. //}
  925. if (pCsuResult->Data.Result == COMMAND_RESULT_NG) {
  926. return COMMAND_RESULT_NG;
  927. }
  928. ShmSelectGunInfo->AuthorStateFromCabinet[plugNum] = pCsuResult->Data.Data[0];
  929. return pCsuResult->Data.Data[0];
  930. break;
  931. case REG_CHARGING_PERMISSION:
  932. //log_info("id = %d, result = %s, action = %s",
  933. // pCsuResult->Head.ID,
  934. // pCsuResult->Data.Result == 1 ? "OK" : "NG",
  935. // pCsuResult->Data.Data[0] == 1 ? "Permitted" : "NOT permitted");
  936. if (pCsuResult->Data.Result == COMMAND_RESULT_NG) {
  937. return COMMAND_RESULT_NG;
  938. }
  939. ShmSelectGunInfo->WaitDoCommPermission[plugNum] = pCsuResult->Data.Data[0];
  940. return pCsuResult->Data.Data[0];
  941. break;
  942. case REG_MISC_CONTROL:
  943. miscCommandHandle(rawDataLen, plugNum, pCsuResult->Data.Data);
  944. break;
  945. case REG_REPORT_CSU_VERSION:
  946. case REG_REPORT_OTHER_VERSION:
  947. break;
  948. case REG_PRESENT_CHARGING_INFO:
  949. pPcPsuOutput = (PcPsuOutput *)&pCsuResult->Data.Data[0];
  950. ShmDcCommonData->PcPsuOutput[plugNum].Voltage = ntohs((uint16_t)pPcPsuOutput->Voltage);
  951. ShmDcCommonData->PcPsuOutput[plugNum].Current = ntohs((uint16_t)pPcPsuOutput->Current);
  952. //log_info("%d power cabinet PSU output vol = %f, cur = %f",
  953. // plugNum,
  954. // (float)ShmDcCommonData->PcPsuOutput[plugNum].Voltage,
  955. // (float)ShmDcCommonData->PcPsuOutput[plugNum].Current);
  956. break;
  957. case REG_QRCODE_URL_INFO:
  958. ret = qrCodeUrlInfoHandle(pCsuResult->Data.Data);
  959. break;
  960. case REG_WAIT_PLUG_IT_STATE:
  961. break;
  962. case REG_Ground_Fault_Detection:
  963. if (pCsuResult->Data.Result == COMMAND_RESULT_NG) {
  964. return COMMAND_RESULT_NG;
  965. }
  966. //集電弓relay 不打開才能進入動作
  967. pDcChargingInfo = (struct ChargingInfoData *)GetDcChargingInfoData(plugNum);
  968. if(pDcChargingInfo->PantographFlag == YES)
  969. {
  970. GroundFaultDetection *gfd = (GroundFaultDetection *)&pCsuResult->Data.Data[0];
  971. if(pDcChargingInfo->GroundFaultStatus != gfd->Status)
  972. {
  973. log_info("id = %d, GFD Result = %d",
  974. pCsuResult->Head.ID,
  975. gfd->Status);
  976. }
  977. pDcChargingInfo->GroundFaultStatus = gfd->Status;
  978. }
  979. break;
  980. default:
  981. break;
  982. }
  983. return ret;
  984. }
  985. static int composeSocketData(int fd,
  986. uint8_t id,
  987. uint8_t opCode,
  988. uint8_t reg,
  989. uint8_t dataLen,
  990. uint8_t *data)
  991. {
  992. int ret = PASS;
  993. int size = 0;
  994. int sendPktLen = 0;
  995. uint8_t i = 0;
  996. uint8_t plugNum = 0;
  997. CsuCmdPkt csuCmdPkt = {0};
  998. CsuResultPkt csuResult = {0};
  999. csuCmdPkt.Head.SeqNum = gDoCommGblData.SeqNum++;
  1000. csuCmdPkt.Head.ID = id;
  1001. csuCmdPkt.Head.OP = opCode;
  1002. csuCmdPkt.Head.DataLen = dataLen + 1; //+1 for register byte
  1003. csuCmdPkt.Data.Register = reg;
  1004. if ((data != NULL) && (dataLen > 0)) {
  1005. memcpy(csuCmdPkt.Data.Data, data, dataLen);
  1006. }
  1007. sendPktLen = csuCmdPkt.Head.DataLen + sizeof(csuCmdPkt.Head);
  1008. //Hexdump((uint8_t *)&csuCmdPkt, sendPktLen);
  1009. //send command packet
  1010. if ((size = sendTcpSocket(fd, (uint8_t *)&csuCmdPkt, sendPktLen)) < 0) {
  1011. log_error("TCP socket send packet fail = %d", size);
  1012. gDoCommGblData.DisConnCount++;
  1013. return FAIL;
  1014. }
  1015. //receive result head
  1016. if ((size = recvTcpSocket(fd, (uint8_t *)&csuResult.Head, sizeof(csuResult.Head))) < 0) {
  1017. log_error("TCP socket RX head fail = %d", size);
  1018. gDoCommGblData.DisConnCount++;
  1019. return FAIL;
  1020. }
  1021. //receive result raw data
  1022. if ((size = recvTcpSocket(fd, (uint8_t *)&csuResult.Data, csuResult.Head.DataLen)) < 0) {
  1023. log_error("TCP socket RX data fail = %d", size);
  1024. gDoCommGblData.DisConnCount++;
  1025. return FAIL;
  1026. }
  1027. for (i = 0; i < sizeof(gDoCommGblData.ConnectorID); i++) {
  1028. if (id == gDoCommGblData.ConnectorID[i]) {
  1029. plugNum = i;
  1030. break;
  1031. }
  1032. }
  1033. ret = responsePackeHandle(fd, (uint8_t *)&csuResult, plugNum, csuCmdPkt.Data.Register);
  1034. gDoCommGblData.DisConnCount = 0; //送收資料沒有問題, 清除中斷網路計數
  1035. return ret;
  1036. }
  1037. static int writeWaitPlugItState(int fd, uint8_t id)
  1038. {
  1039. int ret = PASS;
  1040. uint8_t data[2] = {pSysInfo->WaitForPlugit, 0};
  1041. //printf("WaitForPlugit = %d", pSysInfo->WaitForPlugit);
  1042. if ((ret = composeSocketData(fd,
  1043. id,
  1044. OP_WRITE_DATA,
  1045. REG_WAIT_PLUG_IT_STATE,
  1046. 1,
  1047. &data[0])) == FAIL) {
  1048. return ret;
  1049. }
  1050. return ret;
  1051. }
  1052. static int readQRcodeURLAndSystemDate(int fd)
  1053. {
  1054. int ret = PASS;
  1055. ret = composeSocketData(fd,
  1056. ID_REGISTER,
  1057. OP_READ_DATA,
  1058. REG_QRCODE_URL_INFO,
  1059. 0,
  1060. NULL);
  1061. return ret;
  1062. }
  1063. static int writePresentChargingInfo(int fd, uint8_t plugNum, uint8_t id)
  1064. {
  1065. int ret = PASS;
  1066. uint8_t dataBuf[16] = {0};
  1067. PreChargingInfo *pPreChargingInfo = (PreChargingInfo *)dataBuf;
  1068. pDcChargingInfo = (struct ChargingInfoData *)GetDcChargingInfoData(plugNum);
  1069. if(pDcChargingInfo->PantographFlag == NO)
  1070. {
  1071. pPreChargingInfo->PresentChargingVoltage = htons((uint16_t)(pDcChargingInfo->PresentChargingVoltage * 10));
  1072. pPreChargingInfo->PresentChargingCurrent = htons((uint16_t)(pDcChargingInfo->PresentChargingCurrent * 10));
  1073. }
  1074. else
  1075. {
  1076. pPreChargingInfo->PresentChargingVoltage = htons((uint16_t)(ShmDcCommonData->PcPsuOutput[plugNum].Voltage));
  1077. pPreChargingInfo->PresentChargingCurrent = htons((uint16_t)(ShmDcCommonData->PcPsuOutput[plugNum].Current));
  1078. }
  1079. pPreChargingInfo->RemainChargingDuration = htonl(pDcChargingInfo->RemainChargingDuration);
  1080. pPreChargingInfo->EvBatterySoc = pDcChargingInfo->EvBatterySoc;
  1081. ret = composeSocketData(fd,
  1082. id,
  1083. OP_WRITE_DATA,
  1084. REG_PRESENT_CHARGING_INFO,
  1085. sizeof(PreChargingInfo),
  1086. (uint8_t *)pPreChargingInfo);
  1087. return ret;
  1088. }
  1089. static int writeOtherModuleVersion(int fd)
  1090. {
  1091. int ret = PASS;
  1092. uint8_t data[255] = {0};
  1093. uint8_t dataLen = 0;
  1094. //report other module version message
  1095. memcpy(&data[dataLen], pSysInfo->FanModuleFwRev, VERSION_BUF_SIZE);
  1096. dataLen += VERSION_BUF_SIZE;
  1097. memcpy(&data[dataLen], pSysInfo->RelayModuleFwRev, VERSION_BUF_SIZE);
  1098. dataLen += VERSION_BUF_SIZE;
  1099. memcpy(&data[dataLen], pSysInfo->Connector1FwRev, VERSION_BUF_SIZE);
  1100. dataLen += VERSION_BUF_SIZE;
  1101. memcpy(&data[dataLen], pSysInfo->Connector2FwRev, VERSION_BUF_SIZE);
  1102. dataLen += VERSION_BUF_SIZE;
  1103. memcpy(&data[dataLen], pSysInfo->LedModuleFwRev, VERSION_BUF_SIZE);
  1104. dataLen += VERSION_BUF_SIZE;
  1105. ret = composeSocketData(fd,
  1106. ID_REGISTER,
  1107. OP_WRITE_DATA,
  1108. REG_REPORT_OTHER_VERSION,
  1109. dataLen,
  1110. &data[0]);
  1111. return ret;
  1112. }
  1113. static int writeCsuModuleVersion(int fd)
  1114. {
  1115. int ret = PASS;
  1116. uint8_t data[255] = {0};
  1117. uint8_t dataLen = 0;
  1118. //report CSU platform version message
  1119. memcpy(&data[dataLen], pSysInfo->CsuBootLoadFwRev, VERSION_BUF_SIZE);
  1120. dataLen += VERSION_BUF_SIZE;
  1121. memcpy(&data[dataLen], pSysInfo->CsuKernelFwRev, VERSION_BUF_SIZE);
  1122. dataLen += VERSION_BUF_SIZE;
  1123. memcpy(&data[dataLen], pSysInfo->CsuRootFsFwRev, VERSION_BUF_SIZE);
  1124. dataLen += VERSION_BUF_SIZE;
  1125. memcpy(&data[dataLen], pSysInfo->CsuPrimFwRev, VERSION_BUF_SIZE);
  1126. dataLen += VERSION_BUF_SIZE;
  1127. ret = composeSocketData(fd,
  1128. ID_REGISTER,
  1129. OP_WRITE_DATA,
  1130. REG_REPORT_CSU_VERSION,
  1131. dataLen,
  1132. &data[0]);
  1133. return ret;
  1134. }
  1135. static int readMiscCommand(int fd, uint8_t id)
  1136. {
  1137. int ret = PASS;
  1138. ret = composeSocketData(fd,
  1139. id,
  1140. OP_READ_DATA,
  1141. REG_MISC_CONTROL,
  1142. 0,
  1143. NULL);
  1144. return ret;
  1145. }
  1146. static int readChargePermission(int fd, uint8_t id)
  1147. {
  1148. return composeSocketData(fd,
  1149. id,
  1150. OP_READ_DATA,
  1151. REG_CHARGING_PERMISSION,
  1152. 0,
  1153. NULL);
  1154. }
  1155. static int writeGroundFaultDetection(int fd, uint8_t status, uint8_t id)
  1156. {
  1157. int ret = PASS;
  1158. uint8_t dataBuf[1] = {status};
  1159. ret = composeSocketData(fd,
  1160. id,
  1161. OP_WRITE_DATA,
  1162. REG_Ground_Fault_Detection,
  1163. 1,
  1164. &dataBuf[0]);
  1165. return ret;
  1166. }
  1167. static int writeUserID(int fd, uint8_t id, uint8_t *pUserID)
  1168. {
  1169. if ((strlen((char *)pUserID) <= 0) || (pUserID == NULL)) {
  1170. return FAIL;
  1171. }
  1172. return composeSocketData(fd,
  1173. id,
  1174. OP_WRITE_DATA,
  1175. REG_USER_ID,
  1176. strlen((char *)pUserID),
  1177. pUserID);
  1178. }
  1179. static int writeConnectorState(int fd, uint8_t plugNum, uint8_t id)
  1180. {
  1181. uint8_t dataBuf[10] = {'\0'};
  1182. ConnectorState *pConnState = (ConnectorState *)dataBuf;
  1183. static char vendorErrorCodeTmp[2][WARNING_CODE_SIZE] = {0};
  1184. int ret = PASS;
  1185. pDcChargingInfo = (struct ChargingInfoData *)GetDcChargingInfoData(plugNum);
  1186. pConnState->ConnectorTemp = pDcChargingInfo->ConnectorTemp;
  1187. pConnState->ChillerTemp = pDcChargingInfo->ChillerTemp;
  1188. if (pDcChargingInfo->SystemStatus <= S_AUTHORIZING) {
  1189. pConnState->State = CONN_ST_IDLE; //idle
  1190. strncpy((char *)ShmOCPP16Data->StatusNotification[plugNum].VendorErrorCode,
  1191. "",
  1192. sizeof(ShmOCPP16Data->StatusNotification[plugNum].VendorErrorCode));
  1193. strncpy(&vendorErrorCodeTmp[plugNum][0], "", WARNING_CODE_SIZE);
  1194. } else if ((pDcChargingInfo->SystemStatus <= S_PREPARING_FOR_EVSE) ||
  1195. (pDcChargingInfo->SystemStatus == S_CCS_PRECHARGE_ST0) ||
  1196. (pDcChargingInfo->SystemStatus == S_CCS_PRECHARGE_ST1)) {
  1197. pConnState->State = CONN_ST_PREPARING; //preparing
  1198. } else if (pDcChargingInfo->SystemStatus == S_CHARGING) {
  1199. pConnState->State = CONN_ST_CHARGING; //charging
  1200. } else if (pDcChargingInfo->SystemStatus == S_TERMINATING) {
  1201. pConnState->State = CONN_ST_TERMINATING; //terminating
  1202. } else if ((pDcChargingInfo->SystemStatus == S_ALARM) ||
  1203. (pDcChargingInfo->SystemStatus == S_FAULT)) {
  1204. pConnState->State = CONN_ST_ALARM;
  1205. strncpy((char *)pConnState->WarningCode,
  1206. (char *)ShmOCPP16Data->StatusNotification[plugNum].VendorErrorCode,
  1207. WARNING_CODE_SIZE);
  1208. if (strncmp(&vendorErrorCodeTmp[plugNum][0], "", WARNING_CODE_SIZE) == 0) {
  1209. strncpy(&vendorErrorCodeTmp[plugNum][0],
  1210. (char *)ShmOCPP16Data->StatusNotification[plugNum].VendorErrorCode,
  1211. WARNING_CODE_SIZE);
  1212. vendorErrorCodeTmp[plugNum][6] = '\0';
  1213. log_info("1 ID = %d, VendorErrorCode = %s",
  1214. plugNum,
  1215. (char *)ShmOCPP16Data->StatusNotification[plugNum].VendorErrorCode);
  1216. } else {
  1217. if (strncmp(&vendorErrorCodeTmp[plugNum][0],
  1218. (char *)ShmOCPP16Data->StatusNotification[plugNum].VendorErrorCode,
  1219. WARNING_CODE_SIZE) != 0
  1220. ) {
  1221. strncpy(&vendorErrorCodeTmp[plugNum][0],
  1222. (char *)ShmOCPP16Data->StatusNotification[plugNum].VendorErrorCode,
  1223. WARNING_CODE_SIZE);
  1224. vendorErrorCodeTmp[plugNum][6] = '\0';
  1225. log_info("2 ID = %d, VendorErrorCode = %s",
  1226. plugNum,
  1227. (char *)ShmOCPP16Data->StatusNotification[plugNum].VendorErrorCode);
  1228. }
  1229. }
  1230. }
  1231. ret = composeSocketData(fd,
  1232. id,
  1233. OP_WRITE_DATA,
  1234. REG_CONNECTOR_STATE,
  1235. sizeof(dataBuf) - 1,
  1236. &dataBuf[0]);
  1237. return ret;
  1238. }
  1239. static int writePlugInStatus(int fd, uint8_t plugNum, uint8_t id)
  1240. {
  1241. int ret = PASS;
  1242. pDcChargingInfo = (struct ChargingInfoData *)GetDcChargingInfoData(plugNum);
  1243. uint8_t dataBuf[2] = {pDcChargingInfo->ConnectorPlugIn, 0};
  1244. ret = composeSocketData(fd,
  1245. id,
  1246. OP_WRITE_DATA,
  1247. REG_PLUG_IN_STATE,
  1248. 1,
  1249. &dataBuf[0]);
  1250. return ret;
  1251. }
  1252. static int readSoftwareUpdate(int fd, uint8_t gunID)
  1253. {
  1254. int ret = PASS;
  1255. uint8_t dataBuf[2] = {pSysInfo->FirmwareUpdate, 0};
  1256. ret = composeSocketData(fd,
  1257. gunID,
  1258. OP_READ_DATA,
  1259. REG_SOFTWARE_UPDATE,
  1260. 1,
  1261. &dataBuf[0]);
  1262. return ret;
  1263. }
  1264. static int writeChargingTarget(int fd, uint8_t plugNum, uint8_t id)
  1265. {
  1266. int ret = PASS;
  1267. float ChargingVolt, ChargingAmp;
  1268. uint8_t dataBuf[4] = {0};
  1269. pDcChargingInfo = (struct ChargingInfoData *)GetDcChargingInfoData(plugNum);
  1270. ChargingVolt = pDcChargingInfo->EvBatterytargetVoltage;
  1271. ChargingAmp = pDcChargingInfo->EvBatterytargetCurrent;
  1272. ChargingVolt *= 10;
  1273. ChargingAmp *= 10;
  1274. dataBuf[0] = ((uint16_t)ChargingVolt >> 8) & 0xFF;
  1275. dataBuf[1] = ((uint16_t)ChargingVolt) & 0xFF;
  1276. dataBuf[2] = ((uint16_t)ChargingAmp >> 8) & 0xFF;
  1277. dataBuf[3] = ((uint16_t)ChargingAmp) & 0xFF;
  1278. ret = composeSocketData(fd,
  1279. id,
  1280. OP_WRITE_DATA,
  1281. REG_CHARGING_TARGET,
  1282. sizeof(dataBuf),
  1283. &dataBuf[0]);
  1284. return ret;
  1285. }
  1286. static int readChargingCapability(int fd, uint8_t id)
  1287. {
  1288. int ret = PASS;
  1289. ret = composeSocketData(fd,
  1290. id,
  1291. OP_READ_DATA,
  1292. REG_CHARGING_CAP,
  1293. 0,
  1294. NULL);
  1295. return ret;
  1296. }
  1297. static int writeDispenserStatus(int fd, uint8_t gunID)
  1298. {
  1299. uint8_t warningCount = 0;
  1300. uint8_t count = 0;
  1301. uint8_t CurWarnCodeTmp[10][6] = {0};
  1302. uint8_t PreWarnCodeTmp[10][6] = {0};
  1303. int ret = PASS;
  1304. if ((pSysWarning->WarningCount <= 0) &&
  1305. (gPreSysWarningInfo.WarningCount <= 0)) {
  1306. return FAIL;
  1307. }
  1308. warningCount = pSysWarning->WarningCount;
  1309. for (count = 0; count < warningCount; count++) {
  1310. memcpy(CurWarnCodeTmp[count], pSysWarning->WarningCode[count], WARNING_CODE_SIZE);
  1311. }
  1312. warningCount = gPreSysWarningInfo.WarningCount;
  1313. for (count = 0; count < warningCount; count++) {
  1314. memcpy(PreWarnCodeTmp[count], gPreSysWarningInfo.WarningCode[count], WARNING_CODE_SIZE);
  1315. }
  1316. if (strcmp((char *)&CurWarnCodeTmp[0], (char *)&PreWarnCodeTmp[0]) == 0) {
  1317. return FAIL;
  1318. }
  1319. ret = composeSocketData(fd,
  1320. gunID,
  1321. OP_WRITE_DATA,
  1322. REG_DISPENSER_STATUS,
  1323. strlen((char *)CurWarnCodeTmp),
  1324. &CurWarnCodeTmp[0][0]);
  1325. return ret;
  1326. }
  1327. static int readPowerCabinetStatus(int fd, uint8_t gunID)
  1328. {
  1329. int ret = PASS;
  1330. ret = composeSocketData(fd,
  1331. gunID,
  1332. OP_READ_DATA,
  1333. REG_POWER_CABINET_STATUS,
  1334. 0,
  1335. NULL);
  1336. return ret;
  1337. }
  1338. static int readConnectorID(int fd)
  1339. {
  1340. int ret = PASS;
  1341. ret = composeSocketData(fd,
  1342. ID_REGISTER,
  1343. OP_READ_DATA,
  1344. REG_CONNECTOR_ID,
  1345. 0,
  1346. NULL);
  1347. return ret;
  1348. }
  1349. static int WriteModelName(int fd)
  1350. {
  1351. int ret = PASS;
  1352. uint8_t Tmp = 0;
  1353. uint8_t TmpBuf[255] = {0};
  1354. memcpy(TmpBuf,
  1355. pSysConfig->ModelName,
  1356. strlen((char *)pSysConfig->ModelName));
  1357. Tmp = (uint8_t)(ShmPrimaryMcuData->InputDet.bits.Key2 << 2 |
  1358. ShmPrimaryMcuData->InputDet.bits.Key1 << 1 |
  1359. ShmPrimaryMcuData->InputDet.bits.Key0);
  1360. TmpBuf[strlen((char *)pSysConfig->ModelName)] = Tmp; //Dispenser switch value
  1361. ret = composeSocketData(fd,
  1362. ID_REGISTER,
  1363. OP_WRITE_DATA,
  1364. REG_MODEL_NAME,
  1365. strlen((char *)pSysConfig->ModelName) + 1,
  1366. &TmpBuf[0]);
  1367. return ret;
  1368. }
  1369. static void calDisconnectCount(uint8_t *pValue, uint8_t maxCount)
  1370. {
  1371. sleep(3); //wait 1 second
  1372. if ((*pValue) >= maxCount) {
  1373. setTcpStatus(ABNORMAL);
  1374. } else {
  1375. (*pValue)++;
  1376. }
  1377. }
  1378. static int CheckNetworkStatus(void)
  1379. {
  1380. char *ipAddr = (char *)&pSysConfig->Eth0Interface.EthIpAddress;
  1381. //printf("Check network IP Header = %s\n", ipAddr);
  1382. if (strstr(ipAddr, CMP_ETH_IP_HEAD) != NULL) {
  1383. return 1;
  1384. }
  1385. return 0;
  1386. }
  1387. static void updateFirmwareProcess(int fd, uint8_t gunID, uint8_t totalConnCount)
  1388. {
  1389. bool canUpdateFirmware = true;
  1390. uint8_t plugNum = 0;
  1391. uint8_t ackCount = 5;
  1392. struct timeb updateTime;
  1393. pDcChargingInfo = (struct ChargingInfoData *)GetDcChargingInfoData(plugNum);
  1394. if (pSysWarning->Level != 2) {
  1395. for (plugNum = 0; plugNum < totalConnCount; plugNum++) {
  1396. if (pDcChargingInfo->SystemStatus != S_IDLE &&
  1397. pDcChargingInfo->SystemStatus != S_RESERVATION &&
  1398. pDcChargingInfo->SystemStatus != S_MAINTAIN) {
  1399. canUpdateFirmware = false;
  1400. }
  1401. }
  1402. }
  1403. if (canUpdateFirmware) {
  1404. ftime(&updateTime);
  1405. if (DiffTimeb(gRegTimeUp[0][REG_SOFTWARE_UPDATE], updateTime) > LOOP_RETRY_TIME * 5) {
  1406. readSoftwareUpdate(fd, gunID);
  1407. ftime(&gRegTimeUp[0][REG_SOFTWARE_UPDATE]);
  1408. }
  1409. if (pSysInfo->FirmwareUpdate == YES) {
  1410. while (ackCount != 0) {
  1411. ftime(&updateTime);
  1412. if (DiffTimeb(gRegTimeUp[0][REG_SOFTWARE_UPDATE], updateTime) > LOOP_RETRY_TIME / 2) {
  1413. readSoftwareUpdate(fd, gunID);
  1414. ftime(&gRegTimeUp[0][REG_SOFTWARE_UPDATE]);
  1415. if (pSysInfo->FirmwareUpdate == NO) {
  1416. ackCount--;
  1417. }
  1418. }
  1419. usleep(128);
  1420. }
  1421. }
  1422. }
  1423. }
  1424. static void checkAuthorProcess(int fd, uint8_t plugNum)
  1425. {
  1426. int ret = 0;
  1427. uint8_t gunID = 0;
  1428. struct timeb AuthNowTime;
  1429. #if defined DD360Audi
  1430. gunID = gDoCommGblData.ConnectorID[pSysInfo->CurGunSelected];
  1431. //gunID = gDoCommGblData.ConnectorID[plugNum];
  1432. if (pSysConfig->AuthorisationMode) {
  1433. gunID = ID_REGISTER;
  1434. ShmSelectGunInfo->PricesInfo[pSysInfo->CurGunSelected].Balance = 0.0;
  1435. }
  1436. #else
  1437. gunID = ID_REGISTER;
  1438. ShmSelectGunInfo->PricesInfo[pSysInfo->CurGunSelected].Balance = 0.0; //非Audi 不需要等待主櫃回報餘額
  1439. #endif // DD360Audi
  1440. if ((ShmOCPP16Data->SpMsg.bits.AuthorizeReq == YES) ||
  1441. (pSysInfo->AuthorizeFlag == YES)) {
  1442. ftime(&AuthNowTime);
  1443. if (DiffTimeb(gRegTimeUp[plugNum][REG_USER_ID], AuthNowTime) > LOOP_RETRY_TIME) {
  1444. ret = writeUserID(fd,
  1445. gunID,
  1446. pSysConfig->UserId);
  1447. if (ret >= 0) {
  1448. memset(ShmOCPP16Data->Authorize.ResponseIdTagInfo.Status,
  1449. 0,
  1450. sizeof(ShmOCPP16Data->Authorize.ResponseIdTagInfo.Status));
  1451. if (ret == 0) {
  1452. strcpy((char *)ShmOCPP16Data->Authorize.ResponseIdTagInfo.Status, "Invalid");
  1453. } else {
  1454. strcpy((char *)ShmOCPP16Data->Authorize.ResponseIdTagInfo.Status, "Accepted");
  1455. }
  1456. //printf("%d Balance = %.2f, %.2f\n",
  1457. // plugNum,
  1458. // ShmSelectGunInfo->PricesInfo[plugNum].Balance,
  1459. // FAIL_BALANCE_PRICES);
  1460. if (ShmSelectGunInfo->PricesInfo[plugNum].Balance != FAIL_BALANCE_PRICES) {
  1461. ShmOCPP16Data->SpMsg.bits.AuthorizeConf = YES; //isAuthorizedComplete
  1462. ShmOCPP16Data->SpMsg.bits.AuthorizeReq = NO;
  1463. pSysInfo->AuthorizeFlag = NO;
  1464. ShmPsuData->SystemAvailablePower = NO;
  1465. ShmPsuData->SystemPresentPsuQuantity = NO;
  1466. }
  1467. }
  1468. ftime(&gRegTimeUp[plugNum][REG_USER_ID]);
  1469. }
  1470. }
  1471. }
  1472. static int messageTrigger(int fd, uint8_t plugNum, uint8_t gunID, uint8_t curReg)
  1473. {
  1474. int ret = DISPENSER_INIT_SUCC;
  1475. int isContinue = 1;
  1476. struct timeb NowTime;
  1477. while (isContinue) {
  1478. usleep(128);
  1479. ftime(&NowTime);
  1480. switch (curReg) {
  1481. case REG_MODEL_NAME:
  1482. if (WriteModelName(fd) == PASS) {
  1483. gDoCommGblData.DisConnCount = 0;
  1484. curReg = REG_CONNECTOR_ID;
  1485. } else {
  1486. sleep(1);
  1487. }
  1488. break;
  1489. case REG_CONNECTOR_ID:
  1490. if (readConnectorID(fd) == PASS) {
  1491. gDoCommGblData.DisConnCount = 0;
  1492. curReg = REG_REPORT_CSU_VERSION;
  1493. } else {
  1494. sleep(1);
  1495. }
  1496. break;
  1497. case REG_POWER_CABINET_STATUS:
  1498. if (DiffTimeb(gRegTimeUp[plugNum][curReg], NowTime) > LOOP_RETRY_TIME ||
  1499. DiffTimeb(gRegTimeUp[plugNum][curReg], NowTime) < 0) {
  1500. readPowerCabinetStatus(fd, gunID);
  1501. ftime(&gRegTimeUp[plugNum][curReg]);
  1502. }
  1503. curReg = REG_DISPENSER_STATUS;
  1504. break;
  1505. case REG_DISPENSER_STATUS:
  1506. if (DiffTimeb(gRegTimeUp[plugNum][curReg], NowTime) > LOOP_RETRY_TIME ||
  1507. DiffTimeb(gRegTimeUp[plugNum][curReg], NowTime) < 0) {
  1508. writeDispenserStatus(fd, gunID);
  1509. ftime(&gRegTimeUp[plugNum][curReg]);
  1510. }
  1511. curReg = REG_PLUG_IN_STATE;
  1512. break;
  1513. //case REG_CHARGING_CAP:
  1514. // readChargingCapability(fd, plugNum, gunID);
  1515. // break;
  1516. //case REG_CHARGING_TARGET:
  1517. // writeChargingTarget(fd, plugNum, gunID);
  1518. // break;
  1519. //case REG_SOFTWARE_UPDATE:
  1520. // if (ChargingData[plugNum]->SystemStatus == S_IDLE) {
  1521. // if (DiffTimeb(gRegTimeUp[plugNum][curReg], NowTime) > L OOP_RETRY_TIME||
  1522. //DiffTimeb(gRegTimeUp[plugNum][curReg], NowTime) < 0 * 1) {
  1523. // readSoftwareUpdate(fd);
  1524. // ftime(&gRegTimeUp[plugNum][curReg]);
  1525. // }
  1526. // while (pSysInfo->FirmwareUpdate == YES) {
  1527. // ftime(&NowTime);
  1528. // if (DiffTimeb(gRegTimeUp[plugNum][curReg], NowT ime) > LOOP_RETRY_TIME||
  1529. //DiffTimeb(gRegTimeUp[plugNum][curReg], NowTime) < 0) {
  1530. // readSoftwareUpdate(fd);
  1531. // }
  1532. // usleep(128);
  1533. // }
  1534. // }
  1535. // isContinue = 0;
  1536. // break;
  1537. case REG_PLUG_IN_STATE:
  1538. if (DiffTimeb(gRegTimeUp[plugNum][curReg], NowTime) > LOOP_RETRY_TIME ||
  1539. DiffTimeb(gRegTimeUp[plugNum][curReg], NowTime) < 0) {
  1540. writePlugInStatus(fd, plugNum, gunID);
  1541. ftime(&gRegTimeUp[plugNum][curReg]);
  1542. }
  1543. curReg = REG_CONNECTOR_STATE;
  1544. break;
  1545. case REG_CONNECTOR_STATE:
  1546. if (DiffTimeb(gRegTimeUp[plugNum][curReg], NowTime) > LOOP_RETRY_TIME ||
  1547. DiffTimeb(gRegTimeUp[plugNum][curReg], NowTime) < 0) {
  1548. writeConnectorState(fd, plugNum, gunID);
  1549. ftime(&gRegTimeUp[plugNum][curReg]);
  1550. }
  1551. //check misc command from power cabinet
  1552. if (gDoCommGblData.MiscCmd != 0) {
  1553. if (gDoCommGblData.MiscCmd != REG_MISC_CONTROL) {
  1554. log_error("misc command failed = %x", gDoCommGblData.MiscCmd);
  1555. }
  1556. curReg = gDoCommGblData.MiscCmd;
  1557. } else {
  1558. curReg = REG_QRCODE_URL_INFO;
  1559. }
  1560. break;
  1561. //case REG_USER_ID:
  1562. // writeUserID(fd, gunID, uint8_t *pUserID);
  1563. // break;
  1564. //case REG_CHARGING_PERMISSION:
  1565. // readChargePermission(fd, gunID);
  1566. // break;
  1567. case REG_MISC_CONTROL:
  1568. readMiscCommand(fd, gunID);
  1569. isContinue = 0;
  1570. break;
  1571. case REG_REPORT_CSU_VERSION:
  1572. case REG_REPORT_OTHER_VERSION:
  1573. if (gDoCommGblData.MiscCmd != 0) {
  1574. writeCsuModuleVersion(fd);
  1575. writeOtherModuleVersion(fd);
  1576. clearMiscCommand();
  1577. isContinue = 0;
  1578. } else {
  1579. //power up write dispenser version and get power cabinet system ID
  1580. if ((writeCsuModuleVersion(fd) == PASS) &&
  1581. (writeOtherModuleVersion(fd) == PASS) &&
  1582. (readQRcodeURLAndSystemDate(fd) == PASS)
  1583. ) {
  1584. //gDoCommGblData.DisConnCount = 0;
  1585. isContinue = 0;
  1586. }
  1587. }
  1588. break;
  1589. //case REG_PRESENT_CHARGING_INFO:
  1590. // break;
  1591. case REG_QRCODE_URL_INFO:
  1592. if (gunID == 1) {
  1593. if (DiffTimeb(gRegTimeUp[plugNum][curReg], NowTime) > (LOOP_RETRY_TIME * 10) ||
  1594. DiffTimeb(gRegTimeUp[plugNum][curReg], NowTime) < 0
  1595. ) {
  1596. readQRcodeURLAndSystemDate(fd);
  1597. ftime(&gRegTimeUp[plugNum][curReg]);
  1598. }
  1599. }
  1600. isContinue = 0;
  1601. //curReg = REG_SOFTWARE_UPDATE;
  1602. break;
  1603. //case REG_WAIT_PLUG_IT_STATE:
  1604. // if (DiffTimeb(gRegTimeUp[plugNum][curReg], NowTime) > (LOOP_RETRY_TIME / 10)) {
  1605. // writeWaitPlugItState(fd, gunID);
  1606. // ftime(&gRegTimeUp[plugNum][curReg]);
  1607. // }
  1608. // isContinue = 0;
  1609. // break;
  1610. default:
  1611. log_error("error curReg = %x", curReg);
  1612. gDoCommGblData.MiscCmd = 0;
  1613. gDoCommGblData.DisConnCount = CHECK_NETWORK_FAIL_COUNT;
  1614. isContinue = 0;
  1615. break;
  1616. }
  1617. if (gDoCommGblData.DisConnCount >= CHECK_NETWORK_FAIL_COUNT) {
  1618. return DISPENSER_SOCKET_RECONN;
  1619. }
  1620. }//for
  1621. return ret;
  1622. }
  1623. //static bool isDetectPlugin()
  1624. //{
  1625. // if (pSysInfo->WaitForPlugit == YES) {
  1626. // return YES;
  1627. // }
  1628. //
  1629. // return NO;
  1630. //}
  1631. static void systemStatusProcess(int fd, uint8_t totalGun, uint8_t plugNum, uint8_t gunID)
  1632. {
  1633. uint8_t i = 0;
  1634. struct timeb AuthNowTime = {0};
  1635. pDcChargingInfo = (struct ChargingInfoData *)GetDcChargingInfoData(plugNum);
  1636. switch (pDcChargingInfo->SystemStatus) {
  1637. case S_IDLE:
  1638. case S_RESERVATION:
  1639. case S_MAINTAIN:
  1640. case S_ALARM:
  1641. case S_AUTHORIZING:
  1642. checkAuthorProcess(fd, plugNum);
  1643. //authorization complete, write wait plug it state
  1644. if (ShmSelectGunInfo->PricesInfo[plugNum].Balance == FAIL_BALANCE_PRICES) {
  1645. break;
  1646. }
  1647. ftime(&AuthNowTime);
  1648. if (DiffTimeb(gRegTimeUp[plugNum][REG_WAIT_PLUG_IT_STATE], AuthNowTime) > (LOOP_RETRY_TIME / 10) ||
  1649. DiffTimeb(gRegTimeUp[plugNum][REG_WAIT_PLUG_IT_STATE], AuthNowTime) < 0
  1650. ) {
  1651. writeWaitPlugItState(fd, gunID);
  1652. ftime(&gRegTimeUp[plugNum][REG_WAIT_PLUG_IT_STATE]);
  1653. }
  1654. /*
  1655. ftime(&AuthNowTime);
  1656. if (DiffTimeb(gRegTimeUp[plugNum][REG_MISC_CONTROL], AuthNowTime) > (LOOP_RETRY_TIME / 10) ||
  1657. DiffTimeb(gRegTimeUp[plugNum][REG_MISC_CONTROL], AuthNowTime) < 0
  1658. ) {
  1659. readMiscCommand(fd, gunID);
  1660. ftime(&gRegTimeUp[plugNum][REG_MISC_CONTROL]);
  1661. }
  1662. */
  1663. ftime(&AuthNowTime);
  1664. if (DiffTimeb(gRegTimeUp[plugNum][REG_CHARGING_CAP], AuthNowTime) > LOOP_RETRY_TIME ||
  1665. DiffTimeb(gRegTimeUp[plugNum][REG_CHARGING_CAP], AuthNowTime) < 0
  1666. ) {
  1667. readChargingCapability(fd, gunID);
  1668. if (pDcChargingInfo->PantographFlag)
  1669. writeGroundFaultDetection(fd, 0, gunID);
  1670. ftime(&gRegTimeUp[plugNum][REG_CHARGING_CAP]);
  1671. }
  1672. break;
  1673. case S_PREPARNING: //get permission
  1674. ftime(&AuthNowTime);
  1675. if (DiffTimeb(gRegTimeUp[plugNum][REG_CONNECTOR_STATE], AuthNowTime) > LOOP_RETRY_TIME ||
  1676. DiffTimeb(gRegTimeUp[plugNum][REG_CONNECTOR_STATE], AuthNowTime) < 0
  1677. ) {
  1678. writeConnectorState(fd, plugNum, gunID);
  1679. ftime(&gRegTimeUp[plugNum][REG_CONNECTOR_STATE]);
  1680. }
  1681. ftime(&AuthNowTime);
  1682. if (DiffTimeb(gRegTimeUp[plugNum][REG_CHARGING_PERMISSION], AuthNowTime) > LOOP_RETRY_TIME ||
  1683. DiffTimeb(gRegTimeUp[plugNum][REG_CHARGING_PERMISSION], AuthNowTime) < 0
  1684. ) {
  1685. if (readChargePermission(fd, gunID) && readChargingCapability(fd, gunID)) {
  1686. for (i = 0; i < totalGun; i++) {
  1687. pDcChargingInfo = (struct ChargingInfoData *)GetDcChargingInfoData(i);
  1688. ShmPsuData->SystemAvailablePower += pDcChargingInfo->AvailableChargingPower;
  1689. }
  1690. ShmPsuData->SystemPresentPsuQuantity = (ShmPsuData->SystemAvailablePower / 30);
  1691. }
  1692. ftime(&gRegTimeUp[plugNum][REG_CHARGING_PERMISSION]);
  1693. }
  1694. ftime(&AuthNowTime);
  1695. if (DiffTimeb(gRegTimeUp[plugNum][REG_PRESENT_CHARGING_INFO], AuthNowTime) > LOOP_RETRY_TIME ||
  1696. DiffTimeb(gRegTimeUp[plugNum][REG_PRESENT_CHARGING_INFO], AuthNowTime) < 0
  1697. ) {
  1698. writePresentChargingInfo(fd, plugNum, gunID);
  1699. if (pDcChargingInfo->PantographFlag)
  1700. writeGroundFaultDetection(fd, 0, gunID);
  1701. ftime(&gRegTimeUp[plugNum][REG_PRESENT_CHARGING_INFO]);
  1702. }
  1703. break;
  1704. case S_PREPARING_FOR_EV://wait connector lock
  1705. ftime(&AuthNowTime);
  1706. if (DiffTimeb(gRegTimeUp[plugNum][REG_CHARGING_CAP], AuthNowTime) > LOOP_RETRY_TIME ||
  1707. DiffTimeb(gRegTimeUp[plugNum][REG_CHARGING_CAP], AuthNowTime) < 0
  1708. ) {
  1709. readChargingCapability(fd, gunID);
  1710. ftime(&gRegTimeUp[plugNum][REG_CHARGING_CAP]);
  1711. }
  1712. ftime(&AuthNowTime);
  1713. if (DiffTimeb(gRegTimeUp[plugNum][REG_CHARGING_PERMISSION], AuthNowTime) > LOOP_RETRY_TIME ||
  1714. DiffTimeb(gRegTimeUp[plugNum][REG_CHARGING_PERMISSION], AuthNowTime) < 0
  1715. ) {
  1716. if (readChargePermission(fd, gunID) == 0) {
  1717. log_info("S_PREPARING_FOR_EV Stop charging by power cabinet's permission = %d, %d",
  1718. plugNum,
  1719. REG_CHARGING_PERMISSION);
  1720. pDcChargingInfo->StopChargeFlag = POWER_CABINET_STOP_CHARGING;
  1721. }
  1722. ftime(&gRegTimeUp[plugNum][REG_CHARGING_PERMISSION]);
  1723. }
  1724. ftime(&AuthNowTime);
  1725. if (DiffTimeb(gRegTimeUp[plugNum][REG_PRESENT_CHARGING_INFO], AuthNowTime) > LOOP_RETRY_TIME ||
  1726. DiffTimeb(gRegTimeUp[plugNum][REG_PRESENT_CHARGING_INFO], AuthNowTime) < 0
  1727. ) {
  1728. writePresentChargingInfo(fd, plugNum, gunID);
  1729. if (pDcChargingInfo->PantographFlag)
  1730. writeGroundFaultDetection(fd, 0, gunID);
  1731. ftime(&gRegTimeUp[plugNum][REG_PRESENT_CHARGING_INFO]);
  1732. }
  1733. break;
  1734. case S_PREPARING_FOR_EVSE: //insaulation test
  1735. case S_CCS_PRECHARGE_ST0:
  1736. case S_CCS_PRECHARGE_ST1:
  1737. writeChargingTarget(fd, plugNum, gunID);
  1738. if (pDcChargingInfo->PantographFlag)
  1739. writeGroundFaultDetection(fd, 1, gunID);
  1740. ftime(&AuthNowTime);
  1741. if (DiffTimeb(gRegTimeUp[plugNum][REG_CHARGING_CAP], AuthNowTime) > LOOP_RETRY_TIME ||
  1742. DiffTimeb(gRegTimeUp[plugNum][REG_CHARGING_CAP], AuthNowTime) < 0
  1743. ) {
  1744. readChargingCapability(fd, gunID);
  1745. ftime(&gRegTimeUp[plugNum][REG_CHARGING_CAP]);
  1746. }
  1747. ftime(&AuthNowTime);
  1748. if (DiffTimeb(gRegTimeUp[plugNum][REG_CHARGING_PERMISSION], AuthNowTime) > LOOP_RETRY_TIME ||
  1749. DiffTimeb(gRegTimeUp[plugNum][REG_CHARGING_PERMISSION], AuthNowTime) < 0
  1750. ) {
  1751. if (readChargePermission(fd, gunID) == 0) {
  1752. log_info("S_PREPARING_FOR_EVSE Stop charging by power cabinet's permission = %d, %d",
  1753. plugNum,
  1754. REG_CHARGING_PERMISSION);
  1755. pDcChargingInfo->StopChargeFlag = POWER_CABINET_STOP_CHARGING;
  1756. }
  1757. ftime(&gRegTimeUp[plugNum][REG_CHARGING_PERMISSION]);
  1758. }
  1759. ftime(&AuthNowTime);
  1760. if (DiffTimeb(gRegTimeUp[plugNum][REG_PRESENT_CHARGING_INFO], AuthNowTime) > LOOP_RETRY_TIME ||
  1761. DiffTimeb(gRegTimeUp[plugNum][REG_PRESENT_CHARGING_INFO], AuthNowTime) < 0
  1762. ) {
  1763. writePresentChargingInfo(fd, plugNum, gunID);
  1764. ftime(&gRegTimeUp[plugNum][REG_PRESENT_CHARGING_INFO]);
  1765. }
  1766. break;
  1767. case S_CHARGING: //charging
  1768. case S_TERMINATING:
  1769. if(pDcChargingInfo->Type == _Type_GB || pDcChargingInfo->Type == _Type_Chademo)
  1770. {
  1771. if (pDcChargingInfo->PantographFlag)
  1772. writeGroundFaultDetection(fd, 0, gunID);
  1773. }
  1774. else
  1775. {
  1776. if (pDcChargingInfo->PantographFlag)
  1777. writeGroundFaultDetection(fd, 1, gunID);
  1778. }
  1779. ftime(&AuthNowTime);
  1780. if (DiffTimeb(gRegTimeUp[plugNum][REG_CHARGING_CAP], AuthNowTime) > LOOP_RETRY_TIME ||
  1781. DiffTimeb(gRegTimeUp[plugNum][REG_CHARGING_CAP], AuthNowTime) < 0
  1782. ) {
  1783. readChargingCapability(fd, gunID);
  1784. ftime(&gRegTimeUp[plugNum][REG_CHARGING_CAP]);
  1785. }
  1786. ftime(&AuthNowTime);
  1787. if (DiffTimeb(gRegTimeUp[plugNum][REG_CHARGING_PERMISSION], AuthNowTime) > LOOP_RETRY_TIME ||
  1788. DiffTimeb(gRegTimeUp[plugNum][REG_CHARGING_PERMISSION], AuthNowTime) < 0
  1789. ) {
  1790. if (readChargePermission(fd, gunID) == 0) {
  1791. log_info("Stop charging by power cabinet's permission = %d, %d",
  1792. plugNum,
  1793. REG_CHARGING_PERMISSION);
  1794. pDcChargingInfo->StopChargeFlag = POWER_CABINET_STOP_CHARGING;
  1795. //printf("%d StopChargeFlag = %d\n", plugNum, pDcChargingInfo->StopChargeFlag);
  1796. }
  1797. ftime(&gRegTimeUp[plugNum][REG_CHARGING_PERMISSION]);
  1798. }
  1799. writeChargingTarget(fd, plugNum, gunID);
  1800. ftime(&AuthNowTime);
  1801. if (DiffTimeb(gRegTimeUp[plugNum][REG_PRESENT_CHARGING_INFO], AuthNowTime) > LOOP_RETRY_TIME ||
  1802. DiffTimeb(gRegTimeUp[plugNum][REG_PRESENT_CHARGING_INFO], AuthNowTime) < 0
  1803. ) {
  1804. writePresentChargingInfo(fd, plugNum, gunID);
  1805. ftime(&gRegTimeUp[plugNum][REG_PRESENT_CHARGING_INFO]);
  1806. }
  1807. break;
  1808. case S_COMPLETE:
  1809. ftime(&AuthNowTime);
  1810. if (DiffTimeb(gRegTimeUp[plugNum][REG_CHARGING_CAP], AuthNowTime) > LOOP_RETRY_TIME ||
  1811. DiffTimeb(gRegTimeUp[plugNum][REG_CHARGING_CAP], AuthNowTime) < 0
  1812. ) {
  1813. readChargingCapability(fd, gunID);
  1814. if (pDcChargingInfo->PantographFlag)
  1815. writeGroundFaultDetection(fd, 0, gunID);
  1816. ftime(&gRegTimeUp[plugNum][REG_CHARGING_CAP]);
  1817. }
  1818. break;
  1819. default:
  1820. break;
  1821. }
  1822. }
  1823. static int networkCreatePorcess(void)
  1824. {
  1825. int fd = 0;
  1826. uint8_t discount = 0;
  1827. while (pSysInfo->SelfTestSeq != _STEST_COMPLETE) {
  1828. usleep(128);
  1829. }
  1830. setTcpStatus(ABNORMAL);
  1831. /**************** Check Network **********/
  1832. //log_info("Check Dispenser Network Information");
  1833. while (pSysInfo->SelfTestSeq != _STEST_COMPLETE) {
  1834. usleep(125);
  1835. }
  1836. while (!CheckNetworkStatus()) {
  1837. calDisconnectCount(&gDoCommGblData.DisConnCount, CHECK_NETWORK_FAIL_COUNT);
  1838. if( gDoCommGblData.DisConnCount != discount ) {
  1839. discount = gDoCommGblData.DisConnCount;
  1840. log_error("disconnect count = % d", gDoCommGblData.DisConnCount);
  1841. }
  1842. }
  1843. log_info("Dispenser Network Information checked: IP = % s Netmask = % s, Gateway = % s",
  1844. pSysConfig->Eth0Interface.EthIpAddress,
  1845. pSysConfig->Eth0Interface.EthSubmaskAddress,
  1846. pSysConfig->Eth0Interface.EthGatewayAddress);
  1847. gDoCommGblData.DisConnCount = 0;
  1848. discount = 0;
  1849. /**************** Power cabinet connection **********/
  1850. log_info("Connect to Power Cabinet");
  1851. while ((fd = doCommConnToServer()) <= 0) {
  1852. calDisconnectCount(&gDoCommGblData.DisConnCount, CONNECT_SERVER_FAIL_COUNT);
  1853. if( gDoCommGblData.DisConnCount != discount && fd == -1) {
  1854. discount = gDoCommGblData.DisConnCount;
  1855. log_error("disconnect count = %d, fd = %d", gDoCommGblData.DisConnCount, fd);
  1856. }
  1857. }
  1858. log_info("Power cabinet connected(fd = % d): IP = % s Netmask = % s, Gateway = % s",
  1859. fd,
  1860. pSysConfig->Eth0Interface.EthIpAddress,
  1861. pSysConfig->Eth0Interface.EthSubmaskAddress,
  1862. pSysConfig->Eth0Interface.EthGatewayAddress);
  1863. gDoCommGblData.DisConnCount = 0;
  1864. destroySelectGun(DESTROY_ALL_SEL);
  1865. sleep(3);
  1866. setTcpStatus(NORMAL);
  1867. return fd;
  1868. }
  1869. int main(int argc, char *argv[])
  1870. {
  1871. uint8_t plugNum = 0, gunID = 0;
  1872. uint8_t index = 0;
  1873. uint8_t totalConnCount = 0;
  1874. uint8_t initDone = DISPENER_INIT_FAIL;
  1875. char tmpbuf[256] = {0};
  1876. int fd = 0;
  1877. int isContinue = 1;
  1878. InitSocketSigPipe();
  1879. /**************** Initialization **********/
  1880. if (CreateAllCsuShareMemory() == FAIL) {
  1881. log_error("create share memory error");
  1882. return FAIL;
  1883. }
  1884. MappingGunChargingInfo("DoComm Task");
  1885. pSysConfig = (struct SysConfigData *)GetShmSysConfigData();
  1886. pSysInfo = (struct SysInfoData *)GetShmSysInfoData();
  1887. pSysWarning = (struct WARNING_CODE_INFO *)GetShmSysWarningInfo();
  1888. pAlarmCode = (struct AlarmCodeData *)GetShmAlarmCodeData();
  1889. ShmPsuData = (struct PsuData *)GetShmPsuData();
  1890. ShmPrimaryMcuData = (struct PrimaryMcuData *)GetShmPrimaryMcuData();
  1891. ShmOCPP16Data = (struct OCPP16Data *)GetShmOCPP16Data();
  1892. ShmSelectGunInfo = (SelectGunInfo *)GetShmSelectGunInfo();
  1893. ShmDcCommonData = (DcCommonInfo *)GetShmDcCommonData();
  1894. totalConnCount = pSysConfig->TotalConnectorCount;
  1895. for (index = 0; index < totalConnCount; index++) {
  1896. clearPricesInfo(index);
  1897. }
  1898. //initial trigger message timer
  1899. for (index = 0; index < MAX_REGISTER_NUM; index++) {
  1900. ftime(&gRegTimeUp[0][index]);
  1901. usleep(128);
  1902. ftime(&gRegTimeUp[1][index]);
  1903. usleep(50000);
  1904. }
  1905. setTcpStatus(NORMAL);
  1906. while (isContinue) {
  1907. if (initDone == DISPENER_INIT_FAIL) {
  1908. fd = networkCreatePorcess();
  1909. initDone = messageTrigger(fd, 0, 0, REG_MODEL_NAME); //first trigger model name and connector id
  1910. } else {
  1911. plugNum = 0;
  1912. gunID = 0;
  1913. for (plugNum = 0; plugNum < totalConnCount; plugNum++) {
  1914. //checkAuthorProcess(fd, plugNum);
  1915. //plugNum : setup chargingData value for bottom layer
  1916. //gunID : connector Id from power cabinet, 1 = left gun, 2 = right gun,
  1917. gunID = gDoCommGblData.ConnectorID[plugNum];
  1918. systemStatusProcess(fd, totalConnCount, plugNum, gunID);
  1919. initDone = messageTrigger(fd,
  1920. plugNum,
  1921. gunID,
  1922. REG_POWER_CABINET_STATUS);
  1923. if (initDone == DISPENSER_SOCKET_RECONN) {
  1924. break;
  1925. }
  1926. initDone = messageTrigger(fd,
  1927. plugNum,
  1928. gunID,
  1929. REG_MISC_CONTROL);
  1930. if (initDone == DISPENSER_SOCKET_RECONN) {
  1931. break;
  1932. }
  1933. }
  1934. }
  1935. if (initDone != DISPENSER_SOCKET_RECONN) {
  1936. //update dispenser firmware
  1937. updateFirmwareProcess(fd, gDoCommGblData.ConnectorID[0], totalConnCount);
  1938. usleep(100000);
  1939. continue;
  1940. }
  1941. //Ethernet error recovery handle
  1942. log_info("Disconnected from power cabinet...re-connecting");
  1943. setTcpStatus(ABNORMAL);
  1944. if (pSysConfig->Eth0Interface.EthDhcpClient == 0) {
  1945. sprintf(tmpbuf,
  1946. "/sbin/udhcpc -i eth0 -x hostname:CSU3_%s -s /root/dhcp_script/eth0.script > /dev/null &",
  1947. pSysConfig->SystemId);
  1948. system(tmpbuf);
  1949. }
  1950. gDoCommGblData.DisConnCount = 0;
  1951. gDoCommGblData.SeqNum = 0;
  1952. closeSocket(fd);
  1953. fd = 0;
  1954. initDone = DISPENER_INIT_FAIL;
  1955. }
  1956. }