/*
// 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.
*/

#include "tlbmc/utils/fru_reader.h"

#include <algorithm>
#include <cassert>
#include <cstdint>
#include <cstring>

namespace milotic_tlbmc {

ssize_t FRUReader::read(off_t start, size_t len, uint8_t* outbuf) {
  size_t done = 0;
  size_t remaining = len;
  size_t cursor = start;
  while (done < len) {
    if (eof.has_value() && cursor >= eof.value()) {
      break;
    }

    const uint8_t* blkData = nullptr;
    size_t available = 0;
    size_t blk = cursor / cacheBlockSize;
    size_t blkOffset = cursor % cacheBlockSize;
    auto findBlk = cache.find(blk);
    if (findBlk == cache.end()) {
      // miss, populate cache
      uint8_t* newData = cache[blk].data();
      int64_t ret = readFunc(blk * cacheBlockSize, cacheBlockSize, newData);

      // if we've reached the end of the eeprom, record its size
      if (ret >= 0 && static_cast<size_t>(ret) < cacheBlockSize) {
        eof = (blk * cacheBlockSize) + ret;
      }

      if (ret <= 0) {
        // don't leave empty blocks in the cache
        cache.erase(blk);
        return done != 0U ? done : ret;
      }

      blkData = newData;
      available = ret;
    } else {
      // hit, use cached data
      blkData = findBlk->second.data();

      // if the hit is to the block containing the (previously
      // discovered on the miss that populated it) end of the eeprom,
      // don't copy spurious bytes past the end
      if (eof.has_value() && (eof.value() / cacheBlockSize == blk)) {
        available = eof.value() % cacheBlockSize;
      } else {
        available = cacheBlockSize;
      }
    }

    size_t toCopy = (blkOffset >= available)
                        ? 0
                        : std::min(available - blkOffset, remaining);

    // NOLINTNEXTLINE(cppcoreguidelines-pro-bounds-pointer-arithmetic)
    memcpy(outbuf + done, blkData + blkOffset, toCopy);
    cursor += toCopy;
    done += toCopy;
    remaining -= toCopy;
  }

  return done;
}

}  // namespace milotic_tlbmc
