1//
2// Copyright 2017 The Android Open Source Project
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#include "mct_protocol.h"
18
19#include <assert.h>
20
21#define LOG_TAG "android.hardware.bluetooth-hci-mct"
22#include <utils/Log.h>
23
24#include <fcntl.h>
25
26namespace android {
27namespace hardware {
28namespace bluetooth {
29namespace hci {
30
31MctProtocol::MctProtocol(int* fds, PacketReadCallback event_cb,
32                         PacketReadCallback acl_cb)
33    : event_cb_(event_cb),
34      acl_cb_(acl_cb),
35      event_packetizer_([this]() { OnEventPacketReady(); }),
36      acl_packetizer_([this]() { OnAclDataPacketReady(); }) {
37  for (int i = 0; i < CH_MAX; i++) {
38    uart_fds_[i] = fds[i];
39  }
40}
41
42size_t MctProtocol::Send(uint8_t type, const uint8_t* data, size_t length) {
43  if (type == HCI_PACKET_TYPE_COMMAND)
44    return WriteSafely(uart_fds_[CH_CMD], data, length);
45  if (type == HCI_PACKET_TYPE_ACL_DATA)
46    return WriteSafely(uart_fds_[CH_ACL_OUT], data, length);
47  LOG_ALWAYS_FATAL("%s: Unimplemented packet type = %d", __func__, type);
48  return 0;
49}
50
51void MctProtocol::OnEventPacketReady() {
52  event_cb_(event_packetizer_.GetPacket());
53}
54
55void MctProtocol::OnAclDataPacketReady() {
56  acl_cb_(acl_packetizer_.GetPacket());
57}
58
59void MctProtocol::OnEventDataReady(int fd) {
60  event_packetizer_.OnDataReady(fd, HCI_PACKET_TYPE_EVENT);
61}
62
63void MctProtocol::OnAclDataReady(int fd) {
64  acl_packetizer_.OnDataReady(fd, HCI_PACKET_TYPE_ACL_DATA);
65}
66
67}  // namespace hci
68}  // namespace bluetooth
69}  // namespace hardware
70}  // namespace android
71