• 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 #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 rootcanal {
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 phy_id, uint32_t device_id);
58   virtual void Send(
59       model::packets::LinkLayerPacketView packet,
60       uint32_t phy_id, uint32_t device_id);
61   std::list<std::shared_ptr<PhyLayer>> phy_layers_;
62 
63  private:
64   Phy::Type phy_type_;
65   uint32_t next_id_{1};
66   const uint32_t factory_id_;
67 };
68 
69 class PhyLayerImpl : public PhyLayer {
70  public:
71   PhyLayerImpl(Phy::Type phy_type, uint32_t id,
72                const std::function<void(model::packets::LinkLayerPacketView)>&
73                    device_receive,
74                uint32_t device_id, PhyLayerFactory* factory);
75   ~PhyLayerImpl() override;
76 
77   void Send(
78       std::shared_ptr<model::packets::LinkLayerPacketBuilder> packet) override;
79   void Send(model::packets::LinkLayerPacketView packet) override;
80   void Receive(model::packets::LinkLayerPacketView packet) override;
81   void Unregister() override;
82   bool IsFactoryId(uint32_t factory_id) override;
83   void TimerTick() override;
84 
85  private:
86   PhyLayerFactory* factory_;
87 };
88 }  // namespace rootcanal
89