//
// Copyright 2017 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 "mct_protocol.h"

#include <assert.h>

#define LOG_TAG "android.hardware.bluetooth-hci-mct"
#include <utils/Log.h>

#include <fcntl.h>

namespace android {
namespace hardware {
namespace bluetooth {
namespace hci {

MctProtocol::MctProtocol(int* fds, PacketReadCallback event_cb,
                         PacketReadCallback acl_cb)
    : event_cb_(event_cb),
      acl_cb_(acl_cb),
      event_packetizer_([this]() { OnEventPacketReady(); }),
      acl_packetizer_([this]() { OnAclDataPacketReady(); }) {
  for (int i = 0; i < CH_MAX; i++) {
    uart_fds_[i] = fds[i];
  }
}

size_t MctProtocol::Send(uint8_t type, const uint8_t* data, size_t length) {
  if (type == HCI_PACKET_TYPE_COMMAND)
    return WriteSafely(uart_fds_[CH_CMD], data, length);
  if (type == HCI_PACKET_TYPE_ACL_DATA)
    return WriteSafely(uart_fds_[CH_ACL_OUT], data, length);
  LOG_ALWAYS_FATAL("%s: Unimplemented packet type = %d", __func__, type);
  return 0;
}

void MctProtocol::OnEventPacketReady() {
  event_cb_(event_packetizer_.GetPacket());
}

void MctProtocol::OnAclDataPacketReady() {
  acl_cb_(acl_packetizer_.GetPacket());
}

void MctProtocol::OnEventDataReady(int fd) {
  event_packetizer_.OnDataReady(fd, HCI_PACKET_TYPE_EVENT);
}

void MctProtocol::OnAclDataReady(int fd) {
  acl_packetizer_.OnDataReady(fd, HCI_PACKET_TYPE_ACL_DATA);
}

}  // namespace hci
}  // namespace bluetooth
}  // namespace hardware
}  // namespace android