1 /*
2 * Copyright (C) 2017 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
18 #include "DynamicSensorManager.h"
19 #include "sensors.h"
20
21 #include <cutils/properties.h>
22 #include <media/stagefright/foundation/ADebug.h>
23 #include <utils/Log.h>
24
25 #include <errno.h>
26 #include <string.h>
27 using namespace android;
28
29 ////////////////////////////////////////////////////////////////////////////////
30
SensorContext(const struct hw_module_t * module)31 SensorContext::SensorContext(const struct hw_module_t *module) {
32 memset(&device, 0, sizeof(device));
33
34 device.common.tag = HARDWARE_DEVICE_TAG;
35 device.common.version = SENSORS_DEVICE_API_VERSION_1_3;
36 device.common.module = const_cast<hw_module_t *>(module);
37 device.common.close = CloseWrapper;
38 device.activate = ActivateWrapper;
39 device.setDelay = SetDelayWrapper;
40 device.poll = PollWrapper;
41 device.batch = BatchWrapper;
42 device.flush = FlushWrapper;
43
44 // initialize dynamic sensor manager
45 int32_t base = property_get_int32("sensor.dynamic_sensor_hal.handle_base", kDynamicHandleBase);
46 int32_t count =
47 property_get_int32("sensor.dynamic_sensor_hal.handle_count", kMaxDynamicHandleCount);
48 mDynamicSensorManager.reset(DynamicSensorManager::createInstance(base, count, nullptr));
49 }
50
close()51 int SensorContext::close() {
52 delete this;
53 return 0;
54 }
55
activate(int handle,int enabled)56 int SensorContext::activate(int handle, int enabled) {
57 return mDynamicSensorManager->activate(handle, enabled);
58 }
59
setDelay(int handle,int64_t delayNs)60 int SensorContext::setDelay(int handle, int64_t delayNs) {
61 return mDynamicSensorManager->setDelay(handle, delayNs);
62 }
63
poll(sensors_event_t * data,int count)64 int SensorContext::poll(sensors_event_t *data, int count) {
65 return mDynamicSensorManager->poll(data, count);
66 }
67
batch(int handle,int64_t sampling_period_ns,int64_t max_report_latency_ns)68 int SensorContext::batch(
69 int handle,
70 int64_t sampling_period_ns,
71 int64_t max_report_latency_ns) {
72 return mDynamicSensorManager->batch(handle, sampling_period_ns, max_report_latency_ns);
73 }
74
flush(int handle)75 int SensorContext::flush(int handle) {
76 return mDynamicSensorManager->flush(handle);
77 }
78
79 // static
CloseWrapper(struct hw_device_t * dev)80 int SensorContext::CloseWrapper(struct hw_device_t *dev) {
81 return reinterpret_cast<SensorContext *>(dev)->close();
82 }
83
84 // static
ActivateWrapper(struct sensors_poll_device_t * dev,int handle,int enabled)85 int SensorContext::ActivateWrapper(
86 struct sensors_poll_device_t *dev, int handle, int enabled) {
87 return reinterpret_cast<SensorContext *>(dev)->activate(handle, enabled);
88 }
89
90 // static
SetDelayWrapper(struct sensors_poll_device_t * dev,int handle,int64_t delayNs)91 int SensorContext::SetDelayWrapper(
92 struct sensors_poll_device_t *dev, int handle, int64_t delayNs) {
93 return reinterpret_cast<SensorContext *>(dev)->setDelay(handle, delayNs);
94 }
95
96 // static
PollWrapper(struct sensors_poll_device_t * dev,sensors_event_t * data,int count)97 int SensorContext::PollWrapper(
98 struct sensors_poll_device_t *dev, sensors_event_t *data, int count) {
99 return reinterpret_cast<SensorContext *>(dev)->poll(data, count);
100 }
101
102 // static
BatchWrapper(struct sensors_poll_device_1 * dev,int handle,int flags,int64_t sampling_period_ns,int64_t max_report_latency_ns)103 int SensorContext::BatchWrapper(
104 struct sensors_poll_device_1 *dev,
105 int handle,
106 int flags,
107 int64_t sampling_period_ns,
108 int64_t max_report_latency_ns) {
109 (void) flags;
110 return reinterpret_cast<SensorContext *>(dev)->batch(
111 handle, sampling_period_ns, max_report_latency_ns);
112 }
113
114 // static
FlushWrapper(struct sensors_poll_device_1 * dev,int handle)115 int SensorContext::FlushWrapper(struct sensors_poll_device_1 *dev, int handle) {
116 return reinterpret_cast<SensorContext *>(dev)->flush(handle);
117 }
118
getSensorList(sensor_t const ** list)119 size_t SensorContext::getSensorList(sensor_t const **list) {
120 *list = &(mDynamicSensorManager->getDynamicMetaSensor());
121 return 1;
122 }
123
124 ////////////////////////////////////////////////////////////////////////////////
125
126 static sensor_t const *sensor_list;
127
open_sensors(const struct hw_module_t * module,const char *,struct hw_device_t ** dev)128 static int open_sensors(
129 const struct hw_module_t *module,
130 const char *,
131 struct hw_device_t **dev) {
132 SensorContext *ctx = new SensorContext(module);
133 ctx->getSensorList(&sensor_list);
134 *dev = &ctx->device.common;
135 return 0;
136 }
137
138 static struct hw_module_methods_t sensors_module_methods = {
139 .open = open_sensors
140 };
141
get_sensors_list(struct sensors_module_t *,struct sensor_t const ** list)142 static int get_sensors_list(
143 struct sensors_module_t *,
144 struct sensor_t const **list) {
145 *list = sensor_list;
146 return 1;
147 }
148
set_operation_mode(unsigned int mode)149 static int set_operation_mode(unsigned int mode) {
150 return (mode) ? -EINVAL : 0;
151 }
152
153 struct sensors_module_t HAL_MODULE_INFO_SYM = {
154 .common = {
155 .tag = HARDWARE_MODULE_TAG,
156 .version_major = 1,
157 .version_minor = 0,
158 .id = SENSORS_HARDWARE_MODULE_ID,
159 .name = "Google Dynamic Sensor Manager",
160 .author = "Google",
161 .methods = &sensors_module_methods,
162 .dso = NULL,
163 .reserved = {0},
164 },
165 .get_sensors_list = get_sensors_list,
166 .set_operation_mode = set_operation_mode,
167 };
168