Patrick Venture | c0f499b | 2018-09-14 17:57:42 -0700 | [diff] [blame] | 1 | #pragma once |
| 2 | |
Patrick Venture | aceb4ba | 2018-09-27 14:50:37 -0700 | [diff] [blame] | 3 | #include <blobs-ipmid/blobs.hpp> |
Patrick Venture | 6c415c6 | 2018-11-14 14:01:36 -0800 | [diff] [blame] | 4 | #include <memory> |
Patrick Venture | c0f499b | 2018-09-14 17:57:42 -0700 | [diff] [blame] | 5 | #include <string> |
| 6 | #include <unordered_map> |
| 7 | #include <vector> |
| 8 | |
Patrick Venture | 6c415c6 | 2018-11-14 14:01:36 -0800 | [diff] [blame] | 9 | #ifdef __cplusplus |
| 10 | extern "C" { |
| 11 | #endif |
| 12 | |
| 13 | /** |
| 14 | * This method must be declared as extern C for blob manager to lookup the |
| 15 | * symbol. |
| 16 | */ |
| 17 | std::unique_ptr<blobs::GenericBlobInterface> createHandler(); |
| 18 | |
| 19 | #ifdef __cplusplus |
| 20 | } |
| 21 | #endif |
| 22 | |
Patrick Venture | c0f499b | 2018-09-14 17:57:42 -0700 | [diff] [blame] | 23 | namespace blobs |
| 24 | { |
| 25 | |
| 26 | constexpr int kBufferSize = 1024; |
| 27 | |
| 28 | struct ExampleBlob |
| 29 | { |
| 30 | ExampleBlob() = default; |
| 31 | ExampleBlob(uint16_t id, uint16_t flags) : |
Patrick Venture | e01c0af | 2018-09-27 14:56:42 -0700 | [diff] [blame] | 32 | sessionId(id), flags(flags), |
| 33 | state(StateFlags::open_read | StateFlags::open_write), length(0) |
Patrick Venture | c0f499b | 2018-09-14 17:57:42 -0700 | [diff] [blame] | 34 | { |
| 35 | } |
| 36 | |
| 37 | /* The blob handler session id. */ |
| 38 | uint16_t sessionId; |
| 39 | |
| 40 | /* The flags passed into open. */ |
| 41 | uint16_t flags; |
| 42 | |
Patrick Venture | e01c0af | 2018-09-27 14:56:42 -0700 | [diff] [blame] | 43 | /* The current state. */ |
| 44 | uint16_t state; |
| 45 | |
Patrick Venture | c0f499b | 2018-09-14 17:57:42 -0700 | [diff] [blame] | 46 | /* The buffer is a fixed size, but length represents the number of bytes |
| 47 | * expected to be used contiguously from offset 0. |
| 48 | */ |
| 49 | uint32_t length; |
| 50 | |
| 51 | /* The staging buffer. */ |
| 52 | uint8_t buffer[kBufferSize]; |
| 53 | }; |
| 54 | |
| 55 | class ExampleBlobHandler : public GenericBlobInterface |
| 56 | { |
| 57 | public: |
| 58 | /* We want everything explicitly default. */ |
| 59 | ExampleBlobHandler() = default; |
| 60 | ~ExampleBlobHandler() = default; |
| 61 | ExampleBlobHandler(const ExampleBlobHandler&) = default; |
| 62 | ExampleBlobHandler& operator=(const ExampleBlobHandler&) = default; |
| 63 | ExampleBlobHandler(ExampleBlobHandler&&) = default; |
| 64 | ExampleBlobHandler& operator=(ExampleBlobHandler&&) = default; |
| 65 | |
| 66 | bool canHandleBlob(const std::string& path) override; |
| 67 | std::vector<std::string> getBlobIds() override; |
| 68 | bool deleteBlob(const std::string& path) override; |
Patrick Venture | 8bc1177 | 2019-06-04 07:20:24 -0700 | [diff] [blame] | 69 | bool stat(const std::string& path, BlobMeta* meta) override; |
Patrick Venture | c0f499b | 2018-09-14 17:57:42 -0700 | [diff] [blame] | 70 | bool open(uint16_t session, uint16_t flags, |
| 71 | const std::string& path) override; |
| 72 | std::vector<uint8_t> read(uint16_t session, uint32_t offset, |
| 73 | uint32_t requestedSize) override; |
| 74 | bool write(uint16_t session, uint32_t offset, |
| 75 | const std::vector<uint8_t>& data) override; |
Patrick Venture | 5c4b17b | 2018-10-04 10:32:22 -0700 | [diff] [blame] | 76 | bool writeMeta(uint16_t session, uint32_t offset, |
| 77 | const std::vector<uint8_t>& data) override; |
Patrick Venture | c0f499b | 2018-09-14 17:57:42 -0700 | [diff] [blame] | 78 | bool commit(uint16_t session, const std::vector<uint8_t>& data) override; |
| 79 | bool close(uint16_t session) override; |
Patrick Venture | 8bc1177 | 2019-06-04 07:20:24 -0700 | [diff] [blame] | 80 | bool stat(uint16_t session, BlobMeta* meta) override; |
Patrick Venture | c0f499b | 2018-09-14 17:57:42 -0700 | [diff] [blame] | 81 | bool expire(uint16_t session) override; |
| 82 | |
| 83 | constexpr static char supportedPath[] = "/dev/fake/command"; |
| 84 | |
| 85 | private: |
| 86 | ExampleBlob* getSession(uint16_t id); |
| 87 | |
| 88 | std::unordered_map<uint16_t, ExampleBlob> sessions; |
| 89 | }; |
| 90 | |
| 91 | } // namespace blobs |