My Project
generic_functions.cpp
1 /*
2  * uuid-modbus - Microcontroller asynchronous Modbus library
3  * Copyright 2021-2022 Simon Arlott
4  *
5  * This program is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation, either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program. If not, see <http://www.gnu.org/licenses/>.
17  */
18 
19 #include <uuid/modbus.h>
20 
21 #include <Arduino.h>
22 
23 #include <algorithm>
24 #include <cstdarg>
25 #include <cstdint>
26 #include <memory>
27 
28 namespace uuid {
29 
30 namespace modbus {
31 
32 Request::Request(uint16_t device, uint8_t function_code, uint16_t timeout_ms,
33  const std::shared_ptr<Response> &response)
34  : device_(device), function_code_(function_code), timeout_ms_(timeout_ms),
35  response_(response) {
36 }
37 
38 uint16_t Request::encode(frame_buffer_t &frame) {
39  frame[0] = device();
40  frame[1] = function_code();
41  return 2;
42 }
43 
44 bool Response::check_length(frame_buffer_t &frame, uint16_t actual, uint16_t expected) {
45  if (actual != expected) {
46  logger.err(F("Length mismatch for function %02X from device %u, expected %u received %u"),
47  frame[1], frame[0], expected, actual);
48  return false;
49  } else {
50  return true;
51  }
52 }
53 
54 } // namespace modbus
55 
56 } // namespace uuid
uuid::modbus::frame_buffer_t
std::array< uint8_t, MAX_MESSAGE_SIZE+1 > frame_buffer_t
Definition: modbus.h:67
uuid::log::Logger::err
void err(const char *format,...) const
Log a message at level Level::ERR.
Definition: log.cpp:170
uuid::modbus::Request::encode
virtual uint16_t encode(frame_buffer_t &frame)
Encode this request and store it in a message frame buffer.
Definition: generic_functions.cpp:38
uuid::modbus::Request::device
uint16_t device() const
Get the destination device address.
Definition: modbus.h:381
uuid::modbus::logger
const uuid::log::Logger logger
Definition: modbus.cpp:37
uuid::modbus::Request::Request
Request(uint16_t device, uint8_t function_code, uint16_t timeout_ms, const std::shared_ptr< Response > &response)
Create a new request message (not directly useful).
Definition: generic_functions.cpp:32
uuid::modbus::Response::check_length
bool check_length(frame_buffer_t &frame, uint16_t actual, uint16_t expected)
Check the length of the message frame is correct and log an error if it is not.
Definition: generic_functions.cpp:44
uuid::modbus::Request::function_code
uint8_t function_code() const
Get the function code of the request.
Definition: modbus.h:389
uuid
Common utilities.
Definition: get_uptime_ms.cpp:28