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; |
| 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 | |
| 54 | Sensors::Sensors() |
| 55 | : mInitCheck(NO_INIT), |
| 56 | mSensorModule(nullptr), |
| 57 | mSensorDevice(nullptr) { |
Nick Vaccaro | d133e4c | 2016-11-17 00:14:07 -0800 | [diff] [blame] | 58 | status_t err = OK; |
| 59 | if (UseMultiHal()) { |
| 60 | mSensorModule = ::get_multi_hal_module_info(); |
| 61 | } else { |
| 62 | err = hw_get_module( |
Andreas Huber | db49a41 | 2016-10-10 13:23:59 -0700 | [diff] [blame] | 63 | SENSORS_HARDWARE_MODULE_ID, |
| 64 | (hw_module_t const **)&mSensorModule); |
Nick Vaccaro | d133e4c | 2016-11-17 00:14:07 -0800 | [diff] [blame] | 65 | } |
Andreas Huber | db49a41 | 2016-10-10 13:23:59 -0700 | [diff] [blame] | 66 | 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 | |
| 101 | status_t Sensors::initCheck() const { |
| 102 | return mInitCheck; |
| 103 | } |
| 104 | |
Peng Xu | 0873e64 | 2017-01-08 13:28:10 -0800 | [diff] [blame^] | 105 | Return<void> Sensors::getSensorsList(getSensorsList_cb _hidl_cb) { |
Andreas Huber | db49a41 | 2016-10-10 13:23:59 -0700 | [diff] [blame] | 106 | 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 Xu | 0873e64 | 2017-01-08 13:28:10 -0800 | [diff] [blame^] | 119 | _hidl_cb(out); |
Andreas Huber | db49a41 | 2016-10-10 13:23:59 -0700 | [diff] [blame] | 120 | |
| 121 | return Void(); |
| 122 | } |
| 123 | |
| 124 | int Sensors::getHalDeviceVersion() const { |
| 125 | if (!mSensorDevice) { |
| 126 | return -1; |
| 127 | } |
| 128 | |
| 129 | return mSensorDevice->common.version; |
| 130 | } |
| 131 | |
| 132 | Return<Result> Sensors::setOperationMode(OperationMode mode) { |
| 133 | return ResultFromStatus(mSensorModule->set_operation_mode((uint32_t)mode)); |
| 134 | } |
| 135 | |
| 136 | Return<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 | |
| 145 | Return<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 Xu | 0873e64 | 2017-01-08 13:28:10 -0800 | [diff] [blame^] | 154 | Return<void> Sensors::poll(int32_t maxCount, poll_cb _hidl_cb) { |
Andreas Huber | db49a41 | 2016-10-10 13:23:59 -0700 | [diff] [blame] | 155 | hidl_vec<Event> out; |
| 156 | hidl_vec<SensorInfo> dynamicSensorsAdded; |
| 157 | |
| 158 | if (maxCount <= 0) { |
Peng Xu | 0873e64 | 2017-01-08 13:28:10 -0800 | [diff] [blame^] | 159 | _hidl_cb(Result::BAD_VALUE, out, dynamicSensorsAdded); |
Andreas Huber | db49a41 | 2016-10-10 13:23:59 -0700 | [diff] [blame] | 160 | 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 Xu | 0873e64 | 2017-01-08 13:28:10 -0800 | [diff] [blame^] | 171 | _hidl_cb(ResultFromStatus(err), out, dynamicSensorsAdded); |
Andreas Huber | db49a41 | 2016-10-10 13:23:59 -0700 | [diff] [blame] | 172 | 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 Xu | 0873e64 | 2017-01-08 13:28:10 -0800 | [diff] [blame^] | 202 | _hidl_cb(Result::OK, out, dynamicSensorsAdded); |
Andreas Huber | db49a41 | 2016-10-10 13:23:59 -0700 | [diff] [blame] | 203 | |
| 204 | return Void(); |
| 205 | } |
| 206 | |
| 207 | Return<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 | |
| 221 | Return<Result> Sensors::flush(int32_t sensor_handle) { |
| 222 | return ResultFromStatus(mSensorDevice->flush(mSensorDevice, sensor_handle)); |
| 223 | } |
| 224 | |
| 225 | Return<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 Xu | 0f0df7e | 2017-01-05 23:39:08 -0800 | [diff] [blame] | 237 | Return<void> Sensors::registerDirectChannel( |
Peng Xu | 0873e64 | 2017-01-08 13:28:10 -0800 | [diff] [blame^] | 238 | const SharedMemInfo& mem, registerDirectChannel_cb _hidl_cb) { |
Peng Xu | 0f0df7e | 2017-01-05 23:39:08 -0800 | [diff] [blame] | 239 | //TODO(b/30985702): finish implementation |
| 240 | (void) mem; |
Peng Xu | 0873e64 | 2017-01-08 13:28:10 -0800 | [diff] [blame^] | 241 | _hidl_cb(Result::INVALID_OPERATION, -1); |
Peng Xu | 0f0df7e | 2017-01-05 23:39:08 -0800 | [diff] [blame] | 242 | return Void(); |
| 243 | } |
| 244 | |
| 245 | Return<Result> Sensors::unregisterDirectChannel(int32_t channelHandle) { |
| 246 | //TODO(b/30985702): finish implementation |
| 247 | (void) channelHandle; |
| 248 | return Result::INVALID_OPERATION; |
| 249 | } |
| 250 | |
| 251 | Return<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 Huber | db49a41 | 2016-10-10 13:23:59 -0700 | [diff] [blame] | 263 | // static |
| 264 | void 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 | |
| 276 | ISensors *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 |