ReDoComm.c 73 KB

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