update ErrorCode, status, lifecycle
This commit is contained in:
46
include/lidarlib/error.hpp
Normal file
46
include/lidarlib/error.hpp
Normal file
@@ -0,0 +1,46 @@
|
||||
#pragma once
|
||||
|
||||
namespace lidarlib {
|
||||
|
||||
// Result of open() and the sticky status behind last_error(). Ok == 0 so
|
||||
// `if (err != ErrorCode::Ok)` reads naturally at call sites.
|
||||
enum class ErrorCode {
|
||||
Ok = 0,
|
||||
|
||||
// Lifecycle misuse — the call was refused, the instance state is unchanged.
|
||||
AlreadyOpen, // open() called while already open
|
||||
NotOpen, // recv_scan()/spin_once() called before open()
|
||||
|
||||
// open() failures
|
||||
SocketError, // socket() creation failed
|
||||
InvalidAddress, // ip string is not a valid IPv4 address
|
||||
PortInUse, // bind: local port already taken (EADDRINUSE/EACCES)
|
||||
BindFailed, // bind failed for another reason
|
||||
ConnectionRefused, // TCP connect refused (device up, port closed)
|
||||
ConnectionFailed, // TCP connect failed (unreachable, no route, ...)
|
||||
HandshakeFailed, // connected, but the start-stream command failed
|
||||
|
||||
// Runtime failures
|
||||
Timeout, // no (complete) scan within timeout_ms
|
||||
DeviceDisconnected, // peer closed the connection / socket recv error
|
||||
};
|
||||
|
||||
inline const char* to_string(ErrorCode e) {
|
||||
switch (e) {
|
||||
case ErrorCode::Ok: return "Ok";
|
||||
case ErrorCode::AlreadyOpen: return "AlreadyOpen";
|
||||
case ErrorCode::NotOpen: return "NotOpen";
|
||||
case ErrorCode::SocketError: return "SocketError";
|
||||
case ErrorCode::InvalidAddress: return "InvalidAddress";
|
||||
case ErrorCode::PortInUse: return "PortInUse";
|
||||
case ErrorCode::BindFailed: return "BindFailed";
|
||||
case ErrorCode::ConnectionRefused: return "ConnectionRefused";
|
||||
case ErrorCode::ConnectionFailed: return "ConnectionFailed";
|
||||
case ErrorCode::HandshakeFailed: return "HandshakeFailed";
|
||||
case ErrorCode::Timeout: return "Timeout";
|
||||
case ErrorCode::DeviceDisconnected: return "DeviceDisconnected";
|
||||
}
|
||||
return "Unknown";
|
||||
}
|
||||
|
||||
} // namespace lidarlib
|
||||
@@ -1,4 +1,5 @@
|
||||
#pragma once
|
||||
#include "lidarlib/error.hpp"
|
||||
#include <cstdint>
|
||||
#include <vector>
|
||||
#include <string>
|
||||
@@ -83,14 +84,28 @@ class Lidar {
|
||||
public:
|
||||
virtual ~Lidar() = default;
|
||||
|
||||
virtual bool open() = 0;
|
||||
// ErrorCode::Ok on success. Calling open() on an already-open instance
|
||||
// returns AlreadyOpen and leaves the connection untouched.
|
||||
virtual ErrorCode open() = 0;
|
||||
// Idempotent: safe to call before open() or more than once.
|
||||
virtual void close() = 0;
|
||||
// Block until one full scan; false on error/timeout. timeout_ms = 0 → block
|
||||
// indefinitely. No default on purpose: drivers differ (OLEI 1000, SICK 2000).
|
||||
// Block until one full scan; false on error/timeout (see last_error()).
|
||||
// timeout_ms = 0 → block indefinitely. No default on purpose: drivers
|
||||
// differ (OLEI 1000, SICK 2000).
|
||||
virtual bool recv_scan(ScanResult& out, int timeout_ms) = 0;
|
||||
virtual void set_scan_callback(ScanCallback cb) = 0;
|
||||
virtual bool spin_once() = 0;
|
||||
virtual const char* detected_model() const = 0;
|
||||
|
||||
virtual bool is_open() const = 0;
|
||||
// Status of the most recent open()/recv_scan()/spin_once() call.
|
||||
ErrorCode last_error() const { return last_error_; }
|
||||
|
||||
protected:
|
||||
ErrorCode set_error(ErrorCode e) { last_error_ = e; return e; }
|
||||
|
||||
private:
|
||||
ErrorCode last_error_ = ErrorCode::Ok;
|
||||
};
|
||||
|
||||
// OLEI UDP driver.
|
||||
@@ -109,11 +124,12 @@ public:
|
||||
Driver(const Driver&) = delete;
|
||||
Driver& operator=(const Driver&) = delete;
|
||||
|
||||
bool open() override;
|
||||
ErrorCode open() override;
|
||||
void close() override;
|
||||
bool recv_scan(ScanResult& out, int timeout_ms = 1000) override;
|
||||
void set_scan_callback(ScanCallback cb) override { cb_ = std::move(cb); }
|
||||
bool spin_once() override;
|
||||
bool is_open() const override { return sock_fd_ >= 0; }
|
||||
|
||||
// Model name read from the Family B/C header; "AUTO" until one is seen.
|
||||
const char* detected_model() const override { return detected_model_name_.c_str(); }
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
#pragma once
|
||||
// One-include convenience header for the whole public API.
|
||||
#include "lidarlib/error.hpp"
|
||||
#include "lidarlib/lidar.hpp"
|
||||
#include "lidarlib/sick_lidar.hpp"
|
||||
#include "lidarlib/config.hpp"
|
||||
|
||||
@@ -29,11 +29,12 @@ public:
|
||||
SickDriver& operator=(const SickDriver&) = delete;
|
||||
|
||||
// Connect + send "sEN LMDscandata 1" to start continuous scan output.
|
||||
bool open() override;
|
||||
ErrorCode open() override;
|
||||
void close() override;
|
||||
bool recv_scan(ScanResult& out, int timeout_ms = 2000) override;
|
||||
void set_scan_callback(ScanCallback cb) override { cb_ = std::move(cb); }
|
||||
bool spin_once() override;
|
||||
bool is_open() const override { return sock_fd_ >= 0; }
|
||||
|
||||
// No model string on the wire — returns the configured name.
|
||||
const char* detected_model() const override { return detected_model_name_.c_str(); }
|
||||
@@ -73,11 +74,12 @@ public:
|
||||
NanoScanDriver(const NanoScanDriver&) = delete;
|
||||
NanoScanDriver& operator=(const NanoScanDriver&) = delete;
|
||||
|
||||
bool open() override;
|
||||
ErrorCode open() override;
|
||||
void close() override;
|
||||
bool recv_scan(ScanResult& out, int timeout_ms = 1000) override;
|
||||
void set_scan_callback(ScanCallback cb) override { cb_ = std::move(cb); }
|
||||
bool spin_once() override;
|
||||
bool is_open() const override { return sock_fd_ >= 0; }
|
||||
|
||||
// No model string on the wire — returns the configured name.
|
||||
const char* detected_model() const override { return detected_model_name_.c_str(); }
|
||||
|
||||
Reference in New Issue
Block a user