Blame view

websocket/websocket.c 2.66 KB
f3d8bf26   ftaingla   fichiers finaux
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
  #include <stdio.h>
  #include <stdlib.h>
  #include <string.h>
  #include <unistd.h>
  #include <termios.h>
  
  #include "serial.h"
  
  
  #define         SERIAL_DEVICE           "/dev/ttyACM0"
  #include <libwebsockets.h>
  
  #define MAX_FRAME_SIZE  1024
  #define WAIT_DELAY      50
  
  int sd;
  
  //balek de ça
  static int callback_http(struct libwebsocket_context *this,struct libwebsocket *wsi,enum libwebsocket_callback_reasons reason,void *user,void *in,size_t len)
  {
  	return 0;
  }
  
  //le plus important est ici
  static int callback_my(struct libwebsocket_context * this,struct libwebsocket *wsi,enum libwebsocket_callback_reasons reason,void *user,void *in,size_t len)
  {
  
  	static char *message=NULL;
  	static int msize=0;
  	
  
  	
  	switch(reason)
  	{
  		case LWS_CALLBACK_ESTABLISHED: /
  			printf("connection established\n");
  			message=NULL;
  			
  			libwebsocket_callback_on_writable(this,wsi);
  		break;
  		case LWS_CALLBACK_RECEIVE: 
  			
  			printf("received data: %s\n",(char *)in);
  			
  			message=malloc(len+LWS_SEND_BUFFER_PRE_PADDING+LWS_SEND_BUFFER_POST_PADDING);
  
  			if(message==NULL)
  			{ 
  				perror("callback_my.malloc"); exit(EXIT_FAILURE); 
  			}
  			e + le début du buffer d'envoi "LWS_SEND_BUFFER_PRE_PADDING"
  			memcpy(message+LWS_SEND_BUFFER_PRE_PADDING,in,len);
  		
  			msize=len;
  			// on envoie a l'arduiono la valeur envoyé  par le  site web  
  			write(sd,message,(len+LWS_SEND_BUFFER_PRE_PADDING+LWS_SEND_BUFFER_POST_PADDING));
  			libwebsocket_callback_on_writable(this,wsi);
  		break;
  		case LWS_CALLBACK_SERVER_WRITEABLE: 
  			
  			if(message!=NULL)
  			{	
  		
  				read(sd,message,len+LWS_SEND_BUFFER_PRE_PADDING+LWS_SEND_BUFFER_POST_PADDING);
  				
  				char *out=message+LWS_SEND_BUFFER_PRE_PADDING;
  				
  				
  				libwebsocket_write(wsi,(unsigned char *)out,msize,LWS_WRITE_TEXT);
  			
  				free(message);
  				message=NULL;
  			}
  		break;
  		default:
  		break;
  	}
  	return 0;
  }
  
  static struct libwebsocket_protocols protocols[] = {
  	{
  		"http-only",   // name
  		callback_http, // callback
  		0,             // data size
  		0              // maximum frame size
  	},
  	{"myprotocol",callback_my,0,MAX_FRAME_SIZE},
  	{NULL,NULL,0,0}
  	};
  
  
  
  int main(void) 
  {
  	int port=9000;
  	struct lws_context_creation_info info;
  	memset(&info,0,sizeof info);
  	info.port=port;
  	info.protocols=protocols;
  	info.gid=-1;
  	info.uid=-1;
  	struct libwebsocket_context *context=libwebsocket_create_context(&info);
  	
  	// communication port série  arduino 
  	int c=0;	
  	sd=serialOpen(SERIAL_DEVICE,SERIAL_BOTH);	
  	serialConfig(sd,B9600);	
  	
  	if(context==NULL)
  	{
  	  fprintf(stderr, "libwebsocket init failed\n");
  	  return -1;
  	}
  	printf("starting server...\n");
  	
  	while(1)
  	{
  	  libwebsocket_service(context,WAIT_DELAY);
  	}
  	libwebsocket_context_destroy(context);
  	return 0;
  }