Module_OcppBackend20.c 32 KB

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