1 /*
2  * Copyright (C) 2020 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 #include <android-base/unique_fd.h>
19 #include <V2_1/SubHal.h>
20 #include <cstdint>
21 #include <thread>
22 
23 namespace goldfish {
24 namespace ahs = ::android::hardware::sensors;
25 namespace ahs21 = ahs::V2_1;
26 namespace ahs10 = ahs::V1_0;
27 
28 using ahs21::implementation::IHalProxyCallback;
29 using ahs21::Event;
30 using ahs10::OperationMode;
31 using ahs10::RateLevel;
32 using ahs10::Result;
33 using ahs10::SharedMemInfo;
34 
35 using ::android::base::unique_fd;
36 using ::android::hardware::hidl_handle;
37 using ::android::hardware::hidl_string;
38 using ::android::hardware::hidl_vec;
39 using ::android::hardware::Return;
40 using ::android::sp;
41 
42 struct MultihalSensors : public ahs21::implementation::ISensorsSubHal {
43     MultihalSensors();
44     ~MultihalSensors();
45 
46     Return<void> debug(const hidl_handle& fd, const hidl_vec<hidl_string>& args) override;
47     Return<void> getSensorsList_2_1(getSensorsList_2_1_cb _hidl_cb) override;
48     Return<Result> setOperationMode(OperationMode mode) override;
49     Return<Result> activate(int32_t sensorHandle, bool enabled) override;
50     Return<Result> batch(int32_t sensorHandle,
51                            int64_t samplingPeriodNs,
52                            int64_t maxReportLatencyNs) override;
53     Return<Result> flush(int32_t sensorHandle) override;
54     Return<Result> injectSensorData_2_1(const Event& event) override;
55 
56 
57     Return<void> registerDirectChannel(const SharedMemInfo& mem,
58                                        registerDirectChannel_cb _hidl_cb) override;
59     Return<Result> unregisterDirectChannel(int32_t channelHandle) override;
60     Return<void> configDirectReport(int32_t sensorHandle,
61                                     int32_t channelHandle,
62                                     RateLevel rate,
63                                     configDirectReport_cb _hidl_cb) override;
64 
65     const std::string getName() override;
66     Return<Result> initialize(const sp<IHalProxyCallback>& halProxyCallback) override;
67 
68 private:
69     struct QemuSensorsProtocolState {
70         int64_t timeBiasNs = -500000000;
71 
72         static constexpr float kSensorNoValue = -1e+30;
73 
74         // on change sensors (host does not support them)
75         float lastAmbientTemperatureValue = kSensorNoValue;
76         float lastProximityValue = kSensorNoValue;
77         float lastLightValue = kSensorNoValue;
78         float lastRelativeHumidityValue = kSensorNoValue;
79         float lastHingeAngle0Value = kSensorNoValue;
80         float lastHingeAngle1Value = kSensorNoValue;
81         float lastHingeAngle2Value = kSensorNoValue;
82     };
83 
84     static bool activateQemuSensorImpl(int pipe, int sensorHandle, bool enabled);
85     bool disableAllSensors();
86     void parseQemuSensorEvent(const int pipe, QemuSensorsProtocolState* state);
87     void postSensorEvent(const Event& event);
88     void postSensorEventLocked(const Event& event);
89 
90     void qemuSensorListenerThread();
91     static void qemuSensorListenerThreadStart(MultihalSensors* that);
92 
93     static constexpr char kCMD_QUIT = 'q';
94     bool qemuSensorThreadSendCommand(char cmd) const;
95 
96     // set in ctor, never change
97     const unique_fd     m_qemuSensorsFd;
98     uint32_t            m_availableSensorsMask = 0;
99     // a pair of connected sockets to talk to the worker thread
100     unique_fd           m_callersFd;        // a caller writes here
101     unique_fd           m_sensorThreadFd;   // the worker thread listens from here
102     std::thread         m_sensorThread;
103 
104     // changed by API
105     uint32_t                m_activeSensorsMask = 0;
106     OperationMode           m_opMode = OperationMode::NORMAL;
107     sp<IHalProxyCallback>   m_halProxyCallback;
108     mutable std::mutex      m_apiMtx;
109 };
110 
111 }  // namespace goldfish
112