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 #pragma once
18 
19 #include <vhal_v2_0/VehicleObjectPool.h>
20 #include <vhal_v2_0/VehiclePropertyStore.h>
21 #include <vhal_v2_0/VehicleServer.h>
22 
23 #include "DefaultConfig.h"
24 #include "GeneratorHub.h"
25 
26 namespace android {
27 namespace hardware {
28 namespace automotive {
29 namespace vehicle {
30 namespace V2_0 {
31 
32 namespace impl {
33 
34 // This contains the server operation for VHAL running in emulator.
35 class DefaultVehicleHalServer : public IVehicleServer {
36   public:
37     DefaultVehicleHalServer();
38 
39     // Send all the property values to client.
40     void sendAllValuesToClient();
41 
42     // Methods from IVehicleServer
43 
44     std::vector<VehiclePropConfig> onGetAllPropertyConfig() const override;
45 
46     StatusCode onSetProperty(const VehiclePropValue& value, bool updateStatus) override;
47 
48     // Dump/debug the Default VHAL server. If options is empty, the internal information for the
49     // server would be dumped. Otherwise, the options would be treated as debug commands and sent
50     // to debug function to handle the commands.
51     DumpResult onDump(const std::vector<std::string>& options) override;
52 
53     // Set the Property Value Pool used in this server
54     void setValuePool(VehiclePropValuePool* valuePool);
55 
56   protected:
57     using VehiclePropValuePtr = recyclable_ptr<VehiclePropValue>;
58     GeneratorHub* getGeneratorHub();
59 
60     VehiclePropValuePool* getValuePool() const;
61 
62     void onFakeValueGenerated(const VehiclePropValue& value);
63 
64     StatusCode handleGenerateFakeDataRequest(const VehiclePropValue& request);
65 
66     VehiclePropValuePtr createApPowerStateReq(VehicleApPowerStateReq req, int32_t param);
67 
68     VehiclePropValuePtr createHwInputKeyProp(VehicleHwKeyInputAction action, int32_t keyCode,
69                                              int32_t targetDisplay);
70 
71     void storePropInitialValue(const ConfigDeclaration& config);
72 
73     // Handles debug commands. The first option must be "--debughal" otherwise the command would be
74     // ignored. The second option specifies the operations to execute. Different operations require
75     // different input options, for detail, see the helpInfo printed by getHelpInfo().
76     DumpResult debugCommand(const std::vector<std::string>& options);
77 
78     // Gets help info. Contains the usage for different debug commands.
79     std::string getHelpInfo();
80 
81     // If "persist.vendor.vhal_init_value_override" is true, try to override the properties default
82     // values according to JSON files in 'overrideDir'. Would be called in constructor using
83     // VENDOR_OVERRIDE_DIR as overrideDir.
84     void maybeOverrideProperties(const char* overrideDir);
85 
86     // Handles "--genfakedata" debug command.
87     DumpResult genFakeDataCommand(const std::vector<std::string>& options);
88 
89     // Handles "--setint" or "--setfloat" or "--setbool" debug command.
90     DumpResult setValueCommand(const std::vector<std::string>& options);
91 
92   protected:
93     GeneratorHub mGeneratorHub{
94             [this](const VehiclePropValue& value) { return onFakeValueGenerated(value); }};
95 
96     VehiclePropValuePool* mValuePool{nullptr};
97     VehiclePropertyStore mServerSidePropStore;
98 
99   private:
100     // Expose protected methods to unit test.
101     friend class DefaultVhalImplTestHelper;
102     // Override the properties using config files in 'overrideDir'.
103     void overrideProperties(const char* overrideDir);
104 };
105 
106 }  // namespace impl
107 
108 }  // namespace V2_0
109 }  // namespace vehicle
110 }  // namespace automotive
111 }  // namespace hardware
112 }  // namespace android
113