4ed129b3
tcattela
Commit des fichie...
|
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
|
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <libwebsockets.h>
#include <unistd.h>
#include <fcntl.h>
#include <termios.h>
#include <sys/types.h>
#include <sys/ioctl.h>
#include <sys/file.h>
#include <linux/serial.h>
#include <math.h>
#include "serial.h"
#define MAX_FRAME_SIZE 1024
#define WAIT_DELAY 50
#define SERIAL_DEVICE "/dev/ttyUSB0"
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;
}
static int callback_my(
struct libwebsocket_context * this,
struct libwebsocket *wsi,enum libwebsocket_callback_reasons reason,
void *user,void *in,size_t len)
{
|
78a024f7
tcattela
Ajustements et cl...
|
41
|
static char *mesage=NULL;
|
4ed129b3
tcattela
Commit des fichie...
|
42
43
44
|
static int msize=0;
switch(reason)
{
|
4ed129b3
tcattela
Commit des fichie...
|
45
46
|
case LWS_CALLBACK_ESTABLISHED:
printf("connection established\n");
|
78a024f7
tcattela
Ajustements et cl...
|
47
|
mesage=NULL;
|
4ed129b3
tcattela
Commit des fichie...
|
48
49
50
51
|
// Declenchement d'un prochain envoi au navigateur
libwebsocket_callback_on_writable(this,wsi);
break;
|
78a024f7
tcattela
Ajustements et cl...
|
52
|
|
4ed129b3
tcattela
Commit des fichie...
|
53
54
|
case LWS_CALLBACK_RECEIVE:
|
78a024f7
tcattela
Ajustements et cl...
|
55
56
57
58
59
60
61
|
// Ici sont traites les mesages envoyes par le navigateur
char* recei=(char *)in;
int rec = *recei - 48; //réception de l'instruction, convertie en entier
printf("received data: %d\n",rec);
mesage=malloc(len+LWS_SEND_BUFFER_PRE_PADDING+LWS_SEND_BUFFER_POST_PADDING);
if(mesage==NULL)
{
|
4ed129b3
tcattela
Commit des fichie...
|
62
63
|
perror("callback_my.malloc");
exit(EXIT_FAILURE);
|
78a024f7
tcattela
Ajustements et cl...
|
64
65
66
67
68
|
}
char value[3];
sprintf (value, "%d", (10*serial_read(&rec))+rec); //réception de la valeur demandée, sous forme de str
printf("value = %s\n",value);
memcpy(mesage+LWS_SEND_BUFFER_PRE_PADDING,value,1 + strlen(value));
|
4ed129b3
tcattela
Commit des fichie...
|
69
70
|
// Declenchement d'un prochain envoi au navigateur
|
4ed129b3
tcattela
Commit des fichie...
|
71
72
73
|
msize=strlen(value);
libwebsocket_callback_on_writable(this,wsi);
break;
|
78a024f7
tcattela
Ajustements et cl...
|
74
|
|
4ed129b3
tcattela
Commit des fichie...
|
75
|
case LWS_CALLBACK_SERVER_WRITEABLE:
|
78a024f7
tcattela
Ajustements et cl...
|
76
77
78
79
|
// Ici sont envoyes les mesages au navigateur
if(mesage!=NULL)
{
char *out=mesage+LWS_SEND_BUFFER_PRE_PADDING;
|
4ed129b3
tcattela
Commit des fichie...
|
80
81
82
|
printf("%s\n",out);
printf("%d\n",msize);
libwebsocket_write(wsi,(unsigned char *)out,msize,LWS_WRITE_TEXT);
|
78a024f7
tcattela
Ajustements et cl...
|
83
84
85
|
free(mesage);
mesage=NULL;
}
|
4ed129b3
tcattela
Commit des fichie...
|
86
|
break;
|
78a024f7
tcattela
Ajustements et cl...
|
87
|
|
4ed129b3
tcattela
Commit des fichie...
|
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
|
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}
};
////
// SERIAL FUNCTIONS
////
//
// Open serial port device
//
int serialOpen(char *device,int mode){
int flags=(mode==SERIAL_READ?O_RDONLY:(mode==SERIAL_WRITE?O_WRONLY:O_RDWR));
int fd=open(device,flags|O_NOCTTY);
if(fd<0){ perror(device); exit(-1); }
return fd;
}
//
// Serial port configuration
//
void serialConfig(int fd,int speed){
struct termios new;
bzero(&new,sizeof(new));
new.c_cflag=CLOCAL|CREAD|speed|CS8;
new.c_iflag=0;
new.c_oflag=0;
new.c_lflag=0; /* set input mode (non-canonical, no echo,...) */
new.c_cc[VTIME]=0; /* inter-character timer unused */
new.c_cc[VMIN]=1; /* blocking read until 1 char received */
if(tcsetattr(fd,TCSANOW,&new)<0){ perror("serialInit.tcsetattr"); exit(-1); }
}
//
// Serial port termination
//
void serialClose(int fd){
close(fd);
}
////
// User Functions
////
int serial_read(int *a)
{
int c=*a;
int i=0;
int n=0;
int value=0;
int sd=serialOpen(SERIAL_DEVICE,SERIAL_BOTH);
serialConfig(sd,B9600);
|
78a024f7
tcattela
Ajustements et cl...
|
160
|
if(write(sd,&c,sizeof(char))!=1){ perror("main.write"); exit(-1); } //Envoi de l'instruction
|
4ed129b3
tcattela
Commit des fichie...
|
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
|
//Read the number of decimals
if(read(sd,&n,sizeof(char))!=1){ perror("main.read"); exit(-1); }
printf("%d\n",n-48);
n=n-48;
//Read and add all decimals
for (i=0;i<n;i++)
{
if(read(sd,&c,sizeof(char))!=1){ perror("main.read"); exit(-1); }
value=value+(c-48)*pow(10,n-i-1);
}
serialClose(sd);
return value;
//printf("%d\n",value);
exit(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);
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;
}
|