DoComm.c 75 KB

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