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