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 #include "SensorsSubHal.h"
18 
19 #include <android/hardware/sensors/2.0/types.h>
20 #include <log/log.h>
21 
sensorsHalGetSubHal(uint32_t * version)22 ISensorsSubHal* sensorsHalGetSubHal(uint32_t* version) {
23 #if defined SUPPORT_CONTINUOUS_SENSORS && defined SUPPORT_ON_CHANGE_SENSORS
24     static ::android::hardware::sensors::V2_0::subhal::implementation::AllSensorsSubHal subHal;
25 #elif defined SUPPORT_CONTINUOUS_SENSORS
26     static ::android::hardware::sensors::V2_0::subhal::implementation::ContinuousSensorsSubHal
27             subHal;
28 #elif defined SUPPORT_ON_CHANGE_SENSORS
29     static ::android::hardware::sensors::V2_0::subhal::implementation::OnChangeSensorsSubHal subHal;
30 #else
31     static ::android::hardware::sensors::V2_0::subhal::implementation::SensorsSubHal subHal;
32 #endif  // defined SUPPORT_CONTINUOUS_SENSORS && defined SUPPORT_ON_CHANGE_SENSORS
33     *version = SUB_HAL_2_0_VERSION;
34     return &subHal;
35 }
36 
37 namespace android {
38 namespace hardware {
39 namespace sensors {
40 namespace V2_0 {
41 namespace subhal {
42 namespace implementation {
43 
44 using ::android::hardware::Void;
45 using ::android::hardware::sensors::V1_0::Event;
46 using ::android::hardware::sensors::V1_0::OperationMode;
47 using ::android::hardware::sensors::V1_0::RateLevel;
48 using ::android::hardware::sensors::V1_0::Result;
49 using ::android::hardware::sensors::V1_0::SharedMemInfo;
50 using ::android::hardware::sensors::V2_0::SensorTimeout;
51 using ::android::hardware::sensors::V2_0::WakeLockQueueFlagBits;
52 using ::android::hardware::sensors::V2_0::implementation::ScopedWakelock;
53 
SensorsSubHal()54 SensorsSubHal::SensorsSubHal() : mCallback(nullptr), mNextHandle(1) {}
55 
56 // Methods from ::android::hardware::sensors::V2_0::ISensors follow.
getSensorsList(getSensorsList_cb _hidl_cb)57 Return<void> SensorsSubHal::getSensorsList(getSensorsList_cb _hidl_cb) {
58     std::vector<SensorInfo> sensors;
59     for (const auto& sensor : mSensors) {
60         sensors.push_back(sensor.second->getSensorInfo());
61     }
62 
63     _hidl_cb(sensors);
64     return Void();
65 }
66 
setOperationMode(OperationMode mode)67 Return<Result> SensorsSubHal::setOperationMode(OperationMode mode) {
68     for (auto sensor : mSensors) {
69         sensor.second->setOperationMode(mode);
70     }
71     mCurrentOperationMode = mode;
72     return Result::OK;
73 }
74 
activate(int32_t sensorHandle,bool enabled)75 Return<Result> SensorsSubHal::activate(int32_t sensorHandle, bool enabled) {
76     auto sensor = mSensors.find(sensorHandle);
77     if (sensor != mSensors.end()) {
78         sensor->second->activate(enabled);
79         return Result::OK;
80     }
81     return Result::BAD_VALUE;
82 }
83 
batch(int32_t sensorHandle,int64_t samplingPeriodNs,int64_t)84 Return<Result> SensorsSubHal::batch(int32_t sensorHandle, int64_t samplingPeriodNs,
85                                     int64_t /* maxReportLatencyNs */) {
86     auto sensor = mSensors.find(sensorHandle);
87     if (sensor != mSensors.end()) {
88         sensor->second->batch(samplingPeriodNs);
89         return Result::OK;
90     }
91     return Result::BAD_VALUE;
92 }
93 
flush(int32_t sensorHandle)94 Return<Result> SensorsSubHal::flush(int32_t sensorHandle) {
95     auto sensor = mSensors.find(sensorHandle);
96     if (sensor != mSensors.end()) {
97         return sensor->second->flush();
98     }
99     return Result::BAD_VALUE;
100 }
101 
injectSensorData(const Event & event)102 Return<Result> SensorsSubHal::injectSensorData(const Event& event) {
103     auto sensor = mSensors.find(event.sensorHandle);
104     if (sensor != mSensors.end()) {
105         return sensor->second->injectEvent(event);
106     }
107 
108     return Result::BAD_VALUE;
109 }
110 
registerDirectChannel(const SharedMemInfo &,registerDirectChannel_cb _hidl_cb)111 Return<void> SensorsSubHal::registerDirectChannel(const SharedMemInfo& /* mem */,
112                                                   registerDirectChannel_cb _hidl_cb) {
113     _hidl_cb(Result::INVALID_OPERATION, -1 /* channelHandle */);
114     return Return<void>();
115 }
116 
unregisterDirectChannel(int32_t)117 Return<Result> SensorsSubHal::unregisterDirectChannel(int32_t /* channelHandle */) {
118     return Result::INVALID_OPERATION;
119 }
120 
configDirectReport(int32_t,int32_t,RateLevel,configDirectReport_cb _hidl_cb)121 Return<void> SensorsSubHal::configDirectReport(int32_t /* sensorHandle */,
122                                                int32_t /* channelHandle */, RateLevel /* rate */,
123                                                configDirectReport_cb _hidl_cb) {
124     _hidl_cb(Result::INVALID_OPERATION, 0 /* reportToken */);
125     return Return<void>();
126 }
127 
debug(const hidl_handle & fd,const hidl_vec<hidl_string> & args)128 Return<void> SensorsSubHal::debug(const hidl_handle& fd, const hidl_vec<hidl_string>& args) {
129     if (fd.getNativeHandle() == nullptr || fd->numFds < 1) {
130         ALOGE("%s: missing fd for writing", __FUNCTION__);
131         return Void();
132     }
133 
134     FILE* out = fdopen(dup(fd->data[0]), "w");
135 
136     if (args.size() != 0) {
137         fprintf(out,
138                 "Note: sub-HAL %s currently does not support args. Input arguments are "
139                 "ignored.\n",
140                 getName().c_str());
141     }
142 
143     std::ostringstream stream;
144     stream << "Available sensors:" << std::endl;
145     for (auto sensor : mSensors) {
146         SensorInfo info = sensor.second->getSensorInfo();
147         stream << "Name: " << info.name << std::endl;
148         stream << "Min delay: " << info.minDelay << std::endl;
149         stream << "Flags: " << info.flags << std::endl;
150     }
151     stream << std::endl;
152 
153     fprintf(out, "%s", stream.str().c_str());
154 
155     fclose(out);
156     return Return<void>();
157 }
158 
initialize(const sp<IHalProxyCallback> & halProxyCallback)159 Return<Result> SensorsSubHal::initialize(const sp<IHalProxyCallback>& halProxyCallback) {
160     mCallback = halProxyCallback;
161     setOperationMode(OperationMode::NORMAL);
162     return Result::OK;
163 }
164 
postEvents(const std::vector<Event> & events,bool wakeup)165 void SensorsSubHal::postEvents(const std::vector<Event>& events, bool wakeup) {
166     ScopedWakelock wakelock = mCallback->createScopedWakelock(wakeup);
167     mCallback->postEvents(events, std::move(wakelock));
168 }
169 
ContinuousSensorsSubHal()170 ContinuousSensorsSubHal::ContinuousSensorsSubHal() {
171     AddSensor<AccelSensor>();
172     AddSensor<GyroSensor>();
173     AddSensor<MagnetometerSensor>();
174     AddSensor<PressureSensor>();
175     AddSensor<DeviceTempSensor>();
176 }
177 
OnChangeSensorsSubHal()178 OnChangeSensorsSubHal::OnChangeSensorsSubHal() {
179     AddSensor<AmbientTempSensor>();
180     AddSensor<LightSensor>();
181     AddSensor<ProximitySensor>();
182     AddSensor<RelativeHumiditySensor>();
183 }
184 
AllSensorsSubHal()185 AllSensorsSubHal::AllSensorsSubHal() {
186     AddSensor<AccelSensor>();
187     AddSensor<GyroSensor>();
188     AddSensor<MagnetometerSensor>();
189     AddSensor<PressureSensor>();
190     AddSensor<DeviceTempSensor>();
191     AddSensor<AmbientTempSensor>();
192     AddSensor<LightSensor>();
193     AddSensor<ProximitySensor>();
194     AddSensor<RelativeHumiditySensor>();
195 }
196 
setOperationMode(OperationMode)197 Return<Result> SetOperationModeFailingSensorsSubHal::setOperationMode(OperationMode /*mode*/) {
198     return Result::BAD_VALUE;
199 }
200 
getSensorsList(getSensorsList_cb _hidl_cb)201 Return<void> AllSupportDirectChannelSensorsSubHal::getSensorsList(getSensorsList_cb _hidl_cb) {
202     std::vector<SensorInfo> sensors;
203     for (const auto& sensor : mSensors) {
204         SensorInfo sensorInfo = sensor.second->getSensorInfo();
205         sensorInfo.flags |= V1_0::SensorFlagBits::MASK_DIRECT_CHANNEL;
206         sensorInfo.flags |= V1_0::SensorFlagBits::MASK_DIRECT_REPORT;
207         sensors.push_back(sensorInfo);
208     }
209     _hidl_cb(sensors);
210     return Void();
211 }
212 
getSensorsList(getSensorsList_cb _hidl_cb)213 Return<void> DoesNotSupportDirectChannelSensorsSubHal::getSensorsList(getSensorsList_cb _hidl_cb) {
214     std::vector<SensorInfo> sensors;
215     for (const auto& sensor : mSensors) {
216         SensorInfo sensorInfo = sensor.second->getSensorInfo();
217         sensorInfo.flags &= ~static_cast<uint32_t>(V1_0::SensorFlagBits::MASK_DIRECT_CHANNEL);
218         sensorInfo.flags &= ~static_cast<uint32_t>(V1_0::SensorFlagBits::MASK_DIRECT_REPORT);
219         sensors.push_back(sensorInfo);
220     }
221     _hidl_cb(sensors);
222     return Void();
223 }
224 
addDynamicSensors(const std::vector<SensorInfo> & sensorsAdded)225 void AddAndRemoveDynamicSensorsSubHal::addDynamicSensors(
226         const std::vector<SensorInfo>& sensorsAdded) {
227     mCallback->onDynamicSensorsConnected(sensorsAdded);
228 }
229 
removeDynamicSensors(const std::vector<int32_t> & sensorHandlesRemoved)230 void AddAndRemoveDynamicSensorsSubHal::removeDynamicSensors(
231         const std::vector<int32_t>& sensorHandlesRemoved) {
232     mCallback->onDynamicSensorsDisconnected(sensorHandlesRemoved);
233 }
234 
235 }  // namespace implementation
236 }  // namespace subhal
237 }  // namespace V2_0
238 }  // namespace sensors
239 }  // namespace hardware
240 }  // namespace android
241