DoComm.c 104 KB

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