OSDN Git Service

STPK-1429 Use properties values for user-visible sensor description
[android-x86/hardware-intel-libsensors.git] / description.c
1 /*
2  * Copyright (C) 2014 Intel Corporation.
3  */
4
5 #include <stdlib.h>
6 #include <utils/Log.h>
7 #include <cutils/properties.h>
8 #include <hardware/sensors.h>
9 #include "enumeration.h"
10
11 /*
12  * This information should be provided on a sensor basis through a configuration
13  * file, or we should build a catalog of known sensors.
14  */
15
16 #define IIO_SENSOR_HAL_VERSION  1
17
18
19 static int sensor_get_st_prop (int s, const char* sel, char val[MAX_NAME_SIZE])
20 {
21         char prop_name[PROP_NAME_MAX];
22         char prop_val[PROP_VALUE_MAX];
23         int i                   = sensor_info[s].catalog_index;
24         const char *prefix      = sensor_catalog[i].tag;
25
26         sprintf(prop_name, PROP_BASE, prefix, sel);
27
28         if (property_get(prop_name, prop_val, "")) {
29                 strncpy(val, prop_val, MAX_NAME_SIZE-1);
30                 val[MAX_NAME_SIZE-1] = '\0';
31                 return 0;
32         }
33
34         return -1;
35 }
36
37
38 static int sensor_get_fl_prop (int s, const char* sel, float* val)
39 {
40         char buf[MAX_NAME_SIZE];
41
42         if (sensor_get_st_prop(s, sel, buf))
43                 return -1;
44
45         *val = (float) strtod(buf, NULL);
46         return 0;
47 }
48
49
50 char* sensor_get_name (int s)
51 {
52         if (sensor_info[s].friendly_name[0] != '\0' ||
53                 !sensor_get_st_prop(s, "name", sensor_info[s].friendly_name))
54                         return sensor_info[s].friendly_name;
55
56         /* If we got a iio device name from sysfs, use it */
57         if (sensor_info[s].internal_name[0]) {
58                 snprintf(sensor_info[s].friendly_name, MAX_NAME_SIZE, "S%d-%s",
59                          s, sensor_info[s].internal_name);
60                 sensor_info[s].friendly_name[MAX_NAME_SIZE-1] = '\0';
61         } else {
62                 sprintf(sensor_info[s].friendly_name, "S%d", s);
63         }
64
65         return sensor_info[s].friendly_name;
66 }
67
68
69 char* sensor_get_vendor (int s)
70 {
71         if (sensor_info[s].vendor_name[0] ||
72                 !sensor_get_st_prop(s, "vendor", sensor_info[s].vendor_name))
73                         return sensor_info[s].vendor_name;
74
75         return "";
76 }
77
78
79 int sensor_get_version (int s)
80 {
81         return IIO_SENSOR_HAL_VERSION;
82 }
83
84
85 float sensor_get_max_range (int s)
86 {
87         int catalog_index;
88         int sensor_type;
89
90         if (sensor_info[s].max_range != 0.0 ||
91                 !sensor_get_fl_prop(s, "max_range", &sensor_info[s].max_range))
92                         return sensor_info[s].max_range;
93
94         /* Try returning a sensible value given the sensor type */
95
96         /* We should cap returned samples accordingly... */
97
98         catalog_index = sensor_info[s].catalog_index;
99         sensor_type = sensor_catalog[catalog_index].type;
100
101         switch (sensor_type) {
102                 case SENSOR_TYPE_ACCELEROMETER:         /* m/s^2        */
103                         return 50;
104
105                 case SENSOR_TYPE_MAGNETIC_FIELD:        /* micro-tesla  */
106                         return 500;
107
108                 case SENSOR_TYPE_ORIENTATION:           /* degrees      */
109                         return 360;
110
111                 case SENSOR_TYPE_GYROSCOPE:             /* radians/s    */
112                         return 10;
113
114                 case SENSOR_TYPE_LIGHT:                 /* SI lux units */
115                         return 50000;
116
117                 case SENSOR_TYPE_AMBIENT_TEMPERATURE:   /* °C          */
118                 case SENSOR_TYPE_TEMPERATURE:           /* °C          */
119                 case SENSOR_TYPE_PROXIMITY:             /* centimeters  */
120                 case SENSOR_TYPE_PRESSURE:              /* hecto-pascal */
121                 case SENSOR_TYPE_RELATIVE_HUMIDITY:     /* percent */
122                         return 100;
123
124                 default:
125                         return 0.0;
126                 }
127 }
128
129
130 float sensor_get_resolution (int s)
131 {
132         if (sensor_info[s].resolution != 0.0 ||
133                 !sensor_get_fl_prop(s, "resolution", &sensor_info[s].resolution))
134                         return sensor_info[s].resolution;
135
136         return 0;
137 }
138
139
140 float sensor_get_power (int s)
141 {
142         /* mA used while sensor is in use ; not sure about volts :) */
143         if (sensor_info[s].power != 0.0 ||
144                 !sensor_get_fl_prop(s, "power", &sensor_info[s].power))
145                         return sensor_info[s].power;
146
147         return 0;
148 }