summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBruce Beare <bruce.j.beare@intel.com>2015-12-03 15:01:41 -0800
committerBruce Beare <bruce.j.beare@intel.com>2015-12-07 17:24:50 -0800
commiteb7a12e8a7d774024db21b5aca2efe21c3b6ba35 (patch)
treec5ab5b9ebb5751c82b6f926850924ad838aaf1f6
parent105e14acdd63e7d14bb8cfc4c15923c0b42b0b87 (diff)
downloadcommon-eb7a12e8a7d774024db21b5aca2efe21c3b6ba35.tar.gz
Sensor HAL test needs to find first equipped accelerometer
The sensor HAL test won't find the accelerometer if the HAL supports several... and the equipped one isn't listed in the HAL first. BUG=TBS Change-Id: I96b36c02f49ecd3779fd0b3d869febc8a9067630 Tracked-on: https://jira01.devtools.intel.com/browse/BP-154 Signed-off-by: Bruce Beare <bruce.j.beare@intel.com> Reviewed-on: https://android.intel.com/444226 Reviewed-by: Musca, Constantin <constantin.musca@intel.com> Tested-by: Musca, Constantin <constantin.musca@intel.com> Reviewed-by: Serban, Mihai <mihai.serban@intel.com>
-rw-r--r--sensors_example/hal-example-app.cpp49
1 files changed, 28 insertions, 21 deletions
diff --git a/sensors_example/hal-example-app.cpp b/sensors_example/hal-example-app.cpp
index 428636e..2a7cf83 100644
--- a/sensors_example/hal-example-app.cpp
+++ b/sensors_example/hal-example-app.cpp
@@ -39,42 +39,49 @@ int main(int argc, char* argv[]) {
sensor_t const* sensor_list = nullptr;
- int sensor_count = sensor_module->get_sensors_list(sensor_module,
- &sensor_list);
- printf("Found %d sensors\n", sensor_count);
-
- int accelerometer_index = -1;
+ int sensor_count = sensor_module->get_sensors_list(sensor_module, &sensor_list);
+ printf("Found %d supported sensors\n", sensor_count);
for (int i = 0; i < sensor_count; i++) {
- printf("Found %s\n", sensor_list[i].name);
- if (sensor_list[i].type == SENSOR_TYPE_ACCELEROMETER) {
- accelerometer_index = i;
- }
+ printf("HAL supports sensor %s\n", sensor_list[i].name);
}
- if (accelerometer_index == -1) {
- fprintf(stderr, "No accelerometer found\n");
- return 1;
- }
-
- // sensors_poll_device_1_t is used in HAL versions >= 1.0.
- sensors_poll_device_1_t* sensor_device = nullptr;
// sensors_open_1 is used in HAL versions >= 1.0.
+ sensors_poll_device_1_t* sensor_device = nullptr;
ret = sensors_open_1(&sensor_module->common, &sensor_device);
if (ret || !sensor_device) {
- fprintf(stderr, "Failed to open the accelerometer device\n");
+ fprintf(stderr, "Failed to open the sensor HAL\n");
return 1;
}
- ret = sensor_device->activate(
+ // Find the first accellerometer that can be opened
+ int accelerometer_index = -1;
+ for (int i = 0; i < sensor_count; i++) {
+ if (sensor_list[i].type != SENSOR_TYPE_ACCELEROMETER)
+ continue;
+ ret = sensor_device->activate(
reinterpret_cast<sensors_poll_device_t*>(sensor_device),
- sensor_list[accelerometer_index].handle, 1 /* enabled */);
+ sensor_list[i].handle, 1 /* enabled */);
+ if (ret) {
+ fprintf(stderr, "Unable to activate %s\n", sensor_list[i].name);
+ continue;
+ }
+
+ // found an equipped accelerometer.
+ accelerometer_index = i;
+ break;
+ }
+
if (ret) {
- fprintf(stderr, "Failed to enable the accelerometer\n");
- sensors_close_1(sensor_device);
+ fprintf(stderr, "No accelerometer found\n");
+ ret = sensors_close_1(sensor_device);
+ if (ret)
+ fprintf(stderr, "Failed to close the accelerometer device\n");
return 1;
}
+ printf("\nSensor %s activated\n", sensor_list[accelerometer_index].name);
+
const int kNumSamples = 10;
const int kNumEvents = 1;
const int kWaitTimeSecs = 1;