forked from openDAQ/ASAM-CMP-Modules
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinterface_common_fb.cpp
More file actions
211 lines (178 loc) · 6.6 KB
/
interface_common_fb.cpp
File metadata and controls
211 lines (178 loc) · 6.6 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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
#include <coreobjects/argument_info_factory.h>
#include <coreobjects/callable_info_factory.h>
#include <coretypes/listobject_factory.h>
#include <opendaq/component_type_private.h>
#include <asam_cmp_common_lib/interface_common_fb.h>
#include <asam_cmp_common_lib/stream_common_fb_impl.h>
#include <fmt/core.h>
BEGIN_NAMESPACE_ASAM_CMP_COMMON
using ASAM::CMP::PayloadType;
InterfaceCommonFb::InterfaceCommonFb(const ModuleInfoPtr& moduleInfo,
const ContextPtr& ctx,
const ComponentPtr& parent,
const StringPtr& localId,
const InterfaceCommonInit& init)
: FunctionBlock(CreateType(moduleInfo), ctx, parent, localId)
, interfaceIdManager(init.interfaceIdManager)
, interfaceId(init.id)
, payloadType(0)
, isUpdating(false)
, needsPropertyChanged(false)
, createdStreams(0)
{
initProperties();
}
FunctionBlockTypePtr InterfaceCommonFb::CreateType(const ModuleInfoPtr& moduleInfo)
{
auto fbType = FunctionBlockType("AsamCmpInterface", "AsamCmpInterface", "ASAM CMP Interface");
checkErrorInfo(fbType.asPtr<IComponentTypePrivate>(true)->setModuleInfo(moduleInfo));
return fbType;
}
void InterfaceCommonFb::initProperties()
{
StringPtr propName = "InterfaceId";
auto prop = IntPropertyBuilder(propName, interfaceId)
.setMinValue(static_cast<Int>(std::numeric_limits<uint32_t>::min()))
.setMaxValue(static_cast<Int>(std::numeric_limits<uint32_t>::max()))
.build();
objPtr.addProperty(prop);
objPtr.getOnPropertyValueWrite(propName) +=
[this](PropertyObjectPtr& obj, PropertyValueEventArgsPtr& args) { propertyChangedIfNotUpdating(); };
propName = "PayloadType";
ListPtr<StringPtr> payloadTypes{"Undefined", "CAN", "CAN FD", "Analog", "Ethernet"};
prop = SelectionPropertyBuilder(propName, payloadTypes, 0).build();
objPtr.addProperty(prop);
objPtr.getOnPropertyValueWrite(propName) +=
[this](PropertyObjectPtr& obj, PropertyValueEventArgsPtr& args) { propertyChangedIfNotUpdating(); };
propName = "AddStream";
prop = FunctionPropertyBuilder(propName, ProcedureInfo(List<IArgumentInfo>())).setReadOnly(true).build();
objPtr.addProperty(prop);
objPtr.asPtr<IPropertyObjectProtected>().setProtectedPropertyValue(propName, Procedure([this] { addStream(); }));
propName = "RemoveStream";
prop = FunctionPropertyBuilder(propName, ProcedureInfo(List<IArgumentInfo>(ArgumentInfo("nInd", ctInt)))).setReadOnly(true).build();
objPtr.addProperty(prop);
objPtr.asPtr<IPropertyObjectProtected>().setProtectedPropertyValue(propName, Procedure([this](IntPtr nInd) { removeStream(nInd); }));
}
void InterfaceCommonFb::addStream()
{
auto lock = this->getRecursiveConfigLock();
addStreamInternal();
}
void InterfaceCommonFb::removeStream(size_t nInd)
{
auto lock = this->getRecursiveConfigLock();
removeStreamInternal(nInd);
}
void InterfaceCommonFb::updateInterfaceIdInternal()
{
Int newId = objPtr.getPropertyValue("InterfaceId");
if (newId == interfaceId)
return;
interfaceIdManager->removeId(interfaceId);
interfaceId = newId;
interfaceIdManager->addId(interfaceId);
}
void InterfaceCommonFb::updatePayloadTypeInternal()
{
Int newType = objPtr.getPropertyValue("PayloadType");
if (newType < 0 || static_cast<size_t>(newType) > maxPayloadIndex())
{
objPtr.setPropertyValue("PayloadType", payloadTypeToIndex(payloadType.getType()));
}
else
{
payloadType.setType(indexToPayloadType(newType));
for (const auto& fb : functionBlocks.getItems())
{
fb.as<IStreamCommon>(true)->setPayloadType(payloadType);
}
}
}
void InterfaceCommonFb::removeStreamInternal(size_t nInd)
{
auto fb = functionBlocks.getItems().getItemAt(nInd);
uint8_t streamId = static_cast<Int>(fb.getPropertyValue("StreamId"));
functionBlocks.removeItem(fb);
streamIdManager.removeId(streamId);
}
daq::ErrCode INTERFACE_FUNC InterfaceCommonFb::beginUpdate()
{
daq::ErrCode result = FunctionBlock::beginUpdate();
if (result == OPENDAQ_SUCCESS)
isUpdating = true;
return result;
}
void InterfaceCommonFb::endApplyProperties(const UpdatingActions& propsAndValues, bool parentUpdating)
{
auto lock = this->getRecursiveConfigLock();
FunctionBlock::endApplyProperties(propsAndValues, parentUpdating);
if (needsPropertyChanged)
{
propertyChanged();
needsPropertyChanged = false;
}
isUpdating = false;
}
void InterfaceCommonFb::propertyChanged()
{
updateInterfaceIdInternal();
updatePayloadTypeInternal();
}
void InterfaceCommonFb::propertyChangedIfNotUpdating()
{
if (!isUpdating)
{
auto lock = this->getRecursiveConfigLock();
propertyChanged();
}
else
needsPropertyChanged = true;
}
DictPtr<IString, IFunctionBlockType> InterfaceCommonFb::onGetAvailableFunctionBlockTypes()
{
auto streamType = StreamCommonFb::CreateType(this->type.getModuleInfo());
return Dict<IString, IFunctionBlockType>({ {streamType.getId(), streamType} });
}
FunctionBlockPtr InterfaceCommonFb::onAddFunctionBlock(const StringPtr& typeId, const PropertyObjectPtr& config)
{
if (typeId == StreamCommonFb::CreateType(this->type.getModuleInfo()).getId())
{
addStream();
auto streamFb = this->functionBlocks.getItems().getItemAt(this->functionBlocks.getItems().getCount() - 1);
return streamFb;
}
throw NotFoundException("Function block not found");
}
const static std::unordered_map<uint32_t, Int> payloadTypeToIndexMap = {
{PayloadType::invalid, 0}, {PayloadType::can, 1}, {PayloadType::canFd, 2}, {PayloadType::analog, 3}, {PayloadType::ethernet, 4}};
const std::vector<uint32_t> indexToPayloadTypeMap = []() {
std::vector<uint32_t> vec;
vec.resize(payloadTypeToIndexMap.size());
for (const auto& [key, value] : payloadTypeToIndexMap)
{
vec[value] = key;
}
return vec;
}();
uint32_t InterfaceCommonFb::indexToPayloadType(Int index) const
{
if (static_cast<size_t>(index) < indexToPayloadTypeMap.size())
{
return indexToPayloadTypeMap[index];
}
return PayloadType::invalid;
}
Int InterfaceCommonFb::payloadTypeToIndex(PayloadType type) const
{
auto it = payloadTypeToIndexMap.find(type.getType());
if (it != payloadTypeToIndexMap.end())
{
return it->second;
}
return 0;
}
size_t InterfaceCommonFb::maxPayloadIndex() const
{
return payloadTypeToIndexMap.size() - 1;
}
END_NAMESPACE_ASAM_CMP_COMMON