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 <stddef.h> // for size_t 20 21 #include <cstdint> // for uint8_t, uint32_t 22 #include <memory> // for shared_ptr, make_shared 23 #include <string> // for string 24 #include <vector> // for vector 25 26 #include "device.h" // for Device 27 #include "include/phy.h" // for Phy, Phy::Type 28 #include "net/async_data_channel.h" // for AsyncDataChannel 29 #include "packets/link_layer_packets.h" // for LinkLayerPacketView 30 31 namespace rootcanal { 32 33 using android::net::AsyncDataChannel; 34 35 class LinkLayerSocketDevice : public Device { 36 public: 37 LinkLayerSocketDevice(std::shared_ptr<AsyncDataChannel> socket_fd, 38 Phy::Type phy_type); 39 LinkLayerSocketDevice(LinkLayerSocketDevice&& s) = default; 40 virtual ~LinkLayerSocketDevice() = default; 41 Create(std::shared_ptr<AsyncDataChannel> socket_fd,Phy::Type phy_type)42 static std::unique_ptr<Device> Create( 43 std::shared_ptr<AsyncDataChannel> socket_fd, Phy::Type phy_type) { 44 return std::make_unique<LinkLayerSocketDevice>(socket_fd, phy_type); 45 } 46 GetTypeString()47 virtual std::string GetTypeString() const override { 48 return "link_layer_socket_device"; 49 } 50 51 virtual void ReceiveLinkLayerPacket( 52 model::packets::LinkLayerPacketView packet, Phy::Type type, 53 int8_t rssi) override; 54 55 virtual void Tick() override; 56 virtual void Close() override; 57 58 static constexpr size_t kSizeBytes = sizeof(uint32_t); 59 60 private: 61 std::shared_ptr<AsyncDataChannel> socket_; 62 Phy::Type phy_type_; 63 bool receiving_size_{true}; 64 size_t bytes_left_{kSizeBytes}; 65 size_t offset_{0}; 66 std::shared_ptr<std::vector<uint8_t>> size_bytes_; 67 std::shared_ptr<std::vector<uint8_t>> received_; 68 std::vector<model::packets::LinkLayerPacketView> packet_queue_; 69 }; 70 71 } // namespace rootcanal 72