DoComm.c 86 KB

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