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 #define LOG_TAG "automotive.vehicle@2.0-impl"
18
19 #include "VehicleUtils.h"
20
21 #include <log/log.h>
22
23 namespace android {
24 namespace hardware {
25 namespace automotive {
26 namespace vehicle {
27 namespace V2_0 {
28
29 //namespace utils {
30
createVehiclePropValue(VehiclePropertyType type,size_t vecSize)31 std::unique_ptr<VehiclePropValue> createVehiclePropValue(
32 VehiclePropertyType type, size_t vecSize) {
33 auto val = std::unique_ptr<VehiclePropValue>(new VehiclePropValue());
34 switch (type) {
35 case VehiclePropertyType::INT32: // fall through
36 case VehiclePropertyType::INT32_VEC: // fall through
37 case VehiclePropertyType::BOOLEAN:
38 val->value.int32Values.resize(vecSize);
39 break;
40 case VehiclePropertyType::FLOAT: // fall through
41 case VehiclePropertyType::FLOAT_VEC:
42 val->value.floatValues.resize(vecSize);
43 break;
44 case VehiclePropertyType::INT64:
45 case VehiclePropertyType::INT64_VEC:
46 val->value.int64Values.resize(vecSize);
47 break;
48 case VehiclePropertyType::BYTES:
49 val->value.bytes.resize(vecSize);
50 break;
51 case VehiclePropertyType::STRING:
52 case VehiclePropertyType::MIXED:
53 break; // Valid, but nothing to do.
54 default:
55 ALOGE("createVehiclePropValue: unknown type: %d", toInt(type));
56 val.reset(nullptr);
57 }
58 return val;
59 }
60
getVehicleRawValueVectorSize(const VehiclePropValue::RawValue & value,VehiclePropertyType type)61 size_t getVehicleRawValueVectorSize(
62 const VehiclePropValue::RawValue& value, VehiclePropertyType type) {
63 switch (type) {
64 case VehiclePropertyType::INT32: // fall through
65 case VehiclePropertyType::INT32_VEC: // fall through
66 case VehiclePropertyType::BOOLEAN:
67 return value.int32Values.size();
68 case VehiclePropertyType::FLOAT: // fall through
69 case VehiclePropertyType::FLOAT_VEC:
70 return value.floatValues.size();
71 case VehiclePropertyType::INT64:
72 case VehiclePropertyType::INT64_VEC:
73 return value.int64Values.size();
74 case VehiclePropertyType::BYTES:
75 return value.bytes.size();
76 default:
77 return 0;
78 }
79 }
80
copyVehicleRawValue(VehiclePropValue::RawValue * dest,const VehiclePropValue::RawValue & src)81 void copyVehicleRawValue(VehiclePropValue::RawValue* dest,
82 const VehiclePropValue::RawValue& src) {
83 dest->int32Values = src.int32Values;
84 dest->floatValues = src.floatValues;
85 dest->int64Values = src.int64Values;
86 dest->bytes = src.bytes;
87 dest->stringValue = src.stringValue;
88 }
89
90 #ifdef __ANDROID__
91
92 template<typename T>
copyHidlVec(hidl_vec<T> * dest,const hidl_vec<T> & src)93 inline void copyHidlVec(hidl_vec <T>* dest, const hidl_vec <T>& src) {
94 for (size_t i = 0; i < std::min(dest->size(), src.size()); i++) {
95 (*dest)[i] = src[i];
96 }
97 }
98
99 template<typename T>
shallowCopyHidlVec(hidl_vec<T> * dest,const hidl_vec<T> & src)100 void shallowCopyHidlVec(hidl_vec <T>* dest, const hidl_vec <T>& src) {
101 if (src.size() > 0) {
102 dest->setToExternal(const_cast<T*>(&src[0]), src.size());
103 } else if (dest->size() > 0) {
104 dest->resize(0);
105 }
106 }
107
shallowCopyHidlStr(hidl_string * dest,const hidl_string & src)108 void shallowCopyHidlStr(hidl_string* dest, const hidl_string& src) {
109 if (src.empty()) {
110 dest->clear();
111 } else {
112 dest->setToExternal(src.c_str(), src.size());
113 }
114 }
115
shallowCopy(VehiclePropValue * dest,const VehiclePropValue & src)116 void shallowCopy(VehiclePropValue* dest, const VehiclePropValue& src) {
117 dest->prop = src.prop;
118 dest->areaId = src.areaId;
119 dest->status = src.status;
120 dest->timestamp = src.timestamp;
121 shallowCopyHidlVec(&dest->value.int32Values, src.value.int32Values);
122 shallowCopyHidlVec(&dest->value.int64Values, src.value.int64Values);
123 shallowCopyHidlVec(&dest->value.floatValues, src.value.floatValues);
124 shallowCopyHidlVec(&dest->value.bytes, src.value.bytes);
125 shallowCopyHidlStr(&dest->value.stringValue, src.value.stringValue);
126 }
127
128 #endif // __ANDROID__
129
130 //} // namespace utils
131
132 } // namespace V2_0
133 } // namespace vehicle
134 } // namespace automotive
135 } // namespace hardware
136 } // namespace android
137