1 #pragma once
2 
3 #include "lpcsnoop/snoop.hpp"
4 
5 #include <boost/asio.hpp>
6 #include <gpiod.hpp>
7 #include <sdbusplus/asio/connection.hpp>
8 #include <sdbusplus/asio/object_server.hpp>
9 #include <sdbusplus/asio/property.hpp>
10 #include <sdbusplus/bus.hpp>
11 #include <sdbusplus/server.hpp>
12 #include <xyz/openbmc_project/Chassis/Buttons/HostSelector/server.hpp>
13 #include <xyz/openbmc_project/State/Boot/Raw/server.hpp>
14 
15 #include <filesystem>
16 #include <iostream>
17 
18 const std::string ipmiSnoopObject = "/xyz/openbmc_project/state/boot/raw";
19 
20 const int hostParseIdx = 3;
21 const int maxPostcode = 255;
22 const int maxPosition = 4;
23 
24 bool sevenSegmentLedEnabled = true;
25 
26 std::vector<gpiod::line> led_lines;
27 
28 using Selector =
29     sdbusplus::xyz::openbmc_project::Chassis::Buttons::server::HostSelector;
30 
31 std::unique_ptr<sdbusplus::bus::match_t> matchSignal;
32 
33 const std::string selectorService = "xyz.openbmc_project.Chassis.Buttons";
34 const std::string selectorObject =
35     "/xyz/openbmc_project/Chassis/Buttons/HostSelector";
36 const std::string selectorIface =
37     "xyz.openbmc_project.Chassis.Buttons.HostSelector";
38 
39 const std::string rawObject = "/xyz/openbmc_project/state/boot";
40 const std::string rawIface = "xyz.openbmc_project.State.Boot.Raw";
41 const std::string rawService = "xyz.openbmc_project.State.Boot.Raw";
42 
43 uint32_t getSelectorPosition(sdbusplus::bus_t& bus)
44 {
45     const std::string propertyName = "Position";
46 
47     auto method = bus.new_method_call(selectorService.c_str(),
48                                       selectorObject.c_str(),
49                                       "org.freedesktop.DBus.Properties", "Get");
50     method.append(selectorIface.c_str(), propertyName);
51 
52     try
53     {
54         std::variant<uint32_t> value{};
55         auto reply = bus.call(method);
56         reply.read(value);
57         return std::get<uint32_t>(value);
58     }
59     catch (const sdbusplus::exception_t& ex)
60     {
61         std::cerr << "GetProperty call failed. " << ex.what() << std::endl;
62         return 0;
63     }
64 }
65 
66 struct IpmiPostReporter : PostObject
67 {
68     IpmiPostReporter(sdbusplus::bus_t& bus, const char* objPath) :
69         PostObject(bus, objPath), bus(bus),
70         propertiesChangedSignalRaw(
71             bus,
72             sdbusplus::bus::match::rules::propertiesChanged(objPath, rawIface),
73 
74             [this, &bus](sdbusplus::message_t& msg) {
75         using primarycode_t = uint64_t;
76         using secondarycode_t = std::vector<uint8_t>;
77         using postcode_t = std::tuple<primarycode_t, secondarycode_t>;
78 
79         /* sevenSegmentLedEnabled flag is set when GPIO pins are not
80         there 7 seg display for fewer platforms. So, the code for
81         postcode dispay and Get Selector position can be skipped in
82         those platforms.
83         */
84         if (!sevenSegmentLedEnabled)
85         {
86             return;
87         }
88 
89         std::string objectName;
90         std::string InterfaceName;
91         std::map<std::string, std::variant<postcode_t>> msgData;
92         msg.read(InterfaceName, msgData);
93 
94         std::filesystem::path name(msg.get_path());
95         objectName = name.filename();
96 
97         std::string hostNumStr = objectName.substr(hostParseIdx);
98         size_t hostNum = std::stoi(hostNumStr);
99 
100         size_t position = getSelectorPosition(bus);
101 
102         if (position > maxPosition)
103         {
104             std::cerr << "Invalid position. Position should be 1 to 4 "
105                          "for all hosts "
106                       << std::endl;
107         }
108 
109         // Check if it was the Value property that changed.
110         auto valPropMap = msgData.find("Value");
111         if (valPropMap == msgData.end())
112         {
113             std::cerr << "Value property is not found " << std::endl;
114             return;
115         }
116         uint64_t postcode =
117             std::get<0>(std::get<postcode_t>(valPropMap->second));
118 
119         if (postcode <= maxPostcode)
120         {
121             if (position == hostNum)
122             {
123                 uint8_t postcode_8bit =
124                     static_cast<uint8_t>(postcode & 0x0000FF);
125 
126                 // write postcode into seven segment display
127                 if (postCodeDisplay(postcode_8bit) < 0)
128                 {
129                     fprintf(stderr, "Error in display the postcode\n");
130                 }
131             }
132             else
133             {
134                 fprintf(stderr, "Host Selector Position and host "
135                                 "number is not matched..\n");
136             }
137         }
138         else
139         {
140             fprintf(stderr, "invalid postcode value \n");
141         }
142             })
143     {}
144 
145     sdbusplus::bus_t& bus;
146     sdbusplus::bus::match_t propertiesChangedSignalRaw;
147     int postCodeDisplay(uint8_t);
148     void getSelectorPositionSignal(sdbusplus::bus_t& bus);
149 };
150 
151 // Configure the seven segment display connected GPIOs direction
152 int configGPIODirOutput()
153 {
154     std::string gpioStr;
155     // Need to define gpio names LED_POST_CODE_0 to 8 in dts file
156     std::string gpioName = "LED_POST_CODE_";
157     const int value = 0;
158 
159     for (int iteration = 0; iteration < 8; iteration++)
160     {
161         gpioStr = gpioName + std::to_string(iteration);
162         gpiod::line gpioLine = gpiod::find_line(gpioStr);
163 
164         if (!gpioLine)
165         {
166             std::string errMsg = "Failed to find the " + gpioStr + " line";
167             std::cerr << errMsg.c_str() << std::endl;
168 
169             /* sevenSegmentLedEnabled flag is unset when GPIO pins are not there
170              * 7 seg display for fewer platforms.
171              */
172             sevenSegmentLedEnabled = false;
173             return -1;
174         }
175 
176         led_lines.push_back(gpioLine);
177         // Request GPIO output to specified value
178         try
179         {
180             gpioLine.request({__FUNCTION__,
181                               gpiod::line_request::DIRECTION_OUTPUT,
182                               gpiod::line_request::FLAG_ACTIVE_LOW},
183                              value);
184         }
185         catch (std::exception&)
186         {
187             std::string errMsg = "Failed to request " + gpioStr + " output";
188             std::cerr << errMsg.c_str() << std::endl;
189             return -1;
190         }
191     }
192 
193     return 0;
194 }
195 
196 // Display the received postcode into seven segment display
197 int IpmiPostReporter::postCodeDisplay(uint8_t status)
198 {
199     for (int iteration = 0; iteration < 8; iteration++)
200     {
201         // split byte to write into GPIOs
202         int value = !((status >> iteration) & 0x01);
203 
204         led_lines[iteration].set_value(value);
205     }
206     return 0;
207 }
208