DoComm.c 74 KB

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