Arthur Ishiguro | 5e3eaa8 | 2021-11-11 18:05:56 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2021 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 | #ifndef ANDROID_ISENSOR_HAL_WRAPPER_H |
| 18 | #define ANDROID_ISENSOR_HAL_WRAPPER_H |
| 19 | |
| 20 | #include <hardware/sensors.h> |
| 21 | #include <stdint.h> |
| 22 | #include <sys/types.h> |
| 23 | |
| 24 | #include "SensorService.h" |
| 25 | |
| 26 | namespace android { |
| 27 | |
| 28 | /** |
| 29 | * A wrapper for various types of HAL implementation, e.g. to distinguish HIDL and AIDL versions. |
| 30 | */ |
| 31 | class ISensorHalWrapper { |
| 32 | public: |
Arthur Ishiguro | 24804dc | 2021-11-12 17:17:09 +0000 | [diff] [blame] | 33 | class SensorDeviceCallback { |
| 34 | public: |
| 35 | virtual void onDynamicSensorsConnected( |
Arthur Ishiguro | 5e3eaa8 | 2021-11-11 18:05:56 +0000 | [diff] [blame] | 36 | const std::vector<sensor_t> &dynamicSensorsAdded) = 0; |
| 37 | |
Arthur Ishiguro | 24804dc | 2021-11-12 17:17:09 +0000 | [diff] [blame] | 38 | virtual void onDynamicSensorsDisconnected( |
Arthur Ishiguro | 5e3eaa8 | 2021-11-11 18:05:56 +0000 | [diff] [blame] | 39 | const std::vector<int32_t> &dynamicSensorHandlesRemoved) = 0; |
Arthur Ishiguro | 24804dc | 2021-11-12 17:17:09 +0000 | [diff] [blame] | 40 | |
| 41 | virtual ~SensorDeviceCallback(){}; |
Arthur Ishiguro | 5e3eaa8 | 2021-11-11 18:05:56 +0000 | [diff] [blame] | 42 | }; |
| 43 | |
Arthur Ishiguro | 24804dc | 2021-11-12 17:17:09 +0000 | [diff] [blame] | 44 | enum HalConnectionStatus { |
| 45 | CONNECTED, // Successfully connected to the HAL |
| 46 | DOES_NOT_EXIST, // Could not find the HAL |
| 47 | FAILED_TO_CONNECT, // Found the HAL but failed to connect/initialize |
| 48 | UNKNOWN, |
| 49 | }; |
| 50 | |
| 51 | virtual ~ISensorHalWrapper(){}; |
| 52 | |
Arthur Ishiguro | 5e3eaa8 | 2021-11-11 18:05:56 +0000 | [diff] [blame] | 53 | /** |
| 54 | * Connects to the underlying sensors HAL. This should also be used for any reconnections |
| 55 | * due to HAL resets. |
| 56 | */ |
Arthur Ishiguro | 24804dc | 2021-11-12 17:17:09 +0000 | [diff] [blame] | 57 | virtual bool connect(SensorDeviceCallback *callback) = 0; |
| 58 | |
| 59 | virtual void prepareForReconnect() = 0; |
| 60 | |
| 61 | virtual bool supportsPolling() = 0; |
| 62 | |
| 63 | virtual bool supportsMessageQueues() = 0; |
Arthur Ishiguro | 5e3eaa8 | 2021-11-11 18:05:56 +0000 | [diff] [blame] | 64 | |
| 65 | /** |
| 66 | * Polls for available sensor events. This could be using the traditional sensors |
| 67 | * polling or from a FMQ. |
| 68 | */ |
Arthur Ishiguro | 24804dc | 2021-11-12 17:17:09 +0000 | [diff] [blame] | 69 | virtual ssize_t poll(sensors_event_t *buffer, size_t count) = 0; |
| 70 | |
| 71 | virtual ssize_t pollFmq(sensors_event_t *buffer, size_t maxNumEventsToRead) = 0; |
Arthur Ishiguro | 5e3eaa8 | 2021-11-11 18:05:56 +0000 | [diff] [blame] | 72 | |
| 73 | /** |
| 74 | * The below functions directly mirrors the sensors HAL definitions. |
| 75 | */ |
| 76 | virtual std::vector<sensor_t> getSensorsList() = 0; |
| 77 | |
| 78 | virtual status_t setOperationMode(SensorService::Mode mode) = 0; |
| 79 | |
| 80 | virtual status_t activate(int32_t sensorHandle, bool enabled) = 0; |
| 81 | |
| 82 | virtual status_t batch(int32_t sensorHandle, int64_t samplingPeriodNs, |
| 83 | int64_t maxReportLatencyNs) = 0; |
| 84 | |
| 85 | virtual status_t flush(int32_t sensorHandle) = 0; |
| 86 | |
| 87 | virtual status_t injectSensorData(const sensors_event_t *event) = 0; |
| 88 | |
| 89 | virtual status_t registerDirectChannel(const sensors_direct_mem_t *memory, |
| 90 | int32_t *channelHandle) = 0; |
| 91 | |
Arthur Ishiguro | 24804dc | 2021-11-12 17:17:09 +0000 | [diff] [blame] | 92 | virtual status_t unregisterDirectChannel(int32_t channelHandle) = 0; |
Arthur Ishiguro | 5e3eaa8 | 2021-11-11 18:05:56 +0000 | [diff] [blame] | 93 | |
| 94 | virtual status_t configureDirectChannel(int32_t sensorHandle, int32_t channelHandle, |
| 95 | const struct sensors_direct_cfg_t *config) = 0; |
Arthur Ishiguro | 24804dc | 2021-11-12 17:17:09 +0000 | [diff] [blame] | 96 | |
Arthur Ishiguro | 24804dc | 2021-11-12 17:17:09 +0000 | [diff] [blame] | 97 | virtual void writeWakeLockHandled(uint32_t count) = 0; |
| 98 | |
| 99 | std::atomic_bool mReconnecting = false; |
Mark Wheatley | d8119da | 2024-02-23 22:57:18 +0000 | [diff] [blame] | 100 | |
| 101 | std::atomic_bool mInHalBypassMode = false; |
Arthur Ishiguro | 24804dc | 2021-11-12 17:17:09 +0000 | [diff] [blame] | 102 | }; |
Arthur Ishiguro | 5e3eaa8 | 2021-11-11 18:05:56 +0000 | [diff] [blame] | 103 | |
| 104 | } // namespace android |
| 105 | |
| 106 | #endif // ANDROID_ISENSOR_HAL_WRAPPER_H |