Vehicle HAL reference impl Part I

Implemented:
  - defined VehicleHal
  - object pool for VehiclePropValue objects
  - batching of vehicle HAL events
  - subscription management

Test: unit tests provided

Bug: b/31971746
Change-Id: Idd2d0aee7b32a975c3db54812be235e13f52905a
diff --git a/vehicle/2.0/default/impl/DefaultConfig.h b/vehicle/2.0/default/impl/DefaultConfig.h
new file mode 100644
index 0000000..6f04626
--- /dev/null
+++ b/vehicle/2.0/default/impl/DefaultConfig.h
@@ -0,0 +1,91 @@
+/*
+ * 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.
+ */
+
+#ifndef android_hardware_vehicle_V2_0_impl_DefaultConfig_H_
+#define android_hardware_vehicle_V2_0_impl_DefaultConfig_H_
+
+#include <android/hardware/vehicle/2.0/IVehicle.h>
+
+namespace android {
+namespace hardware {
+namespace vehicle {
+namespace V2_0 {
+
+namespace impl {
+
+const VehiclePropConfig kVehicleProperties[] = {
+    {
+        .prop = VehicleProperty::INFO_MAKE,
+        .access = VehiclePropertyAccess::READ,
+        .changeMode = VehiclePropertyChangeMode::STATIC,
+        .permissionModel = VehiclePermissionModel::OEM_ONLY,
+    },
+
+    {
+        .prop = VehicleProperty::HVAC_FAN_SPEED,
+        .access = VehiclePropertyAccess::READ_WRITE,
+        .changeMode = VehiclePropertyChangeMode::ON_CHANGE,
+        .permissionModel = VehiclePermissionModel::NO_RESTRICTION,
+        .supportedAreas = static_cast<int32_t>(
+            VehicleAreaZone::ROW_1_LEFT | VehicleAreaZone::ROW_1_RIGHT),
+        .areaConfigs = init_hidl_vec({
+                 VehicleAreaConfig {
+                     .areaId = val(VehicleAreaZone::ROW_2_LEFT),
+                     .minInt32Value = 1,
+                     .maxInt32Value = 7},
+                 VehicleAreaConfig {
+                     .areaId = val(VehicleAreaZone::ROW_1_RIGHT),
+                     .minInt32Value = 1,
+                     .maxInt32Value = 5,
+                 }
+             }),
+    },
+
+    {
+        .prop = VehicleProperty::INFO_FUEL_CAPACITY,
+        .access = VehiclePropertyAccess::READ,
+        .changeMode = VehiclePropertyChangeMode::ON_CHANGE,
+        .permissionModel = VehiclePermissionModel::OEM_ONLY,
+        .areaConfigs = init_hidl_vec({
+                                         VehicleAreaConfig {
+                                             .minFloatValue = 0,
+                                             .maxFloatValue = 1.0
+                                         }
+                                     })
+    },
+
+    {
+        .prop = VehicleProperty::DISPLAY_BRIGHTNESS,
+        .access = VehiclePropertyAccess::READ_WRITE,
+        .changeMode = VehiclePropertyChangeMode::ON_CHANGE,
+        .permissionModel = VehiclePermissionModel::OEM_ONLY,
+        .areaConfigs = init_hidl_vec({
+                                         VehicleAreaConfig {
+                                             .minInt32Value = 0,
+                                             .maxInt32Value = 10
+                                         }
+                                     })
+    }
+};
+
+}  // impl
+
+}  // namespace V2_0
+}  // namespace vehicle
+}  // namespace hardware
+}  // namespace android
+
+#endif // android_hardware_vehicle_V2_0_impl_DefaultConfig_H_
diff --git a/vehicle/2.0/default/impl/DefaultVehicleHal.cpp b/vehicle/2.0/default/impl/DefaultVehicleHal.cpp
new file mode 100644
index 0000000..6ca0f9f
--- /dev/null
+++ b/vehicle/2.0/default/impl/DefaultVehicleHal.cpp
@@ -0,0 +1,110 @@
+/*
+ * 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.
+ */
+
+#include "DefaultVehicleHal.h"
+
+namespace android {
+namespace hardware {
+namespace vehicle {
+namespace V2_0 {
+
+namespace impl {
+
+VehicleHal::VehiclePropValuePtr DefaultVehicleHal::get(VehicleProperty property,
+                                                       int32_t areaId,
+                                                       status_t* outStatus) {
+    *outStatus = OK;
+
+    VehiclePropValuePtr v;
+
+    switch (property) {
+        case VehicleProperty::INFO_MAKE:
+            v = getValuePool()->obtainString("Default Car");
+            break;
+        case VehicleProperty::HVAC_FAN_SPEED:
+            int32_t value;
+            if ((*outStatus = getHvacFanSpeed(areaId, &value)) == OK) {
+                v = getValuePool()->obtainInt32(value);
+            }
+            break;
+        case VehicleProperty::INFO_FUEL_CAPACITY:
+            v = getValuePool()->obtainFloat(0.75f);
+            break;
+        case VehicleProperty::DISPLAY_BRIGHTNESS:
+            v = getValuePool()->obtainInt32(brightness);
+            break;
+        default:
+            *outStatus = BAD_VALUE;
+    }
+
+    if (*outStatus == OK && v.get() != nullptr) {
+        v->prop = property;
+        v->areaId = areaId;
+        v->timestamp = elapsedRealtimeNano();
+    }
+
+    return v;
+}
+
+status_t DefaultVehicleHal::set(const VehiclePropValue& propValue) {
+    auto property = propValue.prop;
+
+    status_t status = OK;
+
+    switch (property) {
+        case VehicleProperty::HVAC_FAN_SPEED:
+            status = setHvacFanSpeed(propValue.areaId,
+                                     propValue.value.int32Values[0]);
+            break;
+        case VehicleProperty::DISPLAY_BRIGHTNESS:
+            brightness = propValue.value.int32Values[0];
+            break;
+        default:
+            status = BAD_VALUE;
+    }
+
+    return status;
+}
+
+status_t DefaultVehicleHal::getHvacFanSpeed(int32_t areaId,
+                                            int32_t* outValue)  {
+    if (areaId == val(VehicleAreaZone::ROW_1_LEFT)) {
+        *outValue = fanSpeedRow1Left;
+    } else if (areaId == val(VehicleAreaZone::ROW_2_RIGHT)) {
+        *outValue = fanSpeedRow1Right;
+    } else {
+        return BAD_VALUE;
+    }
+    return OK;
+}
+
+status_t DefaultVehicleHal::setHvacFanSpeed(int32_t areaId, int32_t value) {
+    if (areaId == val(VehicleAreaZone::ROW_1_LEFT)) {
+        fanSpeedRow1Left = value;
+    } else if (areaId == val(VehicleAreaZone::ROW_2_RIGHT)) {
+        fanSpeedRow1Right = value;
+    } else {
+        return BAD_VALUE;
+    }
+    return OK;
+}
+
+}  // impl
+
+}  // namespace V2_0
+}  // namespace vehicle
+}  // namespace hardware
+}  // namespace android
diff --git a/vehicle/2.0/default/impl/DefaultVehicleHal.h b/vehicle/2.0/default/impl/DefaultVehicleHal.h
new file mode 100644
index 0000000..7d0b7cb
--- /dev/null
+++ b/vehicle/2.0/default/impl/DefaultVehicleHal.h
@@ -0,0 +1,73 @@
+/*
+ * 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.
+ */
+
+#ifndef android_hardware_vehicle_V2_0_impl_DefaultVehicleHal_H_
+#define android_hardware_vehicle_V2_0_impl_DefaultVehicleHal_H_
+
+#include <VehicleHal.h>
+#include <impl/DefaultConfig.h>
+#include <utils/SystemClock.h>
+
+namespace android {
+namespace hardware {
+namespace vehicle {
+namespace V2_0 {
+
+namespace impl {
+
+class DefaultVehicleHal : public VehicleHal {
+public:
+    std::vector<VehiclePropConfig> listProperties() override {
+        return std::vector<VehiclePropConfig>(std::begin(kVehicleProperties),
+                                              std::end(kVehicleProperties));
+    }
+
+    VehiclePropValuePtr get(VehicleProperty property,
+                            int32_t areaId,
+                            status_t* outStatus) override;
+
+    status_t set(const VehiclePropValue& propValue) override;
+
+    status_t subscribe(VehicleProperty property,
+                       int32_t areas,
+                       float sampleRate) {
+        // TODO(pavelm): implement
+        return OK;
+    }
+
+    status_t unsubscribe(VehicleProperty property) {
+        // TODO(pavelm): implement
+        return OK;
+    }
+
+private:
+    status_t getHvacFanSpeed(int32_t areaId, int32_t* outValue);
+    status_t setHvacFanSpeed(int32_t areaId, int32_t value);
+private:
+    int32_t fanSpeedRow1Left = 3;
+    int32_t fanSpeedRow1Right = 5;
+    int32_t brightness = 7;
+};
+
+}  // impl
+
+}  // namespace V2_0
+}  // namespace vehicle
+}  // namespace hardware
+}  // namespace android
+
+
+#endif  // android_hardware_vehicle_V2_0_impl_DefaultVehicleHal_H_