Commit 541804b30e03919a2cfadef7ab29546acc8f48dc

Authored by Stéphane Raimbault
1 parent 37ed6f90

Dynamic memory allocation of device name (closes #11)

src/modbus-private.h
... ... @@ -111,6 +111,7 @@ typedef struct _modbus_backend {
111 111 void (*close) (modbus_t *ctx);
112 112 int (*flush) (modbus_t *ctx);
113 113 int (*select) (modbus_t *ctx, fd_set *rset, struct timeval *tv, int msg_length);
  114 + void (*free) (modbus_t *ctx);
114 115 } modbus_backend_t;
115 116  
116 117 struct _modbus {
... ...
src/modbus-rtu-private.h
... ... @@ -59,16 +59,8 @@ struct win32_ser {
59 59 #endif /* _WIN32 */
60 60  
61 61 typedef struct _modbus_rtu {
62   - /* Device: "/dev/ttyS0", "/dev/ttyUSB0" or "/dev/tty.USA19*" on Mac OS X for
63   - KeySpan USB<->Serial adapters this string had to be made bigger on OS X
64   - as the directory+file name was bigger than 19 bytes. Making it 67 bytes
65   - for now, but OS X does support 256 byte file names. May become a problem
66   - in the future. */
67   -#if defined(__APPLE_CC__)
68   - char device[64];
69   -#else
70   - char device[16];
71   -#endif
  62 + /* Device: "/dev/ttyS0", "/dev/ttyUSB0" or "/dev/tty.USA19*" on Mac OS X. */
  63 + char *device;
72 64 /* Bauds: 9600, 19200, 57600, 115200, etc */
73 65 int baud;
74 66 /* Data bit */
... ...
src/modbus-rtu.c
... ... @@ -987,6 +987,12 @@ int _modbus_rtu_select(modbus_t *ctx, fd_set *rset,
987 987 return s_rc;
988 988 }
989 989  
  990 +void _modbus_rtu_free(modbus_t *ctx) {
  991 + free(((modbus_rtu_t*)ctx->backend_data)->device);
  992 + free(ctx->backend_data);
  993 + free(ctx);
  994 +}
  995 +
990 996 const modbus_backend_t _modbus_rtu_backend = {
991 997 _MODBUS_BACKEND_TYPE_RTU,
992 998 _MODBUS_RTU_HEADER_LENGTH,
... ... @@ -1005,7 +1011,8 @@ const modbus_backend_t _modbus_rtu_backend = {
1005 1011 _modbus_rtu_connect,
1006 1012 _modbus_rtu_close,
1007 1013 _modbus_rtu_flush,
1008   - _modbus_rtu_select
  1014 + _modbus_rtu_select,
  1015 + _modbus_rtu_free
1009 1016 };
1010 1017  
1011 1018 modbus_t* modbus_new_rtu(const char *device,
... ... @@ -1014,8 +1021,7 @@ modbus_t* modbus_new_rtu(const char *device,
1014 1021 {
1015 1022 modbus_t *ctx;
1016 1023 modbus_rtu_t *ctx_rtu;
1017   - size_t dest_size;
1018   - size_t ret_size;
  1024 + size_t device_size;
1019 1025  
1020 1026 ctx = (modbus_t *) malloc(sizeof(modbus_t));
1021 1027 _modbus_init_common(ctx);
... ... @@ -1024,21 +1030,16 @@ modbus_t* modbus_new_rtu(const char *device,
1024 1030 ctx->backend_data = (modbus_rtu_t *) malloc(sizeof(modbus_rtu_t));
1025 1031 ctx_rtu = (modbus_rtu_t *)ctx->backend_data;
1026 1032  
1027   - dest_size = sizeof(ctx_rtu->device);
1028   - ret_size = strlcpy(ctx_rtu->device, device, dest_size);
1029   - if (ret_size == 0) {
  1033 + device_size = sizeof(device);
  1034 + if (device_size == 0) {
1030 1035 fprintf(stderr, "The device string is empty\n");
1031 1036 modbus_free(ctx);
1032 1037 errno = EINVAL;
1033 1038 return NULL;
1034 1039 }
1035 1040  
1036   - if (ret_size >= dest_size) {
1037   - fprintf(stderr, "The device string has been truncated\n");
1038   - modbus_free(ctx);
1039   - errno = EINVAL;
1040   - return NULL;
1041   - }
  1041 + ctx_rtu->device = (char *) malloc(device_size);
  1042 + strcpy(ctx_rtu->device, device);
1042 1043  
1043 1044 ctx_rtu->baud = baud;
1044 1045 if (parity == 'N' || parity == 'E' || parity == 'O') {
... ...
src/modbus-tcp.c
... ... @@ -654,6 +654,11 @@ int _modbus_tcp_select(modbus_t *ctx, fd_set *rset, struct timeval *tv, int leng
654 654 return s_rc;
655 655 }
656 656  
  657 +void _modbus_tcp_free(modbus_t *ctx) {
  658 + free(ctx->backend_data);
  659 + free(ctx);
  660 +}
  661 +
657 662 const modbus_backend_t _modbus_tcp_backend = {
658 663 _MODBUS_BACKEND_TYPE_TCP,
659 664 _MODBUS_TCP_HEADER_LENGTH,
... ... @@ -672,7 +677,8 @@ const modbus_backend_t _modbus_tcp_backend = {
672 677 _modbus_tcp_connect,
673 678 _modbus_tcp_close,
674 679 _modbus_tcp_flush,
675   - _modbus_tcp_select
  680 + _modbus_tcp_select,
  681 + _modbus_tcp_free
676 682 };
677 683  
678 684  
... ... @@ -694,7 +700,8 @@ const modbus_backend_t _modbus_tcp_pi_backend = {
694 700 _modbus_tcp_pi_connect,
695 701 _modbus_tcp_close,
696 702 _modbus_tcp_flush,
697   - _modbus_tcp_select
  703 + _modbus_tcp_select,
  704 + _modbus_tcp_free
698 705 };
699 706  
700 707 modbus_t* modbus_new_tcp(const char *ip, int port)
... ...
src/modbus.c
... ... @@ -1462,8 +1462,7 @@ void modbus_free(modbus_t *ctx)
1462 1462 if (ctx == NULL)
1463 1463 return;
1464 1464  
1465   - free(ctx->backend_data);
1466   - free(ctx);
  1465 + ctx->backend->free(ctx);
1467 1466 }
1468 1467  
1469 1468 void modbus_set_debug(modbus_t *ctx, int boolean)
... ...