blob: 6252fbed8a55ba6dffd4c5cb3ce0a2f6d3b9a94d [file] [log] [blame]
Hridya Valsaraju6cf95f32017-02-22 10:51:01 -08001#include "Baz.h"
2#include <android-base/logging.h>
3
4namespace android {
5namespace hardware {
6namespace tests {
7namespace baz {
8namespace V1_0 {
9namespace implementation {
10
11struct BazCallback : public IBazCallback {
12 Return<void> heyItsMe(const sp<IBazCallback> &cb) override;
13 Return<void> hey() override;
14};
15
16Return<void> BazCallback::heyItsMe(
17 const sp<IBazCallback> &cb) {
18 LOG(INFO) << "SERVER: heyItsMe cb = " << cb.get();
19
20 return Void();
21}
22
23Return<void> BazCallback::hey() {
24 LOG(INFO) << "SERVER: hey";
25
26 return Void();
27}
28
29// TODO(b/35703683) : replace usage of below methods with toString()
30
31static std::string to_string(const IBaz::Foo::Bar &bar);
32static std::string to_string(const IBaz::Foo &foo);
33static std::string to_string(const hidl_string &s);
34static std::string to_string(bool x);
35static std::string to_string(const IBaz::StringMatrix5x3 &M);
Hridya Valsaraju6cf95f32017-02-22 10:51:01 -080036
37template<typename T, size_t SIZE>
38static std::string to_string(const hidl_array<T, SIZE> &array);
39
40template<size_t SIZE>
41static std::string to_string(const hidl_array<uint8_t, SIZE> &array);
42
43template<typename T>
44static std::string to_string(const hidl_vec<T> &vec) {
45 std::string out;
46 out = "[";
47 for (size_t i = 0; i < vec.size(); ++i) {
48 if (i > 0) {
49 out += ", ";
50 }
51 out += to_string(vec[i]);
52 }
53 out += "]";
54
55 return out;
56}
57
58template<typename T, size_t SIZE>
59static std::string to_string(const hidl_array<T, SIZE> &array) {
60 std::string out;
61 out = "[";
62 for (size_t i = 0; i < SIZE; ++i) {
63 if (i > 0) {
64 out += ", ";
65 }
66 out += to_string(array[i]);
67 }
68 out += "]";
69
70 return out;
71}
72
73template<size_t SIZE>
74static std::string to_string(const hidl_array<uint8_t, SIZE> &array) {
75 std::string out;
76 for (size_t i = 0; i < SIZE; ++i) {
77 if (i > 0) {
78 out += ":";
79 }
80
81 char tmp[3];
82 sprintf(tmp, "%02x", array[i]);
83
84 out += tmp;
85 }
86
87 return out;
88}
89
90template<typename T, size_t SIZE1, size_t SIZE2>
91static std::string to_string(const hidl_array<T, SIZE1, SIZE2> &array) {
92 std::string out;
93 out = "[";
94 for (size_t i = 0; i < SIZE1; ++i) {
95 if (i > 0) {
96 out += ", ";
97 }
98
99 out += "[";
100 for (size_t j = 0; j < SIZE2; ++j) {
101 if (j > 0) {
102 out += ", ";
103 }
104
105 out += to_string(array[i][j]);
106 }
107 out += "]";
108 }
109 out += "]";
110
111 return out;
112}
113
114static std::string to_string(bool x) {
115 return x ? "true" : "false";
116}
117
118static std::string to_string(const hidl_string &s) {
119 return std::string("'") + s.c_str() + "'";
120}
121
122static std::string to_string(const IBaz::Foo::Bar &bar) {
123 std::string out;
124 out = "Bar(";
125 out += "z = " + to_string(bar.z) + ", ";
126 out += "s = '" + std::string(bar.s.c_str()) + "'";
127 out += ")";
128
129 return out;
130}
131
132static std::string to_string(const IBaz::Foo &foo) {
133 std::string out;
134 out = "Foo(";
135 out += "x = " + to_string(foo.x) + ", ";
136 out += "y = " + to_string(foo.y) + ", ";
137 out += "aaa = " + to_string(foo.aaa);
138 out += ")";
139
140 return out;
141}
142
143static std::string to_string(const IBaz::StringMatrix5x3 &M) {
144 return to_string(M.s);
145}
146
Hridya Valsaraju6cf95f32017-02-22 10:51:01 -0800147static std::string VectorOfArray_to_string(const IBaz::VectorOfArray &in) {
148 std::string out;
149 out += "VectorOfArray(";
150
151 for (size_t i = 0; i < in.addresses.size(); ++i) {
152 if (i > 0) {
153 out += ", ";
154 }
155
156 for (size_t j = 0; j < 6; ++j) {
157 if (j > 0) {
158 out += ":";
159 }
160
161 char tmp[3];
162 sprintf(tmp, "%02x", in.addresses[i][j]);
163
164 out += tmp;
165 }
166 }
167
168 out += ")";
169
170 return out;
171}
172
173// Methods from ::android::hardware::tests::baz::V1_0::IBase follow.
174Return<void> Baz::someBaseMethod() {
175 LOG(INFO) << "Baz::someBaseMethod";
176
177 return Void();
178}
179
180Return<bool> Baz::someBoolMethod(bool x) {
181 LOG(INFO) << "Baz::someBoolMethod(" << to_string(x) << ")";
182
183 return !x;
184}
185
186Return<void> Baz::someBoolArrayMethod(const hidl_array<bool, 3>& x,
187 someBoolArrayMethod_cb _hidl_cb) {
188 LOG(INFO) << "Baz::someBoolArrayMethod("
189 << to_string(x[0])
190 << ", "
191 << to_string(x[1])
192 << ", "
193 << to_string(x[2])
194 << ")";
195
196 hidl_array<bool, 4> out;
197 out[0] = !x[0];
198 out[1] = !x[1];
199 out[2] = !x[2];
200 out[3] = true;
201
202 _hidl_cb(out);
203
204 return Void();
205}
206
207Return<void> Baz::someBoolVectorMethod(const hidl_vec<bool>& x, someBoolVectorMethod_cb _hidl_cb) {
208 LOG(INFO) << "Baz::someBoolVectorMethod(" << to_string(x) << ")";
209
210 hidl_vec<bool> out;
211 out.resize(x.size());
212 for (size_t i = 0; i < x.size(); ++i) {
213 out[i] = !x[i];
214 }
215
216 _hidl_cb(out);
217
218 return Void();
219}
220
221Return<void> Baz::someOtherBaseMethod(const IBase::Foo& foo, someOtherBaseMethod_cb _hidl_cb) {
222 LOG(INFO) << "Baz::someOtherBaseMethod "
223 << to_string(foo);
224
225 _hidl_cb(foo);
226
227 return Void();
228}
229
230Return<void> Baz::someMethodWithFooArrays(const hidl_array<IBase::Foo, 2>& fooInput,
231 someMethodWithFooArrays_cb _hidl_cb) {
232 LOG(INFO) << "Baz::someMethodWithFooArrays "
233 << to_string(fooInput);
234
235 hidl_array<IBaz::Foo, 2> fooOutput;
236 fooOutput[0] = fooInput[1];
237 fooOutput[1] = fooInput[0];
238
239 _hidl_cb(fooOutput);
240
241 return Void();
242}
243
244Return<void> Baz::someMethodWithFooVectors(const hidl_vec<IBase::Foo>& fooInput,
245 someMethodWithFooVectors_cb _hidl_cb) {
246 LOG(INFO) << "Baz::someMethodWithFooVectors "
247 << to_string(fooInput);
248
249 hidl_vec<IBaz::Foo> fooOutput;
250 fooOutput.resize(2);
251 fooOutput[0] = fooInput[1];
252 fooOutput[1] = fooInput[0];
253
254 _hidl_cb(fooOutput);
255
256 return Void();
257}
258
259Return<void> Baz::someMethodWithVectorOfArray(const IBase::VectorOfArray& in,
260 someMethodWithVectorOfArray_cb _hidl_cb) {
261 LOG(INFO) << "Baz::someMethodWithVectorOfArray "
262 << VectorOfArray_to_string(in);
263
264 IBase::VectorOfArray out;
265
266 const size_t n = in.addresses.size();
267 out.addresses.resize(n);
268
269 for (size_t i = 0; i < n; ++i) {
270 out.addresses[i] = in.addresses[n - 1 - i];
271 }
272
273 _hidl_cb(out);
274
275 return Void();
276}
277
278Return<void> Baz::someMethodTakingAVectorOfArray(const hidl_vec<hidl_array<uint8_t, 6>>& in,
279 someMethodTakingAVectorOfArray_cb _hidl_cb) {
280 LOG(INFO) << "Baz::someMethodTakingAVectorOfArray "
281 << to_string(in);
282
283 const size_t n = in.size();
284
285 hidl_vec<hidl_array<uint8_t, 6> > out;
286 out.resize(n);
287
288 for (size_t i = 0; i < n; ++i) {
289 out[i] = in[n - 1 - i];
290 }
291
292 _hidl_cb(out);
293
294 return Void();
295}
296
297Return<void> Baz::transpose(const IBase::StringMatrix5x3& in, transpose_cb _hidl_cb) {
298 LOG(INFO) << "Baz::transpose " << to_string(in);
299
300 IBase::StringMatrix3x5 out;
301 for (size_t i = 0; i < 3; ++i) {
302 for (size_t j = 0; j < 5; ++j) {
303 out.s[i][j] = in.s[j][i];
304 }
305 }
306
307 _hidl_cb(out);
308
309 return Void();
310}
311
312Return<void> Baz::transpose2(const hidl_array<hidl_string, 5, 3>& in, transpose2_cb _hidl_cb) {
313 LOG(INFO) << "Baz::transpose2 " << to_string(in);
314
315 hidl_array<hidl_string, 3, 5> out;
316 for (size_t i = 0; i < 3; ++i) {
317 for (size_t j = 0; j < 5; ++j) {
318 out[i][j] = in[j][i];
319 }
320 }
321
322 _hidl_cb(out);
323
324 return Void();
325}
326
327Return<void> Baz::takeAMask(IBase::BitField bf,
328 uint8_t first,
329 const IBase::MyMask& second,
330 uint8_t third,
331 takeAMask_cb _hidl_cb) {
332 _hidl_cb(bf, bf | first, second.value & bf, (bf | bf) & third);
333 return Void();
334}
335
336// Methods from ::android::hardware::tests::baz::V1_0::IBaz follow.
337
338Return<void> Baz::doThis(float param) {
339 LOG(INFO) << "Baz::doThis(" << param << ")";
340
341 return Void();
342}
343
344Return<int32_t> Baz::doThatAndReturnSomething(int64_t param) {
345 LOG(INFO) << "Baz::doThatAndReturnSomething(" << param << ")";
346
347 return 666;
348}
349
350Return<double> Baz::doQuiteABit(int32_t a, int64_t b, float c, double d) {
351 LOG(INFO) << "Baz::doQuiteABit("
352 << a
353 << ", "
354 << b
355 << ", "
356 << c
357 << ", "
358 << d
359 << ")";
360
361 return 666.5;
362}
363
364Return<void> Baz::doSomethingElse(const hidl_array<int32_t, 15>& param,
365 doSomethingElse_cb _hidl_cb) {
366 LOG(INFO) << "Baz::doSomethingElse(...)";
367
368 hidl_array<int32_t, 32> result;
369 for (size_t i = 0; i < 15; ++i) {
370 result[i] = 2 * param[i];
371 result[15 + i] = param[i];
372 }
373 result[30] = 1;
374 result[31] = 2;
375
376 _hidl_cb(result);
377
378 return Void();
379}
380
381Return<void> Baz::doStuffAndReturnAString(doStuffAndReturnAString_cb _hidl_cb) {
382 LOG(INFO) << "doStuffAndReturnAString";
383
384 hidl_string s;
385 s = "Hello, world!";
386
387 _hidl_cb(s);
388
389 return Void();
390}
391
392Return<void> Baz::mapThisVector(const hidl_vec<int32_t>& param, mapThisVector_cb _hidl_cb) {
393 LOG(INFO) << "mapThisVector";
394
395 hidl_vec<int32_t> out;
396 out.resize(param.size());
397 for (size_t i = 0; i < param.size(); ++i) {
398 out[i] = param[i] * 2;
399 }
400
401 _hidl_cb(out);
402
403 return Void();
404}
405
406Return<void> Baz::callMe(const sp<IBazCallback>& cb) {
407 LOG(INFO) << "callMe " << cb.get();
408
409 if (cb != NULL) {
410 sp<IBazCallback> my_cb = new BazCallback;
411 cb->heyItsMe(my_cb);
412 }
413
414 return Void();
415}
416
417Return<void> Baz::callMeLater(const sp<IBazCallback>& cb) {
418 LOG(INFO) << "callMeLater " << cb.get();
419
420 mStoredCallback = cb;
421
422 return Void();
423}
424
425Return<void> Baz::iAmFreeNow() {
426 if (mStoredCallback != nullptr) {
427 mStoredCallback->hey();
428 }
429 return Void();
430}
431
432Return<void> Baz::dieNow() {
433 exit(1);
434 return Void();
435}
436
437Return<IBaz::SomeEnum> Baz::useAnEnum(IBaz::SomeEnum zzz) {
438 LOG(INFO) << "useAnEnum " << (int)zzz;
439
440 return SomeEnum::goober;
441}
442
443Return<void> Baz::haveSomeStrings(const hidl_array<hidl_string, 3>& array,
444 haveSomeStrings_cb _hidl_cb) {
445 LOG(INFO) << "haveSomeStrings("
446 << to_string(array)
447 << ")";
448
449 hidl_array<hidl_string, 2> result;
450 result[0] = "Hello";
451 result[1] = "World";
452
453 _hidl_cb(result);
454
455 return Void();
456}
457
458Return<void> Baz::haveAStringVec(const hidl_vec<hidl_string>& vector,
459 haveAStringVec_cb _hidl_cb) {
460 LOG(INFO) << "haveAStringVec(" << to_string(vector) << ")";
461
462 hidl_vec<hidl_string> result;
463 result.resize(2);
464
465 result[0] = "Hello";
466 result[1] = "World";
467
468 _hidl_cb(result);
469
470 return Void();
471}
472
473Return<void> Baz::returnABunchOfStrings(returnABunchOfStrings_cb _hidl_cb) {
474 hidl_string eins; eins = "Eins";
475 hidl_string zwei; zwei = "Zwei";
476 hidl_string drei; drei = "Drei";
477 _hidl_cb(eins, zwei, drei);
478
479 return Void();
480}
481
482Return<uint8_t> Baz::returnABitField() {
483 return 0;
484}
485
486Return<uint32_t> Baz::size(uint32_t size) {
487 return size;
488}
489
490Return<void> Baz::getNestedStructs(getNestedStructs_cb _hidl_cb) {
491 int size = 5;
492 hidl_vec<IBaz::NestedStruct> result;
493 result.resize(size);
494 for (int i = 0; i < size; i++) {
495 result[i].a = i;
496 if (i == 1) {
497 result[i].matrices.resize(6);
498 }
499 }
500 _hidl_cb(result);
501 return Void();
502}
503// Methods from ::android::hidl::base::V1_0::IBase follow.
504
505IBaz* HIDL_FETCH_IBaz(const char* /* name */) {
506 return new Baz();
507}
508
509} // namespace implementation
510} // namespace V1_0
511} // namespace baz
512} // namespace tests
513} // namespace hardware
514} // namespace android