DoComm.c 78 KB

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