xref: /openbmc/phosphor-host-postd/main.cpp (revision 35b6c67c)
1 /**
2  * Copyright 2017 Google Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #include <fcntl.h>
18 #include <getopt.h>
19 #include <poll.h>
20 #include <unistd.h>
21 
22 #include <array>
23 #include <cstdint>
24 #include <iostream>
25 #include <memory>
26 #include <thread>
27 
28 #include "lpcsnoop/snoop.hpp"
29 
30 static const char* snoopFilename = "/dev/aspeed-lpc-snoop0";
31 
32 /*
33  * 256 bytes is a nice amount.  It's improbable we'd need this many, but its
34  * gives us leg room in the event the driver poll doesn't return in a timely
35  * fashion.  So, mostly arbitrarily chosen.
36  */
37 static constexpr size_t BUFFER_SIZE = 256;
38 
39 /*
40  * Process any incoming dbus inquiries, which include introspection etc.
41  */
42 void ProcessDbus(sdbusplus::bus::bus& bus)
43 {
44     while (true)
45     {
46         bus.process_discard();
47         bus.wait(); // wait indefinitely
48     }
49 
50     return;
51 }
52 
53 static void usage(const char* name)
54 {
55     fprintf(stderr,
56             "Usage: %s [-d <DEVICE>]\n"
57             "  -d, --device <DEVICE>  use <DEVICE> file. Default is '%s'\n\n",
58             name, snoopFilename);
59 }
60 
61 /*
62  * TODO(venture): this only listens one of the possible snoop ports, but
63  * doesn't share the namespace.
64  *
65  * This polls() the lpc snoop character device and it owns the dbus object
66  * whose value is the latest port 80h value.
67  */
68 int main(int argc, char* argv[])
69 {
70     int rc = 0;
71     int opt;
72     struct pollfd pollset;
73     int pollr;
74     int readb;
75     int postFd = -1;
76     std::array<uint8_t, BUFFER_SIZE> buffer;
77 
78     /*
79      * These string constants are only used in this method within this object
80      * and this object is the only object feeding into the final binary.
81      *
82      * If however, another object is added to this binary it would be proper
83      * to move these declarations to be global and extern to the other object.
84      */
85     const char* snoopObject = SNOOP_OBJECTPATH;
86     const char* snoopDbus = SNOOP_BUSNAME;
87 
88     bool deferSignals = true;
89 
90     static const struct option long_options[] = {
91         {"device", required_argument, NULL, 'd'}, {0, 0, 0, 0}};
92 
93     while ((opt = getopt_long(argc, argv, "d:", long_options, NULL)) != -1)
94     {
95         switch (opt)
96         {
97             case 0:
98                 break;
99             case 'd':
100                 snoopFilename = optarg;
101                 break;
102             default:
103                 usage(argv[0]);
104                 exit(EXIT_FAILURE);
105         }
106     }
107 
108     postFd = open(snoopFilename, 0);
109     if (postFd < 0)
110     {
111         fprintf(stderr, "Unable to open: %s\n", snoopFilename);
112         return -1;
113     }
114 
115     pollset.fd = postFd;
116     pollset.events |= POLLIN;
117 
118     auto bus = sdbusplus::bus::new_default();
119 
120     // Add systemd object manager.
121     sdbusplus::server::manager::manager(bus, snoopObject);
122 
123     PostReporter reporter(bus, snoopObject, deferSignals);
124     reporter.emit_object_added();
125 
126     bus.request_name(snoopDbus);
127 
128     /*
129      * I don't see a public interface for getting the underlying sd_bus*
130      * so instead of poll(bus, driver), I'll just create a separate thread.
131      *
132      * TODO(venture): There may be a way to use sdevent to poll both the file
133      * and the dbus in the same event loop.  If I could get the sdbus pointer
134      * from bus directly, I'd grab a file handler from it, and then just poll on
135      * both in one loop.  From a cursory look at sdevent, I should be able to do
136      * something similar with that at some point.
137      */
138     std::thread lt(ProcessDbus, std::ref(bus));
139 
140     /* infinitely listen for POST codes and broadcast. */
141     while (true)
142     {
143         pollr = poll(&pollset, 1, -1); /* polls indefinitely. */
144         if (pollr < 0)
145         {
146             /* poll returned error. */
147             rc = -errno;
148             goto exit;
149         }
150 
151         if (pollr > 0)
152         {
153             if (pollset.revents & POLLIN)
154             {
155                 readb = read(postFd, buffer.data(), buffer.size());
156                 if (readb < 0)
157                 {
158                     /* Read failure. */
159                     rc = readb;
160                     goto exit;
161                 }
162                 else
163                 {
164                     /* Broadcast the bytes read. */
165                     for (int i = 0; i < readb; i++)
166                     {
167                         reporter.value(buffer[i]);
168                     }
169                 }
170             }
171         }
172     }
173 
174 exit:
175     if (postFd > -1)
176     {
177         close(postFd);
178     }
179 
180     lt.join();
181 
182     return rc;
183 }
184