summaryrefslogtreecommitdiff
path: root/tests/foo/1.0/default/Foo.cpp
blob: 461568bb7557d1fdcae12797bb9996ded76b446c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396

#define LOG_TAG "hidl_test"

#include "Foo.h"
#include <android-base/logging.h>
#include <hidl-test/FooHelper.h>
#include <inttypes.h>
#include <utils/Timers.h>

namespace android {
namespace hardware {
namespace tests {
namespace foo {
namespace V1_0 {
namespace implementation {

// Methods from ::android::hardware::tests::foo::V1_0::IFoo follow.
Return<void> Foo::convertToBoolIfSmall(Discriminator d, const hidl_vec<Union>& u,
                                       convertToBoolIfSmall_cb _hidl_cb) {
    hidl_vec<ContainsUnion> res(u.size());
    for (size_t i = 0; i < u.size(); i++) {
        ContainsUnion& outValue = res[i];

        if (d == Discriminator::BOOL) {
            outValue.discriminator = Discriminator::BOOL;
            outValue.value.boolValue = u[i].boolValue;
        } else {
            uint64_t value = u[i].intValue;
            if (value == 0 || value == 1) {
                outValue.discriminator = Discriminator::BOOL;
                outValue.value.boolValue = static_cast<bool>(value);
            } else {
                outValue.discriminator = Discriminator::INT;
                outValue.value.intValue = value;
            }
        }
    }
    _hidl_cb(res);
    return Void();
}

Return<void> Foo::doThis(float param) {
    LOG(INFO) << "SERVER(Foo) doThis(" << param << ")";

    return Void();
}

Return<int32_t> Foo::doThatAndReturnSomething(
        int64_t param) {
    LOG(INFO) << "SERVER(Foo) doThatAndReturnSomething(" << param << ")";

    return 666;
}

Return<double> Foo::doQuiteABit(
        int32_t a,
        int64_t b,
        float c,
        double d) {
    LOG(INFO) << "SERVER(Foo) doQuiteABit("
              << a
              << ", "
              << b
              << ", "
              << c
              << ", "
              << d
              << ")";

    return 666.5;
}

Return<void> Foo::doSomethingElse(
        const hidl_array<int32_t, 15> &param, doSomethingElse_cb _cb) {
    LOG(INFO) << "SERVER(Foo) doSomethingElse(...)";

    hidl_array<int32_t, 32> result;
    for (size_t i = 0; i < 15; ++i) {
        result[i] = 2 * param[i];
        result[15 + i] = param[i];
    }
    result[30] = 1;
    result[31] = 2;

    _cb(result);

    return Void();
}

Return<void> Foo::doStuffAndReturnAString(
        doStuffAndReturnAString_cb _cb) {
    LOG(INFO) << "SERVER(Foo) doStuffAndReturnAString";

    _cb("Hello, world");

    return Void();
}

Return<void> Foo::mapThisVector(
        const hidl_vec<int32_t> &param, mapThisVector_cb _cb) {
    LOG(INFO) << "SERVER(Foo) mapThisVector";

    hidl_vec<int32_t> out;
    out.resize(param.size());

    for (size_t i = 0; i < out.size(); ++i) {
        out[i] = param[i] * 2;
    }

    _cb(out);

    return Void();
}

Return<void> Foo::callMe(
        const sp<IFooCallback> &cb) {
    LOG(INFO) << "SERVER(Foo) callMe " << cb.get();

    if (cb != NULL) {
        hidl_array<nsecs_t, 3> c;
        LOG(INFO) << "SERVER(Foo) callMe "
                  << cb.get()
                  << " calling IFooCallback::heyItsYou, should return immediately";
        c[0] = systemTime();
        cb->heyItsYou(cb);
        c[0] = systemTime() - c[0];
        LOG(INFO) << "SERVER(Foo) callMe "
                  << cb.get()
                  << " calling IFooCallback::heyItsYou, returned after"
                  << c[0]
                  << "ns";
        LOG(INFO) << "SERVER(Foo) callMe "
                  << cb.get()
                  << " calling IFooCallback::heyItsYouIsntIt, should block for"
                  << DELAY_S
                  << " seconds";
        c[1] = systemTime();
        bool answer = cb->heyItsYouIsntIt(cb);
        c[1] = systemTime() - c[1];

        LOG(INFO) << "SERVER(Foo) callMe "
                  << cb.get()
                  << " calling IFooCallback::heyItsYouIsntIt, responded with "
                  << answer
                  << " after "
                  << c[1]
                  << "ns";

        LOG(INFO) << "SERVER(Foo) callMe "
                  << cb.get()
                  << " calling IFooCallback::heyItsTheMeaningOfLife,"
                  << " should return immediately";
        c[2] = systemTime();
        cb->heyItsTheMeaningOfLife(42);
        c[2] = systemTime() - c[2];

        LOG(INFO) << "SERVER(Foo) callMe "
                  << cb.get()
                  << " cAfter call to IFooCallback::heyItsTheMeaningOfLife responded after "
                  << c[2]
                  << "ns";
        LOG(INFO) << "SERVER(Foo) callMe "
                  << cb.get()
                  << " calling IFooCallback::youBlockedMeFor to report times";
        cb->youBlockedMeFor(c);
        LOG(INFO) << "SERVER(Foo) callMe "
                  << cb.get()
                  << " After call to IFooCallback::youBlockedMeFor";
    }

    return Void();
}

Return<Foo::SomeEnum> Foo::useAnEnum(SomeEnum param) {
    LOG(INFO) << "SERVER(Foo) useAnEnum " << (int)param;

    return SomeEnum::goober;
}

Return<void> Foo::haveAGooberVec(const hidl_vec<Goober>& param) {
    LOG(INFO) << "SERVER(Foo) haveAGooberVec &param = " << &param;

    return Void();
}

Return<void> Foo::haveAGoober(const Goober &g) {
    LOG(INFO) << "SERVER(Foo) haveaGoober g=" << &g;

    return Void();
}

Return<void> Foo::haveAGooberArray(const hidl_array<Goober, 20> & /* lots */) {
    LOG(INFO) << "SERVER(Foo) haveAGooberArray";

    return Void();
}

Return<void> Foo::haveATypeFromAnotherFile(const Abc &def) {
    LOG(INFO) << "SERVER(Foo) haveATypeFromAnotherFile def=" << &def;

    return Void();
}

Return<void> Foo::haveSomeStrings(
        const hidl_array<hidl_string, 3> &array,
        haveSomeStrings_cb _cb) {

    LOG(INFO) << "SERVER(Foo) haveSomeStrings([\""
              << array[0].c_str()
              << "\", \""
              << array[1].c_str()
              << "\", \""
              << array[2].c_str()
              << "\"])";

    hidl_array<hidl_string, 2> result;
    result[0] = "Hello";
    result[1] = "World";

    _cb(result);

    return Void();
}

Return<void> Foo::haveAStringVec(
        const hidl_vec<hidl_string> &vector,
        haveAStringVec_cb _cb) {
    LOG(INFO) << "SERVER(Foo) haveAStringVec([\""
              << vector[0].c_str()
              << "\", \""
              << vector[1].c_str()
              << "\", \""
              << vector[2].c_str()
              << "\"])";

    hidl_vec<hidl_string> result;
    result.resize(2);

    result[0] = "Hello";
    result[1] = "World";

    _cb(result);

    return Void();
}

Return<void> Foo::transposeMe(
        const hidl_array<float, 3, 5> &in, transposeMe_cb _cb) {
    LOG(INFO) << "SERVER(Foo) transposeMe(" << to_string(in).c_str() << ")";

    hidl_array<float, 5, 3> out;
    for (size_t i = 0; i < 5; ++i) {
        for (size_t j = 0; j < 3; ++j) {
            out[i][j] = in[j][i];
        }
    }

    LOG(INFO) << "SERVER(Foo) transposeMe returning " << to_string(out).c_str();

    _cb(out);

    return Void();
}

Return<void> Foo::callingDrWho(
        const MultiDimensional &in, callingDrWho_cb _hidl_cb) {
    LOG(INFO) << "SERVER(Foo) callingDrWho(" << MultiDimensionalToString(in).c_str() << ")";

    MultiDimensional out;
    for (size_t i = 0; i < 5; ++i) {
        for (size_t j = 0; j < 3; ++j) {
            out.quuxMatrix[i][j].first = in.quuxMatrix[4 - i][2 - j].last;
            out.quuxMatrix[i][j].last = in.quuxMatrix[4 - i][2 - j].first;
        }
    }

    _hidl_cb(out);

    return Void();
}

Return<void> Foo::transpose(const StringMatrix5x3 &in, transpose_cb _hidl_cb) {
    LOG(INFO) << "SERVER(Foo) transpose " << to_string(in);

    StringMatrix3x5 out;
    for (size_t i = 0; i < 3; ++i) {
        for (size_t j = 0; j < 5; ++j) {
            out.s[i][j] = in.s[j][i];
        }
    }

    _hidl_cb(out);

    return Void();
}

Return<void> Foo::transpose2(
        const hidl_array<hidl_string, 5, 3> &in, transpose2_cb _hidl_cb) {
    LOG(INFO) << "SERVER(Foo) transpose2 " << to_string(in);

    hidl_array<hidl_string, 3, 5> out;
    for (size_t i = 0; i < 3; ++i) {
        for (size_t j = 0; j < 5; ++j) {
            out[i][j] = in[j][i];
        }
    }

    _hidl_cb(out);

    return Void();
}

Return<void> Foo::sendVec(
        const hidl_vec<uint8_t> &data, sendVec_cb _hidl_cb) {
    _hidl_cb(data);

    return Void();
}

Return<void> Foo::sendVecVec(sendVecVec_cb _hidl_cb) {
    hidl_vec<hidl_vec<uint8_t>> data;
    _hidl_cb(data);

    return Void();
}

Return<void> Foo::haveAVectorOfInterfaces(
        const hidl_vec<sp<ISimple> > &in,
        haveAVectorOfInterfaces_cb _hidl_cb) {
    _hidl_cb(in);

    return Void();
}

Return<void> Foo::haveAVectorOfGenericInterfaces(
        const hidl_vec<sp<android::hidl::base::V1_0::IBase> > &in,
        haveAVectorOfGenericInterfaces_cb _hidl_cb) {
    _hidl_cb(in);
    return Void();
}

Return<void> Foo::createMyHandle(createMyHandle_cb _hidl_cb) {
    native_handle_t* nh = native_handle_create(0, 10);
    int data[] = {2,3,5,7,11,13,17,19,21,23};
    CHECK(sizeof(data) == 10 * sizeof(int));
    memcpy(nh->data, data, sizeof(data));
    mHandles.push_back(nh);

    MyHandle h;
    h.guard = 666;
    h.h = nh;
    _hidl_cb(h);
    return Void();
}

Return<void> Foo::createHandles(uint32_t size, createHandles_cb _hidl_cb) {
    hidl_vec<hidl_handle> handles;
    handles.resize(size);
    for(uint32_t i = 0; i < size; ++i) {
        createMyHandle([&](const MyHandle& h) {
            handles[i] = h.h;
        });
    }
    _hidl_cb(handles);
    return Void();
}

Return<void> Foo::closeHandles() {
    for(native_handle_t* h : mHandles) {
        native_handle_delete(h);
    }
    mHandles.clear();
    return Void();
}

Return<void> Foo::echoNullInterface(const sp<IFooCallback> &cb, echoNullInterface_cb _hidl_cb) {
    _hidl_cb(cb == nullptr, cb);

    return Void();
}

Return<void> Foo::repeatWithFmq(const IFoo::WithFmq& withFmq, repeatWithFmq_cb _hidl_cb) {
    _hidl_cb(withFmq);
    return Void();
}

IFoo* HIDL_FETCH_IFoo(const char* /* name */) {
    return new Foo();
}

} // namespace implementation
}  // namespace V1_0
}  // namespace foo
}  // namespace tests
}  // namespace hardware
}  // namespace android