blob: e4ef99db16e91a296616a6081492f05c177ba048 [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;
45 case BAD_VALUE:
46 return Result::BAD_VALUE;
47 case PERMISSION_DENIED:
48 return Result::PERMISSION_DENIED;
49 default:
50 return Result::INVALID_OPERATION;
51 }
52}
53
54Sensors::Sensors()
55 : mInitCheck(NO_INIT),
56 mSensorModule(nullptr),
57 mSensorDevice(nullptr) {
Nick Vaccarod133e4c2016-11-17 00:14:07 -080058 status_t err = OK;
59 if (UseMultiHal()) {
60 mSensorModule = ::get_multi_hal_module_info();
61 } else {
62 err = hw_get_module(
Andreas Huberdb49a412016-10-10 13:23:59 -070063 SENSORS_HARDWARE_MODULE_ID,
64 (hw_module_t const **)&mSensorModule);
Nick Vaccarod133e4c2016-11-17 00:14:07 -080065 }
Andreas Huberdb49a412016-10-10 13:23:59 -070066 if (mSensorModule == NULL) {
67 err = UNKNOWN_ERROR;
68 }
69
70 if (err != OK) {
71 LOG(ERROR) << "Couldn't load "
72 << SENSORS_HARDWARE_MODULE_ID
73 << " module ("
74 << strerror(-err)
75 << ")";
76
77 mInitCheck = err;
78 return;
79 }
80
81 err = sensors_open_1(&mSensorModule->common, &mSensorDevice);
82
83 if (err != OK) {
84 LOG(ERROR) << "Couldn't open device for module "
85 << SENSORS_HARDWARE_MODULE_ID
86 << " ("
87 << strerror(-err)
88 << ")";
89
90 mInitCheck = err;
91 return;
92 }
93
94 // Require all the old HAL APIs to be present except for injection, which
95 // is considered optional.
96 CHECK_GE(getHalDeviceVersion(), SENSORS_DEVICE_API_VERSION_1_3);
97
98 mInitCheck = OK;
99}
100
101status_t Sensors::initCheck() const {
102 return mInitCheck;
103}
104
Peng Xu0873e642017-01-08 13:28:10 -0800105Return<void> Sensors::getSensorsList(getSensorsList_cb _hidl_cb) {
Andreas Huberdb49a412016-10-10 13:23:59 -0700106 sensor_t const *list;
107 size_t count = mSensorModule->get_sensors_list(mSensorModule, &list);
108
109 hidl_vec<SensorInfo> out;
110 out.resize(count);
111
112 for (size_t i = 0; i < count; ++i) {
113 const sensor_t *src = &list[i];
114 SensorInfo *dst = &out[i];
115
116 convertFromSensor(*src, dst);
117 }
118
Peng Xu0873e642017-01-08 13:28:10 -0800119 _hidl_cb(out);
Andreas Huberdb49a412016-10-10 13:23:59 -0700120
121 return Void();
122}
123
124int Sensors::getHalDeviceVersion() const {
125 if (!mSensorDevice) {
126 return -1;
127 }
128
129 return mSensorDevice->common.version;
130}
131
132Return<Result> Sensors::setOperationMode(OperationMode mode) {
133 return ResultFromStatus(mSensorModule->set_operation_mode((uint32_t)mode));
134}
135
136Return<Result> Sensors::activate(
137 int32_t sensor_handle, bool enabled) {
138 return ResultFromStatus(
139 mSensorDevice->activate(
140 reinterpret_cast<sensors_poll_device_t *>(mSensorDevice),
141 sensor_handle,
142 enabled));
143}
144
Peng Xu0873e642017-01-08 13:28:10 -0800145Return<void> Sensors::poll(int32_t maxCount, poll_cb _hidl_cb) {
Andreas Huberdb49a412016-10-10 13:23:59 -0700146 hidl_vec<Event> out;
147 hidl_vec<SensorInfo> dynamicSensorsAdded;
148
149 if (maxCount <= 0) {
Peng Xu0873e642017-01-08 13:28:10 -0800150 _hidl_cb(Result::BAD_VALUE, out, dynamicSensorsAdded);
Andreas Huberdb49a412016-10-10 13:23:59 -0700151 return Void();
152 }
153
Peng Xucefa91b2017-01-16 03:10:40 -0800154 int bufferSize = maxCount <= kPollMaxBufferSize ? maxCount : kPollMaxBufferSize;
155
156 std::unique_ptr<sensors_event_t[]> data(new sensors_event_t[bufferSize]);
Andreas Huberdb49a412016-10-10 13:23:59 -0700157
158 int err = mSensorDevice->poll(
159 reinterpret_cast<sensors_poll_device_t *>(mSensorDevice),
Peng Xucefa91b2017-01-16 03:10:40 -0800160 data.get(), bufferSize);
Andreas Huberdb49a412016-10-10 13:23:59 -0700161
162 if (err < 0) {
Peng Xu0873e642017-01-08 13:28:10 -0800163 _hidl_cb(ResultFromStatus(err), out, dynamicSensorsAdded);
Andreas Huberdb49a412016-10-10 13:23:59 -0700164 return Void();
165 }
166
167 const size_t count = (size_t)err;
168
169 for (size_t i = 0; i < count; ++i) {
170 if (data[i].type != SENSOR_TYPE_DYNAMIC_SENSOR_META) {
171 continue;
172 }
173
174 const dynamic_sensor_meta_event_t *dyn = &data[i].dynamic_sensor_meta;
175
176 if (!dyn->connected) {
177 continue;
178 }
179
180 CHECK(dyn->sensor != nullptr);
181 CHECK_EQ(dyn->sensor->handle, dyn->handle);
182
183 SensorInfo info;
184 convertFromSensor(*dyn->sensor, &info);
185
186 size_t numDynamicSensors = dynamicSensorsAdded.size();
187 dynamicSensorsAdded.resize(numDynamicSensors + 1);
188 dynamicSensorsAdded[numDynamicSensors] = info;
189 }
190
191 out.resize(count);
192 convertFromSensorEvents(err, data.get(), &out);
193
Peng Xu0873e642017-01-08 13:28:10 -0800194 _hidl_cb(Result::OK, out, dynamicSensorsAdded);
Andreas Huberdb49a412016-10-10 13:23:59 -0700195
196 return Void();
197}
198
199Return<Result> Sensors::batch(
200 int32_t sensor_handle,
Andreas Huberdb49a412016-10-10 13:23:59 -0700201 int64_t sampling_period_ns,
202 int64_t max_report_latency_ns) {
203 return ResultFromStatus(
204 mSensorDevice->batch(
205 mSensorDevice,
206 sensor_handle,
Peng Xu1f12c7a2017-01-11 11:08:45 -0800207 0, /*flags*/
Andreas Huberdb49a412016-10-10 13:23:59 -0700208 sampling_period_ns,
209 max_report_latency_ns));
210}
211
212Return<Result> Sensors::flush(int32_t sensor_handle) {
213 return ResultFromStatus(mSensorDevice->flush(mSensorDevice, sensor_handle));
214}
215
216Return<Result> Sensors::injectSensorData(const Event& event) {
217 if (getHalDeviceVersion() < SENSORS_DEVICE_API_VERSION_1_4) {
218 return Result::INVALID_OPERATION;
219 }
220
221 sensors_event_t out;
222 convertToSensorEvent(event, &out);
223
224 return ResultFromStatus(
225 mSensorDevice->inject_sensor_data(mSensorDevice, &out));
226}
227
Peng Xu0f0df7e2017-01-05 23:39:08 -0800228Return<void> Sensors::registerDirectChannel(
Peng Xu0873e642017-01-08 13:28:10 -0800229 const SharedMemInfo& mem, registerDirectChannel_cb _hidl_cb) {
Peng Xu0f0df7e2017-01-05 23:39:08 -0800230 //TODO(b/30985702): finish implementation
231 (void) mem;
Peng Xu0873e642017-01-08 13:28:10 -0800232 _hidl_cb(Result::INVALID_OPERATION, -1);
Peng Xu0f0df7e2017-01-05 23:39:08 -0800233 return Void();
234}
235
236Return<Result> Sensors::unregisterDirectChannel(int32_t channelHandle) {
237 //TODO(b/30985702): finish implementation
238 (void) channelHandle;
239 return Result::INVALID_OPERATION;
240}
241
242Return<void> Sensors::configDirectReport(
243 int32_t sensorHandle, int32_t channelHandle, RateLevel rate,
244 configDirectReport_cb _hidl_cb) {
245 //TODO(b/30985702): finish implementation
246 (void) sensorHandle;
247 (void) channelHandle;
248 (void) rate;
249
250 _hidl_cb(Result::INVALID_OPERATION, -1);
251 return Void();
252}
253
Andreas Huberdb49a412016-10-10 13:23:59 -0700254// static
255void Sensors::convertFromSensorEvents(
256 size_t count,
257 const sensors_event_t *srcArray,
258 hidl_vec<Event> *dstVec) {
259 for (size_t i = 0; i < count; ++i) {
260 const sensors_event_t &src = srcArray[i];
261 Event *dst = &(*dstVec)[i];
262
263 convertFromSensorEvent(src, dst);
264 }
265}
266
267ISensors *HIDL_FETCH_ISensors(const char * /* hal */) {
268 Sensors *sensors = new Sensors;
269 if (sensors->initCheck() != OK) {
270 delete sensors;
271 sensors = nullptr;
272
273 return nullptr;
274 }
275
276 return sensors;
277}
278
279} // namespace implementation
280} // namespace V1_0
281} // namespace sensors
282} // namespace hardware
283} // namespace android