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 | #include "HalProxy.h" |
| 18 | |
| 19 | #include <android/hardware/sensors/2.0/types.h> |
| 20 | |
| 21 | namespace android { |
| 22 | namespace hardware { |
| 23 | namespace sensors { |
| 24 | namespace V2_0 { |
| 25 | namespace implementation { |
| 26 | |
| 27 | // TODO: Use this wake lock name as the prefix to all sensors HAL wake locks acquired. |
| 28 | // constexpr const char* kWakeLockName = "SensorsHAL_WAKEUP"; |
| 29 | |
| 30 | // TODO: Use the following class as a starting point for implementing the full HalProxyCallback |
| 31 | // along with being inspiration for how to implement the ScopedWakelock class. |
| 32 | /** |
| 33 | * Callback class used to provide the HalProxy with the index of which subHal is invoking |
| 34 | */ |
| 35 | class SensorsCallbackProxy : public ISensorsCallback { |
| 36 | public: |
| 37 | SensorsCallbackProxy(wp<HalProxy>& halProxy, int32_t subHalIndex) |
| 38 | : mHalProxy(halProxy), mSubHalIndex(subHalIndex) {} |
| 39 | |
| 40 | Return<void> onDynamicSensorsConnected( |
| 41 | const hidl_vec<SensorInfo>& dynamicSensorsAdded) override { |
| 42 | sp<HalProxy> halProxy(mHalProxy.promote()); |
| 43 | if (halProxy != nullptr) { |
| 44 | return halProxy->onDynamicSensorsConnected(dynamicSensorsAdded, mSubHalIndex); |
| 45 | } |
| 46 | return Return<void>(); |
| 47 | } |
| 48 | |
| 49 | Return<void> onDynamicSensorsDisconnected( |
| 50 | const hidl_vec<int32_t>& dynamicSensorHandlesRemoved) override { |
| 51 | sp<HalProxy> halProxy(mHalProxy.promote()); |
| 52 | if (halProxy != nullptr) { |
| 53 | return halProxy->onDynamicSensorsDisconnected(dynamicSensorHandlesRemoved, |
| 54 | mSubHalIndex); |
| 55 | } |
| 56 | return Return<void>(); |
| 57 | } |
| 58 | |
| 59 | private: |
| 60 | wp<HalProxy>& mHalProxy; |
| 61 | int32_t mSubHalIndex; |
| 62 | }; |
| 63 | |
| 64 | HalProxy::HalProxy() { |
| 65 | // TODO: Initialize all sub-HALs and discover sensors. |
| 66 | } |
| 67 | |
| 68 | HalProxy::~HalProxy() { |
| 69 | // TODO: Join any running threads and clean up FMQs and any other allocated |
| 70 | // state. |
| 71 | } |
| 72 | |
| 73 | Return<void> HalProxy::getSensorsList(getSensorsList_cb /* _hidl_cb */) { |
| 74 | // TODO: Output sensors list created as part of HalProxy(). |
| 75 | return Void(); |
| 76 | } |
| 77 | |
| 78 | Return<Result> HalProxy::setOperationMode(OperationMode /* mode */) { |
| 79 | // TODO: Proxy API call to all sub-HALs and return appropriate result. |
| 80 | return Result::INVALID_OPERATION; |
| 81 | } |
| 82 | |
| 83 | Return<Result> HalProxy::activate(int32_t /* sensorHandle */, bool /* enabled */) { |
| 84 | // TODO: Proxy API call to appropriate sub-HAL. |
| 85 | return Result::INVALID_OPERATION; |
| 86 | } |
| 87 | |
| 88 | Return<Result> HalProxy::initialize( |
| 89 | const ::android::hardware::MQDescriptorSync<Event>& eventQueueDescriptor, |
| 90 | const ::android::hardware::MQDescriptorSync<uint32_t>& wakeLockDescriptor, |
| 91 | const sp<ISensorsCallback>& sensorsCallback) { |
| 92 | Result result = Result::OK; |
| 93 | |
| 94 | // TODO: clean up sensor requests, if not already done elsewhere through a death recipient, and |
| 95 | // clean up any other resources that exist (FMQs, flags, threads, etc.) |
| 96 | |
| 97 | mDynamicSensorsCallback = sensorsCallback; |
| 98 | |
| 99 | // Create the Event FMQ from the eventQueueDescriptor. Reset the read/write positions. |
| 100 | mEventQueue = |
| 101 | std::make_unique<EventMessageQueue>(eventQueueDescriptor, true /* resetPointers */); |
| 102 | |
| 103 | // Create the EventFlag that is used to signal to the framework that sensor events have been |
| 104 | // written to the Event FMQ |
| 105 | if (EventFlag::createEventFlag(mEventQueue->getEventFlagWord(), &mEventQueueFlag) != OK) { |
| 106 | result = Result::BAD_VALUE; |
| 107 | } |
| 108 | |
| 109 | // Create the Wake Lock FMQ that is used by the framework to communicate whenever WAKE_UP |
| 110 | // events have been successfully read and handled by the framework. |
| 111 | mWakeLockQueue = |
| 112 | std::make_unique<WakeLockMessageQueue>(wakeLockDescriptor, true /* resetPointers */); |
| 113 | |
| 114 | if (!mDynamicSensorsCallback || !mEventQueue || !mWakeLockQueue || mEventQueueFlag == nullptr) { |
| 115 | result = Result::BAD_VALUE; |
| 116 | } |
| 117 | |
| 118 | // TODO: start threads to read wake locks and process events from sub HALs. |
| 119 | |
| 120 | return result; |
| 121 | } |
| 122 | |
| 123 | Return<Result> HalProxy::batch(int32_t /* sensorHandle */, int64_t /* samplingPeriodNs */, |
| 124 | int64_t /* maxReportLatencyNs */) { |
| 125 | // TODO: Proxy API call to appropriate sub-HAL. |
| 126 | return Result::INVALID_OPERATION; |
| 127 | } |
| 128 | |
| 129 | Return<Result> HalProxy::flush(int32_t /* sensorHandle */) { |
| 130 | // TODO: Proxy API call to appropriate sub-HAL. |
| 131 | return Result::INVALID_OPERATION; |
| 132 | } |
| 133 | |
| 134 | Return<Result> HalProxy::injectSensorData(const Event& /* event */) { |
| 135 | // TODO: Proxy API call to appropriate sub-HAL. |
| 136 | return Result::INVALID_OPERATION; |
| 137 | } |
| 138 | |
| 139 | Return<void> HalProxy::registerDirectChannel(const SharedMemInfo& /* mem */, |
| 140 | registerDirectChannel_cb _hidl_cb) { |
| 141 | // TODO: During init, discover the first sub-HAL in the config that has sensors with direct |
| 142 | // channel support, if any, and proxy the API call there. |
| 143 | _hidl_cb(Result::INVALID_OPERATION, -1 /* channelHandle */); |
| 144 | return Return<void>(); |
| 145 | } |
| 146 | |
| 147 | Return<Result> HalProxy::unregisterDirectChannel(int32_t /* channelHandle */) { |
| 148 | // TODO: During init, discover the first sub-HAL in the config that has sensors with direct |
| 149 | // channel support, if any, and proxy the API call there. |
| 150 | return Result::INVALID_OPERATION; |
| 151 | } |
| 152 | |
| 153 | Return<void> HalProxy::configDirectReport(int32_t /* sensorHandle */, int32_t /* channelHandle */, |
| 154 | RateLevel /* rate */, configDirectReport_cb _hidl_cb) { |
| 155 | // TODO: During init, discover the first sub-HAL in the config that has sensors with direct |
| 156 | // channel support, if any, and proxy the API call there. |
| 157 | _hidl_cb(Result::INVALID_OPERATION, 0 /* reportToken */); |
| 158 | return Return<void>(); |
| 159 | } |
| 160 | |
| 161 | Return<void> HalProxy::debug(const hidl_handle& /* fd */, const hidl_vec<hidl_string>& /* args */) { |
| 162 | // TODO: output debug information |
| 163 | return Return<void>(); |
| 164 | } |
| 165 | |
| 166 | Return<void> HalProxy::onDynamicSensorsConnected( |
| 167 | const hidl_vec<SensorInfo>& /* dynamicSensorsAdded */, int32_t /* subHalIndex */) { |
| 168 | // TODO: Map the SensorInfo to the global list and then invoke the framework's callback. |
| 169 | return Return<void>(); |
| 170 | } |
| 171 | |
| 172 | Return<void> HalProxy::onDynamicSensorsDisconnected( |
| 173 | const hidl_vec<int32_t>& /* dynamicSensorHandlesRemoved */, int32_t /* subHalIndex */) { |
| 174 | // TODO: Unmap the SensorInfo from the global list and then invoke the framework's callback. |
| 175 | return Return<void>(); |
| 176 | } |
| 177 | |
| 178 | } // namespace implementation |
| 179 | } // namespace V2_0 |
| 180 | } // namespace sensors |
| 181 | } // namespace hardware |
| 182 | } // namespace android |