Andreas Huber | db49a41 | 2016-10-10 13:23:59 -0700 | [diff] [blame] | 1 | /* |
| 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 Huber | db49a41 | 2016-10-10 13:23:59 -0700 | [diff] [blame] | 18 | #include "convert.h" |
Nick Vaccaro | d133e4c | 2016-11-17 00:14:07 -0800 | [diff] [blame] | 19 | #include "multihal.h" |
Andreas Huber | db49a41 | 2016-10-10 13:23:59 -0700 | [diff] [blame] | 20 | |
| 21 | #include <android-base/logging.h> |
| 22 | |
Nick Vaccaro | d133e4c | 2016-11-17 00:14:07 -0800 | [diff] [blame] | 23 | #include <sys/stat.h> |
| 24 | |
Andreas Huber | db49a41 | 2016-10-10 13:23:59 -0700 | [diff] [blame] | 25 | namespace android { |
| 26 | namespace hardware { |
| 27 | namespace sensors { |
| 28 | namespace V1_0 { |
| 29 | namespace implementation { |
| 30 | |
Nick Vaccaro | d133e4c | 2016-11-17 00:14:07 -0800 | [diff] [blame] | 31 | /* |
| 32 | * If a multi-hal configuration file exists in the proper location, |
| 33 | * return true indicating we need to use multi-hal functionality. |
| 34 | */ |
| 35 | static 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 Huber | db49a41 | 2016-10-10 13:23:59 -0700 | [diff] [blame] | 41 | static Result ResultFromStatus(status_t err) { |
| 42 | switch (err) { |
| 43 | case OK: |
| 44 | return Result::OK; |
Andreas Huber | db49a41 | 2016-10-10 13:23:59 -0700 | [diff] [blame] | 45 | case PERMISSION_DENIED: |
| 46 | return Result::PERMISSION_DENIED; |
Peng Xu | 89df2e7 | 2017-01-09 19:12:42 -0800 | [diff] [blame] | 47 | case NO_MEMORY: |
| 48 | return Result::NO_MEMORY; |
| 49 | case BAD_VALUE: |
| 50 | return Result::BAD_VALUE; |
Andreas Huber | db49a41 | 2016-10-10 13:23:59 -0700 | [diff] [blame] | 51 | default: |
| 52 | return Result::INVALID_OPERATION; |
| 53 | } |
| 54 | } |
| 55 | |
| 56 | Sensors::Sensors() |
| 57 | : mInitCheck(NO_INIT), |
| 58 | mSensorModule(nullptr), |
| 59 | mSensorDevice(nullptr) { |
Nick Vaccaro | d133e4c | 2016-11-17 00:14:07 -0800 | [diff] [blame] | 60 | status_t err = OK; |
| 61 | if (UseMultiHal()) { |
| 62 | mSensorModule = ::get_multi_hal_module_info(); |
| 63 | } else { |
| 64 | err = hw_get_module( |
Andreas Huber | db49a41 | 2016-10-10 13:23:59 -0700 | [diff] [blame] | 65 | SENSORS_HARDWARE_MODULE_ID, |
| 66 | (hw_module_t const **)&mSensorModule); |
Nick Vaccaro | d133e4c | 2016-11-17 00:14:07 -0800 | [diff] [blame] | 67 | } |
Andreas Huber | db49a41 | 2016-10-10 13:23:59 -0700 | [diff] [blame] | 68 | 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 | |
| 103 | status_t Sensors::initCheck() const { |
| 104 | return mInitCheck; |
| 105 | } |
| 106 | |
Peng Xu | 0873e64 | 2017-01-08 13:28:10 -0800 | [diff] [blame] | 107 | Return<void> Sensors::getSensorsList(getSensorsList_cb _hidl_cb) { |
Andreas Huber | db49a41 | 2016-10-10 13:23:59 -0700 | [diff] [blame] | 108 | 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 Xu | 0873e64 | 2017-01-08 13:28:10 -0800 | [diff] [blame] | 121 | _hidl_cb(out); |
Andreas Huber | db49a41 | 2016-10-10 13:23:59 -0700 | [diff] [blame] | 122 | |
| 123 | return Void(); |
| 124 | } |
| 125 | |
| 126 | int Sensors::getHalDeviceVersion() const { |
| 127 | if (!mSensorDevice) { |
| 128 | return -1; |
| 129 | } |
| 130 | |
| 131 | return mSensorDevice->common.version; |
| 132 | } |
| 133 | |
| 134 | Return<Result> Sensors::setOperationMode(OperationMode mode) { |
| 135 | return ResultFromStatus(mSensorModule->set_operation_mode((uint32_t)mode)); |
| 136 | } |
| 137 | |
| 138 | Return<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 Xu | 0873e64 | 2017-01-08 13:28:10 -0800 | [diff] [blame] | 147 | Return<void> Sensors::poll(int32_t maxCount, poll_cb _hidl_cb) { |
Andreas Huber | db49a41 | 2016-10-10 13:23:59 -0700 | [diff] [blame] | 148 | hidl_vec<Event> out; |
| 149 | hidl_vec<SensorInfo> dynamicSensorsAdded; |
| 150 | |
| 151 | if (maxCount <= 0) { |
Peng Xu | 0873e64 | 2017-01-08 13:28:10 -0800 | [diff] [blame] | 152 | _hidl_cb(Result::BAD_VALUE, out, dynamicSensorsAdded); |
Andreas Huber | db49a41 | 2016-10-10 13:23:59 -0700 | [diff] [blame] | 153 | return Void(); |
| 154 | } |
| 155 | |
Peng Xu | cefa91b | 2017-01-16 03:10:40 -0800 | [diff] [blame] | 156 | int bufferSize = maxCount <= kPollMaxBufferSize ? maxCount : kPollMaxBufferSize; |
| 157 | |
| 158 | std::unique_ptr<sensors_event_t[]> data(new sensors_event_t[bufferSize]); |
Andreas Huber | db49a41 | 2016-10-10 13:23:59 -0700 | [diff] [blame] | 159 | |
| 160 | int err = mSensorDevice->poll( |
| 161 | reinterpret_cast<sensors_poll_device_t *>(mSensorDevice), |
Peng Xu | cefa91b | 2017-01-16 03:10:40 -0800 | [diff] [blame] | 162 | data.get(), bufferSize); |
Andreas Huber | db49a41 | 2016-10-10 13:23:59 -0700 | [diff] [blame] | 163 | |
| 164 | if (err < 0) { |
Peng Xu | 0873e64 | 2017-01-08 13:28:10 -0800 | [diff] [blame] | 165 | _hidl_cb(ResultFromStatus(err), out, dynamicSensorsAdded); |
Andreas Huber | db49a41 | 2016-10-10 13:23:59 -0700 | [diff] [blame] | 166 | return Void(); |
| 167 | } |
| 168 | |
| 169 | const size_t count = (size_t)err; |
| 170 | |
| 171 | for (size_t i = 0; i < count; ++i) { |
| 172 | if (data[i].type != SENSOR_TYPE_DYNAMIC_SENSOR_META) { |
| 173 | continue; |
| 174 | } |
| 175 | |
| 176 | const dynamic_sensor_meta_event_t *dyn = &data[i].dynamic_sensor_meta; |
| 177 | |
| 178 | if (!dyn->connected) { |
| 179 | continue; |
| 180 | } |
| 181 | |
| 182 | CHECK(dyn->sensor != nullptr); |
| 183 | CHECK_EQ(dyn->sensor->handle, dyn->handle); |
| 184 | |
| 185 | SensorInfo info; |
| 186 | convertFromSensor(*dyn->sensor, &info); |
| 187 | |
| 188 | size_t numDynamicSensors = dynamicSensorsAdded.size(); |
| 189 | dynamicSensorsAdded.resize(numDynamicSensors + 1); |
| 190 | dynamicSensorsAdded[numDynamicSensors] = info; |
| 191 | } |
| 192 | |
| 193 | out.resize(count); |
| 194 | convertFromSensorEvents(err, data.get(), &out); |
| 195 | |
Peng Xu | 0873e64 | 2017-01-08 13:28:10 -0800 | [diff] [blame] | 196 | _hidl_cb(Result::OK, out, dynamicSensorsAdded); |
Andreas Huber | db49a41 | 2016-10-10 13:23:59 -0700 | [diff] [blame] | 197 | |
| 198 | return Void(); |
| 199 | } |
| 200 | |
| 201 | Return<Result> Sensors::batch( |
| 202 | int32_t sensor_handle, |
Andreas Huber | db49a41 | 2016-10-10 13:23:59 -0700 | [diff] [blame] | 203 | int64_t sampling_period_ns, |
| 204 | int64_t max_report_latency_ns) { |
| 205 | return ResultFromStatus( |
| 206 | mSensorDevice->batch( |
| 207 | mSensorDevice, |
| 208 | sensor_handle, |
Peng Xu | 1f12c7a | 2017-01-11 11:08:45 -0800 | [diff] [blame] | 209 | 0, /*flags*/ |
Andreas Huber | db49a41 | 2016-10-10 13:23:59 -0700 | [diff] [blame] | 210 | sampling_period_ns, |
| 211 | max_report_latency_ns)); |
| 212 | } |
| 213 | |
| 214 | Return<Result> Sensors::flush(int32_t sensor_handle) { |
| 215 | return ResultFromStatus(mSensorDevice->flush(mSensorDevice, sensor_handle)); |
| 216 | } |
| 217 | |
| 218 | Return<Result> Sensors::injectSensorData(const Event& event) { |
| 219 | if (getHalDeviceVersion() < SENSORS_DEVICE_API_VERSION_1_4) { |
| 220 | return Result::INVALID_OPERATION; |
| 221 | } |
| 222 | |
| 223 | sensors_event_t out; |
| 224 | convertToSensorEvent(event, &out); |
| 225 | |
| 226 | return ResultFromStatus( |
| 227 | mSensorDevice->inject_sensor_data(mSensorDevice, &out)); |
| 228 | } |
| 229 | |
Peng Xu | 0f0df7e | 2017-01-05 23:39:08 -0800 | [diff] [blame] | 230 | Return<void> Sensors::registerDirectChannel( |
Peng Xu | 0873e64 | 2017-01-08 13:28:10 -0800 | [diff] [blame] | 231 | const SharedMemInfo& mem, registerDirectChannel_cb _hidl_cb) { |
Peng Xu | 89df2e7 | 2017-01-09 19:12:42 -0800 | [diff] [blame] | 232 | if (mSensorDevice->register_direct_channel == nullptr |
| 233 | || mSensorDevice->config_direct_report == nullptr) { |
| 234 | // HAL does not support |
| 235 | _hidl_cb(Result::INVALID_OPERATION, -1); |
| 236 | return Void(); |
| 237 | } |
| 238 | |
| 239 | sensors_direct_mem_t m; |
| 240 | if (!convertFromSharedMemInfo(mem, &m)) { |
| 241 | _hidl_cb(Result::BAD_VALUE, -1); |
| 242 | return Void(); |
| 243 | } |
| 244 | |
| 245 | int err = mSensorDevice->register_direct_channel(mSensorDevice, &m, -1); |
| 246 | |
| 247 | if (err < 0) { |
| 248 | _hidl_cb(ResultFromStatus(err), -1); |
| 249 | } else { |
| 250 | int32_t channelHandle = static_cast<int32_t>(err); |
| 251 | _hidl_cb(Result::OK, channelHandle); |
| 252 | } |
Peng Xu | 0f0df7e | 2017-01-05 23:39:08 -0800 | [diff] [blame] | 253 | return Void(); |
| 254 | } |
| 255 | |
| 256 | Return<Result> Sensors::unregisterDirectChannel(int32_t channelHandle) { |
Peng Xu | 89df2e7 | 2017-01-09 19:12:42 -0800 | [diff] [blame] | 257 | if (mSensorDevice->register_direct_channel == nullptr |
| 258 | || mSensorDevice->config_direct_report == nullptr) { |
| 259 | // HAL does not support |
| 260 | return Result::INVALID_OPERATION; |
| 261 | } |
| 262 | |
| 263 | mSensorDevice->register_direct_channel(mSensorDevice, nullptr, channelHandle); |
| 264 | |
| 265 | return Result::OK; |
Peng Xu | 0f0df7e | 2017-01-05 23:39:08 -0800 | [diff] [blame] | 266 | } |
| 267 | |
| 268 | Return<void> Sensors::configDirectReport( |
| 269 | int32_t sensorHandle, int32_t channelHandle, RateLevel rate, |
| 270 | configDirectReport_cb _hidl_cb) { |
Peng Xu | 89df2e7 | 2017-01-09 19:12:42 -0800 | [diff] [blame] | 271 | if (mSensorDevice->register_direct_channel == nullptr |
| 272 | || mSensorDevice->config_direct_report == nullptr) { |
| 273 | // HAL does not support |
| 274 | _hidl_cb(Result::INVALID_OPERATION, -1); |
| 275 | return Void(); |
| 276 | } |
Peng Xu | 0f0df7e | 2017-01-05 23:39:08 -0800 | [diff] [blame] | 277 | |
Peng Xu | 89df2e7 | 2017-01-09 19:12:42 -0800 | [diff] [blame] | 278 | sensors_direct_cfg_t cfg = { |
| 279 | .rate_level = convertFromRateLevel(rate) |
| 280 | }; |
| 281 | if (cfg.rate_level < 0) { |
| 282 | _hidl_cb(Result::BAD_VALUE, -1); |
| 283 | return Void(); |
| 284 | } |
| 285 | |
| 286 | int err = mSensorDevice->config_direct_report(mSensorDevice, |
| 287 | sensorHandle, channelHandle, &cfg); |
| 288 | |
| 289 | if (rate == RateLevel::STOP) { |
| 290 | _hidl_cb(ResultFromStatus(err), -1); |
| 291 | } else { |
| 292 | _hidl_cb(err > 0 ? Result::OK : ResultFromStatus(err), err); |
| 293 | } |
Peng Xu | 0f0df7e | 2017-01-05 23:39:08 -0800 | [diff] [blame] | 294 | return Void(); |
| 295 | } |
| 296 | |
Andreas Huber | db49a41 | 2016-10-10 13:23:59 -0700 | [diff] [blame] | 297 | // static |
| 298 | void Sensors::convertFromSensorEvents( |
| 299 | size_t count, |
| 300 | const sensors_event_t *srcArray, |
| 301 | hidl_vec<Event> *dstVec) { |
| 302 | for (size_t i = 0; i < count; ++i) { |
| 303 | const sensors_event_t &src = srcArray[i]; |
| 304 | Event *dst = &(*dstVec)[i]; |
| 305 | |
| 306 | convertFromSensorEvent(src, dst); |
| 307 | } |
| 308 | } |
| 309 | |
| 310 | ISensors *HIDL_FETCH_ISensors(const char * /* hal */) { |
| 311 | Sensors *sensors = new Sensors; |
| 312 | if (sensors->initCheck() != OK) { |
| 313 | delete sensors; |
| 314 | sensors = nullptr; |
| 315 | |
| 316 | return nullptr; |
| 317 | } |
| 318 | |
| 319 | return sensors; |
| 320 | } |
| 321 | |
| 322 | } // namespace implementation |
| 323 | } // namespace V1_0 |
| 324 | } // namespace sensors |
| 325 | } // namespace hardware |
| 326 | } // namespace android |