DoComm.c 75 KB

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