mirror of
https://github.com/jkcoxson/idevice.git
synced 2026-03-02 06:26:15 +01:00
Create location simulation example in cpp
This commit is contained in:
49
cpp/include/idevice++/adapter_stream.hpp
Normal file
49
cpp/include/idevice++/adapter_stream.hpp
Normal file
@@ -0,0 +1,49 @@
|
||||
// Jackson Coxson
|
||||
|
||||
#pragma once
|
||||
#include <cstddef>
|
||||
#include <cstdint>
|
||||
#include <idevice++/bindings.hpp>
|
||||
#include <idevice++/ffi.hpp>
|
||||
#include <vector>
|
||||
|
||||
struct IdeviceFfiError;
|
||||
struct AdapterStreamHandle;
|
||||
|
||||
namespace IdeviceFFI {
|
||||
|
||||
// Non-owning view over a stream (must call close(); no implicit free provided)
|
||||
class AdapterStream {
|
||||
public:
|
||||
explicit AdapterStream(AdapterStreamHandle* h) noexcept : h_(h) {}
|
||||
|
||||
AdapterStream(const AdapterStream&) = delete;
|
||||
AdapterStream& operator=(const AdapterStream&) = delete;
|
||||
|
||||
AdapterStream(AdapterStream&& other) noexcept : h_(other.h_) { other.h_ = nullptr; }
|
||||
AdapterStream& operator=(AdapterStream&& other) noexcept {
|
||||
if (this != &other) {
|
||||
h_ = other.h_;
|
||||
other.h_ = nullptr;
|
||||
}
|
||||
return *this;
|
||||
}
|
||||
|
||||
~AdapterStream() noexcept = default; // no auto-close; caller controls
|
||||
|
||||
AdapterStreamHandle* raw() const noexcept { return h_; }
|
||||
|
||||
bool close(FfiError& err);
|
||||
bool send(const uint8_t* data, size_t len, FfiError& err);
|
||||
bool send(const std::vector<uint8_t>& buf, FfiError& err) {
|
||||
return send(buf.data(), buf.size(), err);
|
||||
}
|
||||
|
||||
// recv into caller-provided buffer (resizes to actual length)
|
||||
bool recv(std::vector<uint8_t>& out, FfiError& err, size_t max_hint = 2048);
|
||||
|
||||
private:
|
||||
AdapterStreamHandle* h_{};
|
||||
};
|
||||
|
||||
} // namespace IdeviceFFI
|
||||
101
cpp/include/idevice++/core_device_proxy.hpp
Normal file
101
cpp/include/idevice++/core_device_proxy.hpp
Normal file
@@ -0,0 +1,101 @@
|
||||
// Jackson Coxson
|
||||
|
||||
#ifndef IDEVICE_CORE_DEVICE_PROXY_H
|
||||
#define IDEVICE_CORE_DEVICE_PROXY_H
|
||||
|
||||
#include <idevice++/bindings.hpp>
|
||||
#include <idevice++/ffi.hpp>
|
||||
#include <idevice++/provider.hpp>
|
||||
#include <idevice++/readwrite.hpp>
|
||||
|
||||
namespace IdeviceFFI {
|
||||
|
||||
using CoreProxyPtr = std::unique_ptr<CoreDeviceProxyHandle,
|
||||
FnDeleter<CoreDeviceProxyHandle, core_device_proxy_free>>;
|
||||
using AdapterPtr = std::unique_ptr<AdapterHandle, FnDeleter<AdapterHandle, adapter_free>>;
|
||||
|
||||
struct CoreClientParams {
|
||||
uint16_t mtu{};
|
||||
std::string address; // freed from Rust side after copy
|
||||
std::string netmask; // freed from Rust side after copy
|
||||
};
|
||||
|
||||
class Adapter {
|
||||
public:
|
||||
~Adapter() noexcept = default;
|
||||
Adapter(Adapter&&) noexcept = default;
|
||||
Adapter& operator=(Adapter&&) noexcept = default;
|
||||
Adapter(const Adapter&) = delete;
|
||||
Adapter& operator=(const Adapter&) = delete;
|
||||
|
||||
static Adapter adopt(AdapterHandle* h) noexcept { return Adapter(h); }
|
||||
AdapterHandle* raw() const noexcept { return handle_.get(); }
|
||||
|
||||
// Enable PCAP
|
||||
bool pcap(const std::string& path, FfiError& err) {
|
||||
if (IdeviceFfiError* e = ::adapter_pcap(handle_.get(), path.c_str())) {
|
||||
err = FfiError(e);
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
// Connect to a port, returns a ReadWrite stream (to be consumed by RSD/CoreDeviceProxy)
|
||||
std::optional<ReadWrite> connect(uint16_t port, FfiError& err) {
|
||||
ReadWriteOpaque* s = nullptr;
|
||||
if (IdeviceFfiError* e = ::adapter_connect(handle_.get(), port, &s)) {
|
||||
err = FfiError(e);
|
||||
return std::nullopt;
|
||||
}
|
||||
return ReadWrite::adopt(s);
|
||||
}
|
||||
|
||||
private:
|
||||
explicit Adapter(AdapterHandle* h) noexcept : handle_(h) {}
|
||||
AdapterPtr handle_{};
|
||||
};
|
||||
|
||||
class CoreDeviceProxy {
|
||||
public:
|
||||
// Factory: connect using a Provider (NOT consumed on success or error)
|
||||
static std::optional<CoreDeviceProxy> connect(Provider& provider, FfiError& err);
|
||||
|
||||
// Factory: from a socket; Rust consumes the socket regardless of result → we release before
|
||||
// call
|
||||
static std::optional<CoreDeviceProxy> from_socket(Idevice&& socket, FfiError& err);
|
||||
|
||||
// Send/recv
|
||||
bool send(const uint8_t* data, size_t len, FfiError& err);
|
||||
bool send(const std::vector<uint8_t>& buf, FfiError& err) {
|
||||
return send(buf.data(), buf.size(), err);
|
||||
}
|
||||
|
||||
// recv into a pre-sized buffer; resizes to actual bytes received
|
||||
bool recv(std::vector<uint8_t>& out, FfiError& err);
|
||||
|
||||
// Handshake info
|
||||
std::optional<CoreClientParams> get_client_parameters(FfiError& err) const;
|
||||
std::optional<std::string> get_server_address(FfiError& err) const;
|
||||
std::optional<uint16_t> get_server_rsd_port(FfiError& err) const;
|
||||
|
||||
// Consuming creation of a TCP adapter: Rust consumes the proxy handle
|
||||
std::optional<Adapter> create_tcp_adapter(FfiError& err) &&;
|
||||
|
||||
// RAII / moves
|
||||
~CoreDeviceProxy() noexcept = default;
|
||||
CoreDeviceProxy(CoreDeviceProxy&&) noexcept = default;
|
||||
CoreDeviceProxy& operator=(CoreDeviceProxy&&) noexcept = default;
|
||||
CoreDeviceProxy(const CoreDeviceProxy&) = delete;
|
||||
CoreDeviceProxy& operator=(const CoreDeviceProxy&) = delete;
|
||||
|
||||
CoreDeviceProxyHandle* raw() const noexcept { return handle_.get(); }
|
||||
CoreDeviceProxyHandle* release() noexcept { return handle_.release(); }
|
||||
static CoreDeviceProxy adopt(CoreDeviceProxyHandle* h) noexcept { return CoreDeviceProxy(h); }
|
||||
|
||||
private:
|
||||
explicit CoreDeviceProxy(CoreDeviceProxyHandle* h) noexcept : handle_(h) {}
|
||||
CoreProxyPtr handle_{};
|
||||
};
|
||||
|
||||
} // namespace IdeviceFFI
|
||||
#endif
|
||||
38
cpp/include/idevice++/location_simulation.hpp
Normal file
38
cpp/include/idevice++/location_simulation.hpp
Normal file
@@ -0,0 +1,38 @@
|
||||
// Jackson Coxson
|
||||
|
||||
#pragma once
|
||||
#include <idevice++/bindings.hpp>
|
||||
#include <idevice++/remote_server.hpp>
|
||||
#include <memory>
|
||||
#include <optional>
|
||||
|
||||
namespace IdeviceFFI {
|
||||
|
||||
using LocSimPtr = std::unique_ptr<LocationSimulationHandle,
|
||||
FnDeleter<LocationSimulationHandle, location_simulation_free>>;
|
||||
|
||||
class LocationSimulation {
|
||||
public:
|
||||
// Factory: borrows the RemoteServer; not consumed
|
||||
static std::optional<LocationSimulation> create(RemoteServer& server, FfiError& err);
|
||||
|
||||
bool clear(FfiError& err);
|
||||
bool set(double latitude, double longitude, FfiError& err);
|
||||
|
||||
~LocationSimulation() noexcept = default;
|
||||
LocationSimulation(LocationSimulation&&) noexcept = default;
|
||||
LocationSimulation& operator=(LocationSimulation&&) noexcept = default;
|
||||
LocationSimulation(const LocationSimulation&) = delete;
|
||||
LocationSimulation& operator=(const LocationSimulation&) = delete;
|
||||
|
||||
LocationSimulationHandle* raw() const noexcept { return handle_.get(); }
|
||||
static LocationSimulation adopt(LocationSimulationHandle* h) noexcept {
|
||||
return LocationSimulation(h);
|
||||
}
|
||||
|
||||
private:
|
||||
explicit LocationSimulation(LocationSimulationHandle* h) noexcept : handle_(h) {}
|
||||
LocSimPtr handle_{};
|
||||
};
|
||||
|
||||
} // namespace IdeviceFFI
|
||||
45
cpp/include/idevice++/readwrite.hpp
Normal file
45
cpp/include/idevice++/readwrite.hpp
Normal file
@@ -0,0 +1,45 @@
|
||||
// Jackson Coxson
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <idevice++/bindings.hpp>
|
||||
|
||||
namespace IdeviceFFI {
|
||||
|
||||
// A move-only holder for a fat-pointer stream. It does NOT free on destruction.
|
||||
// Always pass ownership to an FFI that consumes it by calling release().
|
||||
class ReadWrite {
|
||||
public:
|
||||
ReadWrite() noexcept : ptr_(nullptr) {}
|
||||
explicit ReadWrite(ReadWriteOpaque* p) noexcept : ptr_(p) {}
|
||||
|
||||
ReadWrite(const ReadWrite&) = delete;
|
||||
ReadWrite& operator=(const ReadWrite&) = delete;
|
||||
|
||||
ReadWrite(ReadWrite&& other) noexcept : ptr_(other.ptr_) { other.ptr_ = nullptr; }
|
||||
ReadWrite& operator=(ReadWrite&& other) noexcept {
|
||||
if (this != &other) {
|
||||
ptr_ = other.ptr_;
|
||||
other.ptr_ = nullptr;
|
||||
}
|
||||
return *this;
|
||||
}
|
||||
|
||||
~ReadWrite() noexcept = default; // no dtor – Rust consumers own free/drop
|
||||
|
||||
ReadWriteOpaque* raw() const noexcept { return ptr_; }
|
||||
ReadWriteOpaque* release() noexcept {
|
||||
auto* p = ptr_;
|
||||
ptr_ = nullptr;
|
||||
return p;
|
||||
}
|
||||
|
||||
static ReadWrite adopt(ReadWriteOpaque* p) noexcept { return ReadWrite(p); }
|
||||
|
||||
explicit operator bool() const noexcept { return ptr_ != nullptr; }
|
||||
|
||||
private:
|
||||
ReadWriteOpaque* ptr_;
|
||||
};
|
||||
|
||||
} // namespace IdeviceFFI
|
||||
44
cpp/include/idevice++/remote_server.hpp
Normal file
44
cpp/include/idevice++/remote_server.hpp
Normal file
@@ -0,0 +1,44 @@
|
||||
// Jackson Coxson
|
||||
|
||||
#ifndef IDEVICE_REMOTE_SERVER_H
|
||||
#define IDEVICE_REMOTE_SERVER_H
|
||||
|
||||
#pragma once
|
||||
#include <idevice++/core_device_proxy.hpp>
|
||||
#include <idevice++/idevice.hpp>
|
||||
#include <idevice++/readwrite.hpp>
|
||||
#include <idevice++/rsd.hpp>
|
||||
#include <memory>
|
||||
#include <optional>
|
||||
|
||||
namespace IdeviceFFI {
|
||||
|
||||
using RemoteServerPtr =
|
||||
std::unique_ptr<RemoteServerHandle, FnDeleter<RemoteServerHandle, remote_server_free>>;
|
||||
|
||||
class RemoteServer {
|
||||
public:
|
||||
// Factory: consumes the ReadWrite stream regardless of result
|
||||
static std::optional<RemoteServer> from_socket(ReadWrite&& rw, FfiError& err);
|
||||
|
||||
// Factory: borrows adapter + handshake (neither is consumed)
|
||||
static std::optional<RemoteServer>
|
||||
connect_rsd(Adapter& adapter, RsdHandshake& rsd, FfiError& err);
|
||||
|
||||
// RAII / moves
|
||||
~RemoteServer() noexcept = default;
|
||||
RemoteServer(RemoteServer&&) noexcept = default;
|
||||
RemoteServer& operator=(RemoteServer&&) noexcept = default;
|
||||
RemoteServer(const RemoteServer&) = delete;
|
||||
RemoteServer& operator=(const RemoteServer&) = delete;
|
||||
|
||||
RemoteServerHandle* raw() const noexcept { return handle_.get(); }
|
||||
static RemoteServer adopt(RemoteServerHandle* h) noexcept { return RemoteServer(h); }
|
||||
|
||||
private:
|
||||
explicit RemoteServer(RemoteServerHandle* h) noexcept : handle_(h) {}
|
||||
RemoteServerPtr handle_{};
|
||||
};
|
||||
|
||||
} // namespace IdeviceFFI
|
||||
#endif
|
||||
55
cpp/include/idevice++/rsd.hpp
Normal file
55
cpp/include/idevice++/rsd.hpp
Normal file
@@ -0,0 +1,55 @@
|
||||
// Jackson Coxson
|
||||
|
||||
#ifndef IDEVICE_RSD_H
|
||||
#define IDEVICE_RSD_H
|
||||
|
||||
#include <idevice++/ffi.hpp>
|
||||
#include <idevice++/idevice.hpp>
|
||||
#include <idevice++/readwrite.hpp>
|
||||
#include <vector>
|
||||
|
||||
namespace IdeviceFFI {
|
||||
|
||||
struct RsdService {
|
||||
std::string name;
|
||||
std::string entitlement;
|
||||
uint16_t port{};
|
||||
bool uses_remote_xpc{};
|
||||
std::vector<std::string> features;
|
||||
int64_t service_version{-1};
|
||||
};
|
||||
|
||||
using RsdPtr =
|
||||
std::unique_ptr<RsdHandshakeHandle, FnDeleter<RsdHandshakeHandle, rsd_handshake_free>>;
|
||||
|
||||
class RsdHandshake {
|
||||
public:
|
||||
// Factory: consumes the ReadWrite socket regardless of result
|
||||
static std::optional<RsdHandshake> from_socket(ReadWrite&& rw, FfiError& err);
|
||||
|
||||
// Basic info
|
||||
std::optional<size_t> protocol_version(FfiError& err) const;
|
||||
std::optional<std::string> uuid(FfiError& err) const;
|
||||
|
||||
// Services
|
||||
std::optional<std::vector<RsdService>> services(FfiError& err) const;
|
||||
std::optional<bool> service_available(const std::string& name, FfiError& err) const;
|
||||
std::optional<RsdService> service_info(const std::string& name, FfiError& err) const;
|
||||
|
||||
// RAII / moves
|
||||
~RsdHandshake() noexcept = default;
|
||||
RsdHandshake(RsdHandshake&&) noexcept = default;
|
||||
RsdHandshake& operator=(RsdHandshake&&) noexcept = default;
|
||||
RsdHandshake(const RsdHandshake&) = delete;
|
||||
RsdHandshake& operator=(const RsdHandshake&) = delete;
|
||||
|
||||
RsdHandshakeHandle* raw() const noexcept { return handle_.get(); }
|
||||
static RsdHandshake adopt(RsdHandshakeHandle* h) noexcept { return RsdHandshake(h); }
|
||||
|
||||
private:
|
||||
explicit RsdHandshake(RsdHandshakeHandle* h) noexcept : handle_(h) {}
|
||||
RsdPtr handle_{};
|
||||
};
|
||||
|
||||
} // namespace IdeviceFFI
|
||||
#endif
|
||||
Reference in New Issue
Block a user