Module_OcppBackend20.c 32 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225
  1. #include "Module_OcppBackend20.h"
  2. typedef enum boolean { FALSE, TRUE } BOOL;
  3. struct lws *wsi_client;
  4. struct lws_context *context;
  5. static int req_SendQueue = 0;
  6. pthread_t tid_connectServer;
  7. pthread_t tid_ProcQueue;
  8. pthread_t tid_Watchdog;
  9. struct StartTime
  10. {
  11. unsigned int connect;
  12. unsigned int bootNotification;
  13. }startTime;
  14. //==========================================
  15. // Function prototype
  16. //==========================================
  17. void ReceivedMessage(void *in, size_t len);
  18. int SendBufLen=0;//(1024*4);//(1024*3);
  19. unsigned char SendBuffer[1024*20]={0};
  20. static int ConnectionEstablished=0;
  21. static int TransactionMessageResend = 1; // the number of retry to submit a transaction-related message when the Central System fails to process it.
  22. static int TransactionQueueNum = 0;
  23. static int OfflineTransactionQueueNum = 0; // Number of offline transactions
  24. static int OfflineTransaction = 0;
  25. static int IsUsing = FALSE;
  26. char OcppPath[384]={0};
  27. char OcppProtocol[10]={0},OcppHost[128]={0}, OcppTempPath[256]={0};
  28. int OcppPort=0;
  29. unsigned char StartTransactionIdTagTemp[20]={0};
  30. uint32_t startTimeDog;
  31. uint32_t startTimeQueue;
  32. uint8_t isWebsocketSendable = 1;
  33. uint8_t counterLwsRestart = 0;;
  34. uint8_t counterQueueSent = 0;
  35. uint8_t counterConnect = 0;
  36. //=================================
  37. // Common routine
  38. //=================================
  39. int GetTransactionQueueNum(void)
  40. {
  41. return TransactionQueueNum;
  42. }
  43. //==========================================
  44. // Web socket tranceive routine
  45. //==========================================
  46. int SendData(struct lws *wsi)
  47. {
  48. int n;
  49. int len;
  50. unsigned char out[LWS_SEND_BUFFER_PRE_PADDING + (1024*20) + LWS_SEND_BUFFER_POST_PADDING] = {0};
  51. len = strlen((char *)SendBuffer);
  52. if(len == 0)
  53. return 0;
  54. memcpy (out + LWS_SEND_BUFFER_PRE_PADDING, SendBuffer, len );
  55. DEBUG_OCPPMESSAGE_INFO("===========> %s\n", out + LWS_SEND_BUFFER_PRE_PADDING);
  56. n = lws_write(wsi, out + LWS_SEND_BUFFER_PRE_PADDING, len, LWS_WRITE_TEXT);
  57. memset(SendBuffer, 0, len);
  58. SendBufLen = 0;
  59. return n;
  60. }
  61. static int OCPP20Callback(struct lws *wsi, enum lws_callback_reasons reason, void *user, void *in, size_t len)
  62. {
  63. char buf[256]={0}, hash[20]={0}, key_b64[40]={0}, tempin[65536]={0}, sstr[65536]={0};
  64. uint8_t auth_b64[256]={0}, boxId[128]={0}, password[64]={0};
  65. int c = 0;
  66. char *loc;
  67. switch (reason)
  68. {
  69. case LWS_CALLBACK_PROTOCOL_INIT:
  70. DEBUG_INFO("LWS_CALLBACK_PROTOCOL_INIT\n");
  71. break;
  72. case LWS_CALLBACK_CLIENT_FILTER_PRE_ESTABLISH:
  73. DEBUG_INFO("LWS_CALLBACK_CLIENT_FILTER_PRE_ESTABLISH\n");
  74. DEBUG_OCPPMESSAGE_INFO("----- Handshake: Client Request START -----\n");
  75. lws_hdr_copy(wsi, buf, ARRAY_SIZE(buf) - 1, _WSI_TOKEN_CLIENT_URI);
  76. DEBUG_OCPPMESSAGE_INFO("GET %s HTTP/1.1 \n", buf);
  77. lws_hdr_copy(wsi, buf, ARRAY_SIZE(buf) - 1, _WSI_TOKEN_CLIENT_HOST);
  78. DEBUG_OCPPMESSAGE_INFO("Host: %s\n", buf);
  79. DEBUG_OCPPMESSAGE_INFO("Upgrade: websocket\n");
  80. DEBUG_OCPPMESSAGE_INFO("Connection: Upgrade\n");
  81. lws_b64_encode_string(hash, 16, key_b64, ARRAY_SIZE(key_b64));// Sec-WebSocket-Key
  82. DEBUG_OCPPMESSAGE_INFO("Sec-WebSocket-Key: %s\n", key_b64);
  83. lws_hdr_copy(wsi, buf, ARRAY_SIZE(buf) - 1, _WSI_TOKEN_CLIENT_SENT_PROTOCOLS);
  84. DEBUG_OCPPMESSAGE_INFO("Sec-WebSocket-Protocol: %s\n", buf);
  85. DEBUG_OCPPMESSAGE_INFO("Sec-WebSocket-Version: %d\n", SPEC_LATEST_SUPPORTED);
  86. DEBUG_OCPPMESSAGE_INFO("----- Handshake: Client Request END -----\n");
  87. DEBUG_OCPPMESSAGE_INFO("----- Handshake: Server response START -----\n");
  88. lws_hdr_copy(wsi, buf, ARRAY_SIZE(buf) - 1, WSI_TOKEN_HTTP);
  89. DEBUG_OCPPMESSAGE_INFO("HTTP/1.1 %s\n", buf);
  90. lws_hdr_copy(wsi, buf, ARRAY_SIZE(buf) - 1, WSI_TOKEN_UPGRADE);
  91. DEBUG_OCPPMESSAGE_INFO("Upgrade: %s\n", buf);
  92. lws_hdr_copy(wsi, buf, ARRAY_SIZE(buf) - 1, WSI_TOKEN_CONNECTION);
  93. DEBUG_OCPPMESSAGE_INFO("Connection: %s\n", buf);
  94. lws_hdr_copy(wsi, buf, ARRAY_SIZE(buf) - 1, WSI_TOKEN_ACCEPT);
  95. DEBUG_OCPPMESSAGE_INFO("Sec-WebSocket-Accept: %s\n", buf);
  96. lws_hdr_copy(wsi, buf, ARRAY_SIZE(buf) - 1, WSI_TOKEN_PROTOCOL);
  97. DEBUG_OCPPMESSAGE_INFO("Sec-WebSocket-Protocol: %s\n", buf);
  98. DEBUG_OCPPMESSAGE_INFO("----- Handshake: Server response END -----\n");
  99. break;
  100. case LWS_CALLBACK_FILTER_PROTOCOL_CONNECTION:
  101. DEBUG_INFO("LWS_CALLBACK_FILTER_PROTOCOL_CONNECTION\n");
  102. break;
  103. case LWS_CALLBACK_WSI_DESTROY:
  104. DEBUG_INFO("LWS_CALLBACK_WSI_DESTROY\n");
  105. pthread_detach(tid_connectServer);
  106. SetServerSign(FALSE);
  107. ConnectionEstablished = 0;
  108. context = NULL;
  109. break;
  110. case LWS_CALLBACK_LOCK_POLL:
  111. break;
  112. case LWS_CALLBACK_ADD_POLL_FD:
  113. DEBUG_INFO("LWS_CALLBACK_ADD_POLL_FD\n");
  114. break;
  115. case LWS_CALLBACK_DEL_POLL_FD:
  116. DEBUG_INFO("LWS_CALLBACK_DEL_POLL_FD\n");
  117. break;
  118. case LWS_CALLBACK_UNLOCK_POLL:
  119. break;
  120. case LWS_CALLBACK_CHANGE_MODE_POLL_FD:
  121. break;
  122. case LWS_CALLBACK_WSI_CREATE:
  123. DEBUG_INFO("LWS_CALLBACK_WSI_CREATE\n");
  124. break;
  125. case LWS_CALLBACK_GET_THREAD_ID:
  126. break;
  127. case LWS_CALLBACK_CLIENT_APPEND_HANDSHAKE_HEADER:
  128. DEBUG_INFO("LWS_CALLBACK_CLIENT_APPEND_HANDSHAKE_HEADER\n");
  129. unsigned char** pos = (unsigned char**)in;
  130. unsigned char* end = (*pos) + len;
  131. switch(GetOcppSecurityProfile())
  132. {
  133. case 1:
  134. case 2:
  135. case 3:
  136. GetOcppChargerBoxId(boxId);
  137. GetOcppSecurityPassword(password);
  138. sprintf(buf, "%s:%s", boxId, password);
  139. lws_b64_encode_string(buf, strlen(buf), (char*)auth_b64, ARRAY_SIZE(auth_b64));
  140. sprintf(buf, "Basic %s", auth_b64);
  141. if (lws_add_http_header_by_token(wsi, WSI_TOKEN_HTTP_AUTHORIZATION, (uint8_t *)buf, strlen(buf), pos, end))
  142. {
  143. DEBUG_ERROR("lws_add_http_header_by_token : WSI_TOKEN_HTTP_AUTHORIZATION\n");
  144. return -1;
  145. }
  146. DEBUG_OCPPMESSAGE_INFO("Authorization: %s\n", buf);
  147. break;
  148. case 0:
  149. default:
  150. break;
  151. }
  152. break;
  153. case LWS_CALLBACK_CLIENT_ESTABLISHED: //3
  154. DEBUG_INFO("LWS_CALLBACK_CLIENT_ESTABLISHED\n");
  155. char frontUUID[100] ={0};
  156. char frontData[QUEUE_MESSAGE_LENGTH] ={0};
  157. int queueNotEmpty = FALSE;
  158. //connected
  159. ConnectionEstablished=1;
  160. SetOcppConnStatus(TRUE);
  161. queueNotEmpty = queue_operation(QUEUE_OPERATION_SHOWFRONT,frontUUID, frontData);
  162. if(queueNotEmpty == TRUE)
  163. {
  164. OfflineTransaction = 1; // 0: no packets in queue. 1: There are packets in queue.
  165. }
  166. TransactionMessageResend = 1;
  167. //get offline number
  168. queue_operation(QUEUE_OPERATION_SHOWQUEUE,"","");
  169. OfflineTransactionQueueNum =TransactionQueueNum ;
  170. break;
  171. case LWS_CALLBACK_CLIENT_CONNECTION_ERROR://1
  172. DEBUG_ERROR("LWS_CALLBACK_CLIENT_CONNECTION_ERROR %s\n", (char *)in );
  173. //disconnected
  174. ConnectionEstablished=0;
  175. DEBUG_OCPPMESSAGE_INFO("===== Handshake: Client START =====\n");
  176. lws_hdr_copy(wsi, buf, ARRAY_SIZE(buf) - 1, _WSI_TOKEN_CLIENT_URI);
  177. DEBUG_OCPPMESSAGE_INFO("GET %s HTTP/1.1 \n", buf);
  178. lws_hdr_copy(wsi, buf, ARRAY_SIZE(buf) - 1, _WSI_TOKEN_CLIENT_HOST);
  179. DEBUG_OCPPMESSAGE_INFO("Host: %s\n", buf);
  180. DEBUG_OCPPMESSAGE_INFO("Upgrade: websocket\n");
  181. DEBUG_OCPPMESSAGE_INFO("Connection: Upgrade\n");
  182. lws_b64_encode_string(hash, 16, key_b64, ARRAY_SIZE(key_b64));// Sec-WebSocket-Key
  183. DEBUG_OCPPMESSAGE_INFO("Sec-WebSocket-Key: %s\n", key_b64);
  184. lws_hdr_copy(wsi, buf, ARRAY_SIZE(buf) - 1, _WSI_TOKEN_CLIENT_SENT_PROTOCOLS);
  185. DEBUG_OCPPMESSAGE_INFO("Sec-WebSocket-Protocol: %s\n", buf);
  186. DEBUG_OCPPMESSAGE_INFO("Sec-WebSocket-Version: %d\n", SPEC_LATEST_SUPPORTED);
  187. DEBUG_OCPPMESSAGE_INFO("===== Handshake: Client END =====\n");
  188. DEBUG_OCPPMESSAGE_INFO("===== Handshake: Server response START =====\n");
  189. lws_hdr_copy(wsi, buf, ARRAY_SIZE(buf) - 1, WSI_TOKEN_HTTP);
  190. DEBUG_OCPPMESSAGE_INFO("HTTP/1.1 %s\n", buf);
  191. lws_hdr_copy(wsi, buf, ARRAY_SIZE(buf) - 1, WSI_TOKEN_UPGRADE);
  192. DEBUG_OCPPMESSAGE_INFO("Upgrade: %s\n", buf);
  193. lws_hdr_copy(wsi, buf, ARRAY_SIZE(buf) - 1, WSI_TOKEN_CONNECTION);
  194. DEBUG_OCPPMESSAGE_INFO("Connection: %s\n", buf);
  195. lws_hdr_copy(wsi, buf, ARRAY_SIZE(buf) - 1, WSI_TOKEN_ACCEPT);
  196. DEBUG_OCPPMESSAGE_INFO("Sec-WebSocket-Accept: %s\n", buf);
  197. lws_hdr_copy(wsi, buf, ARRAY_SIZE(buf) - 1, WSI_TOKEN_PROTOCOL);
  198. DEBUG_OCPPMESSAGE_INFO("Sec-WebSocket-Protocol: %s\n", buf);
  199. DEBUG_OCPPMESSAGE_INFO("===== Handshake: Server response END =====\n");
  200. break;
  201. case LWS_CALLBACK_CLOSED://4
  202. DEBUG_INFO("LWS_CALLBACK_CLOSED\n");
  203. //disconnected
  204. ConnectionEstablished=0;
  205. break;
  206. case LWS_CALLBACK_CLIENT_WRITEABLE://10
  207. //if(need to send message and its relevant data already store into SendBuffer)
  208. SendData(wsi);
  209. //lws_rx_flow_control( wsi, 1 );
  210. break;
  211. case LWS_CALLBACK_CLIENT_RECEIVE://8
  212. ((char *)in)[len] = '\0';
  213. DEBUG_OCPPMESSAGE_INFO("<==== %s\n", (char *)in);
  214. //**********Receive Message**********/
  215. c = 0;
  216. loc = strstr((const char *)in, "][2,");
  217. if(loc == NULL)
  218. {
  219. loc = strstr((const char *)in, "][3,");
  220. if(loc == NULL)
  221. {
  222. loc = strstr((const char *)in, "][4,");
  223. }
  224. }
  225. memset(sstr, 0, ARRAY_SIZE(sstr) );
  226. if(loc != NULL)
  227. {
  228. DEBUG_INFO("There are continuous second packet []\n");
  229. while (loc[1+c] != '\0')
  230. {
  231. sstr[c] = loc[1+c];
  232. c++;
  233. }
  234. sstr[c] = '\0';
  235. strcpy(tempin, sstr);
  236. DEBUG_INFO("Final Receive: %s\n", tempin);
  237. }
  238. else
  239. {
  240. strcpy(tempin,(char *)in);
  241. }
  242. ReceivedMessage((void *)strtrim(tempin), strlen(tempin));
  243. isWebsocketSendable = 1;
  244. break;
  245. case LWS_CALLBACK_CLIENT_RECEIVE_PONG:
  246. DEBUG_INFO("LWS_CALLBACK_CLIENT_RECEIVE_PONG\n");
  247. DEBUG_OCPPMESSAGE_INFO("<==== Get PONG packet.\n");
  248. break;
  249. case LWS_CALLBACK_OPENSSL_PERFORM_SERVER_CERT_VERIFICATION:
  250. DEBUG_INFO("LWS_CALLBACK_OPENSSL_PERFORM_SERVER_CERT_VERIFICATION\n");
  251. break;
  252. case LWS_CALLBACK_OPENSSL_LOAD_EXTRA_CLIENT_VERIFY_CERTS:
  253. DEBUG_INFO("LWS_CALLBACK_OPENSSL_LOAD_EXTRA_CLIENT_VERIFY_CERTS\n");
  254. break;
  255. case LWS_CALLBACK_PROTOCOL_DESTROY:
  256. DEBUG_INFO("LWS_CALLBACK_PROTOCOL_DESTROY\n");
  257. break;
  258. case LWS_CALLBACK_RECEIVE_PONG:
  259. DEBUG_INFO("LWS_CALLBACK_RECEIVE_PONG\n");
  260. break;
  261. default:
  262. DEBUG_INFO("Reason = %d\n", reason);
  263. break;
  264. }
  265. return 0;
  266. }
  267. static struct lws_protocols protocols[] =
  268. {
  269. {
  270. "ocpp2.0",
  271. OCPP20Callback,
  272. 65536,//65536,//10240,
  273. 65536,//65536,//10240,
  274. },
  275. {
  276. "ocpp2.0",
  277. OCPP20Callback,
  278. 65536,//65536,//10240,
  279. 65536,//65536,//10240,
  280. },
  281. {
  282. NULL, NULL, 0 /* End of list */
  283. }
  284. };
  285. void* ConnectWsServer(void* data) //int ConnectWsServer()
  286. {
  287. struct lws_context_creation_info ContextInfo;
  288. struct lws_client_connect_info ConnInfo;
  289. int use_ssl=0;
  290. counterConnect += 1;
  291. // If internet available synchronize datetime with ntp server
  292. if(GetInternetConn() == 1)
  293. {
  294. system("pkill ntpd");
  295. DEBUG_INFO("NTP synchronize with Microsoft\n", system("/usr/sbin/ntpd -nqp time.windows.com &"));
  296. DEBUG_INFO("NTP synchronize with China\n", system("/usr/sbin/ntpd -nqp cn.ntp.org.cn &"));
  297. DEBUG_INFO("NTP synchronize with Taiwan\n", system("/usr/sbin/ntpd -nqp tock.stdtime.gov.tw &"));
  298. DEBUG_INFO("NTP synchronize with Europe\n", system("/usr/sbin/ntpd -nqp 0.europe.pool.ntp.org &"));
  299. }
  300. if(context!=NULL)
  301. {
  302. pthread_detach(pthread_self());
  303. lws_context_destroy(context);
  304. ConnectionEstablished=0;
  305. context = NULL;
  306. }
  307. checkNetworkProfile();
  308. memset(&ContextInfo, 0, sizeof(struct lws_context_creation_info));
  309. if((GetOcppServerURL()==0) || (GetOcppPort() == 0) || (GetOcppPath()==0))
  310. {
  311. DEBUG_ERROR("OCPP URL is NULL or OCPP Port is zero or OCPP Path is NULL\n");
  312. goto end;
  313. }
  314. if((strcmp(OcppProtocol,"ws")==0)&&(strlen(OcppProtocol)== 2))
  315. {
  316. DEBUG_INFO("Web socket is non-security mode.\n");
  317. use_ssl=0;
  318. }
  319. else if((strcmp(OcppProtocol,"wss")==0)&&(strlen(OcppProtocol)== 3))
  320. {
  321. DEBUG_INFO("Web socket is security mode.\n");
  322. use_ssl=1;
  323. }
  324. ContextInfo.port = CONTEXT_PORT_NO_LISTEN;
  325. ContextInfo.iface = NULL;
  326. ContextInfo.ssl_private_key_password = NULL;
  327. ContextInfo.ssl_cert_filepath = NULL;//"./ssl_key/client_cert.pem";
  328. ContextInfo.ssl_private_key_filepath = NULL;//"./ssl_key/client_key.pem";
  329. ContextInfo.ssl_ca_filepath = "/root/cacert.pem";//"./cacert.pem";
  330. ContextInfo.ssl_cipher_list = NULL; //use default one
  331. ContextInfo.gid = -1;
  332. ContextInfo.uid = -1;
  333. if(use_ssl)
  334. {
  335. ContextInfo.options |= LWS_SERVER_OPTION_DO_SSL_GLOBAL_INIT ;
  336. }
  337. ContextInfo.protocols = protocols;
  338. ContextInfo.timeout_secs = GetBackendConnectionTimeout();
  339. ContextInfo.ws_ping_pong_interval = GetWebSocketPingInterval();
  340. ContextInfo.ka_time = 20;
  341. ContextInfo.keepalive_timeout = 5;
  342. ContextInfo.ka_probes = 2;
  343. ContextInfo.ka_interval = 5;
  344. context = lws_create_context(&ContextInfo);
  345. if (context == NULL)
  346. {
  347. DEBUG_ERROR("lws_create_context NG");
  348. goto end;
  349. }
  350. memset(&ConnInfo,0,sizeof(struct lws_client_connect_info));
  351. // fill up below information
  352. ConnInfo.context = context;
  353. ConnInfo.address=(const char *)OcppHost;
  354. DEBUG_INFO("ConnInfo.address: %s\n", ConnInfo.address);
  355. ConnInfo.port = GetOcppPort();
  356. DEBUG_INFO("ConnInfo.port: %d\n", ConnInfo.port);
  357. ConnInfo.path=(const char *)OcppPath;
  358. DEBUG_INFO("ConnInfo.path: %s\n", ConnInfo.path);
  359. char addr_port[256] = { 0 };
  360. sprintf(addr_port, "%s:%u", ConnInfo.address, (ConnInfo.port & 65535) );
  361. ConnInfo.host= addr_port; // ConnInfo.address;//lws_canonical_hostname(context);
  362. //ConnInfo.origin="origin";
  363. ConnInfo.protocol = protocols[1].name;
  364. ConnInfo.ietf_version_or_minus_one = -1;
  365. if(use_ssl)
  366. {
  367. #ifdef TLS_VALID_CERT_EXPIRED
  368. ConnInfo.ssl_connection = LCCSCF_USE_SSL | LCCSCF_ALLOW_SELFSIGNED | LCCSCF_SKIP_SERVER_CERT_HOSTNAME_CHECK;
  369. DEBUG_INFO("TLS does not allow expired certification.\n");
  370. #else
  371. ConnInfo.ssl_connection = LCCSCF_USE_SSL | LCCSCF_ALLOW_SELFSIGNED | LCCSCF_SKIP_SERVER_CERT_HOSTNAME_CHECK | LCCSCF_ALLOW_EXPIRED;
  372. DEBUG_INFO("TLS allow expired certification.\n");
  373. #endif
  374. }
  375. wsi_client = lws_client_connect_via_info(&ConnInfo);
  376. if (!wsi_client)
  377. {
  378. DEBUG_ERROR("lws_client_connect_via_info NG\n");
  379. goto end;
  380. }
  381. counterConnect=0;
  382. DEBUG_INFO("counterConnect: %d\n", counterConnect);
  383. end:
  384. pthread_exit(NULL/*(void *) fname*/);
  385. }
  386. int isQueueOverSize()
  387. {
  388. FILE *fp;
  389. uint32_t file_size;
  390. uint8_t result = FALSE;
  391. fp = fopen("/Storage/OCPP/TransactionRelatedQueue" , "r");
  392. if(fp != NULL)
  393. {
  394. fseek(fp, 0L, SEEK_END);
  395. file_size = ftell(fp);
  396. if(file_size > (100*1024*1024))
  397. {
  398. result = TRUE;
  399. DEBUG_WARN("Queue file over size.\n");
  400. }
  401. fclose(fp);
  402. }
  403. return result;
  404. }
  405. int showfront(char *uuid, char *data)
  406. {
  407. FILE *fp;
  408. int result = FALSE; // 1: TRUE 0:FALSE
  409. char str[QUEUE_MESSAGE_LENGTH]={0};
  410. char sstr[50]={ 0 };//sstr[200]={ 0 };
  411. int c = 0;
  412. char *loc;
  413. char rmFileCmd[100]={0};
  414. struct stat stats;
  415. stat("/Storage/OCPP", &stats);
  416. // Check for directory existence
  417. if (S_ISDIR(stats.st_mode) == 1)
  418. {
  419. //DEBUG_INFO("\n OCPP directory exist \n");
  420. }
  421. else
  422. {
  423. DEBUG_INFO("\n OCPP directory not exist, create dir \n");
  424. sprintf(rmFileCmd,"mkdir -p %s","/Storage/OCPP");
  425. system(rmFileCmd);
  426. }
  427. memset(rmFileCmd, 0, ARRAY_SIZE(rmFileCmd));
  428. if((access("/Storage/OCPP/TransactionRelatedQueue",F_OK))!=-1)
  429. {
  430. //DEBUG_INFO("TransactionRelatedQueue exist.\n");
  431. }
  432. else
  433. {
  434. //DEBUG_INFO("TransactionRelatedQueue not exist\n");
  435. FILE *log = fopen("/Storage/OCPP/TransactionRelatedQueue", "w+");
  436. if(log == NULL)
  437. {
  438. DEBUG_INFO("Can't Create File TransactionRelatedQueue \n");
  439. return FALSE;
  440. }
  441. else
  442. {
  443. fclose(log);
  444. }
  445. }
  446. /* opening file for reading */
  447. fp = fopen("/Storage/OCPP/TransactionRelatedQueue" , "r");
  448. if(fp == NULL) {
  449. DEBUG_INFO("Error opening TransactionRelatedQueue file");
  450. return FALSE;
  451. }
  452. if( fgets (str, QUEUE_MESSAGE_LENGTH, fp)!=NULL ) {
  453. /* writing content to stdout */
  454. //DEBUG_INFO("str=%s",str);
  455. if ((str[0] == '\n')||(strcmp(str,"")==0))
  456. {
  457. DEBUG_INFO("It is a blank line");
  458. fclose(fp);
  459. memset(rmFileCmd, 0, ARRAY_SIZE(rmFileCmd));
  460. sprintf(rmFileCmd,"rm -f %s","/Storage/OCPP/TransactionRelatedQueue");
  461. system(rmFileCmd);
  462. result = FALSE;
  463. return result;
  464. }
  465. else
  466. {
  467. //puts(str);
  468. //----------------uuid--------------//
  469. loc = strstr(str, "\"");
  470. memset(sstr ,0, ARRAY_SIZE(sstr) );
  471. c = 0;
  472. while (loc[1+c] != '\"')
  473. {
  474. sstr[c] = loc[1+c];
  475. c++;
  476. }
  477. sstr[c] = '\0';
  478. //DEBUG_INFO("\n uuid:%s", sstr);
  479. //DEBUG_INFO("\n data:%s", str);
  480. strcpy(uuid,sstr);
  481. strcpy(data,str);
  482. result = TRUE;
  483. }
  484. }
  485. else
  486. {
  487. //DEBUG_INFO("queue is null\n");
  488. strcpy(uuid,"");
  489. strcpy(data,"");
  490. result = FALSE;
  491. }
  492. fclose(fp);
  493. return result;
  494. }
  495. int addq(char *uuid, char *data)
  496. {
  497. FILE *outfile;
  498. char rmFileCmd[100]={0};
  499. struct stat stats;
  500. stat("/Storage/OCPP", &stats);
  501. DEBUG_INFO("addq\n");
  502. // Check for directory existence
  503. if (S_ISDIR(stats.st_mode) == 1)
  504. {
  505. //DEBUG_INFO("\n OCPP directory exist \n");
  506. }
  507. else
  508. {
  509. //DEBUG_INFO("\n OCPP directory not exist, create dir \n");
  510. sprintf(rmFileCmd,"mkdir -p %s","/Storage/OCPP");
  511. system(rmFileCmd);
  512. }
  513. memset(rmFileCmd, 0, ARRAY_SIZE(rmFileCmd));
  514. if((access("/Storage/OCPP/TransactionRelatedQueue",F_OK))!=-1)
  515. {
  516. //DEBUG_INFO("TransactionRelatedQueue exist.\n");
  517. }
  518. else
  519. {
  520. //DEBUG_INFO("TransactionRelatedQueue not exist\n");
  521. FILE *log = fopen("/Storage/OCPP/TransactionRelatedQueue", "w+");
  522. if(log == NULL)
  523. {
  524. //DEBUG_INFO("Can't Create File TransactionRelatedQueue \n");
  525. return FALSE;
  526. }
  527. else
  528. {
  529. fclose(log);
  530. }
  531. }
  532. // open file for writing
  533. outfile = fopen ("/Storage/OCPP/TransactionRelatedQueue", "a");
  534. DEBUG_INFO("data = %s\n",data);
  535. fputs(data, outfile);
  536. fputs("\n", outfile);
  537. fclose (outfile);
  538. TransactionQueueNum = TransactionQueueNum + 1;
  539. if(OfflineTransaction == 1) // 0: no offline Transaction 1: offline Transaction
  540. {
  541. OfflineTransactionQueueNum = OfflineTransactionQueueNum + 1;
  542. }
  543. DEBUG_INFO("add queue end\n");
  544. system("/bin/fsync -d /dev/mtdblock13;/bin/sync &");
  545. return FALSE;
  546. }
  547. int delq()
  548. {
  549. char tempfile[] = "/Storage/OCPP/delqtemp.json";
  550. FILE *infile;
  551. FILE *outfile;
  552. int resultRename=0;
  553. char filename[60]={0};
  554. char rmFileCmd[100]={0};
  555. struct stat stats;
  556. stat("/Storage/OCPP", &stats);
  557. DEBUG_INFO("delq()\n");
  558. // Check for directory existence
  559. if (S_ISDIR(stats.st_mode) == 1)
  560. {
  561. //DEBUG_INFO("\n OCPP directory exist \n");
  562. }
  563. else
  564. {
  565. //DEBUG_INFO("\n OCPP directory not exist, create dir \n");
  566. sprintf(rmFileCmd,"mkdir -p %s","/Storage/OCPP");
  567. system(rmFileCmd);
  568. }
  569. memset(rmFileCmd, 0, ARRAY_SIZE(rmFileCmd));
  570. if((access("/Storage/OCPP/TransactionRelatedQueue",F_OK))!=-1)
  571. {
  572. //DEBUG_INFO("TransactionRelatedQueue exist.\n");
  573. }
  574. else
  575. {
  576. //DEBUG_INFO("TransactionRelatedQueue not exist\n");
  577. FILE *log = fopen("/Storage/OCPP/TransactionRelatedQueue", "w+");
  578. if(log == NULL)
  579. {
  580. //DEBUG_INFO("log is NULL\n");
  581. return 0;
  582. }
  583. else
  584. {
  585. fclose(log);
  586. }
  587. }
  588. // open file for writing
  589. strcpy(filename, "/Storage/OCPP/TransactionRelatedQueue");
  590. infile = fopen ("/Storage/OCPP/TransactionRelatedQueue", "r");
  591. outfile = fopen (tempfile, "w");
  592. /*检测到文件结束标识返回1,否则返回0。*/
  593. //DEBUG_INFO("feof(infile) =%d\n",feof(infile));
  594. int c;
  595. c = fgetc(infile);
  596. //printf("file c:%d\n",c);
  597. rewind(infile);
  598. if(c == EOF)
  599. {
  600. //DEBUG_INFO("TransactionRelatedQueue is NULL\n");
  601. fclose(infile);
  602. fclose(outfile);
  603. sprintf(rmFileCmd,"rm -f %s",tempfile);
  604. system(rmFileCmd);
  605. }
  606. else
  607. {
  608. char buf[QUEUE_MESSAGE_LENGTH]={0};
  609. int i = 0;
  610. //DEBUG_INFO("Orignal File is not NULL\n");
  611. while (fgets(buf, sizeof(buf), infile) != NULL)
  612. {
  613. //printf("Orignal File get strings \n");
  614. buf[strlen(buf) - 1] = '\0'; // eat the newline fgets() stores
  615. if(i==0)
  616. {
  617. TransactionQueueNum = TransactionQueueNum - 1;
  618. TransactionMessageResend = 1;
  619. DEBUG_INFO("delete the item\n");
  620. }
  621. if(i != 0)
  622. {
  623. fprintf(outfile,"%s\n", buf);
  624. }
  625. i = i + 1;
  626. }
  627. fclose(infile);
  628. fclose(outfile);
  629. sprintf(rmFileCmd,"rm -f %s",filename);
  630. system(rmFileCmd);
  631. resultRename = rename(tempfile, filename);
  632. if(resultRename == 0)
  633. {
  634. //DEBUG_INFO("TransactionRelatedQueue file renamed successfully");
  635. }
  636. else
  637. {
  638. //DEBUG_INFO("Error: unable to rename the TransactionRelatedQueue file");
  639. }
  640. DEBUG_INFO("delq() end\n");
  641. }
  642. system("/bin/fsync -d /dev/mtdblock13;/bin/sync &");
  643. return 0;
  644. }
  645. int showqueue()
  646. {
  647. char rmFileCmd[100]={0};
  648. struct stat stats;
  649. stat("/Storage/OCPP", &stats);
  650. // Check for directory existence
  651. if (S_ISDIR(stats.st_mode) == 1)
  652. {
  653. //DEBUG_INFO("\n OCPP directory exist \n");
  654. }
  655. else
  656. {
  657. //DEBUG_INFO("\n OCPP directory not exist, create dir \n");
  658. sprintf(rmFileCmd,"mkdir -p %s","/Storage/OCPP");
  659. system(rmFileCmd);
  660. }
  661. memset(rmFileCmd, 0, ARRAY_SIZE(rmFileCmd));
  662. if((access("/Storage/OCPP/TransactionRelatedQueue",F_OK))!=-1)
  663. {
  664. //DEBUG_INFO("TransactionRelatedQueue exist.\n");
  665. }
  666. else
  667. {
  668. //DEBUG_INFO("TransactionRelatedQueue not exist\n");
  669. FILE *log = fopen("/Storage/OCPP/TransactionRelatedQueue", "w+");
  670. if(log == NULL)
  671. {
  672. DEBUG_INFO("log is NULL\n");
  673. return FALSE;
  674. }
  675. else
  676. {
  677. fclose(log);
  678. }
  679. }
  680. FILE *fp = fopen("/Storage/OCPP/TransactionRelatedQueue", "r");
  681. char line[QUEUE_MESSAGE_LENGTH]={0};
  682. // check if file exist (and you can open it) or not
  683. if (fp == NULL) {
  684. DEBUG_INFO("can open file TransactionRelatedQueue!");
  685. return FALSE;
  686. }
  687. TransactionQueueNum = 0; // the number of packets in queue
  688. while(fgets(line, sizeof line, fp) != NULL) {
  689. //DEBUG_INFO("%s\n", line);
  690. TransactionQueueNum = TransactionQueueNum + 1; //the number of packets in queue
  691. }
  692. fclose(fp);
  693. return TRUE;
  694. }
  695. int sentqueue(){
  696. FILE *fp;
  697. int result = FALSE; // 1: TRUE 0:FALSE
  698. char str[QUEUE_MESSAGE_LENGTH]={0};
  699. char cmdBuf[100]={0};
  700. struct stat stats;
  701. json_object *queueJson;
  702. DEBUG_INFO("Sent queue.\n");
  703. stat("/Storage/OCPP", &stats);
  704. // Check for directory existence
  705. if (S_ISDIR(stats.st_mode) == 1)
  706. {
  707. //DEBUG_INFO("\n OCPP directory exist \n");
  708. }
  709. else
  710. {
  711. //DEBUG_INFO("\n OCPP directory not exist, create dir \n");
  712. sprintf(cmdBuf,"mkdir -p %s","/Storage/OCPP");
  713. system(cmdBuf);
  714. }
  715. /* opening file for reading */
  716. fp = fopen("/Storage/OCPP/TransactionRelatedQueue" , "r");
  717. if(fp == NULL)
  718. {
  719. DEBUG_ERROR("Error opening file");
  720. return FALSE;
  721. }
  722. if( fgets (str, QUEUE_MESSAGE_LENGTH, fp)!=NULL )
  723. {
  724. queueJson = json_tokener_parse(str);
  725. if(!is_error(queueJson))
  726. {
  727. LWS_Send(str);
  728. }
  729. json_object_put(queueJson);
  730. result = TRUE;
  731. }
  732. else
  733. {
  734. result = FALSE;
  735. }
  736. fclose(fp);
  737. return result;
  738. }
  739. void* processTransactionQueue(void* data)
  740. {
  741. char frontUUID[100] ={0};
  742. char frontData[QUEUE_MESSAGE_LENGTH/*1024*4*/] ={0};
  743. int queueNotEmpty = FALSE;
  744. while(1)
  745. {
  746. if(!req_SendQueue && ((((time((time_t*)NULL) - startTimeQueue) > (TransactionMessageRetryIntervalGet()>10?TransactionMessageRetryIntervalGet():10))) || (isWebsocketSendable && ((time((time_t*)NULL) - startTimeQueue) >= ((counterQueueSent>=20)?5:1)))))
  747. {
  748. if(FirstHeartBeatResponse() == 1)
  749. {
  750. memset(frontUUID, 0, ARRAY_SIZE(frontUUID));
  751. memset(frontData, 0, ARRAY_SIZE(frontData));
  752. queueNotEmpty = FALSE;
  753. queueNotEmpty = queue_operation(QUEUE_OPERATION_SHOWFRONT,frontUUID, frontData);//showfront(frontUUID, frontData); ---> remove temporally
  754. if((queueNotEmpty == TRUE) && (GetOcppConnStatus() == 1)) //OcppConnStatus 0: disconnected, 1: connected
  755. {
  756. if(isWebsocketSendable)
  757. DEBUG_INFO("isWebsocketSendable on.\n");
  758. if((((time((time_t*)NULL) - startTimeQueue) > (TransactionMessageRetryIntervalGet()>10?TransactionMessageRetryIntervalGet():10))))
  759. DEBUG_INFO("Queue timer(%d) over spec(%d).\n", (time((time_t*)NULL) - startTimeQueue), TransactionMessageRetryIntervalGet());
  760. if((OfflineTransaction == 1) && (OfflineTransactionQueueNum != 0)) //OfflineTransaction 0: no offline Transaction 1: offline Transaction
  761. {
  762. DEBUG_INFO("Sent message from queue request off-line first.\n");
  763. req_SendQueue = 1; // 0: no packets to send 1: send the top packet in queue
  764. OfflineTransactionQueueNum = OfflineTransactionQueueNum - 1;
  765. if(OfflineTransactionQueueNum == 0)
  766. {
  767. OfflineTransaction = 0;
  768. }
  769. }
  770. else
  771. {
  772. if(TransactionMessageResend <= TransactionMessageAttemptsGet()) //
  773. {
  774. DEBUG_INFO("Sent message from queue request.\n");
  775. DEBUG_INFO("TransactionMessageResend = %d\n",TransactionMessageResend);
  776. req_SendQueue = 1;
  777. TransactionMessageResend += 1;
  778. }
  779. else
  780. {
  781. DEBUG_INFO("Transaction message resend(%d) over spec(%d) message abandon.\n", TransactionMessageResend, TransactionMessageAttemptsGet());
  782. queue_operation(QUEUE_OPERATION_DEL,"",""); //// delete item
  783. TransactionMessageResend = 1;
  784. }
  785. }
  786. }
  787. }
  788. if(GetOcppConnStatus() == 0)
  789. {
  790. if(queueNotEmpty == TRUE)
  791. {
  792. OfflineTransaction = 1; // 0: no offline Transaction 1: offline Transaction
  793. }
  794. }
  795. // Refresh queue timer
  796. startTimeQueue = time((time_t*)NULL);
  797. if((counterQueueSent >= 10) || (queueNotEmpty == FALSE))
  798. {
  799. counterQueueSent = 0;
  800. }
  801. else
  802. {
  803. counterQueueSent += 1;
  804. }
  805. }
  806. usleep(500000);
  807. }
  808. pthread_exit(NULL); //
  809. return 0;
  810. }
  811. void* processWatchdog()
  812. {
  813. for(;;)
  814. {
  815. if(((time((time_t*)NULL) - startTimeDog) > 10) && (context != NULL))
  816. {
  817. DEBUG_INFO("LWS watch dog timeout.\n");
  818. lws_cancel_service(context);
  819. lws_cancel_service_pt(wsi_client);
  820. if(counterLwsRestart >= 2)
  821. {
  822. DEBUG_INFO("LWS watch dog timeout over 3 count.\n");
  823. system("pkill OcppBackend");
  824. }
  825. else
  826. counterLwsRestart++;
  827. startTimeDog = time((time_t*)NULL);
  828. }
  829. if(counterConnect >= 2)
  830. {
  831. DEBUG_INFO("Connect OCPP server timeout over 3 count.\n");
  832. system("pkill OcppBackend");
  833. }
  834. /*
  835. if(system("pidof -s Module_PhBackend > /dev/null") != 0)
  836. {
  837. DEBUG_INFO("Module_PhBackend not running, restart it.\r\n");
  838. system("/root/Module_PhBackend &");
  839. }*/
  840. sleep(1);
  841. }
  842. pthread_exit(NULL); //
  843. }
  844. void CheckTransactionPacket(char *uuid)
  845. {
  846. char frontUUID[100]={0};
  847. char frontData[QUEUE_MESSAGE_LENGTH]={0};
  848. int queueNotEmpty = FALSE;
  849. int cmpResult = 0;
  850. queueNotEmpty = queue_operation(QUEUE_OPERATION_SHOWFRONT,frontUUID, frontData);//showfront(frontUUID, frontData); ---> remove temporally
  851. if(queueNotEmpty == TRUE)
  852. {
  853. cmpResult = strcmp(frontUUID, uuid);
  854. if (cmpResult == 0)
  855. {
  856. DEBUG_INFO("Receive queue response match.\n");
  857. queue_operation(QUEUE_OPERATION_DEL,"","");//delq(); ---> remove temporally
  858. TransactionMessageResend = 1;
  859. }
  860. else
  861. DEBUG_INFO("Receive queue response mismatch.\n");
  862. }
  863. }
  864. int queue_operation(int type, char *frontUUID, char *frontData)
  865. {
  866. int result=0;
  867. while(1)
  868. {
  869. if (!IsUsing )
  870. {
  871. IsUsing = TRUE;
  872. if(type == QUEUE_OPERATION_SHOWQUEUE) // show items in queue
  873. {
  874. result = showqueue();
  875. }
  876. else if(type == QUEUE_OPERATION_SHOWFRONT) // show first item
  877. {
  878. result = showfront(frontUUID, frontData);
  879. }
  880. else if(type == QUEUE_OPERATION_DEL) // delete item
  881. {
  882. result = delq();
  883. }
  884. else if(type == QUEUE_OPERATION_SENT) // sent items in queue
  885. {
  886. result = sentqueue();
  887. }
  888. else if(type == QUEUE_OPERATION_ADD) // add items to the queue
  889. {
  890. // If queue file over size only add start * stop transaction message
  891. if(!isQueueOverSize() || (strstr(frontData, "MeterValues") == NULL))
  892. {
  893. result = addq(frontUUID, frontData);
  894. }
  895. }
  896. IsUsing = FALSE;
  897. break;
  898. }
  899. usleep(100000);
  900. }
  901. return result;
  902. }
  903. int removeMessageSentFile(void)
  904. {
  905. char rmFileCmd[100]={0};
  906. struct stat stats;
  907. stat("/Storage/OCPP", &stats);
  908. // Check for directory existence
  909. if(S_ISDIR(stats.st_mode) == 1)
  910. {
  911. //DEBUG_INFO("\n OCPP directory exist \n");
  912. }
  913. else
  914. {
  915. DEBUG_INFO("\n directory not exist, create dir \n");
  916. sprintf(rmFileCmd,"mkdir -p %s","/Storage/OCPP");
  917. system(rmFileCmd);
  918. }
  919. memset(rmFileCmd, 0, ARRAY_SIZE(rmFileCmd));
  920. if((access("/Storage/OCPP/MessageSent",F_OK))!=-1)
  921. {
  922. DEBUG_INFO("MessageSent file exist.\n");
  923. sprintf(rmFileCmd,"rm -f %s","/Storage/OCPP/MessageSent");
  924. system(rmFileCmd);
  925. }
  926. memset(rmFileCmd, 0, ARRAY_SIZE(rmFileCmd));
  927. return 0;
  928. }
  929. static int changeChageWebSocketPingInterval = FALSE;
  930. void ChageWebSocketPingInterval(int WebSocketPingInterval)
  931. {
  932. changeChageWebSocketPingInterval = TRUE;
  933. }
  934. //================================================
  935. // Main process
  936. //================================================
  937. int main(void)
  938. {
  939. char rmFileCmd[100]={0};
  940. struct stat stats;
  941. DEBUG_INFO("Module_OcppBackend20 task initialization...\n");
  942. //lws_set_log_level(LLL_PARSER | LLL_HEADER | LLL_ERR | LLL_WARN | LLL_NOTICE | LLL_INFO | LLL_DEBUG | LLL_EXT | LLL_CLIENT | LLL_LATENCY , NULL);
  943. if(ProcessShareMemory()== FAIL)
  944. {
  945. return FAIL;
  946. }
  947. // Check & create OCPP dir
  948. stat("/Storage/OCPP", &stats);
  949. if(S_ISDIR(stats.st_mode) != 1)
  950. {
  951. DEBUG_INFO("OCPP directory not exist, create dir \n");
  952. sprintf(rmFileCmd,"mkdir -p /Storage/OCPP");
  953. system(rmFileCmd);
  954. }
  955. memset(rmFileCmd, 0, ARRAY_SIZE(rmFileCmd));
  956. // Create Process: Resend Transaction
  957. pthread_create(&tid_ProcQueue, NULL, processTransactionQueue, NULL);
  958. pthread_create(&tid_Watchdog, NULL, processWatchdog, NULL);
  959. // Sqlite3 initial
  960. if(DB_Initial() != PASS)
  961. {
  962. DEBUG_ERROR("OCPP 2.0 local database initial fail.\n");
  963. return 0;
  964. }
  965. initialConfigurationTable();
  966. removeMessageSentFile();
  967. for(;;)
  968. {
  969. startTimeDog = time((time_t*)NULL);
  970. counterLwsRestart = 0;
  971. // Connect server
  972. if(ConnectionEstablished==0) // Check InternetConn 0: disconnected, 1: connected
  973. {
  974. isWebsocketSendable = 1;
  975. SetOcppConnStatus(FALSE);
  976. SetServerSign(FALSE);
  977. InitialSystemValue();
  978. if((time((time_t*)NULL)-startTime.connect) >= (GetWebSocketPingInterval()>=30?GetWebSocketPingInterval()+5:30))
  979. {
  980. DEBUG_INFO("Server connecting...\n");
  981. pthread_create(&tid_connectServer, NULL, ConnectWsServer, NULL);
  982. startTime.connect=time((time_t*)NULL);
  983. }
  984. CheckSystemValue();
  985. }
  986. else
  987. {
  988. // Sign in
  989. if((GetServerSign() == FALSE) &&
  990. (isConnectorInitMode(0) != TRUE) &&
  991. ( (GetBootNotificationInterval()>0) ? ((time((time_t*)NULL)-startTime.bootNotification) >= GetBootNotificationInterval()) : ((time((time_t*)NULL)-startTime.bootNotification) >= 10) )
  992. )
  993. {
  994. sendBootNotificationRequest();
  995. startTime.bootNotification=time((time_t*)NULL);
  996. }
  997. // On line operation
  998. if(GetServerSign() == TRUE)
  999. {
  1000. // Send message from queue
  1001. if((req_SendQueue == 1) && isWebsocketSendable)
  1002. {
  1003. queue_operation(QUEUE_OPERATION_SENT, "", "");
  1004. req_SendQueue = 0;
  1005. }
  1006. // Check System Value
  1007. CheckSystemValue();
  1008. if(GetHeartBeatWithNOResponse() >= 3)
  1009. {
  1010. lws_context_destroy(context);
  1011. ConnectionEstablished = 0;
  1012. context = NULL;
  1013. SetHeartBeatWithNOResponse();
  1014. }
  1015. if((changeChageWebSocketPingInterval == TRUE) || (GetOcppConnStatus() == 0))
  1016. {
  1017. DEBUG_INFO("GetOcppConnStatus() = %d\n", GetOcppConnStatus());
  1018. changeChageWebSocketPingInterval = FALSE;
  1019. lws_context_destroy(context);
  1020. ConnectionEstablished = 0;
  1021. context = NULL;
  1022. }
  1023. }
  1024. }
  1025. do
  1026. {
  1027. lws_service(context, 0);//timeout_ms
  1028. }while((SendBufLen>0) && (context!=NULL) && GetInternetConn());
  1029. refreshProcDogTimer();
  1030. usleep(100000);
  1031. }
  1032. pthread_join(tid_ProcQueue, NULL);
  1033. pthread_join(tid_Watchdog, NULL);
  1034. return FAIL;
  1035. }