1 /*
2 * Copyright (C) 2021 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 "BaseSensorObject.h"
18 #include "DynamicSensorsSubHal.h"
19
20 #include <convertV2_1.h>
21 #include <hardware/sensors-base.h>
22 #include <log/log.h>
23
24 #include <chrono>
25 #include <thread>
26
27 using ::android::hardware::sensors::V1_0::Result;
28 using ::android::hardware::sensors::V2_0::implementation::ScopedWakelock;
29 using ::android::hardware::sensors::V2_1::implementation::convertFromSensorEvent;
30 using ::android::hardware::sensors::V2_1::SensorInfo;
31 using ::android::hardware::sensors::V2_1::SensorType;
32 template<class T> using Return = ::android::hardware::Return<T>;
33 using ::android::hardware::Void;
34
35 namespace android {
36 namespace SensorHalExt {
37
ResultFromStatus(status_t err)38 static Result ResultFromStatus(status_t err) {
39 switch (err) {
40 case ::android::OK:
41 return Result::OK;
42 case ::android::PERMISSION_DENIED:
43 return Result::PERMISSION_DENIED;
44 case ::android::NO_MEMORY:
45 return Result::NO_MEMORY;
46 case ::android::BAD_VALUE:
47 return Result::BAD_VALUE;
48 default:
49 return Result::INVALID_OPERATION;
50 }
51 }
52
DynamicSensorsSubHal()53 DynamicSensorsSubHal::DynamicSensorsSubHal() {
54 // initialize dynamic sensor manager
55 mDynamicSensorManager.reset(
56 DynamicSensorManager::createInstance(kDynamicHandleBase,
57 kMaxDynamicHandleCount,
58 this /* callback */));
59 }
60
61 // ISensors.
setOperationMode(OperationMode mode)62 Return<Result> DynamicSensorsSubHal::setOperationMode(OperationMode mode) {
63 return (mode == static_cast<OperationMode>(SENSOR_HAL_NORMAL_MODE) ?
64 Result::OK : Result::BAD_VALUE);
65 }
66
activate(int32_t sensor_handle,bool enabled)67 Return<Result> DynamicSensorsSubHal::activate(int32_t sensor_handle,
68 bool enabled) {
69 int rc = mDynamicSensorManager->activate(sensor_handle, enabled);
70 return ResultFromStatus(rc);
71 }
72
batch(int32_t sensor_handle,int64_t sampling_period_ns,int64_t max_report_latency_ns)73 Return<Result> DynamicSensorsSubHal::batch(
74 int32_t sensor_handle, int64_t sampling_period_ns,
75 int64_t max_report_latency_ns) {
76 int rc = mDynamicSensorManager->batch(sensor_handle, sampling_period_ns,
77 max_report_latency_ns);
78 return ResultFromStatus(rc);
79 }
80
flush(int32_t sensor_handle)81 Return<Result> DynamicSensorsSubHal::flush(int32_t sensor_handle) {
82 int rc = mDynamicSensorManager->flush(sensor_handle);
83 return ResultFromStatus(rc);
84 }
85
registerDirectChannel(const SharedMemInfo & mem __unused,registerDirectChannel_cb callback __unused)86 Return<void> DynamicSensorsSubHal::registerDirectChannel(
87 const SharedMemInfo& mem __unused,
88 registerDirectChannel_cb callback __unused) {
89 ALOGE("DynamicSensorsSubHal::registerDirectChannel not supported.");
90
91 return Void();
92 }
93
unregisterDirectChannel(int32_t channel_handle __unused)94 Return<Result> DynamicSensorsSubHal::unregisterDirectChannel(
95 int32_t channel_handle __unused) {
96 ALOGE("DynamicSensorsSubHal::unregisterDirectChannel not supported.");
97
98 return Result::INVALID_OPERATION;
99 }
100
configDirectReport(int32_t sensor_handle __unused,int32_t channel_handle __unused,RateLevel rate __unused,configDirectReport_cb callback __unused)101 Return<void> DynamicSensorsSubHal::configDirectReport(
102 int32_t sensor_handle __unused, int32_t channel_handle __unused,
103 RateLevel rate __unused, configDirectReport_cb callback __unused) {
104 ALOGE("DynamicSensorsSubHal::configDirectReport not supported.");
105
106 return Void();
107 }
108
getSensorsList_2_1(getSensorsList_2_1_cb callback)109 Return<void> DynamicSensorsSubHal::getSensorsList_2_1(
110 getSensorsList_2_1_cb callback) {
111 const sensor_t& sensor_info = mDynamicSensorManager->getDynamicMetaSensor();
112 std::vector<SensorInfo> sensors;
113
114 ALOGD("DynamicSensorsSubHal::getSensorsList_2_1 invoked.");
115
116 // get the dynamic sensor info
117 sensors.resize(1);
118 sensors[0].sensorHandle = sensor_info.handle;
119 sensors[0].name = sensor_info.name;
120 sensors[0].vendor = sensor_info.vendor;
121 sensors[0].version = 1;
122 sensors[0].type = static_cast<SensorType>(sensor_info.type);
123 sensors[0].typeAsString = sensor_info.stringType;
124 sensors[0].maxRange = sensor_info.maxRange;
125 sensors[0].resolution = sensor_info.resolution;
126 sensors[0].power = sensor_info.power;
127 sensors[0].minDelay = sensor_info.minDelay;
128 sensors[0].fifoReservedEventCount = sensor_info.fifoReservedEventCount;
129 sensors[0].fifoMaxEventCount = sensor_info.fifoMaxEventCount;
130 sensors[0].requiredPermission = sensor_info.requiredPermission;
131 sensors[0].maxDelay = sensor_info.maxDelay;
132 sensors[0].flags = sensor_info.flags;
133
134 callback(sensors);
135
136 return Void();
137 }
138
injectSensorData_2_1(const Event & event __unused)139 Return<Result> DynamicSensorsSubHal::injectSensorData_2_1(
140 const Event& event __unused) {
141 ALOGE("DynamicSensorsSubHal::injectSensorData_2_1 not supported.");
142
143 return Result::INVALID_OPERATION;
144 }
145
debug(const hidl_handle & handle __unused,const hidl_vec<hidl_string> & args __unused)146 Return<void> DynamicSensorsSubHal::debug(
147 const hidl_handle& handle __unused,
148 const hidl_vec<hidl_string>& args __unused) {
149 return Void();
150 }
151
152 // ISensorsSubHal.
initialize(const sp<IHalProxyCallback> & hal_proxy_callback)153 Return<Result> DynamicSensorsSubHal::initialize(
154 const sp<IHalProxyCallback>& hal_proxy_callback) {
155 ALOGD("DynamicSensorsSubHal::initialize invoked.");
156
157 mHalProxyCallback = hal_proxy_callback;
158
159 return Result::OK;
160 }
161
162 // SensorEventCallback.
submitEvent(SP (BaseSensorObject)sensor,const sensors_event_t & e)163 int DynamicSensorsSubHal::submitEvent(SP(BaseSensorObject) sensor,
164 const sensors_event_t& e) {
165 std::vector<Event> events;
166 Event hal_event;
167 bool wakeup;
168
169 if (e.type == SENSOR_TYPE_DYNAMIC_SENSOR_META) {
170 const dynamic_sensor_meta_event_t* sensor_meta;
171
172 sensor_meta = static_cast<const dynamic_sensor_meta_event_t*>(
173 &(e.dynamic_sensor_meta));
174 if (sensor_meta->connected != 0) {
175 // The sensor framework must be notified of the connected sensor
176 // through the callback before handling the sensor added event. If
177 // it isn't, it will assert when looking up the sensor handle when
178 // processing the sensor added event.
179 //
180 // TODO (b/201529167): Fix dynamic sensors addition / removal when
181 // converting to AIDL.
182 // The sensor framework runs in a separate process from the sensor
183 // HAL, and it processes events in a dedicated thread, so it's
184 // possible the event handling can be done before the callback is
185 // run. Thus, a delay is added after sending notification of the
186 // connected sensor.
187 onSensorConnected(sensor_meta->handle, sensor_meta->sensor);
188 std::this_thread::sleep_for(std::chrono::milliseconds(1000));
189 }
190 }
191
192 convertFromSensorEvent(e, &hal_event);
193 events.push_back(hal_event);
194 if (sensor && sensor->getSensor()) {
195 wakeup = sensor->getSensor()->flags & SENSOR_FLAG_WAKE_UP;
196 } else {
197 wakeup = false;
198 }
199 ScopedWakelock wakelock = mHalProxyCallback->createScopedWakelock(wakeup);
200 mHalProxyCallback->postEvents(events, std::move(wakelock));
201
202 return 0;
203 }
204
onSensorConnected(int handle,const sensor_t * sensor_info)205 void DynamicSensorsSubHal::onSensorConnected(
206 int handle, const sensor_t* sensor_info) {
207 hidl_vec<SensorInfo> sensor_list;
208
209 sensor_list.resize(1);
210 sensor_list[0].sensorHandle = handle;
211 sensor_list[0].name = sensor_info->name;
212 sensor_list[0].vendor = sensor_info->vendor;
213 sensor_list[0].version = 1;
214 sensor_list[0].type = static_cast<SensorType>(sensor_info->type);
215 sensor_list[0].typeAsString = sensor_info->stringType;
216 sensor_list[0].maxRange = sensor_info->maxRange;
217 sensor_list[0].resolution = sensor_info->resolution;
218 sensor_list[0].power = sensor_info->power;
219 sensor_list[0].minDelay = sensor_info->minDelay;
220 sensor_list[0].fifoReservedEventCount = sensor_info->fifoReservedEventCount;
221 sensor_list[0].fifoMaxEventCount = sensor_info->fifoMaxEventCount;
222 sensor_list[0].requiredPermission = sensor_info->requiredPermission;
223 sensor_list[0].maxDelay = sensor_info->maxDelay;
224 sensor_list[0].flags = sensor_info->flags;
225
226 mHalProxyCallback->onDynamicSensorsConnected_2_1(sensor_list);
227 }
228
229 } // namespace SensorHalExt
230 } // namespace android
231
232 using ::android::hardware::sensors::V2_1::implementation::ISensorsSubHal;
sensorsHalGetSubHal_2_1(uint32_t * version)233 ISensorsSubHal* sensorsHalGetSubHal_2_1(uint32_t* version) {
234 static android::SensorHalExt::DynamicSensorsSubHal subHal;
235
236 *version = SUB_HAL_2_1_VERSION;
237 return &subHal;
238 }
239
240