xref: /openbmc/entity-manager/src/gpio-presence/gpio_presence_manager.hpp (revision 8c4b1d999a13b27d6edc84cfaff1112a0cc07091)
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     // delete our configuration for the object at 'objPath'
59     // @param[in] objPath         path of the object we want to forget
60     auto removeConfig(const std::string& objPath) -> void;
61 
62     // fetch configuration from dbus via object mapper
63     // and register dbus matches for configuration
64     auto initialize() -> sdbusplus::async::task<void>;
65 
66     // handle config interface added
67     // @param[in] obj    object path of the configuration
68     auto addConfigHandler(sdbusplus::message::object_path obj) -> void;
69 
70     // async block on fdio gpio event and handle it
71     // @param[in] gpioLine     name of the gpio to watch for events
72     auto readGPIOAsyncEvent(std::string gpioLine)
73         -> sdbusplus::async::task<void>;
74 
75     // maps dbus object paths to configurations
76     // e.g. /xyz/openbmc_project/inventory/system/board/My_Baseboard/cable0
77     std::unordered_map<std::string, std::unique_ptr<DevicePresence>>
78         presenceMap;
79 
80     // maps gpio names to fdios
81     std::unordered_map<std::string, std::unique_ptr<sdbusplus::async::fdio>>
82         fdios;
83     // maps gpio names to libgpiod lines
84     std::unordered_map<std::string, ::gpiod::line> gpioLines;
85 
86     sdbusplus::async::context& ctx;
87     sdbusplus::server::manager_t manager;
88 
89     ConfigProvider configProvider;
90 };
91 
92 } // namespace gpio_presence
93