diff options
| author | Pavel Maltsev <pavelm@google.com> | 2016-12-05 11:03:52 -0800 |
|---|---|---|
| committer | Pavel Maltsev <pavelm@google.com> | 2016-12-09 11:26:05 -0800 |
| commit | 0e0a925aea98a63120e3ce81e6f9ca83441a7b1f (patch) | |
| tree | 9c9ff0b12f7d85946f5c9b4b1799ef2fc1b2e9fd /vehicle | |
| parent | f8c117d424688c7db95fd35d389d55600549dbd6 (diff) | |
| download | android_hardware_interfaces-0e0a925aea98a63120e3ce81e6f9ca83441a7b1f.tar.gz android_hardware_interfaces-0e0a925aea98a63120e3ce81e6f9ca83441a7b1f.tar.bz2 android_hardware_interfaces-0e0a925aea98a63120e3ce81e6f9ca83441a7b1f.zip | |
Fixing logging in Vehicle HAL, code clean up
Test: this just refactoring, verified existing tests still passing
Change-Id: I70ad3928f9fe6b2f6c677b4d6848f263f3c25d11
Fix: b/33297409
Diffstat (limited to 'vehicle')
14 files changed, 478 insertions, 409 deletions
diff --git a/vehicle/2.0/default/Android.mk b/vehicle/2.0/default/Android.mk index e61aaa33c..4a27eeb48 100644 --- a/vehicle/2.0/default/Android.mk +++ b/vehicle/2.0/default/Android.mk @@ -25,13 +25,15 @@ LOCAL_SRC_FILES := \ vehicle_hal_manager/AccessControlConfigParser.cpp \ vehicle_hal_manager/SubscriptionManager.cpp \ vehicle_hal_manager/VehicleHalManager.cpp \ + vehicle_hal_manager/VehicleObjectPool.cpp \ + vehicle_hal_manager/VehicleUtils.cpp \ LOCAL_SHARED_LIBRARIES := \ - liblog \ libbinder \ libhidlbase \ libhidltransport \ libhwbinder \ + liblog \ libutils \ $(module_prefix) \ @@ -47,11 +49,11 @@ LOCAL_SRC_FILES:= \ impl/DefaultVehicleHal.cpp \ LOCAL_SHARED_LIBRARIES := \ - liblog \ libbinder \ libhidlbase \ libhidltransport \ libhwbinder \ + liblog \ libutils \ $(module_prefix) \ @@ -69,17 +71,17 @@ LOCAL_WHOLE_STATIC_LIBRARIES := $(module_prefix)-manager-lib LOCAL_SRC_FILES:= \ tests/AccessControlConfigParser_test.cpp \ - tests/VehicleObjectPool_test.cpp \ - tests/VehiclePropConfigIndex_test.cpp \ tests/SubscriptionManager_test.cpp \ tests/VehicleHalManager_test.cpp \ + tests/VehicleObjectPool_test.cpp \ + tests/VehiclePropConfigIndex_test.cpp \ LOCAL_SHARED_LIBRARIES := \ - liblog \ libbinder \ libhidlbase \ libhidltransport \ libhwbinder \ + liblog \ libutils \ $(module_prefix) \ @@ -105,11 +107,11 @@ LOCAL_WHOLE_STATIC_LIBRARIES := \ $(module_prefix)-default-impl-lib \ LOCAL_SHARED_LIBRARIES := \ - liblog \ libbinder \ libhidlbase \ libhidltransport \ libhwbinder \ + liblog \ libutils \ android.hardware.vehicle@2.0 diff --git a/vehicle/2.0/default/impl/DefaultConfig.h b/vehicle/2.0/default/impl/DefaultConfig.h index b1c49c884..e4ca5ca03 100644 --- a/vehicle/2.0/default/impl/DefaultConfig.h +++ b/vehicle/2.0/default/impl/DefaultConfig.h @@ -83,12 +83,13 @@ const VehiclePropConfig kVehicleProperties[] = { .changeMode = VehiclePropertyChangeMode::ON_CHANGE, .permissionModel = VehiclePermissionModel::NO_RESTRICTION, .supportedAreas = toInt(VehicleAreaZone::ROW_1), - .areaConfigs = init_hidl_vec({ - VehicleAreaConfig { - .areaId = toInt(VehicleAreaZone::ROW_1), - .minInt32Value = 1, - .maxInt32Value = 7 - }}) + .areaConfigs = { + VehicleAreaConfig { + .areaId = toInt(VehicleAreaZone::ROW_1), + .minInt32Value = 1, + .maxInt32Value = 7 + } + } }, { @@ -107,7 +108,7 @@ const VehiclePropConfig kVehicleProperties[] = { .supportedAreas = VehicleAreaZone::ROW_1_LEFT | VehicleAreaZone::ROW_1_RIGHT, - .areaConfigs = init_hidl_vec({ + .areaConfigs = { VehicleAreaConfig { .areaId = toInt(VehicleAreaZone::ROW_1_LEFT), .minFloatValue = 16, @@ -117,7 +118,8 @@ const VehiclePropConfig kVehicleProperties[] = { .areaId = toInt(VehicleAreaZone::ROW_1_RIGHT), .minFloatValue = 16, .maxFloatValue = 32, - }}) + } + } }, { @@ -146,12 +148,12 @@ const VehiclePropConfig kVehicleProperties[] = { .access = VehiclePropertyAccess::READ, .changeMode = VehiclePropertyChangeMode::ON_CHANGE, .permissionModel = VehiclePermissionModel::OEM_ONLY, - .areaConfigs = init_hidl_vec({ - VehicleAreaConfig { - .minFloatValue = 0, - .maxFloatValue = 1.0 - } - }) + .areaConfigs = { + VehicleAreaConfig { + .minFloatValue = 0, + .maxFloatValue = 1.0 + } + } }, { @@ -159,12 +161,12 @@ const VehiclePropConfig kVehicleProperties[] = { .access = VehiclePropertyAccess::READ_WRITE, .changeMode = VehiclePropertyChangeMode::ON_CHANGE, .permissionModel = VehiclePermissionModel::OEM_ONLY, - .areaConfigs = init_hidl_vec({ - VehicleAreaConfig { - .minInt32Value = 0, - .maxInt32Value = 10 - } - }) + .areaConfigs = { + VehicleAreaConfig { + .minInt32Value = 0, + .maxInt32Value = 10 + } + } } }; diff --git a/vehicle/2.0/default/tests/SubscriptionManager_test.cpp b/vehicle/2.0/default/tests/SubscriptionManager_test.cpp index 171a692b5..863142ef2 100644 --- a/vehicle/2.0/default/tests/SubscriptionManager_test.cpp +++ b/vehicle/2.0/default/tests/SubscriptionManager_test.cpp @@ -43,35 +43,32 @@ public: sp<IVehicleCallback> cb2 = new MockedVehicleCallback(); sp<IVehicleCallback> cb3 = new MockedVehicleCallback(); - hidl_vec<SubscribeOptions> subscrToProp1 = init_hidl_vec( - { - SubscribeOptions { - .propId = PROP1, - .vehicleAreas = toInt(VehicleAreaZone::ROW_1_LEFT), - .flags = SubscribeFlags::HAL_EVENT - }, - }); - - hidl_vec<SubscribeOptions> subscrToProp2 = init_hidl_vec( - { - SubscribeOptions { - .propId = PROP2, - .flags = SubscribeFlags::HAL_EVENT - }, - }); - - hidl_vec<SubscribeOptions> subscrToProp1and2 = init_hidl_vec( - { - SubscribeOptions { - .propId = PROP1, - .vehicleAreas = toInt(VehicleAreaZone::ROW_1_LEFT), - .flags = SubscribeFlags::HAL_EVENT - }, - SubscribeOptions { - .propId = PROP2, - .flags = SubscribeFlags::HAL_EVENT - }, - }); + hidl_vec<SubscribeOptions> subscrToProp1 = { + SubscribeOptions { + .propId = PROP1, + .vehicleAreas = toInt(VehicleAreaZone::ROW_1_LEFT), + .flags = SubscribeFlags::HAL_EVENT + }, + }; + + hidl_vec<SubscribeOptions> subscrToProp2 = { + SubscribeOptions { + .propId = PROP2, + .flags = SubscribeFlags::HAL_EVENT + }, + }; + + hidl_vec<SubscribeOptions> subscrToProp1and2 = { + SubscribeOptions { + .propId = PROP1, + .vehicleAreas = toInt(VehicleAreaZone::ROW_1_LEFT), + .flags = SubscribeFlags::HAL_EVENT + }, + SubscribeOptions { + .propId = PROP2, + .flags = SubscribeFlags::HAL_EVENT + }, + }; static std::list<sp<IVehicleCallback>> extractCallbacks( const std::list<sp<HalClient>>& clients) { @@ -144,14 +141,13 @@ TEST_F(SubscriptionManagerTest, mulipleSubscriptions) { // Same property, but different zone, to make sure we didn't unsubscribe // from previous zone. - manager.addOrUpdateSubscription(cb1, init_hidl_vec( - { - SubscribeOptions { + manager.addOrUpdateSubscription(cb1, { + SubscribeOptions { .propId = PROP1, .vehicleAreas = toInt(VehicleAreaZone::ROW_2), .flags = SubscribeFlags::DEFAULT } - })); + }); clients = manager.getSubscribedClients(PROP1, toInt(VehicleAreaZone::ROW_1_LEFT), diff --git a/vehicle/2.0/default/tests/VehicleHalManager_test.cpp b/vehicle/2.0/default/tests/VehicleHalManager_test.cpp index d06f6f4fa..964c7c815 100644 --- a/vehicle/2.0/default/tests/VehicleHalManager_test.cpp +++ b/vehicle/2.0/default/tests/VehicleHalManager_test.cpp @@ -182,8 +182,8 @@ public: }; TEST_F(VehicleHalManagerTest, getPropConfigs) { - hidl_vec<VehicleProperty> properties = init_hidl_vec( - { VehicleProperty::HVAC_FAN_SPEED,VehicleProperty::INFO_MAKE} ); + hidl_vec<VehicleProperty> properties = + { VehicleProperty::HVAC_FAN_SPEED, VehicleProperty::INFO_MAKE }; bool called = false; manager->getPropConfigs(properties, @@ -197,7 +197,7 @@ TEST_F(VehicleHalManagerTest, getPropConfigs) { ASSERT_TRUE(called); // Verify callback received. called = false; - manager->getPropConfigs(init_hidl_vec({VehicleProperty::HVAC_FAN_SPEED}), + manager->getPropConfigs({ VehicleProperty::HVAC_FAN_SPEED }, [&called] (StatusCode status, const hidl_vec<VehiclePropConfig>& c) { ASSERT_EQ(StatusCode::OK, status); @@ -230,13 +230,12 @@ TEST_F(VehicleHalManagerTest, halErrorEvent) { sp<MockedVehicleCallback> cb = new MockedVehicleCallback(); - hidl_vec<SubscribeOptions> options = init_hidl_vec( - { - SubscribeOptions { - .propId = PROP, - .flags = SubscribeFlags::DEFAULT - }, - }); + hidl_vec<SubscribeOptions> options = { + SubscribeOptions { + .propId = PROP, + .flags = SubscribeFlags::DEFAULT + }, + }; StatusCode res = manager->subscribe(cb, options); ASSERT_EQ(StatusCode::OK, res); @@ -249,13 +248,12 @@ TEST_F(VehicleHalManagerTest, subscribe) { sp<MockedVehicleCallback> cb = new MockedVehicleCallback(); - hidl_vec<SubscribeOptions> options = init_hidl_vec( - { - SubscribeOptions { - .propId = PROP, - .flags = SubscribeFlags::DEFAULT - }, - }); + hidl_vec<SubscribeOptions> options = { + SubscribeOptions { + .propId = PROP, + .flags = SubscribeFlags::DEFAULT + } + }; StatusCode res = manager->subscribe(cb, options); ASSERT_EQ(StatusCode::OK, res); @@ -291,13 +289,12 @@ TEST_F(VehicleHalManagerTest, subscribe_WriteOnly) { sp<MockedVehicleCallback> cb = new MockedVehicleCallback(); - hidl_vec<SubscribeOptions> options = init_hidl_vec( - { - SubscribeOptions { - .propId = PROP, - .flags = SubscribeFlags::HAL_EVENT - }, - }); + hidl_vec<SubscribeOptions> options = { + SubscribeOptions { + .propId = PROP, + .flags = SubscribeFlags::HAL_EVENT + }, + }; StatusCode res = manager->subscribe(cb, options); // Unable to subscribe on Hal Events for write-only properties. diff --git a/vehicle/2.0/default/tests/VehicleHalTestUtils.h b/vehicle/2.0/default/tests/VehicleHalTestUtils.h index 16d0be912..586b5abff 100644 --- a/vehicle/2.0/default/tests/VehicleHalTestUtils.h +++ b/vehicle/2.0/default/tests/VehicleHalTestUtils.h @@ -43,17 +43,17 @@ const VehiclePropConfig kVehicleProperties[] = { .permissionModel = VehiclePermissionModel::NO_RESTRICTION, .supportedAreas = static_cast<int32_t>( VehicleAreaZone::ROW_1_LEFT | VehicleAreaZone::ROW_1_RIGHT), - .areaConfigs = init_hidl_vec({ - VehicleAreaConfig { - .areaId = toInt(VehicleAreaZone::ROW_1_LEFT), - .minInt32Value = 1, - .maxInt32Value = 7}, - VehicleAreaConfig { - .areaId = toInt(VehicleAreaZone::ROW_1_RIGHT), - .minInt32Value = 1, - .maxInt32Value = 5, - } - }), + .areaConfigs = { + VehicleAreaConfig { + .areaId = toInt(VehicleAreaZone::ROW_1_LEFT), + .minInt32Value = 1, + .maxInt32Value = 7}, + VehicleAreaConfig { + .areaId = toInt(VehicleAreaZone::ROW_1_RIGHT), + .minInt32Value = 1, + .maxInt32Value = 5, + } + } }, // Write-only property @@ -64,17 +64,17 @@ const VehiclePropConfig kVehicleProperties[] = { .permissionModel = VehiclePermissionModel::NO_RESTRICTION, .supportedAreas = static_cast<int32_t>( VehicleAreaZone::ROW_1_LEFT | VehicleAreaZone::ROW_1_RIGHT), - .areaConfigs = init_hidl_vec({ - VehicleAreaConfig { - .areaId = toInt(VehicleAreaZone::ROW_1_LEFT), - .minInt32Value = 64, - .maxInt32Value = 80}, - VehicleAreaConfig { - .areaId = toInt(VehicleAreaZone::ROW_1_RIGHT), - .minInt32Value = 64, - .maxInt32Value = 80, - } - }), + .areaConfigs = { + VehicleAreaConfig { + .areaId = toInt(VehicleAreaZone::ROW_1_LEFT), + .minInt32Value = 64, + .maxInt32Value = 80}, + VehicleAreaConfig { + .areaId = toInt(VehicleAreaZone::ROW_1_RIGHT), + .minInt32Value = 64, + .maxInt32Value = 80, + } + } }, { @@ -82,12 +82,12 @@ const VehiclePropConfig kVehicleProperties[] = { .access = VehiclePropertyAccess::READ, .changeMode = VehiclePropertyChangeMode::ON_CHANGE, .permissionModel = VehiclePermissionModel::OEM_ONLY, - .areaConfigs = init_hidl_vec({ - VehicleAreaConfig { - .minFloatValue = 0, - .maxFloatValue = 1.0 - } - }) + .areaConfigs = { + VehicleAreaConfig { + .minFloatValue = 0, + .maxFloatValue = 1.0 + } + } }, { @@ -95,12 +95,12 @@ const VehiclePropConfig kVehicleProperties[] = { .access = VehiclePropertyAccess::READ_WRITE, .changeMode = VehiclePropertyChangeMode::ON_CHANGE, .permissionModel = VehiclePermissionModel::OEM_ONLY, - .areaConfigs = init_hidl_vec({ - VehicleAreaConfig { - .minInt32Value = 0, - .maxInt32Value = 10 - } - }) + .areaConfigs = { + VehicleAreaConfig { + .minInt32Value = 0, + .maxInt32Value = 10 + } + } }, { diff --git a/vehicle/2.0/default/vehicle_hal_manager/AccessControlConfigParser.cpp b/vehicle/2.0/default/vehicle_hal_manager/AccessControlConfigParser.cpp index 1d436c507..d6458c280 100644 --- a/vehicle/2.0/default/vehicle_hal_manager/AccessControlConfigParser.cpp +++ b/vehicle/2.0/default/vehicle_hal_manager/AccessControlConfigParser.cpp @@ -15,13 +15,14 @@ */ #define LOG_TAG "android.hardware.vehicle@2.0-impl" -#include <android/log.h> + +#include "AccessControlConfigParser.h" #include <fstream> #include <sstream> #include <iostream> -#include "AccessControlConfigParser.h" +#include <android/log.h> namespace android { namespace hardware { diff --git a/vehicle/2.0/default/vehicle_hal_manager/AccessControlConfigParser.h b/vehicle/2.0/default/vehicle_hal_manager/AccessControlConfigParser.h index 17cbbd643..5cd0c3ec4 100644 --- a/vehicle/2.0/default/vehicle_hal_manager/AccessControlConfigParser.h +++ b/vehicle/2.0/default/vehicle_hal_manager/AccessControlConfigParser.h @@ -21,6 +21,7 @@ #include <vector> #include <unordered_map> #include <list> + #include <android/hardware/vehicle/2.0/types.h> namespace android { diff --git a/vehicle/2.0/default/vehicle_hal_manager/SubscriptionManager.cpp b/vehicle/2.0/default/vehicle_hal_manager/SubscriptionManager.cpp index 910413e13..c190c714e 100644 --- a/vehicle/2.0/default/vehicle_hal_manager/SubscriptionManager.cpp +++ b/vehicle/2.0/default/vehicle_hal_manager/SubscriptionManager.cpp @@ -16,12 +16,13 @@ #define LOG_TAG "android.hardware.vehicle@2.0-impl" +#include "SubscriptionManager.h" + #include <cmath> -#include <utils/Errors.h> +#include <android/log.h> #include "VehicleUtils.h" -#include "SubscriptionManager.h" namespace android { namespace hardware { diff --git a/vehicle/2.0/default/vehicle_hal_manager/VehicleHalManager.cpp b/vehicle/2.0/default/vehicle_hal_manager/VehicleHalManager.cpp index 267c515e3..b4eb48467 100644 --- a/vehicle/2.0/default/vehicle_hal_manager/VehicleHalManager.cpp +++ b/vehicle/2.0/default/vehicle_hal_manager/VehicleHalManager.cpp @@ -13,19 +13,17 @@ * See the License for the specific language governing permissions and * limitations under the License. */ + #define LOG_TAG "android.hardware.vehicle@2.0-impl" -#include <cmath> +#include "VehicleHalManager.h" -#include <utils/Errors.h> -#include <android/log.h> -#include <hidl/Status.h> -#include <future> -#include <bitset> #include <fstream> + +#include <android/log.h> #include <private/android_filesystem_config.h> -#include "VehicleHalManager.h" +#include "VehicleUtils.h" namespace android { namespace hardware { diff --git a/vehicle/2.0/default/vehicle_hal_manager/VehicleHalManager.h b/vehicle/2.0/default/vehicle_hal_manager/VehicleHalManager.h index cb846c9ec..519b09b7e 100644 --- a/vehicle/2.0/default/vehicle_hal_manager/VehicleHalManager.h +++ b/vehicle/2.0/default/vehicle_hal_manager/VehicleHalManager.h @@ -17,24 +17,24 @@ #ifndef android_hardware_vehicle_V2_0_VehicleHalManager_H_ #define android_hardware_vehicle_V2_0_VehicleHalManager_H_ -#include <stdint.h> #include <inttypes.h> +#include <stdint.h> #include <sys/types.h> -#include <memory> + +#include <list> #include <map> +#include <memory> #include <set> -#include <list> - -#include <hwbinder/IPCThreadState.h> #include <android/hardware/vehicle/2.0/IVehicle.h> +#include <hwbinder/IPCThreadState.h> #include "AccessControlConfigParser.h" #include "ConcurrentQueue.h" #include "SubscriptionManager.h" #include "VehicleHal.h" -#include "VehiclePropConfigIndex.h" #include "VehicleObjectPool.h" +#include "VehiclePropConfigIndex.h" namespace android { namespace hardware { diff --git a/vehicle/2.0/default/vehicle_hal_manager/VehicleObjectPool.cpp b/vehicle/2.0/default/vehicle_hal_manager/VehicleObjectPool.cpp new file mode 100644 index 000000000..70b93e7d7 --- /dev/null +++ b/vehicle/2.0/default/vehicle_hal_manager/VehicleObjectPool.cpp @@ -0,0 +1,157 @@ +/* + * Copyright (C) 2016 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#define LOG_TAG "android.hardware.vehicle@2.0-impl" + +#include "VehicleObjectPool.h" + +#include <android/log.h> + +#include "VehicleUtils.h" + +namespace android { +namespace hardware { +namespace vehicle { +namespace V2_0 { + +VehiclePropValuePool::RecyclableType VehiclePropValuePool::obtain( + VehiclePropertyType type, size_t vecSize) { + return isDisposable(type, vecSize) + ? obtainDisposable(type, vecSize) + : obtainRecylable(type, vecSize); +} + +VehiclePropValuePool::RecyclableType VehiclePropValuePool::obtain( + const VehiclePropValue& src) { + if (src.prop == VehicleProperty::INVALID) { + ALOGE("Unable to obtain an object from pool for unknown property"); + return RecyclableType(); + } + VehiclePropertyType type = getPropType(src.prop); + size_t vecSize = getVehicleRawValueVectorSize(src.value, type);; + auto dest = obtain(type, vecSize); + + dest->prop = src.prop; + dest->areaId = src.areaId; + dest->timestamp = src.timestamp; + copyVehicleRawValue(&dest->value, src.value); + + return dest; +} + +VehiclePropValuePool::RecyclableType VehiclePropValuePool::obtainInt32( + int32_t value) { + auto val = obtain(VehiclePropertyType::INT32); + val->value.int32Values[0] = value; + return val; +} + +VehiclePropValuePool::RecyclableType VehiclePropValuePool::obtainInt64( + int64_t value) { + auto val = obtain(VehiclePropertyType::INT64); + val->value.int64Values[0] = value; + return val; +} + +VehiclePropValuePool::RecyclableType VehiclePropValuePool::obtainFloat( + float value) { + auto val = obtain(VehiclePropertyType::FLOAT); + val->value.floatValues[0] = value; + return val; +} + +VehiclePropValuePool::RecyclableType VehiclePropValuePool::obtainString( + const char* cstr) { + auto val = obtain(VehiclePropertyType::STRING); + val->value.stringValue = cstr; + return val; +} + +VehiclePropValuePool::RecyclableType VehiclePropValuePool::obtainRecylable( + VehiclePropertyType type, size_t vecSize) { + // VehiclePropertyType is not overlapping with vectorSize. + int32_t key = static_cast<int32_t>(type) + | static_cast<int32_t>(vecSize); + + std::lock_guard<std::mutex> g(mLock); + auto it = mValueTypePools.find(key); + + if (it == mValueTypePools.end()) { + auto newPool(std::make_unique<InternalPool>(type, vecSize)); + it = mValueTypePools.emplace(key, std::move(newPool)).first; + } + return it->second->obtain(); +} + +VehiclePropValuePool::RecyclableType VehiclePropValuePool::obtainBoolean( + bool value) { + return obtainInt32(value); +} + +VehiclePropValuePool::RecyclableType VehiclePropValuePool::obtainDisposable( + VehiclePropertyType valueType, size_t vectorSize) const { + return RecyclableType { + createVehiclePropValue(valueType, vectorSize).release(), + mDisposableDeleter + }; +} + +VehiclePropValuePool::RecyclableType VehiclePropValuePool::obtain( + VehiclePropertyType type) { + return obtain(type, 1); +} + + +void VehiclePropValuePool::InternalPool::recycle(VehiclePropValue* o) { + if (o == nullptr) { + ALOGE("Attempt to recycle nullptr"); + return; + } + + if (!check(&o->value)) { + ALOGE("Discarding value for prop 0x%x because it contains " + "data that is not consistent with this pool. " + "Expected type: %d, vector size: %d", + o->prop, mPropType, mVectorSize); + delete o; + } else { + ObjectPool<VehiclePropValue>::recycle(o); + } +} + +bool VehiclePropValuePool::InternalPool::check(VehiclePropValue::RawValue* v) { + return check(&v->int32Values, + (VehiclePropertyType::INT32 == mPropType + || VehiclePropertyType::INT32_VEC == mPropType + || VehiclePropertyType::BOOLEAN == mPropType)) + && check(&v->floatValues, + (VehiclePropertyType::FLOAT == mPropType + || VehiclePropertyType::FLOAT_VEC == mPropType)) + && check(&v->int64Values, + VehiclePropertyType::INT64 == mPropType) + && check(&v->bytes, + VehiclePropertyType::BYTES == mPropType) + && v->stringValue.size() == 0; +} + +VehiclePropValue* VehiclePropValuePool::InternalPool::createObject() { + return createVehiclePropValue(mPropType, mVectorSize).release(); +} + +} // namespace V2_0 +} // namespace vehicle +} // namespace hardware +} // namespace android diff --git a/vehicle/2.0/default/vehicle_hal_manager/VehicleObjectPool.h b/vehicle/2.0/default/vehicle_hal_manager/VehicleObjectPool.h index 002938056..1ca9211e3 100644 --- a/vehicle/2.0/default/vehicle_hal_manager/VehicleObjectPool.h +++ b/vehicle/2.0/default/vehicle_hal_manager/VehicleObjectPool.h @@ -18,19 +18,10 @@ #ifndef android_hardware_vehicle_V2_0_VehicleObjectPool_H_ #define android_hardware_vehicle_V2_0_VehicleObjectPool_H_ -#include "VehicleUtils.h" // defines LOG_TAG and includes <android/log.h> - -#include <iostream> -#include <memory> #include <deque> -#include <string> #include <map> #include <mutex> -#ifndef LOG_TAG -#define LOG_TAG "android.hardware.vehicle@2.0-impl" -#endif -#include <android/log.h> #include <android/hardware/vehicle/2.0/types.h> namespace android { @@ -38,8 +29,6 @@ namespace hardware { namespace vehicle { namespace V2_0 { -using android::hardware::hidl_vec; - // Handy metric mostly for unit tests and debug. #define INC_METRIC_IF_DEBUG(val) PoolStats::instance()->val++; struct PoolStats { @@ -53,6 +42,29 @@ struct PoolStats { } }; +template<typename T> +struct Deleter { + using OnDeleteFunc = std::function<void(T*)>; + + Deleter(const OnDeleteFunc& f) : mOnDelete(f) {}; + + Deleter() = default; + Deleter(const Deleter&) = default; + + void operator()(T* o) { + mOnDelete(o); + } +private: + OnDeleteFunc mOnDelete; +}; + +/** + * This is std::unique_ptr<> with custom delete operation that typically moves + * the pointer it holds back to ObjectPool. + */ +template <typename T> +using recyclable_ptr = typename std::unique_ptr<T, Deleter<T>>; + /** * Generic abstract object pool class. Users of this class must implement * #createObject method. @@ -68,24 +80,7 @@ public: ObjectPool() = default; virtual ~ObjectPool() = default; - struct Deleter { - using OnDeleteFunc = std::function<void(T*)>; - - Deleter(const OnDeleteFunc& f) : mOnDelete(f) {}; - - Deleter() = default; - Deleter(const Deleter&) = default; - - void operator()(T* o) { - mOnDelete(o); - } - private: - OnDeleteFunc mOnDelete; - }; - - using RecyclableType = std::unique_ptr<T, Deleter>; - - virtual RecyclableType obtain() { + virtual recyclable_ptr<T> obtain() { std::lock_guard<std::mutex> g(mLock); INC_METRIC_IF_DEBUG(Obtained) if (mObjects.empty()) { @@ -112,34 +107,26 @@ protected: } private: - const Deleter& getDeleter() { + const Deleter<T>& getDeleter() { if (!mDeleter.get()) { - Deleter *d = new Deleter(std::bind(&ObjectPool::recycle, - this, - std::placeholders::_1)); + Deleter<T> *d = new Deleter<T>(std::bind( + &ObjectPool::recycle, this, std::placeholders::_1)); mDeleter.reset(d); } return *mDeleter.get(); } - RecyclableType wrap(T* raw) { - return RecyclableType { raw, getDeleter() }; + recyclable_ptr<T> wrap(T* raw) { + return recyclable_ptr<T> { raw, getDeleter() }; } private: mutable std::mutex mLock; std::deque<std::unique_ptr<T>> mObjects; - std::unique_ptr<Deleter> mDeleter; + std::unique_ptr<Deleter<T>> mDeleter; }; /** - * This is std::unique_ptr<> with custom delete operation that typically moves - * the pointer it holds back to ObectPool. - */ -template <typename T> -using recyclable_ptr = typename ObjectPool<T>::RecyclableType; - -/** * This class provides a pool of recycable VehiclePropertyValue objects. * * It has only one overloaded public method - obtain(...), users must call this @@ -186,66 +173,20 @@ public: * */ VehiclePropValuePool(size_t maxRecyclableVectorSize = 4) : - mMaxRecyclableVectorSize(maxRecyclableVectorSize) { }; + mMaxRecyclableVectorSize(maxRecyclableVectorSize) {}; - RecyclableType obtain(VehiclePropertyType type) { - return obtain(type, 1); - } + RecyclableType obtain(VehiclePropertyType type); - RecyclableType obtain(VehiclePropertyType type, size_t vecSize) { - return isDisposable(type, vecSize) - ? obtainDisposable(type, vecSize) - : obtainRecylable(type, vecSize); - } - - RecyclableType obtain(const VehiclePropValue& src) { - if (src.prop == VehicleProperty::INVALID) { - ALOGE("Unable to obtain an object from pool for unknown property"); - return RecyclableType(); - } - VehiclePropertyType type = getPropType(src.prop); - size_t vecSize = getVehicleRawValueVectorSize(src.value, type);; - auto dest = obtain(type, vecSize); - - dest->prop = src.prop; - dest->areaId = src.areaId; - dest->timestamp = src.timestamp; - copyVehicleRawValue(&dest->value, src.value); - - return dest; - } - - RecyclableType obtainBoolean(bool value) { - return obtainInt32(value); - } - - RecyclableType obtainInt32(int32_t value) { - auto val = obtain(VehiclePropertyType::INT32); - val->value.int32Values[0] = value; - return val; - } - - RecyclableType obtainInt64(int64_t value) { - auto val = obtain(VehiclePropertyType::INT64); - val->value.int64Values[0] = value; - return val; - } - - RecyclableType obtainFloat(float value) { - auto val = obtain(VehiclePropertyType::FLOAT); - val->value.floatValues[0] = value; - return val; - } - - RecyclableType obtainString(const char* cstr) { - auto val = obtain(VehiclePropertyType::STRING); - val->value.stringValue = cstr; - return val; - } + RecyclableType obtain(VehiclePropertyType type, size_t vecSize); + RecyclableType obtain(const VehiclePropValue& src); + RecyclableType obtainBoolean(bool value); + RecyclableType obtainInt32(int32_t value); + RecyclableType obtainInt64(int64_t value); + RecyclableType obtainFloat(float value); + RecyclableType obtainString(const char* cstr); VehiclePropValuePool(VehiclePropValuePool& ) = delete; VehiclePropValuePool& operator=(VehiclePropValuePool&) = delete; - private: bool isDisposable(VehiclePropertyType type, size_t vecSize) const { return vecSize > mMaxRecyclableVectorSize || @@ -253,70 +194,23 @@ private: } RecyclableType obtainDisposable(VehiclePropertyType valueType, - size_t vectorSize) const { - return RecyclableType { - createVehiclePropValue(valueType, vectorSize).release(), - mDisposableDeleter - }; - } - - RecyclableType obtainRecylable(VehiclePropertyType type, size_t vecSize) { - // VehiclePropertyType is not overlapping with vectorSize. - int32_t key = static_cast<int32_t>(type) - | static_cast<int32_t>(vecSize); - - std::lock_guard<std::mutex> g(mLock); - auto it = mValueTypePools.find(key); - - if (it == mValueTypePools.end()) { - auto newPool(std::make_unique<InternalPool>(type, vecSize)); - it = mValueTypePools.emplace(key, std::move(newPool)).first; - } - return it->second->obtain(); - } + size_t vectorSize) const; + RecyclableType obtainRecylable(VehiclePropertyType type, + size_t vecSize); class InternalPool: public ObjectPool<VehiclePropValue> { public: InternalPool(VehiclePropertyType type, size_t vectorSize) - : mPropType(type), mVectorSize(vectorSize) { - } + : mPropType(type), mVectorSize(vectorSize) {} RecyclableType obtain() { return ObjectPool<VehiclePropValue>::obtain(); } protected: - VehiclePropValue* createObject() override { - return createVehiclePropValue(mPropType, mVectorSize).release(); - } - - void recycle(VehiclePropValue* o) override { - ALOGE_IF(o == nullptr, "Attempt to recycle nullptr"); - - if (!check(&o->value)) { - ALOGE("Discarding value for prop 0x%x because it contains " - "data that is not consistent with this pool. " - "Expected type: %d, vector size: %d", - o->prop, mPropType, mVectorSize); - delete o; - } - ObjectPool<VehiclePropValue>::recycle(o); - } - + VehiclePropValue* createObject() override; + void recycle(VehiclePropValue* o) override; private: - bool check(VehiclePropValue::RawValue* v) { - return check(&v->int32Values, - (VehiclePropertyType::INT32 == mPropType - || VehiclePropertyType::INT32_VEC == mPropType - || VehiclePropertyType::BOOLEAN == mPropType)) - && check(&v->floatValues, - (VehiclePropertyType::FLOAT == mPropType - || VehiclePropertyType::FLOAT_VEC == mPropType)) - && check(&v->int64Values, - VehiclePropertyType::INT64 == mPropType) - && check(&v->bytes, - VehiclePropertyType::BYTES == mPropType) - && v->stringValue.size() == 0; - } + bool check(VehiclePropValue::RawValue* v); template <typename VecType> bool check(hidl_vec<VecType>* vec, bool expected) { @@ -328,7 +222,7 @@ private: }; private: - const ObjectPool<VehiclePropValue>::Deleter mDisposableDeleter { + const Deleter<VehiclePropValue> mDisposableDeleter { [] (VehiclePropValue* v) { delete v; } diff --git a/vehicle/2.0/default/vehicle_hal_manager/VehicleUtils.cpp b/vehicle/2.0/default/vehicle_hal_manager/VehicleUtils.cpp new file mode 100644 index 000000000..c461833fa --- /dev/null +++ b/vehicle/2.0/default/vehicle_hal_manager/VehicleUtils.cpp @@ -0,0 +1,127 @@ +/* + * Copyright (C) 2016 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#define LOG_TAG "android.hardware.vehicle@2.0-impl" + +#include "VehicleUtils.h" + +#include <android/log.h> + +namespace android { +namespace hardware { +namespace vehicle { +namespace V2_0 { + +//namespace utils { + +std::unique_ptr<VehiclePropValue> createVehiclePropValue( + VehiclePropertyType type, size_t vecSize) { + auto val = std::unique_ptr<VehiclePropValue>(new VehiclePropValue); + switch (type) { + case VehiclePropertyType::INT32: // fall through + case VehiclePropertyType::INT32_VEC: // fall through + case VehiclePropertyType::BOOLEAN: + val->value.int32Values.resize(vecSize); + break; + case VehiclePropertyType::FLOAT: // fall through + case VehiclePropertyType::FLOAT_VEC: + val->value.floatValues.resize(vecSize); + break; + case VehiclePropertyType::INT64: + val->value.int64Values.resize(vecSize); + break; + case VehiclePropertyType::BYTES: + val->value.bytes.resize(vecSize); + break; + case VehiclePropertyType::STRING: + break; // Valid, but nothing to do. + default: + ALOGE("createVehiclePropValue: unknown type: %d", type); + val.reset(nullptr); + } + return val; +} + +size_t getVehicleRawValueVectorSize( + const VehiclePropValue::RawValue& value, VehiclePropertyType type) { + switch (type) { + case VehiclePropertyType::INT32: // fall through + case VehiclePropertyType::INT32_VEC: // fall through + case VehiclePropertyType::BOOLEAN: + return value.int32Values.size(); + case VehiclePropertyType::FLOAT: // fall through + case VehiclePropertyType::FLOAT_VEC: + return value.floatValues.size(); + case VehiclePropertyType::INT64: + return value.int64Values.size(); + case VehiclePropertyType::BYTES: + return value.bytes.size(); + default: + return 0; + } +} + +template<typename T> +inline void copyHidlVec(hidl_vec <T>* dest, const hidl_vec <T>& src) { + for (size_t i = 0; i < std::min(dest->size(), src.size()); i++) { + (*dest)[i] = src[i]; + } +} + +void copyVehicleRawValue(VehiclePropValue::RawValue* dest, + const VehiclePropValue::RawValue& src) { + dest->int32Values = src.int32Values; + dest->floatValues = src.floatValues; + dest->int64Values = src.int64Values; + dest->bytes = src.bytes; + dest->stringValue = src.stringValue; +} + +template<typename T> +void shallowCopyHidlVec(hidl_vec <T>* dest, const hidl_vec <T>& src) { + if (src.size() > 0) { + dest->setToExternal(const_cast<T*>(&src[0]), src.size()); + } else if (dest->size() > 0) { + dest->resize(0); + } +} + +void shallowCopyHidlStr(hidl_string* dest, const hidl_string& src) { + if (!src.empty()) { + dest->setToExternal(src.c_str(), src.size()); + } else if (dest->size() > 0) { + dest->setToExternal(0, 0); + } +} + +void shallowCopy(VehiclePropValue* dest, const VehiclePropValue& src) { + dest->prop = src.prop; + dest->areaId = src.areaId; + dest->timestamp = src.timestamp; + shallowCopyHidlVec(&dest->value.int32Values, src.value.int32Values); + shallowCopyHidlVec(&dest->value.int64Values, src.value.int64Values); + shallowCopyHidlVec(&dest->value.floatValues, src.value.floatValues); + shallowCopyHidlVec(&dest->value.bytes, src.value.bytes); + shallowCopyHidlStr(&dest->value.stringValue, src.value.stringValue); +} + + +//} // namespace utils + +} // namespace V2_0 +} // namespace vehicle +} // namespace hardware +} // namespace android diff --git a/vehicle/2.0/default/vehicle_hal_manager/VehicleUtils.h b/vehicle/2.0/default/vehicle_hal_manager/VehicleUtils.h index f06e97ef2..1177dddae 100644 --- a/vehicle/2.0/default/vehicle_hal_manager/VehicleUtils.h +++ b/vehicle/2.0/default/vehicle_hal_manager/VehicleUtils.h @@ -17,11 +17,6 @@ #ifndef android_hardware_vehicle_V2_0_VehicleUtils_H_ #define android_hardware_vehicle_V2_0_VehicleUtils_H_ -#ifndef LOG_TAG -#define LOG_TAG "android.hardware.vehicle@2.0-impl" -#endif -#include <android/log.h> - #include <memory> #include <hidl/HidlSupport.h> @@ -36,43 +31,22 @@ namespace V2_0 { /** Represents all supported areas for a property. Can be used is */ constexpr int32_t kAllSupportedAreas = 0; -template <typename T> -inline hidl_vec<T> init_hidl_vec(std::initializer_list<const T> values) { - hidl_vec<T> vector; - vector.resize(values.size()); - size_t i = 0; - for (auto& c : values) { - vector[i++] = c; - } - return vector; -} - -/** - * Logical 'and' operator for class enums. The return type will be enum's - * underline type - */ -template <typename ENUM> -inline typename std::underlying_type<ENUM>::type operator &(ENUM v1, ENUM v2) { - return static_cast<typename std::underlying_type<ENUM>::type>(v1) - & static_cast<typename std::underlying_type<ENUM>::type>(v2); -} - /** Returns underlying (integer) value for given enum. */ -template <typename ENUM> +template<typename ENUM> inline typename std::underlying_type<ENUM>::type toInt(ENUM const value) { return static_cast<typename std::underlying_type<ENUM>::type>(value); } inline VehiclePropertyType getPropType(VehicleProperty prop) { return static_cast<VehiclePropertyType>( - static_cast<int32_t>(prop) - & static_cast<int32_t>(VehiclePropertyType::MASK)); + static_cast<int32_t>(prop) + & static_cast<int32_t>(VehiclePropertyType::MASK)); } inline VehiclePropertyGroup getPropGroup(VehicleProperty prop) { return static_cast<VehiclePropertyGroup>( - static_cast<int32_t>(prop) - & static_cast<int32_t>(VehiclePropertyGroup::MASK)); + static_cast<int32_t>(prop) + & static_cast<int32_t>(VehiclePropertyGroup::MASK)); } inline VehicleArea getPropArea(VehicleProperty prop) { @@ -84,106 +58,25 @@ inline bool isGlobalProp(VehicleProperty prop) { return getPropArea(prop) == VehicleArea::GLOBAL; } -inline bool checkPropType(VehicleProperty prop, VehiclePropertyType type) { - return getPropType(prop) == type; -} - inline bool isSystemProperty(VehicleProperty prop) { return VehiclePropertyGroup::SYSTEM == getPropGroup(prop); } -inline std::unique_ptr<VehiclePropValue> createVehiclePropValue( - VehiclePropertyType type, size_t vecSize) { - auto val = std::unique_ptr<VehiclePropValue>(new VehiclePropValue); - switch (type) { - case VehiclePropertyType::INT32: // fall through - case VehiclePropertyType::INT32_VEC: // fall through - case VehiclePropertyType::BOOLEAN: - val->value.int32Values.resize(vecSize); - break; - case VehiclePropertyType::FLOAT: - case VehiclePropertyType::FLOAT_VEC: // fall through - val->value.floatValues.resize(vecSize); - break; - case VehiclePropertyType::INT64: - val->value.int64Values.resize(vecSize); - break; - case VehiclePropertyType::BYTES: - val->value.bytes.resize(vecSize); - break; - case VehiclePropertyType::STRING: - break; // Valid, but nothing to do. - default: - ALOGE("createVehiclePropValue: unknown type: %d", type); - val.reset(nullptr); - } - return val; -} - -inline size_t getVehicleRawValueVectorSize( - const VehiclePropValue::RawValue& value, VehiclePropertyType type) { - switch (type) { - case VehiclePropertyType::INT32: // fall through - case VehiclePropertyType::INT32_VEC: // fall through - case VehiclePropertyType::BOOLEAN: - return value.int32Values.size(); - case VehiclePropertyType::FLOAT: // fall through - case VehiclePropertyType::FLOAT_VEC: - return value.floatValues.size(); - case VehiclePropertyType::INT64: - return value.int64Values.size(); - case VehiclePropertyType::BYTES: - return value.bytes.size(); - default: - return 0; - } -} +std::unique_ptr<VehiclePropValue> createVehiclePropValue( + VehiclePropertyType type, size_t vecSize); -/** Copies vector src to dest, dest should have enough space. */ -template <typename T> -inline void copyHidlVec(hidl_vec<T>* dest, const hidl_vec<T>& src) { - for (size_t i = 0; i < std::min(dest->size(), src.size()); i++) { - (*dest)[i] = src[i]; - } -} +size_t getVehicleRawValueVectorSize( + const VehiclePropValue::RawValue& value, VehiclePropertyType type); -inline void copyVehicleRawValue(VehiclePropValue::RawValue* dest, - const VehiclePropValue::RawValue& src) { - copyHidlVec(&dest->int32Values, src.int32Values); - copyHidlVec(&dest->floatValues, src.floatValues); - copyHidlVec(&dest->int64Values, src.int64Values); - copyHidlVec(&dest->bytes, src.bytes); - dest->stringValue = src.stringValue; -} +void copyVehicleRawValue(VehiclePropValue::RawValue* dest, + const VehiclePropValue::RawValue& src); -template <typename T> -inline void shallowCopyHidlVec(hidl_vec<T>* dest, const hidl_vec<T>& src) { - if (src.size() > 0) { - dest->setToExternal(const_cast<T*>(&src[0]), src.size()); - } else if (dest->size() > 0) { - dest->resize(0); - } -} +template<typename T> +void shallowCopyHidlVec(hidl_vec<T>* dest, const hidl_vec<T>& src); -inline void shallowCopyHidlStr(hidl_string* dest, const hidl_string& src ) { - if (!src.empty()) { - dest->setToExternal(src.c_str(), src.size()); - } else if (dest->size() > 0) { - dest->setToExternal(0, 0); - } -} +void shallowCopyHidlStr(hidl_string* dest, const hidl_string& src); -inline void shallowCopy(VehiclePropValue* dest, - const VehiclePropValue& src) { - dest->prop = src.prop; - dest->areaId = src.areaId; - dest->timestamp = src.timestamp; - shallowCopyHidlVec(&dest->value.int32Values, src.value.int32Values); - shallowCopyHidlVec(&dest->value.int64Values, src.value.int64Values); - shallowCopyHidlVec(&dest->value.floatValues, src.value.floatValues); - shallowCopyHidlVec(&dest->value.bytes, src.value.bytes); - shallowCopyHidlStr(&dest->value.stringValue, src.value.stringValue); -} +void shallowCopy(VehiclePropValue* dest, const VehiclePropValue& src); } // namespace V2_0 } // namespace vehicle |
