Skip to content

Commit 7a44c6a

Browse files
author
Jan Phillip Willmann
committed
Removed definition of printf and fprintf to make it compatible to ESP32
1 parent 5bf098b commit 7a44c6a

File tree

1 file changed

+39
-5
lines changed

1 file changed

+39
-5
lines changed

src/libmodbus/modbus-tcp.cpp

+39-5
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,10 @@
2828
# define SHUT_RDWR 2
2929
# define close closesocket
3030
#elif defined(ARDUINO)
31-
#ifndef DEBUG
32-
#define printf(...) {}
33-
#define fprintf(...) {}
34-
#endif
31+
// #ifndef DEBUG
32+
// #define printf(...) {}
33+
// #define fprintf(...) {}
34+
// #endif
3535
#else
3636
# include <sys/socket.h>
3737
# include <sys/ioctl.h>
@@ -88,8 +88,10 @@ static int _modbus_tcp_init_win32(void)
8888
WSADATA wsaData;
8989

9090
if (WSAStartup(MAKEWORD(2, 2), &wsaData) != 0) {
91+
#ifdef DEBUG
9192
fprintf(stderr, "WSAStartup() returned error code %d\n",
9293
(unsigned int)GetLastError());
94+
#endif
9395
errno = EIO;
9496
return -1;
9597
}
@@ -239,20 +241,24 @@ static int _modbus_tcp_pre_check_confirmation(modbus_t *ctx, const uint8_t *req,
239241
#endif
240242
/* Check transaction ID */
241243
if (req[0] != rsp[0] || req[1] != rsp[1]) {
242-
if (ctx->debug) {
244+
#ifdef DEBUG
245+
if (ctx->debug)
243246
fprintf(stderr, "Invalid transaction ID received 0x%X (not 0x%X)\n",
244247
(rsp[0] << 8) + rsp[1], (req[0] << 8) + req[1]);
245248
}
249+
#endif
246250
errno = EMBBADDATA;
247251
return -1;
248252
}
249253

250254
/* Check protocol ID */
251255
if (rsp[2] != 0x0 && rsp[3] != 0x0) {
256+
#ifdef DEBUG
252257
if (ctx->debug) {
253258
fprintf(stderr, "Invalid protocol ID received 0x%X (not 0x0)\n",
254259
(rsp[2] << 8) + rsp[3]);
255260
}
261+
#endif
256262
errno = EMBBADDATA;
257263
return -1;
258264
}
@@ -394,9 +400,11 @@ static int _modbus_tcp_connect(modbus_t *ctx)
394400
return -1;
395401
}
396402

403+
#ifdef DEBUG
397404
if (ctx->debug) {
398405
printf("Connecting to %s:%d\n", ctx_tcp->ip, ctx_tcp->port);
399406
}
407+
#endif
400408

401409
addr.sin_family = AF_INET;
402410
addr.sin_port = htons(ctx_tcp->port);
@@ -442,9 +450,11 @@ static int _modbus_tcp_pi_connect(modbus_t *ctx)
442450
rc = getaddrinfo(ctx_tcp_pi->node, ctx_tcp_pi->service,
443451
&ai_hints, &ai_list);
444452
if (rc != 0) {
453+
#ifdef DEBUG
445454
if (ctx->debug) {
446455
fprintf(stderr, "Error returned by getaddrinfo: %s\n", gai_strerror(rc));
447456
}
457+
#endif
448458
errno = ECONNREFUSED;
449459
return -1;
450460
}
@@ -468,9 +478,11 @@ static int _modbus_tcp_pi_connect(modbus_t *ctx)
468478
if (ai_ptr->ai_family == AF_INET)
469479
_modbus_tcp_set_ipv4_options(s);
470480

481+
#ifdef DEBUG
471482
if (ctx->debug) {
472483
printf("Connecting to [%s]:%s\n", ctx_tcp_pi->node, ctx_tcp_pi->service);
473484
}
485+
#endif
474486

475487
rc = _connect(s, ai_ptr->ai_addr, ai_ptr->ai_addrlen, &ctx->response_timeout);
476488
if (rc == -1) {
@@ -674,9 +686,11 @@ int modbus_tcp_pi_listen(modbus_t *ctx, int nb_connection)
674686
ai_list = NULL;
675687
rc = getaddrinfo(node, service, &ai_hints, &ai_list);
676688
if (rc != 0) {
689+
#ifdef DEBUG
677690
if (ctx->debug) {
678691
fprintf(stderr, "Error returned by getaddrinfo: %s\n", gai_strerror(rc));
679692
}
693+
#endif
680694
errno = ECONNREFUSED;
681695
return -1;
682696
}
@@ -779,10 +793,12 @@ int modbus_tcp_accept(modbus_t *ctx, int *s)
779793
return -1;
780794
}
781795

796+
#ifdef DEBUG
782797
if (ctx->debug) {
783798
printf("The client connection from %s is accepted\n",
784799
inet_ntoa(addr.sin_addr));
785800
}
801+
#endif
786802

787803
return ctx->s;
788804
#endif
@@ -811,9 +827,11 @@ int modbus_tcp_pi_accept(modbus_t *ctx, int *s)
811827
*s = -1;
812828
}
813829

830+
#ifdef DEBUG
814831
if (ctx->debug) {
815832
printf("The client connection is accepted.\n");
816833
}
834+
#endif
817835

818836
return ctx->s;
819837
}
@@ -840,9 +858,11 @@ static int _modbus_tcp_select(modbus_t *ctx, fd_set *rset, struct timeval *tv, i
840858
#else
841859
while ((s_rc = select(ctx->s+1, rset, NULL, NULL, tv)) == -1) {
842860
if (errno == EINTR) {
861+
#ifdef DEBUG
843862
if (ctx->debug) {
844863
fprintf(stderr, "A non blocked signal was caught\n");
845864
}
865+
#endif
846866
/* Necessary after an error */
847867
FD_ZERO(rset);
848868
FD_SET(ctx->s, rset);
@@ -933,7 +953,9 @@ modbus_t* modbus_new_tcp(const char *ip, int port)
933953
sa.sa_handler = SIG_IGN;
934954
if (sigaction(SIGPIPE, &sa, NULL) < 0) {
935955
/* The debug flag can't be set here... */
956+
#ifdef DEBUG
936957
fprintf(stderr, "Coud not install SIGPIPE handler.\n");
958+
#endif
937959
return NULL;
938960
}
939961
#endif
@@ -957,14 +979,18 @@ modbus_t* modbus_new_tcp(const char *ip, int port)
957979
dest_size = sizeof(char) * 16;
958980
ret_size = strlcpy(ctx_tcp->ip, ip, dest_size);
959981
if (ret_size == 0) {
982+
#ifdef DEBUG
960983
fprintf(stderr, "The IP string is empty\n");
984+
#endif
961985
modbus_free(ctx);
962986
errno = EINVAL;
963987
return NULL;
964988
}
965989

966990
if (ret_size >= dest_size) {
991+
#ifdef DEBUG
967992
fprintf(stderr, "The IP string has been truncated\n");
993+
#endif
968994
modbus_free(ctx);
969995
errno = EINVAL;
970996
return NULL;
@@ -1006,14 +1032,18 @@ modbus_t* modbus_new_tcp_pi(const char *node, const char *service)
10061032
dest_size = sizeof(char) * _MODBUS_TCP_PI_NODE_LENGTH;
10071033
ret_size = strlcpy(ctx_tcp_pi->node, node, dest_size);
10081034
if (ret_size == 0) {
1035+
#ifdef DEBUG
10091036
fprintf(stderr, "The node string is empty\n");
1037+
#endif
10101038
modbus_free(ctx);
10111039
errno = EINVAL;
10121040
return NULL;
10131041
}
10141042

10151043
if (ret_size >= dest_size) {
1044+
#ifdef DEBUG
10161045
fprintf(stderr, "The node string has been truncated\n");
1046+
#endif
10171047
modbus_free(ctx);
10181048
errno = EINVAL;
10191049
return NULL;
@@ -1029,14 +1059,18 @@ modbus_t* modbus_new_tcp_pi(const char *node, const char *service)
10291059
}
10301060

10311061
if (ret_size == 0) {
1062+
#ifdef DEBUG
10321063
fprintf(stderr, "The service string is empty\n");
1064+
#endif
10331065
modbus_free(ctx);
10341066
errno = EINVAL;
10351067
return NULL;
10361068
}
10371069

10381070
if (ret_size >= dest_size) {
1071+
#ifdef DEBUG
10391072
fprintf(stderr, "The service string has been truncated\n");
1073+
#endif
10401074
modbus_free(ctx);
10411075
errno = EINVAL;
10421076
return NULL;

0 commit comments

Comments
 (0)