RoveComm C++ 3.0.0
Welcome to the RoveComm C++ repository of the Mars Rover Design Team (MRDT) at Missouri University of Science and Technology (Missouri S&T)! API reference contains the source code and other resources for the development of RoveComm C++. The RoveComm C++ project is the RoveComm interface for high level systems and should not be used for microcontrollers.
Loading...
Searching...
No Matches
RoveCommPacket.h
Go to the documentation of this file.
1
10
11#ifndef ROVECOMM_PACKET_H
12#define ROVECOMM_PACKET_H
13
14#include "./RoveCommConsts.h"
15#include "./RoveCommManifest.h"
16
18#include <cstdint>
19#include <cstring>
20#include <span>
21#include <vector>
22
23#if defined(__ROVECOMM_WINDOWS_MODE__) && __ROVECOMM_WINDOWS_MODE__ == 1
24#if !defined(ssize_t) && defined(_MSC_VER)
25typedef int ssize_t;
26#endif
27#ifndef _WIN32_WINNT
28#define _WIN32_WINNT 0x0600
29#elif _WIN32_WINNT < 0x0600
30#undef _WIN32_WINNT
31#define _WIN32_WINNT 0x0600
32#endif
33#define _WINSOCKAPI_
34#include <winsock2.h>
35#include <mswsock.h>
36#include <ws2tcpip.h>
37#include <windows.h>
38#else
39#include <arpa/inet.h>
40#include <fcntl.h>
41#include <sys/socket.h>
42#include <unistd.h>
43#endif
44
46
47// These are used to convert 64-bit doubles to network bit order and back.
48#define htonll(x) ((1 == htonl(1)) ? (x) : (((uint64_t) htonl((x) & 0xFFFFFFFFUL)) << 32) | htonl((uint32_t) ((x) >> 32)))
49#define ntohll(x) ((1 == ntohl(1)) ? (x) : (((uint64_t) ntohl((x) & 0xFFFFFFFFUL)) << 32) | ntohl((uint32_t) ((x) >> 32)))
50
51
59namespace rovecomm
60{
61
73 template<typename T>
75 {
76 public:
77 uint16_t unDataId{};
78 std::vector<T> vData;
80
81 uint16_t GetDataCount() const { return static_cast<uint16_t>(vData.size()); }
82
83 size_t GetDataSize() const { return static_cast<uint16_t>(vData.size() * manifest::Helpers::DataTypeSize(eDataType)); }
84
85 uint8_t GetDataType() const { return static_cast<uint8_t>(eDataType); }
86 };
87
88 // RoveCommPacket and RoveCommData packing and unpacking functions
89 template<typename T>
90 std::vector<uint8_t> PackPacket(const RoveCommPacket<T>& stPacket);
91
92 template<typename T>
93 RoveCommPacket<T> UnpackData(std::span<const uint8_t> stData);
94
95
104 {
105 public:
106 uint16_t unDataId = 0;
108 uint64_t ullID = 0;
109 };
110
111 // Packet creation utilities
112
113 template<manifest::ManifestEntry Entry>
114 using EntryType = typename manifest::Helpers::RoveCommToCType<Entry.DATA_TYPE>::c_type;
115
116 template<manifest::ManifestEntry Entry>
118 {
119 RoveCommPacket<EntryType<Entry>> stPacket{.unDataId = Entry.DATA_ID, .vData = {}};
120 stPacket.vData.resize(Entry.DATA_COUNT);
121 return stPacket;
122 }
123
124 template<manifest::ManifestEntry Entry, manifest::RoveCommType... Args>
126 {
127 // Assume that the user expects the data count to be exactly correct since they are entering the arguments directly.
128 // The runtime functions will extend or truncate the data as needed.
129 static_assert(sizeof...(Args) == Entry.DATA_COUNT, "The number of arguments provided does not match the data count specified in the manifest entry.");
130 return {.unDataId = Entry.DATA_ID, .vData = {args...}};
131 }
132
133 template<manifest::ManifestEntry Entry>
135 {
136 RoveCommPacket<EntryType<Entry>> stPacket = {.unDataId = Entry.DATA_ID, .vData = {spData.begin(), spData.end()}};
137 stPacket.vData.resize(Entry.DATA_COUNT);
138 return stPacket;
139 }
140
141 template<manifest::ManifestEntry Entry>
143 {
144 RoveCommPacket<EntryType<Entry>> stPacket = {.unDataId = Entry.DATA_ID, .vData = ilData};
145 stPacket.vData.resize(Entry.DATA_COUNT);
146 return stPacket;
147 }
148
149} // namespace rovecomm
150
151#endif // ROVECOMM_PACKET_H
Defines Global Constants within RoveComm C++.
RoveComm Manifest.
Allows constraining templates to only RoveComm supported types.
Definition RoveCommManifest.h:53
constexpr size_t DataTypeSize(DataTypes eDataType)
Definition RoveCommManifest.h:1244
DataTypes
Enumeration of Data Types to be used in RoveComm.
Definition RoveCommManifest.h:34
@ UINT8_T
Definition RoveCommManifest.h:36
The RoveComm namespace contains all of the functionality for the RoveComm library....
Definition RoveCommConsts.h:23
typename manifest::Helpers::RoveCommToCType< Entry.DATA_TYPE >::c_type EntryType
Definition RoveCommPacket.h:114
RoveCommPacket< T > UnpackData(std::span< const uint8_t > spBytes)
Create a RoveCommPacket structure from a RoveCommData structure.
Definition RoveCommPacket.cpp:144
std::vector< uint8_t > PackPacket(const RoveCommPacket< T > &stPacket)
Create a RoveCommData structure from a RoveCommPacket structure.
Definition RoveCommPacket.cpp:44
RoveCommPacket< EntryType< Entry > > CreatePacket()
Definition RoveCommPacket.h:117
Definition RoveCommManifest.h:1263
Definition RoveCommManifest.h:1331
Manifest Entry Object for RoveComm.
Definition RoveCommManifest.h:106
int DATA_COUNT
Definition RoveCommManifest.h:109
int DATA_ID
Definition RoveCommManifest.h:108
Identifies one callback registered with a node's On(), so Clear() can remove that callback without to...
Definition RoveCommPacket.h:104
uint64_t ullID
Definition RoveCommPacket.h:108
uint16_t unDataId
Definition RoveCommPacket.h:106
manifest::DataTypes eDataType
Definition RoveCommPacket.h:107
The RoveCommPacket struct is used to store data in a packet format for transmission over a network....
Definition RoveCommPacket.h:75
uint16_t unDataId
Definition RoveCommPacket.h:77
uint8_t GetDataType() const
Definition RoveCommPacket.h:85
static constexpr manifest::DataTypes eDataType
Definition RoveCommPacket.h:79
size_t GetDataSize() const
Definition RoveCommPacket.h:83
uint16_t GetDataCount() const
Definition RoveCommPacket.h:81
std::vector< T > vData
Definition RoveCommPacket.h:78