blob: aadf84888e4fbae3f4fc1cf277bd6999c3eb4ea0 [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
156 std::unique_ptr<sensors_event_t[]> data(new sensors_event_t[maxCount]);
157
158 int err = mSensorDevice->poll(
159 reinterpret_cast<sensors_poll_device_t *>(mSensorDevice),
160 data.get(),
161 maxCount);
162
163 if (err < 0) {
Peng Xu0873e642017-01-08 13:28:10 -0800164 _hidl_cb(ResultFromStatus(err), out, dynamicSensorsAdded);
Andreas Huberdb49a412016-10-10 13:23:59 -0700165 return Void();
166 }
167
168 const size_t count = (size_t)err;
169
170 for (size_t i = 0; i < count; ++i) {
171 if (data[i].type != SENSOR_TYPE_DYNAMIC_SENSOR_META) {
172 continue;
173 }
174
175 const dynamic_sensor_meta_event_t *dyn = &data[i].dynamic_sensor_meta;
176
177 if (!dyn->connected) {
178 continue;
179 }
180
181 CHECK(dyn->sensor != nullptr);
182 CHECK_EQ(dyn->sensor->handle, dyn->handle);
183
184 SensorInfo info;
185 convertFromSensor(*dyn->sensor, &info);
186
187 size_t numDynamicSensors = dynamicSensorsAdded.size();
188 dynamicSensorsAdded.resize(numDynamicSensors + 1);
189 dynamicSensorsAdded[numDynamicSensors] = info;
190 }
191
192 out.resize(count);
193 convertFromSensorEvents(err, data.get(), &out);
194
Peng Xu0873e642017-01-08 13:28:10 -0800195 _hidl_cb(Result::OK, out, dynamicSensorsAdded);
Andreas Huberdb49a412016-10-10 13:23:59 -0700196
197 return Void();
198}
199
200Return<Result> Sensors::batch(
201 int32_t sensor_handle,
Andreas Huberdb49a412016-10-10 13:23:59 -0700202 int64_t sampling_period_ns,
203 int64_t max_report_latency_ns) {
204 return ResultFromStatus(
205 mSensorDevice->batch(
206 mSensorDevice,
207 sensor_handle,
Peng Xu1f12c7a2017-01-11 11:08:45 -0800208 0, /*flags*/
Andreas Huberdb49a412016-10-10 13:23:59 -0700209 sampling_period_ns,
210 max_report_latency_ns));
211}
212
213Return<Result> Sensors::flush(int32_t sensor_handle) {
214 return ResultFromStatus(mSensorDevice->flush(mSensorDevice, sensor_handle));
215}
216
217Return<Result> Sensors::injectSensorData(const Event& event) {
218 if (getHalDeviceVersion() < SENSORS_DEVICE_API_VERSION_1_4) {
219 return Result::INVALID_OPERATION;
220 }
221
222 sensors_event_t out;
223 convertToSensorEvent(event, &out);
224
225 return ResultFromStatus(
226 mSensorDevice->inject_sensor_data(mSensorDevice, &out));
227}
228
Peng Xu0f0df7e2017-01-05 23:39:08 -0800229Return<void> Sensors::registerDirectChannel(
Peng Xu0873e642017-01-08 13:28:10 -0800230 const SharedMemInfo& mem, registerDirectChannel_cb _hidl_cb) {
Peng Xu89df2e72017-01-09 19:12:42 -0800231 if (mSensorDevice->register_direct_channel == nullptr
232 || mSensorDevice->config_direct_report == nullptr) {
233 // HAL does not support
234 _hidl_cb(Result::INVALID_OPERATION, -1);
235 return Void();
236 }
237
238 sensors_direct_mem_t m;
239 if (!convertFromSharedMemInfo(mem, &m)) {
240 _hidl_cb(Result::BAD_VALUE, -1);
241 return Void();
242 }
243
244 int err = mSensorDevice->register_direct_channel(mSensorDevice, &m, -1);
245
246 if (err < 0) {
247 _hidl_cb(ResultFromStatus(err), -1);
248 } else {
249 int32_t channelHandle = static_cast<int32_t>(err);
250 _hidl_cb(Result::OK, channelHandle);
251 }
Peng Xu0f0df7e2017-01-05 23:39:08 -0800252 return Void();
253}
254
255Return<Result> Sensors::unregisterDirectChannel(int32_t channelHandle) {
Peng Xu89df2e72017-01-09 19:12:42 -0800256 if (mSensorDevice->register_direct_channel == nullptr
257 || mSensorDevice->config_direct_report == nullptr) {
258 // HAL does not support
259 return Result::INVALID_OPERATION;
260 }
261
262 mSensorDevice->register_direct_channel(mSensorDevice, nullptr, channelHandle);
263
264 return Result::OK;
Peng Xu0f0df7e2017-01-05 23:39:08 -0800265}
266
267Return<void> Sensors::configDirectReport(
268 int32_t sensorHandle, int32_t channelHandle, RateLevel rate,
269 configDirectReport_cb _hidl_cb) {
Peng Xu89df2e72017-01-09 19:12:42 -0800270 if (mSensorDevice->register_direct_channel == nullptr
271 || mSensorDevice->config_direct_report == nullptr) {
272 // HAL does not support
273 _hidl_cb(Result::INVALID_OPERATION, -1);
274 return Void();
275 }
Peng Xu0f0df7e2017-01-05 23:39:08 -0800276
Peng Xu89df2e72017-01-09 19:12:42 -0800277 sensors_direct_cfg_t cfg = {
278 .rate_level = convertFromRateLevel(rate)
279 };
280 if (cfg.rate_level < 0) {
281 _hidl_cb(Result::BAD_VALUE, -1);
282 return Void();
283 }
284
285 int err = mSensorDevice->config_direct_report(mSensorDevice,
286 sensorHandle, channelHandle, &cfg);
287
288 if (rate == RateLevel::STOP) {
289 _hidl_cb(ResultFromStatus(err), -1);
290 } else {
291 _hidl_cb(err > 0 ? Result::OK : ResultFromStatus(err), err);
292 }
Peng Xu0f0df7e2017-01-05 23:39:08 -0800293 return Void();
294}
295
Andreas Huberdb49a412016-10-10 13:23:59 -0700296// static
297void Sensors::convertFromSensorEvents(
298 size_t count,
299 const sensors_event_t *srcArray,
300 hidl_vec<Event> *dstVec) {
301 for (size_t i = 0; i < count; ++i) {
302 const sensors_event_t &src = srcArray[i];
303 Event *dst = &(*dstVec)[i];
304
305 convertFromSensorEvent(src, dst);
306 }
307}
308
309ISensors *HIDL_FETCH_ISensors(const char * /* hal */) {
310 Sensors *sensors = new Sensors;
311 if (sensors->initCheck() != OK) {
312 delete sensors;
313 sensors = nullptr;
314
315 return nullptr;
316 }
317
318 return sensors;
319}
320
321} // namespace implementation
322} // namespace V1_0
323} // namespace sensors
324} // namespace hardware
325} // namespace android