-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathudp.c
71 lines (57 loc) · 1.34 KB
/
udp.c
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
#include <stdio.h>
#include <syslog.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <string.h>
#include <unistd.h>
#include "input.h"
#include "udp.h"
#define PORT 18169
#define BUFFERSIZE 2
int sd; // Socket file descriptor
signed char buffer[BUFFERSIZE];
int open_udp()
{
// Create a socket.
sd = socket(AF_INET, SOCK_DGRAM, 0);
if (sd < 0)
{
syslog(LOG_ERR, "Unable to create a socket.");
return sd;
}
// Name the socket
struct sockaddr_in address;
address.sin_family = AF_INET; // IPv4
address.sin_addr.s_addr = htonl(INADDR_ANY); // Any IP address
address.sin_port = htons(PORT);
if (bind(sd, (struct sockaddr *) &address, sizeof(address)) < 0)
{
syslog(LOG_ERR, "Unable to bind socket.");
return -1;
}
// Set the timeout to one second
struct timeval tv;
tv.tv_sec = 1;
tv.tv_usec = 0;
setsockopt(sd, SOL_SOCKET, SO_RCVTIMEO, (char *) &tv, sizeof(struct timeval));
syslog(LOG_INFO, "Listening for UDP packets. Port: %d.", PORT);
return 0;
}
int read_inputevent_udp(struct inputdev_event *ev)
{
// Receive data from socket (blocking function call w/ timeout)
int recvlen = recv(sd, buffer, BUFFERSIZE, 0);
if (recvlen > 0)
{
int target = buffer[0];
int magnitude = buffer[1];
ev->target = target;
ev->magnitude = ((float) magnitude) / 100.0f;
return 1;
}
return 0;
}
void close_udp()
{
close(sd);
}