-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathrepeater.cpp
More file actions
185 lines (155 loc) · 5.68 KB
/
repeater.cpp
File metadata and controls
185 lines (155 loc) · 5.68 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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
/* -*- mode: C++; c-basic-offset: 4; indent-tabs-mode: nil -*-
vi:ai:tabstop=8:shiftwidth=4:softtabstop=4:expandtab
*/
/*
* Author: Gabriel Burca <gburca dash binder at ebixio dot com>
*
* Sample code for using binders in Android from C++
*
* The Demo service provides 3 operations: push(), alert(), add(). See
* the IDemo class documentation to see what they do.
*
* Both the server and client code are included below.
*
* To view the log output:
* adb logcat -v time binder_demo:* *:S
*
* To run, create 2 adb shell sessions. In the first one run "binder" with no
* arguments to start the service. In the second one run "binder N" where N is
* an integer, to start a client that connects to the service and calls push(N),
* alert(), and add(N, 5).
*/
#define LOG_TAG "binder_repeater"
/* For relevant code see:
frameworks/native/{include,libs}/binder/{IInterface,Parcel}.{h,cpp}
system/core/include/utils/{Errors,RefBase}.h
*/
#include <stdlib.h>
#include <string>
#include <utils/RefBase.h>
#include <utils/Log.h>
#include <binder/TextOutput.h>
#include <binder/IInterface.h>
#include <binder/IBinder.h>
#include <binder/ProcessState.h>
#include <binder/IServiceManager.h>
#include <binder/IPCThreadState.h>
using namespace android;
#define INFO(...) \
do { \
printf(__VA_ARGS__); \
printf("\n"); \
ALOGD(__VA_ARGS__); \
} while(0)
void assert_fail(const char *file, int line, const char *func, const char *expr) {
INFO("assertion failed at file %s, line %d, function %s:",
file, line, func);
INFO("%s", expr);
abort();
}
#define ASSERT(e) \
do { \
if (!(e)) \
assert_fail(__FILE__, __LINE__, __func__, #e); \
} while(0)
// Where to print the parcel contents: aout, alog, aerr. alog doesn't seem to work.
#define PLOG aout
const std::string base64_chars =
"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
"abcdefghijklmnopqrstuvwxyz"
"0123456789+/";
inline bool is_base64(unsigned char c) {
return (isalnum(c) || (c == '+') || (c == '/'));
}
std::string base64_decode(std::string const& encoded_string) {
int in_len = encoded_string.size();
int i = 0;
int j = 0;
int in_ = 0;
unsigned char char_array_4[4], char_array_3[3];
std::string ret;
while (in_len-- && ( encoded_string[in_] != '=') && is_base64(encoded_string[in_])) {
char_array_4[i++] = encoded_string[in_]; in_++;
if (i ==4) {
for (i = 0; i <4; i++)
char_array_4[i] = base64_chars.find(char_array_4[i]);
char_array_3[0] = ( char_array_4[0] << 2 ) + ((char_array_4[1] & 0x30) >> 4);
char_array_3[1] = ((char_array_4[1] & 0xf) << 4) + ((char_array_4[2] & 0x3c) >> 2);
char_array_3[2] = ((char_array_4[2] & 0x3) << 6) + char_array_4[3];
for (i = 0; (i < 3); i++)
ret += char_array_3[i];
i = 0;
}
}
if (i) {
for (j = 0; j < i; j++)
char_array_4[j] = base64_chars.find(char_array_4[j]);
char_array_3[0] = (char_array_4[0] << 2) + ((char_array_4[1] & 0x30) >> 4);
char_array_3[1] = ((char_array_4[1] & 0xf) << 4) + ((char_array_4[2] & 0x3c) >> 2);
for (j = 0; (j < i - 1); j++) ret += char_array_3[j];
}
return ret;
}
//Please keep sync with official implementation
struct Parcel_model {
status_t mError;
uint8_t* mData; //useful
size_t mDataSize; //useful
size_t mDataCapacity;
mutable size_t mDataPos;
binder_size_t* mObjects; //useful
size_t mObjectsSize; //useful
size_t mObjectsCapacity;
mutable size_t mNextObjectHint;
mutable bool mObjectsSorted;
mutable bool mFdsKnown;
mutable bool mHasFds;
bool mAllowFds;
void* mOwner;
void* mOwnerCookie;
Parcel_model(char *data, size_t Dsize, uint64_t mobjects, size_t Osize):
mError(0), mData(0), mDataSize(Dsize),
mDataCapacity(0), mDataPos(0), mObjects(0), mObjectsSize(Osize),
mObjectsCapacity(0), mNextObjectHint(0), mObjectsSorted(0), mFdsKnown(0),
mHasFds(0), mAllowFds(0), mOwner(0), mOwnerCookie(0) {
std::string res = base64_decode(std::string(data));
mData = (uint8_t*)malloc(Dsize*sizeof(uint8_t));
memcpy(mData, res.c_str(), Dsize);
mObjects = (binder_size_t*)malloc(sizeof(binder_size_t));
*mObjects = mobjects;
}
};
void log_reply(Parcel *reply){
Parcel_model *r = reinterpret_cast<Parcel_model*>(reply);
size_t i = 0;
for(i = 0; i < r->mDataSize; ++i){
printf("%02x ", r->mData[i]);
if((i+1) % 16 == 0)
printf("\n");
}
if(i % 16 != 0)
printf("\n");
}
int main(int argc, char **argv) {
if (argc == 8) {
sp<IServiceManager> sm = defaultServiceManager();
ASSERT(sm != 0);
sp<IBinder> binder = sm->getService(String16(argv[1]));
// TODO: If the "Demo" service is not running, getService times out and binder == 0.
ASSERT(binder != 0);
Parcel reply;
Parcel_model data(argv[4], atoll(argv[5]), atoll(argv[6]), atoll(argv[6]));
status_t err = binder->transact(atoll(argv[2]), *reinterpret_cast<Parcel*>(&data), &reply, atoll(argv[3]));
if (!err) {
//NO_ERROR = 0
//Details in http://androidxref.com/9.0.0_r3/xref/system/core/libutils/include/utils/Errors.h#27
puts("Sent successfuly.");
} else {
printf("err set to: %d\n", err);
}
log_reply(&reply);
} else {
INFO("%s service_name cmd flag base64(data) mDataSize mObjects mObjectsSize", argv[0]);
}
return 0;
}