free_auxdata() added to mbus_handle interface for freeing context specific data

This commit is contained in:
jakubovsky 2012-07-04 19:08:51 +02:00
parent 645e5eb482
commit 34255c7237
6 changed files with 49 additions and 0 deletions

View File

@ -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)
{

View File

@ -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

View File

@ -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);
}
}
//------------------------------------------------------------------------------
//
//------------------------------------------------------------------------------

View File

@ -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 */

View File

@ -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);
}
}
//------------------------------------------------------------------------------
//
//------------------------------------------------------------------------------

View File

@ -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 */