• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2018 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 "link_layer_socket_device.h"
18 
19 #include <unistd.h>
20 
21 #include "packet/packet_view.h"
22 #include "packet/raw_builder.h"
23 #include "packet/view.h"
24 
25 using std::vector;
26 
27 namespace test_vendor_lib {
28 
LinkLayerSocketDevice(int socket_fd,Phy::Type phy_type)29 LinkLayerSocketDevice::LinkLayerSocketDevice(int socket_fd, Phy::Type phy_type)
30     : socket_(socket_fd),
31       phy_type_(phy_type),
32       size_bytes_(std::make_shared<std::vector<uint8_t>>(kSizeBytes)) {}
33 
TimerTick()34 void LinkLayerSocketDevice::TimerTick() {
35   if (receiving_size_) {
36     size_t bytes_received =
37         socket_.TryReceive(kSizeBytes, size_bytes_->data() + offset_);
38     if (bytes_received < bytes_left_) {
39       bytes_left_ -= bytes_received;
40       offset_ += bytes_received;
41       return;
42     }
43     bluetooth::packet::PacketView<bluetooth::packet::kLittleEndian> size(
44         {bluetooth::packet::View(size_bytes_, 0, kSizeBytes)});
45     bytes_left_ = size.begin().extract<uint32_t>();
46     received_ = std::make_shared<std::vector<uint8_t>>(bytes_left_);
47     offset_ = 0;
48     receiving_size_ = false;
49   }
50   size_t bytes_received = socket_.TryReceive(bytes_left_, received_->data() + offset_);
51   if (bytes_received < bytes_left_) {
52     bytes_left_ -= bytes_received;
53     offset_ += bytes_received;
54     return;
55   }
56   bytes_left_ = kSizeBytes;
57   offset_ = 0;
58   receiving_size_ = true;
59   auto packet = model::packets::LinkLayerPacketView::Create(
60       bluetooth::packet::PacketView<bluetooth::packet::kLittleEndian>(
61           received_));
62   ASSERT(packet.IsValid());
63   SendLinkLayerPacket(packet, phy_type_);
64 }
65 
IncomingPacket(model::packets::LinkLayerPacketView packet)66 void LinkLayerSocketDevice::IncomingPacket(
67     model::packets::LinkLayerPacketView packet) {
68   auto size_packet = bluetooth::packet::RawBuilder();
69   size_packet.AddOctets4(packet.size());
70   std::vector<uint8_t> size_bytes;
71   bluetooth::packet::BitInserter bit_inserter(size_bytes);
72   size_packet.Serialize(bit_inserter);
73 
74   if (socket_.TrySend(size_bytes) == kSizeBytes) {
75     std::vector<uint8_t> payload_bytes{packet.begin(), packet.end()};
76     socket_.TrySend(payload_bytes);
77   }
78 }
79 
80 }  // namespace test_vendor_lib
81