xref: /openbmc/phosphor-ipmi-flash/bmc/log-handler/log_handler.hpp (revision a49a3f79e4bea32929d7be8201645c7d83b0f2ae)
1*a49a3f79SGaurav Gandhi // Copyright 2021 Google Inc.
2*a49a3f79SGaurav Gandhi //
3*a49a3f79SGaurav Gandhi // Licensed under the Apache License, Version 2.0 (the "License");
4*a49a3f79SGaurav Gandhi // you may not use this file except in compliance with the License.
5*a49a3f79SGaurav Gandhi // You may obtain a copy of the License at
6*a49a3f79SGaurav Gandhi //
7*a49a3f79SGaurav Gandhi //      http://www.apache.org/licenses/LICENSE-2.0
8*a49a3f79SGaurav Gandhi //
9*a49a3f79SGaurav Gandhi // Unless required by applicable law or agreed to in writing, software
10*a49a3f79SGaurav Gandhi // distributed under the License is distributed on an "AS IS" BASIS,
11*a49a3f79SGaurav Gandhi // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12*a49a3f79SGaurav Gandhi // See the License for the specific language governing permissions and
13*a49a3f79SGaurav Gandhi // limitations under the License.
14*a49a3f79SGaurav Gandhi 
15*a49a3f79SGaurav Gandhi #pragma once
16*a49a3f79SGaurav Gandhi #include "handler_config.hpp"
17*a49a3f79SGaurav Gandhi #include "image_handler.hpp"
18*a49a3f79SGaurav Gandhi #include "status.hpp"
19*a49a3f79SGaurav Gandhi #include "util.hpp"
20*a49a3f79SGaurav Gandhi 
21*a49a3f79SGaurav Gandhi #include <blobs-ipmid/blobs.hpp>
22*a49a3f79SGaurav Gandhi 
23*a49a3f79SGaurav Gandhi #include <cstdint>
24*a49a3f79SGaurav Gandhi #include <map>
25*a49a3f79SGaurav Gandhi #include <memory>
26*a49a3f79SGaurav Gandhi #include <optional>
27*a49a3f79SGaurav Gandhi #include <set>
28*a49a3f79SGaurav Gandhi #include <string>
29*a49a3f79SGaurav Gandhi #include <string_view>
30*a49a3f79SGaurav Gandhi #include <unordered_map>
31*a49a3f79SGaurav Gandhi #include <vector>
32*a49a3f79SGaurav Gandhi 
33*a49a3f79SGaurav Gandhi namespace ipmi_flash
34*a49a3f79SGaurav Gandhi {
35*a49a3f79SGaurav Gandhi 
36*a49a3f79SGaurav Gandhi class LogBlobHandler : public blobs::GenericBlobInterface
37*a49a3f79SGaurav Gandhi {
38*a49a3f79SGaurav Gandhi   public:
39*a49a3f79SGaurav Gandhi     struct ActionPack
40*a49a3f79SGaurav Gandhi     {
41*a49a3f79SGaurav Gandhi         /** Only file operation action supported currently */
42*a49a3f79SGaurav Gandhi         std::unique_ptr<TriggerableActionInterface> onOpen;
43*a49a3f79SGaurav Gandhi         std::unique_ptr<TriggerableActionInterface> onDelete;
44*a49a3f79SGaurav Gandhi     };
45*a49a3f79SGaurav Gandhi 
46*a49a3f79SGaurav Gandhi     /**
47*a49a3f79SGaurav Gandhi      * Create a LogBlobHandler.
48*a49a3f79SGaurav Gandhi      *
49*a49a3f79SGaurav Gandhi      * @param[in] configs - list of blob configurations to support
50*a49a3f79SGaurav Gandhi      */
51*a49a3f79SGaurav Gandhi     LogBlobHandler(std::vector<HandlerConfig<ActionPack>>&& configs);
52*a49a3f79SGaurav Gandhi 
53*a49a3f79SGaurav Gandhi     ~LogBlobHandler() = default;
54*a49a3f79SGaurav Gandhi     LogBlobHandler(const LogBlobHandler&) = delete;
55*a49a3f79SGaurav Gandhi     LogBlobHandler& operator=(const LogBlobHandler&) = delete;
56*a49a3f79SGaurav Gandhi     LogBlobHandler(LogBlobHandler&&) = default;
57*a49a3f79SGaurav Gandhi     LogBlobHandler& operator=(LogBlobHandler&&) = default;
58*a49a3f79SGaurav Gandhi 
59*a49a3f79SGaurav Gandhi     bool canHandleBlob(const std::string& path) override;
60*a49a3f79SGaurav Gandhi     std::vector<std::string> getBlobIds() override;
61*a49a3f79SGaurav Gandhi     bool deleteBlob(const std::string& path) override;
62*a49a3f79SGaurav Gandhi     bool stat(const std::string&, blobs::BlobMeta* meta) override;
63*a49a3f79SGaurav Gandhi     bool open(uint16_t session, uint16_t flags,
64*a49a3f79SGaurav Gandhi               const std::string& path) override;
65*a49a3f79SGaurav Gandhi     std::vector<uint8_t> read(uint16_t session, uint32_t offset,
66*a49a3f79SGaurav Gandhi                               uint32_t requestedSize) override;
write(uint16_t,uint32_t,const std::vector<uint8_t> &)67*a49a3f79SGaurav Gandhi     bool write(uint16_t, uint32_t, const std::vector<uint8_t>&) override
68*a49a3f79SGaurav Gandhi     {
69*a49a3f79SGaurav Gandhi         return false; /* not supported */
70*a49a3f79SGaurav Gandhi     };
writeMeta(uint16_t,uint32_t,const std::vector<uint8_t> &)71*a49a3f79SGaurav Gandhi     bool writeMeta(uint16_t, uint32_t, const std::vector<uint8_t>&) override
72*a49a3f79SGaurav Gandhi     {
73*a49a3f79SGaurav Gandhi         return false; /* not supported */
74*a49a3f79SGaurav Gandhi     }
commit(uint16_t,const std::vector<uint8_t> &)75*a49a3f79SGaurav Gandhi     bool commit(uint16_t, const std::vector<uint8_t>&) override
76*a49a3f79SGaurav Gandhi     {
77*a49a3f79SGaurav Gandhi         return false; // not supported
78*a49a3f79SGaurav Gandhi     }
79*a49a3f79SGaurav Gandhi     bool close(uint16_t session) override;
80*a49a3f79SGaurav Gandhi     bool stat(uint16_t session, blobs::BlobMeta* meta) override;
81*a49a3f79SGaurav Gandhi     bool expire(uint16_t session) override;
82*a49a3f79SGaurav Gandhi 
83*a49a3f79SGaurav Gandhi   private:
84*a49a3f79SGaurav Gandhi     struct SessionInfo;
85*a49a3f79SGaurav Gandhi 
86*a49a3f79SGaurav Gandhi     struct BlobInfo
87*a49a3f79SGaurav Gandhi     {
88*a49a3f79SGaurav Gandhi         Pinned<std::string> blobId;
89*a49a3f79SGaurav Gandhi         std::unique_ptr<ActionPack> actions;
90*a49a3f79SGaurav Gandhi         std::unique_ptr<ImageHandlerInterface> handler;
91*a49a3f79SGaurav Gandhi         std::set<SessionInfo*> sessionsToUpdate;
92*a49a3f79SGaurav Gandhi     };
93*a49a3f79SGaurav Gandhi 
94*a49a3f79SGaurav Gandhi     struct SessionInfo
95*a49a3f79SGaurav Gandhi     {
96*a49a3f79SGaurav Gandhi         BlobInfo* blob;
97*a49a3f79SGaurav Gandhi 
98*a49a3f79SGaurav Gandhi         // A cached copy of the version data shared by all clients for a single
99*a49a3f79SGaurav Gandhi         // execution of the version retrieval action. This is is null until the
100*a49a3f79SGaurav Gandhi         // TriggerableAction has completed. If the action is an error, the
101*a49a3f79SGaurav Gandhi         // shared object is nullopt. Otherwise, contains a vector of the version
102*a49a3f79SGaurav Gandhi         // data when successfully read.
103*a49a3f79SGaurav Gandhi         std::shared_ptr<const std::optional<std::vector<uint8_t>>> data;
104*a49a3f79SGaurav Gandhi     };
105*a49a3f79SGaurav Gandhi 
106*a49a3f79SGaurav Gandhi     std::unordered_map<std::string_view, std::unique_ptr<BlobInfo>> blobInfoMap;
107*a49a3f79SGaurav Gandhi     std::unordered_map<uint16_t, std::unique_ptr<SessionInfo>> sessionInfoMap;
108*a49a3f79SGaurav Gandhi };
109*a49a3f79SGaurav Gandhi 
110*a49a3f79SGaurav Gandhi } // namespace ipmi_flash
111