blob: 813cebd239ebd1fb97d512056f96fe6b6a64a4a8 [file] [log] [blame]
Zach Johnson917efb12017-02-26 23:46:05 -08001//
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 <android-base/logging.h>
23#include <utils/Log.h>
24
25#include <fcntl.h>
26
27namespace android {
28namespace hardware {
29namespace bluetooth {
30namespace hci {
31
32MctProtocol::MctProtocol(int* fds, PacketReadCallback event_cb,
33 PacketReadCallback acl_cb)
34 : event_cb_(event_cb),
35 acl_cb_(acl_cb),
36 event_packetizer_([this]() { OnEventPacketReady(); }),
37 acl_packetizer_([this]() { OnAclDataPacketReady(); }) {
38 for (int i = 0; i < CH_MAX; i++) {
39 uart_fds_[i] = fds[i];
40 }
41}
42
43size_t MctProtocol::Send(uint8_t type, const uint8_t* data, size_t length) {
44 if (type == HCI_PACKET_TYPE_COMMAND)
45 return WriteSafely(uart_fds_[CH_CMD], data, length);
46 if (type == HCI_PACKET_TYPE_ACL_DATA)
47 return WriteSafely(uart_fds_[CH_ACL_OUT], data, length);
48 CHECK(type == HCI_PACKET_TYPE_COMMAND || type == HCI_PACKET_TYPE_ACL_DATA);
49 return 0;
50}
51
52void MctProtocol::OnEventPacketReady() {
53 event_cb_(event_packetizer_.GetPacket());
54}
55
56void MctProtocol::OnAclDataPacketReady() {
57 acl_cb_(acl_packetizer_.GetPacket());
58}
59
60void MctProtocol::OnEventDataReady(int fd) {
61 event_packetizer_.OnDataReady(fd, HCI_PACKET_TYPE_EVENT);
62}
63
64void MctProtocol::OnAclDataReady(int fd) {
65 acl_packetizer_.OnDataReady(fd, HCI_PACKET_TYPE_ACL_DATA);
66}
67
68} // namespace hci
69} // namespace bluetooth
70} // namespace hardware
71} // namespace android