-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathreceiver.c
87 lines (69 loc) · 2.05 KB
/
receiver.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include "pthread.h"
#include <inttypes.h>
#include "state.h"
/** Configure this **/
#define LOCAL_HOST "169.254.100.16" // IP of local interface
#define R_PORT 8000
#define REMOTE_HOST "169.254.227.87"
#define S_PORT 8001
/** **/
#define TX_INTERVAL_MS 300
#define STATE_SIZE sizeof(DIJOYSTATE2_t)
void *send_force(void *arg) {
int8_t force = 0;
int sockfd;
struct sockaddr_in servaddr;
servaddr.sin_family = AF_INET;
servaddr.sin_port = htons(S_PORT);
servaddr.sin_addr.s_addr = inet_addr(REMOTE_HOST);
if ((sockfd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
perror("failed to create socket");
exit(EXIT_FAILURE);
}
while (1) {
usleep(TX_INTERVAL_MS * 1000);
printf("Send force %d\n", force);
force += 5;
sendto(sockfd, (char*) &force, 1, MSG_CONFIRM,
(struct sockaddr *) &servaddr, sizeof(servaddr));
}
}
int main() {
int sockfd;
char buffer[STATE_SIZE + 1];
struct sockaddr_in servaddr = { 0 };
if ((sockfd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
perror("failed to create socket");
exit(EXIT_FAILURE);
}
servaddr.sin_family = AF_INET;
servaddr.sin_port = htons(R_PORT);
servaddr.sin_addr.s_addr = inet_addr(LOCAL_HOST);
if (bind(sockfd, (const struct sockaddr *) &servaddr,
sizeof(servaddr)) < 0) {
perror("bind failed");
exit(EXIT_FAILURE);
}
DIJOYSTATE2_t state;
char recvbuf[sizeof(DIJOYSTATE2_t) + 4];
pthread_t send_tid;
pthread_create(&send_tid, NULL, send_force, NULL);
while(1) {
//printf("Wait recv\n");
int n, len;
n = recvfrom(sockfd, recvbuf, STATE_SIZE, MSG_WAITALL,
(struct sockaddr *) &servaddr, &len);
uint32_t packet_ct = ((uint32_t*) recvbuf)[0];
memcpy(&state, recvbuf + 4, sizeof(state));
printf("Receive state (Pkt: %8X) : Wheel: %d | Throttle: %d | Brake: %d\n", packet_ct, state.lX, state.lY, state.lRz);
}
return 0;
}