blob: a297096c8bc0270b92369531dc870f0e654dae7a [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
Pavel Maltsevb0324b42016-09-27 21:00:41 -070020#include <queue>
21
Keun-young Park28dd4702015-11-19 18:06:04 -080022#include <IVehicleNetworkListener.h>
23
24namespace android {
25
26class IVehicleNetworkTestListener : public BnVehicleNetworkListener {
27public:
28 IVehicleNetworkTestListener() :
Pavel Maltsevb0324b42016-09-27 21:00:41 -070029 mHalRestartCount(0),
30 mOnPropertySetValues()
31 {};
Keun-young Park28dd4702015-11-19 18:06:04 -080032
33 virtual void onEvents(sp<VehiclePropValueListHolder>& events) {
34 String8 msg("events ");
35 Mutex::Autolock autolock(mLock);
36 for (auto& e : events->getList()) {
37 ssize_t index = mEventCounts.indexOfKey(e->prop);
38 if (index < 0) {
39 mEventCounts.add(e->prop, 1); // 1st event
40 msg.appendFormat("0x%x:%d ", e->prop, 1);
41 } else {
42 int count = mEventCounts.valueAt(index);
43 count++;
44 mEventCounts.replaceValueAt(index, count);
45 msg.appendFormat("0x%x:%d ", e->prop, count);
46 }
47 }
48 msg.append("\n");
49 std::cout<<msg.string();
50 mCondition.signal();
51 }
52
53 virtual void onHalError(int32_t errorCode, int32_t property, int32_t operation) {
54 Mutex::Autolock autolock(mHalErrorLock);
55 mErrorCode = errorCode;
56 mProperty = property;
57 mOperation = operation;
58 mHalErrorCondition.signal();
59 }
60
Keun-young Park558e9b12016-02-26 13:52:45 -080061 virtual void onHalRestart(bool /*inMocking*/) {
Keun-young Park28dd4702015-11-19 18:06:04 -080062 Mutex::Autolock autolock(mHalRestartLock);
63 mHalRestartCount++;
64 mHalRestartCondition.signal();
65 }
66
Pavel Maltsevb0324b42016-09-27 21:00:41 -070067 virtual void onPropertySet(const vehicle_prop_value_t& value) {
68 Mutex::Autolock autolock(mOnPropertySetLock);
69
70 std::unique_ptr<ScopedVehiclePropValue> scopedValue(new ScopedVehiclePropValue);
Keun-young Park7151bb32016-10-12 21:14:19 -070071 VehiclePropValueUtil::copyVehiclePropValue(&scopedValue->value,
Pavel Maltsevb0324b42016-09-27 21:00:41 -070072 value);
73 mOnPropertySetValues.push(std::move(scopedValue));
74 mOnPropertySetCondition.signal();
75 }
76
Keun-young Park28dd4702015-11-19 18:06:04 -080077 void waitForEvents(nsecs_t reltime) {
78 Mutex::Autolock autolock(mLock);
79 mCondition.waitRelative(mLock, reltime);
80 }
81
82 bool waitForEvent(int32_t property, nsecs_t reltime) {
83 Mutex::Autolock autolock(mLock);
84 int startCount = getEventCountLocked(property);
85 int currentCount = startCount;
86 int64_t now = android::elapsedRealtimeNano();
87 int64_t endTime = now + reltime;
88 while ((startCount == currentCount) && (now < endTime)) {
89 mCondition.waitRelative(mLock, endTime - now);
90 currentCount = getEventCountLocked(property);
91 now = android::elapsedRealtimeNano();
92 }
93 return (startCount != currentCount);
94 }
95
96 int getEventCount(int32_t property) {
97 Mutex::Autolock autolock(mLock);
98 return getEventCountLocked(property);
99 }
100
101 int getHalRestartCount() {
102 Mutex::Autolock autolock(mHalRestartLock);
103 return mHalRestartCount;
104 }
105
106 void waitForHalRestart(nsecs_t reltime) {
107 Mutex::Autolock autolock(mHalRestartLock);
108 mHalRestartCondition.waitRelative(mHalRestartLock, reltime);
109 }
110
111 void waitForHalError(nsecs_t reltime) {
112 Mutex::Autolock autolock(mHalErrorLock);
113 mHalErrorCondition.waitRelative(mHalErrorLock, reltime);
114 }
115
Pavel Maltsevb0324b42016-09-27 21:00:41 -0700116 status_t waitForOnPropertySet(nsecs_t reltime, std::unique_ptr<ScopedVehiclePropValue>* out) {
117 Mutex::Autolock autolock(mOnPropertySetLock);
118
119 status_t r = mOnPropertySetValues.empty()
120 ? mOnPropertySetCondition.waitRelative(mOnPropertySetLock, reltime)
121 : NO_ERROR;
122
123 if (r == NO_ERROR) {
Keun-young Park7151bb32016-10-12 21:14:19 -0700124 VehiclePropValueUtil::copyVehiclePropValue(&(*out)->value, /* dest */
Pavel Maltsevb0324b42016-09-27 21:00:41 -0700125 mOnPropertySetValues.back()->value /* src */);
126 mOnPropertySetValues.pop();
127 }
128 return r;
129 }
130
Keun-young Park28dd4702015-11-19 18:06:04 -0800131 bool isErrorMatching(int32_t errorCode, int32_t property, int32_t operation) {
132 Mutex::Autolock autolock(mHalErrorLock);
133 return mErrorCode == errorCode && mProperty == property && mOperation == operation;
134 }
135
136private:
137 int getEventCountLocked(int32_t property) {
138 ssize_t index = mEventCounts.indexOfKey(property);
139 if (index < 0) {
140 return 0;
141 } else {
142 return mEventCounts.valueAt(index);
143 }
144 }
145
146
147private:
148 Mutex mLock;
149 Condition mCondition;
150 KeyedVector<int32_t, int> mEventCounts;
151
152 Mutex mHalRestartLock;
153 Condition mHalRestartCondition;
154 int mHalRestartCount;
155
156 Mutex mHalErrorLock;
157 Condition mHalErrorCondition;
158 int32_t mErrorCode;
159 int32_t mProperty;
160 int32_t mOperation;
Pavel Maltsevb0324b42016-09-27 21:00:41 -0700161
162 Mutex mOnPropertySetLock;
163 Condition mOnPropertySetCondition;
164 std::queue<std::unique_ptr<ScopedVehiclePropValue>> mOnPropertySetValues;
Keun-young Park28dd4702015-11-19 18:06:04 -0800165};
166
167}; // namespace android
Keun-young Parkaaeffaf2015-11-25 17:24:10 -0800168#endif // ANDROID_IVEHICLENETWORK_TEST_LISTER_H