1 
2 #define LOG_TAG "hidl_test"
3 
4 #include "FooCallback.h"
5 #include <hidl-test/FooHelper.h>
6 #include <inttypes.h>
7 #include <log/log.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 enum {
18     NOT_REPORTED = -1LL
19 };
20 
FooCallback()21 FooCallback::FooCallback()
22         : mLock{}, mCond{} {
23     for (size_t i = 0; i < invokeInfo.size(); i++) {
24         invokeInfo[i].invoked = false;
25         invokeInfo[i].timeNs = NOT_REPORTED;
26         invokeInfo[i].callerBlockedNs = NOT_REPORTED;
27     }
28 }
29 
heyItsYou(const sp<IFooCallback> & _cb)30 Return<void> FooCallback::heyItsYou(
31         const sp<IFooCallback> &_cb) {
32     nsecs_t start = systemTime();
33     ALOGI("SERVER(FooCallback) 1: heyItsYou cb = %p", _cb.get());
34     nsecs_t end = systemTime();
35     {
36         Mutex::Autolock lock(mLock);
37         invokeInfo[0].invoked = true;
38         invokeInfo[0].timeNs = end - start;
39         mCond.signal();
40     }
41     ALOGI("SERVER(FooCallback) 2: heyItsYou returned");
42     return Void();
43 }
44 
heyItsYouIsntIt(const sp<IFooCallback> & _cb)45 Return<bool> FooCallback::heyItsYouIsntIt(const sp<IFooCallback> &_cb) {
46     nsecs_t start = systemTime();
47     ALOGI("SERVER(FooCallback) 3: heyItsYouIsntIt cb = %p sleeping for %" PRId64 " seconds", _cb.get(), DELAY_S);
48     sleep(DELAY_S);
49     ALOGI("SERVER(FooCallback) 4: heyItsYouIsntIt cb = %p responding", _cb.get());
50     nsecs_t end = systemTime();
51     {
52         Mutex::Autolock lock(mLock);
53         invokeInfo[1].invoked = true;
54         invokeInfo[1].timeNs = end - start;
55         mCond.signal();
56     }
57     ALOGI("SERVER(FooCallback) 5: heyItsYouIsntIt cb = %p responding", _cb.get());
58     return true;
59 }
60 
heyItsTheMeaningOfLife(uint8_t tmol)61 Return<void> FooCallback::heyItsTheMeaningOfLife(uint8_t tmol) {
62     nsecs_t start = systemTime();
63     ALOGI("SERVER(FooCallback) 6.1: heyItsTheMeaningOfLife = %d sleeping for %" PRId64 " seconds", tmol, DELAY_S);
64     sleep(DELAY_S);
65     ALOGI("SERVER(FooCallback) 6.2: heyItsTheMeaningOfLife = %d done sleeping", tmol);
66     nsecs_t end = systemTime();
67     {
68         Mutex::Autolock lock(mLock);
69         invokeInfo[2].invoked = true;
70         invokeInfo[2].timeNs = end - start;
71         mCond.signal();
72     }
73     ALOGI("SERVER(FooCallback) 6.3: heyItsTheMeaningOfLife returned");
74     return Void();
75 }
76 
reportResults(int64_t ns,reportResults_cb cb)77 Return<void> FooCallback::reportResults(int64_t ns, reportResults_cb cb) {
78     ALOGI("SERVER(FooCallback) 8.1: reportResults(%" PRId64 " seconds)", nanoseconds_to_seconds(ns));
79     nsecs_t leftToWaitNs = ns;
80     bool cond;
81     {
82         Mutex::Autolock lock(mLock);
83         while ((cond = ((!invokeInfo[0].invoked ||
84                 !invokeInfo[1].invoked ||
85                 !invokeInfo[2].invoked ||
86                 invokeInfo[0].callerBlockedNs == NOT_REPORTED ||
87                 invokeInfo[1].callerBlockedNs == NOT_REPORTED ||
88                 invokeInfo[2].callerBlockedNs == NOT_REPORTED)   &&
89                leftToWaitNs > 0))) {
90             nsecs_t start = systemTime();
91             ::android::status_t rc = mCond.waitRelative(mLock, leftToWaitNs);
92             if (rc != ::android::OK) {
93                 ALOGW("SERVER(FooCallback)::reportResults(%" PRId64 " ns) Condition::waitRelative(%" PRId64 ") returned error (%d)", ns, leftToWaitNs, rc);
94                 if (rc == -ETIMEDOUT) {
95                     // time's up
96                     leftToWaitNs = -INT32_MAX;
97                 }
98                 break;
99             }
100             ALOGI("SERVER(FooCallback)::reportResults(%" PRId64 " ns) Condition::waitRelative was signalled", ns);
101             leftToWaitNs -= systemTime() - start;
102         }
103     }
104     ALOGI("SERVER(FooCallback) 8.2: reportResults returned;"
105             "invoked? %d, %d, %d; leftToWaitNs = %" PRId64 "; cond = %d",
106             invokeInfo[0].invoked, invokeInfo[1].invoked, invokeInfo[2].invoked,
107             leftToWaitNs, cond);
108     cb(leftToWaitNs, invokeInfo);
109     return Void();
110 }
111 
youBlockedMeFor(const hidl_array<int64_t,3> & ns)112 Return<void> FooCallback::youBlockedMeFor(const hidl_array<int64_t, 3> &ns) {
113     ALOGI("SERVER(FooCallback) 7.1: youBlockedMeFor");
114     {
115         Mutex::Autolock lock(mLock);
116         for (size_t i = 0; i < 3; i++) {
117             invokeInfo[i].callerBlockedNs = ns[i];
118         }
119         mCond.signal();
120     }
121     ALOGI("SERVER(FooCallback) 7.2: returned");
122     return Void();
123 }
124 
HIDL_FETCH_IFooCallback(const char *)125 IFooCallback* HIDL_FETCH_IFooCallback(const char* /* name */) {
126     return new FooCallback();
127 }
128 
129 } // namespace implementation
130 }  // namespace V1_0
131 }  // namespace foo
132 }  // namespace tests
133 }  // namespace hardware
134 }  // namespace android
135