1 /*
2  * Copyright (C) 2020 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 #include "sensor_list.h"
18 
19 namespace goldfish {
20 using ahs::V2_1::SensorType;
21 using ahs::V1_0::SensorFlagBits;
22 
23 constexpr char kAospVendor[] = "The Android Open Source Project";
24 
25 const char* const kQemuSensorName[] = {
26     "acceleration",
27     "gyroscope",
28     "magnetic-field",
29     "orientation",
30     "temperature",
31     "proximity",
32     "light",
33     "pressure",
34     "humidity",
35     "magnetic-field-uncalibrated",
36     "gyroscope-uncalibrated",
37     "hinge-angle0",
38     "hinge-angle1",
39     "hinge-angle2",
40     "heart-rate",
41     "rgbc-light",
42     "wrist-tilt",
43     "acceleration-uncalibrated",
44 };
45 
46 const SensorInfo kAllSensors[] = {
47     {
48         .sensorHandle = kSensorHandleAccelerometer,
49         .name = "Goldfish 3-axis Accelerometer",
50         .vendor = kAospVendor,
51         .version = 1,
52         .type = SensorType::ACCELEROMETER,
53         .typeAsString = "android.sensor.accelerometer",
54         .maxRange = 39.3,
55         .resolution = 1.0 / 4032.0,
56         .power = 3.0,
57         .minDelay = 10000,
58         .fifoReservedEventCount = 0,
59         .fifoMaxEventCount = 0,
60         .requiredPermission = "",
61         .maxDelay = 500000,
62         .flags = SensorFlagBits::DATA_INJECTION |
63                  SensorFlagBits::ADDITIONAL_INFO |
64                  SensorFlagBits::CONTINUOUS_MODE
65     },
66     {
67         .sensorHandle = kSensorHandleGyroscope,
68         .name = "Goldfish 3-axis Gyroscope",
69         .vendor = kAospVendor,
70         .version = 1,
71         .type = SensorType::GYROSCOPE,
72         .typeAsString = "android.sensor.gyroscope",
73         .maxRange = 16.46,
74         .resolution = 1.0 / 1000.0,
75         .power = 3.0,
76         .minDelay = 10000,
77         .fifoReservedEventCount = 0,
78         .fifoMaxEventCount = 0,
79         .requiredPermission = "",
80         .maxDelay = 500000,
81         .flags = SensorFlagBits::DATA_INJECTION |
82                  SensorFlagBits::ADDITIONAL_INFO |
83                  SensorFlagBits::CONTINUOUS_MODE
84     },
85     {
86         .sensorHandle = kSensorHandleMagneticField,
87         .name = "Goldfish 3-axis Magnetic field sensor",
88         .vendor = kAospVendor,
89         .version = 1,
90         .type = SensorType::MAGNETIC_FIELD,
91         .typeAsString = "android.sensor.magnetic_field",
92         .maxRange = 2000.0,
93         .resolution = .5,
94         .power = 6.7,
95         .minDelay = 10000,
96         .fifoReservedEventCount = 0,
97         .fifoMaxEventCount = 0,
98         .requiredPermission = "",
99         .maxDelay = 500000,
100         .flags = SensorFlagBits::DATA_INJECTION |
101                  SensorFlagBits::CONTINUOUS_MODE
102     },
103     {
104         .sensorHandle = kSensorHandleOrientation,
105         .name = "Goldfish Orientation sensor",
106         .vendor = kAospVendor,
107         .version = 1,
108         .type = SensorType::ORIENTATION,
109         .typeAsString = "android.sensor.orientation",
110         .maxRange = 360.0,
111         .resolution = 1.0,
112         .power = 9.7,
113         .minDelay = 10000,
114         .fifoReservedEventCount = 0,
115         .fifoMaxEventCount = 0,
116         .requiredPermission = "",
117         .maxDelay = 500000,
118         .flags = SensorFlagBits::DATA_INJECTION |
119                  SensorFlagBits::CONTINUOUS_MODE
120     },
121     {
122         .sensorHandle = kSensorHandleAmbientTemperature,
123         .name = "Goldfish Ambient Temperature sensor",
124         .vendor = kAospVendor,
125         .version = 1,
126         .type = SensorType::AMBIENT_TEMPERATURE,
127         .typeAsString = "android.sensor.ambient_temperature",
128         .maxRange = 80.0,
129         .resolution = 1.0,
130         .power = 0.0,
131         .minDelay = 0,
132         .fifoReservedEventCount = 0,
133         .fifoMaxEventCount = 0,
134         .requiredPermission = "",
135         .maxDelay = 0,
136         .flags = SensorFlagBits::DATA_INJECTION |
137                  SensorFlagBits::ON_CHANGE_MODE
138     },
139     {
140         .sensorHandle = kSensorHandleProximity,
141         .name = "Goldfish Proximity sensor",
142         .vendor = kAospVendor,
143         .version = 1,
144         .type = SensorType::PROXIMITY,
145         .typeAsString = "android.sensor.proximity",
146         .maxRange = 1.0,
147         .resolution = 1.0,
148         .power = 20.0,
149         .minDelay = 0,
150         .fifoReservedEventCount = 0,
151         .fifoMaxEventCount = 0,
152         .requiredPermission = "",
153         .maxDelay = 0,
154         .flags = SensorFlagBits::DATA_INJECTION |
155                  SensorFlagBits::ON_CHANGE_MODE |
156                  SensorFlagBits::WAKE_UP
157     },
158     {
159         .sensorHandle = kSensorHandleLight,
160         .name = "Goldfish Light sensor",
161         .vendor = kAospVendor,
162         .version = 1,
163         .type = SensorType::LIGHT,
164         .typeAsString = "android.sensor.light",
165         .maxRange = 40000.0,
166         .resolution = 1.0,
167         .power = 20.0,
168         .minDelay = 0,
169         .fifoReservedEventCount = 0,
170         .fifoMaxEventCount = 0,
171         .requiredPermission = "",
172         .maxDelay = 0,
173         .flags = SensorFlagBits::DATA_INJECTION |
174                  SensorFlagBits::ON_CHANGE_MODE
175     },
176     {
177         .sensorHandle = kSensorHandlePressure,
178         .name = "Goldfish Pressure sensor",
179         .vendor = kAospVendor,
180         .version = 1,
181         .type = SensorType::PRESSURE,
182         .typeAsString = "android.sensor.pressure",
183         .maxRange = 800.0,
184         .resolution = 1.0,
185         .power = 20.0,
186         .minDelay = 10000,
187         .fifoReservedEventCount = 0,
188         .fifoMaxEventCount = 0,
189         .requiredPermission = "",
190         .maxDelay = 500000,
191         .flags = SensorFlagBits::DATA_INJECTION |
192                  SensorFlagBits::CONTINUOUS_MODE
193     },
194     {
195         .sensorHandle = kSensorHandleRelativeHumidity,
196         .name = "Goldfish Humidity sensor",
197         .vendor = kAospVendor,
198         .version = 1,
199         .type = SensorType::RELATIVE_HUMIDITY,
200         .typeAsString = "android.sensor.relative_humidity",
201         .maxRange = 100.0,
202         .resolution = 1.0,
203         .power = 20.0,
204         .minDelay = 0,
205         .fifoReservedEventCount = 0,
206         .fifoMaxEventCount = 0,
207         .requiredPermission = "",
208         .maxDelay = 0,
209         .flags = SensorFlagBits::DATA_INJECTION |
210                  SensorFlagBits::ON_CHANGE_MODE
211     },
212     {
213         .sensorHandle = kSensorHandleMagneticFieldUncalibrated,
214         .name = "Goldfish 3-axis Magnetic field sensor (uncalibrated)",
215         .vendor = kAospVendor,
216         .version = 1,
217         .type = SensorType::MAGNETIC_FIELD_UNCALIBRATED,
218         .typeAsString = "android.sensor.magnetic_field_uncalibrated",
219         .maxRange = 2000.0,
220         .resolution = 0.5,
221         .power = 6.7,
222         .minDelay = 10000,
223         .fifoReservedEventCount = 0,
224         .fifoMaxEventCount = 0,
225         .requiredPermission = "",
226         .maxDelay = 500000,
227         .flags = SensorFlagBits::DATA_INJECTION | 0
228     },
229     {
230         .sensorHandle = kSensorHandleGyroscopeFieldUncalibrated,
231         .name = "Goldfish 3-axis Gyroscope (uncalibrated)",
232         .vendor = kAospVendor,
233         .version = 1,
234         .type = SensorType::GYROSCOPE_UNCALIBRATED,
235         .typeAsString = "android.sensor.gyroscope_uncalibrated",
236         .maxRange = 16.46,
237         .resolution = 1.0 / 1000.0,
238         .power = 3.0,
239         .minDelay = 10000,
240         .fifoReservedEventCount = 0,
241         .fifoMaxEventCount = 0,
242         .requiredPermission = "",
243         .maxDelay = 500000,
244         .flags = SensorFlagBits::DATA_INJECTION |
245                  SensorFlagBits::CONTINUOUS_MODE
246     },
247     {
248         .sensorHandle = kSensorHandleHingeAngle0,
249         .name = "Goldfish hinge sensor0 (in degrees)",
250         .vendor = kAospVendor,
251         .version = 1,
252         .type = SensorType::HINGE_ANGLE,
253         .typeAsString = "android.sensor.hinge_angle",
254         .maxRange = 360,
255         .resolution = 1.0,
256         .power = 3.0,
257         .minDelay = 0,
258         .fifoReservedEventCount = 0,
259         .fifoMaxEventCount = 0,
260         .requiredPermission = "",
261         .maxDelay = 0,
262         .flags = SensorFlagBits::DATA_INJECTION |
263                  SensorFlagBits::ON_CHANGE_MODE |
264                  SensorFlagBits::WAKE_UP
265     },
266     {
267         .sensorHandle = kSensorHandleHingeAngle1,
268         .name = "Goldfish hinge sensor1 (in degrees)",
269         .vendor = kAospVendor,
270         .version = 1,
271         .type = SensorType::HINGE_ANGLE,
272         .typeAsString = "android.sensor.hinge_angle",
273         .maxRange = 360,
274         .resolution = 1.0,
275         .power = 3.0,
276         .minDelay = 0,
277         .fifoReservedEventCount = 0,
278         .fifoMaxEventCount = 0,
279         .requiredPermission = "",
280         .maxDelay = 0,
281         .flags = SensorFlagBits::DATA_INJECTION |
282                  SensorFlagBits::ON_CHANGE_MODE |
283                  SensorFlagBits::WAKE_UP
284     },
285     {
286         .sensorHandle = kSensorHandleHingeAngle2,
287         .name = "Goldfish hinge sensor2 (in degrees)",
288         .vendor = kAospVendor,
289         .version = 1,
290         .type = SensorType::HINGE_ANGLE,
291         .typeAsString = "android.sensor.hinge_angle",
292         .maxRange = 360,
293         .resolution = 1.0,
294         .power = 3.0,
295         .minDelay = 0,
296         .fifoReservedEventCount = 0,
297         .fifoMaxEventCount = 0,
298         .requiredPermission = "",
299         .maxDelay = 0,
300         .flags = SensorFlagBits::DATA_INJECTION |
301                  SensorFlagBits::ON_CHANGE_MODE |
302                  SensorFlagBits::WAKE_UP
303     },
304     {
305         .sensorHandle = kSensorHandleHeartRate,
306         .name = "Goldfish Heart rate sensor",
307         .vendor = kAospVendor,
308         .version = 1,
309         .type = SensorType::HEART_RATE,
310         .typeAsString = "android.sensor.heart_rate",
311         .maxRange = 500.0,
312         .resolution = 1.0,
313         .power = 20.0,
314         .minDelay = 0,
315         .fifoReservedEventCount = 0,
316         .fifoMaxEventCount = 0,
317         .requiredPermission = "android.permission.BODY_SENSORS",
318         .maxDelay = 500000,
319         .flags = SensorFlagBits::DATA_INJECTION |
320                  SensorFlagBits::ON_CHANGE_MODE
321     },
322     {
323       // rgbc-light
324     },
325     {
326         .sensorHandle = kSensorHandleWristTilt,
327         .name = "Goldfish wrist tilt gesture sensor",
328         .vendor = kAospVendor,
329         .version = 1,
330         .type = SensorType::WRIST_TILT_GESTURE,
331         .typeAsString = "android.sensor.wrist_tilt_gesture",
332         .maxRange = 1.0,
333         .resolution = 1.0,
334         .power = 20.0,
335         .minDelay = 0,
336         .fifoReservedEventCount = 0,
337         .fifoMaxEventCount = 0,
338         .maxDelay = 500000,
339         .flags = SensorFlagBits::DATA_INJECTION |
340                  SensorFlagBits::SPECIAL_REPORTING_MODE |
341                  SensorFlagBits::WAKE_UP
342     },
343     {
344         .sensorHandle = kSensorHandleAccelerometerUncalibrated,
345         .name = "Goldfish 3-axis Accelerometer Uncalibrated",
346         .vendor = kAospVendor,
347         .version = 1,
348         .type = SensorType::ACCELEROMETER_UNCALIBRATED,
349         .typeAsString = "android.sensor.accelerometer_uncalibrated",
350         .maxRange = 39.3,
351         .resolution = 1.0 / 4032.0,
352         .power = 3.0,
353         .minDelay = 10000,
354         .fifoReservedEventCount = 0,
355         .fifoMaxEventCount = 0,
356         .requiredPermission = "",
357         .maxDelay = 500000,
358         .flags = SensorFlagBits::DATA_INJECTION |
359                  SensorFlagBits::ADDITIONAL_INFO |
360                  SensorFlagBits::CONTINUOUS_MODE
361     }};
362 
363 constexpr int kSensorNumber = sizeof(kAllSensors) / sizeof(kAllSensors[0]);
364 
365 static_assert(kSensorNumber == sizeof(kQemuSensorName) / sizeof(kQemuSensorName[0]),
366               "sizes of kAllSensors and kQemuSensorName arrays must match");
367 
getSensorNumber()368 int getSensorNumber() { return kSensorNumber; }
369 
isSensorHandleValid(const int h)370 bool isSensorHandleValid(const int h) {
371     return h >= 0 && h < kSensorNumber;
372 }
373 
getSensorInfoByHandle(const int h)374 const SensorInfo* getSensorInfoByHandle(const int h) {
375     return isSensorHandleValid(h) ? &kAllSensors[h] : nullptr;
376 }
377 
getQemuSensorNameByHandle(const int h)378 const char* getQemuSensorNameByHandle(const int h) {
379     return kQemuSensorName[h];
380 }
381 
382 }  // namespace goldfish
383