Commit e8ac540771dd91f0900e2c24f9fff02f6e808237
1 parent
8a96bcb6
Reduce the number of params in functions to enhance the speed of
execution.
Showing
1 changed file
with
54 additions
and
45 deletions
modbus/modbus.c
| @@ -52,6 +52,14 @@ | @@ -52,6 +52,14 @@ | ||
| 52 | 52 | ||
| 53 | #define UNKNOWN_ERROR_MSG "Not defined in modbus specification" | 53 | #define UNKNOWN_ERROR_MSG "Not defined in modbus specification" |
| 54 | 54 | ||
| 55 | +/* This structure reduces the number of params in functions and so | ||
| 56 | + * optimizes the speed of execution (~ 37%). */ | ||
| 57 | +typedef struct { | ||
| 58 | + int slave; | ||
| 59 | + int function; | ||
| 60 | + int t_id; | ||
| 61 | +} sft_t; | ||
| 62 | + | ||
| 55 | static const uint8_t NB_TAB_ERROR_MSG = 12; | 63 | static const uint8_t NB_TAB_ERROR_MSG = 12; |
| 56 | static const char *TAB_ERROR_MSG[] = { | 64 | static const char *TAB_ERROR_MSG[] = { |
| 57 | /* 0x00 */ UNKNOWN_ERROR_MSG, | 65 | /* 0x00 */ UNKNOWN_ERROR_MSG, |
| @@ -254,23 +262,23 @@ static int build_query_basis(modbus_param_t *mb_param, int slave, | @@ -254,23 +262,23 @@ static int build_query_basis(modbus_param_t *mb_param, int slave, | ||
| 254 | } | 262 | } |
| 255 | 263 | ||
| 256 | /* Builds a RTU response header */ | 264 | /* Builds a RTU response header */ |
| 257 | -static int build_response_basis_rtu(int slave, int function, uint8_t *response) | 265 | +static int build_response_basis_rtu(sft_t *sft, uint8_t *response) |
| 258 | { | 266 | { |
| 259 | - response[0] = slave; | ||
| 260 | - response[1] = function; | 267 | + response[0] = sft->slave; |
| 268 | + response[1] = sft->function; | ||
| 261 | 269 | ||
| 262 | return PRESET_RESPONSE_LENGTH_RTU; | 270 | return PRESET_RESPONSE_LENGTH_RTU; |
| 263 | } | 271 | } |
| 264 | 272 | ||
| 265 | /* Builds a TCP response header */ | 273 | /* Builds a TCP response header */ |
| 266 | -static int build_response_basis_tcp(int slave, int function, uint8_t *response, int t_id) | 274 | +static int build_response_basis_tcp(sft_t *sft, uint8_t *response) |
| 267 | { | 275 | { |
| 268 | /* Extract from MODBUS Messaging on TCP/IP Implementation | 276 | /* Extract from MODBUS Messaging on TCP/IP Implementation |
| 269 | Guide V1.0b (page 23/46): | 277 | Guide V1.0b (page 23/46): |
| 270 | The transaction identifier is used to associate the future | 278 | The transaction identifier is used to associate the future |
| 271 | response with the request. */ | 279 | response with the request. */ |
| 272 | - response[0] = t_id >> 8; | ||
| 273 | - response[1] = t_id & 0x00ff; | 280 | + response[0] = sft->t_id >> 8; |
| 281 | + response[1] = sft->t_id & 0x00ff; | ||
| 274 | 282 | ||
| 275 | /* Protocol Modbus */ | 283 | /* Protocol Modbus */ |
| 276 | response[2] = 0; | 284 | response[2] = 0; |
| @@ -278,19 +286,19 @@ static int build_response_basis_tcp(int slave, int function, uint8_t *response, | @@ -278,19 +286,19 @@ static int build_response_basis_tcp(int slave, int function, uint8_t *response, | ||
| 278 | 286 | ||
| 279 | /* Length to fix later with set_message_length_tcp (4 and 5) */ | 287 | /* Length to fix later with set_message_length_tcp (4 and 5) */ |
| 280 | 288 | ||
| 281 | - response[6] = slave; | ||
| 282 | - response[7] = function; | 289 | + response[6] = sft->slave; |
| 290 | + response[7] = sft->function; | ||
| 283 | 291 | ||
| 284 | return PRESET_RESPONSE_LENGTH_TCP; | 292 | return PRESET_RESPONSE_LENGTH_TCP; |
| 285 | } | 293 | } |
| 286 | 294 | ||
| 287 | -static int build_response_basis(modbus_param_t *mb_param, int slave, | ||
| 288 | - int function, uint8_t *response, int t_id) | 295 | +static int build_response_basis(modbus_param_t *mb_param, sft_t *sft, |
| 296 | + uint8_t *response) | ||
| 289 | { | 297 | { |
| 290 | if (mb_param->type_com == RTU) | 298 | if (mb_param->type_com == RTU) |
| 291 | - return build_response_basis_rtu(slave, function, response); | 299 | + return build_response_basis_rtu(sft, response); |
| 292 | else | 300 | else |
| 293 | - return build_response_basis_tcp(slave, function, response, t_id); | 301 | + return build_response_basis_tcp(sft, response); |
| 294 | } | 302 | } |
| 295 | 303 | ||
| 296 | /* Sets the length of TCP message in the message (query and response) */ | 304 | /* Sets the length of TCP message in the message (query and response) */ |
| @@ -698,15 +706,14 @@ static int response_io_status(int address, int count, | @@ -698,15 +706,14 @@ static int response_io_status(int address, int count, | ||
| 698 | } | 706 | } |
| 699 | 707 | ||
| 700 | /* Build the exception response */ | 708 | /* Build the exception response */ |
| 701 | -static int response_exception(modbus_param_t *mb_param, int slave, | ||
| 702 | - int function, int exception_code, | ||
| 703 | - uint8_t *response, int t_id) | 709 | +static int response_exception(modbus_param_t *mb_param, sft_t *sft, |
| 710 | + int exception_code, uint8_t *response) | ||
| 704 | { | 711 | { |
| 705 | int response_length; | 712 | int response_length; |
| 706 | 713 | ||
| 707 | - response_length = build_response_basis(mb_param, slave, | ||
| 708 | - function + 0x80, response, | ||
| 709 | - t_id); | 714 | + sft->function = sft->function + 0x80; |
| 715 | + response_length = build_response_basis(mb_param, sft, response); | ||
| 716 | + | ||
| 710 | /* Positive exception code */ | 717 | /* Positive exception code */ |
| 711 | response[response_length++] = -exception_code; | 718 | response[response_length++] = -exception_code; |
| 712 | 719 | ||
| @@ -727,12 +734,14 @@ void manage_query(modbus_param_t *mb_param, uint8_t *query, | @@ -727,12 +734,14 @@ void manage_query(modbus_param_t *mb_param, uint8_t *query, | ||
| 727 | uint16_t address = (query[offset+2] << 8) + query[offset+3]; | 734 | uint16_t address = (query[offset+2] << 8) + query[offset+3]; |
| 728 | uint8_t response[MAX_MESSAGE_LENGTH]; | 735 | uint8_t response[MAX_MESSAGE_LENGTH]; |
| 729 | int resp_length = 0; | 736 | int resp_length = 0; |
| 730 | - int t_id; | 737 | + sft_t sft; |
| 731 | 738 | ||
| 739 | + sft.slave = slave; | ||
| 740 | + sft.function = function; | ||
| 732 | if (mb_param->type_com == TCP) | 741 | if (mb_param->type_com == TCP) |
| 733 | - t_id = (query[0] << 8) + query[1]; | 742 | + sft.t_id = (query[0] << 8) + query[1]; |
| 734 | else | 743 | else |
| 735 | - t_id = 0; | 744 | + sft.t_id = 0; |
| 736 | 745 | ||
| 737 | switch (function) { | 746 | switch (function) { |
| 738 | case FC_READ_COIL_STATUS: { | 747 | case FC_READ_COIL_STATUS: { |
| @@ -741,10 +750,10 @@ void manage_query(modbus_param_t *mb_param, uint8_t *query, | @@ -741,10 +750,10 @@ void manage_query(modbus_param_t *mb_param, uint8_t *query, | ||
| 741 | if ((address + count) > mb_mapping->nb_coil_status) { | 750 | if ((address + count) > mb_mapping->nb_coil_status) { |
| 742 | printf("Illegal data address %0X in read_coil_status\n", | 751 | printf("Illegal data address %0X in read_coil_status\n", |
| 743 | address + count); | 752 | address + count); |
| 744 | - resp_length = response_exception(mb_param, slave, function, | ||
| 745 | - ILLEGAL_DATA_ADDRESS, response, t_id); | 753 | + resp_length = response_exception(mb_param, &sft, |
| 754 | + ILLEGAL_DATA_ADDRESS, response); | ||
| 746 | } else { | 755 | } else { |
| 747 | - resp_length = build_response_basis(mb_param, slave, function, response, t_id); | 756 | + resp_length = build_response_basis(mb_param, &sft, response); |
| 748 | response[resp_length++] = (count / 8) + ((count % 8) ? 1 : 0); | 757 | response[resp_length++] = (count / 8) + ((count % 8) ? 1 : 0); |
| 749 | resp_length = response_io_status(address, count, | 758 | resp_length = response_io_status(address, count, |
| 750 | mb_mapping->tab_coil_status, | 759 | mb_mapping->tab_coil_status, |
| @@ -759,10 +768,10 @@ void manage_query(modbus_param_t *mb_param, uint8_t *query, | @@ -759,10 +768,10 @@ void manage_query(modbus_param_t *mb_param, uint8_t *query, | ||
| 759 | if ((address + count) > mb_mapping->nb_input_status) { | 768 | if ((address + count) > mb_mapping->nb_input_status) { |
| 760 | printf("Illegal data address %0X in read_input_status\n", | 769 | printf("Illegal data address %0X in read_input_status\n", |
| 761 | address + count); | 770 | address + count); |
| 762 | - resp_length = response_exception(mb_param, slave, function, | ||
| 763 | - ILLEGAL_DATA_ADDRESS, response, t_id); | 771 | + resp_length = response_exception(mb_param, &sft, |
| 772 | + ILLEGAL_DATA_ADDRESS, response); | ||
| 764 | } else { | 773 | } else { |
| 765 | - resp_length = build_response_basis(mb_param, slave, function, response, t_id); | 774 | + resp_length = build_response_basis(mb_param, &sft, response); |
| 766 | response[resp_length++] = (count / 8) + ((count % 8) ? 1 : 0); | 775 | response[resp_length++] = (count / 8) + ((count % 8) ? 1 : 0); |
| 767 | resp_length = response_io_status(address, count, | 776 | resp_length = response_io_status(address, count, |
| 768 | mb_mapping->tab_input_status, | 777 | mb_mapping->tab_input_status, |
| @@ -776,12 +785,12 @@ void manage_query(modbus_param_t *mb_param, uint8_t *query, | @@ -776,12 +785,12 @@ void manage_query(modbus_param_t *mb_param, uint8_t *query, | ||
| 776 | if ((address + count) > mb_mapping->nb_holding_registers) { | 785 | if ((address + count) > mb_mapping->nb_holding_registers) { |
| 777 | printf("Illegal data address %0X in read_holding_registers\n", | 786 | printf("Illegal data address %0X in read_holding_registers\n", |
| 778 | address + count); | 787 | address + count); |
| 779 | - resp_length = response_exception(mb_param, slave, function, | ||
| 780 | - ILLEGAL_DATA_ADDRESS, response, t_id); | 788 | + resp_length = response_exception(mb_param, &sft, |
| 789 | + ILLEGAL_DATA_ADDRESS, response); | ||
| 781 | } else { | 790 | } else { |
| 782 | int i; | 791 | int i; |
| 783 | 792 | ||
| 784 | - resp_length = build_response_basis(mb_param, slave, function, response, t_id); | 793 | + resp_length = build_response_basis(mb_param, &sft, response); |
| 785 | response[resp_length++] = count << 1; | 794 | response[resp_length++] = count << 1; |
| 786 | for (i = address; i < address + count; i++) { | 795 | for (i = address; i < address + count; i++) { |
| 787 | response[resp_length++] = mb_mapping->tab_holding_registers[i] >> 8; | 796 | response[resp_length++] = mb_mapping->tab_holding_registers[i] >> 8; |
| @@ -797,12 +806,12 @@ void manage_query(modbus_param_t *mb_param, uint8_t *query, | @@ -797,12 +806,12 @@ void manage_query(modbus_param_t *mb_param, uint8_t *query, | ||
| 797 | if ((address + count) > mb_mapping->nb_input_registers) { | 806 | if ((address + count) > mb_mapping->nb_input_registers) { |
| 798 | printf("Illegal data address %0X in read_input_registers\n", | 807 | printf("Illegal data address %0X in read_input_registers\n", |
| 799 | address + count); | 808 | address + count); |
| 800 | - resp_length = response_exception(mb_param, slave, function, | ||
| 801 | - ILLEGAL_DATA_ADDRESS, response, t_id); | 809 | + resp_length = response_exception(mb_param, &sft, |
| 810 | + ILLEGAL_DATA_ADDRESS, response); | ||
| 802 | } else { | 811 | } else { |
| 803 | int i; | 812 | int i; |
| 804 | 813 | ||
| 805 | - resp_length = build_response_basis(mb_param, slave, function, response, t_id); | 814 | + resp_length = build_response_basis(mb_param, &sft, response); |
| 806 | response[resp_length++] = count << 1; | 815 | response[resp_length++] = count << 1; |
| 807 | for (i = address; i < address + count; i++) { | 816 | for (i = address; i < address + count; i++) { |
| 808 | response[resp_length++] = mb_mapping->tab_input_registers[i] >> 8; | 817 | response[resp_length++] = mb_mapping->tab_input_registers[i] >> 8; |
| @@ -814,8 +823,8 @@ void manage_query(modbus_param_t *mb_param, uint8_t *query, | @@ -814,8 +823,8 @@ void manage_query(modbus_param_t *mb_param, uint8_t *query, | ||
| 814 | case FC_FORCE_SINGLE_COIL: | 823 | case FC_FORCE_SINGLE_COIL: |
| 815 | if (address >= mb_mapping->nb_coil_status) { | 824 | if (address >= mb_mapping->nb_coil_status) { |
| 816 | printf("Illegal data address %0X in force_singe_coil\n", address); | 825 | printf("Illegal data address %0X in force_singe_coil\n", address); |
| 817 | - resp_length = response_exception(mb_param, slave, function, | ||
| 818 | - ILLEGAL_DATA_ADDRESS, response, t_id); | 826 | + resp_length = response_exception(mb_param, &sft, |
| 827 | + ILLEGAL_DATA_ADDRESS, response); | ||
| 819 | } else { | 828 | } else { |
| 820 | int data = (query[offset+4] << 8) + query[offset+5]; | 829 | int data = (query[offset+4] << 8) + query[offset+5]; |
| 821 | 830 | ||
| @@ -829,16 +838,16 @@ void manage_query(modbus_param_t *mb_param, uint8_t *query, | @@ -829,16 +838,16 @@ void manage_query(modbus_param_t *mb_param, uint8_t *query, | ||
| 829 | } else { | 838 | } else { |
| 830 | printf("Illegal data value %0X in force_single_coil request at address %0X\n", | 839 | printf("Illegal data value %0X in force_single_coil request at address %0X\n", |
| 831 | data, address); | 840 | data, address); |
| 832 | - resp_length = response_exception(mb_param, slave, function, | ||
| 833 | - ILLEGAL_DATA_VALUE, response, t_id); | 841 | + resp_length = response_exception(mb_param, &sft, |
| 842 | + ILLEGAL_DATA_VALUE, response); | ||
| 834 | } | 843 | } |
| 835 | } | 844 | } |
| 836 | break; | 845 | break; |
| 837 | case FC_PRESET_SINGLE_REGISTER: | 846 | case FC_PRESET_SINGLE_REGISTER: |
| 838 | if (address >= mb_mapping->nb_holding_registers) { | 847 | if (address >= mb_mapping->nb_holding_registers) { |
| 839 | printf("Illegal data address %0X in preset_holding_register\n", address); | 848 | printf("Illegal data address %0X in preset_holding_register\n", address); |
| 840 | - resp_length = response_exception(mb_param, slave, function, | ||
| 841 | - ILLEGAL_DATA_ADDRESS, response, t_id); | 849 | + resp_length = response_exception(mb_param, &sft, |
| 850 | + ILLEGAL_DATA_ADDRESS, response); | ||
| 842 | } else { | 851 | } else { |
| 843 | int data = (query[offset+4] << 8) + query[offset+5]; | 852 | int data = (query[offset+4] << 8) + query[offset+5]; |
| 844 | 853 | ||
| @@ -853,10 +862,10 @@ void manage_query(modbus_param_t *mb_param, uint8_t *query, | @@ -853,10 +862,10 @@ void manage_query(modbus_param_t *mb_param, uint8_t *query, | ||
| 853 | if ((address + count) > mb_mapping->nb_coil_status) { | 862 | if ((address + count) > mb_mapping->nb_coil_status) { |
| 854 | printf("Illegal data address %0X in force_multiple_coils\n", | 863 | printf("Illegal data address %0X in force_multiple_coils\n", |
| 855 | address + count); | 864 | address + count); |
| 856 | - resp_length = response_exception(mb_param, slave, function, | ||
| 857 | - ILLEGAL_DATA_ADDRESS, response, t_id); | 865 | + resp_length = response_exception(mb_param, &sft, |
| 866 | + ILLEGAL_DATA_ADDRESS, response); | ||
| 858 | } else { | 867 | } else { |
| 859 | - resp_length = build_response_basis(mb_param, slave, function, response, t_id); | 868 | + resp_length = build_response_basis(mb_param, &sft, response); |
| 860 | /* 4 to copy the coil address (2) and the quantity of coils */ | 869 | /* 4 to copy the coil address (2) and the quantity of coils */ |
| 861 | memcpy(response + resp_length, query + resp_length, 4); | 870 | memcpy(response + resp_length, query + resp_length, 4); |
| 862 | resp_length += 4; | 871 | resp_length += 4; |
| @@ -869,10 +878,10 @@ void manage_query(modbus_param_t *mb_param, uint8_t *query, | @@ -869,10 +878,10 @@ void manage_query(modbus_param_t *mb_param, uint8_t *query, | ||
| 869 | if ((address + count) > mb_mapping->nb_holding_registers) { | 878 | if ((address + count) > mb_mapping->nb_holding_registers) { |
| 870 | printf("Illegal data address %0X in preset_multiple_registers\n", | 879 | printf("Illegal data address %0X in preset_multiple_registers\n", |
| 871 | address + count); | 880 | address + count); |
| 872 | - resp_length = response_exception(mb_param, slave, function, | ||
| 873 | - ILLEGAL_DATA_ADDRESS, response, t_id); | 881 | + resp_length = response_exception(mb_param, &sft, |
| 882 | + ILLEGAL_DATA_ADDRESS, response); | ||
| 874 | } else { | 883 | } else { |
| 875 | - resp_length = build_response_basis(mb_param, slave, function, response, t_id); | 884 | + resp_length = build_response_basis(mb_param, &sft, response); |
| 876 | /* 4 to copy the address (2) and the no. of registers */ | 885 | /* 4 to copy the address (2) and the no. of registers */ |
| 877 | memcpy(response + resp_length, query + resp_length, 4); | 886 | memcpy(response + resp_length, query + resp_length, 4); |
| 878 | resp_length += 4; | 887 | resp_length += 4; |