blob: cf4f9755c67ad4affe878763b11f362abe6778f4 [file] [log] [blame]
Yifan Hongd5b5b2e2016-10-06 13:50:49 -07001
2#define LOG_TAG "hidl_test"
3
4#include "Foo.h"
5#include "FooCallback.h"
6#include <android-base/logging.h>
Yifan Hong30dc3de2016-10-17 11:38:15 -07007#include <hidl-test/FooHelper.h>
Yifan Hongd5b5b2e2016-10-06 13:50:49 -07008#include <inttypes.h>
9#include <utils/Timers.h>
10
11namespace android {
12namespace hardware {
13namespace tests {
14namespace foo {
15namespace V1_0 {
16namespace implementation {
17
18// Methods from ::android::hardware::tests::foo::V1_0::IFoo follow.
19Return<void> Foo::doThis(float param) {
20 ALOGI("SERVER(Foo) doThis(%.2f)", param);
21
22 return Void();
23}
24
Yifan Hongd5b5b2e2016-10-06 13:50:49 -070025Return<int32_t> Foo::doThatAndReturnSomething(
26 int64_t param) {
27 LOG(INFO) << "SERVER(Foo) doThatAndReturnSomething(" << param << ")";
28
29 return 666;
30}
31
32Return<double> Foo::doQuiteABit(
33 int32_t a,
34 int64_t b,
35 float c,
36 double d) {
37 LOG(INFO) << "SERVER(Foo) doQuiteABit("
38 << a
39 << ", "
40 << b
41 << ", "
42 << c
43 << ", "
44 << d
45 << ")";
46
47 return 666.5;
48}
49
50Return<void> Foo::doSomethingElse(
51 const hidl_array<int32_t, 15> &param, doSomethingElse_cb _cb) {
52 ALOGI("SERVER(Foo) doSomethingElse(...)");
53
54 hidl_array<int32_t, 32> result;
55 for (size_t i = 0; i < 15; ++i) {
56 result[i] = 2 * param[i];
57 result[15 + i] = param[i];
58 }
59 result[30] = 1;
60 result[31] = 2;
61
62 _cb(result);
63
64 return Void();
65}
66
67Return<void> Foo::doStuffAndReturnAString(
68 doStuffAndReturnAString_cb _cb) {
69 ALOGI("SERVER(Foo) doStuffAndReturnAString");
70
Steven Moreland54813ed2016-10-24 10:46:43 -070071 _cb("Hello, world");
Yifan Hongd5b5b2e2016-10-06 13:50:49 -070072
73 return Void();
74}
75
76Return<void> Foo::mapThisVector(
77 const hidl_vec<int32_t> &param, mapThisVector_cb _cb) {
78 ALOGI("SERVER(Foo) mapThisVector");
79
80 hidl_vec<int32_t> out;
81 out.resize(param.size());
82
83 for (size_t i = 0; i < out.size(); ++i) {
84 out[i] = param[i] * 2;
85 }
86
87 _cb(out);
88
89 return Void();
90}
91
92Return<void> Foo::callMe(
93 const sp<IFooCallback> &cb) {
94 ALOGI("SERVER(Foo) callMe %p", cb.get());
95
96 if (cb != NULL) {
97
98 hidl_array<nsecs_t, 3> c;
99 ALOGI("SERVER(Foo) callMe %p calling IFooCallback::heyItsYou, " \
100 "should return immediately", cb.get());
101 c[0] = systemTime();
102 cb->heyItsYou(cb);
103 c[0] = systemTime() - c[0];
104 ALOGI("SERVER(Foo) callMe %p calling IFooCallback::heyItsYou " \
105 "returned after %" PRId64 "ns", cb.get(), c[0]);
106
107 ALOGI("SERVER(Foo) callMe %p calling IFooCallback::heyItsYouIsntIt, " \
108 "should block for %" PRId64 " seconds", cb.get(),
Yifan Hong30dc3de2016-10-17 11:38:15 -0700109 DELAY_S);
Yifan Hongd5b5b2e2016-10-06 13:50:49 -0700110 c[1] = systemTime();
111 bool answer = cb->heyItsYouIsntIt(cb);
112 c[1] = systemTime() - c[1];
113 ALOGI("SERVER(Foo) callMe %p IFooCallback::heyItsYouIsntIt " \
114 "responded with %d after %" PRId64 "ns", cb.get(), answer, c[1]);
115
116 ALOGI("SERVER(Foo) callMe %p calling " \
117 "IFooCallback::heyItsTheMeaningOfLife, " \
118 "should return immediately ", cb.get());
119 c[2] = systemTime();
120 cb->heyItsTheMeaningOfLife(42);
121 c[2] = systemTime() - c[2];
122 ALOGI("SERVER(Foo) callMe %p After call to " \
123 "IFooCallback::heyItsTheMeaningOfLife " \
124 "responded after %" PRId64 "ns", cb.get(), c[2]);
125
126 ALOGI("SERVER(Foo) callMe %p calling IFooCallback::youBlockedMeFor " \
127 "to report times", cb.get());
128 cb->youBlockedMeFor(c);
129 ALOGI("SERVER(Foo) callMe %p After call to " \
130 "IFooCallback::heyYouBlockedMeFor", cb.get());
131 }
132
133 return Void();
134}
135
136Return<Foo::SomeEnum> Foo::useAnEnum(SomeEnum param) {
137 ALOGI("SERVER(Foo) useAnEnum %d", (int)param);
138
139 return SomeEnum::goober;
140}
141
142Return<void> Foo::haveAGooberVec(const hidl_vec<Goober>& param) {
143 ALOGI("SERVER(Foo) haveAGooberVec &param = %p", &param);
144
145 return Void();
146}
147
148Return<void> Foo::haveAGoober(const Goober &g) {
149 ALOGI("SERVER(Foo) haveaGoober g=%p", &g);
150
151 return Void();
152}
153
154Return<void> Foo::haveAGooberArray(const hidl_array<Goober, 20> & /* lots */) {
155 ALOGI("SERVER(Foo) haveAGooberArray");
156
157 return Void();
158}
159
160Return<void> Foo::haveATypeFromAnotherFile(const Abc &def) {
161 ALOGI("SERVER(Foo) haveATypeFromAnotherFile def=%p", &def);
162
163 return Void();
164}
165
166Return<void> Foo::haveSomeStrings(
167 const hidl_array<hidl_string, 3> &array,
168 haveSomeStrings_cb _cb) {
169 ALOGI("SERVER(Foo) haveSomeStrings([\"%s\", \"%s\", \"%s\"])",
170 array[0].c_str(),
171 array[1].c_str(),
172 array[2].c_str());
173
174 hidl_array<hidl_string, 2> result;
175 result[0] = "Hello";
176 result[1] = "World";
177
178 _cb(result);
179
180 return Void();
181}
182
183Return<void> Foo::haveAStringVec(
184 const hidl_vec<hidl_string> &vector,
185 haveAStringVec_cb _cb) {
186 ALOGI("SERVER(Foo) haveAStringVec([\"%s\", \"%s\", \"%s\"])",
187 vector[0].c_str(),
188 vector[1].c_str(),
189 vector[2].c_str());
190
191 hidl_vec<hidl_string> result;
192 result.resize(2);
193
194 result[0] = "Hello";
195 result[1] = "World";
196
197 _cb(result);
198
199 return Void();
200}
201
Yifan Hongd5b5b2e2016-10-06 13:50:49 -0700202Return<void> Foo::transposeMe(
203 const hidl_array<float, 3, 5> &in, transposeMe_cb _cb) {
204 ALOGI("SERVER(Foo) transposeMe(%s)", to_string(in).c_str());
205
206 hidl_array<float, 5, 3> out;
207 for (size_t i = 0; i < 5; ++i) {
208 for (size_t j = 0; j < 3; ++j) {
209 out[i][j] = in[j][i];
210 }
211 }
212
213 ALOGI("SERVER(Foo) transposeMe returning %s", to_string(out).c_str());
214
215 _cb(out);
216
217 return Void();
218}
Yifan Hongd5b5b2e2016-10-06 13:50:49 -0700219
220Return<void> Foo::callingDrWho(
221 const MultiDimensional &in, callingDrWho_cb _hidl_cb) {
222 ALOGI("SERVER(Foo) callingDrWho(%s)", MultiDimensionalToString(in).c_str());
223
224 MultiDimensional out;
225 for (size_t i = 0; i < 5; ++i) {
226 for (size_t j = 0; j < 3; ++j) {
227 out.quuxMatrix[i][j].first = in.quuxMatrix[4 - i][2 - j].last;
228 out.quuxMatrix[i][j].last = in.quuxMatrix[4 - i][2 - j].first;
229 }
230 }
231
232 _hidl_cb(out);
233
234 return Void();
235}
236
237Return<void> Foo::transpose(const StringMatrix5x3 &in, transpose_cb _hidl_cb) {
238 LOG(INFO) << "SERVER(Foo) transpose " << to_string(in);
239
240 StringMatrix3x5 out;
241 for (size_t i = 0; i < 3; ++i) {
242 for (size_t j = 0; j < 5; ++j) {
243 out.s[i][j] = in.s[j][i];
244 }
245 }
246
247 _hidl_cb(out);
248
249 return Void();
250}
251
252Return<void> Foo::transpose2(
253 const hidl_array<hidl_string, 5, 3> &in, transpose2_cb _hidl_cb) {
254 LOG(INFO) << "SERVER(Foo) transpose2 " << to_string(in);
255
256 hidl_array<hidl_string, 3, 5> out;
257 for (size_t i = 0; i < 3; ++i) {
258 for (size_t j = 0; j < 5; ++j) {
259 out[i][j] = in[j][i];
260 }
261 }
262
263 _hidl_cb(out);
264
265 return Void();
266}
267
268Return<void> Foo::sendVec(
269 const hidl_vec<uint8_t> &data, sendVec_cb _hidl_cb) {
270 _hidl_cb(data);
271
272 return Void();
273}
274
275Return<void> Foo::sendVecVec(sendVecVec_cb _hidl_cb) {
276 hidl_vec<hidl_vec<uint8_t>> data;
277 _hidl_cb(data);
278
279 return Void();
280}
281
Andreas Huber847b1452016-10-19 14:10:55 -0700282Return<void> Foo::haveAVectorOfInterfaces(
283 const hidl_vec<sp<ISimple> > &in,
284 haveAVectorOfInterfaces_cb _hidl_cb) {
285 _hidl_cb(in);
286
287 return Void();
288}
289
290Return<void> Foo::haveAVectorOfGenericInterfaces(
291 const hidl_vec<sp<android::hardware::IBinder> > &in,
292 haveAVectorOfGenericInterfaces_cb _hidl_cb) {
293 _hidl_cb(in);
Yifan Hongebfa6332016-10-14 10:41:41 -0700294 return Void();
295}
Andreas Huber847b1452016-10-19 14:10:55 -0700296
Yifan Hongebfa6332016-10-14 10:41:41 -0700297Return<void> Foo::createMyHandle(createMyHandle_cb _hidl_cb) {
298 native_handle_t* nh = native_handle_create(0, 10);
299 int data[] = {2,3,5,7,11,13,17,19,21,23};
300 CHECK(sizeof(data) == 10 * sizeof(int));
301 memcpy(nh->data, data, sizeof(data));
302 mHandles.push_back(nh);
303
304 MyHandle h;
305 h.guard = 666;
306 h.h = nh;
307 _hidl_cb(h);
308 return Void();
309}
310
311Return<void> Foo::createHandles(uint32_t size, createHandles_cb _hidl_cb) {
312 hidl_vec<const native_handle_t*> handles;
313 handles.resize(size);
314 for(uint32_t i = 0; i < size; ++i) {
315 createMyHandle([&](const MyHandle& h) {
316 handles[i] = h.h;
317 });
318 }
319 _hidl_cb(handles);
320 return Void();
321}
322
323Return<void> Foo::closeHandles() {
324 for(native_handle_t* h : mHandles) {
325 native_handle_delete(h);
326 }
327 mHandles.clear();
Andreas Huber847b1452016-10-19 14:10:55 -0700328 return Void();
329}
Yifan Hongd5b5b2e2016-10-06 13:50:49 -0700330
331IFoo* HIDL_FETCH_IFoo(const char* /* name */) {
332 return new Foo();
333}
334
335} // namespace implementation
336} // namespace V1_0
337} // namespace foo
338} // namespace tests
339} // namespace hardware
340} // namespace android