diff options
author | Lee Campbell <leecam@google.com> | 2016-02-29 15:20:05 -0800 |
---|---|---|
committer | Lee Campbell <leecam@google.com> | 2016-02-29 15:51:58 -0800 |
commit | aba202d04b995688358f49000eb9fb810a9c82bf (patch) | |
tree | 3e3a1758c7b63892e09d385beaa6210213e719ef /daemon | |
parent | d84d8daea2ae265cd6d63c8684b2d47d6d8fab1d (diff) | |
download | peripheralmanager-aba202d04b995688358f49000eb9fb810a9c82bf.tar.gz |
PeripheralIO - Add Gpio Manager
Adding logic to handle GPIO
BUG: 27406165
Change-Id: I913726e6090ad96676be67a3780cd16ea91148af
Diffstat (limited to 'daemon')
-rw-r--r-- | daemon/Android.mk | 3 | ||||
-rw-r--r-- | daemon/gpio_manager.cc | 97 | ||||
-rw-r--r-- | daemon/gpio_manager.h | 93 | ||||
-rw-r--r-- | daemon/gpio_manager_unittest.cc | 52 | ||||
-rw-r--r-- | daemon/pin_mux_manager.cc | 2 | ||||
-rw-r--r-- | daemon/pin_mux_manager.h | 2 |
6 files changed, 247 insertions, 2 deletions
diff --git a/daemon/Android.mk b/daemon/Android.mk index a369c87..55640f9 100644 --- a/daemon/Android.mk +++ b/daemon/Android.mk @@ -81,6 +81,7 @@ LOCAL_SHARED_LIBRARIES := \ LOCAL_SRC_FILES := \ gpio_driver_sysfs.cc \ + gpio_manager.cc \ peripheral_manager.cc \ pin_mux_manager.cc \ @@ -99,6 +100,7 @@ LOCAL_SHARED_LIBRARIES := \ LOCAL_SRC_FILES := \ gpio_driver_sysfs.cc \ + gpio_manager.cc \ pin_mux_manager.cc \ include $(BUILD_HOST_STATIC_LIBRARY) @@ -116,6 +118,7 @@ LOCAL_SHARED_LIBRARIES := \ libchrome \ LOCAL_SRC_FILES := \ + gpio_manager_unittest.cc \ pin_mux_manager_unittest.cc \ include $(BUILD_HOST_NATIVE_TEST)
\ No newline at end of file diff --git a/daemon/gpio_manager.cc b/daemon/gpio_manager.cc new file mode 100644 index 0000000..5c697b5 --- /dev/null +++ b/daemon/gpio_manager.cc @@ -0,0 +1,97 @@ +/* + * 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 "gpio_manager.h" + +namespace android { + +std::unique_ptr<GpioManager> g_gpio_manager; + +GpioManager::GpioManager() {} + +GpioManager::~GpioManager() {} + +// static +GpioManager* GpioManager::GetGpioManager() { + if (!g_gpio_manager) { + g_gpio_manager.reset(new GpioManager()); + } + return g_gpio_manager.get(); +} + +bool GpioManager::RegisterGpioSysfs(const std::string& name, uint32_t index) { + if (sysfs_pins_.count(name)) + return false; + sysfs_pins_[name].index = index; + return true; +} + +bool GpioManager::SetPinMux(const std::string& name, const std::string& mux) { + if (!sysfs_pins_.count(name)) + return false; + sysfs_pins_[name].mux = mux; + return true; +} + +std::vector<std::string> GpioManager::GetGpioPins() { + std::vector<std::string> pins; + for (auto& i : sysfs_pins_) + pins.push_back(i.first); + return pins; +} + +bool GpioManager::RegisterDriver( + std::unique_ptr<GpioDriverInfoBase> driver_info) { + std::string key = driver_info->Compat(); + driver_infos_[key] = std::move(driver_info); + return true; +} + +std::unique_ptr<GpioPin> GpioManager::OpenGpioPin(const std::string& name) { + // Get the Pin from the BSP. + auto pin_it = sysfs_pins_.find(name); + if (pin_it == sysfs_pins_.end()) + return nullptr; + + // Check its not alread in use + if (pin_it->second.driver_) { + return nullptr; + } + + // Find a driver. + // Currently there is only hardcoded support for GPIOSYSFS + auto driver_info_it = driver_infos_.find("GPIOSYSFS"); + + // Fail if there is no driver. + if (driver_info_it == driver_infos_.end()) + return nullptr; + + std::unique_ptr<GpioDriverInterface> driver(driver_info_it->second->Probe()); + + // Set pin mux + if (!pin_it->second.mux.empty()) { + PinMuxManager::GetPinMuxManager()->SetGpio(pin_it->second.mux); + } + + if (!driver->Init(pin_it->second.index)) + return nullptr; + + pin_it->second.driver_ = std::move(driver); + + return std::unique_ptr<GpioPin>(new GpioPin(&(pin_it->second))); +} + +} // namespace android diff --git a/daemon/gpio_manager.h b/daemon/gpio_manager.h new file mode 100644 index 0000000..62a6fb3 --- /dev/null +++ b/daemon/gpio_manager.h @@ -0,0 +1,93 @@ +/* + * 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 SYSTEM_PERIPHERALMANAGER_DAEMON_GPIO_MANAGER_H_ +#define SYSTEM_PERIPHERALMANAGER_DAEMON_GPIO_MANAGER_H_ + +#include <stdint.h> + +#include <map> +#include <memory> +#include <string> +#include <vector> + +#include <base/macros.h> + +#include "gpio_driver.h" +#include "pin_mux_manager.h" + +namespace android { + +struct GpioPinSysfs { + uint32_t index; + std::string mux; + std::unique_ptr<GpioDriverInterface> driver_; +}; + +class GpioPin { + public: + // TODO(leecam): pins should have a generic device + // reference, not a sysfs one. + GpioPin(GpioPinSysfs* pin) : pin_(pin) {} + ~GpioPin() { pin_->driver_.reset(); } + + bool SetValue(bool val) { return pin_->driver_->SetValue(val); } + + bool GetValue(bool* val) { return pin_->driver_->GetValue(val); } + + bool SetDirection(Direction direction) { + if (!pin_->mux.empty()) { + bool dir = true; + if (direction == kDirectionIn) + dir = false; + PinMuxManager::GetPinMuxManager()->SetGpioDirection(pin_->mux, dir); + } + return pin_->driver_->SetDirection(direction); + } + + private: + GpioPinSysfs* pin_; +}; + +class GpioManager { + public: + friend class GpioManagerTest; + ~GpioManager(); + + // Get the singleton. + static GpioManager* GetGpioManager(); + + // Used by the BSP to tell PMan of an GPIO Pin. + bool RegisterGpioSysfs(const std::string& name, uint32_t index); + bool SetPinMux(const std::string& name, const std::string& mux); + std::vector<std::string> GetGpioPins(); + + bool RegisterDriver(std::unique_ptr<GpioDriverInfoBase> driver_info); + + std::unique_ptr<GpioPin> OpenGpioPin(const std::string& name); + + private: + GpioManager(); + + std::map<std::string, std::unique_ptr<GpioDriverInfoBase>> driver_infos_; + std::map<std::string, GpioPinSysfs> sysfs_pins_; + + DISALLOW_COPY_AND_ASSIGN(GpioManager); +}; + +} // namespace android + +#endif // SYSTEM_PERIPHERALMANAGER_DAEMON_GPIO_MANAGER_H_ diff --git a/daemon/gpio_manager_unittest.cc b/daemon/gpio_manager_unittest.cc new file mode 100644 index 0000000..0e7b07b --- /dev/null +++ b/daemon/gpio_manager_unittest.cc @@ -0,0 +1,52 @@ +/* + * 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 <stdio.h> + +#include <gtest/gtest.h> + +#include "gpio_driver_sysfs.h" +#include "gpio_manager.h" + +namespace android { + +class GpioManagerTest : public ::testing::Test { + public: + GpioManagerTest() {} + ~GpioManagerTest() = default; + + protected: + GpioManager manager; +}; + +TEST_F(GpioManagerTest, RegisterDriver) { + manager.RegisterDriver(std::unique_ptr<GpioDriverInfoBase>( + new GpioDriverInfo<GpioDriverSysfs, void*>(nullptr))); +} + +TEST_F(GpioManagerTest, BasicOpen) { + manager.RegisterDriver(std::unique_ptr<GpioDriverInfoBase>( + new GpioDriverInfo<GpioDriverSysfs, void*>(nullptr))); + + manager.RegisterGpioSysfs("IO11", 40); + + std::unique_ptr<GpioPin> pin(manager.OpenGpioPin("IO11")); +} + +// TODO(leecam): Once this stablizes and has a stub, +// write lots more tests. + +} // namespace android
\ No newline at end of file diff --git a/daemon/pin_mux_manager.cc b/daemon/pin_mux_manager.cc index eb1d5b4..5500c33 100644 --- a/daemon/pin_mux_manager.cc +++ b/daemon/pin_mux_manager.cc @@ -27,7 +27,7 @@ PinMuxManager::PinMuxManager() {} PinMuxManager::~PinMuxManager() {} // static -PinMuxManager* PinMuxManager::GetPinMuxManger() { +PinMuxManager* PinMuxManager::GetPinMuxManager() { if (!g_pin_mux_manager) { g_pin_mux_manager.reset(new PinMuxManager()); } diff --git a/daemon/pin_mux_manager.h b/daemon/pin_mux_manager.h index 48826fd..3d64722 100644 --- a/daemon/pin_mux_manager.h +++ b/daemon/pin_mux_manager.h @@ -50,7 +50,7 @@ class PinMuxManager { ~PinMuxManager(); // Get the singleton. - static PinMuxManager* GetPinMuxManger(); + static PinMuxManager* GetPinMuxManager(); // The following Register* functions as used to configure // the pin mux configuation. These are called by the during |