blob: 4083be1af4d88f10e09cfa9c838ed8419a18f51c [file] [log] [blame]
Zev Weiss309c0b12022-02-25 01:44:12 +00001/*
2// Copyright (c) 2022 Equinix, Inc.
3//
4// Licensed under the Apache License, Version 2.0 (the "License");
5// you may not use this file except in compliance with the License.
6// You may obtain a copy of the License at
7//
8// http://www.apache.org/licenses/LICENSE-2.0
9//
10// Unless required by applicable law or agreed to in writing, software
11// distributed under the License is distributed on an "AS IS" BASIS,
12// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13// See the License for the specific language governing permissions and
14// limitations under the License.
15*/
16
17#pragma once
18
19#include <cstdint>
Patrick Williamsb9a1f962024-07-13 16:34:49 -050020#include <cstdio>
Zev Weiss309c0b12022-02-25 01:44:12 +000021#include <functional>
22#include <map>
23#include <optional>
24#include <utility>
25
26extern "C"
27{
28// For I2C_SMBUS_BLOCK_MAX
29#include <linux/i2c.h>
30}
31
32// A function to read up to I2C_SMBUS_BLOCK_MAX bytes of FRU data. Returns
33// negative on error, or the number of bytes read otherwise, which may be (but
34// is not guaranteed to be) less than len if the read would go beyond the end
35// of the FRU.
36using ReadBlockFunc =
37 std::function<int64_t(off_t offset, size_t len, uint8_t* outbuf)>;
38
39// A caching wrapper around a ReadBlockFunc
40class FRUReader
41{
42 public:
Ed Tanous3013fb42022-07-09 08:27:06 -070043 explicit FRUReader(ReadBlockFunc readFunc) : readFunc(std::move(readFunc))
Zev Weiss309c0b12022-02-25 01:44:12 +000044 {}
45 // The ::read() operation here is analogous to ReadBlockFunc (with the same
46 // return value semantics), but is not subject to SMBus block size
47 // limitations; it can read as much data as needed in a single call.
48 ssize_t read(off_t start, size_t len, uint8_t* outbuf);
49
50 private:
51 static constexpr size_t cacheBlockSize = 32;
52 static_assert(cacheBlockSize <= I2C_SMBUS_BLOCK_MAX);
53 using CacheBlock = std::array<uint8_t, cacheBlockSize>;
54
55 // indexed by block number (byte number / block size)
56 using Cache = std::map<uint32_t, CacheBlock>;
57
58 ReadBlockFunc readFunc;
59 Cache cache;
60
61 // byte offset of the end of the FRU (if readFunc has reported it)
62 std::optional<size_t> eof;
63};