1 /*
2  * Copyright (C) 2015 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 SENSORS_H_
18 
19 #define SENSORS_H_
20 
21 #include <hardware/hardware.h>
22 #include <hardware/sensors.h>
23 #include <media/stagefright/foundation/ABase.h>
24 #include <utils/RefBase.h>
25 
26 #include <memory>
27 #include <unordered_set>
28 #include <vector>
29 
30 using android::sp;
31 
32 namespace android {
33     struct HubConnection;
34 } // namespace android
35 using android::HubConnection;
36 
37 namespace android {
38     namespace SensorHalExt {
39         class BaseSensorObject;
40         class DynamicSensorManager;
41         class SensorEventCallback;
42     } // namespace BaseSensorObject
43 } // namespace android
44 
45 using android::SensorHalExt::BaseSensorObject;
46 using android::SensorHalExt::DynamicSensorManager;
47 using android::SensorHalExt::SensorEventCallback;
48 
49 struct SensorContext {
50     struct sensors_poll_device_1 device;
51 
52     explicit SensorContext(const struct hw_module_t *module);
53 
54     bool getHubAlive();
55 
56     size_t getSensorList(sensor_t const **list);
57 
58 private:
59 
60     int close();
61     int activate(int handle, int enabled);
62     int setDelay(int handle, int64_t delayNs);
63     int poll(sensors_event_t *data, int count);
64 
65     int batch(int handle, int64_t sampling_period_ns,
66               int64_t max_report_latency_ns);
67 
68     int flush(int handle);
69 
70     int register_direct_channel(
71             const struct sensors_direct_mem_t* mem, int channel_handle);
72 
73     int config_direct_report(
74             int sensor_handle, int channel_handle, const struct sensors_direct_cfg_t * config);
75 
76     int inject_sensor_data(const struct sensors_event_t *event);
77 
78     void initializeHalExtension();
79 
80     // static wrappers
81     static int CloseWrapper(struct hw_device_t *dev);
82 
83     static int ActivateWrapper(
84             struct sensors_poll_device_t *dev, int handle, int enabled);
85 
86     static int SetDelayWrapper(
87             struct sensors_poll_device_t *dev, int handle, int64_t delayNs);
88 
89     static int PollWrapper(
90             struct sensors_poll_device_t *dev, sensors_event_t *data, int count);
91 
92     static int BatchWrapper(
93             struct sensors_poll_device_1 *dev,
94             int handle,
95             int flags,
96             int64_t sampling_period_ns,
97             int64_t max_report_latency_ns);
98 
99     static int FlushWrapper(struct sensors_poll_device_1 *dev, int handle);
100 
101     static int RegisterDirectChannelWrapper(struct sensors_poll_device_1 *dev,
102             const struct sensors_direct_mem_t* mem, int channel_handle);
103     static int ConfigDirectReportWrapper(struct sensors_poll_device_1 *dev,
104             int sensor_handle, int channel_handle, const struct sensors_direct_cfg_t * config);
105     static int InjectSensorDataWrapper(struct sensors_poll_device_1 *dev, const sensors_event_t *event);
106 
107     class SensorOperation {
108     public:
109         virtual bool owns(int handle) = 0;
110         virtual int activate(int handle, int enabled) = 0;
111         virtual int setDelay(int handle, int64_t delayNs) = 0;
112         virtual int batch(
113                 int handle, int64_t sampling_period_ns,
114                 int64_t max_report_latency_ns) = 0;
115         virtual int flush(int handle) = 0;
~SensorOperationSensorContext116         virtual ~SensorOperation() {}
117     };
118 
119     class HubConnectionOperation : public SensorOperation {
120     public:
121         HubConnectionOperation(sp<HubConnection> hubConnection);
122         virtual bool owns(int handle) override;
123         virtual int activate(int handle, int enabled) override;
124         virtual int setDelay(int handle, int64_t delayNs) override;
125         virtual int batch(
126                 int handle, int64_t sampling_period_ns,
127                 int64_t max_report_latency_ns) override;
128         virtual int flush(int handle) override;
~HubConnectionOperationSensorContext129         virtual ~HubConnectionOperation() {}
130     private:
131         sp<HubConnection> mHubConnection;
132         std::unordered_set<int> mHandles;
133     };
134 
135     std::vector<sensor_t> mSensorList;
136 
137     sp<HubConnection> mHubConnection;
138     std::vector<std::unique_ptr<SensorOperation> > mOperationHandler;
139 
140 #ifdef DYNAMIC_SENSOR_EXT_ENABLED
141 private:
142     class DynamicSensorManagerOperation : public SensorOperation {
143     public:
144         DynamicSensorManagerOperation(DynamicSensorManager* manager);
145         virtual bool owns(int handle) override;
146         virtual int activate(int handle, int enabled) override;
147         virtual int setDelay(int handle, int64_t delayNs) override;
148         virtual int batch(
149                 int handle, int64_t sampling_period_ns,
150                 int64_t max_report_latency_ns) override;
151         virtual int flush(int handle) override;
~DynamicSensorManagerOperationSensorContext152         virtual ~DynamicSensorManagerOperation() {}
153     private:
154         std::unique_ptr<DynamicSensorManager> mDynamicSensorManager;
155     };
156 
157     static constexpr int32_t kDynamicHandleBase = 0x10000;
158     static constexpr int32_t kMaxDynamicHandleCount = 0xF0000; // ~1M handles, enough before reboot
159 
160     std::unique_ptr<SensorEventCallback> mEventCallback;
161 #endif //DYNAMIC_SENSOR_EXT_ENABLED
162 
163     DISALLOW_EVIL_CONSTRUCTORS(SensorContext);
164 };
165 
166 #endif  // SENSORS_H_
167