xref: /openbmc/phosphor-buttons/inc/serial_uart_mux.hpp (revision 940083bdf3c7566de9808bb8601de63d7edb048a)
1 
2 #pragma once
3 #include "button_factory.hpp"
4 #include "button_interface.hpp"
5 #include "common.hpp"
6 #include "config.hpp"
7 #include "gpio.hpp"
8 #include "xyz/openbmc_project/Chassis/Buttons/HostSelector/server.hpp"
9 #include "xyz/openbmc_project/Chassis/Common/error.hpp"
10 #include "xyz/openbmc_project/Inventory/Item/server.hpp"
11 
12 #include <unistd.h>
13 
14 #include <phosphor-logging/elog-errors.hpp>
15 #include <sdbusplus/bus.hpp>
16 #include <sdbusplus/bus/match.hpp>
17 static constexpr auto DEBUG_CARD_PRESENT_GPIO = "debug_card_present";
18 static constexpr auto SERIAL_CONSOLE_SWITCH = "SERIAL_UART_MUX";
19 
20 class SerialUartMux final : public ButtonIface
21 {
22   public:
SerialUartMux(sdbusplus::bus_t & bus,const char * path,EventPtr & event,ButtonConfig & buttonCfg)23     SerialUartMux(sdbusplus::bus_t& bus, [[maybe_unused]] const char* path,
24                   EventPtr& event, ButtonConfig& buttonCfg) :
25         ButtonIface(bus, event, buttonCfg)
26     {
27         init();
28 
29         // read the platform specific config of host number to uart mux map
30         std::unordered_map<std::string, size_t> uartMuxMapJson =
31             buttonCfg.extraJsonInfo.at("serial_uart_mux_map")
32                 .get<decltype(uartMuxMapJson)>();
33         for (auto& [key, value] : uartMuxMapJson)
34         {
35             auto index = std::stoi(key);
36             serialUartMuxMap[index] = value;
37         }
38         if (buttonCfg.gpios.size() < 3)
39         {
40             throw std::runtime_error("not enough gpio configs found");
41         }
42 
43         for (auto& gpio : buttonCfg.gpios)
44         {
45             if (gpio.name == DEBUG_CARD_PRESENT_GPIO)
46             {
47                 debugCardPresentGpio = gpio;
48                 break;
49             }
50         }
51 
52         gpioLineCount = buttonCfg.gpios.size() - 1;
53     }
54 
~SerialUartMux()55     ~SerialUartMux()
56     {
57         deInit();
58     }
59     void init() override;
getFormFactorName()60     static constexpr std::string getFormFactorName()
61     {
62         return SERIAL_CONSOLE_SWITCH;
63     }
getDbusObjectPath()64     static constexpr std::string getDbusObjectPath()
65     {
66         return "NO_DBUS_OBJECT";
67     }
68 
69     void hostSelectorPositionChanged(sdbusplus::message_t& msg);
70     void configSerialConsoleMux(size_t position);
71     bool isOCPDebugCardPresent();
72 
handleEvent(sd_event_source *,int,uint32_t)73     void handleEvent(sd_event_source*, int, uint32_t) override {}
74 
75   protected:
76     size_t gpioLineCount;
77     std::unique_ptr<sdbusplus::bus::match_t> hostPositionChanged;
78     GpioInfo debugCardPresentGpio;
79     std::unordered_map<size_t, size_t> serialUartMuxMap;
80 };
81