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 
202 // Methods from ::android::hardware::tests::baz::V1_0::IBaz follow.
203 
doThis(float param)204 Return<void> Baz::doThis(float param) {
205     LOG(INFO) << "Baz::doThis(" << param << ")";
206 
207     return Void();
208 }
209 
doThatAndReturnSomething(int64_t param)210 Return<int32_t> Baz::doThatAndReturnSomething(int64_t param) {
211     LOG(INFO) << "Baz::doThatAndReturnSomething(" << param << ")";
212 
213     return 666;
214 }
215 
doQuiteABit(int32_t a,int64_t b,float c,double d)216 Return<double> Baz::doQuiteABit(int32_t a, int64_t b, float c, double d) {
217     LOG(INFO) << "Baz::doQuiteABit("
218               << a
219               << ", "
220               << b
221               << ", "
222               << c
223               << ", "
224               << d
225               << ")";
226 
227     return 666.5;
228 }
229 
doSomethingElse(const hidl_array<int32_t,15> & param,doSomethingElse_cb _hidl_cb)230 Return<void> Baz::doSomethingElse(const hidl_array<int32_t, 15>& param,
231                                   doSomethingElse_cb _hidl_cb) {
232     LOG(INFO) << "Baz::doSomethingElse(...)";
233 
234     hidl_array<int32_t, 32> result;
235     for (size_t i = 0; i < 15; ++i) {
236         result[i] = 2 * param[i];
237         result[15 + i] = param[i];
238     }
239     result[30] = 1;
240     result[31] = 2;
241 
242     _hidl_cb(result);
243 
244     return Void();
245 }
246 
doStuffAndReturnAString(doStuffAndReturnAString_cb _hidl_cb)247 Return<void> Baz::doStuffAndReturnAString(doStuffAndReturnAString_cb _hidl_cb) {
248     LOG(INFO) << "doStuffAndReturnAString";
249 
250     hidl_string s;
251     s = "Hello, world!";
252 
253     _hidl_cb(s);
254 
255     return Void();
256 }
257 
mapThisVector(const hidl_vec<int32_t> & param,mapThisVector_cb _hidl_cb)258 Return<void> Baz::mapThisVector(const hidl_vec<int32_t>& param, mapThisVector_cb _hidl_cb) {
259     LOG(INFO) << "mapThisVector";
260 
261     hidl_vec<int32_t> out;
262     out.resize(param.size());
263     for (size_t i = 0; i < param.size(); ++i) {
264         out[i] = param[i] * 2;
265     }
266 
267     _hidl_cb(out);
268 
269     return Void();
270 }
271 
callMe(const sp<IBazCallback> & cb)272 Return<void> Baz::callMe(const sp<IBazCallback>& cb) {
273     LOG(INFO) << "callMe " << cb.get();
274 
275     if (cb != NULL) {
276         sp<IBazCallback> my_cb = new BazCallback;
277         cb->heyItsMe(my_cb);
278     }
279 
280     return Void();
281 }
282 
callMeLater(const sp<IBazCallback> & cb)283 Return<void> Baz::callMeLater(const sp<IBazCallback>& cb) {
284     LOG(INFO) << "callMeLater " << cb.get();
285 
286     mStoredCallback = cb;
287 
288     return Void();
289 }
290 
iAmFreeNow()291 Return<void> Baz::iAmFreeNow() {
292     if (mStoredCallback != nullptr) {
293         mStoredCallback->hey();
294     }
295     return Void();
296 }
297 
dieNow()298 Return<void> Baz::dieNow() {
299     exit(1);
300     return Void();
301 }
302 
useAnEnum(IBaz::SomeEnum zzz)303 Return<IBaz::SomeEnum> Baz::useAnEnum(IBaz::SomeEnum zzz) {
304     LOG(INFO) << "useAnEnum " << (int)zzz;
305 
306     return SomeEnum::goober;
307 }
308 
haveSomeStrings(const hidl_array<hidl_string,3> & array,haveSomeStrings_cb _hidl_cb)309 Return<void> Baz::haveSomeStrings(const hidl_array<hidl_string, 3>& array,
310                                   haveSomeStrings_cb _hidl_cb) {
311     LOG(INFO) << "haveSomeStrings("
312               << toString(array)
313               << ")";
314 
315     hidl_array<hidl_string, 2> result;
316     result[0] = "Hello";
317     result[1] = "World";
318 
319     _hidl_cb(result);
320 
321     return Void();
322 }
323 
haveAStringVec(const hidl_vec<hidl_string> & vector,haveAStringVec_cb _hidl_cb)324 Return<void> Baz::haveAStringVec(const hidl_vec<hidl_string>& vector,
325                                  haveAStringVec_cb _hidl_cb) {
326     LOG(INFO) << "haveAStringVec(" << toString(vector) << ")";
327 
328     hidl_vec<hidl_string> result;
329     result.resize(2);
330 
331     result[0] = "Hello";
332     result[1] = "World";
333 
334     _hidl_cb(result);
335 
336     return Void();
337 }
338 
returnABunchOfStrings(returnABunchOfStrings_cb _hidl_cb)339 Return<void> Baz::returnABunchOfStrings(returnABunchOfStrings_cb _hidl_cb) {
340     hidl_string eins; eins = "Eins";
341     hidl_string zwei; zwei = "Zwei";
342     hidl_string drei; drei = "Drei";
343     _hidl_cb(eins, zwei, drei);
344 
345     return Void();
346 }
347 
returnABitField()348 Return<uint8_t> Baz::returnABitField() {
349     return 0;
350 }
351 
size(uint32_t size)352 Return<uint32_t> Baz::size(uint32_t size) {
353     return size;
354 }
355 
getNestedStructs(getNestedStructs_cb _hidl_cb)356 Return<void> Baz::getNestedStructs(getNestedStructs_cb _hidl_cb) {
357     int size = 5;
358     hidl_vec<IBaz::NestedStruct> result;
359     result.resize(size);
360     for (int i = 0; i < size; i++) {
361         result[i].a = i;
362         if (i == 1) {
363             result[i].matrices.resize(6);
364         }
365     }
366     _hidl_cb(result);
367     return Void();
368 }
369 // Methods from ::android::hidl::base::V1_0::IBase follow.
370 
HIDL_FETCH_IBaz(const char *)371 IBaz* HIDL_FETCH_IBaz(const char* /* name */) {
372     return new Baz();
373 }
374 
375 }  // namespace implementation
376 }  // namespace V1_0
377 }  // namespace baz
378 }  // namespace tests
379 }  // namespace hardware
380 }  // namespace android
381