ReDoComm.c 69 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134
  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 id, 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 (id == 2) {
  477. clearMiscCommand();
  478. }
  479. break;
  480. }
  481. DEBUG_INFO("change availability id = %d, value = %d\r\n", id - 1, value);
  482. ShmOCPP16Data->CsMsg.bits[id - 1].ChangeAvailabilityReq = YES;
  483. if (value == YES) {
  484. strcpy((char *)ShmOCPP16Data->ChangeAvailability[id - 1].Type, "Operative");
  485. } else if (value == NO) {
  486. strcpy((char *)ShmOCPP16Data->ChangeAvailability[id - 1].Type, "Inoperative");
  487. }
  488. if (id == 2) {
  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 != (id - 1)) {
  512. clearMiscCommand();
  513. break;
  514. }
  515. if (getConfirmSelectedGun(id - 1) == FAIL) {
  516. DEBUG_INFO("Remote start not select gun\r\n");
  517. break;
  518. }
  519. gAudiCustInfo->PricesInfo[id - 1].Balance = transPricesUnit((int)value);
  520. DEBUG_INFO("%d misc balance = %.2f\r\n", id, gAudiCustInfo->PricesInfo[id - 1].Balance);
  521. clearMiscCommand();
  522. break;
  523. //--- Control Dispenser ---
  524. case MISC_CMD_HARDWARE_REBOOT:
  525. if (value != YES) {
  526. clearMiscCommand();
  527. break;
  528. }
  529. DEBUG_INFO("Hardware reboot\r\n");
  530. ShmOCPP16Data->MsMsg.bits.ResetReq = YES;
  531. strcpy((char *)ShmOCPP16Data->Reset.Type, "Hard");
  532. clearMiscCommand();
  533. break;
  534. case MISC_CMD_SOFTWARE_RESTART:
  535. if (value != YES) {
  536. clearMiscCommand();
  537. break;
  538. }
  539. clearMiscCommand();
  540. DEBUG_INFO("Software reboot\r\n");
  541. ShmOCPP16Data->MsMsg.bits.ResetReq = YES;
  542. strcpy((char *)ShmOCPP16Data->Reset.Type, "Soft");
  543. break;
  544. case MISC_CMD_REMOTE_START_CHARGING:
  545. if (value != YES) {
  546. if (id == 2) {
  547. clearMiscCommand();
  548. }
  549. break;
  550. }
  551. DEBUG_INFO("Remote start charging id = %d\r\n", id);
  552. //if (getConfirmSelectedGun(id - 1) == FAIL) {
  553. // DEBUG_INFO("Remote start not select gun\r\n");
  554. // break;
  555. //}
  556. if (getSelGunWaitToAuthor(id - 1) == FAIL) {
  557. DEBUG_ERROR("Remote start gun already charging\r\n");
  558. break;
  559. }
  560. if (ShmSysConfigAndInfo->SysInfo.CurGunSelected != (id - 1)) {
  561. if (ShmSysConfigAndInfo->SysInfo.CurGunSelected == LEFT_GUN_NUM &&
  562. (gAudiCustInfo->SelGunInfo.LeftGun == SEL_GUN_CONFIRM ||
  563. gAudiCustInfo->SelGunInfo.LeftGun == SEL_GUN_ATHOR)) {
  564. gAudiCustInfo->SelGunInfo.LeftGun = SEL_GUN_RELEASE;
  565. strcpy((char *)ShmSysConfigAndInfo->SysConfig.UserId, "");
  566. ShmSysConfigAndInfo->SysInfo.WaitForPlugit = NO;
  567. sleep(1); //Jerry add
  568. ShmSysConfigAndInfo->SysInfo.SystemPage = _LCM_SELECT_GUN;
  569. gettimeofday(&ShmSysConfigAndInfo->SysInfo.SystemTimeoutTimer, NULL);
  570. ShmSysConfigAndInfo->SysInfo.SystemTimeoutFlag = Timeout_None;
  571. }
  572. if (ShmSysConfigAndInfo->SysInfo.CurGunSelected == RIGHT_GUN_NUM &&
  573. (gAudiCustInfo->SelGunInfo.RightGun == SEL_GUN_CONFIRM ||
  574. gAudiCustInfo->SelGunInfo.RightGun == SEL_GUN_ATHOR)) {
  575. gAudiCustInfo->SelGunInfo.RightGun = SEL_GUN_RELEASE;
  576. strcpy((char *)ShmSysConfigAndInfo->SysConfig.UserId, "");
  577. ShmSysConfigAndInfo->SysInfo.WaitForPlugit = NO;
  578. sleep(1); //Jerry add
  579. ShmSysConfigAndInfo->SysInfo.SystemPage = _LCM_SELECT_GUN;
  580. gettimeofday(&ShmSysConfigAndInfo->SysInfo.SystemTimeoutTimer, NULL);
  581. ShmSysConfigAndInfo->SysInfo.SystemTimeoutFlag = Timeout_None;
  582. }
  583. ShmSysConfigAndInfo->SysInfo.CurGunSelected = (id - 1);
  584. ShmSysConfigAndInfo->SysInfo.CurGunSelectedByAc = NO_DEFINE;
  585. }
  586. setConfirmSelGun(ShmSysConfigAndInfo->SysInfo.CurGunSelected);
  587. ShmOCPP16Data->CsMsg.bits[id - 1].RemoteStartTransactionReq = YES;
  588. gAudiCustInfo->PricesInfo[id - 1].Balance = 0.00;
  589. clearMiscCommand();
  590. break;
  591. case MISC_CMD_REMOTE_STOP_CHARGING:
  592. if (value != YES) {
  593. if (id == 2) {
  594. clearMiscCommand();
  595. }
  596. break;
  597. }
  598. strcpy((char *)ShmSysConfigAndInfo->SysConfig.UserId, "");
  599. ShmSysConfigAndInfo->SysInfo.WaitForPlugit = NO;
  600. ShmSysConfigAndInfo->SysInfo.SystemPage = _LCM_SELECT_GUN;
  601. gettimeofday(&ShmSysConfigAndInfo->SysInfo.SystemTimeoutTimer, NULL);
  602. ShmSysConfigAndInfo->SysInfo.SystemTimeoutFlag = Timeout_None;
  603. destroySelectGun(id - 1);
  604. clearMiscCommand();
  605. break;
  606. case MISC_CMD_REMOTE_UNLOCK:
  607. if (value != YES) {
  608. if (id == 2) {
  609. clearMiscCommand();
  610. }
  611. break;
  612. }
  613. if (isDetectPlugin() == YES) {
  614. ClearDetectPluginFlag();
  615. strcpy((char *)ShmSysConfigAndInfo->SysConfig.UserId, "");
  616. ShmSysConfigAndInfo->SysInfo.WaitForPlugit = NO;
  617. ShmSysConfigAndInfo->SysInfo.SystemPage = _LCM_SELECT_GUN;
  618. gettimeofday(&ShmSysConfigAndInfo->SysInfo.SystemTimeoutTimer, NULL);
  619. ShmSysConfigAndInfo->SysInfo.SystemTimeoutFlag = Timeout_None;
  620. destroySelectGun(id - 1);
  621. } else {
  622. ChargingData[id - 1]->StopChargeFlag = YES;
  623. }
  624. break;
  625. default:
  626. clearMiscCommand();
  627. break;
  628. }
  629. usleep(128);
  630. }
  631. return ret;
  632. }
  633. static int chargingcapabilityHandle(uint8_t *data, uint8_t gunID)
  634. {
  635. uint8_t addr = 0;
  636. float MaxVolt, MaxCurrent, MaxPower;
  637. static PricesInfo pricesInfo[2] = {0};
  638. CapabilityInfo *pCapabilityInfo = NULL;
  639. AccountInfo *pAccountInfo = NULL;
  640. //--- charging capability information --------------------------------------
  641. pCapabilityInfo = (CapabilityInfo *)&data[0];
  642. MaxVolt = (float)(ntohs(pCapabilityInfo->OutputVoltage));
  643. if (MaxVolt >= 0) {
  644. if (MaxVolt > MAX_VOLTAGE) {
  645. MaxVolt = MAX_VOLTAGE;
  646. }
  647. ChargingData[gunID]->MaximumChargingVoltage = MaxVolt;
  648. }
  649. MaxCurrent = (float)(ntohs(pCapabilityInfo->OutputCurrent));
  650. if (MaxCurrent >= 0) {
  651. if (MaxCurrent > MAX_CURRENCY) {
  652. MaxCurrent = MAX_CURRENCY;
  653. }
  654. ChargingData[gunID]->AvailableChargingCurrent = MaxCurrent;
  655. }
  656. MaxPower = (float)(ntohs(pCapabilityInfo->OutputPower));
  657. if (MaxPower >= 0) {
  658. if (MaxPower > MAX_POWER) {
  659. MaxPower = MAX_POWER;
  660. }
  661. ChargingData[gunID]->AvailableChargingPower = MaxPower;
  662. }
  663. //MaxVolt = (float)(data[0] << 8 |data[1]);
  664. //MaxCurrent = (float)(data[2] << 8 | data[3]);
  665. //MaxPower = (float)(data[4] << 8 | data[5]);
  666. //printf("MaxVolt=%f, MaxCurrent=%f, MaxPower=%f,\n", MaxVolt, MaxCurrent, MaxPower);
  667. //--- user prices information ----------------------------------------------
  668. addr = (sizeof(CapabilityInfo) - 2); //2 byte reserved
  669. pAccountInfo = (AccountInfo *)&data[addr];
  670. ShmSysConfigAndInfo->SysConfig.BillingData.Currency = pAccountInfo->Currency;
  671. gAudiCustInfo->PricesInfo[gunID].UserPrices = transPricesUnit(ntohl(pAccountInfo->UserPrices));
  672. ChargingData[gunID]->ChargingFee = transPricesUnit(ntohl(pAccountInfo->TotalCost));
  673. gAudiCustInfo->PricesInfo[gunID].Balance = transPricesUnit(ntohl(pAccountInfo->Balance));
  674. if ((pricesInfo[gunID].UserPrices != gAudiCustInfo->PricesInfo[gunID].UserPrices) ||
  675. (pricesInfo[gunID].Balance != gAudiCustInfo->PricesInfo[gunID].Balance)) {
  676. pricesInfo[gunID].UserPrices = gAudiCustInfo->PricesInfo[gunID].UserPrices;
  677. pricesInfo[gunID].Balance = gAudiCustInfo->PricesInfo[gunID].Balance;
  678. DEBUG_INFO("id = %d, user prices = %.2f, Total cost = %.2f, Account balances = %.2f, currency = %s\r\n",
  679. gunID,
  680. gAudiCustInfo->PricesInfo[gunID].UserPrices,
  681. ChargingData[gunID]->ChargingFee,
  682. gAudiCustInfo->PricesInfo[gunID].Balance,
  683. (uint8_t *)Currency[ShmSysConfigAndInfo->SysConfig.BillingData.Currency]);
  684. }
  685. return PASS;
  686. }
  687. static void addFaultCodeToBuf(uint8_t *Code)
  688. {
  689. uint8_t warningCount = ShmSysConfigAndInfo->SysWarningInfo.WarningCount;
  690. if (warningCount < 10) {
  691. memcpy(&ShmSysConfigAndInfo->SysWarningInfo.WarningCode[warningCount][0],
  692. Code,
  693. strlen((char *)Code));
  694. ShmSysConfigAndInfo->SysWarningInfo.WarningCount++;
  695. }
  696. }
  697. static void removeFaultCodeToBuf(uint8_t *Code)
  698. {
  699. uint8_t find = 0x01;
  700. uint8_t i = 0;
  701. char _code[7] = {0};
  702. sprintf(_code, "%s", Code);
  703. // 把相關的錯誤碼一次移除,避免重複顯示
  704. while (find) {
  705. usleep(128);
  706. find = 0x00;
  707. for (i = 0; i < ShmSysConfigAndInfo->SysWarningInfo.WarningCount; i++) {
  708. usleep(128);
  709. if (find == 0x00) {
  710. if (memcmp(&ShmSysConfigAndInfo->SysWarningInfo.WarningCode[i][0], _code, 7) == 0) {
  711. find = 0x01;
  712. }
  713. } else {
  714. memcpy(&ShmSysConfigAndInfo->SysWarningInfo.WarningCode[i - 1][0],
  715. &ShmSysConfigAndInfo->SysWarningInfo.WarningCode[i][0], 7);
  716. }
  717. }
  718. if (find) {
  719. ShmSysConfigAndInfo->SysWarningInfo.WarningCount--;
  720. }
  721. }
  722. }
  723. bool CompareArrayIsZero(uint8_t *array, unsigned int array_size)
  724. {
  725. uint8_t i;
  726. if (array != NULL) {
  727. for (i = 0; i < array_size; i++) {
  728. if (array[i] != 0) {
  729. return false;
  730. }
  731. }
  732. }
  733. return true;
  734. }
  735. static int powerCabinetStatusProcess(uint8_t dataLen, uint8_t *data)
  736. {
  737. uint8_t ret = 0;
  738. uint8_t i = 0;
  739. uint8_t count = 0;
  740. uint8_t EventCodeTmp[7] = {0};
  741. uint8_t StatusArray[MAX_REGISTER_NUM][WARNING_CODE_SIZE] = {0};
  742. uint8_t remaindLen = dataLen % dataLen;
  743. uint8_t statusCodeError = 0;
  744. if (remaindLen != 0) {
  745. dataLen -= remaindLen;
  746. }
  747. if (dataLen < WARNING_CODE_SIZE) {
  748. DEBUG_ERROR("fail status code length = %d\r\n", dataLen);
  749. Hexdump(data, dataLen);
  750. if (ShmSysConfigAndInfo->SysWarningInfo.WarningCount > 0) {
  751. for (i = 0; i < ShmSysConfigAndInfo->SysWarningInfo.WarningCount; i++) {
  752. usleep(128);
  753. if (ShmSysConfigAndInfo->SysWarningInfo.WarningCode[i][1] >= '3' || //from backend or power cabinet
  754. (ShmSysConfigAndInfo->SysWarningInfo.WarningCode[i][0] >= 'B' &&
  755. ShmSysConfigAndInfo->SysWarningInfo.WarningCode[i][1] >= '4')) {
  756. memset(EventCodeTmp, 0, sizeof(EventCodeTmp));
  757. memcpy(EventCodeTmp,
  758. ShmSysConfigAndInfo->SysWarningInfo.WarningCode[i],
  759. sizeof(EventCodeTmp));
  760. removeFaultCodeToBuf(EventCodeTmp);
  761. }
  762. }
  763. }
  764. return FAIL;
  765. }
  766. for (count = 0; count < dataLen; count += WARNING_CODE_SIZE) {
  767. usleep(128);
  768. // misc command status code handle
  769. if (strncmp((char *)&data[count], MISC_ST_MISC_CMD, WARNING_CODE_SIZE) == 0) {
  770. //if (gDoCommGblData.MiscCmd != REG_MISC_CONTROL) {
  771. gDoCommGblData.MiscCmd = REG_MISC_CONTROL;
  772. //}
  773. memset((char *)&data[count], 0, WARNING_CODE_SIZE);
  774. continue;
  775. } else if (strncmp((char *)&data[count], MISC_ST_VERSION, WARNING_CODE_SIZE) == 0) {
  776. //if (gDoCommGblData.MiscCmd != REG_REPORT_CSU_VERSION) {
  777. gDoCommGblData.MiscCmd = REG_REPORT_CSU_VERSION;
  778. //}
  779. memset((char *)&data[count], 0, WARNING_CODE_SIZE);
  780. continue;
  781. }
  782. if (CompareArrayIsZero((uint8_t *)&data[count], WARNING_CODE_SIZE) == true) {
  783. memset(EventCodeTmp, 0, sizeof(EventCodeTmp));
  784. memcpy(EventCodeTmp, (char *)&data[count], sizeof(EventCodeTmp));
  785. DEBUG_ERROR("error status = %s\r\n", EventCodeTmp);
  786. continue;
  787. }
  788. strncpy((char *)&StatusArray[count / WARNING_CODE_SIZE], (char *)&data[count], WARNING_CODE_SIZE);
  789. ret = 0;
  790. for (i = 0; i < ShmSysConfigAndInfo->SysWarningInfo.WarningCount; i++) {
  791. usleep(128);
  792. if (memcmp(&ShmSysConfigAndInfo->SysWarningInfo.WarningCode[i][0],
  793. StatusArray[count / WARNING_CODE_SIZE],
  794. WARNING_CODE_SIZE) == 0) {
  795. ret = 1;
  796. break;
  797. }
  798. }
  799. if (ret == 0) {
  800. addFaultCodeToBuf(StatusArray[count / WARNING_CODE_SIZE]);
  801. }
  802. //Rtn=StatusCodeProcessing(StatusArray[count/6]);
  803. }
  804. for (i = 0; i < ShmSysConfigAndInfo->SysWarningInfo.WarningCount; i++) {
  805. usleep(128);
  806. //if (ShmSysConfigAndInfo->SysWarningInfo.WarningCode[i][0] >= 'B' &&
  807. // ShmSysConfigAndInfo->SysWarningInfo.WarningCode[i][1] >= '4') {
  808. // memset(EventCodeTmp, 0, sizeof(EventCodeTmp));
  809. // memcpy(EventCodeTmp,
  810. // ShmSysConfigAndInfo->SysWarningInfo.WarningCode[i],
  811. // sizeof(EventCodeTmp));
  812. // removeFaultCodeToBuf(EventCodeTmp);
  813. //}
  814. if (ShmSysConfigAndInfo->SysWarningInfo.WarningCode[i][1] >= '3' || //from backend or power cabinet 0x33
  815. (ShmSysConfigAndInfo->SysWarningInfo.WarningCode[i][0] >= 'B' &&
  816. ShmSysConfigAndInfo->SysWarningInfo.WarningCode[i][1] >= '4')) {
  817. ret = 0;
  818. for (count = 0; count < (dataLen / WARNING_CODE_SIZE); count++) {
  819. usleep(128);
  820. if (memcmp(&ShmSysConfigAndInfo->SysWarningInfo.WarningCode[i][0],
  821. StatusArray[count],
  822. WARNING_CODE_SIZE) == 0) {
  823. ret = 1;
  824. break;
  825. }
  826. }
  827. if (ret == 0) {
  828. memset(EventCodeTmp, 0, sizeof(EventCodeTmp));
  829. memcpy(EventCodeTmp,
  830. ShmSysConfigAndInfo->SysWarningInfo.WarningCode[i],
  831. sizeof(EventCodeTmp));
  832. removeFaultCodeToBuf(EventCodeTmp);
  833. }
  834. }
  835. }
  836. return PASS;
  837. }
  838. static int responsePackeHandle(int fd, uint8_t *pResult, uint8_t id, uint8_t reg)
  839. {
  840. int ret = PASS;
  841. uint8_t rawDataLen = 0;
  842. CsuResultPkt *pCsuResult = (CsuResultPkt *)pResult;
  843. SoftwareUpdInfo *pSoftwareUpd = NULL;
  844. //Hexdump((uint8_t *)pCsuResult, sizeof(CmdHead) + pCsuResult->Head.DataLen);
  845. if (compareOpcode(pCsuResult->Head.OP) == FAIL ||
  846. compareResult(pCsuResult->Data.Result) == FAIL ||
  847. compareRegister(pCsuResult->Data.Register, reg) == FAIL) {
  848. if (reg != REG_CHARGING_PERMISSION) {
  849. return FAIL;
  850. }
  851. }
  852. rawDataLen = (pCsuResult->Head.DataLen - 2); //2 byte = register and result byte
  853. switch (pCsuResult->Data.Register) {
  854. case REG_MODEL_NAME:
  855. break;
  856. case REG_CONNECTOR_ID:
  857. gDoCommGblData.ConnectorID[0] = pCsuResult->Data.Data[0];
  858. gDoCommGblData.ConnectorID[1] = pCsuResult->Data.Data[1];
  859. DEBUG_INFO("OK (Left connector ID = %d, Right connector ID = %d)\n",
  860. pCsuResult->Data.Data[0],
  861. pCsuResult->Data.Data[1]);
  862. break;
  863. case REG_POWER_CABINET_STATUS:
  864. ret = powerCabinetStatusProcess(rawDataLen, pCsuResult->Data.Data);
  865. break;
  866. case REG_DISPENSER_STATUS:
  867. memset(&gPreSysWarningInfo, 0, sizeof(struct WARNING_CODE_INFO));
  868. memcpy(&gPreSysWarningInfo,
  869. &(ShmSysConfigAndInfo->SysWarningInfo),
  870. sizeof(struct WARNING_CODE_INFO));
  871. break;
  872. case REG_CHARGING_CAP:
  873. chargingcapabilityHandle(pCsuResult->Data.Data, id - 1);
  874. break;
  875. case REG_CHARGING_TARGET:
  876. break;
  877. case REG_SOFTWARE_UPDATE:
  878. pSoftwareUpd = (SoftwareUpdInfo *)&pCsuResult->Data.Data[0];
  879. if ((strcmp((char *)pSoftwareUpd->ImgName, "") == 0) ||
  880. (rawDataLen == 0) ||
  881. pSoftwareUpd->UpdateState != 1) {
  882. ret = FAIL;
  883. break;
  884. }
  885. ret = FAIL;
  886. ret = updateFirmwareHandle(pSoftwareUpd->ImgName);
  887. break;
  888. case REG_PLUG_IN_STATE:
  889. break;
  890. case REG_CONNECTOR_STATE:
  891. break;
  892. case REG_USER_ID:
  893. //DEBUG_INFO("%s\n", pCsuResult->Data.Data[0] == 1 ? "Accept" : "Reject" );
  894. //if (pCsuResult->Data.Data[0] <= 0) {
  895. // return FAIL;
  896. //}
  897. if (pCsuResult->Data.Result == COMMAND_RESULT_NG) {
  898. return COMMAND_RESULT_NG;
  899. }
  900. return pCsuResult->Data.Data[0];
  901. break;
  902. case REG_CHARGING_PERMISSION:
  903. //DEBUG_INFO("id = %d, result = %s, action = %s\r\n",
  904. // pCsuResult->Head.ID,
  905. // pCsuResult->Data.Result == 1 ? "OK" : "NG",
  906. // pCsuResult->Data.Data[0] == 1 ? "Permitted" : "NOT permitted");
  907. if (pCsuResult->Data.Result == COMMAND_RESULT_NG) {
  908. return COMMAND_RESULT_NG;
  909. }
  910. return pCsuResult->Data.Data[0];
  911. break;
  912. case REG_MISC_CONTROL:
  913. miscCommandHandle(rawDataLen, pCsuResult->Head.ID, pCsuResult->Data.Data);
  914. break;
  915. case REG_REPORT_CSU_VERSION:
  916. case REG_REPORT_OTHER_VERSION:
  917. break;
  918. case REG_PRESENT_CHARGING_INFO:
  919. break;
  920. case REG_QRCODE_URL_INFO:
  921. ret = qrCodeUrlInfoHandle(pCsuResult->Data.Data);
  922. break;
  923. case REG_WAIT_PLUG_IT_STATE:
  924. break;
  925. default:
  926. break;
  927. }
  928. return ret;
  929. }
  930. static int composeSocketData(int fd,
  931. uint8_t id,
  932. uint8_t opCode,
  933. uint8_t reg,
  934. uint8_t dataLen,
  935. uint8_t *data)
  936. {
  937. int ret = PASS;
  938. int size = 0;
  939. int sendPktLen = 0;
  940. CsuCmdPkt csuCmdPkt = {0};
  941. CsuResultPkt csuResult = {0};
  942. csuCmdPkt.Head.SeqNum = gDoCommGblData.SeqNum++;
  943. csuCmdPkt.Head.ID = id;
  944. csuCmdPkt.Head.OP = opCode;
  945. csuCmdPkt.Head.DataLen = dataLen + 1; //+1 for register byte
  946. csuCmdPkt.Data.Register = reg;
  947. if ((data != NULL) && (dataLen > 0)) {
  948. memcpy(csuCmdPkt.Data.Data, data, dataLen);
  949. }
  950. sendPktLen = csuCmdPkt.Head.DataLen + sizeof(csuCmdPkt.Head);
  951. //Hexdump((uint8_t *)&csuCmdPkt, sendPktLen);
  952. //send command packet
  953. if ((size = sendTcpSocket(fd, (uint8_t *)&csuCmdPkt, sendPktLen)) < 0) {
  954. DEBUG_ERROR("TCP socket send packet fail = %d\r\n", size);
  955. gDoCommGblData.DisConnCount++;
  956. return FAIL;
  957. }
  958. //receive result head
  959. if ((size = recvTcpSocket(fd, (uint8_t *)&csuResult.Head, sizeof(csuResult.Head))) < 0) {
  960. DEBUG_ERROR("TCP socket RX head fail = %d\r\n", size);
  961. gDoCommGblData.DisConnCount++;
  962. return FAIL;
  963. }
  964. //receive result raw data
  965. if ((size = recvTcpSocket(fd, (uint8_t *)&csuResult.Data, csuResult.Head.DataLen)) < 0) {
  966. DEBUG_ERROR("TCP socket RX data fail = %d\r\n", size);
  967. gDoCommGblData.DisConnCount++;
  968. return FAIL;
  969. }
  970. ret = responsePackeHandle(fd, (uint8_t *)&csuResult, id, csuCmdPkt.Data.Register);
  971. return ret;
  972. }
  973. static int writeWaitPlugItState(int fd, uint8_t id)
  974. {
  975. int ret = PASS;
  976. uint8_t data[2] = {ShmSysConfigAndInfo->SysInfo.WaitForPlugit, 0};
  977. //printf("WaitForPlugit = %d\r\n", ShmSysConfigAndInfo->SysInfo.WaitForPlugit);
  978. if ((ret = composeSocketData(fd,
  979. id,
  980. OP_WRITE_DATA,
  981. REG_WAIT_PLUG_IT_STATE,
  982. 1,
  983. &data[0])) == FAIL) {
  984. return ret;
  985. }
  986. return ret;
  987. }
  988. static int readQRcodeURLAndSystemDate(int fd)
  989. {
  990. int ret = PASS;
  991. ret = composeSocketData(fd,
  992. ID_REGISTER,
  993. OP_READ_DATA,
  994. REG_QRCODE_URL_INFO,
  995. 0,
  996. NULL);
  997. return ret;
  998. }
  999. static int writePresentChargingInfo(int fd, uint8_t plugNum, uint8_t id)
  1000. {
  1001. int ret = PASS;
  1002. uint8_t dataBuf[16] = {0};
  1003. PreChargingInfo *pPreChargingInfo = (PreChargingInfo *)dataBuf;
  1004. pPreChargingInfo->PresentChargingVoltage = htons((uint16_t)ChargingData[plugNum]->PresentChargingVoltage * 10);
  1005. pPreChargingInfo->PresentChargingCurrent = htons((uint16_t)ChargingData[plugNum]->PresentChargingCurrent * 10);
  1006. pPreChargingInfo->RemainChargingDuration = htonl(ChargingData[plugNum]->RemainChargingDuration);
  1007. pPreChargingInfo->EvBatterySoc = ChargingData[plugNum]->EvBatterySoc;
  1008. ret = composeSocketData(fd,
  1009. id,
  1010. OP_WRITE_DATA,
  1011. REG_PRESENT_CHARGING_INFO,
  1012. sizeof(PreChargingInfo),
  1013. (uint8_t *)pPreChargingInfo);
  1014. return ret;
  1015. }
  1016. static int writeOtherModuleVersion(int fd)
  1017. {
  1018. int ret = PASS;
  1019. uint8_t data[255] = {0};
  1020. uint8_t dataLen = 0;
  1021. //report other module version message
  1022. memcpy(&data[dataLen], ShmSysConfigAndInfo->SysInfo.FanModuleFwRev, VERSION_BUF_SIZE);
  1023. dataLen += VERSION_BUF_SIZE;
  1024. memcpy(&data[dataLen], ShmSysConfigAndInfo->SysInfo.RelayModuleFwRev, VERSION_BUF_SIZE);
  1025. dataLen += VERSION_BUF_SIZE;
  1026. memcpy(&data[dataLen], ShmSysConfigAndInfo->SysInfo.Connector1FwRev, VERSION_BUF_SIZE);
  1027. dataLen += VERSION_BUF_SIZE;
  1028. memcpy(&data[dataLen], ShmSysConfigAndInfo->SysInfo.Connector2FwRev, VERSION_BUF_SIZE);
  1029. dataLen += VERSION_BUF_SIZE;
  1030. memcpy(&data[dataLen], ShmSysConfigAndInfo->SysInfo.LedModuleFwRev, VERSION_BUF_SIZE);
  1031. dataLen += VERSION_BUF_SIZE;
  1032. ret = composeSocketData(fd,
  1033. ID_REGISTER,
  1034. OP_WRITE_DATA,
  1035. REG_REPORT_OTHER_VERSION,
  1036. dataLen,
  1037. &data[0]);
  1038. return ret;
  1039. }
  1040. static int writeCsuModuleVersion(int fd)
  1041. {
  1042. int ret = PASS;
  1043. uint8_t data[255] = {0};
  1044. uint8_t dataLen = 0;
  1045. //report CSU platform version message
  1046. memcpy(&data[dataLen], ShmSysConfigAndInfo->SysInfo.CsuBootLoadFwRev, VERSION_BUF_SIZE);
  1047. dataLen += VERSION_BUF_SIZE;
  1048. memcpy(&data[dataLen], ShmSysConfigAndInfo->SysInfo.CsuKernelFwRev, VERSION_BUF_SIZE);
  1049. dataLen += VERSION_BUF_SIZE;
  1050. memcpy(&data[dataLen], ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev, VERSION_BUF_SIZE);
  1051. dataLen += VERSION_BUF_SIZE;
  1052. memcpy(&data[dataLen], ShmSysConfigAndInfo->SysInfo.CsuPrimFwRev, VERSION_BUF_SIZE);
  1053. dataLen += VERSION_BUF_SIZE;
  1054. ret = composeSocketData(fd,
  1055. ID_REGISTER,
  1056. OP_WRITE_DATA,
  1057. REG_REPORT_CSU_VERSION,
  1058. dataLen,
  1059. &data[0]);
  1060. return ret;
  1061. }
  1062. static int readMiscCommand(int fd, uint8_t id)
  1063. {
  1064. int ret = PASS;
  1065. ret = composeSocketData(fd,
  1066. id,
  1067. OP_READ_DATA,
  1068. REG_MISC_CONTROL,
  1069. 0,
  1070. NULL);
  1071. return ret;
  1072. }
  1073. static int readChargePermission(int fd, uint8_t id)
  1074. {
  1075. //int ret = PASS;
  1076. //if ((ret = composeSocketData(fd,
  1077. // id,
  1078. // OP_READ_DATA,
  1079. // REG_CHARGING_PERMISSION,
  1080. // 0,
  1081. // NULL)) == FAIL) {
  1082. // return ret;
  1083. //}
  1084. return composeSocketData(fd,
  1085. id,
  1086. OP_READ_DATA,
  1087. REG_CHARGING_PERMISSION,
  1088. 0,
  1089. NULL);
  1090. }
  1091. static int writeUserID(int fd, uint8_t id, uint8_t *pUserID)
  1092. {
  1093. if ((strlen((char *)pUserID) <= 0) || (pUserID == NULL)) {
  1094. return FAIL;
  1095. }
  1096. return composeSocketData(fd,
  1097. id,
  1098. OP_WRITE_DATA,
  1099. REG_USER_ID,
  1100. strlen((char *)pUserID),
  1101. pUserID);
  1102. }
  1103. static int writeConnectorState(int fd, uint8_t plugNum, uint8_t id)
  1104. {
  1105. uint8_t dataBuf[2] = {0};
  1106. int ret = PASS;
  1107. if (ChargingData[plugNum]->SystemStatus <= 2) {
  1108. dataBuf[0] = 0; //idle
  1109. } else if ((ChargingData[plugNum]->SystemStatus <= 7) ||
  1110. (ChargingData[plugNum]->SystemStatus == 17) ||
  1111. (ChargingData[plugNum]->SystemStatus == 18)) {
  1112. dataBuf[0] = 1; //preparing
  1113. } else if (ChargingData[plugNum]->SystemStatus == 8) {
  1114. dataBuf[0] = 2; //charging
  1115. } else if (ChargingData[plugNum]->SystemStatus == 9) {
  1116. dataBuf[0] = 3; //terminating
  1117. }
  1118. ret = composeSocketData(fd,
  1119. id,
  1120. OP_WRITE_DATA,
  1121. REG_CONNECTOR_STATE,
  1122. 1,
  1123. &dataBuf[0]);
  1124. return ret;
  1125. }
  1126. static int writePlugInStatus(int fd, uint8_t plugNum, uint8_t id)
  1127. {
  1128. int ret = PASS;
  1129. uint8_t dataBuf[2] = {ChargingData[plugNum]->ConnectorPlugIn, 0};
  1130. ret = composeSocketData(fd,
  1131. id,
  1132. OP_WRITE_DATA,
  1133. REG_PLUG_IN_STATE,
  1134. 1,
  1135. &dataBuf[0]);
  1136. return ret;
  1137. }
  1138. static int readSoftwareUpdate(int fd)
  1139. {
  1140. int ret = PASS;
  1141. uint8_t dataBuf[2] = {ShmSysConfigAndInfo->SysInfo.FirmwareUpdate, 0};
  1142. ret = composeSocketData(fd,
  1143. 0x01,
  1144. OP_READ_DATA,
  1145. REG_SOFTWARE_UPDATE,
  1146. 1,
  1147. &dataBuf[0]);
  1148. return ret;
  1149. }
  1150. static int writeChargingTarget(int fd, uint8_t plugNum, uint8_t id)
  1151. {
  1152. int ret = PASS;
  1153. float ChargingVolt, ChargingAmp;
  1154. uint8_t dataBuf[4] = {0};
  1155. ChargingVolt = ChargingData[plugNum]->EvBatterytargetVoltage;
  1156. ChargingAmp = ChargingData[plugNum]->EvBatterytargetCurrent;
  1157. ChargingVolt *= 10;
  1158. ChargingAmp *= 10;
  1159. dataBuf[0] = ((uint16_t)ChargingVolt >> 8) & 0xFF;
  1160. dataBuf[1] = ((uint16_t)ChargingVolt) & 0xFF;
  1161. dataBuf[2] = ((uint16_t)ChargingAmp >> 8) & 0xFF;
  1162. dataBuf[3] = ((uint16_t)ChargingAmp) & 0xFF;
  1163. ret = composeSocketData(fd,
  1164. id,
  1165. OP_WRITE_DATA,
  1166. REG_CHARGING_TARGET,
  1167. sizeof(dataBuf),
  1168. &dataBuf[0]);
  1169. return ret;
  1170. }
  1171. static int readChargingCapability(int fd, uint8_t id)
  1172. {
  1173. int ret = PASS;
  1174. ret = composeSocketData(fd,
  1175. id,
  1176. OP_READ_DATA,
  1177. REG_CHARGING_CAP,
  1178. 0,
  1179. NULL);
  1180. return ret;
  1181. }
  1182. static int writeDispenserStatus(int fd)
  1183. {
  1184. uint8_t warningCount = 0;
  1185. uint8_t count = 0;
  1186. uint8_t CurWarnCodeTmp[10][6] = {0};
  1187. uint8_t PreWarnCodeTmp[10][6] = {0};
  1188. int ret = PASS;
  1189. if ((ShmSysConfigAndInfo->SysWarningInfo.WarningCount <= 0) &&
  1190. (gPreSysWarningInfo.WarningCount <= 0)) {
  1191. return FAIL;
  1192. }
  1193. warningCount = ShmSysConfigAndInfo->SysWarningInfo.WarningCount;
  1194. for (count = 0; count < warningCount; count++) {
  1195. memcpy(CurWarnCodeTmp[count], ShmSysConfigAndInfo->SysWarningInfo.WarningCode[count], WARNING_CODE_SIZE);
  1196. }
  1197. warningCount = gPreSysWarningInfo.WarningCount;
  1198. for (count = 0; count < warningCount; count++) {
  1199. memcpy(PreWarnCodeTmp[count], gPreSysWarningInfo.WarningCode[count], WARNING_CODE_SIZE);
  1200. }
  1201. if (strcmp((char *)&CurWarnCodeTmp[0], (char *)&PreWarnCodeTmp[0]) == 0) {
  1202. return FAIL;
  1203. }
  1204. ret = composeSocketData(fd,
  1205. ID_OF_DISPENSER,
  1206. OP_WRITE_DATA,
  1207. REG_DISPENSER_STATUS,
  1208. strlen((char *)CurWarnCodeTmp),
  1209. &CurWarnCodeTmp[0][0]);
  1210. return ret;
  1211. }
  1212. static int readPowerCabinetStatus(int fd)
  1213. {
  1214. int ret = PASS;
  1215. ret = composeSocketData(fd,
  1216. ID_OF_DISPENSER,
  1217. OP_READ_DATA,
  1218. REG_POWER_CABINET_STATUS,
  1219. 0,
  1220. NULL);
  1221. return ret;
  1222. }
  1223. static int readConnectorID(int fd)
  1224. {
  1225. int ret = PASS;
  1226. ret = composeSocketData(fd,
  1227. ID_REGISTER,
  1228. OP_READ_DATA,
  1229. REG_CONNECTOR_ID,
  1230. 0,
  1231. NULL);
  1232. return ret;
  1233. }
  1234. static int WriteModelName(int fd)
  1235. {
  1236. int ret = PASS;
  1237. uint8_t Tmp = 0;
  1238. uint8_t TmpBuf[255] = {0};
  1239. memcpy(TmpBuf,
  1240. ShmSysConfigAndInfo->SysConfig.ModelName,
  1241. strlen((char *)ShmSysConfigAndInfo->SysConfig.ModelName));
  1242. Tmp = (uint8_t)(ShmPrimaryMcuData->InputDet.bits.Key2 << 2 |
  1243. ShmPrimaryMcuData->InputDet.bits.Key1 << 1 |
  1244. ShmPrimaryMcuData->InputDet.bits.Key0);
  1245. TmpBuf[strlen((char *)ShmSysConfigAndInfo->SysConfig.ModelName)] = Tmp; //Dispenser switch value
  1246. ret = composeSocketData(fd,
  1247. ID_REGISTER,
  1248. OP_WRITE_DATA,
  1249. REG_MODEL_NAME,
  1250. strlen((char *)ShmSysConfigAndInfo->SysConfig.ModelName) + 1,
  1251. &TmpBuf[0]);
  1252. return ret;
  1253. }
  1254. static void calDisconnectCount(uint8_t *pValue, uint8_t maxCount)
  1255. {
  1256. sleep(3); //wait 1 second
  1257. if ((*pValue) >= maxCount) {
  1258. setTcpStatus(ABNORMAL);
  1259. } else {
  1260. (*pValue)++;
  1261. }
  1262. }
  1263. static int CheckNetworkStatus(void)
  1264. {
  1265. char *ipAddr = (char *)&ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthIpAddress;
  1266. //printf("Check network IP Header = %s\n", ipAddr);
  1267. if (strstr(ipAddr, CMP_ETH_IP_HEAD) != NULL) {
  1268. return 1;
  1269. }
  1270. return 0;
  1271. }
  1272. static int FindChargingInfoData(byte target, struct ChargingInfoData **chargingData)
  1273. {
  1274. uint8_t index = 0;
  1275. for (index = 0; index < CHAdeMO_QUANTITY; index++) {
  1276. if (ShmSysConfigAndInfo->SysInfo.ChademoChargingData[index].Index == target) {
  1277. chargingData[target] = &ShmSysConfigAndInfo->SysInfo.ChademoChargingData[index];
  1278. return 1;
  1279. }
  1280. }
  1281. for (index = 0; index < CCS_QUANTITY; index++) {
  1282. if (ShmSysConfigAndInfo->SysInfo.CcsChargingData[index].Index == target) {
  1283. chargingData[target] = &ShmSysConfigAndInfo->SysInfo.CcsChargingData[index];
  1284. return 1;
  1285. }
  1286. }
  1287. for (index = 0; index < GB_QUANTITY; index++) {
  1288. if (ShmSysConfigAndInfo->SysInfo.GbChargingData[index].Index == target) {
  1289. chargingData[target] = &ShmSysConfigAndInfo->SysInfo.GbChargingData[index];
  1290. return 1;
  1291. }
  1292. }
  1293. return 0;
  1294. }
  1295. static int InitShareMemory(void)
  1296. {
  1297. int result = 1;
  1298. int MeterSMId = 0;
  1299. if ((MeterSMId = shmget(ShmSysConfigAndInfoKey, sizeof(struct SysConfigAndInfo), 0777)) < 0) {
  1300. DEBUG_ERROR("shmget ShmSysConfigAndInfo NG\n");
  1301. result = 0;
  1302. } else if ((ShmSysConfigAndInfo = shmat(MeterSMId, NULL, 0)) == (void *) - 1) {
  1303. DEBUG_ERROR("[shmat ShmSysConfigAndInfo NG\n");
  1304. result = 0;
  1305. }
  1306. if ((MeterSMId = shmget(ShmStatusCodeKey, sizeof(struct StatusCodeData), 0777)) < 0) {
  1307. DEBUG_ERROR("shmget ShmStatusCodeData NG\n");
  1308. result = 0;
  1309. } else if ((ShmStatusCodeData = shmat(MeterSMId, NULL, 0)) == (void *) - 1) {
  1310. DEBUG_ERROR("shmat ShmStatusCodeData NG\n");
  1311. result = 0;
  1312. }
  1313. //creat ShmPsuData
  1314. if ((MeterSMId = shmget(ShmPsuKey, sizeof(struct PsuData), 0777)) < 0) {
  1315. DEBUG_ERROR("shmget ShmPsuData NG \n");
  1316. result = 0;
  1317. } else if ((ShmPsuData = shmat(MeterSMId, NULL, 0)) == (void *) - 1) {
  1318. DEBUG_ERROR("shmat ShmPsuData NG \n");
  1319. result = 0;
  1320. }
  1321. //creat ShmOCPP16Data
  1322. if ((MeterSMId = shmget(ShmOcppModuleKey, sizeof(struct OCPP16Data), 0777)) < 0) {
  1323. DEBUG_ERROR("shmget ShmOCPP16Data NG \n");
  1324. result = 0;
  1325. } else if ((ShmOCPP16Data = shmat(MeterSMId, NULL, 0)) == (void *) - 1) {
  1326. DEBUG_ERROR("shmat ShmOCPP16Data NG \n");
  1327. result = 0;
  1328. }
  1329. if ((MeterSMId = shmget(ShmPrimaryMcuKey, sizeof(struct PrimaryMcuData), 0777)) < 0) {
  1330. DEBUG_ERROR("shmget ShmPrimaryMcuData NG\n");
  1331. result = 0;
  1332. } else if ((ShmPrimaryMcuData = shmat(MeterSMId, NULL, 0)) == (void *) - 1) {
  1333. DEBUG_ERROR("shmat ShmPrimaryMcuData NG\n");
  1334. result = 0;
  1335. }
  1336. //creat Audi customization info
  1337. if ((MeterSMId = shmget(ShmAudiCustInfoKey, sizeof(AudiCustInfo), IPC_CREAT | 0777)) < 0) {
  1338. DEBUG_ERROR("[main]CreatShareMemory:shmget select gun info NG \n");
  1339. return 0;
  1340. } else if ((gAudiCustInfo = shmat(MeterSMId, NULL, 0)) == (void *) - 1) {
  1341. DEBUG_ERROR("[main]CreatShareMemory:shmat shmget select gun info \n");
  1342. return 0;
  1343. }
  1344. return result;
  1345. }
  1346. static void updateFirmwareProcess(int fd, uint8_t totalConnCount)
  1347. {
  1348. bool canUpdateFirmware = true;
  1349. uint8_t plugNum = 0;
  1350. uint8_t ackCount = 5;
  1351. struct timeb updateTime;
  1352. if (ShmSysConfigAndInfo->SysWarningInfo.Level != 2) {
  1353. for (plugNum = 0; plugNum < totalConnCount; plugNum++) {
  1354. if (ChargingData[plugNum]->SystemStatus != S_IDLE &&
  1355. ChargingData[plugNum]->SystemStatus != S_RESERVATION &&
  1356. ChargingData[plugNum]->SystemStatus != S_MAINTAIN) {
  1357. canUpdateFirmware = false;
  1358. }
  1359. }
  1360. }
  1361. if (canUpdateFirmware) {
  1362. ftime(&updateTime);
  1363. if (DiffTimeb(gRegTimeUp[0][REG_SOFTWARE_UPDATE], updateTime) > LOOP_RETRY_TIME * 5) {
  1364. readSoftwareUpdate(fd);
  1365. ftime(&gRegTimeUp[0][REG_SOFTWARE_UPDATE]);
  1366. }
  1367. if (ShmSysConfigAndInfo->SysInfo.FirmwareUpdate == YES) {
  1368. while (ackCount != 0) {
  1369. ftime(&updateTime);
  1370. if (DiffTimeb(gRegTimeUp[0][REG_SOFTWARE_UPDATE], updateTime) > LOOP_RETRY_TIME / 2) {
  1371. readSoftwareUpdate(fd);
  1372. ftime(&gRegTimeUp[0][REG_SOFTWARE_UPDATE]);
  1373. if (ShmSysConfigAndInfo->SysInfo.FirmwareUpdate == NO) {
  1374. ackCount--;
  1375. }
  1376. }
  1377. usleep(128);
  1378. }
  1379. }
  1380. }
  1381. }
  1382. static void checkAuthorProcess(int fd, uint8_t plugNum)
  1383. {
  1384. int ret = 0;
  1385. struct timeb AuthNowTime;
  1386. if ((ShmOCPP16Data->SpMsg.bits.AuthorizeReq == YES) || (ShmSysConfigAndInfo->SysInfo.AuthorizeFlag == YES)) {
  1387. ftime(&AuthNowTime);
  1388. if (DiffTimeb(gRegTimeUp[plugNum][REG_USER_ID], AuthNowTime) > LOOP_RETRY_TIME) {
  1389. ret = writeUserID(fd,
  1390. ShmSysConfigAndInfo->SysInfo.CurGunSelected + 1,
  1391. ShmSysConfigAndInfo->SysConfig.UserId);
  1392. if (ret >= 0) {
  1393. memset(ShmOCPP16Data->Authorize.ResponseIdTagInfo.Status,
  1394. 0,
  1395. sizeof(ShmOCPP16Data->Authorize.ResponseIdTagInfo.Status));
  1396. if (ret == 0) {
  1397. strcpy((char *)ShmOCPP16Data->Authorize.ResponseIdTagInfo.Status, "Invalid");
  1398. } else {
  1399. strcpy((char *)ShmOCPP16Data->Authorize.ResponseIdTagInfo.Status, "Accepted");
  1400. }
  1401. //printf("gAudiCustInfo->PricesInfo[plugNum].Balance = %.2f, %.2f\r\n",
  1402. // gAudiCustInfo->PricesInfo[plugNum].Balance,
  1403. // FAIL_BALANCE_PRICES);
  1404. if (gAudiCustInfo->PricesInfo[plugNum].Balance != FAIL_BALANCE_PRICES) {
  1405. ShmOCPP16Data->SpMsg.bits.AuthorizeConf = 1; //isAuthorizedComplete
  1406. ShmOCPP16Data->SpMsg.bits.AuthorizeReq = 0;
  1407. ShmSysConfigAndInfo->SysInfo.AuthorizeFlag = 0;
  1408. ShmPsuData->SystemAvailablePower = 0;
  1409. ShmPsuData->SystemPresentPsuQuantity = 0;
  1410. }
  1411. }
  1412. ftime(&gRegTimeUp[plugNum][REG_USER_ID]);
  1413. }
  1414. }
  1415. }
  1416. static int messageTrigger(int fd, uint8_t plugNum, uint8_t gunID, uint8_t curReg)
  1417. {
  1418. int ret = DISPENSER_INIT_SUCC;
  1419. int isContinue = 1;
  1420. struct timeb NowTime;
  1421. while (isContinue) {
  1422. usleep(128);
  1423. ftime(&NowTime);
  1424. switch (curReg) {
  1425. case REG_MODEL_NAME:
  1426. if (WriteModelName(fd) == PASS) {
  1427. gDoCommGblData.DisConnCount = 0;
  1428. curReg = REG_CONNECTOR_ID;
  1429. }
  1430. break;
  1431. case REG_CONNECTOR_ID:
  1432. if (readConnectorID(fd) == PASS) {
  1433. gDoCommGblData.DisConnCount = 0;
  1434. curReg = REG_REPORT_CSU_VERSION;
  1435. }
  1436. break;
  1437. case REG_POWER_CABINET_STATUS:
  1438. if (DiffTimeb(gRegTimeUp[plugNum][curReg], NowTime) > LOOP_RETRY_TIME) {
  1439. readPowerCabinetStatus(fd);
  1440. ftime(&gRegTimeUp[plugNum][curReg]);
  1441. }
  1442. curReg = REG_DISPENSER_STATUS;
  1443. break;
  1444. case REG_DISPENSER_STATUS:
  1445. if (DiffTimeb(gRegTimeUp[plugNum][curReg], NowTime) > LOOP_RETRY_TIME) {
  1446. writeDispenserStatus(fd);
  1447. ftime(&gRegTimeUp[plugNum][curReg]);
  1448. }
  1449. curReg = REG_PLUG_IN_STATE;
  1450. break;
  1451. //case REG_CHARGING_CAP:
  1452. // readChargingCapability(fd, plugNum, gunID);
  1453. // break;
  1454. //case REG_CHARGING_TARGET:
  1455. // writeChargingTarget(fd, plugNum, gunID);
  1456. // break;
  1457. //case REG_SOFTWARE_UPDATE:
  1458. // if (ChargingData[plugNum]->SystemStatus == S_IDLE) {
  1459. // if (DiffTimeb(gRegTimeUp[plugNum][curReg], NowTime) > LOOP_RETRY_TIME * 10) {
  1460. // readSoftwareUpdate(fd);
  1461. // ftime(&gRegTimeUp[plugNum][curReg]);
  1462. // }
  1463. // while (ShmSysConfigAndInfo->SysInfo.FirmwareUpdate == YES) {
  1464. // ftime(&NowTime);
  1465. // if (DiffTimeb(gRegTimeUp[plugNum][curReg], NowTime) > LOOP_RETRY_TIME) {
  1466. // readSoftwareUpdate(fd);
  1467. // }
  1468. // usleep(128);
  1469. // }
  1470. // }
  1471. // isContinue = 0;
  1472. // break;
  1473. case REG_PLUG_IN_STATE:
  1474. if (DiffTimeb(gRegTimeUp[plugNum][curReg], NowTime) > LOOP_RETRY_TIME) {
  1475. writePlugInStatus(fd, plugNum, gunID);
  1476. ftime(&gRegTimeUp[plugNum][curReg]);
  1477. }
  1478. curReg = REG_CONNECTOR_STATE;
  1479. break;
  1480. case REG_CONNECTOR_STATE:
  1481. if (DiffTimeb(gRegTimeUp[plugNum][curReg], NowTime) > LOOP_RETRY_TIME) {
  1482. writeConnectorState(fd, plugNum, gunID);
  1483. ftime(&gRegTimeUp[plugNum][curReg]);
  1484. }
  1485. //check misc command from power cabinet
  1486. if (gDoCommGblData.MiscCmd != 0) {
  1487. curReg = gDoCommGblData.MiscCmd;
  1488. } else {
  1489. curReg = REG_QRCODE_URL_INFO;
  1490. }
  1491. break;
  1492. //case REG_USER_ID:
  1493. // writeUserID(fd, gunID, uint8_t *pUserID);
  1494. // break;
  1495. //case REG_CHARGING_PERMISSION:
  1496. // readChargePermission(fd, gunID);
  1497. // break;
  1498. case REG_MISC_CONTROL:
  1499. readMiscCommand(fd, gunID);
  1500. isContinue = 0;
  1501. break;
  1502. case REG_REPORT_CSU_VERSION:
  1503. case REG_REPORT_OTHER_VERSION:
  1504. if (gDoCommGblData.MiscCmd != 0) {
  1505. writeCsuModuleVersion(fd);
  1506. writeOtherModuleVersion(fd);
  1507. clearMiscCommand();
  1508. isContinue = 0;
  1509. } else {
  1510. //power up write dispenser version and get power cabinet system ID
  1511. if ((writeCsuModuleVersion(fd) == PASS) &&
  1512. (writeOtherModuleVersion(fd) == PASS) &&
  1513. (readQRcodeURLAndSystemDate(fd) == PASS)) {
  1514. //gDoCommGblData.DisConnCount = 0;
  1515. isContinue = 0;
  1516. }
  1517. }
  1518. break;
  1519. //case REG_PRESENT_CHARGING_INFO:
  1520. // break;
  1521. case REG_QRCODE_URL_INFO:
  1522. if (gunID == 1) {
  1523. if (DiffTimeb(gRegTimeUp[plugNum][curReg], NowTime) > (LOOP_RETRY_TIME * 10)) {
  1524. readQRcodeURLAndSystemDate(fd);
  1525. ftime(&gRegTimeUp[plugNum][curReg]);
  1526. }
  1527. }
  1528. isContinue = 0;
  1529. //curReg = REG_SOFTWARE_UPDATE;
  1530. break;
  1531. //case REG_WAIT_PLUG_IT_STATE:
  1532. // if (DiffTimeb(gRegTimeUp[plugNum][curReg], NowTime) > (LOOP_RETRY_TIME / 10)) {
  1533. // writeWaitPlugItState(fd, gunID);
  1534. // ftime(&gRegTimeUp[plugNum][curReg]);
  1535. // }
  1536. // isContinue = 0;
  1537. // break;
  1538. default:
  1539. DEBUG_ERROR("error curReg = %x\r\n", curReg);
  1540. gDoCommGblData.MiscCmd = 0;
  1541. gDoCommGblData.DisConnCount = CHECK_NETWORK_FAIL_COUNT;
  1542. isContinue = 0;
  1543. break;
  1544. }
  1545. if (gDoCommGblData.DisConnCount >= CHECK_NETWORK_FAIL_COUNT) {
  1546. return DISPENSER_SOCKET_RECONN;
  1547. }
  1548. }//for
  1549. return ret;
  1550. }
  1551. //static bool isDetectPlugin()
  1552. //{
  1553. // if (ShmSysConfigAndInfo->SysInfo.WaitForPlugit == YES) {
  1554. // return YES;
  1555. // }
  1556. //
  1557. // return NO;
  1558. //}
  1559. static void systemStatusProcess(int fd, uint8_t plugNum, uint8_t gunID)
  1560. {
  1561. struct timeb AuthNowTime = {0};
  1562. switch (ChargingData[plugNum]->SystemStatus) {
  1563. case S_IDLE:
  1564. case S_RESERVATION:
  1565. case S_MAINTAIN:
  1566. case S_ALARM:
  1567. case S_AUTHORIZING:
  1568. checkAuthorProcess(fd, plugNum);
  1569. //authorization complete, write wait plug it state
  1570. if (gAudiCustInfo->PricesInfo[plugNum].Balance != FAIL_BALANCE_PRICES) {
  1571. ftime(&AuthNowTime);
  1572. if (DiffTimeb(gRegTimeUp[plugNum][REG_WAIT_PLUG_IT_STATE], AuthNowTime) > (LOOP_RETRY_TIME / 10)) {
  1573. writeWaitPlugItState(fd, gunID);
  1574. ftime(&gRegTimeUp[plugNum][REG_WAIT_PLUG_IT_STATE]);
  1575. }
  1576. ftime(&AuthNowTime);
  1577. if (DiffTimeb(gRegTimeUp[plugNum][REG_CHARGING_CAP], AuthNowTime) > LOOP_RETRY_TIME) {
  1578. readChargingCapability(fd, gunID);
  1579. ftime(&gRegTimeUp[plugNum][REG_CHARGING_CAP]);
  1580. }
  1581. }
  1582. break;
  1583. case S_PREPARNING: //get permission
  1584. ftime(&AuthNowTime);
  1585. if (DiffTimeb(gRegTimeUp[plugNum][REG_CONNECTOR_STATE], AuthNowTime) > LOOP_RETRY_TIME) {
  1586. writeConnectorState(fd, plugNum, gunID);
  1587. ftime(&gRegTimeUp[plugNum][REG_CONNECTOR_STATE]);
  1588. }
  1589. ftime(&AuthNowTime);
  1590. if (DiffTimeb(gRegTimeUp[plugNum][REG_CHARGING_PERMISSION], AuthNowTime) > LOOP_RETRY_TIME) {
  1591. if (readChargePermission(fd, gunID) && readChargingCapability(fd, gunID)) {
  1592. ShmPsuData->SystemAvailablePower = (ChargingData[0]->AvailableChargingPower +
  1593. ChargingData[1]->AvailableChargingPower);
  1594. ShmPsuData->SystemPresentPsuQuantity = (ShmPsuData->SystemAvailablePower / 30);
  1595. }
  1596. ftime(&gRegTimeUp[plugNum][REG_CHARGING_PERMISSION]);
  1597. }
  1598. ftime(&AuthNowTime);
  1599. if (DiffTimeb(gRegTimeUp[plugNum][REG_PRESENT_CHARGING_INFO], AuthNowTime) > LOOP_RETRY_TIME) {
  1600. writePresentChargingInfo(fd, plugNum, gunID);
  1601. ftime(&gRegTimeUp[plugNum][REG_PRESENT_CHARGING_INFO]);
  1602. }
  1603. break;
  1604. case S_PREPARING_FOR_EV://wait connector lock
  1605. ftime(&AuthNowTime);
  1606. if (DiffTimeb(gRegTimeUp[plugNum][REG_CHARGING_CAP], AuthNowTime) > LOOP_RETRY_TIME) {
  1607. readChargingCapability(fd, gunID);
  1608. ftime(&gRegTimeUp[plugNum][REG_CHARGING_CAP]);
  1609. }
  1610. ftime(&AuthNowTime);
  1611. if (DiffTimeb(gRegTimeUp[plugNum][REG_CHARGING_PERMISSION], AuthNowTime) > LOOP_RETRY_TIME) {
  1612. if (readChargePermission(fd, gunID) == 0) {
  1613. DEBUG_INFO("Stop charging by power cabinet's permission = %d, %d\r\n",
  1614. plugNum,
  1615. REG_CHARGING_PERMISSION);
  1616. ChargingData[plugNum]->StopChargeFlag = 1;
  1617. }
  1618. ftime(&gRegTimeUp[plugNum][REG_CHARGING_PERMISSION]);
  1619. }
  1620. ftime(&AuthNowTime);
  1621. if (DiffTimeb(gRegTimeUp[plugNum][REG_PRESENT_CHARGING_INFO], AuthNowTime) > LOOP_RETRY_TIME) {
  1622. writePresentChargingInfo(fd, plugNum, gunID);
  1623. ftime(&gRegTimeUp[plugNum][REG_PRESENT_CHARGING_INFO]);
  1624. }
  1625. break;
  1626. case S_PREPARING_FOR_EVSE: //insaulation test
  1627. case S_CCS_PRECHARGE_ST0:
  1628. case S_CCS_PRECHARGE_ST1:
  1629. writeChargingTarget(fd, plugNum, gunID);
  1630. ftime(&AuthNowTime);
  1631. if (DiffTimeb(gRegTimeUp[plugNum][REG_CHARGING_CAP], AuthNowTime) > LOOP_RETRY_TIME) {
  1632. readChargingCapability(fd, gunID);
  1633. ftime(&gRegTimeUp[plugNum][REG_CHARGING_CAP]);
  1634. }
  1635. ftime(&AuthNowTime);
  1636. if (DiffTimeb(gRegTimeUp[plugNum][REG_CHARGING_PERMISSION], AuthNowTime) > LOOP_RETRY_TIME) {
  1637. if (readChargePermission(fd, gunID) == 0) {
  1638. DEBUG_INFO("Stop charging by power cabinet's permission = %d, %d\r\n",
  1639. plugNum,
  1640. REG_CHARGING_PERMISSION);
  1641. ChargingData[plugNum]->StopChargeFlag = 1;
  1642. }
  1643. ftime(&gRegTimeUp[plugNum][REG_CHARGING_PERMISSION]);
  1644. }
  1645. ftime(&AuthNowTime);
  1646. if (DiffTimeb(gRegTimeUp[plugNum][REG_PRESENT_CHARGING_INFO], AuthNowTime) > LOOP_RETRY_TIME) {
  1647. writePresentChargingInfo(fd, plugNum, gunID);
  1648. ftime(&gRegTimeUp[plugNum][REG_PRESENT_CHARGING_INFO]);
  1649. }
  1650. break;
  1651. case S_CHARGING: //charging
  1652. case S_TERMINATING:
  1653. ftime(&AuthNowTime);
  1654. if (DiffTimeb(gRegTimeUp[plugNum][REG_CHARGING_CAP], AuthNowTime) > LOOP_RETRY_TIME) {
  1655. readChargingCapability(fd, gunID);
  1656. ftime(&gRegTimeUp[plugNum][REG_CHARGING_CAP]);
  1657. }
  1658. ftime(&AuthNowTime);
  1659. if (DiffTimeb(gRegTimeUp[plugNum][REG_CHARGING_PERMISSION], AuthNowTime) > LOOP_RETRY_TIME) {
  1660. if (readChargePermission(fd, gunID) == 0) {
  1661. DEBUG_INFO("Stop charging by power cabinet's permission = %d, %d\r\n",
  1662. plugNum,
  1663. REG_CHARGING_PERMISSION);
  1664. ChargingData[plugNum]->StopChargeFlag = 1;
  1665. }
  1666. ftime(&gRegTimeUp[plugNum][REG_CHARGING_PERMISSION]);
  1667. }
  1668. writeChargingTarget(fd, plugNum, gunID);
  1669. ftime(&AuthNowTime);
  1670. if (DiffTimeb(gRegTimeUp[plugNum][REG_PRESENT_CHARGING_INFO], AuthNowTime) > LOOP_RETRY_TIME) {
  1671. writePresentChargingInfo(fd, plugNum, gunID);
  1672. ftime(&gRegTimeUp[plugNum][REG_PRESENT_CHARGING_INFO]);
  1673. }
  1674. break;
  1675. case S_COMPLETE:
  1676. ftime(&AuthNowTime);
  1677. if (DiffTimeb(gRegTimeUp[plugNum][REG_CHARGING_CAP], AuthNowTime) > LOOP_RETRY_TIME) {
  1678. readChargingCapability(fd, gunID);
  1679. ftime(&gRegTimeUp[plugNum][REG_CHARGING_CAP]);
  1680. }
  1681. break;
  1682. default:
  1683. break;
  1684. }
  1685. }
  1686. static int networkCreatePorcess(void)
  1687. {
  1688. int fd = 0;
  1689. /**************** Check Network **********/
  1690. DEBUG_INFO("Check Dispenser Network Information");
  1691. while (!CheckNetworkStatus()) {
  1692. calDisconnectCount(&gDoCommGblData.DisConnCount, CHECK_NETWORK_FAIL_COUNT);
  1693. DEBUG_ERROR("disconnect count = % d\r\n", gDoCommGblData.DisConnCount);
  1694. }
  1695. DEBUG_INFO("Dispenser Network Information checked: IP = % s Netmask = % s, Gateway = % s",
  1696. ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthIpAddress,
  1697. ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthSubmaskAddress,
  1698. ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthGatewayAddress);
  1699. gDoCommGblData.DisConnCount = 0;
  1700. /**************** Power cabinet connection **********/
  1701. DEBUG_INFO("Connect to Power Cabinet");
  1702. while ((fd = doCommConnToServer()) <= 0) {
  1703. calDisconnectCount(&gDoCommGblData.DisConnCount, CONNECT_SERVER_FAIL_COUNT);
  1704. DEBUG_ERROR("disconnect count = %d, fd = %d\r\n", gDoCommGblData.DisConnCount, fd);
  1705. }
  1706. DEBUG_INFO("Power cabinet connected(fd = % d): IP = % s Netmask = % s, Gateway = % s",
  1707. fd,
  1708. ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthIpAddress,
  1709. ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthSubmaskAddress,
  1710. ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthGatewayAddress);
  1711. gDoCommGblData.DisConnCount = 0;
  1712. destroySelectGun(DESTROY_ALL_SEL);
  1713. sleep(3);
  1714. setTcpStatus(NORMAL);
  1715. return fd;
  1716. }
  1717. int main(int argc, char *argv[])
  1718. {
  1719. uint8_t plugNum = 0, gunID = 0;
  1720. uint8_t index = 0;
  1721. uint8_t totalConnCount = 0;
  1722. uint8_t initDone = DISPENER_INIT_FAIL;
  1723. int fd = 0;
  1724. int isContinue = 1;
  1725. InitSocketSigPipe();
  1726. /**************** Initialization **********/
  1727. if (InitShareMemory() == 0) {
  1728. if (ShmStatusCodeData != NULL) {
  1729. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.FailToCreateShareMemory = YES;
  1730. }
  1731. DEBUG_ERROR("DoComm Initial share memory fail\r\n");
  1732. sleep(5);
  1733. return 0;
  1734. }
  1735. totalConnCount = ShmSysConfigAndInfo->SysConfig.TotalConnectorCount;
  1736. for (index = 0; index < totalConnCount; index++) {
  1737. if (!FindChargingInfoData(index, &ChargingData[0])) {
  1738. DEBUG_ERROR("FindChargingInfoData false \n");
  1739. break;
  1740. }
  1741. clearPricesInfo(index);
  1742. }
  1743. //initial trigger message timer
  1744. for (index = 0; index < MAX_REGISTER_NUM; index++) {
  1745. ftime(&gRegTimeUp[0][index]);
  1746. ftime(&gRegTimeUp[1][index]);
  1747. usleep(50000);
  1748. }
  1749. setTcpStatus(NORMAL);
  1750. while (isContinue) {
  1751. if (initDone == DISPENER_INIT_FAIL) {
  1752. fd = networkCreatePorcess();
  1753. initDone = messageTrigger(fd, 0, 0, REG_MODEL_NAME); //first trigger model name and connector id
  1754. } else {
  1755. checkAuthorProcess(fd, 0);
  1756. plugNum = 0;
  1757. gunID = 0;
  1758. for (plugNum = 0; plugNum < totalConnCount; plugNum++) {
  1759. //plugNum = setup chargingData value for bottom layer
  1760. gunID = (plugNum + 1); //connector Id, 1 = left gun, 2 = right gun
  1761. systemStatusProcess(fd, plugNum, gunID);
  1762. initDone = messageTrigger(fd,
  1763. plugNum,
  1764. gunID,
  1765. REG_POWER_CABINET_STATUS);
  1766. if (initDone == DISPENSER_SOCKET_RECONN) {
  1767. break;
  1768. }
  1769. }
  1770. }
  1771. if (initDone == DISPENSER_SOCKET_RECONN) {
  1772. DEBUG_INFO("Disconnected from power cabinet...re-connecting");
  1773. setTcpStatus(ABNORMAL);
  1774. gDoCommGblData.DisConnCount = 0;
  1775. gDoCommGblData.SeqNum = 0;
  1776. closeSocket(fd);
  1777. fd = 0;
  1778. initDone = DISPENER_INIT_FAIL;
  1779. }
  1780. //update dispenser firmware
  1781. if (initDone != DISPENER_INIT_FAIL) {
  1782. updateFirmwareProcess(fd, totalConnCount);
  1783. }
  1784. usleep(1000);
  1785. }
  1786. }