Mathias Agopian | b1e212e | 2010-07-08 16:44:54 -0700 | [diff] [blame] | 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 | |
Andreas Gampe | 1aa58f9 | 2015-12-16 14:17:44 -0800 | [diff] [blame] | 17 | #include <inttypes.h> |
Brian Carlstrom | 03cb00b | 2010-08-24 17:57:25 -0700 | [diff] [blame] | 18 | #include <string.h> |
Mathias Agopian | b1e212e | 2010-07-08 16:44:54 -0700 | [diff] [blame] | 19 | #include <stdint.h> |
Elliott Hughes | 5f5c546 | 2010-08-19 10:21:01 -0700 | [diff] [blame] | 20 | #include <string.h> |
Mathias Agopian | b1e212e | 2010-07-08 16:44:54 -0700 | [diff] [blame] | 21 | #include <sys/cdefs.h> |
| 22 | #include <sys/types.h> |
| 23 | |
| 24 | #include <cutils/log.h> |
| 25 | |
| 26 | #include <hardware/sensors.h> |
Mathias Agopian | cc3f6a3 | 2011-09-18 15:21:33 -0700 | [diff] [blame] | 27 | #include <utils/Timers.h> |
Mathias Agopian | b1e212e | 2010-07-08 16:44:54 -0700 | [diff] [blame] | 28 | |
| 29 | char 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 Agopian | cc3f6a3 | 2011-09-18 15:21:33 -0700 | [diff] [blame] | 37 | case SENSOR_TYPE_GYROSCOPE: |
| 38 | return "Gyr"; |
Mathias Agopian | b1e212e | 2010-07-08 16:44:54 -0700 | [diff] [blame] | 39 | case SENSOR_TYPE_LIGHT: |
| 40 | return "Lux"; |
Mathias Agopian | cc3f6a3 | 2011-09-18 15:21:33 -0700 | [diff] [blame] | 41 | 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 Agopian | b1e212e | 2010-07-08 16:44:54 -0700 | [diff] [blame] | 57 | } |
| 58 | return "ukn"; |
| 59 | } |
| 60 | |
Andreas Gampe | 1aa58f9 | 2015-12-16 14:17:44 -0800 | [diff] [blame] | 61 | int main(int /* argc */, char** /* argv */) |
Mathias Agopian | b1e212e | 2010-07-08 16:44:54 -0700 | [diff] [blame] | 62 | { |
| 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 Agopian | cc3f6a3 | 2011-09-18 15:21:33 -0700 | [diff] [blame] | 73 | 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 Agopian | b1e212e | 2010-07-08 16:44:54 -0700 | [diff] [blame] | 79 | struct sensor_t const* list; |
| 80 | int count = module->get_sensors_list(module, &list); |
Mathias Agopian | cc3f6a3 | 2011-09-18 15:21:33 -0700 | [diff] [blame] | 81 | printf("%d sensors found:\n", count); |
Mathias Agopian | b1e212e | 2010-07-08 16:44:54 -0700 | [diff] [blame] | 82 | 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 Agopian | cc3f6a3 | 2011-09-18 15:21:33 -0700 | [diff] [blame] | 101 | static const size_t numEvents = 16; |
| 102 | sensors_event_t buffer[numEvents]; |
Mathias Agopian | b1e212e | 2010-07-08 16:44:54 -0700 | [diff] [blame] | 103 | |
| 104 | for (int i=0 ; i<count ; i++) { |
Mathias Agopian | 1a2bf61 | 2010-07-20 18:01:19 -0700 | [diff] [blame] | 105 | 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 Agopian | b1e212e | 2010-07-08 16:44:54 -0700 | [diff] [blame] | 114 | 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 Agopian | cc3f6a3 | 2011-09-18 15:21:33 -0700 | [diff] [blame] | 120 | device->setDelay(device, list[i].handle, ms2ns(10)); |
Mathias Agopian | b1e212e | 2010-07-08 16:44:54 -0700 | [diff] [blame] | 121 | } |
| 122 | |
| 123 | do { |
Mathias Agopian | cc3f6a3 | 2011-09-18 15:21:33 -0700 | [diff] [blame] | 124 | int n = device->poll(device, buffer, numEvents); |
Mathias Agopian | b1e212e | 2010-07-08 16:44:54 -0700 | [diff] [blame] | 125 | 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 Agopian | cdefccd | 2010-07-15 18:29:03 -0700 | [diff] [blame] | 132 | const sensors_event_t& data = buffer[i]; |
| 133 | |
| 134 | if (data.version != sizeof(sensors_event_t)) { |
Andreas Gampe | 1aa58f9 | 2015-12-16 14:17:44 -0800 | [diff] [blame] | 135 | printf("incorrect event version (version=%d, expected=%zu", |
Mathias Agopian | cdefccd | 2010-07-15 18:29:03 -0700 | [diff] [blame] | 136 | data.version, sizeof(sensors_event_t)); |
| 137 | break; |
| 138 | } |
| 139 | |
Mathias Agopian | 1a2bf61 | 2010-07-20 18:01:19 -0700 | [diff] [blame] | 140 | switch(data.type) { |
Mathias Agopian | b1e212e | 2010-07-08 16:44:54 -0700 | [diff] [blame] | 141 | case SENSOR_TYPE_ACCELEROMETER: |
Mathias Agopian | b1e212e | 2010-07-08 16:44:54 -0700 | [diff] [blame] | 142 | case SENSOR_TYPE_MAGNETIC_FIELD: |
Mathias Agopian | b1e212e | 2010-07-08 16:44:54 -0700 | [diff] [blame] | 143 | case SENSOR_TYPE_ORIENTATION: |
Mathias Agopian | cc3f6a3 | 2011-09-18 15:21:33 -0700 | [diff] [blame] | 144 | case SENSOR_TYPE_GYROSCOPE: |
| 145 | case SENSOR_TYPE_GRAVITY: |
| 146 | case SENSOR_TYPE_LINEAR_ACCELERATION: |
| 147 | case SENSOR_TYPE_ROTATION_VECTOR: |
Andreas Gampe | 1aa58f9 | 2015-12-16 14:17:44 -0800 | [diff] [blame] | 148 | printf("sensor=%s, time=%" PRId64 ", value=<%5.1f,%5.1f,%5.1f>\n", |
Mathias Agopian | 1a2bf61 | 2010-07-20 18:01:19 -0700 | [diff] [blame] | 149 | getSensorName(data.type), |
Mathias Agopian | cdefccd | 2010-07-15 18:29:03 -0700 | [diff] [blame] | 150 | data.timestamp, |
Mathias Agopian | cc3f6a3 | 2011-09-18 15:21:33 -0700 | [diff] [blame] | 151 | data.data[0], |
| 152 | data.data[1], |
| 153 | data.data[2]); |
Mathias Agopian | b1e212e | 2010-07-08 16:44:54 -0700 | [diff] [blame] | 154 | break; |
Mathias Agopian | cc3f6a3 | 2011-09-18 15:21:33 -0700 | [diff] [blame] | 155 | |
Mathias Agopian | b1e212e | 2010-07-08 16:44:54 -0700 | [diff] [blame] | 156 | case SENSOR_TYPE_LIGHT: |
Mathias Agopian | cc3f6a3 | 2011-09-18 15:21:33 -0700 | [diff] [blame] | 157 | 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 Gampe | 1aa58f9 | 2015-12-16 14:17:44 -0800 | [diff] [blame] | 162 | printf("sensor=%s, time=%" PRId64 ", value=%f\n", |
Mathias Agopian | 1a2bf61 | 2010-07-20 18:01:19 -0700 | [diff] [blame] | 163 | getSensorName(data.type), |
Mathias Agopian | cdefccd | 2010-07-15 18:29:03 -0700 | [diff] [blame] | 164 | data.timestamp, |
Mathias Agopian | cc3f6a3 | 2011-09-18 15:21:33 -0700 | [diff] [blame] | 165 | data.data[0]); |
Mathias Agopian | b1e212e | 2010-07-08 16:44:54 -0700 | [diff] [blame] | 166 | break; |
Mathias Agopian | cc3f6a3 | 2011-09-18 15:21:33 -0700 | [diff] [blame] | 167 | |
Mathias Agopian | b1e212e | 2010-07-08 16:44:54 -0700 | [diff] [blame] | 168 | default: |
Andreas Gampe | 1aa58f9 | 2015-12-16 14:17:44 -0800 | [diff] [blame] | 169 | printf("sensor=%d, time=% " PRId64 ", value=<%f,%f,%f, ...>\n", |
Mathias Agopian | 1a2bf61 | 2010-07-20 18:01:19 -0700 | [diff] [blame] | 170 | data.type, |
Mathias Agopian | cdefccd | 2010-07-15 18:29:03 -0700 | [diff] [blame] | 171 | data.timestamp, |
Mathias Agopian | cc3f6a3 | 2011-09-18 15:21:33 -0700 | [diff] [blame] | 172 | data.data[0], |
| 173 | data.data[1], |
| 174 | data.data[2]); |
Mathias Agopian | b1e212e | 2010-07-08 16:44:54 -0700 | [diff] [blame] | 175 | break; |
| 176 | } |
| 177 | } |
Mathias Agopian | b1e212e | 2010-07-08 16:44:54 -0700 | [diff] [blame] | 178 | } 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 | } |