-
Notifications
You must be signed in to change notification settings - Fork 0
/
udp_packets.c
145 lines (94 loc) · 4.88 KB
/
udp_packets.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
//
// Created by Will Gulian on 10/10/18.
//
#include <netinet/in.h>
#include "udp_packets.h"
bool is_info_packet_valid(struct info_packet *packet) {
return packet->magic_number == MAGIC_INFO_PACKET;
}
bool is_raw_info_packet_valid(void *raw_data) {
struct info_packet *network_packet = (struct info_packet *) raw_data;
return network_packet->magic_number == htonl(MAGIC_INFO_PACKET);
}
bool encode_info_packet(struct info_packet *packet, void *raw_data) {
struct info_packet *network_packet = (struct info_packet *) raw_data;
network_packet->magic_number = htonl(packet->magic_number);
network_packet->pod_status = htonl(packet->pod_status);
network_packet->timestamp = htonl(packet->timestamp);
network_packet->acceleration = htonl(packet->acceleration);
network_packet->velocity = htonl(packet->velocity);
network_packet->battery_voltage = htonl(packet->battery_voltage);
network_packet->battery_temp = htonl(packet->battery_temp);
network_packet->pod_temp = htonl(packet->pod_temp);
network_packet->tank_pressure = htonl(packet->tank_pressure);
network_packet->left_brake_pressure = htonl(packet->left_brake_pressure);
network_packet->right_brake_pressure = htonl(packet->right_brake_pressure);
return true;
}
bool decode_info_packet(void *raw_data, struct info_packet *packet) {
struct info_packet *network_packet = (struct info_packet *) raw_data;
packet->magic_number = ntohl(network_packet->magic_number);
packet->pod_status = ntohl(network_packet->pod_status);
packet->timestamp = ntohl(network_packet->timestamp);
packet->acceleration = ntohl(network_packet->acceleration);
packet->velocity = ntohl(network_packet->velocity);
packet->battery_voltage = ntohl(network_packet->battery_voltage);
packet->battery_temp = ntohl(network_packet->battery_temp);
packet->pod_temp = ntohl(network_packet->pod_temp);
packet->tank_pressure = ntohl(network_packet->tank_pressure);
packet->left_brake_pressure = ntohl(network_packet->left_brake_pressure);
packet->right_brake_pressure = ntohl(network_packet->right_brake_pressure);
return true;
}
bool is_control_packet_valid(struct control_packet *packet) {
return packet->magic_number == MAGIC_CONTROL_PACKET;
}
bool is_raw_control_packet_valid(void *raw_data) {
struct control_packet *network_packet = (struct control_packet *) raw_data;
return network_packet->magic_number == htonl(MAGIC_CONTROL_PACKET);
}
bool encode_control_packet(struct control_packet *packet, void *raw_data) {
struct control_packet *network_packet = (struct control_packet *) raw_data;
network_packet->magic_number = htonl(packet->magic_number);
network_packet->command = htonl(packet->command);
network_packet->timestamp = htonl(packet->timestamp);
return true;
}
bool decode_control_packet(void *raw_data, struct control_packet *packet) {
struct control_packet *network_packet = (struct control_packet *) raw_data;
packet->magic_number = ntohl(network_packet->magic_number);
packet->command = ntohl(network_packet->command);
packet->timestamp = ntohl(network_packet->timestamp);
return true;
}
bool is_hyperloop_telemetry_packet_valid(struct hyperloop_telemetry_packet *packet) {
return true;
}
bool encode_hyperloop_telemetry_packet(struct hyperloop_telemetry_packet *packet, void *raw_data) {
struct hyperloop_telemetry_packet *network_packet = (struct hyperloop_telemetry_packet *) raw_data;
network_packet->team_id = packet->team_id;
network_packet->status = packet->status;
network_packet->position = htonl(packet->position);
network_packet->velocity = htonl(packet->velocity);
network_packet->acceleration = htonl(packet->acceleration);
network_packet->battery_voltage = htonl(packet->battery_voltage);
network_packet->battery_current = htonl(packet->battery_current);
network_packet->battery_temp = htonl(packet->battery_temp);
network_packet->pod_temp = htonl(packet->pod_temp);
network_packet->stripe_count = htonl(packet->stripe_count);
return true;
}
bool decode_hyperloop_telemetry_packet(void *raw_data, struct hyperloop_telemetry_packet *packet) {
struct hyperloop_telemetry_packet *network_packet = (struct hyperloop_telemetry_packet *) raw_data;
packet->team_id = network_packet->team_id;
packet->status = network_packet->status;
packet->position = ntohl(network_packet->position);
packet->velocity = ntohl(network_packet->velocity);
packet->acceleration = ntohl(network_packet->acceleration);
packet->battery_voltage = ntohl(network_packet->battery_voltage);
packet->battery_current = ntohl(network_packet->battery_current);
packet->battery_temp = ntohl(network_packet->battery_temp);
packet->pod_temp = ntohl(network_packet->pod_temp);
packet->stripe_count = ntohl(network_packet->stripe_count);
return true;
}