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( |
George Liu | c78e23d | 2023-08-10 15:49:07 +0800 | [diff] [blame] | 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 | ->expected(0, 1); |
George Liu | 7d7cc9e | 2023-08-02 08:52:01 +0800 | [diff] [blame] | 88 | app.add_option( |
George Liu | c78e23d | 2023-08-10 15:49:07 +0800 | [diff] [blame] | 89 | "-e,--extra-ifaces", ifaces, |
| 90 | "List of interfaces to associate to inventory item\n" |
| 91 | "Format is a comma separated list of interfaces.\n" |
| 92 | "For example: /xyz/openbmc_project/.../1,/xyz/openbmc_project/.../2") |
| 93 | ->expected(0, 1); |
George Liu | 7d7cc9e | 2023-08-02 08:52:01 +0800 | [diff] [blame] | 94 | |
| 95 | /* Parse input parameter */ |
| 96 | try |
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 | app.parse(argc, argv); |
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 | catch (const CLI::Error& e) |
Gunnar Mills | 7263915 | 2017-06-22 15:06:21 -0500 | [diff] [blame] | 101 | { |
George Liu | 7d7cc9e | 2023-08-02 08:52:01 +0800 | [diff] [blame] | 102 | return app.exit(e); |
Gunnar Mills | 7263915 | 2017-06-22 15:06:21 -0500 | [diff] [blame] | 103 | } |
Gunnar Mills | 80292bb | 2017-07-05 16:34:51 -0500 | [diff] [blame] | 104 | |
Matt Spinler | 902d1c3 | 2017-09-01 11:03:02 -0500 | [diff] [blame] | 105 | std::vector<Driver> driverList; |
| 106 | |
Patrick Venture | dace680 | 2018-11-01 16:52:10 -0700 | [diff] [blame] | 107 | // Driver list is optional |
George Liu | 7d7cc9e | 2023-08-02 08:52:01 +0800 | [diff] [blame] | 108 | if (!drivers.empty()) |
Matt Spinler | d5636b0 | 2017-09-01 14:00:19 -0500 | [diff] [blame] | 109 | { |
| 110 | if (getDrivers(drivers, driverList) < 0) |
| 111 | { |
George Liu | 7d7cc9e | 2023-08-02 08:52:01 +0800 | [diff] [blame] | 112 | lg2::error("Failed to parser drivers: {DRIVERS}", "DRIVERS", |
| 113 | drivers); |
| 114 | return -1; |
Matt Spinler | d5636b0 | 2017-09-01 14:00:19 -0500 | [diff] [blame] | 115 | } |
| 116 | } |
Matt Spinler | 902d1c3 | 2017-09-01 11:03:02 -0500 | [diff] [blame] | 117 | |
Anthony Wilson | 206f004 | 2019-05-02 00:02:23 -0500 | [diff] [blame] | 118 | std::vector<Interface> ifaceList; |
| 119 | |
| 120 | // Extra interfaces list is optional |
George Liu | 7d7cc9e | 2023-08-02 08:52:01 +0800 | [diff] [blame] | 121 | if (!ifaces.empty()) |
Anthony Wilson | 206f004 | 2019-05-02 00:02:23 -0500 | [diff] [blame] | 122 | { |
| 123 | std::stringstream ss(ifaces); |
| 124 | Interface iface; |
| 125 | while (std::getline(ss, iface, ',')) |
| 126 | { |
| 127 | ifaceList.push_back(iface); |
| 128 | } |
| 129 | } |
| 130 | |
Gunnar Mills | 80292bb | 2017-07-05 16:34:51 -0500 | [diff] [blame] | 131 | auto bus = sdbusplus::bus::new_default(); |
Gunnar Mills | 765725e | 2017-07-06 14:17:44 -0500 | [diff] [blame] | 132 | auto rc = 0; |
| 133 | sd_event* event = nullptr; |
| 134 | rc = sd_event_default(&event); |
| 135 | if (rc < 0) |
| 136 | { |
George Liu | 2a8848c | 2023-08-01 13:49:28 +0800 | [diff] [blame] | 137 | lg2::error("Error creating a default sd_event handler"); |
Gunnar Mills | 765725e | 2017-07-06 14:17:44 -0500 | [diff] [blame] | 138 | return rc; |
| 139 | } |
| 140 | EventPtr eventP{event}; |
| 141 | event = nullptr; |
Gunnar Mills | 7263915 | 2017-06-22 15:06:21 -0500 | [diff] [blame] | 142 | |
Patrick Venture | dace680 | 2018-11-01 16:52:10 -0700 | [diff] [blame] | 143 | Presence presence(bus, inventory, path, std::stoul(key), name, eventP, |
Anthony Wilson | 206f004 | 2019-05-02 00:02:23 -0500 | [diff] [blame] | 144 | driverList, ifaceList); |
Gunnar Mills | 765725e | 2017-07-06 14:17:44 -0500 | [diff] [blame] | 145 | |
| 146 | while (true) |
| 147 | { |
| 148 | // -1 denotes wait forever |
Patrick Venture | dace680 | 2018-11-01 16:52:10 -0700 | [diff] [blame] | 149 | rc = sd_event_run(eventP.get(), (uint64_t)-1); |
Gunnar Mills | 765725e | 2017-07-06 14:17:44 -0500 | [diff] [blame] | 150 | if (rc < 0) |
| 151 | { |
George Liu | 2a8848c | 2023-08-01 13:49:28 +0800 | [diff] [blame] | 152 | lg2::error("Failure in processing request: {RC}", "RC", rc); |
Gunnar Mills | 765725e | 2017-07-06 14:17:44 -0500 | [diff] [blame] | 153 | break; |
| 154 | } |
| 155 | } |
| 156 | return rc; |
Gunnar Mills | 7263915 | 2017-06-22 15:06:21 -0500 | [diff] [blame] | 157 | } |