| Christopher Meis | 75ff167 | 2025-09-23 11:40:06 +0200 | [diff] [blame^] | 1 | // SPDX-License-Identifier: Apache-2.0 |
| 2 | // SPDX-FileCopyrightText: Copyright OpenBMC Authors |
| 3 | |
| Gunnar Mills | 7263915 | 2017-06-22 15:06:21 -0500 | [diff] [blame] | 4 | #include "gpio_presence.hpp" |
| 5 | |
| Patrick Venture | dace680 | 2018-11-01 16:52:10 -0700 | [diff] [blame] | 6 | #include <systemd/sd-event.h> |
| 7 | |
| George Liu | 7d7cc9e | 2023-08-02 08:52:01 +0800 | [diff] [blame] | 8 | #include <CLI/CLI.hpp> |
| George Liu | 2a8848c | 2023-08-01 13:49:28 +0800 | [diff] [blame] | 9 | #include <phosphor-logging/lg2.hpp> |
| Patrick Venture | dace680 | 2018-11-01 16:52:10 -0700 | [diff] [blame] | 10 | |
| Patrick Williams | 39084b4 | 2023-05-10 07:50:58 -0500 | [diff] [blame] | 11 | #include <iostream> |
| 12 | |
| Gunnar Mills | 7263915 | 2017-06-22 15:06:21 -0500 | [diff] [blame] | 13 | using namespace phosphor::gpio; |
| Gunnar Mills | 5f10110 | 2017-06-29 13:07:39 -0500 | [diff] [blame] | 14 | using namespace phosphor::gpio::presence; |
| Gunnar Mills | 7263915 | 2017-06-22 15:06:21 -0500 | [diff] [blame] | 15 | |
| Matt Spinler | d5636b0 | 2017-09-01 14:00:19 -0500 | [diff] [blame] | 16 | /** |
| 17 | * Pulls out the path,device pairs from the string |
| 18 | * passed in |
| 19 | * |
| 20 | * @param[in] driverString - space separated path,device pairs |
| 21 | * @param[out] drivers - vector of device,path tuples filled in |
| 22 | * from driverString |
| 23 | * |
| 24 | * @return int - 0 if successful, < 0 else |
| 25 | */ |
| Patrick Venture | 3c4a23e | 2018-10-14 14:26:35 -0700 | [diff] [blame] | 26 | static int getDrivers(const std::string& driverString, |
| Matt Spinler | d5636b0 | 2017-09-01 14:00:19 -0500 | [diff] [blame] | 27 | std::vector<Driver>& drivers) |
| 28 | { |
| 29 | std::istringstream stream{driverString}; |
| 30 | |
| 31 | while (true) |
| 32 | { |
| 33 | std::string entry; |
| 34 | |
| Patrick Venture | dace680 | 2018-11-01 16:52:10 -0700 | [diff] [blame] | 35 | // Extract each path,device pair |
| Matt Spinler | d5636b0 | 2017-09-01 14:00:19 -0500 | [diff] [blame] | 36 | stream >> entry; |
| 37 | |
| 38 | if (entry.empty()) |
| 39 | { |
| 40 | break; |
| 41 | } |
| 42 | |
| Patrick Venture | dace680 | 2018-11-01 16:52:10 -0700 | [diff] [blame] | 43 | // Extract the path and device and save them |
| Matt Spinler | d5636b0 | 2017-09-01 14:00:19 -0500 | [diff] [blame] | 44 | auto pos = entry.rfind(','); |
| 45 | if (pos != std::string::npos) |
| 46 | { |
| 47 | auto path = entry.substr(0, pos); |
| 48 | auto device = entry.substr(pos + 1); |
| 49 | |
| Patrick Williams | 162bd71 | 2023-06-01 09:19:10 -0500 | [diff] [blame] | 50 | drivers.emplace_back(std::move(device), std::move(path)); |
| Matt Spinler | d5636b0 | 2017-09-01 14:00:19 -0500 | [diff] [blame] | 51 | } |
| 52 | else |
| 53 | { |
| George Liu | 2a8848c | 2023-08-01 13:49:28 +0800 | [diff] [blame] | 54 | lg2::error("Invalid path,device combination: {ENTRY}", "ENTRY", |
| 55 | entry); |
| Matt Spinler | d5636b0 | 2017-09-01 14:00:19 -0500 | [diff] [blame] | 56 | return -1; |
| 57 | } |
| 58 | } |
| 59 | |
| 60 | return 0; |
| 61 | } |
| 62 | |
| George Liu | 7d7cc9e | 2023-08-02 08:52:01 +0800 | [diff] [blame] | 63 | int main(int argc, char** argv) |
| Gunnar Mills | 7263915 | 2017-06-22 15:06:21 -0500 | [diff] [blame] | 64 | { |
| George Liu | 7d7cc9e | 2023-08-02 08:52:01 +0800 | [diff] [blame] | 65 | CLI::App app{"Monitor gpio presence status"}; |
| Gunnar Mills | 7263915 | 2017-06-22 15:06:21 -0500 | [diff] [blame] | 66 | |
| George Liu | 7d7cc9e | 2023-08-02 08:52:01 +0800 | [diff] [blame] | 67 | std::string path{}; |
| 68 | std::string key{}; |
| 69 | std::string name{}; |
| 70 | std::string inventory{}; |
| 71 | std::string drivers{}; |
| 72 | std::string ifaces{}; |
| 73 | |
| 74 | /* Add an input option */ |
| 75 | app.add_option( |
| 76 | "-p,--path", path, |
| 77 | " Path of device to read for GPIO pin state to determine presence of inventory item") |
| 78 | ->required(); |
| 79 | app.add_option("-k,--key", key, "Input GPIO key number")->required(); |
| 80 | app.add_option("-n,--name", name, "Pretty name of the inventory item") |
| 81 | ->required(); |
| 82 | app.add_option("-i,--inventory", inventory, |
| 83 | "Object path under inventory that will be created") |
| 84 | ->required(); |
| 85 | app.add_option( |
| George Liu | c78e23d | 2023-08-10 15:49:07 +0800 | [diff] [blame] | 86 | "-d,--drivers", drivers, |
| 87 | "List of drivers to bind when card is added and unbind when card is removed\n" |
| 88 | "Format is a space separated list of path,device pairs.\n" |
| 89 | "For example: /sys/bus/i2c/drivers/some-driver,3-0068") |
| 90 | ->expected(0, 1); |
| George Liu | 7d7cc9e | 2023-08-02 08:52:01 +0800 | [diff] [blame] | 91 | app.add_option( |
| George Liu | c78e23d | 2023-08-10 15:49:07 +0800 | [diff] [blame] | 92 | "-e,--extra-ifaces", ifaces, |
| 93 | "List of interfaces to associate to inventory item\n" |
| 94 | "Format is a comma separated list of interfaces.\n" |
| 95 | "For example: /xyz/openbmc_project/.../1,/xyz/openbmc_project/.../2") |
| 96 | ->expected(0, 1); |
| George Liu | 7d7cc9e | 2023-08-02 08:52:01 +0800 | [diff] [blame] | 97 | |
| 98 | /* Parse input parameter */ |
| 99 | try |
| Gunnar Mills | 7263915 | 2017-06-22 15:06:21 -0500 | [diff] [blame] | 100 | { |
| George Liu | 7d7cc9e | 2023-08-02 08:52:01 +0800 | [diff] [blame] | 101 | app.parse(argc, argv); |
| Gunnar Mills | 7263915 | 2017-06-22 15:06:21 -0500 | [diff] [blame] | 102 | } |
| George Liu | 7d7cc9e | 2023-08-02 08:52:01 +0800 | [diff] [blame] | 103 | catch (const CLI::Error& e) |
| Gunnar Mills | 7263915 | 2017-06-22 15:06:21 -0500 | [diff] [blame] | 104 | { |
| George Liu | 7d7cc9e | 2023-08-02 08:52:01 +0800 | [diff] [blame] | 105 | return app.exit(e); |
| Gunnar Mills | 7263915 | 2017-06-22 15:06:21 -0500 | [diff] [blame] | 106 | } |
| Gunnar Mills | 80292bb | 2017-07-05 16:34:51 -0500 | [diff] [blame] | 107 | |
| Matt Spinler | 902d1c3 | 2017-09-01 11:03:02 -0500 | [diff] [blame] | 108 | std::vector<Driver> driverList; |
| 109 | |
| Patrick Venture | dace680 | 2018-11-01 16:52:10 -0700 | [diff] [blame] | 110 | // Driver list is optional |
| George Liu | 7d7cc9e | 2023-08-02 08:52:01 +0800 | [diff] [blame] | 111 | if (!drivers.empty()) |
| Matt Spinler | d5636b0 | 2017-09-01 14:00:19 -0500 | [diff] [blame] | 112 | { |
| 113 | if (getDrivers(drivers, driverList) < 0) |
| 114 | { |
| George Liu | 7d7cc9e | 2023-08-02 08:52:01 +0800 | [diff] [blame] | 115 | lg2::error("Failed to parser drivers: {DRIVERS}", "DRIVERS", |
| 116 | drivers); |
| 117 | return -1; |
| Matt Spinler | d5636b0 | 2017-09-01 14:00:19 -0500 | [diff] [blame] | 118 | } |
| 119 | } |
| Matt Spinler | 902d1c3 | 2017-09-01 11:03:02 -0500 | [diff] [blame] | 120 | |
| Anthony Wilson | 206f004 | 2019-05-02 00:02:23 -0500 | [diff] [blame] | 121 | std::vector<Interface> ifaceList; |
| 122 | |
| 123 | // Extra interfaces list is optional |
| George Liu | 7d7cc9e | 2023-08-02 08:52:01 +0800 | [diff] [blame] | 124 | if (!ifaces.empty()) |
| Anthony Wilson | 206f004 | 2019-05-02 00:02:23 -0500 | [diff] [blame] | 125 | { |
| 126 | std::stringstream ss(ifaces); |
| 127 | Interface iface; |
| 128 | while (std::getline(ss, iface, ',')) |
| 129 | { |
| 130 | ifaceList.push_back(iface); |
| 131 | } |
| 132 | } |
| 133 | |
| Gunnar Mills | 80292bb | 2017-07-05 16:34:51 -0500 | [diff] [blame] | 134 | auto bus = sdbusplus::bus::new_default(); |
| Gunnar Mills | 765725e | 2017-07-06 14:17:44 -0500 | [diff] [blame] | 135 | auto rc = 0; |
| 136 | sd_event* event = nullptr; |
| 137 | rc = sd_event_default(&event); |
| 138 | if (rc < 0) |
| 139 | { |
| George Liu | 2a8848c | 2023-08-01 13:49:28 +0800 | [diff] [blame] | 140 | lg2::error("Error creating a default sd_event handler"); |
| Gunnar Mills | 765725e | 2017-07-06 14:17:44 -0500 | [diff] [blame] | 141 | return rc; |
| 142 | } |
| 143 | EventPtr eventP{event}; |
| 144 | event = nullptr; |
| Gunnar Mills | 7263915 | 2017-06-22 15:06:21 -0500 | [diff] [blame] | 145 | |
| Patrick Venture | dace680 | 2018-11-01 16:52:10 -0700 | [diff] [blame] | 146 | Presence presence(bus, inventory, path, std::stoul(key), name, eventP, |
| Anthony Wilson | 206f004 | 2019-05-02 00:02:23 -0500 | [diff] [blame] | 147 | driverList, ifaceList); |
| Gunnar Mills | 765725e | 2017-07-06 14:17:44 -0500 | [diff] [blame] | 148 | |
| 149 | while (true) |
| 150 | { |
| 151 | // -1 denotes wait forever |
| Patrick Venture | dace680 | 2018-11-01 16:52:10 -0700 | [diff] [blame] | 152 | rc = sd_event_run(eventP.get(), (uint64_t)-1); |
| Gunnar Mills | 765725e | 2017-07-06 14:17:44 -0500 | [diff] [blame] | 153 | if (rc < 0) |
| 154 | { |
| George Liu | 2a8848c | 2023-08-01 13:49:28 +0800 | [diff] [blame] | 155 | lg2::error("Failure in processing request: {RC}", "RC", rc); |
| Gunnar Mills | 765725e | 2017-07-06 14:17:44 -0500 | [diff] [blame] | 156 | break; |
| 157 | } |
| 158 | } |
| 159 | return rc; |
| Gunnar Mills | 7263915 | 2017-06-22 15:06:21 -0500 | [diff] [blame] | 160 | } |