xref: /openbmc/dbus-sensors/src/mctp/MCTPReactor.hpp (revision 18b6186e531ae37dd22b634c6530f793528473f4)
1275f7c39SAndrew Jeffery #pragma once
2275f7c39SAndrew Jeffery 
3275f7c39SAndrew Jeffery #include "MCTPDeviceRepository.hpp"
4275f7c39SAndrew Jeffery #include "MCTPEndpoint.hpp"
5275f7c39SAndrew Jeffery #include "Utils.hpp"
6275f7c39SAndrew Jeffery 
7*18b6186eSEd Tanous #include <cstdint>
8*18b6186eSEd Tanous #include <functional>
9*18b6186eSEd Tanous #include <memory>
10*18b6186eSEd Tanous #include <optional>
11*18b6186eSEd Tanous #include <set>
12275f7c39SAndrew Jeffery #include <string>
13275f7c39SAndrew Jeffery #include <vector>
14275f7c39SAndrew Jeffery 
15275f7c39SAndrew Jeffery struct AssociationServer
16275f7c39SAndrew Jeffery {
17275f7c39SAndrew Jeffery     virtual ~AssociationServer() = default;
18275f7c39SAndrew Jeffery 
19275f7c39SAndrew Jeffery     virtual void associate(const std::string& path,
20275f7c39SAndrew Jeffery                            const std::vector<Association>& associations) = 0;
21275f7c39SAndrew Jeffery     virtual void disassociate(const std::string& path) = 0;
22275f7c39SAndrew Jeffery };
23275f7c39SAndrew Jeffery 
24275f7c39SAndrew Jeffery class MCTPReactor : public std::enable_shared_from_this<MCTPReactor>
25275f7c39SAndrew Jeffery {
26275f7c39SAndrew Jeffery     using MCTPDeviceFactory = std::function<std::shared_ptr<MCTPDevice>(
27275f7c39SAndrew Jeffery         const std::string& interface, const std::vector<std::uint8_t>& physaddr,
28275f7c39SAndrew Jeffery         std::optional<std::uint8_t> eid)>;
29275f7c39SAndrew Jeffery 
30275f7c39SAndrew Jeffery   public:
31275f7c39SAndrew Jeffery     MCTPReactor() = delete;
32275f7c39SAndrew Jeffery     MCTPReactor(const MCTPReactor&) = delete;
33275f7c39SAndrew Jeffery     MCTPReactor(MCTPReactor&&) = delete;
MCTPReactor(AssociationServer & server)34275f7c39SAndrew Jeffery     explicit MCTPReactor(AssociationServer& server) : server(server) {}
35275f7c39SAndrew Jeffery     ~MCTPReactor() = default;
36275f7c39SAndrew Jeffery     MCTPReactor& operator=(const MCTPReactor&) = delete;
37275f7c39SAndrew Jeffery     MCTPReactor& operator=(MCTPReactor&&) = delete;
38275f7c39SAndrew Jeffery 
39275f7c39SAndrew Jeffery     void tick();
40275f7c39SAndrew Jeffery 
41275f7c39SAndrew Jeffery     void manageMCTPDevice(const std::string& path,
42275f7c39SAndrew Jeffery                           const std::shared_ptr<MCTPDevice>& device);
43275f7c39SAndrew Jeffery     void unmanageMCTPDevice(const std::string& path);
44275f7c39SAndrew Jeffery 
45275f7c39SAndrew Jeffery   private:
46275f7c39SAndrew Jeffery     static std::optional<std::string> findSMBusInterface(int bus);
47275f7c39SAndrew Jeffery 
48275f7c39SAndrew Jeffery     AssociationServer& server;
49275f7c39SAndrew Jeffery     MCTPDeviceRepository devices;
50275f7c39SAndrew Jeffery 
51275f7c39SAndrew Jeffery     // Tracks MCTP devices that have failed their setup
52275f7c39SAndrew Jeffery     std::set<std::shared_ptr<MCTPDevice>> deferred;
53275f7c39SAndrew Jeffery 
54275f7c39SAndrew Jeffery     void deferSetup(const std::shared_ptr<MCTPDevice>& dev);
55275f7c39SAndrew Jeffery     void setupEndpoint(const std::shared_ptr<MCTPDevice>& dev);
56275f7c39SAndrew Jeffery     void trackEndpoint(const std::shared_ptr<MCTPEndpoint>& ep);
57275f7c39SAndrew Jeffery     void untrackEndpoint(const std::shared_ptr<MCTPEndpoint>& ep);
58275f7c39SAndrew Jeffery };
59