blob: 78e34e52ccd323b706fc389118075a593b6d6085 [file] [log] [blame]
Pavel Maltseva2f426a2016-10-04 10:17:05 -07001/*
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_vehicle_V2_0_VehicleUtils_H_
18#define android_hardware_vehicle_V2_0_VehicleUtils_H_
19
20#include <hidl/HidlSupport.h>
21#include <android/hardware/vehicle/2.0/types.h>
22
23namespace android {
24namespace hardware {
25namespace vehicle {
26namespace V2_0 {
27
28hidl_string init_hidl_string(const char *cstr) {
29 hidl_string hidlString;
30 hidlString = cstr;
31 return hidlString;
32}
33
34template <typename T>
35hidl_vec<T> init_hidl_vec(std::initializer_list<T> values) {
36 hidl_vec<T> vector;
37 vector.resize(values.size());
38 size_t i = 0;
39 for (auto& c : values) {
40 vector[i++] = c;
41 }
42 return vector;
43}
44
45// OR operator for class enums. The return type will be enum's underline type.
46template <typename ENUM>
47typename std::underlying_type<ENUM>::type operator |(ENUM v1, ENUM v2) {
48 return static_cast<typename std::underlying_type<ENUM>::type>(v1)
49 | static_cast<typename std::underlying_type<ENUM>::type>(v2);
50}
51
52// AND operator for class enums. The return type will be enum's underline type.
53template <typename ENUM>
54typename std::underlying_type<ENUM>::type operator &(ENUM v1, ENUM v2) {
55 return static_cast<typename std::underlying_type<ENUM>::type>(v1)
56 | static_cast<typename std::underlying_type<ENUM>::type>(v2);
57}
58
59// Returns underlying (integer) value for given enum.
60template <typename ENUM>
61typename std::underlying_type<ENUM>::type enum_val(ENUM const value)
62{
63 return static_cast<typename std::underlying_type<ENUM>::type>(value);
64}
65
66VehiclePropertyType getPropType(VehicleProperty prop) {
67 return static_cast<VehiclePropertyType>(
68 static_cast<int32_t>(prop) & static_cast<int32_t>(VehiclePropertyType::MASK));
69}
70
71VehiclePropertyGroup getPropGroup(VehicleProperty prop) {
72 return static_cast<VehiclePropertyGroup>(
73 static_cast<int32_t>(prop) & static_cast<int32_t>(VehiclePropertyGroup::MASK));
74}
75
76bool checkPropType(VehicleProperty prop, VehiclePropertyType type) {
77 return getPropType(prop) == type;
78}
79
80bool isSystemProperty(VehicleProperty prop) {
81 return VehiclePropertyGroup::SYSTEM == getPropGroup(prop);
82}
83
84
85} // namespace V2_0
86} // namespace vehicle
87} // namespace hardware
88} // namespace android
89
90#endif android_hardware_vehicle_V2_0_VehicleUtils_H_