64 std::optional<ParseError>
parse;
186 std::vector<std::byte>
raw;
188 InquireResult() =
default;
192 InquireResult & operator=(InquireResult &&) noexcept = default;
193 InquireResult(const InquireResult &) = delete;
194 InquireResult & operator=(const InquireResult &) = delete;
195 ~InquireResult() = default;
198 [[nodiscard]] std::optional<std::span<const std::
byte>>
get(
Key key) const noexcept
214 [[nodiscard]]
static std::expected<Session, SessionError>
connect(
217 [[nodiscard]]
static std::expected<Session, SessionError>
connect(
229 [[nodiscard]]
const std::string &
serial_number() const noexcept {
return serial_; }
243 [[nodiscard]] std::expected<RawAck, SessionError>
request(
244 std::uint16_t cmd_id, std::span<const std::byte> data,
245 std::optional<RequestOptions> opts = std::nullopt);
254 std::optional<RequestOptions> opts = std::nullopt);
256 [[nodiscard]] std::expected<ParamConfigAck, SessionError>
configure(
257 std::span<const KeyValue> kvs, std::optional<RequestOptions> opts = std::nullopt);
259 [[nodiscard]] std::expected<InquireResult, SessionError>
inquire(
260 std::span<const std::uint16_t> keys, std::optional<RequestOptions> opts = std::nullopt);
262 [[nodiscard]] std::expected<InquireResult, SessionError>
inquire(
263 std::span<const Key> keys, std::optional<RequestOptions> opts = std::nullopt);
265 [[nodiscard]] std::expected<SimpleAck, SessionError>
reboot(
266 std::uint16_t timeout_ms = 100, std::optional<RequestOptions> opts = std::nullopt);
269 std::optional<RequestOptions> opts = std::nullopt);
272 std::uint64_t pps_time_ns, std::optional<RequestOptions> opts = std::nullopt);
280 [[nodiscard]] std::expected<WorkState, SessionError>
work_state(
281 std::optional<RequestOptions> opts = std::nullopt);
285 WorkState target, std::chrono::milliseconds timeout);
294 friend struct detail::SessionAccess;
299 [[nodiscard]] std::expected<
void,
SessionError> read_serial();
307 std::uint32_t next_seq_ = 1;
308 std::atomic<
bool> cancel_{
false};
Waits for readability on a set of sockets.
Definition transport.hpp:205
std::expected< RawAck, SessionError > request(std::uint16_t cmd_id, std::span< const std::byte > data, std::optional< RequestOptions > opts=std::nullopt)
Send cmd_id with data and wait for the matching ACK.
std::expected< DiscoveryAck, SessionError > discovery_ack(std::optional< RequestOptions > opts=std::nullopt)
0x0000 sent unicast to this LiDAR.
const SessionStats & stats() const noexcept
Request counters since connect().
Definition session.hpp:237
Session & operator=(Session &&other) noexcept
See the move constructor.
std::expected< SimpleAck, SessionError > factory_reset(std::optional< RequestOptions > opts=std::nullopt)
0x0201.
Endpoint local_endpoint() const noexcept
Address the command socket is bound to.
Definition session.hpp:233
static std::expected< Session, SessionError > connect(const DiscoveredDevice &device, const SessionOptions &options={})
Connect using a discovery result (cmd endpoint = ip:cmd_port).
Endpoint lidar_endpoint() const noexcept
Command endpoint of the LiDAR (ip:cmd_port).
Definition session.hpp:231
std::expected< InquireResult, SessionError > inquire(std::span< const Key > keys, std::optional< RequestOptions > opts=std::nullopt)
0x0101 with typed keys.
const SessionOptions & options() const noexcept
Options passed to connect().
Definition session.hpp:235
std::expected< SimpleAck, SessionError > reboot(std::uint16_t timeout_ms=100, std::optional< RequestOptions > opts=std::nullopt)
0x0200. The LiDAR goes silent after the ACK.
std::expected< void, SessionError > wait_for_state(WorkState target, std::chrono::milliseconds timeout)
Poll 0x8006 until target is observed.
static std::expected< Session, SessionError > connect(Endpoint cmd_endpoint, const SessionOptions &options={})
Connect to a known command endpoint; the serial number is read from the device.
std::expected< SimpleAck, SessionError > set_gps_time(std::uint64_t pps_time_ns, std::optional< RequestOptions > opts=std::nullopt)
0x0202.
std::expected< InquireResult, SessionError > inquire(std::span< const std::uint16_t > keys, std::optional< RequestOptions > opts=std::nullopt)
0x0101.
std::expected< WorkState, SessionError > work_state(std::optional< RequestOptions > opts=std::nullopt)
Read key 0x8006.
Session(Session &&other) noexcept
Takes over the socket, the counters and a pending cancel request of other.
std::expected< ParamConfigAck, SessionError > configure(std::span< const KeyValue > kvs, std::optional< RequestOptions > opts=std::nullopt)
0x0100. ret_code 0x21 (effective after reboot) is returned as success.
void cancel() noexcept
Abort a blocking call from another thread; it returns kCancelled.
const std::string & serial_number() const noexcept
Serial number of the connected LiDAR.
Definition session.hpp:229
Non-blocking IPv4 UDP socket. Move-only; closes the descriptor on destruction.
Definition transport.hpp:143
Symbol visibility helpers.
Key definitions and typed encoders/decoders for the Mid-360 key-value list (section "0x0102 LiDAR Inf...
The livox_mid360_core library: everything declared by the public headers.
Definition bytes.hpp:14
constexpr std::uint8_t kMid360DevType
dev_type a Mid-360 reports in the 0x0000 ACK.
Definition session.hpp:88
std::optional< std::span< const std::byte > > find_key(std::span< const KeyValue > kvs, Key key) noexcept
Finds the first entry with key in a parsed list.
constexpr std::uint16_t kDefaultHostCommandPort
host port for commands
Definition protocol.hpp:40
std::expected< std::vector< DiscoveredDevice >, SessionError > discover(const DiscoveryOptions &options={})
Send 0x0000 and collect ACKs.
Key
Keys of the key-value list (0x0100 configure, 0x0101 inquire, 0x0102 push).
Definition keys.hpp:32
std::array< std::uint8_t, 4 > Ipv4
IPv4 address in network order, e.g. {192, 168, 1, 12}.
Definition keys.hpp:93
DeviceType
Device type named by the dev_type byte of the 0x0000 ACK (#58).
Definition session.hpp:82
@ kUnknown
any other value; the raw byte stays in DiscoveredDevice::dev_type
Definition session.hpp:83
RetCode
Result code carried as the first byte of every ACK.
Definition protocol.hpp:93
WorkState
LiDAR working state (key 0x8006 cur_work_state / 0x001A work_tgt_mode).
Definition protocol.hpp:113
std::string_view to_string(Event::Kind kind) noexcept
Name of an event kind, e.g. "state_changed".
constexpr DeviceType to_device_type(std::uint8_t dev_type) noexcept
Map a raw dev_type byte; every value other than kMid360DevType is kUnknown.
Definition session.hpp:91
SessionErrorKind
Category of a SessionError.
Definition session.hpp:44
@ kLidarRejected
ACK with ret_code != success; see ret_code / error_key.
Definition session.hpp:48
@ kTransport
socket error; see transport
Definition session.hpp:45
@ kTimeout
no matching ACK within all attempts
Definition session.hpp:46
@ kInvalidArgument
rejected before sending; see error_key for the offending key
Definition session.hpp:51
@ kCancelled
cancel() was called
Definition session.hpp:50
@ kUnexpectedState
wait_for_state observed ERROR or UPGRADE; see work_state
Definition session.hpp:49
@ kBadResponse
ACK arrived but its payload did not parse / serial mismatch.
Definition session.hpp:47
Pure-function protocol layer for the Livox Mid-360: framing, parsing and serialisation of control-com...
One LiDAR that answered the 0x0000 discovery broadcast.
Definition session.hpp:101
std::string serial_number
from the ACK, NUL padding removed
Definition session.hpp:102
Ipv4 ip
lidar_ip from the ACK
Definition session.hpp:103
Endpoint from
where the ACK actually came from
Definition session.hpp:106
std::uint16_t cmd_port
command port from the ACK
Definition session.hpp:104
std::uint8_t dev_type
device type code from the ACK, kept raw
Definition session.hpp:105
constexpr DeviceType device_type() const noexcept
dev_type as a DeviceType.
Definition session.hpp:109
Parameters of discover().
Definition session.hpp:117
std::optional< DeviceType > device_type
Keep only the devices of this type (#58); nullopt keeps every answer.
Definition session.hpp:129
std::vector< Endpoint > targets
Unicast targets (ip:port).
Definition session.hpp:120
Ipv4 bind_address
Local address to bind the discovery socket to (selects the interface for broadcast).
Definition session.hpp:123
std::chrono::milliseconds timeout
total time to wait for ACKs
Definition session.hpp:121
std::stop_token stop
Optional stop token: when a stop is requested the call returns kCancelled within about 100 ms (issue ...
Definition session.hpp:126
IPv4 address + UDP port. Mid-360 is IPv4 only.
Definition transport.hpp:37
std::vector< KeyValue > values
views into raw
Definition session.hpp:185
std::vector< std::byte > raw
owned copy of the ACK payload
Definition session.hpp:186
std::optional< std::span< const std::byte > > get(Key key) const noexcept
Value bytes of key, or nullopt when the ACK did not carry it.
Definition session.hpp:198
InquireResult(InquireResult &&) noexcept=default
Moves keep values valid: the views follow the moved raw buffer.
RetCode ret_code
ret_code of the ACK
Definition session.hpp:184
Result of a raw request: the ACK payload, owned.
Definition session.hpp:175
std::vector< std::byte > data
ACK payload (the frame's data field).
Definition session.hpp:178
std::uint32_t seq_num
sequence number shared by the request and the ACK
Definition session.hpp:177
std::uint16_t cmd_id
command the ACK answers
Definition session.hpp:176
Timeout and retry policy of one request.
Definition session.hpp:143
std::uint32_t attempts
total datagrams sent (1 = no retry)
Definition session.hpp:145
std::chrono::milliseconds timeout
per attempt
Definition session.hpp:144
Error of a Session call or of discover(). Only the fields named by kind are meaningful.
Definition session.hpp:59
RetCode ret_code
kLidarRejected only
Definition session.hpp:65
std::optional< WorkState > work_state
kUnexpectedState only
Definition session.hpp:69
std::uint32_t attempts
datagrams sent for this request
Definition session.hpp:62
std::uint16_t cmd_id
command in flight, 0 when not applicable
Definition session.hpp:61
std::optional< TransportError > transport
kTransport only
Definition session.hpp:63
std::optional< ParseError > parse
kBadResponse when the ACK payload did not parse
Definition session.hpp:64
SessionErrorKind kind
what went wrong
Definition session.hpp:60
std::uint16_t error_key
kLidarRejected on 0x0100 (from the ACK) and 0x0101 (for kParamNotSupport the first requested key the ...
Definition session.hpp:68
Parameters of Session::connect().
Definition session.hpp:150
std::stop_token stop
Optional stop token checked by every blocking call (like cancel(), but level-triggered and shared by ...
Definition session.hpp:160
std::chrono::milliseconds state_poll_interval
wait_for_state() poll period
Definition session.hpp:156
std::uint16_t host_command_port
0 = ephemeral
Definition session.hpp:151
Ipv4 bind_address
local address of the command socket
Definition session.hpp:152
bool verify_serial
On connect, read key 0x8000 and compare with the discovered serial number.
Definition session.hpp:154
RequestOptions request
default for calls that pass no RequestOptions
Definition session.hpp:155
Counters of one Session, see Session::stats().
Definition session.hpp:165
std::uint64_t timeouts
requests that exhausted all attempts
Definition session.hpp:168
std::uint64_t requests
request() calls
Definition session.hpp:166
std::uint64_t retries
datagrams sent beyond the first per request
Definition session.hpp:167
std::uint64_t late_acks
ACKs that matched no pending request.
Definition session.hpp:169
std::uint64_t bad_frames
datagrams that failed parse_command_frame
Definition session.hpp:170
UDP transport layer: a thin, dependency-free wrapper over POSIX sockets.