DoComm.c 75 KB

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