ReDoComm.c 70 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160
  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. case MISC_CMD_BACKEND_STATUS :
  526. gAudiCustInfo->EthDevStatus.Backend = value;
  527. break;
  528. case MISC_CMD_ETHERNET_STATUS :
  529. gAudiCustInfo->EthDevStatus.Ethernet = value;
  530. break;
  531. case MISC_CMD_WIFI_STATUS :
  532. gAudiCustInfo->EthDevStatus.Wifi = value;
  533. break;
  534. case MISC_CMD_4G_STATUS :
  535. gAudiCustInfo->EthDevStatus.FourG = value;
  536. break;
  537. //--- Control Dispenser ---
  538. case MISC_CMD_HARDWARE_REBOOT:
  539. if (value != YES) {
  540. clearMiscCommand();
  541. break;
  542. }
  543. DEBUG_INFO("Hardware reboot\r\n");
  544. ShmOCPP16Data->MsMsg.bits.ResetReq = YES;
  545. strcpy((char *)ShmOCPP16Data->Reset.Type, "Hard");
  546. clearMiscCommand();
  547. break;
  548. case MISC_CMD_SOFTWARE_RESTART:
  549. if (value != YES) {
  550. clearMiscCommand();
  551. break;
  552. }
  553. clearMiscCommand();
  554. DEBUG_INFO("Software reboot\r\n");
  555. ShmOCPP16Data->MsMsg.bits.ResetReq = YES;
  556. strcpy((char *)ShmOCPP16Data->Reset.Type, "Soft");
  557. break;
  558. case MISC_CMD_REMOTE_START_CHARGING:
  559. if (value != YES) {
  560. if (id == 2) {
  561. clearMiscCommand();
  562. }
  563. break;
  564. }
  565. DEBUG_INFO("Remote start charging id = %d\r\n", id);
  566. //if (getConfirmSelectedGun(id - 1) == FAIL) {
  567. // DEBUG_INFO("Remote start not select gun\r\n");
  568. // break;
  569. //}
  570. #if defined DD360Audi
  571. if (getSelGunWaitToAuthor(id - 1) == FAIL) {
  572. DEBUG_ERROR("Remote start gun already charging\r\n");
  573. break;
  574. }
  575. if (ShmSysConfigAndInfo->SysInfo.CurGunSelected != (id - 1)) {
  576. if (ShmSysConfigAndInfo->SysInfo.CurGunSelected == LEFT_GUN_NUM &&
  577. (gAudiCustInfo->SelGunInfo.LeftGun == SEL_GUN_CONFIRM ||
  578. gAudiCustInfo->SelGunInfo.LeftGun == SEL_GUN_ATHOR)) {
  579. gAudiCustInfo->SelGunInfo.LeftGun = SEL_GUN_RELEASE;
  580. strcpy((char *)ShmSysConfigAndInfo->SysConfig.UserId, "");
  581. ShmSysConfigAndInfo->SysInfo.WaitForPlugit = NO;
  582. sleep(1); //Jerry add
  583. ShmSysConfigAndInfo->SysInfo.SystemPage = _LCM_SELECT_GUN;
  584. gettimeofday(&ShmSysConfigAndInfo->SysInfo.SystemTimeoutTimer, NULL);
  585. ShmSysConfigAndInfo->SysInfo.SystemTimeoutFlag = Timeout_None;
  586. }
  587. if (ShmSysConfigAndInfo->SysInfo.CurGunSelected == RIGHT_GUN_NUM &&
  588. (gAudiCustInfo->SelGunInfo.RightGun == SEL_GUN_CONFIRM ||
  589. gAudiCustInfo->SelGunInfo.RightGun == SEL_GUN_ATHOR)) {
  590. gAudiCustInfo->SelGunInfo.RightGun = SEL_GUN_RELEASE;
  591. strcpy((char *)ShmSysConfigAndInfo->SysConfig.UserId, "");
  592. ShmSysConfigAndInfo->SysInfo.WaitForPlugit = NO;
  593. sleep(1); //Jerry add
  594. ShmSysConfigAndInfo->SysInfo.SystemPage = _LCM_SELECT_GUN;
  595. gettimeofday(&ShmSysConfigAndInfo->SysInfo.SystemTimeoutTimer, NULL);
  596. ShmSysConfigAndInfo->SysInfo.SystemTimeoutFlag = Timeout_None;
  597. }
  598. ShmSysConfigAndInfo->SysInfo.CurGunSelected = (id - 1);
  599. ShmSysConfigAndInfo->SysInfo.CurGunSelectedByAc = NO_DEFINE;
  600. }
  601. setConfirmSelGun(ShmSysConfigAndInfo->SysInfo.CurGunSelected);
  602. #else
  603. ShmSysConfigAndInfo->SysInfo.CurGunSelected = (id - 1);
  604. ShmSysConfigAndInfo->SysInfo.CurGunSelectedByAc = NO_DEFINE;
  605. #endif //defined DD360Audi
  606. ShmOCPP16Data->CsMsg.bits[id - 1].RemoteStartTransactionReq = YES;
  607. gAudiCustInfo->PricesInfo[id - 1].Balance = 0.00;
  608. clearMiscCommand();
  609. break;
  610. case MISC_CMD_REMOTE_STOP_CHARGING:
  611. if (value != YES) {
  612. if (id == 2) {
  613. clearMiscCommand();
  614. }
  615. break;
  616. }
  617. strcpy((char *)ShmSysConfigAndInfo->SysConfig.UserId, "");
  618. ShmSysConfigAndInfo->SysInfo.WaitForPlugit = NO;
  619. ShmSysConfigAndInfo->SysInfo.SystemPage = _LCM_SELECT_GUN;
  620. gettimeofday(&ShmSysConfigAndInfo->SysInfo.SystemTimeoutTimer, NULL);
  621. ShmSysConfigAndInfo->SysInfo.SystemTimeoutFlag = Timeout_None;
  622. destroySelectGun(id - 1);
  623. clearMiscCommand();
  624. break;
  625. case MISC_CMD_REMOTE_UNLOCK:
  626. if (value != YES) {
  627. if (id == 2) {
  628. clearMiscCommand();
  629. }
  630. break;
  631. }
  632. if (isDetectPlugin() == YES) {
  633. ClearDetectPluginFlag();
  634. strcpy((char *)ShmSysConfigAndInfo->SysConfig.UserId, "");
  635. ShmSysConfigAndInfo->SysInfo.WaitForPlugit = NO;
  636. ShmSysConfigAndInfo->SysInfo.SystemPage = _LCM_SELECT_GUN;
  637. gettimeofday(&ShmSysConfigAndInfo->SysInfo.SystemTimeoutTimer, NULL);
  638. ShmSysConfigAndInfo->SysInfo.SystemTimeoutFlag = Timeout_None;
  639. destroySelectGun(id - 1);
  640. } else {
  641. ChargingData[id - 1]->StopChargeFlag = YES;
  642. }
  643. break;
  644. default:
  645. clearMiscCommand();
  646. break;
  647. }
  648. usleep(128);
  649. }
  650. return ret;
  651. }
  652. static int chargingcapabilityHandle(uint8_t *data, uint8_t gunID)
  653. {
  654. uint8_t addr = 0;
  655. float MaxVolt, MaxCurrent, MaxPower;
  656. static PricesInfo pricesInfo[2] = {0};
  657. CapabilityInfo *pCapabilityInfo = NULL;
  658. AccountInfo *pAccountInfo = NULL;
  659. //--- charging capability information --------------------------------------
  660. pCapabilityInfo = (CapabilityInfo *)&data[0];
  661. MaxVolt = (float)(ntohs(pCapabilityInfo->OutputVoltage));
  662. if (MaxVolt >= 0) {
  663. if (MaxVolt > MAX_VOLTAGE) {
  664. MaxVolt = MAX_VOLTAGE;
  665. }
  666. ChargingData[gunID]->MaximumChargingVoltage = MaxVolt;
  667. }
  668. MaxCurrent = (float)(ntohs(pCapabilityInfo->OutputCurrent));
  669. if (MaxCurrent >= 0) {
  670. if (MaxCurrent > MAX_CURRENCY) {
  671. MaxCurrent = MAX_CURRENCY;
  672. }
  673. ChargingData[gunID]->AvailableChargingCurrent = MaxCurrent;
  674. }
  675. MaxPower = (float)(ntohs(pCapabilityInfo->OutputPower));
  676. if (MaxPower >= 0) {
  677. if (MaxPower > MAX_POWER) {
  678. MaxPower = MAX_POWER;
  679. }
  680. ChargingData[gunID]->AvailableChargingPower = MaxPower;
  681. }
  682. //MaxVolt = (float)(data[0] << 8 |data[1]);
  683. //MaxCurrent = (float)(data[2] << 8 | data[3]);
  684. //MaxPower = (float)(data[4] << 8 | data[5]);
  685. //printf("MaxVolt=%f, MaxCurrent=%f, MaxPower=%f,\n", MaxVolt, MaxCurrent, MaxPower);
  686. //--- user prices information ----------------------------------------------
  687. addr = (sizeof(CapabilityInfo) - 2); //2 byte reserved
  688. pAccountInfo = (AccountInfo *)&data[addr];
  689. ShmSysConfigAndInfo->SysConfig.BillingData.Currency = pAccountInfo->Currency;
  690. gAudiCustInfo->PricesInfo[gunID].UserPrices = transPricesUnit(ntohl(pAccountInfo->UserPrices));
  691. ChargingData[gunID]->ChargingFee = transPricesUnit(ntohl(pAccountInfo->TotalCost));
  692. gAudiCustInfo->PricesInfo[gunID].Balance = transPricesUnit(ntohl(pAccountInfo->Balance));
  693. if ((pricesInfo[gunID].UserPrices != gAudiCustInfo->PricesInfo[gunID].UserPrices) ||
  694. (pricesInfo[gunID].Balance != gAudiCustInfo->PricesInfo[gunID].Balance)) {
  695. pricesInfo[gunID].UserPrices = gAudiCustInfo->PricesInfo[gunID].UserPrices;
  696. pricesInfo[gunID].Balance = gAudiCustInfo->PricesInfo[gunID].Balance;
  697. DEBUG_INFO("id = %d, user prices = %.2f, Total cost = %.2f, Account balances = %.2f, currency = %s\r\n",
  698. gunID,
  699. gAudiCustInfo->PricesInfo[gunID].UserPrices,
  700. ChargingData[gunID]->ChargingFee,
  701. gAudiCustInfo->PricesInfo[gunID].Balance,
  702. (uint8_t *)Currency[ShmSysConfigAndInfo->SysConfig.BillingData.Currency]);
  703. }
  704. return PASS;
  705. }
  706. static void addFaultCodeToBuf(uint8_t *Code)
  707. {
  708. uint8_t warningCount = ShmSysConfigAndInfo->SysWarningInfo.WarningCount;
  709. if (warningCount < 10) {
  710. memcpy(&ShmSysConfigAndInfo->SysWarningInfo.WarningCode[warningCount][0],
  711. Code,
  712. strlen((char *)Code));
  713. ShmSysConfigAndInfo->SysWarningInfo.WarningCount++;
  714. }
  715. }
  716. static void removeFaultCodeToBuf(uint8_t *Code)
  717. {
  718. uint8_t find = 0x01;
  719. uint8_t i = 0;
  720. char _code[7] = {0};
  721. sprintf(_code, "%s", Code);
  722. // 把相關的錯誤碼一次移除,避免重複顯示
  723. while (find) {
  724. usleep(128);
  725. find = 0x00;
  726. for (i = 0; i < ShmSysConfigAndInfo->SysWarningInfo.WarningCount; i++) {
  727. usleep(128);
  728. if (find == 0x00) {
  729. if (memcmp(&ShmSysConfigAndInfo->SysWarningInfo.WarningCode[i][0], _code, 7) == 0) {
  730. find = 0x01;
  731. }
  732. } else {
  733. memcpy(&ShmSysConfigAndInfo->SysWarningInfo.WarningCode[i - 1][0],
  734. &ShmSysConfigAndInfo->SysWarningInfo.WarningCode[i][0], 7);
  735. }
  736. }
  737. if (find) {
  738. ShmSysConfigAndInfo->SysWarningInfo.WarningCount--;
  739. }
  740. }
  741. }
  742. bool CompareArrayIsZero(uint8_t *array, unsigned int array_size)
  743. {
  744. uint8_t i;
  745. if (array != NULL) {
  746. for (i = 0; i < array_size; i++) {
  747. if (array[i] != 0) {
  748. return false;
  749. }
  750. }
  751. }
  752. return true;
  753. }
  754. static int powerCabinetStatusProcess(uint8_t dataLen, uint8_t *data)
  755. {
  756. uint8_t ret = 0;
  757. uint8_t i = 0;
  758. uint8_t count = 0;
  759. uint8_t EventCodeTmp[7] = {0};
  760. uint8_t StatusArray[MAX_REGISTER_NUM][WARNING_CODE_SIZE] = {0};
  761. uint8_t remaindLen = dataLen % dataLen;
  762. uint8_t statusCodeError = 0;
  763. if (remaindLen != 0) {
  764. dataLen -= remaindLen;
  765. }
  766. if (dataLen < WARNING_CODE_SIZE) {
  767. DEBUG_ERROR("fail status code length = %d\r\n", dataLen);
  768. Hexdump(data, dataLen);
  769. if (ShmSysConfigAndInfo->SysWarningInfo.WarningCount > 0) {
  770. for (i = 0; i < ShmSysConfigAndInfo->SysWarningInfo.WarningCount; i++) {
  771. usleep(128);
  772. if (ShmSysConfigAndInfo->SysWarningInfo.WarningCode[i][1] >= '3' || //from backend or power cabinet
  773. (ShmSysConfigAndInfo->SysWarningInfo.WarningCode[i][0] >= 'B' &&
  774. ShmSysConfigAndInfo->SysWarningInfo.WarningCode[i][1] >= '4')) {
  775. memset(EventCodeTmp, 0, sizeof(EventCodeTmp));
  776. memcpy(EventCodeTmp,
  777. ShmSysConfigAndInfo->SysWarningInfo.WarningCode[i],
  778. sizeof(EventCodeTmp));
  779. removeFaultCodeToBuf(EventCodeTmp);
  780. }
  781. }
  782. }
  783. return FAIL;
  784. }
  785. for (count = 0; count < dataLen; count += WARNING_CODE_SIZE) {
  786. usleep(128);
  787. // misc command status code handle
  788. if (strncmp((char *)&data[count], MISC_ST_MISC_CMD, WARNING_CODE_SIZE) == 0) {
  789. //if (gDoCommGblData.MiscCmd != REG_MISC_CONTROL) {
  790. gDoCommGblData.MiscCmd = REG_MISC_CONTROL;
  791. //}
  792. memset((char *)&data[count], 0, WARNING_CODE_SIZE);
  793. continue;
  794. } else if (strncmp((char *)&data[count], MISC_ST_VERSION, WARNING_CODE_SIZE) == 0) {
  795. //if (gDoCommGblData.MiscCmd != REG_REPORT_CSU_VERSION) {
  796. gDoCommGblData.MiscCmd = REG_REPORT_CSU_VERSION;
  797. //}
  798. memset((char *)&data[count], 0, WARNING_CODE_SIZE);
  799. continue;
  800. }
  801. if (CompareArrayIsZero((uint8_t *)&data[count], WARNING_CODE_SIZE) == true) {
  802. memset(EventCodeTmp, 0, sizeof(EventCodeTmp));
  803. memcpy(EventCodeTmp, (char *)&data[count], sizeof(EventCodeTmp));
  804. DEBUG_ERROR("error status = %s\r\n", EventCodeTmp);
  805. continue;
  806. }
  807. strncpy((char *)&StatusArray[count / WARNING_CODE_SIZE], (char *)&data[count], WARNING_CODE_SIZE);
  808. ret = 0;
  809. for (i = 0; i < ShmSysConfigAndInfo->SysWarningInfo.WarningCount; i++) {
  810. usleep(128);
  811. if (memcmp(&ShmSysConfigAndInfo->SysWarningInfo.WarningCode[i][0],
  812. StatusArray[count / WARNING_CODE_SIZE],
  813. WARNING_CODE_SIZE) == 0) {
  814. ret = 1;
  815. break;
  816. }
  817. }
  818. if (ret == 0) {
  819. addFaultCodeToBuf(StatusArray[count / WARNING_CODE_SIZE]);
  820. }
  821. //Rtn=StatusCodeProcessing(StatusArray[count/6]);
  822. }
  823. for (i = 0; i < ShmSysConfigAndInfo->SysWarningInfo.WarningCount; i++) {
  824. usleep(128);
  825. //if (ShmSysConfigAndInfo->SysWarningInfo.WarningCode[i][0] >= 'B' &&
  826. // ShmSysConfigAndInfo->SysWarningInfo.WarningCode[i][1] >= '4') {
  827. // memset(EventCodeTmp, 0, sizeof(EventCodeTmp));
  828. // memcpy(EventCodeTmp,
  829. // ShmSysConfigAndInfo->SysWarningInfo.WarningCode[i],
  830. // sizeof(EventCodeTmp));
  831. // removeFaultCodeToBuf(EventCodeTmp);
  832. //}
  833. if (ShmSysConfigAndInfo->SysWarningInfo.WarningCode[i][1] >= '3' || //from backend or power cabinet 0x33
  834. (ShmSysConfigAndInfo->SysWarningInfo.WarningCode[i][0] >= 'B' &&
  835. ShmSysConfigAndInfo->SysWarningInfo.WarningCode[i][1] >= '4')) {
  836. ret = 0;
  837. for (count = 0; count < (dataLen / WARNING_CODE_SIZE); count++) {
  838. usleep(128);
  839. if (memcmp(&ShmSysConfigAndInfo->SysWarningInfo.WarningCode[i][0],
  840. StatusArray[count],
  841. WARNING_CODE_SIZE) == 0) {
  842. ret = 1;
  843. break;
  844. }
  845. }
  846. if (ret == 0) {
  847. memset(EventCodeTmp, 0, sizeof(EventCodeTmp));
  848. memcpy(EventCodeTmp,
  849. ShmSysConfigAndInfo->SysWarningInfo.WarningCode[i],
  850. sizeof(EventCodeTmp));
  851. removeFaultCodeToBuf(EventCodeTmp);
  852. }
  853. }
  854. }
  855. return PASS;
  856. }
  857. static int responsePackeHandle(int fd, uint8_t *pResult, uint8_t id, uint8_t reg)
  858. {
  859. int ret = PASS;
  860. uint8_t rawDataLen = 0;
  861. CsuResultPkt *pCsuResult = (CsuResultPkt *)pResult;
  862. SoftwareUpdInfo *pSoftwareUpd = NULL;
  863. //Hexdump((uint8_t *)pCsuResult, sizeof(CmdHead) + pCsuResult->Head.DataLen);
  864. if (compareOpcode(pCsuResult->Head.OP) == FAIL ||
  865. compareResult(pCsuResult->Data.Result) == FAIL ||
  866. compareRegister(pCsuResult->Data.Register, reg) == FAIL) {
  867. if (reg != REG_CHARGING_PERMISSION) {
  868. return FAIL;
  869. }
  870. }
  871. rawDataLen = (pCsuResult->Head.DataLen - 2); //2 byte = register and result byte
  872. switch (pCsuResult->Data.Register) {
  873. case REG_MODEL_NAME:
  874. break;
  875. case REG_CONNECTOR_ID:
  876. gDoCommGblData.ConnectorID[0] = pCsuResult->Data.Data[0];
  877. gDoCommGblData.ConnectorID[1] = pCsuResult->Data.Data[1];
  878. DEBUG_INFO("OK (Left connector ID = %d, Right connector ID = %d)\n",
  879. pCsuResult->Data.Data[0],
  880. pCsuResult->Data.Data[1]);
  881. break;
  882. case REG_POWER_CABINET_STATUS:
  883. ret = powerCabinetStatusProcess(rawDataLen, pCsuResult->Data.Data);
  884. break;
  885. case REG_DISPENSER_STATUS:
  886. memset(&gPreSysWarningInfo, 0, sizeof(struct WARNING_CODE_INFO));
  887. memcpy(&gPreSysWarningInfo,
  888. &(ShmSysConfigAndInfo->SysWarningInfo),
  889. sizeof(struct WARNING_CODE_INFO));
  890. break;
  891. case REG_CHARGING_CAP:
  892. chargingcapabilityHandle(pCsuResult->Data.Data, id - 1);
  893. break;
  894. case REG_CHARGING_TARGET:
  895. break;
  896. case REG_SOFTWARE_UPDATE:
  897. pSoftwareUpd = (SoftwareUpdInfo *)&pCsuResult->Data.Data[0];
  898. if ((strcmp((char *)pSoftwareUpd->ImgName, "") == 0) ||
  899. (rawDataLen == 0) ||
  900. pSoftwareUpd->UpdateState != 1) {
  901. ret = FAIL;
  902. break;
  903. }
  904. ret = FAIL;
  905. ret = updateFirmwareHandle(pSoftwareUpd->ImgName);
  906. break;
  907. case REG_PLUG_IN_STATE:
  908. break;
  909. case REG_CONNECTOR_STATE:
  910. break;
  911. case REG_USER_ID:
  912. //DEBUG_INFO("%s\n", pCsuResult->Data.Data[0] == 1 ? "Accept" : "Reject" );
  913. //if (pCsuResult->Data.Data[0] <= 0) {
  914. // return FAIL;
  915. //}
  916. if (pCsuResult->Data.Result == COMMAND_RESULT_NG) {
  917. return COMMAND_RESULT_NG;
  918. }
  919. return pCsuResult->Data.Data[0];
  920. break;
  921. case REG_CHARGING_PERMISSION:
  922. //DEBUG_INFO("id = %d, result = %s, action = %s\r\n",
  923. // pCsuResult->Head.ID,
  924. // pCsuResult->Data.Result == 1 ? "OK" : "NG",
  925. // pCsuResult->Data.Data[0] == 1 ? "Permitted" : "NOT permitted");
  926. if (pCsuResult->Data.Result == COMMAND_RESULT_NG) {
  927. return COMMAND_RESULT_NG;
  928. }
  929. return pCsuResult->Data.Data[0];
  930. break;
  931. case REG_MISC_CONTROL:
  932. miscCommandHandle(rawDataLen, pCsuResult->Head.ID, pCsuResult->Data.Data);
  933. break;
  934. case REG_REPORT_CSU_VERSION:
  935. case REG_REPORT_OTHER_VERSION:
  936. break;
  937. case REG_PRESENT_CHARGING_INFO:
  938. break;
  939. case REG_QRCODE_URL_INFO:
  940. ret = qrCodeUrlInfoHandle(pCsuResult->Data.Data);
  941. break;
  942. case REG_WAIT_PLUG_IT_STATE:
  943. break;
  944. default:
  945. break;
  946. }
  947. return ret;
  948. }
  949. static int composeSocketData(int fd,
  950. uint8_t id,
  951. uint8_t opCode,
  952. uint8_t reg,
  953. uint8_t dataLen,
  954. uint8_t *data)
  955. {
  956. int ret = PASS;
  957. int size = 0;
  958. int sendPktLen = 0;
  959. CsuCmdPkt csuCmdPkt = {0};
  960. CsuResultPkt csuResult = {0};
  961. csuCmdPkt.Head.SeqNum = gDoCommGblData.SeqNum++;
  962. csuCmdPkt.Head.ID = id;
  963. csuCmdPkt.Head.OP = opCode;
  964. csuCmdPkt.Head.DataLen = dataLen + 1; //+1 for register byte
  965. csuCmdPkt.Data.Register = reg;
  966. if ((data != NULL) && (dataLen > 0)) {
  967. memcpy(csuCmdPkt.Data.Data, data, dataLen);
  968. }
  969. sendPktLen = csuCmdPkt.Head.DataLen + sizeof(csuCmdPkt.Head);
  970. //Hexdump((uint8_t *)&csuCmdPkt, sendPktLen);
  971. //send command packet
  972. if ((size = sendTcpSocket(fd, (uint8_t *)&csuCmdPkt, sendPktLen)) < 0) {
  973. DEBUG_ERROR("TCP socket send packet fail = %d\r\n", size);
  974. gDoCommGblData.DisConnCount++;
  975. return FAIL;
  976. }
  977. //receive result head
  978. if ((size = recvTcpSocket(fd, (uint8_t *)&csuResult.Head, sizeof(csuResult.Head))) < 0) {
  979. DEBUG_ERROR("TCP socket RX head fail = %d\r\n", size);
  980. gDoCommGblData.DisConnCount++;
  981. return FAIL;
  982. }
  983. //receive result raw data
  984. if ((size = recvTcpSocket(fd, (uint8_t *)&csuResult.Data, csuResult.Head.DataLen)) < 0) {
  985. DEBUG_ERROR("TCP socket RX data fail = %d\r\n", size);
  986. gDoCommGblData.DisConnCount++;
  987. return FAIL;
  988. }
  989. ret = responsePackeHandle(fd, (uint8_t *)&csuResult, id, csuCmdPkt.Data.Register);
  990. return ret;
  991. }
  992. static int writeWaitPlugItState(int fd, uint8_t id)
  993. {
  994. int ret = PASS;
  995. uint8_t data[2] = {ShmSysConfigAndInfo->SysInfo.WaitForPlugit, 0};
  996. //printf("WaitForPlugit = %d\r\n", ShmSysConfigAndInfo->SysInfo.WaitForPlugit);
  997. if ((ret = composeSocketData(fd,
  998. id,
  999. OP_WRITE_DATA,
  1000. REG_WAIT_PLUG_IT_STATE,
  1001. 1,
  1002. &data[0])) == FAIL) {
  1003. return ret;
  1004. }
  1005. return ret;
  1006. }
  1007. static int readQRcodeURLAndSystemDate(int fd)
  1008. {
  1009. int ret = PASS;
  1010. ret = composeSocketData(fd,
  1011. ID_REGISTER,
  1012. OP_READ_DATA,
  1013. REG_QRCODE_URL_INFO,
  1014. 0,
  1015. NULL);
  1016. return ret;
  1017. }
  1018. static int writePresentChargingInfo(int fd, uint8_t plugNum, uint8_t id)
  1019. {
  1020. int ret = PASS;
  1021. uint8_t dataBuf[16] = {0};
  1022. PreChargingInfo *pPreChargingInfo = (PreChargingInfo *)dataBuf;
  1023. pPreChargingInfo->PresentChargingVoltage = htons((uint16_t)ChargingData[plugNum]->PresentChargingVoltage * 10);
  1024. pPreChargingInfo->PresentChargingCurrent = htons((uint16_t)ChargingData[plugNum]->PresentChargingCurrent * 10);
  1025. pPreChargingInfo->RemainChargingDuration = htonl(ChargingData[plugNum]->RemainChargingDuration);
  1026. pPreChargingInfo->EvBatterySoc = ChargingData[plugNum]->EvBatterySoc;
  1027. ret = composeSocketData(fd,
  1028. id,
  1029. OP_WRITE_DATA,
  1030. REG_PRESENT_CHARGING_INFO,
  1031. sizeof(PreChargingInfo),
  1032. (uint8_t *)pPreChargingInfo);
  1033. return ret;
  1034. }
  1035. static int writeOtherModuleVersion(int fd)
  1036. {
  1037. int ret = PASS;
  1038. uint8_t data[255] = {0};
  1039. uint8_t dataLen = 0;
  1040. //report other module version message
  1041. memcpy(&data[dataLen], ShmSysConfigAndInfo->SysInfo.FanModuleFwRev, VERSION_BUF_SIZE);
  1042. dataLen += VERSION_BUF_SIZE;
  1043. memcpy(&data[dataLen], ShmSysConfigAndInfo->SysInfo.RelayModuleFwRev, VERSION_BUF_SIZE);
  1044. dataLen += VERSION_BUF_SIZE;
  1045. memcpy(&data[dataLen], ShmSysConfigAndInfo->SysInfo.Connector1FwRev, VERSION_BUF_SIZE);
  1046. dataLen += VERSION_BUF_SIZE;
  1047. memcpy(&data[dataLen], ShmSysConfigAndInfo->SysInfo.Connector2FwRev, VERSION_BUF_SIZE);
  1048. dataLen += VERSION_BUF_SIZE;
  1049. memcpy(&data[dataLen], ShmSysConfigAndInfo->SysInfo.LedModuleFwRev, VERSION_BUF_SIZE);
  1050. dataLen += VERSION_BUF_SIZE;
  1051. ret = composeSocketData(fd,
  1052. ID_REGISTER,
  1053. OP_WRITE_DATA,
  1054. REG_REPORT_OTHER_VERSION,
  1055. dataLen,
  1056. &data[0]);
  1057. return ret;
  1058. }
  1059. static int writeCsuModuleVersion(int fd)
  1060. {
  1061. int ret = PASS;
  1062. uint8_t data[255] = {0};
  1063. uint8_t dataLen = 0;
  1064. //report CSU platform version message
  1065. memcpy(&data[dataLen], ShmSysConfigAndInfo->SysInfo.CsuBootLoadFwRev, VERSION_BUF_SIZE);
  1066. dataLen += VERSION_BUF_SIZE;
  1067. memcpy(&data[dataLen], ShmSysConfigAndInfo->SysInfo.CsuKernelFwRev, VERSION_BUF_SIZE);
  1068. dataLen += VERSION_BUF_SIZE;
  1069. memcpy(&data[dataLen], ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev, VERSION_BUF_SIZE);
  1070. dataLen += VERSION_BUF_SIZE;
  1071. memcpy(&data[dataLen], ShmSysConfigAndInfo->SysInfo.CsuPrimFwRev, VERSION_BUF_SIZE);
  1072. dataLen += VERSION_BUF_SIZE;
  1073. ret = composeSocketData(fd,
  1074. ID_REGISTER,
  1075. OP_WRITE_DATA,
  1076. REG_REPORT_CSU_VERSION,
  1077. dataLen,
  1078. &data[0]);
  1079. return ret;
  1080. }
  1081. static int readMiscCommand(int fd, uint8_t id)
  1082. {
  1083. int ret = PASS;
  1084. ret = composeSocketData(fd,
  1085. id,
  1086. OP_READ_DATA,
  1087. REG_MISC_CONTROL,
  1088. 0,
  1089. NULL);
  1090. return ret;
  1091. }
  1092. static int readChargePermission(int fd, uint8_t id)
  1093. {
  1094. //int ret = PASS;
  1095. //if ((ret = composeSocketData(fd,
  1096. // id,
  1097. // OP_READ_DATA,
  1098. // REG_CHARGING_PERMISSION,
  1099. // 0,
  1100. // NULL)) == FAIL) {
  1101. // return ret;
  1102. //}
  1103. return composeSocketData(fd,
  1104. id,
  1105. OP_READ_DATA,
  1106. REG_CHARGING_PERMISSION,
  1107. 0,
  1108. NULL);
  1109. }
  1110. static int writeUserID(int fd, uint8_t id, uint8_t *pUserID)
  1111. {
  1112. if ((strlen((char *)pUserID) <= 0) || (pUserID == NULL)) {
  1113. return FAIL;
  1114. }
  1115. return composeSocketData(fd,
  1116. id,
  1117. OP_WRITE_DATA,
  1118. REG_USER_ID,
  1119. strlen((char *)pUserID),
  1120. pUserID);
  1121. }
  1122. static int writeConnectorState(int fd, uint8_t plugNum, uint8_t id)
  1123. {
  1124. uint8_t dataBuf[8] = {0};
  1125. uint8_t alarmCode[WARNING_CODE_SIZE] = {0};
  1126. int ret = PASS;
  1127. if (ChargingData[plugNum]->SystemStatus <= S_AUTHORIZING) {
  1128. dataBuf[0] = 0; //idle
  1129. } else if ((ChargingData[plugNum]->SystemStatus <= S_PREPARING_FOR_EVSE) ||
  1130. (ChargingData[plugNum]->SystemStatus == S_CCS_PRECHARGE_ST0) ||
  1131. (ChargingData[plugNum]->SystemStatus == S_CCS_PRECHARGE_ST1)) {
  1132. dataBuf[0] = 1; //preparing
  1133. } else if (ChargingData[plugNum]->SystemStatus == S_CHARGING) {
  1134. dataBuf[0] = 2; //charging
  1135. } else if (ChargingData[plugNum]->SystemStatus == S_TERMINATING) {
  1136. dataBuf[0] = 3; //terminating
  1137. } else if (ChargingData[plugNum]->SystemStatus == S_ALARM) {
  1138. dataBuf[0] = 4; //Alarm
  1139. memcpy(&alarmCode[1], ShmOCPP16Data->StatusNotification[plugNum].VendorErrorCode, WARNING_CODE_SIZE);
  1140. }
  1141. ret = composeSocketData(fd,
  1142. id,
  1143. OP_WRITE_DATA,
  1144. REG_CONNECTOR_STATE,
  1145. sizeof(dataBuf),
  1146. &dataBuf[0]);
  1147. return ret;
  1148. }
  1149. static int writePlugInStatus(int fd, uint8_t plugNum, uint8_t id)
  1150. {
  1151. int ret = PASS;
  1152. uint8_t dataBuf[2] = {ChargingData[plugNum]->ConnectorPlugIn, 0};
  1153. ret = composeSocketData(fd,
  1154. id,
  1155. OP_WRITE_DATA,
  1156. REG_PLUG_IN_STATE,
  1157. 1,
  1158. &dataBuf[0]);
  1159. return ret;
  1160. }
  1161. static int readSoftwareUpdate(int fd)
  1162. {
  1163. int ret = PASS;
  1164. uint8_t dataBuf[2] = {ShmSysConfigAndInfo->SysInfo.FirmwareUpdate, 0};
  1165. ret = composeSocketData(fd,
  1166. 0x01,
  1167. OP_READ_DATA,
  1168. REG_SOFTWARE_UPDATE,
  1169. 1,
  1170. &dataBuf[0]);
  1171. return ret;
  1172. }
  1173. static int writeChargingTarget(int fd, uint8_t plugNum, uint8_t id)
  1174. {
  1175. int ret = PASS;
  1176. float ChargingVolt, ChargingAmp;
  1177. uint8_t dataBuf[4] = {0};
  1178. ChargingVolt = ChargingData[plugNum]->EvBatterytargetVoltage;
  1179. ChargingAmp = ChargingData[plugNum]->EvBatterytargetCurrent;
  1180. ChargingVolt *= 10;
  1181. ChargingAmp *= 10;
  1182. dataBuf[0] = ((uint16_t)ChargingVolt >> 8) & 0xFF;
  1183. dataBuf[1] = ((uint16_t)ChargingVolt) & 0xFF;
  1184. dataBuf[2] = ((uint16_t)ChargingAmp >> 8) & 0xFF;
  1185. dataBuf[3] = ((uint16_t)ChargingAmp) & 0xFF;
  1186. ret = composeSocketData(fd,
  1187. id,
  1188. OP_WRITE_DATA,
  1189. REG_CHARGING_TARGET,
  1190. sizeof(dataBuf),
  1191. &dataBuf[0]);
  1192. return ret;
  1193. }
  1194. static int readChargingCapability(int fd, uint8_t id)
  1195. {
  1196. int ret = PASS;
  1197. ret = composeSocketData(fd,
  1198. id,
  1199. OP_READ_DATA,
  1200. REG_CHARGING_CAP,
  1201. 0,
  1202. NULL);
  1203. return ret;
  1204. }
  1205. static int writeDispenserStatus(int fd)
  1206. {
  1207. uint8_t warningCount = 0;
  1208. uint8_t count = 0;
  1209. uint8_t CurWarnCodeTmp[10][6] = {0};
  1210. uint8_t PreWarnCodeTmp[10][6] = {0};
  1211. int ret = PASS;
  1212. if ((ShmSysConfigAndInfo->SysWarningInfo.WarningCount <= 0) &&
  1213. (gPreSysWarningInfo.WarningCount <= 0)) {
  1214. return FAIL;
  1215. }
  1216. warningCount = ShmSysConfigAndInfo->SysWarningInfo.WarningCount;
  1217. for (count = 0; count < warningCount; count++) {
  1218. memcpy(CurWarnCodeTmp[count], ShmSysConfigAndInfo->SysWarningInfo.WarningCode[count], WARNING_CODE_SIZE);
  1219. }
  1220. warningCount = gPreSysWarningInfo.WarningCount;
  1221. for (count = 0; count < warningCount; count++) {
  1222. memcpy(PreWarnCodeTmp[count], gPreSysWarningInfo.WarningCode[count], WARNING_CODE_SIZE);
  1223. }
  1224. if (strcmp((char *)&CurWarnCodeTmp[0], (char *)&PreWarnCodeTmp[0]) == 0) {
  1225. return FAIL;
  1226. }
  1227. ret = composeSocketData(fd,
  1228. ID_OF_DISPENSER,
  1229. OP_WRITE_DATA,
  1230. REG_DISPENSER_STATUS,
  1231. strlen((char *)CurWarnCodeTmp),
  1232. &CurWarnCodeTmp[0][0]);
  1233. return ret;
  1234. }
  1235. static int readPowerCabinetStatus(int fd)
  1236. {
  1237. int ret = PASS;
  1238. ret = composeSocketData(fd,
  1239. ID_OF_DISPENSER,
  1240. OP_READ_DATA,
  1241. REG_POWER_CABINET_STATUS,
  1242. 0,
  1243. NULL);
  1244. return ret;
  1245. }
  1246. static int readConnectorID(int fd)
  1247. {
  1248. int ret = PASS;
  1249. ret = composeSocketData(fd,
  1250. ID_REGISTER,
  1251. OP_READ_DATA,
  1252. REG_CONNECTOR_ID,
  1253. 0,
  1254. NULL);
  1255. return ret;
  1256. }
  1257. static int WriteModelName(int fd)
  1258. {
  1259. int ret = PASS;
  1260. uint8_t Tmp = 0;
  1261. uint8_t TmpBuf[255] = {0};
  1262. memcpy(TmpBuf,
  1263. ShmSysConfigAndInfo->SysConfig.ModelName,
  1264. strlen((char *)ShmSysConfigAndInfo->SysConfig.ModelName));
  1265. Tmp = (uint8_t)(ShmPrimaryMcuData->InputDet.bits.Key2 << 2 |
  1266. ShmPrimaryMcuData->InputDet.bits.Key1 << 1 |
  1267. ShmPrimaryMcuData->InputDet.bits.Key0);
  1268. TmpBuf[strlen((char *)ShmSysConfigAndInfo->SysConfig.ModelName)] = Tmp; //Dispenser switch value
  1269. ret = composeSocketData(fd,
  1270. ID_REGISTER,
  1271. OP_WRITE_DATA,
  1272. REG_MODEL_NAME,
  1273. strlen((char *)ShmSysConfigAndInfo->SysConfig.ModelName) + 1,
  1274. &TmpBuf[0]);
  1275. return ret;
  1276. }
  1277. static void calDisconnectCount(uint8_t *pValue, uint8_t maxCount)
  1278. {
  1279. sleep(3); //wait 1 second
  1280. if ((*pValue) >= maxCount) {
  1281. setTcpStatus(ABNORMAL);
  1282. } else {
  1283. (*pValue)++;
  1284. }
  1285. }
  1286. static int CheckNetworkStatus(void)
  1287. {
  1288. char *ipAddr = (char *)&ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthIpAddress;
  1289. //printf("Check network IP Header = %s\n", ipAddr);
  1290. if (strstr(ipAddr, CMP_ETH_IP_HEAD) != NULL) {
  1291. return 1;
  1292. }
  1293. return 0;
  1294. }
  1295. static int FindChargingInfoData(byte target, struct ChargingInfoData **chargingData)
  1296. {
  1297. uint8_t index = 0;
  1298. for (index = 0; index < CHAdeMO_QUANTITY; index++) {
  1299. if (ShmSysConfigAndInfo->SysInfo.ChademoChargingData[index].Index == target) {
  1300. chargingData[target] = &ShmSysConfigAndInfo->SysInfo.ChademoChargingData[index];
  1301. return 1;
  1302. }
  1303. }
  1304. for (index = 0; index < CCS_QUANTITY; index++) {
  1305. if (ShmSysConfigAndInfo->SysInfo.CcsChargingData[index].Index == target) {
  1306. chargingData[target] = &ShmSysConfigAndInfo->SysInfo.CcsChargingData[index];
  1307. return 1;
  1308. }
  1309. }
  1310. for (index = 0; index < GB_QUANTITY; index++) {
  1311. if (ShmSysConfigAndInfo->SysInfo.GbChargingData[index].Index == target) {
  1312. chargingData[target] = &ShmSysConfigAndInfo->SysInfo.GbChargingData[index];
  1313. return 1;
  1314. }
  1315. }
  1316. return 0;
  1317. }
  1318. static int InitShareMemory(void)
  1319. {
  1320. int result = 1;
  1321. int MeterSMId = 0;
  1322. if ((MeterSMId = shmget(ShmSysConfigAndInfoKey, sizeof(struct SysConfigAndInfo), 0777)) < 0) {
  1323. DEBUG_ERROR("shmget ShmSysConfigAndInfo NG\n");
  1324. result = 0;
  1325. } else if ((ShmSysConfigAndInfo = shmat(MeterSMId, NULL, 0)) == (void *) - 1) {
  1326. DEBUG_ERROR("[shmat ShmSysConfigAndInfo NG\n");
  1327. result = 0;
  1328. }
  1329. if ((MeterSMId = shmget(ShmStatusCodeKey, sizeof(struct StatusCodeData), 0777)) < 0) {
  1330. DEBUG_ERROR("shmget ShmStatusCodeData NG\n");
  1331. result = 0;
  1332. } else if ((ShmStatusCodeData = shmat(MeterSMId, NULL, 0)) == (void *) - 1) {
  1333. DEBUG_ERROR("shmat ShmStatusCodeData NG\n");
  1334. result = 0;
  1335. }
  1336. //creat ShmPsuData
  1337. if ((MeterSMId = shmget(ShmPsuKey, sizeof(struct PsuData), 0777)) < 0) {
  1338. DEBUG_ERROR("shmget ShmPsuData NG \n");
  1339. result = 0;
  1340. } else if ((ShmPsuData = shmat(MeterSMId, NULL, 0)) == (void *) - 1) {
  1341. DEBUG_ERROR("shmat ShmPsuData NG \n");
  1342. result = 0;
  1343. }
  1344. //creat ShmOCPP16Data
  1345. if ((MeterSMId = shmget(ShmOcppModuleKey, sizeof(struct OCPP16Data), 0777)) < 0) {
  1346. DEBUG_ERROR("shmget ShmOCPP16Data NG \n");
  1347. result = 0;
  1348. } else if ((ShmOCPP16Data = shmat(MeterSMId, NULL, 0)) == (void *) - 1) {
  1349. DEBUG_ERROR("shmat ShmOCPP16Data NG \n");
  1350. result = 0;
  1351. }
  1352. if ((MeterSMId = shmget(ShmPrimaryMcuKey, sizeof(struct PrimaryMcuData), 0777)) < 0) {
  1353. DEBUG_ERROR("shmget ShmPrimaryMcuData NG\n");
  1354. result = 0;
  1355. } else if ((ShmPrimaryMcuData = shmat(MeterSMId, NULL, 0)) == (void *) - 1) {
  1356. DEBUG_ERROR("shmat ShmPrimaryMcuData NG\n");
  1357. result = 0;
  1358. }
  1359. //creat Audi customization info
  1360. if ((MeterSMId = shmget(ShmAudiCustInfoKey, sizeof(AudiCustInfo), IPC_CREAT | 0777)) < 0) {
  1361. DEBUG_ERROR("[main]CreatShareMemory:shmget select gun info NG \n");
  1362. return 0;
  1363. } else if ((gAudiCustInfo = shmat(MeterSMId, NULL, 0)) == (void *) - 1) {
  1364. DEBUG_ERROR("[main]CreatShareMemory:shmat shmget select gun info \n");
  1365. return 0;
  1366. }
  1367. return result;
  1368. }
  1369. static void updateFirmwareProcess(int fd, uint8_t totalConnCount)
  1370. {
  1371. bool canUpdateFirmware = true;
  1372. uint8_t plugNum = 0;
  1373. uint8_t ackCount = 5;
  1374. struct timeb updateTime;
  1375. if (ShmSysConfigAndInfo->SysWarningInfo.Level != 2) {
  1376. for (plugNum = 0; plugNum < totalConnCount; plugNum++) {
  1377. if (ChargingData[plugNum]->SystemStatus != S_IDLE &&
  1378. ChargingData[plugNum]->SystemStatus != S_RESERVATION &&
  1379. ChargingData[plugNum]->SystemStatus != S_MAINTAIN) {
  1380. canUpdateFirmware = false;
  1381. }
  1382. }
  1383. }
  1384. if (canUpdateFirmware) {
  1385. ftime(&updateTime);
  1386. if (DiffTimeb(gRegTimeUp[0][REG_SOFTWARE_UPDATE], updateTime) > LOOP_RETRY_TIME * 5) {
  1387. readSoftwareUpdate(fd);
  1388. ftime(&gRegTimeUp[0][REG_SOFTWARE_UPDATE]);
  1389. }
  1390. if (ShmSysConfigAndInfo->SysInfo.FirmwareUpdate == YES) {
  1391. while (ackCount != 0) {
  1392. ftime(&updateTime);
  1393. if (DiffTimeb(gRegTimeUp[0][REG_SOFTWARE_UPDATE], updateTime) > LOOP_RETRY_TIME / 2) {
  1394. readSoftwareUpdate(fd);
  1395. ftime(&gRegTimeUp[0][REG_SOFTWARE_UPDATE]);
  1396. if (ShmSysConfigAndInfo->SysInfo.FirmwareUpdate == NO) {
  1397. ackCount--;
  1398. }
  1399. }
  1400. usleep(128);
  1401. }
  1402. }
  1403. }
  1404. }
  1405. static void checkAuthorProcess(int fd, uint8_t plugNum)
  1406. {
  1407. int ret = 0;
  1408. struct timeb AuthNowTime;
  1409. if ((ShmOCPP16Data->SpMsg.bits.AuthorizeReq == YES) || (ShmSysConfigAndInfo->SysInfo.AuthorizeFlag == YES)) {
  1410. ftime(&AuthNowTime);
  1411. if (DiffTimeb(gRegTimeUp[plugNum][REG_USER_ID], AuthNowTime) > LOOP_RETRY_TIME) {
  1412. ret = writeUserID(fd,
  1413. ShmSysConfigAndInfo->SysInfo.CurGunSelected + 1,
  1414. ShmSysConfigAndInfo->SysConfig.UserId);
  1415. if (ret >= 0) {
  1416. memset(ShmOCPP16Data->Authorize.ResponseIdTagInfo.Status,
  1417. 0,
  1418. sizeof(ShmOCPP16Data->Authorize.ResponseIdTagInfo.Status));
  1419. if (ret == 0) {
  1420. strcpy((char *)ShmOCPP16Data->Authorize.ResponseIdTagInfo.Status, "Invalid");
  1421. } else {
  1422. strcpy((char *)ShmOCPP16Data->Authorize.ResponseIdTagInfo.Status, "Accepted");
  1423. }
  1424. //printf("gAudiCustInfo->PricesInfo[plugNum].Balance = %.2f, %.2f\r\n",
  1425. // gAudiCustInfo->PricesInfo[plugNum].Balance,
  1426. // FAIL_BALANCE_PRICES);
  1427. if (gAudiCustInfo->PricesInfo[plugNum].Balance != FAIL_BALANCE_PRICES) {
  1428. ShmOCPP16Data->SpMsg.bits.AuthorizeConf = 1; //isAuthorizedComplete
  1429. ShmOCPP16Data->SpMsg.bits.AuthorizeReq = 0;
  1430. ShmSysConfigAndInfo->SysInfo.AuthorizeFlag = 0;
  1431. ShmPsuData->SystemAvailablePower = 0;
  1432. ShmPsuData->SystemPresentPsuQuantity = 0;
  1433. }
  1434. }
  1435. ftime(&gRegTimeUp[plugNum][REG_USER_ID]);
  1436. }
  1437. }
  1438. }
  1439. static int messageTrigger(int fd, uint8_t plugNum, uint8_t gunID, uint8_t curReg)
  1440. {
  1441. int ret = DISPENSER_INIT_SUCC;
  1442. int isContinue = 1;
  1443. struct timeb NowTime;
  1444. while (isContinue) {
  1445. usleep(128);
  1446. ftime(&NowTime);
  1447. switch (curReg) {
  1448. case REG_MODEL_NAME:
  1449. if (WriteModelName(fd) == PASS) {
  1450. gDoCommGblData.DisConnCount = 0;
  1451. curReg = REG_CONNECTOR_ID;
  1452. }
  1453. break;
  1454. case REG_CONNECTOR_ID:
  1455. if (readConnectorID(fd) == PASS) {
  1456. gDoCommGblData.DisConnCount = 0;
  1457. curReg = REG_REPORT_CSU_VERSION;
  1458. }
  1459. break;
  1460. case REG_POWER_CABINET_STATUS:
  1461. if (DiffTimeb(gRegTimeUp[plugNum][curReg], NowTime) > LOOP_RETRY_TIME) {
  1462. readPowerCabinetStatus(fd);
  1463. ftime(&gRegTimeUp[plugNum][curReg]);
  1464. }
  1465. curReg = REG_DISPENSER_STATUS;
  1466. break;
  1467. case REG_DISPENSER_STATUS:
  1468. if (DiffTimeb(gRegTimeUp[plugNum][curReg], NowTime) > LOOP_RETRY_TIME) {
  1469. writeDispenserStatus(fd);
  1470. ftime(&gRegTimeUp[plugNum][curReg]);
  1471. }
  1472. curReg = REG_PLUG_IN_STATE;
  1473. break;
  1474. //case REG_CHARGING_CAP:
  1475. // readChargingCapability(fd, plugNum, gunID);
  1476. // break;
  1477. //case REG_CHARGING_TARGET:
  1478. // writeChargingTarget(fd, plugNum, gunID);
  1479. // break;
  1480. //case REG_SOFTWARE_UPDATE:
  1481. // if (ChargingData[plugNum]->SystemStatus == S_IDLE) {
  1482. // if (DiffTimeb(gRegTimeUp[plugNum][curReg], NowTime) > LOOP_RETRY_TIME * 10) {
  1483. // readSoftwareUpdate(fd);
  1484. // ftime(&gRegTimeUp[plugNum][curReg]);
  1485. // }
  1486. // while (ShmSysConfigAndInfo->SysInfo.FirmwareUpdate == YES) {
  1487. // ftime(&NowTime);
  1488. // if (DiffTimeb(gRegTimeUp[plugNum][curReg], NowTime) > LOOP_RETRY_TIME) {
  1489. // readSoftwareUpdate(fd);
  1490. // }
  1491. // usleep(128);
  1492. // }
  1493. // }
  1494. // isContinue = 0;
  1495. // break;
  1496. case REG_PLUG_IN_STATE:
  1497. if (DiffTimeb(gRegTimeUp[plugNum][curReg], NowTime) > LOOP_RETRY_TIME) {
  1498. writePlugInStatus(fd, plugNum, gunID);
  1499. ftime(&gRegTimeUp[plugNum][curReg]);
  1500. }
  1501. curReg = REG_CONNECTOR_STATE;
  1502. break;
  1503. case REG_CONNECTOR_STATE:
  1504. if (DiffTimeb(gRegTimeUp[plugNum][curReg], NowTime) > LOOP_RETRY_TIME) {
  1505. writeConnectorState(fd, plugNum, gunID);
  1506. ftime(&gRegTimeUp[plugNum][curReg]);
  1507. }
  1508. //check misc command from power cabinet
  1509. if (gDoCommGblData.MiscCmd != 0) {
  1510. curReg = gDoCommGblData.MiscCmd;
  1511. } else {
  1512. curReg = REG_QRCODE_URL_INFO;
  1513. }
  1514. break;
  1515. //case REG_USER_ID:
  1516. // writeUserID(fd, gunID, uint8_t *pUserID);
  1517. // break;
  1518. //case REG_CHARGING_PERMISSION:
  1519. // readChargePermission(fd, gunID);
  1520. // break;
  1521. case REG_MISC_CONTROL:
  1522. readMiscCommand(fd, gunID);
  1523. isContinue = 0;
  1524. break;
  1525. case REG_REPORT_CSU_VERSION:
  1526. case REG_REPORT_OTHER_VERSION:
  1527. if (gDoCommGblData.MiscCmd != 0) {
  1528. writeCsuModuleVersion(fd);
  1529. writeOtherModuleVersion(fd);
  1530. clearMiscCommand();
  1531. isContinue = 0;
  1532. } else {
  1533. //power up write dispenser version and get power cabinet system ID
  1534. if ((writeCsuModuleVersion(fd) == PASS) &&
  1535. (writeOtherModuleVersion(fd) == PASS) &&
  1536. (readQRcodeURLAndSystemDate(fd) == PASS)) {
  1537. //gDoCommGblData.DisConnCount = 0;
  1538. isContinue = 0;
  1539. }
  1540. }
  1541. break;
  1542. //case REG_PRESENT_CHARGING_INFO:
  1543. // break;
  1544. case REG_QRCODE_URL_INFO:
  1545. if (gunID == 1) {
  1546. if (DiffTimeb(gRegTimeUp[plugNum][curReg], NowTime) > (LOOP_RETRY_TIME * 10)) {
  1547. readQRcodeURLAndSystemDate(fd);
  1548. ftime(&gRegTimeUp[plugNum][curReg]);
  1549. }
  1550. }
  1551. isContinue = 0;
  1552. //curReg = REG_SOFTWARE_UPDATE;
  1553. break;
  1554. //case REG_WAIT_PLUG_IT_STATE:
  1555. // if (DiffTimeb(gRegTimeUp[plugNum][curReg], NowTime) > (LOOP_RETRY_TIME / 10)) {
  1556. // writeWaitPlugItState(fd, gunID);
  1557. // ftime(&gRegTimeUp[plugNum][curReg]);
  1558. // }
  1559. // isContinue = 0;
  1560. // break;
  1561. default:
  1562. DEBUG_ERROR("error curReg = %x\r\n", curReg);
  1563. gDoCommGblData.MiscCmd = 0;
  1564. gDoCommGblData.DisConnCount = CHECK_NETWORK_FAIL_COUNT;
  1565. isContinue = 0;
  1566. break;
  1567. }
  1568. if (gDoCommGblData.DisConnCount >= CHECK_NETWORK_FAIL_COUNT) {
  1569. return DISPENSER_SOCKET_RECONN;
  1570. }
  1571. }//for
  1572. return ret;
  1573. }
  1574. //static bool isDetectPlugin()
  1575. //{
  1576. // if (ShmSysConfigAndInfo->SysInfo.WaitForPlugit == YES) {
  1577. // return YES;
  1578. // }
  1579. //
  1580. // return NO;
  1581. //}
  1582. static void systemStatusProcess(int fd, uint8_t plugNum, uint8_t gunID)
  1583. {
  1584. struct timeb AuthNowTime = {0};
  1585. switch (ChargingData[plugNum]->SystemStatus) {
  1586. case S_IDLE:
  1587. case S_RESERVATION:
  1588. case S_MAINTAIN:
  1589. case S_ALARM:
  1590. case S_AUTHORIZING:
  1591. checkAuthorProcess(fd, plugNum);
  1592. //authorization complete, write wait plug it state
  1593. if (gAudiCustInfo->PricesInfo[plugNum].Balance != FAIL_BALANCE_PRICES) {
  1594. ftime(&AuthNowTime);
  1595. if (DiffTimeb(gRegTimeUp[plugNum][REG_WAIT_PLUG_IT_STATE], AuthNowTime) > (LOOP_RETRY_TIME / 10)) {
  1596. writeWaitPlugItState(fd, gunID);
  1597. ftime(&gRegTimeUp[plugNum][REG_WAIT_PLUG_IT_STATE]);
  1598. }
  1599. ftime(&AuthNowTime);
  1600. if (DiffTimeb(gRegTimeUp[plugNum][REG_CHARGING_CAP], AuthNowTime) > LOOP_RETRY_TIME) {
  1601. readChargingCapability(fd, gunID);
  1602. ftime(&gRegTimeUp[plugNum][REG_CHARGING_CAP]);
  1603. }
  1604. }
  1605. break;
  1606. case S_PREPARNING: //get permission
  1607. ftime(&AuthNowTime);
  1608. if (DiffTimeb(gRegTimeUp[plugNum][REG_CONNECTOR_STATE], AuthNowTime) > LOOP_RETRY_TIME) {
  1609. writeConnectorState(fd, plugNum, gunID);
  1610. ftime(&gRegTimeUp[plugNum][REG_CONNECTOR_STATE]);
  1611. }
  1612. ftime(&AuthNowTime);
  1613. if (DiffTimeb(gRegTimeUp[plugNum][REG_CHARGING_PERMISSION], AuthNowTime) > LOOP_RETRY_TIME) {
  1614. if (readChargePermission(fd, gunID) && readChargingCapability(fd, gunID)) {
  1615. ShmPsuData->SystemAvailablePower = (ChargingData[0]->AvailableChargingPower +
  1616. ChargingData[1]->AvailableChargingPower);
  1617. ShmPsuData->SystemPresentPsuQuantity = (ShmPsuData->SystemAvailablePower / 30);
  1618. }
  1619. ftime(&gRegTimeUp[plugNum][REG_CHARGING_PERMISSION]);
  1620. }
  1621. ftime(&AuthNowTime);
  1622. if (DiffTimeb(gRegTimeUp[plugNum][REG_PRESENT_CHARGING_INFO], AuthNowTime) > LOOP_RETRY_TIME) {
  1623. writePresentChargingInfo(fd, plugNum, gunID);
  1624. ftime(&gRegTimeUp[plugNum][REG_PRESENT_CHARGING_INFO]);
  1625. }
  1626. break;
  1627. case S_PREPARING_FOR_EV://wait connector lock
  1628. ftime(&AuthNowTime);
  1629. if (DiffTimeb(gRegTimeUp[plugNum][REG_CHARGING_CAP], AuthNowTime) > LOOP_RETRY_TIME) {
  1630. readChargingCapability(fd, gunID);
  1631. ftime(&gRegTimeUp[plugNum][REG_CHARGING_CAP]);
  1632. }
  1633. ftime(&AuthNowTime);
  1634. if (DiffTimeb(gRegTimeUp[plugNum][REG_CHARGING_PERMISSION], AuthNowTime) > LOOP_RETRY_TIME) {
  1635. if (readChargePermission(fd, gunID) == 0) {
  1636. DEBUG_INFO("Stop charging by power cabinet's permission = %d, %d\r\n",
  1637. plugNum,
  1638. REG_CHARGING_PERMISSION);
  1639. ChargingData[plugNum]->StopChargeFlag = 1;
  1640. }
  1641. ftime(&gRegTimeUp[plugNum][REG_CHARGING_PERMISSION]);
  1642. }
  1643. ftime(&AuthNowTime);
  1644. if (DiffTimeb(gRegTimeUp[plugNum][REG_PRESENT_CHARGING_INFO], AuthNowTime) > LOOP_RETRY_TIME) {
  1645. writePresentChargingInfo(fd, plugNum, gunID);
  1646. ftime(&gRegTimeUp[plugNum][REG_PRESENT_CHARGING_INFO]);
  1647. }
  1648. break;
  1649. case S_PREPARING_FOR_EVSE: //insaulation test
  1650. case S_CCS_PRECHARGE_ST0:
  1651. case S_CCS_PRECHARGE_ST1:
  1652. writeChargingTarget(fd, plugNum, gunID);
  1653. ftime(&AuthNowTime);
  1654. if (DiffTimeb(gRegTimeUp[plugNum][REG_CHARGING_CAP], AuthNowTime) > LOOP_RETRY_TIME) {
  1655. readChargingCapability(fd, gunID);
  1656. ftime(&gRegTimeUp[plugNum][REG_CHARGING_CAP]);
  1657. }
  1658. ftime(&AuthNowTime);
  1659. if (DiffTimeb(gRegTimeUp[plugNum][REG_CHARGING_PERMISSION], AuthNowTime) > LOOP_RETRY_TIME) {
  1660. if (readChargePermission(fd, gunID) == 0) {
  1661. DEBUG_INFO("Stop charging by power cabinet's permission = %d, %d\r\n",
  1662. plugNum,
  1663. REG_CHARGING_PERMISSION);
  1664. ChargingData[plugNum]->StopChargeFlag = 1;
  1665. }
  1666. ftime(&gRegTimeUp[plugNum][REG_CHARGING_PERMISSION]);
  1667. }
  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_CHARGING: //charging
  1675. case S_TERMINATING:
  1676. ftime(&AuthNowTime);
  1677. if (DiffTimeb(gRegTimeUp[plugNum][REG_CHARGING_CAP], AuthNowTime) > LOOP_RETRY_TIME) {
  1678. readChargingCapability(fd, gunID);
  1679. ftime(&gRegTimeUp[plugNum][REG_CHARGING_CAP]);
  1680. }
  1681. ftime(&AuthNowTime);
  1682. if (DiffTimeb(gRegTimeUp[plugNum][REG_CHARGING_PERMISSION], AuthNowTime) > LOOP_RETRY_TIME) {
  1683. if (readChargePermission(fd, gunID) == 0) {
  1684. DEBUG_INFO("Stop charging by power cabinet's permission = %d, %d\r\n",
  1685. plugNum,
  1686. REG_CHARGING_PERMISSION);
  1687. ChargingData[plugNum]->StopChargeFlag = 1;
  1688. }
  1689. ftime(&gRegTimeUp[plugNum][REG_CHARGING_PERMISSION]);
  1690. }
  1691. writeChargingTarget(fd, plugNum, gunID);
  1692. ftime(&AuthNowTime);
  1693. if (DiffTimeb(gRegTimeUp[plugNum][REG_PRESENT_CHARGING_INFO], AuthNowTime) > LOOP_RETRY_TIME) {
  1694. writePresentChargingInfo(fd, plugNum, gunID);
  1695. ftime(&gRegTimeUp[plugNum][REG_PRESENT_CHARGING_INFO]);
  1696. }
  1697. break;
  1698. case S_COMPLETE:
  1699. ftime(&AuthNowTime);
  1700. if (DiffTimeb(gRegTimeUp[plugNum][REG_CHARGING_CAP], AuthNowTime) > LOOP_RETRY_TIME) {
  1701. readChargingCapability(fd, gunID);
  1702. ftime(&gRegTimeUp[plugNum][REG_CHARGING_CAP]);
  1703. }
  1704. break;
  1705. default:
  1706. break;
  1707. }
  1708. }
  1709. static int networkCreatePorcess(void)
  1710. {
  1711. int fd = 0;
  1712. /**************** Check Network **********/
  1713. DEBUG_INFO("Check Dispenser Network Information");
  1714. while (!CheckNetworkStatus()) {
  1715. calDisconnectCount(&gDoCommGblData.DisConnCount, CHECK_NETWORK_FAIL_COUNT);
  1716. DEBUG_ERROR("disconnect count = % d\r\n", gDoCommGblData.DisConnCount);
  1717. }
  1718. DEBUG_INFO("Dispenser Network Information checked: IP = % s Netmask = % s, Gateway = % s",
  1719. ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthIpAddress,
  1720. ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthSubmaskAddress,
  1721. ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthGatewayAddress);
  1722. gDoCommGblData.DisConnCount = 0;
  1723. /**************** Power cabinet connection **********/
  1724. DEBUG_INFO("Connect to Power Cabinet");
  1725. while ((fd = doCommConnToServer()) <= 0) {
  1726. calDisconnectCount(&gDoCommGblData.DisConnCount, CONNECT_SERVER_FAIL_COUNT);
  1727. DEBUG_ERROR("disconnect count = %d, fd = %d\r\n", gDoCommGblData.DisConnCount, fd);
  1728. }
  1729. DEBUG_INFO("Power cabinet connected(fd = % d): IP = % s Netmask = % s, Gateway = % s",
  1730. fd,
  1731. ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthIpAddress,
  1732. ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthSubmaskAddress,
  1733. ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthGatewayAddress);
  1734. gDoCommGblData.DisConnCount = 0;
  1735. destroySelectGun(DESTROY_ALL_SEL);
  1736. sleep(3);
  1737. setTcpStatus(NORMAL);
  1738. return fd;
  1739. }
  1740. int main(int argc, char *argv[])
  1741. {
  1742. uint8_t plugNum = 0, gunID = 0;
  1743. uint8_t index = 0;
  1744. uint8_t totalConnCount = 0;
  1745. uint8_t initDone = DISPENER_INIT_FAIL;
  1746. int fd = 0;
  1747. int isContinue = 1;
  1748. InitSocketSigPipe();
  1749. /**************** Initialization **********/
  1750. if (InitShareMemory() == 0) {
  1751. if (ShmStatusCodeData != NULL) {
  1752. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.FailToCreateShareMemory = YES;
  1753. }
  1754. DEBUG_ERROR("DoComm Initial share memory fail\r\n");
  1755. sleep(5);
  1756. return 0;
  1757. }
  1758. totalConnCount = ShmSysConfigAndInfo->SysConfig.TotalConnectorCount;
  1759. for (index = 0; index < totalConnCount; index++) {
  1760. if (!FindChargingInfoData(index, &ChargingData[0])) {
  1761. DEBUG_ERROR("FindChargingInfoData false \n");
  1762. break;
  1763. }
  1764. clearPricesInfo(index);
  1765. }
  1766. //initial trigger message timer
  1767. for (index = 0; index < MAX_REGISTER_NUM; index++) {
  1768. ftime(&gRegTimeUp[0][index]);
  1769. ftime(&gRegTimeUp[1][index]);
  1770. usleep(50000);
  1771. }
  1772. setTcpStatus(NORMAL);
  1773. while (isContinue) {
  1774. if (initDone == DISPENER_INIT_FAIL) {
  1775. fd = networkCreatePorcess();
  1776. initDone = messageTrigger(fd, 0, 0, REG_MODEL_NAME); //first trigger model name and connector id
  1777. } else {
  1778. checkAuthorProcess(fd, 0);
  1779. plugNum = 0;
  1780. gunID = 0;
  1781. for (plugNum = 0; plugNum < totalConnCount; plugNum++) {
  1782. //plugNum = setup chargingData value for bottom layer
  1783. gunID = (plugNum + 1); //connector Id, 1 = left gun, 2 = right gun
  1784. systemStatusProcess(fd, plugNum, gunID);
  1785. initDone = messageTrigger(fd,
  1786. plugNum,
  1787. gunID,
  1788. REG_POWER_CABINET_STATUS);
  1789. if (initDone == DISPENSER_SOCKET_RECONN) {
  1790. break;
  1791. }
  1792. }
  1793. }
  1794. if (initDone == DISPENSER_SOCKET_RECONN) {
  1795. DEBUG_INFO("Disconnected from power cabinet...re-connecting");
  1796. setTcpStatus(ABNORMAL);
  1797. gDoCommGblData.DisConnCount = 0;
  1798. gDoCommGblData.SeqNum = 0;
  1799. closeSocket(fd);
  1800. fd = 0;
  1801. initDone = DISPENER_INIT_FAIL;
  1802. }
  1803. //update dispenser firmware
  1804. if (initDone != DISPENER_INIT_FAIL) {
  1805. updateFirmwareProcess(fd, totalConnCount);
  1806. }
  1807. usleep(1000);
  1808. }
  1809. }