Gunnar Mills | 7263915 | 2017-06-22 15:06:21 -0500 | [diff] [blame] | 1 | #include "argument.hpp" |
| 2 | #include "gpio_presence.hpp" |
| 3 | |
Patrick Venture | dace680 | 2018-11-01 16:52:10 -0700 | [diff] [blame] | 4 | #include <systemd/sd-event.h> |
| 5 | |
| 6 | #include <iostream> |
| 7 | #include <phosphor-logging/log.hpp> |
| 8 | |
Gunnar Mills | 7263915 | 2017-06-22 15:06:21 -0500 | [diff] [blame] | 9 | using namespace phosphor::logging; |
| 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 | |
| 47 | drivers.emplace_back(device, path); |
| 48 | } |
| 49 | else |
| 50 | { |
| 51 | std::cerr << "Invalid path,device combination: " << entry << "\n"; |
| 52 | return -1; |
| 53 | } |
| 54 | } |
| 55 | |
| 56 | return 0; |
| 57 | } |
| 58 | |
Gunnar Mills | 7263915 | 2017-06-22 15:06:21 -0500 | [diff] [blame] | 59 | int main(int argc, char* argv[]) |
| 60 | { |
| 61 | auto options = ArgumentParser(argc, argv); |
| 62 | |
| 63 | auto inventory = options["inventory"]; |
| 64 | auto key = options["key"]; |
| 65 | auto path = options["path"]; |
Matt Spinler | d5636b0 | 2017-09-01 14:00:19 -0500 | [diff] [blame] | 66 | auto drivers = options["drivers"]; |
Gunnar Mills | 7263915 | 2017-06-22 15:06:21 -0500 | [diff] [blame] | 67 | if (argc < 4) |
| 68 | { |
| 69 | std::cerr << "Too few arguments\n"; |
| 70 | options.usage(argv); |
| 71 | } |
| 72 | |
| 73 | if (inventory == ArgumentParser::emptyString) |
| 74 | { |
| 75 | std::cerr << "Inventory argument required\n"; |
| 76 | options.usage(argv); |
| 77 | } |
| 78 | |
| 79 | if (key == ArgumentParser::emptyString) |
| 80 | { |
| 81 | std::cerr << "GPIO key argument required\n"; |
| 82 | options.usage(argv); |
| 83 | } |
| 84 | |
| 85 | if (path == ArgumentParser::emptyString) |
| 86 | { |
| 87 | std::cerr << "Device path argument required\n"; |
| 88 | options.usage(argv); |
| 89 | } |
Gunnar Mills | 80292bb | 2017-07-05 16:34:51 -0500 | [diff] [blame] | 90 | |
Matt Spinler | 902d1c3 | 2017-09-01 11:03:02 -0500 | [diff] [blame] | 91 | std::vector<Driver> driverList; |
| 92 | |
Patrick Venture | dace680 | 2018-11-01 16:52:10 -0700 | [diff] [blame] | 93 | // Driver list is optional |
Matt Spinler | d5636b0 | 2017-09-01 14:00:19 -0500 | [diff] [blame] | 94 | if (drivers != ArgumentParser::emptyString) |
| 95 | { |
| 96 | if (getDrivers(drivers, driverList) < 0) |
| 97 | { |
| 98 | options.usage(argv); |
| 99 | } |
| 100 | } |
Matt Spinler | 902d1c3 | 2017-09-01 11:03:02 -0500 | [diff] [blame] | 101 | |
Gunnar Mills | 80292bb | 2017-07-05 16:34:51 -0500 | [diff] [blame] | 102 | auto bus = sdbusplus::bus::new_default(); |
Gunnar Mills | 765725e | 2017-07-06 14:17:44 -0500 | [diff] [blame] | 103 | auto rc = 0; |
| 104 | sd_event* event = nullptr; |
| 105 | rc = sd_event_default(&event); |
| 106 | if (rc < 0) |
| 107 | { |
| 108 | log<level::ERR>("Error creating a default sd_event handler"); |
| 109 | return rc; |
| 110 | } |
| 111 | EventPtr eventP{event}; |
| 112 | event = nullptr; |
Gunnar Mills | 7263915 | 2017-06-22 15:06:21 -0500 | [diff] [blame] | 113 | |
Gunnar Mills | 765725e | 2017-07-06 14:17:44 -0500 | [diff] [blame] | 114 | auto name = options["name"]; |
Patrick Venture | dace680 | 2018-11-01 16:52:10 -0700 | [diff] [blame] | 115 | Presence presence(bus, inventory, path, std::stoul(key), name, eventP, |
| 116 | driverList); |
Gunnar Mills | 765725e | 2017-07-06 14:17:44 -0500 | [diff] [blame] | 117 | |
| 118 | while (true) |
| 119 | { |
| 120 | // -1 denotes wait forever |
Patrick Venture | dace680 | 2018-11-01 16:52:10 -0700 | [diff] [blame] | 121 | rc = sd_event_run(eventP.get(), (uint64_t)-1); |
Gunnar Mills | 765725e | 2017-07-06 14:17:44 -0500 | [diff] [blame] | 122 | if (rc < 0) |
| 123 | { |
| 124 | log<level::ERR>("Failure in processing request", |
| 125 | entry("ERROR=%s", strerror(-rc))); |
| 126 | break; |
| 127 | } |
| 128 | } |
| 129 | return rc; |
Gunnar Mills | 7263915 | 2017-06-22 15:06:21 -0500 | [diff] [blame] | 130 | } |