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
getSensorName(int type)27 char const* getSensorName(int type) {
28 switch(type) {
29 case SENSOR_TYPE_ACCELEROMETER:
30 return "Acc";
31 case SENSOR_TYPE_MAGNETIC_FIELD:
32 return "Mag";
33 case SENSOR_TYPE_ORIENTATION:
34 return "Ori";
35 case SENSOR_TYPE_PROXIMITY:
36 return "Prx";
37 case SENSOR_TYPE_TEMPERATURE:
38 return "Tmp";
39 case SENSOR_TYPE_LIGHT:
40 return "Lux";
41 }
42 return "ukn";
43 }
44
main(int argc,char ** argv)45 int main(int argc, char** argv)
46 {
47 int err;
48 struct sensors_poll_device_t* device;
49 struct sensors_module_t* module;
50
51 err = hw_get_module(SENSORS_HARDWARE_MODULE_ID, (hw_module_t const**)&module);
52 if (err != 0) {
53 printf("hw_get_module() failed (%s)\n", strerror(-err));
54 return 0;
55 }
56
57 struct sensor_t const* list;
58 int count = module->get_sensors_list(module, &list);
59 for (int i=0 ; i<count ; i++) {
60 printf("%s\n"
61 "\tvendor: %s\n"
62 "\tversion: %d\n"
63 "\thandle: %d\n"
64 "\ttype: %d\n"
65 "\tmaxRange: %f\n"
66 "\tresolution: %f\n"
67 "\tpower: %f mA\n",
68 list[i].name,
69 list[i].vendor,
70 list[i].version,
71 list[i].handle,
72 list[i].type,
73 list[i].maxRange,
74 list[i].resolution,
75 list[i].power);
76 }
77
78 sensors_event_t buffer[16];
79
80 err = sensors_open(&module->common, &device);
81 if (err != 0) {
82 printf("sensors_open() failed (%s)\n", strerror(-err));
83 return 0;
84 }
85
86 for (int i=0 ; i<count ; i++) {
87 err = device->activate(device, list[i].handle, 0);
88 if (err != 0) {
89 printf("deactivate() for '%s'failed (%s)\n",
90 list[i].name, strerror(-err));
91 return 0;
92 }
93 }
94
95 for (int i=0 ; i<count ; i++) {
96 err = device->activate(device, list[i].handle, 1);
97 if (err != 0) {
98 printf("activate() for '%s'failed (%s)\n",
99 list[i].name, strerror(-err));
100 return 0;
101 }
102 device->setDelay(device, list[i].handle, 10000000);
103 }
104
105 do {
106 int n = device->poll(device, buffer, 16);
107 if (n < 0) {
108 printf("poll() failed (%s)\n", strerror(-err));
109 break;
110 }
111
112 printf("read %d events:\n", n);
113 for (int i=0 ; i<n ; i++) {
114 const sensors_event_t& data = buffer[i];
115
116 if (data.version != sizeof(sensors_event_t)) {
117 printf("incorrect event version (version=%d, expected=%d",
118 data.version, sizeof(sensors_event_t));
119 break;
120 }
121
122 switch(data.type) {
123 case SENSOR_TYPE_ACCELEROMETER:
124 printf("sensor=%s, time=%lld, value=<%5.1f,%5.1f,%5.1f>\n",
125 getSensorName(data.type),
126 data.timestamp,
127 data.acceleration.x,
128 data.acceleration.y,
129 data.acceleration.z);
130 break;
131 case SENSOR_TYPE_MAGNETIC_FIELD:
132 printf("sensor=%s, time=%lld, value=<%5.1f,%5.1f,%5.1f>\n",
133 getSensorName(data.type),
134 data.timestamp,
135 data.magnetic.x,
136 data.magnetic.y,
137 data.magnetic.z);
138 break;
139 case SENSOR_TYPE_ORIENTATION:
140 printf("sensor=%s, time=%lld, value=<%5.1f,%5.1f,%5.1f>\n",
141 getSensorName(data.type),
142 data.timestamp,
143 data.orientation.azimuth,
144 data.orientation.pitch,
145 data.orientation.roll);
146 break;
147 case SENSOR_TYPE_PROXIMITY:
148 printf("sensor=%s, time=%lld, value=%f\n",
149 getSensorName(data.type),
150 data.timestamp,
151 data.distance);
152 break;
153 case SENSOR_TYPE_TEMPERATURE:
154 printf("sensor=%s, time=%lld, value=%f\n",
155 getSensorName(data.type),
156 data.timestamp,
157 data.temperature);
158 break;
159 case SENSOR_TYPE_LIGHT:
160 printf("sensor=%s, time=%lld, value=%f\n",
161 getSensorName(data.type),
162 data.timestamp,
163 data.light);
164 break;
165 default:
166 printf("sensor=%d, time=%lld, value=<%f,%f,%f>\n",
167 data.type,
168 data.timestamp,
169 data.acceleration.x,
170 data.acceleration.y,
171 data.acceleration.z);
172 break;
173 }
174 }
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