blob: 47a3a2873b36d007db94f52e1e20fec6da9d6f3f [file] [log] [blame]
Keun-young Park28dd4702015-11-19 18:06:04 -08001/*
2 * Copyright (C) 2015 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
Keun-young Parkaaeffaf2015-11-25 17:24:10 -080017#ifndef ANDROID_IVEHICLENETWORK_TEST_LISTER_H
18#define ANDROID_IVEHICLENETWORK_TEST_LISTER_H
Keun-young Park28dd4702015-11-19 18:06:04 -080019
20#include <IVehicleNetworkListener.h>
21
22namespace android {
23
24class IVehicleNetworkTestListener : public BnVehicleNetworkListener {
25public:
26 IVehicleNetworkTestListener() :
27 mHalRestartCount(0) {};
28
29 virtual void onEvents(sp<VehiclePropValueListHolder>& events) {
30 String8 msg("events ");
31 Mutex::Autolock autolock(mLock);
32 for (auto& e : events->getList()) {
33 ssize_t index = mEventCounts.indexOfKey(e->prop);
34 if (index < 0) {
35 mEventCounts.add(e->prop, 1); // 1st event
36 msg.appendFormat("0x%x:%d ", e->prop, 1);
37 } else {
38 int count = mEventCounts.valueAt(index);
39 count++;
40 mEventCounts.replaceValueAt(index, count);
41 msg.appendFormat("0x%x:%d ", e->prop, count);
42 }
43 }
44 msg.append("\n");
45 std::cout<<msg.string();
46 mCondition.signal();
47 }
48
49 virtual void onHalError(int32_t errorCode, int32_t property, int32_t operation) {
50 Mutex::Autolock autolock(mHalErrorLock);
51 mErrorCode = errorCode;
52 mProperty = property;
53 mOperation = operation;
54 mHalErrorCondition.signal();
55 }
56
Keun-young Park558e9b12016-02-26 13:52:45 -080057 virtual void onHalRestart(bool /*inMocking*/) {
Keun-young Park28dd4702015-11-19 18:06:04 -080058 Mutex::Autolock autolock(mHalRestartLock);
59 mHalRestartCount++;
60 mHalRestartCondition.signal();
61 }
62
63 void waitForEvents(nsecs_t reltime) {
64 Mutex::Autolock autolock(mLock);
65 mCondition.waitRelative(mLock, reltime);
66 }
67
68 bool waitForEvent(int32_t property, nsecs_t reltime) {
69 Mutex::Autolock autolock(mLock);
70 int startCount = getEventCountLocked(property);
71 int currentCount = startCount;
72 int64_t now = android::elapsedRealtimeNano();
73 int64_t endTime = now + reltime;
74 while ((startCount == currentCount) && (now < endTime)) {
75 mCondition.waitRelative(mLock, endTime - now);
76 currentCount = getEventCountLocked(property);
77 now = android::elapsedRealtimeNano();
78 }
79 return (startCount != currentCount);
80 }
81
82 int getEventCount(int32_t property) {
83 Mutex::Autolock autolock(mLock);
84 return getEventCountLocked(property);
85 }
86
87 int getHalRestartCount() {
88 Mutex::Autolock autolock(mHalRestartLock);
89 return mHalRestartCount;
90 }
91
92 void waitForHalRestart(nsecs_t reltime) {
93 Mutex::Autolock autolock(mHalRestartLock);
94 mHalRestartCondition.waitRelative(mHalRestartLock, reltime);
95 }
96
97 void waitForHalError(nsecs_t reltime) {
98 Mutex::Autolock autolock(mHalErrorLock);
99 mHalErrorCondition.waitRelative(mHalErrorLock, reltime);
100 }
101
102 bool isErrorMatching(int32_t errorCode, int32_t property, int32_t operation) {
103 Mutex::Autolock autolock(mHalErrorLock);
104 return mErrorCode == errorCode && mProperty == property && mOperation == operation;
105 }
106
107private:
108 int getEventCountLocked(int32_t property) {
109 ssize_t index = mEventCounts.indexOfKey(property);
110 if (index < 0) {
111 return 0;
112 } else {
113 return mEventCounts.valueAt(index);
114 }
115 }
116
117
118private:
119 Mutex mLock;
120 Condition mCondition;
121 KeyedVector<int32_t, int> mEventCounts;
122
123 Mutex mHalRestartLock;
124 Condition mHalRestartCondition;
125 int mHalRestartCount;
126
127 Mutex mHalErrorLock;
128 Condition mHalErrorCondition;
129 int32_t mErrorCode;
130 int32_t mProperty;
131 int32_t mOperation;
132};
133
134}; // namespace android
Keun-young Parkaaeffaf2015-11-25 17:24:10 -0800135#endif // ANDROID_IVEHICLENETWORK_TEST_LISTER_H