1 //
2 // Copyright (c) 2010-2023 Antmicro
3 // Copyright (c) 2011-2015 Realtime Embedded
4 //
5 // This file is licensed under the MIT License.
6 // Full license text is available in 'licenses/MIT.txt'.
7 //
8 using System;
9 using System.Linq;
10 using System.Collections.Generic;
11 using Antmicro.Renode.Core;
12 using Antmicro.Renode.Core.Structure;
13 using Antmicro.Renode.Exceptions;
14 
15 namespace Antmicro.Renode.Peripherals.Network
16 {
17     public abstract class NetworkWithPHY: IPeripheral, IPeripheralContainer<IPhysicalLayer, PHYRegistrationPoint>
18     {
NetworkWithPHY(IMachine machine)19         protected NetworkWithPHY(IMachine machine)
20         {
21             phys = new Dictionary<uint, IPhysicalLayer>();
22             this.machine = machine;
23         }
24 
25         #region IRegister[IPHYInterface,PHYRegistrationPoint] implementation
Register(IPhysicalLayer peripheral, PHYRegistrationPoint registrationPoint)26         public void Register(IPhysicalLayer peripheral, PHYRegistrationPoint registrationPoint)
27         {
28             if (phys.ContainsKey(registrationPoint.Id))
29             {
30                 throw new RecoverableException("Selected registration port is already taken.");
31             }
32             phys.Add(registrationPoint.Id, peripheral);
33             machine.RegisterAsAChildOf(this, peripheral, registrationPoint);
34         }
35 
Unregister(IPhysicalLayer peripheral)36         public void Unregister(IPhysicalLayer peripheral)
37         {
38             var key = phys.First(x => x.Value == peripheral).Key;
39             phys.Remove(key);
40             machine.UnregisterAsAChildOf(this, peripheral);
41         }
42         #endregion
43 
44         #region IContainer[IPHYInterface,PHYRegistrationPoint] implementation
GetRegistrationPoints(IPhysicalLayer peripheral)45         public IEnumerable<PHYRegistrationPoint> GetRegistrationPoints(IPhysicalLayer peripheral)
46         {
47             return phys.Select(x => new PHYRegistrationPoint(x.Key));
48         }
49 
50         public IEnumerable<IRegistered<IPhysicalLayer, PHYRegistrationPoint>> Children =>
51             phys.Select(x => Registered.Create(x.Value, new PHYRegistrationPoint(x.Key)));
52         #endregion
53 
TryGetPhy(uint id, out IPhysicalLayer<T> phy)54         protected bool TryGetPhy<T>(uint id, out IPhysicalLayer<T> phy)
55         {
56             if(phys.TryGetValue(id, out var _phy) && _phy is IPhysicalLayer<T>)
57             {
58                 phy = (IPhysicalLayer<T>)_phy;
59                 return true;
60             }
61             phy = default(IPhysicalLayer<T>);
62             return false;
63         }
64 
TryGetPhy(uint id, out IPhysicalLayer<T, V> phy)65         protected bool TryGetPhy<T, V>(uint id, out IPhysicalLayer<T, V> phy)
66         {
67             if(phys.TryGetValue(id, out var _phy) && _phy is IPhysicalLayer<T, V>)
68             {
69                 phy = (IPhysicalLayer<T, V>)_phy;
70                 return true;
71             }
72             phy = default(IPhysicalLayer<T, V>);
73             return false;
74         }
75 
76         protected Dictionary<uint, IPhysicalLayer> phys;
77         protected IMachine machine;
78 
Reset()79         public abstract void Reset();
80     }
81 }
82 
83