Module_OcppBackend.c 46 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594
  1. #include "Module_OcppBackend.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. struct timespec connect;
  12. struct timespec bootNotification;
  13. struct timespec reConnect;
  14. struct timespec disconnectServer;
  15. struct timespec startTimeQueue;
  16. struct timespec pingOn;
  17. }startTime;
  18. struct QueueOpInfo queueOpInfo;
  19. //==========================================
  20. // Function prototype
  21. //==========================================
  22. void ReceivedMessage(void *in, size_t len);
  23. int SendBufLen=0;//(1024*4);//(1024*3);
  24. unsigned char SendBuffer[WEBSOCKET_BUFFER_SIZE]={0};
  25. static int ConnectionEstablished=0;
  26. int defaultWaitingTime = 10; //10 second
  27. char OcppPath[384]={0};
  28. char OcppProtocol[10]={0},OcppHost[128]={0}, OcppTempPath[256]={0};
  29. int OcppPort=0;
  30. unsigned char StartTransactionIdTagTemp[20]={0};
  31. int LastQueuedTransactionId[8] = {0};
  32. uint8_t isWebsocketSendable = 1;
  33. uint8_t isQueueSendable = 1;
  34. uint8_t counterQueueSent = 0;
  35. uint8_t counterConnect = 0;
  36. uint8_t counterPingSend = 0;
  37. sqlite3 *db;
  38. char *errMsg = NULL;
  39. static char *createsql = "CREATE TABLE IF NOT EXISTS log_buffer("
  40. "idx integer primary key,"
  41. "user_id text,"
  42. "cmd_sn text,"
  43. "charger_id text,"
  44. "gun_type text,"
  45. "gun_no text,"
  46. "rfid_no text,"
  47. "stime text,"
  48. "etime text,"
  49. "time_len text,"
  50. "s_soc text,"
  51. "e_soc text,"
  52. "stop_reason text,"
  53. "power text,"
  54. "meter_before text,"
  55. "meter_after text,"
  56. "charge_price text,"
  57. "reserve text,"
  58. "surplus_before text,"
  59. "surplus_after text,"
  60. "service_price text,"
  61. "is_pay text,"
  62. "charge_strategy text,"
  63. "charge_parameter text,"
  64. "vin text,"
  65. "vehicle_no text,"
  66. "start_method text,"
  67. "card_type text,"
  68. "is_upload text,"
  69. "guid text UNIQUE,"
  70. "is_buf2OK text);";
  71. static char *sqlOcppAuthCache = "create table if not exists ocpp_auth_cache (idx integer primary key,"
  72. "idtag text UNIQUE,"
  73. "parent_idtag text,"
  74. "expir_date text,"
  75. "status text);";
  76. static char *sqlOcppAuthLocal = "create table if not exists ocpp_auth_local (idx integer primary key,"
  77. "idtag text UNIQUE,"
  78. "parent_idtag text,"
  79. "expir_date text,"
  80. "status text,"
  81. "version text);";
  82. static char *sqlTransaction = "create table if not exists ocpp_transaction_record (idx integer primary key,"
  83. "occurDatetime text,"
  84. "message_type text,"
  85. "message_content text);";
  86. static char *sqlPeriodEnergy = "create table if not exists ocpp_period_energy ( transactionId integer primary key UNIQUE,"
  87. "connectorId integer,"
  88. "occurDatetime text,"
  89. "periodEnergy text);";
  90. static char *sqlReportDeduct = "create table if not exists report_deduct_info ( idx integer primary key,"
  91. "txId integer UNIQUE,"
  92. "creditNo text,"
  93. "deductResult text,"
  94. "isDonateInvoice text,"
  95. "amount text,"
  96. "vemData text,"
  97. "ROC text,"
  98. "RRN text,"
  99. "approvalNo text,"
  100. "storeId text,"
  101. "isUploaded text);";
  102. static char *sqlReportOccupancyDeduct = "create table if not exists report_occupancyDeduct_info ( idx integer primary key,"
  103. "occupancySN text,"
  104. "creditNo text,"
  105. "deductResult text,"
  106. "amount text,"
  107. "approvalNo text,"
  108. "isUploaded text);";
  109. static char *sqlMeterValueBuffer = "create table if not exists meterValuesBuffer( idx integer primary key,"
  110. "occurDatetime text,"
  111. "gun_index text,"
  112. "transactionId text,"
  113. "ReadingContext text,"
  114. "meterValue text);";
  115. //=================================
  116. // Common routine
  117. //=================================
  118. int GetTransactionQueueNum(void)
  119. {
  120. return queueOpInfo.TransactionQueueNum;
  121. }
  122. //==========================================
  123. // Web socket tranceive routine
  124. //==========================================
  125. int SendData(struct lws *wsi)
  126. {
  127. int n;
  128. int len;
  129. unsigned char out[LWS_SEND_BUFFER_PRE_PADDING + ARRAY_SIZE(SendBuffer) + LWS_SEND_BUFFER_POST_PADDING] = {0};
  130. len = strlen((char *)SendBuffer);
  131. if(len == 0)return 0;
  132. if((strstr((char*)SendBuffer, "\"MeterValues\"") != NULL)
  133. || (strstr((char*)SendBuffer, "\"StartTransaction\"") != NULL)
  134. || (strstr((char*)SendBuffer, "\"StopTransaction\"") != NULL))
  135. {
  136. isQueueSendable = 0;
  137. }
  138. memcpy (out + LWS_SEND_BUFFER_PRE_PADDING, SendBuffer, len );
  139. DEBUG_OCPPMESSAGE_INFO("===========> %s\n", out + LWS_SEND_BUFFER_PRE_PADDING);
  140. n = lws_write(wsi, out + LWS_SEND_BUFFER_PRE_PADDING, len, LWS_WRITE_TEXT);
  141. memset(SendBuffer, 0, len);
  142. SendBufLen = 0;
  143. return n;
  144. }
  145. int SendPing(struct lws *wsi)
  146. {
  147. uint8_t ping[LWS_PRE + 125];
  148. DEBUG_OCPPMESSAGE_INFO("===========> Set PING packet.\n");
  149. return lws_write(wsi, ping + LWS_PRE, 0, LWS_WRITE_PING);
  150. }
  151. static int OCPP16Callback(struct lws *wsi, enum lws_callback_reasons reason, void *user, void *in, size_t len)
  152. {
  153. char buf[256]={0}, hash[20]={0}, key_b64[40]={0}, tempin[WEBSOCKET_BUFFER_SIZE]={0}, sstr[WEBSOCKET_BUFFER_SIZE]={0};
  154. uint8_t auth_b64[256]={0}, boxId[128]={0}, password[64]={0};
  155. int c = 0;
  156. char *loc;
  157. switch (reason)
  158. {
  159. case LWS_CALLBACK_PROTOCOL_INIT:
  160. DEBUG_INFO("LWS_CALLBACK_PROTOCOL_INIT\n");
  161. break;
  162. case LWS_CALLBACK_CLIENT_FILTER_PRE_ESTABLISH:
  163. DEBUG_INFO("LWS_CALLBACK_CLIENT_FILTER_PRE_ESTABLISH\n");
  164. DEBUG_OCPPMESSAGE_INFO("----- Handshake: Client Request START -----\n");
  165. lws_hdr_copy(wsi, buf, ARRAY_SIZE(buf) - 1, _WSI_TOKEN_CLIENT_URI);
  166. DEBUG_OCPPMESSAGE_INFO("GET %s HTTP/1.1 \n", buf);
  167. lws_hdr_copy(wsi, buf, ARRAY_SIZE(buf) - 1, _WSI_TOKEN_CLIENT_HOST);
  168. DEBUG_OCPPMESSAGE_INFO("Host: %s\n", buf);
  169. DEBUG_OCPPMESSAGE_INFO("Upgrade: websocket\n");
  170. DEBUG_OCPPMESSAGE_INFO("Connection: Upgrade\n");
  171. lws_b64_encode_string(hash, 16, key_b64, ARRAY_SIZE(key_b64));// Sec-WebSocket-Key
  172. DEBUG_OCPPMESSAGE_INFO("Sec-WebSocket-Key: %s\n", key_b64);
  173. lws_hdr_copy(wsi, buf, ARRAY_SIZE(buf) - 1, _WSI_TOKEN_CLIENT_SENT_PROTOCOLS);
  174. DEBUG_OCPPMESSAGE_INFO("Sec-WebSocket-Protocol: %s\n", buf);
  175. DEBUG_OCPPMESSAGE_INFO("Sec-WebSocket-Version: %d\n", SPEC_LATEST_SUPPORTED);
  176. DEBUG_OCPPMESSAGE_INFO("----- Handshake: Client Request END -----\n");
  177. DEBUG_OCPPMESSAGE_INFO("----- Handshake: Server response START -----\n");
  178. lws_hdr_copy(wsi, buf, ARRAY_SIZE(buf) - 1, WSI_TOKEN_HTTP);
  179. DEBUG_OCPPMESSAGE_INFO("HTTP/1.1 %s\n", buf);
  180. lws_hdr_copy(wsi, buf, ARRAY_SIZE(buf) - 1, WSI_TOKEN_UPGRADE);
  181. DEBUG_OCPPMESSAGE_INFO("Upgrade: %s\n", buf);
  182. lws_hdr_copy(wsi, buf, ARRAY_SIZE(buf) - 1, WSI_TOKEN_CONNECTION);
  183. DEBUG_OCPPMESSAGE_INFO("Connection: %s\n", buf);
  184. lws_hdr_copy(wsi, buf, ARRAY_SIZE(buf) - 1, WSI_TOKEN_ACCEPT);
  185. DEBUG_OCPPMESSAGE_INFO("Sec-WebSocket-Accept: %s\n", buf);
  186. lws_hdr_copy(wsi, buf, ARRAY_SIZE(buf) - 1, WSI_TOKEN_PROTOCOL);
  187. DEBUG_OCPPMESSAGE_INFO("Sec-WebSocket-Protocol: %s\n", buf);
  188. DEBUG_OCPPMESSAGE_INFO("----- Handshake: Server response END -----\n");
  189. SetOcppVersion((uint8_t*)buf);
  190. break;
  191. case LWS_CALLBACK_FILTER_PROTOCOL_CONNECTION:
  192. DEBUG_INFO("LWS_CALLBACK_FILTER_PROTOCOL_CONNECTION\n");
  193. break;
  194. case LWS_CALLBACK_WSI_DESTROY:
  195. DEBUG_INFO("LWS_CALLBACK_WSI_DESTROY\n");
  196. pthread_detach(tid_connectServer);
  197. SetServerSign(FALSE);
  198. ConnectionEstablished = 0;
  199. context = NULL;
  200. break;
  201. case LWS_CALLBACK_LOCK_POLL:
  202. break;
  203. case LWS_CALLBACK_ADD_POLL_FD:
  204. DEBUG_INFO("LWS_CALLBACK_ADD_POLL_FD\n");
  205. break;
  206. case LWS_CALLBACK_DEL_POLL_FD:
  207. DEBUG_INFO("LWS_CALLBACK_DEL_POLL_FD\n");
  208. break;
  209. case LWS_CALLBACK_UNLOCK_POLL:
  210. break;
  211. case LWS_CALLBACK_CHANGE_MODE_POLL_FD:
  212. break;
  213. case LWS_CALLBACK_WSI_CREATE:
  214. DEBUG_INFO("LWS_CALLBACK_WSI_CREATE\n");
  215. break;
  216. case LWS_CALLBACK_GET_THREAD_ID:
  217. break;
  218. case LWS_CALLBACK_CLIENT_APPEND_HANDSHAKE_HEADER:
  219. DEBUG_INFO("LWS_CALLBACK_CLIENT_APPEND_HANDSHAKE_HEADER\n");
  220. unsigned char** pos = (unsigned char**)in;
  221. unsigned char* end = (*pos) + len;
  222. switch(GetOcppSecurityProfile())
  223. {
  224. case 1:
  225. case 2:
  226. case 3:
  227. GetOcppChargerBoxId(boxId);
  228. GetOcppSecurityPassword(password);
  229. sprintf(buf, "%s:%s", boxId, password);
  230. lws_b64_encode_string(buf, strlen(buf), (char*)auth_b64, ARRAY_SIZE(auth_b64));
  231. sprintf(buf, "Basic %s", auth_b64);
  232. if (lws_add_http_header_by_token(wsi, WSI_TOKEN_HTTP_AUTHORIZATION, (uint8_t *)buf, strlen(buf), pos, end))
  233. {
  234. DEBUG_ERROR("lws_add_http_header_by_token : WSI_TOKEN_HTTP_AUTHORIZATION\n");
  235. return -1;
  236. }
  237. DEBUG_OCPPMESSAGE_INFO("SecurityProfile: %d\n", GetOcppSecurityProfile());
  238. DEBUG_OCPPMESSAGE_INFO("Password: %s\n", password);
  239. DEBUG_OCPPMESSAGE_INFO("Authorization: %s\n", buf);
  240. break;
  241. case 0:
  242. default:
  243. break;
  244. }
  245. break;
  246. case LWS_CALLBACK_CLIENT_ESTABLISHED: //3
  247. DEBUG_INFO("LWS_CALLBACK_CLIENT_ESTABLISHED\n");
  248. //connected
  249. ConnectionEstablished=1;
  250. SetOcppConnStatus(TRUE);
  251. refreshStartTimer(&startTime.pingOn);
  252. counterPingSend = 0;
  253. queueOpInfo.TransactionMessageResend = 0;
  254. break;
  255. case LWS_CALLBACK_CLIENT_CONNECTION_ERROR://1
  256. DEBUG_ERROR("LWS_CALLBACK_CLIENT_CONNECTION_ERROR %s\n", (char *)in );
  257. //disconnected
  258. ConnectionEstablished=0;
  259. DEBUG_OCPPMESSAGE_INFO("===== Handshake: Client START =====\n");
  260. lws_hdr_copy(wsi, buf, ARRAY_SIZE(buf) - 1, _WSI_TOKEN_CLIENT_URI);
  261. DEBUG_OCPPMESSAGE_INFO("GET %s HTTP/1.1 \n", buf);
  262. lws_hdr_copy(wsi, buf, ARRAY_SIZE(buf) - 1, _WSI_TOKEN_CLIENT_HOST);
  263. DEBUG_OCPPMESSAGE_INFO("Host: %s\n", buf);
  264. DEBUG_OCPPMESSAGE_INFO("Upgrade: websocket\n");
  265. DEBUG_OCPPMESSAGE_INFO("Connection: Upgrade\n");
  266. lws_b64_encode_string(hash, 16, key_b64, ARRAY_SIZE(key_b64));// Sec-WebSocket-Key
  267. DEBUG_OCPPMESSAGE_INFO("Sec-WebSocket-Key: %s\n", key_b64);
  268. lws_hdr_copy(wsi, buf, ARRAY_SIZE(buf) - 1, _WSI_TOKEN_CLIENT_SENT_PROTOCOLS);
  269. DEBUG_OCPPMESSAGE_INFO("Sec-WebSocket-Protocol: %s\n", buf);
  270. DEBUG_OCPPMESSAGE_INFO("Sec-WebSocket-Version: %d\n", SPEC_LATEST_SUPPORTED);
  271. DEBUG_OCPPMESSAGE_INFO("===== Handshake: Client END =====\n");
  272. DEBUG_OCPPMESSAGE_INFO("===== Handshake: Server response START =====\n");
  273. lws_hdr_copy(wsi, buf, ARRAY_SIZE(buf) - 1, WSI_TOKEN_HTTP);
  274. DEBUG_OCPPMESSAGE_INFO("HTTP/1.1 %s\n", buf);
  275. lws_hdr_copy(wsi, buf, ARRAY_SIZE(buf) - 1, WSI_TOKEN_UPGRADE);
  276. DEBUG_OCPPMESSAGE_INFO("Upgrade: %s\n", buf);
  277. lws_hdr_copy(wsi, buf, ARRAY_SIZE(buf) - 1, WSI_TOKEN_CONNECTION);
  278. DEBUG_OCPPMESSAGE_INFO("Connection: %s\n", buf);
  279. lws_hdr_copy(wsi, buf, ARRAY_SIZE(buf) - 1, WSI_TOKEN_ACCEPT);
  280. DEBUG_OCPPMESSAGE_INFO("Sec-WebSocket-Accept: %s\n", buf);
  281. lws_hdr_copy(wsi, buf, ARRAY_SIZE(buf) - 1, WSI_TOKEN_PROTOCOL);
  282. DEBUG_OCPPMESSAGE_INFO("Sec-WebSocket-Protocol: %s\n", buf);
  283. DEBUG_OCPPMESSAGE_INFO("===== Handshake: Server response END =====\n");
  284. break;
  285. case LWS_CALLBACK_CLOSED://4
  286. DEBUG_INFO("LWS_CALLBACK_CLOSED\n");
  287. //disconnected
  288. ConnectionEstablished=0;
  289. break;
  290. case LWS_CALLBACK_CLIENT_WRITEABLE://10
  291. if(isWebsocketSendable && (0 < GetWebSocketPingInterval()) && (GetWebSocketPingInterval() <= getDiffSecNow(startTime.pingOn)) && (GetServerSign() == TRUE))
  292. SendPing(wsi);
  293. else
  294. SendData(wsi);
  295. break;
  296. case LWS_CALLBACK_CLIENT_RECEIVE://8
  297. ((char *)in)[len] = '\0';
  298. DEBUG_OCPPMESSAGE_INFO("<===== %s\n", (char *)in);
  299. //**********Receive Message**********/
  300. c = 0;
  301. loc = strstr((const char *)in, "][2,");
  302. if(loc == NULL)
  303. {
  304. loc = strstr((const char *)in, "][3,");
  305. if(loc == NULL)
  306. {
  307. loc = strstr((const char *)in, "][4,");
  308. }
  309. }
  310. memset(sstr, 0, ARRAY_SIZE(sstr) );
  311. if(loc != NULL)
  312. {
  313. DEBUG_INFO("There are continuous second packet []\n");
  314. while (loc[1+c] != '\0')
  315. {
  316. sstr[c] = loc[1+c];
  317. c++;
  318. }
  319. sstr[c] = '\0';
  320. strcpy(tempin, sstr);
  321. DEBUG_INFO("Final Receive: %s\n", tempin);
  322. }
  323. else
  324. {
  325. strcpy(tempin,(char *)in);
  326. }
  327. ReceivedMessage((void *)strtrim(tempin), strlen(tempin));
  328. isWebsocketSendable = 1;
  329. refreshStartTimer(&startTime.pingOn);
  330. break;
  331. case LWS_CALLBACK_CLIENT_RECEIVE_PONG:
  332. DEBUG_INFO("LWS_CALLBACK_CLIENT_RECEIVE_PONG\n");
  333. DEBUG_OCPPMESSAGE_INFO("<===== Get PONG packet.\n");
  334. refreshStartTimer(&startTime.pingOn);
  335. isWebsocketSendable = 1;
  336. counterPingSend = 0;
  337. break;
  338. case LWS_CALLBACK_OPENSSL_PERFORM_SERVER_CERT_VERIFICATION:
  339. DEBUG_INFO("LWS_CALLBACK_OPENSSL_PERFORM_SERVER_CERT_VERIFICATION\n");
  340. break;
  341. case LWS_CALLBACK_OPENSSL_LOAD_EXTRA_CLIENT_VERIFY_CERTS:
  342. DEBUG_INFO("LWS_CALLBACK_OPENSSL_LOAD_EXTRA_CLIENT_VERIFY_CERTS\n");
  343. break;
  344. case LWS_CALLBACK_PROTOCOL_DESTROY:
  345. DEBUG_INFO("LWS_CALLBACK_PROTOCOL_DESTROY\n");
  346. break;
  347. case LWS_CALLBACK_RECEIVE_PONG:
  348. DEBUG_INFO("LWS_CALLBACK_RECEIVE_PONG\n");
  349. break;
  350. case LWS_CALLBACK_WS_PEER_INITIATED_CLOSE:
  351. DEBUG_INFO("LWS_CALLBACK_WS_PEER_INITIATED_CLOSE\n");
  352. break;
  353. case LWS_CALLBACK_CLOSED_CLIENT_HTTP:
  354. DEBUG_INFO("LWS_CALLBACK_CLOSED_CLIENT_HTTP\n");
  355. if(GetInternetConn() == 1)
  356. {
  357. DEBUG_INFO("Download new CA certification.\n", system("wget --no-check-certificate -O /root/cacert.pem http://curl.haxx.se/ca/cacert.pem &"));
  358. }
  359. break;
  360. default:
  361. DEBUG_INFO("Reason = %d\n", reason);
  362. break;
  363. }
  364. return 0;
  365. }
  366. static struct lws_protocols protocols[] =
  367. {
  368. {
  369. "ocpp1.6",
  370. OCPP16Callback,
  371. WEBSOCKET_BUFFER_SIZE,
  372. WEBSOCKET_BUFFER_SIZE,
  373. },
  374. {
  375. "ocpp2.0.1",
  376. OCPP16Callback,
  377. WEBSOCKET_BUFFER_SIZE,
  378. WEBSOCKET_BUFFER_SIZE,
  379. },
  380. {
  381. "ocpp1.6,ocpp2.0.1",
  382. OCPP16Callback,
  383. WEBSOCKET_BUFFER_SIZE,
  384. WEBSOCKET_BUFFER_SIZE,
  385. },
  386. {
  387. NULL, NULL, 0 /* End of list */
  388. }
  389. };
  390. void* ConnectWsServer(void* data) //int ConnectWsServer()
  391. {
  392. struct lws_context_creation_info ContextInfo;
  393. struct lws_client_connect_info ConnInfo;
  394. int use_ssl=0;
  395. counterConnect += 1;
  396. // If internet available synchronize datetime with ntp server
  397. if(GetInternetConn() == 1)
  398. {
  399. system("pkill ntpd");
  400. DEBUG_INFO("NTP synchronize with Microsoft\n", system("/usr/sbin/ntpd -nqp time.windows.com &"));
  401. DEBUG_INFO("NTP synchronize with China\n", system("/usr/sbin/ntpd -nqp cn.ntp.org.cn &"));
  402. DEBUG_INFO("NTP synchronize with Taiwan\n", system("/usr/sbin/ntpd -nqp tock.stdtime.gov.tw &"));
  403. DEBUG_INFO("NTP synchronize with Europe\n", system("/usr/sbin/ntpd -nqp 0.europe.pool.ntp.org &"));
  404. }
  405. if(context!=NULL)
  406. {
  407. pthread_detach(pthread_self());
  408. lws_context_destroy(context);
  409. ConnectionEstablished=0;
  410. context = NULL;
  411. }
  412. memset(&ContextInfo, 0, sizeof(struct lws_context_creation_info));
  413. if((GetOcppServerURL()==0) || (GetOcppPort() == 0) || (GetOcppPath()==0))
  414. {
  415. DEBUG_ERROR("OCPP URL is NULL or OCPP Port is zero or OCPP Path is NULL\n");
  416. goto end;
  417. }
  418. if((strcmp(OcppProtocol,"ws")==0)&&(strlen(OcppProtocol)== 2))
  419. {
  420. DEBUG_INFO("Web socket is non-security mode.\n");
  421. use_ssl=0;
  422. }
  423. else if((strcmp(OcppProtocol,"wss")==0)&&(strlen(OcppProtocol)== 3))
  424. {
  425. DEBUG_INFO("Web socket is security mode.\n");
  426. use_ssl=1;
  427. }
  428. MergeCertificates();
  429. ContextInfo.port = CONTEXT_PORT_NO_LISTEN;
  430. ContextInfo.iface = NULL;
  431. ContextInfo.ssl_private_key_password = NULL;
  432. ContextInfo.ssl_cert_filepath = ((GetOcppSecurityProfile()==3) && (access("/Storage/OCPP/certCP.pem",F_OK) != -1))?"/Storage/OCPP/certCP.pem":NULL;
  433. ContextInfo.ssl_private_key_filepath = ((GetOcppSecurityProfile()==3) && (access("/Storage/OCPP/certCP.key",F_OK) != -1))?"/Storage/OCPP/certCP.key":NULL;
  434. ContextInfo.ssl_ca_filepath = "/root/certCA.pem";
  435. ContextInfo.ssl_cipher_list = NULL; //use default one
  436. ContextInfo.gid = -1;
  437. ContextInfo.uid = -1;
  438. if(use_ssl)
  439. {
  440. ContextInfo.options |= LWS_SERVER_OPTION_DO_SSL_GLOBAL_INIT;
  441. }
  442. ContextInfo.protocols = protocols;
  443. ContextInfo.timeout_secs = GetBackendConnectionTimeout();
  444. //ContextInfo.ws_ping_pong_interval = GetWebSocketPingInterval();
  445. ContextInfo.ka_time = 20;
  446. ContextInfo.keepalive_timeout = 5;
  447. ContextInfo.ka_probes = 2;
  448. ContextInfo.ka_interval = 5;
  449. context = lws_create_context(&ContextInfo);
  450. if (context == NULL)
  451. {
  452. DEBUG_ERROR("lws_create_context NG");
  453. goto end;
  454. }
  455. memset(&ConnInfo,0,sizeof(struct lws_client_connect_info));
  456. // fill up below information
  457. ConnInfo.context = context;
  458. ConnInfo.address=(const char *)OcppHost;
  459. DEBUG_INFO("ConnInfo.address: %s\n", ConnInfo.address);
  460. ConnInfo.port = GetOcppPort();
  461. DEBUG_INFO("ConnInfo.port: %d\n", ConnInfo.port);
  462. ConnInfo.path=(const char *)OcppPath;
  463. DEBUG_INFO("ConnInfo.path: %s\n", ConnInfo.path);
  464. char addr_port[256] = { 0 };
  465. sprintf(addr_port, "%s:%u", ConnInfo.address, (ConnInfo.port & 65535) );
  466. ConnInfo.host= addr_port; // ConnInfo.address;//lws_canonical_hostname(context);
  467. //ConnInfo.origin="origin";
  468. ConnInfo.protocol = protocols[0].name;
  469. ConnInfo.ietf_version_or_minus_one = -1;
  470. if(use_ssl)
  471. {
  472. #ifdef TLS_VALID_CERT_EXPIRED
  473. ConnInfo.ssl_connection = LCCSCF_USE_SSL | LCCSCF_ALLOW_SELFSIGNED | LCCSCF_SKIP_SERVER_CERT_HOSTNAME_CHECK;
  474. DEBUG_INFO("TLS does not allow expired certification.\n");
  475. #else
  476. ConnInfo.ssl_connection = LCCSCF_USE_SSL | LCCSCF_ALLOW_SELFSIGNED | LCCSCF_SKIP_SERVER_CERT_HOSTNAME_CHECK | LCCSCF_ALLOW_EXPIRED;
  477. DEBUG_INFO("TLS allow expired certification.\n");
  478. #endif
  479. }
  480. wsi_client = lws_client_connect_via_info(&ConnInfo);
  481. if (!wsi_client)
  482. {
  483. DEBUG_ERROR("lws_client_connect_via_info NG\n");
  484. //goto end;
  485. }
  486. counterConnect=0;
  487. DEBUG_INFO("counterConnect: %d\n", counterConnect);
  488. end:
  489. pthread_exit(NULL/*(void *) fname*/);
  490. }
  491. int isQueueOverSize()
  492. {
  493. FILE *fp;
  494. uint32_t file_size;
  495. uint8_t result = FALSE;
  496. fp = fopen("/Storage/OCPP/TransactionRelatedQueue" , "r");
  497. if(fp != NULL)
  498. {
  499. fseek(fp, 0L, SEEK_END);
  500. file_size = ftell(fp);
  501. if(file_size > (500*1024*1024))
  502. {
  503. result = TRUE;
  504. DEBUG_WARN("Queue file over size.\n");
  505. }
  506. fclose(fp);
  507. }
  508. return result;
  509. }
  510. int showfront(char *uuid, char *data)
  511. {
  512. FILE *fp;
  513. int result = FALSE; // 1: TRUE 0:FALSE
  514. char str[QUEUE_MESSAGE_LENGTH]={0};
  515. char sstr[50]={ 0 };//sstr[200]={ 0 };
  516. int c = 0;
  517. char *loc;
  518. char rmFileCmd[100]={0};
  519. struct stat stats;
  520. stat("/Storage/OCPP", &stats);
  521. // Check for directory existence
  522. if (S_ISDIR(stats.st_mode) == 1)
  523. {
  524. //DEBUG_INFO("\n OCPP directory exist \n");
  525. }
  526. else
  527. {
  528. DEBUG_INFO("\n OCPP directory not exist, create dir \n");
  529. sprintf(rmFileCmd,"mkdir -p %s","/Storage/OCPP");
  530. system(rmFileCmd);
  531. }
  532. memset(rmFileCmd, 0, ARRAY_SIZE(rmFileCmd));
  533. if((access("/Storage/OCPP/TransactionRelatedQueue",F_OK))!=-1)
  534. {
  535. //DEBUG_INFO("TransactionRelatedQueue exist.\n");
  536. }
  537. else
  538. {
  539. //DEBUG_INFO("TransactionRelatedQueue not exist\n");
  540. FILE *log = fopen("/Storage/OCPP/TransactionRelatedQueue", "w+");
  541. if(log == NULL)
  542. {
  543. DEBUG_INFO("Can't Create File TransactionRelatedQueue \n");
  544. return FALSE;
  545. }
  546. else
  547. {
  548. fclose(log);
  549. }
  550. }
  551. /* opening file for reading */
  552. fp = fopen("/Storage/OCPP/TransactionRelatedQueue" , "r");
  553. if(fp == NULL) {
  554. DEBUG_INFO("Error opening TransactionRelatedQueue file");
  555. return FALSE;
  556. }
  557. if( fgets (str, QUEUE_MESSAGE_LENGTH, fp)!=NULL ) {
  558. /* writing content to stdout */
  559. //DEBUG_INFO("str=%s",str);
  560. if ((str[0] == '\n')||(strcmp(str,"")==0))
  561. {
  562. DEBUG_INFO("It is a blank line");
  563. fclose(fp);
  564. memset(rmFileCmd, 0, ARRAY_SIZE(rmFileCmd));
  565. sprintf(rmFileCmd,"rm -f %s","/Storage/OCPP/TransactionRelatedQueue");
  566. system(rmFileCmd);
  567. result = FALSE;
  568. return result;
  569. }
  570. else
  571. {
  572. //puts(str);
  573. //----------------uuid--------------//
  574. loc = strstr(str, "\"");
  575. memset(sstr ,0, ARRAY_SIZE(sstr) );
  576. c = 0;
  577. while (loc[1+c] != '\"')
  578. {
  579. sstr[c] = loc[1+c];
  580. c++;
  581. }
  582. sstr[c] = '\0';
  583. //DEBUG_INFO("\n uuid:%s", sstr);
  584. //DEBUG_INFO("\n data:%s", str);
  585. strcpy(uuid,sstr);
  586. strcpy(data,str);
  587. result = TRUE;
  588. }
  589. }
  590. else
  591. {
  592. //DEBUG_INFO("queue is null\n");
  593. strcpy(uuid,"");
  594. strcpy(data,"");
  595. result = FALSE;
  596. }
  597. fclose(fp);
  598. return result;
  599. }
  600. int addq(char *uuid, char *data)
  601. {
  602. FILE *outfile;
  603. char rmFileCmd[100]={0};
  604. struct stat stats;
  605. stat("/Storage/OCPP", &stats);
  606. DEBUG_INFO("addq\n");
  607. // Check for directory existence
  608. if (S_ISDIR(stats.st_mode) == 1)
  609. {
  610. //DEBUG_INFO("\n OCPP directory exist \n");
  611. }
  612. else
  613. {
  614. //DEBUG_INFO("\n OCPP directory not exist, create dir \n");
  615. sprintf(rmFileCmd,"mkdir -p %s","/Storage/OCPP");
  616. system(rmFileCmd);
  617. }
  618. memset(rmFileCmd, 0, ARRAY_SIZE(rmFileCmd));
  619. if((access("/Storage/OCPP/TransactionRelatedQueue",F_OK))!=-1)
  620. {
  621. //DEBUG_INFO("TransactionRelatedQueue exist.\n");
  622. }
  623. else
  624. {
  625. //DEBUG_INFO("TransactionRelatedQueue not exist\n");
  626. FILE *log = fopen("/Storage/OCPP/TransactionRelatedQueue", "w+");
  627. if(log == NULL)
  628. {
  629. //DEBUG_INFO("Can't Create File TransactionRelatedQueue \n");
  630. return FALSE;
  631. }
  632. else
  633. {
  634. fclose(log);
  635. }
  636. }
  637. // open file for writing
  638. outfile = fopen ("/Storage/OCPP/TransactionRelatedQueue", "a");
  639. DEBUG_INFO("data = %s\n",data);
  640. fputs(data, outfile);
  641. fputs("\n", outfile);
  642. fclose (outfile);
  643. queueOpInfo.TransactionQueueNum += 1;
  644. DEBUG_INFO("add queue end\n");
  645. system("/bin/fsync -d /dev/mtdblock13;/bin/sync &");
  646. return FALSE;
  647. }
  648. //---------------- delq(): delete the top item --------------//
  649. int delq()
  650. {
  651. char tempfile[] = "/Storage/OCPP/delqtemp.json";
  652. FILE *infile;
  653. FILE *outfile;
  654. int resultRename=0;
  655. char filename[60]={0};
  656. char rmFileCmd[100]={0};
  657. struct stat stats;
  658. stat("/Storage/OCPP", &stats);
  659. DEBUG_INFO("delq()\n");
  660. // Check for directory existence
  661. if (S_ISDIR(stats.st_mode) == 1)
  662. {
  663. //DEBUG_INFO("\n OCPP directory exist \n");
  664. }
  665. else
  666. {
  667. //DEBUG_INFO("\n OCPP directory not exist, create dir \n");
  668. sprintf(rmFileCmd,"mkdir -p %s","/Storage/OCPP");
  669. system(rmFileCmd);
  670. }
  671. memset(rmFileCmd, 0, ARRAY_SIZE(rmFileCmd));
  672. if((access("/Storage/OCPP/TransactionRelatedQueue",F_OK))!=-1)
  673. {
  674. //DEBUG_INFO("TransactionRelatedQueue exist.\n");
  675. }
  676. else
  677. {
  678. //DEBUG_INFO("TransactionRelatedQueue not exist\n");
  679. FILE *log = fopen("/Storage/OCPP/TransactionRelatedQueue", "w+");
  680. if(log == NULL)
  681. {
  682. //DEBUG_INFO("log is NULL\n");
  683. return 0;
  684. }
  685. else
  686. {
  687. fclose(log);
  688. }
  689. }
  690. // open file for writing
  691. strcpy(filename, "/Storage/OCPP/TransactionRelatedQueue");
  692. infile = fopen ("/Storage/OCPP/TransactionRelatedQueue", "r");
  693. outfile = fopen (tempfile, "w");
  694. /*检测到文件结束标识返回1,否则返回0。*/
  695. //DEBUG_INFO("feof(infile) =%d\n",feof(infile));
  696. int c;
  697. c = fgetc(infile);
  698. //printf("file c:%d\n",c);
  699. rewind(infile);
  700. if(c == EOF)
  701. {
  702. //DEBUG_INFO("TransactionRelatedQueue is NULL\n");
  703. fclose(infile);
  704. fclose(outfile);
  705. sprintf(rmFileCmd,"rm -f %s",tempfile);
  706. system(rmFileCmd);
  707. }
  708. else
  709. {
  710. char buf[QUEUE_MESSAGE_LENGTH]={0};
  711. int i = 0;
  712. //DEBUG_INFO("Orignal File is not NULL\n");
  713. while (fgets(buf, sizeof(buf), infile) != NULL)
  714. {
  715. //printf("Orignal File get strings \n");
  716. buf[strlen(buf) - 1] = '\0'; // eat the newline fgets() stores
  717. if(i==0)
  718. {
  719. queueOpInfo.TransactionQueueNum -= 1;
  720. queueOpInfo.TransactionMessageResend = 0;
  721. DEBUG_INFO("delete the item\n");
  722. }
  723. if(i != 0)
  724. {
  725. fprintf(outfile,"%s\n", buf);
  726. }
  727. i = i + 1;
  728. }
  729. fclose(infile);
  730. fclose(outfile);
  731. sprintf(rmFileCmd,"rm -f %s",filename);
  732. system(rmFileCmd);
  733. resultRename = rename(tempfile, filename);
  734. if(resultRename == 0)
  735. {
  736. //DEBUG_INFO("TransactionRelatedQueue file renamed successfully");
  737. }
  738. else
  739. {
  740. //DEBUG_INFO("Error: unable to rename the TransactionRelatedQueue file");
  741. }
  742. DEBUG_INFO("delq() end\n");
  743. }
  744. system("/bin/fsync -d /dev/mtdblock13;/bin/sync &");
  745. return 0;
  746. }
  747. int showqueue()
  748. {
  749. char rmFileCmd[100]={0};
  750. struct stat stats;
  751. stat("/Storage/OCPP", &stats);
  752. // Check for directory existence
  753. if (S_ISDIR(stats.st_mode) == 1)
  754. {
  755. //DEBUG_INFO("\n OCPP directory exist \n");
  756. }
  757. else
  758. {
  759. //DEBUG_INFO("\n OCPP directory not exist, create dir \n");
  760. sprintf(rmFileCmd,"mkdir -p %s","/Storage/OCPP");
  761. system(rmFileCmd);
  762. }
  763. memset(rmFileCmd, 0, ARRAY_SIZE(rmFileCmd));
  764. if((access("/Storage/OCPP/TransactionRelatedQueue",F_OK))!=-1)
  765. {
  766. //DEBUG_INFO("TransactionRelatedQueue exist.\n");
  767. }
  768. else
  769. {
  770. //DEBUG_INFO("TransactionRelatedQueue not exist\n");
  771. FILE *log = fopen("/Storage/OCPP/TransactionRelatedQueue", "w+");
  772. if(log == NULL)
  773. {
  774. DEBUG_INFO("log is NULL\n");
  775. return FALSE;
  776. }
  777. else
  778. {
  779. fclose(log);
  780. }
  781. }
  782. FILE *fp = fopen("/Storage/OCPP/TransactionRelatedQueue", "r");
  783. char line[QUEUE_MESSAGE_LENGTH]={0};
  784. // check if file exist (and you can open it) or not
  785. if (fp == NULL) {
  786. DEBUG_INFO("can open file TransactionRelatedQueue!");
  787. return FALSE;
  788. }
  789. queueOpInfo.TransactionQueueNum = 0; // the number of packets in queue
  790. while(fgets(line, sizeof line, fp) != NULL) {
  791. //DEBUG_INFO("%s\n", line);
  792. queueOpInfo.TransactionQueueNum += 1; //the number of packets in queue
  793. }
  794. fclose(fp);
  795. return TRUE;
  796. }
  797. int sentqueue()
  798. {
  799. int result = FAIL;
  800. struct stat stats;
  801. FILE *fp;
  802. json_object *obj = NULL;
  803. json_object *objPayload = NULL;
  804. json_object *objData = NULL;
  805. char cmd[128];
  806. char str[QUEUE_MESSAGE_LENGTH]={0};
  807. char queueData[QUEUE_MESSAGE_LENGTH]={0};
  808. char payload[QUEUE_MESSAGE_LENGTH]={0};
  809. char key_value[65]={0};
  810. char hashData[65]={0};
  811. char action[32];
  812. char guid[37];
  813. uint8_t connectorId;
  814. DEBUG_INFO("Sent queue.\n");
  815. stat("/Storage/OCPP", &stats);
  816. // Check for directory existence
  817. if (S_ISDIR(stats.st_mode) != 1)
  818. {
  819. //DEBUG_INFO("\n OCPP directory not exist, create dir \n");
  820. sprintf(cmd, "mkdir -p /Storage/OCPP");
  821. system(cmd);
  822. }
  823. if((fp = fopen("/Storage/OCPP/TransactionRelatedQueue" , "r")) == NULL)
  824. {
  825. DEBUG_ERROR("Error opening file");
  826. }
  827. else
  828. {
  829. // parse message content
  830. if(fgets(str, QUEUE_MESSAGE_LENGTH, fp) != NULL)
  831. {
  832. // parse connectorId
  833. connectorId = (str[0]-0x30);
  834. memcpy(&queueData, &str[2], strlen(str)-2);
  835. obj = json_tokener_parse(queueData);
  836. if(!is_error(obj))
  837. {
  838. sprintf(guid, "%s", json_object_get_string(json_object_array_get_idx(obj, 1)));
  839. sprintf(action, "%s", json_object_get_string(json_object_array_get_idx(obj, 2)));
  840. sprintf(payload, "%s", json_object_to_json_string_ext(json_object_array_get_idx(obj, 3), JSON_C_TO_STRING_PLAIN));
  841. objPayload = json_tokener_parse(payload);
  842. if(!is_error(objPayload))
  843. {
  844. if(strstr(action, "StartTransaction") != NULL)
  845. {
  846. if(hashmap_operation(HASH_OP_GET, guid, key_value) == TRUE)
  847. {
  848. //DEBUG_INFO("\n 1. sent queue guid=%s\n",guid);
  849. }
  850. else
  851. {
  852. char idtag[21]={0};
  853. char timestamp[36]={0};
  854. int meterStart=0;
  855. int reservationId=-1;
  856. sprintf(hashData, "StartTransaction,%d", (connectorId-1));
  857. hashmap_operation(HASH_OP_ADD, guid, hashData);
  858. if(json_object_object_get(objPayload, "idTag") != NULL)
  859. {
  860. sprintf(idtag, "%s", json_object_get_string(json_object_object_get(objPayload, "idTag")));
  861. }
  862. if(json_object_object_get(objPayload, "meterStart") != NULL)
  863. {
  864. meterStart = json_object_get_int(json_object_object_get(objPayload, "meterStart"));
  865. }
  866. if(json_object_object_get(objPayload, "reservationId") != NULL)
  867. {
  868. reservationId = json_object_get_int(json_object_object_get(objPayload, "reservationId"));
  869. }
  870. if(json_object_object_get(objPayload, "timestamp") != NULL)
  871. {
  872. sprintf(timestamp, "%s", json_object_get_string(json_object_object_get(objPayload, "timestamp")));
  873. }
  874. FillStartTransaction(connectorId, (unsigned char*)idtag, meterStart, reservationId, (unsigned char*)timestamp);
  875. //DEBUG_INFO("\n 2. sent queue guid=%s\n",guid);
  876. }
  877. LWS_Send((char*)json_object_to_json_string_ext(obj, JSON_C_TO_STRING_PLAIN));
  878. json_object_put(objPayload);
  879. }
  880. else if((strstr(action, "MeterValues") != NULL) || strstr(action, "StopTransaction") != NULL)
  881. {
  882. char idtag[21]={0};
  883. int transactionId_org=0;
  884. int transactionId_map=0;
  885. if(json_object_object_get(objPayload, "transactionId") != NULL)
  886. {
  887. transactionId_org = json_object_get_int(json_object_object_get(objPayload, "transactionId"));
  888. if(json_object_object_get(objPayload, "idTag") != NULL)
  889. {
  890. sprintf(idtag, "%s", json_object_get_string(json_object_object_get(objPayload, "idTag")));
  891. }
  892. //Get IdTag from StartTransaction , store to StartTransactionIdTagTemp, For StopTransaction usage in Queue (StartTransaction. StopTransaction user id different), get actual TransactionId
  893. GetStartTransactionIdTag(connectorId-1);
  894. transactionId_map = GetTransactionId(connectorId, (unsigned char*)idtag, ((strstr(action, "StopTransaction") != NULL)?YES:NO));
  895. DEBUG_INFO("queue map transactionId = %d\n", transactionId_map);
  896. DEBUG_INFO("original connectorId = %d\n", connectorId);
  897. DEBUG_INFO("original transactionId = %d\n", transactionId_org);
  898. DEBUG_INFO("original IdtagStr = %s\n", "");
  899. if((transactionId_map != 0)&&(transactionId_org != transactionId_map))
  900. {
  901. //replace transactionId
  902. json_object_object_add(objPayload, "transactionId", json_object_new_int(transactionId_map));
  903. }
  904. else
  905. {
  906. transactionId_map = transactionId_org;
  907. }
  908. LastQueuedTransactionId[connectorId-1] = transactionId_map;
  909. //FillTransactionId(connectorId, transactionId_map);
  910. DEBUG_INFO("Final transactionId = %d\n", transactionId_map);
  911. }
  912. json_object_array_put_idx(obj, 3, objPayload);
  913. LWS_Send((char*)json_object_to_json_string_ext(obj, JSON_C_TO_STRING_PLAIN));
  914. if(strstr(action, "StopTransaction") != NULL)
  915. queueOpInfo.stopTransactionId = transactionId_map;
  916. }
  917. else
  918. {
  919. int transactionId_org=0;
  920. int transactionId_map=0;
  921. if(strstr(json_object_get_string(json_object_object_get(objPayload, "messageId")), "ConnectorUnplugged") != NULL)
  922. {
  923. objData = json_tokener_parse(json_object_get_string(json_object_object_get(objPayload, "data")));
  924. if(!is_error(objData))
  925. {
  926. transactionId_org = json_object_get_int(json_object_object_get(objData, "idTx"));
  927. //Get IdTag from StartTransaction , store to StartTransactionIdTagTemp, For StopTransaction usage in Queue (StartTransaction. StopTransaction user id different), get actual TransactionId
  928. GetStartTransactionIdTag(connectorId-1);
  929. transactionId_map = GetTransactionId(connectorId, (unsigned char*)"", NO);
  930. DEBUG_INFO("queue map transactionId = %d\n", transactionId_map);
  931. DEBUG_INFO("original connectorId = %d\n", connectorId);
  932. DEBUG_INFO("original transactionId = %d\n", transactionId_org);
  933. DEBUG_INFO("original IdtagStr = %s\n", "");
  934. if((transactionId_map != 0)&&(transactionId_org == 0)&&(transactionId_org != transactionId_map))
  935. {
  936. //replace transactionId
  937. json_object_object_add(objData, "idTx", json_object_new_int(transactionId_map));
  938. }
  939. else if((transactionId_map == 0) && (transactionId_org == 0))
  940. {
  941. //replace transactionId
  942. transactionId_map = GetStartTransactionId(connectorId-1);
  943. json_object_object_add(objData, "idTx", json_object_new_int(transactionId_map));
  944. }
  945. else
  946. {
  947. transactionId_map = transactionId_org;
  948. }
  949. DEBUG_INFO("Final transactionId = %d\n", transactionId_map);
  950. json_object_object_add(objPayload, "data", json_object_new_string(json_object_to_json_string_ext(objData, JSON_C_TO_STRING_PLAIN)));
  951. json_object_array_put_idx(obj, 3, objPayload);
  952. LWS_Send((char*)json_object_to_json_string_ext(obj, JSON_C_TO_STRING_PLAIN));
  953. }
  954. }
  955. else if(strstr(json_object_get_string(json_object_object_get(objPayload, "messageId")), "ID_OCMF") != NULL)
  956. {
  957. DEBUG_INFO(">>> Sending OCMF queue message...\n");
  958. objData = json_tokener_parse(json_object_get_string(json_object_object_get(objPayload, "data")));
  959. if(!is_error(objData))
  960. {
  961. transactionId_org = json_object_get_int(json_object_object_get(objData, "txId"));
  962. DEBUG_INFO("original transactionId = %d\n", transactionId_org);
  963. if(transactionId_org == 0)
  964. {
  965. //replace transactionId
  966. transactionId_map = GetStartTransactionId(connectorId-1);
  967. if(transactionId_map == 0)
  968. transactionId_map = LastQueuedTransactionId[connectorId-1];
  969. json_object_object_add(objData, "txId", json_object_new_int(transactionId_map));
  970. }
  971. else
  972. {
  973. transactionId_map = transactionId_org;
  974. }
  975. DEBUG_INFO("Final transactionId = %d\n", transactionId_map);
  976. json_object_object_add(objPayload, "data", json_object_new_string(json_object_to_json_string_ext(objData, JSON_C_TO_STRING_PLAIN)));
  977. json_object_array_put_idx(obj, 3, objPayload);
  978. LWS_Send((char*)json_object_to_json_string_ext(obj, JSON_C_TO_STRING_PLAIN));
  979. }
  980. }
  981. }
  982. result = PASS;
  983. }
  984. }
  985. json_object_put(obj);
  986. }
  987. }
  988. fclose(fp);
  989. return result;
  990. }
  991. void* processTransactionQueue(void* data)
  992. {
  993. char frontUUID[100] ={0};
  994. char frontData[QUEUE_MESSAGE_LENGTH] ={0};
  995. int queueNotEmpty = FALSE;
  996. while(1)
  997. {
  998. if(!req_SendQueue && ((getDiffSecNow(startTime.startTimeQueue) >= ((TransactionMessageRetryIntervalGet()>10?TransactionMessageRetryIntervalGet():10)*(queueOpInfo.TransactionMessageResend>1?2:1))) || (isWebsocketSendable && isQueueSendable && (getDiffSecNow(startTime.startTimeQueue) >= ((counterQueueSent>=20)?5:0)))))
  999. {
  1000. if(FirstHeartBeatResponse() == 1)
  1001. {
  1002. memset(frontUUID, 0, ARRAY_SIZE(frontUUID));
  1003. memset(frontData, 0, ARRAY_SIZE(frontData));
  1004. queueNotEmpty = queue_operation(QUEUE_OPERATION_SHOWFRONT,frontUUID, frontData);
  1005. if((queueNotEmpty == TRUE) && (GetOcppConnStatus() == 1))
  1006. {
  1007. if(isWebsocketSendable)
  1008. DEBUG_INFO("isWebsocketSendable on.\n");
  1009. if(isQueueSendable)
  1010. DEBUG_INFO("isQueueSendable on.\n");
  1011. if(((getDiffSecNow(startTime.startTimeQueue) > (TransactionMessageRetryIntervalGet()>10?TransactionMessageRetryIntervalGet():10))))
  1012. DEBUG_INFO("Queue timer(%d) over spec(%d).\n", getDiffSecNow(startTime.startTimeQueue), TransactionMessageRetryIntervalGet());
  1013. if(queueOpInfo.TransactionMessageResend < TransactionMessageAttemptsGet())
  1014. {
  1015. DEBUG_INFO("Sent message from queue request.\n");
  1016. DEBUG_INFO("TransactionMessageResend time: %d\n", queueOpInfo.TransactionMessageResend);
  1017. req_SendQueue = 1;
  1018. queueOpInfo.TransactionMessageResend += 1;
  1019. }
  1020. else
  1021. {
  1022. DEBUG_INFO("Transaction message resend(%d) over spec(%d) message abandon.\n", queueOpInfo.TransactionMessageResend, TransactionMessageAttemptsGet());
  1023. queue_operation(QUEUE_OPERATION_DEL,"","");
  1024. queueOpInfo.TransactionMessageResend = 0;
  1025. req_SendQueue = 0;
  1026. }
  1027. }
  1028. }
  1029. // Refresh queue timer
  1030. refreshStartTimer(&startTime.startTimeQueue);
  1031. if((counterQueueSent >= 10) || (queueNotEmpty == FALSE))
  1032. {
  1033. counterQueueSent = 0;
  1034. }
  1035. else
  1036. {
  1037. counterQueueSent += 1;
  1038. }
  1039. }
  1040. usleep(500000);
  1041. }
  1042. pthread_exit(NULL);
  1043. return 0;
  1044. }
  1045. void* processWatchdog()
  1046. {
  1047. for(;;)
  1048. {
  1049. if((getDiffSecNow(startTime.disconnectServer) >= 7200))
  1050. {
  1051. DEBUG_INFO("OCPP server disconnect timer(%d) over 7200 seconds.\n", getDiffSecNow(startTime.disconnectServer));
  1052. system("killall OcppBackend");
  1053. }
  1054. if(counterConnect >= 3)
  1055. {
  1056. DEBUG_INFO("Connect OCPP server timeout over 3 count.\n");
  1057. system("killall OcppBackend");
  1058. }
  1059. if((0 < GetWebSocketPingInterval()) && (0 < counterPingSend) && ((GetWebSocketPingInterval()+5) <= getDiffSecNow(startTime.pingOn)) && (wsi_client != NULL) && (GetServerSign() == TRUE))
  1060. {
  1061. DEBUG_WARN("Pong packet receive timeout.\n");
  1062. //system("killall OcppBackend");
  1063. lws_context_destroy(context);
  1064. ConnectionEstablished = 0;
  1065. context = NULL;
  1066. }
  1067. usleep(500000);
  1068. }
  1069. pthread_exit(NULL); //
  1070. }
  1071. void CheckTransactionPacket(char *uuid)
  1072. {
  1073. char frontUUID[100]={0};
  1074. char frontData[QUEUE_MESSAGE_LENGTH]={0};
  1075. int queueNotEmpty = FALSE;
  1076. int cmpResult = 0;
  1077. queueNotEmpty = queue_operation(QUEUE_OPERATION_SHOWFRONT,frontUUID, frontData);//showfront(frontUUID, frontData); ---> remove temporally
  1078. if(queueNotEmpty == TRUE)
  1079. {
  1080. cmpResult = strcmp(frontUUID, uuid);
  1081. if (cmpResult == 0)
  1082. {
  1083. DEBUG_INFO("Receive queue response match.\n");
  1084. queue_operation(QUEUE_OPERATION_DEL,"","");//delq(); ---> remove temporally
  1085. queueOpInfo.TransactionMessageResend = 0;
  1086. }
  1087. else
  1088. DEBUG_INFO("Receive queue response mismatch.\n");
  1089. }
  1090. }
  1091. int queue_operation(int type, char *frontUUID, char *frontData)
  1092. {
  1093. int result=0;
  1094. while(1)
  1095. {
  1096. if (!queueOpInfo.IsUsing )
  1097. {
  1098. queueOpInfo.IsUsing = TRUE;
  1099. if(type == QUEUE_OPERATION_SHOWQUEUE) // show items in queue
  1100. {
  1101. result = showqueue();
  1102. }
  1103. else if(type == QUEUE_OPERATION_SHOWFRONT) // show first item
  1104. {
  1105. result = showfront(frontUUID, frontData);
  1106. }
  1107. else if(type == QUEUE_OPERATION_DEL) // delete item
  1108. {
  1109. result = delq();
  1110. }
  1111. else if(type == QUEUE_OPERATION_SENT) // sent items in queue
  1112. {
  1113. result = sentqueue();
  1114. }
  1115. else if(type == QUEUE_OPERATION_ADD) // add items to the queue
  1116. {
  1117. // If queue file over size only add start * stop transaction message
  1118. if(!isQueueOverSize() || (strstr(frontData, "MeterValues") == NULL))
  1119. {
  1120. result = addq(frontUUID, frontData);
  1121. }
  1122. }
  1123. queueOpInfo.IsUsing = FALSE;
  1124. break;
  1125. }
  1126. usleep(100000);
  1127. }
  1128. return result;
  1129. }
  1130. int removeMessageSentFile(void)
  1131. {
  1132. char rmFileCmd[100]={0};
  1133. struct stat stats;
  1134. stat("/Storage/OCPP", &stats);
  1135. // Check for directory existence
  1136. if(S_ISDIR(stats.st_mode) == 1)
  1137. {
  1138. //DEBUG_INFO("\n OCPP directory exist \n");
  1139. }
  1140. else
  1141. {
  1142. DEBUG_INFO("\n directory not exist, create dir \n");
  1143. sprintf(rmFileCmd,"mkdir -p %s","/Storage/OCPP");
  1144. system(rmFileCmd);
  1145. }
  1146. stat("/Storage/OCPP/TransactionRelatedQueue", &stats);
  1147. if(stats.st_size < 10)
  1148. {
  1149. memset(rmFileCmd, 0, ARRAY_SIZE(rmFileCmd));
  1150. if((access("/Storage/OCPP/MessageSent",F_OK))!=-1)
  1151. {
  1152. DEBUG_INFO("MessageSent file exist.\n");
  1153. sprintf(rmFileCmd,"rm -f %s","/Storage/OCPP/MessageSent");
  1154. system(rmFileCmd);
  1155. }
  1156. memset(rmFileCmd, 0, ARRAY_SIZE(rmFileCmd));
  1157. }
  1158. return 0;
  1159. }
  1160. //================================================
  1161. // Main process
  1162. //================================================
  1163. int main(void)
  1164. {
  1165. char rmFileCmd[100]={0};
  1166. struct stat stats;
  1167. queueOpInfo.IsUsing = FALSE;
  1168. queueOpInfo.TransactionMessageResend = 0;
  1169. DEBUG_INFO("Module_OcppBackend task initialization...\n");
  1170. DEBUG_INFO("Git update date: 2022/09/28 \n");
  1171. //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);
  1172. if(ProcessShareMemory()== FAIL)
  1173. {
  1174. return FAIL;
  1175. }
  1176. // Check & create OCPP dir
  1177. stat("/Storage/OCPP", &stats);
  1178. if(S_ISDIR(stats.st_mode) != 1)
  1179. {
  1180. DEBUG_INFO("OCPP directory not exist, create dir \n");
  1181. sprintf(rmFileCmd,"mkdir -p /Storage/OCPP");
  1182. system(rmFileCmd);
  1183. }
  1184. memset(rmFileCmd, 0, ARRAY_SIZE(rmFileCmd));
  1185. // Create Process: Resend Transaction
  1186. refreshStartTimer(&startTime.disconnectServer);
  1187. pthread_create(&tid_ProcQueue, NULL, processTransactionQueue, NULL);
  1188. pthread_create(&tid_Watchdog, NULL, processWatchdog, NULL);
  1189. // Sqlite3 initial
  1190. sqlite3_config(SQLITE_CONFIG_URI,1);
  1191. if(sqlite3_open("file:/Storage/OCPP/charger.db", &db))
  1192. {
  1193. DEBUG_INFO( "Can't open database: %s\n", sqlite3_errmsg(db));
  1194. sqlite3_close( db );
  1195. exit(0);
  1196. }
  1197. else
  1198. {
  1199. DEBUG_INFO( "Opened database successfully\n");
  1200. }
  1201. //Create Table log buffer
  1202. if(sqlite3_exec(db, createsql, 0, 0, &errMsg) != SQLITE_OK)
  1203. {
  1204. DEBUG_INFO( "Create log buffer table error message: %s\n", errMsg);
  1205. return 0;
  1206. }
  1207. else
  1208. {
  1209. DEBUG_INFO( "Opened log buffer table successfully\n");
  1210. }
  1211. // Create Table OcppAuthCache
  1212. if(sqlite3_exec(db, sqlOcppAuthCache, 0, 0, &errMsg) != SQLITE_OK)
  1213. {
  1214. DEBUG_INFO( "Create OcppAuthCache error message: %s\n", errMsg);
  1215. return 0;
  1216. }
  1217. else
  1218. {
  1219. DEBUG_INFO( "Opened OcppAuthCache table successfully\n");
  1220. }
  1221. // Create Table OcppAuthLocal
  1222. if(sqlite3_exec(db, sqlOcppAuthLocal, 0, 0, &errMsg) != SQLITE_OK)
  1223. {
  1224. DEBUG_INFO( "Create Table OcppAuthLocal error %s\n",errMsg);
  1225. return 0;
  1226. }
  1227. else
  1228. {
  1229. DEBUG_INFO( "Opened OcppAuthLocal table successfully\n");
  1230. }
  1231. // Create Table Transaction
  1232. if(sqlite3_exec(db, sqlTransaction, 0, 0, &errMsg) != SQLITE_OK)
  1233. {
  1234. DEBUG_INFO( "Create Table ocpp_transaction_record error %s\n",errMsg);
  1235. return 0;
  1236. }
  1237. else
  1238. {
  1239. DEBUG_INFO( "Opened ocpp_transaction_record table successfully\n");
  1240. }
  1241. // Create transaction period energy
  1242. if(sqlite3_exec(db, sqlPeriodEnergy, 0, 0, &errMsg) != SQLITE_OK)
  1243. {
  1244. DEBUG_INFO( "Create Table ocpp_period_energy error %s\n",errMsg);
  1245. return 0;
  1246. }
  1247. else
  1248. {
  1249. DEBUG_INFO( "Opened ocpp_period_energy table successfully\n");
  1250. }
  1251. // Create credit deduct info
  1252. if(sqlite3_exec(db, sqlReportDeduct, 0, 0, &errMsg) != SQLITE_OK)
  1253. {
  1254. DEBUG_INFO( "Create Table report_deduct_info error %s\n",errMsg);
  1255. return 0;
  1256. }
  1257. else
  1258. {
  1259. DEBUG_INFO( "Opened report_deduct_info table successfully\n");
  1260. }
  1261. // Create occupancy deduct info
  1262. if(sqlite3_exec(db, sqlReportOccupancyDeduct, 0, 0, &errMsg) != SQLITE_OK)
  1263. {
  1264. DEBUG_INFO( "Create Table report_occupancyDeduct_info error %s\n",errMsg);
  1265. return 0;
  1266. }
  1267. else
  1268. {
  1269. DEBUG_INFO( "Opened report_occupancyDeduct_info table successfully\n");
  1270. }
  1271. // Create meter values buffer table
  1272. if(sqlite3_exec(db, sqlMeterValueBuffer, 0, 0, &errMsg) != SQLITE_OK)
  1273. {
  1274. DEBUG_INFO( "Create Table meterValuesBuffer error %s\n",errMsg);
  1275. return 0;
  1276. }
  1277. else
  1278. {
  1279. DEBUG_INFO( "Opened meterValuesBuffer table successfully\n");
  1280. }
  1281. if(initialConfigurationTable() != PASS)
  1282. {
  1283. DEBUG_WARN("OCPPConfiguration version mismatch, upgrade it.\n");
  1284. system("rm -f /Storage/OCPP/OCPPConfiguration");
  1285. initialConfigurationTable();
  1286. }
  1287. removeMessageSentFile();
  1288. for(;;)
  1289. {
  1290. // Connect server
  1291. if(ConnectionEstablished==0) // Check InternetConn 0: disconnected, 1: connected
  1292. {
  1293. isWebsocketSendable = 1;
  1294. isQueueSendable = 1;
  1295. SetOcppConnStatus(FALSE);
  1296. SetServerSign(FALSE);
  1297. InitialSystemValue();
  1298. if(getDiffSecNow(startTime.connect) >= 30)
  1299. {
  1300. DEBUG_INFO("Server connecting...\n");
  1301. pthread_create(&tid_connectServer, NULL, ConnectWsServer, NULL);
  1302. refreshStartTimer(&startTime.connect);
  1303. }
  1304. CheckSystemValue();
  1305. }
  1306. else
  1307. {
  1308. // Sign in
  1309. if((GetServerSign() == FALSE) &&
  1310. (isConnectorInitMode(0) != TRUE) &&
  1311. ( (GetBootNotificationInterval()>0) ? (getDiffSecNow(startTime.bootNotification) >= GetBootNotificationInterval()) : (getDiffSecNow(startTime.bootNotification) >= defaultWaitingTime) )
  1312. )
  1313. {
  1314. sendBootNotificationRequest();
  1315. refreshStartTimer(&startTime.bootNotification);
  1316. }
  1317. // Check System Value
  1318. CheckSystemValue();
  1319. // On line operation
  1320. if(GetServerSign() == TRUE)
  1321. {
  1322. // Send message from queue
  1323. if((req_SendQueue == 1) && (isWebsocketSendable || ((queueOpInfo.TransactionMessageResend > 1) && (queueOpInfo.PreTransactionMessageResend != queueOpInfo.TransactionMessageResend))))
  1324. {
  1325. queue_operation(QUEUE_OPERATION_SENT, "", "");
  1326. req_SendQueue = 0;
  1327. queueOpInfo.PreTransactionMessageResend = queueOpInfo.TransactionMessageResend;
  1328. }
  1329. // PING packet
  1330. if(isWebsocketSendable && (0 < GetWebSocketPingInterval()) && ((GetWebSocketPingInterval()+counterPingSend) <= getDiffSecNow(startTime.pingOn)))
  1331. {
  1332. lws_callback_on_writable(wsi_client);
  1333. counterPingSend++;
  1334. }
  1335. if(GetHeartBeatWithNOResponse() >= 30)
  1336. {
  1337. lws_context_destroy(context);
  1338. ConnectionEstablished = 0;
  1339. context = NULL;
  1340. SetHeartBeatWithNOResponse();
  1341. DEBUG_WARN("Heartbeat re-send over 30 count.\n");
  1342. }
  1343. if((GetOcppConnStatus() == 0))
  1344. {
  1345. if(getDiffSecNow(startTime.reConnect) >= 3)
  1346. {
  1347. DEBUG_INFO("GetOcppConnStatus() = %d\n", GetOcppConnStatus());
  1348. lws_context_destroy(context);
  1349. ConnectionEstablished = 0;
  1350. context = NULL;
  1351. }
  1352. }
  1353. else
  1354. {
  1355. refreshStartTimer(&startTime.reConnect);
  1356. }
  1357. refreshStartTimer(&startTime.disconnectServer);
  1358. }
  1359. }
  1360. do
  1361. {
  1362. lws_service(context, 0);//timeout_ms
  1363. }while((SendBufLen>0) && (context!=NULL) && GetOcppConnStatus());
  1364. refreshProcDogTimer();
  1365. usleep(100000);
  1366. }
  1367. pthread_join(tid_ProcQueue, NULL);
  1368. pthread_join(tid_Watchdog, NULL);
  1369. return FAIL;
  1370. }