RIT VEXU Core API
Loading...
Searching...
No Matches
protocol.h
1#pragma once
2#include <sys/types.h>
3#include <v5_api.h>
4
5#include <cstdint>
6#include <cstdio>
7#include <cstring>
8#include <string>
9#include <vector>
10
11#include "types.h"
12
13namespace VDB {
14uint32_t time_ms();
15void delay_ms(uint32_t ms);
16} // namespace VDB
17
18#define VDPWarnf(fmt, ...) printf("WARN: " fmt "\n", ##__VA_ARGS__)
19#define VDPDebugf(fmt, ...) printf("DEBUG: " fmt "\n", ##__VA_ARGS__)
20#define VDPTracef(fmt, ...) printf("TRACE: " fmt "\n", ##__VA_ARGS__)
21
22namespace VDP {
23constexpr size_t MAX_CHANNELS = 256;
24
26using ChannelID = uint8_t;
27
33enum class PacketType : uint8_t {
34 Data = 0b00000000,
35 Schema = 0b00000001,
36};
37
43enum class PacketFunction : uint8_t {
44 Send = 0b00000000,
45 Acknowledge = 0b00000010,
46};
47
54enum PacketValidity : uint8_t {
55 Ok,
56 BadChecksum,
57 TooSmall,
58};
59
62 PacketType type;
63 PacketFunction func;
64};
65
71VDP::PacketValidity validate_packet(const VDP::Packet& packet);
72
78VDP::Packet checksum_pac(VDP::Packet in);
79
81uint8_t make_header_byte(PacketHeader head);
82
84PacketHeader decode_header_byte(uint8_t hb);
85
87Packet checksum_pac(VDP::Packet in);
88
90template <typename T>
91 requires IsField<std::remove_cvref_t<T>>::value
92class Channel {
93 public:
99 explicit Channel(T& data, ChannelID id) : id_(id), data_(data), last_received_(0) {}
100
102 ChannelID get_id() const { return id_; };
103
105 T& get_data() { return data_; }
106
112 bool apply_update(VDP::PacketSpan data_packet) {
113 std::memcpy(&last_received_, data_packet.subspan(0, 4).data(), sizeof(uint32_t));
114 return data_.apply_update(data_packet.subspan(4));
115 }
116
121 VDP::Packet serialize(PacketType pac_type) {
122 VDP::Packet out;
123 // add the header and channel id to the packet
124 out.push_back((uint8_t)PacketFunction::Send | (uint8_t)pac_type);
125 out.push_back((uint8_t)id_);
126
127 // either serialize the data held by the channel or the channel schema
128 VDP::Packet packet_body(sizeof(uint32_t));
129 if (pac_type == PacketType::Data) {
130 // add the timestamp to the packet
131 uint64_t timestamp = vexSystemHighResTimeGet();
132
133 VDP::Packet timestamp_pac(sizeof(uint64_t));
134
135 // Copy the timestamp into the vector's underlying storage
136 std::memcpy(timestamp_pac.data(), &timestamp, sizeof(uint64_t));
137
138 out.insert(out.end(), timestamp_pac.begin(), timestamp_pac.end());
139
140 packet_body = data_.serialize_data();
141 } else if (pac_type == PacketType::Schema) {
142 packet_body = data_.serialize_schema();
143 }
144 out.insert(out.end(), packet_body.begin(), packet_body.end());
145
146 // add the checksum to the packet
147 VDP::Packet checksum = checksum_pac(out);
148 out.insert(out.end(), checksum.begin(), checksum.end());
149 return out;
150 };
151
153 std::string schema_to_string() const {
154 std::string out = "";
155 out += "{\n id : " + std::to_string(id_) + ",\n";
156 out += data_.schema_to_string(1) + "\n}";
157 return out;
158 }
159
161 std::string data_to_string() const {
162 std::string out = "";
163 out += "{\n id : " + std::to_string(id_) + ",\n";
164 out += " last received: " + std::to_string(id_) + ",\n";
165 out += data_.data_to_string(1) + "\n}";
166 return out;
167 }
168
169 bool acked = false;
170
171 private:
172 ChannelID id_;
173 T data_;
174 uint32_t last_received_;
175};
176
178template <typename T>
179Channel(ChannelID, T) -> Channel<T>;
180
181} // namespace VDP
std::string schema_to_string() const
Gets the channel's schema as a string.
Definition protocol.h:153
VDP::Packet serialize(PacketType pac_type)
Serializes the channel as a packet to be sent over a wire to the debug board.
Definition protocol.h:121
Channel(T &data, ChannelID id)
Constructs a channel of data to be sent to the debug board.
Definition protocol.h:99
std::string data_to_string() const
Gets the channel's data as a string.
Definition protocol.h:161
T & get_data()
Gets the data stored inside the channel.
Definition protocol.h:105
ChannelID get_id() const
Gets the channel's id.
Definition protocol.h:102
bool apply_update(VDP::PacketSpan data_packet)
Applies an update from a received packet of data to the data held by the channel.
Definition protocol.h:112
Definition protocol.h:13
Check for if a held field type is a Field.
Definition types.h:198
A packet header is byte that combines a packet function and packet type.
Definition protocol.h:61