Skip to content

Commit 60875c9

Browse files
committed
Improve compilation flags - \#11
Signed-off-by: Mateusz Mazur <mateusz.mazur@e.email>
1 parent 7d5352a commit 60875c9

File tree

4 files changed

+32
-18
lines changed

4 files changed

+32
-18
lines changed

CMakeLists.txt

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,7 @@ project(protocolConverter)
33

44
set(CMAKE_CXX_STANDARD 17)
55

6-
if (DCMAKE_BUILD_TYPE MATCHES Debug)
7-
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Wextra -lpthread -Wall")
8-
endif()
6+
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Wextra -Werror -pedantic")
97

108
option(MODBUS_EXAMPLE "Build example program" OFF)
119
option(MODBUS_TESTS "Build tests" OFF)
@@ -21,4 +19,3 @@ if(MODBUS_EXAMPLE)
2119
add_executable(ex example/main.cpp)
2220
target_link_libraries(ex Modbus)
2321
endif()
24-

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@
22

33
Library for high level Modbus frame manipulation, including encoding and decoding, written in modern C++17.
44

5-
Additionally, library contains simple linux TCP and RTU implementation,
5+
Additionally, library contains reference TCP and RTU implementation (on Linux only !),
66
that you can use as a starting point for you application !
7+
Until there will some reliable way to test it, it will be treated as such.
78

89
# Table of content
910
- [Why](#why)

src/Serial/connection.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
// Licensed under: MIT License <http://opensource.org/licenses/MIT>
44

55
#include "Serial/connection.hpp"
6+
#include <sys/poll.h>
67

78
using namespace MB::Serial;
89

@@ -52,7 +53,10 @@ std::vector<uint8_t> Connection::sendException(const MB::ModbusException &except
5253
std::vector<uint8_t> Connection::awaitRawMessage() {
5354
std::vector<uint8_t> data(1024);
5455

55-
pollfd waitingFD = {.fd = _fd, .events = POLLIN, .revents = POLLIN};
56+
pollfd waitingFD;
57+
waitingFD.fd = this->_fd;
58+
waitingFD.events = POLLIN;
59+
waitingFD.revents = POLLIN;
5660

5761
if (::poll(&waitingFD, 1, _timeout) <= 0) {
5862
throw MB::ModbusException(MB::utils::Timeout);

src/TCP/connection.cpp

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
// Licensed under: MIT License <http://opensource.org/licenses/MIT>
44

55
#include "TCP/connection.hpp"
6+
#include <sys/poll.h>
7+
#include <sys/socket.h>
68

79
using namespace MB::TCP;
810

@@ -86,8 +88,11 @@ std::vector<uint8_t> Connection::sendException(const MB::ModbusException &ex) {
8688
}
8789

8890
std::vector<uint8_t> Connection::awaitRawMessage() {
89-
pollfd _pfd = {.fd = _sockfd, .events = POLLIN, .revents = POLLIN};
90-
if (::poll(&_pfd, 1, 60 * 1000 /* 1 minute means the connection has died */) <= 0) {
91+
pollfd pfd;
92+
pfd.fd = this->_sockfd;
93+
pfd.events = POLLIN;
94+
pfd.revents = POLLIN;
95+
if (::poll(&pfd, 1, 60 * 1000 /* 1 minute means the connection has died */) <= 0) {
9196
throw MB::ModbusException(MB::utils::ConnectionClosed);
9297
}
9398

@@ -108,8 +113,11 @@ std::vector<uint8_t> Connection::awaitRawMessage() {
108113
}
109114

110115
MB::ModbusRequest Connection::awaitRequest() {
111-
pollfd _pfd = {.fd = _sockfd, .events = POLLIN, .revents = POLLIN};
112-
if (::poll(&_pfd, 1, 60 * 1000 /* 1 minute means the connection has died */) <= 0) {
116+
pollfd pfd;
117+
pfd.fd = this->_sockfd;
118+
pfd.events = POLLIN;
119+
pfd.revents = POLLIN;
120+
if (::poll(&pfd, 1, 60 * 1000 /* 1 minute means the connection has died */) <= 0) {
113121
throw MB::ModbusException(MB::utils::Timeout);
114122
}
115123

@@ -136,13 +144,17 @@ MB::ModbusRequest Connection::awaitRequest() {
136144
}
137145

138146
MB::ModbusResponse Connection::awaitResponse() {
139-
pollfd _pfd = {.fd = _sockfd, .events = POLLIN, .revents = POLLIN};
140-
if (::poll(&_pfd, 1, _timeout) <= 0) {
147+
pollfd pfd;
148+
pfd.fd = this->_sockfd;
149+
pfd.events = POLLIN;
150+
pfd.revents = POLLIN;
151+
152+
if (::poll(&pfd, 1, this->_timeout) <= 0) {
141153
throw MB::ModbusException(MB::utils::Timeout);
142154
}
143155

144156
std::vector<uint8_t> r(1024);
145-
auto size = ::recv(_sockfd, r.begin().base(), r.size(), 0);
157+
auto size = ::recv(this->_sockfd, r.begin().base(), r.size(), 0);
146158

147159
if (size == -1)
148160
throw MB::ModbusException(MB::utils::ProtocolError);
@@ -155,7 +167,7 @@ MB::ModbusResponse Connection::awaitResponse() {
155167

156168
const auto resultMessageID = *reinterpret_cast<uint16_t *>(&r[0]);
157169

158-
if (resultMessageID != _messageID)
170+
if (resultMessageID != this->_messageID)
159171
throw MB::ModbusException(MB::utils::InvalidMessageID);
160172

161173
r.erase(r.begin(), r.begin() + 6);
@@ -180,10 +192,10 @@ Connection Connection::with(std::string addr, int port) {
180192
if (sock == -1)
181193
throw std::runtime_error("Cannot open socket, errno = " + std::to_string(errno));
182194

183-
sockaddr_in server = {.sin_family = AF_INET,
184-
.sin_port = htons(port),
185-
.sin_addr = {inet_addr(addr.c_str())},
186-
.sin_zero = {}};
195+
sockaddr_in server;
196+
server.sin_family = AF_INET;
197+
server.sin_port = ::htons(port);
198+
server.sin_addr = { inet_addr(addr.c_str()) };
187199

188200
if (::connect(sock, reinterpret_cast<struct sockaddr *>(&server), sizeof(server)) < 0)
189201
throw std::runtime_error("Cannot connect, errno = " + std::to_string(errno));

0 commit comments

Comments
 (0)