blob: c79164c021bbdee8ebbd50d6b8b39ebff9d67d28 [file] [log] [blame]
keunyounge18e25d2015-08-28 15:57:19 -07001/*
2 * Copyright (C) 2015 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
keunyoungd32f4e62015-09-21 11:33:06 -070017#define LOG_TAG "VehicleNetwork"
18
19#include <utils/Log.h>
20
keunyounge18e25d2015-08-28 15:57:19 -070021#include <IVehicleNetwork.h>
22#include "VehicleNetworkProtoUtil.h"
23
24namespace android {
25
26static status_t copyString(const std::string& in, uint8_t** out, int32_t* len) {
27 *len = in.length();
keunyoung7d74e6d2015-10-14 15:43:10 -070028 if (*len == 0) {
29 *out = NULL;
30 return NO_ERROR;
31 }
keunyounge18e25d2015-08-28 15:57:19 -070032 *out = new uint8_t[*len];
33 ASSERT_OR_HANDLE_NO_MEMORY(*out, return NO_MEMORY);
34 memcpy(*out, in.data(), *len);
35 return NO_ERROR;
36}
37
38status_t VehicleNetworkProtoUtil::toVehiclePropValue(const vehicle_prop_value_t& in,
39 VehiclePropValue& out, bool /*inPlace*/) {
40 out.set_prop(in.prop);
41 out.set_value_type(in.value_type);
42 out.set_timestamp(in.timestamp);
Keun-young Park71b2f5c2016-03-10 18:44:40 -080043 out.set_zone(in.zone);
keunyounge18e25d2015-08-28 15:57:19 -070044 switch (in.value_type) {
45 case VEHICLE_VALUE_TYPE_STRING: {
46 //TODO fix ugly copy here for inplace mode
keunyoung7d74e6d2015-10-14 15:43:10 -070047 if (in.value.str_value.len > 0) {
48 out.set_string_value((char*)in.value.str_value.data, in.value.str_value.len);
49 }
keunyounge18e25d2015-08-28 15:57:19 -070050 } break;
51 case VEHICLE_VALUE_TYPE_BYTES: {
keunyoung7d74e6d2015-10-14 15:43:10 -070052 if (in.value.bytes_value.len > 0) {
53 out.set_bytes_value(in.value.bytes_value.data, in.value.bytes_value.len);
54 }
keunyounge18e25d2015-08-28 15:57:19 -070055 } break;
Keun-young Parke4b186e2016-01-14 11:05:21 -080056 case VEHICLE_VALUE_TYPE_FLOAT:
keunyoungd32f4e62015-09-21 11:33:06 -070057 case VEHICLE_VALUE_TYPE_FLOAT_VEC2:
58 case VEHICLE_VALUE_TYPE_FLOAT_VEC3:
59 case VEHICLE_VALUE_TYPE_FLOAT_VEC4: {
Keun-young Parke4b186e2016-01-14 11:05:21 -080060 int expectedSize = in.value_type - VEHICLE_VALUE_TYPE_FLOAT + 1;
keunyoungd32f4e62015-09-21 11:33:06 -070061 for (int i = 0; i < expectedSize; i++) {
62 out.add_float_values(in.value.float_array[i]);
63 }
keunyounge18e25d2015-08-28 15:57:19 -070064 } break;
65 case VEHICLE_VALUE_TYPE_INT64: {
66 out.set_int64_value(in.value.int64_value);
67 } break;
Keun-young Park71b2f5c2016-03-10 18:44:40 -080068 case VEHICLE_VALUE_TYPE_BOOLEAN:
69 case VEHICLE_VALUE_TYPE_ZONED_BOOLEAN: {
keunyoungd32f4e62015-09-21 11:33:06 -070070 out.add_int32_values(in.value.int32_value);
71 } break;
Keun-young Parke4b186e2016-01-14 11:05:21 -080072 case VEHICLE_VALUE_TYPE_INT32:
keunyoungd32f4e62015-09-21 11:33:06 -070073 case VEHICLE_VALUE_TYPE_INT32_VEC2:
74 case VEHICLE_VALUE_TYPE_INT32_VEC3:
75 case VEHICLE_VALUE_TYPE_INT32_VEC4: {
Keun-young Parke4b186e2016-01-14 11:05:21 -080076 int expectedSize = in.value_type - VEHICLE_VALUE_TYPE_INT32 + 1;
keunyoungd32f4e62015-09-21 11:33:06 -070077 for (int i = 0; i < expectedSize; i++) {
78 out.add_int32_values(in.value.int32_array[i]);
79 }
keunyounge18e25d2015-08-28 15:57:19 -070080 } break;
81 case VEHICLE_VALUE_TYPE_ZONED_INT32:
Keun-young Parke4b186e2016-01-14 11:05:21 -080082 case VEHICLE_VALUE_TYPE_ZONED_INT32_VEC2:
Keun-young Park71b2f5c2016-03-10 18:44:40 -080083 case VEHICLE_VALUE_TYPE_ZONED_INT32_VEC3:
84 case VEHICLE_VALUE_TYPE_ZONED_INT32_VEC4: {
85 int expectedSize = in.value_type - VEHICLE_VALUE_TYPE_ZONED_INT32 + 1;
Keun-young Parke4b186e2016-01-14 11:05:21 -080086 for (int i = 0; i < expectedSize; i++) {
Keun-young Park71b2f5c2016-03-10 18:44:40 -080087 out.add_int32_values(in.value.int32_array[i]);
Keun-young Parke4b186e2016-01-14 11:05:21 -080088 }
keunyounge18e25d2015-08-28 15:57:19 -070089 } break;
Keun-young Parke4b186e2016-01-14 11:05:21 -080090 case VEHICLE_VALUE_TYPE_ZONED_FLOAT:
91 case VEHICLE_VALUE_TYPE_ZONED_FLOAT_VEC2:
Keun-young Park71b2f5c2016-03-10 18:44:40 -080092 case VEHICLE_VALUE_TYPE_ZONED_FLOAT_VEC3:
93 case VEHICLE_VALUE_TYPE_ZONED_FLOAT_VEC4: {
Keun-young Parke4b186e2016-01-14 11:05:21 -080094 int expectedSize = in.value_type - VEHICLE_VALUE_TYPE_ZONED_FLOAT + 1;
Keun-young Parke4b186e2016-01-14 11:05:21 -080095 for (int i = 0; i < expectedSize; i++) {
Keun-young Park71b2f5c2016-03-10 18:44:40 -080096 out.add_float_values(in.value.float_array[i]);
Keun-young Parke4b186e2016-01-14 11:05:21 -080097 }
keunyounge18e25d2015-08-28 15:57:19 -070098 } break;
99 }
100 return NO_ERROR;
101}
102
103status_t VehicleNetworkProtoUtil::fromVehiclePropValue(const VehiclePropValue& in,
keunyoung7d74e6d2015-10-14 15:43:10 -0700104 vehicle_prop_value_t& out, bool /*inPlace*/, bool canIgnoreNoData) {
keunyounge18e25d2015-08-28 15:57:19 -0700105 out.prop = in.prop();
106 out.value_type = in.value_type();
107 out.timestamp = in.timestamp();
Keun-young Park71b2f5c2016-03-10 18:44:40 -0800108 out.zone = in.zone();
keunyounge18e25d2015-08-28 15:57:19 -0700109 switch (out.value_type) {
110 case VEHICLE_VALUE_TYPE_STRING: {
111 if (!in.has_string_value()) {
112 // set to NULL so that client can just delete this safely.
113 out.value.str_value.data = NULL;
114 out.value.str_value.len = 0;
keunyoung7d74e6d2015-10-14 15:43:10 -0700115 if (canIgnoreNoData) {
116 return NO_ERROR;
117 } else {
118 ALOGE("fromVehiclePropValue, no string data");
119 return BAD_VALUE;
120 }
keunyounge18e25d2015-08-28 15:57:19 -0700121 }
122 //TODO fix copy...
123 status_t r = copyString(in.string_value(), &(out.value.str_value.data),
124 &(out.value.str_value.len));
125 if (r != NO_ERROR) {
126 out.value.str_value.data = NULL;
127 out.value.str_value.len = 0;
keunyoung7d74e6d2015-10-14 15:43:10 -0700128 ALOGE("copyString for string failed %d", r);
keunyounge18e25d2015-08-28 15:57:19 -0700129 return r;
130 }
131 } break;
132 case VEHICLE_VALUE_TYPE_BYTES: {
133 if (!in.has_bytes_value()) {
134 out.value.bytes_value.data = NULL;
135 out.value.bytes_value.len = 0;
keunyoung7d74e6d2015-10-14 15:43:10 -0700136 if (canIgnoreNoData) {
137 return NO_ERROR;
138 } else {
139 ALOGE("fromVehiclePropValue, no byte data");
140 return BAD_VALUE;
141 }
keunyounge18e25d2015-08-28 15:57:19 -0700142 }
143 status_t r = copyString(in.bytes_value(), &(out.value.bytes_value.data),
144 &(out.value.bytes_value.len));
145 if (r != NO_ERROR) {
146 out.value.bytes_value.data = NULL;
147 out.value.bytes_value.len = 0;
keunyoung7d74e6d2015-10-14 15:43:10 -0700148 ALOGE("copyString for bytes failed %d", r);
keunyounge18e25d2015-08-28 15:57:19 -0700149 return r;
150 }
151 } break;
Keun-young Park71b2f5c2016-03-10 18:44:40 -0800152 case VEHICLE_VALUE_TYPE_FLOAT:
keunyoungd32f4e62015-09-21 11:33:06 -0700153 case VEHICLE_VALUE_TYPE_FLOAT_VEC2:
154 case VEHICLE_VALUE_TYPE_FLOAT_VEC3:
155 case VEHICLE_VALUE_TYPE_FLOAT_VEC4: {
Keun-young Park71b2f5c2016-03-10 18:44:40 -0800156 int expectedSize = out.value_type - VEHICLE_VALUE_TYPE_FLOAT + 1;
keunyoungd32f4e62015-09-21 11:33:06 -0700157 if (in.float_values_size() != expectedSize) {
keunyoung7d74e6d2015-10-14 15:43:10 -0700158 if (canIgnoreNoData) {
159 return NO_ERROR;
160 }
keunyoungd32f4e62015-09-21 11:33:06 -0700161 ALOGE("float value, wrong size %d, expecting %d", in.float_values_size(),
162 expectedSize);
163 return BAD_VALUE;
164 }
165 for (int i = 0; i < expectedSize; i++) {
166 out.value.float_array[i] = in.float_values(i);
167 }
keunyounge18e25d2015-08-28 15:57:19 -0700168 } break;
169 case VEHICLE_VALUE_TYPE_INT64: {
170 if (!in.has_int64_value()) {
keunyoung7d74e6d2015-10-14 15:43:10 -0700171 if (canIgnoreNoData) {
172 return NO_ERROR;
173 }
keunyoungd32f4e62015-09-21 11:33:06 -0700174 ALOGE("no int64 value");
keunyounge18e25d2015-08-28 15:57:19 -0700175 return BAD_VALUE;
176 }
177 out.value.int64_value = in.int64_value();
178 } break;
Keun-young Park71b2f5c2016-03-10 18:44:40 -0800179 case VEHICLE_VALUE_TYPE_BOOLEAN:
180 case VEHICLE_VALUE_TYPE_ZONED_BOOLEAN: {
keunyoungd32f4e62015-09-21 11:33:06 -0700181 if (in.int32_values_size() != 1) {
keunyoung7d74e6d2015-10-14 15:43:10 -0700182 if (canIgnoreNoData) {
183 return NO_ERROR;
184 }
keunyoungd32f4e62015-09-21 11:33:06 -0700185 ALOGE("no int32 value");
keunyounge18e25d2015-08-28 15:57:19 -0700186 return BAD_VALUE;
187 }
keunyoungd32f4e62015-09-21 11:33:06 -0700188 out.value.int32_value = in.int32_values(0);
189 } break;
Keun-young Parke4b186e2016-01-14 11:05:21 -0800190 case VEHICLE_VALUE_TYPE_INT32:
keunyoungd32f4e62015-09-21 11:33:06 -0700191 case VEHICLE_VALUE_TYPE_INT32_VEC2:
192 case VEHICLE_VALUE_TYPE_INT32_VEC3:
193 case VEHICLE_VALUE_TYPE_INT32_VEC4: {
Keun-young Parke4b186e2016-01-14 11:05:21 -0800194 int expectedSize = out.value_type - VEHICLE_VALUE_TYPE_INT32 + 1;
keunyoungd32f4e62015-09-21 11:33:06 -0700195 if (in.int32_values_size() != expectedSize) {
keunyoung7d74e6d2015-10-14 15:43:10 -0700196 if (canIgnoreNoData) {
197 return NO_ERROR;
198 }
keunyoungd32f4e62015-09-21 11:33:06 -0700199 ALOGE("int32 value, wrong size %d, expecting %d", in.int32_values_size(),
200 expectedSize);
201 return BAD_VALUE;
202 }
203 for (int i = 0; i < expectedSize; i++) {
204 out.value.int32_array[i] = in.int32_values(i);
205 }
keunyounge18e25d2015-08-28 15:57:19 -0700206 } break;
Keun-young Park71b2f5c2016-03-10 18:44:40 -0800207
keunyounge18e25d2015-08-28 15:57:19 -0700208 case VEHICLE_VALUE_TYPE_ZONED_INT32:
Keun-young Parke4b186e2016-01-14 11:05:21 -0800209 case VEHICLE_VALUE_TYPE_ZONED_INT32_VEC2:
Keun-young Park71b2f5c2016-03-10 18:44:40 -0800210 case VEHICLE_VALUE_TYPE_ZONED_INT32_VEC3:
211 case VEHICLE_VALUE_TYPE_ZONED_INT32_VEC4: {
212 int expectedSize = out.value_type - VEHICLE_VALUE_TYPE_ZONED_INT32 + 1;
213 if (in.int32_values_size() != expectedSize) {
keunyoung7d74e6d2015-10-14 15:43:10 -0700214 if (canIgnoreNoData) {
215 return NO_ERROR;
216 }
Keun-young Park71b2f5c2016-03-10 18:44:40 -0800217 ALOGE("int32 value, wrong size %d, expecting %d", in.int32_values_size(),
218 expectedSize);
keunyounge18e25d2015-08-28 15:57:19 -0700219 return BAD_VALUE;
220 }
Keun-young Parke4b186e2016-01-14 11:05:21 -0800221 for (int i = 0; i < expectedSize; i++) {
Keun-young Park71b2f5c2016-03-10 18:44:40 -0800222 out.value.int32_array[i] = in.int32_values(i);
Keun-young Parke4b186e2016-01-14 11:05:21 -0800223 }
keunyounge18e25d2015-08-28 15:57:19 -0700224 } break;
Keun-young Parke4b186e2016-01-14 11:05:21 -0800225 case VEHICLE_VALUE_TYPE_ZONED_FLOAT:
226 case VEHICLE_VALUE_TYPE_ZONED_FLOAT_VEC2:
Keun-young Park71b2f5c2016-03-10 18:44:40 -0800227 case VEHICLE_VALUE_TYPE_ZONED_FLOAT_VEC3:
228 case VEHICLE_VALUE_TYPE_ZONED_FLOAT_VEC4:{
Keun-young Parke4b186e2016-01-14 11:05:21 -0800229 int expectedSize = out.value_type - VEHICLE_VALUE_TYPE_ZONED_FLOAT + 1;
Keun-young Park71b2f5c2016-03-10 18:44:40 -0800230 if (in.float_values_size() != expectedSize) {
keunyoung7d74e6d2015-10-14 15:43:10 -0700231 if (canIgnoreNoData) {
232 return NO_ERROR;
233 }
Keun-young Park71b2f5c2016-03-10 18:44:40 -0800234 ALOGE("float value, wrong size %d, expecting %d", in.float_values_size(),
235 expectedSize);
keunyounge18e25d2015-08-28 15:57:19 -0700236 return BAD_VALUE;
237 }
Keun-young Parke4b186e2016-01-14 11:05:21 -0800238 for (int i = 0; i < expectedSize; i++) {
Keun-young Park71b2f5c2016-03-10 18:44:40 -0800239 out.value.float_array[i] = in.float_values(i);
Keun-young Parke4b186e2016-01-14 11:05:21 -0800240 }
keunyounge18e25d2015-08-28 15:57:19 -0700241 } break;
keunyoung7d74e6d2015-10-14 15:43:10 -0700242 default: {
243 if (canIgnoreNoData) {
244 return NO_ERROR;
245 }
246 ALOGE("fromVehiclePropValue unknown type 0x%x", out.value_type);
keunyounge18e25d2015-08-28 15:57:19 -0700247 return BAD_VALUE;
keunyoung7d74e6d2015-10-14 15:43:10 -0700248 }
keunyounge18e25d2015-08-28 15:57:19 -0700249 }
250 return NO_ERROR;
251}
252
253status_t VehicleNetworkProtoUtil::toVehiclePropValues(const List<vehicle_prop_value_t*>& in,
254 VehiclePropValues& out) {
255 status_t r;
256 for (auto& v : in) {
257 VehiclePropValue* value = out.add_values();
258 r = toVehiclePropValue(*v, *value);
259 if (r != NO_ERROR) {
260 out.clear_values();
261 return r;
262 }
263 }
264 return NO_ERROR;
265}
266
267status_t VehicleNetworkProtoUtil::fromVehiclePropValues(const VehiclePropValues& in,
268 List<vehicle_prop_value_t*>& out) {
269 status_t r;
270 for (int i = 0; i < in.values_size(); i++) {
271 vehicle_prop_value_t* v = new vehicle_prop_value_t();
272 memset(v, 0, sizeof(vehicle_prop_value_t));
273 ASSERT_OR_HANDLE_NO_MEMORY(v, r = NO_MEMORY;goto error);
Keun-young Park5af1fda2015-12-08 18:15:27 -0800274 r = fromVehiclePropValue(in.values(i), *v);
keunyounge18e25d2015-08-28 15:57:19 -0700275 if (r != NO_ERROR) {
276 delete v;
277 goto error;
278 }
279 out.push_back(v);
280 }
281 return NO_ERROR;
282error:
283 // clean up everything in List
284 for (auto pv : out) {
285 VehiclePropValueUtil::deleteMembers(pv);
286 }
287 return r;
288}
289
290status_t VehicleNetworkProtoUtil::toVehiclePropConfig(const vehicle_prop_config_t& in,
291 VehiclePropConfig& out) {
292 out.set_prop(in.prop);
293 out.set_access(in.access);
294 out.set_change_mode(in.change_mode);
295 out.set_value_type(in.value_type);
296 out.set_permission_model(in.permission_model);
Keun-young Park71b2f5c2016-03-10 18:44:40 -0800297 out.set_zones(in.vehicle_zone_flags);
Keun-young Park0727f952015-12-21 14:30:07 -0800298 for (unsigned int i = 0; i < sizeof(in.config_array) / sizeof(int32_t); i++) {
299 out.add_config_array(in.config_array[i]);
300 }
keunyounge18e25d2015-08-28 15:57:19 -0700301 if (in.config_string.data != NULL && in.config_string.len != 0) {
302 out.set_config_string((char*)in.config_string.data, in.config_string.len);
303 } else {
304 out.clear_config_string();
305 }
Keun-young Park7d3fd012016-05-11 14:54:50 -0700306 int numZones = VehicleNetworkUtil::countNumberOfZones(in.vehicle_zone_flags);
307 if (numZones == 0) {
308 numZones = 1;
309 }
keunyounge18e25d2015-08-28 15:57:19 -0700310 switch (in.value_type) {
Keun-young Park7d3fd012016-05-11 14:54:50 -0700311 case VEHICLE_VALUE_TYPE_ZONED_FLOAT:
312 case VEHICLE_VALUE_TYPE_ZONED_FLOAT_VEC2:
313 case VEHICLE_VALUE_TYPE_ZONED_FLOAT_VEC3:
314 case VEHICLE_VALUE_TYPE_ZONED_FLOAT_VEC4:
keunyounge18e25d2015-08-28 15:57:19 -0700315 case VEHICLE_VALUE_TYPE_FLOAT:
Keun-young Park450faba2016-02-10 18:15:12 -0800316 case VEHICLE_VALUE_TYPE_FLOAT_VEC2:
317 case VEHICLE_VALUE_TYPE_FLOAT_VEC3:
Keun-young Park71b2f5c2016-03-10 18:44:40 -0800318 case VEHICLE_VALUE_TYPE_FLOAT_VEC4: {
Keun-young Park71b2f5c2016-03-10 18:44:40 -0800319 if (in.float_min_values == NULL) {
320 if (in.float_max_values == NULL) {
321 // all the same min/max
322 for (int i = 0; i < numZones; i++) {
323 out.add_float_maxs(in.float_max_value);
324 out.add_float_mins(in.float_min_value);
325 }
326 } else { // invalid combination
327 ALOGW("Zoned property 0x%x, min_values NULL while max_values not NULL",
328 in.prop);
329 return BAD_VALUE;
330 }
331 } else {
332 if (in.float_max_values != NULL) {
333 for (int i = 0; i < numZones; i++) {
334 out.add_float_maxs(in.float_max_values[i]);
335 out.add_float_mins(in.float_min_values[i]);
336 }
337 } else { // invalid combination
338 ALOGW("Zoned property 0x%x, max_values NULL while min_values not NULL",
339 in.prop);
340 return BAD_VALUE;
341 }
342 }
keunyounge18e25d2015-08-28 15:57:19 -0700343 } break;
344 case VEHICLE_VALUE_TYPE_INT64: {
Keun-young Park7d3fd012016-05-11 14:54:50 -0700345 if (in.int64_min_values == NULL) {
346 if (in.int64_max_values == NULL) {
347 // all the same min/max
348 for (int i = 0; i < numZones; i++) {
349 out.add_int64_maxs(in.int64_max_value);
350 out.add_int64_mins(in.int64_min_value);
351 }
352 } else { // invalid combination
353 ALOGW("Zoned property 0x%x, min_values NULL while max_values not NULL",
354 in.prop);
355 return BAD_VALUE;
356 }
357 } else {
358 if (in.int64_max_values != NULL) {
359 for (int i = 0; i < numZones; i++) {
360 out.add_int64_maxs(in.int64_max_values[i]);
361 out.add_int64_mins(in.int64_min_values[i]);
362 }
363 } else { // invalid combination
364 ALOGW("Zoned property 0x%x, max_values NULL while min_values not NULL",
365 in.prop);
366 return BAD_VALUE;
367 }
368 }
Keun-young Park71b2f5c2016-03-10 18:44:40 -0800369 } break;
Keun-young Park450faba2016-02-10 18:15:12 -0800370 case VEHICLE_VALUE_TYPE_ZONED_INT32:
371 case VEHICLE_VALUE_TYPE_ZONED_INT32_VEC2:
Keun-young Park71b2f5c2016-03-10 18:44:40 -0800372 case VEHICLE_VALUE_TYPE_ZONED_INT32_VEC3:
Keun-young Park7d3fd012016-05-11 14:54:50 -0700373 case VEHICLE_VALUE_TYPE_ZONED_INT32_VEC4:
374 case VEHICLE_VALUE_TYPE_INT32:
375 case VEHICLE_VALUE_TYPE_INT32_VEC2:
376 case VEHICLE_VALUE_TYPE_INT32_VEC3:
377 case VEHICLE_VALUE_TYPE_INT32_VEC4: {
Keun-young Park71b2f5c2016-03-10 18:44:40 -0800378 if (in.int32_min_values == NULL) {
379 if (in.int32_max_values == NULL) {
380 // all the same min/max
381 for (int i = 0; i < numZones; i++) {
382 out.add_int32_maxs(in.int32_max_value);
383 out.add_int32_mins(in.int32_min_value);
384 }
385 } else { // invalid combination
386 ALOGW("Zoned property 0x%x, min_values NULL while max_values not NULL",
387 in.prop);
388 return BAD_VALUE;
389 }
390 } else {
391 if (in.int32_max_values != NULL) {
392 for (int i = 0; i < numZones; i++) {
393 out.add_int32_maxs(in.int32_max_values[i]);
394 out.add_int32_mins(in.int32_min_values[i]);
395 }
396 } else { // invalid combination
397 ALOGW("Zoned property 0x%x, max_values NULL while min_values not NULL",
398 in.prop);
399 return BAD_VALUE;
400 }
401 }
keunyounge18e25d2015-08-28 15:57:19 -0700402 } break;
403 }
404 out.set_sample_rate_max(in.max_sample_rate);
405 out.set_sample_rate_min(in.min_sample_rate);
406 return NO_ERROR;
407}
408
409status_t VehicleNetworkProtoUtil::fromVehiclePropConfig(const VehiclePropConfig& in,
410 vehicle_prop_config_t& out) {
411 out.prop = in.prop();
412 out.access = in.access();
413 out.change_mode = in.change_mode();
414 out.value_type = in.value_type();
415 out.permission_model = in.permission_model();
Keun-young Park71b2f5c2016-03-10 18:44:40 -0800416 out.vehicle_zone_flags = in.zones();
Keun-young Park0727f952015-12-21 14:30:07 -0800417 int maxConfigSize = sizeof(out.config_array) / sizeof(int32_t);
418 int configSize = in.config_array_size();
419 if (configSize > maxConfigSize) {
420 return BAD_VALUE;
421 }
422 int i = 0;
423 for (; i < configSize; i++) {
424 out.config_array[i] = in.config_array(i);
425 }
426 for (; i < maxConfigSize; i++) {
427 out.config_array[i] = 0;
428 }
keunyounge18e25d2015-08-28 15:57:19 -0700429 if (in.has_config_string()) {
430 status_t r = copyString(in.config_string(), &(out.config_string.data),
431 &(out.config_string.len));
432 if (r != NO_ERROR) {
433 return r;
434 }
435 } else {
436 out.config_string.data = NULL;
437 out.config_string.len = 0;
438 }
Keun-young Park7d3fd012016-05-11 14:54:50 -0700439 int numZones = VehicleNetworkUtil::countNumberOfZones(out.vehicle_zone_flags);
440 if (numZones == 0) {
441 numZones = 1;
442 }
keunyounge18e25d2015-08-28 15:57:19 -0700443 switch (out.value_type) {
Keun-young Park7d3fd012016-05-11 14:54:50 -0700444 case VEHICLE_VALUE_TYPE_ZONED_FLOAT:
445 case VEHICLE_VALUE_TYPE_ZONED_FLOAT_VEC2:
446 case VEHICLE_VALUE_TYPE_ZONED_FLOAT_VEC3:
447 case VEHICLE_VALUE_TYPE_ZONED_FLOAT_VEC4:
keunyounge18e25d2015-08-28 15:57:19 -0700448 case VEHICLE_VALUE_TYPE_FLOAT:
Keun-young Park450faba2016-02-10 18:15:12 -0800449 case VEHICLE_VALUE_TYPE_FLOAT_VEC2:
450 case VEHICLE_VALUE_TYPE_FLOAT_VEC3:
Keun-young Park71b2f5c2016-03-10 18:44:40 -0800451 case VEHICLE_VALUE_TYPE_FLOAT_VEC4: {
Keun-young Park71b2f5c2016-03-10 18:44:40 -0800452 int maxSize = in.float_maxs_size();
453 int minSize = in.float_mins_size();
454 if (maxSize != minSize) {
455 ALOGW("Zoned property 0x%x, config maxSize %d minSize %d", out.prop, maxSize,
456 minSize);
457 return BAD_VALUE;
458 }
459 if (maxSize == 0) {
460 out.float_max_value = 0;
461 out.float_min_value = 0;
462 out.float_max_values = NULL;
463 out.float_min_values = NULL;
464 } else if (maxSize == 1) { // one for all
465 out.float_max_value = in.float_maxs(0);
466 out.float_min_value = in.float_mins(0);
467 out.float_max_values = NULL;
468 out.float_min_values = NULL;
Keun-young Park7d3fd012016-05-11 14:54:50 -0700469 } else {
470 if ((numZones > 1) && (numZones != maxSize)) {
Keun-young Park71b2f5c2016-03-10 18:44:40 -0800471 ALOGW("Zoned property 0x%x, config maxSize %d num Zones %d", out.prop, maxSize,
472 numZones);
473 return BAD_VALUE;
474 }
Keun-young Park7d3fd012016-05-11 14:54:50 -0700475 out.float_max_values = new float[maxSize];
Keun-young Park71b2f5c2016-03-10 18:44:40 -0800476 ASSERT_OR_HANDLE_NO_MEMORY(out.float_max_values, return NO_MEMORY);
Keun-young Park7d3fd012016-05-11 14:54:50 -0700477 out.float_min_values = new float[maxSize];
Keun-young Park71b2f5c2016-03-10 18:44:40 -0800478 ASSERT_OR_HANDLE_NO_MEMORY(out.float_min_values, return NO_MEMORY);
Keun-young Park7d3fd012016-05-11 14:54:50 -0700479 for (int i = 0; i < maxSize; i++) {
Keun-young Park71b2f5c2016-03-10 18:44:40 -0800480 out.float_max_values[i] = in.float_maxs(i);
481 out.float_min_values[i] = in.float_mins(i);
482 }
483 }
484 } break;
keunyounge18e25d2015-08-28 15:57:19 -0700485 case VEHICLE_VALUE_TYPE_INT64: {
Keun-young Park7d3fd012016-05-11 14:54:50 -0700486 int maxSize = in.int64_maxs_size();
487 int minSize = in.int64_mins_size();
488 if (maxSize != minSize) {
489 ALOGW("Zoned property 0x%x, config maxSize %d minSize %d", out.prop, maxSize,
490 minSize);
491 return BAD_VALUE;
492 }
493 if (maxSize == 0) {
keunyoung1ab8e182015-09-24 09:25:22 -0700494 out.int64_max_value = 0;
495 out.int64_min_value = 0;
Keun-young Park7d3fd012016-05-11 14:54:50 -0700496 out.int64_max_values = NULL;
497 out.int64_min_values = NULL;
498 } else if (maxSize == 1) { // one for all
499 out.int64_max_value = in.int64_maxs(0);
500 out.int64_min_value = in.int64_mins(0);
501 out.int64_max_values = NULL;
502 out.int64_min_values = NULL;
keunyounge18e25d2015-08-28 15:57:19 -0700503 } else {
Keun-young Park7d3fd012016-05-11 14:54:50 -0700504 if ((numZones > 1) && (numZones != maxSize)) {
505 ALOGW("Zoned property 0x%x, config maxSize %d num Zones %d", out.prop, maxSize,
506 numZones);
507 return BAD_VALUE;
508 }
509 out.int64_max_values = new int64_t[maxSize];
510 ASSERT_OR_HANDLE_NO_MEMORY(out.int64_max_values, return NO_MEMORY);
511 out.int64_min_values = new int64_t[maxSize];
512 ASSERT_OR_HANDLE_NO_MEMORY(out.int64_min_values, return NO_MEMORY);
513 for (int i = 0; i < maxSize; i++) {
514 out.int64_max_values[i] = in.int64_maxs(i);
515 out.int64_min_values[i] = in.int64_mins(i);
516 }
keunyounge18e25d2015-08-28 15:57:19 -0700517 }
518 } break;
Keun-young Park71b2f5c2016-03-10 18:44:40 -0800519 case VEHICLE_VALUE_TYPE_ZONED_INT32:
520 case VEHICLE_VALUE_TYPE_ZONED_INT32_VEC2:
521 case VEHICLE_VALUE_TYPE_ZONED_INT32_VEC3:
Keun-young Park7d3fd012016-05-11 14:54:50 -0700522 case VEHICLE_VALUE_TYPE_ZONED_INT32_VEC4:
523 case VEHICLE_VALUE_TYPE_INT32:
524 case VEHICLE_VALUE_TYPE_INT32_VEC2:
525 case VEHICLE_VALUE_TYPE_INT32_VEC3:
526 case VEHICLE_VALUE_TYPE_INT32_VEC4: {
Keun-young Park71b2f5c2016-03-10 18:44:40 -0800527 int maxSize = in.int32_maxs_size();
528 int minSize = in.int32_mins_size();
529 if (maxSize != minSize) {
530 ALOGW("Zoned property 0x%x, config maxSize %d minSize %d", out.prop, maxSize,
531 minSize);
532 return BAD_VALUE;
533 }
534 if (maxSize == 0) {
535 out.int32_max_value = 0;
536 out.int32_min_value = 0;
537 out.int32_max_values = NULL;
538 out.int32_min_values = NULL;
539 } else if (maxSize == 1) { // one for all
540 out.int32_max_value = in.int32_maxs(0);
541 out.int32_min_value = in.int32_mins(0);
542 out.int32_max_values = NULL;
543 out.int32_min_values = NULL;
Keun-young Park7d3fd012016-05-11 14:54:50 -0700544 } else {
545 if ((numZones > 1) && (numZones != maxSize)) {
Keun-young Park71b2f5c2016-03-10 18:44:40 -0800546 ALOGW("Zoned property 0x%x, config maxSize %d num Zones %d", out.prop, maxSize,
547 numZones);
548 return BAD_VALUE;
549 }
Keun-young Park7d3fd012016-05-11 14:54:50 -0700550 out.int32_max_values = new int32_t[maxSize];
Keun-young Park71b2f5c2016-03-10 18:44:40 -0800551 ASSERT_OR_HANDLE_NO_MEMORY(out.int32_max_values, return NO_MEMORY);
Keun-young Park7d3fd012016-05-11 14:54:50 -0700552 out.int32_min_values = new int32_t[maxSize];
Keun-young Park71b2f5c2016-03-10 18:44:40 -0800553 ASSERT_OR_HANDLE_NO_MEMORY(out.int32_min_values, return NO_MEMORY);
Keun-young Park7d3fd012016-05-11 14:54:50 -0700554 for (int i = 0; i < maxSize; i++) {
Keun-young Park71b2f5c2016-03-10 18:44:40 -0800555 out.int32_max_values[i] = in.int32_maxs(i);
556 out.int32_min_values[i] = in.int32_mins(i);
557 }
558 }
559 } break;
keunyounge18e25d2015-08-28 15:57:19 -0700560 }
561 out.max_sample_rate = in.sample_rate_max();
562 out.min_sample_rate = in.sample_rate_min();
563 return NO_ERROR;
564}
565
keunyoungd32f4e62015-09-21 11:33:06 -0700566status_t VehicleNetworkProtoUtil::toVehiclePropConfigs(List<vehicle_prop_config_t const*> &in,
567 VehiclePropConfigs& out) {
keunyounge18e25d2015-08-28 15:57:19 -0700568 status_t r;
keunyoungd32f4e62015-09-21 11:33:06 -0700569 for (auto& inEntry : in) {
keunyounge18e25d2015-08-28 15:57:19 -0700570 VehiclePropConfig* config = out.add_configs();
keunyoungd32f4e62015-09-21 11:33:06 -0700571 r = toVehiclePropConfig(*inEntry, *config);
keunyounge18e25d2015-08-28 15:57:19 -0700572 if (r != NO_ERROR) {
573 out.clear_configs();
574 return r;
575 }
576 }
577 return NO_ERROR;
578}
579
580status_t VehicleNetworkProtoUtil::fromVehiclePropConfigs(const VehiclePropConfigs& in,
keunyoungd32f4e62015-09-21 11:33:06 -0700581 List<vehicle_prop_config_t const*>& out) {
keunyounge18e25d2015-08-28 15:57:19 -0700582 int32_t n = in.configs_size();
keunyounge18e25d2015-08-28 15:57:19 -0700583 status_t r;
584 for (int32_t i = 0; i < n; i++) {
keunyoungd32f4e62015-09-21 11:33:06 -0700585 vehicle_prop_config_t* entry = new vehicle_prop_config_t();
586 ASSERT_OR_HANDLE_NO_MEMORY(entry, r = NO_MEMORY; goto error);
587 memset(entry, 0, sizeof(vehicle_prop_config_t));
588 r = fromVehiclePropConfig(in.configs(i), *entry);
keunyounge18e25d2015-08-28 15:57:19 -0700589 if (r != NO_ERROR) {
keunyoungd32f4e62015-09-21 11:33:06 -0700590 goto error;
keunyounge18e25d2015-08-28 15:57:19 -0700591 }
keunyoungd32f4e62015-09-21 11:33:06 -0700592 out.push_back(entry);
keunyounge18e25d2015-08-28 15:57:19 -0700593 }
594 return NO_ERROR;
keunyoungd32f4e62015-09-21 11:33:06 -0700595error:
596 for (auto& e : out) {
597 vehicle_prop_config_t* eDelete = const_cast<vehicle_prop_config_t*>(e);
598 VehiclePropertiesUtil::deleteMembers(eDelete);
599 delete eDelete;
600 }
601 out.clear();
602 return r;
keunyounge18e25d2015-08-28 15:57:19 -0700603}
604
keunyoung7d74e6d2015-10-14 15:43:10 -0700605status_t VehiclePropValueBinderUtil::writeToParcel(Parcel& parcel,
606 const vehicle_prop_value_t& value) {
607 parcel.writeInt32(1); // 0 means no value. For compatibility with aidl based code.
608 std::unique_ptr<VehiclePropValue> v(new VehiclePropValue());
609 ASSERT_OR_HANDLE_NO_MEMORY(v.get(), return NO_MEMORY);
610 VehicleNetworkProtoUtil::toVehiclePropValue(value, *v.get());
611 int size = v->ByteSize();
612 WritableBlobHolder blob(new Parcel::WritableBlob());
613 ASSERT_OR_HANDLE_NO_MEMORY(blob.blob, return NO_MEMORY);
614 parcel.writeInt32(size);
615 parcel.writeBlob(size, false, blob.blob);
616 v->SerializeToArray(blob.blob->data(), size);
617 return NO_ERROR;
618}
619
620status_t VehiclePropValueBinderUtil::readFromParcel(const Parcel& parcel,
621 vehicle_prop_value_t* value, bool deleteMembers, bool canIgnoreNoData) {
622 if (parcel.readInt32() == 0) { // no result
623 ALOGE("readFromParcel, null data");
624 return BAD_VALUE;
625 }
626 ReadableBlobHolder blob(new Parcel::ReadableBlob());
627 ASSERT_OR_HANDLE_NO_MEMORY(blob.blob, return NO_MEMORY);
628 int32_t size = parcel.readInt32();
Keun-young Park20539ba2016-10-10 17:57:34 -0700629 if (size < 0) {
630 ALOGE("readFromParcel, bad blob size %d", size);
631 return BAD_VALUE;
632 }
keunyoung7d74e6d2015-10-14 15:43:10 -0700633 status_t status = parcel.readBlob(size, blob.blob);
634 if (status != NO_ERROR) {
635 ALOGE("readFromParcel, cannot read blob");
636 return status;
637 }
638 std::unique_ptr<VehiclePropValue> v(new VehiclePropValue());
639 ASSERT_OR_HANDLE_NO_MEMORY(v.get(), return NO_MEMORY);
640 if (!v->ParseFromArray(blob.blob->data(), size)) {
641 ALOGE("readFromParcel, cannot parse");
642 return BAD_VALUE;
643 }
644 if (deleteMembers) {
645 VehiclePropValueUtil::deleteMembers(value);
646 }
647 return VehicleNetworkProtoUtil::fromVehiclePropValue(*v.get(), *value, false /*inPlace*/,
648 canIgnoreNoData);
649}
650
keunyounge18e25d2015-08-28 15:57:19 -0700651}; //namespace android
652