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