12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658 |
- #include <sys/time.h>
- #include <sys/timeb.h>
- #include <sys/types.h>
- #include <sys/stat.h>
- #include <sys/ioctl.h>
- #include <sys/socket.h>
- #include <sys/ipc.h>
- #include <sys/shm.h>
- #include <sys/mman.h>
- #include <linux/wireless.h>
- #include <linux/sockios.h>
- #include <linux/socket.h>
- #include <arpa/inet.h>
- #include <netinet/in.h>
- #include <unistd.h>
- #include <stdarg.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include <unistd.h>
- #include <fcntl.h>
- #include <termios.h>
- #include <errno.h>
- #include <string.h>
- #include <time.h>
- #include <ctype.h>
- #include <ifaddrs.h>
- #include "libwebsockets.h"
- #include <lws_config.h>
- #include "hashmap.h"
- #include "SystemLogMessage.h"
- #include "ShareMemory.h"
- #include <pthread.h>
- #include "MessageHandler.h"
- #include "sqlite3.h"
- #ifndef SPEC_LATEST_SUPPORTED
- #define SPEC_LATEST_SUPPORTED 13
- #endif
- #define Debug
- //#define ARRAY_SIZE(A) (sizeof(A) / sizeof(A[0]))
- #define PASS 1
- #define FAIL -1
- typedef enum boolean { FALSE, TRUE } BOOL;
- struct lws *wsi_client;
- struct lws_context *context;
- static int sendbuffer = 0;
- extern int server_sign;
- extern void CheckSystemValue(void);
- //extern int TransactionMessageAttemptsGet(void);
- extern int FirstHeartBeatResponse(void);
- extern void OCPP_get_TableAuthlocalAllData(void);
- extern int TransactionMessageAttemptsGet(void);
- extern int TransactionMessageRetryIntervalGet(void);
- extern int GetOcppConnStatus(void);
- extern void SetOcppConnStatus(uint8_t status);
- extern int GetHeartBeatWithNOResponse(void);
- extern void SetHeartBeatWithNOResponse(void);
- pthread_mutex_t mutex1 = PTHREAD_MUTEX_INITIALIZER;
- pthread_mutex_t lock_sentData = PTHREAD_MUTEX_INITIALIZER;
- pthread_mutex_t receiveData = PTHREAD_MUTEX_INITIALIZER;
- //==========================================
- // Function prototype
- //==========================================
- void trim(char *s);
- int mystrcmp(char *p1,char *p2);
- void substr(char *dest, const char* src, unsigned int start, unsigned int cnt);
- void getSubStr(char *dest, char* src, char *split, int idx);
- void split(char **arr, char *str, const char *del);
- int strpos(char *source, char *substr, int skip);
- int strposs(char *source, char *substr, int idx);
- char *random_uuid( char buf[37] );
- static int OCPP16Callback(struct lws *wsi, enum lws_callback_reasons reason, void *user, void *in, size_t len);
- char * strchr(const char *p, int ch);
- void ReceivedMessage(void *in, size_t len);
- struct StartTime
- {
- unsigned int connect;
- unsigned int bootNotification;
- }startTime;
- #if 0
- unsigned char *SendBuffer;
- int SendBufLen=(1024*4);//(1024*3);
- #endif
- int SendBufLen=(1024*4);//(1024*3);
- unsigned char SendBuffer[1024*4]={0};
- static int ConnectionEstablished=0;
- static int TransactionMessageResend = 1;
- static int TransactionQueueInterval = 10;//3; // TransactionMessageAttempts
- static int TransactionQueueNum = 0;
- static int OfflineTransaction = 0;
- int defaultWaitingTime = 10; //10 second
- char OcppPath[160]={0};
- char OcppProtocol[10]={0},OcppHost[50]={0}, OcppTempPath[50]={0};
- //===============================
- // OCPP SentQueue TransactionId
- //===============================
- static int SentQueueTransactionId[10]={0};
- extern int BootNotificationInterval;
- //char guid[37];
- //map_t hashMap;
- //data_struct_t* mapItem; --- remove for temporally
- //data_struct_t mapItem[0]={0};
- sqlite3 *db;
- char *errMsg = NULL;
- static char *createsql = "CREATE TABLE IF NOT EXISTS log_buffer("
- "idx integer primary key,"
- "user_id text,"
- "cmd_sn text,"
- "charger_id text,"
- "gun_type text,"
- "gun_no text,"
- "rfid_no text,"
- "stime text,"
- "etime text,"
- "time_len text,"
- "s_soc text,"
- "e_soc text,"
- "stop_reason text,"
- "power text,"
- "meter_before text,"
- "meter_after text,"
- "charge_price text,"
- "reserve text,"
- "surplus_before text,"
- "surplus_after text,"
- "service_price text,"
- "is_pay text,"
- "charge_strategy text,"
- "charge_parameter text,"
- "vin text,"
- "vehicle_no text,"
- "start_method text,"
- "card_type text,"
- "is_upload text,"
- "guid text UNIQUE,"
- "is_buf2OK text);";
- #if 0
- static char *createsChargingRecordsql = "CREATE TABLE IF NOT EXISTS ChargingRecord("
- "idx integer primary key,"
- "gun_type text,"
- "connectorId text,"
- "idTag text,"
- "transactionId text,"
- "stime text,"
- "etime text,"
- "time_len text,"
- "s_soc text,"
- "e_soc text,"
- "stop_reason text,"
- "power text,"
- "meter_before text,"
- "meter_after text,"
- "reservationId text,"
- "guid text UNIQUE);";
- //"is_buf2OK text);";
- #endif
- static char *sqlOcppAuthCache = "create table if not exists ocpp_auth_cache (idx integer primary key,"
- "idtag text UNIQUE,"
- "parent_idtag text,"
- "expir_date text,"
- "status text);";
- static char *sqlOcppAuthLocal = "create table if not exists ocpp_auth_local (idx integer primary key,"
- "idtag text UNIQUE,"
- "parent_idtag text,"
- "expir_date text,"
- "status text,"
- "version text);";
- #if 0
- 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')";
- #endif
- int DiffTimeb(struct timeb ST, struct timeb ET)
- {
- //return milli-second
- unsigned int StartTime,StopTime;
- StartTime=(unsigned int)ST.time;
- StopTime=(unsigned int)ET.time;
- return (StopTime-StartTime)*1000+ET.millitm-ST.millitm;
- }
- //=================================
- // Common routine
- //=================================
- void trim(char *s)
- {
- int i=0, j, k, l=0;
- while((s[i]==' ')||(s[i]=='\t')||(s[i]=='\n'))
- i++;
- j = strlen(s)-1;
- while((s[j]==' ')||(s[j]=='\t')||(s[j]=='\n'))
- j--;
- if(i==0 && j==strlen(s)-1) { }
- else if(i==0) s[j+1] = '\0';
- else {
- for(k=i; k<=j; k++) s[l++] = s[k];
- s[l] = '\0';
- }
- }
- int mystrcmp(char *p1,char *p2)
- {
- while(*p1==*p2)
- {
- if(*p1=='\0' || *p2=='\0')
- break;
- p1++;
- p2++;
- }
- if(*p1=='\0' && *p2=='\0')
- return(PASS);
- else
- return(FAIL);
- }
- void substr(char *dest, const char* src, unsigned int start, unsigned int cnt)
- {
- strncpy(dest, src + start, cnt);
- dest[cnt] = 0;
- }
- void getSubStr(char *dest, char* src, char *split, int idx)
- {
- int start = (strposs(src,",",idx)+1);
- int cnt = (strposs(src,",",idx+1)-2)-(strposs(src,",",idx)+1);
- strncpy(dest, src + start, cnt);
- dest[cnt] = 0;
- }
- void split(char **arr, char *str, const char *del)
- {
- char *s = strtok(str, del);
- while(s != NULL)
- {
- *arr++ = s;
- s = strtok(NULL, del);
- }
- }
- int strpos(char *source, char *substr, int skip)
- {
- char stack[strlen(source)];
- strncpy(stack, source+skip, strlen(source)-skip);
- char *p = strstr(stack, substr);
- if (p)
- return p - stack+skip;
- return -1;
- }
- int strposs(char *source, char *substr, int idx)
- {
- char stack[strlen(source)];
- int result=0;
- int count=0;
- while(count<=idx)
- {
- memset(stack,0,sizeof stack);
- strncpy(stack, source+result, strlen(source)-result);
- int loc = strcspn(stack, substr);
- if(loc>0)
- result += (loc+1);
- else
- result = -1;
- count++;
- }
- return result;
- }
- //static int testnum = 0;
- char *random_uuid( char buf[37] )
- {
- const char *c = "89ab";
- char *p = buf;
- int n;
- #if 0 // test
- time_t seconds;
- seconds = time(NULL);
- testnum = testnum % 999;
- testnum = testnum + 1;
- //printf("\n testnum:%d\n", testnum);
- //printf("\n seconds:%d\n", seconds);
- sprintf(p, "%03d%034d", testnum, seconds);
- //printf("\n p:%s\n", p);
- #endif
- #if 1
- for( n = 0; n < 16; ++n )
- {
- int b = rand()%255;
- switch( n )
- {
- case 6:
- sprintf(p, "4%x", b%15 );
- break;
- case 8:
- sprintf(p, "%c%x", c[rand()%strlen(c)], b%15 );
- break;
- default:
- sprintf(p, "%02x", b);
- break;
- }
- p += 2;
- switch( n )
- {
- case 3:
- case 5:
- case 7:
- case 9:
- *p++ = '-';
- break;
- }
- }
- *p = 0;
- #endif
- return buf;
- }
- //==========================================
- // Web socket tranceive routine
- //==========================================
- int SendData(struct lws *wsi)
- {
- int n;
- int len;
- char *ret;
- unsigned char out[LWS_SEND_BUFFER_PRE_PADDING + 4096 + LWS_SEND_BUFFER_POST_PADDING] = {0};
- len = strlen((char *)SendBuffer);
- if(len == 0)
- return 0;
- memcpy (out + LWS_SEND_BUFFER_PRE_PADDING, SendBuffer, len );
- ret = strstr((const char *)(out + LWS_SEND_BUFFER_PRE_PADDING), "Heartbeat");
- if(ret == NULL)
- {
- DEBUG_OCPPMESSAGE_INFO(" SendData= %s\n", out + LWS_SEND_BUFFER_PRE_PADDING);
- }
- n = lws_write(wsi, out + LWS_SEND_BUFFER_PRE_PADDING, len, LWS_WRITE_TEXT);
- memset(SendBuffer, 0, len);
- return n;
- }
- static int OCPP16Callback(struct lws *wsi, enum lws_callback_reasons reason, void *user, void *in, size_t len)
- {
- switch (reason)
- {
- case LWS_CALLBACK_PROTOCOL_INIT:
- #ifdef SystemLogMessage
- DEBUG_INFO("LWS_CALLBACK_PROTOCOL_INIT\n");
- #endif
- break;
- case LWS_CALLBACK_CLIENT_FILTER_PRE_ESTABLISH:
- #ifdef SystemLogMessage
- DEBUG_INFO("LWS_CALLBACK_CLIENT_FILTER_PRE_ESTABLISH\n");
- #endif
- char buf[64];
- char hash[20], key_b64[40];
- lws_b64_encode_string(hash, 16, key_b64, sizeof(key_b64));// Sec-WebSocket-Key
- DEBUG_OCPPMESSAGE_INFO("------------------Handshake: Client Request START----------------\n");
- lws_hdr_copy(wsi, buf, sizeof(buf) - 1, _WSI_TOKEN_CLIENT_URI);
- DEBUG_OCPPMESSAGE_INFO("GET %s HTTP/1.1 \n", buf);
- lws_hdr_copy(wsi, buf, sizeof(buf) - 1, _WSI_TOKEN_CLIENT_HOST);
- DEBUG_OCPPMESSAGE_INFO("Host: %s\n", buf);
- //lws_hdr_copy(wsi, buf, sizeof(buf) - 1, _WSI_TOKEN_CLIENT_PEER_ADDRESS);
- //DEBUG_OCPPMESSAGE_INFO("_WSI_TOKEN_CLIENT_PEER_ADDRESS %s\n", buf);
- DEBUG_OCPPMESSAGE_INFO("Upgrade: websocket\n");
- DEBUG_OCPPMESSAGE_INFO("Connection: Upgrade\n");
- DEBUG_OCPPMESSAGE_INFO("Sec-WebSocket-Key: %s\n", key_b64);
- lws_hdr_copy(wsi, buf, sizeof(buf) - 1, _WSI_TOKEN_CLIENT_SENT_PROTOCOLS);
- DEBUG_OCPPMESSAGE_INFO("Sec-WebSocket-Protocol: %s\n", buf);
- DEBUG_OCPPMESSAGE_INFO("Sec-WebSocket-Version: %d\n", SPEC_LATEST_SUPPORTED);
- DEBUG_OCPPMESSAGE_INFO("------------------Handshake: Client Request END----------------\n");
- DEBUG_OCPPMESSAGE_INFO("------------------Handshake: Server response START----------------\n");
- lws_hdr_copy(wsi, buf, sizeof(buf) - 1, WSI_TOKEN_HTTP);
- DEBUG_OCPPMESSAGE_INFO("HTTP/1.1 %s\n", buf);
- lws_hdr_copy(wsi, buf, sizeof(buf) - 1, WSI_TOKEN_UPGRADE);
- DEBUG_OCPPMESSAGE_INFO("Upgrade: %s\n", buf);
- lws_hdr_copy(wsi, buf, sizeof(buf) - 1, WSI_TOKEN_CONNECTION);
- DEBUG_OCPPMESSAGE_INFO("Connection: %s\n", buf);
- lws_hdr_copy(wsi, buf, sizeof(buf) - 1, WSI_TOKEN_ACCEPT);
- DEBUG_OCPPMESSAGE_INFO("Sec-WebSocket-Accept: %s\n", buf);
- lws_hdr_copy(wsi, buf, sizeof(buf) - 1, WSI_TOKEN_PROTOCOL);
- DEBUG_OCPPMESSAGE_INFO("Sec-WebSocket-Protocol: %s\n", buf);
- DEBUG_OCPPMESSAGE_INFO("------------------Handshake: Server response END----------------\n");
- break;
- case LWS_CALLBACK_FILTER_PROTOCOL_CONNECTION:
- #ifdef SystemLogMessage
- DEBUG_INFO("LWS_CALLBACK_FILTER_PROTOCOL_CONNECTION\n");
- #endif
- break;
- case LWS_CALLBACK_WSI_DESTROY:
- #ifdef SystemLogMessage
- DEBUG_INFO("LWS_CALLBACK_WSI_DESTROY\n");
- #endif
- server_sign = FALSE;
- break;
- case LWS_CALLBACK_LOCK_POLL:
- break;
- case LWS_CALLBACK_ADD_POLL_FD:
- #ifdef SystemLogMessage
- DEBUG_INFO("LWS_CALLBACK_ADD_POLL_FD\n");
- #endif
- break;
- case LWS_CALLBACK_DEL_POLL_FD:
- #ifdef SystemLogMessage
- DEBUG_INFO("LWS_CALLBACK_DEL_POLL_FD\n");
- #endif
- break;
- case LWS_CALLBACK_UNLOCK_POLL:
- break;
- case LWS_CALLBACK_CHANGE_MODE_POLL_FD:
- break;
- case LWS_CALLBACK_WSI_CREATE:
- #ifdef SystemLogMessage
- DEBUG_INFO("LWS_CALLBACK_WSI_CREATE\n");
- #endif
- break;
- case LWS_CALLBACK_GET_THREAD_ID:
- break;
- case LWS_CALLBACK_CLIENT_APPEND_HANDSHAKE_HEADER:
- {
- #ifdef SystemLogMessage
- DEBUG_INFO("LWS_CALLBACK_CLIENT_APPEND_HANDSHAKE_HEADER\n");
- #endif
- #if 0
- unsigned char **p, *end;
- struct lws *parent;
- char buf[8192];
- parent = lws_get_parent(wsi);
- if (!parent)
- break;
- p = (unsigned char **)in;
- end = (*p) + len;
- #endif
- }
- break;
- case LWS_CALLBACK_CLIENT_ESTABLISHED://3
- #ifdef SystemLogMessage
- DEBUG_INFO("LWS_CALLBACK_CLIENT_ESTABLISHED\n");
- #endif
- //connected
- ConnectionEstablished=1;
- break;
- case LWS_CALLBACK_CLIENT_CONNECTION_ERROR://1
- #ifdef SystemLogMessage
- DEBUG_ERROR("LWS_CALLBACK_CLIENT_CONNECTION_ERROR %s\n", (char *)in );
- #endif
- //disconnected
- ConnectionEstablished=0;
- char buf1[64];
- char hash1[20]={0}, key_b641[40]={0};
- lws_b64_encode_string(hash1, 16, key_b641, sizeof(key_b641));// Sec-WebSocket-Key
- DEBUG_OCPPMESSAGE_INFO("------------------Handshake: Client START----------------\n");
- lws_hdr_copy(wsi, buf, sizeof(buf) - 1, _WSI_TOKEN_CLIENT_URI);
- DEBUG_OCPPMESSAGE_INFO("GET %s HTTP/1.1 \n", buf);
- lws_hdr_copy(wsi, buf, sizeof(buf) - 1, _WSI_TOKEN_CLIENT_HOST);
- DEBUG_OCPPMESSAGE_INFO("Host: %s\n", buf);
- //lws_hdr_copy(wsi, buf, sizeof(buf) - 1, _WSI_TOKEN_CLIENT_PEER_ADDRESS);
- //DEBUG_OCPPMESSAGE_INFO("_WSI_TOKEN_CLIENT_PEER_ADDRESS %s\n", buf);
- DEBUG_OCPPMESSAGE_INFO("Upgrade: websocket\n");
- DEBUG_OCPPMESSAGE_INFO("Connection: Upgrade\n");
- DEBUG_OCPPMESSAGE_INFO("Sec-WebSocket-Key: %s\n", key_b641);
- lws_hdr_copy(wsi, buf, sizeof(buf) - 1, _WSI_TOKEN_CLIENT_SENT_PROTOCOLS);
- DEBUG_OCPPMESSAGE_INFO("Sec-WebSocket-Protocol: %s\n", buf);
- DEBUG_OCPPMESSAGE_INFO("Sec-WebSocket-Version: %d\n", SPEC_LATEST_SUPPORTED);
- DEBUG_OCPPMESSAGE_INFO("------------------Handshake: Client END----------------\n");
- DEBUG_OCPPMESSAGE_INFO("------------------Handshake: Server response START----------------\n");
- lws_hdr_copy(wsi, buf1, sizeof(buf1) - 1, WSI_TOKEN_HTTP);
- DEBUG_OCPPMESSAGE_INFO("HTTP/1.1 %s\n", buf1);
- lws_hdr_copy(wsi, buf1, sizeof(buf1) - 1, WSI_TOKEN_UPGRADE);
- DEBUG_OCPPMESSAGE_INFO("Upgrade: %s\n", buf1);
- lws_hdr_copy(wsi, buf1, sizeof(buf1) - 1, WSI_TOKEN_CONNECTION);
- DEBUG_OCPPMESSAGE_INFO("Connection: %s\n", buf1);
- lws_hdr_copy(wsi, buf1, sizeof(buf1) - 1, WSI_TOKEN_ACCEPT);
- DEBUG_OCPPMESSAGE_INFO("Sec-WebSocket-Accept: %s\n", buf1);
- lws_hdr_copy(wsi, buf1, sizeof(buf1) - 1, WSI_TOKEN_PROTOCOL);
- DEBUG_OCPPMESSAGE_INFO("Sec-WebSocket-Protocol: %s\n", buf1);
- DEBUG_OCPPMESSAGE_INFO("------------------Handshake: Server response END----------------\n");
- break;
- case LWS_CALLBACK_CLOSED://4
- #ifdef SystemLogMessage
- DEBUG_INFO("LWS_CALLBACK_CLOSED\n");
- ConnectionEstablished=0;
- #endif
- //disconnected
- break;
- case LWS_CALLBACK_CLIENT_WRITEABLE://10
- //if(need to send message and its relevant data already store into SendBuffer)
- #ifdef SystemLogMessage
- //DEBUG_INFO("LWS_CALLBACK_CLIENT_WRITEABLE\n");
- #endif
- SendData(wsi);
- break;
- case LWS_CALLBACK_CLIENT_RECEIVE://8
- ((char *)in)[len] = '\0';
- //Print Out Adjudgement
- {
- char *ret, *ret1;
- ret = strstr((const char *)in, "currentTime");
- ret1 = strstr((const char *)in, "interval");
- if(!((ret != NULL)&&(ret1 == NULL)))
- {
- DEBUG_OCPPMESSAGE_INFO("Original Receive %s\n", (char *)in);
- }
- }
- char tempin[1024*4]={0};
- int c = 0;
- char *loc;
- char sstr[600]={ 0 };
- //**********Receive Message**********/
- c = 0;
- loc = strstr((const char *)in, "][2,");
- if(loc == NULL)
- {
- loc = strstr((const char *)in, "][3,");
- if(loc == NULL)
- {
- loc = strstr((const char *)in, "][4,");
- }
- }
- memset(sstr ,0, sizeof(sstr) );
- if(loc != NULL)
- {
- DEBUG_INFO("there are continuous second packet []\n");
- while (loc[1+c] != '\0')
- {
- sstr[c] = loc[1+c];
- c++;
- }
- sstr[c] = '\0';
- strcpy(tempin, sstr);
- DEBUG_INFO("Final Receive: %s\n", tempin);
- }
- else
- {
- strcpy(tempin,(char *)in);
- }
- ReceivedMessage((void *)tempin, len);
- break;
- default:
- #ifdef Debug
- DEBUG_INFO("OCPP16Callback:reason=%d\n", reason);
- #endif
- break;
- }
- return 0;
- }
- static struct lws_protocols protocols[] = {
- {
- "ocpp1.6",
- OCPP16Callback,
- 10240,
- 10240,
- },
- {
- "ocpp1.6",
- OCPP16Callback,
- 10240,
- 10240,
- },
- {
- NULL, NULL, 0 /* End of list */
- }
- };
- int ConnectWsServer()
- {
- int result = PASS;
- struct lws_context_creation_info ContextInfo;
- struct lws_client_connect_info ConnInfo;
- int use_ssl=0;
- //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);
- if(context!=NULL)
- lws_context_destroy(context);
- memset(&ContextInfo, 0, sizeof(struct lws_context_creation_info));
- if((GetOcppServerURL()==0) || (GetOcppPort() == 0) || (GetOcppPath()==0) )
- {
- result = FAIL;
- DEBUG_ERROR("OCPP URL is NULL or OCPP Port is zero or OCPP Path is NULL\n");
- return result;
- }
- if((strcmp(OcppProtocol,"ws")==0)&&(strlen(OcppProtocol)== 2))
- {
- printf("ws!!! \n");
- use_ssl=0;
- }
- else if((strcmp(OcppProtocol,"wss")==0)&&(strlen(OcppProtocol)== 3))
- {
- printf("wss!!! \n");
- use_ssl=1;
- }
- ContextInfo.port = CONTEXT_PORT_NO_LISTEN;
- ContextInfo.iface = NULL;
- ContextInfo.ssl_private_key_password = NULL;
- ContextInfo.ssl_cert_filepath = NULL;//"./ssl_key/client_cert.pem";
- ContextInfo.ssl_private_key_filepath = NULL;//"./ssl_key/client_key.pem";
- ContextInfo.ssl_ca_filepath = "/root/cacert.pem";//"./cacert.pem";
- ContextInfo.ssl_cipher_list = NULL; //use default one
- ContextInfo.gid = -1;
- ContextInfo.uid = -1;
- if(use_ssl)
- {
- ContextInfo.options |= LWS_SERVER_OPTION_DO_SSL_GLOBAL_INIT ;
- }
- ContextInfo.protocols = protocols;
- ContextInfo.timeout_secs = 9999;//30;
- //if(ping pong enabled)
- ContextInfo.ws_ping_pong_interval = 30;//0 for none, else interval in seconds
- context = lws_create_context(&ContextInfo);
- if (context == NULL)
- {
- #ifdef SystemLogMessage
- DEBUG_ERROR("lws_create_context NG");
- #endif
- result = FAIL;
- }
- memset(&ConnInfo,0,sizeof(struct lws_client_connect_info));
- // fill up below information
- ConnInfo.context = context;
- ConnInfo.address=(const char *)OcppHost;
- printf("\n ConnInfo.address=%s\n",ConnInfo.address);
- ConnInfo.port = GetOcppPort();
- printf("\n ConnInfo.port=%d\n",ConnInfo.port);
- ConnInfo.path=(const char *)OcppPath;
- printf("\n ConnInfo.path=%s\n",ConnInfo.path);
- ConnInfo.host=ConnInfo.address;//lws_canonical_hostname(context);
- ConnInfo.origin="origin";
- ConnInfo.protocol = protocols[1].name;
- ConnInfo.ietf_version_or_minus_one = -1;
- if(use_ssl)
- ConnInfo.ssl_connection = LCCSCF_USE_SSL | LCCSCF_ALLOW_SELFSIGNED | LCCSCF_SKIP_SERVER_CERT_HOSTNAME_CHECK;;
- wsi_client = lws_client_connect_via_info(&ConnInfo);
- if (!wsi_client)
- {
- #ifdef SystemLogMessage
- DEBUG_ERROR("lws_client_connect_via_info NG");
- #endif
- result = FAIL;
- }
- return result;
- }
- //void createq();
- int showfront(char *uuid, char *data);
- int addq(char *uuid, char *data) ;
- int delq();
- int sentqueue();
- void CheckTransactionPacket(char *uuid);
- int queue_operation(int type, char *frontUUID, char *frontData);
- int showfront(char *uuid, char *data) {
- FILE *fp;
- int result = FALSE; // 1: TRUE 0:FALSE
- char str[1200]={0};
- char sstr[50]={ 0 };//sstr[200]={ 0 };
- int c = 0;
- char *loc;
- char rmFileCmd[100]={0};
- struct stat stats;
- stat("/Storage/OCPP", &stats);
- // Check for directory existence
- if (S_ISDIR(stats.st_mode) == 1)
- {
- //DEBUG_INFO("\n OCPP directory exist \n");
- }
- else
- {
- DEBUG_INFO("\n OCPP directory not exist, create dir \n");
- sprintf(rmFileCmd,"mkdir -p %s","/Storage/OCPP");
- system(rmFileCmd);
- }
- memset(rmFileCmd, 0, sizeof rmFileCmd);
- if((access("/Storage/OCPP/TransactionRelatedQueue",F_OK))!=-1)
- {
- //DEBUG_INFO("TransactionRelatedQueue exist.\n");
- }
- else
- {
- //DEBUG_INFO("TransactionRelatedQueue not exist\n");
- FILE *log = fopen("/Storage/OCPP/TransactionRelatedQueue", "w+");
- if(log == NULL)
- {
- DEBUG_INFO("Can't Create File TransactionRelatedQueue \n");
- return 0;
- }
- else
- {
- fclose(log);
- }
- }
- /* opening file for reading */
- fp = fopen("/Storage/OCPP/TransactionRelatedQueue" , "r");
- if(fp == NULL) {
- DEBUG_INFO("Error opening TransactionRelatedQueue file");
- return FALSE;
- }
- #if 0
- if(fgetc(fp)==EOF)
- {
- //DEBUG_INFO("It is end of file");
- fclose(fp);
- memset(rmFileCmd, 0, sizeof rmFileCmd);
- if((access("/Storage/OCPP/TransactionRelatedQueue",F_OK))!=-1)
- {
- sprintf(rmFileCmd,"rm -f %s","/Storage/OCPP/TransactionRelatedQueue");
- system(rmFileCmd);
- }
- result = FALSE;
- return result;
- }
- #endif
- if( fgets (str, 1200, fp)!=NULL ) {
- /* writing content to stdout */
- //DEBUG_INFO("str=%s",str);
- if ((str[0] == '\n')||(strcmp(str,"")==0))
- {
- DEBUG_INFO("It is a blank line");
- fclose(fp);
- memset(rmFileCmd, 0, sizeof rmFileCmd);
- sprintf(rmFileCmd,"rm -f %s","/Storage/OCPP/TransactionRelatedQueue");
- system(rmFileCmd);
- result = FALSE;
- return result;
- }
- else
- {
- //puts(str);
- /*********************uuid***************/
- loc = strstr(str, "\"");
- memset(sstr ,0, sizeof(sstr) );
- c = 0;
- while (loc[1+c] != '\"')
- {
- sstr[c] = loc[1+c];
- c++;
- }
- sstr[c] = '\0';
- //DEBUG_INFO("\n uuid:%s", sstr);
- //DEBUG_INFO("\n data:%s", str);
- strcpy(uuid,sstr);
- strcpy(data,str);
- result = TRUE;
- }
- }
- else
- {
- //DEBUG_INFO("queue is null\n");
- strcpy(uuid,"");
- strcpy(data,"");
- result = FALSE;
- }
- fclose(fp);
- return result;
- }
- int addq(char *uuid, char *data) {
- FILE *outfile;
- char rmFileCmd[100]={0};
- struct stat stats;
- stat("/Storage/OCPP", &stats);
- DEBUG_INFO("addq\n");
- // Check for directory existence
- if (S_ISDIR(stats.st_mode) == 1)
- {
- DEBUG_INFO("\n OCPP directory exist \n");
- }
- else
- {
- //DEBUG_INFO("\n OCPP directory not exist, create dir \n");
- sprintf(rmFileCmd,"mkdir -p %s","/Storage/OCPP");
- system(rmFileCmd);
- }
- memset(rmFileCmd, 0, sizeof rmFileCmd);
- if((access("/Storage/OCPP/TransactionRelatedQueue",F_OK))!=-1)
- {
- //DEBUG_INFO("TransactionRelatedQueue exist.\n");
- }
- else
- {
- //DEBUG_INFO("TransactionRelatedQueue not exist\n");
- FILE *log = fopen("/Storage/OCPP/TransactionRelatedQueue", "w+");
- if(log == NULL)
- {
- DEBUG_INFO("Can't Create File TransactionRelatedQueue \n");
- return 0;
- }
- else
- {
- fclose(log);
- }
- }
- // open file for writing
- outfile = fopen ("/Storage/OCPP/TransactionRelatedQueue", "a");
- //DEBUG_INFO("data=%s\n",data);
- fputs(data, outfile);
- fputs("\n", outfile);
- fclose (outfile);
- TransactionQueueNum = TransactionQueueNum + 1;
- return 0;
- }
- int delq() {
- char tempfile[] = "/Storage/OCPP/temp.json";
- FILE *infile;
- FILE *outfile;
- int resultRename=0;
- char filename[60]={0};
- char rmFileCmd[100]={0};
- struct stat stats;
- stat("/Storage/OCPP", &stats);
- DEBUG_INFO("delq()\n");
- // Check for directory existence
- if (S_ISDIR(stats.st_mode) == 1)
- {
- DEBUG_INFO("\n OCPP directory exist \n");
- }
- else
- {
- DEBUG_INFO("\n OCPP directory not exist, create dir \n");
- sprintf(rmFileCmd,"mkdir -p %s","/Storage/OCPP");
- system(rmFileCmd);
- }
- memset(rmFileCmd, 0, sizeof rmFileCmd);
- if((access("/Storage/OCPP/TransactionRelatedQueue",F_OK))!=-1)
- {
- //DEBUG_INFO("TransactionRelatedQueue exist.\n");
- }
- else
- {
- //DEBUG_INFO("TransactionRelatedQueue not exist\n");
- FILE *log = fopen("/Storage/OCPP/TransactionRelatedQueue", "w+");
- if(log == NULL)
- {
- DEBUG_INFO("log is NULL\n");
- return 0;
- }
- else
- {
- fclose(log);
- }
- }
- // open file for writing
- strcpy(filename, "/Storage/OCPP/TransactionRelatedQueue");
- infile = fopen ("/Storage/OCPP/TransactionRelatedQueue", "r");
- outfile = fopen (tempfile, "w");
- /*检测到文件结束标识返回1,否则返回0。*/
- //DEBUG_INFO("feof(infile) =%d\n",feof(infile));
- int c;
- c = fgetc(infile);
- //printf("file c:%d\n",c);
- rewind(infile);
- if(c == EOF)
- {
- //DEBUG_INFO("TransactionRelatedQueue is NULL\n");
- fclose(infile);
- fclose(outfile);
- sprintf(rmFileCmd,"rm -f %s",tempfile);
- system(rmFileCmd);
- }
- else
- {
- char buf[1200]={0};
- int i = 0;
- //DEBUG_INFO("Orignal File is not NULL\n");
- while (fgets(buf, sizeof(buf), infile) != NULL)
- {
- //printf("Orignal File get strings \n");
- buf[strlen(buf) - 1] = '\0'; // eat the newline fgets() stores
- if(i==0)
- {
- TransactionQueueNum = TransactionQueueNum - 1;
- TransactionQueueInterval = 0;
- TransactionMessageResend = 1;
- }
- if(i != 0)
- fprintf(outfile,"%s\n", buf);
- i = i + 1;
- }
- fclose(infile);
- fclose(outfile);
- sprintf(rmFileCmd,"rm -f %s",filename);
- system(rmFileCmd);
- resultRename = rename(tempfile, filename);
- if(resultRename == 0)
- {
- //DEBUG_INFO("TransactionRelatedQueue file renamed successfully");
- }
- else
- {
- //DEBUG_INFO("Error: unable to rename the TransactionRelatedQueue file");
- }
- }
- return 0;
- }
- int showqueue() {
- char rmFileCmd[100]={0};
- struct stat stats;
- stat("/Storage/OCPP", &stats);
- // Check for directory existence
- if (S_ISDIR(stats.st_mode) == 1)
- {
- //DEBUG_INFO("\n OCPP directory exist \n");
- }
- else
- {
- //DEBUG_INFO("\n OCPP directory not exist, create dir \n");
- sprintf(rmFileCmd,"mkdir -p %s","/Storage/OCPP");
- system(rmFileCmd);
- }
- memset(rmFileCmd, 0, sizeof rmFileCmd);
- if((access("/Storage/OCPP/TransactionRelatedQueue",F_OK))!=-1)
- {
- //DEBUG_INFO("TransactionRelatedQueue exist.\n");
- }
- else
- {
- //DEBUG_INFO("TransactionRelatedQueue not exist\n");
- FILE *log = fopen("/Storage/OCPP/TransactionRelatedQueue", "w+");
- if(log == NULL)
- {
- DEBUG_INFO("log is NULL\n");
- return 0;
- }
- else
- {
- fclose(log);
- }
- }
- FILE *fp = fopen("/Storage/OCPP/TransactionRelatedQueue", "r");
- char line[1200]={0};
- // check if file exist (and you can open it) or not
- if (fp == NULL) {
- DEBUG_INFO("can open file TransactionRelatedQueue!");
- return 0;
- }
- while(fgets(line, sizeof line, fp) != NULL) {
- DEBUG_INFO("%s\n", line);
- }
- fclose(fp);
- return 0;
- }
- int sentqueue(){
- FILE *fp;
- int result = FALSE; // 1: TRUE 0:FALSE
- int temptransactionId = 0;
- int tempconnectorId = 0;
- int gunIndex = 0;
- char guid[37]={0};
- char tempdata[65]={0};
- char key_value[65]={0};
- int IsStopTransaction = FALSE;
- char str[1200]={0};
- char strcomposite[1200]={0};
- char rmFileCmd[100]={0};
- struct stat stats;
- char sstr[28]={ 0 };
- int c = 0;
- char *loc;
- DEBUG_INFO("sentqueue\n");
- stat("/Storage/OCPP", &stats);
- // Check for directory existence
- if (S_ISDIR(stats.st_mode) == 1)
- {
- //DEBUG_INFO("\n OCPP directory exist \n");
- }
- else
- {
- //DEBUG_INFO("\n OCPP directory not exist, create dir \n");
- sprintf(rmFileCmd,"mkdir -p %s","/Storage/OCPP");
- system(rmFileCmd);
- }
- memset(rmFileCmd, 0, sizeof rmFileCmd);
- /* opening file for reading */
- fp = fopen("/Storage/OCPP/TransactionRelatedQueue" , "r");
- if(fp == NULL) {
- DEBUG_INFO("Error opening file");
- return FALSE;
- }
- if( fgets (str, 1200, fp)!=NULL ) {
- /* writing content to stdout */
- //*********************StopTransaction***************************/
- loc = strstr(str, "StopTransaction");
- c = 0;
- memset(sstr ,0, sizeof(sstr) );
- if(loc != NULL)
- {
- IsStopTransaction = TRUE;
- }
- //*********************connectorId***************************/
- loc = strstr(str, "connectorId");
- c = 0;
- memset(sstr ,0, sizeof(sstr) );
- if(loc != NULL)
- {
- while (loc[strlen("connectorId")+2+c] != ',')
- {
- sstr[c] = loc[strlen("connectorId")+2+c];
- c++;
- }
- sstr[c] = '\0';
- tempconnectorId = atoi(sstr);
- //DEBUG_INFO("\n tempconnectorId:%d\n", tempconnectorId);
- }
- //*********************StartTransaction***************************/
- loc = strstr(str, "StartTransaction");
- c = 0;
- memset(sstr ,0, sizeof(sstr) );
- if(loc != NULL)
- {
- //DEBUG_INFO("\n sent queue StartTransaction\n");
- if(tempconnectorId > 0)
- {
- sprintf(tempdata, "StartTransaction,%d", (tempconnectorId-1));
- }
- //else if(tempconnectorId == 0)
- //{
- // sprintf(tempdata, "StartTransaction,%d", 0);
- //}
- memset(sstr ,0, sizeof(sstr) );
- //DEBUG_INFO("\n 0-0. sent queue str=%s\n",str);
- c=0;
- while (str[4+c] != '\"')
- {
- sstr[c] = str[4+c];
- c++;
- }
- sstr[c] = '\0';
- strcpy(guid, sstr);
- //DEBUG_INFO("\n 0. sent queue sstr=%s\n",sstr);
- if(hashmap_operation(1, guid, key_value) == TRUE)
- {
- //DEBUG_INFO("\n 1. sent queue guid=%s\n",guid);
- }
- else
- {
- hashmap_operation(0, guid, tempdata);
- //DEBUG_INFO("\n 2. sent queue guid=%s\n",guid);
- }
- }
- //****************transactionId********************/
- c=0;
- loc = strstr(str, "transactionId");
- memset(sstr ,0, sizeof(sstr) );
- if(loc != NULL)
- {
- while ((loc[strlen("transactionId")+2+c] != '}') && (loc[strlen("transactionId")+2+c] != ','))
- {
- sstr[c] = loc[strlen("transactionId")+2+c];
- c++;
- }
- sstr[c] = '\0';
- temptransactionId = atoi(sstr);
- //puts(str);
- //DEBUG_INFO("\n uuid:%s", "");
- //DEBUG_INFO("\n data:%s", str);
- if(IsStopTransaction == TRUE)
- {
- for(int i=0; i <10; i++)
- {
- //DEBUG_INFO("\n StopTransaction i:%d\n", i);
- //DEBUG_INFO("\n StopTransaction SentQueueTransactionId[i]:%d\n", SentQueueTransactionId[i]);
- //DEBUG_INFO("\n sStopTransaction temptransactionId:%d\n", temptransactionId);
- if(SentQueueTransactionId[i] == temptransactionId)
- {
- tempconnectorId = i;
- gunIndex = i;
- break;
- }
- }
- }
- else
- {
- if(tempconnectorId > 0)
- {
- SentQueueTransactionId[tempconnectorId-1] = temptransactionId;
- gunIndex = tempconnectorId-1;
- }
- }
- //DEBUG_INFO("\n gunIndex=%d \n",gunIndex);
- //DEBUG_INFO("\n temptransactionId=%d \n",temptransactionId);
- //DEBUG_INFO("\n GetTransactionId(gunIndex)=%d \n",GetTransactionId(gunIndex));
- if((GetTransactionId(gunIndex) != 0)&&(temptransactionId != GetTransactionId(gunIndex)))
- {
- strncpy(strcomposite,str, (loc-str)+2+strlen("transactionId"));
- sprintf(strcomposite+((loc-str)+2+strlen("transactionId")),"%d",GetTransactionId(gunIndex));
- strcat(strcomposite, loc+strlen("transactionId")+2+c);
- LWS_Send(strcomposite);
- }
- else
- {
- LWS_Send(str);
- }
- if((IsStopTransaction == TRUE)&&(GetTransactionId(gunIndex) != 0))
- {
- SetTransactionIdZero(gunIndex);
- }
- }
- else
- {
- LWS_Send(str);
- }
- result = TRUE;
- }
- else
- {
- result = FALSE;
- }
- fclose(fp);
- return result;
- }
- //
- void* processTransactionQueue(void* data) {
- char frontUUID[100] ={0};
- char frontData[1200/*1024*4*/] ={0};
- int queueNotEmpty = 0;
- while(1)
- {
- if(FirstHeartBeatResponse() == 1)
- {
- memset(frontUUID, 0, sizeof(frontUUID));
- memset(frontData, 0, sizeof(frontData));
- queueNotEmpty = 0;
- queueNotEmpty = queue_operation(1,frontUUID, frontData);//showfront(frontUUID, frontData); ---> remove temporally
- if((queueNotEmpty == 1) && (GetOcppConnStatus() == 1))
- {
- if((OfflineTransaction == 1) && (TransactionQueueNum != 0))
- {
- TransactionQueueInterval = 10;//2;
- sleep(TransactionQueueInterval);
- sendbuffer = 1;
- }
- else
- {
- OfflineTransaction = 0;
- if(TransactionMessageResend < TransactionMessageAttemptsGet()) //
- {
- //DEBUG_INFO("TransactionMessageResend=%d\n",TransactionMessageResend);
- //DEBUG_INFO("TransactionMessageAttemptsGet=%d\n",TransactionMessageAttemptsGet());
- //DEBUG_INFO("processTransactionQueue queue is not empty!\n");
- TransactionQueueInterval= TransactionMessageRetryIntervalGet()* TransactionMessageResend;
- //DEBUG_INFO("TransactionQueueInterval =%d\n",TransactionQueueInterval);
- sendbuffer = 1;
- TransactionMessageResend = TransactionMessageResend + 1;
- //DEBUG_INFO("After ADD,TransactionMessageResend=%d\n",TransactionMessageResend);
- sleep(TransactionQueueInterval);
- }
- else
- {
- queue_operation(2,"",""); //// delete item
- TransactionQueueInterval = 0;
- TransactionMessageResend = 1;
- sleep(TransactionQueueInterval);
- }
- }
- }
- else if((queueNotEmpty == 1) && (GetOcppConnStatus() == 0))
- {
- OfflineTransaction = 1;
- }
- else
- {
- TransactionQueueInterval = 0;//10;
- sleep(TransactionQueueInterval);
- }
- }
- usleep(5000);
- }
- pthread_exit(NULL); //
- return 0;
- }
- void CheckTransactionPacket(char *uuid)
- {
- char frontUUID[100]={0};
- char frontData[1200]={0};
- int queueNotEmpty = 0;
- int cmpResult = 0;
- //queue_operation(0,"","");//showqueue(); ---> remove temporally
- queueNotEmpty = queue_operation(1,frontUUID, frontData);//showfront(frontUUID, frontData); ---> remove temporally
- if(queueNotEmpty == 1)
- {
- cmpResult = strcmp(frontUUID, uuid);
- //cmpResult = strcmp(frontData, uuid);
- if (cmpResult == 0)
- {
- DEBUG_INFO("TransactionPacket Compare All right!\n");
- //DEBUG_INFO("frontUUID=%s\n",frontUUID);
- //DEBUG_INFO("uuid=%s\n",uuid);
- queue_operation(2,"","");//delq(); ---> remove temporally
- TransactionQueueInterval = 0;
- TransactionMessageResend = 1;
- }
- }
- }
- /* type: 0 (showqueue ); type: 1(showfront); type: 2(delq) type: 3 (sentqueue) type: 4 (addq) type: 5(store queue to /Storage/OCPP/ )*/
- int queue_operation(int type, char *frontUUID, char *frontData)
- {
- pthread_mutex_unlock(&lock_sentData);
- pthread_mutex_lock(&lock_sentData);
- int result=0;
- if(type == 0) // show items in queue
- result = showqueue();
- else if(type == 1) // show first item
- result = showfront(frontUUID, frontData);
- else if(type == 2) // delete item
- result = delq();
- else if(type == 3) // sent items in queue
- result = sentqueue();
- else if(type == 4) // add items to the queue
- result = addq(frontUUID, frontData);
- pthread_mutex_unlock(&lock_sentData);
- return result;
- }
- char * strchr(const char *p, int ch)
- {
- char c;
- c = ch;
- for (;; ++p) {
- if (*p == c)
- return ((char *)p);
- if (*p == '\0')
- return (NULL);
- }
- /* NOTREACHED */
- return NULL;
- }
- int removeMessageSentFile(void)
- {
- char rmFileCmd[100]={0};
- struct stat stats;
- //
- stat("/Storage/OCPP", &stats);
- // Check for directory existence
- if (S_ISDIR(stats.st_mode) == 1)
- {
- //DEBUG_INFO("\n OCPP directory exist \n");
- }
- else
- {
- DEBUG_INFO("\n directory not exist, create dir \n");
- sprintf(rmFileCmd,"mkdir -p %s","/Storage/OCPP");
- system(rmFileCmd);
- }
- memset(rmFileCmd, 0, sizeof rmFileCmd);
- if((access("/Storage/OCPP/MessageSent",F_OK))!=-1)
- {
- DEBUG_INFO("MessageSent file exist.\n");
- sprintf(rmFileCmd,"rm -f %s","/Storage/OCPP/MessageSent");
- system(rmFileCmd);
- }
- memset(rmFileCmd, 0, sizeof rmFileCmd);
- return 0;
- }
- void work(char s[]) //删除首尾空格
- {int i,j;
- for(i=0;s[i]==' ';i++); //找到开头第一个非空格
- for(j=0;s[i];)s[j++]=s[i++]; //删除开头空格
- for(i--;s[i]==' ';i--)s[i]='\0'; //删除末尾空格
- }
- #define SystemLogMessage
- //================================================
- // Main process
- //================================================
- int main(void)
- {
- char rmFileCmd[100]={0};
- struct stat stats;
- pthread_t t;
- #ifdef SystemLogMessage
- DEBUG_INFO("Initialization...\n");
- #endif
- if(ProcessShareMemory()== FAIL)
- {
- return FAIL;
- }
- //Create OCPP dir
- stat("/Storage/OCPP", &stats);
- // Check for directory existence
- if (S_ISDIR(stats.st_mode) == 1)
- {
- //DEBUG_INFO("\n OCPP directory exist \n");
- }
- else
- {
- DEBUG_INFO("\n OCPP directory not exist, create dir \n");
- sprintf(rmFileCmd,"mkdir -p %s","/Storage/OCPP");
- system(rmFileCmd);
- }
- memset(rmFileCmd, 0, sizeof rmFileCmd);
- //Create Process: Resend Transaction
- pthread_create(&t, NULL, processTransactionQueue, NULL); //
- sqlite3_config(SQLITE_CONFIG_URI,1);
- if(sqlite3_open("file:/Storage/OCPP/charger.db", &db))
- {
- #ifdef Debug
- DEBUG_INFO( "Can't open database: %s\n", sqlite3_errmsg(db));
- #endif
- sqlite3_close( db );
- exit(0);
- }
- else
- {
- #ifdef Debug
- DEBUG_INFO( "Opened database successfully\n");
- #endif
- }
- //Create Table log buffer
- int rc =sqlite3_exec(db, createsql, 0, 0, &errMsg);
- if (SQLITE_OK != rc)
- {
- #ifdef Debug
- DEBUG_INFO( "Create log buffer table error message: %s\n", errMsg);
- #endif
- return 0;
- }
- else
- {
- #ifdef Debug
- DEBUG_INFO( "Opened log buffer table successfully\n");
- #endif
- }
- // Create Table OcppAuthCache
- rc =sqlite3_exec(db, sqlOcppAuthCache, 0, 0, &errMsg);
- if (SQLITE_OK != rc)
- {
- #ifdef Debug
- DEBUG_INFO( "Create OcppAuthCache error message: %s\n", errMsg);
- #endif
- return 0;
- }
- else
- {
- #ifdef Debug
- DEBUG_INFO( "Opened OcppAuthCache table successfully\n");
- #endif
- }
- // Create Table OcppAuthLocal
- rc =sqlite3_exec(db, sqlOcppAuthLocal, 0, 0, &errMsg);
- if (SQLITE_OK != rc)
- {
- #ifdef Debug
- DEBUG_INFO( "Create Table OcppAuthLocal error %s\n",errMsg);
- #endif
- return 0;
- }
- else
- {
- #ifdef Debug
- DEBUG_INFO( "Opened OcppAuthLocal table successfully\n");
- #endif
- }
- initialConfigurationTable();
- removeMessageSentFile();
- OCPP_get_TableAuthlocalAllData();
- for(;;)
- {
- while(ConnectionEstablished==0)
- {
- SetOcppConnStatus(FALSE);
- if((time((time_t*)NULL)-startTime.connect)>=60)
- {
- #ifdef Debug
- DEBUG_INFO("Execute ConnectWsServer\n");
- #endif
- ConnectWsServer();
- startTime.connect=time((time_t*)NULL);
- }
- // Check System Value, process offline Transaction
- CheckSystemValue();
- lws_service(context, 10000);//timeout_ms
- }
- if(( (BootNotificationInterval != 0 && ((time((time_t*)NULL)-startTime.bootNotification)>=BootNotificationInterval) ) || ((time((time_t*)NULL)-startTime.bootNotification)>=defaultWaitingTime) ) && ((server_sign == FALSE)/*|| (server_pending == TRUE)*/))
- {
- //hashmapForMessageNew();
- sendBootNotificationRequest();
- startTime.bootNotification=time((time_t*)NULL);
- }
- if(server_sign == TRUE)
- {
- if(sendbuffer == 1)
- {
- queue_operation(3, "", "");//sentqueue()
- sendbuffer = 0;
- }
- SetOcppConnStatus(TRUE);
- // Check System Value
- CheckSystemValue();
- if(GetHeartBeatWithNOResponse() >= 3)
- {
- lws_context_destroy(context);
- ConnectionEstablished=0;
- context = NULL;
- SetHeartBeatWithNOResponse();
- }
- }
- lws_service(context, 100);//timeout_ms
- }
- pthread_join(t, NULL); //
- //hashmapForMessageFree();
- return FAIL;
- }
|