Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in
Toggle navigation
Menu
Open sidebar
Jan Koniarik
emlabcpp
Commits
97242970
Commit
97242970
authored
Oct 17, 2021
by
Jan Koniarik
Browse files
protocol error record now contains pointer to err message
parent
a37f6358
Changes
4
Hide whitespace changes
Inline
Side-by-side
include/emlabcpp/protocol/def.h
View file @
97242970
...
...
@@ -69,10 +69,10 @@ struct protocol_def< D, Endianess >
}
static
constexpr
auto
deserialize
(
const
view
<
const
uint8_t
*
>&
buffer
)
->
either
<
protocol_result
<
size_type
,
value_type
>
,
protocol_error_record
>
->
either
<
protocol_result
<
size_type
,
value_type
>
,
protocol_error_record
>
{
if
(
buffer
.
size
()
<
max_size
)
{
return
protocol_error_record
{
PROTOCOL_NS
,
LOWSIZE_ERR
,
0
};
return
protocol_error_record
{
&
LOWSIZE_ERR
,
0
};
}
value_type
res
{};
...
...
@@ -136,7 +136,7 @@ struct protocol_def< std::array< D, N >, Endianess >
[
&
](
protocol_error_record
rec
)
{
rec
.
byte_index
+=
static_cast
<
uint16_t
>
(
iter
-
buffer
.
begin
()
);
opt_err
=
rec
;
opt_err
.
emplace
(
rec
)
;
}
);
if
(
opt_err
)
{
...
...
@@ -205,7 +205,7 @@ struct protocol_def< std::tuple< Ds... >, Endianess >
[
&
](
protocol_error_record
rec
)
{
rec
.
byte_index
+=
static_cast
<
uint16_t
>
(
iter
-
buffer
.
begin
()
);
opt_err
=
rec
;
opt_err
.
emplace
(
rec
)
;
}
);
return
opt_err
.
has_value
();
...
...
@@ -281,7 +281,7 @@ struct protocol_def< std::variant< Ds... >, Endianess >
id_type
id
=
sub_res
.
val
;
bounded
used
=
sub_res
.
used
;
result_either
res
=
protocol_error_record
{
PROTOCOL_NS
,
UNDEFVAR_ERR
,
0
};
result_either
res
=
protocol_error_record
{
&
UNDEFVAR_ERR
,
0
};
view
item_view
{
buffer
.
begin
()
+
*
used
,
buffer
.
end
()
};
...
...
@@ -342,7 +342,7 @@ struct protocol_def< std::bitset< N >, Endianess >
->
either
<
protocol_result
<
size_type
,
value_type
>
,
protocol_error_record
>
{
if
(
buffer
.
size
()
<
max_size
)
{
return
protocol_error_record
{
PROTOCOL_NS
,
LOWSIZE_ERR
,
0
};
return
protocol_error_record
{
&
LOWSIZE_ERR
,
0
};
}
std
::
bitset
<
N
>
res
;
for
(
std
::
size_t
i
:
range
(
max_size
)
)
{
...
...
@@ -381,7 +381,7 @@ struct protocol_def< protocol_sizeless_message< N >, Endianess >
auto
opt_msg
=
value_type
::
make
(
buffer
);
if
(
!
opt_msg
)
{
return
protocol_error_record
{
PROTOCOL_NS
,
BIGSIZE_ERR
,
0
};
return
protocol_error_record
{
&
BIGSIZE_ERR
,
0
};
}
auto
opt_bused
=
size_type
::
make
(
opt_msg
->
size
()
);
...
...
@@ -468,7 +468,7 @@ struct protocol_def< bounded< D, Min, Max >, Endianess >
[
&
](
auto
sub_res
)
->
result_either
{
auto
opt_val
=
value_type
::
make
(
sub_res
.
val
);
if
(
!
opt_val
)
{
return
protocol_error_record
{
PROTOCOL_NS
,
BOUNDS_ERR
,
0
};
return
protocol_error_record
{
&
BOUNDS_ERR
,
0
};
}
return
protocol_result
{
sub_res
.
used
,
*
opt_val
};
}
);
...
...
@@ -521,7 +521,7 @@ struct protocol_def< protocol_sized_buffer< CounterDef, D >, Endianess >
auto
start_iter
=
buffer
.
begin
()
+
counter_def
::
max_size
;
if
(
std
::
distance
(
start_iter
,
buffer
.
end
()
)
<
used
)
{
return
protocol_error_record
{
PROTOCOL_NS
,
LOWSIZE_ERR
,
0
};
return
protocol_error_record
{
&
LOWSIZE_ERR
,
0
};
}
return
sub_def
::
deserialize
(
view_n
(
start_iter
,
used
)
)
...
...
@@ -563,7 +563,7 @@ struct protocol_def< tag< V >, Endianess >
return
sub_def
::
deserialize
(
buffer
).
bind_left
(
[
&
](
auto
sub_res
)
->
return_either
{
if
(
sub_res
.
val
!=
V
)
{
return
protocol_error_record
{
PROTOCOL_NS
,
BADVAL_ERR
,
0
};
return
protocol_error_record
{
&
BADVAL_ERR
,
0
};
}
return
protocol_result
{
sub_res
.
used
,
tag
<
V
>
{}
};
...
...
@@ -632,7 +632,7 @@ struct protocol_def< protocol_group< Ds... >, Endianess >
return
*
opt_res
;
}
return
protocol_error_record
{
PROTOCOL_NS
,
GROUP_ERR
,
0
};
return
protocol_error_record
{
&
GROUP_ERR
,
0
};
}
};
...
...
@@ -665,7 +665,7 @@ struct protocol_def< protocol_mark, Endianess >
->
either
<
protocol_result
<
size_type
,
value_type
>
,
protocol_error_record
>
{
if
(
buffer
.
size
()
<
max_size
)
{
return
protocol_error_record
{
PROTOCOL_NS
,
LOWSIZE_ERR
,
0
};
return
protocol_error_record
{
&
LOWSIZE_ERR
,
0
};
}
value_type
res
{};
std
::
copy
(
buffer
.
begin
(),
buffer
.
begin
()
+
max_size
,
res
.
begin
()
);
...
...
@@ -688,8 +688,7 @@ struct protocol_def< protocol_error_record, Endianess >
static
constexpr
size_type
serialize_at
(
std
::
span
<
uint8_t
,
max_size
>
buffer
,
value_type
item
)
{
mark_def
::
serialize_at
(
buffer
.
subspan
<
0
,
8
>
(),
item
.
ns
);
mark_def
::
serialize_at
(
buffer
.
subspan
<
8
,
8
>
(),
item
.
err
);
mark_def
::
serialize_at
(
buffer
.
subspan
<
0
,
16
>
(),
*
item
.
err
);
bi_def
::
serialize_at
(
buffer
.
subspan
<
16
,
2
>
(),
item
.
byte_index
);
return
size_type
{};
...
...
@@ -698,23 +697,18 @@ struct protocol_def< protocol_error_record, Endianess >
static
constexpr
auto
deserialize
(
const
view
<
const
uint8_t
*
>&
buffer
)
->
either
<
protocol_result
<
size_type
,
value_type
>
,
protocol_error_record
>
{
if
(
buffer
.
size
()
<
max_size
)
{
return
protocol_error_record
{
PROTOCOL_NS
,
LOWSIZE_ERR
,
0
};
}
protocol_error_record
res
;
return
protocol_error_record
{
&
LOWSIZE_ERR
,
0
};
// TODO: fix this
/*protocol_error_record res;
return mark_def::deserialize( view_n( buffer.begin(), 8 ) )
.
bind_left
(
[
&
](
auto
ns_res
)
{
res
.
ns
=
ns_res
.
val
;
return
mark_def
::
deserialize
(
view_n
(
buffer
.
begin
()
+
8
,
8
)
);
}
)
.bind_left( [&]( auto err_res ) {
res.err = err_res.val;
return
bi
_def
::
deserialize
(
view_n
(
buffer
.
begin
()
+
16
,
2
)
);
return
mark
_def::deserialize( view_n( buffer.begin() + 16,
8
) );
} )
.convert_left( [&]( auto bi_res ) {
res.byte_index = bi_res.val;
return protocol_result{ size_type{}, res };
}
);
} );
*/
}
};
...
...
include/emlabcpp/protocol/error.h
View file @
97242970
...
...
@@ -11,23 +11,25 @@ namespace emlabcpp
// unique in that namespace, part of the error is also index of byte that caused the problem. These
// can be easily sent with the protocol lib itself.
struct
protocol_mark
:
std
::
array
<
char
,
8
>
struct
protocol_mark
:
std
::
array
<
char
,
16
>
{
using
base_type
=
std
::
array
<
char
,
8
>
;
using
base_type
=
std
::
array
<
char
,
16
>
;
};
struct
protocol_error_record
{
protocol_mark
ns
;
protocol_mark
err
;
uint16_t
byte_index
;
const
protocol_mark
*
err
;
uint16_t
byte_index
;
friend
constexpr
bool
operator
==
(
const
protocol_error_record
&
,
const
protocol_error_record
&
)
=
default
;
operator
==
(
const
protocol_error_record
&
lh
,
const
protocol_error_record
&
rh
)
{
return
lh
.
byte_index
==
rh
.
byte_index
&&
lh
.
err
==
rh
.
err
;
}
};
// Creates protocol_mark from simple string literal.
inline
constexpr
protocol_mark
make_protocol_mark
(
const
char
(
&
msg
)[
9
]
)
inline
constexpr
protocol_mark
make_protocol_mark
(
const
char
(
&
msg
)[
17
]
)
{
// note: do not try to fix the argument type, this is correct approach.
protocol_mark
res
;
...
...
@@ -35,20 +37,17 @@ inline constexpr protocol_mark make_protocol_mark( const char ( &msg )[9] )
return
res
;
}
// error namespace for protocol used by the protocol library by itself.
static
constexpr
auto
PROTOCOL_NS
=
make_protocol_mark
(
"EMLABPRO"
);
// not enough bytes left in the message for the item
static
constexpr
auto
LOWSIZE_ERR
=
make_protocol_mark
(
"LOWSIZE "
);
static
constexpr
auto
LOWSIZE_ERR
=
make_protocol_mark
(
"
EMLABCPP
LOWSIZE "
);
// too much bytes left in the message for the item
static
constexpr
auto
BIGSIZE_ERR
=
make_protocol_mark
(
"BIGSIZE "
);
static
constexpr
auto
BIGSIZE_ERR
=
make_protocol_mark
(
"
EMLABCPP
BIGSIZE "
);
// value in the message is outside of the range of bounded type
static
constexpr
auto
BOUNDS_ERR
=
make_protocol_mark
(
"BOUNDS "
);
static
constexpr
auto
BOUNDS_ERR
=
make_protocol_mark
(
"
EMLABCPP
BOUNDS "
);
// variant id is outside of the range for defined variant
static
constexpr
auto
UNDEFVAR_ERR
=
make_protocol_mark
(
"UNDEFVAR"
);
static
constexpr
auto
UNDEFVAR_ERR
=
make_protocol_mark
(
"
EMLABCPP
UNDEFVAR"
);
// parsed value is not correct, such as constant
static
constexpr
auto
BADVAL_ERR
=
make_protocol_mark
(
"BADVAL "
);
static
constexpr
auto
BADVAL_ERR
=
make_protocol_mark
(
"
EMLABCPP
BADVAL "
);
// no item of group matched the content of message
static
constexpr
auto
GROUP_ERR
=
make_protocol_mark
(
"GRPMATCH"
);
static
constexpr
auto
GROUP_ERR
=
make_protocol_mark
(
"
EMLABCPP
GRPMATCH"
);
}
// namespace emlabcpp
include/emlabcpp/protocol/streams.h
View file @
97242970
...
...
@@ -36,7 +36,7 @@ inline std::ostream& operator<<( std::ostream& os, const protocol_mark& m )
inline
std
::
ostream
&
operator
<<
(
std
::
ostream
&
os
,
const
protocol_error_record
&
rec
)
{
return
os
<<
rec
.
ns
<<
"::"
<<
rec
.
err
<<
" ("
<<
rec
.
byte_index
<<
")"
;
return
os
<<
rec
.
err
<<
" ("
<<
rec
.
byte_index
<<
")"
;
}
inline
std
::
ostream
&
operator
<<
(
std
::
ostream
&
os
,
const
protocol_endianess_enum
&
val
)
...
...
tests/protocol_def_test.cpp
View file @
97242970
...
...
@@ -138,30 +138,29 @@ int main( int argc, char** argv )
make_valid_test_case
<
PROTOCOL_LITTLE_ENDIAN
>
(
uint16_t
{
666
},
{
154
,
2
}
),
make_valid_test_case
<
PROTOCOL_BIG_ENDIAN
>
(
uint16_t
{
666
},
{
2
,
154
}
),
make_valid_test_case
<
PROTOCOL_LITTLE_ENDIAN
>
(
int32_t
{
-
1
},
{
255
,
255
,
255
,
255
}
),
make_invalid_test_case
<
int16_t
>
(
{
255
},
protocol_error_record
{
PROTOCOL_NS
,
LOWSIZE_ERR
,
0
}
),
make_invalid_test_case
<
int16_t
>
(
{
255
},
protocol_error_record
{
&
LOWSIZE_ERR
,
0
}
),
make_invalid_test_case
<
uint32_t
>
(
{
255
,
255
,
255
},
protocol_error_record
{
PROTOCOL_NS
,
LOWSIZE_ERR
,
0
}
),
{
255
,
255
,
255
},
protocol_error_record
{
&
LOWSIZE_ERR
,
0
}
),
// std::array
make_valid_test_case
<
PROTOCOL_LITTLE_ENDIAN
>
(
std
::
array
<
int16_t
,
3
>
{
-
1
,
1
,
666
},
{
255
,
255
,
1
,
0
,
154
,
2
}
),
make_valid_test_case
<
PROTOCOL_BIG_ENDIAN
>
(
std
::
array
<
int16_t
,
3
>
{
-
1
,
1
,
666
},
{
255
,
255
,
0
,
1
,
2
,
154
}
),
make_invalid_test_case
<
std
::
array
<
uint32_t
,
2
>
>
(
{
12
,
12
,
12
},
protocol_error_record
{
PROTOCOL_NS
,
LOWSIZE_ERR
,
0
}
),
{
12
,
12
,
12
},
protocol_error_record
{
&
LOWSIZE_ERR
,
0
}
),
make_invalid_test_case
<
std
::
array
<
uint32_t
,
2
>
>
(
{
12
,
12
,
12
,
12
},
protocol_error_record
{
PROTOCOL_NS
,
LOWSIZE_ERR
,
4
}
),
{
12
,
12
,
12
,
12
},
protocol_error_record
{
&
LOWSIZE_ERR
,
4
}
),
make_invalid_test_case
<
std
::
array
<
uint32_t
,
2
>
>
(
{
12
,
12
,
12
,
12
,
12
,
12
},
protocol_error_record
{
PROTOCOL_NS
,
LOWSIZE_ERR
,
4
}
),
{
12
,
12
,
12
,
12
,
12
,
12
},
protocol_error_record
{
&
LOWSIZE_ERR
,
4
}
),
// std::tuple
make_valid_test_case
<
PROTOCOL_LITTLE_ENDIAN
>
(
std
::
tuple
<
uint8_t
,
int16_t
,
int8_t
>
{
1u
,
666u
,
-
3u
},
{
1
,
154
,
2
,
253
}
),
make_valid_test_case
<
PROTOCOL_BIG_ENDIAN
>
(
std
::
tuple
<
uint8_t
,
int16_t
,
int8_t
>
{
1u
,
666u
,
-
3u
},
{
1
,
2
,
154
,
253
}
),
make_invalid_test_case
<
std
::
tuple
<
uint32_t
>
>
(
{
0
,
0
,
12
},
protocol_error_record
{
PROTOCOL_NS
,
LOWSIZE_ERR
,
0
}
),
{
0
,
0
,
12
},
protocol_error_record
{
&
LOWSIZE_ERR
,
0
}
),
make_invalid_test_case
<
std
::
tuple
<
int8_t
,
uint16_t
,
int8_t
,
int8_t
>
>
(
{
0
,
0
,
12
,
0
},
protocol_error_record
{
PROTOCOL_NS
,
LOWSIZE_ERR
,
4
}
),
{
0
,
0
,
12
,
0
},
protocol_error_record
{
&
LOWSIZE_ERR
,
4
}
),
// std::variant
make_valid_test_case
<
PROTOCOL_LITTLE_ENDIAN
>
(
std
::
variant
<
uint8_t
,
int16_t
,
uint16_t
>
{
int16_t
{
-
3
}
},
{
1
,
253
,
255
}
),
...
...
@@ -170,11 +169,11 @@ int main( int argc, char** argv )
make_valid_test_case
<
PROTOCOL_LITTLE_ENDIAN
>
(
std
::
variant
<
uint8_t
,
int16_t
,
uint16_t
>
{
uint8_t
{
42
}
},
{
0
,
42
}
),
make_invalid_test_case
<
std
::
variant
<
uint8_t
,
int16_t
,
uint16_t
>
>
(
{
3
,
0
,
0
},
protocol_error_record
{
PROTOCOL_NS
,
UNDEFVAR_ERR
,
0
}
),
{
3
,
0
,
0
},
protocol_error_record
{
&
UNDEFVAR_ERR
,
0
}
),
make_invalid_test_case
<
std
::
variant
<
uint8_t
,
int16_t
,
uint16_t
>
>
(
{
0
},
protocol_error_record
{
PROTOCOL_NS
,
LOWSIZE_ERR
,
1
}
),
{
0
},
protocol_error_record
{
&
LOWSIZE_ERR
,
1
}
),
make_invalid_test_case
<
std
::
variant
<
uint8_t
,
int16_t
,
uint16_t
>
>
(
{
1
,
0
},
protocol_error_record
{
PROTOCOL_NS
,
LOWSIZE_ERR
,
1
}
),
{
1
,
0
},
protocol_error_record
{
&
LOWSIZE_ERR
,
1
}
),
// std::bitset
make_valid_test_case
<
PROTOCOL_LITTLE_ENDIAN
>
(
std
::
bitset
<
3
>
{
0b00000111
},
{
0b00000111
}
),
...
...
@@ -187,9 +186,9 @@ int main( int argc, char** argv )
make_valid_test_case
<
PROTOCOL_LITTLE_ENDIAN
>
(
std
::
bitset
<
16
>
{
0xFFFF
},
{
0xFF
,
0xFF
}
),
make_invalid_test_case
<
std
::
bitset
<
7
>
>
(
{},
protocol_error_record
{
PROTOCOL_NS
,
LOWSIZE_ERR
,
0
}
),
{},
protocol_error_record
{
&
LOWSIZE_ERR
,
0
}
),
make_invalid_test_case
<
std
::
bitset
<
9
>
>
(
{
0x0
},
protocol_error_record
{
PROTOCOL_NS
,
LOWSIZE_ERR
,
0
}
),
{
0x0
},
protocol_error_record
{
&
LOWSIZE_ERR
,
0
}
),
// protocol_sizeless_message
make_valid_test_case
<
PROTOCOL_LITTLE_ENDIAN
>
(
*
protocol_sizeless_message
<
8
>::
make
(
std
::
vector
{
1
,
2
,
3
,
4
,
5
}
),
...
...
@@ -198,7 +197,7 @@ int main( int argc, char** argv )
*
protocol_sizeless_message
<
8
>::
make
(
std
::
vector
{
1
,
2
,
3
,
4
,
5
}
),
{
1
,
2
,
3
,
4
,
5
}
),
make_invalid_test_case
<
protocol_sizeless_message
<
4
>
>
(
{
0
,
0
,
0
,
0
,
0
},
protocol_error_record
{
PROTOCOL_NS
,
BIGSIZE_ERR
,
0
}
),
{
0
,
0
,
0
,
0
,
0
},
protocol_error_record
{
&
BIGSIZE_ERR
,
0
}
),
// protocol_offset
make_specific_valid_test_case
<
PROTOCOL_LITTLE_ENDIAN
,
protocol_offset
<
uint16_t
,
0
>
>
(
666u
,
{
154
,
2
}
),
...
...
@@ -209,14 +208,14 @@ int main( int argc, char** argv )
make_specific_valid_test_case
<
PROTOCOL_BIG_ENDIAN
,
protocol_offset
<
uint16_t
,
4
>
>
(
666u
,
{
2
,
158
}
),
make_invalid_test_case
<
protocol_offset
<
uint16_t
,
4
>
>
(
{
255
},
protocol_error_record
{
PROTOCOL_NS
,
LOWSIZE_ERR
,
0
}
),
{
255
},
protocol_error_record
{
&
LOWSIZE_ERR
,
0
}
),
// quantity
make_valid_test_case
<
PROTOCOL_LITTLE_ENDIAN
>
(
tagged_quantity
<
struct
offtag
,
uint16_t
>
{
666u
},
{
154
,
2
}
),
make_valid_test_case
<
PROTOCOL_BIG_ENDIAN
>
(
tagged_quantity
<
struct
offtag
,
uint16_t
>
{
666u
},
{
2
,
154
}
),
make_invalid_test_case
<
tagged_quantity
<
struct
offtag
,
uint16_t
>
>
(
{
255
},
protocol_error_record
{
PROTOCOL_NS
,
LOWSIZE_ERR
,
0
}
),
{
255
},
protocol_error_record
{
&
LOWSIZE_ERR
,
0
}
),
// bounded
make_valid_test_case
<
PROTOCOL_LITTLE_ENDIAN
>
(
bounded
<
uint16_t
,
0
,
1024u
>::
get
<
666u
>
(),
{
154
,
2
}
),
...
...
@@ -225,9 +224,9 @@ int main( int argc, char** argv )
make_valid_test_case
<
PROTOCOL_BIG_ENDIAN
>
(
bounded
<
int16_t
,
-
1
,
1
>::
get
<
-
1
>
(),
{
255
,
255
}
),
make_invalid_test_case
<
bounded
<
int16_t
,
-
1
,
1
>
>
(
{
128
},
protocol_error_record
{
PROTOCOL_NS
,
LOWSIZE_ERR
,
0
}
),
{
128
},
protocol_error_record
{
&
LOWSIZE_ERR
,
0
}
),
make_invalid_test_case
<
bounded
<
int16_t
,
-
1
,
1
>
>
(
{
0
,
128
},
protocol_error_record
{
PROTOCOL_NS
,
BOUNDS_ERR
,
0
}
),
{
0
,
128
},
protocol_error_record
{
&
BOUNDS_ERR
,
0
}
),
// sized_buffer
make_specific_valid_test_case
<
PROTOCOL_LITTLE_ENDIAN
,
...
...
@@ -250,14 +249,14 @@ int main( int argc, char** argv )
protocol_sized_buffer
<
protocol_offset
<
uint16_t
,
2
>
,
uint16_t
>
>
(
666u
,
{
0
,
4
,
2
,
154
}
),
make_invalid_test_case
<
protocol_sized_buffer
<
uint16_t
,
uint16_t
>
>
(
{
0
,
2
,
2
},
protocol_error_record
{
PROTOCOL_NS
,
LOWSIZE_ERR
,
0
}
),
{
0
,
2
,
2
},
protocol_error_record
{
&
LOWSIZE_ERR
,
0
}
),
make_invalid_test_case
<
protocol_sized_buffer
<
uint16_t
,
uint16_t
>
>
(
{
0
,
1
,
2
,
2
},
protocol_error_record
{
PROTOCOL_NS
,
LOWSIZE_ERR
,
2
}
),
{
0
,
1
,
2
,
2
},
protocol_error_record
{
&
LOWSIZE_ERR
,
2
}
),
// tag
make_valid_test_case
<
PROTOCOL_LITTLE_ENDIAN
>
(
tag
<
666u
>
{},
{
154
,
2
,
0
,
0
}
),
make_valid_test_case
<
PROTOCOL_BIG_ENDIAN
>
(
tag
<
666u
>
{},
{
0
,
0
,
2
,
154
}
),
make_invalid_test_case
<
tag
<
666u
>
>
(
{
0
,
0
,
2
,
152
},
protocol_error_record
{
PROTOCOL_NS
,
BADVAL_ERR
,
0
}
),
{
0
,
0
,
2
,
152
},
protocol_error_record
{
&
BADVAL_ERR
,
0
}
),
// group is tested as part of command group
// endianess change
make_specific_valid_test_case
<
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment