1 /* 2 * Copyright (C) 2015 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 #define LOG_TAG "sensors" 18 #define LOG_NDEBUG 1 19 #include <utils/Log.h> 20 21 #include "hubconnection.h" 22 #include "sensorlist.h" 23 #include "sensors.h" 24 25 #include <cutils/ashmem.h> 26 #include <errno.h> 27 #include <math.h> 28 #include <media/stagefright/foundation/ADebug.h> 29 #include <string.h> 30 #include <sys/mman.h> 31 #include <stdlib.h> 32 33 #ifdef DYNAMIC_SENSOR_EXT_ENABLED 34 #include <DynamicSensorManager.h> 35 #include <SensorEventCallback.h> 36 #endif 37 38 #ifdef LEFTY_SERVICE_ENABLED 39 #include "lefty_service.h" 40 #endif 41 42 using namespace android; 43 44 //////////////////////////////////////////////////////////////////////////////// 45 SensorContext(const struct hw_module_t * module)46 SensorContext::SensorContext(const struct hw_module_t *module) 47 : mSensorList(kSensorList, kSensorList + kSensorCount), 48 mHubConnection(HubConnection::getInstance()) { 49 memset(&device, 0, sizeof(device)); 50 51 device.common.tag = HARDWARE_DEVICE_TAG; 52 device.common.version = SENSORS_DEVICE_API_VERSION_1_4; 53 device.common.module = const_cast<hw_module_t *>(module); 54 device.common.close = CloseWrapper; 55 device.activate = ActivateWrapper; 56 device.setDelay = SetDelayWrapper; 57 device.poll = PollWrapper; 58 device.batch = BatchWrapper; 59 device.flush = FlushWrapper; 60 device.inject_sensor_data = InjectSensorDataWrapper; 61 mHubConnection->setRawScale(kScaleAccel, kScaleMag); 62 if (mHubConnection->isDirectReportSupported()) { 63 device.register_direct_channel = RegisterDirectChannelWrapper; 64 device.config_direct_report = ConfigDirectReportWrapper; 65 } 66 67 mOperationHandler.emplace_back(new HubConnectionOperation(mHubConnection)); 68 69 initializeHalExtension(); 70 } 71 close()72 int SensorContext::close() { 73 ALOGV("close"); 74 75 delete this; 76 77 return 0; 78 } 79 activate(int handle,int enabled)80 int SensorContext::activate(int handle, int enabled) { 81 ALOGV("activate"); 82 83 for (auto &h : mOperationHandler) { 84 if (h->owns(handle)) { 85 return h->activate(handle, enabled); 86 } 87 } 88 return INVALID_OPERATION; 89 } 90 setDelay(int handle,int64_t delayNs)91 int SensorContext::setDelay(int handle, int64_t delayNs) { 92 ALOGV("setDelay"); 93 94 for (auto &h: mOperationHandler) { 95 if (h->owns(handle)) { 96 return h->setDelay(handle, delayNs); 97 } 98 } 99 return INVALID_OPERATION; 100 } 101 poll(sensors_event_t * data,int count)102 int SensorContext::poll(sensors_event_t *data, int count) { 103 ALOGV("poll"); 104 105 // Release wakelock if held and no more events in ring buffer 106 mHubConnection->releaseWakeLockIfAppropriate(); 107 108 return mHubConnection->read(data, count); 109 } 110 batch(int handle,int64_t sampling_period_ns,int64_t max_report_latency_ns)111 int SensorContext::batch( 112 int handle, 113 int64_t sampling_period_ns, 114 int64_t max_report_latency_ns) { 115 ALOGV("batch"); 116 117 for (auto &h : mOperationHandler) { 118 if (h->owns(handle)) { 119 return h->batch(handle, sampling_period_ns, max_report_latency_ns); 120 } 121 } 122 return INVALID_OPERATION; 123 } 124 flush(int handle)125 int SensorContext::flush(int handle) { 126 ALOGV("flush"); 127 128 for (auto &h : mOperationHandler) { 129 if (h->owns(handle)) { 130 return h->flush(handle); 131 } 132 } 133 return INVALID_OPERATION; 134 } 135 register_direct_channel(const struct sensors_direct_mem_t * mem,int32_t channel_handle)136 int SensorContext::register_direct_channel( 137 const struct sensors_direct_mem_t *mem, int32_t channel_handle) { 138 if (mem) { 139 //add 140 return mHubConnection->addDirectChannel(mem); 141 } else { 142 //remove 143 mHubConnection->removeDirectChannel(channel_handle); 144 return NO_ERROR; 145 } 146 } 147 config_direct_report(int32_t sensor_handle,int32_t channel_handle,const struct sensors_direct_cfg_t * config)148 int SensorContext::config_direct_report( 149 int32_t sensor_handle, int32_t channel_handle, const struct sensors_direct_cfg_t * config) { 150 int rate_level = config->rate_level; 151 return mHubConnection->configDirectReport(sensor_handle, channel_handle, rate_level); 152 } 153 154 // static CloseWrapper(struct hw_device_t * dev)155 int SensorContext::CloseWrapper(struct hw_device_t *dev) { 156 return reinterpret_cast<SensorContext *>(dev)->close(); 157 } 158 159 // static ActivateWrapper(struct sensors_poll_device_t * dev,int handle,int enabled)160 int SensorContext::ActivateWrapper( 161 struct sensors_poll_device_t *dev, int handle, int enabled) { 162 return reinterpret_cast<SensorContext *>(dev)->activate(handle, enabled); 163 } 164 165 // static SetDelayWrapper(struct sensors_poll_device_t * dev,int handle,int64_t delayNs)166 int SensorContext::SetDelayWrapper( 167 struct sensors_poll_device_t *dev, int handle, int64_t delayNs) { 168 return reinterpret_cast<SensorContext *>(dev)->setDelay(handle, delayNs); 169 } 170 171 // static PollWrapper(struct sensors_poll_device_t * dev,sensors_event_t * data,int count)172 int SensorContext::PollWrapper( 173 struct sensors_poll_device_t *dev, sensors_event_t *data, int count) { 174 return reinterpret_cast<SensorContext *>(dev)->poll(data, count); 175 } 176 177 // static BatchWrapper(struct sensors_poll_device_1 * dev,int handle,int flags,int64_t sampling_period_ns,int64_t max_report_latency_ns)178 int SensorContext::BatchWrapper( 179 struct sensors_poll_device_1 *dev, 180 int handle, 181 int flags, 182 int64_t sampling_period_ns, 183 int64_t max_report_latency_ns) { 184 (void) flags; 185 return reinterpret_cast<SensorContext *>(dev)->batch( 186 handle, sampling_period_ns, max_report_latency_ns); 187 } 188 189 // static FlushWrapper(struct sensors_poll_device_1 * dev,int handle)190 int SensorContext::FlushWrapper(struct sensors_poll_device_1 *dev, int handle) { 191 return reinterpret_cast<SensorContext *>(dev)->flush(handle); 192 } 193 194 // static RegisterDirectChannelWrapper(struct sensors_poll_device_1 * dev,const struct sensors_direct_mem_t * mem,int channel_handle)195 int SensorContext::RegisterDirectChannelWrapper(struct sensors_poll_device_1 *dev, 196 const struct sensors_direct_mem_t* mem, int channel_handle) { 197 return reinterpret_cast<SensorContext *>(dev)->register_direct_channel( 198 mem, channel_handle); 199 } 200 201 // static ConfigDirectReportWrapper(struct sensors_poll_device_1 * dev,int sensor_handle,int channel_handle,const sensors_direct_cfg_t * config)202 int SensorContext::ConfigDirectReportWrapper(struct sensors_poll_device_1 *dev, 203 int sensor_handle, int channel_handle, const sensors_direct_cfg_t * config) { 204 return reinterpret_cast<SensorContext *>(dev)->config_direct_report( 205 sensor_handle, channel_handle, config); 206 } 207 inject_sensor_data(const sensors_event_t * event)208 int SensorContext::inject_sensor_data(const sensors_event_t *event) { 209 ALOGV("inject_sensor_data"); 210 211 // only support set operation parameter, which will have handle == 0 212 if (event == nullptr || event->type != SENSOR_TYPE_ADDITIONAL_INFO) { 213 return -EINVAL; 214 } 215 216 if (event->sensor != SENSORS_HANDLE_BASE - 1) { 217 return -ENOSYS; 218 } 219 220 if (event->additional_info.type == AINFO_BEGIN 221 || event->additional_info.type == AINFO_END) { 222 return 0; 223 } 224 225 mHubConnection->setOperationParameter(event->additional_info); 226 return 0; 227 } 228 229 // static InjectSensorDataWrapper(struct sensors_poll_device_1 * dev,const struct sensors_event_t * event)230 int SensorContext::InjectSensorDataWrapper(struct sensors_poll_device_1 *dev, 231 const struct sensors_event_t *event) { 232 return reinterpret_cast<SensorContext *>(dev)->inject_sensor_data(event); 233 } 234 getHubAlive()235 bool SensorContext::getHubAlive() { 236 return (mHubConnection->initCheck() == OK && mHubConnection->getAliveCheck() == OK); 237 } 238 getSensorList(sensor_t const ** list)239 size_t SensorContext::getSensorList(sensor_t const **list) { 240 ALOGE("sensor p = %p, n = %zu", mSensorList.data(), mSensorList.size()); 241 *list = mSensorList.data(); 242 return mSensorList.size(); 243 } 244 245 // HubConnectionOperation functions HubConnectionOperation(sp<HubConnection> hubConnection)246 SensorContext::HubConnectionOperation::HubConnectionOperation(sp<HubConnection> hubConnection) 247 : mHubConnection(hubConnection) { 248 for (size_t i = 0; i < kSensorCount; i++) { 249 mHandles.emplace(kSensorList[i].handle); 250 } 251 } 252 owns(int handle)253 bool SensorContext::HubConnectionOperation::owns(int handle) { 254 return mHandles.find(handle) != mHandles.end(); 255 } 256 activate(int handle,int enabled)257 int SensorContext::HubConnectionOperation::activate(int handle, int enabled) { 258 mHubConnection->queueActivate(handle, enabled); 259 return 0; 260 } 261 setDelay(int handle,int64_t delayNs)262 int SensorContext::HubConnectionOperation::setDelay(int handle, int64_t delayNs) { 263 // clamp sample rate based on minDelay and maxDelay defined in kSensorList 264 int64_t delayNsClamped = delayNs; 265 for (size_t i = 0; i < kSensorCount; i++) { 266 sensor_t sensor = kSensorList[i]; 267 if (sensor.handle != handle) { 268 continue; 269 } 270 271 if ((sensor.flags & REPORTING_MODE_MASK) == SENSOR_FLAG_CONTINUOUS_MODE) { 272 if ((delayNs/1000) < sensor.minDelay) { 273 delayNsClamped = sensor.minDelay * 1000; 274 } else if ((delayNs/1000) > sensor.maxDelay) { 275 delayNsClamped = sensor.maxDelay * 1000; 276 } 277 } 278 279 break; 280 } 281 282 mHubConnection->queueSetDelay(handle, delayNsClamped); 283 return 0; 284 } 285 batch(int handle,int64_t sampling_period_ns,int64_t max_report_latency_ns)286 int SensorContext::HubConnectionOperation::batch( 287 int handle, int64_t sampling_period_ns, 288 int64_t max_report_latency_ns) { 289 // clamp sample rate based on minDelay and maxDelay defined in kSensorList 290 int64_t sampling_period_ns_clamped = sampling_period_ns; 291 for (size_t i = 0; i < kSensorCount; i++) { 292 sensor_t sensor = kSensorList[i]; 293 if (sensor.handle != handle) { 294 continue; 295 } 296 297 if ((sensor.flags & REPORTING_MODE_MASK) == SENSOR_FLAG_CONTINUOUS_MODE) { 298 if ((sampling_period_ns/1000) < sensor.minDelay) { 299 sampling_period_ns_clamped = sensor.minDelay * 1000; 300 } else if ((sampling_period_ns/1000) > sensor.maxDelay) { 301 sampling_period_ns_clamped = sensor.maxDelay * 1000; 302 } 303 } 304 305 break; 306 } 307 308 mHubConnection->queueBatch(handle, sampling_period_ns_clamped, 309 max_report_latency_ns); 310 return 0; 311 } 312 flush(int handle)313 int SensorContext::HubConnectionOperation::flush(int handle) { 314 mHubConnection->queueFlush(handle); 315 return 0; 316 } 317 318 #ifdef DYNAMIC_SENSOR_EXT_ENABLED 319 namespace { 320 // adaptor class 321 class Callback : public SensorEventCallback { 322 public: Callback(sp<HubConnection> hubConnection)323 Callback(sp<HubConnection> hubConnection) : mHubConnection(hubConnection) {} 324 virtual int submitEvent(sp<BaseSensorObject> source, const sensors_event_t &e) override; 325 private: 326 sp<HubConnection> mHubConnection; 327 }; 328 submitEvent(sp<BaseSensorObject> source,const sensors_event_t & e)329 int Callback::submitEvent(sp<BaseSensorObject> source, const sensors_event_t &e) { 330 (void) source; // irrelavent in this context 331 return (mHubConnection->write(&e, 1) == 1) ? 0 : -ENOSPC; 332 } 333 } // anonymous namespace 334 DynamicSensorManagerOperation(DynamicSensorManager * manager)335 SensorContext::DynamicSensorManagerOperation::DynamicSensorManagerOperation(DynamicSensorManager* manager) 336 : mDynamicSensorManager(manager) { 337 } 338 owns(int handle)339 bool SensorContext::DynamicSensorManagerOperation::owns(int handle) { 340 return mDynamicSensorManager->owns(handle); 341 } 342 activate(int handle,int enabled)343 int SensorContext::DynamicSensorManagerOperation::activate(int handle, int enabled) { 344 return mDynamicSensorManager->activate(handle, enabled); 345 } 346 setDelay(int handle,int64_t delayNs)347 int SensorContext::DynamicSensorManagerOperation::setDelay(int handle, int64_t delayNs) { 348 return mDynamicSensorManager->setDelay(handle, delayNs); 349 } 350 batch(int handle,int64_t sampling_period_ns,int64_t max_report_latency_ns)351 int SensorContext::DynamicSensorManagerOperation::batch(int handle, int64_t sampling_period_ns, 352 int64_t max_report_latency_ns) { 353 return mDynamicSensorManager->batch(handle, sampling_period_ns, max_report_latency_ns); 354 } 355 flush(int handle)356 int SensorContext::DynamicSensorManagerOperation::flush(int handle) { 357 return mDynamicSensorManager->flush(handle); 358 } 359 #endif 360 initializeHalExtension()361 void SensorContext::initializeHalExtension() { 362 #ifdef DYNAMIC_SENSOR_EXT_ENABLED 363 // initialize callback and dynamic sensor manager 364 mEventCallback.reset(new Callback(mHubConnection)); 365 DynamicSensorManager* manager = DynamicSensorManager::createInstance( 366 kDynamicHandleBase, kMaxDynamicHandleCount, mEventCallback.get()); 367 368 // add meta sensor to list 369 mSensorList.push_back(manager->getDynamicMetaSensor()); 370 371 // register operation 372 mOperationHandler.emplace_back(new DynamicSensorManagerOperation(manager)); 373 #endif 374 } 375 376 //////////////////////////////////////////////////////////////////////////////// 377 378 static bool gHubAlive; 379 static sensor_t const *sensor_list; 380 static int n_sensor; 381 open_sensors(const struct hw_module_t * module,const char *,struct hw_device_t ** dev)382 static int open_sensors( 383 const struct hw_module_t *module, 384 const char *, 385 struct hw_device_t **dev) { 386 ALOGV("open_sensors"); 387 388 SensorContext *ctx = new SensorContext(module); 389 n_sensor = ctx->getSensorList(&sensor_list); 390 gHubAlive = ctx->getHubAlive(); 391 *dev = &ctx->device.common; 392 393 #ifdef LEFTY_SERVICE_ENABLED 394 register_lefty_service(); 395 #endif 396 return 0; 397 } 398 399 static struct hw_module_methods_t sensors_module_methods = { 400 .open = open_sensors 401 }; 402 get_sensors_list(struct sensors_module_t *,struct sensor_t const ** list)403 static int get_sensors_list( 404 struct sensors_module_t *, 405 struct sensor_t const **list) { 406 ALOGV("get_sensors_list"); 407 if (gHubAlive && sensor_list != nullptr) { 408 *list = sensor_list; 409 return n_sensor; 410 } else { 411 *list = {}; 412 return 0; 413 } 414 } 415 set_operation_mode(unsigned int mode)416 static int set_operation_mode(unsigned int mode) { 417 ALOGV("set_operation_mode"); 418 419 // This is no-op because there is no sensor in the hal that system can 420 // inject events. Only operation parameter injection is implemented, which 421 // works in both data injection and normal mode. 422 (void) mode; 423 return 0; 424 } 425 426 struct sensors_module_t HAL_MODULE_INFO_SYM = { 427 .common = { 428 .tag = HARDWARE_MODULE_TAG, 429 .version_major = 1, 430 .version_minor = 0, 431 .id = SENSORS_HARDWARE_MODULE_ID, 432 .name = "Google Sensor module", 433 .author = "Google", 434 .methods = &sensors_module_methods, 435 .dso = NULL, 436 .reserved = {0}, 437 }, 438 .get_sensors_list = get_sensors_list, 439 .set_operation_mode = set_operation_mode, 440 }; 441