Skip to content

Commit c5ac04b

Browse files
fix clang-tidy warnings
1 parent 5384e9d commit c5ac04b

File tree

7 files changed

+160
-155
lines changed

7 files changed

+160
-155
lines changed

src/Modbus_TCP_Client_poll.cpp

Lines changed: 40 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,7 @@
1616
#include <sys/socket.h>
1717
#include <system_error>
1818

19-
namespace Modbus {
20-
namespace TCP {
19+
namespace Modbus::TCP {
2120

2221
//* maximum number of Modbus registers (per type)
2322
static constexpr int MAX_REGS = 0x10000;
@@ -37,8 +36,8 @@ static constexpr struct timespec SEMAPHORE_MAX_TIME = {0, 100'000};
3736
Client_Poll::Client_Poll(const std::string &host,
3837
const std::string &service,
3938
modbus_mapping_t *mapping,
40-
std::size_t tcp_timeout,
41-
std::size_t max_clients)
39+
std::size_t tcp_timeout, // NOLINT
40+
std::size_t max_clients) // NOLINT
4241
: max_clients(max_clients), poll_fds(max_clients + 2, {0, 0, 0}) {
4342
const char *host_str = "::";
4443
if (!(host.empty() || host == "any")) host_str = host.c_str();
@@ -50,7 +49,7 @@ Client_Poll::Client_Poll(const std::string &host,
5049
throw std::runtime_error("failed to create modbus instance: " + error_msg);
5150
}
5251

53-
modbus_mapping_t *mb_mapping;
52+
modbus_mapping_t *mb_mapping; // NOLINT
5453

5554
if (mapping == nullptr) {
5655
// create new mapping with the maximum number of registers
@@ -69,7 +68,7 @@ Client_Poll::Client_Poll(const std::string &host,
6968

7069
// use mapping for all client ids
7170
for (std::size_t i = 0; i < MAX_CLIENT_IDS; ++i) {
72-
this->mappings[i] = mb_mapping;
71+
this->mappings[i] = mb_mapping; // NOLINT
7372
}
7473

7574
listen();
@@ -81,11 +80,11 @@ Client_Poll::Client_Poll(const std::string &host,
8180
#endif
8281
}
8382

84-
Client_Poll::Client_Poll(const std::string &host,
85-
const std::string &service,
86-
modbus_mapping_t **mappings,
87-
std::size_t tcp_timeout,
88-
std::size_t max_clients)
83+
Client_Poll::Client_Poll(const std::string &host,
84+
const std::string &service,
85+
std::array<modbus_mapping_t *, MAX_CLIENT_IDS> &mappings,
86+
std::size_t tcp_timeout, // NOLINT
87+
std::size_t max_clients) // NOLINT
8988
: max_clients(max_clients), poll_fds(max_clients + 2, {0, 0, 0}) {
9089
const char *host_str = "::";
9190
if (!(host.empty() || host == "any")) host_str = host.c_str();
@@ -100,7 +99,7 @@ Client_Poll::Client_Poll(const std::string &host,
10099
delete_mapping = nullptr;
101100

102101
for (std::size_t i = 0; i < MAX_CLIENT_IDS; ++i) {
103-
if (mappings[i] == nullptr) {
102+
if (mappings[i] == nullptr) { // NOLINT
104103
if (delete_mapping == nullptr) {
105104
delete_mapping = modbus_mapping_new(MAX_REGS, MAX_REGS, MAX_REGS, MAX_REGS);
106105

@@ -110,9 +109,9 @@ Client_Poll::Client_Poll(const std::string &host,
110109
throw std::runtime_error("failed to allocate memory: " + error_msg);
111110
}
112111
}
113-
this->mappings[i] = delete_mapping;
112+
this->mappings[i] = delete_mapping; // NOLINT
114113
} else {
115-
this->mappings[i] = mappings[i];
114+
this->mappings[i] = mappings[i]; // NOLINT
116115
}
117116
}
118117

@@ -156,9 +155,9 @@ Client_Poll::~Client_Poll() {
156155
}
157156

158157
#ifdef OS_LINUX
159-
void Client_Poll::set_tcp_timeout(std::size_t tcp_timeout) {
158+
void Client_Poll::set_tcp_timeout(std::size_t tcp_timeout) const {
160159
// set user timeout (~= timeout for tcp connection)
161-
unsigned user_timeout = static_cast<unsigned>(tcp_timeout) * 1000;
160+
unsigned user_timeout = static_cast<unsigned>(tcp_timeout) * 1000; // NOLINT
162161
int tmp = setsockopt(server_socket, IPPROTO_TCP, TCP_USER_TIMEOUT, &user_timeout, sizeof(tcp_timeout));
163162
if (tmp != 0) {
164163
throw std::system_error(errno, std::generic_category(), "Failed to set socket option TCP_USER_TIMEOUT");
@@ -172,14 +171,14 @@ void Client_Poll::set_tcp_timeout(std::size_t tcp_timeout) {
172171
}
173172

174173
// send up to 5 keepalive requests during the timeout time, but not more than one per second
175-
unsigned keepintvl = std::max(static_cast<unsigned>(tcp_timeout / 5), 1u);
174+
unsigned keepintvl = std::max(static_cast<unsigned>(tcp_timeout / 5), 1u); // NOLINT
176175
tmp = setsockopt(server_socket, IPPROTO_TCP, TCP_KEEPINTVL, &keepintvl, sizeof(keepintvl));
177176
if (tmp != 0) {
178177
throw std::system_error(errno, std::generic_category(), "Failed to set socket option TCP_KEEPINTVL");
179178
}
180179

181180
// 5 keepalive requests if the timeout time is >= 5s; else send one request each second
182-
unsigned keepcnt = std::min(static_cast<unsigned>(tcp_timeout), 5u);
181+
unsigned keepcnt = std::min(static_cast<unsigned>(tcp_timeout), 5u); // NOLINT
183182
tmp = setsockopt(server_socket, IPPROTO_TCP, TCP_KEEPCNT, &keepcnt, sizeof(keepcnt));
184183
if (tmp != 0) {
185184
throw std::system_error(errno, std::generic_category(), "Failed to set socket option TCP_KEEPCNT");
@@ -213,7 +212,7 @@ static inline timeout_t double_to_timeout_t(double timeout) {
213212
ret.sec = static_cast<uint32_t>(timeout);
214213

215214
double fractional = timeout - static_cast<double>(ret.sec);
216-
ret.usec = static_cast<uint32_t>(fractional * 1000.0 * 1000.0);
215+
ret.usec = static_cast<uint32_t>(fractional * 1000.0 * 1000.0); // NOLINT
217216

218217
return ret;
219218
}
@@ -238,7 +237,7 @@ void Client_Poll::set_response_timeout(double timeout) {
238237
}
239238
}
240239

241-
double Client_Poll::get_byte_timeout() {
240+
[[maybe_unused]] double Client_Poll::get_byte_timeout() {
242241
timeout_t timeout {};
243242

244243
auto ret = modbus_get_byte_timeout(modbus, &timeout.sec, &timeout.usec);
@@ -248,10 +247,10 @@ double Client_Poll::get_byte_timeout() {
248247
throw std::runtime_error("modbus_receive failed: " + error_msg + ' ' + std::to_string(errno));
249248
}
250249

251-
return static_cast<double>(timeout.sec) + (static_cast<double>(timeout.usec) / (1000.0 * 1000.0));
250+
return static_cast<double>(timeout.sec) + (static_cast<double>(timeout.usec) / (1000.0 * 1000.0)); // NOLINT
252251
}
253252

254-
double Client_Poll::get_response_timeout() {
253+
[[maybe_unused]] double Client_Poll::get_response_timeout() {
255254
timeout_t timeout {};
256255

257256
auto ret = modbus_get_response_timeout(modbus, &timeout.sec, &timeout.usec);
@@ -261,7 +260,7 @@ double Client_Poll::get_response_timeout() {
261260
throw std::runtime_error("modbus_receive failed: " + error_msg + ' ' + std::to_string(errno));
262261
}
263262

264-
return static_cast<double>(timeout.sec) + (static_cast<double>(timeout.usec) / (1000.0 * 1000.0));
263+
return static_cast<double>(timeout.sec) + (static_cast<double>(timeout.usec) / (1000.0 * 1000.0)); // NOLINT
265264
}
266265

267266
Client_Poll::run_t Client_Poll::run(int signal_fd, bool reconnect, int timeout) {
@@ -284,7 +283,7 @@ Client_Poll::run_t Client_Poll::run(int signal_fd, bool reconnect, int timeout)
284283
}
285284

286285
// add client sockets to poll
287-
for (auto con : client_addrs) {
286+
for (const auto &con : client_addrs) {
288287
auto &fd = poll_fds[i++];
289288
fd.fd = con.first;
290289
fd.events = POLLIN;
@@ -332,9 +331,9 @@ Client_Poll::run_t Client_Poll::run(int signal_fd, bool reconnect, int timeout)
332331

333332
auto client_socket = modbus_get_socket(modbus);
334333

335-
struct sockaddr_storage peer_addr;
334+
struct sockaddr_storage peer_addr; // NOLINT
336335
socklen_t len = sizeof(peer_addr);
337-
tmp = getpeername(client_socket, reinterpret_cast<struct sockaddr *>(&peer_addr), &len);
336+
tmp = getpeername(client_socket, reinterpret_cast<struct sockaddr *>(&peer_addr), &len); // NOLINT
338337

339338
if (tmp < 0) {
340339
const std::string error_msg = modbus_strerror(errno);
@@ -345,11 +344,11 @@ Client_Poll::run_t Client_Poll::run(int signal_fd, bool reconnect, int timeout)
345344

346345
sstr << sockaddr_to_str(peer_addr);
347346
// the port entries have the same offset and size in sockaddr_in and sockaddr_in6
348-
sstr << ':' << htons(reinterpret_cast<const struct sockaddr_in *>(&peer_addr)->sin_port);
347+
sstr << ':' << htons(reinterpret_cast<const struct sockaddr_in *>(&peer_addr)->sin_port); // NOLINT
349348

350349
client_addrs[client_socket] = sstr.str();
351350
std::cerr << Print_Time::iso << " INFO: [" << active_clients + 1 << "] Modbus Server (" << sstr.str()
352-
<< ") established connection." << std::endl;
351+
<< ") established connection." << std::endl; // NOLINT
353352
} else {
354353
std::ostringstream sstr;
355354
sstr << "poll (server socket) returned unknown revent: " << fd.revents;
@@ -380,27 +379,27 @@ Client_Poll::run_t Client_Poll::run(int signal_fd, bool reconnect, int timeout)
380379
} else if (fd.revents & POLLIN || fd.revents & POLLERR) {
381380
modbus_set_socket(modbus, fd.fd);
382381

383-
uint8_t query[MODBUS_TCP_MAX_ADU_LENGTH];
384-
int rc = modbus_receive(modbus, query);
382+
std::array<uint8_t, MODBUS_TCP_MAX_ADU_LENGTH> query {};
383+
int rc = modbus_receive(modbus, query.data());
385384
if (debug) std::cout.flush();
386385

387386
if (rc > 0) {
388387
const auto CLIENT_ID = query[6];
389388

390389
// get mapping
391-
auto mapping = mappings[CLIENT_ID];
390+
auto mapping = mappings[CLIENT_ID]; // NOLINT
392391

393392
// handle request
394393
if (semaphore) {
395394
if (!semaphore->wait(SEMAPHORE_MAX_TIME)) {
396395
std::cerr << Print_Time::iso << " WARNING: Failed to acquire semaphore '"
397-
<< semaphore->get_name() << "' within 100ms." << std::endl;
396+
<< semaphore->get_name() << "' within 100ms." << std::endl; // NOLINT
398397

399398
semaphore_error_counter += SEMAPHORE_ERROR_INC;
400399

401400
if (semaphore_error_counter >= SEMAPHORE_ERROR_MAX) {
402401
std::cerr << Print_Time::iso << "ERROR: Repeatedly failed to acquire the semaphore"
403-
<< std::endl;
402+
<< std::endl; // NOLINT
404403
close_con(client_addrs);
405404
return run_t::semaphore;
406405
}
@@ -410,19 +409,19 @@ Client_Poll::run_t Client_Poll::run(int signal_fd, bool reconnect, int timeout)
410409
}
411410
}
412411

413-
int ret = modbus_reply(modbus, query, rc, mapping);
412+
int ret = modbus_reply(modbus, query.data(), rc, mapping);
414413
if (semaphore && semaphore->is_acquired()) semaphore->post();
415414
if (debug) std::cout.flush();
416415

417416
if (ret == -1) {
418417
std::cerr << Print_Time::iso << " ERROR: modbus_reply failed: " << modbus_strerror(errno)
419-
<< std::endl;
418+
<< std::endl; // NOLINT
420419
close_con(client_addrs);
421420
}
422421
} else if (rc == -1) {
423422
if (errno != ECONNRESET) {
424423
std::cerr << Print_Time::iso << " ERROR: modbus_receive failed: " << modbus_strerror(errno)
425-
<< std::endl;
424+
<< std::endl; // NOLINT
426425
}
427426
close_con(client_addrs);
428427
} else { // rc == 0
@@ -440,10 +439,10 @@ Client_Poll::run_t Client_Poll::run(int signal_fd, bool reconnect, int timeout)
440439
return run_t::ok;
441440
}
442441

443-
std::string Client_Poll::get_listen_addr() {
444-
struct sockaddr_storage sock_addr;
442+
std::string Client_Poll::get_listen_addr() const {
443+
struct sockaddr_storage sock_addr; // NOLINT
445444
socklen_t len = sizeof(sock_addr);
446-
int tmp = getsockname(server_socket, reinterpret_cast<struct sockaddr *>(&sock_addr), &len);
445+
int tmp = getsockname(server_socket, reinterpret_cast<struct sockaddr *>(&sock_addr), &len); // NOLINT
447446

448447
if (tmp < 0) {
449448
const std::string error_msg = modbus_strerror(errno);
@@ -453,10 +452,9 @@ std::string Client_Poll::get_listen_addr() {
453452
std::ostringstream sstr;
454453
sstr << sockaddr_to_str(sock_addr);
455454
// the port entries have the same offset and size in sockaddr_in and sockaddr_in6
456-
sstr << ':' << htons(reinterpret_cast<const struct sockaddr_in *>(&sock_addr)->sin_port);
455+
sstr << ':' << htons(reinterpret_cast<const struct sockaddr_in *>(&sock_addr)->sin_port); // NOLINT
457456

458457
return sstr.str();
459458
}
460459

461-
} // namespace TCP
462-
} // namespace Modbus
460+
} // namespace Modbus::TCP

src/Modbus_TCP_Client_poll.hpp

Lines changed: 24 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@
44
*/
55
#pragma once
66

7+
#include <array>
78
#include <cstddef>
9+
#include <cstdint>
810
#include <cxxsemaphore.hpp>
911
#include <memory>
1012
#include <modbus/modbus.h>
@@ -15,14 +17,14 @@
1517
#include <unordered_set>
1618
#include <vector>
1719

18-
namespace Modbus {
19-
namespace TCP {
20+
21+
namespace Modbus::TCP {
2022

2123
class Client_Poll {
2224
public:
2325
static constexpr std::size_t MAX_CLIENT_IDS = 256;
2426

25-
enum class run_t { ok, term_signal, term_nocon, timeout, interrupted, semaphore };
27+
enum class run_t : std::uint8_t { ok, term_signal, term_nocon, timeout, interrupted, semaphore };
2628

2729
private:
2830
const std::size_t max_clients;
@@ -31,10 +33,10 @@ class Client_Poll {
3133
bool debug = false; //!< modbus debugging enabled
3234

3335
modbus_t *modbus; //!< modbus object (see libmodbus library)
34-
modbus_mapping_t
35-
*mappings[MAX_CLIENT_IDS]; //!< modbus data objects (one per possible client id) (see libmodbus library)
36-
modbus_mapping_t *delete_mapping; //!< contains a pointer to a mapping that is to be deleted
37-
int server_socket = -1; //!< socket of the modbus connection
36+
std::array<modbus_mapping_t *, MAX_CLIENT_IDS>
37+
mappings {}; //!< modbus data objects (one per possible client id) (see libmodbus library)
38+
modbus_mapping_t *delete_mapping; //!< contains a pointer to a mapping that is to be deleted
39+
int server_socket = -1; //!< socket of the modbus connection
3840
std::unordered_map<int, std::string> client_addrs;
3941

4042
std::unique_ptr<cxxsemaphore::Semaphore> semaphore;
@@ -64,17 +66,22 @@ class Client_Poll {
6466
* @param mappings modbus mappings (one for each possible id)
6567
* @param tcp_timeout tcp timeout (currently only available on linux systems)
6668
*/
67-
Client_Poll(const std::string &host,
68-
const std::string &service,
69-
modbus_mapping_t *mappings[MAX_CLIENT_IDS],
70-
std::size_t tcp_timeout = 5,
71-
std::size_t max_clients = 1);
69+
Client_Poll(const std::string &host,
70+
const std::string &service,
71+
std::array<modbus_mapping_t *, MAX_CLIENT_IDS> &mappings,
72+
std::size_t tcp_timeout = 5,
73+
std::size_t max_clients = 1);
7274

7375
/**
7476
* @brief destroy the modbus client
7577
*/
7678
~Client_Poll();
7779

80+
Client_Poll(const Client_Poll &other) = delete;
81+
Client_Poll(Client_Poll &&other) = delete;
82+
Client_Poll operator&(const Client_Poll &other) = delete;
83+
Client_Poll operator&(Client_Poll &&other) = delete;
84+
7885
/**
7986
* @brief use the semaphore mechanism
8087
*
@@ -93,7 +100,7 @@ class Client_Poll {
93100
*
94101
* @return server listening address
95102
*/
96-
std::string get_listen_addr();
103+
std::string get_listen_addr() const;
97104

98105
/*!
99106
* \brief set byte timeout
@@ -117,13 +124,13 @@ class Client_Poll {
117124
* \brief get byte timeout in seconds
118125
* @return byte timeout
119126
*/
120-
double get_byte_timeout();
127+
[[maybe_unused]] double get_byte_timeout();
121128

122129
/**
123130
* \brief get response timeout in seconds
124131
* @return response timeout
125132
*/
126-
double get_response_timeout();
133+
[[maybe_unused]] double get_response_timeout();
127134

128135
/*! \brief get the modbus socket
129136
*
@@ -144,11 +151,10 @@ class Client_Poll {
144151

145152
private:
146153
#ifdef OS_LINUX
147-
void set_tcp_timeout(std::size_t tcp_timeout);
154+
void set_tcp_timeout(std::size_t tcp_timeout) const;
148155
#endif
149156

150157
void listen();
151158
};
152159

153-
} // namespace TCP
154-
} // namespace Modbus
160+
} // namespace Modbus::TCP

src/Print_Time.cpp

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,15 @@
55

66
#include "Print_Time.hpp"
77

8+
#include <array>
89
#include <ctime>
910

1011
Print_Time Print_Time::iso("%F_%T");
1112

1213
std::ostream &operator<<(std::ostream &o, const Print_Time &p) {
13-
auto now = time(nullptr);
14-
char buf[sizeof "1234-25-78T90:12:34Z"];
15-
strftime(buf, sizeof buf, p.format.c_str(), gmtime(&now));
16-
o << buf;
14+
auto now = time(nullptr);
15+
std::array<char, sizeof "1234-25-78T90:12:34Z"> buf {};
16+
strftime(buf.data(), buf.size(), p.format.c_str(), gmtime(&now));
17+
o << buf.data();
1718
return o;
1819
}

0 commit comments

Comments
 (0)