blob: 73f27d881e57549536df31798e49925b05dc47a0 [file] [log] [blame]
Sampa Misra854e61f2019-08-22 04:36:47 -05001#pragma once
2
3#include "file_io.hpp"
4
5namespace pldm
6{
7
8namespace responder
9{
10
11namespace fs = std::filesystem;
12
13/**
14 * @class FileHandler
15 *
16 * Base class to handle read/write of all oem file types
17 */
18class 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 * @return PLDM status code
28 */
29 virtual int writeFromMemory(uint32_t offset, uint32_t length,
30 uint64_t address) = 0;
31
Deepak Kodihallif6d3a832019-11-19 07:00:29 -060032 /** @brief Method to read an oem file type into host memory. Individual
33 * file types need to override this method to do the file specific
34 * processing
35 * @param[in] offset - offset to read
Deepak Kodihalli75e02f82019-11-20 02:51:05 -060036 * @param[in/out] length - length to be read mentioned by Host
Deepak Kodihallif6d3a832019-11-19 07:00:29 -060037 * @param[in] address - DMA address
38 * @return PLDM status code
39 */
Deepak Kodihalli75e02f82019-11-20 02:51:05 -060040 virtual int readIntoMemory(uint32_t offset, uint32_t& length,
Deepak Kodihallif6d3a832019-11-19 07:00:29 -060041 uint64_t address) = 0;
42
Deepak Kodihalli75e02f82019-11-20 02:51:05 -060043 /** @brief Method to read an oem file type's content into the PLDM response.
44 * @param[in] offset - offset to read
45 * @param[in/out] length - length to be read
46 * @param[in] response - PLDM response
47 * @return PLDM status code
48 */
49 virtual int read(uint32_t offset, uint32_t& length, Response& response) = 0;
50
Sampa Misra18967162020-01-14 02:31:41 -060051 /** @brief Method to write an oem file by type
52 * @param[in] buffer - buffer to be written to file
53 * @param[in] offset - offset to write to
54 * @param[in/out] length - length to be written
55 * @return PLDM status code
56 */
57 virtual int write(const char* buffer, uint32_t offset,
58 uint32_t& length) = 0;
59
Deepak Kodihalli2da1bfe2019-12-14 08:28:09 -060060 virtual int fileAck(uint8_t fileStatus) = 0;
61
Sampa Misra18967162020-01-14 02:31:41 -060062 /** @brief Method to process a new file available notification from the
63 * host. The bmc can chose to do different actions based on the file type.
64 *
65 * @param[in] length - size of the file content to be transferred
66 *
67 * @return PLDM status code
68 */
69 virtual int newFileAvailable(uint64_t length) = 0;
70
Deepak Kodihalli75e02f82019-11-20 02:51:05 -060071 /** @brief Method to read an oem file type's content into the PLDM response.
72 * @param[in] filePath - file to read from
73 * @param[in] offset - offset to read
74 * @param[in/out] length - length to be read
75 * @param[in] response - PLDM response
76 * @return PLDM status code
77 */
78 virtual int readFile(const std::string& filePath, uint32_t offset,
79 uint32_t& length, Response& response);
80
Sampa Misra854e61f2019-08-22 04:36:47 -050081 /** @brief Method to do the file content transfer ove DMA between host and
82 * bmc. This method is made virtual to be overridden in test case. And need
83 * not be defined in other child classes
84 *
85 * @param[in] path - file system path where read/write will be done
86 * @param[in] upstream - direction of DMA transfer. "false" means a
87 * transfer from host to BMC
88 * @param[in] offset - offset to read/write
Deepak Kodihalli75e02f82019-11-20 02:51:05 -060089 * @param[in/out] length - length to be read/write mentioned by Host
Sampa Misra854e61f2019-08-22 04:36:47 -050090 * @param[in] address - DMA address
91 *
92 * @return PLDM status code
93 */
94 virtual int transferFileData(const fs::path& path, bool upstream,
Deepak Kodihalli75e02f82019-11-20 02:51:05 -060095 uint32_t offset, uint32_t& length,
Sampa Misra854e61f2019-08-22 04:36:47 -050096 uint64_t address);
97
Deepak Kodihalli15211b42019-12-14 02:24:49 -060098 virtual int transferFileData(int fd, bool upstream, uint32_t offset,
99 uint32_t& length, uint64_t address);
100
Sampa Misra854e61f2019-08-22 04:36:47 -0500101 /** @brief Constructor to create a FileHandler object
102 */
103 FileHandler(uint32_t fileHandle) : fileHandle(fileHandle)
George Liu6492f522020-06-16 10:34:05 +0800104 {}
Sampa Misra854e61f2019-08-22 04:36:47 -0500105
106 /** FileHandler destructor
107 */
108 virtual ~FileHandler()
George Liu6492f522020-06-16 10:34:05 +0800109 {}
Sampa Misra854e61f2019-08-22 04:36:47 -0500110
111 protected:
112 uint32_t fileHandle; //!< file handle indicating name of file or invalid
113};
114
115/** @brief Method to create individual file handler objects based on file type
116 *
117 * @param[in] fileType - type of file
118 * @param[in] fileHandle - file handle
119 */
120
121std::unique_ptr<FileHandler> getHandlerByType(uint16_t fileType,
122 uint32_t fileHandle);
123
124} // namespace responder
125} // namespace pldm