1 /* 2 * Copyright (C) 2020 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_V2_0_impl_FakeUserHal_H_ 18 #define android_hardware_automotive_vehicle_V2_0_impl_FakeUserHal_H_ 19 20 #include <android-base/format.h> 21 #include <android-base/result.h> 22 23 #include <android/hardware/automotive/vehicle/2.0/types.h> 24 25 namespace android { 26 namespace hardware { 27 namespace automotive { 28 namespace vehicle { 29 namespace V2_0 { 30 31 namespace impl { 32 33 constexpr char kUserHalDumpOption[] = "--user-hal"; 34 35 /** 36 * Class used to emulate a real User HAL behavior through lshal debug requests. 37 */ 38 class FakeUserHal { 39 public: FakeUserHal()40 FakeUserHal() {} 41 42 ~FakeUserHal() = default; 43 44 /** 45 * Checks if the emulator can handle the property. 46 */ 47 bool isSupported(int32_t prop); 48 49 /** 50 * Lets the emulator set the property. 51 * 52 * @return updated property and StatusCode 53 */ 54 android::base::Result<std::unique_ptr<VehiclePropValue>> onSetProperty( 55 const VehiclePropValue& value); 56 57 /** 58 * Gets the property value from the emulator. 59 * 60 * @return property value and StatusCode 61 */ 62 android::base::Result<std::unique_ptr<VehiclePropValue>> onGetProperty( 63 const VehiclePropValue& value); 64 65 /** 66 * Shows the User HAL emulation help. 67 */ 68 std::string showDumpHelp(); 69 70 /** 71 * Dump its contents. 72 */ 73 std::string dump(std::string indent); 74 75 private: 76 /** 77 * INITIAL_USER_INFO is called by Android when it starts, and it's expecting a property change 78 * indicating what the initial user should be. 79 * 80 * During normal circumstances, the emulator will reply right away, passing a response if 81 * InitialUserInfoResponseAction::DEFAULT (so Android could use its own logic to decide which 82 * user to boot). 83 * 84 * But during development / testing, the behavior can be changed using lshal dump, which must 85 * use the areaId to indicate what should happen next. 86 * 87 * So, the behavior of set(INITIAL_USER_INFO) is: 88 * 89 * - if it has an areaId, store the property into mInitialUserResponseFromCmd (as it was called 90 * by lshal). 91 * - else if mInitialUserResponseFromCmd is not set, return a response with the same request id 92 * and InitialUserInfoResponseAction::DEFAULT 93 * - else the behavior is defined by the areaId on mInitialUserResponseFromCmd: 94 * - if it's 1, reply with mInitialUserResponseFromCmd and the right request id 95 * - if it's 2, reply with mInitialUserResponseFromCmd but a wrong request id (so Android can 96 * test this error scenario) 97 * - if it's 3, then don't send a property change (so Android can emulate a timeout) 98 * 99 */ 100 android::base::Result<std::unique_ptr<VehiclePropValue>> onSetInitialUserInfoResponse( 101 const VehiclePropValue& value); 102 103 /** 104 * Used to emulate SWITCH_USER - see onSetInitialUserInfoResponse() for usage. 105 */ 106 android::base::Result<std::unique_ptr<VehiclePropValue>> onSetSwitchUserResponse( 107 const VehiclePropValue& value); 108 109 /** 110 * Used to emulate CREATE_USER - see onSetInitialUserInfoResponse() for usage. 111 */ 112 android::base::Result<std::unique_ptr<VehiclePropValue>> onSetCreateUserResponse( 113 const VehiclePropValue& value); 114 115 /** 116 * Used to emulate set USER_IDENTIFICATION_ASSOCIATION - see onSetInitialUserInfoResponse() for 117 * usage. 118 */ 119 android::base::Result<std::unique_ptr<VehiclePropValue>> onSetUserIdentificationAssociation( 120 const VehiclePropValue& value); 121 122 /** 123 * Used to emulate get USER_IDENTIFICATION_ASSOCIATION - see onSetInitialUserInfoResponse() for 124 * usage. 125 */ 126 android::base::Result<std::unique_ptr<VehiclePropValue>> onGetUserIdentificationAssociation( 127 const VehiclePropValue& value); 128 129 /** 130 * Creates a default USER_IDENTIFICATION_ASSOCIATION when it was not set by lshal. 131 */ 132 android::base::Result<std::unique_ptr<VehiclePropValue>> defaultUserIdentificationAssociation( 133 const VehiclePropValue& request); 134 135 android::base::Result<std::unique_ptr<VehiclePropValue>> sendUserHalResponse( 136 std::unique_ptr<VehiclePropValue> response, int32_t requestId); 137 138 std::unique_ptr<VehiclePropValue> mInitialUserResponseFromCmd; 139 std::unique_ptr<VehiclePropValue> mSwitchUserResponseFromCmd; 140 std::unique_ptr<VehiclePropValue> mCreateUserResponseFromCmd; 141 std::unique_ptr<VehiclePropValue> mSetUserIdentificationAssociationResponseFromCmd; 142 }; 143 144 } // namespace impl 145 146 } // namespace V2_0 147 } // namespace vehicle 148 } // namespace automotive 149 } // namespace hardware 150 } // namespace android 151 152 #endif // android_hardware_automotive_vehicle_V2_0_impl_FakeUserHal_H_ 153