blob: 24573109c2977a190f23df6eec3ecbff73e29a06 [file] [log] [blame]
Andreas Huberdb49a412016-10-10 13:23:59 -07001/*
2 * Copyright (C) 2016 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 "Sensors.h"
Andreas Huberdb49a412016-10-10 13:23:59 -070018#include "convert.h"
Nick Vaccarod133e4c2016-11-17 00:14:07 -080019#include "multihal.h"
Andreas Huberdb49a412016-10-10 13:23:59 -070020
21#include <android-base/logging.h>
22
Nick Vaccarod133e4c2016-11-17 00:14:07 -080023#include <sys/stat.h>
24
Andreas Huberdb49a412016-10-10 13:23:59 -070025namespace android {
26namespace hardware {
27namespace sensors {
28namespace V1_0 {
29namespace implementation {
30
Nick Vaccarod133e4c2016-11-17 00:14:07 -080031/*
32 * If a multi-hal configuration file exists in the proper location,
33 * return true indicating we need to use multi-hal functionality.
34 */
35static bool UseMultiHal() {
36 const std::string& name = MULTI_HAL_CONFIG_FILE_PATH;
37 struct stat buffer;
38 return (stat (name.c_str(), &buffer) == 0);
39}
40
Andreas Huberdb49a412016-10-10 13:23:59 -070041static Result ResultFromStatus(status_t err) {
42 switch (err) {
43 case OK:
44 return Result::OK;
Andreas Huberdb49a412016-10-10 13:23:59 -070045 case PERMISSION_DENIED:
46 return Result::PERMISSION_DENIED;
Peng Xu89df2e72017-01-09 19:12:42 -080047 case NO_MEMORY:
48 return Result::NO_MEMORY;
49 case BAD_VALUE:
50 return Result::BAD_VALUE;
Andreas Huberdb49a412016-10-10 13:23:59 -070051 default:
52 return Result::INVALID_OPERATION;
53 }
54}
55
56Sensors::Sensors()
57 : mInitCheck(NO_INIT),
58 mSensorModule(nullptr),
59 mSensorDevice(nullptr) {
Nick Vaccarod133e4c2016-11-17 00:14:07 -080060 status_t err = OK;
61 if (UseMultiHal()) {
62 mSensorModule = ::get_multi_hal_module_info();
63 } else {
64 err = hw_get_module(
Andreas Huberdb49a412016-10-10 13:23:59 -070065 SENSORS_HARDWARE_MODULE_ID,
66 (hw_module_t const **)&mSensorModule);
Nick Vaccarod133e4c2016-11-17 00:14:07 -080067 }
Andreas Huberdb49a412016-10-10 13:23:59 -070068 if (mSensorModule == NULL) {
69 err = UNKNOWN_ERROR;
70 }
71
72 if (err != OK) {
73 LOG(ERROR) << "Couldn't load "
74 << SENSORS_HARDWARE_MODULE_ID
75 << " module ("
76 << strerror(-err)
77 << ")";
78
79 mInitCheck = err;
80 return;
81 }
82
83 err = sensors_open_1(&mSensorModule->common, &mSensorDevice);
84
85 if (err != OK) {
86 LOG(ERROR) << "Couldn't open device for module "
87 << SENSORS_HARDWARE_MODULE_ID
88 << " ("
89 << strerror(-err)
90 << ")";
91
92 mInitCheck = err;
93 return;
94 }
95
96 // Require all the old HAL APIs to be present except for injection, which
97 // is considered optional.
98 CHECK_GE(getHalDeviceVersion(), SENSORS_DEVICE_API_VERSION_1_3);
99
100 mInitCheck = OK;
101}
102
103status_t Sensors::initCheck() const {
104 return mInitCheck;
105}
106
Peng Xu0873e642017-01-08 13:28:10 -0800107Return<void> Sensors::getSensorsList(getSensorsList_cb _hidl_cb) {
Andreas Huberdb49a412016-10-10 13:23:59 -0700108 sensor_t const *list;
109 size_t count = mSensorModule->get_sensors_list(mSensorModule, &list);
110
111 hidl_vec<SensorInfo> out;
112 out.resize(count);
113
114 for (size_t i = 0; i < count; ++i) {
115 const sensor_t *src = &list[i];
116 SensorInfo *dst = &out[i];
117
118 convertFromSensor(*src, dst);
119 }
120
Peng Xu0873e642017-01-08 13:28:10 -0800121 _hidl_cb(out);
Andreas Huberdb49a412016-10-10 13:23:59 -0700122
123 return Void();
124}
125
126int Sensors::getHalDeviceVersion() const {
127 if (!mSensorDevice) {
128 return -1;
129 }
130
131 return mSensorDevice->common.version;
132}
133
134Return<Result> Sensors::setOperationMode(OperationMode mode) {
135 return ResultFromStatus(mSensorModule->set_operation_mode((uint32_t)mode));
136}
137
138Return<Result> Sensors::activate(
139 int32_t sensor_handle, bool enabled) {
140 return ResultFromStatus(
141 mSensorDevice->activate(
142 reinterpret_cast<sensors_poll_device_t *>(mSensorDevice),
143 sensor_handle,
144 enabled));
145}
146
Peng Xu0873e642017-01-08 13:28:10 -0800147Return<void> Sensors::poll(int32_t maxCount, poll_cb _hidl_cb) {
Peng Xuab7884f2017-03-02 13:36:45 -0800148
Andreas Huberdb49a412016-10-10 13:23:59 -0700149 hidl_vec<Event> out;
150 hidl_vec<SensorInfo> dynamicSensorsAdded;
151
Peng Xuab7884f2017-03-02 13:36:45 -0800152 std::unique_ptr<sensors_event_t[]> data;
153 int err = android::NO_ERROR;
154
155 { // scope of reentry lock
156
157 // This enforces a single client, meaning that a maximum of one client can call poll().
158 // If this function is re-entred, it means that we are stuck in a state that may prevent
159 // the system from proceeding normally.
160 //
161 // Exit and let the system restart the sensor-hal-implementation hidl service.
162 //
163 // This function must not call _hidl_cb(...) or return until there is no risk of blocking.
164 std::unique_lock<std::mutex> lock(mPollLock, std::try_to_lock);
165 if(!lock.owns_lock()){
166 // cannot get the lock, hidl service will go into deadlock if it is not restarted.
167 // This is guaranteed to not trigger in passthrough mode.
Ashutosh Joshi2af9dac2017-03-10 12:53:31 -0800168 LOG(ERROR) <<
Peng Xuab7884f2017-03-02 13:36:45 -0800169 "ISensors::poll() re-entry. I do not know what to do except killing myself.";
Ashutosh Joshi2af9dac2017-03-10 12:53:31 -0800170 ::exit(-1);
Peng Xuab7884f2017-03-02 13:36:45 -0800171 }
172
173 if (maxCount <= 0) {
174 err = android::BAD_VALUE;
175 } else {
176 int bufferSize = maxCount <= kPollMaxBufferSize ? maxCount : kPollMaxBufferSize;
177 data.reset(new sensors_event_t[bufferSize]);
178 err = mSensorDevice->poll(
179 reinterpret_cast<sensors_poll_device_t *>(mSensorDevice),
180 data.get(), bufferSize);
181 }
Andreas Huberdb49a412016-10-10 13:23:59 -0700182 }
183
Andreas Huberdb49a412016-10-10 13:23:59 -0700184 if (err < 0) {
Peng Xu0873e642017-01-08 13:28:10 -0800185 _hidl_cb(ResultFromStatus(err), out, dynamicSensorsAdded);
Andreas Huberdb49a412016-10-10 13:23:59 -0700186 return Void();
187 }
188
189 const size_t count = (size_t)err;
190
191 for (size_t i = 0; i < count; ++i) {
192 if (data[i].type != SENSOR_TYPE_DYNAMIC_SENSOR_META) {
193 continue;
194 }
195
196 const dynamic_sensor_meta_event_t *dyn = &data[i].dynamic_sensor_meta;
197
198 if (!dyn->connected) {
199 continue;
200 }
201
202 CHECK(dyn->sensor != nullptr);
203 CHECK_EQ(dyn->sensor->handle, dyn->handle);
204
205 SensorInfo info;
206 convertFromSensor(*dyn->sensor, &info);
207
208 size_t numDynamicSensors = dynamicSensorsAdded.size();
209 dynamicSensorsAdded.resize(numDynamicSensors + 1);
210 dynamicSensorsAdded[numDynamicSensors] = info;
211 }
212
213 out.resize(count);
214 convertFromSensorEvents(err, data.get(), &out);
215
Peng Xu0873e642017-01-08 13:28:10 -0800216 _hidl_cb(Result::OK, out, dynamicSensorsAdded);
Andreas Huberdb49a412016-10-10 13:23:59 -0700217
218 return Void();
219}
220
221Return<Result> Sensors::batch(
222 int32_t sensor_handle,
Andreas Huberdb49a412016-10-10 13:23:59 -0700223 int64_t sampling_period_ns,
224 int64_t max_report_latency_ns) {
225 return ResultFromStatus(
226 mSensorDevice->batch(
227 mSensorDevice,
228 sensor_handle,
Peng Xu1f12c7a2017-01-11 11:08:45 -0800229 0, /*flags*/
Andreas Huberdb49a412016-10-10 13:23:59 -0700230 sampling_period_ns,
231 max_report_latency_ns));
232}
233
234Return<Result> Sensors::flush(int32_t sensor_handle) {
235 return ResultFromStatus(mSensorDevice->flush(mSensorDevice, sensor_handle));
236}
237
238Return<Result> Sensors::injectSensorData(const Event& event) {
239 if (getHalDeviceVersion() < SENSORS_DEVICE_API_VERSION_1_4) {
240 return Result::INVALID_OPERATION;
241 }
242
243 sensors_event_t out;
244 convertToSensorEvent(event, &out);
245
246 return ResultFromStatus(
247 mSensorDevice->inject_sensor_data(mSensorDevice, &out));
248}
249
Peng Xu0f0df7e2017-01-05 23:39:08 -0800250Return<void> Sensors::registerDirectChannel(
Peng Xu0873e642017-01-08 13:28:10 -0800251 const SharedMemInfo& mem, registerDirectChannel_cb _hidl_cb) {
Peng Xu89df2e72017-01-09 19:12:42 -0800252 if (mSensorDevice->register_direct_channel == nullptr
253 || mSensorDevice->config_direct_report == nullptr) {
254 // HAL does not support
255 _hidl_cb(Result::INVALID_OPERATION, -1);
256 return Void();
257 }
258
259 sensors_direct_mem_t m;
260 if (!convertFromSharedMemInfo(mem, &m)) {
261 _hidl_cb(Result::BAD_VALUE, -1);
262 return Void();
263 }
264
265 int err = mSensorDevice->register_direct_channel(mSensorDevice, &m, -1);
266
267 if (err < 0) {
268 _hidl_cb(ResultFromStatus(err), -1);
269 } else {
270 int32_t channelHandle = static_cast<int32_t>(err);
271 _hidl_cb(Result::OK, channelHandle);
272 }
Peng Xu0f0df7e2017-01-05 23:39:08 -0800273 return Void();
274}
275
276Return<Result> Sensors::unregisterDirectChannel(int32_t channelHandle) {
Peng Xu89df2e72017-01-09 19:12:42 -0800277 if (mSensorDevice->register_direct_channel == nullptr
278 || mSensorDevice->config_direct_report == nullptr) {
279 // HAL does not support
280 return Result::INVALID_OPERATION;
281 }
282
283 mSensorDevice->register_direct_channel(mSensorDevice, nullptr, channelHandle);
284
285 return Result::OK;
Peng Xu0f0df7e2017-01-05 23:39:08 -0800286}
287
288Return<void> Sensors::configDirectReport(
289 int32_t sensorHandle, int32_t channelHandle, RateLevel rate,
290 configDirectReport_cb _hidl_cb) {
Peng Xu89df2e72017-01-09 19:12:42 -0800291 if (mSensorDevice->register_direct_channel == nullptr
292 || mSensorDevice->config_direct_report == nullptr) {
293 // HAL does not support
294 _hidl_cb(Result::INVALID_OPERATION, -1);
295 return Void();
296 }
Peng Xu0f0df7e2017-01-05 23:39:08 -0800297
Peng Xu89df2e72017-01-09 19:12:42 -0800298 sensors_direct_cfg_t cfg = {
299 .rate_level = convertFromRateLevel(rate)
300 };
301 if (cfg.rate_level < 0) {
302 _hidl_cb(Result::BAD_VALUE, -1);
303 return Void();
304 }
305
306 int err = mSensorDevice->config_direct_report(mSensorDevice,
307 sensorHandle, channelHandle, &cfg);
308
309 if (rate == RateLevel::STOP) {
310 _hidl_cb(ResultFromStatus(err), -1);
311 } else {
312 _hidl_cb(err > 0 ? Result::OK : ResultFromStatus(err), err);
313 }
Peng Xu0f0df7e2017-01-05 23:39:08 -0800314 return Void();
315}
316
Andreas Huberdb49a412016-10-10 13:23:59 -0700317// static
318void Sensors::convertFromSensorEvents(
319 size_t count,
320 const sensors_event_t *srcArray,
321 hidl_vec<Event> *dstVec) {
322 for (size_t i = 0; i < count; ++i) {
323 const sensors_event_t &src = srcArray[i];
324 Event *dst = &(*dstVec)[i];
325
326 convertFromSensorEvent(src, dst);
327 }
328}
329
330ISensors *HIDL_FETCH_ISensors(const char * /* hal */) {
331 Sensors *sensors = new Sensors;
332 if (sensors->initCheck() != OK) {
333 delete sensors;
334 sensors = nullptr;
335
336 return nullptr;
337 }
338
339 return sensors;
340}
341
342} // namespace implementation
343} // namespace V1_0
344} // namespace sensors
345} // namespace hardware
346} // namespace android