101123b2aSPatrick Venture #pragma once
201123b2aSPatrick Venture 
301123b2aSPatrick Venture #include "interface.hpp"
401123b2aSPatrick Venture 
501123b2aSPatrick Venture #include <ipmiblob/blob_interface.hpp>
6*3f596287SWilly Tu #include <stdplus/function_view.hpp>
79b37b095SPatrick Venture 
801123b2aSPatrick Venture #include <string>
901123b2aSPatrick Venture 
1001123b2aSPatrick Venture namespace host_tool
1101123b2aSPatrick Venture {
1201123b2aSPatrick Venture 
1301123b2aSPatrick Venture class UpdateHandlerInterface
1401123b2aSPatrick Venture {
1501123b2aSPatrick Venture   public:
1601123b2aSPatrick Venture     virtual ~UpdateHandlerInterface() = default;
1701123b2aSPatrick Venture 
1801123b2aSPatrick Venture     /**
1901123b2aSPatrick Venture      * Check if the goal firmware is listed in the blob_list and that the
2001123b2aSPatrick Venture      * handler's supported data type is available.
2101123b2aSPatrick Venture      *
2201123b2aSPatrick Venture      * @param[in] goalFirmware - the firmware to check /flash/image
2301123b2aSPatrick Venture      * /flash/tarball, etc.
2401123b2aSPatrick Venture      */
2501123b2aSPatrick Venture     virtual bool checkAvailable(const std::string& goalFirmware) = 0;
2601123b2aSPatrick Venture 
2701123b2aSPatrick Venture     /**
2801123b2aSPatrick Venture      * Send the file contents at path to the blob id, target.
2901123b2aSPatrick Venture      *
3001123b2aSPatrick Venture      * @param[in] target - the blob id
3101123b2aSPatrick Venture      * @param[in] path - the source file path
3201123b2aSPatrick Venture      */
3301123b2aSPatrick Venture     virtual void sendFile(const std::string& target,
3401123b2aSPatrick Venture                           const std::string& path) = 0;
3501123b2aSPatrick Venture 
3601123b2aSPatrick Venture     /**
3701123b2aSPatrick Venture      * Trigger verification.
3801123b2aSPatrick Venture      *
3901123b2aSPatrick Venture      * @param[in] target - the verification blob id (may support multiple in the
4001123b2aSPatrick Venture      * future.
416749ba1cSBrandon Kim      * @param[in] ignoreStatus - determines whether to ignore the verification
426749ba1cSBrandon Kim      * status.
4301123b2aSPatrick Venture      * @return true if verified, false if verification errors.
4401123b2aSPatrick Venture      */
456749ba1cSBrandon Kim     virtual bool verifyFile(const std::string& target, bool ignoreStatus) = 0;
4601123b2aSPatrick Venture 
4701123b2aSPatrick Venture     /**
48328f520fSJie Yang      * Read the active firmware version.
49328f520fSJie Yang      *
50328f520fSJie Yang      * @param[in] versionBlob - the version blob id within the version handler.
51328f520fSJie Yang      * @return firmware version
52328f520fSJie Yang      */
53328f520fSJie Yang     virtual std::vector<uint8_t>
54328f520fSJie Yang         readVersion(const std::string& versionBlob) = 0;
55328f520fSJie Yang 
56328f520fSJie Yang     /**
5701123b2aSPatrick Venture      * Cleanup the artifacts by triggering this action.
5801123b2aSPatrick Venture      */
5901123b2aSPatrick Venture     virtual void cleanArtifacts() = 0;
6001123b2aSPatrick Venture };
6101123b2aSPatrick Venture 
6201123b2aSPatrick Venture /** Object that actually handles the update itself. */
6301123b2aSPatrick Venture class UpdateHandler : public UpdateHandlerInterface
6401123b2aSPatrick Venture {
6501123b2aSPatrick Venture   public:
UpdateHandler(ipmiblob::BlobInterface * blob,DataInterface * handler)6601123b2aSPatrick Venture     UpdateHandler(ipmiblob::BlobInterface* blob, DataInterface* handler) :
6701123b2aSPatrick Venture         blob(blob), handler(handler)
689b37b095SPatrick Venture     {}
6901123b2aSPatrick Venture 
7001123b2aSPatrick Venture     ~UpdateHandler() = default;
7101123b2aSPatrick Venture 
7201123b2aSPatrick Venture     bool checkAvailable(const std::string& goalFirmware) override;
7301123b2aSPatrick Venture 
7401123b2aSPatrick Venture     /**
7501123b2aSPatrick Venture      * @throw ToolException on failure.
7601123b2aSPatrick Venture      */
7701123b2aSPatrick Venture     void sendFile(const std::string& target, const std::string& path) override;
7801123b2aSPatrick Venture 
7901123b2aSPatrick Venture     /**
8001123b2aSPatrick Venture      * @throw ToolException on failure (TODO: throw on timeout.)
8101123b2aSPatrick Venture      */
826749ba1cSBrandon Kim     bool verifyFile(const std::string& target, bool ignoreStatus) override;
8301123b2aSPatrick Venture 
84328f520fSJie Yang     std::vector<uint8_t> readVersion(const std::string& versionBlob) override;
85328f520fSJie Yang 
8601123b2aSPatrick Venture     void cleanArtifacts() override;
8701123b2aSPatrick Venture 
8801123b2aSPatrick Venture   private:
8901123b2aSPatrick Venture     ipmiblob::BlobInterface* blob;
9001123b2aSPatrick Venture     DataInterface* handler;
91*3f596287SWilly Tu 
92*3f596287SWilly Tu     /**
93*3f596287SWilly Tu      * @throw ToolException on failure.
94*3f596287SWilly Tu      */
95*3f596287SWilly Tu     void retrySendFile(const std::string& target, const std::string& path);
96*3f596287SWilly Tu 
97*3f596287SWilly Tu     /**
98*3f596287SWilly Tu      * @throw ToolException on failure (TODO: throw on timeout.)
99*3f596287SWilly Tu      */
100*3f596287SWilly Tu     void retryVerifyFile(const std::string& target, bool ignoreStatus);
101*3f596287SWilly Tu 
102*3f596287SWilly Tu     /**
103*3f596287SWilly Tu      * @throw ToolException on failure.
104*3f596287SWilly Tu      */
105*3f596287SWilly Tu     std::vector<uint8_t> retryReadVersion(const std::string& versionBlob);
106*3f596287SWilly Tu 
107*3f596287SWilly Tu     /**
108*3f596287SWilly Tu      * @throw ToolException on failure.
109*3f596287SWilly Tu      */
110*3f596287SWilly Tu     std::vector<uint8_t>
111*3f596287SWilly Tu         retryIfFailed(stdplus::function_view<std::vector<uint8_t>()> callback);
11201123b2aSPatrick Venture };
11301123b2aSPatrick Venture 
11401123b2aSPatrick Venture } // namespace host_tool
115