Mike Lockwood | f847762 | 2013-10-17 08:05:00 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2013 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 <hardware/sensors.h> |
| 18 | #include <fcntl.h> |
| 19 | #include <errno.h> |
| 20 | #include <dirent.h> |
| 21 | #include <math.h> |
| 22 | #include <poll.h> |
| 23 | #include <pthread.h> |
| 24 | #include <cutils/atomic.h> |
| 25 | |
| 26 | #define LOG_NDEBUG 1 |
| 27 | #include <cutils/log.h> |
| 28 | |
| 29 | #include <vector> |
Satya Durga Srinivasu Prabhala | 1fd3618 | 2015-01-20 18:53:35 -0800 | [diff] [blame] | 30 | #include <string> |
| 31 | #include <fstream> |
Mike Lockwood | f847762 | 2013-10-17 08:05:00 -0700 | [diff] [blame] | 32 | #include <map> |
Nick Vaccaro | e914d69 | 2014-10-10 13:32:38 -0700 | [diff] [blame] | 33 | #include <string> |
Mike Lockwood | f847762 | 2013-10-17 08:05:00 -0700 | [diff] [blame] | 34 | |
| 35 | #include <stdio.h> |
| 36 | #include <dlfcn.h> |
| 37 | #include <SensorEventQueue.h> |
| 38 | |
Ching Tzung Lin | 4fd217a | 2015-07-23 10:18:06 -0700 | [diff] [blame] | 39 | #include <limits.h> |
| 40 | #include <stdlib.h> |
Mike Lockwood | f847762 | 2013-10-17 08:05:00 -0700 | [diff] [blame] | 41 | |
| 42 | static const char* CONFIG_FILENAME = "/system/etc/sensors/hals.conf"; |
Mike Lockwood | f847762 | 2013-10-17 08:05:00 -0700 | [diff] [blame] | 43 | static const int MAX_CONF_LINE_LENGTH = 1024; |
| 44 | |
| 45 | static pthread_mutex_t init_modules_mutex = PTHREAD_MUTEX_INITIALIZER; |
| 46 | static pthread_mutex_t init_sensors_mutex = PTHREAD_MUTEX_INITIALIZER; |
| 47 | |
| 48 | // This mutex is shared by all queues |
| 49 | static pthread_mutex_t queue_mutex = PTHREAD_MUTEX_INITIALIZER; |
| 50 | |
| 51 | // Used to pause the multihal poll(). Broadcasted by sub-polling tasks if waiting_for_data. |
| 52 | static pthread_cond_t data_available_cond = PTHREAD_COND_INITIALIZER; |
| 53 | bool waiting_for_data = false; |
| 54 | |
| 55 | /* |
Aaron Whyte | 4d7ac52 | 2014-04-09 15:56:54 -0700 | [diff] [blame] | 56 | * Vector of sub modules, whose indexes are referred to in this file as module_index. |
Mike Lockwood | f847762 | 2013-10-17 08:05:00 -0700 | [diff] [blame] | 57 | */ |
| 58 | static std::vector<hw_module_t *> *sub_hw_modules = NULL; |
| 59 | |
| 60 | /* |
| 61 | * Comparable class that globally identifies a sensor, by module index and local handle. |
| 62 | * A module index is the module's index in sub_hw_modules. |
| 63 | * A local handle is the handle the sub-module assigns to a sensor. |
| 64 | */ |
| 65 | struct FullHandle { |
| 66 | int moduleIndex; |
| 67 | int localHandle; |
| 68 | |
| 69 | bool operator<(const FullHandle &that) const { |
| 70 | if (moduleIndex < that.moduleIndex) { |
| 71 | return true; |
| 72 | } |
| 73 | if (moduleIndex > that.moduleIndex) { |
| 74 | return false; |
| 75 | } |
| 76 | return localHandle < that.localHandle; |
| 77 | } |
| 78 | |
| 79 | bool operator==(const FullHandle &that) const { |
| 80 | return moduleIndex == that.moduleIndex && localHandle == that.localHandle; |
| 81 | } |
| 82 | }; |
| 83 | |
| 84 | std::map<int, FullHandle> global_to_full; |
| 85 | std::map<FullHandle, int> full_to_global; |
| 86 | int next_global_handle = 1; |
| 87 | |
| 88 | static int assign_global_handle(int module_index, int local_handle) { |
| 89 | int global_handle = next_global_handle++; |
| 90 | FullHandle full_handle; |
| 91 | full_handle.moduleIndex = module_index; |
| 92 | full_handle.localHandle = local_handle; |
| 93 | full_to_global[full_handle] = global_handle; |
| 94 | global_to_full[global_handle] = full_handle; |
| 95 | return global_handle; |
| 96 | } |
| 97 | |
Aaron Whyte | 4d7ac52 | 2014-04-09 15:56:54 -0700 | [diff] [blame] | 98 | // Returns the local handle, or -1 if it does not exist. |
Mike Lockwood | f847762 | 2013-10-17 08:05:00 -0700 | [diff] [blame] | 99 | static int get_local_handle(int global_handle) { |
Aaron Whyte | 4d7ac52 | 2014-04-09 15:56:54 -0700 | [diff] [blame] | 100 | if (global_to_full.count(global_handle) == 0) { |
| 101 | ALOGW("Unknown global_handle %d", global_handle); |
| 102 | return -1; |
| 103 | } |
Mike Lockwood | f847762 | 2013-10-17 08:05:00 -0700 | [diff] [blame] | 104 | return global_to_full[global_handle].localHandle; |
| 105 | } |
| 106 | |
Aaron Whyte | 4d7ac52 | 2014-04-09 15:56:54 -0700 | [diff] [blame] | 107 | // Returns the sub_hw_modules index of the module that contains the sensor associates with this |
| 108 | // global_handle, or -1 if that global_handle does not exist. |
Mike Lockwood | f847762 | 2013-10-17 08:05:00 -0700 | [diff] [blame] | 109 | static int get_module_index(int global_handle) { |
Aaron Whyte | 4d7ac52 | 2014-04-09 15:56:54 -0700 | [diff] [blame] | 110 | if (global_to_full.count(global_handle) == 0) { |
| 111 | ALOGW("Unknown global_handle %d", global_handle); |
| 112 | return -1; |
| 113 | } |
Mike Lockwood | f847762 | 2013-10-17 08:05:00 -0700 | [diff] [blame] | 114 | FullHandle f = global_to_full[global_handle]; |
| 115 | ALOGV("FullHandle for global_handle %d: moduleIndex %d, localHandle %d", |
| 116 | global_handle, f.moduleIndex, f.localHandle); |
| 117 | return f.moduleIndex; |
| 118 | } |
| 119 | |
Aaron Whyte | 4d7ac52 | 2014-04-09 15:56:54 -0700 | [diff] [blame] | 120 | // Returns the global handle for this full_handle, or -1 if the full_handle is unknown. |
| 121 | static int get_global_handle(FullHandle* full_handle) { |
| 122 | int global_handle = -1; |
| 123 | if (full_to_global.count(*full_handle)) { |
| 124 | global_handle = full_to_global[*full_handle]; |
| 125 | } else { |
| 126 | ALOGW("Unknown FullHandle: moduleIndex %d, localHandle %d", |
| 127 | full_handle->moduleIndex, full_handle->localHandle); |
| 128 | } |
| 129 | return global_handle; |
| 130 | } |
| 131 | |
Nick Vaccaro | 98d5e94 | 2016-02-09 14:56:17 -0800 | [diff] [blame] | 132 | static const int SENSOR_EVENT_QUEUE_CAPACITY = 36; |
Mike Lockwood | f847762 | 2013-10-17 08:05:00 -0700 | [diff] [blame] | 133 | |
| 134 | struct TaskContext { |
| 135 | sensors_poll_device_t* device; |
| 136 | SensorEventQueue* queue; |
| 137 | }; |
| 138 | |
| 139 | void *writerTask(void* ptr) { |
| 140 | ALOGV("writerTask STARTS"); |
| 141 | TaskContext* ctx = (TaskContext*)ptr; |
| 142 | sensors_poll_device_t* device = ctx->device; |
| 143 | SensorEventQueue* queue = ctx->queue; |
| 144 | sensors_event_t* buffer; |
| 145 | int eventsPolled; |
| 146 | while (1) { |
| 147 | pthread_mutex_lock(&queue_mutex); |
| 148 | if (queue->waitForSpace(&queue_mutex)) { |
| 149 | ALOGV("writerTask waited for space"); |
| 150 | } |
| 151 | int bufferSize = queue->getWritableRegion(SENSOR_EVENT_QUEUE_CAPACITY, &buffer); |
| 152 | // Do blocking poll outside of lock |
| 153 | pthread_mutex_unlock(&queue_mutex); |
| 154 | |
| 155 | ALOGV("writerTask before poll() - bufferSize = %d", bufferSize); |
| 156 | eventsPolled = device->poll(device, buffer, bufferSize); |
| 157 | ALOGV("writerTask poll() got %d events.", eventsPolled); |
Nick Vaccaro | 59d9fb4 | 2016-07-19 10:34:00 -0700 | [diff] [blame^] | 158 | if (eventsPolled <= 0) { |
| 159 | if (eventsPolled < 0) { |
| 160 | ALOGV("writerTask ignored error %d from %s", eventsPolled, device->common.module->name); |
| 161 | ALOGE("ERROR: Fix %s so it does not return error from poll()", device->common.module->name); |
| 162 | } |
Mike Lockwood | f847762 | 2013-10-17 08:05:00 -0700 | [diff] [blame] | 163 | continue; |
| 164 | } |
| 165 | pthread_mutex_lock(&queue_mutex); |
| 166 | queue->markAsWritten(eventsPolled); |
| 167 | ALOGV("writerTask wrote %d events", eventsPolled); |
| 168 | if (waiting_for_data) { |
| 169 | ALOGV("writerTask - broadcast data_available_cond"); |
| 170 | pthread_cond_broadcast(&data_available_cond); |
| 171 | } |
| 172 | pthread_mutex_unlock(&queue_mutex); |
| 173 | } |
| 174 | // never actually returns |
| 175 | return NULL; |
| 176 | } |
| 177 | |
| 178 | /* |
| 179 | * Cache of all sensors, with original handles replaced by global handles. |
| 180 | * This will be handled to get_sensors_list() callers. |
| 181 | */ |
| 182 | static struct sensor_t const* global_sensors_list = NULL; |
| 183 | static int global_sensors_count = -1; |
| 184 | |
| 185 | /* |
| 186 | * Extends a sensors_poll_device_1 by including all the sub-module's devices. |
| 187 | */ |
| 188 | struct sensors_poll_context_t { |
| 189 | /* |
| 190 | * This is the device that SensorDevice.cpp uses to make API calls |
| 191 | * to the multihal, which fans them out to sub-HALs. |
| 192 | */ |
| 193 | sensors_poll_device_1 proxy_device; // must be first |
| 194 | |
| 195 | void addSubHwDevice(struct hw_device_t*); |
| 196 | |
| 197 | int activate(int handle, int enabled); |
| 198 | int setDelay(int handle, int64_t ns); |
| 199 | int poll(sensors_event_t* data, int count); |
| 200 | int batch(int handle, int flags, int64_t period_ns, int64_t timeout); |
| 201 | int flush(int handle); |
| 202 | int close(); |
| 203 | |
| 204 | std::vector<hw_device_t*> sub_hw_devices; |
| 205 | std::vector<SensorEventQueue*> queues; |
| 206 | std::vector<pthread_t> threads; |
| 207 | int nextReadIndex; |
| 208 | |
| 209 | sensors_poll_device_t* get_v0_device_by_handle(int global_handle); |
| 210 | sensors_poll_device_1_t* get_v1_device_by_handle(int global_handle); |
| 211 | int get_device_version_by_handle(int global_handle); |
| 212 | |
| 213 | void copy_event_remap_handle(sensors_event_t* src, sensors_event_t* dest, int sub_index); |
| 214 | }; |
| 215 | |
| 216 | void sensors_poll_context_t::addSubHwDevice(struct hw_device_t* sub_hw_device) { |
| 217 | ALOGV("addSubHwDevice"); |
| 218 | this->sub_hw_devices.push_back(sub_hw_device); |
| 219 | |
| 220 | SensorEventQueue *queue = new SensorEventQueue(SENSOR_EVENT_QUEUE_CAPACITY); |
| 221 | this->queues.push_back(queue); |
| 222 | |
| 223 | TaskContext* taskContext = new TaskContext(); |
| 224 | taskContext->device = (sensors_poll_device_t*) sub_hw_device; |
| 225 | taskContext->queue = queue; |
| 226 | |
| 227 | pthread_t writerThread; |
| 228 | pthread_create(&writerThread, NULL, writerTask, taskContext); |
| 229 | this->threads.push_back(writerThread); |
| 230 | } |
| 231 | |
Aaron Whyte | 4d7ac52 | 2014-04-09 15:56:54 -0700 | [diff] [blame] | 232 | // Returns the device pointer, or NULL if the global handle is invalid. |
| 233 | sensors_poll_device_t* sensors_poll_context_t::get_v0_device_by_handle(int global_handle) { |
| 234 | int sub_index = get_module_index(global_handle); |
Nick Vaccaro | a889c09 | 2016-04-15 10:17:07 -0700 | [diff] [blame] | 235 | if (sub_index < 0 || sub_index >= (int) this->sub_hw_devices.size()) { |
Aaron Whyte | 4d7ac52 | 2014-04-09 15:56:54 -0700 | [diff] [blame] | 236 | return NULL; |
| 237 | } |
Mike Lockwood | f847762 | 2013-10-17 08:05:00 -0700 | [diff] [blame] | 238 | return (sensors_poll_device_t*) this->sub_hw_devices[sub_index]; |
| 239 | } |
| 240 | |
Aaron Whyte | 4d7ac52 | 2014-04-09 15:56:54 -0700 | [diff] [blame] | 241 | // Returns the device pointer, or NULL if the global handle is invalid. |
| 242 | sensors_poll_device_1_t* sensors_poll_context_t::get_v1_device_by_handle(int global_handle) { |
| 243 | int sub_index = get_module_index(global_handle); |
Nick Vaccaro | a889c09 | 2016-04-15 10:17:07 -0700 | [diff] [blame] | 244 | if (sub_index < 0 || sub_index >= (int) this->sub_hw_devices.size()) { |
Aaron Whyte | 4d7ac52 | 2014-04-09 15:56:54 -0700 | [diff] [blame] | 245 | return NULL; |
| 246 | } |
Mike Lockwood | f847762 | 2013-10-17 08:05:00 -0700 | [diff] [blame] | 247 | return (sensors_poll_device_1_t*) this->sub_hw_devices[sub_index]; |
| 248 | } |
| 249 | |
Aaron Whyte | 4d7ac52 | 2014-04-09 15:56:54 -0700 | [diff] [blame] | 250 | // Returns the device version, or -1 if the handle is invalid. |
Mike Lockwood | f847762 | 2013-10-17 08:05:00 -0700 | [diff] [blame] | 251 | int sensors_poll_context_t::get_device_version_by_handle(int handle) { |
| 252 | sensors_poll_device_t* v0 = this->get_v0_device_by_handle(handle); |
Aaron Whyte | 4d7ac52 | 2014-04-09 15:56:54 -0700 | [diff] [blame] | 253 | if (v0) { |
| 254 | return v0->common.version; |
| 255 | } else { |
| 256 | return -1; |
| 257 | } |
Mike Lockwood | f847762 | 2013-10-17 08:05:00 -0700 | [diff] [blame] | 258 | } |
| 259 | |
Nick Vaccaro | e914d69 | 2014-10-10 13:32:38 -0700 | [diff] [blame] | 260 | // Android L requires sensor HALs to be either 1_0 or 1_3 compliant |
| 261 | #define HAL_VERSION_IS_COMPLIANT(version) \ |
| 262 | (version == SENSORS_DEVICE_API_VERSION_1_0 || version >= SENSORS_DEVICE_API_VERSION_1_3) |
| 263 | |
| 264 | // Returns true if HAL is compliant, false if HAL is not compliant or if handle is invalid |
| 265 | static bool halIsCompliant(sensors_poll_context_t *ctx, int handle) { |
| 266 | int version = ctx->get_device_version_by_handle(handle); |
| 267 | return version != -1 && HAL_VERSION_IS_COMPLIANT(version); |
| 268 | } |
| 269 | |
| 270 | const char *apiNumToStr(int version) { |
| 271 | switch(version) { |
| 272 | case SENSORS_DEVICE_API_VERSION_1_0: |
| 273 | return "SENSORS_DEVICE_API_VERSION_1_0"; |
| 274 | case SENSORS_DEVICE_API_VERSION_1_1: |
| 275 | return "SENSORS_DEVICE_API_VERSION_1_1"; |
| 276 | case SENSORS_DEVICE_API_VERSION_1_2: |
| 277 | return "SENSORS_DEVICE_API_VERSION_1_2"; |
| 278 | case SENSORS_DEVICE_API_VERSION_1_3: |
| 279 | return "SENSORS_DEVICE_API_VERSION_1_3"; |
| 280 | default: |
| 281 | return "UNKNOWN"; |
| 282 | } |
| 283 | } |
| 284 | |
Mike Lockwood | f847762 | 2013-10-17 08:05:00 -0700 | [diff] [blame] | 285 | int sensors_poll_context_t::activate(int handle, int enabled) { |
Nick Vaccaro | 93bf996 | 2014-03-17 13:05:09 -0700 | [diff] [blame] | 286 | int retval = -EINVAL; |
Mike Lockwood | f847762 | 2013-10-17 08:05:00 -0700 | [diff] [blame] | 287 | ALOGV("activate"); |
Aaron Whyte | 4d7ac52 | 2014-04-09 15:56:54 -0700 | [diff] [blame] | 288 | int local_handle = get_local_handle(handle); |
Mike Lockwood | f847762 | 2013-10-17 08:05:00 -0700 | [diff] [blame] | 289 | sensors_poll_device_t* v0 = this->get_v0_device_by_handle(handle); |
Nick Vaccaro | e914d69 | 2014-10-10 13:32:38 -0700 | [diff] [blame] | 290 | if (halIsCompliant(this, handle) && local_handle >= 0 && v0) { |
Aaron Whyte | 4d7ac52 | 2014-04-09 15:56:54 -0700 | [diff] [blame] | 291 | retval = v0->activate(v0, local_handle, enabled); |
Nick Vaccaro | e914d69 | 2014-10-10 13:32:38 -0700 | [diff] [blame] | 292 | } else { |
| 293 | ALOGE("IGNORING activate(enable %d) call to non-API-compliant sensor handle=%d !", |
| 294 | enabled, handle); |
Aaron Whyte | 4d7ac52 | 2014-04-09 15:56:54 -0700 | [diff] [blame] | 295 | } |
Mike Lockwood | f847762 | 2013-10-17 08:05:00 -0700 | [diff] [blame] | 296 | ALOGV("retval %d", retval); |
| 297 | return retval; |
| 298 | } |
| 299 | |
| 300 | int sensors_poll_context_t::setDelay(int handle, int64_t ns) { |
Aaron Whyte | 4d7ac52 | 2014-04-09 15:56:54 -0700 | [diff] [blame] | 301 | int retval = -EINVAL; |
Mike Lockwood | f847762 | 2013-10-17 08:05:00 -0700 | [diff] [blame] | 302 | ALOGV("setDelay"); |
Aaron Whyte | 4d7ac52 | 2014-04-09 15:56:54 -0700 | [diff] [blame] | 303 | int local_handle = get_local_handle(handle); |
Mike Lockwood | f847762 | 2013-10-17 08:05:00 -0700 | [diff] [blame] | 304 | sensors_poll_device_t* v0 = this->get_v0_device_by_handle(handle); |
Nick Vaccaro | e914d69 | 2014-10-10 13:32:38 -0700 | [diff] [blame] | 305 | if (halIsCompliant(this, handle) && local_handle >= 0 && v0) { |
Aaron Whyte | 4d7ac52 | 2014-04-09 15:56:54 -0700 | [diff] [blame] | 306 | retval = v0->setDelay(v0, local_handle, ns); |
Nick Vaccaro | e914d69 | 2014-10-10 13:32:38 -0700 | [diff] [blame] | 307 | } else { |
| 308 | ALOGE("IGNORING setDelay() call for non-API-compliant sensor handle=%d !", handle); |
Aaron Whyte | 4d7ac52 | 2014-04-09 15:56:54 -0700 | [diff] [blame] | 309 | } |
Mike Lockwood | f847762 | 2013-10-17 08:05:00 -0700 | [diff] [blame] | 310 | ALOGV("retval %d", retval); |
| 311 | return retval; |
| 312 | } |
| 313 | |
| 314 | void sensors_poll_context_t::copy_event_remap_handle(sensors_event_t* dest, sensors_event_t* src, |
| 315 | int sub_index) { |
| 316 | memcpy(dest, src, sizeof(struct sensors_event_t)); |
| 317 | // A normal event's "sensor" field is a local handle. Convert it to a global handle. |
| 318 | // A meta-data event must have its sensor set to 0, but it has a nested event |
| 319 | // with a local handle that needs to be converted to a global handle. |
| 320 | FullHandle full_handle; |
| 321 | full_handle.moduleIndex = sub_index; |
Aaron Whyte | 4d7ac52 | 2014-04-09 15:56:54 -0700 | [diff] [blame] | 322 | |
Mike Lockwood | f847762 | 2013-10-17 08:05:00 -0700 | [diff] [blame] | 323 | // If it's a metadata event, rewrite the inner payload, not the sensor field. |
Aaron Whyte | 4d7ac52 | 2014-04-09 15:56:54 -0700 | [diff] [blame] | 324 | // If the event's sensor field is unregistered for any reason, rewrite the sensor field |
| 325 | // with a -1, instead of writing an incorrect but plausible sensor number, because |
| 326 | // get_global_handle() returns -1 for unknown FullHandles. |
Mike Lockwood | f847762 | 2013-10-17 08:05:00 -0700 | [diff] [blame] | 327 | if (dest->type == SENSOR_TYPE_META_DATA) { |
| 328 | full_handle.localHandle = dest->meta_data.sensor; |
Aaron Whyte | 4d7ac52 | 2014-04-09 15:56:54 -0700 | [diff] [blame] | 329 | dest->meta_data.sensor = get_global_handle(&full_handle); |
Mike Lockwood | f847762 | 2013-10-17 08:05:00 -0700 | [diff] [blame] | 330 | } else { |
| 331 | full_handle.localHandle = dest->sensor; |
Aaron Whyte | 4d7ac52 | 2014-04-09 15:56:54 -0700 | [diff] [blame] | 332 | dest->sensor = get_global_handle(&full_handle); |
Mike Lockwood | f847762 | 2013-10-17 08:05:00 -0700 | [diff] [blame] | 333 | } |
| 334 | } |
| 335 | |
| 336 | int sensors_poll_context_t::poll(sensors_event_t *data, int maxReads) { |
| 337 | ALOGV("poll"); |
| 338 | int empties = 0; |
Nick Vaccaro | c384b18 | 2014-06-10 18:33:07 -0700 | [diff] [blame] | 339 | int queueCount = 0; |
Mike Lockwood | f847762 | 2013-10-17 08:05:00 -0700 | [diff] [blame] | 340 | int eventsRead = 0; |
| 341 | |
| 342 | pthread_mutex_lock(&queue_mutex); |
Nick Vaccaro | c384b18 | 2014-06-10 18:33:07 -0700 | [diff] [blame] | 343 | queueCount = (int)this->queues.size(); |
Mike Lockwood | f847762 | 2013-10-17 08:05:00 -0700 | [diff] [blame] | 344 | while (eventsRead == 0) { |
| 345 | while (empties < queueCount && eventsRead < maxReads) { |
| 346 | SensorEventQueue* queue = this->queues.at(this->nextReadIndex); |
| 347 | sensors_event_t* event = queue->peek(); |
| 348 | if (event == NULL) { |
| 349 | empties++; |
| 350 | } else { |
| 351 | empties = 0; |
Nick Vaccaro | c384b18 | 2014-06-10 18:33:07 -0700 | [diff] [blame] | 352 | this->copy_event_remap_handle(&data[eventsRead], event, nextReadIndex); |
| 353 | if (data[eventsRead].sensor == -1) { |
| 354 | // Bad handle, do not pass corrupted event upstream ! |
| 355 | ALOGW("Dropping bad local handle event packet on the floor"); |
| 356 | } else { |
| 357 | eventsRead++; |
| 358 | } |
Mike Lockwood | f847762 | 2013-10-17 08:05:00 -0700 | [diff] [blame] | 359 | queue->dequeue(); |
| 360 | } |
| 361 | this->nextReadIndex = (this->nextReadIndex + 1) % queueCount; |
| 362 | } |
| 363 | if (eventsRead == 0) { |
| 364 | // The queues have been scanned and none contain data, so wait. |
| 365 | ALOGV("poll stopping to wait for data"); |
| 366 | waiting_for_data = true; |
| 367 | pthread_cond_wait(&data_available_cond, &queue_mutex); |
| 368 | waiting_for_data = false; |
| 369 | empties = 0; |
| 370 | } |
| 371 | } |
| 372 | pthread_mutex_unlock(&queue_mutex); |
| 373 | ALOGV("poll returning %d events.", eventsRead); |
| 374 | |
| 375 | return eventsRead; |
| 376 | } |
| 377 | |
| 378 | int sensors_poll_context_t::batch(int handle, int flags, int64_t period_ns, int64_t timeout) { |
| 379 | ALOGV("batch"); |
| 380 | int retval = -EINVAL; |
Aaron Whyte | 4d7ac52 | 2014-04-09 15:56:54 -0700 | [diff] [blame] | 381 | int local_handle = get_local_handle(handle); |
| 382 | sensors_poll_device_1_t* v1 = this->get_v1_device_by_handle(handle); |
Nick Vaccaro | e914d69 | 2014-10-10 13:32:38 -0700 | [diff] [blame] | 383 | if (halIsCompliant(this, handle) && local_handle >= 0 && v1) { |
Aaron Whyte | 4d7ac52 | 2014-04-09 15:56:54 -0700 | [diff] [blame] | 384 | retval = v1->batch(v1, local_handle, flags, period_ns, timeout); |
Nick Vaccaro | e914d69 | 2014-10-10 13:32:38 -0700 | [diff] [blame] | 385 | } else { |
| 386 | ALOGE("IGNORING batch() call to non-API-compliant sensor handle=%d !", handle); |
Mike Lockwood | f847762 | 2013-10-17 08:05:00 -0700 | [diff] [blame] | 387 | } |
| 388 | ALOGV("retval %d", retval); |
| 389 | return retval; |
| 390 | } |
| 391 | |
| 392 | int sensors_poll_context_t::flush(int handle) { |
| 393 | ALOGV("flush"); |
| 394 | int retval = -EINVAL; |
Aaron Whyte | 4d7ac52 | 2014-04-09 15:56:54 -0700 | [diff] [blame] | 395 | int local_handle = get_local_handle(handle); |
| 396 | sensors_poll_device_1_t* v1 = this->get_v1_device_by_handle(handle); |
Nick Vaccaro | e914d69 | 2014-10-10 13:32:38 -0700 | [diff] [blame] | 397 | if (halIsCompliant(this, handle) && local_handle >= 0 && v1) { |
Aaron Whyte | 4d7ac52 | 2014-04-09 15:56:54 -0700 | [diff] [blame] | 398 | retval = v1->flush(v1, local_handle); |
Nick Vaccaro | e914d69 | 2014-10-10 13:32:38 -0700 | [diff] [blame] | 399 | } else { |
| 400 | ALOGE("IGNORING flush() call to non-API-compliant sensor handle=%d !", handle); |
Mike Lockwood | f847762 | 2013-10-17 08:05:00 -0700 | [diff] [blame] | 401 | } |
| 402 | ALOGV("retval %d", retval); |
| 403 | return retval; |
| 404 | } |
| 405 | |
| 406 | int sensors_poll_context_t::close() { |
| 407 | ALOGV("close"); |
| 408 | for (std::vector<hw_device_t*>::iterator it = this->sub_hw_devices.begin(); |
| 409 | it != this->sub_hw_devices.end(); it++) { |
| 410 | hw_device_t* dev = *it; |
| 411 | int retval = dev->close(dev); |
| 412 | ALOGV("retval %d", retval); |
| 413 | } |
| 414 | return 0; |
| 415 | } |
| 416 | |
| 417 | |
| 418 | static int device__close(struct hw_device_t *dev) { |
| 419 | sensors_poll_context_t* ctx = (sensors_poll_context_t*) dev; |
| 420 | if (ctx != NULL) { |
| 421 | int retval = ctx->close(); |
| 422 | delete ctx; |
| 423 | } |
| 424 | return 0; |
| 425 | } |
| 426 | |
| 427 | static int device__activate(struct sensors_poll_device_t *dev, int handle, |
| 428 | int enabled) { |
| 429 | sensors_poll_context_t* ctx = (sensors_poll_context_t*) dev; |
| 430 | return ctx->activate(handle, enabled); |
| 431 | } |
| 432 | |
| 433 | static int device__setDelay(struct sensors_poll_device_t *dev, int handle, |
| 434 | int64_t ns) { |
| 435 | sensors_poll_context_t* ctx = (sensors_poll_context_t*) dev; |
| 436 | return ctx->setDelay(handle, ns); |
| 437 | } |
| 438 | |
| 439 | static int device__poll(struct sensors_poll_device_t *dev, sensors_event_t* data, |
| 440 | int count) { |
| 441 | sensors_poll_context_t* ctx = (sensors_poll_context_t*) dev; |
| 442 | return ctx->poll(data, count); |
| 443 | } |
| 444 | |
| 445 | static int device__batch(struct sensors_poll_device_1 *dev, int handle, |
| 446 | int flags, int64_t period_ns, int64_t timeout) { |
| 447 | sensors_poll_context_t* ctx = (sensors_poll_context_t*) dev; |
| 448 | return ctx->batch(handle, flags, period_ns, timeout); |
| 449 | } |
| 450 | |
| 451 | static int device__flush(struct sensors_poll_device_1 *dev, int handle) { |
| 452 | sensors_poll_context_t* ctx = (sensors_poll_context_t*) dev; |
| 453 | return ctx->flush(handle); |
| 454 | } |
| 455 | |
| 456 | static int open_sensors(const struct hw_module_t* module, const char* name, |
| 457 | struct hw_device_t** device); |
| 458 | |
| 459 | static bool starts_with(const char* s, const char* prefix) { |
| 460 | if (s == NULL || prefix == NULL) { |
| 461 | return false; |
| 462 | } |
| 463 | size_t s_size = strlen(s); |
| 464 | size_t prefix_size = strlen(prefix); |
| 465 | return s_size >= prefix_size && strncmp(s, prefix, prefix_size) == 0; |
| 466 | } |
| 467 | |
| 468 | /* |
| 469 | * Adds valid paths from the config file to the vector passed in. |
| 470 | * The vector must not be null. |
| 471 | */ |
Satya Durga Srinivasu Prabhala | 1fd3618 | 2015-01-20 18:53:35 -0800 | [diff] [blame] | 472 | static void get_so_paths(std::vector<std::string> *so_paths) { |
| 473 | std::string line; |
| 474 | std::ifstream conf_file(CONFIG_FILENAME); |
| 475 | |
| 476 | if(!conf_file) { |
Mike Lockwood | f847762 | 2013-10-17 08:05:00 -0700 | [diff] [blame] | 477 | ALOGW("No multihal config file found at %s", CONFIG_FILENAME); |
| 478 | return; |
| 479 | } |
Aaron Whyte | f22e1c1 | 2014-04-15 15:41:38 -0700 | [diff] [blame] | 480 | ALOGV("Multihal config file found at %s", CONFIG_FILENAME); |
Satya Durga Srinivasu Prabhala | 1fd3618 | 2015-01-20 18:53:35 -0800 | [diff] [blame] | 481 | while (std::getline(conf_file, line)) { |
| 482 | ALOGV("config file line: '%s'", line.c_str()); |
| 483 | so_paths->push_back(line); |
Mike Lockwood | f847762 | 2013-10-17 08:05:00 -0700 | [diff] [blame] | 484 | } |
Mike Lockwood | f847762 | 2013-10-17 08:05:00 -0700 | [diff] [blame] | 485 | } |
| 486 | |
| 487 | /* |
| 488 | * Ensures that the sub-module array is initialized. |
| 489 | * This can be first called from get_sensors_list or from open_sensors. |
| 490 | */ |
| 491 | static void lazy_init_modules() { |
| 492 | pthread_mutex_lock(&init_modules_mutex); |
| 493 | if (sub_hw_modules != NULL) { |
| 494 | pthread_mutex_unlock(&init_modules_mutex); |
| 495 | return; |
| 496 | } |
Satya Durga Srinivasu Prabhala | 1fd3618 | 2015-01-20 18:53:35 -0800 | [diff] [blame] | 497 | std::vector<std::string> *so_paths = new std::vector<std::string>(); |
Mike Lockwood | f847762 | 2013-10-17 08:05:00 -0700 | [diff] [blame] | 498 | get_so_paths(so_paths); |
| 499 | |
| 500 | // dlopen the module files and cache their module symbols in sub_hw_modules |
| 501 | sub_hw_modules = new std::vector<hw_module_t *>(); |
| 502 | dlerror(); // clear any old errors |
| 503 | const char* sym = HAL_MODULE_INFO_SYM_AS_STR; |
Satya Durga Srinivasu Prabhala | 1fd3618 | 2015-01-20 18:53:35 -0800 | [diff] [blame] | 504 | for (std::vector<std::string>::iterator it = so_paths->begin(); it != so_paths->end(); it++) { |
| 505 | const char* path = it->c_str(); |
Mike Lockwood | f847762 | 2013-10-17 08:05:00 -0700 | [diff] [blame] | 506 | void* lib_handle = dlopen(path, RTLD_LAZY); |
| 507 | if (lib_handle == NULL) { |
| 508 | ALOGW("dlerror(): %s", dlerror()); |
| 509 | } else { |
Aaron Whyte | f22e1c1 | 2014-04-15 15:41:38 -0700 | [diff] [blame] | 510 | ALOGI("Loaded library from %s", path); |
Mike Lockwood | f847762 | 2013-10-17 08:05:00 -0700 | [diff] [blame] | 511 | ALOGV("Opening symbol \"%s\"", sym); |
| 512 | // clear old errors |
| 513 | dlerror(); |
| 514 | struct hw_module_t* module = (hw_module_t*) dlsym(lib_handle, sym); |
| 515 | const char* error; |
| 516 | if ((error = dlerror()) != NULL) { |
| 517 | ALOGW("Error calling dlsym: %s", error); |
| 518 | } else if (module == NULL) { |
| 519 | ALOGW("module == NULL"); |
| 520 | } else { |
Aaron Whyte | f22e1c1 | 2014-04-15 15:41:38 -0700 | [diff] [blame] | 521 | ALOGV("Loaded symbols from \"%s\"", sym); |
Mike Lockwood | f847762 | 2013-10-17 08:05:00 -0700 | [diff] [blame] | 522 | sub_hw_modules->push_back(module); |
| 523 | } |
| 524 | } |
| 525 | } |
| 526 | pthread_mutex_unlock(&init_modules_mutex); |
| 527 | } |
| 528 | |
| 529 | /* |
| 530 | * Lazy-initializes global_sensors_count, global_sensors_list, and module_sensor_handles. |
| 531 | */ |
| 532 | static void lazy_init_sensors_list() { |
| 533 | ALOGV("lazy_init_sensors_list"); |
| 534 | pthread_mutex_lock(&init_sensors_mutex); |
| 535 | if (global_sensors_list != NULL) { |
| 536 | // already initialized |
| 537 | pthread_mutex_unlock(&init_sensors_mutex); |
| 538 | ALOGV("lazy_init_sensors_list - early return"); |
| 539 | return; |
| 540 | } |
| 541 | |
| 542 | ALOGV("lazy_init_sensors_list needs to do work"); |
| 543 | lazy_init_modules(); |
| 544 | |
| 545 | // Count all the sensors, then allocate an array of blanks. |
| 546 | global_sensors_count = 0; |
| 547 | const struct sensor_t *subhal_sensors_list; |
| 548 | for (std::vector<hw_module_t*>::iterator it = sub_hw_modules->begin(); |
| 549 | it != sub_hw_modules->end(); it++) { |
| 550 | struct sensors_module_t *module = (struct sensors_module_t*) *it; |
| 551 | global_sensors_count += module->get_sensors_list(module, &subhal_sensors_list); |
| 552 | ALOGV("increased global_sensors_count to %d", global_sensors_count); |
| 553 | } |
| 554 | |
| 555 | // The global_sensors_list is full of consts. |
| 556 | // Manipulate this non-const list, and point the const one to it when we're done. |
| 557 | sensor_t* mutable_sensor_list = new sensor_t[global_sensors_count]; |
| 558 | |
| 559 | // index of the next sensor to set in mutable_sensor_list |
| 560 | int mutable_sensor_index = 0; |
| 561 | int module_index = 0; |
| 562 | |
| 563 | for (std::vector<hw_module_t*>::iterator it = sub_hw_modules->begin(); |
| 564 | it != sub_hw_modules->end(); it++) { |
| 565 | hw_module_t *hw_module = *it; |
| 566 | ALOGV("examine one module"); |
| 567 | // Read the sub-module's sensor list. |
| 568 | struct sensors_module_t *module = (struct sensors_module_t*) hw_module; |
| 569 | int module_sensor_count = module->get_sensors_list(module, &subhal_sensors_list); |
| 570 | ALOGV("the module has %d sensors", module_sensor_count); |
| 571 | |
| 572 | // Copy the HAL's sensor list into global_sensors_list, |
| 573 | // with the handle changed to be a global handle. |
| 574 | for (int i = 0; i < module_sensor_count; i++) { |
| 575 | ALOGV("examining one sensor"); |
| 576 | const struct sensor_t *local_sensor = &subhal_sensors_list[i]; |
| 577 | int local_handle = local_sensor->handle; |
| 578 | memcpy(&mutable_sensor_list[mutable_sensor_index], local_sensor, |
| 579 | sizeof(struct sensor_t)); |
| 580 | |
| 581 | // Overwrite the global version's handle with a global handle. |
| 582 | int global_handle = assign_global_handle(module_index, local_handle); |
| 583 | |
| 584 | mutable_sensor_list[mutable_sensor_index].handle = global_handle; |
Aaron Whyte | f22e1c1 | 2014-04-15 15:41:38 -0700 | [diff] [blame] | 585 | ALOGV("module_index %d, local_handle %d, global_handle %d", |
Mike Lockwood | f847762 | 2013-10-17 08:05:00 -0700 | [diff] [blame] | 586 | module_index, local_handle, global_handle); |
| 587 | |
| 588 | mutable_sensor_index++; |
| 589 | } |
| 590 | module_index++; |
| 591 | } |
| 592 | // Set the const static global_sensors_list to the mutable one allocated by this function. |
| 593 | global_sensors_list = mutable_sensor_list; |
| 594 | |
| 595 | pthread_mutex_unlock(&init_sensors_mutex); |
| 596 | ALOGV("end lazy_init_sensors_list"); |
| 597 | } |
| 598 | |
Nick Vaccaro | e914d69 | 2014-10-10 13:32:38 -0700 | [diff] [blame] | 599 | static int module__get_sensors_list(__unused struct sensors_module_t* module, |
Mike Lockwood | f847762 | 2013-10-17 08:05:00 -0700 | [diff] [blame] | 600 | struct sensor_t const** list) { |
| 601 | ALOGV("module__get_sensors_list start"); |
| 602 | lazy_init_sensors_list(); |
| 603 | *list = global_sensors_list; |
| 604 | ALOGV("global_sensors_count: %d", global_sensors_count); |
| 605 | for (int i = 0; i < global_sensors_count; i++) { |
| 606 | ALOGV("sensor type: %d", global_sensors_list[i].type); |
| 607 | } |
| 608 | return global_sensors_count; |
| 609 | } |
| 610 | |
| 611 | static struct hw_module_methods_t sensors_module_methods = { |
Nick Vaccaro | a889c09 | 2016-04-15 10:17:07 -0700 | [diff] [blame] | 612 | .open = open_sensors |
Mike Lockwood | f847762 | 2013-10-17 08:05:00 -0700 | [diff] [blame] | 613 | }; |
| 614 | |
| 615 | struct sensors_module_t HAL_MODULE_INFO_SYM = { |
Nick Vaccaro | a889c09 | 2016-04-15 10:17:07 -0700 | [diff] [blame] | 616 | .common = { |
| 617 | .tag = HARDWARE_MODULE_TAG, |
| 618 | .version_major = 1, |
| 619 | .version_minor = 1, |
| 620 | .id = SENSORS_HARDWARE_MODULE_ID, |
| 621 | .name = "MultiHal Sensor Module", |
| 622 | .author = "Google, Inc", |
| 623 | .methods = &sensors_module_methods, |
| 624 | .dso = NULL, |
| 625 | .reserved = {0}, |
Mike Lockwood | f847762 | 2013-10-17 08:05:00 -0700 | [diff] [blame] | 626 | }, |
Nick Vaccaro | a889c09 | 2016-04-15 10:17:07 -0700 | [diff] [blame] | 627 | .get_sensors_list = module__get_sensors_list |
Mike Lockwood | f847762 | 2013-10-17 08:05:00 -0700 | [diff] [blame] | 628 | }; |
| 629 | |
| 630 | static int open_sensors(const struct hw_module_t* hw_module, const char* name, |
| 631 | struct hw_device_t** hw_device_out) { |
Aaron Whyte | f22e1c1 | 2014-04-15 15:41:38 -0700 | [diff] [blame] | 632 | ALOGV("open_sensors begin..."); |
Mike Lockwood | f847762 | 2013-10-17 08:05:00 -0700 | [diff] [blame] | 633 | |
| 634 | lazy_init_modules(); |
| 635 | |
| 636 | // Create proxy device, to return later. |
| 637 | sensors_poll_context_t *dev = new sensors_poll_context_t(); |
| 638 | memset(dev, 0, sizeof(sensors_poll_device_1_t)); |
| 639 | dev->proxy_device.common.tag = HARDWARE_DEVICE_TAG; |
Nick Vaccaro | e914d69 | 2014-10-10 13:32:38 -0700 | [diff] [blame] | 640 | dev->proxy_device.common.version = SENSORS_DEVICE_API_VERSION_1_3; |
Mike Lockwood | f847762 | 2013-10-17 08:05:00 -0700 | [diff] [blame] | 641 | dev->proxy_device.common.module = const_cast<hw_module_t*>(hw_module); |
| 642 | dev->proxy_device.common.close = device__close; |
| 643 | dev->proxy_device.activate = device__activate; |
| 644 | dev->proxy_device.setDelay = device__setDelay; |
| 645 | dev->proxy_device.poll = device__poll; |
| 646 | dev->proxy_device.batch = device__batch; |
| 647 | dev->proxy_device.flush = device__flush; |
| 648 | |
| 649 | dev->nextReadIndex = 0; |
| 650 | |
| 651 | // Open() the subhal modules. Remember their devices in a vector parallel to sub_hw_modules. |
| 652 | for (std::vector<hw_module_t*>::iterator it = sub_hw_modules->begin(); |
| 653 | it != sub_hw_modules->end(); it++) { |
| 654 | sensors_module_t *sensors_module = (sensors_module_t*) *it; |
| 655 | struct hw_device_t* sub_hw_device; |
| 656 | int sub_open_result = sensors_module->common.methods->open(*it, name, &sub_hw_device); |
Nick Vaccaro | e914d69 | 2014-10-10 13:32:38 -0700 | [diff] [blame] | 657 | if (!sub_open_result) { |
| 658 | if (!HAL_VERSION_IS_COMPLIANT(sub_hw_device->version)) { |
| 659 | ALOGE("SENSORS_DEVICE_API_VERSION_1_3 is required for all sensor HALs"); |
| 660 | ALOGE("This HAL reports non-compliant API level : %s", |
| 661 | apiNumToStr(sub_hw_device->version)); |
| 662 | ALOGE("Sensors belonging to this HAL will get ignored !"); |
| 663 | } |
Nick Vaccaro | 93bf996 | 2014-03-17 13:05:09 -0700 | [diff] [blame] | 664 | dev->addSubHwDevice(sub_hw_device); |
Nick Vaccaro | e914d69 | 2014-10-10 13:32:38 -0700 | [diff] [blame] | 665 | } |
Mike Lockwood | f847762 | 2013-10-17 08:05:00 -0700 | [diff] [blame] | 666 | } |
| 667 | |
| 668 | // Prepare the output param and return |
| 669 | *hw_device_out = &dev->proxy_device.common; |
Aaron Whyte | f22e1c1 | 2014-04-15 15:41:38 -0700 | [diff] [blame] | 670 | ALOGV("...open_sensors end"); |
Mike Lockwood | f847762 | 2013-10-17 08:05:00 -0700 | [diff] [blame] | 671 | return 0; |
| 672 | } |