blob: 55b77850d772943b7357a62011bb1e1dcda9fa16 [file] [log] [blame]
Mathias Agopianb1e212e2010-07-08 16:44:54 -07001/*
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
Andreas Gampe1aa58f92015-12-16 14:17:44 -080017#include <inttypes.h>
Brian Carlstrom03cb00b2010-08-24 17:57:25 -070018#include <string.h>
Mathias Agopianb1e212e2010-07-08 16:44:54 -070019#include <stdint.h>
Elliott Hughes5f5c5462010-08-19 10:21:01 -070020#include <string.h>
Mathias Agopianb1e212e2010-07-08 16:44:54 -070021#include <sys/cdefs.h>
22#include <sys/types.h>
23
24#include <cutils/log.h>
25
26#include <hardware/sensors.h>
Mathias Agopiancc3f6a32011-09-18 15:21:33 -070027#include <utils/Timers.h>
Mathias Agopianb1e212e2010-07-08 16:44:54 -070028
29char const* getSensorName(int type) {
30 switch(type) {
31 case SENSOR_TYPE_ACCELEROMETER:
32 return "Acc";
33 case SENSOR_TYPE_MAGNETIC_FIELD:
34 return "Mag";
35 case SENSOR_TYPE_ORIENTATION:
36 return "Ori";
Mathias Agopiancc3f6a32011-09-18 15:21:33 -070037 case SENSOR_TYPE_GYROSCOPE:
38 return "Gyr";
Mathias Agopianb1e212e2010-07-08 16:44:54 -070039 case SENSOR_TYPE_LIGHT:
40 return "Lux";
Mathias Agopiancc3f6a32011-09-18 15:21:33 -070041 case SENSOR_TYPE_PRESSURE:
42 return "Bar";
43 case SENSOR_TYPE_TEMPERATURE:
44 return "Tmp";
45 case SENSOR_TYPE_PROXIMITY:
46 return "Prx";
47 case SENSOR_TYPE_GRAVITY:
48 return "Grv";
49 case SENSOR_TYPE_LINEAR_ACCELERATION:
50 return "Lac";
51 case SENSOR_TYPE_ROTATION_VECTOR:
52 return "Rot";
53 case SENSOR_TYPE_RELATIVE_HUMIDITY:
54 return "Hum";
55 case SENSOR_TYPE_AMBIENT_TEMPERATURE:
56 return "Tam";
Mathias Agopianb1e212e2010-07-08 16:44:54 -070057 }
58 return "ukn";
59}
60
Andreas Gampe1aa58f92015-12-16 14:17:44 -080061int main(int /* argc */, char** /* argv */)
Mathias Agopianb1e212e2010-07-08 16:44:54 -070062{
63 int err;
64 struct sensors_poll_device_t* device;
65 struct sensors_module_t* module;
66
67 err = hw_get_module(SENSORS_HARDWARE_MODULE_ID, (hw_module_t const**)&module);
68 if (err != 0) {
69 printf("hw_get_module() failed (%s)\n", strerror(-err));
70 return 0;
71 }
72
Mathias Agopiancc3f6a32011-09-18 15:21:33 -070073 err = sensors_open(&module->common, &device);
74 if (err != 0) {
75 printf("sensors_open() failed (%s)\n", strerror(-err));
76 return 0;
77 }
78
Mathias Agopianb1e212e2010-07-08 16:44:54 -070079 struct sensor_t const* list;
80 int count = module->get_sensors_list(module, &list);
Mathias Agopiancc3f6a32011-09-18 15:21:33 -070081 printf("%d sensors found:\n", count);
Mathias Agopianb1e212e2010-07-08 16:44:54 -070082 for (int i=0 ; i<count ; i++) {
83 printf("%s\n"
84 "\tvendor: %s\n"
85 "\tversion: %d\n"
86 "\thandle: %d\n"
87 "\ttype: %d\n"
88 "\tmaxRange: %f\n"
89 "\tresolution: %f\n"
90 "\tpower: %f mA\n",
91 list[i].name,
92 list[i].vendor,
93 list[i].version,
94 list[i].handle,
95 list[i].type,
96 list[i].maxRange,
97 list[i].resolution,
98 list[i].power);
99 }
100
Mathias Agopiancc3f6a32011-09-18 15:21:33 -0700101 static const size_t numEvents = 16;
102 sensors_event_t buffer[numEvents];
Mathias Agopianb1e212e2010-07-08 16:44:54 -0700103
104 for (int i=0 ; i<count ; i++) {
Mathias Agopian1a2bf612010-07-20 18:01:19 -0700105 err = device->activate(device, list[i].handle, 0);
106 if (err != 0) {
107 printf("deactivate() for '%s'failed (%s)\n",
108 list[i].name, strerror(-err));
109 return 0;
110 }
111 }
112
113 for (int i=0 ; i<count ; i++) {
Mathias Agopianb1e212e2010-07-08 16:44:54 -0700114 err = device->activate(device, list[i].handle, 1);
115 if (err != 0) {
116 printf("activate() for '%s'failed (%s)\n",
117 list[i].name, strerror(-err));
118 return 0;
119 }
Mathias Agopiancc3f6a32011-09-18 15:21:33 -0700120 device->setDelay(device, list[i].handle, ms2ns(10));
Mathias Agopianb1e212e2010-07-08 16:44:54 -0700121 }
122
123 do {
Mathias Agopiancc3f6a32011-09-18 15:21:33 -0700124 int n = device->poll(device, buffer, numEvents);
Mathias Agopianb1e212e2010-07-08 16:44:54 -0700125 if (n < 0) {
126 printf("poll() failed (%s)\n", strerror(-err));
127 break;
128 }
129
130 printf("read %d events:\n", n);
131 for (int i=0 ; i<n ; i++) {
Mathias Agopiancdefccd2010-07-15 18:29:03 -0700132 const sensors_event_t& data = buffer[i];
133
134 if (data.version != sizeof(sensors_event_t)) {
Andreas Gampe1aa58f92015-12-16 14:17:44 -0800135 printf("incorrect event version (version=%d, expected=%zu",
Mathias Agopiancdefccd2010-07-15 18:29:03 -0700136 data.version, sizeof(sensors_event_t));
137 break;
138 }
139
Mathias Agopian1a2bf612010-07-20 18:01:19 -0700140 switch(data.type) {
Mathias Agopianb1e212e2010-07-08 16:44:54 -0700141 case SENSOR_TYPE_ACCELEROMETER:
Mathias Agopianb1e212e2010-07-08 16:44:54 -0700142 case SENSOR_TYPE_MAGNETIC_FIELD:
Mathias Agopianb1e212e2010-07-08 16:44:54 -0700143 case SENSOR_TYPE_ORIENTATION:
Mathias Agopiancc3f6a32011-09-18 15:21:33 -0700144 case SENSOR_TYPE_GYROSCOPE:
145 case SENSOR_TYPE_GRAVITY:
146 case SENSOR_TYPE_LINEAR_ACCELERATION:
147 case SENSOR_TYPE_ROTATION_VECTOR:
Andreas Gampe1aa58f92015-12-16 14:17:44 -0800148 printf("sensor=%s, time=%" PRId64 ", value=<%5.1f,%5.1f,%5.1f>\n",
Mathias Agopian1a2bf612010-07-20 18:01:19 -0700149 getSensorName(data.type),
Mathias Agopiancdefccd2010-07-15 18:29:03 -0700150 data.timestamp,
Mathias Agopiancc3f6a32011-09-18 15:21:33 -0700151 data.data[0],
152 data.data[1],
153 data.data[2]);
Mathias Agopianb1e212e2010-07-08 16:44:54 -0700154 break;
Mathias Agopiancc3f6a32011-09-18 15:21:33 -0700155
Mathias Agopianb1e212e2010-07-08 16:44:54 -0700156 case SENSOR_TYPE_LIGHT:
Mathias Agopiancc3f6a32011-09-18 15:21:33 -0700157 case SENSOR_TYPE_PRESSURE:
158 case SENSOR_TYPE_TEMPERATURE:
159 case SENSOR_TYPE_PROXIMITY:
160 case SENSOR_TYPE_RELATIVE_HUMIDITY:
161 case SENSOR_TYPE_AMBIENT_TEMPERATURE:
Andreas Gampe1aa58f92015-12-16 14:17:44 -0800162 printf("sensor=%s, time=%" PRId64 ", value=%f\n",
Mathias Agopian1a2bf612010-07-20 18:01:19 -0700163 getSensorName(data.type),
Mathias Agopiancdefccd2010-07-15 18:29:03 -0700164 data.timestamp,
Mathias Agopiancc3f6a32011-09-18 15:21:33 -0700165 data.data[0]);
Mathias Agopianb1e212e2010-07-08 16:44:54 -0700166 break;
Mathias Agopiancc3f6a32011-09-18 15:21:33 -0700167
Mathias Agopianb1e212e2010-07-08 16:44:54 -0700168 default:
Andreas Gampe1aa58f92015-12-16 14:17:44 -0800169 printf("sensor=%d, time=% " PRId64 ", value=<%f,%f,%f, ...>\n",
Mathias Agopian1a2bf612010-07-20 18:01:19 -0700170 data.type,
Mathias Agopiancdefccd2010-07-15 18:29:03 -0700171 data.timestamp,
Mathias Agopiancc3f6a32011-09-18 15:21:33 -0700172 data.data[0],
173 data.data[1],
174 data.data[2]);
Mathias Agopianb1e212e2010-07-08 16:44:54 -0700175 break;
176 }
177 }
Mathias Agopianb1e212e2010-07-08 16:44:54 -0700178 } while (1); // fix that
179
180
181 for (int i=0 ; i<count ; i++) {
182 err = device->activate(device, list[i].handle, 0);
183 if (err != 0) {
184 printf("deactivate() for '%s'failed (%s)\n",
185 list[i].name, strerror(-err));
186 return 0;
187 }
188 }
189
190 err = sensors_close(device);
191 if (err != 0) {
192 printf("sensors_close() failed (%s)\n", strerror(-err));
193 }
194 return 0;
195}