• 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 #define LOG_TAG "link_layer_socket_device"
18 
19 #include "link_layer_socket_device.h"
20 
21 #include <unistd.h>
22 
23 #include "osi/include/log.h"
24 #include "packets/link_layer/link_layer_packet_builder.h"
25 #include "packets/link_layer/link_layer_packet_view.h"
26 #include "packets/packet_view.h"
27 #include "packets/view.h"
28 
29 using std::vector;
30 
31 namespace test_vendor_lib {
32 
LinkLayerSocketDevice(int socket_fd,Phy::Type phy_type)33 LinkLayerSocketDevice::LinkLayerSocketDevice(int socket_fd, Phy::Type phy_type)
34     : socket_(socket_fd), phy_type_(phy_type) {}
35 
TimerTick()36 void LinkLayerSocketDevice::TimerTick() {
37   if (bytes_left_ == 0) {
38     received_ = std::make_shared<std::vector<uint8_t>>(Link::kSizeBytes);
39     size_t bytes_received = socket_.TryReceive(Link::kSizeBytes, received_->data());
40     if (bytes_received == 0) {
41       return;
42     }
43     CHECK(bytes_received == Link::kSizeBytes) << "bytes_received == " << bytes_received;
44     packets::PacketView<true> size({packets::View(received_, 0, Link::kSizeBytes)});
45     bytes_left_ = size.begin().extract<uint32_t>();
46     received_->resize(Link::kSizeBytes + bytes_left_);
47     offset_ = Link::kSizeBytes;
48   }
49   size_t bytes_received = socket_.TryReceive(bytes_left_, received_->data() + offset_);
50   if (bytes_received == 0) {
51     return;
52   }
53   bytes_left_ -= bytes_received;
54   offset_ += bytes_received;
55   if (bytes_left_ == 0) {
56     SendLinkLayerPacket(packets::LinkLayerPacketBuilder::ReWrap(received_), phy_type_);
57     offset_ = 0;
58     received_.reset();
59   }
60 }
61 
IncomingPacket(packets::LinkLayerPacketView packet)62 void LinkLayerSocketDevice::IncomingPacket(packets::LinkLayerPacketView packet) {
63   socket_.TrySend(packet);
64 }
65 
66 }  // namespace test_vendor_lib
67