Brian Stack | 60fcdcf | 2018-10-16 14:51:34 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2018 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 "Sensors.h" |
| 18 | |
Brian Stack | 897528d | 2018-10-23 10:38:03 -0700 | [diff] [blame] | 19 | #include <android/hardware/sensors/2.0/types.h> |
Brian Stack | 475d4d4 | 2018-10-19 15:58:09 -0700 | [diff] [blame] | 20 | #include <log/log.h> |
| 21 | |
Brian Stack | 60fcdcf | 2018-10-16 14:51:34 -0700 | [diff] [blame] | 22 | namespace android { |
| 23 | namespace hardware { |
| 24 | namespace sensors { |
| 25 | namespace V2_0 { |
| 26 | namespace implementation { |
| 27 | |
| 28 | using ::android::hardware::sensors::V1_0::Event; |
| 29 | using ::android::hardware::sensors::V1_0::OperationMode; |
| 30 | using ::android::hardware::sensors::V1_0::RateLevel; |
| 31 | using ::android::hardware::sensors::V1_0::Result; |
| 32 | using ::android::hardware::sensors::V1_0::SharedMemInfo; |
| 33 | |
Brian Stack | 2927ab7 | 2018-10-23 11:22:55 -0700 | [diff] [blame] | 34 | Sensors::Sensors() : mEventQueueFlag(nullptr) { |
| 35 | std::shared_ptr<AccelSensor> accel = |
| 36 | std::make_shared<AccelSensor>(1 /* sensorHandle */, this /* callback */); |
| 37 | mSensors[accel->getSensorInfo().sensorHandle] = accel; |
| 38 | } |
Brian Stack | 475d4d4 | 2018-10-19 15:58:09 -0700 | [diff] [blame] | 39 | |
| 40 | Sensors::~Sensors() { |
| 41 | deleteEventFlag(); |
| 42 | } |
| 43 | |
Brian Stack | 60fcdcf | 2018-10-16 14:51:34 -0700 | [diff] [blame] | 44 | // Methods from ::android::hardware::sensors::V2_0::ISensors follow. |
Brian Stack | 897528d | 2018-10-23 10:38:03 -0700 | [diff] [blame] | 45 | Return<void> Sensors::getSensorsList(getSensorsList_cb _hidl_cb) { |
| 46 | std::vector<SensorInfo> sensors; |
| 47 | for (const auto& sensor : mSensors) { |
| 48 | sensors.push_back(sensor.second->getSensorInfo()); |
| 49 | } |
| 50 | |
| 51 | // Call the HIDL callback with the SensorInfo |
| 52 | _hidl_cb(sensors); |
| 53 | |
Brian Stack | 60fcdcf | 2018-10-16 14:51:34 -0700 | [diff] [blame] | 54 | return Void(); |
| 55 | } |
| 56 | |
Brian Stack | d23f200 | 2018-11-05 13:48:16 -0800 | [diff] [blame^] | 57 | Return<Result> Sensors::setOperationMode(OperationMode mode) { |
| 58 | for (auto sensor : mSensors) { |
| 59 | sensor.second->setOperationMode(mode); |
| 60 | } |
| 61 | return Result::OK; |
Brian Stack | 60fcdcf | 2018-10-16 14:51:34 -0700 | [diff] [blame] | 62 | } |
| 63 | |
Brian Stack | 897528d | 2018-10-23 10:38:03 -0700 | [diff] [blame] | 64 | Return<Result> Sensors::activate(int32_t sensorHandle, bool enabled) { |
| 65 | auto sensor = mSensors.find(sensorHandle); |
| 66 | if (sensor != mSensors.end()) { |
| 67 | sensor->second->activate(enabled); |
| 68 | return Result::OK; |
| 69 | } |
| 70 | return Result::BAD_VALUE; |
Brian Stack | 60fcdcf | 2018-10-16 14:51:34 -0700 | [diff] [blame] | 71 | } |
| 72 | |
| 73 | Return<Result> Sensors::initialize( |
Brian Stack | 475d4d4 | 2018-10-19 15:58:09 -0700 | [diff] [blame] | 74 | const ::android::hardware::MQDescriptorSync<Event>& eventQueueDescriptor, |
| 75 | const ::android::hardware::MQDescriptorSync<uint32_t>& wakeLockDescriptor, |
| 76 | const sp<ISensorsCallback>& sensorsCallback) { |
| 77 | Result result = Result::OK; |
| 78 | |
| 79 | // Save a reference to the callback |
| 80 | mCallback = sensorsCallback; |
| 81 | |
| 82 | // Create the Event FMQ from the eventQueueDescriptor. Reset the read/write positions. |
| 83 | mEventQueue = |
| 84 | std::make_unique<EventMessageQueue>(eventQueueDescriptor, true /* resetPointers */); |
| 85 | |
| 86 | // Ensure that any existing EventFlag is properly deleted |
| 87 | deleteEventFlag(); |
| 88 | |
| 89 | // Create the EventFlag that is used to signal to the framework that sensor events have been |
| 90 | // written to the Event FMQ |
| 91 | if (EventFlag::createEventFlag(mEventQueue->getEventFlagWord(), &mEventQueueFlag) != OK) { |
| 92 | result = Result::BAD_VALUE; |
| 93 | } |
| 94 | |
| 95 | // Create the Wake Lock FMQ that is used by the framework to communicate whenever WAKE_UP |
| 96 | // events have been successfully read and handled by the framework. |
| 97 | mWakeLockQueue = |
| 98 | std::make_unique<WakeLockMessageQueue>(wakeLockDescriptor, true /* resetPointers */); |
| 99 | |
| 100 | if (!mCallback || !mEventQueue || !mWakeLockQueue || mEventQueueFlag == nullptr) { |
| 101 | result = Result::BAD_VALUE; |
| 102 | } |
| 103 | |
| 104 | return result; |
Brian Stack | 60fcdcf | 2018-10-16 14:51:34 -0700 | [diff] [blame] | 105 | } |
| 106 | |
Brian Stack | 897528d | 2018-10-23 10:38:03 -0700 | [diff] [blame] | 107 | Return<Result> Sensors::batch(int32_t sensorHandle, int64_t samplingPeriodNs, |
Brian Stack | 60fcdcf | 2018-10-16 14:51:34 -0700 | [diff] [blame] | 108 | int64_t /* maxReportLatencyNs */) { |
Brian Stack | 897528d | 2018-10-23 10:38:03 -0700 | [diff] [blame] | 109 | auto sensor = mSensors.find(sensorHandle); |
Brian Stack | 237abc6 | 2018-10-23 11:09:59 -0700 | [diff] [blame] | 110 | if (sensor != mSensors.end()) { |
| 111 | sensor->second->batch(samplingPeriodNs); |
| 112 | return Result::OK; |
Brian Stack | 897528d | 2018-10-23 10:38:03 -0700 | [diff] [blame] | 113 | } |
Brian Stack | 237abc6 | 2018-10-23 11:09:59 -0700 | [diff] [blame] | 114 | return Result::BAD_VALUE; |
Brian Stack | 60fcdcf | 2018-10-16 14:51:34 -0700 | [diff] [blame] | 115 | } |
| 116 | |
Brian Stack | e4f74c7 | 2018-10-29 11:51:46 -0700 | [diff] [blame] | 117 | Return<Result> Sensors::flush(int32_t sensorHandle) { |
| 118 | auto sensor = mSensors.find(sensorHandle); |
| 119 | if (sensor != mSensors.end()) { |
| 120 | return sensor->second->flush(); |
| 121 | } |
| 122 | return Result::BAD_VALUE; |
Brian Stack | 60fcdcf | 2018-10-16 14:51:34 -0700 | [diff] [blame] | 123 | } |
| 124 | |
Brian Stack | d23f200 | 2018-11-05 13:48:16 -0800 | [diff] [blame^] | 125 | Return<Result> Sensors::injectSensorData(const Event& event) { |
| 126 | auto sensor = mSensors.find(event.sensorHandle); |
| 127 | if (sensor != mSensors.end()) { |
| 128 | return sensor->second->injectEvent(event); |
| 129 | } |
| 130 | |
| 131 | return Result::BAD_VALUE; |
Brian Stack | 60fcdcf | 2018-10-16 14:51:34 -0700 | [diff] [blame] | 132 | } |
| 133 | |
| 134 | Return<void> Sensors::registerDirectChannel(const SharedMemInfo& /* mem */, |
Brian Stack | 9a407f2 | 2018-10-19 16:04:11 -0700 | [diff] [blame] | 135 | registerDirectChannel_cb _hidl_cb) { |
| 136 | _hidl_cb(Result::INVALID_OPERATION, 0 /* channelHandle */); |
| 137 | return Return<void>(); |
Brian Stack | 60fcdcf | 2018-10-16 14:51:34 -0700 | [diff] [blame] | 138 | } |
| 139 | |
| 140 | Return<Result> Sensors::unregisterDirectChannel(int32_t /* channelHandle */) { |
Brian Stack | 9a407f2 | 2018-10-19 16:04:11 -0700 | [diff] [blame] | 141 | return Result::INVALID_OPERATION; |
Brian Stack | 60fcdcf | 2018-10-16 14:51:34 -0700 | [diff] [blame] | 142 | } |
| 143 | |
| 144 | Return<void> Sensors::configDirectReport(int32_t /* sensorHandle */, int32_t /* channelHandle */, |
Brian Stack | 9a407f2 | 2018-10-19 16:04:11 -0700 | [diff] [blame] | 145 | RateLevel /* rate */, configDirectReport_cb _hidl_cb) { |
| 146 | _hidl_cb(Result::INVALID_OPERATION, 0 /* reportToken */); |
| 147 | return Return<void>(); |
Brian Stack | 60fcdcf | 2018-10-16 14:51:34 -0700 | [diff] [blame] | 148 | } |
| 149 | |
Brian Stack | 237abc6 | 2018-10-23 11:09:59 -0700 | [diff] [blame] | 150 | void Sensors::postEvents(const std::vector<Event>& events) { |
| 151 | std::lock_guard<std::mutex> l(mLock); |
| 152 | |
| 153 | // TODO: read events from the Wake Lock FMQ in the right place |
| 154 | std::vector<uint32_t> tmp(mWakeLockQueue->availableToRead()); |
| 155 | mWakeLockQueue->read(tmp.data(), mWakeLockQueue->availableToRead()); |
| 156 | |
| 157 | mEventQueue->write(events.data(), events.size()); |
| 158 | mEventQueueFlag->wake(static_cast<uint32_t>(EventQueueFlagBits::READ_AND_PROCESS)); |
| 159 | } |
| 160 | |
Brian Stack | 475d4d4 | 2018-10-19 15:58:09 -0700 | [diff] [blame] | 161 | void Sensors::deleteEventFlag() { |
| 162 | status_t status = EventFlag::deleteEventFlag(&mEventQueueFlag); |
| 163 | if (status != OK) { |
| 164 | ALOGI("Failed to delete event flag: %d", status); |
| 165 | } |
| 166 | } |
| 167 | |
Brian Stack | 60fcdcf | 2018-10-16 14:51:34 -0700 | [diff] [blame] | 168 | } // namespace implementation |
| 169 | } // namespace V2_0 |
| 170 | } // namespace sensors |
| 171 | } // namespace hardware |
| 172 | } // namespace android |