Delphine CC Chiu | ccd7db0 | 2023-02-09 14:48:53 +0800 | [diff] [blame] | 1 | |
Delphine CC Chiu | ccd7db0 | 2023-02-09 14:48:53 +0800 | [diff] [blame] | 2 | #include "button_config.hpp" |
Delphine CC Chiu | 15c60e2 | 2024-04-12 13:01:32 -0500 | [diff] [blame^] | 3 | #include "config.hpp" |
Delphine CC Chiu | ccd7db0 | 2023-02-09 14:48:53 +0800 | [diff] [blame] | 4 | |
| 5 | #include <error.h> |
| 6 | #include <fcntl.h> |
| 7 | #include <unistd.h> |
| 8 | |
| 9 | #include <phosphor-logging/lg2.hpp> |
| 10 | |
| 11 | const std::string cpldDev = "/sys/bus/i2c/devices/"; |
| 12 | |
| 13 | std::string getCpldDevPath(const CpldInfo& info) |
| 14 | { |
| 15 | std::stringstream devPath; |
| 16 | devPath << cpldDev << info.i2cBus << "-" << std::hex << std::setw(4) |
| 17 | << std::setfill('0') << info.i2cAddress << "/" << info.registerName; |
| 18 | return devPath.str(); |
| 19 | } |
| 20 | |
| 21 | int configCpld(ButtonConfig& buttonIFConfig) |
| 22 | { |
| 23 | std::string devPath = getCpldDevPath(buttonIFConfig.cpld); |
| 24 | |
| 25 | auto fd = ::open(devPath.c_str(), O_RDONLY | O_NONBLOCK); |
| 26 | |
| 27 | if (fd < 0) |
| 28 | { |
| 29 | lg2::error("Open {PATH} error: {ERROR}", "PATH", devPath, "ERROR", |
| 30 | errno); |
| 31 | return -1; |
| 32 | } |
| 33 | |
| 34 | buttonIFConfig.cpld.cpldMappedFd = fd; |
| 35 | buttonIFConfig.fds.push_back(fd); |
| 36 | return 0; |
| 37 | } |