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 
20 #include <stdint.h>
21 #include <sys/types.h>
22 
23 #include <binder/BinderService.h>
24 
25 #include <sensor/Sensor.h>
26 #include <sensor/BitTube.h>
27 #include <sensor/ISensorServer.h>
28 #include <sensor/ISensorEventConnection.h>
29 
30 #include "SensorService.h"
31 
32 namespace android {
33 
34 class SensorService;
35 class BitTube;
36 
37 class SensorService::SensorDirectConnection: public BnSensorEventConnection {
38 public:
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;
43     void dump(util::ProtoOutputStream* proto) const;
44     uid_t getUid() const { return mUid; }
45     const String16& getOpPackageName() const { return mOpPackageName; }
46     int32_t getHalChannelHandle() const;
47     bool isEquivalent(const sensors_direct_mem_t *mem) const;
48 
49     // 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);
53 
54 protected:
55     virtual ~SensorDirectConnection();
56     // ISensorEventConnection functions
57     virtual void onFirstRef();
58     virtual sp<BitTube> getSensorChannel() const;
59     virtual status_t enableDisable(int handle, bool enabled, nsecs_t samplingPeriodNs,
60                                    nsecs_t maxBatchReportLatencyNs, int reservedFlags);
61     virtual status_t setEventRate(int handle, nsecs_t samplingPeriodNs);
62     virtual status_t flush();
63     virtual int32_t configureChannel(int handle, int rateLevel);
64     virtual void destroy();
65 private:
66     bool hasSensorAccess() const;
67 
68     // Stops all active sensor direct report requests.
69     //
70     // If backupRecord is true, stopped requests can be recovered
71     // by a subsequent recoverAll() call (e.g. when temporarily stopping
72     // sensors for sensor privacy/restrict mode or when an app becomes
73     // idle).
74     void stopAll(bool backupRecord = false);
75     // Same as stopAll() but with mConnectionLock held.
76     void stopAllLocked(bool backupRecord);
77 
78     // Recover sensor requests previously stopped by stopAll(true).
79     // This method can be called when a sensor access resumes (e.g.
80     // sensor privacy/restrict mode lifted or app becomes active).
81     //
82     // If no requests are backed up by stopAll(), this method is no-op.
83     void recoverAll();
84 
85     const sp<SensorService> mService;
86     const uid_t mUid;
87     const sensors_direct_mem_t mMem;
88     const int32_t mHalChannelHandle;
89     const String16 mOpPackageName;
90 
91     mutable Mutex mConnectionLock;
92     std::unordered_map<int, int> mActivated;
93     std::unordered_map<int, int> mActivatedBackup;
94 
95     mutable Mutex mDestroyLock;
96     bool mDestroyed;
97 };
98 
99 } // namepsace android
100 
101 #endif // ANDROID_SENSOR_DIRECT_CONNECTION_H
102 
103