blob: f9f1ca6ecf4c2e24781425f9f951996e3a7c7767 [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
145Return<Result> Sensors::setDelay(
146 int32_t sensor_handle, int64_t sampling_period_ns) {
147 return ResultFromStatus(
148 mSensorDevice->setDelay(
149 reinterpret_cast<sensors_poll_device_t *>(mSensorDevice),
150 sensor_handle,
151 sampling_period_ns));
152}
153
Peng Xu0873e642017-01-08 13:28:10 -0800154Return<void> Sensors::poll(int32_t maxCount, poll_cb _hidl_cb) {
Andreas Huberdb49a412016-10-10 13:23:59 -0700155 hidl_vec<Event> out;
156 hidl_vec<SensorInfo> dynamicSensorsAdded;
157
158 if (maxCount <= 0) {
Peng Xu0873e642017-01-08 13:28:10 -0800159 _hidl_cb(Result::BAD_VALUE, out, dynamicSensorsAdded);
Andreas Huberdb49a412016-10-10 13:23:59 -0700160 return Void();
161 }
162
163 std::unique_ptr<sensors_event_t[]> data(new sensors_event_t[maxCount]);
164
165 int err = mSensorDevice->poll(
166 reinterpret_cast<sensors_poll_device_t *>(mSensorDevice),
167 data.get(),
168 maxCount);
169
170 if (err < 0) {
Peng Xu0873e642017-01-08 13:28:10 -0800171 _hidl_cb(ResultFromStatus(err), out, dynamicSensorsAdded);
Andreas Huberdb49a412016-10-10 13:23:59 -0700172 return Void();
173 }
174
175 const size_t count = (size_t)err;
176
177 for (size_t i = 0; i < count; ++i) {
178 if (data[i].type != SENSOR_TYPE_DYNAMIC_SENSOR_META) {
179 continue;
180 }
181
182 const dynamic_sensor_meta_event_t *dyn = &data[i].dynamic_sensor_meta;
183
184 if (!dyn->connected) {
185 continue;
186 }
187
188 CHECK(dyn->sensor != nullptr);
189 CHECK_EQ(dyn->sensor->handle, dyn->handle);
190
191 SensorInfo info;
192 convertFromSensor(*dyn->sensor, &info);
193
194 size_t numDynamicSensors = dynamicSensorsAdded.size();
195 dynamicSensorsAdded.resize(numDynamicSensors + 1);
196 dynamicSensorsAdded[numDynamicSensors] = info;
197 }
198
199 out.resize(count);
200 convertFromSensorEvents(err, data.get(), &out);
201
Peng Xu0873e642017-01-08 13:28:10 -0800202 _hidl_cb(Result::OK, out, dynamicSensorsAdded);
Andreas Huberdb49a412016-10-10 13:23:59 -0700203
204 return Void();
205}
206
207Return<Result> Sensors::batch(
208 int32_t sensor_handle,
209 int32_t flags,
210 int64_t sampling_period_ns,
211 int64_t max_report_latency_ns) {
212 return ResultFromStatus(
213 mSensorDevice->batch(
214 mSensorDevice,
215 sensor_handle,
216 flags,
217 sampling_period_ns,
218 max_report_latency_ns));
219}
220
221Return<Result> Sensors::flush(int32_t sensor_handle) {
222 return ResultFromStatus(mSensorDevice->flush(mSensorDevice, sensor_handle));
223}
224
225Return<Result> Sensors::injectSensorData(const Event& event) {
226 if (getHalDeviceVersion() < SENSORS_DEVICE_API_VERSION_1_4) {
227 return Result::INVALID_OPERATION;
228 }
229
230 sensors_event_t out;
231 convertToSensorEvent(event, &out);
232
233 return ResultFromStatus(
234 mSensorDevice->inject_sensor_data(mSensorDevice, &out));
235}
236
Peng Xu0f0df7e2017-01-05 23:39:08 -0800237Return<void> Sensors::registerDirectChannel(
Peng Xu0873e642017-01-08 13:28:10 -0800238 const SharedMemInfo& mem, registerDirectChannel_cb _hidl_cb) {
Peng Xu0f0df7e2017-01-05 23:39:08 -0800239 //TODO(b/30985702): finish implementation
240 (void) mem;
Peng Xu0873e642017-01-08 13:28:10 -0800241 _hidl_cb(Result::INVALID_OPERATION, -1);
Peng Xu0f0df7e2017-01-05 23:39:08 -0800242 return Void();
243}
244
245Return<Result> Sensors::unregisterDirectChannel(int32_t channelHandle) {
246 //TODO(b/30985702): finish implementation
247 (void) channelHandle;
248 return Result::INVALID_OPERATION;
249}
250
251Return<void> Sensors::configDirectReport(
252 int32_t sensorHandle, int32_t channelHandle, RateLevel rate,
253 configDirectReport_cb _hidl_cb) {
254 //TODO(b/30985702): finish implementation
255 (void) sensorHandle;
256 (void) channelHandle;
257 (void) rate;
258
259 _hidl_cb(Result::INVALID_OPERATION, -1);
260 return Void();
261}
262
Andreas Huberdb49a412016-10-10 13:23:59 -0700263// static
264void Sensors::convertFromSensorEvents(
265 size_t count,
266 const sensors_event_t *srcArray,
267 hidl_vec<Event> *dstVec) {
268 for (size_t i = 0; i < count; ++i) {
269 const sensors_event_t &src = srcArray[i];
270 Event *dst = &(*dstVec)[i];
271
272 convertFromSensorEvent(src, dst);
273 }
274}
275
276ISensors *HIDL_FETCH_ISensors(const char * /* hal */) {
277 Sensors *sensors = new Sensors;
278 if (sensors->initCheck() != OK) {
279 delete sensors;
280 sensors = nullptr;
281
282 return nullptr;
283 }
284
285 return sensors;
286}
287
288} // namespace implementation
289} // namespace V1_0
290} // namespace sensors
291} // namespace hardware
292} // namespace android