DoComm.c 105 KB

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