107 lines
2.7 KiB
C++
107 lines
2.7 KiB
C++
#include "clock-constant-drift.h"
|
|
|
|
#include "ns3/log.h"
|
|
#include "ns3/simulator.h"
|
|
#include "ns3/uinteger.h"
|
|
#include <ns3/double.h>
|
|
|
|
#include "ns3/clock.h"
|
|
|
|
namespace ns3
|
|
{
|
|
|
|
NS_LOG_COMPONENT_DEFINE("ConstantDriftClock");
|
|
|
|
NS_OBJECT_ENSURE_REGISTERED(ConstantDriftClock);
|
|
|
|
TypeId
|
|
ConstantDriftClock::GetTypeId()
|
|
{
|
|
static TypeId tid =
|
|
TypeId("ns3::ConstantDriftClock")
|
|
.SetParent<Clock>()
|
|
.SetGroupName("tsn")
|
|
.AddConstructor<ConstantDriftClock>()
|
|
.AddAttribute("InitialOffset",
|
|
"Initial clock offset",
|
|
TimeValue(Time(0)),
|
|
MakeTimeAccessor(&ConstantDriftClock::m_initialOffset),
|
|
MakeTimeChecker())
|
|
.AddAttribute("DriftRate",
|
|
"Clock drift rate in ppm",
|
|
DoubleValue(0),
|
|
MakeDoubleAccessor(&ConstantDriftClock::m_driftRate),
|
|
MakeDoubleChecker<double>());
|
|
return tid;
|
|
}
|
|
|
|
|
|
ConstantDriftClock::ConstantDriftClock()
|
|
{
|
|
NS_LOG_FUNCTION(this);
|
|
}
|
|
|
|
ConstantDriftClock::~ConstantDriftClock()
|
|
{
|
|
NS_LOG_FUNCTION(this);
|
|
}
|
|
|
|
void
|
|
ConstantDriftClock::UpdateTime()
|
|
{
|
|
NS_LOG_FUNCTION(this);
|
|
if(!m_IsInitialised)
|
|
{
|
|
m_lastUpdateTimeValue = m_initialOffset;
|
|
m_IsInitialised = true;
|
|
}
|
|
|
|
Time durationSinceLastUpdate = Simulator::Now() - m_lastUpdateTime;
|
|
|
|
uint64_t t = (m_lastUpdateTimeValue + durationSinceLastUpdate + durationSinceLastUpdate * m_driftRate * pow(10,-6)).GetNanoSeconds();
|
|
Time tmp = Time(NanoSeconds( (t/m_granularity.GetNanoSeconds()) * m_granularity.GetNanoSeconds()));
|
|
if(m_lastUpdateTimeValue != tmp)
|
|
{
|
|
m_lastUpdateTimeValue = tmp;
|
|
m_lastUpdateTime = Simulator::Now();
|
|
}
|
|
}
|
|
|
|
Time
|
|
ConstantDriftClock::GetLocalTime()
|
|
{
|
|
NS_LOG_FUNCTION(this);
|
|
return GetUncorrectedLocalTime() + m_correctionOffset;
|
|
}
|
|
|
|
Time
|
|
ConstantDriftClock::GetUncorrectedLocalTime()
|
|
{
|
|
NS_LOG_FUNCTION(this);
|
|
UpdateTime();
|
|
return m_lastUpdateTimeValue;
|
|
}
|
|
|
|
Time
|
|
ConstantDriftClock::GetTimeAt(Time t)
|
|
{
|
|
NS_LOG_FUNCTION(this);
|
|
UpdateTime();
|
|
Time duration = t - m_lastUpdateTime;
|
|
uint64_t tmp = (m_lastUpdateTimeValue + duration + duration * m_driftRate * pow(10,-6)).GetNanoSeconds();
|
|
return m_correctionOffset + Time(NanoSeconds( (tmp/m_granularity.GetNanoSeconds()) * m_granularity.GetNanoSeconds()));
|
|
|
|
}
|
|
|
|
Time
|
|
ConstantDriftClock::GetDuration(Time d)
|
|
{
|
|
NS_LOG_FUNCTION(this);
|
|
uint64_t tmp = (d/(m_driftRate * pow(10,-6) + 1)).GetNanoSeconds();
|
|
return Time(NanoSeconds( (1 + tmp/m_granularity.GetNanoSeconds()) * m_granularity.GetNanoSeconds()));
|
|
|
|
}
|
|
|
|
|
|
}
|