DoComm.c 105 KB

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