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 #pragma once
18 
19 #include "V2_0/ScopedWakelock.h"
20 #include "V2_0/SubHal.h"
21 #include "V2_1/SubHal.h"
22 #include "convertV2_1.h"
23 
24 #include <android/hardware/sensors/2.1/ISensors.h>
25 #include <android/hardware/sensors/2.1/types.h>
26 #include <log/log.h>
27 
28 namespace android {
29 namespace hardware {
30 namespace sensors {
31 namespace V2_0 {
32 namespace implementation {
33 
34 /**
35  * Interface used to communicate with the HalProxy when subHals interact with their provided
36  * callback.
37  */
38 class ISubHalCallback {
39   public:
~ISubHalCallback()40     virtual ~ISubHalCallback() {}
41 
42     // Below methods from ::android::hardware::sensors::V2_0::ISensorsCallback with a minor change
43     // to pass in the sub-HAL index. While the above methods are invoked from the sensors framework
44     // via the binder, these methods are invoked from a callback provided to sub-HALs inside the
45     // same process as the HalProxy, but potentially running on different threads.
46     virtual Return<void> onDynamicSensorsConnected(
47             const hidl_vec<V2_1::SensorInfo>& dynamicSensorsAdded, int32_t subHalIndex) = 0;
48 
49     virtual Return<void> onDynamicSensorsDisconnected(
50             const hidl_vec<int32_t>& dynamicSensorHandlesRemoved, int32_t subHalIndex) = 0;
51 
52     /**
53      * Post events to the event message queue if there is room to write them. Otherwise post the
54      * remaining events to a background thread for a blocking write with a kPendingWriteTimeoutNs
55      * timeout.
56      *
57      * @param events The list of events to post to the message queue.
58      * @param numWakeupEvents The number of wakeup events in events.
59      * @param wakelock The wakelock associated with this post of events.
60      */
61     virtual void postEventsToMessageQueue(const std::vector<V2_1::Event>& events,
62                                           size_t numWakeupEvents,
63                                           V2_0::implementation::ScopedWakelock wakelock) = 0;
64 
65     /**
66      * Get the sensor info associated with that sensorHandle.
67      *
68      * @param sensorHandle The sensor handle.
69      *
70      * @return The sensor info object in the mapping.
71      */
72     virtual const V2_1::SensorInfo& getSensorInfo(int32_t sensorHandle) = 0;
73 
74     virtual bool areThreadsRunning() = 0;
75 };
76 
77 /**
78  * Callback class given to subhals that allows the HalProxy to know which subhal a given invocation
79  * is coming from.
80  */
81 class HalProxyCallbackBase : public VirtualLightRefBase {
82   public:
HalProxyCallbackBase(ISubHalCallback * callback,V2_0::implementation::IScopedWakelockRefCounter * refCounter,int32_t subHalIndex)83     HalProxyCallbackBase(ISubHalCallback* callback,
84                          V2_0::implementation::IScopedWakelockRefCounter* refCounter,
85                          int32_t subHalIndex)
86         : mCallback(callback), mRefCounter(refCounter), mSubHalIndex(subHalIndex) {}
87 
88     void postEvents(const std::vector<V2_1::Event>& events,
89                     V2_0::implementation::ScopedWakelock wakelock);
90 
91     V2_0::implementation::ScopedWakelock createScopedWakelock(bool lock);
92 
93   protected:
94     ISubHalCallback* mCallback;
95     V2_0::implementation::IScopedWakelockRefCounter* mRefCounter;
96     int32_t mSubHalIndex;
97 
98   private:
99     std::vector<V2_1::Event> processEvents(const std::vector<V2_1::Event>& events,
100                                            size_t* numWakeupEvents) const;
101 };
102 
103 class HalProxyCallbackV2_0 : public HalProxyCallbackBase,
104                              public V2_0::implementation::IHalProxyCallback {
105   public:
HalProxyCallbackV2_0(ISubHalCallback * callback,V2_0::implementation::IScopedWakelockRefCounter * refCounter,int32_t subHalIndex)106     HalProxyCallbackV2_0(ISubHalCallback* callback,
107                          V2_0::implementation::IScopedWakelockRefCounter* refCounter,
108                          int32_t subHalIndex)
109         : HalProxyCallbackBase(callback, refCounter, subHalIndex) {}
110 
onDynamicSensorsConnected(const hidl_vec<V1_0::SensorInfo> & dynamicSensorsAdded)111     Return<void> onDynamicSensorsConnected(
112             const hidl_vec<V1_0::SensorInfo>& dynamicSensorsAdded) override {
113         return mCallback->onDynamicSensorsConnected(
114                 V2_1::implementation::convertToNewSensorInfos(dynamicSensorsAdded), mSubHalIndex);
115     }
116 
onDynamicSensorsDisconnected(const hidl_vec<int32_t> & dynamicSensorHandlesRemoved)117     Return<void> onDynamicSensorsDisconnected(
118             const hidl_vec<int32_t>& dynamicSensorHandlesRemoved) override {
119         return mCallback->onDynamicSensorsDisconnected(dynamicSensorHandlesRemoved, mSubHalIndex);
120     }
121 
postEvents(const std::vector<V1_0::Event> & events,V2_0::implementation::ScopedWakelock wakelock)122     void postEvents(const std::vector<V1_0::Event>& events,
123                     V2_0::implementation::ScopedWakelock wakelock) override {
124         HalProxyCallbackBase::postEvents(V2_1::implementation::convertToNewEvents(events),
125                                          std::move(wakelock));
126     }
127 
createScopedWakelock(bool lock)128     V2_0::implementation::ScopedWakelock createScopedWakelock(bool lock) override {
129         return HalProxyCallbackBase::createScopedWakelock(lock);
130     }
131 };
132 
133 class HalProxyCallbackV2_1 : public HalProxyCallbackBase,
134                              public V2_1::implementation::IHalProxyCallback {
135   public:
HalProxyCallbackV2_1(ISubHalCallback * callback,V2_0::implementation::IScopedWakelockRefCounter * refCounter,int32_t subHalIndex)136     HalProxyCallbackV2_1(ISubHalCallback* callback,
137                          V2_0::implementation::IScopedWakelockRefCounter* refCounter,
138                          int32_t subHalIndex)
139         : HalProxyCallbackBase(callback, refCounter, subHalIndex) {}
140 
onDynamicSensorsConnected_2_1(const hidl_vec<V2_1::SensorInfo> & dynamicSensorsAdded)141     Return<void> onDynamicSensorsConnected_2_1(
142             const hidl_vec<V2_1::SensorInfo>& dynamicSensorsAdded) override {
143         return mCallback->onDynamicSensorsConnected(dynamicSensorsAdded, mSubHalIndex);
144     }
145 
onDynamicSensorsConnected(const hidl_vec<V1_0::SensorInfo> &)146     Return<void> onDynamicSensorsConnected(
147             const hidl_vec<V1_0::SensorInfo>& /* dynamicSensorsAdded */) override {
148         LOG_ALWAYS_FATAL("Old dynamic sensors method can't be used");
149         return Void();
150     }
151 
onDynamicSensorsDisconnected(const hidl_vec<int32_t> & dynamicSensorHandlesRemoved)152     Return<void> onDynamicSensorsDisconnected(
153             const hidl_vec<int32_t>& dynamicSensorHandlesRemoved) override {
154         return mCallback->onDynamicSensorsDisconnected(dynamicSensorHandlesRemoved, mSubHalIndex);
155     }
156 
postEvents(const std::vector<V2_1::Event> & events,V2_0::implementation::ScopedWakelock wakelock)157     void postEvents(const std::vector<V2_1::Event>& events,
158                     V2_0::implementation::ScopedWakelock wakelock) override {
159         return HalProxyCallbackBase::postEvents(events, std::move(wakelock));
160     }
161 
createScopedWakelock(bool lock)162     V2_0::implementation::ScopedWakelock createScopedWakelock(bool lock) override {
163         return HalProxyCallbackBase::createScopedWakelock(lock);
164     }
165 };
166 
167 }  // namespace implementation
168 }  // namespace V2_0
169 }  // namespace sensors
170 }  // namespace hardware
171 }  // namespace android