ReDoComm.c 63 KB

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