blob: cb573fcd07e1e10285de0ee65a49b74129e85025 [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
George Liu2a8848c2023-08-01 13:49:28 +08006#include <phosphor-logging/lg2.hpp>
Patrick Venturedace6802018-11-01 16:52:10 -07007
Patrick Williams39084b42023-05-10 07:50:58 -05008#include <iostream>
9
Gunnar Mills72639152017-06-22 15:06:21 -050010using 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
Patrick Williams162bd712023-06-01 09:19:10 -050047 drivers.emplace_back(std::move(device), std::move(path));
Matt Spinlerd5636b02017-09-01 14:00:19 -050048 }
49 else
50 {
George Liu2a8848c2023-08-01 13:49:28 +080051 lg2::error("Invalid path,device combination: {ENTRY}", "ENTRY",
52 entry);
Matt Spinlerd5636b02017-09-01 14:00:19 -050053 return -1;
54 }
55 }
56
57 return 0;
58}
59
Gunnar Mills72639152017-06-22 15:06:21 -050060int main(int argc, char* argv[])
61{
62 auto options = ArgumentParser(argc, argv);
63
64 auto inventory = options["inventory"];
65 auto key = options["key"];
66 auto path = options["path"];
Matt Spinlerd5636b02017-09-01 14:00:19 -050067 auto drivers = options["drivers"];
Anthony Wilson206f0042019-05-02 00:02:23 -050068 auto ifaces = options["extra-ifaces"];
Gunnar Mills72639152017-06-22 15:06:21 -050069 if (argc < 4)
70 {
71 std::cerr << "Too few arguments\n";
72 options.usage(argv);
73 }
74
75 if (inventory == ArgumentParser::emptyString)
76 {
77 std::cerr << "Inventory argument required\n";
78 options.usage(argv);
79 }
80
81 if (key == ArgumentParser::emptyString)
82 {
83 std::cerr << "GPIO key argument required\n";
84 options.usage(argv);
85 }
86
87 if (path == ArgumentParser::emptyString)
88 {
89 std::cerr << "Device path argument required\n";
90 options.usage(argv);
91 }
Gunnar Mills80292bb2017-07-05 16:34:51 -050092
Matt Spinler902d1c32017-09-01 11:03:02 -050093 std::vector<Driver> driverList;
94
Patrick Venturedace6802018-11-01 16:52:10 -070095 // Driver list is optional
Matt Spinlerd5636b02017-09-01 14:00:19 -050096 if (drivers != ArgumentParser::emptyString)
97 {
98 if (getDrivers(drivers, driverList) < 0)
99 {
100 options.usage(argv);
101 }
102 }
Matt Spinler902d1c32017-09-01 11:03:02 -0500103
Anthony Wilson206f0042019-05-02 00:02:23 -0500104 std::vector<Interface> ifaceList;
105
106 // Extra interfaces list is optional
107 if (ifaces != ArgumentParser::emptyString)
108 {
109 std::stringstream ss(ifaces);
110 Interface iface;
111 while (std::getline(ss, iface, ','))
112 {
113 ifaceList.push_back(iface);
114 }
115 }
116
Gunnar Mills80292bb2017-07-05 16:34:51 -0500117 auto bus = sdbusplus::bus::new_default();
Gunnar Mills765725e2017-07-06 14:17:44 -0500118 auto rc = 0;
119 sd_event* event = nullptr;
120 rc = sd_event_default(&event);
121 if (rc < 0)
122 {
George Liu2a8848c2023-08-01 13:49:28 +0800123 lg2::error("Error creating a default sd_event handler");
Gunnar Mills765725e2017-07-06 14:17:44 -0500124 return rc;
125 }
126 EventPtr eventP{event};
127 event = nullptr;
Gunnar Mills72639152017-06-22 15:06:21 -0500128
Gunnar Mills765725e2017-07-06 14:17:44 -0500129 auto name = options["name"];
Patrick Venturedace6802018-11-01 16:52:10 -0700130 Presence presence(bus, inventory, path, std::stoul(key), name, eventP,
Anthony Wilson206f0042019-05-02 00:02:23 -0500131 driverList, ifaceList);
Gunnar Mills765725e2017-07-06 14:17:44 -0500132
133 while (true)
134 {
135 // -1 denotes wait forever
Patrick Venturedace6802018-11-01 16:52:10 -0700136 rc = sd_event_run(eventP.get(), (uint64_t)-1);
Gunnar Mills765725e2017-07-06 14:17:44 -0500137 if (rc < 0)
138 {
George Liu2a8848c2023-08-01 13:49:28 +0800139 lg2::error("Failure in processing request: {RC}", "RC", rc);
Gunnar Mills765725e2017-07-06 14:17:44 -0500140 break;
141 }
142 }
143 return rc;
Gunnar Mills72639152017-06-22 15:06:21 -0500144}