1 /*
2 * Copyright (C) 2008 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 <string.h>
18 #include <stdint.h>
19 #include <string.h>
20 #include <sys/cdefs.h>
21 #include <sys/types.h>
22
23 #include <cutils/log.h>
24
25 #include <hardware/sensors.h>
26 #include <utils/Timers.h>
27
getSensorName(int type)28 char const* getSensorName(int type) {
29 switch(type) {
30 case SENSOR_TYPE_ACCELEROMETER:
31 return "Acc";
32 case SENSOR_TYPE_MAGNETIC_FIELD:
33 return "Mag";
34 case SENSOR_TYPE_ORIENTATION:
35 return "Ori";
36 case SENSOR_TYPE_GYROSCOPE:
37 return "Gyr";
38 case SENSOR_TYPE_LIGHT:
39 return "Lux";
40 case SENSOR_TYPE_PRESSURE:
41 return "Bar";
42 case SENSOR_TYPE_TEMPERATURE:
43 return "Tmp";
44 case SENSOR_TYPE_PROXIMITY:
45 return "Prx";
46 case SENSOR_TYPE_GRAVITY:
47 return "Grv";
48 case SENSOR_TYPE_LINEAR_ACCELERATION:
49 return "Lac";
50 case SENSOR_TYPE_ROTATION_VECTOR:
51 return "Rot";
52 case SENSOR_TYPE_RELATIVE_HUMIDITY:
53 return "Hum";
54 case SENSOR_TYPE_AMBIENT_TEMPERATURE:
55 return "Tam";
56 }
57 return "ukn";
58 }
59
main(int argc,char ** argv)60 int main(int argc, char** argv)
61 {
62 int err;
63 struct sensors_poll_device_t* device;
64 struct sensors_module_t* module;
65
66 err = hw_get_module(SENSORS_HARDWARE_MODULE_ID, (hw_module_t const**)&module);
67 if (err != 0) {
68 printf("hw_get_module() failed (%s)\n", strerror(-err));
69 return 0;
70 }
71
72 err = sensors_open(&module->common, &device);
73 if (err != 0) {
74 printf("sensors_open() failed (%s)\n", strerror(-err));
75 return 0;
76 }
77
78 struct sensor_t const* list;
79 int count = module->get_sensors_list(module, &list);
80 printf("%d sensors found:\n", count);
81 for (int i=0 ; i<count ; i++) {
82 printf("%s\n"
83 "\tvendor: %s\n"
84 "\tversion: %d\n"
85 "\thandle: %d\n"
86 "\ttype: %d\n"
87 "\tmaxRange: %f\n"
88 "\tresolution: %f\n"
89 "\tpower: %f mA\n",
90 list[i].name,
91 list[i].vendor,
92 list[i].version,
93 list[i].handle,
94 list[i].type,
95 list[i].maxRange,
96 list[i].resolution,
97 list[i].power);
98 }
99
100 static const size_t numEvents = 16;
101 sensors_event_t buffer[numEvents];
102
103 for (int i=0 ; i<count ; i++) {
104 err = device->activate(device, list[i].handle, 0);
105 if (err != 0) {
106 printf("deactivate() for '%s'failed (%s)\n",
107 list[i].name, strerror(-err));
108 return 0;
109 }
110 }
111
112 for (int i=0 ; i<count ; i++) {
113 err = device->activate(device, list[i].handle, 1);
114 if (err != 0) {
115 printf("activate() for '%s'failed (%s)\n",
116 list[i].name, strerror(-err));
117 return 0;
118 }
119 device->setDelay(device, list[i].handle, ms2ns(10));
120 }
121
122 do {
123 int n = device->poll(device, buffer, numEvents);
124 if (n < 0) {
125 printf("poll() failed (%s)\n", strerror(-err));
126 break;
127 }
128
129 printf("read %d events:\n", n);
130 for (int i=0 ; i<n ; i++) {
131 const sensors_event_t& data = buffer[i];
132
133 if (data.version != sizeof(sensors_event_t)) {
134 printf("incorrect event version (version=%d, expected=%d",
135 data.version, sizeof(sensors_event_t));
136 break;
137 }
138
139 switch(data.type) {
140 case SENSOR_TYPE_ACCELEROMETER:
141 case SENSOR_TYPE_MAGNETIC_FIELD:
142 case SENSOR_TYPE_ORIENTATION:
143 case SENSOR_TYPE_GYROSCOPE:
144 case SENSOR_TYPE_GRAVITY:
145 case SENSOR_TYPE_LINEAR_ACCELERATION:
146 case SENSOR_TYPE_ROTATION_VECTOR:
147 printf("sensor=%s, time=%lld, value=<%5.1f,%5.1f,%5.1f>\n",
148 getSensorName(data.type),
149 data.timestamp,
150 data.data[0],
151 data.data[1],
152 data.data[2]);
153 break;
154
155 case SENSOR_TYPE_LIGHT:
156 case SENSOR_TYPE_PRESSURE:
157 case SENSOR_TYPE_TEMPERATURE:
158 case SENSOR_TYPE_PROXIMITY:
159 case SENSOR_TYPE_RELATIVE_HUMIDITY:
160 case SENSOR_TYPE_AMBIENT_TEMPERATURE:
161 printf("sensor=%s, time=%lld, value=%f\n",
162 getSensorName(data.type),
163 data.timestamp,
164 data.data[0]);
165 break;
166
167 default:
168 printf("sensor=%d, time=%lld, value=<%f,%f,%f, ...>\n",
169 data.type,
170 data.timestamp,
171 data.data[0],
172 data.data[1],
173 data.data[2]);
174 break;
175 }
176 }
177 } while (1); // fix that
178
179
180 for (int i=0 ; i<count ; i++) {
181 err = device->activate(device, list[i].handle, 0);
182 if (err != 0) {
183 printf("deactivate() for '%s'failed (%s)\n",
184 list[i].name, strerror(-err));
185 return 0;
186 }
187 }
188
189 err = sensors_close(device);
190 if (err != 0) {
191 printf("sensors_close() failed (%s)\n", strerror(-err));
192 }
193 return 0;
194 }
195