blob: 0b6db17e9293213190829dc81f94cc94e90af94b [file] [log] [blame]
Patrick Venture123b5c02019-03-05 14:01:00 -08001#pragma once
2
3#include "blob_interface.hpp"
4#include "ipmi_interface.hpp"
5
Patrick Venture786a5412019-05-13 07:40:22 -07006#include <memory>
7
Patrick Venture1470bec2019-03-06 07:33:12 -08008namespace ipmiblob
Patrick Venture123b5c02019-03-05 14:01:00 -08009{
10
11class BlobHandler : public BlobInterface
12{
13 public:
14 enum BlobOEMCommands
15 {
16 bmcBlobGetCount = 0,
17 bmcBlobEnumerate = 1,
18 bmcBlobOpen = 2,
19 bmcBlobRead = 3,
20 bmcBlobWrite = 4,
21 bmcBlobCommit = 5,
22 bmcBlobClose = 6,
23 bmcBlobDelete = 7,
24 bmcBlobStat = 8,
25 bmcBlobSessionStat = 9,
26 bmcBlobWriteMeta = 10,
27 };
28
Patrick Venture22fcc842019-05-13 09:06:24 -070029 /**
30 * Create a BlobInterface pointer for use given an ipmi handler.
31 *
32 * @note This is a convenience method.
33 * @return a BlobHandler wrapped as a BlobInterface pointer.
34 */
35 static std::unique_ptr<BlobInterface>
36 CreateBlobHandler(std::unique_ptr<IpmiInterface> ipmi);
37
Patrick Venture786a5412019-05-13 07:40:22 -070038 explicit BlobHandler(std::unique_ptr<IpmiInterface> ipmi) :
39 ipmi(std::move(ipmi)){};
40
41 ~BlobHandler() = default;
42 BlobHandler(const BlobHandler&) = delete;
43 BlobHandler& operator=(const BlobHandler&) = delete;
44 BlobHandler(BlobHandler&&) = default;
45 BlobHandler& operator=(BlobHandler&&) = default;
Patrick Venture123b5c02019-03-05 14:01:00 -080046
47 /**
48 * Retrieve the blob count.
49 *
50 * @return the number of blob_ids found (0 on failure).
51 */
52 int getBlobCount();
53
54 /**
55 * Given an index into the list of blobs, return the name.
56 *
57 * @param[in] index - the index into the list of blob ids.
58 * @return the name as a string or empty on failure.
59 */
60 std::string enumerateBlob(std::uint32_t index);
61
62 /**
63 * @throws BlobException.
64 */
65 void writeMeta(std::uint16_t session, std::uint32_t offset,
66 const std::vector<std::uint8_t>& bytes) override;
67
68 /**
69 * @throw BlobException.
70 */
71 void writeBytes(std::uint16_t session, std::uint32_t offset,
72 const std::vector<std::uint8_t>& bytes) override;
73
74 std::vector<std::string> getBlobList() override;
75
76 /**
77 * @throws BlobException.
78 */
79 StatResponse getStat(const std::string& id) override;
80
81 /**
82 * @throws BlobException.
83 */
Patrick Venture16a99a62019-05-03 17:21:30 -070084 StatResponse getStat(std::uint16_t session) override;
85
86 /**
87 * @throws BlobException.
88 */
Patrick Venture123b5c02019-03-05 14:01:00 -080089 std::uint16_t openBlob(const std::string& id,
90 std::uint16_t handlerFlags) override;
91
92 void closeBlob(std::uint16_t session) override;
93
94 /**
95 * @throws BlobException.
96 */
97 std::vector<std::uint8_t> readBytes(std::uint16_t session,
98 std::uint32_t offset,
99 std::uint32_t length) override;
100
101 private:
102 /**
103 * Send the contents of the payload to IPMI, this method handles wrapping
104 * with the OEN, subcommand and CRC.
105 *
106 * @param[in] command - the blob command.
107 * @param[in] payload - the payload bytes.
108 * @return the bytes returned from the ipmi interface.
109 * @throws BlobException.
110 */
111 std::vector<std::uint8_t>
112 sendIpmiPayload(BlobOEMCommands command,
113 const std::vector<std::uint8_t>& payload);
114
115 /**
116 * Generic blob byte writer.
117 *
118 * @param[in] command - the command associated with this write.
119 * @param[in] session - the session id.
120 * @param[in] offset - the offset for the metadata to write.
121 * @param[in] bytes - the bytes to send.
122 * @throws BlobException on failure.
123 */
124 void writeGeneric(BlobOEMCommands command, std::uint16_t session,
125 std::uint32_t offset,
126 const std::vector<std::uint8_t>& bytes);
127
Patrick Venture17186ae2019-05-06 10:30:55 -0700128 /**
129 * Generic stat reader.
130 *
131 * @param[in] command - the command associated with this write.
132 * @param[in] request - the bytes of the request
133 * @return the metadata StatResponse
134 * @throws BlobException on failure.
135 */
136 StatResponse statGeneric(BlobOEMCommands command,
137 const std::vector<std::uint8_t>& request);
138
Patrick Venture786a5412019-05-13 07:40:22 -0700139 std::unique_ptr<IpmiInterface> ipmi;
Patrick Venture123b5c02019-03-05 14:01:00 -0800140};
141
Patrick Venture1470bec2019-03-06 07:33:12 -0800142} // namespace ipmiblob