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 <list>
20 #include <memory>
21 #include <vector>
22 
23 #include "include/phy.h"
24 #include "packets/link_layer_packets.h"
25 #include "phy_layer.h"
26 
27 namespace test_vendor_lib {
28 
29 class PhyLayerFactory {
30   friend class PhyLayerImpl;
31 
32  public:
33   PhyLayerFactory(Phy::Type phy_type, uint32_t factory_id);
34 
35   virtual ~PhyLayerFactory() = default;
36 
37   Phy::Type GetType();
38 
39   uint32_t GetFactoryId();
40 
41   std::shared_ptr<PhyLayer> GetPhyLayer(
42       const std::function<void(model::packets::LinkLayerPacketView)>&
43           device_receive,
44       uint32_t device_id);
45 
46   void UnregisterPhyLayer(uint32_t id);
47 
48   void UnregisterAllPhyLayers();
49 
50   virtual void TimerTick();
51 
52   virtual std::string ToString() const;
53 
54  protected:
55   virtual void Send(
56       std::shared_ptr<model::packets::LinkLayerPacketBuilder> packet,
57       uint32_t id);
58   virtual void Send(model::packets::LinkLayerPacketView packet, uint32_t id);
59 
60  private:
61   Phy::Type phy_type_;
62   std::list<std::shared_ptr<PhyLayer>> phy_layers_;
63   uint32_t next_id_{1};
64   const uint32_t factory_id_;
65 };
66 
67 class PhyLayerImpl : public PhyLayer {
68  public:
69   PhyLayerImpl(Phy::Type phy_type, uint32_t id,
70                const std::function<void(model::packets::LinkLayerPacketView)>&
71                    device_receive,
72                uint32_t device_id, PhyLayerFactory* factory);
73   ~PhyLayerImpl() override;
74 
75   void Send(
76       std::shared_ptr<model::packets::LinkLayerPacketBuilder> packet) override;
77   void Send(model::packets::LinkLayerPacketView packet) override;
78   void Receive(model::packets::LinkLayerPacketView packet) override;
79   void Unregister() override;
80   bool IsFactoryId(uint32_t factory_id) override;
81   void TimerTick() override;
82 
83 
84  private:
85   PhyLayerFactory* factory_;
86 };
87 }  // namespace test_vendor_lib
88