Implement activate and batch functions

Implements the activate and batch functions for the default Sensors
2.0 implementation.

Bug: 111070257
Test: Builds
Change-Id: I5987ab722cdd97c7cd7ff466d6d989794171b851
diff --git a/sensors/2.0/default/Android.bp b/sensors/2.0/default/Android.bp
index 3bc64e5..11612d3 100644
--- a/sensors/2.0/default/Android.bp
+++ b/sensors/2.0/default/Android.bp
@@ -20,6 +20,7 @@
     relative_install_path: "hw",
     srcs: [
         "service.cpp",
+        "Sensor.cpp",
         "Sensors.cpp",
     ],
     init_rc: ["android.hardware.sensors@2.0-service.rc"],
diff --git a/sensors/2.0/default/Sensor.cpp b/sensors/2.0/default/Sensor.cpp
new file mode 100644
index 0000000..f4c9b57
--- /dev/null
+++ b/sensors/2.0/default/Sensor.cpp
@@ -0,0 +1,50 @@
+/*
+ * Copyright (C) 2018 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 "Sensor.h"
+
+namespace android {
+namespace hardware {
+namespace sensors {
+namespace V2_0 {
+namespace implementation {
+
+Sensor::Sensor() : mIsEnabled(false), mSamplingPeriodNs(0) {}
+
+const SensorInfo& Sensor::getSensorInfo() const {
+    return mSensorInfo;
+}
+
+bool Sensor::batch(int32_t samplingPeriodNs) {
+    bool success = true;
+    if (samplingPeriodNs >= mSensorInfo.minDelay * 1000 &&
+        samplingPeriodNs <= mSensorInfo.maxDelay * 1000) {
+        mSamplingPeriodNs = samplingPeriodNs;
+    } else {
+        success = false;
+    }
+    return success;
+}
+
+void Sensor::activate(bool enable) {
+    mIsEnabled = enable;
+}
+
+}  // namespace implementation
+}  // namespace V2_0
+}  // namespace sensors
+}  // namespace hardware
+}  // namespace android
\ No newline at end of file
diff --git a/sensors/2.0/default/Sensor.h b/sensors/2.0/default/Sensor.h
new file mode 100644
index 0000000..46026a4
--- /dev/null
+++ b/sensors/2.0/default/Sensor.h
@@ -0,0 +1,50 @@
+/*
+ * Copyright (C) 2018 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 ANDROID_HARDWARE_SENSORS_V2_0_SENSOR_H
+#define ANDROID_HARDWARE_SENSORS_V2_0_SENSOR_H
+
+#include <android/hardware/sensors/1.0/types.h>
+
+using ::android::hardware::sensors::V1_0::SensorInfo;
+
+namespace android {
+namespace hardware {
+namespace sensors {
+namespace V2_0 {
+namespace implementation {
+
+class Sensor {
+   public:
+    Sensor();
+
+    const SensorInfo& getSensorInfo() const;
+    bool batch(int32_t samplingPeriodNs);
+    void activate(bool enable);
+
+   protected:
+    bool mIsEnabled;
+    int64_t mSamplingPeriodNs;
+    SensorInfo mSensorInfo;
+};
+
+}  // namespace implementation
+}  // namespace V2_0
+}  // namespace sensors
+}  // namespace hardware
+}  // namespace android
+
+#endif  // ANDROID_HARDWARE_SENSORS_V2_0_SENSOR_H
\ No newline at end of file
diff --git a/sensors/2.0/default/Sensors.cpp b/sensors/2.0/default/Sensors.cpp
index 9fea647..29721fa 100644
--- a/sensors/2.0/default/Sensors.cpp
+++ b/sensors/2.0/default/Sensors.cpp
@@ -16,6 +16,7 @@
 
 #include "Sensors.h"
 
+#include <android/hardware/sensors/2.0/types.h>
 #include <log/log.h>
 
 namespace android {
@@ -37,8 +38,15 @@
 }
 
 // Methods from ::android::hardware::sensors::V2_0::ISensors follow.
-Return<void> Sensors::getSensorsList(getSensorsList_cb /* _hidl_cb */) {
-    // TODO implement
+Return<void> Sensors::getSensorsList(getSensorsList_cb _hidl_cb) {
+    std::vector<SensorInfo> sensors;
+    for (const auto& sensor : mSensors) {
+        sensors.push_back(sensor.second->getSensorInfo());
+    }
+
+    // Call the HIDL callback with the SensorInfo
+    _hidl_cb(sensors);
+
     return Void();
 }
 
@@ -47,9 +55,13 @@
     return Result{};
 }
 
-Return<Result> Sensors::activate(int32_t /* sensorHandle */, bool /* enabled */) {
-    // TODO implement
-    return Result{};
+Return<Result> Sensors::activate(int32_t sensorHandle, bool enabled) {
+    auto sensor = mSensors.find(sensorHandle);
+    if (sensor != mSensors.end()) {
+        sensor->second->activate(enabled);
+        return Result::OK;
+    }
+    return Result::BAD_VALUE;
 }
 
 Return<Result> Sensors::initialize(
@@ -86,10 +98,14 @@
     return result;
 }
 
-Return<Result> Sensors::batch(int32_t /* sensorHandle */, int64_t /* samplingPeriodNs */,
+Return<Result> Sensors::batch(int32_t sensorHandle, int64_t samplingPeriodNs,
                               int64_t /* maxReportLatencyNs */) {
-    // TODO implement
-    return Result{};
+    Result result = Result::BAD_VALUE;
+    auto sensor = mSensors.find(sensorHandle);
+    if (sensor != mSensors.end() && sensor->second->batch(samplingPeriodNs)) {
+        result = Result::OK;
+    }
+    return result;
 }
 
 Return<Result> Sensors::flush(int32_t /* sensorHandle */) {
diff --git a/sensors/2.0/default/Sensors.h b/sensors/2.0/default/Sensors.h
index 15a713a..64ee5c5 100644
--- a/sensors/2.0/default/Sensors.h
+++ b/sensors/2.0/default/Sensors.h
@@ -17,6 +17,8 @@
 #ifndef ANDROID_HARDWARE_SENSORS_V2_0_SENSORS_H
 #define ANDROID_HARDWARE_SENSORS_V2_0_SENSORS_H
 
+#include "Sensor.h"
+
 #include <android/hardware/sensors/2.0/ISensors.h>
 #include <fmq/MessageQueue.h>
 #include <hidl/MQDescriptor.h>
@@ -106,6 +108,11 @@
      * Callback for asynchronous events, such as dynamic sensor connections.
      */
     sp<ISensorsCallback> mCallback;
+
+    /**
+     * A map of the available sensors
+     */
+    std::map<int32_t, std::shared_ptr<Sensor>> mSensors;
 };
 
 }  // namespace implementation