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
main.cpp File Reference

Main program file. Sets up classes and runs main program functions. More...

Include dependency graph for main.cpp:

Functions

int main ()
 RoveComm C++ main function.

Detailed Description

Main program file. Sets up classes and runs main program functions.

Author
Eli Byrd (edbgk.nosp@m.k@ms.nosp@m.t.edu)
Date
2023-07-25

Function Documentation

◆ main()

int main ( )

RoveComm C++ main function.

Returns
int - Exit status number.
Author
Eli Byrd (edbgk.nosp@m.k@ms.nosp@m.t.edu)
Date
2023-06-25
35{
36 using namespace std::literals::chrono_literals;
37 // Print Software Header
38 std::ifstream fHeaderText("../data/ASCII/v3.txt");
39 std::string szHeaderText;
40 if (fHeaderText)
41 {
42 std::ostringstream pHeaderText;
43 pHeaderText << fHeaderText.rdbuf();
44 szHeaderText = pHeaderText.str();
45 }
46
47 std::cout << szHeaderText << std::endl;
48 std::cout << "Copyright \u00A9 2024 - Mars Rover Design Team\n" << std::endl;
49
50 // Define a callback function for handling received TCP packets
51 auto fnTCPCallback = [](const RoveCommPacket<uint8_t>& stPacket)
52 {
53 std::cout << "Received TCP packet with:" << std::endl;
54 std::cout << "\tData ID: " << stPacket.unDataId << std::endl;
55 std::cout << "\tData Count: " << stPacket.GetDataCount() << std::endl;
56 std::cout << "\tData Type: " << stPacket.eDataType << std::endl;
57 std::cout << "\tData: " << std::endl;
58
59 for (auto data : stPacket.vData)
60 {
61 std::cout << "\t\t>>" << (int) data << std::endl;
62 }
63 };
64
65 // Define a callback function for handling received packets
66 auto fnUDPCallback = [](const RoveCommPacket<uint8_t>& stPacket)
67 {
68 std::cout << "Received UDP packet with:" << std::endl;
69 std::cout << "\tData ID: " << stPacket.unDataId << std::endl;
70 std::cout << "\tData Count: " << stPacket.GetDataCount() << std::endl;
71 std::cout << "\tData Type: " << stPacket.eDataType << std::endl;
72 std::cout << "\tData: " << std::endl;
73
74 for (auto data : stPacket.vData)
75 {
76 std::cout << "\t\t>>" << (int) data << std::endl;
77 }
78 };
79
80 // Create RoveComm Nodes
81 RoveCommUDP pRoveCommUDP_Node;
82 RoveCommTCP pRoveCommTCP_Node;
83
84 // Initialize the UDP and TCP nodes
85 if (pRoveCommUDP_Node.Init(11000))
86 {
87 std::cout << "UDP Node Initialized." << std::endl;
88 }
89 else
90 {
91 std::cout << "UDP Node Failed to Initialize." << std::endl;
92 }
93
94 if (pRoveCommTCP_Node.Init("127.0.0.1", 12000))
95 {
96 std::cout << "TCP Node Initialized." << std::endl;
97 }
98 else
99 {
100 std::cout << "TCP Node Failed to Initialize." << std::endl;
101 }
102
105
106 // Add the callback functions for UINT8_T data type to the UDP and TCP nodes
107 CallbackHandle stUDPHandle = pRoveCommUDP_Node.On<STARTAUTONOMY>(fnUDPCallback);
108 // pRoveCommUDP_Node.On<STARTAUTONOMY>([](const auto& stPacket) { std::cout << "UDP Callback 2\n"; });
109 CallbackHandle stTCPHandle = pRoveCommTCP_Node.On<STARTAUTONOMY>(fnTCPCallback);
110 // pRoveCommTCP_Node.On<STARTAUTONOMY>([](const auto& stPacket) { std::cout << "TCP Callback 2\n"; });
111
112 // pRoveCommUDP_Node.WriteTo<STARTAUTONOMY>({200}); // This would send to 192.168.3.100 by default
113
114 // Send the packet to the loopback address
115 pRoveCommUDP_Node.WriteTo<STARTAUTONOMY>({200}, "127.0.0.1", 11000);
116 pRoveCommTCP_Node.WriteTo<STARTAUTONOMY>({200}, "127.0.0.1", 12000);
117
118 // Wait for packets to be processed.
119 std::this_thread::sleep_for(500ms);
120
121 // Remove the callback functions for UINT8_T data type from the UDP and TCP nodes
122 pRoveCommUDP_Node.Clear(stUDPHandle);
123 pRoveCommTCP_Node.Clear(stTCPHandle);
124 // Another way to create packets
125 auto stPacket = CreatePacket<STARTAUTONOMY>(uint8_t{255});
126
127 // Send the packet to the loopback address again; should NOT see any output since we cleared the callbacks
128 pRoveCommUDP_Node.WriteTo(stPacket, "127.0.0.1", 11000);
129 pRoveCommTCP_Node.WriteTo(stPacket, "127.0.0.1", 12000);
130
131 // Wait for packets to be processed.
132 std::this_thread::sleep_for(500ms);
133
134 // Open remote UDP node to subscribe to the local UDP node.
135 RoveCommUDP pRemoteRoveCommUDP_Node;
136 if (pRemoteRoveCommUDP_Node.Init(11003))
137 {
138 std::cout << "Remote UDP Node Initialized." << std::endl;
139 }
140
141 pRemoteRoveCommUDP_Node.Subscribe("127.0.0.1", 11000);
142
143 // Wait for packets to be processed.
144 std::this_thread::sleep_for(500ms);
145
146 pRemoteRoveCommUDP_Node.On<CURRENTSTATE>(
147 [](const auto& stPacket)
148 {
149 (void) stPacket;
150 std::cout << "Remote UDP Callback" << std::endl;
151 });
152
153 // Will send to subscribers, including the remote node.
154 pRoveCommUDP_Node.Write<CURRENTSTATE>({200});
155
156 // Wait for packets to be processed.
157 std::this_thread::sleep_for(500ms);
158
159 pRemoteRoveCommUDP_Node.Unsubscribe("127.0.0.1", 11000);
160
161 // Wait for packets to be processed.
162 std::this_thread::sleep_for(500ms);
163
164 // Callback should NOT be invoked since we unsubscribed the remote node.
165 pRoveCommUDP_Node.Write<CURRENTSTATE>({200});
166
167 // Wait for packets to be processed.
168 std::this_thread::sleep_for(500ms);
169
170 // Close the UDP and TCP sockets
171 pRoveCommUDP_Node.Close();
172 pRoveCommTCP_Node.Close();
173 pRemoteRoveCommUDP_Node.Close();
174
175 std::cout << "All nodes closed." << std::endl;
176
177 exit(0);
178}
The RoveCommTCP class is used to send and receive data over a TCP connection.
Definition RoveCommTCP.h:52
void Clear(const CallbackHandle &stHandle)
Remove the one callback identified by a handle from On(). Other callbacks for the same data ID stay r...
Definition RoveCommTCP.cpp:251
void Close()
Closes the TCP socket.
Definition RoveCommTCP.cpp:459
CallbackHandle On(const uint16_t unDataId, std::function< void(const RoveCommPacket< T > &)> fnCallback)
Register a callback for TCP packets with the given data ID. The callback belongs to this node: packet...
Definition RoveCommTCP.cpp:227
bool Init(const manifest::AddressEntry &stIPAddress={0, 0, 0, 0}, int nPort=manifest::General::ETHERNET_TCP_PORT)
Initializes a TCP socket and binds it to the specified IP address and port. And then starts the threa...
Definition RoveCommTCP.cpp:76
ssize_t WriteTo(const RoveCommPacket< T > &stPacket, const manifest::AddressEntry &stIPAddress, int nPort=manifest::General::ETHERNET_UDP_PORT)
Sends a TCP packet to the specified client IP address and port. The data is packed into a RoveCommDat...
Definition RoveCommTCP.cpp:162
The RoveCommUDP class is used to send and receive data over a UDP connection.
Definition RoveCommUDP.h:52
ssize_t WriteTo(const RoveCommPacket< T > &stPacket, const manifest::AddressEntry &stIPAddress, int nPort=manifest::General::ETHERNET_UDP_PORT)
Sends a packet to a specific destination. This is often used to send a command to a specific device.
Definition RoveCommUDP.h:143
void Close()
Close the UDP socket. This method is called when the RoveCommUDP object is destroyed....
Definition RoveCommUDP.cpp:696
bool Init(int nPort=manifest::General::ETHERNET_UDP_PORT)
Initialize the UDP socket and bind it to the specified port. This method also starts the thread that ...
Definition RoveCommUDP.cpp:110
CallbackHandle On(const uint16_t unDataId, std::function< void(const RoveCommPacket< T > &)> fnCallback)
Register a callback for UDP packets with the given data ID. The callback belongs to this node: packet...
Definition RoveCommUDP.cpp:333
ssize_t Write(const RoveCommPacket< T > &stPacket)
Sends a packet to all subscribers. This is often used to send errors or telemetry data to all devices...
Definition RoveCommUDP.h:160
ssize_t Subscribe(const manifest::AddressEntry &stIPAddress, int nPort=manifest::General::ETHERNET_UDP_PORT)
Definition RoveCommUDP.h:220
void Clear(const CallbackHandle &stHandle)
Remove the one callback identified by a handle from On(). Other callbacks for the same data ID stay r...
Definition RoveCommUDP.cpp:357
ssize_t Unsubscribe(const manifest::AddressEntry &stIPAddress, int nPort=manifest::General::ETHERNET_UDP_PORT)
Definition RoveCommUDP.h:225
constexpr ManifestEntry STARTAUTONOMY
Definition RoveCommManifest.h:533
constexpr ManifestEntry CURRENTSTATE
Definition RoveCommManifest.h:562
RoveCommPacket< EntryType< Entry > > CreatePacket()
Definition RoveCommPacket.h:117
Identifies one callback registered with a node's On(), so Clear() can remove that callback without to...
Definition RoveCommPacket.h:104
The RoveCommPacket struct is used to store data in a packet format for transmission over a network....
Definition RoveCommPacket.h:75
Here is the call graph for this function: