blob: 809d07e98b1203aa0b1cd7dc962a3afc496007e7 [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
Anthony Stangeaacbf942019-08-30 15:21:34 -040049 explicit HalProxy();
50 // Test only constructor.
51 explicit HalProxy(std::vector<ISensorsSubHal*>& subHalList);
Anthony Stangea689f8a2019-07-30 11:35:48 -040052 ~HalProxy();
53
54 // Methods from ::android::hardware::sensors::V2_0::ISensors follow.
55 Return<void> getSensorsList(getSensorsList_cb _hidl_cb) override;
56
57 Return<Result> setOperationMode(OperationMode mode) override;
58
59 Return<Result> activate(int32_t sensorHandle, bool enabled) override;
60
61 Return<Result> initialize(
62 const ::android::hardware::MQDescriptorSync<Event>& eventQueueDescriptor,
63 const ::android::hardware::MQDescriptorSync<uint32_t>& wakeLockDescriptor,
64 const sp<ISensorsCallback>& sensorsCallback) override;
65
66 Return<Result> batch(int32_t sensorHandle, int64_t samplingPeriodNs,
67 int64_t maxReportLatencyNs) override;
68
69 Return<Result> flush(int32_t sensorHandle) override;
70
71 Return<Result> injectSensorData(const Event& event) override;
72
73 Return<void> registerDirectChannel(const SharedMemInfo& mem,
74 registerDirectChannel_cb _hidl_cb) override;
75
76 Return<Result> unregisterDirectChannel(int32_t channelHandle) override;
77
78 Return<void> configDirectReport(int32_t sensorHandle, int32_t channelHandle, RateLevel rate,
79 configDirectReport_cb _hidl_cb) override;
80
81 Return<void> debug(const hidl_handle& fd, const hidl_vec<hidl_string>& args) override;
82
Anthony Stangeaacbf942019-08-30 15:21:34 -040083 // Below methods from ::android::hardware::sensors::V2_0::ISensorsCallback with a minor change
Anthony Stangea689f8a2019-07-30 11:35:48 -040084 // to pass in the sub-HAL index. While the above methods are invoked from the sensors framework
85 // via the binder, these methods are invoked from a callback provided to sub-HALs inside the
86 // same process as the HalProxy, but potentially running on different threads.
87 Return<void> onDynamicSensorsConnected(const hidl_vec<SensorInfo>& dynamicSensorsAdded,
88 int32_t subHalIndex);
89
90 Return<void> onDynamicSensorsDisconnected(const hidl_vec<int32_t>& dynamicSensorHandlesRemoved,
91 int32_t subHalIndex);
92
93 private:
94 using EventMessageQueue = MessageQueue<Event, kSynchronizedReadWrite>;
95 using WakeLockMessageQueue = MessageQueue<uint32_t, kSynchronizedReadWrite>;
96
97 /**
98 * The Event FMQ where sensor events are written
99 */
100 std::unique_ptr<EventMessageQueue> mEventQueue;
101
102 /**
103 * The Wake Lock FMQ that is read to determine when the framework has handled WAKE_UP events
104 */
105 std::unique_ptr<WakeLockMessageQueue> mWakeLockQueue;
106
107 /**
108 * Event Flag to signal to the framework when sensor events are available to be read
109 */
110 EventFlag* mEventQueueFlag;
111
112 /**
113 * Callback to the sensors framework to inform it that new sensors have been added or removed.
114 */
115 sp<ISensorsCallback> mDynamicSensorsCallback;
Stan Rokita28790672019-08-20 14:32:15 -0700116
117 /**
118 * SubHal object pointers that have been saved from vendor dynamic libraries.
119 */
120 std::vector<ISensorsSubHal*> mSubHalList;
Stan Rokita16385312019-09-10 14:54:36 -0700121
122 /*
123 * Get the subhal pointer which can be found by indexing into the mSubHalList vector
124 * using the index from the first byte of sensorHandle.
125 *
126 * @param sensorHandle The handle used to identify a sensor in one of the subhals.
127 */
128 ISensorsSubHal* getSubHalForSensorHandle(uint32_t sensorHandle);
129
130 /*
131 * Zero out the first (most significant) byte in a number. Used in modifying the sensor handles
132 * before passing them to subhals.
133 *
134 * @param num The uint32_t number to work with.
135 *
136 * @return The modified version of num param.
137 */
138 static uint32_t zeroOutFirstByte(uint32_t num);
Anthony Stangea689f8a2019-07-30 11:35:48 -0400139};
140
141} // namespace implementation
142} // namespace V2_0
143} // namespace sensors
144} // namespace hardware
145} // namespace android