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