blob: b9855a6ac5b6ea0e915ef561a1ff02e2495d1c1f [file] [log] [blame]
Anthony Stangea689f8a2019-07-30 11:35:48 -04001/*
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 "SubHal.h"
20
21#include <android/hardware/sensors/2.0/ISensors.h>
22#include <fmq/MessageQueue.h>
23#include <hardware_legacy/power.h>
24#include <hidl/MQDescriptor.h>
25#include <hidl/Status.h>
26
27namespace android {
28namespace hardware {
29namespace sensors {
30namespace V2_0 {
31namespace implementation {
32
33using ::android::sp;
34using ::android::hardware::EventFlag;
35using ::android::hardware::hidl_string;
36using ::android::hardware::hidl_vec;
37using ::android::hardware::MessageQueue;
38using ::android::hardware::MQDescriptor;
39using ::android::hardware::Return;
40using ::android::hardware::Void;
41
42struct HalProxy : public ISensors {
43 using Event = ::android::hardware::sensors::V1_0::Event;
44 using OperationMode = ::android::hardware::sensors::V1_0::OperationMode;
45 using RateLevel = ::android::hardware::sensors::V1_0::RateLevel;
46 using Result = ::android::hardware::sensors::V1_0::Result;
47 using SharedMemInfo = ::android::hardware::sensors::V1_0::SharedMemInfo;
48
49 HalProxy();
50 ~HalProxy();
51
52 // Methods from ::android::hardware::sensors::V2_0::ISensors follow.
53 Return<void> getSensorsList(getSensorsList_cb _hidl_cb) override;
54
55 Return<Result> setOperationMode(OperationMode mode) override;
56
57 Return<Result> activate(int32_t sensorHandle, bool enabled) override;
58
59 Return<Result> initialize(
60 const ::android::hardware::MQDescriptorSync<Event>& eventQueueDescriptor,
61 const ::android::hardware::MQDescriptorSync<uint32_t>& wakeLockDescriptor,
62 const sp<ISensorsCallback>& sensorsCallback) override;
63
64 Return<Result> batch(int32_t sensorHandle, int64_t samplingPeriodNs,
65 int64_t maxReportLatencyNs) override;
66
67 Return<Result> flush(int32_t sensorHandle) override;
68
69 Return<Result> injectSensorData(const Event& event) override;
70
71 Return<void> registerDirectChannel(const SharedMemInfo& mem,
72 registerDirectChannel_cb _hidl_cb) override;
73
74 Return<Result> unregisterDirectChannel(int32_t channelHandle) override;
75
76 Return<void> configDirectReport(int32_t sensorHandle, int32_t channelHandle, RateLevel rate,
77 configDirectReport_cb _hidl_cb) override;
78
79 Return<void> debug(const hidl_handle& fd, const hidl_vec<hidl_string>& args) override;
80
81 // Below methods from ::android::hardware::sensors::V2_0::ISensorsCaback with a minor change
82 // to pass in the sub-HAL index. While the above methods are invoked from the sensors framework
83 // via the binder, these methods are invoked from a callback provided to sub-HALs inside the
84 // same process as the HalProxy, but potentially running on different threads.
85 Return<void> onDynamicSensorsConnected(const hidl_vec<SensorInfo>& dynamicSensorsAdded,
86 int32_t subHalIndex);
87
88 Return<void> onDynamicSensorsDisconnected(const hidl_vec<int32_t>& dynamicSensorHandlesRemoved,
89 int32_t subHalIndex);
90
91 private:
92 using EventMessageQueue = MessageQueue<Event, kSynchronizedReadWrite>;
93 using WakeLockMessageQueue = MessageQueue<uint32_t, kSynchronizedReadWrite>;
94
95 /**
96 * The Event FMQ where sensor events are written
97 */
98 std::unique_ptr<EventMessageQueue> mEventQueue;
99
100 /**
101 * The Wake Lock FMQ that is read to determine when the framework has handled WAKE_UP events
102 */
103 std::unique_ptr<WakeLockMessageQueue> mWakeLockQueue;
104
105 /**
106 * Event Flag to signal to the framework when sensor events are available to be read
107 */
108 EventFlag* mEventQueueFlag;
109
110 /**
111 * Callback to the sensors framework to inform it that new sensors have been added or removed.
112 */
113 sp<ISensorsCallback> mDynamicSensorsCallback;
114};
115
116} // namespace implementation
117} // namespace V2_0
118} // namespace sensors
119} // namespace hardware
120} // namespace android