blob: 891dfe5a7729a12a49425bdb2936fc15e39d30ff [file] [log] [blame]
Arthur Ishiguro5e3eaa82021-11-11 18:05:56 +00001/*
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
26namespace android {
27
28/**
29 * A wrapper for various types of HAL implementation, e.g. to distinguish HIDL and AIDL versions.
30 */
31class ISensorHalWrapper {
32public:
Arthur Ishiguro24804dc2021-11-12 17:17:09 +000033 class SensorDeviceCallback {
34 public:
35 virtual void onDynamicSensorsConnected(
Arthur Ishiguro5e3eaa82021-11-11 18:05:56 +000036 const std::vector<sensor_t> &dynamicSensorsAdded) = 0;
37
Arthur Ishiguro24804dc2021-11-12 17:17:09 +000038 virtual void onDynamicSensorsDisconnected(
Arthur Ishiguro5e3eaa82021-11-11 18:05:56 +000039 const std::vector<int32_t> &dynamicSensorHandlesRemoved) = 0;
Arthur Ishiguro24804dc2021-11-12 17:17:09 +000040
41 virtual ~SensorDeviceCallback(){};
Arthur Ishiguro5e3eaa82021-11-11 18:05:56 +000042 };
43
Arthur Ishiguro24804dc2021-11-12 17:17:09 +000044 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 Ishiguro5e3eaa82021-11-11 18:05:56 +000053 /**
54 * Connects to the underlying sensors HAL. This should also be used for any reconnections
55 * due to HAL resets.
56 */
Arthur Ishiguro24804dc2021-11-12 17:17:09 +000057 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 Ishiguro5e3eaa82021-11-11 18:05:56 +000064
65 /**
66 * Polls for available sensor events. This could be using the traditional sensors
67 * polling or from a FMQ.
68 */
Arthur Ishiguro24804dc2021-11-12 17:17:09 +000069 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 Ishiguro5e3eaa82021-11-11 18:05:56 +000072
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 Ishiguro24804dc2021-11-12 17:17:09 +000092 virtual status_t unregisterDirectChannel(int32_t channelHandle) = 0;
Arthur Ishiguro5e3eaa82021-11-11 18:05:56 +000093
94 virtual status_t configureDirectChannel(int32_t sensorHandle, int32_t channelHandle,
95 const struct sensors_direct_cfg_t *config) = 0;
Arthur Ishiguro24804dc2021-11-12 17:17:09 +000096
Arthur Ishiguro24804dc2021-11-12 17:17:09 +000097 virtual void writeWakeLockHandled(uint32_t count) = 0;
98
99 std::atomic_bool mReconnecting = false;
Mark Wheatleyd8119da2024-02-23 22:57:18 +0000100
101 std::atomic_bool mInHalBypassMode = false;
Arthur Ishiguro24804dc2021-11-12 17:17:09 +0000102};
Arthur Ishiguro5e3eaa82021-11-11 18:05:56 +0000103
104} // namespace android
105
106#endif // ANDROID_ISENSOR_HAL_WRAPPER_H