1 /* 2 * Copyright (C) 2021 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 #ifndef android_hardware_automotive_vehicle_aidl_impl_fake_impl_userhal_include_FakeUserHal_H_ 18 #define android_hardware_automotive_vehicle_aidl_impl_fake_impl_userhal_include_FakeUserHal_H_ 19 20 #include <android-base/format.h> 21 #include <android-base/result.h> 22 #include <android-base/thread_annotations.h> 23 24 #include <VehicleHalTypes.h> 25 #include <VehicleObjectPool.h> 26 #include <VehicleUtils.h> 27 28 #include <memory> 29 #include <mutex> 30 31 namespace android { 32 namespace hardware { 33 namespace automotive { 34 namespace vehicle { 35 namespace fake { 36 37 constexpr char kUserHalDumpOption[] = "--user-hal"; 38 39 // Class used to emulate a real User HAL behavior through lshal debug requests. 40 class FakeUserHal final { 41 public: 42 using ValueResultType = VhalResult<VehiclePropValuePool::RecyclableType>; 43 FakeUserHal(std::shared_ptr<VehiclePropValuePool> valuePool)44 explicit FakeUserHal(std::shared_ptr<VehiclePropValuePool> valuePool) : mValuePool(valuePool) {} 45 46 ~FakeUserHal() = default; 47 48 // Checks if the emulator can handle the property. 49 static bool isSupported(int32_t prop); 50 51 // Lets the emulator set the property. 52 // 53 // @return updated property and StatusCode 54 ValueResultType onSetProperty( 55 const aidl::android::hardware::automotive::vehicle::VehiclePropValue& value); 56 57 // Gets the property value from the emulator. 58 // 59 // @return property value and StatusCode 60 ValueResultType onGetProperty( 61 const aidl::android::hardware::automotive::vehicle::VehiclePropValue& value) const; 62 63 // Shows the User HAL emulation help. 64 std::string showDumpHelp() const; 65 66 // Dump its contents. 67 std::string dump() const; 68 69 private: 70 const std::shared_ptr<VehiclePropValuePool> mValuePool; 71 mutable std::mutex mLock; 72 VehiclePropValuePool::RecyclableType mInitialUserResponseFromCmd GUARDED_BY(mLock); 73 VehiclePropValuePool::RecyclableType mSwitchUserResponseFromCmd GUARDED_BY(mLock); 74 VehiclePropValuePool::RecyclableType mCreateUserResponseFromCmd GUARDED_BY(mLock); 75 VehiclePropValuePool::RecyclableType mSetUserIdentificationAssociationResponseFromCmd 76 GUARDED_BY(mLock); 77 78 // INITIAL_USER_INFO is called by Android when it starts, and it's expecting a property change 79 // indicating what the initial user should be. 80 // 81 // During normal circumstances, the emulator will reply right away, passing a response if 82 // InitialUserInfoResponseAction::DEFAULT (so Android could use its own logic to decide which 83 // user to boot). 84 // 85 // But during development / testing, the behavior can be changed using lshal dump, which must 86 // use the areaId to indicate what should happen next. 87 // 88 // So, the behavior of set(INITIAL_USER_INFO) is: 89 // 90 // - if it has an areaId, store the property into mInitialUserResponseFromCmd (as it was called 91 // by lshal). 92 // - else if mInitialUserResponseFromCmd is not set, return a response with the same request id 93 // and InitialUserInfoResponseAction::DEFAULT 94 // - else the behavior is defined by the areaId on mInitialUserResponseFromCmd: 95 // - if it's 1, reply with mInitialUserResponseFromCmd and the right request id 96 // - if it's 2, reply with mInitialUserResponseFromCmd but a wrong request id (so Android can 97 // test this error scenario) 98 // - if it's 3, then don't send a property change (so Android can emulate a timeout) 99 ValueResultType onSetInitialUserInfoResponse( 100 const aidl::android::hardware::automotive::vehicle::VehiclePropValue& value); 101 102 // Used to emulate SWITCH_USER - see onSetInitialUserInfoResponse() for usage. 103 ValueResultType onSetSwitchUserResponse( 104 const aidl::android::hardware::automotive::vehicle::VehiclePropValue& value); 105 106 // Used to emulate CREATE_USER - see onSetInitialUserInfoResponse() for usage. 107 ValueResultType onSetCreateUserResponse( 108 const aidl::android::hardware::automotive::vehicle::VehiclePropValue& value); 109 110 // Used to emulate set USER_IDENTIFICATION_ASSOCIATION - see onSetInitialUserInfoResponse() for 111 // usage. 112 ValueResultType onSetUserIdentificationAssociation( 113 const aidl::android::hardware::automotive::vehicle::VehiclePropValue& value); 114 115 // Used to emulate get USER_IDENTIFICATION_ASSOCIATION - see onSetInitialUserInfoResponse() for 116 // usage. 117 ValueResultType onGetUserIdentificationAssociation( 118 const aidl::android::hardware::automotive::vehicle::VehiclePropValue& value) const; 119 120 // Creates a default USER_IDENTIFICATION_ASSOCIATION when it was not set by lshal. 121 static ValueResultType defaultUserIdentificationAssociation( 122 const aidl::android::hardware::automotive::vehicle::VehiclePropValue& request); 123 124 ValueResultType sendUserHalResponse(VehiclePropValuePool::RecyclableType response, 125 int32_t requestId); 126 }; 127 128 } // namespace fake 129 } // namespace vehicle 130 } // namespace automotive 131 } // namespace hardware 132 } // namespace android 133 134 #endif // android_hardware_automotive_vehicle_aidl_impl_fake_impl_userhal_include_FakeUserHal_H_ 135