xref: /openbmc/dbus-sensors/src/mctp/MCTPReactor.hpp (revision 18b6186e531ae37dd22b634c6530f793528473f4)
1 #pragma once
2 
3 #include "MCTPDeviceRepository.hpp"
4 #include "MCTPEndpoint.hpp"
5 #include "Utils.hpp"
6 
7 #include <cstdint>
8 #include <functional>
9 #include <memory>
10 #include <optional>
11 #include <set>
12 #include <string>
13 #include <vector>
14 
15 struct AssociationServer
16 {
17     virtual ~AssociationServer() = default;
18 
19     virtual void associate(const std::string& path,
20                            const std::vector<Association>& associations) = 0;
21     virtual void disassociate(const std::string& path) = 0;
22 };
23 
24 class MCTPReactor : public std::enable_shared_from_this<MCTPReactor>
25 {
26     using MCTPDeviceFactory = std::function<std::shared_ptr<MCTPDevice>(
27         const std::string& interface, const std::vector<std::uint8_t>& physaddr,
28         std::optional<std::uint8_t> eid)>;
29 
30   public:
31     MCTPReactor() = delete;
32     MCTPReactor(const MCTPReactor&) = delete;
33     MCTPReactor(MCTPReactor&&) = delete;
MCTPReactor(AssociationServer & server)34     explicit MCTPReactor(AssociationServer& server) : server(server) {}
35     ~MCTPReactor() = default;
36     MCTPReactor& operator=(const MCTPReactor&) = delete;
37     MCTPReactor& operator=(MCTPReactor&&) = delete;
38 
39     void tick();
40 
41     void manageMCTPDevice(const std::string& path,
42                           const std::shared_ptr<MCTPDevice>& device);
43     void unmanageMCTPDevice(const std::string& path);
44 
45   private:
46     static std::optional<std::string> findSMBusInterface(int bus);
47 
48     AssociationServer& server;
49     MCTPDeviceRepository devices;
50 
51     // Tracks MCTP devices that have failed their setup
52     std::set<std::shared_ptr<MCTPDevice>> deferred;
53 
54     void deferSetup(const std::shared_ptr<MCTPDevice>& dev);
55     void setupEndpoint(const std::shared_ptr<MCTPDevice>& dev);
56     void trackEndpoint(const std::shared_ptr<MCTPEndpoint>& ep);
57     void untrackEndpoint(const std::shared_ptr<MCTPEndpoint>& ep);
58 };
59