blob: ef052c3edd135d5cb28891a6e156ea85a41300d8 [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"
18
19#include "convert.h"
20
21#include <android-base/logging.h>
22
23namespace android {
24namespace hardware {
25namespace sensors {
26namespace V1_0 {
27namespace implementation {
28
29static Result ResultFromStatus(status_t err) {
30 switch (err) {
31 case OK:
32 return Result::OK;
33 case BAD_VALUE:
34 return Result::BAD_VALUE;
35 case PERMISSION_DENIED:
36 return Result::PERMISSION_DENIED;
37 default:
38 return Result::INVALID_OPERATION;
39 }
40}
41
42Sensors::Sensors()
43 : mInitCheck(NO_INIT),
44 mSensorModule(nullptr),
45 mSensorDevice(nullptr) {
46 status_t err = hw_get_module(
47 SENSORS_HARDWARE_MODULE_ID,
48 (hw_module_t const **)&mSensorModule);
49
50 if (mSensorModule == NULL) {
51 err = UNKNOWN_ERROR;
52 }
53
54 if (err != OK) {
55 LOG(ERROR) << "Couldn't load "
56 << SENSORS_HARDWARE_MODULE_ID
57 << " module ("
58 << strerror(-err)
59 << ")";
60
61 mInitCheck = err;
62 return;
63 }
64
65 err = sensors_open_1(&mSensorModule->common, &mSensorDevice);
66
67 if (err != OK) {
68 LOG(ERROR) << "Couldn't open device for module "
69 << SENSORS_HARDWARE_MODULE_ID
70 << " ("
71 << strerror(-err)
72 << ")";
73
74 mInitCheck = err;
75 return;
76 }
77
78 // Require all the old HAL APIs to be present except for injection, which
79 // is considered optional.
80 CHECK_GE(getHalDeviceVersion(), SENSORS_DEVICE_API_VERSION_1_3);
81
82 mInitCheck = OK;
83}
84
85status_t Sensors::initCheck() const {
86 return mInitCheck;
87}
88
89Return<void> Sensors::getSensorsList(getSensorsList_cb _aidl_cb) {
90 sensor_t const *list;
91 size_t count = mSensorModule->get_sensors_list(mSensorModule, &list);
92
93 hidl_vec<SensorInfo> out;
94 out.resize(count);
95
96 for (size_t i = 0; i < count; ++i) {
97 const sensor_t *src = &list[i];
98 SensorInfo *dst = &out[i];
99
100 convertFromSensor(*src, dst);
101 }
102
103 _aidl_cb(out);
104
105 return Void();
106}
107
108int Sensors::getHalDeviceVersion() const {
109 if (!mSensorDevice) {
110 return -1;
111 }
112
113 return mSensorDevice->common.version;
114}
115
116Return<Result> Sensors::setOperationMode(OperationMode mode) {
117 return ResultFromStatus(mSensorModule->set_operation_mode((uint32_t)mode));
118}
119
120Return<Result> Sensors::activate(
121 int32_t sensor_handle, bool enabled) {
122 return ResultFromStatus(
123 mSensorDevice->activate(
124 reinterpret_cast<sensors_poll_device_t *>(mSensorDevice),
125 sensor_handle,
126 enabled));
127}
128
129Return<Result> Sensors::setDelay(
130 int32_t sensor_handle, int64_t sampling_period_ns) {
131 return ResultFromStatus(
132 mSensorDevice->setDelay(
133 reinterpret_cast<sensors_poll_device_t *>(mSensorDevice),
134 sensor_handle,
135 sampling_period_ns));
136}
137
138Return<void> Sensors::poll(int32_t maxCount, poll_cb _aidl_cb) {
139 hidl_vec<Event> out;
140 hidl_vec<SensorInfo> dynamicSensorsAdded;
141
142 if (maxCount <= 0) {
143 _aidl_cb(Result::BAD_VALUE, out, dynamicSensorsAdded);
144 return Void();
145 }
146
147 std::unique_ptr<sensors_event_t[]> data(new sensors_event_t[maxCount]);
148
149 int err = mSensorDevice->poll(
150 reinterpret_cast<sensors_poll_device_t *>(mSensorDevice),
151 data.get(),
152 maxCount);
153
154 if (err < 0) {
155 _aidl_cb(ResultFromStatus(err), out, dynamicSensorsAdded);
156 return Void();
157 }
158
159 const size_t count = (size_t)err;
160
161 for (size_t i = 0; i < count; ++i) {
162 if (data[i].type != SENSOR_TYPE_DYNAMIC_SENSOR_META) {
163 continue;
164 }
165
166 const dynamic_sensor_meta_event_t *dyn = &data[i].dynamic_sensor_meta;
167
168 if (!dyn->connected) {
169 continue;
170 }
171
172 CHECK(dyn->sensor != nullptr);
173 CHECK_EQ(dyn->sensor->handle, dyn->handle);
174
175 SensorInfo info;
176 convertFromSensor(*dyn->sensor, &info);
177
178 size_t numDynamicSensors = dynamicSensorsAdded.size();
179 dynamicSensorsAdded.resize(numDynamicSensors + 1);
180 dynamicSensorsAdded[numDynamicSensors] = info;
181 }
182
183 out.resize(count);
184 convertFromSensorEvents(err, data.get(), &out);
185
186 _aidl_cb(Result::OK, out, dynamicSensorsAdded);
187
188 return Void();
189}
190
191Return<Result> Sensors::batch(
192 int32_t sensor_handle,
193 int32_t flags,
194 int64_t sampling_period_ns,
195 int64_t max_report_latency_ns) {
196 return ResultFromStatus(
197 mSensorDevice->batch(
198 mSensorDevice,
199 sensor_handle,
200 flags,
201 sampling_period_ns,
202 max_report_latency_ns));
203}
204
205Return<Result> Sensors::flush(int32_t sensor_handle) {
206 return ResultFromStatus(mSensorDevice->flush(mSensorDevice, sensor_handle));
207}
208
209Return<Result> Sensors::injectSensorData(const Event& event) {
210 if (getHalDeviceVersion() < SENSORS_DEVICE_API_VERSION_1_4) {
211 return Result::INVALID_OPERATION;
212 }
213
214 sensors_event_t out;
215 convertToSensorEvent(event, &out);
216
217 return ResultFromStatus(
218 mSensorDevice->inject_sensor_data(mSensorDevice, &out));
219}
220
221// static
222void Sensors::convertFromSensorEvents(
223 size_t count,
224 const sensors_event_t *srcArray,
225 hidl_vec<Event> *dstVec) {
226 for (size_t i = 0; i < count; ++i) {
227 const sensors_event_t &src = srcArray[i];
228 Event *dst = &(*dstVec)[i];
229
230 convertFromSensorEvent(src, dst);
231 }
232}
233
234ISensors *HIDL_FETCH_ISensors(const char * /* hal */) {
235 Sensors *sensors = new Sensors;
236 if (sensors->initCheck() != OK) {
237 delete sensors;
238 sensors = nullptr;
239
240 return nullptr;
241 }
242
243 return sensors;
244}
245
246} // namespace implementation
247} // namespace V1_0
248} // namespace sensors
249} // namespace hardware
250} // namespace android