1 #pragma once
2 
3 #include "interface.hpp"
4 
5 #include <ipmiblob/blob_interface.hpp>
6 #include <stdplus/function_view.hpp>
7 
8 #include <string>
9 
10 namespace host_tool
11 {
12 
13 class UpdateHandlerInterface
14 {
15   public:
16     virtual ~UpdateHandlerInterface() = default;
17 
18     /**
19      * Check if the goal firmware is listed in the blob_list and that the
20      * handler's supported data type is available.
21      *
22      * @param[in] goalFirmware - the firmware to check /flash/image
23      * /flash/tarball, etc.
24      */
25     virtual bool checkAvailable(const std::string& goalFirmware) = 0;
26 
27     /**
28      * Send the file contents at path to the blob id, target.
29      *
30      * @param[in] target - the blob id
31      * @param[in] path - the source file path
32      */
33     virtual void sendFile(const std::string& target,
34                           const std::string& path) = 0;
35 
36     /**
37      * Trigger verification.
38      *
39      * @param[in] target - the verification blob id (may support multiple in the
40      * future.
41      * @param[in] ignoreStatus - determines whether to ignore the verification
42      * status.
43      * @return true if verified, false if verification errors.
44      */
45     virtual bool verifyFile(const std::string& target, bool ignoreStatus) = 0;
46 
47     /**
48      * Read the active firmware version.
49      *
50      * @param[in] versionBlob - the version blob id within the version handler.
51      * @return firmware version
52      */
53     virtual std::vector<uint8_t>
54         readVersion(const std::string& versionBlob) = 0;
55 
56     /**
57      * Cleanup the artifacts by triggering this action.
58      */
59     virtual void cleanArtifacts() = 0;
60 };
61 
62 /** Object that actually handles the update itself. */
63 class UpdateHandler : public UpdateHandlerInterface
64 {
65   public:
UpdateHandler(ipmiblob::BlobInterface * blob,DataInterface * handler)66     UpdateHandler(ipmiblob::BlobInterface* blob, DataInterface* handler) :
67         blob(blob), handler(handler)
68     {}
69 
70     ~UpdateHandler() = default;
71 
72     bool checkAvailable(const std::string& goalFirmware) override;
73 
74     /**
75      * @throw ToolException on failure.
76      */
77     void sendFile(const std::string& target, const std::string& path) override;
78 
79     /**
80      * @throw ToolException on failure (TODO: throw on timeout.)
81      */
82     bool verifyFile(const std::string& target, bool ignoreStatus) override;
83 
84     std::vector<uint8_t> readVersion(const std::string& versionBlob) override;
85 
86     void cleanArtifacts() override;
87 
88   private:
89     ipmiblob::BlobInterface* blob;
90     DataInterface* handler;
91 
92     /**
93      * @throw ToolException on failure.
94      */
95     void retrySendFile(const std::string& target, const std::string& path);
96 
97     /**
98      * @throw ToolException on failure (TODO: throw on timeout.)
99      */
100     void retryVerifyFile(const std::string& target, bool ignoreStatus);
101 
102     /**
103      * @throw ToolException on failure.
104      */
105     std::vector<uint8_t> retryReadVersion(const std::string& versionBlob);
106 
107     /**
108      * @throw ToolException on failure.
109      */
110     std::vector<uint8_t>
111         retryIfFailed(stdplus::function_view<std::vector<uint8_t>()> callback);
112 };
113 
114 } // namespace host_tool
115