forked from jkcoxson/idevice
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdebug_proxy.cpp
More file actions
143 lines (124 loc) · 3.59 KB
/
Copy pathdebug_proxy.cpp
File metadata and controls
143 lines (124 loc) · 3.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
// Jackson Coxson
#include <cstring>
#include <idevice++/debug_proxy.hpp>
namespace IdeviceFFI {
// ---- helpers ----
static Option<std::string> take_cstring(char *p) {
if (!p)
return None;
std::string s(p);
::idevice_string_free(p);
return Some(s);
}
// ---- DebugCommand ----
Option<DebugCommand> DebugCommand::make(const std::string &name,
const std::vector<std::string> &argv) {
std::vector<const char *> c_argv;
c_argv.reserve(argv.size());
for (auto &a : argv)
c_argv.push_back(a.c_str());
auto *h = ::debugserver_command_new(
name.c_str(),
c_argv.empty() ? nullptr : const_cast<const char *const *>(c_argv.data()),
c_argv.size());
if (!h)
return None;
return Some(DebugCommand(h));
}
// ---- DebugProxy factories ----
Result<DebugProxy, FfiError> DebugProxy::connect_rsd(Adapter &adapter,
RsdHandshake &rsd) {
::DebugProxyHandle *out = nullptr;
FfiError e(::debug_proxy_connect_rsd(adapter.raw(), rsd.raw(), &out));
if (e) {
return Err(e);
}
return Ok(DebugProxy(out));
}
Result<DebugProxy, FfiError>
DebugProxy::from_readwrite_ptr(::ReadWriteOpaque *consumed) {
::DebugProxyHandle *out = nullptr;
FfiError e(::debug_proxy_new(consumed, &out));
if (e) {
return Err(e);
}
return Ok(DebugProxy(out));
}
Result<DebugProxy, FfiError> DebugProxy::from_readwrite(ReadWrite &&rw) {
// Rust consumes the pointer regardless of outcome; release before calling
return from_readwrite_ptr(rw.release());
}
// ---- DebugProxy API ----
Result<Option<std::string>, FfiError>
DebugProxy::send_command(const std::string &name,
const std::vector<std::string> &argv) {
auto cmdRes = DebugCommand::make(name, argv);
if (cmdRes.is_none()) {
// treat as invalid arg
FfiError err;
err.code = 33; // FfiInvalidArg
err.message = "debugserver_command_new failed";
return Err(err);
}
auto cmd = std::move(cmdRes).unwrap();
char *resp_c = nullptr;
FfiError e(::debug_proxy_send_command(handle_, cmd.raw(), &resp_c));
if (e) {
return Err(e);
}
return Ok(take_cstring(resp_c));
}
Result<Option<std::string>, FfiError> DebugProxy::read_response() {
char *resp_c = nullptr;
FfiError e(::debug_proxy_read_response(handle_, &resp_c));
if (e) {
return Err(e);
}
return Ok(take_cstring(resp_c));
}
Result<void, FfiError> DebugProxy::send_raw(const std::vector<uint8_t> &data) {
FfiError e(::debug_proxy_send_raw(handle_, data.data(), data.size()));
if (e) {
return Err(e);
}
return Ok();
}
Result<Option<std::string>, FfiError> DebugProxy::read(std::size_t len) {
char *resp_c = nullptr;
FfiError e(::debug_proxy_read(handle_, len, &resp_c));
if (e) {
return Err(e);
}
return Ok(take_cstring(resp_c));
}
Result<Option<std::string>, FfiError>
DebugProxy::set_argv(const std::vector<std::string> &argv) {
std::vector<const char *> c_argv;
c_argv.reserve(argv.size());
for (auto &a : argv)
c_argv.push_back(a.c_str());
char *resp_c = nullptr;
FfiError e(::debug_proxy_set_argv(
handle_,
c_argv.empty() ? nullptr : const_cast<const char *const *>(c_argv.data()),
c_argv.size(), &resp_c));
if (e) {
return Err(e);
}
return Ok(take_cstring(resp_c));
}
Result<void, FfiError> DebugProxy::send_ack() {
FfiError e(::debug_proxy_send_ack(handle_));
if (e) {
return Err(e);
}
return Ok();
}
Result<void, FfiError> DebugProxy::send_nack() {
FfiError e(::debug_proxy_send_nack(handle_));
if (e) {
return Err(e);
}
return Ok();
}
} // namespace IdeviceFFI