blob: cadb1510974d131e2b6175e755163e02a6ea91da [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>
7#include <inttypes.h>
8#include <utils/Timers.h>
9
10namespace android {
11namespace hardware {
12namespace tests {
13namespace foo {
14namespace V1_0 {
15namespace implementation {
16
17// Methods from ::android::hardware::tests::foo::V1_0::IFoo follow.
18Return<void> Foo::doThis(float param) {
19 ALOGI("SERVER(Foo) doThis(%.2f)", param);
20
21 return Void();
22}
23
Yifan Hongd5b5b2e2016-10-06 13:50:49 -070024Return<int32_t> Foo::doThatAndReturnSomething(
25 int64_t param) {
26 LOG(INFO) << "SERVER(Foo) doThatAndReturnSomething(" << param << ")";
27
28 return 666;
29}
30
31Return<double> Foo::doQuiteABit(
32 int32_t a,
33 int64_t b,
34 float c,
35 double d) {
36 LOG(INFO) << "SERVER(Foo) doQuiteABit("
37 << a
38 << ", "
39 << b
40 << ", "
41 << c
42 << ", "
43 << d
44 << ")";
45
46 return 666.5;
47}
48
49Return<void> Foo::doSomethingElse(
50 const hidl_array<int32_t, 15> &param, doSomethingElse_cb _cb) {
51 ALOGI("SERVER(Foo) doSomethingElse(...)");
52
53 hidl_array<int32_t, 32> result;
54 for (size_t i = 0; i < 15; ++i) {
55 result[i] = 2 * param[i];
56 result[15 + i] = param[i];
57 }
58 result[30] = 1;
59 result[31] = 2;
60
61 _cb(result);
62
63 return Void();
64}
65
66Return<void> Foo::doStuffAndReturnAString(
67 doStuffAndReturnAString_cb _cb) {
68 ALOGI("SERVER(Foo) doStuffAndReturnAString");
69
70 hidl_string s;
71 s = "Hello, world";
72
73 _cb(s);
74
75 return Void();
76}
77
78Return<void> Foo::mapThisVector(
79 const hidl_vec<int32_t> &param, mapThisVector_cb _cb) {
80 ALOGI("SERVER(Foo) mapThisVector");
81
82 hidl_vec<int32_t> out;
83 out.resize(param.size());
84
85 for (size_t i = 0; i < out.size(); ++i) {
86 out[i] = param[i] * 2;
87 }
88
89 _cb(out);
90
91 return Void();
92}
93
94Return<void> Foo::callMe(
95 const sp<IFooCallback> &cb) {
96 ALOGI("SERVER(Foo) callMe %p", cb.get());
97
98 if (cb != NULL) {
99
100 hidl_array<nsecs_t, 3> c;
101 ALOGI("SERVER(Foo) callMe %p calling IFooCallback::heyItsYou, " \
102 "should return immediately", cb.get());
103 c[0] = systemTime();
104 cb->heyItsYou(cb);
105 c[0] = systemTime() - c[0];
106 ALOGI("SERVER(Foo) callMe %p calling IFooCallback::heyItsYou " \
107 "returned after %" PRId64 "ns", cb.get(), c[0]);
108
109 ALOGI("SERVER(Foo) callMe %p calling IFooCallback::heyItsYouIsntIt, " \
110 "should block for %" PRId64 " seconds", cb.get(),
111 FooCallback::DELAY_S);
112 c[1] = systemTime();
113 bool answer = cb->heyItsYouIsntIt(cb);
114 c[1] = systemTime() - c[1];
115 ALOGI("SERVER(Foo) callMe %p IFooCallback::heyItsYouIsntIt " \
116 "responded with %d after %" PRId64 "ns", cb.get(), answer, c[1]);
117
118 ALOGI("SERVER(Foo) callMe %p calling " \
119 "IFooCallback::heyItsTheMeaningOfLife, " \
120 "should return immediately ", cb.get());
121 c[2] = systemTime();
122 cb->heyItsTheMeaningOfLife(42);
123 c[2] = systemTime() - c[2];
124 ALOGI("SERVER(Foo) callMe %p After call to " \
125 "IFooCallback::heyItsTheMeaningOfLife " \
126 "responded after %" PRId64 "ns", cb.get(), c[2]);
127
128 ALOGI("SERVER(Foo) callMe %p calling IFooCallback::youBlockedMeFor " \
129 "to report times", cb.get());
130 cb->youBlockedMeFor(c);
131 ALOGI("SERVER(Foo) callMe %p After call to " \
132 "IFooCallback::heyYouBlockedMeFor", cb.get());
133 }
134
135 return Void();
136}
137
138Return<Foo::SomeEnum> Foo::useAnEnum(SomeEnum param) {
139 ALOGI("SERVER(Foo) useAnEnum %d", (int)param);
140
141 return SomeEnum::goober;
142}
143
144Return<void> Foo::haveAGooberVec(const hidl_vec<Goober>& param) {
145 ALOGI("SERVER(Foo) haveAGooberVec &param = %p", &param);
146
147 return Void();
148}
149
150Return<void> Foo::haveAGoober(const Goober &g) {
151 ALOGI("SERVER(Foo) haveaGoober g=%p", &g);
152
153 return Void();
154}
155
156Return<void> Foo::haveAGooberArray(const hidl_array<Goober, 20> & /* lots */) {
157 ALOGI("SERVER(Foo) haveAGooberArray");
158
159 return Void();
160}
161
162Return<void> Foo::haveATypeFromAnotherFile(const Abc &def) {
163 ALOGI("SERVER(Foo) haveATypeFromAnotherFile def=%p", &def);
164
165 return Void();
166}
167
168Return<void> Foo::haveSomeStrings(
169 const hidl_array<hidl_string, 3> &array,
170 haveSomeStrings_cb _cb) {
171 ALOGI("SERVER(Foo) haveSomeStrings([\"%s\", \"%s\", \"%s\"])",
172 array[0].c_str(),
173 array[1].c_str(),
174 array[2].c_str());
175
176 hidl_array<hidl_string, 2> result;
177 result[0] = "Hello";
178 result[1] = "World";
179
180 _cb(result);
181
182 return Void();
183}
184
185Return<void> Foo::haveAStringVec(
186 const hidl_vec<hidl_string> &vector,
187 haveAStringVec_cb _cb) {
188 ALOGI("SERVER(Foo) haveAStringVec([\"%s\", \"%s\", \"%s\"])",
189 vector[0].c_str(),
190 vector[1].c_str(),
191 vector[2].c_str());
192
193 hidl_vec<hidl_string> result;
194 result.resize(2);
195
196 result[0] = "Hello";
197 result[1] = "World";
198
199 _cb(result);
200
201 return Void();
202}
203
204// NOTE: duplicated code in hidl_test
205using std::to_string;
206
207static std::string to_string(const IFoo::StringMatrix5x3 &M);
208static std::string to_string(const IFoo::StringMatrix3x5 &M);
209static std::string to_string(const hidl_string &s);
210
211template<typename T>
212static std::string to_string(const T *elems, size_t n) {
213 std::string out;
214 out = "[";
215 for (size_t i = 0; i < n; ++i) {
216 if (i > 0) {
217 out += ", ";
218 }
219 out += to_string(elems[i]);
220 }
221 out += "]";
222
223 return out;
224}
225
226template<typename T, size_t SIZE>
227static std::string to_string(const hidl_array<T, SIZE> &array) {
228 return to_string(&array[0], SIZE);
229}
230
231template<typename T, size_t SIZE1, size_t SIZE2>
232static std::string to_string(const hidl_array<T, SIZE1, SIZE2> &array) {
233 std::string out;
234 out = "[";
235 for (size_t i = 0; i < SIZE1; ++i) {
236 if (i > 0) {
237 out += ", ";
238 }
239
240 out += "[";
241 for (size_t j = 0; j < SIZE2; ++j) {
242 if (j > 0) {
243 out += ", ";
244 }
245
246 out += to_string(array[i][j]);
247 }
248 out += "]";
249 }
250 out += "]";
251
252 return out;
253}
254
255template<typename T>
256static std::string to_string(const hidl_vec<T> &vec) {
257 return to_string(&vec[0], vec.size());
258}
259
260static std::string to_string(const IFoo::StringMatrix5x3 &M) {
261 return to_string(M.s);
262}
263
264static std::string to_string(const IFoo::StringMatrix3x5 &M) {
265 return to_string(M.s);
266}
267
268static std::string to_string(const hidl_string &s) {
269 return std::string("'") + s.c_str() + "'";
270}
271
272Return<void> Foo::transposeMe(
273 const hidl_array<float, 3, 5> &in, transposeMe_cb _cb) {
274 ALOGI("SERVER(Foo) transposeMe(%s)", to_string(in).c_str());
275
276 hidl_array<float, 5, 3> out;
277 for (size_t i = 0; i < 5; ++i) {
278 for (size_t j = 0; j < 3; ++j) {
279 out[i][j] = in[j][i];
280 }
281 }
282
283 ALOGI("SERVER(Foo) transposeMe returning %s", to_string(out).c_str());
284
285 _cb(out);
286
287 return Void();
288}
289// end duplicated code
290
291static std::string QuuxToString(const IFoo::Quux &val) {
292 std::string s;
293
294 s = "Quux(first='";
295 s += val.first.c_str();
296 s += "', last='";
297 s += val.last.c_str();
298 s += "')";
299
300 return s;
301}
302
303static std::string MultiDimensionalToString(const IFoo::MultiDimensional &val) {
304 std::string s;
305
306 s += "MultiDimensional(";
307
308 s += "quuxMatrix=[";
309
310 size_t k = 0;
311 for (size_t i = 0; i < 5; ++i) {
312 if (i > 0) {
313 s += ", ";
314 }
315
316 s += "[";
317 for (size_t j = 0; j < 3; ++j, ++k) {
318 if (j > 0) {
319 s += ", ";
320 }
321
322 s += QuuxToString(val.quuxMatrix[i][j]);
323 }
324 }
325 s += "]";
326
327 s += ")";
328
329 return s;
330}
331
332Return<void> Foo::callingDrWho(
333 const MultiDimensional &in, callingDrWho_cb _hidl_cb) {
334 ALOGI("SERVER(Foo) callingDrWho(%s)", MultiDimensionalToString(in).c_str());
335
336 MultiDimensional out;
337 for (size_t i = 0; i < 5; ++i) {
338 for (size_t j = 0; j < 3; ++j) {
339 out.quuxMatrix[i][j].first = in.quuxMatrix[4 - i][2 - j].last;
340 out.quuxMatrix[i][j].last = in.quuxMatrix[4 - i][2 - j].first;
341 }
342 }
343
344 _hidl_cb(out);
345
346 return Void();
347}
348
349Return<void> Foo::transpose(const StringMatrix5x3 &in, transpose_cb _hidl_cb) {
350 LOG(INFO) << "SERVER(Foo) transpose " << to_string(in);
351
352 StringMatrix3x5 out;
353 for (size_t i = 0; i < 3; ++i) {
354 for (size_t j = 0; j < 5; ++j) {
355 out.s[i][j] = in.s[j][i];
356 }
357 }
358
359 _hidl_cb(out);
360
361 return Void();
362}
363
364Return<void> Foo::transpose2(
365 const hidl_array<hidl_string, 5, 3> &in, transpose2_cb _hidl_cb) {
366 LOG(INFO) << "SERVER(Foo) transpose2 " << to_string(in);
367
368 hidl_array<hidl_string, 3, 5> out;
369 for (size_t i = 0; i < 3; ++i) {
370 for (size_t j = 0; j < 5; ++j) {
371 out[i][j] = in[j][i];
372 }
373 }
374
375 _hidl_cb(out);
376
377 return Void();
378}
379
380Return<void> Foo::sendVec(
381 const hidl_vec<uint8_t> &data, sendVec_cb _hidl_cb) {
382 _hidl_cb(data);
383
384 return Void();
385}
386
387Return<void> Foo::sendVecVec(sendVecVec_cb _hidl_cb) {
388 hidl_vec<hidl_vec<uint8_t>> data;
389 _hidl_cb(data);
390
391 return Void();
392}
393
Andreas Huber847b1452016-10-19 14:10:55 -0700394Return<void> Foo::haveAVectorOfInterfaces(
395 const hidl_vec<sp<ISimple> > &in,
396 haveAVectorOfInterfaces_cb _hidl_cb) {
397 _hidl_cb(in);
398
399 return Void();
400}
401
402Return<void> Foo::haveAVectorOfGenericInterfaces(
403 const hidl_vec<sp<android::hardware::IBinder> > &in,
404 haveAVectorOfGenericInterfaces_cb _hidl_cb) {
405 _hidl_cb(in);
406
407 return Void();
408}
Yifan Hongd5b5b2e2016-10-06 13:50:49 -0700409
410IFoo* HIDL_FETCH_IFoo(const char* /* name */) {
411 return new Foo();
412}
413
414} // namespace implementation
415} // namespace V1_0
416} // namespace foo
417} // namespace tests
418} // namespace hardware
419} // namespace android