-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
273 lines (183 loc) · 5.49 KB
/
main.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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <stdbool.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <netdb.h>
#include <pthread.h>
#include <unistd.h>
#include "udp_packets.h"
#include "udp_packets_fmt.h"
#define PORT 1153
#define BUFSIZE 2048
#define simple_ip(b3, b2, b1, b0) ((in_addr_t) htonl(((b3) << 24) | ((b2) << 16) | ((b1) << 8) | (b0)))
struct my_server {
struct sockaddr_in myaddr;
struct sockaddr_in remaddr;
ssize_t recvlen;
uint16_t port;
int fd;
unsigned char buf[BUFSIZE];
};
void init_addr(struct sockaddr_in *addr, uint16_t port) {
memset((void *)addr, 0, sizeof(*addr));
addr->sin_family = AF_INET;
addr->sin_addr.s_addr = htonl(INADDR_ANY);
addr->sin_port = htons(port);
}
bool server_bind(struct my_server *server, uint16_t port) {
init_addr(&server->myaddr, port);
server->port = port;
if ((server->fd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
perror("cannot create socket\n");
return false;
}
if (bind(server->fd, (struct sockaddr *)&server->myaddr, sizeof(server->myaddr)) < 0) {
perror("bind failed");
return false;
}
return true;
}
bool server_recv(struct my_server *server) {
socklen_t addrlen = sizeof(server->remaddr);
server->recvlen = recvfrom(server->fd, server->buf, BUFSIZE, 0, (struct sockaddr *)&server->remaddr, &addrlen);
return server->recvlen > 0;
}
int main_server() {
struct my_server server;
if (!server_bind(&server, PORT)) {
return 1;
}
printf("waiting on port %d\n", server.port);
while (server_recv(&server)) {
printf("received packet with size: %zi\n", server.recvlen);
}
return 0;
}
/* paddr: print the IP address in a standard decimal dotted format */
void paddr(in_addr_t addr) {
unsigned char *a = (unsigned char *)(&addr);
printf("%d.%d.%d.%d\n", a[0], a[1], a[2], a[3]);
}
bool resolve_hostname(const char *host, in_addr_t *addr) {
struct hostent *hp;
hp = gethostbyname(host);
if (!hp) {
fprintf(stderr, "could not obtain address of %s\n", host);
return false;
}
if (hp->h_addr_list[0] != 0) {
memcpy(addr, hp->h_addr_list[0], 4);
return true;
}
fprintf(stderr, "no addresses for %s\n", host);
return false;
}
struct my_client {
struct sockaddr_in servaddr;
int fd;
};
bool client_init(struct my_client *client, in_addr_t servaddr, uint16_t port) {
init_addr(&client->servaddr, port);
client->servaddr.sin_addr.s_addr = servaddr;
if ((client->fd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
perror("cannot create socket\n");
return false;
}
return true;
}
bool client_send(struct my_client *client, const void *payload, size_t size) {
if (sendto(client->fd, payload, size, 0, (struct sockaddr *)&client->servaddr, sizeof(client->servaddr)) < 0) {
perror("sendto failed");
return false;
}
return true;
}
int main_client() {
in_addr_t addr;
// if (!resolve_hostname("localhost", addr)) {
// return 1;
// }
addr = simple_ip(127, 0, 0, 1);
paddr(addr);
struct my_client client;
client_init(&client, addr, PORT);
const char *my_message = "Hello world";
client_send(&client, my_message, strlen(my_message) + 1);
return 0;
}
void *info_listener_thread() {
struct my_server server;
if (!server_bind(&server, PORT)) {
return NULL;
}
printf("waiting on port %d\n", server.port);
while (server_recv(&server)) {
if (server.recvlen != sizeof(struct info_packet)) {
printf("received packet with size: %zi\n", server.recvlen);
}
printf("received potential info packet (size matches)\n");
struct info_packet packet;
decode_info_packet(server.buf, &packet);
print_info_packet(&packet);
}
return NULL;
}
int main(int argc, char **argv) {
bool server = false;
for (int i = 1; i < argc; i++) {
printf("arg %d: %s\n", i, argv[i]);
if (strcmp(argv[i], "server") == 0) {
server = true;
}
}
printf("Mode: %s\n\n", server ? "server" : "device");
// if (server) {
// return main_server();
// } else {
// return main_client();
// }
struct info_packet p;
p.magic_number = MAGIC_INFO_PACKET;
p.timestamp = 5432345;
p.pod_status = STATUS_ACCELERATING;
p.velocity = 12;
p.acceleration = 100;
p.battery_voltage = 1002;
p.pod_temp = 34;
p.battery_temp = 54;
p.tank_pressure = 1234;
p.left_brake_pressure = 543;
p.right_brake_pressure = 565;
print_info_packet(&p);
struct control_packet cp;
cp.magic_number = MAGIC_CONTROL_PACKET;
cp.command = COMMAND_EMERGENCY_STOP;
cp.timestamp = 9286399;
print_control_packet(&cp);
pthread_t thread;
pthread_create(&thread, NULL, info_listener_thread, NULL);
in_addr_t addr;
// if (!resolve_hostname("localhost", addr)) {
// return 1;
// }
addr = simple_ip(127, 0, 0, 1);
paddr(addr);
struct my_client client;
client_init(&client, addr, PORT);
while (1) {
int32_t velocity;
scanf("%d", &velocity);
if (velocity == 0) {
break;
}
p.velocity = velocity;
char payload[sizeof(p)];
encode_info_packet(&p, payload);
client_send(&client, &payload, sizeof(p));
}
// printf("Hello, World!\n");
// return 0;
}