Home
About
Blog
Products
Forum
Support
Contact
Sunbelt Computer Software
PL/B Language Development and Support
Home
About
Blog
Products
Forum
Support
Contact
node/lib/mapping.cpp at python-wrapper · VILLASframework/node · GitHub
Skip to content
Navigation Menu
Sign in
Appearance settings
Platform
AI CODE CREATION
GitHub Copilot
Write better code with AI
GitHub Copilot app
Direct agents from issue to merge
MCP Registry
Integrate external tools
DEVELOPER WORKFLOWS
Actions
Automate any workflow
Codespaces
Instant dev environments
Issues
Plan and track work
Code Review
Manage code changes
Code Quality
Enforce quality at merge
APPLICATION SECURITY
GitHub Advanced Security
Find and fix vulnerabilities
Code security
Secure your code as you build
Secret protection
Stop leaks before they start
EXPLORE
Why GitHub
Documentation
Blog
Changelog
Marketplace
View all features
Solutions
BY COMPANY SIZE
Enterprises
Small and medium teams
Startups
Nonprofits
BY USE CASE
App Modernization
DevSecOps
DevOps
CI/CD
View all use cases
BY INDUSTRY
Healthcare
Financial services
Manufacturing
Government
View all industries
View all solutions
Resources
EXPLORE BY TOPIC
AI
Software Development
DevOps
Security
View all topics
EXPLORE BY TYPE
Customer stories
Events & webinars
Ebooks & reports
Business insights
GitHub Skills
SUPPORT & SERVICES
Documentation
Customer support
Community forum
Trust center
Partners
View all resources
Open Source
COMMUNITY
GitHub Sponsors
Fund open source developers
PROGRAMS
Security Lab
Maintainer Community
GitHub Stars
Archive Program
REPOSITORIES
Topics
Trending
Collections
Enterprise
ENTERPRISE SOLUTIONS
Enterprise platform
AI-powered developer platform
AVAILABLE ADD-ONS
GitHub Advanced Security
Enterprise-grade security features
Copilot for Business
Enterprise-grade AI features
Premium Support
Enterprise-grade 24/7 support
Pricing
Search
/
Sign in
Sign up
Appearance settings
You signed in with another tab or window.
Reload
to refresh your session.
You signed out in another tab or window.
Reload
to refresh your session.
You switched accounts on another tab or window.
Reload
to refresh your session.
Dismiss alert
{{ message }}
Uh oh!
There was an error while loading.
Please reload this page
.
VILLASframework
/
node
Public
Notifications
You must be signed in to change notification settings
Fork
22
Star
21
Code
Issues
62
Pull requests
14
Discussions
Actions
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Discussions
Actions
Security and quality
Insights
Files
Expand file tree
python-wrapper
Breadcrumbs
node
/
lib
/
mapping.cpp
Copy path
Blame
More file actions
Blame
More file actions
Latest commit
History
History
History
336 lines (266 loc) · 7.64 KB
python-wrapper
Breadcrumbs
node
/
lib
/
mapping.cpp
Copy path
Top
File metadata and controls
Code
Blame
336 lines (266 loc) · 7.64 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
/*
Sample value remapping for mux.
*
* Author: Steffen Vogel <post@steffenvogel.de>
* SPDX-FileCopyrightText: 2014-2023 Institute for Automation of Complex Power Systems, RWTH Aachen University
* SPDX-License-Identifier: Apache-2.0
*/
#
include
<
iostream
>
#
include
<
regex
>
#
include
<
villas/exceptions.hpp
>
#
include
<
villas/list.hpp
>
#
include
<
villas/mapping.hpp
>
#
include
<
villas/node.hpp
>
#
include
<
villas/sample.hpp
>
#
include
<
villas/signal.hpp
>
#
include
<
villas/utils.hpp
>
using
namespace
villas
;
using
namespace
villas
::node
;
using
namespace
villas
::utils
;
int
MappingEntry::parseString
(
const
std::string &str) {
std::smatch mr;
std::regex
re
(
RE_MAPPING
);
if
(!
std::regex_match
(str, mr, re))
goto
invalid_format;
if
(mr[
1
].
matched
)
nodeName = mr.
str
(
1
);
if
(mr[
9
].
matched
)
nodeName = mr.
str
(
9
);
if
(mr[
6
].
matched
) {
data.
first
=
strdup
(mr.
str
(
6
).
c_str
());
data.
last
= mr[
7
].
matched
?
strdup
(mr.
str
(
7
).
c_str
()) :
nullptr
;
type = Type::
DATA
;
}
else
if
(mr[
10
].
matched
) {
data.
first
=
strdup
(mr.
str
(
10
).
c_str
());
data.
last
= mr[
11
].
matched
?
strdup
(mr.
str
(
11
).
c_str
()) :
nullptr
;
type = Type::
DATA
;
}
else
if
(mr[
8
].
matched
) {
data.
first
=
strdup
(mr.
str
(
8
).
c_str
());
data.
last
=
nullptr
;
type = Type::
DATA
;
}
else
if
(mr[
2
].
matched
) {
stats.
type
=
Stats::lookupType
(mr.
str
(
3
));
stats.
metric
=
Stats::lookupMetric
(mr.
str
(
2
));
type = Type::
STATS
;
}
else
if
(mr[
5
].
matched
) {
if
(mr.
str
(
5
) ==
"
origin
"
)
timestamp.
type
= TimestampType::
ORIGIN
;
else
if
(mr.
str
(
5
) ==
"
received
"
)
timestamp.
type
= TimestampType::
RECEIVED
;
else
goto
invalid_format;
type = Type::
TIMESTAMP
;
}
else
if
(mr[
4
].
matched
) {
if
(mr.
str
(
4
) ==
"
sequence
"
)
header.
type
= HeaderType::
SEQUENCE
;
else
if
(mr.
str
(
4
) ==
"
length
"
)
header.
type
= HeaderType::
LENGTH
;
else
goto
invalid_format;
type = Type::
HEADER
;
}
//
Only node name given.. We map all data
else
if
(!nodeName.
empty
()) {
data.
first
=
nullptr
;
data.
last
=
nullptr
;
type = Type::
DATA
;
}
return
0
;
invalid_format:
throw
RuntimeError
(
"
Failed to parse mapping expression: {}
"
, str);
}
MappingEntry::MappingEntry
()
//
cppcheck-suppress uninitMemberVar
: node(
nullptr
), type(Type::
UNKNOWN
), length(
0
), offset(
0
), nodeName() {}
int
MappingEntry::parse
(
json_t
*json) {
const
char
*str;
str =
json_string_value
(json);
if
(!str)
return
-
1
;
return
parseString
(str);
}
int
MappingEntry::update
(
struct
Sample
*remapped,
const
struct
Sample
*original)
const
{
unsigned
len = length;
if
(offset + len > remapped->
capacity
)
return
-
1
;
switch
(type) {
case
Type::
STATS
:
remapped->
data
[offset] =
node->
getStats
()->
getValue
(stats.
metric
, stats.
type
);
break
;
case
Type::
TIMESTAMP
: {
const
struct
timespec
*ts;
switch
(timestamp.
type
) {
case
TimestampType::
RECEIVED
:
ts = &original->
ts
.
received
;
break
;
case
TimestampType::
ORIGIN
:
ts = &original->
ts
.
origin
;
break
;
default
:
return
-
1
;
}
remapped->
data
[offset +
0
].
i
= ts->
tv_sec
;
remapped->
data
[offset +
1
].
i
= ts->
tv_nsec
;
break
;
}
case
Type::
HEADER
:
switch
(header.
type
) {
case
HeaderType::
LENGTH
:
remapped->
data
[offset].
i
= original->
length
;
break
;
case
HeaderType::
SEQUENCE
:
remapped->
data
[offset].
i
= original->
sequence
;
break
;
default
:
return
-
1
;
}
break
;
case
Type::
DATA
:
for
(
unsigned
j = data.
offset
, i = offset;
j <
MIN
(original->
length
, (
unsigned
)(data.
offset
+ length));
j++, i++) {
if
(j >= original->
length
)
remapped->
data
[i].
f
= -
1
;
else
remapped->
data
[i] = original->
data
[j];
}
len =
MIN
((
unsigned
)length, original->
length
- data.
offset
);
break
;
case
Type::
UNKNOWN
:
return
-
1
;
}
if
(offset + len > remapped->
length
)
remapped->
length
= offset + len;
return
0
;
}
int
MappingEntry::prepare
(NodeList &nodes) {
if
(!nodeName.
empty
() && node ==
nullptr
) {
node = nodes.
lookup
(nodeName);
if
(!node)
throw
RuntimeError
(
"
Invalid node name in mapping: {}
"
, nodeName);
}
if
(type == Type::
DATA
) {
int
first = -
1
, last = -
1
;
if
(data.
first
) {
if
(node)
first = node->
getInputSignals
()->
getIndexByName
(data.
first
);
if
(first <
0
) {
char
*endptr;
first =
strtoul
(data.
first
, &endptr,
10
);
if
(endptr != data.
first
+
strlen
(data.
first
))
throw
RuntimeError
(
"
Failed to parse data index in mapping: {}
"
,
data.
first
);
}
}
else
{
//
Map all signals
data.
offset
=
0
;
length = -
1
;
goto
end;
}
if
(data.
last
) {
if
(node)
last = node->
getInputSignals
()->
getIndexByName
(data.
last
);
if
(last <
0
) {
char
*endptr;
last =
strtoul
(data.
last
, &endptr,
10
);
if
(endptr != data.
last
+
strlen
(data.
last
))
throw
RuntimeError
(
"
Failed to parse data index in mapping: {}
"
,
data.
last
);
}
}
else
last = first;
//
single element: data[5] => data[5-5]
if
(last < first)
throw
RuntimeError
(
"
Invalid data range indices for mapping: {} < {}
"
,
last, first);
data.
offset
= first;
length = last - first +
1
;
}
else
{
length =
1
;
}
end:
if
(length <
0
)
length = node->
getInputSignals
()->
size
();
return
0
;
}
std::string
MappingEntry::toString
(
unsigned
index)
const
{
assert
(length ==
0
|| (
int
)index < length);
std::stringstream ss;
if
(node)
ss << node->
getNameShort
() <<
"
.
"
;
switch
(type) {
case
Type::
STATS
:
ss <<
"
stats.
"
;
ss << Stats::metrics[stats.
metric
].
name
<<
"
.
"
;
ss << Stats::types[stats.
type
].
name
;
break
;
case
Type::
HEADER
:
ss <<
"
hdr.
"
;
switch
(header.
type
) {
case
HeaderType::
LENGTH
:
ss <<
"
length
"
;
break
;
case
HeaderType::
SEQUENCE
:
ss <<
"
sequence
"
;
break
;
default
: {
}
}
break
;
case
Type::
TIMESTAMP
:
ss <<
"
ts.
"
;
switch
(timestamp.
type
) {
case
TimestampType::
ORIGIN
:
ss <<
"
origin.
"
;
break
;
case
TimestampType::
RECEIVED
:
ss <<
"
received.
"
;
break
;
default
: {
}
}
ss << (index ==
0
?
"
sec
"
:
"
nsec
"
);
break
;
case
Type::
DATA
:
if
(node && index < node->
getInputSignals
()->
size
()) {
auto
s = node->
getInputSignals
()->
getByIndex
(index);
ss <<
"
data[
"
<< s->
name
<<
"
]
"
;
}
else
ss <<
"
data[
"
<< index <<
"
]
"
;
break
;
case
Type::
UNKNOWN
:
return
"
<unknown>
"
;
}
return
ss.
str
();
}
Signal::Ptr
MappingEntry::toSignal
(
unsigned
index)
const
{
auto
name =
toString
(index);
switch
(type) {
case
MappingEntry::Type::
STATS
:
return
std::make_shared<Signal>(name, Stats::metrics[stats.
metric
].
unit
,
Stats::types[stats.
type
].
signal_type
);
case
MappingEntry::Type::
HEADER
:
switch
(header.
type
) {
case
MappingEntry::HeaderType::
LENGTH
:
case
MappingEntry::HeaderType::
SEQUENCE
:
return
std::make_shared<Signal>(name,
"
"
, SignalType::
INTEGER
);
}
break
;
case
MappingEntry::Type::
TIMESTAMP
:
switch
(index) {
case
0
:
return
std::make_shared<Signal>(name,
"
s
"
, SignalType::
INTEGER
);
case
1
:
return
std::make_shared<Signal>(name,
"
ns
"
, SignalType::
INTEGER
);
}
break
;
case
MappingEntry::Type::
DATA
: {
auto
sig = std::make_shared<Signal>(data.
signal
->
name
, data.
signal
->
unit
,
data.
signal
->
type
);
sig->
init
= data.
signal
->
init
;
return
sig;
}
case
MappingEntry::Type::
UNKNOWN
:
break
;
}
return
std::shared_ptr<Signal>();
}
You can’t perform that action at this time.