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