blob: 1fbf5b66bfc598ad53203cc70ee7538c060ae61f [file] [log] [blame]
Patrick Venture123b5c02019-03-05 14:01:00 -08001#pragma once
2
3#include "internal/sys.hpp"
4#include "ipmi_interface.hpp"
5
Anh Phan626bf762024-07-12 11:51:02 +00006#include <atomic>
Patrick Venture2fe4c652019-05-13 07:37:33 -07007#include <memory>
Willy Tu13224372024-07-15 17:00:15 +00008#include <mutex>
Patrick Venture123b5c02019-03-05 14:01:00 -08009#include <vector>
10
Patrick Venture1470bec2019-03-06 07:33:12 -080011namespace ipmiblob
Patrick Venture123b5c02019-03-05 14:01:00 -080012{
13
14class IpmiHandler : public IpmiInterface
15{
16 public:
Patrick Venture2fe4c652019-05-13 07:37:33 -070017 /* 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 Venture92a4a622021-02-04 10:45:00 -080022 explicit IpmiHandler(std::unique_ptr<internal::Sys> sys) :
Patrick Williamsb80a0252024-08-16 15:21:31 -040023 sys(std::move(sys)) {};
Patrick Venture123b5c02019-03-05 14:01:00 -080024
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 Venture958f1ce2019-05-31 17:07:25 -070042 sendPacket(std::uint8_t netfn, std::uint8_t cmd,
43 std::vector<std::uint8_t>& data) override;
Patrick Venture123b5c02019-03-05 14:01:00 -080044
45 private:
Patrick Venture92a4a622021-02-04 10:45:00 -080046 const std::unique_ptr<internal::Sys> sys;
Patrick Venture123b5c02019-03-05 14:01:00 -080047 /** 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 Phan626bf762024-07-12 11:51:02 +000052 std::atomic_int sequence = 0;
Willy Tu13224372024-07-15 17:00:15 +000053
54 // Protect the open fd between different threads
55 std::mutex openMutex;
Patrick Venture123b5c02019-03-05 14:01:00 -080056};
57
Patrick Venture1470bec2019-03-06 07:33:12 -080058} // namespace ipmiblob