blob: 37e2b81d9b430daf8a476a86fb01a1ced3b59a17 [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.
168 LOG(FATAL) <<
169 "ISensors::poll() re-entry. I do not know what to do except killing myself.";
170 }
171
172 if (maxCount <= 0) {
173 err = android::BAD_VALUE;
174 } else {
175 int bufferSize = maxCount <= kPollMaxBufferSize ? maxCount : kPollMaxBufferSize;
176 data.reset(new sensors_event_t[bufferSize]);
177 err = mSensorDevice->poll(
178 reinterpret_cast<sensors_poll_device_t *>(mSensorDevice),
179 data.get(), bufferSize);
180 }
Andreas Huberdb49a412016-10-10 13:23:59 -0700181 }
182
Andreas Huberdb49a412016-10-10 13:23:59 -0700183 if (err < 0) {
Peng Xu0873e642017-01-08 13:28:10 -0800184 _hidl_cb(ResultFromStatus(err), out, dynamicSensorsAdded);
Andreas Huberdb49a412016-10-10 13:23:59 -0700185 return Void();
186 }
187
188 const size_t count = (size_t)err;
189
190 for (size_t i = 0; i < count; ++i) {
191 if (data[i].type != SENSOR_TYPE_DYNAMIC_SENSOR_META) {
192 continue;
193 }
194
195 const dynamic_sensor_meta_event_t *dyn = &data[i].dynamic_sensor_meta;
196
197 if (!dyn->connected) {
198 continue;
199 }
200
201 CHECK(dyn->sensor != nullptr);
202 CHECK_EQ(dyn->sensor->handle, dyn->handle);
203
204 SensorInfo info;
205 convertFromSensor(*dyn->sensor, &info);
206
207 size_t numDynamicSensors = dynamicSensorsAdded.size();
208 dynamicSensorsAdded.resize(numDynamicSensors + 1);
209 dynamicSensorsAdded[numDynamicSensors] = info;
210 }
211
212 out.resize(count);
213 convertFromSensorEvents(err, data.get(), &out);
214
Peng Xu0873e642017-01-08 13:28:10 -0800215 _hidl_cb(Result::OK, out, dynamicSensorsAdded);
Andreas Huberdb49a412016-10-10 13:23:59 -0700216
217 return Void();
218}
219
220Return<Result> Sensors::batch(
221 int32_t sensor_handle,
Andreas Huberdb49a412016-10-10 13:23:59 -0700222 int64_t sampling_period_ns,
223 int64_t max_report_latency_ns) {
224 return ResultFromStatus(
225 mSensorDevice->batch(
226 mSensorDevice,
227 sensor_handle,
Peng Xu1f12c7a2017-01-11 11:08:45 -0800228 0, /*flags*/
Andreas Huberdb49a412016-10-10 13:23:59 -0700229 sampling_period_ns,
230 max_report_latency_ns));
231}
232
233Return<Result> Sensors::flush(int32_t sensor_handle) {
234 return ResultFromStatus(mSensorDevice->flush(mSensorDevice, sensor_handle));
235}
236
237Return<Result> Sensors::injectSensorData(const Event& event) {
238 if (getHalDeviceVersion() < SENSORS_DEVICE_API_VERSION_1_4) {
239 return Result::INVALID_OPERATION;
240 }
241
242 sensors_event_t out;
243 convertToSensorEvent(event, &out);
244
245 return ResultFromStatus(
246 mSensorDevice->inject_sensor_data(mSensorDevice, &out));
247}
248
Peng Xu0f0df7e2017-01-05 23:39:08 -0800249Return<void> Sensors::registerDirectChannel(
Peng Xu0873e642017-01-08 13:28:10 -0800250 const SharedMemInfo& mem, registerDirectChannel_cb _hidl_cb) {
Peng Xu89df2e72017-01-09 19:12:42 -0800251 if (mSensorDevice->register_direct_channel == nullptr
252 || mSensorDevice->config_direct_report == nullptr) {
253 // HAL does not support
254 _hidl_cb(Result::INVALID_OPERATION, -1);
255 return Void();
256 }
257
258 sensors_direct_mem_t m;
259 if (!convertFromSharedMemInfo(mem, &m)) {
260 _hidl_cb(Result::BAD_VALUE, -1);
261 return Void();
262 }
263
264 int err = mSensorDevice->register_direct_channel(mSensorDevice, &m, -1);
265
266 if (err < 0) {
267 _hidl_cb(ResultFromStatus(err), -1);
268 } else {
269 int32_t channelHandle = static_cast<int32_t>(err);
270 _hidl_cb(Result::OK, channelHandle);
271 }
Peng Xu0f0df7e2017-01-05 23:39:08 -0800272 return Void();
273}
274
275Return<Result> Sensors::unregisterDirectChannel(int32_t channelHandle) {
Peng Xu89df2e72017-01-09 19:12:42 -0800276 if (mSensorDevice->register_direct_channel == nullptr
277 || mSensorDevice->config_direct_report == nullptr) {
278 // HAL does not support
279 return Result::INVALID_OPERATION;
280 }
281
282 mSensorDevice->register_direct_channel(mSensorDevice, nullptr, channelHandle);
283
284 return Result::OK;
Peng Xu0f0df7e2017-01-05 23:39:08 -0800285}
286
287Return<void> Sensors::configDirectReport(
288 int32_t sensorHandle, int32_t channelHandle, RateLevel rate,
289 configDirectReport_cb _hidl_cb) {
Peng Xu89df2e72017-01-09 19:12:42 -0800290 if (mSensorDevice->register_direct_channel == nullptr
291 || mSensorDevice->config_direct_report == nullptr) {
292 // HAL does not support
293 _hidl_cb(Result::INVALID_OPERATION, -1);
294 return Void();
295 }
Peng Xu0f0df7e2017-01-05 23:39:08 -0800296
Peng Xu89df2e72017-01-09 19:12:42 -0800297 sensors_direct_cfg_t cfg = {
298 .rate_level = convertFromRateLevel(rate)
299 };
300 if (cfg.rate_level < 0) {
301 _hidl_cb(Result::BAD_VALUE, -1);
302 return Void();
303 }
304
305 int err = mSensorDevice->config_direct_report(mSensorDevice,
306 sensorHandle, channelHandle, &cfg);
307
308 if (rate == RateLevel::STOP) {
309 _hidl_cb(ResultFromStatus(err), -1);
310 } else {
311 _hidl_cb(err > 0 ? Result::OK : ResultFromStatus(err), err);
312 }
Peng Xu0f0df7e2017-01-05 23:39:08 -0800313 return Void();
314}
315
Andreas Huberdb49a412016-10-10 13:23:59 -0700316// static
317void Sensors::convertFromSensorEvents(
318 size_t count,
319 const sensors_event_t *srcArray,
320 hidl_vec<Event> *dstVec) {
321 for (size_t i = 0; i < count; ++i) {
322 const sensors_event_t &src = srcArray[i];
323 Event *dst = &(*dstVec)[i];
324
325 convertFromSensorEvent(src, dst);
326 }
327}
328
329ISensors *HIDL_FETCH_ISensors(const char * /* hal */) {
330 Sensors *sensors = new Sensors;
331 if (sensors->initCheck() != OK) {
332 delete sensors;
333 sensors = nullptr;
334
335 return nullptr;
336 }
337
338 return sensors;
339}
340
341} // namespace implementation
342} // namespace V1_0
343} // namespace sensors
344} // namespace hardware
345} // namespace android