1 /*
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
17 #ifndef __ANDROID_HAL_VEHICLE_TEST_
18 #define __ANDROID_HAL_VEHICLE_TEST_
19
20 #include <gtest/gtest.h>
21 #include <hardware/hardware.h>
22 #include <hardware/vehicle.h>
23
24 namespace tests {
25
26 static const uint64_t kVersion = HARDWARE_DEVICE_API_VERSION_2(1, 0, 1);
27
28 class VehicleModule : public testing::Test {
29 public:
VehicleModule()30 VehicleModule() :
31 vehicle_module_(NULL) {}
~VehicleModule()32 ~VehicleModule() {}
33 protected:
SetUp()34 virtual void SetUp() {
35 const hw_module_t *hw_module = NULL;
36 ASSERT_EQ(0, hw_get_module(VEHICLE_HARDWARE_MODULE_ID, &hw_module))
37 << "Can't get vehicle module";
38 ASSERT_TRUE(NULL != hw_module)
39 << "hw_get_module didn't return a valid hardware module";
40
41 vehicle_module_ = reinterpret_cast<const vehicle_module_t*>(hw_module);
42 }
vehicle_module()43 const vehicle_module_t* vehicle_module() { return vehicle_module_; }
44 private:
45 const vehicle_module_t* vehicle_module_;
46 };
47
48
VehicleEventCallback(const vehicle_prop_value_t * event_data)49 int VehicleEventCallback(const vehicle_prop_value_t* event_data) {
50 // Print what we got.
51 std::cout << "got some value from callback: "
52 << event_data->prop
53 << " uint32 value: "
54 << event_data->value.int32_value << "\n";
55 return 0;
56 }
57
VehicleErrorCallback(int32_t,int32_t,int32_t)58 int VehicleErrorCallback(int32_t /*error_code*/, int32_t /*property*/, int32_t /*operation*/) {
59 // Do nothing.
60 return 0;
61 }
62
63 class VehicleDevice : public VehicleModule {
64 public:
VehicleDevice()65 VehicleDevice() :
66 vehicle_device_(NULL) {}
~VehicleDevice()67 ~VehicleDevice() {}
68 protected:
SetUp()69 virtual void SetUp() {
70 VehicleModule::SetUp();
71 hw_device_t *device = NULL;
72 ASSERT_TRUE(NULL != vehicle_module()->common.methods->open)
73 << "Vehicle open() is unimplemented";
74 ASSERT_EQ(0, vehicle_module()->common.methods->open(
75 (const hw_module_t*)vehicle_module(), NULL, &device))
76 << "Can't open vehicle device";
77 ASSERT_TRUE(NULL != device)
78 << "Vehicle open() returned a NULL device";
79 ASSERT_EQ(kVersion, device->version)
80 << "Unsupported version";
81 vehicle_device_ = reinterpret_cast<vehicle_hw_device_t*>(device);
82 }
vehicle_device()83 vehicle_hw_device_t* vehicle_device() { return vehicle_device_; }
callback_fn()84 vehicle_event_callback_fn callback_fn() {
85 return VehicleEventCallback;
86 }
error_fn()87 vehicle_error_callback_fn error_fn() {
88 return VehicleErrorCallback;
89 }
90
91 private:
92 vehicle_hw_device_t* vehicle_device_;
93 };
94
95 } // namespace tests
96
97 #endif // __ANDROID_HAL_VEHICLE_TEST_
98