xref: /openbmc/entity-manager/src/gpio-presence/gpio_presence_manager.hpp (revision c689762bbde322171310f72793ee52af000eb50b)
1 /*
2  * SPDX-FileCopyrightText: Copyright (c) 2022-2024. All rights
3  * reserved. SPDX-License-Identifier: Apache-2.0
4  */
5 #pragma once
6 
7 #include "config_provider.hpp"
8 #include "device_presence.hpp"
9 #include "sdbusplus/async/fdio.hpp"
10 
11 #include <gpiod.hpp>
12 
13 #include <string>
14 #include <unordered_map>
15 
16 namespace gpio_presence
17 {
18 
19 constexpr auto service = "xyz.openbmc_project.gpiopresence";
20 
21 class GPIOPresenceManager
22 {
23   public:
24     explicit GPIOPresenceManager(sdbusplus::async::context& ctx);
25 
26     // spawns the initialization function
27     auto start() -> void;
28 
29     // @param[in] name       name of device, e.g. 'cable0'
30     // @returns              true if present
31     auto getPresence(const std::string& name) -> bool;
32 
33     // request our dbus name
34     // @returns         our dbus name
35     auto setupBusName() const -> std::string;
36 
37     // add the configuration for object at path 'obj'
38     // @param[in] obj       object path for the new config
39     // @param[in] config    configuration for the new object
40     auto addConfig(const sdbusplus::message::object_path& obj,
41                    std::unique_ptr<DevicePresence> config) -> void;
42 
43     // update presence information based on new gpio state
44     // @param[in] gpioLine       name of the gpio line
45     // @param[in] state          new state of the gpio line
46     auto updatePresence(const std::string& gpioLine, bool state) -> void;
47 
48     // maps gpio names to cached gpio state
49     // true <=> High
50     std::unordered_map<std::string, bool> gpioState;
51 
52   private:
53     // fetch our configuration from dbus
54     // @param[in] obj object path of the configuration
55     auto addConfigFromDbusAsync(sdbusplus::message::object_path obj)
56         -> sdbusplus::async::task<void>;
57 
58     // fetch the parent inventory items 'Compatible' Decorator
59     // @param[in] obj object path of our configuration
60     auto getParentInventoryCompatible(
61         const sdbusplus::message::object_path& obj)
62         -> sdbusplus::async::task<std::vector<std::string>>;
63 
64     // delete our configuration for the object at 'objPath'
65     // @param[in] objPath         path of the object we want to forget
66     auto removeConfig(const std::string& objPath) -> void;
67 
68     // fetch configuration from dbus via object mapper
69     // and register dbus matches for configuration
70     auto initialize() -> sdbusplus::async::task<void>;
71 
72     // handle config interface added
73     // @param[in] obj    object path of the configuration
74     auto addConfigHandler(sdbusplus::message::object_path obj) -> void;
75 
76     // async block on fdio gpio event and handle it
77     // @param[in] gpioLine     name of the gpio to watch for events
78     auto readGPIOAsyncEvent(std::string gpioLine)
79         -> sdbusplus::async::task<void>;
80 
81     // maps dbus object paths to configurations
82     // e.g. /xyz/openbmc_project/inventory/system/board/My_Baseboard/cable0
83     std::unordered_map<std::string, std::unique_ptr<DevicePresence>>
84         presenceMap;
85 
86     // maps gpio names to fdios
87     std::unordered_map<std::string, std::unique_ptr<sdbusplus::async::fdio>>
88         fdios;
89     // maps gpio names to libgpiod lines
90     std::unordered_map<std::string, ::gpiod::line> gpioLines;
91 
92     sdbusplus::async::context& ctx;
93     sdbusplus::server::manager_t manager;
94 
95     ConfigProvider configProvider;
96 };
97 
98 } // namespace gpio_presence
99