/*
 * Copyright (C) 2022 The Android Open Source Project
 *
 * 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 "fake_link.h"

#include <cstring>

#include "chpp/log.h"
#include "packet_util.h"

namespace chpp::test {

void FakeLink::appendTxPacket(uint8_t *data, size_t len) {
  std::vector<uint8_t> pkt;
  pkt.resize(len);
  memcpy(pkt.data(), data, len);
  checkPacketValidity(pkt);
  {
    std::lock_guard<std::mutex> lock(mMutex);
    mTxPackets.emplace_back(std::move(pkt));
    mTxCondVar.notify_all();
  }
}

int FakeLink::getTxPacketCount() {
  std::lock_guard<std::mutex> lock(mMutex);
  return static_cast<int>(mTxPackets.size());
}

bool FakeLink::waitForTxPacket(std::chrono::milliseconds timeout) {
  std::unique_lock<std::mutex> lock(mMutex);
  auto now = std::chrono::system_clock::now();
  CHPP_LOGD("FakeLink::waitForTxPacket waiting...");
  return mTxCondVar.wait_until(lock, now + timeout,
                               [this] { return !mTxPackets.empty(); });
}

bool FakeLink::waitForEmpty(std::chrono::milliseconds timeout) {
  std::unique_lock<std::mutex> lock(mMutex);
  auto now = std::chrono::system_clock::now();
  CHPP_LOGD("FakeLink::waitForEmpty waiting...");
  return mRxCondVar.wait_until(lock, now + timeout,
                               [this] { return mTxPackets.empty(); });
}

std::vector<uint8_t> FakeLink::popTxPacket() {
  std::lock_guard<std::mutex> lock(mMutex);
  assert(!mTxPackets.empty());
  std::vector<uint8_t> vec = std::move(mTxPackets.front());
  mTxPackets.pop_front();
  mRxCondVar.notify_all();
  return vec;
}

void FakeLink::reset() {
  std::lock_guard<std::mutex> lock(mMutex);
  mTxPackets.clear();
}

}  // namespace chpp::test