Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 897528dd authored by Brian Stack's avatar Brian Stack
Browse files

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
parent 9a407f23
Loading
Loading
Loading
Loading
+1 −0
Original line number Diff line number Diff line
@@ -20,6 +20,7 @@ cc_binary {
    relative_install_path: "hw",
    srcs: [
        "service.cpp",
        "Sensor.cpp",
        "Sensors.cpp",
    ],
    init_rc: ["android.hardware.sensors@2.0-service.rc"],
+50 −0
Original line number Diff line number Diff line
/*
 * 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
+50 −0
Original line number Diff line number Diff line
/*
 * 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
+24 −8
Original line number Diff line number Diff line
@@ -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 @@ Sensors::~Sensors() {
}

// 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> Sensors::setOperationMode(OperationMode /* mode */) {
    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> Sensors::initialize(
    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 */) {
+7 −0
Original line number Diff line number Diff line
@@ -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 @@ struct Sensors : public ISensors {
     * 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