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