Commit e8ac540771dd91f0900e2c24f9fff02f6e808237

Authored by Stéphane Raimbault
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 52  
53 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 63 static const uint8_t NB_TAB_ERROR_MSG = 12;
56 64 static const char *TAB_ERROR_MSG[] = {
57 65 /* 0x00 */ UNKNOWN_ERROR_MSG,
... ... @@ -254,23 +262,23 @@ static int build_query_basis(modbus_param_t *mb_param, int slave,
254 262 }
255 263  
256 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 270 return PRESET_RESPONSE_LENGTH_RTU;
263 271 }
264 272  
265 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 276 /* Extract from MODBUS Messaging on TCP/IP Implementation
269 277 Guide V1.0b (page 23/46):
270 278 The transaction identifier is used to associate the future
271 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 283 /* Protocol Modbus */
276 284 response[2] = 0;
... ... @@ -278,19 +286,19 @@ static int build_response_basis_tcp(int slave, int function, uint8_t *response,
278 286  
279 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 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 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 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 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 706 }
699 707  
700 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 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 717 /* Positive exception code */
711 718 response[response_length++] = -exception_code;
712 719  
... ... @@ -727,12 +734,14 @@ void manage_query(modbus_param_t *mb_param, uint8_t *query,
727 734 uint16_t address = (query[offset+2] << 8) + query[offset+3];
728 735 uint8_t response[MAX_MESSAGE_LENGTH];
729 736 int resp_length = 0;
730   - int t_id;
  737 + sft_t sft;
731 738  
  739 + sft.slave = slave;
  740 + sft.function = function;
732 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 743 else
735   - t_id = 0;
  744 + sft.t_id = 0;
736 745  
737 746 switch (function) {
738 747 case FC_READ_COIL_STATUS: {
... ... @@ -741,10 +750,10 @@ void manage_query(modbus_param_t *mb_param, uint8_t *query,
741 750 if ((address + count) > mb_mapping->nb_coil_status) {
742 751 printf("Illegal data address %0X in read_coil_status\n",
743 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 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 757 response[resp_length++] = (count / 8) + ((count % 8) ? 1 : 0);
749 758 resp_length = response_io_status(address, count,
750 759 mb_mapping->tab_coil_status,
... ... @@ -759,10 +768,10 @@ void manage_query(modbus_param_t *mb_param, uint8_t *query,
759 768 if ((address + count) > mb_mapping->nb_input_status) {
760 769 printf("Illegal data address %0X in read_input_status\n",
761 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 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 775 response[resp_length++] = (count / 8) + ((count % 8) ? 1 : 0);
767 776 resp_length = response_io_status(address, count,
768 777 mb_mapping->tab_input_status,
... ... @@ -776,12 +785,12 @@ void manage_query(modbus_param_t *mb_param, uint8_t *query,
776 785 if ((address + count) > mb_mapping->nb_holding_registers) {
777 786 printf("Illegal data address %0X in read_holding_registers\n",
778 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 790 } else {
782 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 794 response[resp_length++] = count << 1;
786 795 for (i = address; i < address + count; i++) {
787 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 806 if ((address + count) > mb_mapping->nb_input_registers) {
798 807 printf("Illegal data address %0X in read_input_registers\n",
799 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 811 } else {
803 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 815 response[resp_length++] = count << 1;
807 816 for (i = address; i < address + count; i++) {
808 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 823 case FC_FORCE_SINGLE_COIL:
815 824 if (address >= mb_mapping->nb_coil_status) {
816 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 828 } else {
820 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 838 } else {
830 839 printf("Illegal data value %0X in force_single_coil request at address %0X\n",
831 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 845 break;
837 846 case FC_PRESET_SINGLE_REGISTER:
838 847 if (address >= mb_mapping->nb_holding_registers) {
839 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 851 } else {
843 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 862 if ((address + count) > mb_mapping->nb_coil_status) {
854 863 printf("Illegal data address %0X in force_multiple_coils\n",
855 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 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 869 /* 4 to copy the coil address (2) and the quantity of coils */
861 870 memcpy(response + resp_length, query + resp_length, 4);
862 871 resp_length += 4;
... ... @@ -869,10 +878,10 @@ void manage_query(modbus_param_t *mb_param, uint8_t *query,
869 878 if ((address + count) > mb_mapping->nb_holding_registers) {
870 879 printf("Illegal data address %0X in preset_multiple_registers\n",
871 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 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 885 /* 4 to copy the address (2) and the no. of registers */
877 886 memcpy(response + resp_length, query + resp_length, 4);
878 887 resp_length += 4;
... ...