keunyoung | e18e25d | 2015-08-28 15:57:19 -0700 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | #ifndef CAR_VEHICLE_NETWORK_SERVICE_H_ |
| 18 | #define CAR_VEHICLE_NETWORK_SERVICE_H_ |
| 19 | |
Keun-young Park | 97e46eb | 2016-04-09 15:02:38 -0700 | [diff] [blame] | 20 | #include <inttypes.h> |
Pavel Maltsev | b0324b4 | 2016-09-27 21:00:41 -0700 | [diff] [blame] | 21 | #include <stdint.h> |
keunyoung | e18e25d | 2015-08-28 15:57:19 -0700 | [diff] [blame] | 22 | #include <sys/types.h> |
Pavel Maltsev | b0324b4 | 2016-09-27 21:00:41 -0700 | [diff] [blame] | 23 | #include <unordered_set> |
keunyoung | e18e25d | 2015-08-28 15:57:19 -0700 | [diff] [blame] | 24 | |
| 25 | #include <memory> |
| 26 | |
| 27 | #include <hardware/hardware.h> |
| 28 | #include <hardware/vehicle.h> |
| 29 | |
| 30 | #include <binder/BinderService.h> |
| 31 | #include <binder/IBinder.h> |
| 32 | #include <binder/IPCThreadState.h> |
| 33 | #include <cutils/compiler.h> |
| 34 | #include <utils/threads.h> |
| 35 | #include <utils/KeyedVector.h> |
| 36 | #include <utils/List.h> |
| 37 | #include <utils/RefBase.h> |
| 38 | #include <utils/SortedVector.h> |
| 39 | #include <utils/StrongPointer.h> |
| 40 | #include <utils/TypeHelpers.h> |
| 41 | |
| 42 | #include <IVehicleNetwork.h> |
| 43 | #include <IVehicleNetworkListener.h> |
Keun-young Park | 28dd470 | 2015-11-19 18:06:04 -0800 | [diff] [blame] | 44 | #include <HandlerThread.h> |
keunyoung | e18e25d | 2015-08-28 15:57:19 -0700 | [diff] [blame] | 45 | |
Sam Hurst | c7152db | 2016-02-29 09:35:22 -0800 | [diff] [blame] | 46 | #include "VehiclePropertyAccessControl.h" |
keunyoung | e18e25d | 2015-08-28 15:57:19 -0700 | [diff] [blame] | 47 | |
| 48 | namespace android { |
| 49 | |
Keun-young Park | 28dd470 | 2015-11-19 18:06:04 -0800 | [diff] [blame] | 50 | // ---------------------------------------------------------------------------- |
| 51 | |
keunyoung | e18e25d | 2015-08-28 15:57:19 -0700 | [diff] [blame] | 52 | class VehicleNetworkService; |
| 53 | |
| 54 | /** |
| 55 | * MessageHandler to dispatch HAL callbacks to pre-defined handler thread context. |
| 56 | * Init / release is handled in the handler thread to allow upper layer to allocate resource |
| 57 | * for the thread. |
| 58 | */ |
| 59 | class VehicleHalMessageHandler : public MessageHandler { |
| 60 | enum { |
keunyoung | e4c90c4 | 2015-11-16 18:42:52 -0800 | [diff] [blame] | 61 | HAL_EVENT = 0, |
| 62 | HAL_ERROR = 1, |
keunyoung | e18e25d | 2015-08-28 15:57:19 -0700 | [diff] [blame] | 63 | }; |
| 64 | |
| 65 | /** |
| 66 | * For dispatching HAL event in batch. Hal events coming in this time frame will be batched |
| 67 | * together. |
| 68 | */ |
| 69 | static const int DISPATCH_INTERVAL_MS = 16; |
Keun-young Park | 28dd470 | 2015-11-19 18:06:04 -0800 | [diff] [blame] | 70 | static const int NUM_PROPERTY_EVENT_LISTS = 2; |
keunyoung | e18e25d | 2015-08-28 15:57:19 -0700 | [diff] [blame] | 71 | public: |
Keun-young Park | 28dd470 | 2015-11-19 18:06:04 -0800 | [diff] [blame] | 72 | // not passing VNS as sp as this is held by VNS always. |
keunyoung | e18e25d | 2015-08-28 15:57:19 -0700 | [diff] [blame] | 73 | VehicleHalMessageHandler(const sp<Looper>& mLooper, VehicleNetworkService& service); |
| 74 | virtual ~VehicleHalMessageHandler(); |
| 75 | |
keunyoung | e18e25d | 2015-08-28 15:57:19 -0700 | [diff] [blame] | 76 | void handleHalEvent(vehicle_prop_value_t *eventData); |
Keun-young Park | 28dd470 | 2015-11-19 18:06:04 -0800 | [diff] [blame] | 77 | void handleHalError(VehicleHalError* error); |
Keun-young Park | 7151bb3 | 2016-10-12 21:14:19 -0700 | [diff] [blame] | 78 | void handleMockStateChange(); |
Keun-young Park | 97e46eb | 2016-04-09 15:02:38 -0700 | [diff] [blame] | 79 | void dump(String8& msg); |
keunyoung | e18e25d | 2015-08-28 15:57:19 -0700 | [diff] [blame] | 80 | |
| 81 | private: |
| 82 | void handleMessage(const Message& message); |
keunyoung | e18e25d | 2015-08-28 15:57:19 -0700 | [diff] [blame] | 83 | void doHandleHalEvent(); |
| 84 | void doHandleHalError(); |
| 85 | |
| 86 | private: |
Keun-young Park | 28dd470 | 2015-11-19 18:06:04 -0800 | [diff] [blame] | 87 | mutable Mutex mLock; |
Keun-young Park | 28dd470 | 2015-11-19 18:06:04 -0800 | [diff] [blame] | 88 | const sp<Looper> mLooper; |
keunyoung | e18e25d | 2015-08-28 15:57:19 -0700 | [diff] [blame] | 89 | VehicleNetworkService& mService; |
keunyoung | e18e25d | 2015-08-28 15:57:19 -0700 | [diff] [blame] | 90 | int mFreeListIndex; |
Keun-young Park | 28dd470 | 2015-11-19 18:06:04 -0800 | [diff] [blame] | 91 | List<vehicle_prop_value_t*> mHalPropertyList[NUM_PROPERTY_EVENT_LISTS]; |
keunyoung | e18e25d | 2015-08-28 15:57:19 -0700 | [diff] [blame] | 92 | int64_t mLastDispatchTime; |
Keun-young Park | 28dd470 | 2015-11-19 18:06:04 -0800 | [diff] [blame] | 93 | List<VehicleHalError*> mHalErrors; |
keunyoung | e18e25d | 2015-08-28 15:57:19 -0700 | [diff] [blame] | 94 | }; |
Keun-young Park | 0727f95 | 2015-12-21 14:30:07 -0800 | [diff] [blame] | 95 | // ---------------------------------------------------------------------------- |
| 96 | class SubscriptionInfo { |
| 97 | public: |
| 98 | float sampleRate; |
| 99 | int32_t zones; |
Pavel Maltsev | b0324b4 | 2016-09-27 21:00:41 -0700 | [diff] [blame] | 100 | int32_t flags; |
Keun-young Park | 0727f95 | 2015-12-21 14:30:07 -0800 | [diff] [blame] | 101 | SubscriptionInfo() |
| 102 | : sampleRate(0), |
Pavel Maltsev | b0324b4 | 2016-09-27 21:00:41 -0700 | [diff] [blame] | 103 | zones(0), |
| 104 | flags(SubscribeFlags::DEFAULT) {}; |
| 105 | SubscriptionInfo(float aSampleRate, int32_t aZones, int32_t aFlags) |
Keun-young Park | 0727f95 | 2015-12-21 14:30:07 -0800 | [diff] [blame] | 106 | : sampleRate(aSampleRate), |
Pavel Maltsev | b0324b4 | 2016-09-27 21:00:41 -0700 | [diff] [blame] | 107 | zones(aZones), |
| 108 | flags(aFlags) {}; |
Keun-young Park | 0727f95 | 2015-12-21 14:30:07 -0800 | [diff] [blame] | 109 | SubscriptionInfo(const SubscriptionInfo& info) |
| 110 | : sampleRate(info.sampleRate), |
Pavel Maltsev | b0324b4 | 2016-09-27 21:00:41 -0700 | [diff] [blame] | 111 | zones(info.zones), |
| 112 | flags(info.flags) {}; |
Keun-young Park | 0727f95 | 2015-12-21 14:30:07 -0800 | [diff] [blame] | 113 | }; |
keunyoung | e18e25d | 2015-08-28 15:57:19 -0700 | [diff] [blame] | 114 | |
| 115 | // ---------------------------------------------------------------------------- |
Keun-young Park | 6748b41 | 2016-04-07 15:51:09 -0700 | [diff] [blame] | 116 | class EventInfo { |
| 117 | public: |
| 118 | int64_t lastTimestamp; |
| 119 | int eventCount; |
| 120 | EventInfo() |
| 121 | : lastTimestamp(0), |
| 122 | eventCount(0) {}; |
| 123 | EventInfo(int64_t aLastTimestamp, int aEventCount) |
| 124 | : lastTimestamp(aLastTimestamp), |
| 125 | eventCount(aEventCount) {}; |
| 126 | EventInfo(const EventInfo& info) |
| 127 | : lastTimestamp(info.lastTimestamp), |
| 128 | eventCount(info.eventCount) {}; |
| 129 | }; |
| 130 | // ---------------------------------------------------------------------------- |
keunyoung | e18e25d | 2015-08-28 15:57:19 -0700 | [diff] [blame] | 131 | |
| 132 | class HalClient : public virtual RefBase { |
| 133 | public: |
Keun-young Park | 28dd470 | 2015-11-19 18:06:04 -0800 | [diff] [blame] | 134 | HalClient(const sp<IVehicleNetworkListener> &listener, pid_t pid, uid_t uid) : |
| 135 | mListener(listener), |
| 136 | mPid(pid), |
| 137 | mUid(uid), |
| 138 | mMonitoringHalRestart(false), |
Keun-young Park | 97e46eb | 2016-04-09 15:02:38 -0700 | [diff] [blame] | 139 | mMonitoringHalError(false), |
| 140 | mLastDispatchedEventCounts(0), |
| 141 | mTotalDispatchedEvents(0), |
| 142 | mLastDispatchTime(0) { |
keunyoung | e18e25d | 2015-08-28 15:57:19 -0700 | [diff] [blame] | 143 | } |
| 144 | |
Keun-young Park | 7151bb3 | 2016-10-12 21:14:19 -0700 | [diff] [blame] | 145 | virtual ~HalClient() { |
Keun-young Park | 0727f95 | 2015-12-21 14:30:07 -0800 | [diff] [blame] | 146 | mSubscriptionInfos.clear(); |
keunyoung | e18e25d | 2015-08-28 15:57:19 -0700 | [diff] [blame] | 147 | } |
| 148 | |
| 149 | pid_t getPid() { |
| 150 | return mPid; |
| 151 | } |
| 152 | |
| 153 | uid_t getUid() { |
| 154 | return mUid; |
| 155 | } |
| 156 | |
Keun-young Park | 0727f95 | 2015-12-21 14:30:07 -0800 | [diff] [blame] | 157 | SubscriptionInfo* getSubscriptionInfo(int32_t property) { |
keunyoung | e18e25d | 2015-08-28 15:57:19 -0700 | [diff] [blame] | 158 | Mutex::Autolock autoLock(mLock); |
Keun-young Park | 0727f95 | 2015-12-21 14:30:07 -0800 | [diff] [blame] | 159 | ssize_t index = mSubscriptionInfos.indexOfKey(property); |
keunyoung | e18e25d | 2015-08-28 15:57:19 -0700 | [diff] [blame] | 160 | if (index < 0) { |
Keun-young Park | 0727f95 | 2015-12-21 14:30:07 -0800 | [diff] [blame] | 161 | return NULL; |
keunyoung | e18e25d | 2015-08-28 15:57:19 -0700 | [diff] [blame] | 162 | } |
Keun-young Park | 0727f95 | 2015-12-21 14:30:07 -0800 | [diff] [blame] | 163 | return &mSubscriptionInfos.editValueAt(index); |
keunyoung | e18e25d | 2015-08-28 15:57:19 -0700 | [diff] [blame] | 164 | } |
| 165 | |
Pavel Maltsev | b0324b4 | 2016-09-27 21:00:41 -0700 | [diff] [blame] | 166 | void setSubscriptionInfo(int32_t property, float sampleRate, int32_t zones, int32_t flags) { |
keunyoung | e18e25d | 2015-08-28 15:57:19 -0700 | [diff] [blame] | 167 | Mutex::Autolock autoLock(mLock); |
Pavel Maltsev | b0324b4 | 2016-09-27 21:00:41 -0700 | [diff] [blame] | 168 | SubscriptionInfo info(sampleRate, zones, flags); |
Keun-young Park | 0727f95 | 2015-12-21 14:30:07 -0800 | [diff] [blame] | 169 | mSubscriptionInfos.add(property, info); |
keunyoung | e18e25d | 2015-08-28 15:57:19 -0700 | [diff] [blame] | 170 | } |
| 171 | |
| 172 | bool removePropertyAndCheckIfActive(int32_t property) { |
Keun-young Park | 28dd470 | 2015-11-19 18:06:04 -0800 | [diff] [blame] | 173 | Mutex::Autolock autoLock(mLock); |
Keun-young Park | 0727f95 | 2015-12-21 14:30:07 -0800 | [diff] [blame] | 174 | mSubscriptionInfos.removeItem(property); |
| 175 | return mSubscriptionInfos.size() > 0 || mMonitoringHalRestart || mMonitoringHalError; |
Keun-young Park | 28dd470 | 2015-11-19 18:06:04 -0800 | [diff] [blame] | 176 | } |
| 177 | |
| 178 | void removeAllProperties() { |
| 179 | Mutex::Autolock autoLock(mLock); |
Keun-young Park | 0727f95 | 2015-12-21 14:30:07 -0800 | [diff] [blame] | 180 | mSubscriptionInfos.clear(); |
Keun-young Park | 28dd470 | 2015-11-19 18:06:04 -0800 | [diff] [blame] | 181 | } |
| 182 | |
| 183 | bool isActive() { |
| 184 | Mutex::Autolock autoLock(mLock); |
Keun-young Park | 0727f95 | 2015-12-21 14:30:07 -0800 | [diff] [blame] | 185 | return mSubscriptionInfos.size() > 0 || mMonitoringHalRestart || mMonitoringHalError; |
Keun-young Park | 28dd470 | 2015-11-19 18:06:04 -0800 | [diff] [blame] | 186 | } |
| 187 | |
| 188 | void setHalRestartMonitoringState(bool state) { |
| 189 | Mutex::Autolock autoLock(mLock); |
| 190 | mMonitoringHalRestart = state; |
| 191 | } |
| 192 | |
| 193 | bool isMonitoringHalRestart() { |
| 194 | Mutex::Autolock autoLock(mLock); |
| 195 | return mMonitoringHalRestart; |
| 196 | } |
| 197 | |
| 198 | void setHalErrorMonitoringState(bool state) { |
| 199 | Mutex::Autolock autoLock(mLock); |
| 200 | mMonitoringHalError = state; |
| 201 | } |
| 202 | |
| 203 | bool isMonitoringHalError() { |
| 204 | Mutex::Autolock autoLock(mLock); |
| 205 | return mMonitoringHalError; |
keunyoung | e18e25d | 2015-08-28 15:57:19 -0700 | [diff] [blame] | 206 | } |
| 207 | |
| 208 | const sp<IVehicleNetworkListener>& getListener() { |
| 209 | return mListener; |
| 210 | } |
| 211 | |
| 212 | const sp<IBinder> getListenerAsBinder() { |
| 213 | return IInterface::asBinder(mListener); |
| 214 | } |
| 215 | |
| 216 | // no lock here as this should be called only from single event looper thread |
| 217 | void addEvent(vehicle_prop_value_t* event) { |
| 218 | mEvents.push_back(event); |
| 219 | } |
| 220 | |
| 221 | // no lock here as this should be called only from single event looper thread |
| 222 | void clearEvents() { |
| 223 | mEvents.clear(); |
| 224 | } |
| 225 | |
| 226 | // no lock here as this should be called only from single event looper thread |
| 227 | List<vehicle_prop_value_t*>& getEventList() { |
| 228 | return mEvents; |
| 229 | } |
| 230 | |
| 231 | // no lock here as this should be called only from single event looper thread |
Pavel Maltsev | b0324b4 | 2016-09-27 21:00:41 -0700 | [diff] [blame] | 232 | status_t dispatchEvents(const int64_t& timestamp) { |
Pavel Maltsev | e531473 | 2016-06-28 16:51:26 -0700 | [diff] [blame] | 233 | ALOGV("dispatchEvents, num Events:%zu", mEvents.size()); |
keunyoung | e18e25d | 2015-08-28 15:57:19 -0700 | [diff] [blame] | 234 | sp<VehiclePropValueListHolder> events(new VehiclePropValueListHolder(&mEvents, |
| 235 | false /*deleteInDestructor */)); |
| 236 | ASSERT_OR_HANDLE_NO_MEMORY(events.get(), return NO_MEMORY); |
Keun-young Park | 97e46eb | 2016-04-09 15:02:38 -0700 | [diff] [blame] | 237 | mLastDispatchTime = timestamp; |
| 238 | mLastDispatchedEventCounts = mEvents.size(); |
| 239 | mTotalDispatchedEvents += mLastDispatchedEventCounts; |
keunyoung | e18e25d | 2015-08-28 15:57:19 -0700 | [diff] [blame] | 240 | mListener->onEvents(events); |
| 241 | mEvents.clear(); |
| 242 | return NO_ERROR; |
| 243 | } |
Keun-young Park | 28dd470 | 2015-11-19 18:06:04 -0800 | [diff] [blame] | 244 | |
| 245 | void dispatchHalError(int32_t errorCode, int32_t property, int32_t operation) { |
| 246 | mListener->onHalError(errorCode, property, operation); |
| 247 | } |
| 248 | |
| 249 | void dispatchHalRestart(bool inMocking) { |
| 250 | mListener->onHalRestart(inMocking); |
| 251 | } |
| 252 | |
Pavel Maltsev | b0324b4 | 2016-09-27 21:00:41 -0700 | [diff] [blame] | 253 | void dispatchPropertySetEvent(const vehicle_prop_value_t& value) { |
| 254 | mListener->onPropertySet(value); |
| 255 | } |
| 256 | |
Keun-young Park | 97e46eb | 2016-04-09 15:02:38 -0700 | [diff] [blame] | 257 | void dump(String8& msg) { |
| 258 | msg.appendFormat("pid:%d, uid:%d, mLastDispatchedEventCounts:%d, mTotalDispatchedEvents:%d" |
| 259 | ", mLastDispatchTime:%" PRId64 "\n", |
| 260 | mPid, mUid, mLastDispatchedEventCounts, mTotalDispatchedEvents, mLastDispatchTime); |
| 261 | } |
| 262 | |
keunyoung | e18e25d | 2015-08-28 15:57:19 -0700 | [diff] [blame] | 263 | private: |
Keun-young Park | 28dd470 | 2015-11-19 18:06:04 -0800 | [diff] [blame] | 264 | mutable Mutex mLock; |
| 265 | const sp<IVehicleNetworkListener> mListener; |
| 266 | const pid_t mPid; |
| 267 | const uid_t mUid; |
Keun-young Park | 0727f95 | 2015-12-21 14:30:07 -0800 | [diff] [blame] | 268 | KeyedVector<int32_t, SubscriptionInfo> mSubscriptionInfos; |
keunyoung | e18e25d | 2015-08-28 15:57:19 -0700 | [diff] [blame] | 269 | List<vehicle_prop_value_t*> mEvents; |
Keun-young Park | 28dd470 | 2015-11-19 18:06:04 -0800 | [diff] [blame] | 270 | bool mMonitoringHalRestart; |
| 271 | bool mMonitoringHalError; |
Keun-young Park | 97e46eb | 2016-04-09 15:02:38 -0700 | [diff] [blame] | 272 | int mLastDispatchedEventCounts; |
| 273 | int mTotalDispatchedEvents; |
| 274 | int64_t mLastDispatchTime; |
keunyoung | e18e25d | 2015-08-28 15:57:19 -0700 | [diff] [blame] | 275 | }; |
| 276 | |
| 277 | class HalClientSpVector : public SortedVector<sp<HalClient> >, public RefBase { |
Keun-young Park | 7151bb3 | 2016-10-12 21:14:19 -0700 | [diff] [blame] | 278 | public: |
| 279 | virtual ~HalClientSpVector() {}; |
keunyoung | e18e25d | 2015-08-28 15:57:19 -0700 | [diff] [blame] | 280 | protected: |
| 281 | virtual int do_compare(const void* lhs, const void* rhs) const { |
| 282 | sp<HalClient>& lh = * (sp<HalClient> * )(lhs); |
| 283 | sp<HalClient>& rh = * (sp<HalClient> * )(rhs); |
| 284 | return compare_type(lh.get(), rh.get()); |
| 285 | } |
| 286 | }; |
| 287 | |
| 288 | // ---------------------------------------------------------------------------- |
| 289 | |
keunyoung | d32f4e6 | 2015-09-21 11:33:06 -0700 | [diff] [blame] | 290 | /** |
| 291 | * Keeps cached value of property values. |
| 292 | * For internal property, static property, and on_change property, caching makes sense. |
| 293 | */ |
| 294 | class PropertyValueCache { |
| 295 | public: |
| 296 | PropertyValueCache(); |
| 297 | virtual ~PropertyValueCache(); |
| 298 | void writeToCache(const vehicle_prop_value_t& value); |
| 299 | bool readFromCache(vehicle_prop_value_t* value); |
| 300 | |
| 301 | private: |
| 302 | KeyedVector<int32_t, vehicle_prop_value_t*> mCache; |
| 303 | }; |
| 304 | |
| 305 | // ---------------------------------------------------------------------------- |
| 306 | |
Keun-young Park | 28dd470 | 2015-11-19 18:06:04 -0800 | [diff] [blame] | 307 | class MockDeathHandler: public IBinder::DeathRecipient { |
| 308 | public: |
| 309 | MockDeathHandler(VehicleNetworkService& vns) : |
| 310 | mService(vns) {}; |
Keun-young Park | 7151bb3 | 2016-10-12 21:14:19 -0700 | [diff] [blame] | 311 | virtual ~MockDeathHandler() {}; |
Keun-young Park | 28dd470 | 2015-11-19 18:06:04 -0800 | [diff] [blame] | 312 | virtual void binderDied(const wp<IBinder>& who); |
| 313 | |
| 314 | private: |
| 315 | VehicleNetworkService& mService; |
| 316 | }; |
| 317 | |
| 318 | // ---------------------------------------------------------------------------- |
keunyoung | e18e25d | 2015-08-28 15:57:19 -0700 | [diff] [blame] | 319 | class VehicleNetworkService : |
| 320 | public BinderService<VehicleNetworkService>, |
| 321 | public BnVehicleNetwork, |
| 322 | public IBinder::DeathRecipient { |
| 323 | public: |
| 324 | static const char* getServiceName() ANDROID_API { return IVehicleNetwork::SERVICE_NAME; }; |
| 325 | |
| 326 | VehicleNetworkService(); |
Keun-young Park | 7151bb3 | 2016-10-12 21:14:19 -0700 | [diff] [blame] | 327 | virtual ~VehicleNetworkService(); |
keunyoung | e18e25d | 2015-08-28 15:57:19 -0700 | [diff] [blame] | 328 | virtual status_t dump(int fd, const Vector<String16>& args); |
| 329 | void release(); |
Keun-young Park | 7151bb3 | 2016-10-12 21:14:19 -0700 | [diff] [blame] | 330 | status_t onHalEvent(const vehicle_prop_value_t *eventData, bool isInjection = false, |
| 331 | bool doCopy = true); |
Keun-young Park | 28dd470 | 2015-11-19 18:06:04 -0800 | [diff] [blame] | 332 | status_t onHalError(int32_t errorCode, int32_t property, int32_t operation, |
| 333 | bool isInjection = false); |
Pavel Maltsev | b0324b4 | 2016-09-27 21:00:41 -0700 | [diff] [blame] | 334 | status_t onPropertySet(const vehicle_prop_value_t& eventData); |
keunyoung | e18e25d | 2015-08-28 15:57:19 -0700 | [diff] [blame] | 335 | /** |
| 336 | * Called by VehicleHalMessageHandler for batching events |
| 337 | */ |
Keun-young Park | 28dd470 | 2015-11-19 18:06:04 -0800 | [diff] [blame] | 338 | void dispatchHalEvents(List<vehicle_prop_value_t*>& events); |
| 339 | void dispatchHalError(VehicleHalError* error); |
keunyoung | e18e25d | 2015-08-28 15:57:19 -0700 | [diff] [blame] | 340 | virtual sp<VehiclePropertiesHolder> listProperties(int32_t property = 0); |
| 341 | virtual status_t setProperty(const vehicle_prop_value_t& value); |
| 342 | virtual status_t getProperty(vehicle_prop_value_t* value); |
Keun-young Park | 71b2f5c | 2016-03-10 18:44:40 -0800 | [diff] [blame] | 343 | virtual void releaseMemoryFromGet(vehicle_prop_value_t* value); |
keunyoung | e18e25d | 2015-08-28 15:57:19 -0700 | [diff] [blame] | 344 | virtual status_t subscribe(const sp<IVehicleNetworkListener> &listener, int32_t property, |
Pavel Maltsev | b0324b4 | 2016-09-27 21:00:41 -0700 | [diff] [blame] | 345 | float sampleRate, int32_t zones, int32_t flags = SubscribeFlags::DEFAULT); |
keunyoung | e18e25d | 2015-08-28 15:57:19 -0700 | [diff] [blame] | 346 | virtual void unsubscribe(const sp<IVehicleNetworkListener> &listener, int32_t property); |
keunyoung | 1ab8e18 | 2015-09-24 09:25:22 -0700 | [diff] [blame] | 347 | virtual status_t injectEvent(const vehicle_prop_value_t& value); |
| 348 | virtual status_t startMocking(const sp<IVehicleNetworkHalMock>& mock); |
| 349 | virtual void stopMocking(const sp<IVehicleNetworkHalMock>& mock); |
Keun-young Park | 28dd470 | 2015-11-19 18:06:04 -0800 | [diff] [blame] | 350 | virtual status_t injectHalError(int32_t errorCode, int32_t property, int32_t operation); |
| 351 | virtual status_t startErrorListening(const sp<IVehicleNetworkListener> &listener); |
| 352 | virtual void stopErrorListening(const sp<IVehicleNetworkListener> &listener); |
| 353 | virtual status_t startHalRestartMonitoring(const sp<IVehicleNetworkListener> &listener); |
| 354 | virtual void stopHalRestartMonitoring(const sp<IVehicleNetworkListener> &listener); |
keunyoung | e18e25d | 2015-08-28 15:57:19 -0700 | [diff] [blame] | 355 | virtual void binderDied(const wp<IBinder>& who); |
keunyoung | d32f4e6 | 2015-09-21 11:33:06 -0700 | [diff] [blame] | 356 | bool isPropertySubsribed(int32_t property); |
Keun-young Park | 28dd470 | 2015-11-19 18:06:04 -0800 | [diff] [blame] | 357 | |
| 358 | void handleHalMockDeath(const wp<IBinder>& who); |
Keun-young Park | 737cdfb | 2016-02-12 15:11:39 -0800 | [diff] [blame] | 359 | protected: |
| 360 | virtual bool isOperationAllowed(int32_t property, bool isWrite); |
keunyoung | e18e25d | 2015-08-28 15:57:19 -0700 | [diff] [blame] | 361 | private: |
| 362 | // RefBase |
| 363 | virtual void onFirstRef(); |
| 364 | status_t loadHal(); |
| 365 | void closeHal(); |
keunyoung | 1ab8e18 | 2015-09-24 09:25:22 -0700 | [diff] [blame] | 366 | vehicle_prop_config_t const * findConfigLocked(int32_t property); |
| 367 | bool isGettableLocked(int32_t property); |
keunyoung | 38ba530 | 2015-10-14 13:50:21 -0700 | [diff] [blame] | 368 | bool isSettableLocked(int32_t property, int32_t valueType); |
keunyoung | 1ab8e18 | 2015-09-24 09:25:22 -0700 | [diff] [blame] | 369 | bool isSubscribableLocked(int32_t property); |
Pavel Maltsev | dfccc5a | 2016-04-11 21:56:04 -0700 | [diff] [blame] | 370 | status_t getProperty(vehicle_prop_value_t *data, bool retry); |
Keun-young Park | 0727f95 | 2015-12-21 14:30:07 -0800 | [diff] [blame] | 371 | static bool isZonedProperty(vehicle_prop_config_t const * config); |
Keun-young Park | 28dd470 | 2015-11-19 18:06:04 -0800 | [diff] [blame] | 372 | sp<HalClient> findClientLocked(sp<IBinder>& ibinder); |
| 373 | sp<HalClient> findOrCreateClientLocked(sp<IBinder>& ibinder, |
| 374 | const sp<IVehicleNetworkListener> &listener); |
| 375 | sp<HalClientSpVector> findClientsVectorForPropertyLocked(int32_t property); |
| 376 | sp<HalClientSpVector> findOrCreateClientsVectorForPropertyLocked(int32_t property); |
| 377 | bool removePropertyFromClientLocked(sp<IBinder>& ibinder, sp<HalClient>& client, |
| 378 | int32_t property); |
Pavel Maltsev | b0324b4 | 2016-09-27 21:00:41 -0700 | [diff] [blame] | 379 | bool hasClientsSubscribedToSetCallLocked(int32_t property, |
| 380 | const sp<HalClientSpVector> &clientsForProperty) const; |
Keun-young Park | 28dd470 | 2015-11-19 18:06:04 -0800 | [diff] [blame] | 381 | void handleHalRestartAndGetClientsToDispatchLocked(List<sp<HalClient> >& clientsToDispatch); |
Pavel Maltsev | dfccc5a | 2016-04-11 21:56:04 -0700 | [diff] [blame] | 382 | status_t notifyClientWithCurrentValue(bool isMocking, const vehicle_prop_config_t *config, |
| 383 | int32_t zones); |
| 384 | status_t notifyClientWithCurrentValue(bool isMocking, int32_t prop, int32_t valueType, |
| 385 | int32_t zone); |
Pavel Maltsev | b0324b4 | 2016-09-27 21:00:41 -0700 | [diff] [blame] | 386 | void dispatchPropertySetEvent(const vehicle_prop_value_t& data, |
| 387 | const sp<HalClientSpVector>& propertyClients); |
Keun-young Park | 28dd470 | 2015-11-19 18:06:04 -0800 | [diff] [blame] | 388 | |
keunyoung | e18e25d | 2015-08-28 15:57:19 -0700 | [diff] [blame] | 389 | static int eventCallback(const vehicle_prop_value_t *eventData); |
keunyoung | 38ba530 | 2015-10-14 13:50:21 -0700 | [diff] [blame] | 390 | static int errorCallback(int32_t errorCode, int32_t property, int32_t operation); |
keunyoung | e18e25d | 2015-08-28 15:57:19 -0700 | [diff] [blame] | 391 | private: |
Keun-young Park | e78bf53 | 2016-04-25 18:59:22 -0700 | [diff] [blame] | 392 | static const int GET_SET_WAIT_TIME_US = 100000; |
| 393 | static const int MAX_GET_SET_RETRY_NUMBER_FOR_NOT_READY = 20; |
Keun-young Park | 71b2f5c | 2016-03-10 18:44:40 -0800 | [diff] [blame] | 394 | |
Sam Hurst | c7152db | 2016-02-29 09:35:22 -0800 | [diff] [blame] | 395 | VehiclePropertyAccessControl mVehiclePropertyAccessControl; |
keunyoung | e18e25d | 2015-08-28 15:57:19 -0700 | [diff] [blame] | 396 | static VehicleNetworkService* sInstance; |
Keun-young Park | 28dd470 | 2015-11-19 18:06:04 -0800 | [diff] [blame] | 397 | sp<HandlerThread> mHandlerThread; |
keunyoung | e18e25d | 2015-08-28 15:57:19 -0700 | [diff] [blame] | 398 | sp<VehicleHalMessageHandler> mHandler; |
| 399 | mutable Mutex mLock; |
| 400 | vehicle_module_t* mModule; |
| 401 | vehicle_hw_device_t* mDevice; |
| 402 | sp<VehiclePropertiesHolder> mProperties; |
| 403 | KeyedVector<sp<IBinder>, sp<HalClient> > mBinderToClientMap; |
Keun-young Park | 28dd470 | 2015-11-19 18:06:04 -0800 | [diff] [blame] | 404 | // client subscribing properties |
keunyoung | e18e25d | 2015-08-28 15:57:19 -0700 | [diff] [blame] | 405 | KeyedVector<int32_t, sp<HalClientSpVector> > mPropertyToClientsMap; |
Keun-young Park | 0727f95 | 2015-12-21 14:30:07 -0800 | [diff] [blame] | 406 | KeyedVector<int32_t, SubscriptionInfo> mSubscriptionInfos; |
Keun-young Park | 6748b41 | 2016-04-07 15:51:09 -0700 | [diff] [blame] | 407 | KeyedVector<int32_t, EventInfo> mEventInfos; |
Pavel Maltsev | b0324b4 | 2016-09-27 21:00:41 -0700 | [diff] [blame] | 408 | std::unordered_set<int32_t> mPropertiesSubscribedToSetCall; |
keunyoung | d32f4e6 | 2015-09-21 11:33:06 -0700 | [diff] [blame] | 409 | PropertyValueCache mCache; |
keunyoung | 1ab8e18 | 2015-09-24 09:25:22 -0700 | [diff] [blame] | 410 | bool mMockingEnabled; |
Keun-young Park | 97e46eb | 2016-04-09 15:02:38 -0700 | [diff] [blame] | 411 | int mDroppedEventsWhileInMocking; |
| 412 | int64_t mLastEventDropTimeWhileInMocking; |
keunyoung | 1ab8e18 | 2015-09-24 09:25:22 -0700 | [diff] [blame] | 413 | sp<IVehicleNetworkHalMock> mHalMock; |
| 414 | sp<VehiclePropertiesHolder> mPropertiesForMocking; |
Keun-young Park | 28dd470 | 2015-11-19 18:06:04 -0800 | [diff] [blame] | 415 | sp<MockDeathHandler> mHalMockDeathHandler; |
keunyoung | e18e25d | 2015-08-28 15:57:19 -0700 | [diff] [blame] | 416 | }; |
| 417 | |
| 418 | }; |
| 419 | |
| 420 | #endif /* CAR_VEHICLE_NETWORK_SERVICE_H_ */ |