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