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 135 uint16_t *data_dest, uint8_t *query);
136 136  
137 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 140 printf("\nERROR %s (%d)\n", string, code);
141 141  
... ... @@ -340,7 +340,7 @@ int check_crc16(modbus_param_t *mb_param,
340 340 sprintf(s_error, "invalid crc received %0X - crc_calc %0X",
341 341 crc_received, crc_calc);
342 342 ret = INVALID_CRC;
343   - error_treat(ret, s_error, mb_param);
  343 + error_treat(mb_param, ret, s_error);
344 344 }
345 345 } else {
346 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 383 or PORT_SOCKET_FAILURE on error */
384 384 if ((ret == -1) || (ret != query_length)) {
385 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 389 return ret;
... ... @@ -425,25 +425,25 @@ static int compute_query_length_data(modbus_param_t *mb_param, uint8_t *msg)
425 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 448 /* Monitors for the reply from the modbus slave or to receive query
449 449 from a modbus master.
... ... @@ -511,7 +511,7 @@ int receive_msg(modbus_param_t *mb_param,
511 511 read_ret = recv(mb_param->fd, p_msg, length_to_read, 0);
512 512  
513 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 515 return PORT_SOCKET_FAILURE;
516 516 } else if (read_ret == 0) {
517 517 printf("Connection closed\n");
... ... @@ -521,7 +521,7 @@ int receive_msg(modbus_param_t *mb_param,
521 521 /* Sums bytes received */
522 522 (*msg_length) += read_ret;
523 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 525 return TOO_MANY_DATAS;
526 526 }
527 527  
... ... @@ -649,9 +649,8 @@ static int modbus_check_response(modbus_param_t *mb_param,
649 649 int exception_code = response[offset + 2];
650 650 // FIXME check test
651 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 654 /* Modbus error code is negative */
656 655 return -exception_code;
657 656 } else {
... ... @@ -660,13 +659,13 @@ static int modbus_check_response(modbus_param_t *mb_param,
660 659 segfault */
661 660 char s_error[64];
662 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 663 free(s_error);
665 664 return INVALID_EXCEPTION_CODE;
666 665 }
667 666 }
668 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 669 return ret;
671 670 } else {
672 671 return ret;
... ...