ReDoComm.c 69 KB

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