aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBertrand SIMONNET <bsimonnet@google.com>2016-03-23 12:20:23 -0700
committerBertrand SIMONNET <bsimonnet@google.com>2016-03-23 14:13:28 -0700
commitf4df29ac8ed4c45868b9bf93ad1d88478a92ceef (patch)
treeb5a827aeed88039fe15ff7108dceb191d3568ec8
parent6187530413fd184856f85ea09bd30040e5426422 (diff)
downloadperipheralmanager-f4df29ac8ed4c45868b9bf93ad1d88478a92ceef.tar.gz
Add tests for the I2c developer C API.
Bug: 27675223 Change-Id: I751083324c09aff20a93290897fc4676ce247722
-rw-r--r--client/Android.mk1
-rw-r--r--client/i2c_unittest.cc149
-rw-r--r--client/peripheral_manager_client_impl.cc4
-rw-r--r--client/peripheral_manager_client_impl.h1
-rw-r--r--client/wrapper.cc8
-rw-r--r--daemon/i2c_manager.cc5
-rw-r--r--daemon/i2c_manager.h5
-rw-r--r--daemon/peripheral_manager_client.cc6
-rw-r--r--daemon/peripheral_manager_client.h2
-rw-r--r--ipc/android/os/IPeripheralManagerClient.aidl2
10 files changed, 182 insertions, 1 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,
diff --git a/daemon/i2c_manager.cc b/daemon/i2c_manager.cc
index 52035d1..7ca084e 100644
--- a/daemon/i2c_manager.cc
+++ b/daemon/i2c_manager.cc
@@ -36,6 +36,11 @@ I2cManager* I2cManager::GetI2cManager() {
return g_i2c_manager.get();
}
+// static
+void I2cManager::ResetI2cManager() {
+ g_i2c_manager.reset();
+}
+
bool I2cManager::RegisterI2cDevBus(const std::string& name,
uint32_t bus) {
if (i2cdev_buses_.count(name))
diff --git a/daemon/i2c_manager.h b/daemon/i2c_manager.h
index 46bee7e..16842d7 100644
--- a/daemon/i2c_manager.h
+++ b/daemon/i2c_manager.h
@@ -72,6 +72,9 @@ class I2cManager {
// Get the singleton.
static I2cManager* GetI2cManager();
+ // Resets the global I2c manager (used for testing).
+ static void ResetI2cManager();
+
bool RegisterI2cDevBus(const std::string& name, uint32_t bus);
std::vector<std::string> GetI2cDevBuses();
@@ -98,4 +101,4 @@ class I2cManager {
} // namespace android
-#endif // SYSTEM_PERIPHERALMANAGER_DAEMON_I2C_MANAGER_H_ \ No newline at end of file
+#endif // SYSTEM_PERIPHERALMANAGER_DAEMON_I2C_MANAGER_H_
diff --git a/daemon/peripheral_manager_client.cc b/daemon/peripheral_manager_client.cc
index 7779775..e821288 100644
--- a/daemon/peripheral_manager_client.cc
+++ b/daemon/peripheral_manager_client.cc
@@ -298,6 +298,12 @@ Status PeripheralManagerClient::LedSetBrightness(const std::string& name,
return Status::fromServiceSpecificError(EREMOTEIO);
}
+Status PeripheralManagerClient::ListI2cBuses(std::vector<std::string>* buses) {
+ *buses = I2cManager::GetI2cManager()->GetI2cDevBuses();
+
+ return Status::ok();
+}
+
Status PeripheralManagerClient::OpenI2cDevice(const std::string& name,
int32_t address) {
if (!I2cManager::GetI2cManager()->HasI2cDevBus(name))
diff --git a/daemon/peripheral_manager_client.h b/daemon/peripheral_manager_client.h
index ab7ded5..99f3999 100644
--- a/daemon/peripheral_manager_client.h
+++ b/daemon/peripheral_manager_client.h
@@ -105,6 +105,8 @@ class PeripheralManagerClient : public BnPeripheralManagerClient {
virtual Status LedSetBrightness(const std::string& name,
int brightness) override;
+ virtual Status ListI2cBuses(std::vector<std::string>* buses) override;
+
virtual Status OpenI2cDevice(const std::string& name,
int32_t address) override;
diff --git a/ipc/android/os/IPeripheralManagerClient.aidl b/ipc/android/os/IPeripheralManagerClient.aidl
index 0fb3561..bdd7ac6 100644
--- a/ipc/android/os/IPeripheralManagerClient.aidl
+++ b/ipc/android/os/IPeripheralManagerClient.aidl
@@ -69,6 +69,8 @@ interface IPeripheralManagerClient {
void LedSetBrightness(@utf8InCpp String name, int brightness);
+ void ListI2cBuses(out @utf8InCpp List<String> buses);
+
void OpenI2cDevice(@utf8InCpp String name, int address);
void ReleaseI2cDevice(@utf8InCpp String name, int address);