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

Commit b6261cb2 authored by Kathan Shukla's avatar Kathan Shukla
Browse files

Default hal for Occupant Awareness.

Bug: 142383127
Test: Verified that the system starts the default hal automatically.
Change-Id: I851f85bc283165891943b82c6c07f3273847b032
parent 382ed1d6
Loading
Loading
Loading
Loading
+32 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2019 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.
 */

cc_binary {
    name: "android.hardware.automotive.occupant_awareness@1.0-service",
    init_rc: ["android.hardware.automotive.occupant_awareness@1.0-service.rc"],
    relative_install_path: "hw",
    vendor: true,
    srcs: [
        "service.cpp",
        "OccupantAwareness.cpp",
    ],
    shared_libs: [
        "libbase",
        "libbinder_ndk",
        "libutils",
        "android.hardware.automotive.occupant_awareness-ndk_platform",
    ],
}
+122 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2019 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 "OccupantAwareness.h"

namespace android {
namespace hardware {
namespace automotive {
namespace occupant_awareness {
namespace V1_0 {
namespace implementation {

using ndk::ScopedAStatus;

static const int32_t kAllCapabilities = OccupantAwareness::CAP_PRESENSE_DETECTION |
                                        OccupantAwareness::CAP_GAZE_DETECTION |
                                        OccupantAwareness::CAP_DRIVER_MONITORING_DETECTION;

ScopedAStatus OccupantAwareness::startDetection(OccupantAwarenessStatus* status) {
    std::lock_guard<std::mutex> lock(mMutex);
    if (mStatus != OccupantAwarenessStatus::NOT_SUPPORTED) {
        mStatus = OccupantAwarenessStatus::NOT_SUPPORTED;
        if (mCallback) {
            mCallback->onSystemStatusChanged(kAllCapabilities,
                                             OccupantAwarenessStatus::NOT_SUPPORTED);
        }
    }
    *status = mStatus;
    return ScopedAStatus::ok();
}

ScopedAStatus OccupantAwareness::stopDetection(OccupantAwarenessStatus* status) {
    std::lock_guard<std::mutex> lock(mMutex);
    if (mStatus != OccupantAwarenessStatus::NOT_INITIALIZED) {
        mStatus = OccupantAwarenessStatus::NOT_INITIALIZED;
        if (mCallback) {
            mCallback->onSystemStatusChanged(kAllCapabilities,
                                             OccupantAwarenessStatus::NOT_INITIALIZED);
        }
    }
    *status = mStatus;
    return ScopedAStatus::ok();
}

ScopedAStatus OccupantAwareness::getCapabilityForRole(Role occupantRole, int32_t* capabilities) {
    if (!isValidRole(occupantRole)) {
        return ScopedAStatus::fromExceptionCode(EX_TRANSACTION_FAILED);
    }

    // No awareness capability for default HAL.
    *capabilities = 0;
    return ScopedAStatus::ok();
}

ScopedAStatus OccupantAwareness::getState(Role occupantRole, int detectionCapability,
                                          OccupantAwarenessStatus* status) {
    if (!isValidRole(occupantRole)) {
        return ScopedAStatus::fromExceptionCode(EX_TRANSACTION_FAILED);
    }

    if (!isValidDetectionCapabilities(detectionCapability) ||
        !isSingularCapability(detectionCapability)) {
        return ScopedAStatus::fromExceptionCode(EX_TRANSACTION_FAILED);
    }

    std::lock_guard<std::mutex> lock(mMutex);
    *status = mStatus;
    return ScopedAStatus::ok();
}

ScopedAStatus OccupantAwareness::setCallback(
        const std::shared_ptr<IOccupantAwarenessClientCallback>& callback) {
    if (callback == nullptr) {
        return ScopedAStatus::fromExceptionCode(EX_TRANSACTION_FAILED);
    }

    std::lock_guard<std::mutex> lock(mMutex);
    mCallback = callback;
    return ScopedAStatus::ok();
}

ScopedAStatus OccupantAwareness::getLatestDetection(OccupantDetections* detections) {
    // No detection generated for default hal.
    (void)detections;
    return ScopedAStatus::fromExceptionCode(EX_TRANSACTION_FAILED);
}

bool OccupantAwareness::isValidRole(Role occupantRole) {
    int intVal = static_cast<int>(occupantRole);
    int allOccupants = static_cast<int>(Role::ALL_OCCUPANTS);
    return (occupantRole != Role::INVALID) && ((intVal & (~allOccupants)) == 0);
}

bool OccupantAwareness::isValidDetectionCapabilities(int detectionCapabilities) {
    return (detectionCapabilities != CAP_NONE) &&
           ((detectionCapabilities & (~kAllCapabilities)) == 0);
}

bool OccupantAwareness::isSingularCapability(int detectionCapability) {
    // Check whether the value is 0, or the value has only one bit set.
    return (detectionCapability & (detectionCapability - 1)) == 0;
}

}  // namespace implementation
}  // namespace V1_0
}  // namespace occupant_awareness
}  // namespace automotive
}  // namespace hardware
}  // namespace android
+67 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2019 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.
 */

#pragma once
#include <aidl/android/hardware/automotive/occupant_awareness/BnOccupantAwareness.h>
#include <aidl/android/hardware/automotive/occupant_awareness/BnOccupantAwarenessClientCallback.h>
#include <utils/StrongPointer.h>

namespace android {
namespace hardware {
namespace automotive {
namespace occupant_awareness {
namespace V1_0 {
namespace implementation {

using ::aidl::android::hardware::automotive::occupant_awareness::BnOccupantAwareness;
using ::aidl::android::hardware::automotive::occupant_awareness::IOccupantAwarenessClientCallback;
using ::aidl::android::hardware::automotive::occupant_awareness::OccupantAwarenessStatus;
using ::aidl::android::hardware::automotive::occupant_awareness::OccupantDetections;
using ::aidl::android::hardware::automotive::occupant_awareness::Role;

/**
 * The default HAL mimics a system which has no Occupant awareness capability. The hal does not
 * do any useful work, and returns appropriate failure code / status.
 **/
class OccupantAwareness : public BnOccupantAwareness {
  public:
    // Methods from ::android::hardware::automotive::occupant_awareness::IOccupantAwareness
    // follow.
    ndk::ScopedAStatus startDetection(OccupantAwarenessStatus* status) override;
    ndk::ScopedAStatus stopDetection(OccupantAwarenessStatus* status) override;
    ndk::ScopedAStatus getCapabilityForRole(Role occupantRole, int32_t* capabilities) override;
    ndk::ScopedAStatus getState(Role occupantRole, int detectionCapability,
                                OccupantAwarenessStatus* status) override;
    ndk::ScopedAStatus setCallback(
            const std::shared_ptr<IOccupantAwarenessClientCallback>& callback) override;
    ndk::ScopedAStatus getLatestDetection(OccupantDetections* detections) override;

  private:
    bool isValidRole(Role occupantRole);
    bool isValidDetectionCapabilities(int detectionCapabilities);
    bool isSingularCapability(int detectionCapability);

    std::mutex mMutex;
    std::shared_ptr<IOccupantAwarenessClientCallback> mCallback = nullptr;
    OccupantAwarenessStatus mStatus = OccupantAwarenessStatus::NOT_INITIALIZED;
};

}  // namespace implementation
}  // namespace V1_0
}  // namespace occupant_awareness
}  // namespace automotive
}  // namespace hardware
}  // namespace android
+4 −0
Original line number Diff line number Diff line
service hal_occupant_awareness_default /vendor/bin/hw/android.hardware.automotive.occupant_awareness@1.0-service
    class hal
    user system
    group system
+55 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2016 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.
 */

#define LOG_TAG "android.hardware.automotive.occupant_awareness@1.0-service"

#include <unistd.h>

#include <android-base/logging.h>
#include <android/binder_manager.h>
#include <android/binder_process.h>

#include "OccupantAwareness.h"

using ::aidl::android::hardware::automotive::occupant_awareness::IOccupantAwareness;
using ::android::hardware::automotive::occupant_awareness::V1_0::implementation::OccupantAwareness;
using ::ndk::ScopedAStatus;
using ::ndk::SharedRefBase;

const static char kOccupantAwarenessServiceName[] = "default";

int main() {
    ABinderProcess_setThreadPoolMaxThreadCount(0);
    LOG(INFO) << "Occupant Awareness service is starting";
    std::shared_ptr<OccupantAwareness> occupantAwareness = SharedRefBase::make<OccupantAwareness>();

    const std::string instance =
            std::string() + IOccupantAwareness::descriptor + "/" + kOccupantAwarenessServiceName;

    binder_status_t status =
            AServiceManager_addService(occupantAwareness->asBinder().get(), instance.c_str());
    if (status == STATUS_OK) {
        LOG(INFO) << "Service " << kOccupantAwarenessServiceName << " is ready";
        ABinderProcess_joinThreadPool();
    } else {
        LOG(ERROR) << "Could not register service " << kOccupantAwarenessServiceName
                   << ", status: " << status;
    }

    // In normal operation, we don't expect the thread pool to exit.
    LOG(ERROR) << "Occupant Awareness service is shutting down";
    return 1;
}
Loading