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;
getUid()43     uid_t getUid() const { return mUid; }
44     int32_t getHalChannelHandle() const;
45     bool isEquivalent(const sensors_direct_mem_t *mem) const;
46 
47     // stop all active sensor report. if backupRecord is set to false,
48     // those report can be recovered by recoverAll
49     // called by SensorService when enter restricted mode
50     void stopAll(bool backupRecord = false);
51 
52     // recover sensor reports previously stopped by stopAll(true)
53     // called by SensorService when return to NORMAL mode.
54     void recoverAll();
55 
56 protected:
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);
66     virtual void destroy();
67 private:
68     const sp<SensorService> mService;
69     const uid_t mUid;
70     const sensors_direct_mem_t mMem;
71     const int32_t mHalChannelHandle;
72     const String16 mOpPackageName;
73 
74     mutable Mutex mConnectionLock;
75     std::unordered_map<int, int> mActivated;
76     std::unordered_map<int, int> mActivatedBackup;
77 
78     mutable Mutex mDestroyLock;
79     bool mDestroyed;
80 };
81 
82 } // namepsace android
83 
84 #endif // ANDROID_SENSOR_DIRECT_CONNECTION_H
85 
86