summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorYiming Chen <yimingc@google.com>2015-10-26 14:16:19 -0700
committerYiming Chen <yimingc@google.com>2015-10-26 14:16:19 -0700
commit4a81a9c30bdb731383c5b0e37b097c7a3ca1902b (patch)
tree9e99d32f6e1b46d1f25b9dc170a94099087456f0
parent61b8682df4b0fe6c6e925bdacc51d9c40639a7b7 (diff)
downloadcommon-4a81a9c30bdb731383c5b0e37b097c7a3ca1902b.tar.gz
Move sensors example code
Move sensors example code from device/generic/brillo/examples to product/google/common. BUG=25282533 Change-Id: I9e7ccb03ab6788bf82720cbd133f817cf817782b
-rw-r--r--sensors_example/Android.mk46
-rw-r--r--sensors_example/README26
-rw-r--r--sensors_example/example_sensors.cpp76
-rw-r--r--sensors_example/example_sensors.h54
-rw-r--r--sensors_example/hal-example-app.cpp114
-rw-r--r--sensors_example/ndk-example-app.cpp132
-rw-r--r--sensors_example/sensors_hal.cpp179
-rw-r--r--sensors_example/sensors_hal.h70
8 files changed, 697 insertions, 0 deletions
diff --git a/sensors_example/Android.mk b/sensors_example/Android.mk
new file mode 100644
index 0000000..3576f5a
--- /dev/null
+++ b/sensors_example/Android.mk
@@ -0,0 +1,46 @@
+#
+# Copyright (C) 2015 The Android Open Source Project
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+#
+
+LOCAL_PATH := $(call my-dir)
+
+# Example Sensors HAL implementation.
+include $(CLEAR_VARS)
+LOCAL_MODULE := sensors.example
+LOCAL_MODULE_RELATIVE_PATH := hw
+LOCAL_MODULE_TAGS := optional
+LOCAL_CFLAGS := -Wno-unused-parameter
+LOCAL_SRC_FILES := \
+ example_sensors.cpp \
+ sensors_hal.cpp
+include $(BUILD_SHARED_LIBRARY)
+
+# Example app that uses sensors HAL.
+include $(CLEAR_VARS)
+LOCAL_MODULE := sensors-hal-example-app
+LOCAL_MODULE_TAGS := optional
+LOCAL_SRC_FILES := hal-example-app.cpp
+LOCAL_CFLAGS := -Wno-unused-parameter
+LOCAL_SHARED_LIBRARIES := libhardware
+include $(BUILD_EXECUTABLE)
+
+# Example app that uses NDK sensors API.
+include $(CLEAR_VARS)
+LOCAL_MODULE := sensors-ndk-example-app
+LOCAL_MODULE_TAGS := optional
+LOCAL_SRC_FILES := ndk-example-app.cpp
+LOCAL_CFLAGS := -Wno-unused-parameter
+LOCAL_SHARED_LIBRARIES := libsensor
+include $(BUILD_EXECUTABLE)
diff --git a/sensors_example/README b/sensors_example/README
new file mode 100644
index 0000000..a8d2c61
--- /dev/null
+++ b/sensors_example/README
@@ -0,0 +1,26 @@
+This folder contains example code for writing native apps that use Brillo
+sensors. The sensors HAL implementation must exist on the device for the apps
+in this folder to run successfully.
+
+Example files:
+ hal-example-app.cpp:
+ An example app that uses the sensors HAL (defined in
+ hardware/libhardware/include/sensors.h) directly. In the app, the list of
+ sensors found on the device is printed. And if an accelerometer exists, the
+ app will read the data and print it out.
+
+ ndk-example-app.cpp:
+ An example app that uses the NDK sensors interface. In the app, the list of
+ sensors found on the device is printed. And then the app tries to read data
+ from accelerometer, proximity sensor, and significant motion sensor, which
+ has continuous, on-change, and one-shot reporting modes respectively.
+
+ sensors_hal.cpp
+ An example implementation of a sensors HAL. It implements a fake
+ accelerometer and custom sensor (defined in example_sensors.{h|cpp}). The
+ accelerometer always returns random data. The custom sensor returns the hour
+ for the local time.
+
+ To use this HAL implementation, push the binary file sensors.example into
+ /system/lib/hw/ folder and rename it to sensors.default (make sure there is
+ no other sensors.* file under that folder).
diff --git a/sensors_example/example_sensors.cpp b/sensors_example/example_sensors.cpp
new file mode 100644
index 0000000..22d5381
--- /dev/null
+++ b/sensors_example/example_sensors.cpp
@@ -0,0 +1,76 @@
+/*
+ * Copyright (C) 2015 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include "example_sensors.h"
+
+#include <stdlib.h>
+#include <time.h>
+
+#include <hardware/sensors.h>
+
+SensorBase::SensorBase() {}
+SensorBase::~SensorBase() {}
+
+int SensorBase::activate(int handle, int enabled) {
+ return 0;
+}
+
+int SensorBase::setDelay(int handle, int64_t ns) {
+ return 0;
+}
+
+int SensorBase::batch(int handle, int flags,
+ int64_t period_ns, int64_t timeout) {
+ return 0;
+}
+
+int SensorBase::flush(int handle) {
+ return 0;
+}
+
+
+Accelerometer::Accelerometer() {}
+Accelerometer::~Accelerometer() {}
+
+int Accelerometer::pollEvents(sensors_event_t* data, int count) {
+ // Returns fake random values.
+ data->acceleration.x = static_cast<float>(random() % kMaxRange);
+ data->acceleration.y = static_cast<float>(random() % kMaxRange);
+ data->acceleration.z = static_cast<float>(random() % kMaxRange);
+ return 1;
+}
+
+
+CustomSensor::CustomSensor() {}
+CustomSensor::~CustomSensor() {}
+
+int CustomSensor::pollEvents(sensors_event_t* data, int count) {
+ if (data) {
+ // For this custom sensor, we will return the hour of the local time.
+ time_t t = time(NULL);
+ struct tm* now = localtime(&t);
+ if (now) {
+ // Any field can be used to return data for a custom sensor.
+ // See definition of struct sensors_event_t in hardware/sensors.h for the
+ // available fields.
+ data->data[0] = now->tm_hour;
+ return 1;
+ }
+ }
+ // Note: poll() return value is the number of events being returned. We use
+ // -1 to signal an error.
+ return -1;
+}
diff --git a/sensors_example/example_sensors.h b/sensors_example/example_sensors.h
new file mode 100644
index 0000000..5e984ea
--- /dev/null
+++ b/sensors_example/example_sensors.h
@@ -0,0 +1,54 @@
+/*
+ * Copyright (C) 2015 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#ifndef SENSORS_EXAMPLE_SENSORS_H
+#define SENSORS_EXAMPLE_SENSORS_H
+
+#include <stdint.h>
+
+struct sensors_event_t;
+
+class SensorBase {
+ public:
+ SensorBase();
+ virtual ~SensorBase();
+
+ virtual int activate(int handle, int enabled);
+ virtual int setDelay(int handle, int64_t ns);
+ virtual int pollEvents(sensors_event_t* data, int count) = 0;
+ virtual int batch(int handle, int flags, int64_t period_ns, int64_t timeout);
+ virtual int flush(int handle);
+};
+
+class Accelerometer : public SensorBase {
+ public:
+ static const int kMaxRange = 1000;
+
+ Accelerometer();
+ ~Accelerometer() override;
+
+ int pollEvents(sensors_event_t* data, int count) override;
+};
+
+class CustomSensor : public SensorBase {
+ public:
+ CustomSensor();
+ ~CustomSensor() override;
+
+ int pollEvents(sensors_event_t* data, int count) override;
+};
+
+#endif // SENSORS_EXAMPLE_SENSORS_H
diff --git a/sensors_example/hal-example-app.cpp b/sensors_example/hal-example-app.cpp
new file mode 100644
index 0000000..428636e
--- /dev/null
+++ b/sensors_example/hal-example-app.cpp
@@ -0,0 +1,114 @@
+/*
+ * Copyright (C) 2015 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+// This file constains an example app that uses sensors HAL.
+
+#include <stdio.h>
+#include <string.h>
+#include <sys/types.h>
+#include <unistd.h>
+
+#include <hardware/hardware.h>
+#include <hardware/sensors.h>
+
+int main(int argc, char* argv[]) {
+ sensors_module_t* sensor_module = nullptr;
+
+ int ret = hw_get_module(
+ SENSORS_HARDWARE_MODULE_ID,
+ const_cast<hw_module_t const**>(
+ reinterpret_cast<hw_module_t**>(&sensor_module)));
+ if (ret || !sensor_module) {
+ fprintf(stderr, "Failed to load %s module: %s\n",
+ SENSORS_HARDWARE_MODULE_ID, strerror(-ret));
+ return 1;
+ }
+
+ 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;
+
+ 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;
+ }
+ }
+ 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.
+ ret = sensors_open_1(&sensor_module->common, &sensor_device);
+ if (ret || !sensor_device) {
+ fprintf(stderr, "Failed to open the accelerometer device\n");
+ return 1;
+ }
+
+ ret = sensor_device->activate(
+ reinterpret_cast<sensors_poll_device_t*>(sensor_device),
+ sensor_list[accelerometer_index].handle, 1 /* enabled */);
+ if (ret) {
+ fprintf(stderr, "Failed to enable the accelerometer\n");
+ sensors_close_1(sensor_device);
+ return 1;
+ }
+
+ const int kNumSamples = 10;
+ const int kNumEvents = 1;
+ const int kWaitTimeSecs = 1;
+
+ for (int i = 0; i < kNumSamples; i++) {
+ sensors_event_t data;
+ int event_count = sensor_device->poll(
+ reinterpret_cast<sensors_poll_device_t*>(sensor_device),
+ &data, kNumEvents);
+ if (!event_count) {
+ fprintf(stderr, "Failed to read data from the accelerometer\n");
+ break;
+ } else {
+ printf("Acceleration: x = %f, y = %f, z = %f\n",
+ data.acceleration.x, data.acceleration.y, data.acceleration.z);
+ }
+
+ sleep(kWaitTimeSecs);
+ }
+
+ ret = sensor_device->activate(
+ reinterpret_cast<sensors_poll_device_t*>(sensor_device),
+ sensor_list[accelerometer_index].handle, 0 /* disabled */);
+ if (ret) {
+ fprintf(stderr, "Failed to disable the accelerometer\n");
+ return 1;
+ }
+
+ // sensors_close_1 is used in HAL versions >= 1.0.
+ ret = sensors_close_1(sensor_device);
+ if (ret) {
+ fprintf(stderr, "Failed to close the accelerometer device\n");
+ return 1;
+ }
+
+ return 0;
+}
diff --git a/sensors_example/ndk-example-app.cpp b/sensors_example/ndk-example-app.cpp
new file mode 100644
index 0000000..78b13ad
--- /dev/null
+++ b/sensors_example/ndk-example-app.cpp
@@ -0,0 +1,132 @@
+/*
+ * Copyright (C) 2015 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+// This file contains an example app that uses sensors NDK API.
+
+#include <stdio.h>
+#include <string.h>
+#include <unistd.h>
+
+#include <map>
+#include <string>
+
+#include <android/looper.h>
+#include <android/sensor.h>
+#include <hardware/sensors.h>
+
+const char kPackageName[] = "ndk-example-app";
+const int kLooperId = 1;
+
+int main(int argc, char* argv[]) {
+ ASensorManager* sensor_manager =
+ ASensorManager_getInstanceForPackage(kPackageName);
+ if (!sensor_manager) {
+ fprintf(stderr, "Failed to get a sensor manager\n");
+ return 1;
+ }
+
+ ASensorList sensor_list;
+ int sensor_count = ASensorManager_getSensorList(sensor_manager, &sensor_list);
+ printf("Found %d sensors\n", sensor_count);
+ for (int i = 0; i < sensor_count; i++) {
+ printf("Found %s\n", ASensor_getName(sensor_list[i]));
+ }
+
+ ASensorEventQueue* queue = ASensorManager_createEventQueue(
+ sensor_manager,
+ ALooper_prepare(ALOOPER_PREPARE_ALLOW_NON_CALLBACKS),
+ kLooperId, NULL /* no callback */, NULL /* no data */);
+ if (!queue) {
+ fprintf(stderr, "Failed to create a sensor event queue\n");
+ return 1;
+ }
+
+ const std::map<int, std::string> kSensorSamples = {
+ /*
+ * Accelerometer:
+ * Reporting mode: continuous. Events are generated continuously.
+ */
+ { SENSOR_TYPE_ACCELEROMETER, "accelerometer" },
+
+ /*
+ * Proximity sensor:
+ * Reporting mode: on-change. Events are generated only when proximity
+ * value has changed.
+ */
+ { SENSOR_TYPE_PROXIMITY, "proximity sensor" },
+
+ /*
+ * Significant motion sensor:
+ * Reporting mode: one-shot. An event is generated when significant
+ * motion is detected. After that, the sensor will be disabled by
+ * itself.
+ */
+ { SENSOR_TYPE_SIGNIFICANT_MOTION, "significant motion sensor" },
+ };
+
+ const int kNumSamples = 10;
+ const int kNumEvents = 1;
+ const int kTimeoutMilliSecs = 1000;
+ const int kWaitTimeSecs = 1;
+
+ for (auto& sensor_type : kSensorSamples) {
+ const ASensor* sensor = ASensorManager_getDefaultSensor(sensor_manager,
+ sensor_type.first);
+ if (sensor && !ASensorEventQueue_enableSensor(queue, sensor)) {
+ for (int i = 0; i < kNumSamples; i++) {
+ int ident = ALooper_pollAll(kTimeoutMilliSecs,
+ NULL /* no output file descriptor */,
+ NULL /* no output event */,
+ NULL /* no output data */);
+ if (ident == kLooperId) {
+ ASensorEvent data;
+ if (ASensorEventQueue_getEvents(queue, &data, kNumEvents)) {
+ if (sensor_type.first == SENSOR_TYPE_ACCELEROMETER) {
+ printf("Acceleration: x = %f, y = %f, z = %f\n",
+ data.acceleration.x, data.acceleration.y,
+ data.acceleration.z);
+ } else if (sensor_type.first == SENSOR_TYPE_PROXIMITY) {
+ printf("Proximity distance: %f\n", data.distance);
+ } else if (sensor_type.first == SENSOR_TYPE_SIGNIFICANT_MOTION) {
+ if (data.data[0] == 1) {
+ printf("Significant motion detected\n");
+ break;
+ }
+ }
+ }
+ }
+ sleep(kWaitTimeSecs);
+ }
+
+ int ret = ASensorEventQueue_disableSensor(queue, sensor);
+ if (ret) {
+ fprintf(stderr, "Failed to disable %s: %s\n",
+ sensor_type.second.c_str(), strerror(ret));
+ }
+ } else {
+ fprintf(stderr, "No %s found or failed to enable it\n",
+ sensor_type.second.c_str());
+ }
+ }
+
+ int ret = ASensorManager_destroyEventQueue(sensor_manager, queue);
+ if (ret) {
+ fprintf(stderr, "Failed to destroy event queue: %s\n", strerror(ret));
+ return 1;
+ }
+
+ return 0;
+}
diff --git a/sensors_example/sensors_hal.cpp b/sensors_example/sensors_hal.cpp
new file mode 100644
index 0000000..ab0e6ad
--- /dev/null
+++ b/sensors_example/sensors_hal.cpp
@@ -0,0 +1,179 @@
+/*
+ * Copyright (C) 2015 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include "sensors_hal.h"
+
+#include <errno.h>
+#include <string.h>
+
+#include "example_sensors.h"
+
+SensorContext::SensorContext(const hw_module_t* module) {
+ memset(&device, 0, sizeof(device));
+
+ device.common.tag = HARDWARE_DEVICE_TAG;
+ device.common.version = SENSORS_DEVICE_API_VERSION_1_0;
+ device.common.module = const_cast<hw_module_t*>(module);
+ device.common.close = CloseWrapper;
+ device.activate = ActivateWrapper;
+ device.setDelay = SetDelayWrapper;
+ device.poll = PollEventsWrapper;
+ device.batch = BatchWrapper;
+ device.flush = FlushWrapper;
+
+ sensors[AvailableSensors::kAccelerometer] = new Accelerometer();
+ sensors[AvailableSensors::kCustom] = new CustomSensor();
+}
+
+SensorContext::~SensorContext() {
+ for (int i = 0; i < AvailableSensors::kNumSensors; i++) {
+ if (sensors[i]) delete sensors[i];
+ }
+}
+
+int SensorContext::activate(int handle, int enabled) {
+ return sensors[handle]->activate(handle, enabled);
+}
+
+int SensorContext::setDelay(int handle, int64_t ns) {
+ return sensors[handle]->setDelay(handle, ns);
+}
+
+int SensorContext::pollEvents(sensors_event_t* data, int count) {
+ return sensors[0]->pollEvents(data, count);
+}
+
+int SensorContext::batch(int handle, int flags,
+ int64_t period_ns, int64_t timeout) {
+ return sensors[handle]->batch(handle, flags, period_ns, timeout);
+}
+
+int SensorContext::flush(int handle) {
+ return sensors[handle]->flush(handle);
+}
+
+// static
+int SensorContext::CloseWrapper(hw_device_t* dev) {
+ SensorContext* sensor_context = reinterpret_cast<SensorContext*>(dev);
+ if (sensor_context) {
+ delete sensor_context;
+ }
+ return 0;
+}
+
+// static
+int SensorContext::ActivateWrapper(sensors_poll_device_t* dev,
+ int handle, int enabled) {
+ return reinterpret_cast<SensorContext*>(dev)->activate(handle, enabled);
+}
+
+// static
+int SensorContext::SetDelayWrapper(sensors_poll_device_t* dev,
+ int handle, int64_t ns) {
+ return reinterpret_cast<SensorContext*>(dev)->setDelay(handle, ns);
+}
+
+// static
+int SensorContext::PollEventsWrapper(sensors_poll_device_t* dev,
+ sensors_event_t* data, int count) {
+ return reinterpret_cast<SensorContext*>(dev)->pollEvents(data, count);
+}
+
+// static
+int SensorContext::BatchWrapper(sensors_poll_device_1_t* dev, int handle,
+ int flags, int64_t period_ns, int64_t timeout) {
+ return reinterpret_cast<SensorContext*>(dev)->batch(handle, flags, period_ns,
+ timeout);
+}
+
+// static
+int SensorContext::FlushWrapper(sensors_poll_device_1_t* dev,
+ int handle) {
+ return reinterpret_cast<SensorContext*>(dev)->flush(handle);
+}
+
+
+static int open_sensors(const struct hw_module_t* module,
+ const char* id, struct hw_device_t** device) {
+ SensorContext* ctx = new SensorContext(module);
+ *device = &ctx->device.common;
+
+ return 0;
+}
+
+static struct hw_module_methods_t sensors_module_methods = {
+ open: open_sensors,
+};
+
+static struct sensor_t kSensorList[] = {
+ { name: "Broken Accelerometer",
+ vendor: "Unknown",
+ version: 1,
+ handle: SensorContext::AvailableSensors::kAccelerometer,
+ type: SENSOR_TYPE_ACCELEROMETER,
+ maxRange: static_cast<float>(Accelerometer::kMaxRange),
+ resolution: 100.0f,
+ power: 0.0f,
+ minDelay: 10000,
+ fifoReservedEventCount: 0,
+ fifoMaxEventCount: 0,
+ SENSOR_STRING_TYPE_ACCELEROMETER,
+ requiredPermission: "",
+ maxDelay: 0,
+ flags: SENSOR_FLAG_CONTINUOUS_MODE,
+ reserved: {},
+ },
+ { name: "Custom Hour of Day Sensor",
+ vendor: "Unknown",
+ version: 1,
+ handle: SensorContext::AvailableSensors::kCustom,
+ type: SENSOR_TYPE_CUSTOM,
+ maxRange: 24.0f,
+ resolution: 1.0f,
+ power: 0.0f,
+ minDelay: 1,
+ fifoReservedEventCount: 0,
+ fifoMaxEventCount: 0,
+ SENSOR_STRING_TYPE_CUSTOM,
+ requiredPermission: "",
+ maxDelay: 0,
+ flags: SENSOR_FLAG_CONTINUOUS_MODE,
+ reserved: {},
+ },
+};
+
+static int get_sensors_list(struct sensors_module_t* module,
+ struct sensor_t const** list) {
+ if (!list) return 0;
+ *list = kSensorList;
+ return sizeof(kSensorList) / sizeof(kSensorList[0]);
+}
+
+struct sensors_module_t HAL_MODULE_INFO_SYM = {
+ common: {
+ tag: HARDWARE_MODULE_TAG,
+ version_major: 1,
+ version_minor: 0,
+ id: SENSORS_HARDWARE_MODULE_ID,
+ name: "Example Sensor Module",
+ author: "Google",
+ methods: &sensors_module_methods,
+ dso: NULL,
+ reserved: {0},
+ },
+ get_sensors_list: get_sensors_list,
+ set_operation_mode: NULL
+};
diff --git a/sensors_example/sensors_hal.h b/sensors_example/sensors_hal.h
new file mode 100644
index 0000000..e563d7e
--- /dev/null
+++ b/sensors_example/sensors_hal.h
@@ -0,0 +1,70 @@
+/*
+ * Copyright (C) 2015 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#ifndef SENSORS_SENSORS_HAL_H
+#define SENSORS_SENSORS_HAL_H
+
+/*
+ * SENSOR_TYPE_CUSTOM
+ * The custom sensor type must have a integer constant starting from 27.
+ * The string name of the custom type must also be defined.
+ */
+#define SENSOR_TYPE_CUSTOM (27)
+#define SENSOR_STRING_TYPE_CUSTOM "brillo.custom"
+
+#include <stdint.h>
+
+#include <hardware/sensors.h>
+
+#include "example_sensors.h"
+
+class SensorContext {
+ public:
+ sensors_poll_device_1_t device;
+
+ SensorContext(const hw_module_t* module);
+ ~SensorContext();
+
+ enum AvailableSensors {
+ kAccelerometer,
+ kCustom,
+ kNumSensors
+ };
+
+ private:
+ int activate(int handle, int enabled);
+ int setDelay(int handle, int64_t ns);
+ int pollEvents(sensors_event_t* data, int count);
+ int batch(int handle, int flags, int64_t period_ns, int64_t timeout);
+ int flush(int handle);
+
+ // The wrapper pass through to the specific instantiation of
+ // the SensorContext.
+ static int CloseWrapper(hw_device_t* dev);
+ static int ActivateWrapper(sensors_poll_device_t* dev, int handle,
+ int enabled);
+ static int SetDelayWrapper(sensors_poll_device_t* dev, int handle,
+ int64_t ns);
+ static int PollEventsWrapper(sensors_poll_device_t* dev,
+ sensors_event_t* data, int count);
+ static int BatchWrapper(sensors_poll_device_1_t* dev, int handle, int flags,
+ int64_t period_ns, int64_t timeout);
+ static int FlushWrapper(sensors_poll_device_1_t* dev, int handle);
+
+ SensorBase* sensors[AvailableSensors::kNumSensors];
+};
+
+#endif // SENSORS_SENSORS_HAL_H