diff options
author | Bertrand SIMONNET <bsimonnet@google.com> | 2016-03-23 12:20:23 -0700 |
---|---|---|
committer | Bertrand SIMONNET <bsimonnet@google.com> | 2016-03-23 14:13:28 -0700 |
commit | f4df29ac8ed4c45868b9bf93ad1d88478a92ceef (patch) | |
tree | b5a827aeed88039fe15ff7108dceb191d3568ec8 /client | |
parent | 6187530413fd184856f85ea09bd30040e5426422 (diff) | |
download | peripheralmanager-f4df29ac8ed4c45868b9bf93ad1d88478a92ceef.tar.gz |
Add tests for the I2c developer C API.
Bug: 27675223
Change-Id: I751083324c09aff20a93290897fc4676ce247722
Diffstat (limited to 'client')
-rw-r--r-- | client/Android.mk | 1 | ||||
-rw-r--r-- | client/i2c_unittest.cc | 149 | ||||
-rw-r--r-- | client/peripheral_manager_client_impl.cc | 4 | ||||
-rw-r--r-- | client/peripheral_manager_client_impl.h | 1 | ||||
-rw-r--r-- | client/wrapper.cc | 8 |
5 files changed, 163 insertions, 0 deletions
diff --git a/client/Android.mk b/client/Android.mk index d8502f0..1819b53 100644 --- a/client/Android.mk +++ b/client/Android.mk @@ -72,6 +72,7 @@ LOCAL_SHARED_LIBRARIES := \ LOCAL_SRC_FILES := \ gpio_unittest.cc \ + i2c_unittest.cc \ led_unittest.cc \ peripheral_manager_client_unittest.cc \ spi_unittest.cc \ diff --git a/client/i2c_unittest.cc b/client/i2c_unittest.cc new file mode 100644 index 0000000..fd04563 --- /dev/null +++ b/client/i2c_unittest.cc @@ -0,0 +1,149 @@ +/* + * 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 <memory> + +#include <gtest/gtest.h> + +#include "fake_devices.h" +#include "i2c_driver_i2cdev.h" +#include "peripheral_manager.h" +#include "peripheralmanager/i2c_device.h" +#include "peripheralmanager/peripheral_manager_client.h" + +using android::CharDeviceFactory; +using android::FakeDeviceFactory; +using android::I2cDriverI2cDev; +using android::I2cDriverInfo; +using android::I2cDriverInfoBase; +using android::I2cManager; +using android::PeripheralManager; + +class I2cTest : public ::testing::Test { + public: + void SetUp() { + pman_.InitForTest(); + I2cManager* man = I2cManager::GetI2cManager(); + man->RegisterDriver(std::unique_ptr<I2cDriverInfoBase>( + new I2cDriverInfo<I2cDriverI2cDev, CharDeviceFactory*>( + &char_device_factory_))); + + man->RegisterI2cDevBus("I2C0", 0); + man->RegisterI2cDevBus("I2C1", 1); + } + + void TearDown() { I2cManager::ResetI2cManager(); } + + private: + PeripheralManager pman_; + FakeDeviceFactory char_device_factory_; +}; + +// Test that we can list the I2c buses correctly. +TEST_F(I2cTest, ListI2cBuses) { + BPeripheralManagerClient* client = BPeripheralManagerClient_new(); + + int nbuses = -1; + char** devices = BPeripheralManagerClient_listI2cBuses(client, &nbuses); + + ASSERT_EQ(2, nbuses); + EXPECT_EQ("I2C0", std::string(devices[0])); + EXPECT_EQ("I2C1", std::string(devices[1])); + + for (int i = 0; i < nbuses; i++) { + free(devices[i]); + } + free(devices); + + BPeripheralManagerClient_delete(client); +} + +// Test that we can open a device and perform basic operations. +TEST_F(I2cTest, OpenDevice) { + BPeripheralManagerClient* client = BPeripheralManagerClient_new(); + + BI2cDevice* device; + ASSERT_EQ( + 0, BPeripheralManagerClient_openI2cDevice(client, "I2C0", 0x10, &device)); + + uint8_t byte; + EXPECT_EQ(0, BI2cDevice_readRegByte(device, 8, &byte)); + + uint16_t word; + EXPECT_EQ(0, BI2cDevice_readRegWord(device, 8, &word)); + + BI2cDevice_delete(device); + BPeripheralManagerClient_delete(client); +} + +// Test that we can't open the same device twice. +TEST_F(I2cTest, CantOpenDeviceTwice) { + BPeripheralManagerClient* client = BPeripheralManagerClient_new(); + + BI2cDevice* device1 = nullptr; + BI2cDevice* device2 = nullptr; + BI2cDevice* device3 = nullptr; + BI2cDevice* device4 = nullptr; + + // Can open the device once. + ASSERT_EQ( + 0, + BPeripheralManagerClient_openI2cDevice(client, "I2C0", 0x10, &device1)); + ASSERT_NE(nullptr, device1); + + // Can open a device on the same address on a different bus. + ASSERT_EQ( + 0, + BPeripheralManagerClient_openI2cDevice(client, "I2C1", 0x10, &device2)); + ASSERT_NE(nullptr, device2); + + // Can open another device on the same bus. + ASSERT_EQ( + 0, + BPeripheralManagerClient_openI2cDevice(client, "I2C0", 0x13, &device3)); + ASSERT_NE(nullptr, device3); + + // Can't open a device already in use. + ASSERT_EQ( + EBUSY, + BPeripheralManagerClient_openI2cDevice(client, "I2C0", 0x10, &device4)); + ASSERT_EQ(nullptr, device4); + + // After releasing the device, we can re-open it. + BI2cDevice_delete(device1); + ASSERT_EQ( + 0, + BPeripheralManagerClient_openI2cDevice(client, "I2C0", 0x10, &device4)); + ASSERT_NE(nullptr, device4); + + BI2cDevice_delete(device2); + BI2cDevice_delete(device3); + BI2cDevice_delete(device4); + BPeripheralManagerClient_delete(client); +} + +// Test that we cleanly fail to open an unknown device. +TEST_F(I2cTest, CantOpenUnknownDevice) { + BPeripheralManagerClient* client = BPeripheralManagerClient_new(); + + BI2cDevice* device = nullptr; + ASSERT_EQ( + ENODEV, + BPeripheralManagerClient_openI2cDevice(client, "I2C5", 0x10, &device)); + ASSERT_EQ(nullptr, device); + + BPeripheralManagerClient_delete(client); +} diff --git a/client/peripheral_manager_client_impl.cc b/client/peripheral_manager_client_impl.cc index 20b804c..8231102 100644 --- a/client/peripheral_manager_client_impl.cc +++ b/client/peripheral_manager_client_impl.cc @@ -82,3 +82,7 @@ int PeripheralManagerClientImpl::OpenI2cDevice( } return status.serviceSpecificErrorCode(); } + +int PeripheralManagerClientImpl::ListI2cBuses(std::vector<std::string>* buses) { + return client_->ListI2cBuses(buses).serviceSpecificErrorCode(); +} diff --git a/client/peripheral_manager_client_impl.h b/client/peripheral_manager_client_impl.h index 69f7b3b..0eb144a 100644 --- a/client/peripheral_manager_client_impl.h +++ b/client/peripheral_manager_client_impl.h @@ -44,6 +44,7 @@ class PeripheralManagerClientImpl { int OpenI2cDevice(const std::string& name, uint32_t address, std::unique_ptr<I2cDeviceImpl>* device); + int ListI2cBuses(std::vector<std::string>* buses); private: android::sp<android::os::IPeripheralManagerClient> client_; diff --git a/client/wrapper.cc b/client/wrapper.cc index e6d9202..472154a 100644 --- a/client/wrapper.cc +++ b/client/wrapper.cc @@ -249,6 +249,14 @@ void BLed_delete(BLed* led) { } // I2C API +char** BPeripheralManagerClient_listI2cBuses( + const BPeripheralManagerClient* client, int* num_i2c_buses) { + std::vector<std::string> list; + client->impl->ListI2cBuses(&list); + *num_i2c_buses = list.size(); + return ConvertStringVectorToC(list); +} + int BPeripheralManagerClient_openI2cDevice( const BPeripheralManagerClient* client, const char* name, |