Ocpp16.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  1. #include <sys/types.h>
  2. #include <sys/stat.h>
  3. #include <fcntl.h>
  4. #include <linux/termios.h>
  5. #include <stdio.h>
  6. #include <string.h>
  7. #include <time.h>
  8. #include <stdlib.h>
  9. #include <sys/ipc.h>
  10. #include <sys/shm.h>
  11. #include <sys/mman.h>
  12. #include <linux/sockios.h>
  13. #include <linux/socket.h>
  14. #include <sys/socket.h>
  15. #include <netinet/in.h>
  16. #include <sys/time.h>
  17. #include <sys/timeb.h>
  18. #include <libwebsockets.h>
  19. #include <lws_config.h>
  20. #include "../../define.h"
  21. #define Debug
  22. struct SysConfigAndInfo *ShmSysConfigAndInfo;
  23. struct StatusCodeData *ShmStatusCodeData;
  24. struct OCPP16Data *ShmOCPP16Data;
  25. struct lws *wsi;
  26. struct lws_context *context;
  27. unsigned char *SendBuffer;
  28. int SendBufLen=(1024*3);
  29. int ConnectionEstablished=0;
  30. #ifdef SystemLogMessage
  31. int StoreLogMsg(unsigned char *DataString)
  32. {
  33. unsigned char Buf[256];
  34. time_t CurrentTime;
  35. struct tm *tm;
  36. memset(Buf,0,sizeof(Buf));
  37. CurrentTime = time(NULL);
  38. tm=localtime(&CurrentTime);
  39. sprintf(Buf,"echo \"%04d.%02d.%02d %02d:%02d:%02d - %s\" >> /Storage/SystemLog/[%04d.%02d]SystemLog",
  40. tm->tm_year+1900,tm->tm_mon+1,tm->tm_mday,tm->tm_hour,tm->tm_min,tm->tm_sec,
  41. DataString,
  42. tm->tm_year+1900,tm->tm_mon+1);
  43. system(Buf);
  44. #ifdef Debug
  45. printf("%s \n",DataString);
  46. #endif
  47. }
  48. #endif
  49. int DiffTimeb(struct timeb ST, struct timeb ET)
  50. {
  51. //return milli-second
  52. unsigned int StartTime,StopTime;
  53. StartTime=(unsigned int)ST.time;
  54. StopTime=(unsigned int)ET.time;
  55. return (StopTime-StartTime)*1000+ET.millitm-ST.millitm;
  56. }
  57. /**************************************************************************************/
  58. /**************************Init all share memory *********************************/
  59. /**************************************************************************************/
  60. int InitShareMemory()
  61. {
  62. int MeterSMId;
  63. //creat ShmSysConfigAndInfo
  64. if ((MeterSMId = shmget(ShmSysConfigAndInfoKey, sizeof(struct SysConfigAndInfo), 0777)) < 0)
  65. {
  66. #ifdef SystemLogMessage
  67. StoreLogMsg("[Ocpp16]InitShareMemory:shmget ShmSysConfigAndInfo NG");
  68. #endif
  69. return 0;
  70. }
  71. else if ((ShmSysConfigAndInfo = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  72. {
  73. #ifdef SystemLogMessage
  74. StoreLogMsg("[Ocpp16]InitShareMemory:shmat ShmSysConfigAndInfo NG");
  75. #endif
  76. return 0;
  77. }
  78. //creat ShmStatusCodeData
  79. if ((MeterSMId = shmget(ShmStatusCodeKey, sizeof(struct StatusCodeData), 0777)) < 0)
  80. {
  81. #ifdef SystemLogMessage
  82. StoreLogMsg("[Ocpp16]InitShareMemory:shmget ShmStatusCodeData NG");
  83. #endif
  84. return 0;
  85. }
  86. else if ((ShmStatusCodeData = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  87. {
  88. #ifdef SystemLogMessage
  89. StoreLogMsg("[Ocpp16]InitShareMemory:shmat ShmStatusCodeData NG");
  90. #endif
  91. return 0;
  92. }
  93. //creat ShmOCPP16Data
  94. if ((MeterSMId = shmget(ShmOcppModuleKey, sizeof(struct OCPP16Data), 0777)) < 0)
  95. {
  96. #ifdef SystemLogMessage
  97. StoreLogMsg("[Ocpp16]InitShareMemory:shmget ShmOCPP16Data NG");
  98. #endif
  99. return 0;
  100. }
  101. else if ((ShmOCPP16Data = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  102. {
  103. #ifdef SystemLogMessage
  104. StoreLogMsg("[Ocpp16]InitShareMemory:shmat ShmOCPP16Data NG");
  105. #endif
  106. return 0;
  107. }
  108. memset(ShmOCPP16Data,0,sizeof(struct PsuData));
  109. return 1;
  110. }
  111. int SendData(struct lws *wsi)
  112. {
  113. int n;
  114. int len;
  115. unsigned char *out = NULL;
  116. len = strlen(SendBuffer);
  117. out = (char *)malloc(sizeof(char)*(LWS_SEND_BUFFER_PRE_PADDING + len + LWS_SEND_BUFFER_POST_PADDING));
  118. memcpy (out + LWS_SEND_BUFFER_PRE_PADDING, SendBuffer, len );
  119. n = lws_write(wsi, out + LWS_SEND_BUFFER_PRE_PADDING, len, LWS_WRITE_TEXT);
  120. free(out);
  121. return n;
  122. }
  123. static int OCPP16Callback(struct lws *wsi, enum lws_callback_reasons reason, void *user, void *in, size_t len)
  124. {
  125. #ifdef Debug
  126. printf("OCPP16Callback:reason=%d\n",reason);
  127. #endif
  128. switch (reason)
  129. {
  130. case LWS_CALLBACK_CLIENT_ESTABLISHED://3
  131. #ifdef SystemLogMessage
  132. StoreLogMsg("[Ocpp16]OCPP16Callback:LWS_CALLBACK_CLIENT_ESTABLISHED");
  133. #endif
  134. //connected
  135. ConnectionEstablished=1;
  136. break;
  137. case LWS_CALLBACK_CLIENT_CONNECTION_ERROR://1
  138. #ifdef Debug
  139. printf("OCPP16Callback:LWS_CALLBACK_CLIENT_CONNECTION_ERROR:%s\n",in);
  140. #endif
  141. #ifdef SystemLogMessage
  142. StoreLogMsg("[Ocpp16]OCPP16Callback:LWS_CALLBACK_CLIENT_CONNECTION_ERROR");
  143. #endif
  144. //disconnected
  145. ConnectionEstablished=0;
  146. break;
  147. case LWS_CALLBACK_CLOSED://4
  148. #ifdef SystemLogMessage
  149. StoreLogMsg("[Ocpp16]OCPP16Callback:LWS_CALLBACK_CLOSED");
  150. #endif
  151. //disconnected
  152. ConnectionEstablished=0;
  153. break;
  154. case LWS_CALLBACK_CLIENT_WRITEABLE://10
  155. //sif(need to send message and its relevant data already store into SendBuffer)
  156. SendData(wsi);
  157. break;
  158. case LWS_CALLBACK_CLIENT_RECEIVE://8
  159. ((char *)in)[len] = '\0';
  160. #ifdef Debug
  161. printf("OCPP16Callback:LWS_CALLBACK_CLIENT_RECEIVE : rx %d '%s'\n", (int)len, (char *)in);
  162. #endif
  163. //parsing received message and do something
  164. break;
  165. default:
  166. break;
  167. }
  168. return 0;
  169. }
  170. static struct lws_protocols protocols[] = {
  171. {
  172. "ocpp1.6",
  173. OCPP16Callback,
  174. 10240,
  175. 10240,
  176. },
  177. {
  178. "ocpp1.6",
  179. OCPP16Callback,
  180. 10240,
  181. 10240,
  182. },
  183. {
  184. NULL, NULL, 0 /* End of list */
  185. }
  186. };
  187. int ConnectWsServer()
  188. {
  189. struct lws_context_creation_info ContextInfo;
  190. struct lws_client_connect_info ConnInfo;
  191. int use_ssl=0;
  192. if(context!=NULL)
  193. lws_context_destroy(context);
  194. memset(&ContextInfo, 0, sizeof(struct lws_context_creation_info));
  195. ContextInfo.port=CONTEXT_PORT_NO_LISTEN;
  196. ContextInfo.iface=NULL;
  197. ContextInfo.ssl_private_key_password=NULL;
  198. ContextInfo.ssl_cert_filepath=NULL;
  199. ContextInfo.ssl_private_key_filepath=NULL;
  200. ContextInfo.ssl_ca_filepath="/root/cacert.pem";
  201. ContextInfo.ssl_cipher_list=NULL; //use default one
  202. ContextInfo.gid=-1;
  203. ContextInfo.uid=-1;
  204. //if(security connection)
  205. // ContextInfo.options |= LWS_SERVER_OPTION_DO_SSL_GLOBAL_INIT;
  206. ContextInfo.protocols=protocols;
  207. ContextInfo.timeout_secs= 30;
  208. //if(ping pong enabled)
  209. //ContextInfo.ws_ping_pong_interval= 15;//0 for none, else interval in seconds
  210. ContextInfo.ws_ping_pong_interval= 0;
  211. context = lws_create_context(&ContextInfo);
  212. if (context == NULL)
  213. {
  214. #ifdef SystemLogMessage
  215. StoreLogMsg("[Ocpp16]ConnectWsServer: lws_create_context NG");
  216. #endif
  217. return 0;
  218. }
  219. memset(&ConnInfo,0,sizeof(struct lws_client_connect_info));
  220. ConnInfo.context = context;
  221. // fill up below information
  222. //ConnInfo.address=ServerIpAddress;
  223. //ConnInfo.port = ServerSrcPort;
  224. //ConnInfo.path=ServerPath;
  225. //ConnInfo.host=ServerHost;
  226. //ConnInfo.origin=ServerOrigin;
  227. /* ws://192.168.1.1:9091/greenlots/ocpp*/
  228. ConnInfo.address = "192.168.1.1";
  229. ConnInfo.port = 9091;
  230. ConnInfo.path="/ocpp/T123456789";
  231. ConnInfo.host = "192.168.1.1:9091";
  232. ConnInfo.origin = "192.168.1.1:9091";
  233. use_ssl = LCCSCF_USE_SSL | LCCSCF_ALLOW_SELFSIGNED | LCCSCF_SKIP_SERVER_CERT_HOSTNAME_CHECK;
  234. //if(security connection)
  235. // ConnInfo.ssl_connection = use_ssl;
  236. //else
  237. ConnInfo.ssl_connection=0;
  238. ConnInfo.protocol = protocols[1].name;
  239. ConnInfo.ietf_version_or_minus_one=-1;
  240. wsi=lws_client_connect_via_info(&ConnInfo);
  241. if (!wsi)
  242. {
  243. #ifdef SystemLogMessage
  244. StoreLogMsg("[Ocpp16]ConnectWsServer: lws_client_connect_via_info NG");
  245. #endif
  246. return 0;
  247. }
  248. return 1;
  249. }
  250. int main(int argc,char *argv[])
  251. {
  252. unsigned int StartTime=0;
  253. /**************** Initialization **********/
  254. /*if(InitShareMemory()==0)
  255. {
  256. #ifdef SystemLogMessage
  257. StoreLogMsg("[Ocpp16]main:InitShareMemory NG");
  258. #endif
  259. if(ShmStatusCodeData!=NULL)
  260. {
  261. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.FailToCreateShareMemory=1;
  262. }
  263. sleep(5);
  264. return 0;
  265. }*/
  266. if((SendBuffer=malloc(SendBufLen))==NULL)
  267. {
  268. #ifdef SystemLogMessage
  269. StoreLogMsg("[Ocpp16]main: malloc(SendBufLen) NG");
  270. #endif
  271. sleep(5);
  272. return 0;
  273. }
  274. while(ConnectionEstablished==0)
  275. {
  276. if((time((time_t*)NULL)-StartTime)>=60)
  277. {
  278. #ifdef debug
  279. printf("[OCPP16:]main:Execute ConnectWsServer1\n");
  280. #endif
  281. ConnectWsServer();
  282. StartTime=time((time_t*)NULL);
  283. }
  284. lws_service(context, 10000);//timeout_ms
  285. }
  286. memset(SendBuffer,0,SendBufLen);
  287. sprintf(SendBuffer,"[2,\"%s\",\"%s\",{\"chargePointVendor\":\"%s\",\"chargePointModel\":\"%s\",\"chargePointSerialNumber\":\"%s\",\"chargeBoxSerialNumber\":\"%s\",\"firmwareVersion\":\"%s\",\"imsi\":\"%s\"}]",
  288. "11112222",
  289. "BootNotification",
  290. "CpVendor",
  291. "CpModel",
  292. "CpSN",
  293. "CbSN",
  294. "CpFwVersion",
  295. "CpImsi");
  296. lws_callback_on_writable(wsi);
  297. while(1)
  298. {
  299. //prcessing
  300. lws_service(context, 500);//500 timeout_ms
  301. };
  302. free(SendBuffer);
  303. }