blob: 41eb945167b730afa01e0c3bb1b2561aaeb4ee87 [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) {
Andreas Huberdb49a412016-10-10 13:23:59 -0700148 hidl_vec<Event> out;
149 hidl_vec<SensorInfo> dynamicSensorsAdded;
150
151 if (maxCount <= 0) {
Peng Xu0873e642017-01-08 13:28:10 -0800152 _hidl_cb(Result::BAD_VALUE, out, dynamicSensorsAdded);
Andreas Huberdb49a412016-10-10 13:23:59 -0700153 return Void();
154 }
155
Peng Xucefa91b2017-01-16 03:10:40 -0800156 int bufferSize = maxCount <= kPollMaxBufferSize ? maxCount : kPollMaxBufferSize;
157
158 std::unique_ptr<sensors_event_t[]> data(new sensors_event_t[bufferSize]);
Andreas Huberdb49a412016-10-10 13:23:59 -0700159
160 int err = mSensorDevice->poll(
161 reinterpret_cast<sensors_poll_device_t *>(mSensorDevice),
Peng Xucefa91b2017-01-16 03:10:40 -0800162 data.get(), bufferSize);
Andreas Huberdb49a412016-10-10 13:23:59 -0700163
164 if (err < 0) {
Peng Xu0873e642017-01-08 13:28:10 -0800165 _hidl_cb(ResultFromStatus(err), out, dynamicSensorsAdded);
Andreas Huberdb49a412016-10-10 13:23:59 -0700166 return Void();
167 }
168
169 const size_t count = (size_t)err;
170
171 for (size_t i = 0; i < count; ++i) {
172 if (data[i].type != SENSOR_TYPE_DYNAMIC_SENSOR_META) {
173 continue;
174 }
175
176 const dynamic_sensor_meta_event_t *dyn = &data[i].dynamic_sensor_meta;
177
178 if (!dyn->connected) {
179 continue;
180 }
181
182 CHECK(dyn->sensor != nullptr);
183 CHECK_EQ(dyn->sensor->handle, dyn->handle);
184
185 SensorInfo info;
186 convertFromSensor(*dyn->sensor, &info);
187
188 size_t numDynamicSensors = dynamicSensorsAdded.size();
189 dynamicSensorsAdded.resize(numDynamicSensors + 1);
190 dynamicSensorsAdded[numDynamicSensors] = info;
191 }
192
193 out.resize(count);
194 convertFromSensorEvents(err, data.get(), &out);
195
Peng Xu0873e642017-01-08 13:28:10 -0800196 _hidl_cb(Result::OK, out, dynamicSensorsAdded);
Andreas Huberdb49a412016-10-10 13:23:59 -0700197
198 return Void();
199}
200
201Return<Result> Sensors::batch(
202 int32_t sensor_handle,
Andreas Huberdb49a412016-10-10 13:23:59 -0700203 int64_t sampling_period_ns,
204 int64_t max_report_latency_ns) {
205 return ResultFromStatus(
206 mSensorDevice->batch(
207 mSensorDevice,
208 sensor_handle,
Peng Xu1f12c7a2017-01-11 11:08:45 -0800209 0, /*flags*/
Andreas Huberdb49a412016-10-10 13:23:59 -0700210 sampling_period_ns,
211 max_report_latency_ns));
212}
213
214Return<Result> Sensors::flush(int32_t sensor_handle) {
215 return ResultFromStatus(mSensorDevice->flush(mSensorDevice, sensor_handle));
216}
217
218Return<Result> Sensors::injectSensorData(const Event& event) {
219 if (getHalDeviceVersion() < SENSORS_DEVICE_API_VERSION_1_4) {
220 return Result::INVALID_OPERATION;
221 }
222
223 sensors_event_t out;
224 convertToSensorEvent(event, &out);
225
226 return ResultFromStatus(
227 mSensorDevice->inject_sensor_data(mSensorDevice, &out));
228}
229
Peng Xu0f0df7e2017-01-05 23:39:08 -0800230Return<void> Sensors::registerDirectChannel(
Peng Xu0873e642017-01-08 13:28:10 -0800231 const SharedMemInfo& mem, registerDirectChannel_cb _hidl_cb) {
Peng Xu89df2e72017-01-09 19:12:42 -0800232 if (mSensorDevice->register_direct_channel == nullptr
233 || mSensorDevice->config_direct_report == nullptr) {
234 // HAL does not support
235 _hidl_cb(Result::INVALID_OPERATION, -1);
236 return Void();
237 }
238
239 sensors_direct_mem_t m;
240 if (!convertFromSharedMemInfo(mem, &m)) {
241 _hidl_cb(Result::BAD_VALUE, -1);
242 return Void();
243 }
244
245 int err = mSensorDevice->register_direct_channel(mSensorDevice, &m, -1);
246
247 if (err < 0) {
248 _hidl_cb(ResultFromStatus(err), -1);
249 } else {
250 int32_t channelHandle = static_cast<int32_t>(err);
251 _hidl_cb(Result::OK, channelHandle);
252 }
Peng Xu0f0df7e2017-01-05 23:39:08 -0800253 return Void();
254}
255
256Return<Result> Sensors::unregisterDirectChannel(int32_t channelHandle) {
Peng Xu89df2e72017-01-09 19:12:42 -0800257 if (mSensorDevice->register_direct_channel == nullptr
258 || mSensorDevice->config_direct_report == nullptr) {
259 // HAL does not support
260 return Result::INVALID_OPERATION;
261 }
262
263 mSensorDevice->register_direct_channel(mSensorDevice, nullptr, channelHandle);
264
265 return Result::OK;
Peng Xu0f0df7e2017-01-05 23:39:08 -0800266}
267
268Return<void> Sensors::configDirectReport(
269 int32_t sensorHandle, int32_t channelHandle, RateLevel rate,
270 configDirectReport_cb _hidl_cb) {
Peng Xu89df2e72017-01-09 19:12:42 -0800271 if (mSensorDevice->register_direct_channel == nullptr
272 || mSensorDevice->config_direct_report == nullptr) {
273 // HAL does not support
274 _hidl_cb(Result::INVALID_OPERATION, -1);
275 return Void();
276 }
Peng Xu0f0df7e2017-01-05 23:39:08 -0800277
Peng Xu89df2e72017-01-09 19:12:42 -0800278 sensors_direct_cfg_t cfg = {
279 .rate_level = convertFromRateLevel(rate)
280 };
281 if (cfg.rate_level < 0) {
282 _hidl_cb(Result::BAD_VALUE, -1);
283 return Void();
284 }
285
286 int err = mSensorDevice->config_direct_report(mSensorDevice,
287 sensorHandle, channelHandle, &cfg);
288
289 if (rate == RateLevel::STOP) {
290 _hidl_cb(ResultFromStatus(err), -1);
291 } else {
292 _hidl_cb(err > 0 ? Result::OK : ResultFromStatus(err), err);
293 }
Peng Xu0f0df7e2017-01-05 23:39:08 -0800294 return Void();
295}
296
Andreas Huberdb49a412016-10-10 13:23:59 -0700297// static
298void Sensors::convertFromSensorEvents(
299 size_t count,
300 const sensors_event_t *srcArray,
301 hidl_vec<Event> *dstVec) {
302 for (size_t i = 0; i < count; ++i) {
303 const sensors_event_t &src = srcArray[i];
304 Event *dst = &(*dstVec)[i];
305
306 convertFromSensorEvent(src, dst);
307 }
308}
309
310ISensors *HIDL_FETCH_ISensors(const char * /* hal */) {
311 Sensors *sensors = new Sensors;
312 if (sensors->initCheck() != OK) {
313 delete sensors;
314 sensors = nullptr;
315
316 return nullptr;
317 }
318
319 return sensors;
320}
321
322} // namespace implementation
323} // namespace V1_0
324} // namespace sensors
325} // namespace hardware
326} // namespace android