| Peng Xu | e36e347 | 2016-11-03 11:57:10 -0700 | [diff] [blame] | 1 | /* | 
|  | 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 Ishiguro | 8a62852 | 2021-09-22 14:10:16 -0700 | [diff] [blame] | 20 | #include <optional> | 
| Peng Xu | e36e347 | 2016-11-03 11:57:10 -0700 | [diff] [blame] | 21 | #include <stdint.h> | 
|  | 22 | #include <sys/types.h> | 
|  | 23 |  | 
|  | 24 | #include <binder/BinderService.h> | 
|  | 25 |  | 
| Mathias Agopian | 801ea09 | 2017-03-06 15:05:04 -0800 | [diff] [blame] | 26 | #include <sensor/Sensor.h> | 
|  | 27 | #include <sensor/BitTube.h> | 
|  | 28 | #include <sensor/ISensorServer.h> | 
|  | 29 | #include <sensor/ISensorEventConnection.h> | 
| Peng Xu | e36e347 | 2016-11-03 11:57:10 -0700 | [diff] [blame] | 30 |  | 
|  | 31 | #include "SensorService.h" | 
|  | 32 |  | 
|  | 33 | namespace android { | 
|  | 34 |  | 
|  | 35 | class SensorService; | 
|  | 36 | class BitTube; | 
|  | 37 |  | 
|  | 38 | class SensorService::SensorDirectConnection: public BnSensorEventConnection { | 
|  | 39 | public: | 
|  | 40 | SensorDirectConnection(const sp<SensorService>& service, uid_t uid, | 
|  | 41 | const sensors_direct_mem_t *mem, int32_t halChannelHandle, | 
|  | 42 | const String16& opPackageName); | 
|  | 43 | void dump(String8& result) const; | 
| Mike Ma | 2474386 | 2020-01-29 00:36:55 -0800 | [diff] [blame] | 44 | void dump(util::ProtoOutputStream* proto) const; | 
| Peng Xu | e36e347 | 2016-11-03 11:57:10 -0700 | [diff] [blame] | 45 | uid_t getUid() const { return mUid; } | 
| Arthur Ishiguro | e3ed3d2 | 2020-04-13 10:29:44 -0700 | [diff] [blame] | 46 | const String16& getOpPackageName() const { return mOpPackageName; } | 
| Peng Xu | e36e347 | 2016-11-03 11:57:10 -0700 | [diff] [blame] | 47 | int32_t getHalChannelHandle() const; | 
|  | 48 | bool isEquivalent(const sensors_direct_mem_t *mem) const; | 
|  | 49 |  | 
| Arthur Ishiguro | e3ed3d2 | 2020-04-13 10:29:44 -0700 | [diff] [blame] | 50 | // 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 Pham | 5198c99 | 2021-02-10 14:15:30 +0100 | [diff] [blame] | 54 | void onMicSensorAccessChanged(bool isMicToggleOn); | 
|  | 55 | userid_t getUserId() const { return mUserId; } | 
| Peng Xu | e36e347 | 2016-11-03 11:57:10 -0700 | [diff] [blame] | 56 |  | 
|  | 57 | protected: | 
|  | 58 | virtual ~SensorDirectConnection(); | 
|  | 59 | // ISensorEventConnection functions | 
|  | 60 | virtual void onFirstRef(); | 
|  | 61 | virtual sp<BitTube> getSensorChannel() const; | 
|  | 62 | virtual status_t enableDisable(int handle, bool enabled, nsecs_t samplingPeriodNs, | 
|  | 63 | nsecs_t maxBatchReportLatencyNs, int reservedFlags); | 
|  | 64 | virtual status_t setEventRate(int handle, nsecs_t samplingPeriodNs); | 
|  | 65 | virtual status_t flush(); | 
|  | 66 | virtual int32_t configureChannel(int handle, int rateLevel); | 
| Peng Xu | 8cbefd7 | 2017-07-10 16:41:08 -0700 | [diff] [blame] | 67 | virtual void destroy(); | 
| Peng Xu | e36e347 | 2016-11-03 11:57:10 -0700 | [diff] [blame] | 68 | private: | 
| Arthur Ishiguro | e3ed3d2 | 2020-04-13 10:29:44 -0700 | [diff] [blame] | 69 | bool hasSensorAccess() const; | 
|  | 70 |  | 
|  | 71 | // Stops all active sensor direct report requests. | 
|  | 72 | // | 
|  | 73 | // If backupRecord is true, stopped requests can be recovered | 
|  | 74 | // by a subsequent recoverAll() call (e.g. when temporarily stopping | 
|  | 75 | // sensors for sensor privacy/restrict mode or when an app becomes | 
|  | 76 | // idle). | 
|  | 77 | void stopAll(bool backupRecord = false); | 
|  | 78 | // Same as stopAll() but with mConnectionLock held. | 
|  | 79 | void stopAllLocked(bool backupRecord); | 
|  | 80 |  | 
|  | 81 | // Recover sensor requests previously stopped by stopAll(true). | 
|  | 82 | // This method can be called when a sensor access resumes (e.g. | 
|  | 83 | // sensor privacy/restrict mode lifted or app becomes active). | 
|  | 84 | // | 
|  | 85 | // If no requests are backed up by stopAll(), this method is no-op. | 
|  | 86 | void recoverAll(); | 
|  | 87 |  | 
| Anh Pham | 5198c99 | 2021-02-10 14:15:30 +0100 | [diff] [blame] | 88 | // Limits all active sensor direct report requests when the mic toggle is flipped to on. | 
|  | 89 | void capRates(); | 
|  | 90 | // Recover sensor requests previously capped by capRates(). | 
|  | 91 | void uncapRates(); | 
|  | 92 |  | 
| Peng Xu | e36e347 | 2016-11-03 11:57:10 -0700 | [diff] [blame] | 93 | const sp<SensorService> mService; | 
|  | 94 | const uid_t mUid; | 
|  | 95 | const sensors_direct_mem_t mMem; | 
|  | 96 | const int32_t mHalChannelHandle; | 
|  | 97 | const String16 mOpPackageName; | 
|  | 98 |  | 
|  | 99 | mutable Mutex mConnectionLock; | 
|  | 100 | std::unordered_map<int, int> mActivated; | 
|  | 101 | std::unordered_map<int, int> mActivatedBackup; | 
| Anh Pham | 5198c99 | 2021-02-10 14:15:30 +0100 | [diff] [blame] | 102 | std::unordered_map<int, int> mMicRateBackup; | 
| Peng Xu | 8cbefd7 | 2017-07-10 16:41:08 -0700 | [diff] [blame] | 103 |  | 
|  | 104 | mutable Mutex mDestroyLock; | 
|  | 105 | bool mDestroyed; | 
| Anh Pham | 5198c99 | 2021-02-10 14:15:30 +0100 | [diff] [blame] | 106 | userid_t mUserId; | 
| Arthur Ishiguro | 8a62852 | 2021-09-22 14:10:16 -0700 | [diff] [blame] | 107 |  | 
|  | 108 | std::optional<bool> mIsRateCappedBasedOnPermission; | 
|  | 109 |  | 
|  | 110 | bool isRateCappedBasedOnPermission() { | 
|  | 111 | if (!mIsRateCappedBasedOnPermission.has_value()) { | 
|  | 112 | mIsRateCappedBasedOnPermission = | 
|  | 113 | mService->isRateCappedBasedOnPermission(mOpPackageName); | 
|  | 114 | } | 
|  | 115 | return mIsRateCappedBasedOnPermission.value(); | 
|  | 116 | } | 
| Peng Xu | e36e347 | 2016-11-03 11:57:10 -0700 | [diff] [blame] | 117 | }; | 
|  | 118 |  | 
|  | 119 | } // namepsace android | 
|  | 120 |  | 
|  | 121 | #endif // ANDROID_SENSOR_DIRECT_CONNECTION_H | 
|  | 122 |  |