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