DoComm.c 75 KB

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