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 
24 #include "VehicleClient.h"
25 #include "VehicleServer.h"
26 
27 namespace android {
28 namespace hardware {
29 namespace automotive {
30 namespace vehicle {
31 namespace V2_0 {
32 
33 /**
34  *  This file defines the interface of client/server pair for HAL-vehicle
35  *  communication. Vehicle HAL may use this interface to talk to the vehicle
36  *  regardless of the underlying communication channels.
37  */
38 
39 /**
40  *  If Android has direct access to the vehicle, then the client and
41  *  the server may act in passthrough mode to avoid extra IPC
42  *
43  *  Template is used here for spliting the logic of operating Android objects (VehicleClientType),
44  *  talking to cars (VehicleServerType) and the commucation between client and server (passthrough
45  *  mode in this case), so that we can easily combine different parts together without duplicating
46  *  codes (for example, in Google VHAL, the server talks to the fake car in the same way no matter
47  *  if it is on top of passthrough connector or VSOCK or any other communication channels between
48  *  client and server)
49  *
50  *  The alternative may be factoring the common logic of every operations for both client and
51  *  server. Which is not always the case. Making sure different non-template connectors calling
52  *  the same method is hard, especially when the engineer maintaining the code may not be aware
53  *  of it when making changes. Template is a clean and easy way to solve this problem in this
54  *  case.
55  */
56 template <typename VehicleClientType, typename VehicleServerType>
57 class IPassThroughConnector : public VehicleClientType, public VehicleServerType {
58     static_assert(std::is_base_of_v<IVehicleClient, VehicleClientType>);
59     static_assert(std::is_base_of_v<IVehicleServer, VehicleServerType>);
60 
61   public:
getAllPropertyConfig()62     std::vector<VehiclePropConfig> getAllPropertyConfig() const override {
63         return this->onGetAllPropertyConfig();
64     }
65 
setProperty(const VehiclePropValue & value,bool updateStatus)66     StatusCode setProperty(const VehiclePropValue& value, bool updateStatus) override {
67         return this->onSetProperty(value, updateStatus);
68     }
69 
onPropertyValueFromCar(const VehiclePropValue & value,bool updateStatus)70     void onPropertyValueFromCar(const VehiclePropValue& value, bool updateStatus) override {
71         return this->onPropertyValue(value, updateStatus);
72     }
73 
dump(const hidl_handle & handle,const hidl_vec<hidl_string> & options)74     bool dump(const hidl_handle& handle, const hidl_vec<hidl_string>& options) override {
75         return this->onDump(handle, options);
76     }
77 
78     // To be implemented:
79     // virtual std::vector<VehiclePropConfig> onGetAllPropertyConfig() = 0;
80     // virtual void onPropertyValue(const VehiclePropValue& value) = 0;
81     // virtual StatusCode onSetProperty(const VehiclePropValue& value) = 0;
82 };
83 
84 }  // namespace V2_0
85 }  // namespace vehicle
86 }  // namespace automotive
87 }  // namespace hardware
88 }  // namespace android
89 
90 #endif  // android_hardware_automotive_vehicle_V2_0_VehicleConnector_H_
91