Commit 50235c4036d2d8bf5d8ecd200fae9381f2d9bbc6

Authored by Stéphane Raimbault
1 parent ea8b20b1

Use int instead of uint8_t/uint16_t in function arguments.

It's slower to pass non aligned values.
@@ -5,7 +5,6 @@ Features @@ -5,7 +5,6 @@ Features
5 * slave must listen only request sent for him 5 * slave must listen only request sent for him
6 6
7 Cleanups 7 Cleanups
8 -* see the max length of a message and define size var accordingly (uint8_t)  
9 * t_id in param_msqg 8 * t_id in param_msqg
10 9
11 Documentation 10 Documentation
modbus/modbus.c
@@ -191,8 +191,8 @@ static unsigned int compute_response_length(modbus_param_t *mb_param, @@ -191,8 +191,8 @@ static unsigned int compute_response_length(modbus_param_t *mb_param,
191 } 191 }
192 192
193 /* Builds a RTU query header */ 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 uint8_t *query) 196 uint8_t *query)
197 { 197 {
198 query[0] = slave; 198 query[0] = slave;
@@ -206,8 +206,8 @@ static int build_query_basis_rtu(uint8_t slave, uint8_t function, @@ -206,8 +206,8 @@ static int build_query_basis_rtu(uint8_t slave, uint8_t function,
206 } 206 }
207 207
208 /* Builds a TCP query header */ 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 uint8_t *query) 211 uint8_t *query)
212 { 212 {
213 static uint16_t t_id = 0; 213 static uint16_t t_id = 0;
@@ -236,9 +236,9 @@ static int build_query_basis_tcp(uint8_t slave, uint8_t function, @@ -236,9 +236,9 @@ static int build_query_basis_tcp(uint8_t slave, uint8_t function,
236 return PRESET_QUERY_LENGTH_TCP; 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 if (mb_param->type_com == RTU) 243 if (mb_param->type_com == RTU)
244 return build_query_basis_rtu(slave, function, start_addr, 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,7 +249,7 @@ static int build_query_basis(modbus_param_t *mb_param, uint8_t slave,
249 } 249 }
250 250
251 /* Builds a RTU response header */ 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 response[0] = slave; 254 response[0] = slave;
255 response[1] = function; 255 response[1] = function;
@@ -258,7 +258,7 @@ static int build_response_basis_rtu(uint8_t slave, uint8_t function, uint8_t *re @@ -258,7 +258,7 @@ static int build_response_basis_rtu(uint8_t slave, uint8_t function, uint8_t *re
258 } 258 }
259 259
260 /* Builds a TCP response header */ 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 static uint16_t t_id = 0; 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,8 +282,8 @@ static int build_response_basis_tcp(uint8_t slave, uint8_t function, uint8_t *re
282 return PRESET_RESPONSE_LENGTH_TCP; 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 if (mb_param->type_com == RTU) 288 if (mb_param->type_com == RTU)
289 return build_response_basis_rtu(slave, function, response); 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,12 +292,10 @@ static int build_response_basis(modbus_param_t *mb_param, uint8_t slave,
292 } 292 }
293 293
294 /* Sets the length of TCP message in the message (query and response) */ 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 msg[4] = mbap_length >> 8; 300 msg[4] = mbap_length >> 8;
303 msg[5] = mbap_length & 0x00FF; 301 msg[5] = mbap_length & 0x00FF;
@@ -354,7 +352,7 @@ int check_crc16(modbus_param_t *mb_param, @@ -354,7 +352,7 @@ int check_crc16(modbus_param_t *mb_param,
354 352
355 /* Sends a query/response over a serial or a TCP communication */ 353 /* Sends a query/response over a serial or a TCP communication */
356 static int modbus_send(modbus_param_t *mb_param, uint8_t *query, 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 int ret; 357 int ret;
360 uint16_t s_crc; 358 uint16_t s_crc;
@@ -392,39 +390,39 @@ static int modbus_send(modbus_param_t *mb_param, uint8_t *query, @@ -392,39 +390,39 @@ static int modbus_send(modbus_param_t *mb_param, uint8_t *query,
392 } 390 }
393 391
394 /* Computes the length of the header following the function code */ 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 if (function <= FC_FORCE_SINGLE_COIL || 397 if (function <= FC_FORCE_SINGLE_COIL ||
400 function == FC_PRESET_SINGLE_REGISTER) 398 function == FC_PRESET_SINGLE_REGISTER)
401 /* Read and single write */ 399 /* Read and single write */
402 - byte = 4; 400 + length = 4;
403 else if (function == FC_FORCE_MULTIPLE_COILS || 401 else if (function == FC_FORCE_MULTIPLE_COILS ||
404 function == FC_PRESET_MULTIPLE_REGISTERS) 402 function == FC_PRESET_MULTIPLE_REGISTERS)
405 /* Multiple write */ 403 /* Multiple write */
406 - byte = 5; 404 + length = 5;
407 else 405 else
408 - byte = 0; 406 + length = 0;
409 407
410 - return byte; 408 + return length;
411 } 409 }
412 410
413 /* Computes the length of the data to write in the query */ 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 if (function == FC_FORCE_MULTIPLE_COILS || 417 if (function == FC_FORCE_MULTIPLE_COILS ||
420 function == FC_PRESET_MULTIPLE_REGISTERS) 418 function == FC_PRESET_MULTIPLE_REGISTERS)
421 - byte = msg[mb_param->header_length + 6]; 419 + length = msg[mb_param->header_length + 6];
422 else 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 #define WAIT_DATA() \ 428 #define WAIT_DATA() \
@@ -458,8 +456,7 @@ static uint8_t compute_query_length_data(modbus_param_t *mb_param, uint8_t *msg) @@ -458,8 +456,7 @@ static uint8_t compute_query_length_data(modbus_param_t *mb_param, uint8_t *msg)
458 - msg_length: number of characters received. */ 456 - msg_length: number of characters received. */
459 int receive_msg(modbus_param_t *mb_param, 457 int receive_msg(modbus_param_t *mb_param,
460 int msg_length_computed, 458 int msg_length_computed,
461 - uint8_t *msg,  
462 - int *msg_length) 459 + uint8_t *msg, int *msg_length)
463 { 460 {
464 int select_ret; 461 int select_ret;
465 int read_ret; 462 int read_ret;
@@ -678,12 +675,12 @@ static int modbus_check_response(modbus_param_t *mb_param, @@ -678,12 +675,12 @@ static int modbus_check_response(modbus_param_t *mb_param,
678 return response_length; 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 uint8_t *tab_io_status, 679 uint8_t *tab_io_status,
683 uint8_t *response, int offset) 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 int i; 684 int i;
688 685
689 for (i = address; i < address+count; i++) { 686 for (i = address; i < address+count; i++) {
@@ -1277,7 +1274,7 @@ void modbus_init_rtu(modbus_param_t *mb_param, char *device, @@ -1277,7 +1274,7 @@ void modbus_init_rtu(modbus_param_t *mb_param, char *device,
1277 to 1024 because it's not necessary to be root to use this port 1274 to 1024 because it's not necessary to be root to use this port
1278 number. 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 memset(mb_param, 0, sizeof(modbus_param_t)); 1279 memset(mb_param, 0, sizeof(modbus_param_t));
1283 strncpy(mb_param->ip, ip, sizeof(char)*16); 1280 strncpy(mb_param->ip, ip, sizeof(char)*16);
@@ -1777,7 +1774,7 @@ int modbus_init_listen_tcp(modbus_param_t *mb_param) @@ -1777,7 +1774,7 @@ int modbus_init_listen_tcp(modbus_param_t *mb_param)
1777 1774
1778 /* Sets many inputs/coils from a single byte value (all 8 bits of the 1775 /* Sets many inputs/coils from a single byte value (all 8 bits of the
1779 byte value are setted) */ 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 int i; 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,7 +1785,7 @@ void set_bits_from_byte(uint8_t *dest, uint16_t address, const uint8_t value)
1788 1785
1789 /* Sets many inputs/coils from a table of bytes (only the bits between 1786 /* Sets many inputs/coils from a table of bytes (only the bits between
1790 address and address + nb_bits are setted) */ 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 int i; 1790 int i;
1794 int shift = 0; 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,7 +1800,7 @@ void set_bits_from_bytes(uint8_t *dest, uint16_t address, uint16_t nb_bits, cons
1803 1800
1804 /* Gets the byte value from many inputs/coils. 1801 /* Gets the byte value from many inputs/coils.
1805 To obtain a full byte, set nb_bits to 8. */ 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 int i; 1805 int i;
1809 uint8_t value = 0; 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,4 +1816,3 @@ uint8_t get_byte_from_bits(const uint8_t *src, uint16_t address, int nb_bits)
1819 1816
1820 return value; 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,7 +229,7 @@ void modbus_init_rtu(modbus_param_t *mb_param, char *device,
229 to 1024 because it's not necessary to be root to use this port 229 to 1024 because it's not necessary to be root to use this port
230 number. 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 /* By default, the error handling mode used is RECONNECT_ON_ERROR. 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,16 +294,15 @@ void manage_query(modbus_param_t *mb_param, uint8_t *query,
294 294
295 /* Sets many inputs/coils from a single byte value (all 8 bits of the 295 /* Sets many inputs/coils from a single byte value (all 8 bits of the
296 byte value are setted) */ 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 /* Sets many inputs/coils from a table of bytes (only the bits between 299 /* Sets many inputs/coils from a table of bytes (only the bits between
301 address and address + nb_bits are setted) */ 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 const uint8_t *tab_byte); 302 const uint8_t *tab_byte);
304 303
305 /* Gets the byte value from many inputs/coils. 304 /* Gets the byte value from many inputs/coils.
306 To obtain a full byte, set nb_bits to 8. */ 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 #endif /* _MODBUS_H_ */ 308 #endif /* _MODBUS_H_ */
tests/unit-test-master.c
@@ -36,8 +36,8 @@ int main(void) @@ -36,8 +36,8 @@ int main(void)
36 modbus_param_t mb_param; 36 modbus_param_t mb_param;
37 int i; 37 int i;
38 uint8_t value; 38 uint8_t value;
39 - uint16_t address;  
40 - uint16_t nb_points; 39 + int address;
  40 + int nb_points;
41 int ret; 41 int ret;
42 42
43 /* RTU parity : none, even, odd */ 43 /* RTU parity : none, even, odd */