1 // 2 // Copyright (c) 2010-2018 Antmicro 3 // 4 // This file is licensed under the MIT License. 5 // Full license text is available in 'licenses/MIT.txt'. 6 // 7 using System; 8 using Antmicro.Renode.Peripherals.Wireless; 9 using Antmicro.Renode.Exceptions; 10 using Antmicro.Renode.Core; 11 12 namespace Antmicro.Renode.Network 13 { 14 public static class RangeLossMediumExtension 15 { SetRangeLossWirelessFunction(this WirelessMedium wirelessMedium, int lossRange = 0, float txRatio = 0, float rxRatio = 0)16 public static void SetRangeLossWirelessFunction(this WirelessMedium wirelessMedium, int lossRange = 0, float txRatio = 0, float rxRatio = 0) 17 { 18 var function = new RangeLossMediumFunction(); 19 function.LossRange = lossRange; 20 function.TxRatio = txRatio; 21 function.RxRatio = rxRatio; 22 wirelessMedium.SetMediumFunction(function); 23 } 24 } 25 26 public class RangeLossMediumFunction : IMediumFunction 27 { CanReach(Position from, Position to)28 public bool CanReach(Position from, Position to) 29 { 30 var distance = Math.Sqrt(Math.Pow((double)(to.X - from.X), 2) + Math.Pow((double)(to.Y - from.Y), 2) + Math.Pow((double)(to.Z - from.Z), 2)); 31 var receptionSuccessRatio = 1 - ((distance / LossRange) * (1 - RxRatio)); 32 var receptionSuccess = EmulationManager.Instance.CurrentEmulation.RandomGenerator.NextDouble(); 33 34 if(receptionSuccess > receptionSuccessRatio) 35 { 36 return false; 37 } 38 return true; 39 } 40 CanTransmit(Position from)41 public bool CanTransmit(Position from) 42 { 43 var transmissionSuccess = EmulationManager.Instance.CurrentEmulation.RandomGenerator.NextDouble(); 44 if(transmissionSuccess > TxRatio) 45 { 46 return false; 47 } 48 return true; 49 } 50 51 public int LossRange { get; set; } 52 53 public float TxRatio 54 { 55 get 56 { 57 return txRatio; 58 } 59 set 60 { 61 if(value < 0 || value > 1.0f) 62 { 63 throw new RecoverableException("TxRatio must be between 0 and 1."); 64 } 65 else 66 { 67 txRatio = value; 68 } 69 } 70 } 71 72 public float RxRatio 73 { 74 get 75 { 76 return rxRatio; 77 } 78 set 79 { 80 if(value < 0 || value > 1.0f) 81 { 82 throw new RecoverableException("RxRatio must be between 0 and 1."); 83 } 84 else 85 { 86 rxRatio = value; 87 } 88 } 89 } 90 91 public string FunctionName { get { return Name; } } 92 93 private float txRatio; 94 private float rxRatio; 95 private const string Name = "range_loss_medium_function"; 96 } 97 } 98 99