blob: 16866d4d5d2e4bc7c13724d1de8c7b66c337ce9c [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"];
Gunnar Mills72639152017-06-22 15:06:21 -050067 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 Mills80292bb2017-07-05 16:34:51 -050090
Matt Spinler902d1c32017-09-01 11:03:02 -050091 std::vector<Driver> driverList;
92
Patrick Venturedace6802018-11-01 16:52:10 -070093 // Driver list is optional
Matt Spinlerd5636b02017-09-01 14:00:19 -050094 if (drivers != ArgumentParser::emptyString)
95 {
96 if (getDrivers(drivers, driverList) < 0)
97 {
98 options.usage(argv);
99 }
100 }
Matt Spinler902d1c32017-09-01 11:03:02 -0500101
Gunnar Mills80292bb2017-07-05 16:34:51 -0500102 auto bus = sdbusplus::bus::new_default();
Gunnar Mills765725e2017-07-06 14:17:44 -0500103 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 Mills72639152017-06-22 15:06:21 -0500113
Gunnar Mills765725e2017-07-06 14:17:44 -0500114 auto name = options["name"];
Patrick Venturedace6802018-11-01 16:52:10 -0700115 Presence presence(bus, inventory, path, std::stoul(key), name, eventP,
116 driverList);
Gunnar Mills765725e2017-07-06 14:17:44 -0500117
118 while (true)
119 {
120 // -1 denotes wait forever
Patrick Venturedace6802018-11-01 16:52:10 -0700121 rc = sd_event_run(eventP.get(), (uint64_t)-1);
Gunnar Mills765725e2017-07-06 14:17:44 -0500122 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 Mills72639152017-06-22 15:06:21 -0500130}