DoComm.c 89 KB

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