blob: 200dfd5d00d03fe2a48af05c18308b74ce11ca5c [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 */
Patrick Venture8865e402019-05-14 13:29:10 -070065 void commit(std::uint16_t session,
66 const std::vector<std::uint8_t>& bytes) override;
67
68 /**
69 * @throws BlobException.
70 */
Patrick Venture123b5c02019-03-05 14:01:00 -080071 void writeMeta(std::uint16_t session, std::uint32_t offset,
72 const std::vector<std::uint8_t>& bytes) override;
73
74 /**
75 * @throw BlobException.
76 */
77 void writeBytes(std::uint16_t session, std::uint32_t offset,
78 const std::vector<std::uint8_t>& bytes) override;
79
80 std::vector<std::string> getBlobList() override;
81
82 /**
83 * @throws BlobException.
84 */
85 StatResponse getStat(const std::string& id) override;
86
87 /**
88 * @throws BlobException.
89 */
Patrick Venture16a99a62019-05-03 17:21:30 -070090 StatResponse getStat(std::uint16_t session) override;
91
92 /**
93 * @throws BlobException.
94 */
Patrick Venture123b5c02019-03-05 14:01:00 -080095 std::uint16_t openBlob(const std::string& id,
96 std::uint16_t handlerFlags) override;
97
98 void closeBlob(std::uint16_t session) override;
99
100 /**
101 * @throws BlobException.
102 */
103 std::vector<std::uint8_t> readBytes(std::uint16_t session,
104 std::uint32_t offset,
105 std::uint32_t length) override;
106
107 private:
108 /**
109 * Send the contents of the payload to IPMI, this method handles wrapping
110 * with the OEN, subcommand and CRC.
111 *
112 * @param[in] command - the blob command.
113 * @param[in] payload - the payload bytes.
114 * @return the bytes returned from the ipmi interface.
115 * @throws BlobException.
116 */
117 std::vector<std::uint8_t>
118 sendIpmiPayload(BlobOEMCommands command,
119 const std::vector<std::uint8_t>& payload);
120
121 /**
122 * Generic blob byte writer.
123 *
124 * @param[in] command - the command associated with this write.
125 * @param[in] session - the session id.
126 * @param[in] offset - the offset for the metadata to write.
127 * @param[in] bytes - the bytes to send.
128 * @throws BlobException on failure.
129 */
130 void writeGeneric(BlobOEMCommands command, std::uint16_t session,
131 std::uint32_t offset,
132 const std::vector<std::uint8_t>& bytes);
133
Patrick Venture17186ae2019-05-06 10:30:55 -0700134 /**
135 * Generic stat reader.
136 *
137 * @param[in] command - the command associated with this write.
138 * @param[in] request - the bytes of the request
139 * @return the metadata StatResponse
140 * @throws BlobException on failure.
141 */
142 StatResponse statGeneric(BlobOEMCommands command,
143 const std::vector<std::uint8_t>& request);
144
Patrick Venture786a5412019-05-13 07:40:22 -0700145 std::unique_ptr<IpmiInterface> ipmi;
Patrick Venture123b5c02019-03-05 14:01:00 -0800146};
147
Patrick Venture1470bec2019-03-06 07:33:12 -0800148} // namespace ipmiblob