#ifndef LIVOX_PROTO1_DEVICE_H #define LIVOX_PROTO1_DEVICE_H #include #include #include #include #include #include #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& 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 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 detectSmoIp(); std::string getSmoIp(); 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 heartbeatTimer; std::unique_ptr heartbeatSocket; std::atomic heartbeatActive; }; } // namespace livoxProto1 #endif // LIVOX_PROTO1_DEVICE_H