Patrick Venture | 123b5c0 | 2019-03-05 14:01:00 -0800 | [diff] [blame] | 1 | #pragma once |
| 2 | |
| 3 | #include "internal/sys.hpp" |
| 4 | #include "ipmi_interface.hpp" |
| 5 | |
Anh Phan | 626bf76 | 2024-07-12 11:51:02 +0000 | [diff] [blame] | 6 | #include <atomic> |
Patrick Venture | 2fe4c65 | 2019-05-13 07:37:33 -0700 | [diff] [blame] | 7 | #include <memory> |
Willy Tu | 1322437 | 2024-07-15 17:00:15 +0000 | [diff] [blame] | 8 | #include <mutex> |
Patrick Venture | 123b5c0 | 2019-03-05 14:01:00 -0800 | [diff] [blame] | 9 | #include <vector> |
| 10 | |
Patrick Venture | 1470bec | 2019-03-06 07:33:12 -0800 | [diff] [blame] | 11 | namespace ipmiblob |
Patrick Venture | 123b5c0 | 2019-03-05 14:01:00 -0800 | [diff] [blame] | 12 | { |
| 13 | |
| 14 | class IpmiHandler : public IpmiInterface |
| 15 | { |
| 16 | public: |
Patrick Venture | 2fe4c65 | 2019-05-13 07:37:33 -0700 | [diff] [blame] | 17 | /* Create an IpmiHandler object with default inputs. It is ill-advised to |
| 18 | * share IpmiHandlers between objects. |
| 19 | */ |
| 20 | static std::unique_ptr<IpmiInterface> CreateIpmiHandler(); |
| 21 | |
Patrick Venture | 92a4a62 | 2021-02-04 10:45:00 -0800 | [diff] [blame] | 22 | explicit IpmiHandler(std::unique_ptr<internal::Sys> sys) : |
Patrick Williams | b80a025 | 2024-08-16 15:21:31 -0400 | [diff] [blame^] | 23 | sys(std::move(sys)) {}; |
Patrick Venture | 123b5c0 | 2019-03-05 14:01:00 -0800 | [diff] [blame] | 24 | |
| 25 | ~IpmiHandler() = default; |
| 26 | IpmiHandler(const IpmiHandler&) = delete; |
| 27 | IpmiHandler& operator=(const IpmiHandler&) = delete; |
| 28 | IpmiHandler(IpmiHandler&&) = default; |
| 29 | IpmiHandler& operator=(IpmiHandler&&) = default; |
| 30 | |
| 31 | /** |
| 32 | * Attempt to open the device node. |
| 33 | * |
| 34 | * @throws IpmiException on failure. |
| 35 | */ |
| 36 | void open(); |
| 37 | |
| 38 | /** |
| 39 | * @throws IpmiException on failure. |
| 40 | */ |
| 41 | std::vector<std::uint8_t> |
Patrick Venture | 958f1ce | 2019-05-31 17:07:25 -0700 | [diff] [blame] | 42 | sendPacket(std::uint8_t netfn, std::uint8_t cmd, |
| 43 | std::vector<std::uint8_t>& data) override; |
Patrick Venture | 123b5c0 | 2019-03-05 14:01:00 -0800 | [diff] [blame] | 44 | |
| 45 | private: |
Patrick Venture | 92a4a62 | 2021-02-04 10:45:00 -0800 | [diff] [blame] | 46 | const std::unique_ptr<internal::Sys> sys; |
Patrick Venture | 123b5c0 | 2019-03-05 14:01:00 -0800 | [diff] [blame] | 47 | /** TODO: Use a smart file descriptor when it's ready. Until then only |
| 48 | * allow moving this object. |
| 49 | */ |
| 50 | int fd = -1; |
| 51 | /* The last IPMI sequence number we used. */ |
Anh Phan | 626bf76 | 2024-07-12 11:51:02 +0000 | [diff] [blame] | 52 | std::atomic_int sequence = 0; |
Willy Tu | 1322437 | 2024-07-15 17:00:15 +0000 | [diff] [blame] | 53 | |
| 54 | // Protect the open fd between different threads |
| 55 | std::mutex openMutex; |
Patrick Venture | 123b5c0 | 2019-03-05 14:01:00 -0800 | [diff] [blame] | 56 | }; |
| 57 | |
Patrick Venture | 1470bec | 2019-03-06 07:33:12 -0800 | [diff] [blame] | 58 | } // namespace ipmiblob |