DoComm.c 75 KB

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