free_auxdata() added to mbus_handle interface for freeing context specific data
This commit is contained in:
parent
645e5eb482
commit
34255c7237
@ -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)
|
||||
{
|
||||
|
@ -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
|
||||
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
//
|
||||
//------------------------------------------------------------------------------
|
||||
|
@ -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 */
|
||||
|
||||
|
||||
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
//
|
||||
//------------------------------------------------------------------------------
|
||||
|
@ -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 */
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user