ab869558
aknockae
La com série entr...
|
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
|
/*
* Test on serial device
*/
////
// Include files
////
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <termios.h>
#include <string.h>
#include <libwebsockets.h>
#include "serial.h"
#define MAX_FRAME_SIZE 1024
#define WAIT_DELAY 50
#define SERIAL_DEVICE "/dev/ttyACM0"
/*****************************************************************************/
/**********************PARTIE WEBSOCKETS + COM SERIE**************************/
/*****************************************************************************/
//Fonction qui retourne un tableau de char, chaque char correspondant à une donnée d'un capteur
char * getDataFromSensors(char *request,int sizeOfRequest)
{
int sd=serialOpen(SERIAL_DEVICE,SERIAL_BOTH);
serialConfig(sd,B9600);
int i;
char *reponse = NULL;
//On envoie le GET au FPGA pour récupérer les données
for(i = 0; i < sizeOfRequest;i++)
{
if(write(sd,(void*)&request[i],sizeof(char))!=1)
{
perror("main.write"); exit(-1);
}
}
printf("\nReponse : ");
reponse = malloc(3*sizeof(char));
for(i=0;i < 3;i++)
{
if(read(sd,(void*)&reponse[i],sizeof(char))!=1)
{
perror("main.read"); exit(-1);
}
}
printf("\nTemperature : %d",reponse[1]);
printf("\nPressure : %d",reponse[0]);
printf("\nHumidity : %d\n",reponse[2]);
serialClose(sd);
return reponse;
}
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)
{
static char *requete = NULL;
static char *reponse = NULL;
static int msize=0;
switch(reason)
{
|
4972db79
aknockae
Reception et envo...
|
81
|
char test[2];
|
ab869558
aknockae
La com série entr...
|
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
|
case LWS_CALLBACK_ESTABLISHED:
printf("connection established\n");
requete=NULL;
// Declenchement d'un prochain envoi au navigateur
libwebsocket_callback_on_writable(this,wsi);
break;
case LWS_CALLBACK_RECEIVE:
// Ici sont traites les messages envoyes par le navigateur
printf("received data: %s\n",(char *)in);
requete=malloc(len+LWS_SEND_BUFFER_PRE_PADDING+LWS_SEND_BUFFER_POST_PADDING);
if(requete==NULL)
{
perror("callback_my.malloc"); exit(EXIT_FAILURE);
}
memcpy(requete+LWS_SEND_BUFFER_PRE_PADDING,in,len);
// Declenchement d'un prochain envoi au navigateur
msize=len;
libwebsocket_callback_on_writable(this,wsi);
break;
case LWS_CALLBACK_SERVER_WRITEABLE:
// Ici sont envoyes les messages au navigateur
//Si la requete a pour valeur GET, on envoie les donnée au site
|
4972db79
aknockae
Reception et envo...
|
108
109
|
printf("entree dans envoie");
|
ab869558
aknockae
La com série entr...
|
110
111
|
if(requete!= NULL)
{
|
4972db79
aknockae
Reception et envo...
|
112
113
114
115
|
char *test = requete + LWS_SEND_BUFFER_PRE_PADDING;
printf("entree dans envoir->requete");
printf("Message : %c%c%c\n",test[0],test[1],test[2]);
if(test[0] == 'G' && test[1] == 'E' && test[2] == 'T')
|
ab869558
aknockae
La com série entr...
|
116
|
{
|
4972db79
aknockae
Reception et envo...
|
117
118
119
|
reponse = malloc(3*sizeof(char));
reponse[0] = 'a';reponse[1] = 'b'; reponse[2] = 'c';
printf("\nSending : %c%c%c",reponse[0],reponse[1],reponse[2]);
|
ab869558
aknockae
La com série entr...
|
120
121
122
|
int length = 3;
unsigned char *buf = malloc(LWS_SEND_BUFFER_PRE_PADDING + length + LWS_SEND_BUFFER_POST_PADDING);
memcpy (buf + LWS_SEND_BUFFER_PRE_PADDING, reponse, length );
|
4972db79
aknockae
Reception et envo...
|
123
|
libwebsocket_write(wsi, buf + LWS_SEND_BUFFER_PRE_PADDING, length, LWS_WRITE_BINARY); //LWS_WRITE_TEXT
|
ab869558
aknockae
La com série entr...
|
124
|
free(reponse);
|
ab869558
aknockae
La com série entr...
|
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
160
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
|
free(requete);
requete = NULL;
reponse = 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}
};
/*****************************************************************/
/**********************MAIN***************************************/
/*****************************************************************/
int main(void){
char j;
int i;
unsigned long count = 0;
//Initialisation WebSockets
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);
//Initialisation com Serie
if(context==NULL)
{
fprintf(stderr, "libwebsocket init failed\n");
return -1;
}
printf("starting server...\n");
//boucle infinie du main
while(1)
{
libwebsocket_service(context,WAIT_DELAY);
}
libwebsocket_context_destroy(context);
return 0;
}
//********************************************************************************//
|