DoComm.c 89 KB

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