blob: f0b02c312556038d4cd9830925363a811cec8876 [file] [log] [blame]
Brian Stack60fcdcf2018-10-16 14:51:34 -07001/*
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 Stack897528d2018-10-23 10:38:03 -070019#include <android/hardware/sensors/2.0/types.h>
Brian Stack475d4d42018-10-19 15:58:09 -070020#include <log/log.h>
21
Brian Stack60fcdcf2018-10-16 14:51:34 -070022namespace android {
23namespace hardware {
24namespace sensors {
25namespace V2_0 {
26namespace implementation {
27
28using ::android::hardware::sensors::V1_0::Event;
29using ::android::hardware::sensors::V1_0::OperationMode;
30using ::android::hardware::sensors::V1_0::RateLevel;
31using ::android::hardware::sensors::V1_0::Result;
32using ::android::hardware::sensors::V1_0::SharedMemInfo;
33
Brian Stack475d4d42018-10-19 15:58:09 -070034Sensors::Sensors() : mEventQueueFlag(nullptr) {}
35
36Sensors::~Sensors() {
37 deleteEventFlag();
38}
39
Brian Stack60fcdcf2018-10-16 14:51:34 -070040// Methods from ::android::hardware::sensors::V2_0::ISensors follow.
Brian Stack897528d2018-10-23 10:38:03 -070041Return<void> Sensors::getSensorsList(getSensorsList_cb _hidl_cb) {
42 std::vector<SensorInfo> sensors;
43 for (const auto& sensor : mSensors) {
44 sensors.push_back(sensor.second->getSensorInfo());
45 }
46
47 // Call the HIDL callback with the SensorInfo
48 _hidl_cb(sensors);
49
Brian Stack60fcdcf2018-10-16 14:51:34 -070050 return Void();
51}
52
53Return<Result> Sensors::setOperationMode(OperationMode /* mode */) {
54 // TODO implement
55 return Result{};
56}
57
Brian Stack897528d2018-10-23 10:38:03 -070058Return<Result> Sensors::activate(int32_t sensorHandle, bool enabled) {
59 auto sensor = mSensors.find(sensorHandle);
60 if (sensor != mSensors.end()) {
61 sensor->second->activate(enabled);
62 return Result::OK;
63 }
64 return Result::BAD_VALUE;
Brian Stack60fcdcf2018-10-16 14:51:34 -070065}
66
67Return<Result> Sensors::initialize(
Brian Stack475d4d42018-10-19 15:58:09 -070068 const ::android::hardware::MQDescriptorSync<Event>& eventQueueDescriptor,
69 const ::android::hardware::MQDescriptorSync<uint32_t>& wakeLockDescriptor,
70 const sp<ISensorsCallback>& sensorsCallback) {
71 Result result = Result::OK;
72
73 // Save a reference to the callback
74 mCallback = sensorsCallback;
75
76 // Create the Event FMQ from the eventQueueDescriptor. Reset the read/write positions.
77 mEventQueue =
78 std::make_unique<EventMessageQueue>(eventQueueDescriptor, true /* resetPointers */);
79
80 // Ensure that any existing EventFlag is properly deleted
81 deleteEventFlag();
82
83 // Create the EventFlag that is used to signal to the framework that sensor events have been
84 // written to the Event FMQ
85 if (EventFlag::createEventFlag(mEventQueue->getEventFlagWord(), &mEventQueueFlag) != OK) {
86 result = Result::BAD_VALUE;
87 }
88
89 // Create the Wake Lock FMQ that is used by the framework to communicate whenever WAKE_UP
90 // events have been successfully read and handled by the framework.
91 mWakeLockQueue =
92 std::make_unique<WakeLockMessageQueue>(wakeLockDescriptor, true /* resetPointers */);
93
94 if (!mCallback || !mEventQueue || !mWakeLockQueue || mEventQueueFlag == nullptr) {
95 result = Result::BAD_VALUE;
96 }
97
98 return result;
Brian Stack60fcdcf2018-10-16 14:51:34 -070099}
100
Brian Stack897528d2018-10-23 10:38:03 -0700101Return<Result> Sensors::batch(int32_t sensorHandle, int64_t samplingPeriodNs,
Brian Stack60fcdcf2018-10-16 14:51:34 -0700102 int64_t /* maxReportLatencyNs */) {
Brian Stack897528d2018-10-23 10:38:03 -0700103 auto sensor = mSensors.find(sensorHandle);
Brian Stack237abc62018-10-23 11:09:59 -0700104 if (sensor != mSensors.end()) {
105 sensor->second->batch(samplingPeriodNs);
106 return Result::OK;
Brian Stack897528d2018-10-23 10:38:03 -0700107 }
Brian Stack237abc62018-10-23 11:09:59 -0700108 return Result::BAD_VALUE;
Brian Stack60fcdcf2018-10-16 14:51:34 -0700109}
110
111Return<Result> Sensors::flush(int32_t /* sensorHandle */) {
112 // TODO implement
113 return Result{};
114}
115
116Return<Result> Sensors::injectSensorData(const Event& /* event */) {
117 // TODO implement
118 return Result{};
119}
120
121Return<void> Sensors::registerDirectChannel(const SharedMemInfo& /* mem */,
Brian Stack9a407f22018-10-19 16:04:11 -0700122 registerDirectChannel_cb _hidl_cb) {
123 _hidl_cb(Result::INVALID_OPERATION, 0 /* channelHandle */);
124 return Return<void>();
Brian Stack60fcdcf2018-10-16 14:51:34 -0700125}
126
127Return<Result> Sensors::unregisterDirectChannel(int32_t /* channelHandle */) {
Brian Stack9a407f22018-10-19 16:04:11 -0700128 return Result::INVALID_OPERATION;
Brian Stack60fcdcf2018-10-16 14:51:34 -0700129}
130
131Return<void> Sensors::configDirectReport(int32_t /* sensorHandle */, int32_t /* channelHandle */,
Brian Stack9a407f22018-10-19 16:04:11 -0700132 RateLevel /* rate */, configDirectReport_cb _hidl_cb) {
133 _hidl_cb(Result::INVALID_OPERATION, 0 /* reportToken */);
134 return Return<void>();
Brian Stack60fcdcf2018-10-16 14:51:34 -0700135}
136
Brian Stack237abc62018-10-23 11:09:59 -0700137void Sensors::postEvents(const std::vector<Event>& events) {
138 std::lock_guard<std::mutex> l(mLock);
139
140 // TODO: read events from the Wake Lock FMQ in the right place
141 std::vector<uint32_t> tmp(mWakeLockQueue->availableToRead());
142 mWakeLockQueue->read(tmp.data(), mWakeLockQueue->availableToRead());
143
144 mEventQueue->write(events.data(), events.size());
145 mEventQueueFlag->wake(static_cast<uint32_t>(EventQueueFlagBits::READ_AND_PROCESS));
146}
147
Brian Stack475d4d42018-10-19 15:58:09 -0700148void Sensors::deleteEventFlag() {
149 status_t status = EventFlag::deleteEventFlag(&mEventQueueFlag);
150 if (status != OK) {
151 ALOGI("Failed to delete event flag: %d", status);
152 }
153}
154
Brian Stack60fcdcf2018-10-16 14:51:34 -0700155} // namespace implementation
156} // namespace V2_0
157} // namespace sensors
158} // namespace hardware
159} // namespace android