Home | History | Annotate | Download | only in default
      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 <android-base/logging.h>
     23 #include <utils/Log.h>
     24 
     25 #include <fcntl.h>
     26 
     27 namespace android {
     28 namespace hardware {
     29 namespace bluetooth {
     30 namespace hci {
     31 
     32 MctProtocol::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 
     43 size_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 
     52 void MctProtocol::OnEventPacketReady() {
     53   event_cb_(event_packetizer_.GetPacket());
     54 }
     55 
     56 void MctProtocol::OnAclDataPacketReady() {
     57   acl_cb_(acl_packetizer_.GetPacket());
     58 }
     59 
     60 void MctProtocol::OnEventDataReady(int fd) {
     61   event_packetizer_.OnDataReady(fd, HCI_PACKET_TYPE_EVENT);
     62 }
     63 
     64 void 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
     72