blob: a063959d78357e0dc043b19d4f9b1253ac26ce14 [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>
Mark Salyzynd88dfe82017-04-11 08:56:09 -070020#include <stdio.h>
Elliott Hughes5f5c5462010-08-19 10:21:01 -070021#include <string.h>
Mathias Agopianb1e212e2010-07-08 16:44:54 -070022#include <sys/cdefs.h>
23#include <sys/types.h>
24
Mark Salyzynd88dfe82017-04-11 08:56:09 -070025#include <log/log.h>
26#include <utils/Timers.h>
Mathias Agopianb1e212e2010-07-08 16:44:54 -070027
28#include <hardware/sensors.h>
29
30char const* getSensorName(int type) {
31 switch(type) {
32 case SENSOR_TYPE_ACCELEROMETER:
33 return "Acc";
34 case SENSOR_TYPE_MAGNETIC_FIELD:
35 return "Mag";
36 case SENSOR_TYPE_ORIENTATION:
37 return "Ori";
Mathias Agopiancc3f6a32011-09-18 15:21:33 -070038 case SENSOR_TYPE_GYROSCOPE:
39 return "Gyr";
Mathias Agopianb1e212e2010-07-08 16:44:54 -070040 case SENSOR_TYPE_LIGHT:
41 return "Lux";
Mathias Agopiancc3f6a32011-09-18 15:21:33 -070042 case SENSOR_TYPE_PRESSURE:
43 return "Bar";
44 case SENSOR_TYPE_TEMPERATURE:
45 return "Tmp";
46 case SENSOR_TYPE_PROXIMITY:
47 return "Prx";
48 case SENSOR_TYPE_GRAVITY:
49 return "Grv";
50 case SENSOR_TYPE_LINEAR_ACCELERATION:
51 return "Lac";
52 case SENSOR_TYPE_ROTATION_VECTOR:
53 return "Rot";
54 case SENSOR_TYPE_RELATIVE_HUMIDITY:
55 return "Hum";
56 case SENSOR_TYPE_AMBIENT_TEMPERATURE:
57 return "Tam";
Mathias Agopianb1e212e2010-07-08 16:44:54 -070058 }
59 return "ukn";
60}
61
Andreas Gampe1aa58f92015-12-16 14:17:44 -080062int main(int /* argc */, char** /* argv */)
Mathias Agopianb1e212e2010-07-08 16:44:54 -070063{
64 int err;
65 struct sensors_poll_device_t* device;
66 struct sensors_module_t* module;
67
68 err = hw_get_module(SENSORS_HARDWARE_MODULE_ID, (hw_module_t const**)&module);
69 if (err != 0) {
70 printf("hw_get_module() failed (%s)\n", strerror(-err));
71 return 0;
72 }
73
Mathias Agopiancc3f6a32011-09-18 15:21:33 -070074 err = sensors_open(&module->common, &device);
75 if (err != 0) {
76 printf("sensors_open() failed (%s)\n", strerror(-err));
77 return 0;
78 }
79
Mathias Agopianb1e212e2010-07-08 16:44:54 -070080 struct sensor_t const* list;
81 int count = module->get_sensors_list(module, &list);
Mathias Agopiancc3f6a32011-09-18 15:21:33 -070082 printf("%d sensors found:\n", count);
Mathias Agopianb1e212e2010-07-08 16:44:54 -070083 for (int i=0 ; i<count ; i++) {
84 printf("%s\n"
85 "\tvendor: %s\n"
86 "\tversion: %d\n"
87 "\thandle: %d\n"
88 "\ttype: %d\n"
89 "\tmaxRange: %f\n"
90 "\tresolution: %f\n"
91 "\tpower: %f mA\n",
92 list[i].name,
93 list[i].vendor,
94 list[i].version,
95 list[i].handle,
96 list[i].type,
97 list[i].maxRange,
98 list[i].resolution,
99 list[i].power);
100 }
101
Mathias Agopiancc3f6a32011-09-18 15:21:33 -0700102 static const size_t numEvents = 16;
103 sensors_event_t buffer[numEvents];
Mathias Agopianb1e212e2010-07-08 16:44:54 -0700104
105 for (int i=0 ; i<count ; i++) {
Mathias Agopian1a2bf612010-07-20 18:01:19 -0700106 err = device->activate(device, list[i].handle, 0);
107 if (err != 0) {
108 printf("deactivate() for '%s'failed (%s)\n",
109 list[i].name, strerror(-err));
110 return 0;
111 }
112 }
113
114 for (int i=0 ; i<count ; i++) {
Mathias Agopianb1e212e2010-07-08 16:44:54 -0700115 err = device->activate(device, list[i].handle, 1);
116 if (err != 0) {
117 printf("activate() for '%s'failed (%s)\n",
118 list[i].name, strerror(-err));
119 return 0;
120 }
Mathias Agopiancc3f6a32011-09-18 15:21:33 -0700121 device->setDelay(device, list[i].handle, ms2ns(10));
Mathias Agopianb1e212e2010-07-08 16:44:54 -0700122 }
123
124 do {
Mathias Agopiancc3f6a32011-09-18 15:21:33 -0700125 int n = device->poll(device, buffer, numEvents);
Mathias Agopianb1e212e2010-07-08 16:44:54 -0700126 if (n < 0) {
127 printf("poll() failed (%s)\n", strerror(-err));
128 break;
129 }
130
131 printf("read %d events:\n", n);
132 for (int i=0 ; i<n ; i++) {
Mathias Agopiancdefccd2010-07-15 18:29:03 -0700133 const sensors_event_t& data = buffer[i];
134
135 if (data.version != sizeof(sensors_event_t)) {
Andreas Gampe1aa58f92015-12-16 14:17:44 -0800136 printf("incorrect event version (version=%d, expected=%zu",
Mathias Agopiancdefccd2010-07-15 18:29:03 -0700137 data.version, sizeof(sensors_event_t));
138 break;
139 }
140
Mathias Agopian1a2bf612010-07-20 18:01:19 -0700141 switch(data.type) {
Mathias Agopianb1e212e2010-07-08 16:44:54 -0700142 case SENSOR_TYPE_ACCELEROMETER:
Mathias Agopianb1e212e2010-07-08 16:44:54 -0700143 case SENSOR_TYPE_MAGNETIC_FIELD:
Mathias Agopianb1e212e2010-07-08 16:44:54 -0700144 case SENSOR_TYPE_ORIENTATION:
Mathias Agopiancc3f6a32011-09-18 15:21:33 -0700145 case SENSOR_TYPE_GYROSCOPE:
146 case SENSOR_TYPE_GRAVITY:
147 case SENSOR_TYPE_LINEAR_ACCELERATION:
148 case SENSOR_TYPE_ROTATION_VECTOR:
Andreas Gampe1aa58f92015-12-16 14:17:44 -0800149 printf("sensor=%s, time=%" PRId64 ", value=<%5.1f,%5.1f,%5.1f>\n",
Mathias Agopian1a2bf612010-07-20 18:01:19 -0700150 getSensorName(data.type),
Mathias Agopiancdefccd2010-07-15 18:29:03 -0700151 data.timestamp,
Mathias Agopiancc3f6a32011-09-18 15:21:33 -0700152 data.data[0],
153 data.data[1],
154 data.data[2]);
Mathias Agopianb1e212e2010-07-08 16:44:54 -0700155 break;
Mathias Agopiancc3f6a32011-09-18 15:21:33 -0700156
Mathias Agopianb1e212e2010-07-08 16:44:54 -0700157 case SENSOR_TYPE_LIGHT:
Mathias Agopiancc3f6a32011-09-18 15:21:33 -0700158 case SENSOR_TYPE_PRESSURE:
159 case SENSOR_TYPE_TEMPERATURE:
160 case SENSOR_TYPE_PROXIMITY:
161 case SENSOR_TYPE_RELATIVE_HUMIDITY:
162 case SENSOR_TYPE_AMBIENT_TEMPERATURE:
Andreas Gampe1aa58f92015-12-16 14:17:44 -0800163 printf("sensor=%s, time=%" PRId64 ", value=%f\n",
Mathias Agopian1a2bf612010-07-20 18:01:19 -0700164 getSensorName(data.type),
Mathias Agopiancdefccd2010-07-15 18:29:03 -0700165 data.timestamp,
Mathias Agopiancc3f6a32011-09-18 15:21:33 -0700166 data.data[0]);
Mathias Agopianb1e212e2010-07-08 16:44:54 -0700167 break;
Mathias Agopiancc3f6a32011-09-18 15:21:33 -0700168
Mathias Agopianb1e212e2010-07-08 16:44:54 -0700169 default:
Andreas Gampe1aa58f92015-12-16 14:17:44 -0800170 printf("sensor=%d, time=% " PRId64 ", value=<%f,%f,%f, ...>\n",
Mathias Agopian1a2bf612010-07-20 18:01:19 -0700171 data.type,
Mathias Agopiancdefccd2010-07-15 18:29:03 -0700172 data.timestamp,
Mathias Agopiancc3f6a32011-09-18 15:21:33 -0700173 data.data[0],
174 data.data[1],
175 data.data[2]);
Mathias Agopianb1e212e2010-07-08 16:44:54 -0700176 break;
177 }
178 }
Mathias Agopianb1e212e2010-07-08 16:44:54 -0700179 } while (1); // fix that
180
181
182 for (int i=0 ; i<count ; i++) {
183 err = device->activate(device, list[i].handle, 0);
184 if (err != 0) {
185 printf("deactivate() for '%s'failed (%s)\n",
186 list[i].name, strerror(-err));
187 return 0;
188 }
189 }
190
191 err = sensors_close(device);
192 if (err != 0) {
193 printf("sensors_close() failed (%s)\n", strerror(-err));
194 }
195 return 0;
196}