1 /*
2 * Copyright (C) 2021 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 "ProtoMsgConverter"
18
19 #include "ProtoMessageConverter.h"
20
21 #include <VehicleUtils.h>
22
23 #include <memory>
24 #include <vector>
25
26 namespace android {
27 namespace hardware {
28 namespace automotive {
29 namespace vehicle {
30 namespace proto_msg_converter {
31
32 namespace aidl_vehicle = ::aidl::android::hardware::automotive::vehicle;
33 namespace proto = ::android::hardware::automotive::vehicle::proto;
34
35 // Copy the vector PROTO_VECNAME of protobuf class PROTO_VALUE to
36 // VHAL_TYPE_VALUE->VHAL_TYPE_VECNAME, every element of PROTO_VECNAME is casted by CAST.
37 #define CAST_COPY_PROTOBUF_VEC_TO_VHAL_TYPE(PROTO_VALUE, PROTO_VECNAME, VHAL_TYPE_VALUE, \
38 VHAL_TYPE_VECNAME, CAST) \
39 do { \
40 (VHAL_TYPE_VALUE)->VHAL_TYPE_VECNAME.resize(PROTO_VALUE.PROTO_VECNAME##_size()); \
41 size_t idx = 0; \
42 for (auto& value : PROTO_VALUE.PROTO_VECNAME()) { \
43 VHAL_TYPE_VALUE->VHAL_TYPE_VECNAME[idx++] = CAST(value); \
44 } \
45 } while (0)
46
47 // Copying the vector PROTO_VECNAME of protobuf class PROTO_VALUE to
48 // VHAL_TYPE_VALUE->VHAL_TYPE_VECNAME.
49 #define COPY_PROTOBUF_VEC_TO_VHAL_TYPE(PROTO_VALUE, PROTO_VECNAME, VHAL_TYPE_VALUE, \
50 VHAL_TYPE_VECNAME) \
51 CAST_COPY_PROTOBUF_VEC_TO_VHAL_TYPE( \
52 PROTO_VALUE, PROTO_VECNAME, VHAL_TYPE_VALUE, VHAL_TYPE_VECNAME, /*NO CAST*/)
53
aidlToProto(const aidl_vehicle::VehiclePropConfig & in,proto::VehiclePropConfig * out)54 void aidlToProto(const aidl_vehicle::VehiclePropConfig& in, proto::VehiclePropConfig* out) {
55 out->set_prop(in.prop);
56 out->set_access(static_cast<proto::VehiclePropertyAccess>(toInt(in.access)));
57 out->set_change_mode(static_cast<proto::VehiclePropertyChangeMode>(toInt(in.changeMode)));
58 out->set_config_string(in.configString.c_str(), in.configString.size());
59 out->set_min_sample_rate(in.minSampleRate);
60 out->set_max_sample_rate(in.maxSampleRate);
61
62 for (auto& configElement : in.configArray) {
63 out->add_config_array(configElement);
64 }
65
66 out->clear_area_configs();
67 for (auto& areaConfig : in.areaConfigs) {
68 auto* protoACfg = out->add_area_configs();
69 protoACfg->set_area_id(areaConfig.areaId);
70 protoACfg->set_access(static_cast<proto::VehiclePropertyAccess>(toInt(areaConfig.access)));
71 protoACfg->set_min_int64_value(areaConfig.minInt64Value);
72 protoACfg->set_max_int64_value(areaConfig.maxInt64Value);
73 protoACfg->set_min_float_value(areaConfig.minFloatValue);
74 protoACfg->set_max_float_value(areaConfig.maxFloatValue);
75 protoACfg->set_min_int32_value(areaConfig.minInt32Value);
76 protoACfg->set_max_int32_value(areaConfig.maxInt32Value);
77 if (areaConfig.supportedEnumValues.has_value()) {
78 for (auto& supportedEnumValue : areaConfig.supportedEnumValues.value()) {
79 protoACfg->add_supported_enum_values(supportedEnumValue);
80 }
81 }
82 protoACfg->set_support_variable_update_rate(areaConfig.supportVariableUpdateRate);
83 if (areaConfig.hasSupportedValueInfo.has_value()) {
84 // Creates the has_supported_value_info field.
85 proto::HasSupportedValueInfo* hasSupportedValueInfoProto =
86 protoACfg->mutable_has_supported_value_info();
87 hasSupportedValueInfoProto->set_has_min_supported_value(
88 areaConfig.hasSupportedValueInfo->hasMinSupportedValue);
89 hasSupportedValueInfoProto->set_has_max_supported_value(
90 areaConfig.hasSupportedValueInfo->hasMaxSupportedValue);
91 hasSupportedValueInfoProto->set_has_supported_values_list(
92 areaConfig.hasSupportedValueInfo->hasSupportedValuesList);
93 }
94 }
95 }
96
protoToAidl(const proto::VehiclePropConfig & in,aidl_vehicle::VehiclePropConfig * out)97 void protoToAidl(const proto::VehiclePropConfig& in, aidl_vehicle::VehiclePropConfig* out) {
98 out->prop = in.prop();
99 out->access = static_cast<aidl_vehicle::VehiclePropertyAccess>(in.access());
100 out->changeMode = static_cast<aidl_vehicle::VehiclePropertyChangeMode>(in.change_mode());
101 out->configString = in.config_string();
102 out->minSampleRate = in.min_sample_rate();
103 out->maxSampleRate = in.max_sample_rate();
104
105 COPY_PROTOBUF_VEC_TO_VHAL_TYPE(in, config_array, out, configArray);
106
107 auto cast_to_acfg = [](const proto::VehicleAreaConfig& protoAcfg) {
108 auto vehicleAreaConfig = aidl_vehicle::VehicleAreaConfig{
109 .areaId = protoAcfg.area_id(),
110 .minInt32Value = protoAcfg.min_int32_value(),
111 .maxInt32Value = protoAcfg.max_int32_value(),
112 .minInt64Value = protoAcfg.min_int64_value(),
113 .maxInt64Value = protoAcfg.max_int64_value(),
114 .minFloatValue = protoAcfg.min_float_value(),
115 .maxFloatValue = protoAcfg.max_float_value(),
116 .access = static_cast<aidl_vehicle::VehiclePropertyAccess>(protoAcfg.access()),
117 .supportVariableUpdateRate = protoAcfg.support_variable_update_rate(),
118 };
119 if (protoAcfg.supported_enum_values().size() != 0) {
120 vehicleAreaConfig.supportedEnumValues = std::vector<int64_t>();
121 COPY_PROTOBUF_VEC_TO_VHAL_TYPE(protoAcfg, supported_enum_values, (&vehicleAreaConfig),
122 supportedEnumValues.value());
123 }
124 if (protoAcfg.has_has_supported_value_info()) {
125 aidl_vehicle::HasSupportedValueInfo hasSupportedValueInfo = {};
126 hasSupportedValueInfo.hasMinSupportedValue =
127 protoAcfg.has_supported_value_info().has_min_supported_value();
128 hasSupportedValueInfo.hasMaxSupportedValue =
129 protoAcfg.has_supported_value_info().has_max_supported_value();
130 hasSupportedValueInfo.hasSupportedValuesList =
131 protoAcfg.has_supported_value_info().has_supported_values_list();
132 vehicleAreaConfig.hasSupportedValueInfo = hasSupportedValueInfo;
133 }
134
135 return vehicleAreaConfig;
136 };
137 CAST_COPY_PROTOBUF_VEC_TO_VHAL_TYPE(in, area_configs, out, areaConfigs, cast_to_acfg);
138 }
139
aidlToProto(const aidl_vehicle::VehiclePropValue & in,proto::VehiclePropValue * out)140 void aidlToProto(const aidl_vehicle::VehiclePropValue& in, proto::VehiclePropValue* out) {
141 out->set_prop(in.prop);
142 out->set_timestamp(in.timestamp);
143 out->set_status(static_cast<proto::VehiclePropertyStatus>(in.status));
144 out->set_area_id(in.areaId);
145 out->set_string_value(in.value.stringValue);
146 out->set_byte_values(in.value.byteValues.data(), in.value.byteValues.size());
147
148 for (auto& int32Value : in.value.int32Values) {
149 out->add_int32_values(int32Value);
150 }
151
152 for (auto& int64Value : in.value.int64Values) {
153 out->add_int64_values(int64Value);
154 }
155
156 for (auto& floatValue : in.value.floatValues) {
157 out->add_float_values(floatValue);
158 }
159 }
160
protoToAidl(const proto::VehiclePropValue & in,aidl_vehicle::VehiclePropValue * out)161 void protoToAidl(const proto::VehiclePropValue& in, aidl_vehicle::VehiclePropValue* out) {
162 out->prop = in.prop();
163 out->timestamp = in.timestamp();
164 out->status = static_cast<aidl_vehicle::VehiclePropertyStatus>(in.status());
165 out->areaId = in.area_id();
166 out->value.stringValue = in.string_value();
167 for (const char& byte : in.byte_values()) {
168 out->value.byteValues.push_back(byte);
169 }
170
171 COPY_PROTOBUF_VEC_TO_VHAL_TYPE(in, int32_values, out, value.int32Values);
172 COPY_PROTOBUF_VEC_TO_VHAL_TYPE(in, int64_values, out, value.int64Values);
173 COPY_PROTOBUF_VEC_TO_VHAL_TYPE(in, float_values, out, value.floatValues);
174 }
175
aidlToProto(const aidl_vehicle::SubscribeOptions & in,proto::SubscribeOptions * out)176 void aidlToProto(const aidl_vehicle::SubscribeOptions& in, proto::SubscribeOptions* out) {
177 out->set_prop_id(in.propId);
178 for (int areaId : in.areaIds) {
179 out->add_area_ids(areaId);
180 }
181 out->set_sample_rate(in.sampleRate);
182 out->set_resolution(in.resolution);
183 out->set_enable_variable_update_rate(in.enableVariableUpdateRate);
184 }
185
protoToAidl(const proto::SubscribeOptions & in,aidl_vehicle::SubscribeOptions * out)186 void protoToAidl(const proto::SubscribeOptions& in, aidl_vehicle::SubscribeOptions* out) {
187 out->propId = in.prop_id();
188 COPY_PROTOBUF_VEC_TO_VHAL_TYPE(in, area_ids, out, areaIds);
189 out->sampleRate = in.sample_rate();
190 out->resolution = in.resolution();
191 out->enableVariableUpdateRate = in.enable_variable_update_rate();
192 }
193
aidlToProto(const PropIdAreaId & in,proto::PropIdAreaId * out)194 void aidlToProto(const PropIdAreaId& in, proto::PropIdAreaId* out) {
195 out->set_prop_id(in.propId);
196 out->set_area_id(in.areaId);
197 }
198
protoToAidl(const proto::PropIdAreaId & in,PropIdAreaId * out)199 void protoToAidl(const proto::PropIdAreaId& in, PropIdAreaId* out) {
200 out->propId = in.prop_id();
201 out->areaId = in.area_id();
202 }
203
aidlToProto(const aidl_vehicle::RawPropValues & in,proto::RawPropValues * out)204 void aidlToProto(const aidl_vehicle::RawPropValues& in, proto::RawPropValues* out) {
205 out->set_string_value(in.stringValue);
206 out->set_byte_values(in.byteValues.data(), in.byteValues.size());
207 for (auto& int32Value : in.int32Values) {
208 out->add_int32_values(int32Value);
209 }
210 for (auto& int64Value : in.int64Values) {
211 out->add_int64_values(int64Value);
212 }
213 for (auto& floatValue : in.floatValues) {
214 out->add_float_values(floatValue);
215 }
216 }
217
protoToAidl(const proto::RawPropValues & in,aidl_vehicle::RawPropValues * out)218 void protoToAidl(const proto::RawPropValues& in, aidl_vehicle::RawPropValues* out) {
219 COPY_PROTOBUF_VEC_TO_VHAL_TYPE(in, int32_values, out, int32Values);
220 COPY_PROTOBUF_VEC_TO_VHAL_TYPE(in, int64_values, out, int64Values);
221 COPY_PROTOBUF_VEC_TO_VHAL_TYPE(in, float_values, out, floatValues);
222 out->stringValue = in.string_value();
223 for (const char& byte : in.byte_values()) {
224 out->byteValues.push_back(byte);
225 }
226 }
227
aidlToProto(const aidl_vehicle::MinMaxSupportedValueResult & in,proto::MinMaxSupportedValueResult * out)228 void aidlToProto(const aidl_vehicle::MinMaxSupportedValueResult& in,
229 proto::MinMaxSupportedValueResult* out) {
230 out->set_status(static_cast<proto::StatusCode>(in.status));
231 if (in.minSupportedValue.has_value()) {
232 aidlToProto(in.minSupportedValue.value(), out->mutable_min_supported_value());
233 }
234 if (in.maxSupportedValue.has_value()) {
235 aidlToProto(in.maxSupportedValue.value(), out->mutable_max_supported_value());
236 }
237 }
238
protoToAidl(const proto::MinMaxSupportedValueResult & in,aidl_vehicle::MinMaxSupportedValueResult * out)239 void protoToAidl(const proto::MinMaxSupportedValueResult& in,
240 aidl_vehicle::MinMaxSupportedValueResult* out) {
241 out->status = static_cast<aidl_vehicle::StatusCode>(in.status());
242 if (in.has_min_supported_value()) {
243 aidl_vehicle::RawPropValues minSupportedValue = {};
244 protoToAidl(in.min_supported_value(), &minSupportedValue);
245 out->minSupportedValue = minSupportedValue;
246 }
247 if (in.has_max_supported_value()) {
248 aidl_vehicle::RawPropValues maxSupportedValue = {};
249 protoToAidl(in.max_supported_value(), &maxSupportedValue);
250 out->maxSupportedValue = maxSupportedValue;
251 }
252 }
253
aidlToProto(const aidl_vehicle::SupportedValuesListResult & in,proto::SupportedValuesListResult * out)254 void aidlToProto(const aidl_vehicle::SupportedValuesListResult& in,
255 proto::SupportedValuesListResult* out) {
256 out->set_status(static_cast<proto::StatusCode>(in.status));
257 if (!in.supportedValuesList.has_value()) {
258 return;
259 }
260 for (const auto& protoSupportedValue : in.supportedValuesList.value()) {
261 if (protoSupportedValue.has_value()) {
262 aidlToProto(protoSupportedValue.value(), out->add_supported_values_list());
263 }
264 }
265 }
266
protoToAidl(const proto::SupportedValuesListResult & in,aidl_vehicle::SupportedValuesListResult * out)267 void protoToAidl(const proto::SupportedValuesListResult& in,
268 aidl_vehicle::SupportedValuesListResult* out) {
269 out->status = static_cast<aidl_vehicle::StatusCode>(in.status());
270 if (out->status != aidl_vehicle::StatusCode::OK) {
271 return;
272 }
273 std::vector<std::optional<aidl_vehicle::RawPropValues>> aidlSupportedValuesList;
274 for (const auto& protoRawPropValues : in.supported_values_list()) {
275 aidl_vehicle::RawPropValues aidlRawPropValues = {};
276 protoToAidl(protoRawPropValues, &aidlRawPropValues);
277 aidlSupportedValuesList.push_back(std::move(aidlRawPropValues));
278 }
279 out->supportedValuesList = std::move(aidlSupportedValuesList);
280 }
281
282 #undef COPY_PROTOBUF_VEC_TO_VHAL_TYPE
283 #undef CAST_COPY_PROTOBUF_VEC_TO_VHAL_TYPE
284
285 } // namespace proto_msg_converter
286 } // namespace vehicle
287 } // namespace automotive
288 } // namespace hardware
289 } // namespace android
290