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