LivoxProto1: Implemented Device and getOrCreateDevice
Includes everything from sending heartbeat msgs to performing the connection handshake. We also accept many params to provider-params to customize and make things easier.
This commit is contained in:
@@ -0,0 +1,103 @@
|
||||
#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();
|
||||
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<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
|
||||
Reference in New Issue
Block a user