1 /*
2  * Copyright (C) 2016 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_VehicleUtils_H_
18 #define android_hardware_automotive_vehicle_V2_0_VehicleUtils_H_
19 
20 #include <memory>
21 
22 #ifdef __ANDROID__
23 #include <hidl/HidlSupport.h>
24 #endif
25 
26 #include <android/hardware/automotive/vehicle/2.0/types.h>
27 
28 namespace android {
29 namespace hardware {
30 namespace automotive {
31 namespace vehicle {
32 namespace V2_0 {
33 
34 /** Represents all supported areas for a property. Can be used is  */
35 constexpr int32_t kAllSupportedAreas = 0;
36 
37 /** Returns underlying (integer) value for given enum. */
38 template<typename ENUM, typename U = typename std::underlying_type<ENUM>::type>
toInt(ENUM const value)39 inline constexpr U toInt(ENUM const value) {
40     return static_cast<U>(value);
41 }
42 
getPropType(int32_t prop)43 inline constexpr VehiclePropertyType getPropType(int32_t prop) {
44     return static_cast<VehiclePropertyType>(
45             prop & toInt(VehiclePropertyType::MASK));
46 }
47 
getPropGroup(int32_t prop)48 inline constexpr VehiclePropertyGroup getPropGroup(int32_t prop) {
49     return static_cast<VehiclePropertyGroup>(
50             prop & toInt(VehiclePropertyGroup::MASK));
51 }
52 
getPropArea(int32_t prop)53 inline constexpr VehicleArea getPropArea(int32_t prop) {
54     return static_cast<VehicleArea>(prop & toInt(VehicleArea::MASK));
55 }
56 
isGlobalProp(int32_t prop)57 inline constexpr bool isGlobalProp(int32_t prop) {
58     return getPropArea(prop) == VehicleArea::GLOBAL;
59 }
60 
isSystemProperty(int32_t prop)61 inline constexpr bool isSystemProperty(int32_t prop) {
62     return VehiclePropertyGroup::SYSTEM == getPropGroup(prop);
63 }
64 
65 std::unique_ptr<VehiclePropValue> createVehiclePropValue(
66     VehiclePropertyType type, size_t vecSize);
67 
68 size_t getVehicleRawValueVectorSize(
69     const VehiclePropValue::RawValue& value, VehiclePropertyType type);
70 
71 void copyVehicleRawValue(VehiclePropValue::RawValue* dest,
72                                 const VehiclePropValue::RawValue& src);
73 
74 #ifdef __ANDROID__
75 
76 template<typename T>
77 void shallowCopyHidlVec(hidl_vec<T>* dest, const hidl_vec<T>& src);
78 
79 void shallowCopyHidlStr(hidl_string* dest, const hidl_string& src);
80 
81 void shallowCopy(VehiclePropValue* dest, const VehiclePropValue& src);
82 
83 #endif  // __ANDROID__
84 
85 }  // namespace V2_0
86 }  // namespace vehicle
87 }  // namespace automotive
88 }  // namespace hardware
89 }  // namespace android
90 
91 #endif // android_hardware_automotive_vehicle_V2_0_VehicleUtils_H_
92