Commit 50235c4036d2d8bf5d8ecd200fae9381f2d9bbc6
1 parent
ea8b20b1
Use int instead of uint8_t/uint16_t in function arguments.
It's slower to pass non aligned values.
Showing
4 changed files
with
42 additions
and
48 deletions
TODO
modbus/modbus.c
| ... | ... | @@ -191,8 +191,8 @@ static unsigned int compute_response_length(modbus_param_t *mb_param, |
| 191 | 191 | } |
| 192 | 192 | |
| 193 | 193 | /* Builds a RTU query header */ |
| 194 | -static int build_query_basis_rtu(uint8_t slave, uint8_t function, | |
| 195 | - uint16_t start_addr, uint16_t count, | |
| 194 | +static int build_query_basis_rtu(int slave, int function, | |
| 195 | + int start_addr, int count, | |
| 196 | 196 | uint8_t *query) |
| 197 | 197 | { |
| 198 | 198 | query[0] = slave; |
| ... | ... | @@ -206,8 +206,8 @@ static int build_query_basis_rtu(uint8_t slave, uint8_t function, |
| 206 | 206 | } |
| 207 | 207 | |
| 208 | 208 | /* Builds a TCP query header */ |
| 209 | -static int build_query_basis_tcp(uint8_t slave, uint8_t function, | |
| 210 | - uint16_t start_addr, uint16_t count, | |
| 209 | +static int build_query_basis_tcp(int slave, int function, | |
| 210 | + int start_addr, int count, | |
| 211 | 211 | uint8_t *query) |
| 212 | 212 | { |
| 213 | 213 | static uint16_t t_id = 0; |
| ... | ... | @@ -236,9 +236,9 @@ static int build_query_basis_tcp(uint8_t slave, uint8_t function, |
| 236 | 236 | return PRESET_QUERY_LENGTH_TCP; |
| 237 | 237 | } |
| 238 | 238 | |
| 239 | -static int build_query_basis(modbus_param_t *mb_param, uint8_t slave, | |
| 240 | - uint8_t function, uint16_t start_addr, | |
| 241 | - uint16_t count, uint8_t *query) | |
| 239 | +static int build_query_basis(modbus_param_t *mb_param, int slave, | |
| 240 | + int function, int start_addr, | |
| 241 | + int count, uint8_t *query) | |
| 242 | 242 | { |
| 243 | 243 | if (mb_param->type_com == RTU) |
| 244 | 244 | return build_query_basis_rtu(slave, function, start_addr, |
| ... | ... | @@ -249,7 +249,7 @@ static int build_query_basis(modbus_param_t *mb_param, uint8_t slave, |
| 249 | 249 | } |
| 250 | 250 | |
| 251 | 251 | /* Builds a RTU response header */ |
| 252 | -static int build_response_basis_rtu(uint8_t slave, uint8_t function, uint8_t *response) | |
| 252 | +static int build_response_basis_rtu(int slave, int function, uint8_t *response) | |
| 253 | 253 | { |
| 254 | 254 | response[0] = slave; |
| 255 | 255 | response[1] = function; |
| ... | ... | @@ -258,7 +258,7 @@ static int build_response_basis_rtu(uint8_t slave, uint8_t function, uint8_t *re |
| 258 | 258 | } |
| 259 | 259 | |
| 260 | 260 | /* Builds a TCP response header */ |
| 261 | -static int build_response_basis_tcp(uint8_t slave, uint8_t function, uint8_t *response) | |
| 261 | +static int build_response_basis_tcp(int slave, int function, uint8_t *response) | |
| 262 | 262 | { |
| 263 | 263 | static uint16_t t_id = 0; |
| 264 | 264 | |
| ... | ... | @@ -282,8 +282,8 @@ static int build_response_basis_tcp(uint8_t slave, uint8_t function, uint8_t *re |
| 282 | 282 | return PRESET_RESPONSE_LENGTH_TCP; |
| 283 | 283 | } |
| 284 | 284 | |
| 285 | -static int build_response_basis(modbus_param_t *mb_param, uint8_t slave, | |
| 286 | - uint8_t function, uint8_t *response) | |
| 285 | +static int build_response_basis(modbus_param_t *mb_param, int slave, | |
| 286 | + int function, uint8_t *response) | |
| 287 | 287 | { |
| 288 | 288 | if (mb_param->type_com == RTU) |
| 289 | 289 | return build_response_basis_rtu(slave, function, response); |
| ... | ... | @@ -292,12 +292,10 @@ static int build_response_basis(modbus_param_t *mb_param, uint8_t slave, |
| 292 | 292 | } |
| 293 | 293 | |
| 294 | 294 | /* Sets the length of TCP message in the message (query and response) */ |
| 295 | -void set_message_length_tcp(uint8_t *msg, size_t msg_length) | |
| 295 | +void set_message_length_tcp(uint8_t *msg, int msg_length) | |
| 296 | 296 | { |
| 297 | - uint16_t mbap_length; | |
| 298 | - | |
| 299 | - /* Substract MBAP header length */ | |
| 300 | - mbap_length = msg_length - 6; | |
| 297 | + /* Substract the header length to the message length */ | |
| 298 | + int mbap_length = msg_length - 6; | |
| 301 | 299 | |
| 302 | 300 | msg[4] = mbap_length >> 8; |
| 303 | 301 | msg[5] = mbap_length & 0x00FF; |
| ... | ... | @@ -354,7 +352,7 @@ int check_crc16(modbus_param_t *mb_param, |
| 354 | 352 | |
| 355 | 353 | /* Sends a query/response over a serial or a TCP communication */ |
| 356 | 354 | static int modbus_send(modbus_param_t *mb_param, uint8_t *query, |
| 357 | - size_t query_length) | |
| 355 | + int query_length) | |
| 358 | 356 | { |
| 359 | 357 | int ret; |
| 360 | 358 | uint16_t s_crc; |
| ... | ... | @@ -392,39 +390,39 @@ static int modbus_send(modbus_param_t *mb_param, uint8_t *query, |
| 392 | 390 | } |
| 393 | 391 | |
| 394 | 392 | /* Computes the length of the header following the function code */ |
| 395 | -static uint8_t compute_query_length_header(uint8_t function) | |
| 393 | +static uint8_t compute_query_length_header(int function) | |
| 396 | 394 | { |
| 397 | - uint8_t byte; | |
| 395 | + int length; | |
| 398 | 396 | |
| 399 | 397 | if (function <= FC_FORCE_SINGLE_COIL || |
| 400 | 398 | function == FC_PRESET_SINGLE_REGISTER) |
| 401 | 399 | /* Read and single write */ |
| 402 | - byte = 4; | |
| 400 | + length = 4; | |
| 403 | 401 | else if (function == FC_FORCE_MULTIPLE_COILS || |
| 404 | 402 | function == FC_PRESET_MULTIPLE_REGISTERS) |
| 405 | 403 | /* Multiple write */ |
| 406 | - byte = 5; | |
| 404 | + length = 5; | |
| 407 | 405 | else |
| 408 | - byte = 0; | |
| 406 | + length = 0; | |
| 409 | 407 | |
| 410 | - return byte; | |
| 408 | + return length; | |
| 411 | 409 | } |
| 412 | 410 | |
| 413 | 411 | /* Computes the length of the data to write in the query */ |
| 414 | -static uint8_t compute_query_length_data(modbus_param_t *mb_param, uint8_t *msg) | |
| 412 | +static int compute_query_length_data(modbus_param_t *mb_param, uint8_t *msg) | |
| 415 | 413 | { |
| 416 | - uint8_t function = msg[mb_param->header_length + 1]; | |
| 417 | - uint8_t byte; | |
| 414 | + int function = msg[mb_param->header_length + 1]; | |
| 415 | + int length; | |
| 418 | 416 | |
| 419 | 417 | if (function == FC_FORCE_MULTIPLE_COILS || |
| 420 | 418 | function == FC_PRESET_MULTIPLE_REGISTERS) |
| 421 | - byte = msg[mb_param->header_length + 6]; | |
| 419 | + length = msg[mb_param->header_length + 6]; | |
| 422 | 420 | else |
| 423 | - byte = 0; | |
| 421 | + length = 0; | |
| 424 | 422 | |
| 425 | - byte += mb_param->checksum_length; | |
| 423 | + length += mb_param->checksum_length; | |
| 426 | 424 | |
| 427 | - return byte; | |
| 425 | + return length; | |
| 428 | 426 | } |
| 429 | 427 | |
| 430 | 428 | #define WAIT_DATA() \ |
| ... | ... | @@ -458,8 +456,7 @@ static uint8_t compute_query_length_data(modbus_param_t *mb_param, uint8_t *msg) |
| 458 | 456 | - msg_length: number of characters received. */ |
| 459 | 457 | int receive_msg(modbus_param_t *mb_param, |
| 460 | 458 | int msg_length_computed, |
| 461 | - uint8_t *msg, | |
| 462 | - int *msg_length) | |
| 459 | + uint8_t *msg, int *msg_length) | |
| 463 | 460 | { |
| 464 | 461 | int select_ret; |
| 465 | 462 | int read_ret; |
| ... | ... | @@ -678,12 +675,12 @@ static int modbus_check_response(modbus_param_t *mb_param, |
| 678 | 675 | return response_length; |
| 679 | 676 | } |
| 680 | 677 | |
| 681 | -static int response_io_status(uint16_t address, uint16_t count, | |
| 678 | +static int response_io_status(int address, int count, | |
| 682 | 679 | uint8_t *tab_io_status, |
| 683 | 680 | uint8_t *response, int offset) |
| 684 | 681 | { |
| 685 | - uint8_t shift = 0; | |
| 686 | - uint8_t byte = 0; | |
| 682 | + int shift = 0; | |
| 683 | + int byte = 0; | |
| 687 | 684 | int i; |
| 688 | 685 | |
| 689 | 686 | for (i = address; i < address+count; i++) { |
| ... | ... | @@ -1277,7 +1274,7 @@ void modbus_init_rtu(modbus_param_t *mb_param, char *device, |
| 1277 | 1274 | to 1024 because it's not necessary to be root to use this port |
| 1278 | 1275 | number. |
| 1279 | 1276 | */ |
| 1280 | -void modbus_init_tcp(modbus_param_t *mb_param, char *ip, uint16_t port) | |
| 1277 | +void modbus_init_tcp(modbus_param_t *mb_param, char *ip, int port) | |
| 1281 | 1278 | { |
| 1282 | 1279 | memset(mb_param, 0, sizeof(modbus_param_t)); |
| 1283 | 1280 | strncpy(mb_param->ip, ip, sizeof(char)*16); |
| ... | ... | @@ -1777,7 +1774,7 @@ int modbus_init_listen_tcp(modbus_param_t *mb_param) |
| 1777 | 1774 | |
| 1778 | 1775 | /* Sets many inputs/coils from a single byte value (all 8 bits of the |
| 1779 | 1776 | byte value are setted) */ |
| 1780 | -void set_bits_from_byte(uint8_t *dest, uint16_t address, const uint8_t value) | |
| 1777 | +void set_bits_from_byte(uint8_t *dest, int address, const uint8_t value) | |
| 1781 | 1778 | { |
| 1782 | 1779 | int i; |
| 1783 | 1780 | |
| ... | ... | @@ -1788,7 +1785,7 @@ void set_bits_from_byte(uint8_t *dest, uint16_t address, const uint8_t value) |
| 1788 | 1785 | |
| 1789 | 1786 | /* Sets many inputs/coils from a table of bytes (only the bits between |
| 1790 | 1787 | address and address + nb_bits are setted) */ |
| 1791 | -void set_bits_from_bytes(uint8_t *dest, uint16_t address, uint16_t nb_bits, const uint8_t tab_byte[]) | |
| 1788 | +void set_bits_from_bytes(uint8_t *dest, int address, int nb_bits, const uint8_t tab_byte[]) | |
| 1792 | 1789 | { |
| 1793 | 1790 | int i; |
| 1794 | 1791 | int shift = 0; |
| ... | ... | @@ -1803,7 +1800,7 @@ void set_bits_from_bytes(uint8_t *dest, uint16_t address, uint16_t nb_bits, cons |
| 1803 | 1800 | |
| 1804 | 1801 | /* Gets the byte value from many inputs/coils. |
| 1805 | 1802 | To obtain a full byte, set nb_bits to 8. */ |
| 1806 | -uint8_t get_byte_from_bits(const uint8_t *src, uint16_t address, int nb_bits) | |
| 1803 | +uint8_t get_byte_from_bits(const uint8_t *src, int address, int nb_bits) | |
| 1807 | 1804 | { |
| 1808 | 1805 | int i; |
| 1809 | 1806 | uint8_t value = 0; |
| ... | ... | @@ -1819,4 +1816,3 @@ uint8_t get_byte_from_bits(const uint8_t *src, uint16_t address, int nb_bits) |
| 1819 | 1816 | |
| 1820 | 1817 | return value; |
| 1821 | 1818 | } |
| 1822 | - | ... | ... |
modbus/modbus.h
| ... | ... | @@ -229,7 +229,7 @@ void modbus_init_rtu(modbus_param_t *mb_param, char *device, |
| 229 | 229 | to 1024 because it's not necessary to be root to use this port |
| 230 | 230 | number. |
| 231 | 231 | */ |
| 232 | -void modbus_init_tcp(modbus_param_t *mb_param, char *ip_address, uint16_t port); | |
| 232 | +void modbus_init_tcp(modbus_param_t *mb_param, char *ip_address, int port); | |
| 233 | 233 | |
| 234 | 234 | /* By default, the error handling mode used is RECONNECT_ON_ERROR. |
| 235 | 235 | |
| ... | ... | @@ -294,16 +294,15 @@ void manage_query(modbus_param_t *mb_param, uint8_t *query, |
| 294 | 294 | |
| 295 | 295 | /* Sets many inputs/coils from a single byte value (all 8 bits of the |
| 296 | 296 | byte value are setted) */ |
| 297 | -void set_bits_from_byte(uint8_t *dest, uint16_t address, | |
| 298 | - const uint8_t value); | |
| 297 | +void set_bits_from_byte(uint8_t *dest, int address, const uint8_t value); | |
| 299 | 298 | |
| 300 | 299 | /* Sets many inputs/coils from a table of bytes (only the bits between |
| 301 | 300 | address and address + nb_bits are setted) */ |
| 302 | -void set_bits_from_bytes(uint8_t *dest, uint16_t address, uint16_t nb_bits, | |
| 301 | +void set_bits_from_bytes(uint8_t *dest, int address, int nb_bits, | |
| 303 | 302 | const uint8_t *tab_byte); |
| 304 | 303 | |
| 305 | 304 | /* Gets the byte value from many inputs/coils. |
| 306 | 305 | To obtain a full byte, set nb_bits to 8. */ |
| 307 | -uint8_t get_byte_from_bits(const uint8_t *src, uint16_t address, int nb_bits); | |
| 306 | +uint8_t get_byte_from_bits(const uint8_t *src, int address, int nb_bits); | |
| 308 | 307 | |
| 309 | 308 | #endif /* _MODBUS_H_ */ | ... | ... |
tests/unit-test-master.c