DoComm.c 74 KB

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