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             val->value.int64Values.resize(vecSize);
46             break;
47         case VehiclePropertyType::BYTES:
48             val->value.bytes.resize(vecSize);
49             break;
50         case VehiclePropertyType::STRING:
51         case VehiclePropertyType::COMPLEX:
52             break; // Valid, but nothing to do.
53         default:
54             ALOGE("createVehiclePropValue: unknown type: %d", type);
55             val.reset(nullptr);
56     }
57     return val;
58 }
59 
getVehicleRawValueVectorSize(const VehiclePropValue::RawValue & value,VehiclePropertyType type)60 size_t getVehicleRawValueVectorSize(
61     const VehiclePropValue::RawValue& value, VehiclePropertyType type) {
62     switch (type) {
63         case VehiclePropertyType::INT32:      // fall through
64         case VehiclePropertyType::INT32_VEC:  // fall through
65         case VehiclePropertyType::BOOLEAN:
66             return value.int32Values.size();
67         case VehiclePropertyType::FLOAT:      // fall through
68         case VehiclePropertyType::FLOAT_VEC:
69             return value.floatValues.size();
70         case VehiclePropertyType::INT64:
71             return value.int64Values.size();
72         case VehiclePropertyType::BYTES:
73             return value.bytes.size();
74         default:
75             return 0;
76     }
77 }
78 
79 template<typename T>
copyHidlVec(hidl_vec<T> * dest,const hidl_vec<T> & src)80 inline void copyHidlVec(hidl_vec <T>* dest, const hidl_vec <T>& src) {
81     for (size_t i = 0; i < std::min(dest->size(), src.size()); i++) {
82         (*dest)[i] = src[i];
83     }
84 }
85 
copyVehicleRawValue(VehiclePropValue::RawValue * dest,const VehiclePropValue::RawValue & src)86 void copyVehicleRawValue(VehiclePropValue::RawValue* dest,
87                          const VehiclePropValue::RawValue& src) {
88     dest->int32Values = src.int32Values;
89     dest->floatValues = src.floatValues;
90     dest->int64Values = src.int64Values;
91     dest->bytes = src.bytes;
92     dest->stringValue = src.stringValue;
93 }
94 
95 template<typename T>
shallowCopyHidlVec(hidl_vec<T> * dest,const hidl_vec<T> & src)96 void shallowCopyHidlVec(hidl_vec <T>* dest, const hidl_vec <T>& src) {
97     if (src.size() > 0) {
98         dest->setToExternal(const_cast<T*>(&src[0]), src.size());
99     } else if (dest->size() > 0) {
100         dest->resize(0);
101     }
102 }
103 
shallowCopyHidlStr(hidl_string * dest,const hidl_string & src)104 void shallowCopyHidlStr(hidl_string* dest, const hidl_string& src) {
105     if (!src.empty()) {
106         dest->setToExternal(src.c_str(), src.size());
107     } else if (dest->size() > 0) {
108         dest->setToExternal(0, 0);
109     }
110 }
111 
shallowCopy(VehiclePropValue * dest,const VehiclePropValue & src)112 void shallowCopy(VehiclePropValue* dest, const VehiclePropValue& src) {
113     dest->prop = src.prop;
114     dest->areaId = src.areaId;
115     dest->timestamp = src.timestamp;
116     shallowCopyHidlVec(&dest->value.int32Values, src.value.int32Values);
117     shallowCopyHidlVec(&dest->value.int64Values, src.value.int64Values);
118     shallowCopyHidlVec(&dest->value.floatValues, src.value.floatValues);
119     shallowCopyHidlVec(&dest->value.bytes, src.value.bytes);
120     shallowCopyHidlStr(&dest->value.stringValue, src.value.stringValue);
121 }
122 
123 
124 //}  // namespace utils
125 
126 }  // namespace V2_0
127 }  // namespace vehicle
128 }  // namespace automotive
129 }  // namespace hardware
130 }  // namespace android
131