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