xref: /openbmc/phosphor-ipmi-blobs/example/example.hpp (revision aceb4baa1b3b58d425c4c706069ec51fb594305f)
1 #pragma once
2 
3 #include <blobs-ipmid/blobs.hpp>
4 #include <string>
5 #include <unordered_map>
6 #include <vector>
7 
8 namespace blobs
9 {
10 
11 constexpr int kBufferSize = 1024;
12 
13 struct ExampleBlob
14 {
15     ExampleBlob() = default;
16     ExampleBlob(uint16_t id, uint16_t flags) :
17         sessionId(id), flags(flags), length(0)
18     {
19     }
20 
21     /* The blob handler session id. */
22     uint16_t sessionId;
23 
24     /* The flags passed into open. */
25     uint16_t flags;
26 
27     /* The buffer is a fixed size, but length represents the number of bytes
28      * expected to be used contiguously from offset 0.
29      */
30     uint32_t length;
31 
32     /* The staging buffer. */
33     uint8_t buffer[kBufferSize];
34 };
35 
36 class ExampleBlobHandler : public GenericBlobInterface
37 {
38   public:
39     /* We want everything explicitly default. */
40     ExampleBlobHandler() = default;
41     ~ExampleBlobHandler() = default;
42     ExampleBlobHandler(const ExampleBlobHandler&) = default;
43     ExampleBlobHandler& operator=(const ExampleBlobHandler&) = default;
44     ExampleBlobHandler(ExampleBlobHandler&&) = default;
45     ExampleBlobHandler& operator=(ExampleBlobHandler&&) = default;
46 
47     bool canHandleBlob(const std::string& path) override;
48     std::vector<std::string> getBlobIds() override;
49     bool deleteBlob(const std::string& path) override;
50     bool stat(const std::string& path, struct BlobMeta* meta) override;
51     bool open(uint16_t session, uint16_t flags,
52               const std::string& path) override;
53     std::vector<uint8_t> read(uint16_t session, uint32_t offset,
54                               uint32_t requestedSize) override;
55     bool write(uint16_t session, uint32_t offset,
56                const std::vector<uint8_t>& data) override;
57     bool commit(uint16_t session, const std::vector<uint8_t>& data) override;
58     bool close(uint16_t session) override;
59     bool stat(uint16_t session, struct BlobMeta* meta) override;
60     bool expire(uint16_t session) override;
61 
62     constexpr static char supportedPath[] = "/dev/fake/command";
63 
64   private:
65     ExampleBlob* getSession(uint16_t id);
66 
67     std::unordered_map<uint16_t, ExampleBlob> sessions;
68 };
69 
70 } // namespace blobs
71