| Patrick Venture | ef3aead | 2018-09-12 08:53:29 -0700 | [diff] [blame] | 1 | #pragma once | 
 | 2 |  | 
| Patrick Venture | dc82ab1 | 2019-01-10 10:15:50 -0800 | [diff] [blame] | 3 | #include <memory> | 
| Patrick Venture | ef3aead | 2018-09-12 08:53:29 -0700 | [diff] [blame] | 4 | #include <string> | 
 | 5 | #include <vector> | 
 | 6 |  | 
 | 7 | namespace blobs | 
 | 8 | { | 
 | 9 |  | 
| Patrick Venture | 4beac9a | 2019-02-11 08:21:10 -0800 | [diff] [blame] | 10 | enum BlobOEMCommands | 
 | 11 | { | 
 | 12 |     bmcBlobGetCount = 0, | 
 | 13 |     bmcBlobEnumerate = 1, | 
 | 14 |     bmcBlobOpen = 2, | 
 | 15 |     bmcBlobRead = 3, | 
 | 16 |     bmcBlobWrite = 4, | 
 | 17 |     bmcBlobCommit = 5, | 
 | 18 |     bmcBlobClose = 6, | 
 | 19 |     bmcBlobDelete = 7, | 
 | 20 |     bmcBlobStat = 8, | 
 | 21 |     bmcBlobSessionStat = 9, | 
 | 22 |     bmcBlobWriteMeta = 10, | 
 | 23 | }; | 
 | 24 |  | 
| Patrick Venture | ef3aead | 2018-09-12 08:53:29 -0700 | [diff] [blame] | 25 | enum OpenFlags | 
 | 26 | { | 
 | 27 |     read = (1 << 0), | 
 | 28 |     write = (1 << 1), | 
 | 29 |     /* bits 3-7 reserved. */ | 
 | 30 |     /* bits 8-15 given blob-specific definitions */ | 
 | 31 | }; | 
 | 32 |  | 
 | 33 | enum StateFlags | 
 | 34 | { | 
 | 35 |     open_read = (1 << 0), | 
 | 36 |     open_write = (1 << 1), | 
 | 37 |     committing = (1 << 2), | 
 | 38 |     committed = (1 << 3), | 
 | 39 |     commit_error = (1 << 4), | 
 | 40 | }; | 
 | 41 |  | 
 | 42 | struct BlobMeta | 
 | 43 | { | 
 | 44 |     uint16_t blobState; | 
 | 45 |     uint32_t size; | 
 | 46 |     std::vector<uint8_t> metadata; | 
 | 47 | }; | 
 | 48 |  | 
 | 49 | /* | 
 | 50 |  * All blob specific objects implement this interface. | 
 | 51 |  */ | 
 | 52 | class GenericBlobInterface | 
 | 53 | { | 
 | 54 |   public: | 
 | 55 |     virtual ~GenericBlobInterface() = default; | 
 | 56 |  | 
 | 57 |     /** | 
 | 58 |      * Checks if the handler will manage this file path. | 
 | 59 |      * | 
 | 60 |      * @param[in] blobId. | 
 | 61 |      * @return bool whether it will manage the file path. | 
 | 62 |      */ | 
 | 63 |     virtual bool canHandleBlob(const std::string& path) = 0; | 
 | 64 |  | 
 | 65 |     /** | 
 | 66 |      * Return the name(s) of the blob(s).  Used during GetCount. | 
 | 67 |      * | 
 | 68 |      * @return List of blobIds this handler manages. | 
 | 69 |      */ | 
 | 70 |     virtual std::vector<std::string> getBlobIds() = 0; | 
 | 71 |  | 
 | 72 |     /** | 
 | 73 |      * Attempt to delete the blob specified by the path. | 
 | 74 |      * | 
 | 75 |      * @param[in] path - the blobId to try and delete. | 
 | 76 |      * @return bool - whether it was able to delete the blob. | 
 | 77 |      */ | 
 | 78 |     virtual bool deleteBlob(const std::string& path) = 0; | 
 | 79 |  | 
 | 80 |     /** | 
 | 81 |      * Return metadata about the blob. | 
 | 82 |      * | 
 | 83 |      * @param[in] path - the blobId for metadata. | 
 | 84 |      * @param[in,out] meta - a pointer to a blobmeta. | 
 | 85 |      * @return bool - true if it was successful. | 
 | 86 |      */ | 
 | 87 |     virtual bool stat(const std::string& path, struct BlobMeta* meta) = 0; | 
 | 88 |  | 
 | 89 |     /* The methods below are per session. */ | 
 | 90 |  | 
 | 91 |     /** | 
 | 92 |      * Attempt to open a session from this path. | 
 | 93 |      * | 
 | 94 |      * @param[in] session - the session id. | 
 | 95 |      * @param[in] flags - the open flags. | 
 | 96 |      * @param[in] path - the blob path. | 
 | 97 |      * @return bool - was able to open the session. | 
 | 98 |      */ | 
 | 99 |     virtual bool open(uint16_t session, uint16_t flags, | 
 | 100 |                       const std::string& path) = 0; | 
 | 101 |  | 
 | 102 |     /** | 
 | 103 |      * Attempt to read from a blob. | 
 | 104 |      * | 
 | 105 |      * @param[in] session - the session id. | 
 | 106 |      * @param[in] offset - offset into the blob. | 
 | 107 |      * @param[in] requestedSize - number of bytes to read. | 
 | 108 |      * @return Bytes read back (0 length on error). | 
 | 109 |      */ | 
 | 110 |     virtual std::vector<uint8_t> read(uint16_t session, uint32_t offset, | 
 | 111 |                                       uint32_t requestedSize) = 0; | 
 | 112 |  | 
 | 113 |     /** | 
 | 114 |      * Attempt to write to a blob. | 
 | 115 |      * | 
 | 116 |      * @param[in] session - the session id. | 
 | 117 |      * @param[in] offset - offset into the blob. | 
 | 118 |      * @param[in] data - the data to write. | 
 | 119 |      * @return bool - was able to write. | 
 | 120 |      */ | 
 | 121 |     virtual bool write(uint16_t session, uint32_t offset, | 
 | 122 |                        const std::vector<uint8_t>& data) = 0; | 
 | 123 |  | 
 | 124 |     /** | 
| Patrick Venture | 5c4b17b | 2018-10-04 10:32:22 -0700 | [diff] [blame] | 125 |      * Attempt to write metadata to a blob. | 
 | 126 |      * | 
 | 127 |      * @param[in] session - the session id. | 
 | 128 |      * @param[in] offset - offset into the blob. | 
 | 129 |      * @param[in] data - the data to write. | 
 | 130 |      * @return bool - was able to write. | 
 | 131 |      */ | 
 | 132 |     virtual bool writeMeta(uint16_t session, uint32_t offset, | 
 | 133 |                            const std::vector<uint8_t>& data) = 0; | 
 | 134 |  | 
 | 135 |     /** | 
| Patrick Venture | ef3aead | 2018-09-12 08:53:29 -0700 | [diff] [blame] | 136 |      * Attempt to commit to a blob. | 
 | 137 |      * | 
 | 138 |      * @param[in] session - the session id. | 
 | 139 |      * @param[in] data - optional commit data. | 
 | 140 |      * @return bool - was able to start commit. | 
 | 141 |      */ | 
 | 142 |     virtual bool commit(uint16_t session, const std::vector<uint8_t>& data) = 0; | 
 | 143 |  | 
 | 144 |     /** | 
 | 145 |      * Attempt to close your session. | 
 | 146 |      * | 
 | 147 |      * @param[in] session - the session id. | 
 | 148 |      * @return bool - was able to close session. | 
 | 149 |      */ | 
 | 150 |     virtual bool close(uint16_t session) = 0; | 
 | 151 |  | 
 | 152 |     /** | 
 | 153 |      * Attempt to return metadata for the session's view of the blob. | 
 | 154 |      * | 
 | 155 |      * @param[in] session - the session id. | 
 | 156 |      * @param[in,out] meta - pointer to update with the BlobMeta. | 
 | 157 |      * @return bool - wether it was successful. | 
 | 158 |      */ | 
 | 159 |     virtual bool stat(uint16_t session, struct BlobMeta* meta) = 0; | 
 | 160 |  | 
 | 161 |     /** | 
 | 162 |      * Attempt to expire a session.  This is called when a session has been | 
 | 163 |      * inactive for at least 10 minutes. | 
 | 164 |      * | 
 | 165 |      * @param[in] session - the session id. | 
 | 166 |      * @return bool - whether the session was able to be closed. | 
 | 167 |      */ | 
 | 168 |     virtual bool expire(uint16_t session) = 0; | 
 | 169 | }; | 
 | 170 | } // namespace blobs | 
| Patrick Venture | dc82ab1 | 2019-01-10 10:15:50 -0800 | [diff] [blame] | 171 |  | 
 | 172 | #ifdef __cplusplus | 
 | 173 | extern "C" { | 
 | 174 | #endif | 
 | 175 |  | 
 | 176 | /** | 
 | 177 |  * All Blob handlers need to implement this method.  It is called after loading | 
 | 178 |  * the library to then get a handle to the blob handler. | 
 | 179 |  * | 
 | 180 |  * @return a unique pointer to your blob handler instance. | 
 | 181 |  */ | 
 | 182 | std::unique_ptr<blobs::GenericBlobInterface> createHandler(); | 
 | 183 |  | 
 | 184 | #ifdef __cplusplus | 
 | 185 | } | 
 | 186 | #endif |