1f0c71df2SPatrick Venture #pragma once
2f0c71df2SPatrick Venture 
3f0c71df2SPatrick Venture #include <cstdint>
4ded66d0fSJason Ling #include <fstream>
5f0c71df2SPatrick Venture #include <memory>
6*56a2273fSJason Ling #include <optional>
7f0c71df2SPatrick Venture #include <string>
8f0c71df2SPatrick Venture #include <vector>
9f0c71df2SPatrick Venture 
10f0c71df2SPatrick Venture namespace ipmi_flash
11f0c71df2SPatrick Venture {
12f0c71df2SPatrick Venture 
13f0c71df2SPatrick Venture /**
14f0c71df2SPatrick Venture  * Each image update mechanism must implement the ImageHandlerInterface.
15f0c71df2SPatrick Venture  */
16f0c71df2SPatrick Venture class ImageHandlerInterface
17f0c71df2SPatrick Venture {
18f0c71df2SPatrick Venture   public:
19f0c71df2SPatrick Venture     virtual ~ImageHandlerInterface() = default;
20f0c71df2SPatrick Venture 
21f0c71df2SPatrick Venture     /**
22f0c71df2SPatrick Venture      * open the firmware update mechanism.
23f0c71df2SPatrick Venture      *
24f0c71df2SPatrick Venture      * @param[in] path - the path passed to the handler (the blob_id).
25f0c71df2SPatrick Venture      * @return bool - returns true on success.
26f0c71df2SPatrick Venture      */
27*56a2273fSJason Ling     virtual bool open(const std::string& path,
28*56a2273fSJason Ling                       std::ios_base::openmode mode) = 0;
29f0c71df2SPatrick Venture 
30f0c71df2SPatrick Venture     /**
31f0c71df2SPatrick Venture      * close the image.
32f0c71df2SPatrick Venture      */
33f0c71df2SPatrick Venture     virtual void close() = 0;
34f0c71df2SPatrick Venture 
35f0c71df2SPatrick Venture     /**
36f0c71df2SPatrick Venture      * write data to the staged file.
37f0c71df2SPatrick Venture      *
38f0c71df2SPatrick Venture      * @param[in] offset - 0-based offset into the file.
39f0c71df2SPatrick Venture      * @param[in] data - the data to write.
40f0c71df2SPatrick Venture      * @return bool - returns true on success.
41f0c71df2SPatrick Venture      */
42f0c71df2SPatrick Venture     virtual bool write(std::uint32_t offset,
43f0c71df2SPatrick Venture                        const std::vector<std::uint8_t>& data) = 0;
44*56a2273fSJason Ling     /**
45*56a2273fSJason Ling      * read data from a file.
46*56a2273fSJason Ling      *
47*56a2273fSJason Ling      * @param[in] offset - 0-based offset into the file.
48*56a2273fSJason Ling      * @param[in] size - the number of bytes
49*56a2273fSJason Ling      * @return std::optional<std::vector<std::uint8_t>> - returns std::nullopt
50*56a2273fSJason Ling      * on failure otherwise returns a vector filled with the bytes read.
51*56a2273fSJason Ling      *
52*56a2273fSJason Ling      */
53*56a2273fSJason Ling     virtual std::optional<std::vector<std::uint8_t>>
54*56a2273fSJason Ling         read(std::uint32_t offset, std::uint32_t size) = 0;
55f0c71df2SPatrick Venture 
56f0c71df2SPatrick Venture     /**
57f0c71df2SPatrick Venture      * return the size of the file (if that notion makes sense).
58f0c71df2SPatrick Venture      *
59f0c71df2SPatrick Venture      * @return the size in bytes of the image staged.
60f0c71df2SPatrick Venture      */
61f0c71df2SPatrick Venture     virtual int getSize() = 0;
62f0c71df2SPatrick Venture };
63f0c71df2SPatrick Venture 
64d4e20de6SPatrick Venture class HandlerPack
65f0c71df2SPatrick Venture {
66d4e20de6SPatrick Venture   public:
HandlerPack(const std::string & name,std::unique_ptr<ImageHandlerInterface> handler)67d4e20de6SPatrick Venture     HandlerPack(const std::string& name,
68d4e20de6SPatrick Venture                 std::unique_ptr<ImageHandlerInterface> handler) :
69d4e20de6SPatrick Venture         blobName(name),
70d4e20de6SPatrick Venture         handler(std::move(handler))
719b37b095SPatrick Venture     {}
72d4e20de6SPatrick Venture 
73d4e20de6SPatrick Venture     HandlerPack() = default;
74d4e20de6SPatrick Venture     ~HandlerPack() = default;
75d4e20de6SPatrick Venture     HandlerPack(const HandlerPack&) = delete;
76d4e20de6SPatrick Venture     HandlerPack& operator=(const HandlerPack&) = delete;
77d4e20de6SPatrick Venture     HandlerPack(HandlerPack&&) = default;
78d4e20de6SPatrick Venture     HandlerPack& operator=(HandlerPack&&) = default;
79d4e20de6SPatrick Venture 
80f0c71df2SPatrick Venture     std::string blobName;
81d4e20de6SPatrick Venture     std::unique_ptr<ImageHandlerInterface> handler;
82f0c71df2SPatrick Venture };
83f0c71df2SPatrick Venture 
84f0c71df2SPatrick Venture } // namespace ipmi_flash
85