xref: /openbmc/entity-manager/src/fru_device/fru_reader.hpp (revision 4e1142d6f418f48ea260132ebb5a4995b2310c90)
1 // SPDX-License-Identifier: Apache-2.0
2 // SPDX-FileCopyrightText: Copyright 2022 Equinix, Inc.
3 
4 #pragma once
5 
6 #include <cstdint>
7 #include <cstdio>
8 #include <functional>
9 #include <map>
10 #include <optional>
11 #include <utility>
12 
13 extern "C"
14 {
15 // For I2C_SMBUS_BLOCK_MAX
16 #include <linux/i2c.h>
17 }
18 
19 // A function to read up to I2C_SMBUS_BLOCK_MAX bytes of FRU data.  Returns
20 // negative on error, or the number of bytes read otherwise, which may be (but
21 // is not guaranteed to be) less than len if the read would go beyond the end
22 // of the FRU.
23 using ReadBlockFunc =
24     std::function<int64_t(off_t offset, size_t len, uint8_t* outbuf)>;
25 
26 // A caching wrapper around a ReadBlockFunc
27 class FRUReader
28 {
29   public:
FRUReader(ReadBlockFunc readFunc)30     explicit FRUReader(ReadBlockFunc readFunc) : readFunc(std::move(readFunc))
31     {}
32     // The ::read() operation here is analogous to ReadBlockFunc (with the same
33     // return value semantics), but is not subject to SMBus block size
34     // limitations; it can read as much data as needed in a single call.
35     ssize_t read(off_t start, size_t len, uint8_t* outbuf);
36 
37   private:
38     static constexpr size_t cacheBlockSize = 32;
39     static_assert(cacheBlockSize <= I2C_SMBUS_BLOCK_MAX);
40     using CacheBlock = std::array<uint8_t, cacheBlockSize>;
41 
42     // indexed by block number (byte number / block size)
43     using Cache = std::map<uint32_t, CacheBlock>;
44 
45     ReadBlockFunc readFunc;
46     Cache cache;
47 
48     // byte offset of the end of the FRU (if readFunc has reported it)
49     std::optional<size_t> eof;
50 };
51