Commit 663084b64ad4dd891a1ec1182e545f9ebf296c70

Authored by Stéphane Raimbault
1 parent dc8ed400

Reorder error_treat arguments (modbu_param_t first)

Showing 1 changed file with 28 additions and 29 deletions
modbus/modbus.c
@@ -135,7 +135,7 @@ static int read_reg_response(modbus_param_t *mb_param, @@ -135,7 +135,7 @@ static int read_reg_response(modbus_param_t *mb_param,
135 uint16_t *data_dest, uint8_t *query); 135 uint16_t *data_dest, uint8_t *query);
136 136
137 /* Treats errors and flush or close connection if necessary */ 137 /* Treats errors and flush or close connection if necessary */
138 -static void error_treat(int code, const char *string, modbus_param_t *mb_param) 138 +static void error_treat(modbus_param_t *mb_param, int code, const char *string)
139 { 139 {
140 printf("\nERROR %s (%d)\n", string, code); 140 printf("\nERROR %s (%d)\n", string, code);
141 141
@@ -340,7 +340,7 @@ int check_crc16(modbus_param_t *mb_param, @@ -340,7 +340,7 @@ int check_crc16(modbus_param_t *mb_param,
340 sprintf(s_error, "invalid crc received %0X - crc_calc %0X", 340 sprintf(s_error, "invalid crc received %0X - crc_calc %0X",
341 crc_received, crc_calc); 341 crc_received, crc_calc);
342 ret = INVALID_CRC; 342 ret = INVALID_CRC;
343 - error_treat(ret, s_error, mb_param); 343 + error_treat(mb_param, ret, s_error);
344 } 344 }
345 } else { 345 } else {
346 /* In TCP, the modbus CRC is not present (see HDLC level) */ 346 /* In TCP, the modbus CRC is not present (see HDLC level) */
@@ -383,7 +383,7 @@ static int modbus_send(modbus_param_t *mb_param, uint8_t *query, @@ -383,7 +383,7 @@ static int modbus_send(modbus_param_t *mb_param, uint8_t *query,
383 or PORT_SOCKET_FAILURE on error */ 383 or PORT_SOCKET_FAILURE on error */
384 if ((ret == -1) || (ret != query_length)) { 384 if ((ret == -1) || (ret != query_length)) {
385 ret = PORT_SOCKET_FAILURE; 385 ret = PORT_SOCKET_FAILURE;
386 - error_treat(ret, "Write port/socket failure", mb_param); 386 + error_treat(mb_param, ret, "Write port/socket failure");
387 } 387 }
388 388
389 return ret; 389 return ret;
@@ -425,25 +425,25 @@ static int compute_query_length_data(modbus_param_t *mb_param, uint8_t *msg) @@ -425,25 +425,25 @@ static int compute_query_length_data(modbus_param_t *mb_param, uint8_t *msg)
425 return length; 425 return length;
426 } 426 }
427 427
428 -#define WAIT_DATA() \  
429 - { \  
430 - while ((select_ret = select(mb_param->fd+1, &rfds, NULL, NULL, &tv)) == -1) { \  
431 - if (errno == EINTR) { \  
432 - printf("A non blocked signal was caught\n"); \  
433 - /* Necessary after an error */ \  
434 - FD_ZERO(&rfds); \  
435 - FD_SET(mb_param->fd, &rfds); \  
436 - } else { \  
437 - error_treat(SELECT_FAILURE, "Select failure", mb_param); \  
438 - return SELECT_FAILURE; \  
439 - } \  
440 - } \  
441 - \  
442 - if (select_ret == 0) { \  
443 - /* Call to error_treat is done later to manage exceptions */ \  
444 - return COMM_TIME_OUT; \  
445 - } \  
446 - } 428 +#define WAIT_DATA() \
  429 +{ \
  430 + while ((select_ret = select(mb_param->fd+1, &rfds, NULL, NULL, &tv)) == -1) { \
  431 + if (errno == EINTR) { \
  432 + printf("A non blocked signal was caught\n"); \
  433 + /* Necessary after an error */ \
  434 + FD_ZERO(&rfds); \
  435 + FD_SET(mb_param->fd, &rfds); \
  436 + } else { \
  437 + error_treat(mb_param, SELECT_FAILURE, "Select failure"); \
  438 + return SELECT_FAILURE; \
  439 + } \
  440 + } \
  441 + \
  442 + if (select_ret == 0) { \
  443 + /* Call to error_treat is done later to manage exceptions */ \
  444 + return COMM_TIME_OUT; \
  445 + } \
  446 +}
447 447
448 /* Monitors for the reply from the modbus slave or to receive query 448 /* Monitors for the reply from the modbus slave or to receive query
449 from a modbus master. 449 from a modbus master.
@@ -511,7 +511,7 @@ int receive_msg(modbus_param_t *mb_param, @@ -511,7 +511,7 @@ int receive_msg(modbus_param_t *mb_param,
511 read_ret = recv(mb_param->fd, p_msg, length_to_read, 0); 511 read_ret = recv(mb_param->fd, p_msg, length_to_read, 0);
512 512
513 if (read_ret == -1) { 513 if (read_ret == -1) {
514 - error_treat(PORT_SOCKET_FAILURE, "Read port/socket failure", mb_param); 514 + error_treat(mb_param, PORT_SOCKET_FAILURE, "Read port/socket failure");
515 return PORT_SOCKET_FAILURE; 515 return PORT_SOCKET_FAILURE;
516 } else if (read_ret == 0) { 516 } else if (read_ret == 0) {
517 printf("Connection closed\n"); 517 printf("Connection closed\n");
@@ -521,7 +521,7 @@ int receive_msg(modbus_param_t *mb_param, @@ -521,7 +521,7 @@ int receive_msg(modbus_param_t *mb_param,
521 /* Sums bytes received */ 521 /* Sums bytes received */
522 (*msg_length) += read_ret; 522 (*msg_length) += read_ret;
523 if ((*msg_length) > MAX_MESSAGE_LENGTH) { 523 if ((*msg_length) > MAX_MESSAGE_LENGTH) {
524 - error_treat(TOO_MANY_DATAS, "Too many datas", mb_param); 524 + error_treat(mb_param, TOO_MANY_DATAS, "Too many datas");
525 return TOO_MANY_DATAS; 525 return TOO_MANY_DATAS;
526 } 526 }
527 527
@@ -649,9 +649,8 @@ static int modbus_check_response(modbus_param_t *mb_param, @@ -649,9 +649,8 @@ static int modbus_check_response(modbus_param_t *mb_param,
649 int exception_code = response[offset + 2]; 649 int exception_code = response[offset + 2];
650 // FIXME check test 650 // FIXME check test
651 if (exception_code < NB_TAB_ERROR_MSG) { 651 if (exception_code < NB_TAB_ERROR_MSG) {
652 - error_treat(-exception_code,  
653 - TAB_ERROR_MSG[response[offset + 2]],  
654 - mb_param); 652 + error_treat(mb_param, -exception_code,
  653 + TAB_ERROR_MSG[response[offset + 2]]);
655 /* Modbus error code is negative */ 654 /* Modbus error code is negative */
656 return -exception_code; 655 return -exception_code;
657 } else { 656 } else {
@@ -660,13 +659,13 @@ static int modbus_check_response(modbus_param_t *mb_param, @@ -660,13 +659,13 @@ static int modbus_check_response(modbus_param_t *mb_param,
660 segfault */ 659 segfault */
661 char s_error[64]; 660 char s_error[64];
662 sprintf(s_error, "Invalid exception code %d", response[offset + 2]); 661 sprintf(s_error, "Invalid exception code %d", response[offset + 2]);
663 - error_treat(INVALID_EXCEPTION_CODE, s_error, mb_param); 662 + error_treat(mb_param, INVALID_EXCEPTION_CODE, s_error);
664 free(s_error); 663 free(s_error);
665 return INVALID_EXCEPTION_CODE; 664 return INVALID_EXCEPTION_CODE;
666 } 665 }
667 } 666 }
668 } else if (ret == COMM_TIME_OUT) { 667 } else if (ret == COMM_TIME_OUT) {
669 - error_treat(ret, "Communication time out", mb_param); 668 + error_treat(mb_param, ret, "Communication time out");
670 return ret; 669 return ret;
671 } else { 670 } else {
672 return ret; 671 return ret;