DoComm.c 75 KB

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