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

Commit 7ed3aeb6 authored by Yu Shan's avatar Yu Shan
Browse files

Migrate fake user hal.

This CL mostly copies the existing user hal library with minor
modifications to use new AIDL type.

Test: atest FakeUserHalTest.
Bug: 201830716
Change-Id: I2bf4e96fb9fd71242e01f89c92cf743f7287ef64
parent 18198d7c
Loading
Loading
Loading
Loading
+32 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2021 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.
 */

package {
    default_applicable_licenses: ["Android-Apache-2.0"],
}

cc_library {
    name: "FakeUserHal",
    vendor: true,
    srcs: ["src/*.cpp"],
    local_include_dirs: ["include"],
    export_include_dirs: ["include"],
    defaults: ["VehicleHalDefaults"],
    static_libs: [
        "VehicleHalUtils",
    ],
    export_static_lib_headers: ["VehicleHalUtils"],
}
+132 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2021 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_automotive_vehicle_aidl_impl_fake_impl_userhal_include_FakeUserHal_H_
#define android_hardware_automotive_vehicle_aidl_impl_fake_impl_userhal_include_FakeUserHal_H_

#include <android-base/format.h>
#include <android-base/result.h>
#include <android-base/thread_annotations.h>

#include <VehicleHalTypes.h>
#include <VehicleObjectPool.h>

#include <memory>
#include <mutex>

namespace android {
namespace hardware {
namespace automotive {
namespace vehicle {
namespace fake {

constexpr char kUserHalDumpOption[] = "--user-hal";

// Class used to emulate a real User HAL behavior through lshal debug requests.
class FakeUserHal final {
  public:
    explicit FakeUserHal(std::shared_ptr<VehiclePropValuePool> valuePool) : mValuePool(valuePool) {}

    ~FakeUserHal() = default;

    // Checks if the emulator can handle the property.
    static bool isSupported(int32_t prop);

    // Lets the emulator set the property.
    //
    // @return updated property and StatusCode
    android::base::Result<VehiclePropValuePool::RecyclableType> onSetProperty(
            const ::aidl::android::hardware::automotive::vehicle::VehiclePropValue& value);

    // Gets the property value from the emulator.
    //
    // @return property value and StatusCode
    android::base::Result<VehiclePropValuePool::RecyclableType> onGetProperty(
            const ::aidl::android::hardware::automotive::vehicle::VehiclePropValue& value) const;

    // Shows the User HAL emulation help.
    std::string showDumpHelp() const;

    // Dump its contents.
    std::string dump(std::string indent) const;

  private:
    const std::shared_ptr<VehiclePropValuePool> mValuePool;
    mutable std::mutex mLock;
    VehiclePropValuePool::RecyclableType mInitialUserResponseFromCmd GUARDED_BY(mLock);
    VehiclePropValuePool::RecyclableType mSwitchUserResponseFromCmd GUARDED_BY(mLock);
    VehiclePropValuePool::RecyclableType mCreateUserResponseFromCmd GUARDED_BY(mLock);
    VehiclePropValuePool::RecyclableType mSetUserIdentificationAssociationResponseFromCmd
            GUARDED_BY(mLock);

    // INITIAL_USER_INFO is called by Android when it starts, and it's expecting a property change
    // indicating what the initial user should be.
    //
    // During normal circumstances, the emulator will reply right away, passing a response if
    // InitialUserInfoResponseAction::DEFAULT (so Android could use its own logic to decide which
    // user to boot).
    //
    // But during development / testing, the behavior can be changed using lshal dump, which must
    // use the areaId to indicate what should happen next.
    //
    // So, the behavior of set(INITIAL_USER_INFO) is:
    //
    // - if it has an areaId, store the property into mInitialUserResponseFromCmd (as it was called
    // by lshal).
    // - else if mInitialUserResponseFromCmd is not set, return a response with the same request id
    // and InitialUserInfoResponseAction::DEFAULT
    // - else the behavior is defined by the areaId on mInitialUserResponseFromCmd:
    // - if it's 1, reply with mInitialUserResponseFromCmd and the right request id
    // - if it's 2, reply with mInitialUserResponseFromCmd but a wrong request id (so Android can
    // test this error scenario)
    // - if it's 3, then don't send a property change (so Android can emulate a timeout)
    android::base::Result<VehiclePropValuePool::RecyclableType> onSetInitialUserInfoResponse(
            const ::aidl::android::hardware::automotive::vehicle::VehiclePropValue& value);

    // Used to emulate SWITCH_USER - see onSetInitialUserInfoResponse() for usage.
    android::base::Result<VehiclePropValuePool::RecyclableType> onSetSwitchUserResponse(
            const ::aidl::android::hardware::automotive::vehicle::VehiclePropValue& value);

    // Used to emulate CREATE_USER - see onSetInitialUserInfoResponse() for usage.
    android::base::Result<VehiclePropValuePool::RecyclableType> onSetCreateUserResponse(
            const ::aidl::android::hardware::automotive::vehicle::VehiclePropValue& value);

    // Used to emulate set USER_IDENTIFICATION_ASSOCIATION - see onSetInitialUserInfoResponse() for
    // usage.
    android::base::Result<VehiclePropValuePool::RecyclableType> onSetUserIdentificationAssociation(
            const ::aidl::android::hardware::automotive::vehicle::VehiclePropValue& value);

    // Used to emulate get USER_IDENTIFICATION_ASSOCIATION - see onSetInitialUserInfoResponse() for
    // usage.
    android::base::Result<VehiclePropValuePool::RecyclableType> onGetUserIdentificationAssociation(
            const ::aidl::android::hardware::automotive::vehicle::VehiclePropValue& value) const;

    // Creates a default USER_IDENTIFICATION_ASSOCIATION when it was not set by lshal.
    static android::base::Result<VehiclePropValuePool::RecyclableType>
    defaultUserIdentificationAssociation(
            const ::aidl::android::hardware::automotive::vehicle::VehiclePropValue& request);

    android::base::Result<VehiclePropValuePool::RecyclableType> sendUserHalResponse(
            VehiclePropValuePool::RecyclableType response, int32_t requestId);
};

}  // namespace fake
}  // namespace vehicle
}  // namespace automotive
}  // namespace hardware
}  // namespace android

#endif  // android_hardware_automotive_vehicle_aidl_impl_fake_impl_userhal_include_FakeUserHal_H_
+88 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2021 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_automotive_vehicle_aidl_impl_fake_impl_userhal_include_UserHalHelper_H_
#define android_hardware_automotive_vehicle_aidl_impl_fake_impl_userhal_include_UserHalHelper_H_

#include <UserHalTypes.h>
#include <VehicleHalTypes.h>
#include <VehicleObjectPool.h>
#include <android-base/result.h>

#include <functional>
#include <memory>

namespace android {
namespace hardware {
namespace automotive {
namespace vehicle {
namespace fake {
namespace user_hal_helper {

// Verify whether the |value| can be casted to the type |T| and return the casted value on success.
// Otherwise, return the error.
template <typename T>
::android::base::Result<T> verifyAndCast(int32_t value);

// Below functions parse VehiclePropValues to the respective User HAL request structs. On success,
// these functions return the User HAL struct. Otherwise, they return the error.
::android::base::Result<::aidl::android::hardware::automotive::vehicle::InitialUserInfoRequest>
toInitialUserInfoRequest(
        const ::aidl::android::hardware::automotive::vehicle::VehiclePropValue& propValue);
::android::base::Result<::aidl::android::hardware::automotive::vehicle::SwitchUserRequest>
toSwitchUserRequest(
        const ::aidl::android::hardware::automotive::vehicle::VehiclePropValue& propValue);
::android::base::Result<::aidl::android::hardware::automotive::vehicle::CreateUserRequest>
toCreateUserRequest(
        const ::aidl::android::hardware::automotive::vehicle::VehiclePropValue& propValue);
::android::base::Result<::aidl::android::hardware::automotive::vehicle::RemoveUserRequest>
toRemoveUserRequest(
        const ::aidl::android::hardware::automotive::vehicle::VehiclePropValue& propValue);
::android::base::Result<
        ::aidl::android::hardware::automotive::vehicle::UserIdentificationGetRequest>
toUserIdentificationGetRequest(
        const ::aidl::android::hardware::automotive::vehicle::VehiclePropValue& propValue);
::android::base::Result<
        ::aidl::android::hardware::automotive::vehicle::UserIdentificationSetRequest>
toUserIdentificationSetRequest(
        const ::aidl::android::hardware::automotive::vehicle::VehiclePropValue& propValue);

// Below functions convert the User HAL structs to VehiclePropValues. On success, these functions
// return the pointer to VehiclePropValue. Otherwise, they return the error.
::android::base::Result<VehiclePropValuePool::RecyclableType> toVehiclePropValue(
        VehiclePropValuePool& pool,
        const ::aidl::android::hardware::automotive::vehicle::SwitchUserRequest& request);
VehiclePropValuePool::RecyclableType toVehiclePropValue(
        VehiclePropValuePool& pool,
        const ::aidl::android::hardware::automotive::vehicle::InitialUserInfoResponse& response);
VehiclePropValuePool::RecyclableType toVehiclePropValue(
        VehiclePropValuePool& pool,
        const ::aidl::android::hardware::automotive::vehicle::SwitchUserResponse& response);
VehiclePropValuePool::RecyclableType toVehiclePropValue(
        VehiclePropValuePool& pool,
        const ::aidl::android::hardware::automotive::vehicle::CreateUserResponse& response);
VehiclePropValuePool::RecyclableType toVehiclePropValue(
        VehiclePropValuePool& pool,
        const ::aidl::android::hardware::automotive::vehicle::UserIdentificationResponse& response);

}  // namespace user_hal_helper
}  // namespace fake
}  // namespace vehicle
}  // namespace automotive
}  // namespace hardware
}  // namespace android

#endif  // android_hardware_automotive_vehicle_aidl_impl_fake_impl_userhal_include_UserHalHelper_H_
+37 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2021 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_automotive_vehicle_aidl_impl_fake_impl_userhal_include_UserHalTypes_H_
#define android_hardware_automotive_vehicle_aidl_impl_fake_impl_userhal_include_UserHalTypes_H_

#include <aidl/android/hardware/automotive/vehicle/CreateUserRequest.h>
#include <aidl/android/hardware/automotive/vehicle/CreateUserResponse.h>
#include <aidl/android/hardware/automotive/vehicle/CreateUserStatus.h>
#include <aidl/android/hardware/automotive/vehicle/InitialUserInfoRequest.h>
#include <aidl/android/hardware/automotive/vehicle/InitialUserInfoRequestType.h>
#include <aidl/android/hardware/automotive/vehicle/InitialUserInfoResponse.h>
#include <aidl/android/hardware/automotive/vehicle/RemoveUserRequest.h>
#include <aidl/android/hardware/automotive/vehicle/SwitchUserMessageType.h>
#include <aidl/android/hardware/automotive/vehicle/SwitchUserRequest.h>
#include <aidl/android/hardware/automotive/vehicle/SwitchUserResponse.h>
#include <aidl/android/hardware/automotive/vehicle/SwitchUserStatus.h>
#include <aidl/android/hardware/automotive/vehicle/UserIdentificationGetRequest.h>
#include <aidl/android/hardware/automotive/vehicle/UserIdentificationResponse.h>
#include <aidl/android/hardware/automotive/vehicle/UserIdentificationSetRequest.h>
#include <aidl/android/hardware/automotive/vehicle/UserInfo.h>
#include <aidl/android/hardware/automotive/vehicle/UsersInfo.h>

#endif  // android_hardware_automotive_vehicle_aidl_impl_fake_impl_userhal_include_UserHalTypes_H_
+374 −0

File added.

Preview size limit exceeded, changes collapsed.

Loading