Zev Weiss | 309c0b1 | 2022-02-25 01:44:12 +0000 | [diff] [blame] | 1 | /* |
| 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 Williams | b9a1f96 | 2024-07-13 16:34:49 -0500 | [diff] [blame] | 20 | #include <cstdio> |
Zev Weiss | 309c0b1 | 2022-02-25 01:44:12 +0000 | [diff] [blame] | 21 | #include <functional> |
| 22 | #include <map> |
| 23 | #include <optional> |
| 24 | #include <utility> |
| 25 | |
| 26 | extern "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. |
| 36 | using ReadBlockFunc = |
| 37 | std::function<int64_t(off_t offset, size_t len, uint8_t* outbuf)>; |
| 38 | |
| 39 | // A caching wrapper around a ReadBlockFunc |
| 40 | class FRUReader |
| 41 | { |
| 42 | public: |
Ed Tanous | 3013fb4 | 2022-07-09 08:27:06 -0700 | [diff] [blame] | 43 | explicit FRUReader(ReadBlockFunc readFunc) : readFunc(std::move(readFunc)) |
Zev Weiss | 309c0b1 | 2022-02-25 01:44:12 +0000 | [diff] [blame] | 44 | {} |
| 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 | }; |