blob: a3f348b66831937235b5efdeb197895e1cc19486 [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
20#include <stdint.h>
21#include <sys/types.h>
22
23#include <binder/BinderService.h>
24
Mathias Agopian801ea092017-03-06 15:05:04 -080025#include <sensor/Sensor.h>
26#include <sensor/BitTube.h>
27#include <sensor/ISensorServer.h>
28#include <sensor/ISensorEventConnection.h>
Peng Xue36e3472016-11-03 11:57:10 -070029
30#include "SensorService.h"
31
32namespace android {
33
34class SensorService;
35class BitTube;
36
37class SensorService::SensorDirectConnection: public BnSensorEventConnection {
38public:
39 SensorDirectConnection(const sp<SensorService>& service, uid_t uid,
40 const sensors_direct_mem_t *mem, int32_t halChannelHandle,
41 const String16& opPackageName);
42 void dump(String8& result) const;
Mike Ma24743862020-01-29 00:36:55 -080043 void dump(util::ProtoOutputStream* proto) const;
Peng Xue36e3472016-11-03 11:57:10 -070044 uid_t getUid() const { return mUid; }
Arthur Ishiguroe3ed3d22020-04-13 10:29:44 -070045 const String16& getOpPackageName() const { return mOpPackageName; }
Peng Xue36e3472016-11-03 11:57:10 -070046 int32_t getHalChannelHandle() const;
47 bool isEquivalent(const sensors_direct_mem_t *mem) const;
48
Arthur Ishiguroe3ed3d22020-04-13 10:29:44 -070049 // Invoked when access to sensors for this connection has changed, e.g. lost or
50 // regained due to changes in the sensor restricted/privacy mode or the
51 // app changed to idle/active status.
52 void onSensorAccessChanged(bool hasAccess);
Anh Pham5198c992021-02-10 14:15:30 +010053 void onMicSensorAccessChanged(bool isMicToggleOn);
54 userid_t getUserId() const { return mUserId; }
Peng Xue36e3472016-11-03 11:57:10 -070055
56protected:
57 virtual ~SensorDirectConnection();
58 // ISensorEventConnection functions
59 virtual void onFirstRef();
60 virtual sp<BitTube> getSensorChannel() const;
61 virtual status_t enableDisable(int handle, bool enabled, nsecs_t samplingPeriodNs,
62 nsecs_t maxBatchReportLatencyNs, int reservedFlags);
63 virtual status_t setEventRate(int handle, nsecs_t samplingPeriodNs);
64 virtual status_t flush();
65 virtual int32_t configureChannel(int handle, int rateLevel);
Peng Xu8cbefd72017-07-10 16:41:08 -070066 virtual void destroy();
Peng Xue36e3472016-11-03 11:57:10 -070067private:
Arthur Ishiguroe3ed3d22020-04-13 10:29:44 -070068 bool hasSensorAccess() const;
69
70 // Stops all active sensor direct report requests.
71 //
72 // If backupRecord is true, stopped requests can be recovered
73 // by a subsequent recoverAll() call (e.g. when temporarily stopping
74 // sensors for sensor privacy/restrict mode or when an app becomes
75 // idle).
76 void stopAll(bool backupRecord = false);
77 // Same as stopAll() but with mConnectionLock held.
78 void stopAllLocked(bool backupRecord);
79
80 // Recover sensor requests previously stopped by stopAll(true).
81 // This method can be called when a sensor access resumes (e.g.
82 // sensor privacy/restrict mode lifted or app becomes active).
83 //
84 // If no requests are backed up by stopAll(), this method is no-op.
85 void recoverAll();
86
Anh Pham5198c992021-02-10 14:15:30 +010087 // Limits all active sensor direct report requests when the mic toggle is flipped to on.
88 void capRates();
89 // Recover sensor requests previously capped by capRates().
90 void uncapRates();
91
Peng Xue36e3472016-11-03 11:57:10 -070092 const sp<SensorService> mService;
93 const uid_t mUid;
94 const sensors_direct_mem_t mMem;
95 const int32_t mHalChannelHandle;
96 const String16 mOpPackageName;
97
98 mutable Mutex mConnectionLock;
99 std::unordered_map<int, int> mActivated;
100 std::unordered_map<int, int> mActivatedBackup;
Anh Pham5198c992021-02-10 14:15:30 +0100101 std::unordered_map<int, int> mMicRateBackup;
Peng Xu8cbefd72017-07-10 16:41:08 -0700102
Anh Phamaf91a912021-02-10 14:10:53 +0100103 std::atomic_bool mIsRateCappedBasedOnPermission;
Peng Xu8cbefd72017-07-10 16:41:08 -0700104 mutable Mutex mDestroyLock;
105 bool mDestroyed;
Anh Pham5198c992021-02-10 14:15:30 +0100106 userid_t mUserId;
Peng Xue36e3472016-11-03 11:57:10 -0700107};
108
109} // namepsace android
110
111#endif // ANDROID_SENSOR_DIRECT_CONNECTION_H
112