blob: 7053b44251ef0fb6c2863f841789443fcc3d3c0e [file] [log] [blame]
Yifan Hongd5b5b2e2016-10-06 13:50:49 -07001
2#define LOG_TAG "hidl_test"
3
4#include "Foo.h"
5#include "FooCallback.h"
6#include <android-base/logging.h>
Yifan Hong30dc3de2016-10-17 11:38:15 -07007#include <hidl-test/FooHelper.h>
Yifan Hongd5b5b2e2016-10-06 13:50:49 -07008#include <inttypes.h>
9#include <utils/Timers.h>
10
11namespace android {
12namespace hardware {
13namespace tests {
14namespace foo {
15namespace V1_0 {
16namespace implementation {
17
18// Methods from ::android::hardware::tests::foo::V1_0::IFoo follow.
19Return<void> Foo::doThis(float param) {
20 ALOGI("SERVER(Foo) doThis(%.2f)", param);
21
22 return Void();
23}
24
Yifan Hongd5b5b2e2016-10-06 13:50:49 -070025Return<int32_t> Foo::doThatAndReturnSomething(
26 int64_t param) {
27 LOG(INFO) << "SERVER(Foo) doThatAndReturnSomething(" << param << ")";
28
29 return 666;
30}
31
32Return<double> Foo::doQuiteABit(
33 int32_t a,
34 int64_t b,
35 float c,
36 double d) {
37 LOG(INFO) << "SERVER(Foo) doQuiteABit("
38 << a
39 << ", "
40 << b
41 << ", "
42 << c
43 << ", "
44 << d
45 << ")";
46
47 return 666.5;
48}
49
50Return<void> Foo::doSomethingElse(
51 const hidl_array<int32_t, 15> &param, doSomethingElse_cb _cb) {
52 ALOGI("SERVER(Foo) doSomethingElse(...)");
53
54 hidl_array<int32_t, 32> result;
55 for (size_t i = 0; i < 15; ++i) {
56 result[i] = 2 * param[i];
57 result[15 + i] = param[i];
58 }
59 result[30] = 1;
60 result[31] = 2;
61
62 _cb(result);
63
64 return Void();
65}
66
67Return<void> Foo::doStuffAndReturnAString(
68 doStuffAndReturnAString_cb _cb) {
69 ALOGI("SERVER(Foo) doStuffAndReturnAString");
70
Steven Moreland54813ed2016-10-24 10:46:43 -070071 _cb("Hello, world");
Yifan Hongd5b5b2e2016-10-06 13:50:49 -070072
73 return Void();
74}
75
76Return<void> Foo::mapThisVector(
77 const hidl_vec<int32_t> &param, mapThisVector_cb _cb) {
78 ALOGI("SERVER(Foo) mapThisVector");
79
80 hidl_vec<int32_t> out;
81 out.resize(param.size());
82
83 for (size_t i = 0; i < out.size(); ++i) {
84 out[i] = param[i] * 2;
85 }
86
87 _cb(out);
88
89 return Void();
90}
91
92Return<void> Foo::callMe(
93 const sp<IFooCallback> &cb) {
94 ALOGI("SERVER(Foo) callMe %p", cb.get());
95
96 if (cb != NULL) {
97
98 hidl_array<nsecs_t, 3> c;
99 ALOGI("SERVER(Foo) callMe %p calling IFooCallback::heyItsYou, " \
100 "should return immediately", cb.get());
101 c[0] = systemTime();
102 cb->heyItsYou(cb);
103 c[0] = systemTime() - c[0];
104 ALOGI("SERVER(Foo) callMe %p calling IFooCallback::heyItsYou " \
105 "returned after %" PRId64 "ns", cb.get(), c[0]);
106
107 ALOGI("SERVER(Foo) callMe %p calling IFooCallback::heyItsYouIsntIt, " \
108 "should block for %" PRId64 " seconds", cb.get(),
Yifan Hong30dc3de2016-10-17 11:38:15 -0700109 DELAY_S);
Yifan Hongd5b5b2e2016-10-06 13:50:49 -0700110 c[1] = systemTime();
Steven Morelanddc5f84f2016-11-22 14:16:37 -0800111 Return<bool> ret = cb->heyItsYouIsntIt(cb);
112 if (!ret.isOk()) {
113 ALOGE("SERVER(Foo) callMe %p encountered transport error (%d).",
114 cb.get(), ret.getStatus().exceptionCode());
115 return Void();
116 }
117 bool answer = ret.get();
Yifan Hongd5b5b2e2016-10-06 13:50:49 -0700118 c[1] = systemTime() - c[1];
119 ALOGI("SERVER(Foo) callMe %p IFooCallback::heyItsYouIsntIt " \
120 "responded with %d after %" PRId64 "ns", cb.get(), answer, c[1]);
121
122 ALOGI("SERVER(Foo) callMe %p calling " \
123 "IFooCallback::heyItsTheMeaningOfLife, " \
124 "should return immediately ", cb.get());
125 c[2] = systemTime();
126 cb->heyItsTheMeaningOfLife(42);
127 c[2] = systemTime() - c[2];
128 ALOGI("SERVER(Foo) callMe %p After call to " \
129 "IFooCallback::heyItsTheMeaningOfLife " \
130 "responded after %" PRId64 "ns", cb.get(), c[2]);
131
132 ALOGI("SERVER(Foo) callMe %p calling IFooCallback::youBlockedMeFor " \
133 "to report times", cb.get());
134 cb->youBlockedMeFor(c);
135 ALOGI("SERVER(Foo) callMe %p After call to " \
136 "IFooCallback::heyYouBlockedMeFor", cb.get());
137 }
138
139 return Void();
140}
141
142Return<Foo::SomeEnum> Foo::useAnEnum(SomeEnum param) {
143 ALOGI("SERVER(Foo) useAnEnum %d", (int)param);
144
145 return SomeEnum::goober;
146}
147
148Return<void> Foo::haveAGooberVec(const hidl_vec<Goober>& param) {
149 ALOGI("SERVER(Foo) haveAGooberVec &param = %p", &param);
150
151 return Void();
152}
153
154Return<void> Foo::haveAGoober(const Goober &g) {
155 ALOGI("SERVER(Foo) haveaGoober g=%p", &g);
156
157 return Void();
158}
159
160Return<void> Foo::haveAGooberArray(const hidl_array<Goober, 20> & /* lots */) {
161 ALOGI("SERVER(Foo) haveAGooberArray");
162
163 return Void();
164}
165
166Return<void> Foo::haveATypeFromAnotherFile(const Abc &def) {
167 ALOGI("SERVER(Foo) haveATypeFromAnotherFile def=%p", &def);
168
169 return Void();
170}
171
172Return<void> Foo::haveSomeStrings(
173 const hidl_array<hidl_string, 3> &array,
174 haveSomeStrings_cb _cb) {
175 ALOGI("SERVER(Foo) haveSomeStrings([\"%s\", \"%s\", \"%s\"])",
176 array[0].c_str(),
177 array[1].c_str(),
178 array[2].c_str());
179
180 hidl_array<hidl_string, 2> result;
181 result[0] = "Hello";
182 result[1] = "World";
183
184 _cb(result);
185
186 return Void();
187}
188
189Return<void> Foo::haveAStringVec(
190 const hidl_vec<hidl_string> &vector,
191 haveAStringVec_cb _cb) {
192 ALOGI("SERVER(Foo) haveAStringVec([\"%s\", \"%s\", \"%s\"])",
193 vector[0].c_str(),
194 vector[1].c_str(),
195 vector[2].c_str());
196
197 hidl_vec<hidl_string> result;
198 result.resize(2);
199
200 result[0] = "Hello";
201 result[1] = "World";
202
203 _cb(result);
204
205 return Void();
206}
207
Yifan Hongd5b5b2e2016-10-06 13:50:49 -0700208Return<void> Foo::transposeMe(
209 const hidl_array<float, 3, 5> &in, transposeMe_cb _cb) {
210 ALOGI("SERVER(Foo) transposeMe(%s)", to_string(in).c_str());
211
212 hidl_array<float, 5, 3> out;
213 for (size_t i = 0; i < 5; ++i) {
214 for (size_t j = 0; j < 3; ++j) {
215 out[i][j] = in[j][i];
216 }
217 }
218
219 ALOGI("SERVER(Foo) transposeMe returning %s", to_string(out).c_str());
220
221 _cb(out);
222
223 return Void();
224}
Yifan Hongd5b5b2e2016-10-06 13:50:49 -0700225
226Return<void> Foo::callingDrWho(
227 const MultiDimensional &in, callingDrWho_cb _hidl_cb) {
228 ALOGI("SERVER(Foo) callingDrWho(%s)", MultiDimensionalToString(in).c_str());
229
230 MultiDimensional out;
231 for (size_t i = 0; i < 5; ++i) {
232 for (size_t j = 0; j < 3; ++j) {
233 out.quuxMatrix[i][j].first = in.quuxMatrix[4 - i][2 - j].last;
234 out.quuxMatrix[i][j].last = in.quuxMatrix[4 - i][2 - j].first;
235 }
236 }
237
238 _hidl_cb(out);
239
240 return Void();
241}
242
243Return<void> Foo::transpose(const StringMatrix5x3 &in, transpose_cb _hidl_cb) {
244 LOG(INFO) << "SERVER(Foo) transpose " << to_string(in);
245
246 StringMatrix3x5 out;
247 for (size_t i = 0; i < 3; ++i) {
248 for (size_t j = 0; j < 5; ++j) {
249 out.s[i][j] = in.s[j][i];
250 }
251 }
252
253 _hidl_cb(out);
254
255 return Void();
256}
257
258Return<void> Foo::transpose2(
259 const hidl_array<hidl_string, 5, 3> &in, transpose2_cb _hidl_cb) {
260 LOG(INFO) << "SERVER(Foo) transpose2 " << to_string(in);
261
262 hidl_array<hidl_string, 3, 5> out;
263 for (size_t i = 0; i < 3; ++i) {
264 for (size_t j = 0; j < 5; ++j) {
265 out[i][j] = in[j][i];
266 }
267 }
268
269 _hidl_cb(out);
270
271 return Void();
272}
273
274Return<void> Foo::sendVec(
275 const hidl_vec<uint8_t> &data, sendVec_cb _hidl_cb) {
276 _hidl_cb(data);
277
278 return Void();
279}
280
281Return<void> Foo::sendVecVec(sendVecVec_cb _hidl_cb) {
282 hidl_vec<hidl_vec<uint8_t>> data;
283 _hidl_cb(data);
284
285 return Void();
286}
287
Andreas Huber847b1452016-10-19 14:10:55 -0700288Return<void> Foo::haveAVectorOfInterfaces(
289 const hidl_vec<sp<ISimple> > &in,
290 haveAVectorOfInterfaces_cb _hidl_cb) {
291 _hidl_cb(in);
292
293 return Void();
294}
295
296Return<void> Foo::haveAVectorOfGenericInterfaces(
297 const hidl_vec<sp<android::hardware::IBinder> > &in,
298 haveAVectorOfGenericInterfaces_cb _hidl_cb) {
299 _hidl_cb(in);
Yifan Hongebfa6332016-10-14 10:41:41 -0700300 return Void();
301}
Andreas Huber847b1452016-10-19 14:10:55 -0700302
Yifan Hongebfa6332016-10-14 10:41:41 -0700303Return<void> Foo::createMyHandle(createMyHandle_cb _hidl_cb) {
304 native_handle_t* nh = native_handle_create(0, 10);
305 int data[] = {2,3,5,7,11,13,17,19,21,23};
306 CHECK(sizeof(data) == 10 * sizeof(int));
307 memcpy(nh->data, data, sizeof(data));
308 mHandles.push_back(nh);
309
310 MyHandle h;
311 h.guard = 666;
312 h.h = nh;
313 _hidl_cb(h);
314 return Void();
315}
316
317Return<void> Foo::createHandles(uint32_t size, createHandles_cb _hidl_cb) {
Martijn Coenen403161a2016-11-18 15:29:32 +0100318 hidl_vec<hidl_handle> handles;
Yifan Hongebfa6332016-10-14 10:41:41 -0700319 handles.resize(size);
320 for(uint32_t i = 0; i < size; ++i) {
321 createMyHandle([&](const MyHandle& h) {
322 handles[i] = h.h;
323 });
324 }
325 _hidl_cb(handles);
326 return Void();
327}
328
329Return<void> Foo::closeHandles() {
330 for(native_handle_t* h : mHandles) {
331 native_handle_delete(h);
332 }
333 mHandles.clear();
Andreas Huber847b1452016-10-19 14:10:55 -0700334 return Void();
335}
Yifan Hongd5b5b2e2016-10-06 13:50:49 -0700336
Martijn Coenenb7307d52016-10-27 11:51:46 +0200337Return<void> Foo::echoNullInterface(const sp<IFooCallback> &cb, echoNullInterface_cb _hidl_cb) {
338 _hidl_cb(cb == nullptr, cb);
339
340 return Void();
341}
342
Yifan Hongd5b5b2e2016-10-06 13:50:49 -0700343IFoo* HIDL_FETCH_IFoo(const char* /* name */) {
344 return new Foo();
345}
346
347} // namespace implementation
348} // namespace V1_0
349} // namespace foo
350} // namespace tests
351} // namespace hardware
352} // namespace android