DoComm.c 105 KB

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