ReDoComm.c 69 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153
  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 (getConfirmSelectedGun(plugNum) == FAIL) {
  516. DEBUG_INFO("Remote start not select gun\r\n");
  517. break;
  518. }
  519. gAudiCustInfo->PricesInfo[plugNum].Balance = transPricesUnit((int)value);
  520. DEBUG_INFO("%d misc balance = %.2f\r\n", plugNum, gAudiCustInfo->PricesInfo[plugNum].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 (plugNum == 1) {
  547. clearMiscCommand();
  548. }
  549. break;
  550. }
  551. DEBUG_INFO("Remote start charging plugNum = %d\r\n", plugNum);
  552. //if (getConfirmSelectedGun(plugNum - 1) == FAIL) {
  553. // DEBUG_INFO("Remote start not select gun\r\n");
  554. // break;
  555. //}
  556. if (getSelGunWaitToAuthor(plugNum) == FAIL) {
  557. DEBUG_ERROR("Remote start gun already charging\r\n");
  558. break;
  559. }
  560. if (ShmSysConfigAndInfo->SysInfo.CurGunSelected != (plugNum)) {
  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 = (plugNum);
  584. ShmSysConfigAndInfo->SysInfo.CurGunSelectedByAc = NO_DEFINE;
  585. }
  586. setConfirmSelGun(ShmSysConfigAndInfo->SysInfo.CurGunSelected);
  587. ShmOCPP16Data->CsMsg.bits[plugNum].RemoteStartTransactionReq = YES;
  588. gAudiCustInfo->PricesInfo[plugNum].Balance = 0.00;
  589. clearMiscCommand();
  590. break;
  591. case MISC_CMD_REMOTE_STOP_CHARGING:
  592. if (value != YES) {
  593. if (plugNum == 1) {
  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(plugNum);
  604. clearMiscCommand();
  605. break;
  606. case MISC_CMD_REMOTE_UNLOCK:
  607. if (value != YES) {
  608. if (plugNum == 1) {
  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(plugNum);
  621. } else {
  622. ChargingData[plugNum]->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 plugNum)
  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[plugNum]->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[plugNum]->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[plugNum]->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[plugNum].UserPrices = transPricesUnit(ntohl(pAccountInfo->UserPrices));
  672. ChargingData[plugNum]->ChargingFee = transPricesUnit(ntohl(pAccountInfo->TotalCost));
  673. gAudiCustInfo->PricesInfo[plugNum].Balance = transPricesUnit(ntohl(pAccountInfo->Balance));
  674. if ((pricesInfo[plugNum].UserPrices != gAudiCustInfo->PricesInfo[plugNum].UserPrices) ||
  675. (pricesInfo[plugNum].Balance != gAudiCustInfo->PricesInfo[plugNum].Balance)) {
  676. pricesInfo[plugNum].UserPrices = gAudiCustInfo->PricesInfo[plugNum].UserPrices;
  677. pricesInfo[plugNum].Balance = gAudiCustInfo->PricesInfo[plugNum].Balance;
  678. DEBUG_INFO("id = %d, user prices = %.2f, Total cost = %.2f, Account balances = %.2f, currency = %s\r\n",
  679. plugNum,
  680. gAudiCustInfo->PricesInfo[plugNum].UserPrices,
  681. ChargingData[plugNum]->ChargingFee,
  682. gAudiCustInfo->PricesInfo[plugNum].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 plugNum, 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, plugNum);
  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, plugNum, 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. uint8_t i = 0;
  941. uint8_t plugNum = 0;
  942. CsuCmdPkt csuCmdPkt = {0};
  943. CsuResultPkt csuResult = {0};
  944. csuCmdPkt.Head.SeqNum = gDoCommGblData.SeqNum++;
  945. csuCmdPkt.Head.ID = id;
  946. csuCmdPkt.Head.OP = opCode;
  947. csuCmdPkt.Head.DataLen = dataLen + 1; //+1 for register byte
  948. csuCmdPkt.Data.Register = reg;
  949. if ((data != NULL) && (dataLen > 0)) {
  950. memcpy(csuCmdPkt.Data.Data, data, dataLen);
  951. }
  952. sendPktLen = csuCmdPkt.Head.DataLen + sizeof(csuCmdPkt.Head);
  953. //Hexdump((uint8_t *)&csuCmdPkt, sendPktLen);
  954. //send command packet
  955. if ((size = sendTcpSocket(fd, (uint8_t *)&csuCmdPkt, sendPktLen)) < 0) {
  956. DEBUG_ERROR("TCP socket send packet fail = %d\r\n", size);
  957. gDoCommGblData.DisConnCount++;
  958. return FAIL;
  959. }
  960. //receive result head
  961. if ((size = recvTcpSocket(fd, (uint8_t *)&csuResult.Head, sizeof(csuResult.Head))) < 0) {
  962. DEBUG_ERROR("TCP socket RX head fail = %d\r\n", size);
  963. gDoCommGblData.DisConnCount++;
  964. return FAIL;
  965. }
  966. //receive result raw data
  967. if ((size = recvTcpSocket(fd, (uint8_t *)&csuResult.Data, csuResult.Head.DataLen)) < 0) {
  968. DEBUG_ERROR("TCP socket RX data fail = %d\r\n", size);
  969. gDoCommGblData.DisConnCount++;
  970. return FAIL;
  971. }
  972. for (i = 0; i < sizeof(gDoCommGblData.ConnectorID); i++) {
  973. if (id == gDoCommGblData.ConnectorID[i]) {
  974. plugNum = i;
  975. break;
  976. }
  977. }
  978. ret = responsePackeHandle(fd, (uint8_t *)&csuResult, plugNum, csuCmdPkt.Data.Register);
  979. return ret;
  980. }
  981. static int writeWaitPlugItState(int fd, uint8_t id)
  982. {
  983. int ret = PASS;
  984. uint8_t data[2] = {ShmSysConfigAndInfo->SysInfo.WaitForPlugit, 0};
  985. //printf("WaitForPlugit = %d\r\n", ShmSysConfigAndInfo->SysInfo.WaitForPlugit);
  986. if ((ret = composeSocketData(fd,
  987. id,
  988. OP_WRITE_DATA,
  989. REG_WAIT_PLUG_IT_STATE,
  990. 1,
  991. &data[0])) == FAIL) {
  992. return ret;
  993. }
  994. return ret;
  995. }
  996. static int readQRcodeURLAndSystemDate(int fd)
  997. {
  998. int ret = PASS;
  999. ret = composeSocketData(fd,
  1000. ID_REGISTER,
  1001. OP_READ_DATA,
  1002. REG_QRCODE_URL_INFO,
  1003. 0,
  1004. NULL);
  1005. return ret;
  1006. }
  1007. static int writePresentChargingInfo(int fd, uint8_t plugNum, uint8_t id)
  1008. {
  1009. int ret = PASS;
  1010. uint8_t dataBuf[16] = {0};
  1011. PreChargingInfo *pPreChargingInfo = (PreChargingInfo *)dataBuf;
  1012. pPreChargingInfo->PresentChargingVoltage = htons((uint16_t)ChargingData[plugNum]->PresentChargingVoltage * 10);
  1013. pPreChargingInfo->PresentChargingCurrent = htons((uint16_t)ChargingData[plugNum]->PresentChargingCurrent * 10);
  1014. pPreChargingInfo->RemainChargingDuration = htonl(ChargingData[plugNum]->RemainChargingDuration);
  1015. pPreChargingInfo->EvBatterySoc = ChargingData[plugNum]->EvBatterySoc;
  1016. ret = composeSocketData(fd,
  1017. id,
  1018. OP_WRITE_DATA,
  1019. REG_PRESENT_CHARGING_INFO,
  1020. sizeof(PreChargingInfo),
  1021. (uint8_t *)pPreChargingInfo);
  1022. return ret;
  1023. }
  1024. static int writeOtherModuleVersion(int fd)
  1025. {
  1026. int ret = PASS;
  1027. uint8_t data[255] = {0};
  1028. uint8_t dataLen = 0;
  1029. //report other module version message
  1030. memcpy(&data[dataLen], ShmSysConfigAndInfo->SysInfo.FanModuleFwRev, VERSION_BUF_SIZE);
  1031. dataLen += VERSION_BUF_SIZE;
  1032. memcpy(&data[dataLen], ShmSysConfigAndInfo->SysInfo.RelayModuleFwRev, VERSION_BUF_SIZE);
  1033. dataLen += VERSION_BUF_SIZE;
  1034. memcpy(&data[dataLen], ShmSysConfigAndInfo->SysInfo.Connector1FwRev, VERSION_BUF_SIZE);
  1035. dataLen += VERSION_BUF_SIZE;
  1036. memcpy(&data[dataLen], ShmSysConfigAndInfo->SysInfo.Connector2FwRev, VERSION_BUF_SIZE);
  1037. dataLen += VERSION_BUF_SIZE;
  1038. memcpy(&data[dataLen], ShmSysConfigAndInfo->SysInfo.LedModuleFwRev, VERSION_BUF_SIZE);
  1039. dataLen += VERSION_BUF_SIZE;
  1040. ret = composeSocketData(fd,
  1041. ID_REGISTER,
  1042. OP_WRITE_DATA,
  1043. REG_REPORT_OTHER_VERSION,
  1044. dataLen,
  1045. &data[0]);
  1046. return ret;
  1047. }
  1048. static int writeCsuModuleVersion(int fd)
  1049. {
  1050. int ret = PASS;
  1051. uint8_t data[255] = {0};
  1052. uint8_t dataLen = 0;
  1053. //report CSU platform version message
  1054. memcpy(&data[dataLen], ShmSysConfigAndInfo->SysInfo.CsuBootLoadFwRev, VERSION_BUF_SIZE);
  1055. dataLen += VERSION_BUF_SIZE;
  1056. memcpy(&data[dataLen], ShmSysConfigAndInfo->SysInfo.CsuKernelFwRev, VERSION_BUF_SIZE);
  1057. dataLen += VERSION_BUF_SIZE;
  1058. memcpy(&data[dataLen], ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev, VERSION_BUF_SIZE);
  1059. dataLen += VERSION_BUF_SIZE;
  1060. memcpy(&data[dataLen], ShmSysConfigAndInfo->SysInfo.CsuPrimFwRev, VERSION_BUF_SIZE);
  1061. dataLen += VERSION_BUF_SIZE;
  1062. ret = composeSocketData(fd,
  1063. ID_REGISTER,
  1064. OP_WRITE_DATA,
  1065. REG_REPORT_CSU_VERSION,
  1066. dataLen,
  1067. &data[0]);
  1068. return ret;
  1069. }
  1070. static int readMiscCommand(int fd, uint8_t id)
  1071. {
  1072. int ret = PASS;
  1073. ret = composeSocketData(fd,
  1074. id,
  1075. OP_READ_DATA,
  1076. REG_MISC_CONTROL,
  1077. 0,
  1078. NULL);
  1079. return ret;
  1080. }
  1081. static int readChargePermission(int fd, uint8_t id)
  1082. {
  1083. //int ret = PASS;
  1084. //if ((ret = composeSocketData(fd,
  1085. // id,
  1086. // OP_READ_DATA,
  1087. // REG_CHARGING_PERMISSION,
  1088. // 0,
  1089. // NULL)) == FAIL) {
  1090. // return ret;
  1091. //}
  1092. return composeSocketData(fd,
  1093. id,
  1094. OP_READ_DATA,
  1095. REG_CHARGING_PERMISSION,
  1096. 0,
  1097. NULL);
  1098. }
  1099. static int writeUserID(int fd, uint8_t id, uint8_t *pUserID)
  1100. {
  1101. if ((strlen((char *)pUserID) <= 0) || (pUserID == NULL)) {
  1102. return FAIL;
  1103. }
  1104. return composeSocketData(fd,
  1105. id,
  1106. OP_WRITE_DATA,
  1107. REG_USER_ID,
  1108. strlen((char *)pUserID),
  1109. pUserID);
  1110. }
  1111. static int writeConnectorState(int fd, uint8_t plugNum, uint8_t id)
  1112. {
  1113. uint8_t dataBuf[2] = {0};
  1114. int ret = PASS;
  1115. if (ChargingData[plugNum]->SystemStatus <= 2) {
  1116. dataBuf[0] = 0; //idle
  1117. } else if ((ChargingData[plugNum]->SystemStatus <= 7) ||
  1118. (ChargingData[plugNum]->SystemStatus == 17) ||
  1119. (ChargingData[plugNum]->SystemStatus == 18)) {
  1120. dataBuf[0] = 1; //preparing
  1121. } else if (ChargingData[plugNum]->SystemStatus == 8) {
  1122. dataBuf[0] = 2; //charging
  1123. } else if (ChargingData[plugNum]->SystemStatus == 9) {
  1124. dataBuf[0] = 3; //terminating
  1125. }
  1126. ret = composeSocketData(fd,
  1127. id,
  1128. OP_WRITE_DATA,
  1129. REG_CONNECTOR_STATE,
  1130. 1,
  1131. &dataBuf[0]);
  1132. return ret;
  1133. }
  1134. static int writePlugInStatus(int fd, uint8_t plugNum, uint8_t id)
  1135. {
  1136. int ret = PASS;
  1137. uint8_t dataBuf[2] = {ChargingData[plugNum]->ConnectorPlugIn, 0};
  1138. ret = composeSocketData(fd,
  1139. id,
  1140. OP_WRITE_DATA,
  1141. REG_PLUG_IN_STATE,
  1142. 1,
  1143. &dataBuf[0]);
  1144. return ret;
  1145. }
  1146. static int readSoftwareUpdate(int fd, uint8_t gunID)
  1147. {
  1148. int ret = PASS;
  1149. uint8_t dataBuf[2] = {ShmSysConfigAndInfo->SysInfo.FirmwareUpdate, 0};
  1150. ret = composeSocketData(fd,
  1151. gunID,
  1152. OP_READ_DATA,
  1153. REG_SOFTWARE_UPDATE,
  1154. 1,
  1155. &dataBuf[0]);
  1156. return ret;
  1157. }
  1158. static int writeChargingTarget(int fd, uint8_t plugNum, uint8_t id)
  1159. {
  1160. int ret = PASS;
  1161. float ChargingVolt, ChargingAmp;
  1162. uint8_t dataBuf[4] = {0};
  1163. ChargingVolt = ChargingData[plugNum]->EvBatterytargetVoltage;
  1164. ChargingAmp = ChargingData[plugNum]->EvBatterytargetCurrent;
  1165. ChargingVolt *= 10;
  1166. ChargingAmp *= 10;
  1167. dataBuf[0] = ((uint16_t)ChargingVolt >> 8) & 0xFF;
  1168. dataBuf[1] = ((uint16_t)ChargingVolt) & 0xFF;
  1169. dataBuf[2] = ((uint16_t)ChargingAmp >> 8) & 0xFF;
  1170. dataBuf[3] = ((uint16_t)ChargingAmp) & 0xFF;
  1171. ret = composeSocketData(fd,
  1172. id,
  1173. OP_WRITE_DATA,
  1174. REG_CHARGING_TARGET,
  1175. sizeof(dataBuf),
  1176. &dataBuf[0]);
  1177. return ret;
  1178. }
  1179. static int readChargingCapability(int fd, uint8_t id)
  1180. {
  1181. int ret = PASS;
  1182. ret = composeSocketData(fd,
  1183. id,
  1184. OP_READ_DATA,
  1185. REG_CHARGING_CAP,
  1186. 0,
  1187. NULL);
  1188. return ret;
  1189. }
  1190. static int writeDispenserStatus(int fd, uint8_t gunID)
  1191. {
  1192. uint8_t warningCount = 0;
  1193. uint8_t count = 0;
  1194. uint8_t CurWarnCodeTmp[10][6] = {0};
  1195. uint8_t PreWarnCodeTmp[10][6] = {0};
  1196. int ret = PASS;
  1197. if ((ShmSysConfigAndInfo->SysWarningInfo.WarningCount <= 0) &&
  1198. (gPreSysWarningInfo.WarningCount <= 0)) {
  1199. return FAIL;
  1200. }
  1201. warningCount = ShmSysConfigAndInfo->SysWarningInfo.WarningCount;
  1202. for (count = 0; count < warningCount; count++) {
  1203. memcpy(CurWarnCodeTmp[count], ShmSysConfigAndInfo->SysWarningInfo.WarningCode[count], WARNING_CODE_SIZE);
  1204. }
  1205. warningCount = gPreSysWarningInfo.WarningCount;
  1206. for (count = 0; count < warningCount; count++) {
  1207. memcpy(PreWarnCodeTmp[count], gPreSysWarningInfo.WarningCode[count], WARNING_CODE_SIZE);
  1208. }
  1209. if (strcmp((char *)&CurWarnCodeTmp[0], (char *)&PreWarnCodeTmp[0]) == 0) {
  1210. return FAIL;
  1211. }
  1212. ret = composeSocketData(fd,
  1213. gunID,
  1214. OP_WRITE_DATA,
  1215. REG_DISPENSER_STATUS,
  1216. strlen((char *)CurWarnCodeTmp),
  1217. &CurWarnCodeTmp[0][0]);
  1218. return ret;
  1219. }
  1220. static int readPowerCabinetStatus(int fd, uint8_t gunID)
  1221. {
  1222. int ret = PASS;
  1223. ret = composeSocketData(fd,
  1224. gunID,
  1225. OP_READ_DATA,
  1226. REG_POWER_CABINET_STATUS,
  1227. 0,
  1228. NULL);
  1229. return ret;
  1230. }
  1231. static int readConnectorID(int fd)
  1232. {
  1233. int ret = PASS;
  1234. ret = composeSocketData(fd,
  1235. ID_REGISTER,
  1236. OP_READ_DATA,
  1237. REG_CONNECTOR_ID,
  1238. 0,
  1239. NULL);
  1240. return ret;
  1241. }
  1242. static int WriteModelName(int fd)
  1243. {
  1244. int ret = PASS;
  1245. uint8_t Tmp = 0;
  1246. uint8_t TmpBuf[255] = {0};
  1247. memcpy(TmpBuf,
  1248. ShmSysConfigAndInfo->SysConfig.ModelName,
  1249. strlen((char *)ShmSysConfigAndInfo->SysConfig.ModelName));
  1250. Tmp = (uint8_t)(ShmPrimaryMcuData->InputDet.bits.Key2 << 2 |
  1251. ShmPrimaryMcuData->InputDet.bits.Key1 << 1 |
  1252. ShmPrimaryMcuData->InputDet.bits.Key0);
  1253. TmpBuf[strlen((char *)ShmSysConfigAndInfo->SysConfig.ModelName)] = Tmp; //Dispenser switch value
  1254. ret = composeSocketData(fd,
  1255. ID_REGISTER,
  1256. OP_WRITE_DATA,
  1257. REG_MODEL_NAME,
  1258. strlen((char *)ShmSysConfigAndInfo->SysConfig.ModelName) + 1,
  1259. &TmpBuf[0]);
  1260. return ret;
  1261. }
  1262. static void calDisconnectCount(uint8_t *pValue, uint8_t maxCount)
  1263. {
  1264. sleep(3); //wait 1 second
  1265. if ((*pValue) >= maxCount) {
  1266. setTcpStatus(ABNORMAL);
  1267. } else {
  1268. (*pValue)++;
  1269. }
  1270. }
  1271. static int CheckNetworkStatus(void)
  1272. {
  1273. char *ipAddr = (char *)&ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthIpAddress;
  1274. //printf("Check network IP Header = %s\n", ipAddr);
  1275. if (strstr(ipAddr, CMP_ETH_IP_HEAD) != NULL) {
  1276. return 1;
  1277. }
  1278. return 0;
  1279. }
  1280. static int FindChargingInfoData(byte target, struct ChargingInfoData **chargingData)
  1281. {
  1282. uint8_t index = 0;
  1283. for (index = 0; index < CHAdeMO_QUANTITY; index++) {
  1284. if (ShmSysConfigAndInfo->SysInfo.ChademoChargingData[index].Index == target) {
  1285. chargingData[target] = &ShmSysConfigAndInfo->SysInfo.ChademoChargingData[index];
  1286. return 1;
  1287. }
  1288. }
  1289. for (index = 0; index < CCS_QUANTITY; index++) {
  1290. if (ShmSysConfigAndInfo->SysInfo.CcsChargingData[index].Index == target) {
  1291. chargingData[target] = &ShmSysConfigAndInfo->SysInfo.CcsChargingData[index];
  1292. return 1;
  1293. }
  1294. }
  1295. for (index = 0; index < GB_QUANTITY; index++) {
  1296. if (ShmSysConfigAndInfo->SysInfo.GbChargingData[index].Index == target) {
  1297. chargingData[target] = &ShmSysConfigAndInfo->SysInfo.GbChargingData[index];
  1298. return 1;
  1299. }
  1300. }
  1301. return 0;
  1302. }
  1303. static int InitShareMemory(void)
  1304. {
  1305. int result = 1;
  1306. int MeterSMId = 0;
  1307. if ((MeterSMId = shmget(ShmSysConfigAndInfoKey, sizeof(struct SysConfigAndInfo), 0777)) < 0) {
  1308. DEBUG_ERROR("shmget ShmSysConfigAndInfo NG\n");
  1309. result = 0;
  1310. } else if ((ShmSysConfigAndInfo = shmat(MeterSMId, NULL, 0)) == (void *) - 1) {
  1311. DEBUG_ERROR("[shmat ShmSysConfigAndInfo NG\n");
  1312. result = 0;
  1313. }
  1314. if ((MeterSMId = shmget(ShmStatusCodeKey, sizeof(struct StatusCodeData), 0777)) < 0) {
  1315. DEBUG_ERROR("shmget ShmStatusCodeData NG\n");
  1316. result = 0;
  1317. } else if ((ShmStatusCodeData = shmat(MeterSMId, NULL, 0)) == (void *) - 1) {
  1318. DEBUG_ERROR("shmat ShmStatusCodeData NG\n");
  1319. result = 0;
  1320. }
  1321. //creat ShmPsuData
  1322. if ((MeterSMId = shmget(ShmPsuKey, sizeof(struct PsuData), 0777)) < 0) {
  1323. DEBUG_ERROR("shmget ShmPsuData NG \n");
  1324. result = 0;
  1325. } else if ((ShmPsuData = shmat(MeterSMId, NULL, 0)) == (void *) - 1) {
  1326. DEBUG_ERROR("shmat ShmPsuData NG \n");
  1327. result = 0;
  1328. }
  1329. //creat ShmOCPP16Data
  1330. if ((MeterSMId = shmget(ShmOcppModuleKey, sizeof(struct OCPP16Data), 0777)) < 0) {
  1331. DEBUG_ERROR("shmget ShmOCPP16Data NG \n");
  1332. result = 0;
  1333. } else if ((ShmOCPP16Data = shmat(MeterSMId, NULL, 0)) == (void *) - 1) {
  1334. DEBUG_ERROR("shmat ShmOCPP16Data NG \n");
  1335. result = 0;
  1336. }
  1337. if ((MeterSMId = shmget(ShmPrimaryMcuKey, sizeof(struct PrimaryMcuData), 0777)) < 0) {
  1338. DEBUG_ERROR("shmget ShmPrimaryMcuData NG\n");
  1339. result = 0;
  1340. } else if ((ShmPrimaryMcuData = shmat(MeterSMId, NULL, 0)) == (void *) - 1) {
  1341. DEBUG_ERROR("shmat ShmPrimaryMcuData NG\n");
  1342. result = 0;
  1343. }
  1344. //creat Audi customization info
  1345. if ((MeterSMId = shmget(ShmAudiCustInfoKey, sizeof(AudiCustInfo), IPC_CREAT | 0777)) < 0) {
  1346. DEBUG_ERROR("[main]CreatShareMemory:shmget select gun info NG \n");
  1347. return 0;
  1348. } else if ((gAudiCustInfo = shmat(MeterSMId, NULL, 0)) == (void *) - 1) {
  1349. DEBUG_ERROR("[main]CreatShareMemory:shmat shmget select gun info \n");
  1350. return 0;
  1351. }
  1352. return result;
  1353. }
  1354. static void updateFirmwareProcess(int fd, uint8_t gunID, uint8_t totalConnCount)
  1355. {
  1356. bool canUpdateFirmware = true;
  1357. uint8_t plugNum = 0;
  1358. uint8_t ackCount = 5;
  1359. struct timeb updateTime;
  1360. if (ShmSysConfigAndInfo->SysWarningInfo.Level != 2) {
  1361. for (plugNum = 0; plugNum < totalConnCount; plugNum++) {
  1362. if (ChargingData[plugNum]->SystemStatus != S_IDLE &&
  1363. ChargingData[plugNum]->SystemStatus != S_RESERVATION &&
  1364. ChargingData[plugNum]->SystemStatus != S_MAINTAIN) {
  1365. canUpdateFirmware = false;
  1366. }
  1367. }
  1368. }
  1369. if (canUpdateFirmware) {
  1370. ftime(&updateTime);
  1371. if (DiffTimeb(gRegTimeUp[0][REG_SOFTWARE_UPDATE], updateTime) > LOOP_RETRY_TIME * 5) {
  1372. readSoftwareUpdate(fd, gunID);
  1373. ftime(&gRegTimeUp[0][REG_SOFTWARE_UPDATE]);
  1374. }
  1375. if (ShmSysConfigAndInfo->SysInfo.FirmwareUpdate == YES) {
  1376. while (ackCount != 0) {
  1377. ftime(&updateTime);
  1378. if (DiffTimeb(gRegTimeUp[0][REG_SOFTWARE_UPDATE], updateTime) > LOOP_RETRY_TIME / 2) {
  1379. readSoftwareUpdate(fd, gunID);
  1380. ftime(&gRegTimeUp[0][REG_SOFTWARE_UPDATE]);
  1381. if (ShmSysConfigAndInfo->SysInfo.FirmwareUpdate == NO) {
  1382. ackCount--;
  1383. }
  1384. }
  1385. usleep(128);
  1386. }
  1387. }
  1388. }
  1389. }
  1390. static void checkAuthorProcess(int fd, uint8_t plugNum)
  1391. {
  1392. int ret = 0;
  1393. uint8_t gunID = 0;
  1394. struct timeb AuthNowTime;
  1395. gunID = gDoCommGblData.ConnectorID[ShmSysConfigAndInfo->SysInfo.CurGunSelected];
  1396. if ((ShmOCPP16Data->SpMsg.bits.AuthorizeReq == YES) || (ShmSysConfigAndInfo->SysInfo.AuthorizeFlag == YES)) {
  1397. ftime(&AuthNowTime);
  1398. if (DiffTimeb(gRegTimeUp[plugNum][REG_USER_ID], AuthNowTime) > LOOP_RETRY_TIME) {
  1399. ret = writeUserID(fd,
  1400. gunID,
  1401. ShmSysConfigAndInfo->SysConfig.UserId);
  1402. if (ret >= 0) {
  1403. memset(ShmOCPP16Data->Authorize.ResponseIdTagInfo.Status,
  1404. 0,
  1405. sizeof(ShmOCPP16Data->Authorize.ResponseIdTagInfo.Status));
  1406. if (ret == 0) {
  1407. strcpy((char *)ShmOCPP16Data->Authorize.ResponseIdTagInfo.Status, "Invalid");
  1408. } else {
  1409. strcpy((char *)ShmOCPP16Data->Authorize.ResponseIdTagInfo.Status, "Accepted");
  1410. }
  1411. //printf("gAudiCustInfo->PricesInfo[plugNum].Balance = %.2f, %.2f\r\n",
  1412. // gAudiCustInfo->PricesInfo[plugNum].Balance,
  1413. // FAIL_BALANCE_PRICES);
  1414. if (gAudiCustInfo->PricesInfo[plugNum].Balance != FAIL_BALANCE_PRICES) {
  1415. ShmOCPP16Data->SpMsg.bits.AuthorizeConf = 1; //isAuthorizedComplete
  1416. ShmOCPP16Data->SpMsg.bits.AuthorizeReq = 0;
  1417. ShmSysConfigAndInfo->SysInfo.AuthorizeFlag = 0;
  1418. ShmPsuData->SystemAvailablePower = 0;
  1419. ShmPsuData->SystemPresentPsuQuantity = 0;
  1420. }
  1421. }
  1422. ftime(&gRegTimeUp[plugNum][REG_USER_ID]);
  1423. }
  1424. }
  1425. }
  1426. static int messageTrigger(int fd, uint8_t plugNum, uint8_t gunID, uint8_t curReg)
  1427. {
  1428. int ret = DISPENSER_INIT_SUCC;
  1429. int isContinue = 1;
  1430. struct timeb NowTime;
  1431. while (isContinue) {
  1432. usleep(128);
  1433. ftime(&NowTime);
  1434. switch (curReg) {
  1435. case REG_MODEL_NAME:
  1436. if (WriteModelName(fd) == PASS) {
  1437. gDoCommGblData.DisConnCount = 0;
  1438. curReg = REG_CONNECTOR_ID;
  1439. } else {
  1440. sleep(1);
  1441. }
  1442. break;
  1443. case REG_CONNECTOR_ID:
  1444. if (readConnectorID(fd) == PASS) {
  1445. gDoCommGblData.DisConnCount = 0;
  1446. curReg = REG_REPORT_CSU_VERSION;
  1447. } else {
  1448. sleep(1);
  1449. }
  1450. break;
  1451. case REG_POWER_CABINET_STATUS:
  1452. if (DiffTimeb(gRegTimeUp[plugNum][curReg], NowTime) > LOOP_RETRY_TIME) {
  1453. readPowerCabinetStatus(fd, gunID);
  1454. ftime(&gRegTimeUp[plugNum][curReg]);
  1455. }
  1456. curReg = REG_DISPENSER_STATUS;
  1457. break;
  1458. case REG_DISPENSER_STATUS:
  1459. if (DiffTimeb(gRegTimeUp[plugNum][curReg], NowTime) > LOOP_RETRY_TIME) {
  1460. writeDispenserStatus(fd, gunID);
  1461. ftime(&gRegTimeUp[plugNum][curReg]);
  1462. }
  1463. curReg = REG_PLUG_IN_STATE;
  1464. break;
  1465. //case REG_CHARGING_CAP:
  1466. // readChargingCapability(fd, plugNum, gunID);
  1467. // break;
  1468. //case REG_CHARGING_TARGET:
  1469. // writeChargingTarget(fd, plugNum, gunID);
  1470. // break;
  1471. //case REG_SOFTWARE_UPDATE:
  1472. // if (ChargingData[plugNum]->SystemStatus == S_IDLE) {
  1473. // if (DiffTimeb(gRegTimeUp[plugNum][curReg], NowTime) > LOOP_RETRY_TIME * 10) {
  1474. // readSoftwareUpdate(fd);
  1475. // ftime(&gRegTimeUp[plugNum][curReg]);
  1476. // }
  1477. // while (ShmSysConfigAndInfo->SysInfo.FirmwareUpdate == YES) {
  1478. // ftime(&NowTime);
  1479. // if (DiffTimeb(gRegTimeUp[plugNum][curReg], NowTime) > LOOP_RETRY_TIME) {
  1480. // readSoftwareUpdate(fd);
  1481. // }
  1482. // usleep(128);
  1483. // }
  1484. // }
  1485. // isContinue = 0;
  1486. // break;
  1487. case REG_PLUG_IN_STATE:
  1488. if (DiffTimeb(gRegTimeUp[plugNum][curReg], NowTime) > LOOP_RETRY_TIME) {
  1489. writePlugInStatus(fd, plugNum, gunID);
  1490. ftime(&gRegTimeUp[plugNum][curReg]);
  1491. }
  1492. curReg = REG_CONNECTOR_STATE;
  1493. break;
  1494. case REG_CONNECTOR_STATE:
  1495. if (DiffTimeb(gRegTimeUp[plugNum][curReg], NowTime) > LOOP_RETRY_TIME) {
  1496. writeConnectorState(fd, plugNum, gunID);
  1497. ftime(&gRegTimeUp[plugNum][curReg]);
  1498. }
  1499. //check misc command from power cabinet
  1500. if (gDoCommGblData.MiscCmd != 0) {
  1501. curReg = gDoCommGblData.MiscCmd;
  1502. } else {
  1503. curReg = REG_QRCODE_URL_INFO;
  1504. }
  1505. break;
  1506. //case REG_USER_ID:
  1507. // writeUserID(fd, gunID, uint8_t *pUserID);
  1508. // break;
  1509. //case REG_CHARGING_PERMISSION:
  1510. // readChargePermission(fd, gunID);
  1511. // break;
  1512. case REG_MISC_CONTROL:
  1513. readMiscCommand(fd, gunID);
  1514. isContinue = 0;
  1515. break;
  1516. case REG_REPORT_CSU_VERSION:
  1517. case REG_REPORT_OTHER_VERSION:
  1518. if (gDoCommGblData.MiscCmd != 0) {
  1519. writeCsuModuleVersion(fd);
  1520. writeOtherModuleVersion(fd);
  1521. clearMiscCommand();
  1522. isContinue = 0;
  1523. } else {
  1524. //power up write dispenser version and get power cabinet system ID
  1525. if ((writeCsuModuleVersion(fd) == PASS) &&
  1526. (writeOtherModuleVersion(fd) == PASS) &&
  1527. (readQRcodeURLAndSystemDate(fd) == PASS)) {
  1528. //gDoCommGblData.DisConnCount = 0;
  1529. isContinue = 0;
  1530. }
  1531. }
  1532. break;
  1533. //case REG_PRESENT_CHARGING_INFO:
  1534. // break;
  1535. case REG_QRCODE_URL_INFO:
  1536. if (gunID == 1) {
  1537. if (DiffTimeb(gRegTimeUp[plugNum][curReg], NowTime) > (LOOP_RETRY_TIME * 10)) {
  1538. readQRcodeURLAndSystemDate(fd);
  1539. ftime(&gRegTimeUp[plugNum][curReg]);
  1540. }
  1541. }
  1542. isContinue = 0;
  1543. //curReg = REG_SOFTWARE_UPDATE;
  1544. break;
  1545. //case REG_WAIT_PLUG_IT_STATE:
  1546. // if (DiffTimeb(gRegTimeUp[plugNum][curReg], NowTime) > (LOOP_RETRY_TIME / 10)) {
  1547. // writeWaitPlugItState(fd, gunID);
  1548. // ftime(&gRegTimeUp[plugNum][curReg]);
  1549. // }
  1550. // isContinue = 0;
  1551. // break;
  1552. default:
  1553. DEBUG_ERROR("error curReg = %x\r\n", curReg);
  1554. gDoCommGblData.MiscCmd = 0;
  1555. gDoCommGblData.DisConnCount = CHECK_NETWORK_FAIL_COUNT;
  1556. isContinue = 0;
  1557. break;
  1558. }
  1559. if (gDoCommGblData.DisConnCount >= CHECK_NETWORK_FAIL_COUNT) {
  1560. return DISPENSER_SOCKET_RECONN;
  1561. }
  1562. }//for
  1563. return ret;
  1564. }
  1565. //static bool isDetectPlugin()
  1566. //{
  1567. // if (ShmSysConfigAndInfo->SysInfo.WaitForPlugit == YES) {
  1568. // return YES;
  1569. // }
  1570. //
  1571. // return NO;
  1572. //}
  1573. static void systemStatusProcess(int fd, uint8_t totalGun, uint8_t plugNum, uint8_t gunID)
  1574. {
  1575. uint8_t i = 0;
  1576. struct timeb AuthNowTime = {0};
  1577. switch (ChargingData[plugNum]->SystemStatus) {
  1578. case S_IDLE:
  1579. case S_RESERVATION:
  1580. case S_MAINTAIN:
  1581. case S_ALARM:
  1582. case S_AUTHORIZING:
  1583. checkAuthorProcess(fd, plugNum);
  1584. //authorization complete, write wait plug it state
  1585. if (gAudiCustInfo->PricesInfo[plugNum].Balance != FAIL_BALANCE_PRICES) {
  1586. ftime(&AuthNowTime);
  1587. if (DiffTimeb(gRegTimeUp[plugNum][REG_WAIT_PLUG_IT_STATE], AuthNowTime) > (LOOP_RETRY_TIME / 10)) {
  1588. writeWaitPlugItState(fd, gunID);
  1589. ftime(&gRegTimeUp[plugNum][REG_WAIT_PLUG_IT_STATE]);
  1590. }
  1591. ftime(&AuthNowTime);
  1592. if (DiffTimeb(gRegTimeUp[plugNum][REG_CHARGING_CAP], AuthNowTime) > LOOP_RETRY_TIME) {
  1593. readChargingCapability(fd, gunID);
  1594. ftime(&gRegTimeUp[plugNum][REG_CHARGING_CAP]);
  1595. }
  1596. }
  1597. break;
  1598. case S_PREPARNING: //get permission
  1599. ftime(&AuthNowTime);
  1600. if (DiffTimeb(gRegTimeUp[plugNum][REG_CONNECTOR_STATE], AuthNowTime) > LOOP_RETRY_TIME) {
  1601. writeConnectorState(fd, plugNum, gunID);
  1602. ftime(&gRegTimeUp[plugNum][REG_CONNECTOR_STATE]);
  1603. }
  1604. ftime(&AuthNowTime);
  1605. if (DiffTimeb(gRegTimeUp[plugNum][REG_CHARGING_PERMISSION], AuthNowTime) > LOOP_RETRY_TIME) {
  1606. if (readChargePermission(fd, gunID) && readChargingCapability(fd, gunID)) {
  1607. for (i = 0; i < totalGun; i++) {
  1608. ShmPsuData->SystemAvailablePower += ChargingData[i]->AvailableChargingPower;
  1609. }
  1610. ShmPsuData->SystemPresentPsuQuantity = (ShmPsuData->SystemAvailablePower / 30);
  1611. }
  1612. ftime(&gRegTimeUp[plugNum][REG_CHARGING_PERMISSION]);
  1613. }
  1614. ftime(&AuthNowTime);
  1615. if (DiffTimeb(gRegTimeUp[plugNum][REG_PRESENT_CHARGING_INFO], AuthNowTime) > LOOP_RETRY_TIME) {
  1616. writePresentChargingInfo(fd, plugNum, gunID);
  1617. ftime(&gRegTimeUp[plugNum][REG_PRESENT_CHARGING_INFO]);
  1618. }
  1619. break;
  1620. case S_PREPARING_FOR_EV://wait connector lock
  1621. ftime(&AuthNowTime);
  1622. if (DiffTimeb(gRegTimeUp[plugNum][REG_CHARGING_CAP], AuthNowTime) > LOOP_RETRY_TIME) {
  1623. readChargingCapability(fd, gunID);
  1624. ftime(&gRegTimeUp[plugNum][REG_CHARGING_CAP]);
  1625. }
  1626. ftime(&AuthNowTime);
  1627. if (DiffTimeb(gRegTimeUp[plugNum][REG_CHARGING_PERMISSION], AuthNowTime) > LOOP_RETRY_TIME) {
  1628. if (readChargePermission(fd, gunID) == 0) {
  1629. DEBUG_INFO("Stop charging by power cabinet's permission = %d, %d\r\n",
  1630. plugNum,
  1631. REG_CHARGING_PERMISSION);
  1632. ChargingData[plugNum]->StopChargeFlag = 1;
  1633. }
  1634. ftime(&gRegTimeUp[plugNum][REG_CHARGING_PERMISSION]);
  1635. }
  1636. ftime(&AuthNowTime);
  1637. if (DiffTimeb(gRegTimeUp[plugNum][REG_PRESENT_CHARGING_INFO], AuthNowTime) > LOOP_RETRY_TIME) {
  1638. writePresentChargingInfo(fd, plugNum, gunID);
  1639. ftime(&gRegTimeUp[plugNum][REG_PRESENT_CHARGING_INFO]);
  1640. }
  1641. break;
  1642. case S_PREPARING_FOR_EVSE: //insaulation test
  1643. case S_CCS_PRECHARGE_ST0:
  1644. case S_CCS_PRECHARGE_ST1:
  1645. writeChargingTarget(fd, plugNum, gunID);
  1646. ftime(&AuthNowTime);
  1647. if (DiffTimeb(gRegTimeUp[plugNum][REG_CHARGING_CAP], AuthNowTime) > LOOP_RETRY_TIME) {
  1648. readChargingCapability(fd, gunID);
  1649. ftime(&gRegTimeUp[plugNum][REG_CHARGING_CAP]);
  1650. }
  1651. ftime(&AuthNowTime);
  1652. if (DiffTimeb(gRegTimeUp[plugNum][REG_CHARGING_PERMISSION], AuthNowTime) > LOOP_RETRY_TIME) {
  1653. if (readChargePermission(fd, gunID) == 0) {
  1654. DEBUG_INFO("Stop charging by power cabinet's permission = %d, %d\r\n",
  1655. plugNum,
  1656. REG_CHARGING_PERMISSION);
  1657. ChargingData[plugNum]->StopChargeFlag = 1;
  1658. }
  1659. ftime(&gRegTimeUp[plugNum][REG_CHARGING_PERMISSION]);
  1660. }
  1661. ftime(&AuthNowTime);
  1662. if (DiffTimeb(gRegTimeUp[plugNum][REG_PRESENT_CHARGING_INFO], AuthNowTime) > LOOP_RETRY_TIME) {
  1663. writePresentChargingInfo(fd, plugNum, gunID);
  1664. ftime(&gRegTimeUp[plugNum][REG_PRESENT_CHARGING_INFO]);
  1665. }
  1666. break;
  1667. case S_CHARGING: //charging
  1668. case S_TERMINATING:
  1669. ftime(&AuthNowTime);
  1670. if (DiffTimeb(gRegTimeUp[plugNum][REG_CHARGING_CAP], AuthNowTime) > LOOP_RETRY_TIME) {
  1671. readChargingCapability(fd, gunID);
  1672. ftime(&gRegTimeUp[plugNum][REG_CHARGING_CAP]);
  1673. }
  1674. ftime(&AuthNowTime);
  1675. if (DiffTimeb(gRegTimeUp[plugNum][REG_CHARGING_PERMISSION], AuthNowTime) > LOOP_RETRY_TIME) {
  1676. if (readChargePermission(fd, gunID) == 0) {
  1677. DEBUG_INFO("Stop charging by power cabinet's permission = %d, %d\r\n",
  1678. plugNum,
  1679. REG_CHARGING_PERMISSION);
  1680. ChargingData[plugNum]->StopChargeFlag = 1;
  1681. }
  1682. ftime(&gRegTimeUp[plugNum][REG_CHARGING_PERMISSION]);
  1683. }
  1684. writeChargingTarget(fd, plugNum, gunID);
  1685. ftime(&AuthNowTime);
  1686. if (DiffTimeb(gRegTimeUp[plugNum][REG_PRESENT_CHARGING_INFO], AuthNowTime) > LOOP_RETRY_TIME) {
  1687. writePresentChargingInfo(fd, plugNum, gunID);
  1688. ftime(&gRegTimeUp[plugNum][REG_PRESENT_CHARGING_INFO]);
  1689. }
  1690. break;
  1691. case S_COMPLETE:
  1692. ftime(&AuthNowTime);
  1693. if (DiffTimeb(gRegTimeUp[plugNum][REG_CHARGING_CAP], AuthNowTime) > LOOP_RETRY_TIME) {
  1694. readChargingCapability(fd, gunID);
  1695. ftime(&gRegTimeUp[plugNum][REG_CHARGING_CAP]);
  1696. }
  1697. break;
  1698. default:
  1699. break;
  1700. }
  1701. }
  1702. static int networkCreatePorcess(void)
  1703. {
  1704. int fd = 0;
  1705. /**************** Check Network **********/
  1706. DEBUG_INFO("Check Dispenser Network Information");
  1707. while (!CheckNetworkStatus()) {
  1708. calDisconnectCount(&gDoCommGblData.DisConnCount, CHECK_NETWORK_FAIL_COUNT);
  1709. DEBUG_ERROR("disconnect count = % d\r\n", gDoCommGblData.DisConnCount);
  1710. }
  1711. DEBUG_INFO("Dispenser Network Information checked: IP = % s Netmask = % s, Gateway = % s",
  1712. ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthIpAddress,
  1713. ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthSubmaskAddress,
  1714. ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthGatewayAddress);
  1715. gDoCommGblData.DisConnCount = 0;
  1716. /**************** Power cabinet connection **********/
  1717. DEBUG_INFO("Connect to Power Cabinet");
  1718. while ((fd = doCommConnToServer()) <= 0) {
  1719. calDisconnectCount(&gDoCommGblData.DisConnCount, CONNECT_SERVER_FAIL_COUNT);
  1720. DEBUG_ERROR("disconnect count = %d, fd = %d\r\n", gDoCommGblData.DisConnCount, fd);
  1721. }
  1722. DEBUG_INFO("Power cabinet connected(fd = % d): IP = % s Netmask = % s, Gateway = % s",
  1723. fd,
  1724. ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthIpAddress,
  1725. ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthSubmaskAddress,
  1726. ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthGatewayAddress);
  1727. gDoCommGblData.DisConnCount = 0;
  1728. destroySelectGun(DESTROY_ALL_SEL);
  1729. sleep(3);
  1730. setTcpStatus(NORMAL);
  1731. return fd;
  1732. }
  1733. int main(int argc, char *argv[])
  1734. {
  1735. uint8_t plugNum = 0, gunID = 0;
  1736. uint8_t index = 0;
  1737. uint8_t totalConnCount = 0;
  1738. uint8_t initDone = DISPENER_INIT_FAIL;
  1739. int fd = 0;
  1740. int isContinue = 1;
  1741. InitSocketSigPipe();
  1742. /**************** Initialization **********/
  1743. if (InitShareMemory() == 0) {
  1744. if (ShmStatusCodeData != NULL) {
  1745. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.FailToCreateShareMemory = YES;
  1746. }
  1747. DEBUG_ERROR("DoComm Initial share memory fail\r\n");
  1748. sleep(5);
  1749. return 0;
  1750. }
  1751. totalConnCount = ShmSysConfigAndInfo->SysConfig.TotalConnectorCount;
  1752. for (index = 0; index < totalConnCount; index++) {
  1753. if (!FindChargingInfoData(index, &ChargingData[0])) {
  1754. DEBUG_ERROR("FindChargingInfoData false \n");
  1755. break;
  1756. }
  1757. clearPricesInfo(index);
  1758. }
  1759. //initial trigger message timer
  1760. for (index = 0; index < MAX_REGISTER_NUM; index++) {
  1761. ftime(&gRegTimeUp[0][index]);
  1762. ftime(&gRegTimeUp[1][index]);
  1763. usleep(50000);
  1764. }
  1765. setTcpStatus(NORMAL);
  1766. while (isContinue) {
  1767. if (initDone == DISPENER_INIT_FAIL) {
  1768. fd = networkCreatePorcess();
  1769. initDone = messageTrigger(fd, 0, 0, REG_MODEL_NAME); //first trigger model name and connector id
  1770. } else {
  1771. checkAuthorProcess(fd, 0);
  1772. plugNum = 0;
  1773. gunID = 0;
  1774. for (plugNum = 0; plugNum < totalConnCount; plugNum++) {
  1775. //plugNum = setup chargingData value for bottom layer
  1776. //gunID = (plugNum + 1); //connector Id, 1 = left gun, 2 = right gun
  1777. gunID = gDoCommGblData.ConnectorID[plugNum];
  1778. systemStatusProcess(fd, totalConnCount, plugNum, gunID);
  1779. initDone = messageTrigger(fd,
  1780. plugNum,
  1781. gunID,
  1782. REG_POWER_CABINET_STATUS);
  1783. if (initDone == DISPENSER_SOCKET_RECONN) {
  1784. break;
  1785. }
  1786. }
  1787. }
  1788. if (initDone == DISPENSER_SOCKET_RECONN) {
  1789. DEBUG_INFO("Disconnected from power cabinet...re-connecting");
  1790. setTcpStatus(ABNORMAL);
  1791. gDoCommGblData.DisConnCount = 0;
  1792. gDoCommGblData.SeqNum = 0;
  1793. closeSocket(fd);
  1794. fd = 0;
  1795. initDone = DISPENER_INIT_FAIL;
  1796. }
  1797. //update dispenser firmware
  1798. if (initDone != DISPENER_INIT_FAIL) {
  1799. updateFirmwareProcess(fd, gDoCommGblData.ConnectorID[0], totalConnCount);
  1800. }
  1801. usleep(1000);
  1802. }
  1803. }