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