d2bf5aceee
We previously used the smoIp provided by the user, but this function is intended to enable us to figure out the correct IP to send to the target device in the Handshake message.
104 lines
2.5 KiB
C++
104 lines
2.5 KiB
C++
#ifndef LIVOX_PROTO1_DEVICE_H
|
|
#define LIVOX_PROTO1_DEVICE_H
|
|
|
|
#include <string>
|
|
#include <cstdint>
|
|
#include <memory>
|
|
#include <atomic>
|
|
#include <optional>
|
|
#include <boost/asio.hpp>
|
|
#include "protocol.h"
|
|
|
|
// Forward declaration
|
|
namespace smo {
|
|
class ComponentThread;
|
|
}
|
|
|
|
namespace livoxProto1 {
|
|
namespace comms {
|
|
|
|
/** EXPLANATION:
|
|
* This class represents a discovered device. It is used to store the
|
|
* device identifier and IP address of a discovered device.
|
|
*/
|
|
class DiscoveredDevice
|
|
{
|
|
public:
|
|
DiscoveredDevice(
|
|
const std::string &deviceIdentifier,
|
|
DeviceType deviceType,
|
|
const std::string &ipAddr);
|
|
|
|
// "Conversion" constructor from BroadcastMessage
|
|
DiscoveredDevice(const BroadcastMessage &msg, const std::string &ipAddr);
|
|
|
|
~DiscoveredDevice() = default;
|
|
|
|
bool operator==(const DiscoveredDevice &other) const
|
|
{
|
|
return comms::deviceIdentifiersEqual(
|
|
deviceIdentifier, other.deviceIdentifier);
|
|
}
|
|
|
|
std::string stringify(void) const;
|
|
std::string getDeviceTypeName(void) const;
|
|
|
|
public:
|
|
std::string deviceIdentifier;
|
|
DeviceType deviceType;
|
|
std::string ipAddr;
|
|
};
|
|
|
|
} // namespace comms
|
|
|
|
class Device
|
|
{
|
|
public:
|
|
Device(const std::string &deviceIdentifier,
|
|
const std::shared_ptr<smo::ComponentThread>& componentThread,
|
|
int handshakeTimeoutMs, int retryDelayMs,
|
|
const std::string& smoIp, uint8_t smoSubnetNbits,
|
|
uint16_t dataPort, uint16_t cmdPort, uint16_t imuPort);
|
|
~Device();
|
|
|
|
public:
|
|
comms::DiscoveredDevice discoveredDevice;
|
|
|
|
// Configuration
|
|
std::shared_ptr<smo::ComponentThread> componentThread;
|
|
int handshakeTimeoutMs, retryDelayMs;
|
|
std::string smoIp;
|
|
uint8_t smoSubnetNbits;
|
|
uint16_t dataPort, cmdPort, imuPort;
|
|
|
|
private:
|
|
// Connection logic
|
|
void connect();
|
|
bool connectToKnownDevice();
|
|
bool connectByDeviceIdentifier();
|
|
bool executeHandshake(
|
|
const std::string& deviceIP, int timeoutMs,
|
|
uint16_t dataPort, uint16_t cmdPort, uint16_t imuPort);
|
|
std::string generateClientDeviceIpFromSerialNumber(
|
|
const std::string& broadcastCode);
|
|
|
|
// IP detection methods
|
|
std::optional<std::string> detectSmoIp(const std::string& deviceIP);
|
|
std::string getSmoIp(const std::string& deviceIP);
|
|
uint32_t getSubnetMaskFor(uint8_t nbits);
|
|
|
|
// Heartbeat mechanism
|
|
void startHeartbeat();
|
|
void sendHeartbeat();
|
|
void onHeartbeatTimer(const boost::system::error_code& error);
|
|
|
|
// Heartbeat state
|
|
std::unique_ptr<boost::asio::deadline_timer> heartbeatTimer;
|
|
std::unique_ptr<boost::asio::ip::udp::socket> heartbeatSocket;
|
|
std::atomic<bool> heartbeatActive;
|
|
};
|
|
|
|
} // namespace livoxProto1
|
|
|
|
#endif // LIVOX_PROTO1_DEVICE_H
|