unitree_sdk2_rs 0.1.0

Unitree SDK2 的 Rust 封装:msg 驱动的 DDS 类型生成 + CDR 序列化 + 订阅/发布/RPC
#pragma once
#include "rust/cxx.h"
#include <memory>
#include <string>
#include <functional>
#include <unordered_map>
#include <unitree/robot/channel/channel_subscriber.hpp>
#include <unitree/robot/channel/channel_publisher.hpp>

namespace unitree {

// ── 类型擦除订阅器基类 ──

class SubscriberBase {
public:
    virtual ~SubscriberBase() = default;
};

// ── 通用订阅器模板:回调把 DDS struct 序列化成 CDR bytes 传给 Rust ──

template<typename DdsT>
class Subscriber : public SubscriberBase {
public:
    Subscriber(std::string topic, std::function<void(rust::Vec<uint8_t>)> on_bytes);
private:
    unitree::robot::ChannelSubscriber<DdsT> sub_;
};

// ── 发布器模板 ──

template<typename DdsT>
class Publisher {
public:
    explicit Publisher(std::string topic);
    ~Publisher() = default;
    void write(const DdsT& msg) const { pub_.Write(msg); }
private:
    mutable unitree::robot::ChannelPublisher<DdsT> pub_;
};

} // namespace unitree

// ── 逐话题胶水(build.rs 从 msg 生成,统一放 gen/ 目录) ──
#include "../gen/gen_topics.h"

namespace unitree {

void boot_dds(int32_t domain_id, rust::Str network_interface, rust::Str config_file);

// ── 统一订阅接口(按类型名派发,topic 名任意) ──

struct ByteHandler;
int32_t subscribe_any(rust::Str topic, rust::Str type_name, rust::Box<ByteHandler> handler);
void unsubscribe(int32_t id);

} // namespace unitree