• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "h4_data_channel_packetizer.h"
18 
19 #include <string.h>  // for strerror, size_t
20 #include <unistd.h>  // for ssize_t
21 
22 #include <cerrno>       // for errno, EAGAIN, ECONNRESET
23 #include <cstdint>      // for uint8_t
24 #include <functional>   // for function
25 #include <type_traits>  // for remove_extent_t
26 #include <utility>      // for move
27 #include <vector>       // for vector
28 
29 #include "log.h"                     // for LOG_ERROR, LOG_ALWAYS_FATAL
30 #include "model/hci/h4_parser.h"     // for H4Parser, ClientDisconnectCa...
31 #include "model/hci/hci_protocol.h"  // for PacketReadCallback, AsyncDataChannel
32 #include "net/async_data_channel.h"  // for AsyncDataChannel
33 
34 namespace rootcanal {
35 
H4DataChannelPacketizer(std::shared_ptr<AsyncDataChannel> socket,PacketReadCallback command_cb,PacketReadCallback event_cb,PacketReadCallback acl_cb,PacketReadCallback sco_cb,PacketReadCallback iso_cb,ClientDisconnectCallback disconnect_cb)36 H4DataChannelPacketizer::H4DataChannelPacketizer(
37     std::shared_ptr<AsyncDataChannel> socket, PacketReadCallback command_cb,
38     PacketReadCallback event_cb, PacketReadCallback acl_cb,
39     PacketReadCallback sco_cb, PacketReadCallback iso_cb,
40     ClientDisconnectCallback disconnect_cb)
41     : uart_socket_(socket),
42       h4_parser_(command_cb, event_cb, acl_cb, sco_cb, iso_cb, true),
43       disconnect_cb_(std::move(disconnect_cb)) {}
44 
Send(uint8_t type,const uint8_t * data,size_t length)45 size_t H4DataChannelPacketizer::Send(uint8_t type, const uint8_t* data,
46                                      size_t length) {
47   ssize_t ret = uart_socket_->Send(&type, sizeof(type));
48   if (ret == -1) {
49     LOG_ERROR("Error writing to UART (%s)", strerror(errno));
50   }
51   size_t to_be_written = ret;
52 
53   ret = uart_socket_->Send(data, length);
54   if (ret == -1) {
55     LOG_ERROR("Error writing to UART (%s)", strerror(errno));
56   }
57   to_be_written += ret;
58 
59   if (to_be_written != length + sizeof(type)) {
60     LOG_ERROR("%d / %d bytes written - something went wrong...",
61               static_cast<int>(to_be_written),
62               static_cast<int>(length + sizeof(type)));
63   }
64   return to_be_written;
65 }
66 
OnDataReady(std::shared_ptr<AsyncDataChannel> socket)67 void H4DataChannelPacketizer::OnDataReady(
68    std::shared_ptr<AsyncDataChannel> socket) {
69 
70   // Continue reading from the async data channel as long as bytes
71   // are available to read. Otherwise this limits the number of HCI
72   // packets parsed to one every 3 ticks.
73   for (;;) {
74     ssize_t bytes_to_read = h4_parser_.BytesRequested();
75     std::vector<uint8_t> buffer(bytes_to_read);
76 
77     ssize_t bytes_read = socket->Recv(buffer.data(), bytes_to_read);
78     if (bytes_read == 0) {
79       LOG_INFO("remote disconnected!");
80       disconnected_ = true;
81       disconnect_cb_();
82       return;
83     }
84     if (bytes_read < 0) {
85       if (errno == EAGAIN) {
86         // No data, try again later.
87         return;
88       }
89       if (errno == ECONNRESET) {
90         // They probably rejected our packet
91         disconnected_ = true;
92         disconnect_cb_();
93         return;
94       }
95       LOG_ALWAYS_FATAL("Read error in %u: %s", h4_parser_.CurrentState(),
96                        strerror(errno));
97     }
98     h4_parser_.Consume(buffer.data(), bytes_read);
99   }
100 }
101 
102 }  // namespace rootcanal
103