Commit f33e5b9ff8cf53db9b4eda3328abd5ec01cbbe65

Authored by Stéphane Raimbault
1 parent 0a671c23

Avoid duplicated code to compute the header_length

Showing 1 changed file with 31 additions and 48 deletions
src/modbus.c
... ... @@ -483,51 +483,43 @@ static int send_msg(modbus_t *ctx, uint8_t *req, int req_length)
483 483 return rc;
484 484 }
485 485  
486   -/* Computes the length of the header following the function code */
487   -static uint8_t compute_indication_length_header(int function)
488   -{
489   - int length;
490   -
491   - if (function <= FC_WRITE_SINGLE_COIL ||
492   - function == FC_WRITE_SINGLE_REGISTER)
493   - /* Read and single write */
494   - length = 4;
495   - else if (function == FC_WRITE_MULTIPLE_COILS ||
496   - function == FC_WRITE_MULTIPLE_REGISTERS)
497   - /* Multiple write */
498   - length = 5;
499   - else if (function == FC_REPORT_SLAVE_ID)
500   - length = 0;
501   - else
502   - length = 0;
  486 +/*
  487 + ---------- Request Indication ----------
  488 + | Client | ---------------------->| Server |
  489 + ---------- Confirmation Response ----------
  490 +*/
503 491  
504   - return length;
505   -}
  492 +typedef enum {
  493 + /* Request message on the server side */
  494 + MSG_INDICATION,
  495 + /* Request message on the client side */
  496 + MSG_CONFIRMATION
  497 +} msg_type_t;
506 498  
507   -/* Computes the length of the header following the function code */
508   -static uint8_t compute_confirmation_length_header(int function)
  499 +/* Computes the header length (to reach the the function code) */
  500 +static uint8_t compute_header_length(int function, msg_type_t msg_type)
509 501 {
510 502 int length;
511 503  
512 504 if (function <= FC_WRITE_SINGLE_COIL ||
513   - function == FC_WRITE_SINGLE_REGISTER)
514   - /* Read and single write */
  505 + function == FC_WRITE_SINGLE_REGISTER) {
515 506 length = 4;
516   - else if (function == FC_WRITE_MULTIPLE_COILS ||
517   - function == FC_WRITE_MULTIPLE_REGISTERS)
518   - /* Multiple write */
  507 + } else if (function == FC_WRITE_MULTIPLE_COILS ||
  508 + function == FC_WRITE_MULTIPLE_REGISTERS) {
519 509 length = 5;
520   - else if (function == FC_REPORT_SLAVE_ID)
521   - /* Report slave_ID */
522   - length = 1;
523   - else
  510 + } else if (function == FC_REPORT_SLAVE_ID) {
  511 + if (msg_type == MSG_INDICATION)
  512 + length = 0;
  513 + else
  514 + length = 1;
  515 + } else {
524 516 length = 0;
525   -
  517 + }
526 518 return length;
527 519 }
528 520  
529 521 /* Computes the length of the data to write in the request */
530   -static int compute_msg_length_data(modbus_t *ctx, uint8_t *msg)
  522 +static int compute_data_length(modbus_t *ctx, uint8_t *msg)
531 523 {
532 524 int function = msg[TAB_HEADER_LENGTH[ctx->type_com]];
533 525 int length;
... ... @@ -600,14 +592,9 @@ static int compute_msg_length_data(modbus_t *ctx, uint8_t *msg)
600 592 - ETIMEDOUT
601 593 - read() or recv() error codes
602 594 */
603   -enum {
604   - /* Request message on the server side */
605   - MSG_INDICATION,
606   - /* Request message on the client side */
607   - MSG_CONFIRMATION
608   -};
609 595  
610   -static int receive_msg(modbus_t *ctx, int msg_length_computed, uint8_t *msg, int type)
  596 +static int receive_msg(modbus_t *ctx, int msg_length_computed,
  597 + uint8_t *msg, msg_type_t msg_type)
611 598 {
612 599 int s_rc;
613 600 int read_rc;
... ... @@ -620,7 +607,7 @@ static int receive_msg(modbus_t *ctx, int msg_length_computed, uint8_t *msg, int
620 607 int msg_length = 0;
621 608  
622 609 if (ctx->debug) {
623   - if (type == MSG_INDICATION) {
  610 + if (msg_type == MSG_INDICATION) {
624 611 printf("Waiting for a indication");
625 612 } else {
626 613 printf("Waiting for a confirmation");
... ... @@ -700,13 +687,9 @@ static int receive_msg(modbus_t *ctx, int msg_length_computed, uint8_t *msg, int
700 687 switch (state) {
701 688 case FUNCTION:
702 689 /* Function code position */
703   - if (type == MSG_INDICATION) {
704   - length_to_read = compute_indication_length_header(
705   - msg[TAB_HEADER_LENGTH[ctx->type_com]]);
706   - } else {
707   - length_to_read = compute_confirmation_length_header(
708   - msg[TAB_HEADER_LENGTH[ctx->type_com]]);
709   - }
  690 + length_to_read = compute_header_length(
  691 + msg[TAB_HEADER_LENGTH[ctx->type_com]],
  692 + msg_type);
710 693 msg_length_computed += length_to_read;
711 694 /* It's useless to check the value of
712 695 msg_length_computed in this case (only
... ... @@ -714,7 +697,7 @@ static int receive_msg(modbus_t *ctx, int msg_length_computed, uint8_t *msg, int
714 697 state = DATA;
715 698 break;
716 699 case DATA:
717   - length_to_read = compute_msg_length_data(ctx, msg);
  700 + length_to_read = compute_data_length(ctx, msg);
718 701 msg_length_computed += length_to_read;
719 702 if (msg_length_computed > TAB_MAX_ADU_LENGTH[ctx->type_com]) {
720 703 errno = EMBBADDATA;
... ...