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:
33     class SensorDeviceCallback {
34     public:
35         virtual void onDynamicSensorsConnected(
36                 const std::vector<sensor_t> &dynamicSensorsAdded) = 0;
37 
38         virtual void onDynamicSensorsDisconnected(
39                 const std::vector<int32_t> &dynamicSensorHandlesRemoved) = 0;
40 
~SensorDeviceCallback()41         virtual ~SensorDeviceCallback(){};
42     };
43 
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 
~ISensorHalWrapper()51     virtual ~ISensorHalWrapper(){};
52 
53     /**
54      * Connects to the underlying sensors HAL. This should also be used for any reconnections
55      * due to HAL resets.
56      */
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;
64 
65     /**
66      * Polls for available sensor events. This could be using the traditional sensors
67      * polling or from a FMQ.
68      */
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;
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 
92     virtual status_t unregisterDirectChannel(int32_t channelHandle) = 0;
93 
94     virtual status_t configureDirectChannel(int32_t sensorHandle, int32_t channelHandle,
95                                             const struct sensors_direct_cfg_t *config) = 0;
96 
97     virtual void writeWakeLockHandled(uint32_t count) = 0;
98 
99     std::atomic_bool mReconnecting = false;
100 
101     std::atomic_bool mInHalBypassMode = false;
102 };
103 
104 } // namespace android
105 
106 #endif // ANDROID_ISENSOR_HAL_WRAPPER_H
107