Commit 53c93ae21370291ee1c0542a359e9260e23cd6a5

Authored by Stéphane Raimbault
1 parent 10263cd1

Fix - Check the return value of modbus_check_response

- reduce the number of variables to store return values (status,
  query_ret and response_ret -> ret)
- add some comments
modbus/modbus.c
... ... @@ -453,7 +453,8 @@ static int compute_query_length_data(modbus_param_t *mb_param, uint8_t *msg)
453 453 from a modbus master.
454 454 This function blocks for timeout seconds if there is no reply.
455 455  
456   - msg_length_computed must be set to MSG_LENGTH_COMPUTED if undefined
  456 + - msg is an array of uint8_t to receive the message
  457 + - msg_length_computed must be set to MSG_LENGTH_UNDEFINED if undefined
457 458  
458 459 Returns a negative number if an error occured.
459 460 The variable msg_length is assigned to the number of characters
... ... @@ -491,7 +492,7 @@ int receive_msg(modbus_param_t *mb_param,
491 492 /* The message length is undefined (query receiving) so
492 493 * we need to analyse the message step by step.
493 494 * At the first step, we want to reach the function
494   - * code because all packets have this information. */
  495 + * code because all packets have that information. */
495 496 msg_length_computed = mb_param->header_length + 2;
496 497 state = FUNCTION;
497 498 } else {
... ... @@ -588,7 +589,7 @@ int receive_msg(modbus_param_t *mb_param,
588 589 }
589 590  
590 591  
591   -/* Checks whether the right response is returned with good checksum.
  592 +/* Checks the right response is returned with good checksum.
592 593  
593 594 Returns:
594 595 - the numbers of values (bits or word) if success
... ... @@ -609,7 +610,8 @@ static int modbus_check_response(modbus_param_t *mb_param,
609 610 ret = receive_msg(mb_param, response_length_computed,
610 611 response, &response_length);
611 612 if (ret == 0) {
612   - /* Good response */
  613 + /* GOOD RESPONSE */
  614 +
613 615 switch (response[offset + 1]) {
614 616 case FC_READ_COIL_STATUS:
615 617 case FC_READ_INPUT_STATUS:
... ... @@ -637,6 +639,8 @@ static int modbus_check_response(modbus_param_t *mb_param,
637 639  
638 640 } else if (ret == COMM_TIME_OUT &&
639 641 response_length == offset + 3 + mb_param->checksum_length) {
  642 + /* EXCEPTION CODE RECEIVED */
  643 +
640 644 /* Optimization allowed because exception response is
641 645 the smallest trame in modbus protocol (3) so always
642 646 raise a timeout error */
... ... @@ -675,9 +679,11 @@ static int modbus_check_response(modbus_param_t *mb_param,
675 679 }
676 680 }
677 681 } else if (ret == COMM_TIME_OUT) {
  682 + /* COMMUNICATION TIME OUT */
678 683 error_treat(mb_param, ret, "Communication time out");
679 684 return ret;
680 685 } else {
  686 + /* OTHER */
681 687 return ret;
682 688 }
683 689  
... ... @@ -929,9 +935,8 @@ int modbus_listen(modbus_param_t *mb_param, uint8_t *query, int *query_length)
929 935 static int read_io_status(modbus_param_t *mb_param, int slave, int function,
930 936 int start_addr, int nb, uint8_t *data_dest)
931 937 {
  938 + int ret;
932 939 int query_length;
933   - int query_ret;
934   - int response_ret;
935 940  
936 941 uint8_t query[MIN_QUERY_LENGTH];
937 942 uint8_t response[MAX_MESSAGE_LENGTH];
... ... @@ -939,18 +944,21 @@ static int read_io_status(modbus_param_t *mb_param, int slave, int function,
939 944 query_length = build_query_basis(mb_param, slave, function,
940 945 start_addr, nb, query);
941 946  
942   - query_ret = modbus_send(mb_param, query, query_length);
943   - if (query_ret > 0) {
  947 + ret = modbus_send(mb_param, query, query_length);
  948 + if (ret > 0) {
944 949 int i, temp, bit;
945 950 int pos = 0;
946 951 int processed = 0;
947 952 int offset;
948 953 int offset_length;
949 954  
950   - response_ret = modbus_check_response(mb_param, query, response);
  955 + ret = modbus_check_response(mb_param, query, response);
  956 + if (ret < 0)
  957 + return ret;
  958 +
951 959 offset = mb_param->header_length;
952 960  
953   - offset_length = offset + response_ret;
  961 + offset_length = offset + ret;
954 962 for (i = offset; i < offset_length; i++) {
955 963 /* Shift reg hi_byte to temp */
956 964 temp = response[3 + i];
... ... @@ -962,11 +970,9 @@ static int read_io_status(modbus_param_t *mb_param, int slave, int function,
962 970 }
963 971  
964 972 }
965   - } else {
966   - response_ret = query_ret;
967 973 }
968 974  
969   - return response_ret;
  975 + return ret;
970 976 }
971 977  
972 978 /* Reads the boolean status of coils and sets the array elements
... ... @@ -1017,9 +1023,8 @@ int read_input_status(modbus_param_t *mb_param, int slave, int start_addr,
1017 1023 static int read_registers(modbus_param_t *mb_param, int slave, int function,
1018 1024 int start_addr, int nb, uint16_t *data_dest)
1019 1025 {
  1026 + int ret;
1020 1027 int query_length;
1021   - int status;
1022   - int query_ret;
1023 1028 uint8_t query[MIN_QUERY_LENGTH];
1024 1029  
1025 1030 if (nb > MAX_REGISTERS) {
... ... @@ -1031,13 +1036,11 @@ static int read_registers(modbus_param_t *mb_param, int slave, int function,
1031 1036 query_length = build_query_basis(mb_param, slave, function,
1032 1037 start_addr, nb, query);
1033 1038  
1034   - query_ret = modbus_send(mb_param, query, query_length);
1035   - if (query_ret > 0)
1036   - status = read_reg_response(mb_param, data_dest, query);
1037   - else
1038   - status = query_ret;
  1039 + ret = modbus_send(mb_param, query, query_length);
  1040 + if (ret > 0)
  1041 + ret = read_reg_response(mb_param, data_dest, query);
1039 1042  
1040   - return status;
  1043 + return ret;
1041 1044 }
1042 1045  
1043 1046 /* Reads the holding registers in a slave and put the data into an
... ... @@ -1116,21 +1119,18 @@ static int preset_response(modbus_param_t *mb_param, uint8_t *query)
1116 1119 static int set_single(modbus_param_t *mb_param, int slave, int function,
1117 1120 int addr, int value)
1118 1121 {
1119   - int status;
  1122 + int ret;
1120 1123 int query_length;
1121   - int query_ret;
1122 1124 uint8_t query[MAX_MESSAGE_LENGTH];
1123 1125  
1124 1126 query_length = build_query_basis(mb_param, slave, function,
1125 1127 addr, value, query);
1126 1128  
1127   - query_ret = modbus_send(mb_param, query, query_length);
1128   - if (query_ret > 0)
1129   - status = preset_response(mb_param, query);
1130   - else
1131   - status = query_ret;
  1129 + ret = modbus_send(mb_param, query, query_length);
  1130 + if (ret > 0)
  1131 + ret = preset_response(mb_param, query);
1132 1132  
1133   - return status;
  1133 + return ret;
1134 1134 }
1135 1135  
1136 1136  
... ... @@ -1166,12 +1166,11 @@ int force_multiple_coils(modbus_param_t *mb_param, int slave,
1166 1166 int start_addr, int nb,
1167 1167 const uint8_t *data_src)
1168 1168 {
  1169 + int ret;
1169 1170 int i;
1170 1171 int byte_count;
1171 1172 int query_length;
1172 1173 int coil_check = 0;
1173   - int status;
1174   - int query_ret;
1175 1174 int pos = 0;
1176 1175  
1177 1176 uint8_t query[MAX_MESSAGE_LENGTH];
... ... @@ -1205,13 +1204,11 @@ int force_multiple_coils(modbus_param_t *mb_param, int slave,
1205 1204 query_length++;
1206 1205 }
1207 1206  
1208   - query_ret = modbus_send(mb_param, query, query_length);
1209   - if (query_ret > 0)
1210   - status = preset_response(mb_param, query);
1211   - else
1212   - status = query_ret;
  1207 + ret = modbus_send(mb_param, query, query_length);
  1208 + if (ret > 0)
  1209 + ret = preset_response(mb_param, query);
1213 1210  
1214   - return status;
  1211 + return ret;
1215 1212 }
1216 1213  
1217 1214 /* Copies the values in the slave from the array given in argument */
... ... @@ -1219,11 +1216,10 @@ int preset_multiple_registers(modbus_param_t *mb_param, int slave,
1219 1216 int start_addr, int nb,
1220 1217 const uint16_t *data_src)
1221 1218 {
  1219 + int ret;
1222 1220 int i;
1223 1221 int query_length;
1224 1222 int byte_count;
1225   - int status;
1226   - int query_ret;
1227 1223  
1228 1224 uint8_t query[MAX_MESSAGE_LENGTH];
1229 1225  
... ... @@ -1244,22 +1240,19 @@ int preset_multiple_registers(modbus_param_t *mb_param, int slave,
1244 1240 query[query_length++] = data_src[i] & 0x00FF;
1245 1241 }
1246 1242  
1247   - query_ret = modbus_send(mb_param, query, query_length);
1248   - if (query_ret > 0)
1249   - status = preset_response(mb_param, query);
1250   - else
1251   - status = query_ret;
  1243 + ret = modbus_send(mb_param, query, query_length);
  1244 + if (ret > 0)
  1245 + ret = preset_response(mb_param, query);
1252 1246  
1253   - return status;
  1247 + return ret;
1254 1248 }
1255 1249  
1256 1250 /* Returns the slave id! */
1257 1251 int report_slave_id(modbus_param_t *mb_param, int slave,
1258 1252 uint8_t *data_dest)
1259 1253 {
  1254 + int ret;
1260 1255 int query_length;
1261   - int query_ret;
1262   - int response_ret;
1263 1256  
1264 1257 uint8_t query[MIN_QUERY_LENGTH];
1265 1258 uint8_t response[MAX_MESSAGE_LENGTH];
... ... @@ -1270,26 +1263,26 @@ int report_slave_id(modbus_param_t *mb_param, int slave,
1270 1263 /* start_addr and count are not used */
1271 1264 query_length -= 4;
1272 1265  
1273   - query_ret = modbus_send(mb_param, query, query_length);
1274   - if (query_ret > 0) {
  1266 + ret = modbus_send(mb_param, query, query_length);
  1267 + if (ret > 0) {
1275 1268 int i;
1276 1269 int offset;
1277 1270 int offset_length;
1278 1271  
1279 1272 /* Byte count, slave id, run indicator status,
1280 1273 additional data */
1281   - response_ret = modbus_check_response(mb_param, query, response);
1282   -
  1274 + ret = modbus_check_response(mb_param, query, response);
  1275 + if (ret < 0)
  1276 + return ret;
  1277 +
1283 1278 offset = mb_param->header_length;
1284   - offset_length = offset + response_ret;
  1279 + offset_length = offset + ret;
1285 1280  
1286 1281 for (i = offset; i < offset_length; i++)
1287 1282 data_dest[i] = response[i];
1288   - } else {
1289   - response_ret = query_ret;
1290 1283 }
1291 1284  
1292   - return response_ret;
  1285 + return ret;
1293 1286 }
1294 1287  
1295 1288 /* Initializes the modbus_param_t structure for RTU
... ...
modbus/modbus.h
... ... @@ -42,6 +42,10 @@ extern &quot;C&quot; {
42 42 /* 8 + HEADER_LENGTH_TCP */
43 43 #define MIN_QUERY_LENGTH 14
44 44  
  45 +/* Page 102, Application Notes of PI–MBUS–300:
  46 + * The maximum length of the entire message must not exceed 256
  47 + * bytes.
  48 + */
45 49 #define MAX_MESSAGE_LENGTH 256
46 50  
47 51 #define MAX_STATUS 800
... ...