Anthony Stange | a689f8a | 2019-07-30 11:35:48 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2019 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 | #pragma once |
| 18 | |
| 19 | #include <android/hardware/sensors/1.0/types.h> |
| 20 | #include <android/hardware/sensors/2.0/ISensors.h> |
| 21 | |
| 22 | #include <vector> |
| 23 | |
| 24 | using ::android::hardware::sensors::V1_0::Event; |
| 25 | using ::android::hardware::sensors::V1_0::Result; |
| 26 | using ::android::hardware::sensors::V1_0::SensorInfo; |
| 27 | |
| 28 | // Indicates the current version of the multiHAL interface formatted as (HAL major version) << 24 | |
| 29 | // (HAL minor version) << 16 | (multiHAL version) |
| 30 | #define SUB_HAL_2_0_VERSION 0x02000000 |
| 31 | |
| 32 | namespace android { |
| 33 | namespace hardware { |
| 34 | namespace sensors { |
| 35 | namespace V2_0 { |
| 36 | namespace implementation { |
| 37 | |
| 38 | /** |
| 39 | * Wrapper around wake lock acquisition functions (acquire/release_wake_lock) that provides a |
| 40 | * RAII-style mechanism for keeping a wake lock held for the duration of a scoped block. |
| 41 | * When a ScopedWakelock is created, it increments the reference count stored in the HalProxy |
| 42 | * for the sub-HALs specific wake lock, acquiring the wake lock if necessary. When the object goes |
| 43 | * out of scope, the ref count is decremented, potentially releasing the wake lock if no other |
| 44 | * references to the wake lock exist. |
| 45 | * |
| 46 | * This class is allocated through the createScopedWakelock callback inside the IHalProxyCallback |
| 47 | * provided to sub-HALs during initialization and should be used for all wake lock acquisition |
| 48 | * inside of the sub-HAL to ensure wake locks are not held indefinitely. |
| 49 | * |
| 50 | * The most prevalent use case for this class will be for posting events to the framework through |
| 51 | * the postEvents HalProxy callback. The expectation is that sub-HALs will create this |
| 52 | * ScopedWakelock through the createScopedWakelock upon receiving a sensor events. The lock boolean |
| 53 | * provided to createScopedWakelock will be set the according to whether the sensor events are |
| 54 | * from wakeup sensors. Then, the sub-HAL will perform any processing necessary before invoking the |
| 55 | * postEvents callback passing in the previously created ScopedWakelock. At this point, ownership |
| 56 | * of the object will be passed to the HalProxy that will then be responsible for ensuring any |
| 57 | * wake locks continue to be held, if necessary. |
| 58 | */ |
| 59 | class ScopedWakelock { |
| 60 | public: |
| 61 | ScopedWakelock(ScopedWakelock&&) = default; |
| 62 | ScopedWakelock& operator=(ScopedWakelock&&) = default; |
| 63 | virtual ~ScopedWakelock() { mLocked = false; }; |
| 64 | |
| 65 | bool isLocked() const { return mLocked; } |
| 66 | |
| 67 | protected: |
| 68 | bool mLocked; |
| 69 | |
| 70 | private: |
| 71 | // TODO: Mark HalProxy's subclass of ScopedWakelock as a friend so that it can be initialized. |
| 72 | ScopedWakelock(); |
| 73 | ScopedWakelock(const ScopedWakelock&) = delete; |
| 74 | ScopedWakelock& operator=(const ScopedWakelock&) = delete; |
| 75 | }; |
| 76 | |
| 77 | /** |
| 78 | * Interface that contains several callbacks into the HalProxy class to communicate dynamic sensor |
| 79 | * changes and sensor events to the framework and acquire wake locks. The HalProxy will ensure |
| 80 | * callbacks occurring at the same time from multiple sub-HALs are synchronized in a safe, efficient |
| 81 | * manner. |
| 82 | */ |
| 83 | class IHalProxyCallback : public ISensorsCallback { |
| 84 | public: |
| 85 | /** |
| 86 | * Thread-safe callback used to post events to the HalProxy. Sub-HALs should invoke this |
| 87 | * whenever new sensor events need to be delivered to the sensors framework. Once invoked, the |
| 88 | * HalProxy will attempt to send events to the sensors framework using a blocking write with a |
| 89 | * 5 second timeout. This write may be done asynchronously if the queue used to communicate |
| 90 | * with the framework is full to avoid blocking sub-HALs for the length of the timeout. If the |
| 91 | * write fails, the events will be dropped and any wake locks held will be released. |
| 92 | * |
| 93 | * The provided ScopedWakelock must be locked if the events are from wakeup sensors. If it's |
| 94 | * not locked accordingly, the HalProxy will crash as this indicates the sub-HAL isn't compliant |
| 95 | * with the sensors HAL 2.0 specification. Additionally, since ScopedWakelock isn't copyable, |
| 96 | * the HalProxy will take ownership of the wake lock given when this method is invoked. Once the |
| 97 | * method returns, the HalProxy will handle holding the wake lock, if necessary, until the |
| 98 | * framework has successfully processed any wakeup events. |
| 99 | * |
| 100 | * No return type is used for this callback to avoid sub-HALs trying to resend events when |
| 101 | * writes fail. Writes should only fail when the framework is under inordinate stress which will |
| 102 | * likely result in a framework restart so retrying will likely only result in overloading the |
| 103 | * HalProxy. Sub-HALs should always assume that the write was a success and perform any |
| 104 | * necessary cleanup. Additionally, the HalProxy will ensure it logs any errors (through ADB and |
| 105 | * bug reports) it encounters during delivery to ensure it's obvious that a failure occurred. |
| 106 | * |
| 107 | * @param events the events that should be sent to the sensors framework |
| 108 | * @param wakelock ScopedWakelock that should be locked to send events from wake sensors and |
| 109 | * unlocked otherwise. |
| 110 | */ |
| 111 | virtual void postEvents(const std::vector<Event>& events, ScopedWakelock wakelock) = 0; |
| 112 | |
| 113 | /** |
| 114 | * Initializes a ScopedWakelock on the stack that, when locked, will increment the reference |
| 115 | * count for the sub-HAL's wake lock managed inside the HalProxy. See the ScopedWakelock class |
| 116 | * definition for how it should be used. |
| 117 | * |
| 118 | * @param lock whether the ScopedWakelock should be locked before it's returned. |
| 119 | * @return the created ScopedWakelock |
| 120 | */ |
| 121 | virtual ScopedWakelock createScopedWakelock(bool lock) = 0; |
| 122 | }; |
| 123 | |
| 124 | /** |
| 125 | * ISensorsSubHal is an interface that sub-HALs must implement in order to be compliant with |
| 126 | * multihal 2.0 and in order for the HalProxy to successfully load and communicate with the sub-HAL. |
| 127 | * |
| 128 | * Any vendor wishing to implement this interface and support multihal 2.0 will need to create a |
| 129 | * dynamic library that exposes sensorsHalGetSubHal (defined below). This library will be loaded by |
| 130 | * the HalProxy when the sensors HAL is initialized and then the HalProxy will retrieve the vendor's |
| 131 | * implementation of sensorsHalGetSubHal. |
| 132 | * |
| 133 | * With the exception of the initialize method, ISensorsSubHal will implement the ISensors.hal spec. |
| 134 | * Any sensor handles given to the HalProxy, either through getSensorsList() or the |
| 135 | * onDynamicSensors(Dis)Connected callbacks, will be translated to avoid clashing with other sub-HAL |
| 136 | * handles. To achieve this, the HalProxy will use the upper byte to store the sub-HAL index and |
| 137 | * sub-HALs can continue to use the lower 3 bytes of the handle. |
| 138 | */ |
| 139 | class ISensorsSubHal : public ISensors { |
Stan Rokita | dc7a8e7 | 2019-08-23 12:35:40 -0700 | [diff] [blame^] | 140 | public: |
Anthony Stange | a689f8a | 2019-07-30 11:35:48 -0400 | [diff] [blame] | 141 | // The ISensors version of initialize isn't used for multihal. Instead, sub-HALs must implement |
| 142 | // the version below to allow communciation logic to centralized in the HalProxy |
| 143 | Return<Result> initialize( |
| 144 | const ::android::hardware::MQDescriptorSync<Event>& /* eventQueueDescriptor */, |
| 145 | const ::android::hardware::MQDescriptorSync<uint32_t>& /* wakeLockDescriptor */, |
| 146 | const sp<ISensorsCallback>& /* sensorsCallback */) final { |
| 147 | return Result::INVALID_OPERATION; |
| 148 | } |
| 149 | |
| 150 | /** |
| 151 | * Method defined in ::android::hidl::base::V1_0::IBase. |
| 152 | * |
| 153 | * This method should write debug information to hidl_handle that is useful for debugging |
| 154 | * issues. Suggestions include: |
| 155 | * - Sensor info including handle values and any other state available in the SensorInfo class |
| 156 | * - List of active sensors and their current sampling period and reporting latency |
| 157 | * - Information about pending flush requests |
| 158 | * - Current operating mode |
| 159 | * - Currently registered direct channel info |
| 160 | * - A history of any of the above |
| 161 | */ |
| 162 | virtual Return<void> debug(const hidl_handle& fd, const hidl_vec<hidl_string>& args) = 0; |
| 163 | |
| 164 | /** |
| 165 | * @return A human-readable name for use in wake locks and logging. |
| 166 | */ |
Anthony Stange | c34e668 | 2019-08-09 11:24:17 -0400 | [diff] [blame] | 167 | virtual const std::string getName() = 0; |
Anthony Stange | a689f8a | 2019-07-30 11:35:48 -0400 | [diff] [blame] | 168 | |
| 169 | /** |
| 170 | * First method invoked on the sub-HAL after it's allocated through sensorsHalGetSubHal() by the |
| 171 | * HalProxy. Sub-HALs should use this to initialize any state and retain the callback given in |
| 172 | * order to communicate with the HalProxy. |
| 173 | * |
| 174 | * @param halProxyCallback callback used to inform the HalProxy when a dynamic sensor's state |
| 175 | * changes, new sensor events should be sent to the framework, and when a new ScopedWakelock |
| 176 | * should be created. |
| 177 | * @return result OK on success |
| 178 | */ |
| 179 | virtual Return<Result> initialize(const sp<IHalProxyCallback>& halProxyCallback) = 0; |
| 180 | }; |
| 181 | |
| 182 | } // namespace implementation |
| 183 | } // namespace V2_0 |
| 184 | } // namespace sensors |
| 185 | } // namespace hardware |
| 186 | } // namespace android |
| 187 | |
| 188 | using ::android::hardware::sensors::V2_0::implementation::ISensorsSubHal; |
| 189 | |
| 190 | /** |
| 191 | * Function that must be exported so the HalProxy class can invoke it on the sub-HAL dynamic |
| 192 | * library. This function will only be invoked once at initialization time. |
| 193 | * |
| 194 | * NOTE: The supported sensors HAL version must match SUB_HAL_2_0_VERSION exactly or the HalProxy |
| 195 | * will fail to initialize. |
| 196 | * |
| 197 | * @param uint32_t when this function returns, this parameter must contain the HAL version that |
| 198 | * this sub-HAL supports. To support this version of multi-HAL, this must be set to |
| 199 | * SUB_HAL_2_0_VERSION. |
| 200 | * @return A statically allocated, valid ISensorsSubHal implementation. |
| 201 | */ |
| 202 | __attribute__((visibility("default"))) extern "C" ISensorsSubHal* sensorsHalGetSubHal( |
| 203 | uint32_t* version); |