Module_OcppBackend.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937
  1. #include <sys/time.h>
  2. #include <sys/timeb.h>
  3. #include <sys/types.h>
  4. #include <sys/stat.h>
  5. #include <sys/ioctl.h>
  6. #include <sys/socket.h>
  7. #include <sys/ipc.h>
  8. #include <sys/shm.h>
  9. #include <sys/shm.h>
  10. #include <sys/mman.h>
  11. #include <linux/wireless.h>
  12. #include <linux/sockios.h>
  13. #include <linux/socket.h>
  14. #include <arpa/inet.h>
  15. #include <netinet/in.h>
  16. #include <unistd.h>
  17. #include <stdarg.h>
  18. #include <stdio.h>
  19. #include <stdlib.h>
  20. #include <unistd.h>
  21. #include <fcntl.h>
  22. #include <termios.h>
  23. #include <errno.h>
  24. #include <string.h>
  25. #include <time.h>
  26. #include <ctype.h>
  27. #include <ifaddrs.h>
  28. #include <json_config.h>
  29. #include <json_object.h>
  30. #include <json_tokener.h>
  31. #include <libwebsockets.h>
  32. #include <lws_config.h>
  33. #include "JsonParser.h"
  34. #include "hashmap.h"
  35. #include "HashTable.h"
  36. #include "SystemLogMessage.h"
  37. #include "ShareMemory.h"
  38. #include <pthread.h>
  39. #include "MessageHandler.h"
  40. #include "sqlite3.h"
  41. #define Debug
  42. //#define ARRAY_SIZE(A) (sizeof(A) / sizeof(A[0]))
  43. #define PASS 1
  44. #define FAIL -1
  45. //==========================================
  46. // Function prototype
  47. //==========================================
  48. void trim(char *s);
  49. int mystrcmp(char *p1,char *p2);
  50. void substr(char *dest, const char* src, unsigned int start, unsigned int cnt);
  51. void getSubStr(char *dest, char* src, char *split, int idx);
  52. void split(char **arr, char *str, const char *del);
  53. int strpos(char *source, char *substr, int skip);
  54. int strposs(char *source, char *substr, int idx);
  55. char *random_uuid( char buf[37] );
  56. static int OCPP16Callback(struct lws *wsi, enum lws_callback_reasons reason, void *user, void *in, size_t len);
  57. char * strchr(const char *p, int ch);
  58. void ClientCoreProfile(HashTable* HandleRequest, HashTable* Handleresponse);
  59. void createq();
  60. int showfront(char *uuid, char *data);
  61. int addq(char *uuid, char *data) ;
  62. int delq();
  63. int sentqueue();
  64. void CheckTransactionPacket(char *uuid);
  65. int queue_operation(int type, char *frontUUID, char *frontData);
  66. //==========================================
  67. // Variables Annoucement
  68. //==========================================
  69. pthread_mutex_t mutex1 = PTHREAD_MUTEX_INITIALIZER;
  70. pthread_mutex_t lock_sentData = PTHREAD_MUTEX_INITIALIZER;
  71. HashTable *tableHandleRequest;
  72. HashTable *tableHandleresponse;
  73. HashTable *tableHandleError ;
  74. extern struct OCPP16Data *ShmOCPP16Data;
  75. struct lws *wsi_client;
  76. struct lws_context *context;
  77. static int sendbuffer = 0;
  78. extern int server_sign;
  79. extern int server_pending;
  80. extern void CheckSystemValue(void);
  81. unsigned char *SendBuffer;
  82. int SendBufLen=(1024*4);//(1024*3);
  83. int ConnectionEstablished=0;
  84. int defaultWaitingTime = 10; //10 second
  85. extern int BootNotificationInterval;
  86. map_t hashMap;
  87. data_struct_t* mapItem;
  88. sqlite3 *db;
  89. char *errMsg = NULL;
  90. struct _node {
  91. char uuid[37];
  92. char data[2000];
  93. struct node *next;
  94. }node;
  95. typedef struct _node *pnode;
  96. pnode front, rear;
  97. struct StartTime
  98. {
  99. unsigned int connect;
  100. unsigned int bootNotification;
  101. }startTime;
  102. static char *createsql = "CREATE TABLE IF NOT EXISTS log_buffer("
  103. "idx integer primary key,"
  104. "user_id text,"
  105. "cmd_sn text,"
  106. "charger_id text,"
  107. "gun_type text,"
  108. "gun_no text,"
  109. "rfid_no text,"
  110. "stime text,"
  111. "etime text,"
  112. "time_len text,"
  113. "s_soc text,"
  114. "e_soc text,"
  115. "stop_reason text,"
  116. "power text,"
  117. "meter_before text,"
  118. "meter_after text,"
  119. "charge_price text,"
  120. "reserve text,"
  121. "surplus_before text,"
  122. "surplus_after text,"
  123. "service_price text,"
  124. "is_pay text,"
  125. "charge_strategy text,"
  126. "charge_parameter text,"
  127. "vin text,"
  128. "vehicle_no text,"
  129. "start_method text,"
  130. "card_type text,"
  131. "is_upload text,"
  132. "guid text UNIQUE,"
  133. "is_buf2OK text);";
  134. static char *createsChargingRecordsql = "CREATE TABLE IF NOT EXISTS ChargingRecord("
  135. "idx integer primary key,"
  136. "gun_type text,"
  137. "connectorId text,"
  138. "idTag text,"
  139. "transactionId text,"
  140. "stime text,"
  141. "etime text,"
  142. "time_len text,"
  143. "s_soc text,"
  144. "e_soc text,"
  145. "stop_reason text,"
  146. "power text,"
  147. "meter_before text,"
  148. "meter_after text,"
  149. "reservationId text,"
  150. "guid text UNIQUE);";
  151. //"is_buf2OK text);";
  152. static char *sqlOcppAuthCache = "create table if not exists ocpp_auth_cache (idx integer primary key,"
  153. "idtag text UNIQUE,"
  154. "parent_idtag text,"
  155. "expir_date text,"
  156. "status text);";
  157. static char *sqlOcppAuthLocal = "create table if not exists ocpp_auth_local (idx integer primary key,"
  158. "idtag text UNIQUE,"
  159. "parent_idtag text,"
  160. "expir_date text,"
  161. "status text,"
  162. "version text);";
  163. static char *sqlInitialOcppAuthLocal = "insert or replace into ocpp_auth_local(idtag, parent_idtag, expir_date, status, version) values('PaHdImHiOnNG','none','2099-12-31T23:59:59.999Z','Accepted','0')";
  164. int DiffTimeb(struct timeb ST, struct timeb ET)
  165. {
  166. //return milli-second
  167. unsigned int StartTime,StopTime;
  168. StartTime=(unsigned int)ST.time;
  169. StopTime=(unsigned int)ET.time;
  170. return (StopTime-StartTime)*1000+ET.millitm-ST.millitm;
  171. }
  172. //=================================
  173. // Common routine
  174. //=================================
  175. void trim(char *s)
  176. {
  177. int i=0, j, k, l=0;
  178. while((s[i]==' ')||(s[i]=='\t')||(s[i]=='\n'))
  179. i++;
  180. j = strlen(s)-1;
  181. while((s[j]==' ')||(s[j]=='\t')||(s[j]=='\n'))
  182. j--;
  183. if(i==0 && j==strlen(s)-1) { }
  184. else if(i==0) s[j+1] = '\0';
  185. else {
  186. for(k=i; k<=j; k++) s[l++] = s[k];
  187. s[l] = '\0';
  188. }
  189. }
  190. int mystrcmp(char *p1,char *p2)
  191. {
  192. while(*p1==*p2)
  193. {
  194. if(*p1=='\0' || *p2=='\0')
  195. break;
  196. p1++;
  197. p2++;
  198. }
  199. if(*p1=='\0' && *p2=='\0')
  200. return(PASS);
  201. else
  202. return(FAIL);
  203. }
  204. void substr(char *dest, const char* src, unsigned int start, unsigned int cnt)
  205. {
  206. strncpy(dest, src + start, cnt);
  207. dest[cnt] = 0;
  208. }
  209. void getSubStr(char *dest, char* src, char *split, int idx)
  210. {
  211. int start = (strposs(src,",",idx)+1);
  212. int cnt = (strposs(src,",",idx+1)-2)-(strposs(src,",",idx)+1);
  213. strncpy(dest, src + start, cnt);
  214. dest[cnt] = 0;
  215. }
  216. void split(char **arr, char *str, const char *del)
  217. {
  218. char *s = strtok(str, del);
  219. while(s != NULL)
  220. {
  221. *arr++ = s;
  222. s = strtok(NULL, del);
  223. }
  224. }
  225. int strpos(char *source, char *substr, int skip)
  226. {
  227. char stack[strlen(source)];
  228. strncpy(stack, source+skip, strlen(source)-skip);
  229. char *p = strstr(stack, substr);
  230. if (p)
  231. return p - stack+skip;
  232. return -1;
  233. }
  234. int strposs(char *source, char *substr, int idx)
  235. {
  236. char stack[strlen(source)];
  237. int result=0;
  238. int count=0;
  239. while(count<=idx)
  240. {
  241. memset(stack,0,sizeof stack);
  242. strncpy(stack, source+result, strlen(source)-result);
  243. int loc = strcspn(stack, substr);
  244. if(loc>0)
  245. result += (loc+1);
  246. else
  247. result = -1;
  248. count++;
  249. }
  250. return result;
  251. }
  252. char *random_uuid( char buf[37] )
  253. {
  254. const char *c = "89ab";
  255. char *p = buf;
  256. int n;
  257. for( n = 0; n < 16; ++n )
  258. {
  259. int b = rand()%255;
  260. switch( n )
  261. {
  262. case 6:
  263. sprintf(p, "4%x", b%15 );
  264. break;
  265. case 8:
  266. sprintf(p, "%c%x", c[rand()%strlen(c)], b%15 );
  267. break;
  268. default:
  269. sprintf(p, "%02x", b);
  270. break;
  271. }
  272. p += 2;
  273. switch( n )
  274. {
  275. case 3:
  276. case 5:
  277. case 7:
  278. case 9:
  279. *p++ = '-';
  280. break;
  281. }
  282. }
  283. *p = 0;
  284. return buf;
  285. }
  286. //==========================================
  287. // Web socket tranceive routine
  288. //==========================================
  289. int SendData(struct lws *wsi)
  290. {
  291. int n;
  292. int len;
  293. unsigned char *out = NULL;
  294. len = strlen((char *)SendBuffer);
  295. out = (unsigned char *)malloc(sizeof(char)*(LWS_SEND_BUFFER_PRE_PADDING + len + LWS_SEND_BUFFER_POST_PADDING));
  296. memcpy (out + LWS_SEND_BUFFER_PRE_PADDING, SendBuffer, len );
  297. n = lws_write(wsi, out + LWS_SEND_BUFFER_PRE_PADDING, len, LWS_WRITE_TEXT);
  298. free(out);
  299. out = NULL;
  300. return n;
  301. }
  302. static int OCPP16Callback(struct lws *wsi, enum lws_callback_reasons reason, void *user, void *in, size_t len)
  303. {
  304. int MsgType;
  305. char UniqueId[37],Action[33],Payload[10241],ErrorCode[129],ErrorDescription[513];
  306. switch (reason)
  307. {
  308. case LWS_CALLBACK_PROTOCOL_INIT:
  309. #ifdef SystemLogMessage
  310. DEBUG_INFO("LWS_CALLBACK_PROTOCOL_INIT\n");
  311. #endif
  312. break;
  313. case LWS_CALLBACK_CLIENT_FILTER_PRE_ESTABLISH:
  314. #ifdef SystemLogMessage
  315. DEBUG_INFO("LWS_CALLBACK_CLIENT_FILTER_PRE_ESTABLISH\n");
  316. #endif
  317. break;
  318. case LWS_CALLBACK_CLOSED_CLIENT_HTTP:
  319. #ifdef SystemLogMessage
  320. DEBUG_INFO("LWS_CALLBACK_CLOSED_CLIENT_HTTP\n");
  321. #endif
  322. break;
  323. case LWS_CALLBACK_WSI_DESTROY:
  324. #ifdef SystemLogMessage
  325. DEBUG_INFO("LWS_CALLBACK_WSI_DESTROY\n");
  326. #endif
  327. server_sign = FALSE;
  328. break;
  329. case LWS_CALLBACK_LOCK_POLL:
  330. #ifdef SystemLogMessage
  331. //DEBUG_INFO("LWS_CALLBACK_LOCK_POLL\n");
  332. #endif
  333. break;
  334. case LWS_CALLBACK_ADD_POLL_FD:
  335. #ifdef SystemLogMessage
  336. DEBUG_INFO("LWS_CALLBACK_ADD_POLL_FD\n");
  337. #endif
  338. break;
  339. case LWS_CALLBACK_DEL_POLL_FD:
  340. #ifdef SystemLogMessage
  341. DEBUG_INFO("LWS_CALLBACK_DEL_POLL_FD\n");
  342. #endif
  343. break;
  344. case LWS_CALLBACK_UNLOCK_POLL:
  345. #ifdef SystemLogMessage
  346. //DEBUG_INFO("LWS_CALLBACK_UNLOCK_POLL\n");
  347. #endif
  348. break;
  349. case LWS_CALLBACK_CHANGE_MODE_POLL_FD:
  350. #ifdef SystemLogMessage
  351. //DEBUG_INFO("LWS_CALLBACK_CHANGE_MODE_POLL_FD\n");
  352. #endif
  353. break;
  354. case LWS_CALLBACK_WSI_CREATE:
  355. #ifdef SystemLogMessage
  356. DEBUG_INFO("LWS_CALLBACK_WSI_CREATE\n");
  357. #endif
  358. break;
  359. case LWS_CALLBACK_GET_THREAD_ID:
  360. //#ifdef SystemLogMessage
  361. //DEBUG_INFO("LWS_CALLBACK_GET_THREAD_ID\n");
  362. //#endif
  363. break;
  364. case LWS_CALLBACK_CLIENT_APPEND_HANDSHAKE_HEADER:
  365. #ifdef SystemLogMessage
  366. DEBUG_INFO("LWS_CALLBACK_CLIENT_APPEND_HANDSHAKE_HEADER\n");
  367. #endif
  368. break;
  369. case LWS_CALLBACK_CLIENT_ESTABLISHED://3
  370. #ifdef SystemLogMessage
  371. DEBUG_INFO("LWS_CALLBACK_CLIENT_ESTABLISHED\n");
  372. #endif
  373. //connected
  374. ConnectionEstablished=1;
  375. break;
  376. case LWS_CALLBACK_CLIENT_CONNECTION_ERROR://1
  377. #ifdef Debug
  378. DEBUG_ERROR("LWS_CALLBACK_CLIENT_CONNECTION_ERROR:%s\n",in);
  379. #endif
  380. #ifdef SystemLogMessage
  381. DEBUG_ERROR("LWS_CALLBACK_CLIENT_CONNECTION_ERROR\n");
  382. #endif
  383. //disconnected
  384. ConnectionEstablished=0;
  385. break;
  386. case LWS_CALLBACK_CLOSED://4
  387. #ifdef SystemLogMessage
  388. DEBUG_INFO("LWS_CALLBACK_CLOSED\n");
  389. ConnectionEstablished=0;
  390. #endif
  391. //disconnected
  392. break;
  393. case LWS_CALLBACK_CLIENT_WRITEABLE://10
  394. //if(need to send message and its relevant data already store into SendBuffer)
  395. #ifdef SystemLogMessage
  396. DEBUG_INFO("LWS_CALLBACK_CLIENT_WRITEABLE\n");
  397. #endif
  398. SendData(wsi);
  399. break;
  400. case LWS_CALLBACK_CLIENT_RECEIVE://8
  401. ((char *)in)[len] = '\0';
  402. #ifdef SystemLogMessage
  403. DEBUG_INFO("LWS_CALLBACK_CLIENT_RECEIVE, RX length: %d\n", (int)len);
  404. //DEBUG_INFO("Message: %s\n", (char *)in);
  405. #endif
  406. ReceivedMessage(in, len);
  407. break;
  408. default:
  409. #ifdef Debug
  410. DEBUG_INFO("OCPP16Callback:reason=%d\n", reason);
  411. #endif
  412. break;
  413. }
  414. return 0;
  415. }
  416. static struct lws_protocols protocols[] = {
  417. {
  418. "ocpp1.6",
  419. OCPP16Callback,
  420. 10240,
  421. 10240,
  422. },
  423. {
  424. "ocpp1.6",
  425. OCPP16Callback,
  426. 10240,
  427. 10240,
  428. },
  429. {
  430. NULL, NULL, 0 /* End of list */
  431. }
  432. };
  433. int ConnectWsServer()
  434. {
  435. int result = PASS;
  436. struct lws_context_creation_info ContextInfo;
  437. struct lws_client_connect_info ConnInfo;
  438. int use_ssl=0;
  439. //lws_set_log_level(LLL_PARSER | LLL_HEADER, NULL);
  440. if(context!=NULL)
  441. lws_context_destroy(context);
  442. memset(&ContextInfo, 0, sizeof(struct lws_context_creation_info));
  443. ContextInfo.port = CONTEXT_PORT_NO_LISTEN;
  444. ContextInfo.iface = NULL;
  445. ContextInfo.ssl_private_key_password = NULL;
  446. ContextInfo.ssl_cert_filepath = NULL;
  447. ContextInfo.ssl_private_key_filepath = NULL;
  448. ContextInfo.ssl_ca_filepath = "/root/cacert.pem";
  449. ContextInfo.ssl_cipher_list = NULL; //use default one
  450. ContextInfo.gid = -1;
  451. ContextInfo.uid = -1;
  452. if(use_ssl)
  453. ContextInfo.options |= LWS_SERVER_OPTION_DO_SSL_GLOBAL_INIT;
  454. ContextInfo.protocols = protocols;
  455. ContextInfo.timeout_secs = 9999;//30;
  456. //if(ping pong enabled)
  457. ContextInfo.ws_ping_pong_interval = 30;//0 for none, else interval in seconds
  458. context = lws_create_context(&ContextInfo);
  459. if (context == NULL)
  460. {
  461. #ifdef SystemLogMessage
  462. DEBUG_ERROR("lws_create_context NG");
  463. #endif
  464. result = FAIL;
  465. }
  466. memset(&ConnInfo,0,sizeof(struct lws_client_connect_info));
  467. // fill up below information
  468. ConnInfo.context = context;
  469. ConnInfo.address=(const char *)GetOcppServerURL();
  470. ConnInfo.port = GetOcppPort();
  471. ConnInfo.path=(const char *)GetOcppPath();
  472. ConnInfo.host=lws_canonical_hostname(context);
  473. ConnInfo.origin="origin";
  474. ConnInfo.protocol = protocols[1].name;
  475. ConnInfo.ietf_version_or_minus_one = -1;
  476. if(use_ssl)
  477. ConnInfo.ssl_connection = LCCSCF_USE_SSL | LCCSCF_ALLOW_SELFSIGNED | LCCSCF_SKIP_SERVER_CERT_HOSTNAME_CHECK;;
  478. wsi_client = lws_client_connect_via_info(&ConnInfo);
  479. if (!wsi_client)
  480. {
  481. #ifdef SystemLogMessage
  482. DEBUG_ERROR("lws_client_connect_via_info NG");
  483. #endif
  484. result = FAIL;
  485. }
  486. printf("6-3\n");
  487. return result;
  488. }
  489. void createq() {
  490. front = rear = (pnode)malloc(sizeof(node));
  491. //front->next = rear->next = NULL;
  492. front = rear = NULL;
  493. }
  494. int showfront(char *uuid, char *data) {
  495. pnode p;
  496. p = front;
  497. if(p)
  498. {
  499. printf("\n uuid:%s", p->uuid);
  500. printf("\n data:%s", p->data);
  501. strcpy(uuid,p->uuid);
  502. strcpy(data,p->data);
  503. return 1;
  504. }
  505. else
  506. {
  507. printf("queue is null\n");
  508. strcpy(uuid,"");
  509. strcpy(data,"");
  510. return 0;
  511. }
  512. }
  513. int addq(char *uuid, char *data) {
  514. pnode p = (pnode )malloc(sizeof(node));
  515. if(p)
  516. {
  517. strcpy(p->uuid, uuid);
  518. strcpy(p->data, data);
  519. p->next = NULL;
  520. if(rear)
  521. {
  522. rear->next = p;
  523. rear = p;
  524. }
  525. else
  526. {
  527. front = rear = p;
  528. }
  529. }
  530. else
  531. {
  532. printf("?¡æ??–å?記憶體空?“新增è??™\n");
  533. }
  534. printf("addq\n");
  535. return 0;
  536. }
  537. int delq() {
  538. pnode p;
  539. if(front)
  540. {
  541. p = front;
  542. if(front->next)
  543. front = front->next;
  544. else
  545. front = rear = NULL;
  546. printf("delete uuid: %s data: %s\n", p->uuid, p->data);
  547. free(p);
  548. }
  549. else
  550. {
  551. printf("queue is Empty\n");
  552. printf("\n delete: queue is null");
  553. }
  554. return 0;
  555. }
  556. int showqueue() {
  557. pnode p;
  558. p = front;
  559. while(p)
  560. {
  561. printf("uuid: %s data: %s\n", p->uuid, p->data);
  562. p = p->next;
  563. }
  564. return 0;
  565. }
  566. int sentqueue(){
  567. pnode p;
  568. p = front;
  569. printf("sentqueue\n");
  570. while(p)
  571. {
  572. printf("uuid: %s data: %s\n", p->uuid, p->data);
  573. LWS_Send(p->data);
  574. p = p->next;
  575. }
  576. printf("sentqueue end\n");
  577. return 0;
  578. }
  579. int storequeue(){
  580. //write queue to flash
  581. unsigned int i,Chk;
  582. unsigned char *ptr = NULL;
  583. int fd,wrd;
  584. // Save factory default setting value to flash backup setting block
  585. fd = open("/Storage/OCPP/queue.txt", O_RDWR);
  586. if (fd < 0)
  587. {
  588. #ifdef SystemLogMessage
  589. DEBUG_ERROR("open /Storage/OCPP/queue.txt NG");
  590. #endif
  591. //free(ptr);
  592. return 0;
  593. }
  594. wrd=write(fd, &front, sizeof(node));
  595. close(fd);
  596. if(wrd!=(sizeof(node)))
  597. {
  598. #ifdef SystemLogMessage
  599. DEBUG_ERROR("write /Storage/OCPP/queue.txt NG");
  600. #endif
  601. //free(ptr);
  602. return 0;
  603. }
  604. return 0;
  605. }
  606. //
  607. void* processTransactionQueue(void* data) {
  608. while(1)
  609. {
  610. char *frontUUID = (char *)malloc(sizeof(char)* 100);
  611. char *frontData = (char *)malloc(sizeof(char)*1024*4);
  612. int queueNotEmpty = 0;
  613. printf("show front\n");
  614. queueNotEmpty = queue_operation(1,frontUUID, frontData);//showfront(frontUUID, frontData); ---> remove temporally
  615. //showfront();
  616. printf("processTransactionQueue\n");
  617. if(queueNotEmpty == 1)
  618. {
  619. printf("processTransactionQueue queue is not empty!\n");
  620. sendbuffer = 1;
  621. }
  622. free(frontUUID);
  623. free(frontData);
  624. sleep(30); // sleep for 30 seconds
  625. }
  626. pthread_exit(NULL); // ���}�l�����
  627. return 0;
  628. }
  629. void CheckTransactionPacket(char *uuid)
  630. {
  631. char *frontUUID = (char *)malloc(sizeof(char)* 100);
  632. char *frontData = (char *)malloc(sizeof(char)*1024*4);
  633. int queueNotEmpty = 0;
  634. int cmpResult = 0;
  635. printf("CheckTransactionPacket 0\n");
  636. queue_operation(0,"","");//showqueue(); ---> remove temporally
  637. queueNotEmpty = queue_operation(1,frontUUID, frontData);//showfront(frontUUID, frontData); ---> remove temporally
  638. printf("CheckTransactionPacket 1\n");
  639. if(queueNotEmpty == 1)
  640. {
  641. cmpResult = strcmp(frontUUID, uuid);
  642. if (cmpResult == 0)
  643. {
  644. printf("All right!\n");
  645. queue_operation(2,"","");//delq(); ---> remove temporally
  646. }
  647. }
  648. free(frontUUID);
  649. free(frontData);
  650. }
  651. /* type: 0 (showqueue ); type: 1(showfront); type: 2(delq) type: 3 (sentqueue) type: 4 (addq) type: 5(store queue to /Storage/OCPP/ )*/
  652. int queue_operation(int type, char *frontUUID, char *frontData)
  653. {
  654. pthread_mutex_lock(&lock_sentData);
  655. int result=0;
  656. if(type == 0)
  657. result = showqueue();
  658. else if(type == 1)
  659. result = showfront(frontUUID, frontData);
  660. else if(type == 2)
  661. result = delq();
  662. else if(type == 3)
  663. result = sentqueue();
  664. else if(type == 4)
  665. result = addq(frontUUID, frontData);
  666. else if(type == 5)
  667. result = storequeue();
  668. pthread_mutex_unlock(&lock_sentData);
  669. return result;
  670. }
  671. char * strchr
  672. (const char *p, int ch)
  673. {
  674. char c;
  675. c = ch;
  676. for (;; ++p) {
  677. if (*p == c)
  678. return ((char *)p);
  679. if (*p == '\0')
  680. return (NULL);
  681. }
  682. /* NOTREACHED */
  683. }
  684. #define SystemLogMessage
  685. //================================================
  686. // Main process
  687. //================================================
  688. int main(void)
  689. {
  690. pthread_t t;
  691. void *ret;
  692. #ifdef SystemLogMessage
  693. DEBUG_INFO("Initialization...\n");
  694. #endif
  695. if(ProcessShareMemory()== FAIL)
  696. {
  697. return FAIL;
  698. }
  699. if((SendBuffer=malloc(SendBufLen))==NULL)
  700. {
  701. #ifdef SystemLogMessage
  702. DEBUG_ERROR("malloc(SendBufLen) NG");
  703. #endif
  704. sleep(5);
  705. return FAIL;
  706. }
  707. printf("2\n");
  708. //inital HandleRequest/HandleResponce/HandleError map table
  709. tableHandleRequest = HashTableNew(3);
  710. tableHandleresponse = HashTableNew(3);
  711. tableHandleError = HashTableNew(3);
  712. printf("3\n");
  713. ClientCoreProfile(tableHandleRequest, tableHandleresponse);
  714. //for uuid map table
  715. hashMap = hashmap_new();
  716. mapItem = malloc(sizeof(data_struct_t));
  717. //create queue
  718. createq();
  719. //Create Process: Resend Transaction
  720. pthread_create(&t, NULL, processTransactionQueue, NULL);
  721. if(sqlite3_open("charger.db", &db))
  722. {
  723. fprintf(stderr, "Can't open database: %s\n", sqlite3_errmsg(db));
  724. sqlite3_close( db );
  725. exit(0);
  726. }
  727. else
  728. {
  729. fprintf(stderr, "Opened database successfully\n");
  730. }
  731. /* 建ç? Table log buffer */
  732. int rc =sqlite3_exec(db, createsql, 0, 0, &errMsg);
  733. if (SQLITE_OK != rc)
  734. {
  735. printf("%s\n",errMsg);
  736. return 0;
  737. }
  738. else
  739. {
  740. fprintf(stderr, "Opened log buffer table successfully\n");
  741. }
  742. /* 建ç? Table OcppAuthCache */
  743. rc =sqlite3_exec(db, sqlOcppAuthCache, 0, 0, &errMsg);
  744. if (SQLITE_OK != rc)
  745. {
  746. printf("%s\n",errMsg);
  747. return 0;
  748. }
  749. else
  750. {
  751. fprintf(stderr, "Opened OcppAuthCache table successfully\n");
  752. }
  753. /* 建ç? Table OcppAuthLocal */
  754. rc =sqlite3_exec(db, sqlOcppAuthLocal, 0, 0, &errMsg);
  755. if (SQLITE_OK != rc)
  756. {
  757. printf("%s\n",errMsg);
  758. return 0;
  759. }
  760. else
  761. {
  762. fprintf(stderr, "Opened OcppAuthLocal table successfully\n");
  763. }
  764. initialConfigurationTable();
  765. for(;;)
  766. {
  767. while(ConnectionEstablished==0)
  768. {
  769. if((time((time_t*)NULL)-startTime.connect)>=60)
  770. {
  771. #ifdef Debug
  772. DEBUG_INFO("Execute ConnectWsServer\n");
  773. #endif
  774. ConnectWsServer();
  775. startTime.connect=time((time_t*)NULL);
  776. }
  777. lws_service(context, 10000);//timeout_ms
  778. }
  779. if(( (BootNotificationInterval != 0 && ((time((time_t*)NULL)-startTime.bootNotification)>=BootNotificationInterval) ) || ((time((time_t*)NULL)- startTime.bootNotification)>=defaultWaitingTime) ) && ((server_sign == FALSE)/*|| (server_pending == TRUE)*/))
  780. {
  781. sendBootNotificationRequest();
  782. startTime.bootNotification=time((time_t*)NULL);
  783. }
  784. if(server_sign == TRUE)
  785. {
  786. printf("sign in 1\n");
  787. if(sendbuffer == 1)
  788. {
  789. queue_operation(3, "", "");
  790. sendbuffer = 0;
  791. queue_operation(5,"", "");
  792. }
  793. // Check System Value
  794. CheckSystemValue();
  795. printf("sign in 2\n");
  796. }
  797. lws_service(context, 100);//timeout_ms
  798. }
  799. pthread_join(t, NULL);
  800. HashTableFree(tableHandleRequest);
  801. HashTableFree(tableHandleresponse);
  802. HashTableFree(tableHandleError);
  803. hashmap_free(hashMap);
  804. free(SendBuffer);
  805. return FAIL;
  806. }