Commit 787d9fce17f40cc040366e5b86edb1cbccb9b77e
0 parents
Ajout des fichiers sources du websocket
Showing
4 changed files
with
213 additions
and
0 deletions
Show diff stats
1 | +++ a/SerialWebsocket/main.c | |
... | ... | @@ -0,0 +1,40 @@ |
1 | +/* | |
2 | + * Test on serial device | |
3 | + */ | |
4 | + | |
5 | +//// | |
6 | +// Include files | |
7 | +//// | |
8 | +#include <stdio.h> | |
9 | +#include <stdlib.h> | |
10 | +#include <unistd.h> | |
11 | +#include <termios.h> | |
12 | + | |
13 | +#include "serial.h" | |
14 | + | |
15 | +//// | |
16 | +// Constants | |
17 | +//// | |
18 | +#define SERIAL_DEVICE "/dev/ttyACM0" | |
19 | + | |
20 | +//// | |
21 | +// Global variables | |
22 | +//// | |
23 | + | |
24 | +//// | |
25 | +// Main function | |
26 | +//// | |
27 | + | |
28 | +int main(void){ | |
29 | +int c=0; | |
30 | +int sd=serialOpen(SERIAL_DEVICE,SERIAL_BOTH); | |
31 | +serialConfig(sd,B9600); | |
32 | +if(write(sd,&c,sizeof(char))!=1){ perror("main.write"); exit(-1); } | |
33 | +int i; | |
34 | +for(i=0;i<8;i++){ | |
35 | + if(read(sd,&c,sizeof(char))!=1){ perror("main.read"); exit(-1); } | |
36 | + printf("%02x\n",c); | |
37 | + } | |
38 | +serialClose(sd); | |
39 | +exit(0); | |
40 | +} | |
0 | 41 | \ No newline at end of file | ... | ... |
1 | +++ a/SerialWebsocket/serial.c | |
... | ... | @@ -0,0 +1,55 @@ |
1 | +/* | |
2 | + * Serial library | |
3 | + */ | |
4 | + | |
5 | +//// | |
6 | +// Include files | |
7 | +//// | |
8 | +#include <stdio.h> | |
9 | +#include <stdlib.h> | |
10 | +#include <unistd.h> | |
11 | +#include <fcntl.h> | |
12 | +#include <termios.h> | |
13 | +#include <strings.h> | |
14 | +#include <sys/types.h> | |
15 | +#include <sys/ioctl.h> | |
16 | +#include <sys/file.h> | |
17 | +#include <linux/serial.h> | |
18 | + | |
19 | +#include "serial.h" | |
20 | + | |
21 | +//// | |
22 | +// Functions | |
23 | +//// | |
24 | + | |
25 | +// | |
26 | +// Open serial port device | |
27 | +// | |
28 | +int serialOpen(char *device,int mode){ | |
29 | +int flags=(mode==SERIAL_READ?O_RDONLY:(mode==SERIAL_WRITE?O_WRONLY:O_RDWR)); | |
30 | +int fd=open(device,flags|O_NOCTTY); | |
31 | +if(fd<0){ perror(device); exit(-1); } | |
32 | +return fd; | |
33 | +} | |
34 | + | |
35 | +// | |
36 | +// Serial port configuration | |
37 | +// | |
38 | +void serialConfig(int fd,int speed){ | |
39 | +struct termios new; | |
40 | +bzero(&new,sizeof(new)); | |
41 | +new.c_cflag=CLOCAL|CREAD|speed|CS8; | |
42 | +new.c_iflag=0; | |
43 | +new.c_oflag=0; | |
44 | +new.c_lflag=0; /* set input mode (non-canonical, no echo,...) */ | |
45 | +new.c_cc[VTIME]=0; /* inter-character timer unused */ | |
46 | +new.c_cc[VMIN]=1; /* blocking read until 1 char received */ | |
47 | +if(tcsetattr(fd,TCSANOW,&new)<0){ perror("serialInit.tcsetattr"); exit(-1); } | |
48 | +} | |
49 | + | |
50 | +// | |
51 | +// Serial port termination | |
52 | +// | |
53 | +void serialClose(int fd){ | |
54 | +close(fd); | |
55 | +} | |
0 | 56 | \ No newline at end of file | ... | ... |
1 | +++ a/SerialWebsocket/serial.h | |
... | ... | @@ -0,0 +1,18 @@ |
1 | +/* | |
2 | + * Public definitions for serial library | |
3 | + */ | |
4 | + | |
5 | +//// | |
6 | +// Constants | |
7 | +//// | |
8 | + | |
9 | +#define SERIAL_READ 0 | |
10 | +#define SERIAL_WRITE 1 | |
11 | +#define SERIAL_BOTH 2 | |
12 | + | |
13 | +//// | |
14 | +// Public prototypes | |
15 | +//// | |
16 | +int serialOpen(char *device,int mode); | |
17 | +void serialConfig(int fd,int speed); | |
18 | +void serialClose(int fd); | |
0 | 19 | \ No newline at end of file | ... | ... |
1 | +++ a/SerialWebsocket/websocket.c | |
... | ... | @@ -0,0 +1,100 @@ |
1 | +#include <stdio.h> | |
2 | +#include <stdlib.h> | |
3 | +#include <string.h> | |
4 | +#include <libwebsockets.h> | |
5 | +#include <unistd.h> | |
6 | +#include <termios.h> | |
7 | + | |
8 | +#include "serial.h" | |
9 | + | |
10 | +#define SERIAL_DEVICE "/dev/ttyACM0" | |
11 | + | |
12 | +#define MAX_FRAME_SIZE 1024 | |
13 | +#define WAIT_DELAY 50 | |
14 | + | |
15 | +int sd; | |
16 | +static int callback_http( | |
17 | + struct libwebsocket_context *this, | |
18 | + struct libwebsocket *wsi,enum libwebsocket_callback_reasons reason, | |
19 | + void *user,void *in,size_t len) | |
20 | +{ | |
21 | +return 0; | |
22 | +} | |
23 | + | |
24 | +static int callback_my( | |
25 | + struct libwebsocket_context * this, | |
26 | + struct libwebsocket *wsi,enum libwebsocket_callback_reasons reason, | |
27 | + void *user,void *in,size_t len) | |
28 | +{ | |
29 | +static char *message=NULL; | |
30 | +static int msize=0; | |
31 | +switch(reason){ | |
32 | + case LWS_CALLBACK_ESTABLISHED: | |
33 | + printf("connection established\n"); | |
34 | + message=NULL; | |
35 | + // Declenchement d'un prochain envoi au navigateur | |
36 | + libwebsocket_callback_on_writable(this,wsi); | |
37 | + break; | |
38 | + case LWS_CALLBACK_RECEIVE: | |
39 | + // Ici sont traites les messages envoyes par le navigateur | |
40 | + printf("received data: %s\n",(char *)in); | |
41 | + write(sd,in,sizeof(char)); | |
42 | + message=malloc(len+LWS_SEND_BUFFER_PRE_PADDING+LWS_SEND_BUFFER_POST_PADDING); | |
43 | + if(message==NULL){ perror("callback_my.malloc"); exit(EXIT_FAILURE); } | |
44 | + memcpy(message+LWS_SEND_BUFFER_PRE_PADDING,in,len); | |
45 | + // Declenchement d'un prochain envoi au navigateur | |
46 | + msize=len; | |
47 | + libwebsocket_callback_on_writable(this,wsi); | |
48 | + break; | |
49 | + case LWS_CALLBACK_SERVER_WRITEABLE: | |
50 | + // Ici sont envoyes les messages au navigateur | |
51 | + if(message!=NULL){ | |
52 | + char *out=message+LWS_SEND_BUFFER_PRE_PADDING; | |
53 | + libwebsocket_write(wsi,(unsigned char *)out,msize,LWS_WRITE_TEXT); | |
54 | + free(message); | |
55 | + message=NULL; | |
56 | + } | |
57 | + break; | |
58 | + default: | |
59 | + break; | |
60 | + } | |
61 | +return 0; | |
62 | +} | |
63 | + | |
64 | +static struct libwebsocket_protocols protocols[] = { | |
65 | + { | |
66 | + "http-only", // name | |
67 | + callback_http, // callback | |
68 | + 0, // data size | |
69 | + 0 // maximum frame size | |
70 | + }, | |
71 | + {"myprotocol",callback_my,0,MAX_FRAME_SIZE}, | |
72 | + {NULL,NULL,0,0} | |
73 | + }; | |
74 | + | |
75 | +int main(void) { | |
76 | +//Initialisation de la liaison série | |
77 | +int c=0; | |
78 | +sd=serialOpen(SERIAL_DEVICE,SERIAL_BOTH); | |
79 | +serialConfig(sd,B9600); | |
80 | + | |
81 | +//Websocket | |
82 | +int port=9000; | |
83 | +struct lws_context_creation_info info; | |
84 | +memset(&info,0,sizeof info); | |
85 | +info.port=port; | |
86 | +info.protocols=protocols; | |
87 | +info.gid=-1; | |
88 | +info.uid=-1; | |
89 | +struct libwebsocket_context *context=libwebsocket_create_context(&info); | |
90 | +if(context==NULL){ | |
91 | + fprintf(stderr, "libwebsocket init failed\n"); | |
92 | + return -1; | |
93 | + } | |
94 | +printf("starting server...\n"); | |
95 | +while(1){ | |
96 | + libwebsocket_service(context,WAIT_DELAY); | |
97 | + } | |
98 | +libwebsocket_context_destroy(context); | |
99 | +return 0; | |
100 | +} | ... | ... |