diff --git a/mbus/mbus-protocol-aux.c b/mbus/mbus-protocol-aux.c index c5e9fd6..e2e759b 100755 --- a/mbus/mbus-protocol-aux.c +++ b/mbus/mbus-protocol-aux.c @@ -1336,6 +1336,7 @@ mbus_context_serial(const char *device) handle->close = mbus_serial_disconnect; handle->recv = mbus_serial_recv_frame; handle->send = mbus_serial_send_frame; + handle->free_auxdata = mbus_tcp_data_free; if ((serial_data->device = strdup(device)) == NULL) { @@ -1376,6 +1377,7 @@ mbus_context_tcp(const char *host, int port) handle->close = mbus_tcp_disconnect; handle->recv = mbus_tcp_recv_frame; handle->send = mbus_tcp_send_frame; + handle->free_auxdata = mbus_tcp_data_free; tcp_data->port = port; if ((tcp_data->host = strdup(host)) == NULL) @@ -1390,6 +1392,16 @@ mbus_context_tcp(const char *host, int port) return handle; } +void +mbus_context_free(mbus_handle * handle) +{ + if (handle) + { + handle->free_auxdata(handle); + free(handle); + } +} + int modbus_connect(mbus_handle * handle) { diff --git a/mbus/mbus-protocol-aux.h b/mbus/mbus-protocol-aux.h index 62f4d96..7d45229 100755 --- a/mbus/mbus-protocol-aux.h +++ b/mbus/mbus-protocol-aux.h @@ -77,6 +77,7 @@ struct _mbus_handle { int (*close) (struct _mbus_handle *handle); int (*send) (struct _mbus_handle *handle, mbus_frame *frame); int (*recv) (struct _mbus_handle *handle, mbus_frame *frame); + void (*free_auxdata) (struct _mbus_handle *handle); void *auxdata; }; @@ -159,6 +160,14 @@ mbus_handle * mbus_context_serial(const char *device); mbus_handle * mbus_context_tcp(const char *host, int port); /** + * Deallocate memory used by M-Bus context. + * + * @param handle Initialized handle + * + */ +void mbus_context_free(mbus_handle * handle); + +/** * Connect to serial bus or TCP gateway depending on context. * * @param handle Initialized handle diff --git a/mbus/mbus-serial.c b/mbus/mbus-serial.c index 3ecd314..2b9ef3d 100755 --- a/mbus/mbus-serial.c +++ b/mbus/mbus-serial.c @@ -168,6 +168,19 @@ mbus_serial_disconnect(mbus_handle *handle) return 0; } +void +mbus_serial_data_free(mbus_handle *handle) +{ + mbus_serial_handle *serial_data; + + if (handle) + { + serial_data = (mbus_serial_handle *) handle->auxdata; + free(serial_data->device); + free(serial_data); + } +} + //------------------------------------------------------------------------------ // //------------------------------------------------------------------------------ diff --git a/mbus/mbus-serial.h b/mbus/mbus-serial.h index 25174e4..97bffae 100755 --- a/mbus/mbus-serial.h +++ b/mbus/mbus-serial.h @@ -33,6 +33,7 @@ int mbus_serial_disconnect(mbus_handle *handle); int mbus_serial_send_frame(mbus_handle *handle, mbus_frame *frame); int mbus_serial_recv_frame(mbus_handle *handle, mbus_frame *frame); int mbus_serial_set_baudrate(mbus_handle *handle, int baudrate); +void mbus_serial_data_free(mbus_handle *handle); #endif /* MBUS_SERIAL_H */ diff --git a/mbus/mbus-tcp.c b/mbus/mbus-tcp.c index 67e4c6f..7bf95b8 100755 --- a/mbus/mbus-tcp.c +++ b/mbus/mbus-tcp.c @@ -89,6 +89,19 @@ mbus_tcp_connect(mbus_handle *handle) return 1; } +void +mbus_tcp_data_free(mbus_handle *handle) +{ + mbus_tcp_handle *tcp_data; + + if (handle) + { + tcp_data = (mbus_tcp_handle *) handle->auxdata; + free(tcp_data->host); + free(tcp_data); + } +} + //------------------------------------------------------------------------------ // //------------------------------------------------------------------------------ diff --git a/mbus/mbus-tcp.h b/mbus/mbus-tcp.h index a6206a0..56b9b8d 100755 --- a/mbus/mbus-tcp.h +++ b/mbus/mbus-tcp.h @@ -31,6 +31,7 @@ int mbus_tcp_connect(mbus_handle *handle); int mbus_tcp_disconnect(mbus_handle *handle); int mbus_tcp_send_frame(mbus_handle *handle, mbus_frame *frame); int mbus_tcp_recv_frame(mbus_handle *handle, mbus_frame *frame); +void mbus_tcp_data_free(mbus_handle *handle); #endif /* MBUS_TCP_H */