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