DoComm.c 89 KB

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