blob: 4083be1af4d88f10e09cfa9c838ed8419a18f51c [file] [log] [blame] [edit]
/*
// Copyright (c) 2022 Equinix, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
*/
#pragma once
#include <cstdint>
#include <cstdio>
#include <functional>
#include <map>
#include <optional>
#include <utility>
extern "C"
{
// For I2C_SMBUS_BLOCK_MAX
#include <linux/i2c.h>
}
// A function to read up to I2C_SMBUS_BLOCK_MAX bytes of FRU data. Returns
// negative on error, or the number of bytes read otherwise, which may be (but
// is not guaranteed to be) less than len if the read would go beyond the end
// of the FRU.
using ReadBlockFunc =
std::function<int64_t(off_t offset, size_t len, uint8_t* outbuf)>;
// A caching wrapper around a ReadBlockFunc
class FRUReader
{
public:
explicit FRUReader(ReadBlockFunc readFunc) : readFunc(std::move(readFunc))
{}
// The ::read() operation here is analogous to ReadBlockFunc (with the same
// return value semantics), but is not subject to SMBus block size
// limitations; it can read as much data as needed in a single call.
ssize_t read(off_t start, size_t len, uint8_t* outbuf);
private:
static constexpr size_t cacheBlockSize = 32;
static_assert(cacheBlockSize <= I2C_SMBUS_BLOCK_MAX);
using CacheBlock = std::array<uint8_t, cacheBlockSize>;
// indexed by block number (byte number / block size)
using Cache = std::map<uint32_t, CacheBlock>;
ReadBlockFunc readFunc;
Cache cache;
// byte offset of the end of the FRU (if readFunc has reported it)
std::optional<size_t> eof;
};