OSDN Git Service

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