Home | History | Annotate | Download | only in src
      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 (at) 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 
     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", type);
     56             val.reset(nullptr);
     57     }
     58     return val;
     59 }
     60 
     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 
     81 template<typename T>
     82 inline void copyHidlVec(hidl_vec <T>* dest, const hidl_vec <T>& src) {
     83     for (size_t i = 0; i < std::min(dest->size(), src.size()); i++) {
     84         (*dest)[i] = src[i];
     85     }
     86 }
     87 
     88 void copyVehicleRawValue(VehiclePropValue::RawValue* dest,
     89                          const VehiclePropValue::RawValue& src) {
     90     dest->int32Values = src.int32Values;
     91     dest->floatValues = src.floatValues;
     92     dest->int64Values = src.int64Values;
     93     dest->bytes = src.bytes;
     94     dest->stringValue = src.stringValue;
     95 }
     96 
     97 template<typename T>
     98 void shallowCopyHidlVec(hidl_vec <T>* dest, const hidl_vec <T>& src) {
     99     if (src.size() > 0) {
    100         dest->setToExternal(const_cast<T*>(&src[0]), src.size());
    101     } else if (dest->size() > 0) {
    102         dest->resize(0);
    103     }
    104 }
    105 
    106 void shallowCopyHidlStr(hidl_string* dest, const hidl_string& src) {
    107     if (src.empty()) {
    108         dest->clear();
    109     } else {
    110         dest->setToExternal(src.c_str(), src.size());
    111     }
    112 }
    113 
    114 void shallowCopy(VehiclePropValue* dest, const VehiclePropValue& src) {
    115     dest->prop = src.prop;
    116     dest->areaId = src.areaId;
    117     dest->status = src.status;
    118     dest->timestamp = src.timestamp;
    119     shallowCopyHidlVec(&dest->value.int32Values, src.value.int32Values);
    120     shallowCopyHidlVec(&dest->value.int64Values, src.value.int64Values);
    121     shallowCopyHidlVec(&dest->value.floatValues, src.value.floatValues);
    122     shallowCopyHidlVec(&dest->value.bytes, src.value.bytes);
    123     shallowCopyHidlStr(&dest->value.stringValue, src.value.stringValue);
    124 }
    125 
    126 
    127 //}  // namespace utils
    128 
    129 }  // namespace V2_0
    130 }  // namespace vehicle
    131 }  // namespace automotive
    132 }  // namespace hardware
    133 }  // namespace android
    134