FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
modbus-cpp/source/response.cpp at master · rayandrew/modbus-cpp · GitHub
rayandrew
/
modbus-cpp
Public
Notifications
You must be signed in to change notification settings
Fork
14
Star
33
Code
Issues
0
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
modbus-cpp
/
source
/
response.cpp
Copy path
More file actions
More file actions
Latest commit
History
History
History
146 lines (118 loc) · 3.99 KB
Breadcrumbs
modbus-cpp
/
source
/
response.cpp
Copy path
File metadata and controls
146 lines (118 loc) · 3.99 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
#
include
<
modbuscpp/modbuscpp/response.hpp
>
#
include
<
fmt/format.h
>
#
include
<
modbuscpp/modbuscpp/logger.hpp
>
#
include
<
modbuscpp/modbuscpp/struct.hpp
>
#
include
<
modbuscpp/modbuscpp/types.hpp
>
#
include
<
modbuscpp/modbuscpp/utilities.hpp
>
namespace
modbus
{
namespace
internal
{
response::response
()
noexcept
{}
response::response
(constants::function_code function,
const
header_t
& req_header,
table* data_table)
noexcept
: adu{function}, req_header_{req_header}, data_table_{data_table} {}
response::~response
() {}
bool
response::initial_check
(
const
packet_t
& packet) {
return
packet.
size
() > header_length;
}
stage
response::check_stage
(
const
packet_t
& packet) {
//
1. check packet size (at least we got header and the function code)
if
(!
initial_check
(packet)) {
//
bad packet
return
stage::bad;
}
std::
uint16_t
tr, pr, len;
std::
uint8_t
un, fun;
struc::unpack
(
fmt::format
(
"
>{}
"
, header_func_format), packet.
data
(), tr, pr,
len, un, fun);
#
ifdef
DEBUG_ON
logger::debug
(
"
Checking header: transaction(req[{:#04x}]=packet[{:#04x}])
"
"
protocol(req[{:#04x}]=packet[{:#04x}])
"
"
unit(req[{:#04x}]=packet[{:#04x}])
"
"
length(expected[{:#04x}]=packet[{:#04x}]
"
,
req_header_.
transaction
, tr, protocol, pr, req_header_.
unit
, un, len,
(packet.
size
() - (header_length -
1
)));
#
endif
//
2. check transaction, protocol, and unit id
if
((req_header_.
transaction
!= tr) || (protocol != pr)
|| (req_header_.
unit
!= un)
|| (len != (packet.
size
() - (header_length -
1
)))) {
//
bad packet
return
stage::bad;
}
//
3. unpack packet and initialize header metadata
decode_header
(packet);
//
4. check expected function is valid or not
if
(!
check_function
(
function
())) {
//
bad packet or defined class
return
stage::bad;
}
auto
expected_function =
utilities::to_underlying
(
function
());
//
5. check expected function code equals with function code from packet
if
((expected_function != function_code_)
&& !
check_function
(function_code_)) {
std::
uint8_t
diff = function_code_ -
0x80
;
//
6. if expected function equals packet's function_code - 0x80
//
exception is occured
if
(expected_function == diff) {
return
stage::error;
}
//
bad packet
return
stage::bad;
}
//
7. all tests passed, return the "right" response
return
stage::passed;
}
void
response::decode
(
const
packet_t
& packet) {
try
{
switch
(
check_stage
(packet)) {
case
internal::stage::bad:
throw
ex::bad_data
();
case
internal::stage::error: {
//
decode the packet
auto
exc = packet.
at
(header_length +
1
);
throw
generate_exception
(
static_cast
<constants::exception_code>(exc),
function
(),
header
());
}
break
;
default
:
decode_passed
(packet);
break
;
}
}
catch
(
const
std::out_of_range&) {
//
anything happens, such as packet is malformed
throw
ex::bad_data
();
}
}
}
//
namespace internal
namespace
response
{
error::error
()
noexcept
{}
packet_t
error::encode
() {
calc_length
(
1
);
packet_t
packet =
header_packet
();
packet.
pop_back
();
packet.
reserve
(header_length +
1
+
1
);
packet_t
pdu =
struc::pack
(
fmt::format
(
"
>{}
"
, format),
utilities::to_underlying
(
function
()) +
0x80
,
utilities::to_underlying
(ec_));
packet.
insert
(packet.
end
(), pdu.
begin
(), pdu.
end
());
if
(packet.
size
() !=
calc_adu_length
(
1
)) {
throw
ex::bad_data
();
}
return
packet;
}
void
error::decode
(
const
packet_t
& packet) {
if
(packet.
size
() !=
calc_adu_length
(
1
)) {
throw
ex::bad_data
();
}
decode_header
(packet);
std::
uint8_t
ec;
struc::unpack
(
fmt::format
(
"
>{}
"
, format), packet.
data
() + header_length +
1
,
ec);
if
(!
check_exception
(ec)) {
throw
ex::bad_exception
();
}
ec_ =
static_cast
<constants::exception_code>(ec);
}
}
//
namespace response
}
//
namespace modbus
Back
|
FazBrowse Home
|
New Git URL