comparison service.c @ 0:e70ea46d6073

Initial import from http://wouhanegaine.free.fr/dev/DSPad02b_neo07.zip
author Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
date Sun, 22 Feb 2015 01:38:06 +0100
parents
children f362b20de51e
comparison
equal deleted inserted replaced
-1:000000000000 0:e70ea46d6073
1 #include "service.h"
2 #include "uinput.h"
3 #include "ds.h"
4 #include "restartOp.h"
5
6 #include <stdio.h>
7 #include <sys/types.h>
8 #include <sys/socket.h>
9 #include <netinet/in.h>
10 #include <stdlib.h>
11 #include <string.h>
12
13
14 /*
15 Create a socket
16 Returns socket descriptor (-1 if error)
17 */
18 int create_socket(unsigned short *port, int type){
19 int sock, autorisation , ok;
20 struct sockaddr_in add;
21 socklen_t l = sizeof(struct sockaddr_in);
22
23 sock = socket(PF_INET, type, 0);
24 if(sock == -1){
25 perror("socket");
26 return -1;
27 }
28
29 /* Pour pouvoir relancer immediatement un serveur TCP */
30 /* ou lancer, sur une meme machine, */
31 /* plusieurs recepteurs UDP sur */
32 /* le meme port de diffusion (broadcast, multicast) */
33 autorisation = 1;
34 ok = setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &autorisation, sizeof(int));
35 if(ok == -1){
36 perror("setsockopt");
37 return -1;
38 }
39
40 memset(&add, 0, l) ;
41 add.sin_family = AF_INET ;
42 add.sin_port = htons(*port);
43 add.sin_addr.s_addr = htonl(INADDR_ANY);
44
45 ok = bind(sock, (struct sockaddr*)&add, l);
46 if(ok == -1){
47 perror("bind");
48 return -1;
49 }
50
51 ok = getsockname(sock, (struct sockaddr*)&add, &l);
52 if(ok == -1){
53 perror("getsockname");
54 return -1;
55 }
56
57 *port = ntohs(add.sin_port);
58 return sock;
59 }
60
61
62
63 /*
64 Receive a DS datagram
65 Returns size read or -1
66 */
67 ssize_t receive_ds_info(int socket, ds_t* ds){
68 struct sockaddr_in add;
69 socklen_t l = sizeof(struct sockaddr_in);
70
71 ssize_t result;
72
73 result = recvfrom_rs(socket, ds, sizeof(ds_t), 0, (struct sockaddr*)&add, &l);
74 if(result == -1) perror("recvfrom_rs");
75 return result;
76 }
77
78
79 /*
80 Start the listening service yeah!
81 */
82 void start_service(void){
83
84 int uinput_fd;
85 int sock_fd;
86 uint16_t udp_port = base_port + curdev;
87
88 ds_t ds;
89 ssize_t result;
90
91
92 fprintf(stdout, " [%d] Creation of service:\n", curdev);
93
94 // Create joystick dev
95 uinput_fd = init_uinput_device();
96 if(uinput_fd == -1){
97 fprintf(stderr, " [%d] Joystick device failed\n", curdev);
98 exit(EXIT_FAILURE);
99 }
100 fprintf(stdout, " [%d] Joystick device OK\n", curdev);
101
102
103 // Create udp socket
104 sock_fd = create_socket(&udp_port, SOCK_DGRAM);
105 if(sock_fd == -1){
106 fprintf(stderr, " [%d] Socket failed\n", curdev);
107 exit(EXIT_FAILURE);
108 }
109 fprintf(stdout, " [%d] Socket on port %d OK\n", curdev, udp_port);
110
111
112 // Device and Socket are ok :-D
113 fprintf(stdout, " [%d] Creation of service: OK\n", curdev);
114
115
116 /* Center device axis when created */
117 do_uinput(uinput_fd, ABS_Y, DS_MAX_Y/2, EV_ABS);
118 do_uinput(uinput_fd, ABS_X, DS_MAX_X/2, EV_ABS);
119
120 while(1){
121 result = receive_ds_info(sock_fd, &ds);
122 if(result == -1){
123 fprintf(stderr, " [%d] DS Info receive failed\n", curdev);
124 }
125 if(result > 0){
126 ds_process_evt(&ds, uinput_fd);
127 }
128 }
129
130 return;
131 }
132
133
134