blob: 920979880845c8adf9fc3f49fe2d115e2abf2828 [file] [log] [blame]
Patrick Venturec0f499b2018-09-14 17:57:42 -07001#include "example/example.hpp"
2
3#include <algorithm>
Patrick Venture5100a382018-09-27 10:40:50 -07004#include <blobs-ipmid/manager.hpp>
Patrick Venturee01c0af2018-09-27 14:56:42 -07005#include <cstring>
Patrick Venture5100a382018-09-27 10:40:50 -07006#include <memory>
7#include <phosphor-logging/log.hpp>
Patrick Venturec0f499b2018-09-14 17:57:42 -07008#include <string>
9#include <vector>
10
11namespace blobs
12{
13
Patrick Venture5100a382018-09-27 10:40:50 -070014using namespace phosphor::logging;
15
Patrick Venturec0f499b2018-09-14 17:57:42 -070016constexpr char ExampleBlobHandler::supportedPath[];
17
18ExampleBlob* ExampleBlobHandler::getSession(uint16_t id)
19{
20 auto search = sessions.find(id);
21 if (search == sessions.end())
22 {
23 return nullptr;
24 }
25 /* Not thread-safe, however, the blob handler deliberately assumes serial
26 * execution. */
27 return &search->second;
28}
29
30bool ExampleBlobHandler::canHandleBlob(const std::string& path)
31{
32 return (path == supportedPath);
33}
34
35std::vector<std::string> ExampleBlobHandler::getBlobIds()
36{
37 return {supportedPath};
38}
39
40bool ExampleBlobHandler::deleteBlob(const std::string& path)
41{
42 return false;
43}
44
45bool ExampleBlobHandler::stat(const std::string& path, struct BlobMeta* meta)
46{
47 return false;
48}
49
50bool ExampleBlobHandler::open(uint16_t session, uint16_t flags,
51 const std::string& path)
52{
53 if (!canHandleBlob(path))
54 {
55 return false;
56 }
57
58 auto findSess = sessions.find(session);
59 if (findSess != sessions.end())
60 {
61 /* This session is already active. */
62 return false;
63 }
64 sessions[session] = ExampleBlob(session, flags);
65 return true;
66}
67
68std::vector<uint8_t> ExampleBlobHandler::read(uint16_t session, uint32_t offset,
69 uint32_t requestedSize)
70{
71 ExampleBlob* sess = getSession(session);
72 if (!sess)
73 {
74 return std::vector<uint8_t>();
75 }
76
77 /* Is the offset beyond the array? */
78 if (offset >= sizeof(sess->buffer))
79 {
80 return std::vector<uint8_t>();
81 }
82
83 /* Determine how many bytes we can read from the offset.
84 * In this case, if they read beyond "size" we allow it.
85 */
86 uint32_t remain = sizeof(sess->buffer) - offset;
87 uint32_t numBytes = std::min(remain, requestedSize);
88 /* Copy the bytes! */
89 std::vector<uint8_t> result(numBytes);
90 std::memcpy(result.data(), &sess->buffer[offset], numBytes);
91 return result;
92}
93
94bool ExampleBlobHandler::write(uint16_t session, uint32_t offset,
95 const std::vector<uint8_t>& data)
96{
97 ExampleBlob* sess = getSession(session);
98 if (!sess)
99 {
100 return false;
101 }
102 /* Is the offset beyond the array? */
103 if (offset >= sizeof(sess->buffer))
104 {
105 return false;
106 }
107 /* Determine whether all their bytes will fit. */
108 uint32_t remain = sizeof(sess->buffer) - offset;
109 if (data.size() > remain)
110 {
111 return false;
112 }
113 sess->length =
114 std::max(offset + data.size(),
115 static_cast<std::vector<uint8_t>::size_type>(sess->length));
116 std::memcpy(&sess->buffer[offset], data.data(), data.size());
117 return true;
118}
119
120bool ExampleBlobHandler::commit(uint16_t session,
121 const std::vector<uint8_t>& data)
122{
123 ExampleBlob* sess = getSession(session);
124 if (!sess)
125 {
126 return false;
127 }
128
129 /* Do something with the staged data!. */
130
131 return false;
132}
133
134bool ExampleBlobHandler::close(uint16_t session)
135{
136 ExampleBlob* sess = getSession(session);
137 if (!sess)
138 {
139 return false;
140 }
141
142 sessions.erase(session);
143 return true;
144}
145
146bool ExampleBlobHandler::stat(uint16_t session, struct BlobMeta* meta)
147{
148 ExampleBlob* sess = getSession(session);
149 if (!sess)
150 {
151 return false;
152 }
153 if (!meta)
154 {
155 return false;
156 }
157 meta->size = sess->length;
158 meta->blobState = sess->state;
159 return true;
160}
161
162bool ExampleBlobHandler::expire(uint16_t session)
163{
164 ExampleBlob* sess = getSession(session);
165 if (!sess)
166 {
167 return false;
168 }
169 /* TODO: implement session expiration behavior. */
170 return false;
171}
172
Patrick Venture5100a382018-09-27 10:40:50 -0700173void setupExampleHandler() __attribute__((constructor));
174
175void setupExampleHandler()
176{
177 BlobManager* manager = getBlobManager();
178 if (!manager->registerHandler(std::make_unique<ExampleBlobHandler>()))
179 {
180 log<level::ERR>("Failed to register Example Handler");
181 }
182}
183
Patrick Venturec0f499b2018-09-14 17:57:42 -0700184} // namespace blobs