blob: 9f21731fa97de3428026bffcf0bf7528aa50addc [file] [log] [blame]
Peng Xue36e3472016-11-03 11:57:10 -07001/*
2 * Copyright (C) 2016 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_SENSOR_DIRECT_CONNECTION_H
18#define ANDROID_SENSOR_DIRECT_CONNECTION_H
19
Rocky Fang8aa09802024-08-01 18:07:54 +000020#include <android-base/thread_annotations.h>
Peng Xue36e3472016-11-03 11:57:10 -070021#include <stdint.h>
22#include <sys/types.h>
Rocky Fang8aa09802024-08-01 18:07:54 +000023#include <optional>
Peng Xue36e3472016-11-03 11:57:10 -070024
25#include <binder/BinderService.h>
26
Mathias Agopian801ea092017-03-06 15:05:04 -080027#include <sensor/Sensor.h>
28#include <sensor/BitTube.h>
29#include <sensor/ISensorServer.h>
30#include <sensor/ISensorEventConnection.h>
Peng Xue36e3472016-11-03 11:57:10 -070031
32#include "SensorService.h"
33
34namespace android {
35
36class SensorService;
37class BitTube;
38
39class SensorService::SensorDirectConnection: public BnSensorEventConnection {
40public:
Rocky Fang8aa09802024-08-01 18:07:54 +000041 SensorDirectConnection(const sp<SensorService>& service, uid_t uid, pid_t pid,
42 const sensors_direct_mem_t* mem, int32_t halChannelHandle,
43 const String16& opPackageName, int deviceId);
Peng Xue36e3472016-11-03 11:57:10 -070044 void dump(String8& result) const;
Mike Ma24743862020-01-29 00:36:55 -080045 void dump(util::ProtoOutputStream* proto) const;
Peng Xue36e3472016-11-03 11:57:10 -070046 uid_t getUid() const { return mUid; }
Arthur Ishiguroe3ed3d22020-04-13 10:29:44 -070047 const String16& getOpPackageName() const { return mOpPackageName; }
Peng Xue36e3472016-11-03 11:57:10 -070048 int32_t getHalChannelHandle() const;
Rocky Fang8aa09802024-08-01 18:07:54 +000049 bool isEquivalent(const sensors_direct_mem_t* mem) const;
Peng Xue36e3472016-11-03 11:57:10 -070050
Arthur Ishiguroe3ed3d22020-04-13 10:29:44 -070051 // Invoked when access to sensors for this connection has changed, e.g. lost or
52 // regained due to changes in the sensor restricted/privacy mode or the
53 // app changed to idle/active status.
54 void onSensorAccessChanged(bool hasAccess);
Anh Pham5198c992021-02-10 14:15:30 +010055 void onMicSensorAccessChanged(bool isMicToggleOn);
56 userid_t getUserId() const { return mUserId; }
Vladimir Komsiyski51c1f5a2023-01-19 18:25:43 +010057 int getDeviceId() const { return mDeviceId; }
Peng Xue36e3472016-11-03 11:57:10 -070058
59protected:
60 virtual ~SensorDirectConnection();
61 // ISensorEventConnection functions
62 virtual void onFirstRef();
63 virtual sp<BitTube> getSensorChannel() const;
64 virtual status_t enableDisable(int handle, bool enabled, nsecs_t samplingPeriodNs,
65 nsecs_t maxBatchReportLatencyNs, int reservedFlags);
66 virtual status_t setEventRate(int handle, nsecs_t samplingPeriodNs);
67 virtual status_t flush();
68 virtual int32_t configureChannel(int handle, int rateLevel);
Peng Xu8cbefd72017-07-10 16:41:08 -070069 virtual void destroy();
Peng Xue36e3472016-11-03 11:57:10 -070070private:
Arthur Ishiguroe3ed3d22020-04-13 10:29:44 -070071 bool hasSensorAccess() const;
72
Vladimir Komsiyski51c1f5a2023-01-19 18:25:43 +010073 // Sends the configuration to the relevant sensor device.
74 int configure(int handle, const sensors_direct_cfg_t* config);
75
Arthur Ishiguroe3ed3d22020-04-13 10:29:44 -070076 // Stops all active sensor direct report requests.
77 //
78 // If backupRecord is true, stopped requests can be recovered
79 // by a subsequent recoverAll() call (e.g. when temporarily stopping
80 // sensors for sensor privacy/restrict mode or when an app becomes
81 // idle).
82 void stopAll(bool backupRecord = false);
83 // Same as stopAll() but with mConnectionLock held.
84 void stopAllLocked(bool backupRecord);
85
86 // Recover sensor requests previously stopped by stopAll(true).
87 // This method can be called when a sensor access resumes (e.g.
88 // sensor privacy/restrict mode lifted or app becomes active).
89 //
90 // If no requests are backed up by stopAll(), this method is no-op.
91 void recoverAll();
92
Anh Pham5198c992021-02-10 14:15:30 +010093 // Limits all active sensor direct report requests when the mic toggle is flipped to on.
94 void capRates();
95 // Recover sensor requests previously capped by capRates().
96 void uncapRates();
97
Rocky Fang8aa09802024-08-01 18:07:54 +000098 // Dumps a set of sensor infos.
99 void dumpSensorInfoWithLock(String8& result, std::unordered_map<int, int> sensors) const
100 EXCLUSIVE_LOCKS_REQUIRED(mConnectionLock);
101
Peng Xue36e3472016-11-03 11:57:10 -0700102 const sp<SensorService> mService;
103 const uid_t mUid;
Rocky Fang8aa09802024-08-01 18:07:54 +0000104 const pid_t mPid;
Peng Xue36e3472016-11-03 11:57:10 -0700105 const sensors_direct_mem_t mMem;
106 const int32_t mHalChannelHandle;
107 const String16 mOpPackageName;
Vladimir Komsiyski51c1f5a2023-01-19 18:25:43 +0100108 const int mDeviceId;
Peng Xue36e3472016-11-03 11:57:10 -0700109
110 mutable Mutex mConnectionLock;
111 std::unordered_map<int, int> mActivated;
112 std::unordered_map<int, int> mActivatedBackup;
Anh Pham5198c992021-02-10 14:15:30 +0100113 std::unordered_map<int, int> mMicRateBackup;
Peng Xu8cbefd72017-07-10 16:41:08 -0700114
115 mutable Mutex mDestroyLock;
116 bool mDestroyed;
Anh Pham5198c992021-02-10 14:15:30 +0100117 userid_t mUserId;
Arthur Ishiguro8a628522021-09-22 14:10:16 -0700118
119 std::optional<bool> mIsRateCappedBasedOnPermission;
120
121 bool isRateCappedBasedOnPermission() {
122 if (!mIsRateCappedBasedOnPermission.has_value()) {
123 mIsRateCappedBasedOnPermission =
124 mService->isRateCappedBasedOnPermission(mOpPackageName);
125 }
126 return mIsRateCappedBasedOnPermission.value();
127 }
Peng Xue36e3472016-11-03 11:57:10 -0700128};
129
130} // namepsace android
131
132#endif // ANDROID_SENSOR_DIRECT_CONNECTION_H
133