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 #pragma once 18 19 #include <cstdint> 20 #include <vector> 21 22 #include "device.h" 23 #include "include/phy.h" 24 #include "packets/link_layer_packets.h" 25 #include "polled_socket.h" 26 27 namespace test_vendor_lib { 28 29 class LinkLayerSocketDevice : public Device { 30 public: 31 LinkLayerSocketDevice(int socket_fd, Phy::Type phy_type); 32 LinkLayerSocketDevice(LinkLayerSocketDevice&& s) = default; 33 virtual ~LinkLayerSocketDevice() = default; 34 Create(int socket_fd,Phy::Type phy_type)35 static std::shared_ptr<Device> Create(int socket_fd, Phy::Type phy_type) { 36 return std::make_shared<LinkLayerSocketDevice>(socket_fd, phy_type); 37 } 38 GetTypeString()39 virtual std::string GetTypeString() const override { 40 return "link_layer_socket_device"; 41 } 42 Initialize(const std::vector<std::string> &)43 virtual void Initialize(const std::vector<std::string>&) override {} 44 45 virtual void IncomingPacket( 46 model::packets::LinkLayerPacketView packet) override; 47 48 virtual void TimerTick() override; 49 50 static constexpr size_t kSizeBytes = sizeof(uint32_t); 51 52 private: 53 net::PolledSocket socket_; 54 Phy::Type phy_type_; 55 bool receiving_size_{true}; 56 size_t bytes_left_{kSizeBytes}; 57 size_t offset_{0}; 58 std::shared_ptr<std::vector<uint8_t>> size_bytes_; 59 std::shared_ptr<std::vector<uint8_t>> received_; 60 std::vector<model::packets::LinkLayerPacketView> packet_queue_; 61 }; 62 63 } // namespace test_vendor_lib 64