ReDoComm.c 72 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203
  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 "../../define.h"
  21. #include "Config.h"
  22. #include "ReDoComm.h"
  23. #include "AudiCust.h"
  24. #include "timeout.h"
  25. //------------------------------------------------------------------------------
  26. #define DEBUG_INFO(format, args...) StoreLogMsg("[%s:%d][%s][Info] "format, __FILE__, __LINE__, __FUNCTION__, ##args)
  27. #define DEBUG_WARN(format, args...) StoreLogMsg("[%s:%d][%s][Warn] "format, __FILE__, __LINE__, __FUNCTION__, ##args)
  28. #define DEBUG_ERROR(format, args...) StoreLogMsg("[%s:%d][%s][Error] "format, __FILE__, __LINE__, __FUNCTION__, ##args)
  29. //------------------------------------------------------------------------------
  30. //static int TcpSock = 0;
  31. //static uint8_t PacketSe;
  32. static DoCommGblData gDoCommGblData = {0};
  33. static struct SysConfigAndInfo *ShmSysConfigAndInfo;
  34. static struct StatusCodeData *ShmStatusCodeData;
  35. static struct PsuData *ShmPsuData;
  36. static struct OCPP16Data *ShmOCPP16Data;
  37. static struct PrimaryMcuData *ShmPrimaryMcuData;
  38. static struct ChargingInfoData *ChargingData[CHAdeMO_QUANTITY + CCS_QUANTITY + GB_QUANTITY];
  39. static struct timeb gRegTimeUp[2][MAX_REGISTER_NUM] = {0};
  40. static struct WARNING_CODE_INFO gPreSysWarningInfo = {0};
  41. static AudiCustInfo *gAudiCustInfo = NULL;
  42. //------------------------------------------------------------------------------
  43. static void removeFaultCodeToBuf(uint8_t *Code);
  44. static void addFaultCodeToBuf(uint8_t *Code);
  45. static int readMiscCommand(int fd, uint8_t id);
  46. static int writeCsuModuleVersion(int fd);
  47. //------------------------------------------------------------------------------
  48. //--- Common function ---
  49. //------------------------------------------------------------------------------
  50. static int StoreLogMsg(const char *fmt, ...)
  51. {
  52. char Buf[4096 + 256];
  53. char buffer[4096];
  54. va_list args;
  55. struct timeb SeqEndTime;
  56. struct tm *tm;
  57. va_start(args, fmt);
  58. int rc = vsnprintf(buffer, sizeof(buffer), fmt, args);
  59. va_end(args);
  60. memset(Buf, 0, sizeof(Buf));
  61. ftime(&SeqEndTime);
  62. SeqEndTime.time = time(NULL);
  63. tm = localtime(&SeqEndTime.time);
  64. if (ShmSysConfigAndInfo->SysConfig.SwitchDebugFlag == 1) {
  65. sprintf(Buf, "%02d:%02d:%02d:%03d - %s",
  66. tm->tm_hour, tm->tm_min, tm->tm_sec, SeqEndTime.millitm, buffer);
  67. printf("%s \n", Buf);
  68. } else {
  69. sprintf(Buf, "echo \"%04d-%02d-%02d %02d:%02d:%02d:%03d - %s\" >> /Storage/SystemLog/[%04d.%02d]SystemLog",
  70. tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday, tm->tm_hour, tm->tm_min, tm->tm_sec, SeqEndTime.millitm,
  71. buffer,
  72. tm->tm_year + 1900, tm->tm_mon + 1);
  73. system(Buf);
  74. }
  75. return rc;
  76. }
  77. static int DiffTimeb(struct timeb ST, struct timeb ET)
  78. {
  79. //return milli-second
  80. unsigned int StartTime, StopTime;
  81. StartTime = (unsigned int)ST.time;
  82. StopTime = (unsigned int)ET.time;
  83. return (StopTime - StartTime) * 1000 + ET.millitm - ST.millitm;
  84. }
  85. /**
  86. * [hexdump : check data]
  87. * @Author Jerry
  88. * @DateTime 2018-12-21
  89. * @param p [description]
  90. * @param size [description]
  91. */
  92. static void Hexdump(const void *p, size_t size)
  93. {
  94. const uint8_t *c = p;
  95. char message[10240] = {0};
  96. uint32_t i = 0;
  97. uint32_t message_len = 0;
  98. assert(p);
  99. //printf("Dumping %u bytes from %p:\r\n", size, p);
  100. message_len += sprintf(&message[message_len], "\r\nDumping %u bytes from %p:\r\n", (unsigned int)size, p);
  101. while (size > 0) {
  102. for (i = 0; i < 16; i++) {
  103. if (i < size) {
  104. //printf("%02x ", c[i]);
  105. message_len += sprintf(&message[message_len], "%02x ", c[i]);
  106. } else {
  107. //printf(" ");
  108. message_len += sprintf(&message[message_len], " ");
  109. }
  110. }
  111. for (i = 0; i < 16; i++) {
  112. if (i < size) {
  113. //printf("%c", c[i] >= 32 && c[i] < 127 ? c[i] : '.');
  114. message_len += sprintf(&message[message_len], "%c", c[i] >= 32 && c[i] < 127 ? c[i] : '.');
  115. } else {
  116. //printf(" ");
  117. message_len += sprintf(&message[message_len], " ");
  118. }
  119. }
  120. //printf("\n");
  121. message_len += sprintf(&message[message_len], "\r\n");
  122. c += 16;
  123. if (size <= 16) {
  124. break;
  125. }
  126. size -= 16;
  127. }
  128. message_len += sprintf(&message[message_len], "\r\n");
  129. DEBUG_INFO("%s", message);
  130. }
  131. static int string2ByteArray(char *input, byte *output)
  132. {
  133. int loop = 0;
  134. int i = 0;
  135. while (input[loop] != '\0') {
  136. output[i++] = input[loop++];
  137. }
  138. output[loop] = '\0';
  139. return loop + 1;
  140. }
  141. static void unixSocketSigPipeHandle(int sig)
  142. {
  143. DEBUG_ERROR("socket packet error %x\r\n", sig);
  144. }
  145. static void InitSocketSigPipe(void)
  146. {
  147. struct sigaction action;
  148. action.sa_handler = unixSocketSigPipeHandle;
  149. sigemptyset(&action.sa_mask);
  150. action.sa_flags = 0;
  151. sigaction(SIGPIPE, &action, NULL);
  152. }
  153. //------------------------------------------------------------------------------
  154. static void setTcpStatus(uint8_t setValue)
  155. {
  156. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.DisconnectedFromDo = setValue;
  157. }
  158. //------------------------------------------------------------------------------
  159. //--- TCP socket function ---
  160. //------------------------------------------------------------------------------
  161. static int sendTcpSocket(int fd, uint8_t *data, uint16_t dataLen)
  162. {
  163. int size = -1;
  164. size = send(fd, data, dataLen , 0);
  165. if ((size < 0) || (errno == EAGAIN)) {
  166. if (size < 0) {
  167. DEBUG_ERROR("Size = %d, EAGAIN error %d:%s\r\n", size, errno, strerror(errno));
  168. }
  169. }
  170. return size;
  171. }
  172. static int recvTcpSocket(int fd, uint8_t *data, uint16_t dataLen)
  173. {
  174. int size = -1;
  175. uint8_t *pdata = (uint8_t *)data;
  176. size = recv(fd, pdata, dataLen, MSG_WAITALL);
  177. if ((errno == EAGAIN) || (size < 0)) {
  178. DEBUG_ERROR("Size = %d, EAGAIN error %d:%s\r\n", size, errno, strerror(errno));
  179. }
  180. return size;
  181. }
  182. static int getSO_ERROR(int fd)
  183. {
  184. int err = 1;
  185. socklen_t len = sizeof err;
  186. if (-1 == getsockopt(fd, SOL_SOCKET, SO_ERROR, (char *)&err, &len)) {
  187. DEBUG_ERROR("getSO_ERROR\r\n");
  188. }
  189. if (err) {
  190. errno = err; // set errno to the socket SO_ERROR
  191. }
  192. return err;
  193. }
  194. static void closeSocket(int fd) // *not* the Windows closesocket()
  195. {
  196. if (fd < 0) {
  197. return;
  198. }
  199. getSO_ERROR(fd); // first clear any errors, which can cause close to fail
  200. if (shutdown(fd, SHUT_RDWR) < 0) { // secondly, terminate the 'reliable' delivery
  201. if (errno != ENOTCONN && errno != EINVAL) { // SGI causes EINVAL
  202. DEBUG_INFO("shutdown\r\n");
  203. }
  204. }
  205. if (close(fd) < 0) { // finally call close()
  206. DEBUG_INFO("client socket close\r\n");
  207. }
  208. }
  209. static int doCommConnToServer(void)
  210. {
  211. struct sockaddr_in dest;
  212. struct timeval tv;
  213. int flag;
  214. int TcpSock = 0;
  215. //if (TcpSock > 0) {
  216. // close(TcpSock);
  217. //}
  218. if ((TcpSock = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
  219. DEBUG_ERROR("Open TCP socket NG");
  220. return -1;
  221. }
  222. //flag = fcntl (TcpSock, F_GETFL, 0);
  223. //if (flag >= 0) {
  224. // flag |= O_NONBLOCK;
  225. // fcntl(TcpSock, F_SETFL, flag );
  226. //}
  227. tv.tv_sec = 0;
  228. tv.tv_usec = 100000;
  229. setsockopt(TcpSock, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(struct timeval));
  230. setsockopt(TcpSock, SOL_SOCKET, SO_SNDTIMEO, &tv, sizeof(struct timeval));
  231. flag = 1;
  232. setsockopt(TcpSock, SOL_SOCKET, MSG_NOSIGNAL, &flag, sizeof(flag));
  233. memset(&dest, 0, sizeof(dest));
  234. dest.sin_family = AF_INET;
  235. dest.sin_port = htons(DoTcpPort);
  236. inet_aton(DoIPAddress, (struct in_addr *) &dest.sin_addr.s_addr);
  237. if (connect(TcpSock, (struct sockaddr *) &dest, sizeof(dest)) != 0) {
  238. close(TcpSock);
  239. return -1;
  240. }
  241. return TcpSock;
  242. }
  243. //------------------------------------------------------------------------------
  244. //--- Audi select gun ---
  245. //------------------------------------------------------------------------------
  246. static void clearPricesInfo(uint8_t id)
  247. {
  248. memset(&gAudiCustInfo->PricesInfo[id], 0, sizeof(PricesInfo));
  249. gAudiCustInfo->PricesInfo[id].Balance = FAIL_BALANCE_PRICES;
  250. }
  251. static void ClearAuthorizedFlag(void)
  252. {
  253. ShmOCPP16Data->SpMsg.bits.AuthorizeConf = NO;
  254. ShmSysConfigAndInfo->SysInfo.AuthorizeFlag = NO;
  255. }
  256. static void ClearDetectPluginFlag(void)
  257. {
  258. ShmSysConfigAndInfo->SysInfo.WaitForPlugit = NO;
  259. }
  260. static bool isDetectPlugin(void)
  261. {
  262. if (ShmSysConfigAndInfo->SysInfo.WaitForPlugit == YES) {
  263. return YES;
  264. }
  265. return NO;
  266. }
  267. static void destroySelectGun(uint8_t curGun)
  268. {
  269. uint8_t i = 0;
  270. uint8_t totalGun = ShmSysConfigAndInfo->SysConfig.TotalConnectorCount;
  271. if (curGun == DESTROY_ALL_SEL) {
  272. gAudiCustInfo->SelGunInfo.RightGun = SEL_GUN_RELEASE;
  273. gAudiCustInfo->SelGunInfo.LeftGun = SEL_GUN_RELEASE;
  274. for (i = 0; i < totalGun; i++) {
  275. ChargingData[i]->TimeoutFlag = Timeout_None;
  276. clearPricesInfo(i);
  277. }
  278. ShmSysConfigAndInfo->SysInfo.CurGunSelected = 0;
  279. strcpy((char *)ShmSysConfigAndInfo->SysConfig.UserId, "");
  280. }
  281. //for charging timeout or complete
  282. if ((curGun == LEFT_GUN_NUM) && (gAudiCustInfo->SelGunInfo.LeftGun != SEL_GUN_RELEASE)) {
  283. gAudiCustInfo->SelGunInfo.LeftGun = SEL_GUN_RELEASE;
  284. if (ShmOCPP16Data->SpMsg.bits.AuthorizeConf != NO) {
  285. ClearAuthorizedFlag();
  286. }
  287. clearPricesInfo(curGun);
  288. }
  289. if ((curGun == RIGHT_GUN_NUM) && (gAudiCustInfo->SelGunInfo.RightGun != SEL_GUN_RELEASE)) {
  290. if (ShmOCPP16Data->SpMsg.bits.AuthorizeConf != NO) {
  291. ClearAuthorizedFlag();
  292. }
  293. clearPricesInfo(curGun);
  294. }
  295. }
  296. static int getConfirmSelectedGun(uint8_t curSel)
  297. {
  298. if (((curSel == LEFT_GUN_NUM) && (gAudiCustInfo->SelGunInfo.LeftGun >= SEL_GUN_CONFIRM)) ||
  299. ((curSel == RIGHT_GUN_NUM) && (gAudiCustInfo->SelGunInfo.RightGun >= SEL_GUN_CONFIRM))) {
  300. return PASS;
  301. }
  302. return FAIL;
  303. }
  304. static int getSelGunWaitToAuthor(uint8_t curGun)
  305. {
  306. if (curGun == LEFT_GUN_NUM && gAudiCustInfo->SelGunInfo.LeftGun == SEL_GUN_ATHOR) {
  307. return FAIL;
  308. }
  309. if (curGun == RIGHT_GUN_NUM && gAudiCustInfo->SelGunInfo.RightGun == SEL_GUN_ATHOR) {
  310. return FAIL;
  311. }
  312. return PASS;
  313. }
  314. static void setConfirmSelGun(uint8_t selGun)
  315. {
  316. if (selGun == LEFT_GUN_NUM && gAudiCustInfo->SelGunInfo.LeftGun == SEL_GUN_RELEASE) {
  317. gAudiCustInfo->SelGunInfo.LeftGun = SEL_GUN_CONFIRM;
  318. //printf("confirmSelGun left\r\n");
  319. } else if (selGun == RIGHT_GUN_NUM && gAudiCustInfo->SelGunInfo.RightGun == SEL_GUN_RELEASE) {
  320. gAudiCustInfo->SelGunInfo.RightGun = SEL_GUN_CONFIRM;
  321. //printf("confirmSelGun right\r\n");
  322. }
  323. }
  324. //------------------------------------------------------------------------------
  325. //--- DoComm function ---
  326. //------------------------------------------------------------------------------
  327. static int compareOpcode(uint8_t opCode)
  328. {
  329. if (opCode != OP_WAIT_RESPONSE) {
  330. //DEBUG_ERROR("response operative code fail\r\n");
  331. return FAIL;
  332. }
  333. return PASS;
  334. }
  335. static int compareResult(uint8_t result)
  336. {
  337. if (result != COMMAND_RESULT_OK) {
  338. //DEBUG_ERROR("response result fail\r\n");
  339. return FAIL;
  340. }
  341. return PASS;
  342. }
  343. static int compareRegister(uint8_t srcReg, uint8_t destReg)
  344. {
  345. if (srcReg != destReg) {
  346. //DEBUG_ERROR("response register fail\r\n");
  347. return FAIL;
  348. }
  349. return PASS;
  350. }
  351. static float transPricesUnit(int prices)
  352. {
  353. //printf("prices = %.2f\r\n", prices * PRICES_UNIT);
  354. return (prices * PRICES_UNIT);
  355. }
  356. static void clearMiscCommand(void)
  357. {
  358. gDoCommGblData.MiscCmd = 0;
  359. }
  360. static int qrCodeUrlInfoHandle(uint8_t *data)
  361. {
  362. int len = 0;
  363. //char cmdBuf[128] = {0};
  364. char localTime[128] = {0};
  365. uint16_t timeLen = 0;
  366. struct timeb SeqEndTime;
  367. struct tm *tm;
  368. if ((char *)&data[0] == '\0') {
  369. DEBUG_ERROR("QR code date error\r\n");
  370. return FAIL;
  371. }
  372. //get local system time
  373. ftime(&SeqEndTime);
  374. SeqEndTime.time = time(NULL);
  375. tm = localtime(&SeqEndTime.time);
  376. timeLen = sprintf(localTime, "%04d-%02d-%02d %02d:%02d",
  377. tm->tm_year + 1900,
  378. tm->tm_mon + 1,
  379. tm->tm_mday,
  380. tm->tm_hour,
  381. tm->tm_min);
  382. //copy QR code string
  383. if (strncmp((char *)localTime, (char *)&data[0], timeLen - 2) != 0) {
  384. memset(ShmSysConfigAndInfo->SysConfig.SystemId, '\0', sizeof(ShmSysConfigAndInfo->SysConfig.SystemId));
  385. //printf("data = %s\r\n", data);
  386. len = string2ByteArray((char *)data, (uint8_t *)ShmSysConfigAndInfo->SysConfig.SystemId);
  387. //printf("SystemId = %s\r\n", ShmSysConfigAndInfo->SysConfig.SystemId);
  388. }
  389. //if ((char *)&data[len] == '\0') {
  390. // DEBUG_ERROR("power cabinet system date error\r\n");
  391. // return FAIL;
  392. //}
  393. //set system date
  394. //if (strncmp((char *)localTime, (char *)&data[len], timeLen) != 0) {
  395. // sprintf(cmdBuf, "date -s \"%s\" >> /dev/null", (char *)&data[len]);
  396. // system(cmdBuf);
  397. // DEBUG_INFO("local time = %s, data time = %s\r\n", localTime, (char *)&data[len]);
  398. // ShmOCPP16Data->MsMsg.bits.ResetReq = YES;
  399. // strcpy((char *)ShmOCPP16Data->Reset.Type, "Soft");
  400. // sleep(3);
  401. // //gDoCommGblData.DisConnCount = CHECK_NETWORK_FAIL_COUNT;
  402. //}
  403. return PASS;
  404. }
  405. static int updateFirmwareHandle(uint8_t *imgName)
  406. {
  407. int ret = PASS;
  408. char cmdBuf[1024] = {0};
  409. sprintf(cmdBuf, "/mnt/%s", imgName);
  410. DEBUG_INFO("Program ready to check file %s\n", cmdBuf);
  411. if ( access(cmdBuf, F_OK) != -1) {
  412. DEBUG_INFO("File '%s' exist.\n", cmdBuf);
  413. ShmSysConfigAndInfo->SysInfo.FirmwareUpdate = YES;
  414. } else {
  415. DEBUG_INFO("File '%s' doesn't exist.\n", cmdBuf);
  416. ret = FAIL;
  417. }
  418. #if 0
  419. char cmdBuf[1024] = {0};
  420. int status = 0;
  421. system("touch ./tftpUpdate.sh"); //創建shell
  422. sprintf(cmdBuf, "echo tftp -gr %s -l %s/%s %s; > ./tftpUpdate.sh",
  423. imgName,
  424. IMAGE_FILE_PATH,
  425. imgName,
  426. DoIPAddress);
  427. system("chmod +x ./tftpUpdate.sh"); //修改權限
  428. status = system("sh tftpUpdate.sh"); //執行shell
  429. if (-1 == status) {
  430. printf("system error!");
  431. } else {
  432. printf("exit status value = [0x%x]\n", status);
  433. if (WIFEXITED(status)) {
  434. if (0 == WEXITSTATUS(status)) {
  435. printf("run shell script successfully.\n");
  436. ShmSysConfigAndInfo->SysInfo.FirmwareUpdate = YES;
  437. } else {
  438. printf("run shell script fail, script exit code: %d\n", WEXITSTATUS(status));
  439. }
  440. } else {
  441. printf("exit status = [%d]\n", WEXITSTATUS(status));
  442. }
  443. }
  444. system("rm -rf ./tftpUpdate.sh"); //刪除shell
  445. #endif //0
  446. return ret;
  447. }
  448. static int miscCommandHandle(uint8_t dataLen, uint8_t plugNum, uint8_t *data)
  449. {
  450. int ret = PASS;
  451. uint8_t i = 0, j = 0;
  452. uint8_t cmdCount = (dataLen / 6);
  453. uint16_t cmd = 0;
  454. uint32_t value = 0;
  455. float prices = 0;
  456. MiscCommand *pMiscCmd = NULL;
  457. if (cmdCount < 1) {
  458. gDoCommGblData.MiscCmd = 0;
  459. //printf("cmdCount fail = %d, data len = %d\r\n", cmdCount, dataLen);
  460. return FAIL;
  461. }
  462. for (i = 0; i < cmdCount; i++) {
  463. pMiscCmd = (MiscCommand *)data + i;
  464. cmd = ntohs(pMiscCmd->CMD);
  465. value = ntohl(*((uint32_t *)&pMiscCmd->Value[0]));
  466. //DEBUG_INFO("misc command = %x, value = %d\r\n", cmd, value);
  467. switch (cmd) {
  468. //--- Execute parameter ---
  469. case MISC_CMD_CONNECOTOR_TIMEOUT:
  470. gAudiCustInfo->RemoteSetup.ConnectionTimeout = value;
  471. DEBUG_INFO("connection timeout = %d\r\n", gAudiCustInfo->RemoteSetup.ConnectionTimeout);
  472. clearMiscCommand();
  473. break;
  474. case MISC_CMD_OPERATIVE_STATE:
  475. if ((value > YES) || (value < NO)) {
  476. if (plugNum == 1) {
  477. clearMiscCommand();
  478. }
  479. break;
  480. }
  481. DEBUG_INFO("change availability plugNum = %d, value = %d\r\n", plugNum, value);
  482. ShmOCPP16Data->CsMsg.bits[plugNum].ChangeAvailabilityReq = YES;
  483. if (value == YES) {
  484. strcpy((char *)ShmOCPP16Data->ChangeAvailability[plugNum].Type, "Operative");
  485. } else if (value == NO) {
  486. strcpy((char *)ShmOCPP16Data->ChangeAvailability[plugNum].Type, "Inoperative");
  487. }
  488. if (plugNum == 1) {
  489. clearMiscCommand();
  490. }
  491. break;
  492. case MISC_CMD_DEFAULT_PRICES:
  493. prices = transPricesUnit((int)value);
  494. DEBUG_INFO("default prices = %.2f\r\n", prices);
  495. ShmSysConfigAndInfo->SysConfig.BillingData.isBilling = YES;
  496. for (j = 0; j < 24; j++) {
  497. ShmSysConfigAndInfo->SysConfig.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. DEBUG_INFO("default currency = %s%c\r\n", (uint8_t *)Currency[value]);
  507. ShmSysConfigAndInfo->SysConfig.BillingData.Currency = value;
  508. clearMiscCommand();
  509. break;
  510. case MISC_CMD_ACCOUNT_BALANCE:
  511. if (ShmSysConfigAndInfo->SysInfo.CurGunSelected != (plugNum)) {
  512. clearMiscCommand();
  513. break;
  514. }
  515. #if defined DD360Audi
  516. if (getConfirmSelectedGun(plugNum) == FAIL) {
  517. DEBUG_INFO("Remote start not select gun\r\n");
  518. break;
  519. }
  520. #endif //DD360Audi
  521. gAudiCustInfo->PricesInfo[plugNum].Balance = transPricesUnit((int)value);
  522. DEBUG_INFO("%d misc balance = %.2f\r\n", plugNum, gAudiCustInfo->PricesInfo[plugNum].Balance);
  523. clearMiscCommand();
  524. break;
  525. case MISC_CMD_BACKEND_STATUS :
  526. gAudiCustInfo->EthDevStatus.Backend = value;
  527. break;
  528. case MISC_CMD_ETHERNET_STATUS :
  529. gAudiCustInfo->EthDevStatus.Ethernet = value;
  530. break;
  531. case MISC_CMD_WIFI_STATUS :
  532. gAudiCustInfo->EthDevStatus.Wifi = value;
  533. break;
  534. case MISC_CMD_4G_STATUS :
  535. gAudiCustInfo->EthDevStatus.FourG = value;
  536. break;
  537. //--- Control Dispenser ---
  538. case MISC_CMD_HARDWARE_REBOOT:
  539. if (value != YES) {
  540. clearMiscCommand();
  541. break;
  542. }
  543. DEBUG_INFO("Hardware reboot\r\n");
  544. ShmOCPP16Data->MsMsg.bits.ResetReq = YES;
  545. strcpy((char *)ShmOCPP16Data->Reset.Type, "Hard");
  546. clearMiscCommand();
  547. break;
  548. case MISC_CMD_SOFTWARE_RESTART:
  549. if (value != YES) {
  550. clearMiscCommand();
  551. break;
  552. }
  553. clearMiscCommand();
  554. DEBUG_INFO("Software reboot\r\n");
  555. ShmOCPP16Data->MsMsg.bits.ResetReq = YES;
  556. strcpy((char *)ShmOCPP16Data->Reset.Type, "Soft");
  557. break;
  558. case MISC_CMD_REMOTE_START_CHARGING:
  559. if (value != YES) {
  560. if (plugNum == 1) {
  561. clearMiscCommand();
  562. }
  563. break;
  564. }
  565. DEBUG_INFO("Remote start charging plugNum = %d\r\n", plugNum);
  566. //if (getConfirmSelectedGun(plugNum - 1) == FAIL) {
  567. // DEBUG_INFO("Remote start not select gun\r\n");
  568. // break;
  569. //}
  570. #if defined DD360Audi
  571. if (getSelGunWaitToAuthor(plugNum) == FAIL) {
  572. DEBUG_ERROR("Remote start gun already charging\r\n");
  573. break;
  574. }
  575. if (ShmSysConfigAndInfo->SysInfo.CurGunSelected != (plugNum)) {
  576. if (ShmSysConfigAndInfo->SysInfo.CurGunSelected == LEFT_GUN_NUM &&
  577. (gAudiCustInfo->SelGunInfo.LeftGun == SEL_GUN_CONFIRM ||
  578. gAudiCustInfo->SelGunInfo.LeftGun == SEL_GUN_ATHOR)) {
  579. gAudiCustInfo->SelGunInfo.LeftGun = SEL_GUN_RELEASE;
  580. strcpy((char *)ShmSysConfigAndInfo->SysConfig.UserId, "");
  581. ShmSysConfigAndInfo->SysInfo.WaitForPlugit = NO;
  582. sleep(1); //Jerry add
  583. ShmSysConfigAndInfo->SysInfo.SystemPage = _LCM_SELECT_GUN;
  584. gettimeofday(&ShmSysConfigAndInfo->SysInfo.SystemTimeoutTimer, NULL);
  585. ShmSysConfigAndInfo->SysInfo.SystemTimeoutFlag = Timeout_None;
  586. }
  587. if (ShmSysConfigAndInfo->SysInfo.CurGunSelected == RIGHT_GUN_NUM &&
  588. (gAudiCustInfo->SelGunInfo.RightGun == SEL_GUN_CONFIRM ||
  589. gAudiCustInfo->SelGunInfo.RightGun == SEL_GUN_ATHOR)) {
  590. gAudiCustInfo->SelGunInfo.RightGun = SEL_GUN_RELEASE;
  591. strcpy((char *)ShmSysConfigAndInfo->SysConfig.UserId, "");
  592. ShmSysConfigAndInfo->SysInfo.WaitForPlugit = NO;
  593. sleep(1); //Jerry add
  594. ShmSysConfigAndInfo->SysInfo.SystemPage = _LCM_SELECT_GUN;
  595. gettimeofday(&ShmSysConfigAndInfo->SysInfo.SystemTimeoutTimer, NULL);
  596. ShmSysConfigAndInfo->SysInfo.SystemTimeoutFlag = Timeout_None;
  597. }
  598. ShmSysConfigAndInfo->SysInfo.CurGunSelected = (plugNum);
  599. ShmSysConfigAndInfo->SysInfo.CurGunSelectedByAc = NO_DEFINE;
  600. }
  601. #else
  602. ShmSysConfigAndInfo->SysInfo.CurGunSelected = (plugNum);
  603. ShmSysConfigAndInfo->SysInfo.CurGunSelectedByAc = NO_DEFINE;
  604. #endif //DD360Audi
  605. setConfirmSelGun(ShmSysConfigAndInfo->SysInfo.CurGunSelected);
  606. ShmOCPP16Data->CsMsg.bits[plugNum].RemoteStartTransactionReq = YES;
  607. gAudiCustInfo->PricesInfo[plugNum].Balance = 0.00;
  608. clearMiscCommand();
  609. break;
  610. case MISC_CMD_REMOTE_STOP_CHARGING:
  611. if (value != YES) {
  612. if (plugNum == 1) {
  613. clearMiscCommand();
  614. }
  615. break;
  616. }
  617. strcpy((char *)ShmSysConfigAndInfo->SysConfig.UserId, "");
  618. ShmSysConfigAndInfo->SysInfo.WaitForPlugit = NO;
  619. ShmSysConfigAndInfo->SysInfo.SystemPage = _LCM_SELECT_GUN;
  620. gettimeofday(&ShmSysConfigAndInfo->SysInfo.SystemTimeoutTimer, NULL);
  621. ShmSysConfigAndInfo->SysInfo.SystemTimeoutFlag = Timeout_None;
  622. destroySelectGun(plugNum);
  623. clearMiscCommand();
  624. break;
  625. case MISC_CMD_REMOTE_UNLOCK:
  626. if (value != YES) {
  627. if (plugNum == 1) {
  628. clearMiscCommand();
  629. }
  630. break;
  631. }
  632. if (isDetectPlugin() == YES) {
  633. ClearDetectPluginFlag();
  634. strcpy((char *)ShmSysConfigAndInfo->SysConfig.UserId, "");
  635. ShmSysConfigAndInfo->SysInfo.WaitForPlugit = NO;
  636. ShmSysConfigAndInfo->SysInfo.SystemPage = _LCM_SELECT_GUN;
  637. gettimeofday(&ShmSysConfigAndInfo->SysInfo.SystemTimeoutTimer, NULL);
  638. ShmSysConfigAndInfo->SysInfo.SystemTimeoutFlag = Timeout_None;
  639. destroySelectGun(plugNum);
  640. } else {
  641. ChargingData[plugNum]->StopChargeFlag = POWER_CABINET_STOP_CHARGING;
  642. }
  643. break;
  644. default:
  645. clearMiscCommand();
  646. break;
  647. }
  648. usleep(128);
  649. }
  650. return ret;
  651. }
  652. static int chargingcapabilityHandle(uint8_t *data, uint8_t plugNum)
  653. {
  654. uint8_t addr = 0;
  655. float MaxVolt, MaxCurrent, MaxPower;
  656. static PricesInfo pricesInfo[2] = {0};
  657. CapabilityInfo *pCapabilityInfo = NULL;
  658. AccountInfo *pAccountInfo = NULL;
  659. //--- charging capability information --------------------------------------
  660. pCapabilityInfo = (CapabilityInfo *)&data[0];
  661. MaxVolt = (float)(ntohs(pCapabilityInfo->OutputVoltage));
  662. if (MaxVolt >= 0) {
  663. if (MaxVolt > MAX_VOLTAGE) {
  664. MaxVolt = MAX_VOLTAGE;
  665. }
  666. ChargingData[plugNum]->MaximumChargingVoltage = MaxVolt;
  667. }
  668. MaxCurrent = (float)(ntohs(pCapabilityInfo->OutputCurrent));
  669. if (MaxCurrent >= 0) {
  670. if (MaxCurrent > MAX_CURRENCY) {
  671. MaxCurrent = MAX_CURRENCY;
  672. }
  673. ChargingData[plugNum]->AvailableChargingCurrent = MaxCurrent;
  674. }
  675. MaxPower = (float)(ntohs(pCapabilityInfo->OutputPower));
  676. if (MaxPower >= 0) {
  677. if (MaxPower > MAX_POWER) {
  678. MaxPower = MAX_POWER;
  679. }
  680. ChargingData[plugNum]->AvailableChargingPower = MaxPower;
  681. }
  682. //MaxVolt = (float)(data[0] << 8 |data[1]);
  683. //MaxCurrent = (float)(data[2] << 8 | data[3]);
  684. //MaxPower = (float)(data[4] << 8 | data[5]);
  685. //printf("MaxVolt=%f, MaxCurrent=%f, MaxPower=%f,\n", MaxVolt, MaxCurrent, MaxPower);
  686. //--- user prices information ----------------------------------------------
  687. addr = (sizeof(CapabilityInfo) - 2); //2 byte reserved
  688. pAccountInfo = (AccountInfo *)&data[addr];
  689. ShmSysConfigAndInfo->SysConfig.BillingData.Currency = pAccountInfo->Currency;
  690. gAudiCustInfo->PricesInfo[plugNum].UserPrices = transPricesUnit(ntohl(pAccountInfo->UserPrices));
  691. ChargingData[plugNum]->ChargingFee = transPricesUnit(ntohl(pAccountInfo->TotalCost));
  692. gAudiCustInfo->PricesInfo[plugNum].Balance = transPricesUnit(ntohl(pAccountInfo->Balance));
  693. if ((pricesInfo[plugNum].UserPrices != gAudiCustInfo->PricesInfo[plugNum].UserPrices) ||
  694. (pricesInfo[plugNum].Balance != gAudiCustInfo->PricesInfo[plugNum].Balance)) {
  695. pricesInfo[plugNum].UserPrices = gAudiCustInfo->PricesInfo[plugNum].UserPrices;
  696. pricesInfo[plugNum].Balance = gAudiCustInfo->PricesInfo[plugNum].Balance;
  697. DEBUG_INFO("id = %d, user prices = %.2f, Total cost = %.2f, Account balances = %.2f, currency = %s\r\n",
  698. plugNum,
  699. gAudiCustInfo->PricesInfo[plugNum].UserPrices,
  700. ChargingData[plugNum]->ChargingFee,
  701. gAudiCustInfo->PricesInfo[plugNum].Balance,
  702. (uint8_t *)Currency[ShmSysConfigAndInfo->SysConfig.BillingData.Currency]);
  703. }
  704. return PASS;
  705. }
  706. static void addFaultCodeToBuf(uint8_t *Code)
  707. {
  708. uint8_t warningCount = ShmSysConfigAndInfo->SysWarningInfo.WarningCount;
  709. if (warningCount < 10) {
  710. memcpy(&ShmSysConfigAndInfo->SysWarningInfo.WarningCode[warningCount][0],
  711. Code,
  712. strlen((char *)Code));
  713. ShmSysConfigAndInfo->SysWarningInfo.WarningCount++;
  714. }
  715. }
  716. static void removeFaultCodeToBuf(uint8_t *Code)
  717. {
  718. uint8_t find = 0x01;
  719. uint8_t i = 0;
  720. char _code[7] = {0};
  721. sprintf(_code, "%s", Code);
  722. // 把相關的錯誤碼一次移除,避免重複顯示
  723. while (find) {
  724. usleep(128);
  725. find = 0x00;
  726. for (i = 0; i < ShmSysConfigAndInfo->SysWarningInfo.WarningCount; i++) {
  727. usleep(128);
  728. if (find == 0x00) {
  729. if (memcmp(&ShmSysConfigAndInfo->SysWarningInfo.WarningCode[i][0], _code, 7) == 0) {
  730. find = 0x01;
  731. }
  732. } else {
  733. memcpy(&ShmSysConfigAndInfo->SysWarningInfo.WarningCode[i - 1][0],
  734. &ShmSysConfigAndInfo->SysWarningInfo.WarningCode[i][0], 7);
  735. }
  736. }
  737. if (find) {
  738. ShmSysConfigAndInfo->SysWarningInfo.WarningCount--;
  739. }
  740. }
  741. }
  742. bool CompareArrayIsZero(uint8_t *array, unsigned int array_size)
  743. {
  744. uint8_t i;
  745. if (array != NULL) {
  746. for (i = 0; i < array_size; i++) {
  747. if (array[i] != 0) {
  748. return false;
  749. }
  750. }
  751. }
  752. return true;
  753. }
  754. static int powerCabinetStatusProcess(uint8_t dataLen, uint8_t *data)
  755. {
  756. uint8_t ret = 0;
  757. uint8_t i = 0;
  758. uint8_t count = 0;
  759. uint8_t EventCodeTmp[7] = {0};
  760. uint8_t StatusArray[MAX_REGISTER_NUM][WARNING_CODE_SIZE] = {0};
  761. uint8_t remaindLen = 0;
  762. uint8_t statusCodeError = 0;
  763. //if (dataLen <= 0) {
  764. // return FAIL;
  765. //}
  766. if (dataLen > 0) {
  767. remaindLen = dataLen % WARNING_CODE_SIZE;
  768. if (remaindLen != 0) {
  769. dataLen -= remaindLen;
  770. }
  771. if (dataLen < WARNING_CODE_SIZE) {
  772. DEBUG_ERROR("fail status code length = %d\r\n", dataLen);
  773. Hexdump(data, dataLen);
  774. if (ShmSysConfigAndInfo->SysWarningInfo.WarningCount > 0) {
  775. for (i = 0; i < ShmSysConfigAndInfo->SysWarningInfo.WarningCount; i++) {
  776. usleep(128);
  777. if (ShmSysConfigAndInfo->SysWarningInfo.WarningCode[i][1] >= '3' || //from backend or power cabinet
  778. (ShmSysConfigAndInfo->SysWarningInfo.WarningCode[i][0] >= 'B' &&
  779. ShmSysConfigAndInfo->SysWarningInfo.WarningCode[i][1] >= '4')) {
  780. memset(EventCodeTmp, 0, sizeof(EventCodeTmp));
  781. memcpy(EventCodeTmp,
  782. ShmSysConfigAndInfo->SysWarningInfo.WarningCode[i],
  783. sizeof(EventCodeTmp));
  784. removeFaultCodeToBuf(EventCodeTmp);
  785. }
  786. }
  787. }
  788. return FAIL;
  789. }
  790. for (count = 0; count < dataLen; count += WARNING_CODE_SIZE) {
  791. usleep(128);
  792. // misc command status code handle
  793. if (strncmp((char *)&data[count], MISC_ST_MISC_CMD, WARNING_CODE_SIZE) == 0) {
  794. //if (gDoCommGblData.MiscCmd != REG_MISC_CONTROL) {
  795. gDoCommGblData.MiscCmd = REG_MISC_CONTROL;
  796. //}
  797. memset((char *)&data[count], 0, WARNING_CODE_SIZE);
  798. continue;
  799. } else if (strncmp((char *)&data[count], MISC_ST_VERSION, WARNING_CODE_SIZE) == 0) {
  800. //if (gDoCommGblData.MiscCmd != REG_REPORT_CSU_VERSION) {
  801. gDoCommGblData.MiscCmd = REG_REPORT_CSU_VERSION;
  802. //}
  803. memset((char *)&data[count], 0, WARNING_CODE_SIZE);
  804. continue;
  805. }
  806. if (CompareArrayIsZero((uint8_t *)&data[count], WARNING_CODE_SIZE) == true) {
  807. memset(EventCodeTmp, 0, sizeof(EventCodeTmp));
  808. memcpy(EventCodeTmp, (char *)&data[count], sizeof(EventCodeTmp));
  809. DEBUG_ERROR("error status = %s\r\n", EventCodeTmp);
  810. continue;
  811. }
  812. strncpy((char *)&StatusArray[count / WARNING_CODE_SIZE], (char *)&data[count], WARNING_CODE_SIZE);
  813. ret = 0;
  814. for (i = 0; i < ShmSysConfigAndInfo->SysWarningInfo.WarningCount; i++) {
  815. usleep(128);
  816. if (memcmp(&ShmSysConfigAndInfo->SysWarningInfo.WarningCode[i][0],
  817. StatusArray[count / WARNING_CODE_SIZE],
  818. WARNING_CODE_SIZE) == 0) {
  819. ret = 1;
  820. break;
  821. }
  822. }
  823. if (ret == 0) {
  824. addFaultCodeToBuf(StatusArray[count / WARNING_CODE_SIZE]);
  825. }
  826. //Rtn=StatusCodeProcessing(StatusArray[count/6]);
  827. }
  828. }
  829. for (i = 0; i < ShmSysConfigAndInfo->SysWarningInfo.WarningCount; i++) {
  830. usleep(128);
  831. //if (ShmSysConfigAndInfo->SysWarningInfo.WarningCode[i][0] >= 'B' &&
  832. // ShmSysConfigAndInfo->SysWarningInfo.WarningCode[i][1] >= '4') {
  833. // memset(EventCodeTmp, 0, sizeof(EventCodeTmp));
  834. // memcpy(EventCodeTmp,
  835. // ShmSysConfigAndInfo->SysWarningInfo.WarningCode[i],
  836. // sizeof(EventCodeTmp));
  837. // removeFaultCodeToBuf(EventCodeTmp);
  838. //}
  839. if (ShmSysConfigAndInfo->SysWarningInfo.WarningCode[i][1] >= '3' || //from backend or power cabinet 0x33
  840. (ShmSysConfigAndInfo->SysWarningInfo.WarningCode[i][0] >= 'B' &&
  841. ShmSysConfigAndInfo->SysWarningInfo.WarningCode[i][1] >= '4')) {
  842. ret = 0;
  843. for (count = 0; count < (dataLen / WARNING_CODE_SIZE); count++) {
  844. usleep(128);
  845. if (memcmp(&ShmSysConfigAndInfo->SysWarningInfo.WarningCode[i][0],
  846. StatusArray[count],
  847. WARNING_CODE_SIZE) == 0) {
  848. ret = 1;
  849. break;
  850. }
  851. }
  852. if (ret == 0) {
  853. memset(EventCodeTmp, 0, sizeof(EventCodeTmp));
  854. memcpy(EventCodeTmp,
  855. ShmSysConfigAndInfo->SysWarningInfo.WarningCode[i],
  856. sizeof(EventCodeTmp));
  857. removeFaultCodeToBuf(EventCodeTmp);
  858. }
  859. }
  860. }
  861. return PASS;
  862. }
  863. static int responsePackeHandle(int fd, uint8_t *pResult, uint8_t plugNum, uint8_t reg)
  864. {
  865. int ret = PASS;
  866. uint8_t rawDataLen = 0;
  867. CsuResultPkt *pCsuResult = (CsuResultPkt *)pResult;
  868. SoftwareUpdInfo *pSoftwareUpd = NULL;
  869. //Hexdump((uint8_t *)pCsuResult, sizeof(CmdHead) + pCsuResult->Head.DataLen);
  870. if (compareOpcode(pCsuResult->Head.OP) == FAIL ||
  871. compareResult(pCsuResult->Data.Result) == FAIL ||
  872. compareRegister(pCsuResult->Data.Register, reg) == FAIL) {
  873. if (reg != REG_CHARGING_PERMISSION) {
  874. return FAIL;
  875. }
  876. }
  877. rawDataLen = (pCsuResult->Head.DataLen - 2); //2 byte = register and result byte
  878. switch (pCsuResult->Data.Register) {
  879. case REG_MODEL_NAME:
  880. break;
  881. case REG_CONNECTOR_ID:
  882. gDoCommGblData.ConnectorID[0] = pCsuResult->Data.Data[0];
  883. gDoCommGblData.ConnectorID[1] = pCsuResult->Data.Data[1];
  884. DEBUG_INFO("OK (Left connector ID = %d, Right connector ID = %d)\n",
  885. pCsuResult->Data.Data[0],
  886. pCsuResult->Data.Data[1]);
  887. break;
  888. case REG_POWER_CABINET_STATUS:
  889. ret = powerCabinetStatusProcess(rawDataLen, pCsuResult->Data.Data);
  890. break;
  891. case REG_DISPENSER_STATUS:
  892. memset(&gPreSysWarningInfo, 0, sizeof(struct WARNING_CODE_INFO));
  893. memcpy(&gPreSysWarningInfo,
  894. &(ShmSysConfigAndInfo->SysWarningInfo),
  895. sizeof(struct WARNING_CODE_INFO));
  896. break;
  897. case REG_CHARGING_CAP:
  898. chargingcapabilityHandle(pCsuResult->Data.Data, plugNum);
  899. break;
  900. case REG_CHARGING_TARGET:
  901. break;
  902. case REG_SOFTWARE_UPDATE:
  903. pSoftwareUpd = (SoftwareUpdInfo *)&pCsuResult->Data.Data[0];
  904. if ((strcmp((char *)pSoftwareUpd->ImgName, "") == 0) ||
  905. (rawDataLen == 0) ||
  906. pSoftwareUpd->UpdateState != 1) {
  907. ret = FAIL;
  908. break;
  909. }
  910. ret = FAIL;
  911. ret = updateFirmwareHandle(pSoftwareUpd->ImgName);
  912. break;
  913. case REG_PLUG_IN_STATE:
  914. break;
  915. case REG_CONNECTOR_STATE:
  916. break;
  917. case REG_USER_ID:
  918. //DEBUG_INFO("%s\n", pCsuResult->Data.Data[0] == 1 ? "Accept" : "Reject" );
  919. //if (pCsuResult->Data.Data[0] <= 0) {
  920. // return FAIL;
  921. //}
  922. if (pCsuResult->Data.Result == COMMAND_RESULT_NG) {
  923. return COMMAND_RESULT_NG;
  924. }
  925. return pCsuResult->Data.Data[0];
  926. break;
  927. case REG_CHARGING_PERMISSION:
  928. //DEBUG_INFO("id = %d, result = %s, action = %s\r\n",
  929. // pCsuResult->Head.ID,
  930. // pCsuResult->Data.Result == 1 ? "OK" : "NG",
  931. // pCsuResult->Data.Data[0] == 1 ? "Permitted" : "NOT permitted");
  932. if (pCsuResult->Data.Result == COMMAND_RESULT_NG) {
  933. return COMMAND_RESULT_NG;
  934. }
  935. return pCsuResult->Data.Data[0];
  936. break;
  937. case REG_MISC_CONTROL:
  938. miscCommandHandle(rawDataLen, plugNum, pCsuResult->Data.Data);
  939. break;
  940. case REG_REPORT_CSU_VERSION:
  941. case REG_REPORT_OTHER_VERSION:
  942. break;
  943. case REG_PRESENT_CHARGING_INFO:
  944. break;
  945. case REG_QRCODE_URL_INFO:
  946. ret = qrCodeUrlInfoHandle(pCsuResult->Data.Data);
  947. break;
  948. case REG_WAIT_PLUG_IT_STATE:
  949. break;
  950. default:
  951. break;
  952. }
  953. return ret;
  954. }
  955. static int composeSocketData(int fd,
  956. uint8_t id,
  957. uint8_t opCode,
  958. uint8_t reg,
  959. uint8_t dataLen,
  960. uint8_t *data)
  961. {
  962. int ret = PASS;
  963. int size = 0;
  964. int sendPktLen = 0;
  965. uint8_t i = 0;
  966. uint8_t plugNum = 0;
  967. CsuCmdPkt csuCmdPkt = {0};
  968. CsuResultPkt csuResult = {0};
  969. csuCmdPkt.Head.SeqNum = gDoCommGblData.SeqNum++;
  970. csuCmdPkt.Head.ID = id;
  971. csuCmdPkt.Head.OP = opCode;
  972. csuCmdPkt.Head.DataLen = dataLen + 1; //+1 for register byte
  973. csuCmdPkt.Data.Register = reg;
  974. if ((data != NULL) && (dataLen > 0)) {
  975. memcpy(csuCmdPkt.Data.Data, data, dataLen);
  976. }
  977. sendPktLen = csuCmdPkt.Head.DataLen + sizeof(csuCmdPkt.Head);
  978. //Hexdump((uint8_t *)&csuCmdPkt, sendPktLen);
  979. //send command packet
  980. if ((size = sendTcpSocket(fd, (uint8_t *)&csuCmdPkt, sendPktLen)) < 0) {
  981. DEBUG_ERROR("TCP socket send packet fail = %d\r\n", size);
  982. gDoCommGblData.DisConnCount++;
  983. return FAIL;
  984. }
  985. //receive result head
  986. if ((size = recvTcpSocket(fd, (uint8_t *)&csuResult.Head, sizeof(csuResult.Head))) < 0) {
  987. DEBUG_ERROR("TCP socket RX head fail = %d\r\n", size);
  988. gDoCommGblData.DisConnCount++;
  989. return FAIL;
  990. }
  991. //receive result raw data
  992. if ((size = recvTcpSocket(fd, (uint8_t *)&csuResult.Data, csuResult.Head.DataLen)) < 0) {
  993. DEBUG_ERROR("TCP socket RX data fail = %d\r\n", size);
  994. gDoCommGblData.DisConnCount++;
  995. return FAIL;
  996. }
  997. for (i = 0; i < sizeof(gDoCommGblData.ConnectorID); i++) {
  998. if (id == gDoCommGblData.ConnectorID[i]) {
  999. plugNum = i;
  1000. break;
  1001. }
  1002. }
  1003. ret = responsePackeHandle(fd, (uint8_t *)&csuResult, plugNum, csuCmdPkt.Data.Register);
  1004. return ret;
  1005. }
  1006. static int writeWaitPlugItState(int fd, uint8_t id)
  1007. {
  1008. int ret = PASS;
  1009. uint8_t data[2] = {ShmSysConfigAndInfo->SysInfo.WaitForPlugit, 0};
  1010. //printf("WaitForPlugit = %d\r\n", ShmSysConfigAndInfo->SysInfo.WaitForPlugit);
  1011. if ((ret = composeSocketData(fd,
  1012. id,
  1013. OP_WRITE_DATA,
  1014. REG_WAIT_PLUG_IT_STATE,
  1015. 1,
  1016. &data[0])) == FAIL) {
  1017. return ret;
  1018. }
  1019. return ret;
  1020. }
  1021. static int readQRcodeURLAndSystemDate(int fd)
  1022. {
  1023. int ret = PASS;
  1024. ret = composeSocketData(fd,
  1025. ID_REGISTER,
  1026. OP_READ_DATA,
  1027. REG_QRCODE_URL_INFO,
  1028. 0,
  1029. NULL);
  1030. return ret;
  1031. }
  1032. static int writePresentChargingInfo(int fd, uint8_t plugNum, uint8_t id)
  1033. {
  1034. int ret = PASS;
  1035. uint8_t dataBuf[16] = {0};
  1036. PreChargingInfo *pPreChargingInfo = (PreChargingInfo *)dataBuf;
  1037. pPreChargingInfo->PresentChargingVoltage = htons((uint16_t)ChargingData[plugNum]->PresentChargingVoltage * 10);
  1038. pPreChargingInfo->PresentChargingCurrent = htons((uint16_t)ChargingData[plugNum]->PresentChargingCurrent * 10);
  1039. pPreChargingInfo->RemainChargingDuration = htonl(ChargingData[plugNum]->RemainChargingDuration);
  1040. pPreChargingInfo->EvBatterySoc = ChargingData[plugNum]->EvBatterySoc;
  1041. ret = composeSocketData(fd,
  1042. id,
  1043. OP_WRITE_DATA,
  1044. REG_PRESENT_CHARGING_INFO,
  1045. sizeof(PreChargingInfo),
  1046. (uint8_t *)pPreChargingInfo);
  1047. return ret;
  1048. }
  1049. static int writeOtherModuleVersion(int fd)
  1050. {
  1051. int ret = PASS;
  1052. uint8_t data[255] = {0};
  1053. uint8_t dataLen = 0;
  1054. //report other module version message
  1055. memcpy(&data[dataLen], ShmSysConfigAndInfo->SysInfo.FanModuleFwRev, VERSION_BUF_SIZE);
  1056. dataLen += VERSION_BUF_SIZE;
  1057. memcpy(&data[dataLen], ShmSysConfigAndInfo->SysInfo.RelayModuleFwRev, VERSION_BUF_SIZE);
  1058. dataLen += VERSION_BUF_SIZE;
  1059. memcpy(&data[dataLen], ShmSysConfigAndInfo->SysInfo.Connector1FwRev, VERSION_BUF_SIZE);
  1060. dataLen += VERSION_BUF_SIZE;
  1061. memcpy(&data[dataLen], ShmSysConfigAndInfo->SysInfo.Connector2FwRev, VERSION_BUF_SIZE);
  1062. dataLen += VERSION_BUF_SIZE;
  1063. memcpy(&data[dataLen], ShmSysConfigAndInfo->SysInfo.LedModuleFwRev, VERSION_BUF_SIZE);
  1064. dataLen += VERSION_BUF_SIZE;
  1065. ret = composeSocketData(fd,
  1066. ID_REGISTER,
  1067. OP_WRITE_DATA,
  1068. REG_REPORT_OTHER_VERSION,
  1069. dataLen,
  1070. &data[0]);
  1071. return ret;
  1072. }
  1073. static int writeCsuModuleVersion(int fd)
  1074. {
  1075. int ret = PASS;
  1076. uint8_t data[255] = {0};
  1077. uint8_t dataLen = 0;
  1078. //report CSU platform version message
  1079. memcpy(&data[dataLen], ShmSysConfigAndInfo->SysInfo.CsuBootLoadFwRev, VERSION_BUF_SIZE);
  1080. dataLen += VERSION_BUF_SIZE;
  1081. memcpy(&data[dataLen], ShmSysConfigAndInfo->SysInfo.CsuKernelFwRev, VERSION_BUF_SIZE);
  1082. dataLen += VERSION_BUF_SIZE;
  1083. memcpy(&data[dataLen], ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev, VERSION_BUF_SIZE);
  1084. dataLen += VERSION_BUF_SIZE;
  1085. memcpy(&data[dataLen], ShmSysConfigAndInfo->SysInfo.CsuPrimFwRev, VERSION_BUF_SIZE);
  1086. dataLen += VERSION_BUF_SIZE;
  1087. ret = composeSocketData(fd,
  1088. ID_REGISTER,
  1089. OP_WRITE_DATA,
  1090. REG_REPORT_CSU_VERSION,
  1091. dataLen,
  1092. &data[0]);
  1093. return ret;
  1094. }
  1095. static int readMiscCommand(int fd, uint8_t id)
  1096. {
  1097. int ret = PASS;
  1098. ret = composeSocketData(fd,
  1099. id,
  1100. OP_READ_DATA,
  1101. REG_MISC_CONTROL,
  1102. 0,
  1103. NULL);
  1104. return ret;
  1105. }
  1106. static int readChargePermission(int fd, uint8_t id)
  1107. {
  1108. //int ret = PASS;
  1109. //if ((ret = composeSocketData(fd,
  1110. // id,
  1111. // OP_READ_DATA,
  1112. // REG_CHARGING_PERMISSION,
  1113. // 0,
  1114. // NULL)) == FAIL) {
  1115. // return ret;
  1116. //}
  1117. return composeSocketData(fd,
  1118. id,
  1119. OP_READ_DATA,
  1120. REG_CHARGING_PERMISSION,
  1121. 0,
  1122. NULL);
  1123. }
  1124. static int writeUserID(int fd, uint8_t id, uint8_t *pUserID)
  1125. {
  1126. if ((strlen((char *)pUserID) <= 0) || (pUserID == NULL)) {
  1127. return FAIL;
  1128. }
  1129. return composeSocketData(fd,
  1130. id,
  1131. OP_WRITE_DATA,
  1132. REG_USER_ID,
  1133. strlen((char *)pUserID),
  1134. pUserID);
  1135. }
  1136. static int writeConnectorState(int fd, uint8_t plugNum, uint8_t id)
  1137. {
  1138. uint8_t dataBuf[8] = {0};
  1139. int ret = PASS;
  1140. if (ChargingData[plugNum]->SystemStatus <= S_AUTHORIZING) {
  1141. dataBuf[0] = CONN_ST_IDLE; //idle
  1142. strcpy((char *)ShmOCPP16Data->StatusNotification[plugNum].VendorErrorCode, "");
  1143. } else if ((ChargingData[plugNum]->SystemStatus <= S_PREPARING_FOR_EVSE) ||
  1144. (ChargingData[plugNum]->SystemStatus == S_CCS_PRECHARGE_ST0) ||
  1145. (ChargingData[plugNum]->SystemStatus == S_CCS_PRECHARGE_ST1)) {
  1146. dataBuf[0] = CONN_ST_PREPARING; //preparing
  1147. } else if (ChargingData[plugNum]->SystemStatus == S_CHARGING) {
  1148. dataBuf[0] = CONN_ST_CHARGING; //charging
  1149. } else if (ChargingData[plugNum]->SystemStatus == S_TERMINATING) {
  1150. dataBuf[0] = CONN_ST_TERMINATING; //terminating
  1151. } else if ((ChargingData[plugNum]->SystemStatus == S_ALARM) ||
  1152. (ChargingData[plugNum]->SystemStatus == S_FAULT)) {
  1153. dataBuf[0] = CONN_ST_ALARM;
  1154. if (strcmp((char *)ShmOCPP16Data->StatusNotification[plugNum].VendorErrorCode, "") != 0) {
  1155. strncpy((char *)&dataBuf[1], (char *)ShmOCPP16Data->StatusNotification[plugNum].VendorErrorCode, WARNING_CODE_SIZE);
  1156. DEBUG_INFO("ID = %d, VendorErrorCode = %s\r\n", plugNum, (char *)ShmOCPP16Data->StatusNotification[plugNum].VendorErrorCode);
  1157. }
  1158. }
  1159. ret = composeSocketData(fd,
  1160. id,
  1161. OP_WRITE_DATA,
  1162. REG_CONNECTOR_STATE,
  1163. sizeof(dataBuf) - 1,
  1164. &dataBuf[0]);
  1165. return ret;
  1166. }
  1167. static int writePlugInStatus(int fd, uint8_t plugNum, uint8_t id)
  1168. {
  1169. int ret = PASS;
  1170. uint8_t dataBuf[2] = {ChargingData[plugNum]->ConnectorPlugIn, 0};
  1171. ret = composeSocketData(fd,
  1172. id,
  1173. OP_WRITE_DATA,
  1174. REG_PLUG_IN_STATE,
  1175. 1,
  1176. &dataBuf[0]);
  1177. return ret;
  1178. }
  1179. static int readSoftwareUpdate(int fd, uint8_t gunID)
  1180. {
  1181. int ret = PASS;
  1182. uint8_t dataBuf[2] = {ShmSysConfigAndInfo->SysInfo.FirmwareUpdate, 0};
  1183. ret = composeSocketData(fd,
  1184. gunID,
  1185. OP_READ_DATA,
  1186. REG_SOFTWARE_UPDATE,
  1187. 1,
  1188. &dataBuf[0]);
  1189. return ret;
  1190. }
  1191. static int writeChargingTarget(int fd, uint8_t plugNum, uint8_t id)
  1192. {
  1193. int ret = PASS;
  1194. float ChargingVolt, ChargingAmp;
  1195. uint8_t dataBuf[4] = {0};
  1196. ChargingVolt = ChargingData[plugNum]->EvBatterytargetVoltage;
  1197. ChargingAmp = ChargingData[plugNum]->EvBatterytargetCurrent;
  1198. ChargingVolt *= 10;
  1199. ChargingAmp *= 10;
  1200. dataBuf[0] = ((uint16_t)ChargingVolt >> 8) & 0xFF;
  1201. dataBuf[1] = ((uint16_t)ChargingVolt) & 0xFF;
  1202. dataBuf[2] = ((uint16_t)ChargingAmp >> 8) & 0xFF;
  1203. dataBuf[3] = ((uint16_t)ChargingAmp) & 0xFF;
  1204. ret = composeSocketData(fd,
  1205. id,
  1206. OP_WRITE_DATA,
  1207. REG_CHARGING_TARGET,
  1208. sizeof(dataBuf),
  1209. &dataBuf[0]);
  1210. return ret;
  1211. }
  1212. static int readChargingCapability(int fd, uint8_t id)
  1213. {
  1214. int ret = PASS;
  1215. ret = composeSocketData(fd,
  1216. id,
  1217. OP_READ_DATA,
  1218. REG_CHARGING_CAP,
  1219. 0,
  1220. NULL);
  1221. return ret;
  1222. }
  1223. static int writeDispenserStatus(int fd, uint8_t gunID)
  1224. {
  1225. uint8_t warningCount = 0;
  1226. uint8_t count = 0;
  1227. uint8_t CurWarnCodeTmp[10][6] = {0};
  1228. uint8_t PreWarnCodeTmp[10][6] = {0};
  1229. int ret = PASS;
  1230. if ((ShmSysConfigAndInfo->SysWarningInfo.WarningCount <= 0) &&
  1231. (gPreSysWarningInfo.WarningCount <= 0)) {
  1232. return FAIL;
  1233. }
  1234. warningCount = ShmSysConfigAndInfo->SysWarningInfo.WarningCount;
  1235. for (count = 0; count < warningCount; count++) {
  1236. memcpy(CurWarnCodeTmp[count], ShmSysConfigAndInfo->SysWarningInfo.WarningCode[count], WARNING_CODE_SIZE);
  1237. }
  1238. warningCount = gPreSysWarningInfo.WarningCount;
  1239. for (count = 0; count < warningCount; count++) {
  1240. memcpy(PreWarnCodeTmp[count], gPreSysWarningInfo.WarningCode[count], WARNING_CODE_SIZE);
  1241. }
  1242. if (strcmp((char *)&CurWarnCodeTmp[0], (char *)&PreWarnCodeTmp[0]) == 0) {
  1243. return FAIL;
  1244. }
  1245. ret = composeSocketData(fd,
  1246. gunID,
  1247. OP_WRITE_DATA,
  1248. REG_DISPENSER_STATUS,
  1249. strlen((char *)CurWarnCodeTmp),
  1250. &CurWarnCodeTmp[0][0]);
  1251. return ret;
  1252. }
  1253. static int readPowerCabinetStatus(int fd, uint8_t gunID)
  1254. {
  1255. int ret = PASS;
  1256. ret = composeSocketData(fd,
  1257. gunID,
  1258. OP_READ_DATA,
  1259. REG_POWER_CABINET_STATUS,
  1260. 0,
  1261. NULL);
  1262. return ret;
  1263. }
  1264. static int readConnectorID(int fd)
  1265. {
  1266. int ret = PASS;
  1267. ret = composeSocketData(fd,
  1268. ID_REGISTER,
  1269. OP_READ_DATA,
  1270. REG_CONNECTOR_ID,
  1271. 0,
  1272. NULL);
  1273. return ret;
  1274. }
  1275. static int WriteModelName(int fd)
  1276. {
  1277. int ret = PASS;
  1278. uint8_t Tmp = 0;
  1279. uint8_t TmpBuf[255] = {0};
  1280. memcpy(TmpBuf,
  1281. ShmSysConfigAndInfo->SysConfig.ModelName,
  1282. strlen((char *)ShmSysConfigAndInfo->SysConfig.ModelName));
  1283. Tmp = (uint8_t)(ShmPrimaryMcuData->InputDet.bits.Key2 << 2 |
  1284. ShmPrimaryMcuData->InputDet.bits.Key1 << 1 |
  1285. ShmPrimaryMcuData->InputDet.bits.Key0);
  1286. TmpBuf[strlen((char *)ShmSysConfigAndInfo->SysConfig.ModelName)] = Tmp; //Dispenser switch value
  1287. ret = composeSocketData(fd,
  1288. ID_REGISTER,
  1289. OP_WRITE_DATA,
  1290. REG_MODEL_NAME,
  1291. strlen((char *)ShmSysConfigAndInfo->SysConfig.ModelName) + 1,
  1292. &TmpBuf[0]);
  1293. return ret;
  1294. }
  1295. static void calDisconnectCount(uint8_t *pValue, uint8_t maxCount)
  1296. {
  1297. sleep(3); //wait 1 second
  1298. if ((*pValue) >= maxCount) {
  1299. setTcpStatus(ABNORMAL);
  1300. } else {
  1301. (*pValue)++;
  1302. }
  1303. }
  1304. static int CheckNetworkStatus(void)
  1305. {
  1306. char *ipAddr = (char *)&ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthIpAddress;
  1307. //printf("Check network IP Header = %s\n", ipAddr);
  1308. if (strstr(ipAddr, CMP_ETH_IP_HEAD) != NULL) {
  1309. return 1;
  1310. }
  1311. return 0;
  1312. }
  1313. static int FindChargingInfoData(byte target, struct ChargingInfoData **chargingData)
  1314. {
  1315. uint8_t index = 0;
  1316. for (index = 0; index < CHAdeMO_QUANTITY; index++) {
  1317. if (ShmSysConfigAndInfo->SysInfo.ChademoChargingData[index].Index == target) {
  1318. chargingData[target] = &ShmSysConfigAndInfo->SysInfo.ChademoChargingData[index];
  1319. return 1;
  1320. }
  1321. }
  1322. for (index = 0; index < CCS_QUANTITY; index++) {
  1323. if (ShmSysConfigAndInfo->SysInfo.CcsChargingData[index].Index == target) {
  1324. chargingData[target] = &ShmSysConfigAndInfo->SysInfo.CcsChargingData[index];
  1325. return 1;
  1326. }
  1327. }
  1328. for (index = 0; index < GB_QUANTITY; index++) {
  1329. if (ShmSysConfigAndInfo->SysInfo.GbChargingData[index].Index == target) {
  1330. chargingData[target] = &ShmSysConfigAndInfo->SysInfo.GbChargingData[index];
  1331. return 1;
  1332. }
  1333. }
  1334. return 0;
  1335. }
  1336. static int InitShareMemory(void)
  1337. {
  1338. int result = 1;
  1339. int MeterSMId = 0;
  1340. if ((MeterSMId = shmget(ShmSysConfigAndInfoKey, sizeof(struct SysConfigAndInfo), 0777)) < 0) {
  1341. DEBUG_ERROR("shmget ShmSysConfigAndInfo NG\n");
  1342. result = 0;
  1343. } else if ((ShmSysConfigAndInfo = shmat(MeterSMId, NULL, 0)) == (void *) - 1) {
  1344. DEBUG_ERROR("[shmat ShmSysConfigAndInfo NG\n");
  1345. result = 0;
  1346. }
  1347. if ((MeterSMId = shmget(ShmStatusCodeKey, sizeof(struct StatusCodeData), 0777)) < 0) {
  1348. DEBUG_ERROR("shmget ShmStatusCodeData NG\n");
  1349. result = 0;
  1350. } else if ((ShmStatusCodeData = shmat(MeterSMId, NULL, 0)) == (void *) - 1) {
  1351. DEBUG_ERROR("shmat ShmStatusCodeData NG\n");
  1352. result = 0;
  1353. }
  1354. //creat ShmPsuData
  1355. if ((MeterSMId = shmget(ShmPsuKey, sizeof(struct PsuData), 0777)) < 0) {
  1356. DEBUG_ERROR("shmget ShmPsuData NG \n");
  1357. result = 0;
  1358. } else if ((ShmPsuData = shmat(MeterSMId, NULL, 0)) == (void *) - 1) {
  1359. DEBUG_ERROR("shmat ShmPsuData NG \n");
  1360. result = 0;
  1361. }
  1362. //creat ShmOCPP16Data
  1363. if ((MeterSMId = shmget(ShmOcppModuleKey, sizeof(struct OCPP16Data), 0777)) < 0) {
  1364. DEBUG_ERROR("shmget ShmOCPP16Data NG \n");
  1365. result = 0;
  1366. } else if ((ShmOCPP16Data = shmat(MeterSMId, NULL, 0)) == (void *) - 1) {
  1367. DEBUG_ERROR("shmat ShmOCPP16Data NG \n");
  1368. result = 0;
  1369. }
  1370. if ((MeterSMId = shmget(ShmPrimaryMcuKey, sizeof(struct PrimaryMcuData), 0777)) < 0) {
  1371. DEBUG_ERROR("shmget ShmPrimaryMcuData NG\n");
  1372. result = 0;
  1373. } else if ((ShmPrimaryMcuData = shmat(MeterSMId, NULL, 0)) == (void *) - 1) {
  1374. DEBUG_ERROR("shmat ShmPrimaryMcuData NG\n");
  1375. result = 0;
  1376. }
  1377. //creat Audi customization info
  1378. if ((MeterSMId = shmget(ShmAudiCustInfoKey, sizeof(AudiCustInfo), IPC_CREAT | 0777)) < 0) {
  1379. DEBUG_ERROR("[main]CreatShareMemory:shmget select gun info NG \n");
  1380. return 0;
  1381. } else if ((gAudiCustInfo = shmat(MeterSMId, NULL, 0)) == (void *) - 1) {
  1382. DEBUG_ERROR("[main]CreatShareMemory:shmat shmget select gun info \n");
  1383. return 0;
  1384. }
  1385. return result;
  1386. }
  1387. static void updateFirmwareProcess(int fd, uint8_t gunID, uint8_t totalConnCount)
  1388. {
  1389. bool canUpdateFirmware = true;
  1390. uint8_t plugNum = 0;
  1391. uint8_t ackCount = 5;
  1392. struct timeb updateTime;
  1393. if (ShmSysConfigAndInfo->SysWarningInfo.Level != 2) {
  1394. for (plugNum = 0; plugNum < totalConnCount; plugNum++) {
  1395. if (ChargingData[plugNum]->SystemStatus != S_IDLE &&
  1396. ChargingData[plugNum]->SystemStatus != S_RESERVATION &&
  1397. ChargingData[plugNum]->SystemStatus != S_MAINTAIN) {
  1398. canUpdateFirmware = false;
  1399. }
  1400. }
  1401. }
  1402. if (canUpdateFirmware) {
  1403. ftime(&updateTime);
  1404. if (DiffTimeb(gRegTimeUp[0][REG_SOFTWARE_UPDATE], updateTime) > LOOP_RETRY_TIME * 5) {
  1405. readSoftwareUpdate(fd, gunID);
  1406. ftime(&gRegTimeUp[0][REG_SOFTWARE_UPDATE]);
  1407. }
  1408. if (ShmSysConfigAndInfo->SysInfo.FirmwareUpdate == YES) {
  1409. while (ackCount != 0) {
  1410. ftime(&updateTime);
  1411. if (DiffTimeb(gRegTimeUp[0][REG_SOFTWARE_UPDATE], updateTime) > LOOP_RETRY_TIME / 2) {
  1412. readSoftwareUpdate(fd, gunID);
  1413. ftime(&gRegTimeUp[0][REG_SOFTWARE_UPDATE]);
  1414. if (ShmSysConfigAndInfo->SysInfo.FirmwareUpdate == NO) {
  1415. ackCount--;
  1416. }
  1417. }
  1418. usleep(128);
  1419. }
  1420. }
  1421. }
  1422. }
  1423. static void checkAuthorProcess(int fd, uint8_t plugNum)
  1424. {
  1425. int ret = 0;
  1426. uint8_t gunID = 0;
  1427. struct timeb AuthNowTime;
  1428. #if defined DD360Audi
  1429. gunID = gDoCommGblData.ConnectorID[ShmSysConfigAndInfo->SysInfo.CurGunSelected];
  1430. //gunID = gDoCommGblData.ConnectorID[plugNum];
  1431. #else
  1432. gunID = ID_REGISTER;
  1433. #endif // DD360Audi
  1434. if ((ShmOCPP16Data->SpMsg.bits.AuthorizeReq == YES) || (ShmSysConfigAndInfo->SysInfo.AuthorizeFlag == YES)) {
  1435. ftime(&AuthNowTime);
  1436. if (DiffTimeb(gRegTimeUp[plugNum][REG_USER_ID], AuthNowTime) > LOOP_RETRY_TIME) {
  1437. ret = writeUserID(fd,
  1438. gunID,
  1439. ShmSysConfigAndInfo->SysConfig.UserId);
  1440. if (ret >= 0) {
  1441. memset(ShmOCPP16Data->Authorize.ResponseIdTagInfo.Status,
  1442. 0,
  1443. sizeof(ShmOCPP16Data->Authorize.ResponseIdTagInfo.Status));
  1444. if (ret == 0) {
  1445. strcpy((char *)ShmOCPP16Data->Authorize.ResponseIdTagInfo.Status, "Invalid");
  1446. } else {
  1447. strcpy((char *)ShmOCPP16Data->Authorize.ResponseIdTagInfo.Status, "Accepted");
  1448. }
  1449. //printf("gAudiCustInfo->PricesInfo[plugNum].Balance = %.2f, %.2f\r\n",
  1450. // gAudiCustInfo->PricesInfo[plugNum].Balance,
  1451. // FAIL_BALANCE_PRICES);
  1452. #if defined DD360Audi
  1453. if (gAudiCustInfo->PricesInfo[plugNum].Balance != FAIL_BALANCE_PRICES)
  1454. #else
  1455. if (gAudiCustInfo->PricesInfo[plugNum].Balance == FAIL_BALANCE_PRICES) {
  1456. gAudiCustInfo->PricesInfo[plugNum].Balance = 0;
  1457. }
  1458. #endif //
  1459. {
  1460. ShmOCPP16Data->SpMsg.bits.AuthorizeConf = 1; //isAuthorizedComplete
  1461. ShmOCPP16Data->SpMsg.bits.AuthorizeReq = 0;
  1462. ShmSysConfigAndInfo->SysInfo.AuthorizeFlag = 0;
  1463. ShmPsuData->SystemAvailablePower = 0;
  1464. ShmPsuData->SystemPresentPsuQuantity = 0;
  1465. }
  1466. }
  1467. ftime(&gRegTimeUp[plugNum][REG_USER_ID]);
  1468. }
  1469. }
  1470. }
  1471. static int messageTrigger(int fd, uint8_t plugNum, uint8_t gunID, uint8_t curReg)
  1472. {
  1473. int ret = DISPENSER_INIT_SUCC;
  1474. int isContinue = 1;
  1475. struct timeb NowTime;
  1476. while (isContinue) {
  1477. usleep(128);
  1478. ftime(&NowTime);
  1479. switch (curReg) {
  1480. case REG_MODEL_NAME:
  1481. if (WriteModelName(fd) == PASS) {
  1482. gDoCommGblData.DisConnCount = 0;
  1483. curReg = REG_CONNECTOR_ID;
  1484. } else {
  1485. sleep(1);
  1486. }
  1487. break;
  1488. case REG_CONNECTOR_ID:
  1489. if (readConnectorID(fd) == PASS) {
  1490. gDoCommGblData.DisConnCount = 0;
  1491. curReg = REG_REPORT_CSU_VERSION;
  1492. } else {
  1493. sleep(1);
  1494. }
  1495. break;
  1496. case REG_POWER_CABINET_STATUS:
  1497. if (DiffTimeb(gRegTimeUp[plugNum][curReg], NowTime) > LOOP_RETRY_TIME) {
  1498. readPowerCabinetStatus(fd, gunID);
  1499. ftime(&gRegTimeUp[plugNum][curReg]);
  1500. }
  1501. curReg = REG_DISPENSER_STATUS;
  1502. break;
  1503. case REG_DISPENSER_STATUS:
  1504. if (DiffTimeb(gRegTimeUp[plugNum][curReg], NowTime) > LOOP_RETRY_TIME) {
  1505. writeDispenserStatus(fd, gunID);
  1506. ftime(&gRegTimeUp[plugNum][curReg]);
  1507. }
  1508. curReg = REG_PLUG_IN_STATE;
  1509. break;
  1510. //case REG_CHARGING_CAP:
  1511. // readChargingCapability(fd, plugNum, gunID);
  1512. // break;
  1513. //case REG_CHARGING_TARGET:
  1514. // writeChargingTarget(fd, plugNum, gunID);
  1515. // break;
  1516. //case REG_SOFTWARE_UPDATE:
  1517. // if (ChargingData[plugNum]->SystemStatus == S_IDLE) {
  1518. // if (DiffTimeb(gRegTimeUp[plugNum][curReg], NowTime) > LOOP_RETRY_TIME * 10) {
  1519. // readSoftwareUpdate(fd);
  1520. // ftime(&gRegTimeUp[plugNum][curReg]);
  1521. // }
  1522. // while (ShmSysConfigAndInfo->SysInfo.FirmwareUpdate == YES) {
  1523. // ftime(&NowTime);
  1524. // if (DiffTimeb(gRegTimeUp[plugNum][curReg], NowTime) > LOOP_RETRY_TIME) {
  1525. // readSoftwareUpdate(fd);
  1526. // }
  1527. // usleep(128);
  1528. // }
  1529. // }
  1530. // isContinue = 0;
  1531. // break;
  1532. case REG_PLUG_IN_STATE:
  1533. if (DiffTimeb(gRegTimeUp[plugNum][curReg], NowTime) > LOOP_RETRY_TIME) {
  1534. writePlugInStatus(fd, plugNum, gunID);
  1535. ftime(&gRegTimeUp[plugNum][curReg]);
  1536. }
  1537. curReg = REG_CONNECTOR_STATE;
  1538. break;
  1539. case REG_CONNECTOR_STATE:
  1540. if (DiffTimeb(gRegTimeUp[plugNum][curReg], NowTime) > LOOP_RETRY_TIME) {
  1541. writeConnectorState(fd, plugNum, gunID);
  1542. ftime(&gRegTimeUp[plugNum][curReg]);
  1543. }
  1544. //check misc command from power cabinet
  1545. if (gDoCommGblData.MiscCmd != 0) {
  1546. curReg = gDoCommGblData.MiscCmd;
  1547. } else {
  1548. curReg = REG_QRCODE_URL_INFO;
  1549. }
  1550. break;
  1551. //case REG_USER_ID:
  1552. // writeUserID(fd, gunID, uint8_t *pUserID);
  1553. // break;
  1554. //case REG_CHARGING_PERMISSION:
  1555. // readChargePermission(fd, gunID);
  1556. // break;
  1557. case REG_MISC_CONTROL:
  1558. readMiscCommand(fd, gunID);
  1559. isContinue = 0;
  1560. break;
  1561. case REG_REPORT_CSU_VERSION:
  1562. case REG_REPORT_OTHER_VERSION:
  1563. if (gDoCommGblData.MiscCmd != 0) {
  1564. writeCsuModuleVersion(fd);
  1565. writeOtherModuleVersion(fd);
  1566. clearMiscCommand();
  1567. isContinue = 0;
  1568. } else {
  1569. //power up write dispenser version and get power cabinet system ID
  1570. if ((writeCsuModuleVersion(fd) == PASS) &&
  1571. (writeOtherModuleVersion(fd) == PASS) &&
  1572. (readQRcodeURLAndSystemDate(fd) == PASS)) {
  1573. //gDoCommGblData.DisConnCount = 0;
  1574. isContinue = 0;
  1575. }
  1576. }
  1577. break;
  1578. //case REG_PRESENT_CHARGING_INFO:
  1579. // break;
  1580. case REG_QRCODE_URL_INFO:
  1581. if (gunID == 1) {
  1582. if (DiffTimeb(gRegTimeUp[plugNum][curReg], NowTime) > (LOOP_RETRY_TIME * 10)) {
  1583. readQRcodeURLAndSystemDate(fd);
  1584. ftime(&gRegTimeUp[plugNum][curReg]);
  1585. }
  1586. }
  1587. isContinue = 0;
  1588. //curReg = REG_SOFTWARE_UPDATE;
  1589. break;
  1590. //case REG_WAIT_PLUG_IT_STATE:
  1591. // if (DiffTimeb(gRegTimeUp[plugNum][curReg], NowTime) > (LOOP_RETRY_TIME / 10)) {
  1592. // writeWaitPlugItState(fd, gunID);
  1593. // ftime(&gRegTimeUp[plugNum][curReg]);
  1594. // }
  1595. // isContinue = 0;
  1596. // break;
  1597. default:
  1598. DEBUG_ERROR("error curReg = %x\r\n", curReg);
  1599. gDoCommGblData.MiscCmd = 0;
  1600. gDoCommGblData.DisConnCount = CHECK_NETWORK_FAIL_COUNT;
  1601. isContinue = 0;
  1602. break;
  1603. }
  1604. if (gDoCommGblData.DisConnCount >= CHECK_NETWORK_FAIL_COUNT) {
  1605. return DISPENSER_SOCKET_RECONN;
  1606. }
  1607. }//for
  1608. return ret;
  1609. }
  1610. //static bool isDetectPlugin()
  1611. //{
  1612. // if (ShmSysConfigAndInfo->SysInfo.WaitForPlugit == YES) {
  1613. // return YES;
  1614. // }
  1615. //
  1616. // return NO;
  1617. //}
  1618. static void systemStatusProcess(int fd, uint8_t totalGun, uint8_t plugNum, uint8_t gunID)
  1619. {
  1620. uint8_t i = 0;
  1621. struct timeb AuthNowTime = {0};
  1622. switch (ChargingData[plugNum]->SystemStatus) {
  1623. case S_IDLE:
  1624. case S_RESERVATION:
  1625. case S_MAINTAIN:
  1626. case S_ALARM:
  1627. case S_AUTHORIZING:
  1628. checkAuthorProcess(fd, plugNum);
  1629. //authorization complete, write wait plug it state
  1630. if (gAudiCustInfo->PricesInfo[plugNum].Balance != FAIL_BALANCE_PRICES) {
  1631. ftime(&AuthNowTime);
  1632. if (DiffTimeb(gRegTimeUp[plugNum][REG_WAIT_PLUG_IT_STATE], AuthNowTime) > (LOOP_RETRY_TIME / 10)) {
  1633. writeWaitPlugItState(fd, gunID);
  1634. ftime(&gRegTimeUp[plugNum][REG_WAIT_PLUG_IT_STATE]);
  1635. }
  1636. ftime(&AuthNowTime);
  1637. if (DiffTimeb(gRegTimeUp[plugNum][REG_CHARGING_CAP], AuthNowTime) > LOOP_RETRY_TIME) {
  1638. readChargingCapability(fd, gunID);
  1639. ftime(&gRegTimeUp[plugNum][REG_CHARGING_CAP]);
  1640. }
  1641. }
  1642. break;
  1643. case S_PREPARNING: //get permission
  1644. ftime(&AuthNowTime);
  1645. if (DiffTimeb(gRegTimeUp[plugNum][REG_CONNECTOR_STATE], AuthNowTime) > LOOP_RETRY_TIME) {
  1646. writeConnectorState(fd, plugNum, gunID);
  1647. ftime(&gRegTimeUp[plugNum][REG_CONNECTOR_STATE]);
  1648. }
  1649. ftime(&AuthNowTime);
  1650. if (DiffTimeb(gRegTimeUp[plugNum][REG_CHARGING_PERMISSION], AuthNowTime) > LOOP_RETRY_TIME) {
  1651. if (readChargePermission(fd, gunID) && readChargingCapability(fd, gunID)) {
  1652. for (i = 0; i < totalGun; i++) {
  1653. ShmPsuData->SystemAvailablePower += ChargingData[i]->AvailableChargingPower;
  1654. }
  1655. ShmPsuData->SystemPresentPsuQuantity = (ShmPsuData->SystemAvailablePower / 30);
  1656. }
  1657. ftime(&gRegTimeUp[plugNum][REG_CHARGING_PERMISSION]);
  1658. }
  1659. ftime(&AuthNowTime);
  1660. if (DiffTimeb(gRegTimeUp[plugNum][REG_PRESENT_CHARGING_INFO], AuthNowTime) > LOOP_RETRY_TIME) {
  1661. writePresentChargingInfo(fd, plugNum, gunID);
  1662. ftime(&gRegTimeUp[plugNum][REG_PRESENT_CHARGING_INFO]);
  1663. }
  1664. break;
  1665. case S_PREPARING_FOR_EV://wait connector lock
  1666. ftime(&AuthNowTime);
  1667. if (DiffTimeb(gRegTimeUp[plugNum][REG_CHARGING_CAP], AuthNowTime) > LOOP_RETRY_TIME) {
  1668. readChargingCapability(fd, gunID);
  1669. ftime(&gRegTimeUp[plugNum][REG_CHARGING_CAP]);
  1670. }
  1671. ftime(&AuthNowTime);
  1672. if (DiffTimeb(gRegTimeUp[plugNum][REG_CHARGING_PERMISSION], AuthNowTime) > LOOP_RETRY_TIME) {
  1673. if (readChargePermission(fd, gunID) == 0) {
  1674. DEBUG_INFO("Stop charging by power cabinet's permission = %d, %d\r\n",
  1675. plugNum,
  1676. REG_CHARGING_PERMISSION);
  1677. ChargingData[plugNum]->StopChargeFlag = POWER_CABINET_STOP_CHARGING;
  1678. }
  1679. ftime(&gRegTimeUp[plugNum][REG_CHARGING_PERMISSION]);
  1680. }
  1681. ftime(&AuthNowTime);
  1682. if (DiffTimeb(gRegTimeUp[plugNum][REG_PRESENT_CHARGING_INFO], AuthNowTime) > LOOP_RETRY_TIME) {
  1683. writePresentChargingInfo(fd, plugNum, gunID);
  1684. ftime(&gRegTimeUp[plugNum][REG_PRESENT_CHARGING_INFO]);
  1685. }
  1686. break;
  1687. case S_PREPARING_FOR_EVSE: //insaulation test
  1688. case S_CCS_PRECHARGE_ST0:
  1689. case S_CCS_PRECHARGE_ST1:
  1690. writeChargingTarget(fd, plugNum, gunID);
  1691. ftime(&AuthNowTime);
  1692. if (DiffTimeb(gRegTimeUp[plugNum][REG_CHARGING_CAP], AuthNowTime) > LOOP_RETRY_TIME) {
  1693. readChargingCapability(fd, gunID);
  1694. ftime(&gRegTimeUp[plugNum][REG_CHARGING_CAP]);
  1695. }
  1696. ftime(&AuthNowTime);
  1697. if (DiffTimeb(gRegTimeUp[plugNum][REG_CHARGING_PERMISSION], AuthNowTime) > LOOP_RETRY_TIME) {
  1698. if (readChargePermission(fd, gunID) == 0) {
  1699. DEBUG_INFO("Stop charging by power cabinet's permission = %d, %d\r\n",
  1700. plugNum,
  1701. REG_CHARGING_PERMISSION);
  1702. ChargingData[plugNum]->StopChargeFlag = POWER_CABINET_STOP_CHARGING;
  1703. }
  1704. ftime(&gRegTimeUp[plugNum][REG_CHARGING_PERMISSION]);
  1705. }
  1706. ftime(&AuthNowTime);
  1707. if (DiffTimeb(gRegTimeUp[plugNum][REG_PRESENT_CHARGING_INFO], AuthNowTime) > LOOP_RETRY_TIME) {
  1708. writePresentChargingInfo(fd, plugNum, gunID);
  1709. ftime(&gRegTimeUp[plugNum][REG_PRESENT_CHARGING_INFO]);
  1710. }
  1711. break;
  1712. case S_CHARGING: //charging
  1713. case S_TERMINATING:
  1714. ftime(&AuthNowTime);
  1715. if (DiffTimeb(gRegTimeUp[plugNum][REG_CHARGING_CAP], AuthNowTime) > LOOP_RETRY_TIME) {
  1716. readChargingCapability(fd, gunID);
  1717. ftime(&gRegTimeUp[plugNum][REG_CHARGING_CAP]);
  1718. }
  1719. ftime(&AuthNowTime);
  1720. if (DiffTimeb(gRegTimeUp[plugNum][REG_CHARGING_PERMISSION], AuthNowTime) > LOOP_RETRY_TIME) {
  1721. if (readChargePermission(fd, gunID) == 0) {
  1722. DEBUG_INFO("Stop charging by power cabinet's permission = %d, %d\r\n",
  1723. plugNum,
  1724. REG_CHARGING_PERMISSION);
  1725. ChargingData[plugNum]->StopChargeFlag = POWER_CABINET_STOP_CHARGING;
  1726. //printf("%d StopChargeFlag = %d\r\n", plugNum, ChargingData[plugNum]->StopChargeFlag);
  1727. }
  1728. ftime(&gRegTimeUp[plugNum][REG_CHARGING_PERMISSION]);
  1729. }
  1730. writeChargingTarget(fd, plugNum, gunID);
  1731. ftime(&AuthNowTime);
  1732. if (DiffTimeb(gRegTimeUp[plugNum][REG_PRESENT_CHARGING_INFO], AuthNowTime) > LOOP_RETRY_TIME) {
  1733. writePresentChargingInfo(fd, plugNum, gunID);
  1734. ftime(&gRegTimeUp[plugNum][REG_PRESENT_CHARGING_INFO]);
  1735. }
  1736. break;
  1737. case S_COMPLETE:
  1738. ftime(&AuthNowTime);
  1739. if (DiffTimeb(gRegTimeUp[plugNum][REG_CHARGING_CAP], AuthNowTime) > LOOP_RETRY_TIME) {
  1740. readChargingCapability(fd, gunID);
  1741. ftime(&gRegTimeUp[plugNum][REG_CHARGING_CAP]);
  1742. }
  1743. break;
  1744. default:
  1745. break;
  1746. }
  1747. }
  1748. static int networkCreatePorcess(void)
  1749. {
  1750. int fd = 0;
  1751. /**************** Check Network **********/
  1752. DEBUG_INFO("Check Dispenser Network Information");
  1753. while (!CheckNetworkStatus()) {
  1754. calDisconnectCount(&gDoCommGblData.DisConnCount, CHECK_NETWORK_FAIL_COUNT);
  1755. DEBUG_ERROR("disconnect count = % d\r\n", gDoCommGblData.DisConnCount);
  1756. }
  1757. DEBUG_INFO("Dispenser Network Information checked: IP = % s Netmask = % s, Gateway = % s",
  1758. ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthIpAddress,
  1759. ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthSubmaskAddress,
  1760. ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthGatewayAddress);
  1761. gDoCommGblData.DisConnCount = 0;
  1762. /**************** Power cabinet connection **********/
  1763. DEBUG_INFO("Connect to Power Cabinet");
  1764. while ((fd = doCommConnToServer()) <= 0) {
  1765. calDisconnectCount(&gDoCommGblData.DisConnCount, CONNECT_SERVER_FAIL_COUNT);
  1766. DEBUG_ERROR("disconnect count = %d, fd = %d\r\n", gDoCommGblData.DisConnCount, fd);
  1767. }
  1768. DEBUG_INFO("Power cabinet connected(fd = % d): IP = % s Netmask = % s, Gateway = % s",
  1769. fd,
  1770. ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthIpAddress,
  1771. ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthSubmaskAddress,
  1772. ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthGatewayAddress);
  1773. gDoCommGblData.DisConnCount = 0;
  1774. destroySelectGun(DESTROY_ALL_SEL);
  1775. sleep(3);
  1776. setTcpStatus(NORMAL);
  1777. return fd;
  1778. }
  1779. int main(int argc, char *argv[])
  1780. {
  1781. uint8_t plugNum = 0, gunID = 0;
  1782. uint8_t index = 0;
  1783. uint8_t totalConnCount = 0;
  1784. uint8_t initDone = DISPENER_INIT_FAIL;
  1785. int fd = 0;
  1786. int isContinue = 1;
  1787. InitSocketSigPipe();
  1788. /**************** Initialization **********/
  1789. if (InitShareMemory() == 0) {
  1790. if (ShmStatusCodeData != NULL) {
  1791. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.FailToCreateShareMemory = YES;
  1792. }
  1793. DEBUG_ERROR("DoComm Initial share memory fail\r\n");
  1794. sleep(5);
  1795. return 0;
  1796. }
  1797. totalConnCount = ShmSysConfigAndInfo->SysConfig.TotalConnectorCount;
  1798. for (index = 0; index < totalConnCount; index++) {
  1799. if (!FindChargingInfoData(index, &ChargingData[0])) {
  1800. DEBUG_ERROR("FindChargingInfoData false \n");
  1801. break;
  1802. }
  1803. clearPricesInfo(index);
  1804. }
  1805. //initial trigger message timer
  1806. for (index = 0; index < MAX_REGISTER_NUM; index++) {
  1807. ftime(&gRegTimeUp[0][index]);
  1808. ftime(&gRegTimeUp[1][index]);
  1809. usleep(50000);
  1810. }
  1811. setTcpStatus(NORMAL);
  1812. while (isContinue) {
  1813. if (initDone == DISPENER_INIT_FAIL) {
  1814. fd = networkCreatePorcess();
  1815. initDone = messageTrigger(fd, 0, 0, REG_MODEL_NAME); //first trigger model name and connector id
  1816. } else {
  1817. //checkAuthorProcess(fd, 0);
  1818. plugNum = 0;
  1819. gunID = 0;
  1820. for (plugNum = 0; plugNum < totalConnCount; plugNum++) {
  1821. //plugNum = setup chargingData value for bottom layer
  1822. //gunID = (plugNum + 1); //connector Id, 1 = left gun, 2 = right gun
  1823. gunID = gDoCommGblData.ConnectorID[plugNum];
  1824. systemStatusProcess(fd, totalConnCount, plugNum, gunID);
  1825. initDone = messageTrigger(fd,
  1826. plugNum,
  1827. gunID,
  1828. REG_POWER_CABINET_STATUS);
  1829. if (initDone == DISPENSER_SOCKET_RECONN) {
  1830. break;
  1831. }
  1832. }
  1833. }
  1834. if (initDone == DISPENSER_SOCKET_RECONN) {
  1835. DEBUG_INFO("Disconnected from power cabinet...re-connecting");
  1836. setTcpStatus(ABNORMAL);
  1837. gDoCommGblData.DisConnCount = 0;
  1838. gDoCommGblData.SeqNum = 0;
  1839. closeSocket(fd);
  1840. fd = 0;
  1841. initDone = DISPENER_INIT_FAIL;
  1842. }
  1843. //update dispenser firmware
  1844. if (initDone != DISPENER_INIT_FAIL) {
  1845. updateFirmwareProcess(fd, gDoCommGblData.ConnectorID[0], totalConnCount);
  1846. }
  1847. usleep(1000);
  1848. }
  1849. }