blob: 385f57bc9213d75d8871863d1c1ef288477332dd [file] [log] [blame]
Tom Josephb4ecdf22019-05-15 12:16:40 +05301#pragma once
2
3#include <stdint.h>
4
5#include <filesystem>
6#include <nlohmann/json.hpp>
7#include <vector>
8
9#include "libpldm/pldm_types.h"
10
11namespace pldm
12{
13
14namespace filetable
15{
16
17namespace fs = std::filesystem;
18using Handle = uint32_t;
19using Json = nlohmann::json;
20using Table = std::vector<uint8_t>;
21
22/** @struct FileEntry
23 *
24 * Data structure for storing information regarding the files supported by
25 * PLDM File I/O. The file handle is used to uniquely identify the file. The
26 * traits provide information whether the file is Read only, Read/Write and
27 * preserved across firmware upgrades.
28 */
29struct FileEntry
30{
31 Handle handle; //!< File handle
32 fs::path fsPath; //!< File path
33 bitfield32_t traits; //!< File traits
34};
35
36/** @class FileTable
37 *
38 * FileTable class encapsulates the data related to files supported by PLDM
39 * File I/O and provides interfaces to lookup files information based on the
40 * file handle and extract the file attribute table. The file attribute table
41 * comprises of metadata for files. Metadata includes the file handle, file
42 * name, current file size and file traits.
43 */
44class FileTable
45{
46 public:
47 /** @brief The file table is initialised by parsing the config file
48 * containing information about the files.
49 *
50 * @param[in] fileTableConfigPath - path to the json file containing
51 * information
52 */
53 FileTable(const std::string& fileTableConfigPath);
54 FileTable() = default;
55 ~FileTable() = default;
56 FileTable(const FileTable&) = default;
57 FileTable& operator=(const FileTable&) = default;
58 FileTable(FileTable&&) = default;
59 FileTable& operator=(FileTable&&) = default;
60
61 /** @brief Get the file attribute table
62 *
63 * @return Table- contents of the file attribute table
64 */
65 Table operator()() const;
66
67 /** @brief Get the FileEntry at the file handle
68 *
69 * @param[in] handle - file handle
70 *
71 * @return FileEntry - file entry at the handle
72 */
73 FileEntry at(Handle handle) const
74 {
75 return tableEntries.at(handle);
76 }
77
78 /** @brief Check is file attribute table is empty
79 *
80 * @return bool - true if file attribute table is empty, false otherwise.
81 */
82 bool isEmpty() const
83 {
84 return fileTable.empty();
85 }
86
87 /** @brief Clear the file table contents
88 *
89 */
90 void clear()
91 {
92 tableEntries.clear();
93 fileTable.clear();
94 padCount = 0;
95 checkSum = 0;
96 }
97
98 private:
99 /** @brief handle to FileEntry mappings for lookups based on file handle */
100 std::unordered_map<Handle, FileEntry> tableEntries;
101
102 /** @brief file attribute table including the pad bytes, except the checksum
103 */
104 std::vector<uint8_t> fileTable;
105
106 /** @brief the pad count of the file attribute table, the number of pad
107 * bytes is between 0 and 3 */
108 uint8_t padCount = 0;
109
110 /** @brief the checksum of the file attribute table */
111 uint32_t checkSum = 0;
112};
113
114} // namespace filetable
115} // namespace pldm