ReDoComm.c 69 KB

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