blob: bfaf8113301d06131f175c015bf5182e37b59d1c [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
Arthur Ishiguro8a628522021-09-22 14:10:16 -070020#include <optional>
Peng Xue36e3472016-11-03 11:57:10 -070021#include <stdint.h>
22#include <sys/types.h>
23
24#include <binder/BinderService.h>
25
Mathias Agopian801ea092017-03-06 15:05:04 -080026#include <sensor/Sensor.h>
27#include <sensor/BitTube.h>
28#include <sensor/ISensorServer.h>
29#include <sensor/ISensorEventConnection.h>
Peng Xue36e3472016-11-03 11:57:10 -070030
31#include "SensorService.h"
32
33namespace android {
34
35class SensorService;
36class BitTube;
37
38class SensorService::SensorDirectConnection: public BnSensorEventConnection {
39public:
40 SensorDirectConnection(const sp<SensorService>& service, uid_t uid,
41 const sensors_direct_mem_t *mem, int32_t halChannelHandle,
Vladimir Komsiyski4871f092023-01-19 18:25:43 +010042 const String16& opPackageName, int deviceId);
Peng Xue36e3472016-11-03 11:57:10 -070043 void dump(String8& result) const;
Mike Ma24743862020-01-29 00:36:55 -080044 void dump(util::ProtoOutputStream* proto) const;
Peng Xue36e3472016-11-03 11:57:10 -070045 uid_t getUid() const { return mUid; }
Arthur Ishiguroe3ed3d22020-04-13 10:29:44 -070046 const String16& getOpPackageName() const { return mOpPackageName; }
Peng Xue36e3472016-11-03 11:57:10 -070047 int32_t getHalChannelHandle() const;
48 bool isEquivalent(const sensors_direct_mem_t *mem) const;
49
Arthur Ishiguroe3ed3d22020-04-13 10:29:44 -070050 // Invoked when access to sensors for this connection has changed, e.g. lost or
51 // regained due to changes in the sensor restricted/privacy mode or the
52 // app changed to idle/active status.
53 void onSensorAccessChanged(bool hasAccess);
Anh Pham5198c992021-02-10 14:15:30 +010054 void onMicSensorAccessChanged(bool isMicToggleOn);
55 userid_t getUserId() const { return mUserId; }
Vladimir Komsiyski4871f092023-01-19 18:25:43 +010056 int getDeviceId() const { return mDeviceId; }
Peng Xue36e3472016-11-03 11:57:10 -070057
58protected:
59 virtual ~SensorDirectConnection();
60 // ISensorEventConnection functions
61 virtual void onFirstRef();
62 virtual sp<BitTube> getSensorChannel() const;
63 virtual status_t enableDisable(int handle, bool enabled, nsecs_t samplingPeriodNs,
64 nsecs_t maxBatchReportLatencyNs, int reservedFlags);
65 virtual status_t setEventRate(int handle, nsecs_t samplingPeriodNs);
66 virtual status_t flush();
67 virtual int32_t configureChannel(int handle, int rateLevel);
Peng Xu8cbefd72017-07-10 16:41:08 -070068 virtual void destroy();
Peng Xue36e3472016-11-03 11:57:10 -070069private:
Arthur Ishiguroe3ed3d22020-04-13 10:29:44 -070070 bool hasSensorAccess() const;
71
Vladimir Komsiyski4871f092023-01-19 18:25:43 +010072 // Sends the configuration to the relevant sensor device.
73 int configure(int handle, const sensors_direct_cfg_t* config);
74
Arthur Ishiguroe3ed3d22020-04-13 10:29:44 -070075 // Stops all active sensor direct report requests.
76 //
77 // If backupRecord is true, stopped requests can be recovered
78 // by a subsequent recoverAll() call (e.g. when temporarily stopping
79 // sensors for sensor privacy/restrict mode or when an app becomes
80 // idle).
81 void stopAll(bool backupRecord = false);
82 // Same as stopAll() but with mConnectionLock held.
83 void stopAllLocked(bool backupRecord);
84
85 // Recover sensor requests previously stopped by stopAll(true).
86 // This method can be called when a sensor access resumes (e.g.
87 // sensor privacy/restrict mode lifted or app becomes active).
88 //
89 // If no requests are backed up by stopAll(), this method is no-op.
90 void recoverAll();
91
Anh Pham5198c992021-02-10 14:15:30 +010092 // Limits all active sensor direct report requests when the mic toggle is flipped to on.
93 void capRates();
94 // Recover sensor requests previously capped by capRates().
95 void uncapRates();
96
Peng Xue36e3472016-11-03 11:57:10 -070097 const sp<SensorService> mService;
98 const uid_t mUid;
99 const sensors_direct_mem_t mMem;
100 const int32_t mHalChannelHandle;
101 const String16 mOpPackageName;
Vladimir Komsiyski4871f092023-01-19 18:25:43 +0100102 const int mDeviceId;
Peng Xue36e3472016-11-03 11:57:10 -0700103
104 mutable Mutex mConnectionLock;
105 std::unordered_map<int, int> mActivated;
106 std::unordered_map<int, int> mActivatedBackup;
Anh Pham5198c992021-02-10 14:15:30 +0100107 std::unordered_map<int, int> mMicRateBackup;
Peng Xu8cbefd72017-07-10 16:41:08 -0700108
109 mutable Mutex mDestroyLock;
110 bool mDestroyed;
Anh Pham5198c992021-02-10 14:15:30 +0100111 userid_t mUserId;
Arthur Ishiguro8a628522021-09-22 14:10:16 -0700112
113 std::optional<bool> mIsRateCappedBasedOnPermission;
114
115 bool isRateCappedBasedOnPermission() {
116 if (!mIsRateCappedBasedOnPermission.has_value()) {
117 mIsRateCappedBasedOnPermission =
118 mService->isRateCappedBasedOnPermission(mOpPackageName);
119 }
120 return mIsRateCappedBasedOnPermission.value();
121 }
Peng Xue36e3472016-11-03 11:57:10 -0700122};
123
124} // namepsace android
125
126#endif // ANDROID_SENSOR_DIRECT_CONNECTION_H
127