/* // 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 #include #include #include #include #include extern "C" { // For I2C_SMBUS_BLOCK_MAX #include } // 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; // 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; // indexed by block number (byte number / block size) using Cache = std::map; ReadBlockFunc readFunc; Cache cache; // byte offset of the end of the FRU (if readFunc has reported it) std::optional eof; };