1 /*
2  * Copyright (C) 2017 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #include "Baz.h"
18 #include <android-base/logging.h>
19 
20 namespace android {
21 namespace hardware {
22 namespace tests {
23 namespace baz {
24 namespace V1_0 {
25 namespace implementation {
26 
27 struct BazCallback : public IBazCallback {
28     Return<void> heyItsMe(const sp<IBazCallback> &cb) override;
29     Return<void> hey() override;
30 };
31 
heyItsMe(const sp<IBazCallback> & cb)32 Return<void> BazCallback::heyItsMe(
33         const sp<IBazCallback> &cb) {
34     LOG(INFO) << "SERVER: heyItsMe cb = " << cb.get();
35 
36     return Void();
37 }
38 
hey()39 Return<void> BazCallback::hey() {
40     LOG(INFO) << "SERVER: hey";
41 
42     return Void();
43 }
44 
45 // Methods from ::android::hardware::tests::baz::V1_0::IBase follow.
isJava()46 Return<bool> Baz::isJava() {
47     LOG(INFO) << "Baz::isJava";
48 
49     return false;
50 }
51 
someBaseMethod()52 Return<void> Baz::someBaseMethod() {
53     LOG(INFO) << "Baz::someBaseMethod";
54 
55     return Void();
56 }
57 
someBoolMethod(bool x)58 Return<bool> Baz::someBoolMethod(bool x) {
59     LOG(INFO) << "Baz::someBoolMethod(" << std::to_string(x) << ")";
60 
61     return !x;
62 }
63 
someBoolArrayMethod(const hidl_array<bool,3> & x,someBoolArrayMethod_cb _hidl_cb)64 Return<void> Baz::someBoolArrayMethod(const hidl_array<bool, 3>& x,
65                                       someBoolArrayMethod_cb _hidl_cb) {
66     LOG(INFO) << "Baz::someBoolArrayMethod(" << toString(x) << ")";
67 
68     hidl_array<bool, 4> out;
69     out[0] = !x[0];
70     out[1] = !x[1];
71     out[2] = !x[2];
72     out[3] = true;
73 
74     _hidl_cb(out);
75 
76     return Void();
77 }
78 
someBoolVectorMethod(const hidl_vec<bool> & x,someBoolVectorMethod_cb _hidl_cb)79 Return<void> Baz::someBoolVectorMethod(const hidl_vec<bool>& x, someBoolVectorMethod_cb _hidl_cb) {
80     LOG(INFO) << "Baz::someBoolVectorMethod(" << toString(x) << ")";
81 
82     hidl_vec<bool> out;
83     out.resize(x.size());
84     for (size_t i = 0; i < x.size(); ++i) {
85         out[i] = !x[i];
86     }
87 
88     _hidl_cb(out);
89 
90     return Void();
91 }
92 
someOtherBaseMethod(const IBase::Foo & foo,someOtherBaseMethod_cb _hidl_cb)93 Return<void> Baz::someOtherBaseMethod(const IBase::Foo& foo, someOtherBaseMethod_cb _hidl_cb) {
94     LOG(INFO) << "Baz::someOtherBaseMethod "
95               << toString(foo);
96 
97     _hidl_cb(foo);
98 
99     return Void();
100 }
101 
someMethodWithFooArrays(const hidl_array<IBase::Foo,2> & fooInput,someMethodWithFooArrays_cb _hidl_cb)102 Return<void> Baz::someMethodWithFooArrays(const hidl_array<IBase::Foo, 2>& fooInput,
103                                           someMethodWithFooArrays_cb _hidl_cb) {
104     LOG(INFO) << "Baz::someMethodWithFooArrays "
105               << toString(fooInput);
106 
107     hidl_array<IBaz::Foo, 2> fooOutput;
108     fooOutput[0] = fooInput[1];
109     fooOutput[1] = fooInput[0];
110 
111     _hidl_cb(fooOutput);
112 
113     return Void();
114 }
115 
someMethodWithFooVectors(const hidl_vec<IBase::Foo> & fooInput,someMethodWithFooVectors_cb _hidl_cb)116 Return<void> Baz::someMethodWithFooVectors(const hidl_vec<IBase::Foo>& fooInput,
117                                            someMethodWithFooVectors_cb _hidl_cb) {
118     LOG(INFO) << "Baz::someMethodWithFooVectors "
119               << toString(fooInput);
120 
121     hidl_vec<IBaz::Foo> fooOutput;
122     fooOutput.resize(2);
123     fooOutput[0] = fooInput[1];
124     fooOutput[1] = fooInput[0];
125 
126     _hidl_cb(fooOutput);
127 
128     return Void();
129 }
130 
someMethodWithVectorOfArray(const IBase::VectorOfArray & in,someMethodWithVectorOfArray_cb _hidl_cb)131 Return<void> Baz::someMethodWithVectorOfArray(const IBase::VectorOfArray& in,
132                                               someMethodWithVectorOfArray_cb _hidl_cb) {
133     LOG(INFO) << "Baz::someMethodWithVectorOfArray "
134               << toString(in);
135 
136     IBase::VectorOfArray out;
137 
138     const size_t n = in.addresses.size();
139     out.addresses.resize(n);
140 
141     for (size_t i = 0; i < n; ++i) {
142         out.addresses[i] = in.addresses[n - 1 - i];
143     }
144 
145     _hidl_cb(out);
146 
147     return Void();
148 }
149 
someMethodTakingAVectorOfArray(const hidl_vec<hidl_array<uint8_t,6>> & in,someMethodTakingAVectorOfArray_cb _hidl_cb)150 Return<void> Baz::someMethodTakingAVectorOfArray(const hidl_vec<hidl_array<uint8_t, 6>>& in,
151                                                  someMethodTakingAVectorOfArray_cb _hidl_cb) {
152     LOG(INFO) << "Baz::someMethodTakingAVectorOfArray "
153               << toString(in);
154 
155     const size_t n = in.size();
156 
157     hidl_vec<hidl_array<uint8_t, 6> > out;
158     out.resize(n);
159 
160     for (size_t i = 0; i < n; ++i) {
161         out[i] = in[n - 1 - i];
162     }
163 
164     _hidl_cb(out);
165 
166     return Void();
167 }
168 
transpose(const IBase::StringMatrix5x3 & in,transpose_cb _hidl_cb)169 Return<void> Baz::transpose(const IBase::StringMatrix5x3& in, transpose_cb _hidl_cb) {
170     LOG(INFO) << "Baz::transpose " << toString(in);
171 
172     IBase::StringMatrix3x5 out;
173     for (size_t i = 0; i < 3; ++i) {
174         for (size_t j = 0; j < 5; ++j) {
175             out.s[i][j] = in.s[j][i];
176         }
177     }
178 
179     _hidl_cb(out);
180 
181     return Void();
182 }
183 
transpose2(const hidl_array<hidl_string,5,3> & in,transpose2_cb _hidl_cb)184 Return<void> Baz::transpose2(const hidl_array<hidl_string, 5, 3>& in, transpose2_cb _hidl_cb) {
185     LOG(INFO) << "Baz::transpose2 " << toString(in);
186 
187     hidl_array<hidl_string, 3, 5> out;
188     for (size_t i = 0; i < 3; ++i) {
189         for (size_t j = 0; j < 5; ++j) {
190             out[i][j] = in[j][i];
191         }
192     }
193 
194     _hidl_cb(out);
195 
196     return Void();
197 }
198 
takeAMask(IBase::BitField bf,uint8_t first,const IBase::MyMask & second,uint8_t third,takeAMask_cb _hidl_cb)199 Return<void> Baz::takeAMask(IBase::BitField bf,
200                             uint8_t first,
201                             const IBase::MyMask& second,
202                             uint8_t third,
203                             takeAMask_cb _hidl_cb) {
204     _hidl_cb(bf, bf | first, second.value & bf, (bf | bf) & third);
205     return Void();
206 }
207 
testArrays(const IBase::LotsOfPrimitiveArrays & in,testArrays_cb _hidl_cb)208 Return<void> Baz::testArrays(
209         const IBase::LotsOfPrimitiveArrays &in,
210         testArrays_cb _hidl_cb) {
211     _hidl_cb(in);
212     return Void();
213 }
214 
testByteVecs(const hidl_vec<IBase::ByteOneDim> & in,testByteVecs_cb _hidl_cb)215 Return<void> Baz::testByteVecs(
216         const hidl_vec<IBase::ByteOneDim> &in,
217         testByteVecs_cb _hidl_cb) {
218     _hidl_cb(in);
219     return Void();
220 }
221 
testBooleanVecs(const hidl_vec<IBase::BooleanOneDim> & in,testBooleanVecs_cb _hidl_cb)222 Return<void> Baz::testBooleanVecs(
223         const hidl_vec<IBase::BooleanOneDim> &in,
224         testBooleanVecs_cb _hidl_cb) {
225     _hidl_cb(in);
226     return Void();
227 }
228 
testDoubleVecs(const hidl_vec<IBase::DoubleOneDim> & in,testDoubleVecs_cb _hidl_cb)229 Return<void> Baz::testDoubleVecs(
230         const hidl_vec<IBase::DoubleOneDim> &in,
231         testDoubleVecs_cb _hidl_cb) {
232     _hidl_cb(in);
233     return Void();
234 }
235 
236 // Methods from ::android::hardware::tests::baz::V1_0::IBaz follow.
237 
doThis(float param)238 Return<void> Baz::doThis(float param) {
239     LOG(INFO) << "Baz::doThis(" << param << ")";
240 
241     return Void();
242 }
243 
doThatAndReturnSomething(int64_t param)244 Return<int32_t> Baz::doThatAndReturnSomething(int64_t param) {
245     LOG(INFO) << "Baz::doThatAndReturnSomething(" << param << ")";
246 
247     return 666;
248 }
249 
doQuiteABit(int32_t a,int64_t b,float c,double d)250 Return<double> Baz::doQuiteABit(int32_t a, int64_t b, float c, double d) {
251     LOG(INFO) << "Baz::doQuiteABit("
252               << a
253               << ", "
254               << b
255               << ", "
256               << c
257               << ", "
258               << d
259               << ")";
260 
261     return 666.5;
262 }
263 
doSomethingElse(const hidl_array<int32_t,15> & param,doSomethingElse_cb _hidl_cb)264 Return<void> Baz::doSomethingElse(const hidl_array<int32_t, 15>& param,
265                                   doSomethingElse_cb _hidl_cb) {
266     LOG(INFO) << "Baz::doSomethingElse(...)";
267 
268     hidl_array<int32_t, 32> result;
269     for (size_t i = 0; i < 15; ++i) {
270         result[i] = 2 * param[i];
271         result[15 + i] = param[i];
272     }
273     result[30] = 1;
274     result[31] = 2;
275 
276     _hidl_cb(result);
277 
278     return Void();
279 }
280 
doStuffAndReturnAString(doStuffAndReturnAString_cb _hidl_cb)281 Return<void> Baz::doStuffAndReturnAString(doStuffAndReturnAString_cb _hidl_cb) {
282     LOG(INFO) << "doStuffAndReturnAString";
283 
284     hidl_string s;
285     s = "Hello, world!";
286 
287     _hidl_cb(s);
288 
289     return Void();
290 }
291 
mapThisVector(const hidl_vec<int32_t> & param,mapThisVector_cb _hidl_cb)292 Return<void> Baz::mapThisVector(const hidl_vec<int32_t>& param, mapThisVector_cb _hidl_cb) {
293     LOG(INFO) << "mapThisVector";
294 
295     hidl_vec<int32_t> out;
296     out.resize(param.size());
297     for (size_t i = 0; i < param.size(); ++i) {
298         out[i] = param[i] * 2;
299     }
300 
301     _hidl_cb(out);
302 
303     return Void();
304 }
305 
callMe(const sp<IBazCallback> & cb)306 Return<void> Baz::callMe(const sp<IBazCallback>& cb) {
307     LOG(INFO) << "callMe " << cb.get();
308 
309     if (cb != NULL) {
310         sp<IBazCallback> my_cb = new BazCallback;
311         cb->heyItsMe(my_cb);
312     }
313 
314     return Void();
315 }
316 
callMeLater(const sp<IBazCallback> & cb)317 Return<void> Baz::callMeLater(const sp<IBazCallback>& cb) {
318     LOG(INFO) << "callMeLater " << cb.get();
319 
320     mStoredCallback = cb;
321 
322     return Void();
323 }
324 
iAmFreeNow()325 Return<void> Baz::iAmFreeNow() {
326     if (mStoredCallback != nullptr) {
327         mStoredCallback->hey();
328     }
329     return Void();
330 }
331 
dieNow()332 Return<void> Baz::dieNow() {
333     exit(1);
334     return Void();
335 }
336 
useAnEnum(IBaz::SomeEnum zzz)337 Return<IBaz::SomeEnum> Baz::useAnEnum(IBaz::SomeEnum zzz) {
338     LOG(INFO) << "useAnEnum " << (int)zzz;
339 
340     return SomeEnum::quux;
341 }
342 
haveSomeStrings(const hidl_array<hidl_string,3> & array,haveSomeStrings_cb _hidl_cb)343 Return<void> Baz::haveSomeStrings(const hidl_array<hidl_string, 3>& array,
344                                   haveSomeStrings_cb _hidl_cb) {
345     LOG(INFO) << "haveSomeStrings("
346               << toString(array)
347               << ")";
348 
349     hidl_array<hidl_string, 2> result;
350     result[0] = "Hello";
351     result[1] = "World";
352 
353     _hidl_cb(result);
354 
355     return Void();
356 }
357 
haveAStringVec(const hidl_vec<hidl_string> & vector,haveAStringVec_cb _hidl_cb)358 Return<void> Baz::haveAStringVec(const hidl_vec<hidl_string>& vector,
359                                  haveAStringVec_cb _hidl_cb) {
360     LOG(INFO) << "haveAStringVec(" << toString(vector) << ")";
361 
362     hidl_vec<hidl_string> result;
363     result.resize(2);
364 
365     result[0] = "Hello";
366     result[1] = "World";
367 
368     _hidl_cb(result);
369 
370     return Void();
371 }
372 
repeatBitfieldVec(const hidl_vec<uint8_t> & vector,repeatBitfieldVec_cb _hidl_cb)373 Return<void> Baz::repeatBitfieldVec(const hidl_vec<uint8_t>& vector,
374                                     repeatBitfieldVec_cb _hidl_cb) {
375     _hidl_cb(vector);
376     return Void();
377 }
378 
returnABunchOfStrings(returnABunchOfStrings_cb _hidl_cb)379 Return<void> Baz::returnABunchOfStrings(returnABunchOfStrings_cb _hidl_cb) {
380     hidl_string eins; eins = "Eins";
381     hidl_string zwei; zwei = "Zwei";
382     hidl_string drei; drei = "Drei";
383     _hidl_cb(eins, zwei, drei);
384 
385     return Void();
386 }
387 
returnABitField()388 Return<uint8_t> Baz::returnABitField() {
389     return 0;
390 }
391 
size(uint32_t size)392 Return<uint32_t> Baz::size(uint32_t size) {
393     return size;
394 }
395 
getNestedStructs(getNestedStructs_cb _hidl_cb)396 Return<void> Baz::getNestedStructs(getNestedStructs_cb _hidl_cb) {
397     int size = 5;
398     hidl_vec<IBaz::NestedStruct> result;
399     result.resize(size);
400     for (int i = 0; i < size; i++) {
401         result[i].a = i;
402         if (i == 1) {
403             result[i].matrices.resize(6);
404         }
405     }
406     _hidl_cb(result);
407     return Void();
408 }
409 
haveSomeStructWithInterface(const StructWithInterface & swi,haveSomeStructWithInterface_cb _hidl_cb)410 Return<void> Baz::haveSomeStructWithInterface(const StructWithInterface& swi,
411                                               haveSomeStructWithInterface_cb _hidl_cb) {
412     _hidl_cb(swi);
413     return Void();
414 }
415 // Methods from ::android::hidl::base::V1_0::IBase follow.
416 
HIDL_FETCH_IBaz(const char *)417 IBaz* HIDL_FETCH_IBaz(const char* /* name */) {
418     return new Baz();
419 }
420 
421 }  // namespace implementation
422 }  // namespace V1_0
423 }  // namespace baz
424 }  // namespace tests
425 }  // namespace hardware
426 }  // namespace android
427