28
28
# define SHUT_RDWR 2
29
29
# define close closesocket
30
30
#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
35
35
#else
36
36
# include < sys/socket.h>
37
37
# include < sys/ioctl.h>
@@ -88,8 +88,10 @@ static int _modbus_tcp_init_win32(void)
88
88
WSADATA wsaData;
89
89
90
90
if (WSAStartup (MAKEWORD (2 , 2 ), &wsaData) != 0 ) {
91
+ #ifdef DEBUG
91
92
fprintf (stderr, " WSAStartup() returned error code %d\n " ,
92
93
(unsigned int )GetLastError ());
94
+ #endif
93
95
errno = EIO;
94
96
return -1 ;
95
97
}
@@ -239,20 +241,24 @@ static int _modbus_tcp_pre_check_confirmation(modbus_t *ctx, const uint8_t *req,
239
241
#endif
240
242
/* Check transaction ID */
241
243
if (req[0 ] != rsp[0 ] || req[1 ] != rsp[1 ]) {
242
- if (ctx->debug ) {
244
+ #ifdef DEBUG
245
+ if (ctx->debug )
243
246
fprintf (stderr, " Invalid transaction ID received 0x%X (not 0x%X)\n " ,
244
247
(rsp[0 ] << 8 ) + rsp[1 ], (req[0 ] << 8 ) + req[1 ]);
245
248
}
249
+ #endif
246
250
errno = EMBBADDATA;
247
251
return -1 ;
248
252
}
249
253
250
254
/* Check protocol ID */
251
255
if (rsp[2 ] != 0x0 && rsp[3 ] != 0x0 ) {
256
+ #ifdef DEBUG
252
257
if (ctx->debug ) {
253
258
fprintf (stderr, " Invalid protocol ID received 0x%X (not 0x0)\n " ,
254
259
(rsp[2 ] << 8 ) + rsp[3 ]);
255
260
}
261
+ #endif
256
262
errno = EMBBADDATA;
257
263
return -1 ;
258
264
}
@@ -394,9 +400,11 @@ static int _modbus_tcp_connect(modbus_t *ctx)
394
400
return -1 ;
395
401
}
396
402
403
+ #ifdef DEBUG
397
404
if (ctx->debug ) {
398
405
printf (" Connecting to %s:%d\n " , ctx_tcp->ip , ctx_tcp->port );
399
406
}
407
+ #endif
400
408
401
409
addr.sin_family = AF_INET;
402
410
addr.sin_port = htons (ctx_tcp->port );
@@ -442,9 +450,11 @@ static int _modbus_tcp_pi_connect(modbus_t *ctx)
442
450
rc = getaddrinfo (ctx_tcp_pi->node , ctx_tcp_pi->service ,
443
451
&ai_hints, &ai_list);
444
452
if (rc != 0 ) {
453
+ #ifdef DEBUG
445
454
if (ctx->debug ) {
446
455
fprintf (stderr, " Error returned by getaddrinfo: %s\n " , gai_strerror (rc));
447
456
}
457
+ #endif
448
458
errno = ECONNREFUSED;
449
459
return -1 ;
450
460
}
@@ -468,9 +478,11 @@ static int _modbus_tcp_pi_connect(modbus_t *ctx)
468
478
if (ai_ptr->ai_family == AF_INET)
469
479
_modbus_tcp_set_ipv4_options (s);
470
480
481
+ #ifdef DEBUG
471
482
if (ctx->debug ) {
472
483
printf (" Connecting to [%s]:%s\n " , ctx_tcp_pi->node , ctx_tcp_pi->service );
473
484
}
485
+ #endif
474
486
475
487
rc = _connect (s, ai_ptr->ai_addr , ai_ptr->ai_addrlen , &ctx->response_timeout );
476
488
if (rc == -1 ) {
@@ -674,9 +686,11 @@ int modbus_tcp_pi_listen(modbus_t *ctx, int nb_connection)
674
686
ai_list = NULL ;
675
687
rc = getaddrinfo (node, service, &ai_hints, &ai_list);
676
688
if (rc != 0 ) {
689
+ #ifdef DEBUG
677
690
if (ctx->debug ) {
678
691
fprintf (stderr, " Error returned by getaddrinfo: %s\n " , gai_strerror (rc));
679
692
}
693
+ #endif
680
694
errno = ECONNREFUSED;
681
695
return -1 ;
682
696
}
@@ -779,10 +793,12 @@ int modbus_tcp_accept(modbus_t *ctx, int *s)
779
793
return -1 ;
780
794
}
781
795
796
+ #ifdef DEBUG
782
797
if (ctx->debug ) {
783
798
printf (" The client connection from %s is accepted\n " ,
784
799
inet_ntoa (addr.sin_addr ));
785
800
}
801
+ #endif
786
802
787
803
return ctx->s ;
788
804
#endif
@@ -811,9 +827,11 @@ int modbus_tcp_pi_accept(modbus_t *ctx, int *s)
811
827
*s = -1 ;
812
828
}
813
829
830
+ #ifdef DEBUG
814
831
if (ctx->debug ) {
815
832
printf (" The client connection is accepted.\n " );
816
833
}
834
+ #endif
817
835
818
836
return ctx->s ;
819
837
}
@@ -840,9 +858,11 @@ static int _modbus_tcp_select(modbus_t *ctx, fd_set *rset, struct timeval *tv, i
840
858
#else
841
859
while ((s_rc = select (ctx->s +1 , rset, NULL , NULL , tv)) == -1 ) {
842
860
if (errno == EINTR) {
861
+ #ifdef DEBUG
843
862
if (ctx->debug ) {
844
863
fprintf (stderr, " A non blocked signal was caught\n " );
845
864
}
865
+ #endif
846
866
/* Necessary after an error */
847
867
FD_ZERO (rset);
848
868
FD_SET (ctx->s , rset);
@@ -933,7 +953,9 @@ modbus_t* modbus_new_tcp(const char *ip, int port)
933
953
sa.sa_handler = SIG_IGN;
934
954
if (sigaction (SIGPIPE, &sa, NULL ) < 0 ) {
935
955
/* The debug flag can't be set here... */
956
+ #ifdef DEBUG
936
957
fprintf (stderr, " Coud not install SIGPIPE handler.\n " );
958
+ #endif
937
959
return NULL ;
938
960
}
939
961
#endif
@@ -957,14 +979,18 @@ modbus_t* modbus_new_tcp(const char *ip, int port)
957
979
dest_size = sizeof (char ) * 16 ;
958
980
ret_size = strlcpy (ctx_tcp->ip , ip, dest_size);
959
981
if (ret_size == 0 ) {
982
+ #ifdef DEBUG
960
983
fprintf (stderr, " The IP string is empty\n " );
984
+ #endif
961
985
modbus_free (ctx);
962
986
errno = EINVAL;
963
987
return NULL ;
964
988
}
965
989
966
990
if (ret_size >= dest_size) {
991
+ #ifdef DEBUG
967
992
fprintf (stderr, " The IP string has been truncated\n " );
993
+ #endif
968
994
modbus_free (ctx);
969
995
errno = EINVAL;
970
996
return NULL ;
@@ -1006,14 +1032,18 @@ modbus_t* modbus_new_tcp_pi(const char *node, const char *service)
1006
1032
dest_size = sizeof (char ) * _MODBUS_TCP_PI_NODE_LENGTH;
1007
1033
ret_size = strlcpy (ctx_tcp_pi->node , node, dest_size);
1008
1034
if (ret_size == 0 ) {
1035
+ #ifdef DEBUG
1009
1036
fprintf (stderr, " The node string is empty\n " );
1037
+ #endif
1010
1038
modbus_free (ctx);
1011
1039
errno = EINVAL;
1012
1040
return NULL ;
1013
1041
}
1014
1042
1015
1043
if (ret_size >= dest_size) {
1044
+ #ifdef DEBUG
1016
1045
fprintf (stderr, " The node string has been truncated\n " );
1046
+ #endif
1017
1047
modbus_free (ctx);
1018
1048
errno = EINVAL;
1019
1049
return NULL ;
@@ -1029,14 +1059,18 @@ modbus_t* modbus_new_tcp_pi(const char *node, const char *service)
1029
1059
}
1030
1060
1031
1061
if (ret_size == 0 ) {
1062
+ #ifdef DEBUG
1032
1063
fprintf (stderr, " The service string is empty\n " );
1064
+ #endif
1033
1065
modbus_free (ctx);
1034
1066
errno = EINVAL;
1035
1067
return NULL ;
1036
1068
}
1037
1069
1038
1070
if (ret_size >= dest_size) {
1071
+ #ifdef DEBUG
1039
1072
fprintf (stderr, " The service string has been truncated\n " );
1073
+ #endif
1040
1074
modbus_free (ctx);
1041
1075
errno = EINVAL;
1042
1076
return NULL ;
0 commit comments