blob: eace07e88c7fda2146e090eacb1b3e76608c33ea [file] [log] [blame]
Gunnar Mills72639152017-06-22 15:06:21 -05001#include "argument.hpp"
2#include "gpio_presence.hpp"
3
Patrick Venturedace6802018-11-01 16:52:10 -07004#include <systemd/sd-event.h>
5
6#include <iostream>
7#include <phosphor-logging/log.hpp>
8
Gunnar Mills72639152017-06-22 15:06:21 -05009using namespace phosphor::logging;
10using namespace phosphor::gpio;
Gunnar Mills5f101102017-06-29 13:07:39 -050011using namespace phosphor::gpio::presence;
Gunnar Mills72639152017-06-22 15:06:21 -050012
Matt Spinlerd5636b02017-09-01 14:00:19 -050013/**
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 Venture3c4a23e2018-10-14 14:26:35 -070023static int getDrivers(const std::string& driverString,
Matt Spinlerd5636b02017-09-01 14:00:19 -050024 std::vector<Driver>& drivers)
25{
26 std::istringstream stream{driverString};
27
28 while (true)
29 {
30 std::string entry;
31
Patrick Venturedace6802018-11-01 16:52:10 -070032 // Extract each path,device pair
Matt Spinlerd5636b02017-09-01 14:00:19 -050033 stream >> entry;
34
35 if (entry.empty())
36 {
37 break;
38 }
39
Patrick Venturedace6802018-11-01 16:52:10 -070040 // Extract the path and device and save them
Matt Spinlerd5636b02017-09-01 14:00:19 -050041 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 Mills72639152017-06-22 15:06:21 -050059int 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 Spinlerd5636b02017-09-01 14:00:19 -050066 auto drivers = options["drivers"];
Anthony Wilson206f0042019-05-02 00:02:23 -050067 auto ifaces = options["extra-ifaces"];
Gunnar Mills72639152017-06-22 15:06:21 -050068 if (argc < 4)
69 {
70 std::cerr << "Too few arguments\n";
71 options.usage(argv);
72 }
73
74 if (inventory == ArgumentParser::emptyString)
75 {
76 std::cerr << "Inventory argument required\n";
77 options.usage(argv);
78 }
79
80 if (key == ArgumentParser::emptyString)
81 {
82 std::cerr << "GPIO key argument required\n";
83 options.usage(argv);
84 }
85
86 if (path == ArgumentParser::emptyString)
87 {
88 std::cerr << "Device path argument required\n";
89 options.usage(argv);
90 }
Gunnar Mills80292bb2017-07-05 16:34:51 -050091
Matt Spinler902d1c32017-09-01 11:03:02 -050092 std::vector<Driver> driverList;
93
Patrick Venturedace6802018-11-01 16:52:10 -070094 // Driver list is optional
Matt Spinlerd5636b02017-09-01 14:00:19 -050095 if (drivers != ArgumentParser::emptyString)
96 {
97 if (getDrivers(drivers, driverList) < 0)
98 {
99 options.usage(argv);
100 }
101 }
Matt Spinler902d1c32017-09-01 11:03:02 -0500102
Anthony Wilson206f0042019-05-02 00:02:23 -0500103 std::vector<Interface> ifaceList;
104
105 // Extra interfaces list is optional
106 if (ifaces != ArgumentParser::emptyString)
107 {
108 std::stringstream ss(ifaces);
109 Interface iface;
110 while (std::getline(ss, iface, ','))
111 {
112 ifaceList.push_back(iface);
113 }
114 }
115
Gunnar Mills80292bb2017-07-05 16:34:51 -0500116 auto bus = sdbusplus::bus::new_default();
Gunnar Mills765725e2017-07-06 14:17:44 -0500117 auto rc = 0;
118 sd_event* event = nullptr;
119 rc = sd_event_default(&event);
120 if (rc < 0)
121 {
122 log<level::ERR>("Error creating a default sd_event handler");
123 return rc;
124 }
125 EventPtr eventP{event};
126 event = nullptr;
Gunnar Mills72639152017-06-22 15:06:21 -0500127
Gunnar Mills765725e2017-07-06 14:17:44 -0500128 auto name = options["name"];
Patrick Venturedace6802018-11-01 16:52:10 -0700129 Presence presence(bus, inventory, path, std::stoul(key), name, eventP,
Anthony Wilson206f0042019-05-02 00:02:23 -0500130 driverList, ifaceList);
Gunnar Mills765725e2017-07-06 14:17:44 -0500131
132 while (true)
133 {
134 // -1 denotes wait forever
Patrick Venturedace6802018-11-01 16:52:10 -0700135 rc = sd_event_run(eventP.get(), (uint64_t)-1);
Gunnar Mills765725e2017-07-06 14:17:44 -0500136 if (rc < 0)
137 {
138 log<level::ERR>("Failure in processing request",
139 entry("ERROR=%s", strerror(-rc)));
140 break;
141 }
142 }
143 return rc;
Gunnar Mills72639152017-06-22 15:06:21 -0500144}