xref: /openbmc/phosphor-ipmi-flash/bmc/version-handler/version_handler.hpp (revision b487eb47e566df7679e740559582f6d25597feba)
185e54f1bSJason Ling #pragma once
2cc7f385bSWilliam A. Kennington III #include "handler_config.hpp"
3c78bfc82SJason Ling #include "image_handler.hpp"
485e54f1bSJason Ling #include "status.hpp"
5c78bfc82SJason Ling #include "util.hpp"
685e54f1bSJason Ling 
785e54f1bSJason Ling #include <blobs-ipmid/blobs.hpp>
885e54f1bSJason Ling 
9c78bfc82SJason Ling #include <cstdint>
10c78bfc82SJason Ling #include <map>
1185e54f1bSJason Ling #include <memory>
129936c456SWilliam A. Kennington III #include <optional>
139936c456SWilliam A. Kennington III #include <set>
14c78bfc82SJason Ling #include <string>
15b45eb5eeSWilliam A. Kennington III #include <string_view>
16c78bfc82SJason Ling #include <unordered_map>
17c78bfc82SJason Ling #include <vector>
18abf17354SWilliam A. Kennington III 
1985e54f1bSJason Ling namespace ipmi_flash
2085e54f1bSJason Ling {
21c78bfc82SJason Ling 
22c78bfc82SJason Ling class VersionBlobHandler : public blobs::GenericBlobInterface
23c78bfc82SJason Ling {
24c78bfc82SJason Ling   public:
25abf17354SWilliam A. Kennington III     struct ActionPack
26abf17354SWilliam A. Kennington III     {
27abf17354SWilliam A. Kennington III         /** Only file operation action supported currently */
28abf17354SWilliam A. Kennington III         std::unique_ptr<TriggerableActionInterface> onOpen;
29abf17354SWilliam A. Kennington III     };
30abf17354SWilliam A. Kennington III 
31c78bfc82SJason Ling     /**
32c78bfc82SJason Ling      * Create a VersionBlobHandler.
33c78bfc82SJason Ling      *
34abf17354SWilliam A. Kennington III      * @param[in] configs - list of blob configurations to support
35c78bfc82SJason Ling      */
36abf17354SWilliam A. Kennington III     VersionBlobHandler(std::vector<HandlerConfig<ActionPack>>&& configs);
37abf17354SWilliam A. Kennington III 
38c78bfc82SJason Ling     ~VersionBlobHandler() = default;
39c78bfc82SJason Ling     VersionBlobHandler(const VersionBlobHandler&) = delete;
40c78bfc82SJason Ling     VersionBlobHandler& operator=(const VersionBlobHandler&) = delete;
41c78bfc82SJason Ling     VersionBlobHandler(VersionBlobHandler&&) = default;
42c78bfc82SJason Ling     VersionBlobHandler& operator=(VersionBlobHandler&&) = default;
43c78bfc82SJason Ling 
44c78bfc82SJason Ling     bool canHandleBlob(const std::string& path) override;
45c78bfc82SJason Ling     std::vector<std::string> getBlobIds() override;
46c78bfc82SJason Ling     bool deleteBlob(const std::string& path) override;
47c78bfc82SJason Ling     bool stat(const std::string&, blobs::BlobMeta* meta) override;
48c78bfc82SJason Ling     bool open(uint16_t session, uint16_t flags,
49c78bfc82SJason Ling               const std::string& path) override;
50c78bfc82SJason Ling     std::vector<uint8_t> read(uint16_t session, uint32_t offset,
51c78bfc82SJason Ling                               uint32_t requestedSize) override;
write(uint16_t,uint32_t,const std::vector<uint8_t> &)52*b487eb47SWilly Tu     bool write(uint16_t, uint32_t, const std::vector<uint8_t>&) override
53c78bfc82SJason Ling     {
54c78bfc82SJason Ling         return false; /* not supported */
55c78bfc82SJason Ling     };
writeMeta(uint16_t,uint32_t,const std::vector<uint8_t> &)56*b487eb47SWilly Tu     bool writeMeta(uint16_t, uint32_t, const std::vector<uint8_t>&) override
57c78bfc82SJason Ling     {
58c78bfc82SJason Ling         return false; /* not supported */
59c78bfc82SJason Ling     }
commit(uint16_t,const std::vector<uint8_t> &)60*b487eb47SWilly Tu     bool commit(uint16_t, const std::vector<uint8_t>&) override
61c78bfc82SJason Ling     {
62c78bfc82SJason Ling         return false; // not supported
63c78bfc82SJason Ling     }
64c78bfc82SJason Ling     bool close(uint16_t session) override;
65c78bfc82SJason Ling     bool stat(uint16_t session, blobs::BlobMeta* meta) override;
66c78bfc82SJason Ling     bool expire(uint16_t session) override;
67c78bfc82SJason Ling 
68c78bfc82SJason Ling   private:
699936c456SWilliam A. Kennington III     struct SessionInfo;
709936c456SWilliam A. Kennington III 
71abf17354SWilliam A. Kennington III     struct BlobInfo
72abf17354SWilliam A. Kennington III     {
73b45eb5eeSWilliam A. Kennington III         Pinned<std::string> blobId;
74abf17354SWilliam A. Kennington III         std::unique_ptr<ActionPack> actions;
75abf17354SWilliam A. Kennington III         std::unique_ptr<ImageHandlerInterface> handler;
769936c456SWilliam A. Kennington III         std::set<SessionInfo*> sessionsToUpdate;
779936c456SWilliam A. Kennington III     };
789936c456SWilliam A. Kennington III 
799936c456SWilliam A. Kennington III     struct SessionInfo
809936c456SWilliam A. Kennington III     {
819936c456SWilliam A. Kennington III         BlobInfo* blob;
829936c456SWilliam A. Kennington III 
839936c456SWilliam A. Kennington III         // A cached copy of the version data shared by all clients for a single
849936c456SWilliam A. Kennington III         // execution of the version retrieval action. This is is null until the
859936c456SWilliam A. Kennington III         // TriggerableAction has completed. If the action is an error, the
869936c456SWilliam A. Kennington III         // shared object is nullopt. Otherwise, contains a vector of the version
879936c456SWilliam A. Kennington III         // data when successfully read.
889936c456SWilliam A. Kennington III         std::shared_ptr<const std::optional<std::vector<uint8_t>>> data;
89abf17354SWilliam A. Kennington III     };
90abf17354SWilliam A. Kennington III 
91b45eb5eeSWilliam A. Kennington III     std::unordered_map<std::string_view, std::unique_ptr<BlobInfo>> blobInfoMap;
929936c456SWilliam A. Kennington III     std::unordered_map<uint16_t, std::unique_ptr<SessionInfo>> sessionInfoMap;
93c78bfc82SJason Ling };
949936c456SWilliam A. Kennington III 
9585e54f1bSJason Ling } // namespace ipmi_flash
96