xref: /aosp_15_r20/hardware/interfaces/automotive/vehicle/2.0/default/common/include/vhal_v2_0/VehicleConnector.h (revision 4d7e907c777eeecc4c5bd7cf640a754fac206ff7)
1 /*
2  * Copyright (C) 2019 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_VehicleConnector_H_
18 #define android_hardware_automotive_vehicle_V2_0_VehicleConnector_H_
19 
20 #include <vector>
21 
22 #include <android/hardware/automotive/vehicle/2.0/types.h>
23 #include <utils/Log.h>
24 
25 #include "VehicleClient.h"
26 #include "VehicleServer.h"
27 
28 namespace android {
29 namespace hardware {
30 namespace automotive {
31 namespace vehicle {
32 namespace V2_0 {
33 
34 /**
35  *  This file defines the interface of client/server pair for HAL-vehicle
36  *  communication. Vehicle HAL may use this interface to talk to the vehicle
37  *  regardless of the underlying communication channels.
38  */
39 
40 /**
41  *  If Android has direct access to the vehicle, then the client and
42  *  the server may act in passthrough mode to avoid extra IPC
43  *
44  *  Template is used here for spliting the logic of operating Android objects (VehicleClientType),
45  *  talking to cars (VehicleServerType) and the commucation between client and server (passthrough
46  *  mode in this case), so that we can easily combine different parts together without duplicating
47  *  codes (for example, in Google VHAL, the server talks to the fake car in the same way no matter
48  *  if it is on top of passthrough connector or VSOCK or any other communication channels between
49  *  client and server)
50  *
51  *  The alternative may be factoring the common logic of every operations for both client and
52  *  server. Which is not always the case. Making sure different non-template connectors calling
53  *  the same method is hard, especially when the engineer maintaining the code may not be aware
54  *  of it when making changes. Template is a clean and easy way to solve this problem in this
55  *  case.
56  */
57 template <typename VehicleClientType, typename VehicleServerType>
58 class IPassThroughConnector : public VehicleClientType, public VehicleServerType {
59     static_assert(std::is_base_of_v<IVehicleClient, VehicleClientType>);
60     static_assert(std::is_base_of_v<IVehicleServer, VehicleServerType>);
61 
62   public:
getAllPropertyConfig()63     std::vector<VehiclePropConfig> getAllPropertyConfig() const override {
64         return this->onGetAllPropertyConfig();
65     }
66 
setProperty(const VehiclePropValue & value,bool updateStatus)67     StatusCode setProperty(const VehiclePropValue& value, bool updateStatus) override {
68         return this->onSetProperty(value, updateStatus);
69     }
70 
onPropertyValueFromCar(const VehiclePropValue & value,bool updateStatus)71     void onPropertyValueFromCar(const VehiclePropValue& value, bool updateStatus) override {
72         return this->onPropertyValue(value, updateStatus);
73     }
74 
dump(const hidl_handle & handle,const hidl_vec<hidl_string> & options)75     bool dump(const hidl_handle& handle, const hidl_vec<hidl_string>& options) override {
76         // Calls server's onDump function and print the dumped info to the handle.
77         std::vector<std::string> stdOptions;
78         for (size_t i = 0; i < options.size(); i++) {
79             stdOptions.push_back(options[i]);
80         }
81         IVehicleServer::DumpResult result = this->onDump(stdOptions);
82         int fd = handle->data[0];
83         if (fd < 0) {
84             ALOGW("Invalid fd from HIDL handle: %d", fd);
85             return false;
86         }
87         if (result.buffer.size() != 0) {
88             dprintf(fd, "[VehicleHalServer] Dumped info: %s\n", result.buffer.c_str());
89         }
90         return result.callerShouldDumpState;
91     }
92 
93     // To be implemented:
94     // virtual std::vector<VehiclePropConfig> onGetAllPropertyConfig() = 0;
95     // virtual void onPropertyValue(const VehiclePropValue& value) = 0;
96     // virtual StatusCode onSetProperty(const VehiclePropValue& value) = 0;
97 };
98 
99 }  // namespace V2_0
100 }  // namespace vehicle
101 }  // namespace automotive
102 }  // namespace hardware
103 }  // namespace android
104 
105 #endif  // android_hardware_automotive_vehicle_V2_0_VehicleConnector_H_
106