Create location simulation example in cpp

This commit is contained in:
Jackson Coxson
2025-08-15 14:56:02 -06:00
parent b00be3fa26
commit 36770ffd67
12 changed files with 820 additions and 0 deletions

View File

@@ -0,0 +1,43 @@
// Jackson Coxson
#include <idevice++/adapter_stream.hpp>
namespace IdeviceFFI {
bool AdapterStream::close(FfiError& err) {
if (!h_)
return true;
if (IdeviceFfiError* e = ::adapter_close(h_)) {
err = FfiError(e);
return false;
}
h_ = nullptr;
return true;
}
bool AdapterStream::send(const uint8_t* data, size_t len, FfiError& err) {
if (!h_)
return false;
if (IdeviceFfiError* e = ::adapter_send(h_, data, len)) {
err = FfiError(e);
return false;
}
return true;
}
bool AdapterStream::recv(std::vector<uint8_t>& out, FfiError& err, size_t max_hint) {
if (!h_)
return false;
if (max_hint == 0)
max_hint = 2048;
out.resize(max_hint);
size_t actual = 0;
if (IdeviceFfiError* e = ::adapter_recv(h_, out.data(), &actual, out.size())) {
err = FfiError(e);
return false;
}
out.resize(actual);
return true;
}
} // namespace IdeviceFFI

119
cpp/src/core_device.cpp Normal file
View File

@@ -0,0 +1,119 @@
// Jackson Coxson
#include <idevice++/core_device_proxy.hpp>
namespace IdeviceFFI {
// ---- Factories ----
std::optional<CoreDeviceProxy> CoreDeviceProxy::connect(Provider& provider, FfiError& err) {
CoreDeviceProxyHandle* out = nullptr;
if (IdeviceFfiError* e = ::core_device_proxy_connect(provider.raw(), &out)) {
err = FfiError(e);
return std::nullopt;
}
return CoreDeviceProxy::adopt(out);
}
std::optional<CoreDeviceProxy> CoreDeviceProxy::from_socket(Idevice&& socket, FfiError& err) {
CoreDeviceProxyHandle* out = nullptr;
// Rust consumes the socket regardless of result → release BEFORE call
IdeviceHandle* raw = socket.release();
if (IdeviceFfiError* e = ::core_device_proxy_new(raw, &out)) {
// socket is already consumed on error; do NOT touch it
err = FfiError(e);
return std::nullopt;
}
return CoreDeviceProxy::adopt(out);
}
// ---- IO ----
bool CoreDeviceProxy::send(const uint8_t* data, size_t len, FfiError& err) {
if (IdeviceFfiError* e = ::core_device_proxy_send(handle_.get(), data, len)) {
err = FfiError(e);
return false;
}
return true;
}
bool CoreDeviceProxy::recv(std::vector<uint8_t>& out, FfiError& err) {
if (out.empty())
out.resize(4096); // a reasonable default; caller can pre-size
size_t actual = 0;
if (IdeviceFfiError* e =
::core_device_proxy_recv(handle_.get(), out.data(), &actual, out.size())) {
err = FfiError(e);
return false;
}
out.resize(actual);
return true;
}
// ---- Handshake ----
std::optional<CoreClientParams> CoreDeviceProxy::get_client_parameters(FfiError& err) const {
uint16_t mtu = 0;
char* addr_c = nullptr;
char* mask_c = nullptr;
if (IdeviceFfiError* e =
::core_device_proxy_get_client_parameters(handle_.get(), &mtu, &addr_c, &mask_c)) {
err = FfiError(e);
return std::nullopt;
}
CoreClientParams params;
params.mtu = mtu;
if (addr_c) {
params.address = addr_c;
::idevice_string_free(addr_c);
}
if (mask_c) {
params.netmask = mask_c;
::idevice_string_free(mask_c);
}
return params;
}
std::optional<std::string> CoreDeviceProxy::get_server_address(FfiError& err) const {
char* addr_c = nullptr;
if (IdeviceFfiError* e = ::core_device_proxy_get_server_address(handle_.get(), &addr_c)) {
err = FfiError(e);
return std::nullopt;
}
std::string s;
if (addr_c) {
s = addr_c;
::idevice_string_free(addr_c);
}
return s;
}
std::optional<uint16_t> CoreDeviceProxy::get_server_rsd_port(FfiError& err) const {
uint16_t port = 0;
if (IdeviceFfiError* e = ::core_device_proxy_get_server_rsd_port(handle_.get(), &port)) {
err = FfiError(e);
return std::nullopt;
}
return port;
}
// ---- Adapter creation (consumes *this) ----
std::optional<Adapter> CoreDeviceProxy::create_tcp_adapter(FfiError& err) && {
AdapterHandle* out = nullptr;
// Rust consumes the proxy regardless of result → release BEFORE call
CoreDeviceProxyHandle* raw = this->release();
if (IdeviceFfiError* e = ::core_device_proxy_create_tcp_adapter(raw, &out)) {
err = FfiError(e);
return std::nullopt;
}
return Adapter::adopt(out);
}
} // namespace IdeviceFFI

View File

@@ -0,0 +1,32 @@
// Jackson Coxson
#include <idevice++/location_simulation.hpp>
namespace IdeviceFFI {
std::optional<LocationSimulation> LocationSimulation::create(RemoteServer& server, FfiError& err) {
LocationSimulationHandle* out = nullptr;
if (IdeviceFfiError* e = ::location_simulation_new(server.raw(), &out)) {
err = FfiError(e);
return std::nullopt;
}
return LocationSimulation::adopt(out);
}
bool LocationSimulation::clear(FfiError& err) {
if (IdeviceFfiError* e = ::location_simulation_clear(handle_.get())) {
err = FfiError(e);
return false;
}
return true;
}
bool LocationSimulation::set(double latitude, double longitude, FfiError& err) {
if (IdeviceFfiError* e = ::location_simulation_set(handle_.get(), latitude, longitude)) {
err = FfiError(e);
return false;
}
return true;
}
} // namespace IdeviceFFI

30
cpp/src/remote_server.cpp Normal file
View File

@@ -0,0 +1,30 @@
// Jackson Coxson
#include <idevice++/remote_server.hpp>
namespace IdeviceFFI {
std::optional<RemoteServer> RemoteServer::from_socket(ReadWrite&& rw, FfiError& err) {
RemoteServerHandle* out = nullptr;
// Rust consumes the stream regardless of result, release BEFORE the call
ReadWriteOpaque* raw = rw.release();
if (IdeviceFfiError* e = ::remote_server_new(raw, &out)) {
err = FfiError(e);
return std::nullopt;
}
return RemoteServer::adopt(out);
}
std::optional<RemoteServer>
RemoteServer::connect_rsd(Adapter& adapter, RsdHandshake& rsd, FfiError& err) {
RemoteServerHandle* out = nullptr;
if (IdeviceFfiError* e = ::remote_server_connect_rsd(adapter.raw(), rsd.raw(), &out)) {
err = FfiError(e);
return std::nullopt;
}
return RemoteServer::adopt(out);
}
} // namespace IdeviceFFI

129
cpp/src/rsd.cpp Normal file
View File

@@ -0,0 +1,129 @@
// Jackson Coxson
#include <idevice++/rsd.hpp>
namespace IdeviceFFI {
// ---------- helpers to copy/free CRsdService ----------
static RsdService to_cpp_and_free(CRsdService* c) {
RsdService s;
if (c->name)
s.name = c->name;
if (c->entitlement)
s.entitlement = c->entitlement;
s.port = c->port;
s.uses_remote_xpc = c->uses_remote_xpc;
s.service_version = c->service_version;
// features
if (c->features && c->features_count > 0) {
auto** arr = c->features;
s.features.reserve(c->features_count);
for (size_t i = 0; i < c->features_count; ++i) {
if (arr[i])
s.features.emplace_back(arr[i]);
}
}
// release the C allocation now that we've copied
rsd_free_service(c);
return s;
}
static std::vector<RsdService> to_cpp_and_free(CRsdServiceArray* arr) {
std::vector<RsdService> out;
if (!arr || !arr->services || arr->count == 0) {
if (arr)
rsd_free_services(arr);
return out;
}
out.reserve(arr->count);
auto* begin = arr->services;
for (size_t i = 0; i < arr->count; ++i) {
out.emplace_back(RsdService{begin[i].name ? begin[i].name : "",
begin[i].entitlement ? begin[i].entitlement : "",
begin[i].port,
begin[i].uses_remote_xpc,
{}, // features, fill below
begin[i].service_version});
// features for this service
if (begin[i].features && begin[i].features_count > 0) {
auto** feats = begin[i].features;
out.back().features.reserve(begin[i].features_count);
for (size_t j = 0; j < begin[i].features_count; ++j) {
if (feats[j])
out.back().features.emplace_back(feats[j]);
}
}
}
// free the array + nested C strings now that we've copied
rsd_free_services(arr);
return out;
}
// ---------- factory ----------
std::optional<RsdHandshake> RsdHandshake::from_socket(ReadWrite&& rw, FfiError& err) {
RsdHandshakeHandle* out = nullptr;
// Rust consumes the socket regardless of result ⇒ release BEFORE call.
ReadWriteOpaque* raw = rw.release();
if (IdeviceFfiError* e = rsd_handshake_new(raw, &out)) {
err = FfiError(e);
return std::nullopt;
}
return RsdHandshake::adopt(out);
}
// ---------- queries ----------
std::optional<size_t> RsdHandshake::protocol_version(FfiError& err) const {
size_t v = 0;
if (IdeviceFfiError* e = rsd_get_protocol_version(handle_.get(), &v)) {
err = FfiError(e);
return std::nullopt;
}
return v;
}
std::optional<std::string> RsdHandshake::uuid(FfiError& err) const {
char* c = nullptr;
if (IdeviceFfiError* e = rsd_get_uuid(handle_.get(), &c)) {
err = FfiError(e);
return std::nullopt;
}
std::string out;
if (c) {
out = c;
rsd_free_string(c);
}
return out;
}
std::optional<std::vector<RsdService>> RsdHandshake::services(FfiError& err) const {
CRsdServiceArray* arr = nullptr;
if (IdeviceFfiError* e = rsd_get_services(handle_.get(), &arr)) {
err = FfiError(e);
return std::nullopt;
}
return to_cpp_and_free(arr);
}
std::optional<bool> RsdHandshake::service_available(const std::string& name, FfiError& err) const {
bool avail = false;
if (IdeviceFfiError* e = rsd_service_available(handle_.get(), name.c_str(), &avail)) {
err = FfiError(e);
return std::nullopt;
}
return avail;
}
std::optional<RsdService> RsdHandshake::service_info(const std::string& name, FfiError& err) const {
CRsdService* svc = nullptr;
if (IdeviceFfiError* e = rsd_get_service_info(handle_.get(), name.c_str(), &svc)) {
err = FfiError(e);
return std::nullopt;
}
return to_cpp_and_free(svc);
}
} // namespace IdeviceFFI