-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathcapture_fb.cpp
More file actions
175 lines (145 loc) · 5.36 KB
/
capture_fb.cpp
File metadata and controls
175 lines (145 loc) · 5.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
#include <asam_cmp_capture_module/capture_fb.h>
#include <asam_cmp_capture_module/interface_fb.h>
#include <coreobjects/callable_info_factory.h>
#include <coreobjects/argument_info_factory.h>
#include <set>
#include <fmt/format.h>
#include <asam_cmp/cmp_header.h>
#include <asam_cmp_common_lib/ethernet_pcpp_itf.h>
#include <chrono>
BEGIN_NAMESPACE_ASAM_CMP_CAPTURE_MODULE
CaptureFb::CaptureFb(const ModuleInfoPtr& moduleInfo,
const ContextPtr& ctx,
const ComponentPtr& parent,
const StringPtr& localId,
const CaptureFbInit& init)
: asam_cmp_common_lib::CaptureCommonFb(moduleInfo, ctx, parent, localId)
, allowJumboFrames(false)
, ethernetWrapper(init.ethernetWrapper)
, selectedEthernetDeviceName(init.selectedDeviceName)
, parentDevice(FindParentDevice(parent))
{
initProperties();
initEncoders();
initStatusPacket();
startStatusLoop();
}
CaptureFb::~CaptureFb()
{
stopStatusLoop();
}
void CaptureFb::initProperties()
{
initDeviceInfoProperties(false);
deviceDescription = "DefaultDeviceDescription";
setPropertyValueInternal(String("DeviceDescription").asPtr<IString>(true), deviceDescription, false, false, false);
serialNumber = "DefaultSerialNumber";
setPropertyValueInternal(String("SerialNumber").asPtr<IString>(true), serialNumber, false, false, false);
hardwareVersion = "DefaultHardwareVersion";
setPropertyValueInternal(String("HardwareVersion").asPtr<IString>(true), hardwareVersion, false, false, false);
softwareVersion = "DefaultSoftwareVersion";
setPropertyValueInternal(String("SoftwareVersion").asPtr<IString>(true), softwareVersion, false, false, false);
}
void CaptureFb::propertyChanged()
{
asam_cmp_common_lib::CaptureCommonFb::propertyChanged();
initEncoders();
updateCaptureData();
}
void CaptureFb::updateCaptureData()
{
std::scoped_lock lock{statusSync};
captureStatusPacket.setDeviceId(deviceId);
static_cast<ASAM::CMP::CaptureModulePayload&>(captureStatusPacket.getPayload())
.setData(
deviceDescription.toView(),
serialNumber.toView(),
hardwareVersion.toView(),
softwareVersion.toView(),
vendorData
);
captureStatus.update(captureStatusPacket);
}
void CaptureFb::initEncoders()
{
encoders.init(deviceId);
}
void CaptureFb::initStatusPacket()
{
captureStatusPacket.setPayload(ASAM::CMP::CaptureModulePayload());
captureStatusPacket.getPayload().setMessageType(ASAM::CMP::CmpHeader::MessageType::status);
updateCaptureData();
}
void CaptureFb::addInterfaceInternal(){
std::scoped_lock lock{statusSync};
auto newId = interfaceIdManager.getFirstUnusedId();
InterfaceFbInit init{&encoders, captureStatus, statusSync, ethernetWrapper, allowJumboFrames, selectedEthernetDeviceName};
addInterfaceWithParams<InterfaceFb>(newId, init);
}
void CaptureFb::removeInterfaceInternal(size_t nInd)
{
std::scoped_lock lock{statusSync};
int id = functionBlocks.getItems().getItemAt(nInd).getPropertyValue("InterfaceId");
captureStatus.removeInterfaceById(id);
asam_cmp_common_lib::CaptureCommonFb::removeInterfaceInternal(nInd);
}
ASAM::CMP::DataContext CaptureFb::createEncoderDataContext() const
{
assert(!allowJumboFrames);
return {64, 1500};
}
uint64_t CaptureFb::getCurrentSystemTime()
{
if (!parentDevice.assigned())
return std::chrono::duration_cast<std::chrono::nanoseconds>(std::chrono::system_clock::now().time_since_epoch()).count();
DeviceDomainPtr domain = parentDevice.getDomain();
const auto domainResolution = domain.getTickResolution();
return static_cast<Int>(parentDevice.getTicksSinceOrigin()) * (SimplifiedRatioPtr(domainResolution) * NanoTicksPerSec);
}
DevicePtr CaptureFb::FindParentDevice(const ComponentPtr& parent)
{
auto parentDevice = parent;
while (parentDevice.assigned() && !parentDevice.asPtrOrNull<IDevice>().assigned())
parentDevice = parentDevice.getParent();
return parentDevice;
}
void CaptureFb::statusLoop()
{
auto encoderContext = createEncoderDataContext();
std::unique_lock<std::mutex> lock(statusSync);
while (!stopStatusSending)
{
cv.wait_for(lock, std::chrono::milliseconds(sendingSyncLoopTime));
if (!stopStatusSending)
{
auto time = getCurrentSystemTime();
auto encodeAndSend = [&](ASAM::CMP::Packet& packet, uint64_t time) {
packet.setTimestamp(time);
auto encodedData = encoders.encode(1, packet, encoderContext);
for (const auto& e : encodedData)
ethernetWrapper->sendPacket(e);
};
encodeAndSend(captureStatus.getPacket(), time);
for (SizeT i = 0; i < captureStatus.getInterfaceStatusCount(); ++i)
{
auto packet = captureStatus.getInterfaceStatus(i).getPacket();
encodeAndSend(packet, time);
}
}
}
}
void CaptureFb::startStatusLoop()
{
stopStatusSending = false;
statusThread = std::thread{&CaptureFb::statusLoop, this};
}
void CaptureFb::stopStatusLoop()
{
{
auto lock2 = getRecursiveConfigLock();
stopStatusSending = true;
}
cv.notify_one();
statusThread.join();
}
END_NAMESPACE_ASAM_CMP_CAPTURE_MODULE