blob: 8903397a2bf77fe1a0964ac00d2e46f3a7aa595d [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
154 std::unique_ptr<sensors_event_t[]> data(new sensors_event_t[maxCount]);
155
156 int err = mSensorDevice->poll(
157 reinterpret_cast<sensors_poll_device_t *>(mSensorDevice),
158 data.get(),
159 maxCount);
160
161 if (err < 0) {
Peng Xu0873e642017-01-08 13:28:10 -0800162 _hidl_cb(ResultFromStatus(err), out, dynamicSensorsAdded);
Andreas Huberdb49a412016-10-10 13:23:59 -0700163 return Void();
164 }
165
166 const size_t count = (size_t)err;
167
168 for (size_t i = 0; i < count; ++i) {
169 if (data[i].type != SENSOR_TYPE_DYNAMIC_SENSOR_META) {
170 continue;
171 }
172
173 const dynamic_sensor_meta_event_t *dyn = &data[i].dynamic_sensor_meta;
174
175 if (!dyn->connected) {
176 continue;
177 }
178
179 CHECK(dyn->sensor != nullptr);
180 CHECK_EQ(dyn->sensor->handle, dyn->handle);
181
182 SensorInfo info;
183 convertFromSensor(*dyn->sensor, &info);
184
185 size_t numDynamicSensors = dynamicSensorsAdded.size();
186 dynamicSensorsAdded.resize(numDynamicSensors + 1);
187 dynamicSensorsAdded[numDynamicSensors] = info;
188 }
189
190 out.resize(count);
191 convertFromSensorEvents(err, data.get(), &out);
192
Peng Xu0873e642017-01-08 13:28:10 -0800193 _hidl_cb(Result::OK, out, dynamicSensorsAdded);
Andreas Huberdb49a412016-10-10 13:23:59 -0700194
195 return Void();
196}
197
198Return<Result> Sensors::batch(
199 int32_t sensor_handle,
Andreas Huberdb49a412016-10-10 13:23:59 -0700200 int64_t sampling_period_ns,
201 int64_t max_report_latency_ns) {
202 return ResultFromStatus(
203 mSensorDevice->batch(
204 mSensorDevice,
205 sensor_handle,
Peng Xu1f12c7a2017-01-11 11:08:45 -0800206 0, /*flags*/
Andreas Huberdb49a412016-10-10 13:23:59 -0700207 sampling_period_ns,
208 max_report_latency_ns));
209}
210
211Return<Result> Sensors::flush(int32_t sensor_handle) {
212 return ResultFromStatus(mSensorDevice->flush(mSensorDevice, sensor_handle));
213}
214
215Return<Result> Sensors::injectSensorData(const Event& event) {
216 if (getHalDeviceVersion() < SENSORS_DEVICE_API_VERSION_1_4) {
217 return Result::INVALID_OPERATION;
218 }
219
220 sensors_event_t out;
221 convertToSensorEvent(event, &out);
222
223 return ResultFromStatus(
224 mSensorDevice->inject_sensor_data(mSensorDevice, &out));
225}
226
Peng Xu0f0df7e2017-01-05 23:39:08 -0800227Return<void> Sensors::registerDirectChannel(
Peng Xu0873e642017-01-08 13:28:10 -0800228 const SharedMemInfo& mem, registerDirectChannel_cb _hidl_cb) {
Peng Xu0f0df7e2017-01-05 23:39:08 -0800229 //TODO(b/30985702): finish implementation
230 (void) mem;
Peng Xu0873e642017-01-08 13:28:10 -0800231 _hidl_cb(Result::INVALID_OPERATION, -1);
Peng Xu0f0df7e2017-01-05 23:39:08 -0800232 return Void();
233}
234
235Return<Result> Sensors::unregisterDirectChannel(int32_t channelHandle) {
236 //TODO(b/30985702): finish implementation
237 (void) channelHandle;
238 return Result::INVALID_OPERATION;
239}
240
241Return<void> Sensors::configDirectReport(
242 int32_t sensorHandle, int32_t channelHandle, RateLevel rate,
243 configDirectReport_cb _hidl_cb) {
244 //TODO(b/30985702): finish implementation
245 (void) sensorHandle;
246 (void) channelHandle;
247 (void) rate;
248
249 _hidl_cb(Result::INVALID_OPERATION, -1);
250 return Void();
251}
252
Andreas Huberdb49a412016-10-10 13:23:59 -0700253// static
254void Sensors::convertFromSensorEvents(
255 size_t count,
256 const sensors_event_t *srcArray,
257 hidl_vec<Event> *dstVec) {
258 for (size_t i = 0; i < count; ++i) {
259 const sensors_event_t &src = srcArray[i];
260 Event *dst = &(*dstVec)[i];
261
262 convertFromSensorEvent(src, dst);
263 }
264}
265
266ISensors *HIDL_FETCH_ISensors(const char * /* hal */) {
267 Sensors *sensors = new Sensors;
268 if (sensors->initCheck() != OK) {
269 delete sensors;
270 sensors = nullptr;
271
272 return nullptr;
273 }
274
275 return sensors;
276}
277
278} // namespace implementation
279} // namespace V1_0
280} // namespace sensors
281} // namespace hardware
282} // namespace android