1 #pragma once
2 
3 #include "file_io.hpp"
4 
5 namespace pldm
6 {
7 
8 namespace responder
9 {
10 
11 namespace fs = std::filesystem;
12 
13 /**
14  *  @class FileHandler
15  *
16  *  Base class to handle read/write of all oem file types
17  */
18 class FileHandler
19 {
20   public:
21     /** @brief Method to write an oem file type from host memory. Individual
22      *  file types need to override this method to do the file specific
23      *  processing
24      *  @param[in] offset - offset to read/write
25      *  @param[in] length - length to be read/write mentioned by Host
26      *  @param[in] address - DMA address
27      *  @param[in] oemPlatformHandler - oem handler for PLDM platform related
28      *                                  tasks
29      *  @return PLDM status code
30      */
31     virtual int writeFromMemory(uint32_t offset, uint32_t length,
32                                 uint64_t address,
33                                 oem_platform::Handler* oemPlatformHandler) = 0;
34 
35     /** @brief Method to read an oem file type into host memory. Individual
36      *  file types need to override this method to do the file specific
37      *  processing
38      *  @param[in] offset - offset to read
39      *  @param[in] length - length to be read mentioned by Host
40      *  @param[in] address - DMA address
41      *  @param[in] oemPlatformHandler - oem handler for PLDM platform related
42      *                                  tasks
43      *  @return PLDM status code
44      */
45     virtual int readIntoMemory(uint32_t offset, uint32_t length,
46                                uint64_t address,
47                                oem_platform::Handler* oemPlatformHandler) = 0;
48 
49     /** @brief Method to read an oem file type's content into the PLDM response.
50      *  @param[in] offset - offset to read
51      *  @param[in/out] length - length to be read
52      *  @param[in] response - PLDM response
53      *  @param[in] oemPlatformHandler - oem handler for PLDM platform related
54      *                                  tasks
55      *  @return PLDM status code
56      */
57     virtual int read(uint32_t offset, uint32_t& length, Response& response,
58                      oem_platform::Handler* oemPlatformHandler) = 0;
59 
60     /** @brief Method to write an oem file by type
61      *  @param[in] buffer - buffer to be written to file
62      *  @param[in] offset - offset to write to
63      *  @param[in/out] length - length to be written
64      *  @param[in] oemPlatformHandler - oem handler for PLDM platform related
65      *                                  tasks
66      *  @return PLDM status code
67      */
68     virtual int write(const char* buffer, uint32_t offset, uint32_t& length,
69                       oem_platform::Handler* oemPlatformHandler) = 0;
70 
71     virtual int fileAck(uint8_t fileStatus) = 0;
72 
73     /** @brief Method to process a new file available notification from the
74      *  host. The bmc can chose to do different actions based on the file type.
75      *
76      *  @param[in] length - size of the file content to be transferred
77      *
78      *  @return PLDM status code
79      */
80     virtual int newFileAvailable(uint64_t length) = 0;
81 
82     /** @brief Method to read an oem file type's content into the PLDM response.
83      *  @param[in] filePath - file to read from
84      *  @param[in] offset - offset to read
85      *  @param[in/out] length - length to be read
86      *  @param[in] response - PLDM response
87      *  @return PLDM status code
88      */
89     virtual int readFile(const std::string& filePath, uint32_t offset,
90                          uint32_t& length, Response& response);
91 
92     /** @brief Method to do the file content transfer ove DMA between host and
93      *  bmc. This method is made virtual to be overridden in test case. And need
94      *  not be defined in other child classes
95      *
96      *  @param[in] path - file system path  where read/write will be done
97      *  @param[in] upstream - direction of DMA transfer. "false" means a
98      *                        transfer from host to BMC
99      *  @param[in] offset - offset to read/write
100      *  @param[in/out] length - length to be read/write mentioned by Host
101      *  @param[in] address - DMA address
102      *
103      *  @return PLDM status code
104      */
105     virtual int transferFileData(const fs::path& path, bool upstream,
106                                  uint32_t offset, uint32_t& length,
107                                  uint64_t address);
108 
109     virtual int transferFileData(int fd, bool upstream, uint32_t offset,
110                                  uint32_t& length, uint64_t address);
111 
112     virtual int transferFileDataToSocket(int fd, uint32_t& length,
113                                          uint64_t address);
114 
115     /** @brief Constructor to create a FileHandler object
116      */
FileHandler(uint32_t fileHandle)117     FileHandler(uint32_t fileHandle) : fileHandle(fileHandle) {}
118 
119     /** FileHandler destructor
120      */
~FileHandler()121     virtual ~FileHandler() {}
122 
123   protected:
124     uint32_t fileHandle; //!< file handle indicating name of file or invalid
125 };
126 
127 /** @brief Method to create individual file handler objects based on file type
128  *
129  *  @param[in] fileType - type of file
130  *  @param[in] fileHandle - file handle
131  */
132 
133 std::unique_ptr<FileHandler> getHandlerByType(uint16_t fileType,
134                                               uint32_t fileHandle);
135 } // namespace responder
136 } // namespace pldm
137