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 ANDROID_VEHICLE_NETWORK_H |
| 18 | #define ANDROID_VEHICLE_NETWORK_H |
| 19 | |
| 20 | #include <stdint.h> |
| 21 | #include <sys/types.h> |
| 22 | |
keunyoung | e18e25d | 2015-08-28 15:57:19 -0700 | [diff] [blame] | 23 | #include <binder/IInterface.h> |
| 24 | #include <binder/IMemory.h> |
| 25 | |
Keun-young Park | 28dd470 | 2015-11-19 18:06:04 -0800 | [diff] [blame] | 26 | #include <utils/threads.h> |
| 27 | #include <utils/Errors.h> |
| 28 | #include <utils/List.h> |
| 29 | #include <utils/RefBase.h> |
| 30 | |
keunyoung | e18e25d | 2015-08-28 15:57:19 -0700 | [diff] [blame] | 31 | #include "IVehicleNetwork.h" |
Keun-young Park | 28dd470 | 2015-11-19 18:06:04 -0800 | [diff] [blame] | 32 | #include "HandlerThread.h" |
keunyoung | e18e25d | 2015-08-28 15:57:19 -0700 | [diff] [blame] | 33 | |
| 34 | namespace android { |
| 35 | |
| 36 | // ---------------------------------------------------------------------------- |
| 37 | |
| 38 | /** |
| 39 | * Listener for client to implement to get events from Vehicle network service. |
| 40 | */ |
| 41 | class VehicleNetworkListener : public RefBase |
| 42 | { |
| 43 | public: |
| 44 | VehicleNetworkListener() {}; |
| 45 | virtual ~VehicleNetworkListener() {}; |
| 46 | virtual void onEvents(sp<VehiclePropValueListHolder>& events) = 0; |
Keun-young Park | 28dd470 | 2015-11-19 18:06:04 -0800 | [diff] [blame] | 47 | virtual void onHalError(int32_t errorCode, int32_t property, int32_t operation) = 0; |
| 48 | virtual void onHalRestart(bool inMocking) = 0; |
Pavel Maltsev | b0324b4 | 2016-09-27 21:00:41 -0700 | [diff] [blame] | 49 | virtual void onPropertySet(const vehicle_prop_value_t& value) = 0; |
keunyoung | e18e25d | 2015-08-28 15:57:19 -0700 | [diff] [blame] | 50 | }; |
| 51 | |
| 52 | // ---------------------------------------------------------------------------- |
Keun-young Park | 28dd470 | 2015-11-19 18:06:04 -0800 | [diff] [blame] | 53 | |
| 54 | /** For internal event handling, not for client */ |
| 55 | class VehicleNetworkEventMessageHandler : public MessageHandler { |
| 56 | enum { |
| 57 | EVENT_EVENTS = 0, |
| 58 | EVENT_HAL_ERROR = 1, |
| 59 | EVENT_HAL_RESTART = 2, |
Pavel Maltsev | b0324b4 | 2016-09-27 21:00:41 -0700 | [diff] [blame] | 60 | EVENT_ON_SET = 3, |
Keun-young Park | 28dd470 | 2015-11-19 18:06:04 -0800 | [diff] [blame] | 61 | }; |
| 62 | public: |
| 63 | VehicleNetworkEventMessageHandler(const sp<Looper>& looper, |
| 64 | sp<VehicleNetworkListener>& listener); |
| 65 | virtual ~VehicleNetworkEventMessageHandler(); |
| 66 | |
| 67 | void handleHalEvents(sp<VehiclePropValueListHolder>& events); |
| 68 | void handleHalError(int32_t errorCode, int32_t property, int32_t operation); |
Keun-young Park | aaeffaf | 2015-11-25 17:24:10 -0800 | [diff] [blame] | 69 | /** |
| 70 | * This error must be handled always. This can be called in vehicle network service's crash |
| 71 | * as well. |
| 72 | */ |
Keun-young Park | 28dd470 | 2015-11-19 18:06:04 -0800 | [diff] [blame] | 73 | void handleHalRestart(bool inMocking); |
| 74 | |
Pavel Maltsev | b0324b4 | 2016-09-27 21:00:41 -0700 | [diff] [blame] | 75 | void handleOnPropertySet(const vehicle_prop_value_t& value); |
| 76 | |
Keun-young Park | 28dd470 | 2015-11-19 18:06:04 -0800 | [diff] [blame] | 77 | private: |
| 78 | virtual void handleMessage(const Message& message); |
| 79 | void doHandleHalEvents(); |
| 80 | void doHandleHalError(); |
| 81 | void doHandleHalRestart(); |
Pavel Maltsev | b0324b4 | 2016-09-27 21:00:41 -0700 | [diff] [blame] | 82 | void doHandleOnPropertySet(); |
Keun-young Park | 28dd470 | 2015-11-19 18:06:04 -0800 | [diff] [blame] | 83 | private: |
| 84 | mutable Mutex mLock; |
| 85 | sp<Looper> mLooper; |
| 86 | sp<VehicleNetworkListener>& mListener; |
| 87 | List<sp<VehiclePropValueListHolder>> mEvents; |
| 88 | List<VehicleHalError*> mHalErrors; |
| 89 | List<bool> mHalRestartEvents; |
Pavel Maltsev | b0324b4 | 2016-09-27 21:00:41 -0700 | [diff] [blame] | 90 | List<vehicle_prop_value_t*> mSetValueEvents; |
Keun-young Park | 28dd470 | 2015-11-19 18:06:04 -0800 | [diff] [blame] | 91 | }; |
| 92 | |
| 93 | // ---------------------------------------------------------------------------- |
| 94 | |
keunyoung | e18e25d | 2015-08-28 15:57:19 -0700 | [diff] [blame] | 95 | /** |
| 96 | * Vehicle network API for low level components like HALs to access / control car information. |
| 97 | * This is reference counted. So use with sp<>. |
| 98 | */ |
| 99 | class VehicleNetwork : public IBinder::DeathRecipient, public BnVehicleNetworkListener |
| 100 | { |
| 101 | public: |
| 102 | /** |
| 103 | * Factory method for VehicleNetwork. Client should use this method to create |
| 104 | * a new instance. |
| 105 | */ |
| 106 | static sp<VehicleNetwork> createVehicleNetwork(sp<VehicleNetworkListener> &listener); |
| 107 | |
| 108 | virtual ~VehicleNetwork(); |
| 109 | |
| 110 | /** Set int32 value */ |
| 111 | status_t setInt32Property(int32_t property, int32_t value); |
| 112 | /** get int32 value */ |
| 113 | status_t getInt32Property(int32_t property, int32_t* value, int64_t* timestamp); |
| 114 | status_t setInt64Property(int32_t property, int64_t value); |
| 115 | status_t getInt64Property(int32_t property, int64_t* value, int64_t* timestamp); |
| 116 | status_t setFloatProperty(int32_t property, float value); |
| 117 | status_t getFloatProperty(int32_t property, float* value, int64_t* timestamp); |
| 118 | status_t setStringProperty(int32_t property, const String8& value); |
| 119 | status_t getStringProperty(int32_t property, String8& value, int64_t* timestamp); |
| 120 | sp<VehiclePropertiesHolder> listProperties(int32_t property = 0); |
| 121 | /** For generic value setting. At least prop, value_type, and value should be set. */ |
| 122 | status_t setProperty(const vehicle_prop_value_t& value); |
| 123 | /** For generic value getting. value->prop should be set. */ |
| 124 | status_t getProperty(vehicle_prop_value_t* value); |
Pavel Maltsev | b0324b4 | 2016-09-27 21:00:41 -0700 | [diff] [blame] | 125 | status_t subscribe(int32_t property, float sampleRate, int32_t zones = 0, |
| 126 | int32_t flags = SubscribeFlags::DEFAULT); |
keunyoung | e18e25d | 2015-08-28 15:57:19 -0700 | [diff] [blame] | 127 | void unsubscribe(int32_t property); |
| 128 | |
Keun-young Park | 28dd470 | 2015-11-19 18:06:04 -0800 | [diff] [blame] | 129 | // Only for testing purpose |
| 130 | status_t injectEvent(const vehicle_prop_value_t& value); |
| 131 | |
| 132 | // starting / stopping mocking not added yet. |
| 133 | status_t startMocking(const sp<IVehicleNetworkHalMock>& mock); |
| 134 | void stopMocking(const sp<IVehicleNetworkHalMock>& mock); |
| 135 | |
| 136 | // only for testing |
| 137 | status_t injectHalError(int32_t errorCode, int32_t property, int32_t operation); |
| 138 | |
| 139 | status_t startErrorListening(); |
| 140 | void stopErrorListening(); |
| 141 | |
keunyoung | e18e25d | 2015-08-28 15:57:19 -0700 | [diff] [blame] | 142 | //IBinder::DeathRecipient, not for client |
| 143 | void binderDied(const wp<IBinder>& who); |
| 144 | // BnVehicleNetworkListener, not for client |
Keun-young Park | 28dd470 | 2015-11-19 18:06:04 -0800 | [diff] [blame] | 145 | void onEvents(sp<VehiclePropValueListHolder>& events); |
| 146 | void onHalError(int32_t errorCode, int32_t property, int32_t operation); |
| 147 | void onHalRestart(bool inMocking); |
Pavel Maltsev | b0324b4 | 2016-09-27 21:00:41 -0700 | [diff] [blame] | 148 | void onPropertySet(const vehicle_prop_value_t& value); |
keunyoung | e18e25d | 2015-08-28 15:57:19 -0700 | [diff] [blame] | 149 | |
| 150 | private: |
| 151 | VehicleNetwork(sp<IVehicleNetwork>& vehicleNetwork, sp<VehicleNetworkListener> &listener); |
Keun-young Park | 28dd470 | 2015-11-19 18:06:04 -0800 | [diff] [blame] | 152 | // RefBase |
| 153 | virtual void onFirstRef(); |
Keun-young Park | aaeffaf | 2015-11-25 17:24:10 -0800 | [diff] [blame] | 154 | sp<IVehicleNetwork> getService(); |
| 155 | sp<VehicleNetworkEventMessageHandler> getEventHandler(); |
keunyoung | e18e25d | 2015-08-28 15:57:19 -0700 | [diff] [blame] | 156 | |
| 157 | private: |
| 158 | sp<IVehicleNetwork> mService; |
| 159 | sp<VehicleNetworkListener> mClientListener; |
| 160 | Mutex mLock; |
Keun-young Park | 28dd470 | 2015-11-19 18:06:04 -0800 | [diff] [blame] | 161 | sp<HandlerThread> mHandlerThread; |
| 162 | sp<VehicleNetworkEventMessageHandler> mEventHandler; |
keunyoung | e18e25d | 2015-08-28 15:57:19 -0700 | [diff] [blame] | 163 | }; |
| 164 | |
| 165 | // ---------------------------------------------------------------------------- |
| 166 | |
| 167 | }; // namespace android |
| 168 | |
| 169 | #endif /* ANDROID_VEHICLE_NETWORK_H */ |
| 170 | |