Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
B
Base DAQ
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Package Registry
Container Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
BIAS
DAQs
Base DAQ
Compare revisions
ce99aaa20706cca85200a375cee63d7414cebb74 to f8149d2831d10e7f467d96ee11335f01bfad81b5
Compare revisions
Changes are shown as if the
source
revision was being merged into the
target
revision.
Learn more about comparing revisions.
Source
bias/daqs/base-daq
Select target project
No results found
f8149d2831d10e7f467d96ee11335f01bfad81b5
Select Git revision
Branches
main
Tags
v1.0
Swap
Target
bias/daqs/base-daq
Select target project
bias/daqs/base-daq
1 result
ce99aaa20706cca85200a375cee63d7414cebb74
Select Git revision
Branches
main
Tags
v1.0
Show changes
Only incoming changes from source
Include changes to target since source was created
Compare
Commits on Source (1)
update for log
· f8149d28
Valerio Pastore
authored
1 year ago
f8149d28
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
include/Base_Packet.h
+80
-56
80 additions, 56 deletions
include/Base_Packet.h
with
80 additions
and
56 deletions
include/Base_Packet.h
View file @
f8149d28
...
...
@@ -16,6 +16,8 @@
#include
<cmath>
#include
<functional>
#include
<optional>
#include
<ctime>
#include
<iomanip>
namespace
inaf
::
oasbo
::
PacketLib
{
...
...
@@ -32,23 +34,20 @@ protected:
std
::
unordered_map
<
uint
,
std
::
string
>
indexToFieldNameMap
;
std
::
function
<
Structure
(
std
::
string
)
>
sourceReadingFunc
;
void
updateFieldSizes
(
const
Structure
&
paramsTuple
)
{
void
updateFieldSizes
(
const
Structure
&
paramsTuple
)
{
std
::
for_each
(
paramsTuple
.
begin
(),
paramsTuple
.
end
(),
[
&
](
const
std
::
tuple
<
uint
,
std
::
string
,
uint
>
&
tup
)
{
this
->
fieldSizes
.
push_back
(
std
::
get
<
2
>
(
tup
));
});
}
void
updateFieldOffsets
(
const
Structure
&
paramsTuple
)
{
void
updateFieldOffsets
(
const
Structure
&
paramsTuple
)
{
uint
offset
=
0
;
for
(
size_t
i
=
0
;
i
<
paramsTuple
.
size
();
i
++
)
{
indexToOffsetsMap
[
i
]
=
offset
;
offset
+=
std
::
get
<
2
>
(
paramsTuple
[
i
]);
}
}
void
updateFieldNameAndIndexMap
(
const
Structure
&
paramsTuple
)
{
void
updateFieldNameAndIndexMap
(
const
Structure
&
paramsTuple
)
{
std
::
for_each
(
paramsTuple
.
begin
(),
paramsTuple
.
end
(),
[
&
](
const
std
::
tuple
<
uint
,
std
::
string
,
uint
>
&
tup
)
{
this
->
fieldNameToIndexMap
[
std
::
get
<
1
>
(
tup
)]
=
std
::
get
<
0
>
(
...
...
@@ -63,24 +62,27 @@ protected:
updateFieldSizes
(
structure
);
updateFieldOffsets
(
structure
);
updateFieldNameAndIndexMap
(
structure
);
uint
bitSize
=
std
::
accumulate
(
fieldSizes
.
begin
(),
fieldSizes
.
end
(),
0
);
this
->
byteSize
=
bitSize
%
8
==
0
?
bitSize
/
8
:
bitSize
/
8
+
1
;
uint
bitSize
=
std
::
accumulate
(
fieldSizes
.
begin
(),
fieldSizes
.
end
(),
0
);
this
->
byteSize
=
bitSize
%
8
==
0
?
bitSize
/
8
:
bitSize
/
8
+
1
;
}
public
:
virtual
~
BasePacketStructure
()
=
default
;
BasePacketStructure
(
std
::
string
source
,
std
::
function
<
Structure
(
std
::
string
)
>
sourceReadingFunc
)
:
source
(
source
),
sourceReadingFunc
(
sourceReadingFunc
){
BasePacketStructure
(
std
::
string
source
,
std
::
function
<
Structure
(
std
::
string
)
>
sourceReadingFunc
)
:
source
(
source
),
sourceReadingFunc
(
sourceReadingFunc
)
{
this
->
updateStructure
(
source
);
};
BasePacketStructure
(
const
BasePacketStructure
&
other
){
this
->
source
=
other
.
source
;
this
->
byteSize
=
other
.
byteSize
;
this
->
fieldSizes
=
other
.
fieldSizes
;
this
->
indexToOffsetsMap
=
other
.
indexToOffsetsMap
;
this
->
fieldNameToIndexMap
=
other
.
fieldNameToIndexMap
;
this
->
indexToFieldNameMap
=
other
.
indexToFieldNameMap
;
this
->
sourceReadingFunc
=
other
.
sourceReadingFunc
;
}
;
BasePacketStructure
(
const
BasePacketStructure
&
other
)
{
this
->
source
=
other
.
source
;
this
->
byteSize
=
other
.
byteSize
;
this
->
fieldSizes
=
other
.
fieldSizes
;
this
->
indexToOffsetsMap
=
other
.
indexToOffsetsMap
;
this
->
fieldNameToIndexMap
=
other
.
fieldNameToIndexMap
;
this
->
indexToFieldNameMap
=
other
.
indexToFieldNameMap
;
this
->
sourceReadingFunc
=
other
.
sourceReadingFunc
;
}
void
changeSource
(
std
::
string
source
)
{
...
...
@@ -96,8 +98,11 @@ public:
if
(
index
<=
this
->
numberOfFields
())
{
return
indexToOffsetsMap
.
at
(
index
);
}
else
{
std
::
cerr
<<
"No field at "
<<
index
<<
", max is "
<<
numberOfFields
()
<<
", returning nullopt."
<<
std
::
endl
;
time_t
now
=
time
(
nullptr
)
;
std
::
cerr
<<
"["
<<
std
::
put_time
(
localtime
(
&
now
),
"%Y-%m-%d %H:%M:%S"
)
<<
"]
\t
[Base Packet]
\t
"
<<
"No field at "
<<
index
<<
", max is "
<<
numberOfFields
()
<<
", returning nullopt."
<<
std
::
endl
;
return
std
::
nullopt
;
}
}
...
...
@@ -105,8 +110,11 @@ public:
if
(
index
<=
this
->
numberOfFields
())
{
return
this
->
fieldSizes
[
index
];
}
else
{
std
::
cerr
<<
"No field at "
<<
index
<<
", max is "
<<
numberOfFields
()
<<
", returning nullopt."
<<
std
::
endl
;
time_t
now
=
time
(
nullptr
)
;
std
::
cerr
<<
"["
<<
std
::
put_time
(
localtime
(
&
now
),
"%Y-%m-%d %H:%M:%S"
)
<<
"]
\t
[Base Packet]
\t
"
<<
"No field at "
<<
index
<<
", max is "
<<
numberOfFields
()
<<
", returning nullopt."
<<
std
::
endl
;
return
std
::
nullopt
;
}
}
...
...
@@ -119,7 +127,9 @@ public:
try
{
return
this
->
fieldNameToIndexMap
.
at
(
fieldName
);
}
catch
(
const
std
::
out_of_range
&
oor
)
{
std
::
cerr
<<
"No field of name "
<<
fieldName
time_t
now
=
time
(
nullptr
)
;
std
::
cerr
<<
"["
<<
std
::
put_time
(
localtime
(
&
now
),
"%Y-%m-%d %H:%M:%S"
)
<<
"]
\t
[Base Packet]
\t
"
<<
"No field of name "
<<
fieldName
<<
", returning nullopt."
<<
std
::
endl
;
return
std
::
nullopt
;
}
...
...
@@ -129,8 +139,11 @@ public:
try
{
return
this
->
indexToFieldNameMap
.
at
(
index
);
}
catch
(
const
std
::
out_of_range
&
oor
)
{
std
::
cerr
<<
"No field at "
<<
index
<<
", max is "
<<
numberOfFields
()
<<
", returning nullopt."
<<
std
::
endl
;
time_t
now
=
time
(
nullptr
);
std
::
cerr
<<
"["
<<
std
::
put_time
(
localtime
(
&
now
),
"%Y-%m-%d %H:%M:%S"
)
<<
"]
\t
[Base Packet]
\t
"
<<
"No field at "
<<
index
<<
", max is "
<<
numberOfFields
()
<<
", returning nullopt."
<<
std
::
endl
;
return
std
::
nullopt
;
}
}
...
...
@@ -146,11 +159,11 @@ public:
template
<
typename
ValueType
>
class
bit_iterator
{
using
iterator_category
=
std
::
random_access_iterator_tag
;
using
value_type
=
ValueType
;
using
difference_type
=
std
::
ptrdiff_t
;
using
pointer
=
ValueType
*
;
using
reference
=
ValueType
&
;
using
iterator_category
=
std
::
random_access_iterator_tag
;
using
value_type
=
ValueType
;
using
difference_type
=
std
::
ptrdiff_t
;
using
pointer
=
ValueType
*
;
using
reference
=
ValueType
&
;
public:
bit_iterator
(
const
uint8_t
*
data
,
int
offset
,
...
...
@@ -280,12 +293,12 @@ public:
this
->
binaryPointer
=
new
uint8_t
[
structure
.
getByteSize
()];
}
BasePacketTempl
(
const
BasePacketTempl
&
other
)
{
this
->
structure
=
new
BasePacketStructure
(
other
.
getPacketStructure
());
this
->
binaryPointer
=
new
uint8_t
[
other
.
structure
->
getByteSize
()];
std
::
memcpy
(
this
->
binaryPointer
,
other
.
binaryPointer
,
other
.
structure
->
getByteSize
());
}
BasePacketTempl
(
const
BasePacketTempl
&
other
)
{
this
->
structure
=
new
BasePacketStructure
(
other
.
getPacketStructure
());
this
->
binaryPointer
=
new
uint8_t
[
other
.
structure
->
getByteSize
()];
std
::
memcpy
(
this
->
binaryPointer
,
other
.
binaryPointer
,
other
.
structure
->
getByteSize
());
}
virtual
~
BasePacketTempl
()
=
default
;
...
...
@@ -293,9 +306,9 @@ public:
size_t
newSize
=
structure
.
getByteSize
();
size_t
oldSize
=
this
->
structure
->
getByteSize
();
uint8_t
*
buff
=
new
uint8_t
[
newSize
];
uint8_t
*
buff
=
new
uint8_t
[
newSize
];
std
::
memset
(
buff
,
0
,
newSize
);
std
::
memcpy
(
buff
,
binaryPointer
,
std
::
min
(
newSize
,
oldSize
));
std
::
memcpy
(
buff
,
binaryPointer
,
std
::
min
(
newSize
,
oldSize
));
delete
this
->
binaryPointer
;
this
->
binaryPointer
=
new
uint8_t
[
newSize
];
...
...
@@ -304,8 +317,6 @@ public:
this
->
structure
=
&
structure
;
}
std
::
optional
<
ValueType
>
readValueFromBinaryAt
(
uint
index
)
const
{
auto
offset
=
structure
->
bitOffsetOf
(
index
);
// offset from the beginning of the byte
auto
num_bits
=
structure
->
bitSizeOf
(
index
);
//remaining bits to read
...
...
@@ -313,9 +324,11 @@ public:
return
_readValueFromBinaryAt_
(
binaryPointer
,
offset
.
value
(),
num_bits
.
value
());
else
{
std
::
cerr
<<
"Error: No field at "
<<
index
<<
", max is "
<<
structure
->
numberOfFields
()
<<
", returning nullopt"
<<
std
::
endl
;
time_t
now
=
time
(
nullptr
)
;
std
::
cerr
<<
"["
<<
std
::
put_time
(
localtime
(
&
now
),
"%Y-%m-%d %H:%M:%S"
)
<<
"]
\t
[Base Packet]
\t
"
<<
"Error: No field at "
<<
index
<<
", max is "
<<
structure
->
numberOfFields
()
<<
", returning nullopt"
<<
std
::
endl
;
return
std
::
nullopt
;
}
}
...
...
@@ -327,11 +340,14 @@ public:
int
copyToBinaryPointer
(
const
uint8_t
*
from
,
uint
size
)
{
uint
max_writable
=
this
->
structure
->
getByteSize
();
if
(
size
>
max_writable
)
{
std
::
cerr
<<
"Error: you are trying to copy "
<<
size
<<
" byte where the max size is: "
<<
max_writable
<<
std
::
endl
;
std
::
cerr
<<
"
\t
I copy only until
"
<<
max_writable
<<
" byte"
time_t
now
=
time
(
nullptr
)
;
std
::
cerr
<<
"["
<<
std
::
put_time
(
localtime
(
&
now
),
"%Y-%m-%d %H:%M:%S"
)
<<
"]
\t
[Base Packet]
\t
"
<<
"Error: you are trying to copy "
<<
size
<<
" byte where the max size is:
"
<<
max_writable
<<
std
::
endl
;
std
::
cerr
<<
"["
<<
std
::
put_time
(
localtime
(
&
now
),
"%Y-%m-%d %H:%M:%S"
)
<<
"]
\t
[Base Packet]
\t
"
<<
"
\t
I copy only until "
<<
max_writable
<<
" byte"
<<
std
::
endl
;
std
::
memcpy
(
binaryPointer
,
from
,
max_writable
);
return
max_writable
;
}
else
{
...
...
@@ -343,17 +359,23 @@ public:
int
copyToBinaryPointer
(
const
uint8_t
*
from
,
uint
size
,
uint
offset
)
{
uint
max_writable
=
this
->
structure
->
getByteSize
();
if
(
offset
>
max_writable
)
{
std
::
cerr
<<
"Error: you are trying to copy starting from "
<<
offset
<<
" byte where the max size is: "
<<
max_writable
time_t
now
=
time
(
nullptr
)
;
std
::
cerr
<<
"["
<<
std
::
put_time
(
localtime
(
&
now
),
"%Y-%m-%d %H:%M:%S"
)
<<
"]
\t
[Base Packet]
\t
"
<<
"Error: you are trying to copy starting from "
<<
offset
<<
" byte where the max size is: "
<<
max_writable
<<
std
::
endl
;
return
-
1
;
}
if
(
size
+
offset
>
max_writable
)
{
std
::
cerr
<<
"Error: you are trying to copy "
<<
size
+
offset
<<
" byte where the max size is: "
<<
max_writable
<<
std
::
endl
;
std
::
cerr
<<
"
\t
I copy only until "
<<
max_writable
<<
" byte"
<<
std
::
endl
;
time_t
now
=
time
(
nullptr
)
;
std
::
cerr
<<
"["
<<
std
::
put_time
(
localtime
(
&
now
),
"%Y-%m-%d %H:%M:%S"
)
<<
"]
\t
[Base Packet]
\t
"
<<
"Error: you are trying to copy "
<<
size
+
offset
<<
" byte where the max size is: "
<<
max_writable
<<
std
::
endl
;
std
::
cerr
<<
"["
<<
std
::
put_time
(
localtime
(
&
now
),
"%Y-%m-%d %H:%M:%S"
)
<<
"]
\t
[Base Packet]
\t
"
<<
"
\t
I copy only until "
<<
max_writable
<<
" byte"
<<
std
::
endl
;
int
to_write
=
max_writable
-
offset
;
std
::
memcpy
(
&
binaryPointer
[
offset
],
from
,
to_write
);
return
to_write
;
...
...
@@ -397,8 +419,10 @@ public:
if
(
!
offset
.
has_value
()
||
!
numbits
.
has_value
())
return
std
::
nullopt
;
if
(
numbits
<
min_req
)
{
std
::
cerr
<<
"Error: you are trying to write "
<<
value
<<
" which requires at least "
<<
min_req
time_t
now
=
time
(
nullptr
)
;
std
::
cerr
<<
"["
<<
std
::
put_time
(
localtime
(
&
now
),
"%Y-%m-%d %H:%M:%S"
)
<<
"]
\t
[Base Packet]
\t
"
<<
"Error: you are trying to write "
<<
value
<<
" which requires at least "
<<
min_req
<<
" bits in a field of size "
<<
numbits
<<
std
::
endl
;
return
std
::
nullopt
;
}
...
...
This diff is collapsed.
Click to expand it.